1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// SPDX-License-Identifier: GPL-2.0-only
/* * This file is part of UBIFS.
*
* Copyright (C) 2006-2008 Nokia Corporation.
* Copyright (C) 2006, 2007 University of Szeged, Hungary
*
* Authors: Artem Bityutskiy (Битюцкий Артём)
* Adrian Hunter
* Zoltan Sogor
*/
/*
* This file implements directory operations.
*
* All FS operations in this file allocate budget before writing anything to the
* media. If they fail to allocate it, the error is returned. The only
* exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
* if they unable to allocate the budget, because deletion %-ENOSPC failure is
* not what users are usually ready to get. UBIFS budgeting subsystem has some
* space reserved for these purposes.
*
* All operations in this file write all inodes which they change straight
* away, instead of marking them dirty. For example, 'ubifs_link()' changes
* @i_size of the parent inode and writes the parent inode together with the
* target inode. This was done to simplify file-system recovery which would
* otherwise be very difficult to do. The only exception is rename which marks
* the re-named inode dirty (because its @i_ctime is updated) but does not
* write it, but just marks it as dirty.
*/
#include <sys/stat.h>
#include "linux_err.h"
#include "bitops.h"
#include "kmem.h"
#include "ubifs.h"
#include "defs.h"
#include "debug.h"
#include "key.h"
#include "misc.h"
/**
* inherit_flags - inherit flags of the parent inode.
* @c: UBIFS file-system description object
* @dir: parent inode
* @mode: new inode mode flags
*
* This is a helper function for 'ubifs_new_inode()' which inherits flag of the
* parent directory inode @dir. UBIFS inodes inherit the following flags:
* o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
* sub-directory basis;
* o %UBIFS_SYNC_FL - useful for the same reasons;
* o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
*
* This function returns the inherited flags.
*/
static int inherit_flags(struct ubifs_info *c, const struct inode *dir,
unsigned int mode)
{
int flags;
const struct ubifs_inode *ui = ubifs_inode(dir);
ubifs_assert(c, S_ISDIR(dir->mode));
flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
if (!S_ISDIR(mode))
/* The "DIRSYNC" flag only applies to directories */
flags &= ~UBIFS_DIRSYNC_FL;
return flags;
}
/**
* ubifs_new_inode - allocate new UBIFS inode object.
* @c: UBIFS file-system description object
* @dir: parent inode
* @mode: inode mode flags
*
* This function finds an unused inode number, allocates new ubifs inode and
* initializes it. Returns new ubifs inode in case of success and an error code
* in case of failure.
*/
static struct ubifs_inode *ubifs_new_inode(struct ubifs_info *c,
const struct inode *dir,
unsigned int mode)
{
int err;
time_t now = time(NULL);
struct ubifs_inode *ui;
struct inode *inode;
ui = kzalloc(sizeof(struct ubifs_inode), GFP_KERNEL);
if (!ui)
return ERR_PTR(-ENOMEM);
inode = &ui->vfs_inode;
inode->atime_sec = inode->ctime_sec = inode->mtime_sec = now;
inode->nlink = 1;
inode->mode = mode;
if (dir) {
/* Create non root dir. */
inode->uid = dir->uid;
inode->gid = dir->gid;
if ((dir->mode & S_ISGID) && S_ISDIR(mode))
inode->mode |= S_ISGID;
ui->flags = inherit_flags(c, dir, mode);
}
if (S_ISDIR(mode))
ui->ui_size = UBIFS_INO_NODE_SZ;
if (S_ISREG(mode))
ui->compr_type = c->default_compr;
else
ui->compr_type = UBIFS_COMPR_NONE;
if (dir) {
spin_lock(&c->cnt_lock);
/* Inode number overflow is currently not supported */
if (c->highest_inum >= INUM_WARN_WATERMARK) {
if (c->highest_inum >= INUM_WATERMARK) {
spin_unlock(&c->cnt_lock);
ubifs_err(c, "out of inode numbers");
err = -EINVAL;
goto out;
}
ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
(unsigned long)c->highest_inum, INUM_WATERMARK);
}
inode->inum = ++c->highest_inum;
} else {
/* Create root dir. */
inode->inum = UBIFS_ROOT_INO;
}
/*
* The creation sequence number remains with this inode for its
* lifetime. All nodes for this inode have a greater sequence number,
* and so it is possible to distinguish obsolete nodes belonging to a
* previous incarnation of the same inode number - for example, for the
* purpose of rebuilding the index.
*/
ui->creat_sqnum = ++c->max_sqnum;
spin_unlock(&c->cnt_lock);
return ui;
out:
kfree(ui);
return ERR_PTR(err);
}
/**
* ubifs_lookup_by_inum - look up the UBIFS inode according to inode number.
* @c: UBIFS file-system description object
* @inum: inode number
*
* This function looks up the UBIFS inode according to a given inode number.
* Returns zero in case of success and an error code in case of failure.
*/
struct ubifs_inode *ubifs_lookup_by_inum(struct ubifs_info *c, ino_t inum)
{
int err;
union ubifs_key key;
struct inode *inode;
struct ubifs_inode *ui;
struct ubifs_ino_node *ino = NULL;
ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
if (!ino)
return ERR_PTR(-ENOMEM);
ui = kzalloc(sizeof(struct ubifs_inode), GFP_KERNEL);
if (!ui) {
err = -ENOMEM;
goto out;
}
inode = &ui->vfs_inode;
ino_key_init(c, &key, inum);
err = ubifs_tnc_lookup(c, &key, ino);
if (err) {
kfree(ui);
ubifs_assert(c, !get_failure_reason_callback(c));
goto out;
}
inode = &ui->vfs_inode;
inode->inum = inum;
inode->uid = le32_to_cpu(ino->uid);
inode->gid = le32_to_cpu(ino->gid);
inode->mode = le32_to_cpu(ino->mode);
inode->nlink = le32_to_cpu(ino->nlink);
inode->atime_sec = le64_to_cpu(ino->atime_sec);
inode->ctime_sec = le64_to_cpu(ino->ctime_sec);
inode->mtime_sec = le64_to_cpu(ino->mtime_sec);
inode->atime_nsec = le32_to_cpu(ino->atime_nsec);
inode->ctime_nsec = le32_to_cpu(ino->ctime_nsec);
inode->mtime_nsec = le32_to_cpu(ino->mtime_nsec);
ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
ui->xattr_size = le32_to_cpu(ino->xattr_size);
ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
ui->xattr_names = le32_to_cpu(ino->xattr_names);
ui->compr_type = le16_to_cpu(ino->compr_type);
ui->ui_size = le64_to_cpu(ino->size);
ui->flags = le32_to_cpu(ino->flags);
ui->data_len = le32_to_cpu(ino->data_len);
out:
kfree(ino);
return err ? ERR_PTR(err) : ui;
}
struct ubifs_inode *ubifs_lookup(struct ubifs_info *c,
struct ubifs_inode *dir_ui,
const struct fscrypt_name *nm)
{
int err;
ino_t inum;
union ubifs_key key;
struct ubifs_dent_node *dent;
if (fname_len(nm) > UBIFS_MAX_NLEN)
return ERR_PTR(-ENAMETOOLONG);
dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
if (!dent)
return ERR_PTR(-ENOMEM);
dent_key_init(c, &key, dir_ui->vfs_inode.inum, nm);
err = ubifs_tnc_lookup_nm(c, &key, dent, nm);
if (err) {
kfree(dent);
ubifs_assert(c, !get_failure_reason_callback(c));
return ERR_PTR(err);
}
inum = le64_to_cpu(dent->inum);
kfree(dent);
return ubifs_lookup_by_inum(c, inum);
}
int ubifs_mkdir(struct ubifs_info *c, struct ubifs_inode *dir_ui,
const struct fscrypt_name *nm, unsigned int mode)
{
struct ubifs_inode *ui;
struct inode *inode, *dir = &dir_ui->vfs_inode;
int err, sz_change;
struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
.dirtied_ino = 1};
/*
* Budget request settings: new inode, new direntry and changing parent
* directory inode.
*/
dbg_gen("dent '%s', mode %#hx in dir ino %lu",
fname_name(nm), mode, dir->inum);
/* New dir is not allowed to be created under an encrypted directory. */
ubifs_assert(c, !(dir_ui->flags & UBIFS_CRYPT_FL));
err = ubifs_budget_space(c, &req);
if (err)
return err;
sz_change = CALC_DENT_SIZE(fname_len(nm));
ui = ubifs_new_inode(c, dir, S_IFDIR | mode);
if (IS_ERR(ui)) {
err = PTR_ERR(ui);
goto out_budg;
}
inode = &ui->vfs_inode;
inode->nlink++;
dir->nlink++;
dir_ui->ui_size += sz_change;
dir->ctime_sec = dir->mtime_sec = inode->ctime_sec;
err = ubifs_jnl_update_file(c, dir_ui, nm, ui);
if (err) {
ubifs_err(c, "cannot create directory, error %d", err);
goto out_cancel;
}
kfree(ui);
ubifs_release_budget(c, &req);
return 0;
out_cancel:
dir_ui->ui_size -= sz_change;
dir->nlink--;
kfree(ui);
out_budg:
ubifs_release_budget(c, &req);
return err;
}
/**
* ubifs_link_recovery - link a disconnected file into the target directory.
* @c: UBIFS file-system description object
* @dir_ui: target directory
* @ui: the UBIFS inode of disconnected file
* @nm: directory entry name
*
* This function links the inode of disconnected file to a directory entry name
* under the target directory. Returns zero in case of success and an error code
* in case of failure.
*/
int ubifs_link_recovery(struct ubifs_info *c, struct ubifs_inode *dir_ui,
struct ubifs_inode *ui, const struct fscrypt_name *nm)
{
struct inode *inode = &ui->vfs_inode, *dir = &dir_ui->vfs_inode;
int err, sz_change;
struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
.dirtied_ino_d = ALIGN(ui->data_len, 8) };
time_t now = time(NULL);
/*
* Budget request settings: new direntry, changing the target inode,
* changing the parent inode.
*/
dbg_gen("dent '%s' to ino %lu (nlink %d) in dir ino %lu",
fname_name(nm), inode->inum, inode->nlink, dir->inum);
/* New dir is not allowed to be created under an encrypted directory. */
ubifs_assert(c, !(dir_ui->flags & UBIFS_CRYPT_FL));
sz_change = CALC_DENT_SIZE(fname_len(nm));
err = ubifs_budget_space(c, &req);
if (err)
return err;
inode->ctime_sec = now;
dir_ui->ui_size += sz_change;
dir->ctime_sec = dir->mtime_sec = now;
err = ubifs_jnl_update_file(c, dir_ui, nm, ui);
if (err)
goto out_cancel;
ubifs_release_budget(c, &req);
return 0;
out_cancel:
dir_ui->ui_size -= sz_change;
ubifs_release_budget(c, &req);
return err;
}
/**
* ubifs_create_root - create the root inode.
* @c: UBIFS file-system description object
*
* This function creates a new inode for the root directory. Returns zero in
* case of success and an error code in case of failure.
*/
int ubifs_create_root(struct ubifs_info *c)
{
int err;
struct inode *inode;
struct ubifs_budget_req req = { .new_ino = 1 };
struct ubifs_inode *ui;
/* Budget request settings: new inode. */
dbg_gen("create root dir");
err = ubifs_budget_space(c, &req);
if (err)
return err;
ui = ubifs_new_inode(c, NULL, S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
if (IS_ERR(ui)) {
err = PTR_ERR(ui);
goto out_budg;
}
inode = &ui->vfs_inode;
inode->nlink = 2;
ui->ui_size = UBIFS_INO_NODE_SZ;
ui->flags = UBIFS_COMPR_FL;
err = ubifs_jnl_update_file(c, NULL, NULL, ui);
if (err)
goto out_ui;
kfree(ui);
ubifs_release_budget(c, &req);
return 0;
out_ui:
kfree(ui);
out_budg:
ubifs_release_budget(c, &req);
ubifs_err(c, "cannot create root dir, error %d", err);
return err;
}
|