]>
Commit | Line | Data |
---|---|---|
1e51764a AB |
1 | /* * This file is part of UBIFS. |
2 | * | |
3 | * Copyright (C) 2006-2008 Nokia Corporation. | |
4 | * Copyright (C) 2006, 2007 University of Szeged, Hungary | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published by | |
8 | * the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * Authors: Artem Bityutskiy (Битюцкий Артём) | |
20 | * Adrian Hunter | |
21 | * Zoltan Sogor | |
22 | */ | |
23 | ||
24 | /* | |
25 | * This file implements directory operations. | |
26 | * | |
27 | * All FS operations in this file allocate budget before writing anything to the | |
28 | * media. If they fail to allocate it, the error is returned. The only | |
29 | * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even | |
30 | * if they unable to allocate the budget, because deletion %-ENOSPC failure is | |
31 | * not what users are usually ready to get. UBIFS budgeting subsystem has some | |
32 | * space reserved for these purposes. | |
33 | * | |
34 | * All operations in this file write all inodes which they change straight | |
35 | * away, instead of marking them dirty. For example, 'ubifs_link()' changes | |
36 | * @i_size of the parent inode and writes the parent inode together with the | |
37 | * target inode. This was done to simplify file-system recovery which would | |
38 | * otherwise be very difficult to do. The only exception is rename which marks | |
39 | * the re-named inode dirty (because its @i_ctime is updated) but does not | |
40 | * write it, but just marks it as dirty. | |
41 | */ | |
42 | ||
43 | #include "ubifs.h" | |
44 | ||
45 | /** | |
46 | * inherit_flags - inherit flags of the parent inode. | |
47 | * @dir: parent inode | |
48 | * @mode: new inode mode flags | |
49 | * | |
50 | * This is a helper function for 'ubifs_new_inode()' which inherits flag of the | |
51 | * parent directory inode @dir. UBIFS inodes inherit the following flags: | |
52 | * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on | |
53 | * sub-directory basis; | |
54 | * o %UBIFS_SYNC_FL - useful for the same reasons; | |
55 | * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories. | |
56 | * | |
57 | * This function returns the inherited flags. | |
58 | */ | |
ad44be5c | 59 | static int inherit_flags(const struct inode *dir, umode_t mode) |
1e51764a AB |
60 | { |
61 | int flags; | |
62 | const struct ubifs_inode *ui = ubifs_inode(dir); | |
63 | ||
64 | if (!S_ISDIR(dir->i_mode)) | |
65 | /* | |
66 | * The parent is not a directory, which means that an extended | |
67 | * attribute inode is being created. No flags. | |
68 | */ | |
69 | return 0; | |
70 | ||
71 | flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL); | |
72 | if (!S_ISDIR(mode)) | |
73 | /* The "DIRSYNC" flag only applies to directories */ | |
74 | flags &= ~UBIFS_DIRSYNC_FL; | |
75 | return flags; | |
76 | } | |
77 | ||
78 | /** | |
79 | * ubifs_new_inode - allocate new UBIFS inode object. | |
80 | * @c: UBIFS file-system description object | |
81 | * @dir: parent directory inode | |
82 | * @mode: inode mode flags | |
83 | * | |
84 | * This function finds an unused inode number, allocates new inode and | |
85 | * initializes it. Returns new inode in case of success and an error code in | |
86 | * case of failure. | |
87 | */ | |
d475a507 | 88 | struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir, |
ad44be5c | 89 | umode_t mode) |
1e51764a | 90 | { |
d475a507 | 91 | int err; |
1e51764a AB |
92 | struct inode *inode; |
93 | struct ubifs_inode *ui; | |
d475a507 RW |
94 | bool encrypted = false; |
95 | ||
96 | if (ubifs_crypt_is_encrypted(dir)) { | |
97 | err = fscrypt_get_encryption_info(dir); | |
98 | if (err) { | |
99 | ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err); | |
100 | return ERR_PTR(err); | |
101 | } | |
102 | ||
103 | if (!fscrypt_has_encryption_key(dir)) | |
104 | return ERR_PTR(-EPERM); | |
105 | ||
106 | encrypted = true; | |
107 | } | |
1e51764a AB |
108 | |
109 | inode = new_inode(c->vfs_sb); | |
110 | ui = ubifs_inode(inode); | |
111 | if (!inode) | |
112 | return ERR_PTR(-ENOMEM); | |
113 | ||
114 | /* | |
115 | * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and | |
116 | * marking them dirty in file write path (see 'file_update_time()'). | |
117 | * UBIFS has to fully control "clean <-> dirty" transitions of inodes | |
118 | * to make budgeting work. | |
119 | */ | |
12e776a0 | 120 | inode->i_flags |= S_NOCMTIME; |
1e51764a | 121 | |
abf5d08a | 122 | inode_init_owner(inode, dir, mode); |
1e51764a AB |
123 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
124 | ubifs_current_time(inode); | |
125 | inode->i_mapping->nrpages = 0; | |
1e51764a AB |
126 | |
127 | switch (mode & S_IFMT) { | |
128 | case S_IFREG: | |
129 | inode->i_mapping->a_ops = &ubifs_file_address_operations; | |
130 | inode->i_op = &ubifs_file_inode_operations; | |
131 | inode->i_fop = &ubifs_file_operations; | |
132 | break; | |
133 | case S_IFDIR: | |
134 | inode->i_op = &ubifs_dir_inode_operations; | |
135 | inode->i_fop = &ubifs_dir_operations; | |
136 | inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ; | |
137 | break; | |
138 | case S_IFLNK: | |
139 | inode->i_op = &ubifs_symlink_inode_operations; | |
140 | break; | |
141 | case S_IFSOCK: | |
142 | case S_IFIFO: | |
143 | case S_IFBLK: | |
144 | case S_IFCHR: | |
145 | inode->i_op = &ubifs_file_inode_operations; | |
146 | break; | |
147 | default: | |
148 | BUG(); | |
149 | } | |
150 | ||
151 | ui->flags = inherit_flags(dir, mode); | |
152 | ubifs_set_inode_flags(inode); | |
153 | if (S_ISREG(mode)) | |
154 | ui->compr_type = c->default_compr; | |
155 | else | |
156 | ui->compr_type = UBIFS_COMPR_NONE; | |
157 | ui->synced_i_size = 0; | |
158 | ||
159 | spin_lock(&c->cnt_lock); | |
160 | /* Inode number overflow is currently not supported */ | |
161 | if (c->highest_inum >= INUM_WARN_WATERMARK) { | |
162 | if (c->highest_inum >= INUM_WATERMARK) { | |
163 | spin_unlock(&c->cnt_lock); | |
235c362b | 164 | ubifs_err(c, "out of inode numbers"); |
1e51764a AB |
165 | make_bad_inode(inode); |
166 | iput(inode); | |
167 | return ERR_PTR(-EINVAL); | |
168 | } | |
1a7e985d | 169 | ubifs_warn(c, "running out of inode numbers (current %lu, max %u)", |
e84461ad | 170 | (unsigned long)c->highest_inum, INUM_WATERMARK); |
1e51764a AB |
171 | } |
172 | ||
173 | inode->i_ino = ++c->highest_inum; | |
1e51764a AB |
174 | /* |
175 | * The creation sequence number remains with this inode for its | |
176 | * lifetime. All nodes for this inode have a greater sequence number, | |
177 | * and so it is possible to distinguish obsolete nodes belonging to a | |
178 | * previous incarnation of the same inode number - for example, for the | |
179 | * purpose of rebuilding the index. | |
180 | */ | |
181 | ui->creat_sqnum = ++c->max_sqnum; | |
182 | spin_unlock(&c->cnt_lock); | |
d475a507 RW |
183 | |
184 | if (encrypted) { | |
185 | err = fscrypt_inherit_context(dir, inode, &encrypted, true); | |
186 | if (err) { | |
187 | ubifs_err(c, "fscrypt_inherit_context failed: %i", err); | |
188 | make_bad_inode(inode); | |
189 | iput(inode); | |
190 | return ERR_PTR(err); | |
191 | } | |
192 | } | |
193 | ||
1e51764a AB |
194 | return inode; |
195 | } | |
196 | ||
bb2615d4 AB |
197 | static int dbg_check_name(const struct ubifs_info *c, |
198 | const struct ubifs_dent_node *dent, | |
199 | const struct qstr *nm) | |
1e51764a | 200 | { |
2b1844a8 | 201 | if (!dbg_is_chk_gen(c)) |
1e51764a AB |
202 | return 0; |
203 | if (le16_to_cpu(dent->nlen) != nm->len) | |
204 | return -EINVAL; | |
205 | if (memcmp(dent->name, nm->name, nm->len)) | |
206 | return -EINVAL; | |
207 | return 0; | |
208 | } | |
209 | ||
1e51764a | 210 | static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 211 | unsigned int flags) |
1e51764a AB |
212 | { |
213 | int err; | |
214 | union ubifs_key key; | |
215 | struct inode *inode = NULL; | |
216 | struct ubifs_dent_node *dent; | |
217 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
218 | ||
4cb2a01d | 219 | dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino); |
1e51764a | 220 | |
9270b2f4 RW |
221 | if (ubifs_crypt_is_encrypted(dir)) { |
222 | err = fscrypt_get_encryption_info(dir); | |
223 | ||
224 | /* | |
225 | * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is | |
226 | * created while the directory was encrypted and we | |
227 | * have access to the key. | |
228 | */ | |
229 | if (fscrypt_has_encryption_key(dir)) | |
230 | fscrypt_set_encrypted_dentry(dentry); | |
231 | fscrypt_set_d_op(dentry); | |
232 | if (err && err != -ENOKEY) | |
233 | return ERR_PTR(err); | |
234 | } | |
235 | ||
1e51764a AB |
236 | if (dentry->d_name.len > UBIFS_MAX_NLEN) |
237 | return ERR_PTR(-ENAMETOOLONG); | |
238 | ||
239 | dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); | |
240 | if (!dent) | |
241 | return ERR_PTR(-ENOMEM); | |
242 | ||
243 | dent_key_init(c, &key, dir->i_ino, &dentry->d_name); | |
244 | ||
245 | err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name); | |
246 | if (err) { | |
720b499c | 247 | if (err == -ENOENT) { |
1e51764a AB |
248 | dbg_gen("not found"); |
249 | goto done; | |
250 | } | |
251 | goto out; | |
252 | } | |
253 | ||
bb2615d4 | 254 | if (dbg_check_name(c, dent, &dentry->d_name)) { |
1e51764a AB |
255 | err = -EINVAL; |
256 | goto out; | |
257 | } | |
258 | ||
259 | inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum)); | |
260 | if (IS_ERR(inode)) { | |
261 | /* | |
262 | * This should not happen. Probably the file-system needs | |
263 | * checking. | |
264 | */ | |
265 | err = PTR_ERR(inode); | |
235c362b | 266 | ubifs_err(c, "dead directory entry '%pd', error %d", |
4cb2a01d | 267 | dentry, err); |
1e51764a AB |
268 | ubifs_ro_mode(c, err); |
269 | goto out; | |
270 | } | |
271 | ||
272 | done: | |
273 | kfree(dent); | |
274 | /* | |
275 | * Note, d_splice_alias() would be required instead if we supported | |
276 | * NFS. | |
277 | */ | |
278 | d_add(dentry, inode); | |
279 | return NULL; | |
280 | ||
281 | out: | |
282 | kfree(dent); | |
283 | return ERR_PTR(err); | |
284 | } | |
285 | ||
4acdaf27 | 286 | static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 287 | bool excl) |
1e51764a AB |
288 | { |
289 | struct inode *inode; | |
290 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
291 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
292 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
293 | .dirtied_ino = 1 }; | |
294 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
295 | ||
296 | /* | |
297 | * Budget request settings: new inode, new direntry, changing the | |
298 | * parent directory inode. | |
299 | */ | |
300 | ||
4cb2a01d AV |
301 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
302 | dentry, mode, dir->i_ino); | |
1e51764a AB |
303 | |
304 | err = ubifs_budget_space(c, &req); | |
305 | if (err) | |
306 | return err; | |
307 | ||
308 | inode = ubifs_new_inode(c, dir, mode); | |
309 | if (IS_ERR(inode)) { | |
310 | err = PTR_ERR(inode); | |
311 | goto out_budg; | |
312 | } | |
313 | ||
d7f0b70d SN |
314 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
315 | if (err) | |
9401a795 | 316 | goto out_inode; |
d7f0b70d | 317 | |
1e51764a AB |
318 | mutex_lock(&dir_ui->ui_mutex); |
319 | dir->i_size += sz_change; | |
320 | dir_ui->ui_size = dir->i_size; | |
321 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
322 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
323 | if (err) | |
324 | goto out_cancel; | |
325 | mutex_unlock(&dir_ui->ui_mutex); | |
326 | ||
327 | ubifs_release_budget(c, &req); | |
328 | insert_inode_hash(inode); | |
329 | d_instantiate(dentry, inode); | |
330 | return 0; | |
331 | ||
332 | out_cancel: | |
333 | dir->i_size -= sz_change; | |
334 | dir_ui->ui_size = dir->i_size; | |
335 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 336 | out_inode: |
1e51764a AB |
337 | make_bad_inode(inode); |
338 | iput(inode); | |
339 | out_budg: | |
340 | ubifs_release_budget(c, &req); | |
235c362b | 341 | ubifs_err(c, "cannot create regular file, error %d", err); |
1e51764a AB |
342 | return err; |
343 | } | |
344 | ||
9e0a1fff RW |
345 | static int do_tmpfile(struct inode *dir, struct dentry *dentry, |
346 | umode_t mode, struct inode **whiteout) | |
474b9370 RW |
347 | { |
348 | struct inode *inode; | |
349 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
350 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1}; | |
351 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1 }; | |
352 | struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir); | |
353 | int err, instantiated = 0; | |
354 | ||
355 | /* | |
356 | * Budget request settings: new dirty inode, new direntry, | |
357 | * budget for dirtied inode will be released via writeback. | |
358 | */ | |
359 | ||
360 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", | |
361 | dentry, mode, dir->i_ino); | |
362 | ||
363 | err = ubifs_budget_space(c, &req); | |
364 | if (err) | |
365 | return err; | |
366 | ||
367 | err = ubifs_budget_space(c, &ino_req); | |
368 | if (err) { | |
369 | ubifs_release_budget(c, &req); | |
370 | return err; | |
371 | } | |
372 | ||
373 | inode = ubifs_new_inode(c, dir, mode); | |
374 | if (IS_ERR(inode)) { | |
375 | err = PTR_ERR(inode); | |
376 | goto out_budg; | |
377 | } | |
378 | ui = ubifs_inode(inode); | |
379 | ||
9e0a1fff RW |
380 | if (whiteout) { |
381 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
382 | ubifs_assert(inode->i_op == &ubifs_file_inode_operations); | |
383 | } | |
384 | ||
474b9370 RW |
385 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
386 | if (err) | |
387 | goto out_inode; | |
388 | ||
389 | mutex_lock(&ui->ui_mutex); | |
390 | insert_inode_hash(inode); | |
9e0a1fff RW |
391 | |
392 | if (whiteout) { | |
393 | mark_inode_dirty(inode); | |
394 | drop_nlink(inode); | |
395 | *whiteout = inode; | |
396 | } else { | |
397 | d_tmpfile(dentry, inode); | |
398 | } | |
474b9370 | 399 | ubifs_assert(ui->dirty); |
9e0a1fff | 400 | |
474b9370 RW |
401 | instantiated = 1; |
402 | mutex_unlock(&ui->ui_mutex); | |
403 | ||
404 | mutex_lock(&dir_ui->ui_mutex); | |
405 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
406 | if (err) | |
407 | goto out_cancel; | |
408 | mutex_unlock(&dir_ui->ui_mutex); | |
409 | ||
410 | ubifs_release_budget(c, &req); | |
411 | ||
412 | return 0; | |
413 | ||
414 | out_cancel: | |
415 | mutex_unlock(&dir_ui->ui_mutex); | |
416 | out_inode: | |
417 | make_bad_inode(inode); | |
418 | if (!instantiated) | |
419 | iput(inode); | |
420 | out_budg: | |
421 | ubifs_release_budget(c, &req); | |
422 | if (!instantiated) | |
423 | ubifs_release_budget(c, &ino_req); | |
424 | ubifs_err(c, "cannot create temporary file, error %d", err); | |
425 | return err; | |
426 | } | |
427 | ||
9e0a1fff RW |
428 | static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry, |
429 | umode_t mode) | |
430 | { | |
431 | return do_tmpfile(dir, dentry, mode, NULL); | |
432 | } | |
433 | ||
1e51764a AB |
434 | /** |
435 | * vfs_dent_type - get VFS directory entry type. | |
436 | * @type: UBIFS directory entry type | |
437 | * | |
438 | * This function converts UBIFS directory entry type into VFS directory entry | |
439 | * type. | |
440 | */ | |
441 | static unsigned int vfs_dent_type(uint8_t type) | |
442 | { | |
443 | switch (type) { | |
444 | case UBIFS_ITYPE_REG: | |
445 | return DT_REG; | |
446 | case UBIFS_ITYPE_DIR: | |
447 | return DT_DIR; | |
448 | case UBIFS_ITYPE_LNK: | |
449 | return DT_LNK; | |
450 | case UBIFS_ITYPE_BLK: | |
451 | return DT_BLK; | |
452 | case UBIFS_ITYPE_CHR: | |
453 | return DT_CHR; | |
454 | case UBIFS_ITYPE_FIFO: | |
455 | return DT_FIFO; | |
456 | case UBIFS_ITYPE_SOCK: | |
457 | return DT_SOCK; | |
458 | default: | |
459 | BUG(); | |
460 | } | |
461 | return 0; | |
462 | } | |
463 | ||
464 | /* | |
465 | * The classical Unix view for directory is that it is a linear array of | |
466 | * (name, inode number) entries. Linux/VFS assumes this model as well. | |
467 | * Particularly, 'readdir()' call wants us to return a directory entry offset | |
468 | * which later may be used to continue 'readdir()'ing the directory or to | |
469 | * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this | |
470 | * model because directory entries are identified by keys, which may collide. | |
471 | * | |
472 | * UBIFS uses directory entry hash value for directory offsets, so | |
473 | * 'seekdir()'/'telldir()' may not always work because of possible key | |
474 | * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work | |
475 | * properly by means of saving full directory entry name in the private field | |
476 | * of the file description object. | |
477 | * | |
478 | * This means that UBIFS cannot support NFS which requires full | |
479 | * 'seekdir()'/'telldir()' support. | |
480 | */ | |
01122e06 | 481 | static int ubifs_readdir(struct file *file, struct dir_context *ctx) |
1e51764a | 482 | { |
c83ed4c9 | 483 | int err = 0; |
1e51764a AB |
484 | struct qstr nm; |
485 | union ubifs_key key; | |
486 | struct ubifs_dent_node *dent; | |
496ad9aa | 487 | struct inode *dir = file_inode(file); |
1e51764a AB |
488 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
489 | ||
01122e06 | 490 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos); |
1e51764a | 491 | |
01122e06 | 492 | if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2) |
1e51764a AB |
493 | /* |
494 | * The directory was seek'ed to a senseless position or there | |
495 | * are no more entries. | |
496 | */ | |
497 | return 0; | |
498 | ||
605c912b AB |
499 | if (file->f_version == 0) { |
500 | /* | |
501 | * The file was seek'ed, which means that @file->private_data | |
502 | * is now invalid. This may also be just the first | |
503 | * 'ubifs_readdir()' invocation, in which case | |
504 | * @file->private_data is NULL, and the below code is | |
505 | * basically a no-op. | |
506 | */ | |
507 | kfree(file->private_data); | |
508 | file->private_data = NULL; | |
509 | } | |
510 | ||
511 | /* | |
512 | * 'generic_file_llseek()' unconditionally sets @file->f_version to | |
513 | * zero, and we use this for detecting whether the file was seek'ed. | |
514 | */ | |
515 | file->f_version = 1; | |
516 | ||
1e51764a | 517 | /* File positions 0 and 1 correspond to "." and ".." */ |
01122e06 | 518 | if (ctx->pos < 2) { |
1e51764a | 519 | ubifs_assert(!file->private_data); |
01122e06 | 520 | if (!dir_emit_dots(file, ctx)) |
1e51764a AB |
521 | return 0; |
522 | ||
523 | /* Find the first entry in TNC and save it */ | |
524 | lowest_dent_key(c, &key, dir->i_ino); | |
525 | nm.name = NULL; | |
526 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
527 | if (IS_ERR(dent)) { | |
528 | err = PTR_ERR(dent); | |
529 | goto out; | |
530 | } | |
531 | ||
01122e06 | 532 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
533 | file->private_data = dent; |
534 | } | |
535 | ||
536 | dent = file->private_data; | |
537 | if (!dent) { | |
538 | /* | |
539 | * The directory was seek'ed to and is now readdir'ed. | |
01122e06 | 540 | * Find the entry corresponding to @ctx->pos or the closest one. |
1e51764a | 541 | */ |
01122e06 | 542 | dent_key_init_hash(c, &key, dir->i_ino, ctx->pos); |
1e51764a AB |
543 | nm.name = NULL; |
544 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
545 | if (IS_ERR(dent)) { | |
546 | err = PTR_ERR(dent); | |
547 | goto out; | |
548 | } | |
01122e06 | 549 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
550 | file->private_data = dent; |
551 | } | |
552 | ||
553 | while (1) { | |
554 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", | |
7424bac8 | 555 | dent->name, (unsigned long long)le64_to_cpu(dent->inum), |
1e51764a | 556 | key_hash_flash(c, &dent->key)); |
0ecb9529 HH |
557 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > |
558 | ubifs_inode(dir)->creat_sqnum); | |
1e51764a AB |
559 | |
560 | nm.len = le16_to_cpu(dent->nlen); | |
01122e06 | 561 | if (!dir_emit(ctx, dent->name, nm.len, |
1e51764a | 562 | le64_to_cpu(dent->inum), |
01122e06 | 563 | vfs_dent_type(dent->type))) |
1e51764a AB |
564 | return 0; |
565 | ||
566 | /* Switch to the next entry */ | |
567 | key_read(c, &dent->key, &key); | |
568 | nm.name = dent->name; | |
569 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
570 | if (IS_ERR(dent)) { | |
571 | err = PTR_ERR(dent); | |
572 | goto out; | |
573 | } | |
574 | ||
575 | kfree(file->private_data); | |
01122e06 | 576 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
577 | file->private_data = dent; |
578 | cond_resched(); | |
579 | } | |
580 | ||
581 | out: | |
aeeb14f7 RW |
582 | kfree(file->private_data); |
583 | file->private_data = NULL; | |
584 | ||
c83ed4c9 | 585 | if (err != -ENOENT) |
235c362b | 586 | ubifs_err(c, "cannot find next direntry, error %d", err); |
a00052a2 RW |
587 | else |
588 | /* | |
589 | * -ENOENT is a non-fatal error in this context, the TNC uses | |
590 | * it to indicate that the cursor moved past the current directory | |
591 | * and readdir() has to stop. | |
592 | */ | |
593 | err = 0; | |
594 | ||
1e51764a | 595 | |
605c912b | 596 | /* 2 is a special value indicating that there are no more direntries */ |
01122e06 | 597 | ctx->pos = 2; |
c83ed4c9 | 598 | return err; |
1e51764a AB |
599 | } |
600 | ||
1e51764a AB |
601 | /* Free saved readdir() state when the directory is closed */ |
602 | static int ubifs_dir_release(struct inode *dir, struct file *file) | |
603 | { | |
604 | kfree(file->private_data); | |
605 | file->private_data = NULL; | |
606 | return 0; | |
607 | } | |
608 | ||
609 | /** | |
82c1593c | 610 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
1e51764a AB |
611 | * @inode1: first inode |
612 | * @inode2: second inode | |
82c1593c AB |
613 | * |
614 | * We do not implement any tricks to guarantee strict lock ordering, because | |
615 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
616 | * wrapper function. | |
1e51764a AB |
617 | */ |
618 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) | |
619 | { | |
82c1593c AB |
620 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
621 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1e51764a AB |
622 | } |
623 | ||
624 | /** | |
82c1593c | 625 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
1e51764a AB |
626 | * @inode1: first inode |
627 | * @inode2: second inode | |
628 | */ | |
629 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) | |
630 | { | |
1e51764a | 631 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
82c1593c | 632 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
1e51764a AB |
633 | } |
634 | ||
635 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, | |
636 | struct dentry *dentry) | |
637 | { | |
638 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 639 | struct inode *inode = d_inode(old_dentry); |
1e51764a AB |
640 | struct ubifs_inode *ui = ubifs_inode(inode); |
641 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
642 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
643 | struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2, | |
dab4b4d2 | 644 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
1e51764a AB |
645 | |
646 | /* | |
647 | * Budget request settings: new direntry, changing the target inode, | |
648 | * changing the parent inode. | |
649 | */ | |
650 | ||
4cb2a01d AV |
651 | dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu", |
652 | dentry, inode->i_ino, | |
1e51764a | 653 | inode->i_nlink, dir->i_ino); |
5955102c AV |
654 | ubifs_assert(inode_is_locked(dir)); |
655 | ubifs_assert(inode_is_locked(inode)); | |
8b3884a8 | 656 | |
ac7e47a9 RW |
657 | if (ubifs_crypt_is_encrypted(dir) && |
658 | !fscrypt_has_permitted_context(dir, inode)) | |
659 | return -EPERM; | |
660 | ||
d808efb4 | 661 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
662 | if (err) |
663 | return err; | |
664 | ||
665 | err = ubifs_budget_space(c, &req); | |
666 | if (err) | |
667 | return err; | |
668 | ||
669 | lock_2_inodes(dir, inode); | |
670 | inc_nlink(inode); | |
7de9c6ee | 671 | ihold(inode); |
1e51764a AB |
672 | inode->i_ctime = ubifs_current_time(inode); |
673 | dir->i_size += sz_change; | |
674 | dir_ui->ui_size = dir->i_size; | |
675 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
676 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
677 | if (err) | |
678 | goto out_cancel; | |
679 | unlock_2_inodes(dir, inode); | |
680 | ||
681 | ubifs_release_budget(c, &req); | |
682 | d_instantiate(dentry, inode); | |
683 | return 0; | |
684 | ||
685 | out_cancel: | |
686 | dir->i_size -= sz_change; | |
687 | dir_ui->ui_size = dir->i_size; | |
688 | drop_nlink(inode); | |
689 | unlock_2_inodes(dir, inode); | |
690 | ubifs_release_budget(c, &req); | |
691 | iput(inode); | |
692 | return err; | |
693 | } | |
694 | ||
695 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) | |
696 | { | |
697 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 698 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
699 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
700 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
701 | int err, budgeted = 1; | |
702 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
c43be108 | 703 | unsigned int saved_nlink = inode->i_nlink; |
1e51764a AB |
704 | |
705 | /* | |
706 | * Budget request settings: deletion direntry, deletion inode (+1 for | |
707 | * @dirtied_ino), changing the parent directory inode. If budgeting | |
708 | * fails, go ahead anyway because we have extra space reserved for | |
709 | * deletions. | |
710 | */ | |
711 | ||
4cb2a01d AV |
712 | dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu", |
713 | dentry, inode->i_ino, | |
1e51764a | 714 | inode->i_nlink, dir->i_ino); |
5955102c AV |
715 | ubifs_assert(inode_is_locked(dir)); |
716 | ubifs_assert(inode_is_locked(inode)); | |
d808efb4 | 717 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
718 | if (err) |
719 | return err; | |
720 | ||
721 | err = ubifs_budget_space(c, &req); | |
722 | if (err) { | |
723 | if (err != -ENOSPC) | |
724 | return err; | |
1e51764a AB |
725 | budgeted = 0; |
726 | } | |
727 | ||
728 | lock_2_inodes(dir, inode); | |
729 | inode->i_ctime = ubifs_current_time(dir); | |
730 | drop_nlink(inode); | |
731 | dir->i_size -= sz_change; | |
732 | dir_ui->ui_size = dir->i_size; | |
733 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
734 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
735 | if (err) | |
736 | goto out_cancel; | |
737 | unlock_2_inodes(dir, inode); | |
738 | ||
739 | if (budgeted) | |
740 | ubifs_release_budget(c, &req); | |
741 | else { | |
742 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 743 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
744 | smp_wmb(); |
745 | } | |
746 | return 0; | |
747 | ||
748 | out_cancel: | |
749 | dir->i_size += sz_change; | |
750 | dir_ui->ui_size = dir->i_size; | |
c43be108 | 751 | set_nlink(inode, saved_nlink); |
1e51764a AB |
752 | unlock_2_inodes(dir, inode); |
753 | if (budgeted) | |
754 | ubifs_release_budget(c, &req); | |
755 | return err; | |
756 | } | |
757 | ||
758 | /** | |
759 | * check_dir_empty - check if a directory is empty or not. | |
1e51764a AB |
760 | * @dir: VFS inode object of the directory to check |
761 | * | |
762 | * This function checks if directory @dir is empty. Returns zero if the | |
763 | * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes | |
764 | * in case of of errors. | |
765 | */ | |
f6337d84 | 766 | int ubifs_check_dir_empty(struct inode *dir) |
1e51764a | 767 | { |
f6337d84 | 768 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
1e51764a AB |
769 | struct qstr nm = { .name = NULL }; |
770 | struct ubifs_dent_node *dent; | |
771 | union ubifs_key key; | |
772 | int err; | |
773 | ||
774 | lowest_dent_key(c, &key, dir->i_ino); | |
775 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
776 | if (IS_ERR(dent)) { | |
777 | err = PTR_ERR(dent); | |
778 | if (err == -ENOENT) | |
779 | err = 0; | |
780 | } else { | |
781 | kfree(dent); | |
782 | err = -ENOTEMPTY; | |
783 | } | |
784 | return err; | |
785 | } | |
786 | ||
787 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) | |
788 | { | |
789 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 790 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
791 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); |
792 | int err, budgeted = 1; | |
793 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
794 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
795 | ||
796 | /* | |
797 | * Budget request settings: deletion direntry, deletion inode and | |
798 | * changing the parent inode. If budgeting fails, go ahead anyway | |
799 | * because we have extra space reserved for deletions. | |
800 | */ | |
801 | ||
4cb2a01d AV |
802 | dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry, |
803 | inode->i_ino, dir->i_ino); | |
5955102c AV |
804 | ubifs_assert(inode_is_locked(dir)); |
805 | ubifs_assert(inode_is_locked(inode)); | |
f6337d84 | 806 | err = ubifs_check_dir_empty(d_inode(dentry)); |
1e51764a AB |
807 | if (err) |
808 | return err; | |
809 | ||
810 | err = ubifs_budget_space(c, &req); | |
811 | if (err) { | |
812 | if (err != -ENOSPC) | |
813 | return err; | |
814 | budgeted = 0; | |
815 | } | |
816 | ||
817 | lock_2_inodes(dir, inode); | |
818 | inode->i_ctime = ubifs_current_time(dir); | |
819 | clear_nlink(inode); | |
820 | drop_nlink(dir); | |
821 | dir->i_size -= sz_change; | |
822 | dir_ui->ui_size = dir->i_size; | |
823 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
824 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
825 | if (err) | |
826 | goto out_cancel; | |
827 | unlock_2_inodes(dir, inode); | |
828 | ||
829 | if (budgeted) | |
830 | ubifs_release_budget(c, &req); | |
831 | else { | |
832 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 833 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
834 | smp_wmb(); |
835 | } | |
836 | return 0; | |
837 | ||
838 | out_cancel: | |
839 | dir->i_size += sz_change; | |
840 | dir_ui->ui_size = dir->i_size; | |
841 | inc_nlink(dir); | |
c43be108 | 842 | set_nlink(inode, 2); |
1e51764a AB |
843 | unlock_2_inodes(dir, inode); |
844 | if (budgeted) | |
845 | ubifs_release_budget(c, &req); | |
846 | return err; | |
847 | } | |
848 | ||
18bb1db3 | 849 | static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1e51764a AB |
850 | { |
851 | struct inode *inode; | |
852 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
853 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
854 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
182854b4 | 855 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
1e51764a AB |
856 | |
857 | /* | |
858 | * Budget request settings: new inode, new direntry and changing parent | |
859 | * directory inode. | |
860 | */ | |
861 | ||
4cb2a01d AV |
862 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
863 | dentry, mode, dir->i_ino); | |
1e51764a AB |
864 | |
865 | err = ubifs_budget_space(c, &req); | |
866 | if (err) | |
867 | return err; | |
868 | ||
869 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); | |
870 | if (IS_ERR(inode)) { | |
871 | err = PTR_ERR(inode); | |
872 | goto out_budg; | |
873 | } | |
874 | ||
d7f0b70d SN |
875 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
876 | if (err) | |
9401a795 | 877 | goto out_inode; |
d7f0b70d | 878 | |
1e51764a AB |
879 | mutex_lock(&dir_ui->ui_mutex); |
880 | insert_inode_hash(inode); | |
881 | inc_nlink(inode); | |
882 | inc_nlink(dir); | |
883 | dir->i_size += sz_change; | |
884 | dir_ui->ui_size = dir->i_size; | |
885 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
886 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
887 | if (err) { | |
235c362b | 888 | ubifs_err(c, "cannot create directory, error %d", err); |
1e51764a AB |
889 | goto out_cancel; |
890 | } | |
891 | mutex_unlock(&dir_ui->ui_mutex); | |
892 | ||
893 | ubifs_release_budget(c, &req); | |
894 | d_instantiate(dentry, inode); | |
895 | return 0; | |
896 | ||
897 | out_cancel: | |
898 | dir->i_size -= sz_change; | |
899 | dir_ui->ui_size = dir->i_size; | |
900 | drop_nlink(dir); | |
901 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 902 | out_inode: |
1e51764a AB |
903 | make_bad_inode(inode); |
904 | iput(inode); | |
905 | out_budg: | |
906 | ubifs_release_budget(c, &req); | |
907 | return err; | |
908 | } | |
909 | ||
910 | static int ubifs_mknod(struct inode *dir, struct dentry *dentry, | |
1a67aafb | 911 | umode_t mode, dev_t rdev) |
1e51764a AB |
912 | { |
913 | struct inode *inode; | |
914 | struct ubifs_inode *ui; | |
915 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
916 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
917 | union ubifs_dev_desc *dev = NULL; | |
918 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
919 | int err, devlen = 0; | |
920 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
921 | .new_ino_d = ALIGN(devlen, 8), |
922 | .dirtied_ino = 1 }; | |
1e51764a AB |
923 | |
924 | /* | |
925 | * Budget request settings: new inode, new direntry and changing parent | |
926 | * directory inode. | |
927 | */ | |
928 | ||
4cb2a01d | 929 | dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino); |
1e51764a | 930 | |
1e51764a AB |
931 | if (S_ISBLK(mode) || S_ISCHR(mode)) { |
932 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); | |
933 | if (!dev) | |
934 | return -ENOMEM; | |
935 | devlen = ubifs_encode_dev(dev, rdev); | |
936 | } | |
937 | ||
938 | err = ubifs_budget_space(c, &req); | |
939 | if (err) { | |
940 | kfree(dev); | |
941 | return err; | |
942 | } | |
943 | ||
944 | inode = ubifs_new_inode(c, dir, mode); | |
945 | if (IS_ERR(inode)) { | |
946 | kfree(dev); | |
947 | err = PTR_ERR(inode); | |
948 | goto out_budg; | |
949 | } | |
950 | ||
951 | init_special_inode(inode, inode->i_mode, rdev); | |
952 | inode->i_size = ubifs_inode(inode)->ui_size = devlen; | |
953 | ui = ubifs_inode(inode); | |
954 | ui->data = dev; | |
955 | ui->data_len = devlen; | |
956 | ||
d7f0b70d SN |
957 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
958 | if (err) | |
9401a795 | 959 | goto out_inode; |
d7f0b70d | 960 | |
1e51764a AB |
961 | mutex_lock(&dir_ui->ui_mutex); |
962 | dir->i_size += sz_change; | |
963 | dir_ui->ui_size = dir->i_size; | |
964 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
965 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
966 | if (err) | |
967 | goto out_cancel; | |
968 | mutex_unlock(&dir_ui->ui_mutex); | |
969 | ||
970 | ubifs_release_budget(c, &req); | |
971 | insert_inode_hash(inode); | |
972 | d_instantiate(dentry, inode); | |
973 | return 0; | |
974 | ||
975 | out_cancel: | |
976 | dir->i_size -= sz_change; | |
977 | dir_ui->ui_size = dir->i_size; | |
978 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 979 | out_inode: |
1e51764a AB |
980 | make_bad_inode(inode); |
981 | iput(inode); | |
982 | out_budg: | |
983 | ubifs_release_budget(c, &req); | |
984 | return err; | |
985 | } | |
986 | ||
987 | static int ubifs_symlink(struct inode *dir, struct dentry *dentry, | |
988 | const char *symname) | |
989 | { | |
990 | struct inode *inode; | |
991 | struct ubifs_inode *ui; | |
992 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
993 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
994 | int err, len = strlen(symname); | |
995 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
996 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
997 | .new_ino_d = ALIGN(len, 8), |
998 | .dirtied_ino = 1 }; | |
1e51764a AB |
999 | |
1000 | /* | |
1001 | * Budget request settings: new inode, new direntry and changing parent | |
1002 | * directory inode. | |
1003 | */ | |
1004 | ||
4cb2a01d AV |
1005 | dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry, |
1006 | symname, dir->i_ino); | |
1e51764a AB |
1007 | |
1008 | if (len > UBIFS_MAX_INO_DATA) | |
1009 | return -ENAMETOOLONG; | |
1010 | ||
1011 | err = ubifs_budget_space(c, &req); | |
1012 | if (err) | |
1013 | return err; | |
1014 | ||
1015 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); | |
1016 | if (IS_ERR(inode)) { | |
1017 | err = PTR_ERR(inode); | |
1018 | goto out_budg; | |
1019 | } | |
1020 | ||
1021 | ui = ubifs_inode(inode); | |
1022 | ui->data = kmalloc(len + 1, GFP_NOFS); | |
1023 | if (!ui->data) { | |
1024 | err = -ENOMEM; | |
1025 | goto out_inode; | |
1026 | } | |
1027 | ||
1028 | memcpy(ui->data, symname, len); | |
1029 | ((char *)ui->data)[len] = '\0'; | |
0f301bd3 | 1030 | inode->i_link = ui->data; |
1e51764a AB |
1031 | /* |
1032 | * The terminating zero byte is not written to the flash media and it | |
1033 | * is put just to make later in-memory string processing simpler. Thus, | |
1034 | * data length is @len, not @len + %1. | |
1035 | */ | |
1036 | ui->data_len = len; | |
1037 | inode->i_size = ubifs_inode(inode)->ui_size = len; | |
1038 | ||
d7f0b70d SN |
1039 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
1040 | if (err) | |
9401a795 | 1041 | goto out_inode; |
d7f0b70d | 1042 | |
1e51764a AB |
1043 | mutex_lock(&dir_ui->ui_mutex); |
1044 | dir->i_size += sz_change; | |
1045 | dir_ui->ui_size = dir->i_size; | |
1046 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
1047 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
1048 | if (err) | |
1049 | goto out_cancel; | |
1050 | mutex_unlock(&dir_ui->ui_mutex); | |
1051 | ||
1052 | ubifs_release_budget(c, &req); | |
1053 | insert_inode_hash(inode); | |
1054 | d_instantiate(dentry, inode); | |
1055 | return 0; | |
1056 | ||
1057 | out_cancel: | |
1058 | dir->i_size -= sz_change; | |
1059 | dir_ui->ui_size = dir->i_size; | |
1060 | mutex_unlock(&dir_ui->ui_mutex); | |
1061 | out_inode: | |
1062 | make_bad_inode(inode); | |
1063 | iput(inode); | |
1064 | out_budg: | |
1065 | ubifs_release_budget(c, &req); | |
1066 | return err; | |
1067 | } | |
1068 | ||
1069 | /** | |
9e0a1fff | 1070 | * lock_4_inodes - a wrapper for locking three UBIFS inodes. |
1e51764a AB |
1071 | * @inode1: first inode |
1072 | * @inode2: second inode | |
1073 | * @inode3: third inode | |
9e0a1fff | 1074 | * @inode4: fouth inode |
1e51764a | 1075 | * |
82c1593c | 1076 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
9e0a1fff | 1077 | * @inode2 whereas @inode3 and @inode4 may be %NULL. |
82c1593c AB |
1078 | * |
1079 | * We do not implement any tricks to guarantee strict lock ordering, because | |
1080 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
1081 | * wrapper function. | |
1e51764a | 1082 | */ |
9e0a1fff RW |
1083 | static void lock_4_inodes(struct inode *inode1, struct inode *inode2, |
1084 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1085 | { |
82c1593c AB |
1086 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
1087 | if (inode2 != inode1) | |
1088 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1089 | if (inode3) | |
1090 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); | |
9e0a1fff RW |
1091 | if (inode4) |
1092 | mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4); | |
1e51764a AB |
1093 | } |
1094 | ||
1095 | /** | |
9e0a1fff | 1096 | * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
1e51764a AB |
1097 | * @inode1: first inode |
1098 | * @inode2: second inode | |
1099 | * @inode3: third inode | |
9e0a1fff | 1100 | * @inode4: fouth inode |
1e51764a | 1101 | */ |
9e0a1fff RW |
1102 | static void unlock_4_inodes(struct inode *inode1, struct inode *inode2, |
1103 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1104 | { |
9e0a1fff RW |
1105 | if (inode4) |
1106 | mutex_unlock(&ubifs_inode(inode4)->ui_mutex); | |
1e51764a AB |
1107 | if (inode3) |
1108 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); | |
82c1593c AB |
1109 | if (inode1 != inode2) |
1110 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | |
1111 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | |
1e51764a AB |
1112 | } |
1113 | ||
390975ac RW |
1114 | static int do_rename(struct inode *old_dir, struct dentry *old_dentry, |
1115 | struct inode *new_dir, struct dentry *new_dentry, | |
1116 | unsigned int flags) | |
1e51764a AB |
1117 | { |
1118 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
2b0143b5 DH |
1119 | struct inode *old_inode = d_inode(old_dentry); |
1120 | struct inode *new_inode = d_inode(new_dentry); | |
9e0a1fff | 1121 | struct inode *whiteout = NULL; |
1e51764a | 1122 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); |
9e0a1fff | 1123 | struct ubifs_inode *whiteout_ui = NULL; |
1e51764a AB |
1124 | int err, release, sync = 0, move = (new_dir != old_dir); |
1125 | int is_dir = S_ISDIR(old_inode->i_mode); | |
1126 | int unlink = !!new_inode; | |
1127 | int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len); | |
1128 | int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len); | |
1129 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, | |
1130 | .dirtied_ino = 3 }; | |
1131 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1, | |
dab4b4d2 | 1132 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
1e51764a | 1133 | struct timespec time; |
782759b9 | 1134 | unsigned int uninitialized_var(saved_nlink); |
1e51764a | 1135 | |
f03b8ad8 MS |
1136 | if (flags & ~RENAME_NOREPLACE) |
1137 | return -EINVAL; | |
1138 | ||
1e51764a AB |
1139 | /* |
1140 | * Budget request settings: deletion direntry, new direntry, removing | |
1141 | * the old inode, and changing old and new parent directory inodes. | |
1142 | * | |
1143 | * However, this operation also marks the target inode as dirty and | |
1144 | * does not write it, so we allocate budget for the target inode | |
1145 | * separately. | |
1146 | */ | |
1147 | ||
9e0a1fff | 1148 | dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x", |
4cb2a01d | 1149 | old_dentry, old_inode->i_ino, old_dir->i_ino, |
9e0a1fff RW |
1150 | new_dentry, new_dir->i_ino, flags); |
1151 | ||
82c1593c | 1152 | if (unlink) |
5955102c | 1153 | ubifs_assert(inode_is_locked(new_inode)); |
82c1593c | 1154 | |
ac7e47a9 RW |
1155 | if (old_dir != new_dir) { |
1156 | if (ubifs_crypt_is_encrypted(new_dir) && | |
1157 | !fscrypt_has_permitted_context(new_dir, old_inode)) | |
1158 | return -EPERM; | |
1159 | } | |
1160 | ||
1e51764a | 1161 | if (unlink && is_dir) { |
f6337d84 | 1162 | err = ubifs_check_dir_empty(new_inode); |
1e51764a AB |
1163 | if (err) |
1164 | return err; | |
1165 | } | |
1166 | ||
1167 | err = ubifs_budget_space(c, &req); | |
1168 | if (err) | |
1169 | return err; | |
1170 | err = ubifs_budget_space(c, &ino_req); | |
1171 | if (err) { | |
1172 | ubifs_release_budget(c, &req); | |
1173 | return err; | |
1174 | } | |
1175 | ||
9e0a1fff RW |
1176 | if (flags & RENAME_WHITEOUT) { |
1177 | union ubifs_dev_desc *dev = NULL; | |
1178 | ||
1179 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); | |
1180 | if (!dev) { | |
1181 | ubifs_release_budget(c, &req); | |
1182 | ubifs_release_budget(c, &ino_req); | |
1183 | return -ENOMEM; | |
1184 | } | |
1185 | ||
1186 | err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout); | |
1187 | if (err) { | |
1188 | ubifs_release_budget(c, &req); | |
1189 | ubifs_release_budget(c, &ino_req); | |
1190 | kfree(dev); | |
1191 | return err; | |
1192 | } | |
1193 | ||
1194 | whiteout->i_state |= I_LINKABLE; | |
1195 | whiteout_ui = ubifs_inode(whiteout); | |
1196 | whiteout_ui->data = dev; | |
1197 | whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); | |
1198 | ubifs_assert(!whiteout_ui->dirty); | |
1199 | } | |
1200 | ||
1201 | lock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
1e51764a AB |
1202 | |
1203 | /* | |
1204 | * Like most other Unix systems, set the @i_ctime for inodes on a | |
1205 | * rename. | |
1206 | */ | |
1207 | time = ubifs_current_time(old_dir); | |
1208 | old_inode->i_ctime = time; | |
1209 | ||
1210 | /* We must adjust parent link count when renaming directories */ | |
1211 | if (is_dir) { | |
1212 | if (move) { | |
1213 | /* | |
1214 | * @old_dir loses a link because we are moving | |
1215 | * @old_inode to a different directory. | |
1216 | */ | |
1217 | drop_nlink(old_dir); | |
1218 | /* | |
1219 | * @new_dir only gains a link if we are not also | |
1220 | * overwriting an existing directory. | |
1221 | */ | |
1222 | if (!unlink) | |
1223 | inc_nlink(new_dir); | |
1224 | } else { | |
1225 | /* | |
1226 | * @old_inode is not moving to a different directory, | |
1227 | * but @old_dir still loses a link if we are | |
1228 | * overwriting an existing directory. | |
1229 | */ | |
1230 | if (unlink) | |
1231 | drop_nlink(old_dir); | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | old_dir->i_size -= old_sz; | |
1236 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1237 | old_dir->i_mtime = old_dir->i_ctime = time; | |
1238 | new_dir->i_mtime = new_dir->i_ctime = time; | |
1239 | ||
1240 | /* | |
1241 | * And finally, if we unlinked a direntry which happened to have the | |
1242 | * same name as the moved direntry, we have to decrement @i_nlink of | |
1243 | * the unlinked inode and change its ctime. | |
1244 | */ | |
1245 | if (unlink) { | |
1246 | /* | |
1247 | * Directories cannot have hard-links, so if this is a | |
c43be108 | 1248 | * directory, just clear @i_nlink. |
1e51764a | 1249 | */ |
c43be108 | 1250 | saved_nlink = new_inode->i_nlink; |
1e51764a | 1251 | if (is_dir) |
c43be108 AB |
1252 | clear_nlink(new_inode); |
1253 | else | |
1e51764a AB |
1254 | drop_nlink(new_inode); |
1255 | new_inode->i_ctime = time; | |
1e51764a AB |
1256 | } else { |
1257 | new_dir->i_size += new_sz; | |
1258 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode | |
1263 | * is dirty, because this will be done later on at the end of | |
1264 | * 'ubifs_rename()'. | |
1265 | */ | |
1266 | if (IS_SYNC(old_inode)) { | |
1267 | sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); | |
1268 | if (unlink && IS_SYNC(new_inode)) | |
1269 | sync = 1; | |
1270 | } | |
9e0a1fff RW |
1271 | |
1272 | if (whiteout) { | |
1273 | struct ubifs_budget_req wht_req = { .dirtied_ino = 1, | |
1274 | .dirtied_ino_d = \ | |
1275 | ALIGN(ubifs_inode(whiteout)->data_len, 8) }; | |
1276 | ||
1277 | err = ubifs_budget_space(c, &wht_req); | |
1278 | if (err) { | |
1279 | ubifs_release_budget(c, &req); | |
1280 | ubifs_release_budget(c, &ino_req); | |
1281 | kfree(whiteout_ui->data); | |
1282 | whiteout_ui->data_len = 0; | |
1283 | iput(whiteout); | |
1284 | return err; | |
1285 | } | |
1286 | ||
1287 | inc_nlink(whiteout); | |
1288 | mark_inode_dirty(whiteout); | |
1289 | whiteout->i_state &= ~I_LINKABLE; | |
1290 | iput(whiteout); | |
1291 | } | |
1292 | ||
1293 | err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, whiteout, | |
1e51764a AB |
1294 | sync); |
1295 | if (err) | |
1296 | goto out_cancel; | |
1297 | ||
9e0a1fff | 1298 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
1e51764a AB |
1299 | ubifs_release_budget(c, &req); |
1300 | ||
1301 | mutex_lock(&old_inode_ui->ui_mutex); | |
1302 | release = old_inode_ui->dirty; | |
1303 | mark_inode_dirty_sync(old_inode); | |
1304 | mutex_unlock(&old_inode_ui->ui_mutex); | |
1305 | ||
1306 | if (release) | |
1307 | ubifs_release_budget(c, &ino_req); | |
1308 | if (IS_SYNC(old_inode)) | |
a9185b41 | 1309 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
1e51764a AB |
1310 | return err; |
1311 | ||
1312 | out_cancel: | |
1313 | if (unlink) { | |
c43be108 | 1314 | set_nlink(new_inode, saved_nlink); |
1e51764a AB |
1315 | } else { |
1316 | new_dir->i_size -= new_sz; | |
1317 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1318 | } | |
1319 | old_dir->i_size += old_sz; | |
1320 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1321 | if (is_dir) { | |
1322 | if (move) { | |
1323 | inc_nlink(old_dir); | |
1324 | if (!unlink) | |
1325 | drop_nlink(new_dir); | |
1326 | } else { | |
1327 | if (unlink) | |
1328 | inc_nlink(old_dir); | |
1329 | } | |
1330 | } | |
9e0a1fff RW |
1331 | if (whiteout) { |
1332 | drop_nlink(whiteout); | |
1333 | iput(whiteout); | |
1334 | } | |
1335 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
1e51764a AB |
1336 | ubifs_release_budget(c, &ino_req); |
1337 | ubifs_release_budget(c, &req); | |
1338 | return err; | |
1339 | } | |
1340 | ||
9ec64962 RW |
1341 | static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry, |
1342 | struct inode *new_dir, struct dentry *new_dentry) | |
1343 | { | |
1344 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
1345 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, | |
1346 | .dirtied_ino = 2 }; | |
1347 | int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); | |
1348 | struct inode *fst_inode = d_inode(old_dentry); | |
1349 | struct inode *snd_inode = d_inode(new_dentry); | |
1350 | struct timespec time; | |
1351 | int err; | |
1352 | ||
1353 | ubifs_assert(fst_inode && snd_inode); | |
1354 | ||
ac7e47a9 RW |
1355 | if ((ubifs_crypt_is_encrypted(old_dir) || |
1356 | ubifs_crypt_is_encrypted(new_dir)) && | |
1357 | (old_dir != new_dir) && | |
1358 | (!fscrypt_has_permitted_context(new_dir, fst_inode) || | |
1359 | !fscrypt_has_permitted_context(old_dir, snd_inode))) | |
1360 | return -EPERM; | |
1361 | ||
9ec64962 RW |
1362 | lock_4_inodes(old_dir, new_dir, NULL, NULL); |
1363 | ||
1364 | time = ubifs_current_time(old_dir); | |
1365 | fst_inode->i_ctime = time; | |
1366 | snd_inode->i_ctime = time; | |
1367 | old_dir->i_mtime = old_dir->i_ctime = time; | |
1368 | new_dir->i_mtime = new_dir->i_ctime = time; | |
1369 | ||
1370 | if (old_dir != new_dir) { | |
1371 | if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) { | |
1372 | inc_nlink(new_dir); | |
1373 | drop_nlink(old_dir); | |
1374 | } | |
1375 | else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) { | |
1376 | drop_nlink(new_dir); | |
1377 | inc_nlink(old_dir); | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry, | |
1382 | sync); | |
1383 | ||
1384 | unlock_4_inodes(old_dir, new_dir, NULL, NULL); | |
1385 | ubifs_release_budget(c, &req); | |
1386 | ||
1387 | return err; | |
1388 | } | |
1389 | ||
390975ac | 1390 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9ec64962 RW |
1391 | struct inode *new_dir, struct dentry *new_dentry, |
1392 | unsigned int flags) | |
1393 | { | |
1394 | if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE)) | |
1395 | return -EINVAL; | |
1396 | ||
1397 | ubifs_assert(inode_is_locked(old_dir)); | |
1398 | ubifs_assert(inode_is_locked(new_dir)); | |
1399 | ||
1400 | if (flags & RENAME_EXCHANGE) | |
1401 | return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry); | |
1402 | ||
390975ac | 1403 | return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
9ec64962 RW |
1404 | } |
1405 | ||
1e51764a AB |
1406 | int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
1407 | struct kstat *stat) | |
1408 | { | |
1409 | loff_t size; | |
2b0143b5 | 1410 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
1411 | struct ubifs_inode *ui = ubifs_inode(inode); |
1412 | ||
1413 | mutex_lock(&ui->ui_mutex); | |
6d42e7e9 | 1414 | generic_fillattr(inode, stat); |
1e51764a AB |
1415 | stat->blksize = UBIFS_BLOCK_SIZE; |
1416 | stat->size = ui->ui_size; | |
1417 | ||
1418 | /* | |
1419 | * Unfortunately, the 'stat()' system call was designed for block | |
1420 | * device based file systems, and it is not appropriate for UBIFS, | |
1421 | * because UBIFS does not have notion of "block". For example, it is | |
1422 | * difficult to tell how many block a directory takes - it actually | |
1423 | * takes less than 300 bytes, but we have to round it to block size, | |
1424 | * which introduces large mistake. This makes utilities like 'du' to | |
1425 | * report completely senseless numbers. This is the reason why UBIFS | |
1426 | * goes the same way as JFFS2 - it reports zero blocks for everything | |
1427 | * but regular files, which makes more sense than reporting completely | |
1428 | * wrong sizes. | |
1429 | */ | |
1430 | if (S_ISREG(inode->i_mode)) { | |
1431 | size = ui->xattr_size; | |
1432 | size += stat->size; | |
1433 | size = ALIGN(size, UBIFS_BLOCK_SIZE); | |
1434 | /* | |
1435 | * Note, user-space expects 512-byte blocks count irrespectively | |
1436 | * of what was reported in @stat->size. | |
1437 | */ | |
1438 | stat->blocks = size >> 9; | |
1439 | } else | |
1440 | stat->blocks = 0; | |
1441 | mutex_unlock(&ui->ui_mutex); | |
1442 | return 0; | |
1443 | } | |
1444 | ||
ba40e6a3 RW |
1445 | static int ubifs_dir_open(struct inode *dir, struct file *file) |
1446 | { | |
1447 | if (ubifs_crypt_is_encrypted(dir)) | |
1448 | return fscrypt_get_encryption_info(dir) ? -EACCES : 0; | |
1449 | ||
1450 | return 0; | |
1451 | } | |
1452 | ||
e8b81566 | 1453 | const struct inode_operations ubifs_dir_inode_operations = { |
1e51764a AB |
1454 | .lookup = ubifs_lookup, |
1455 | .create = ubifs_create, | |
1456 | .link = ubifs_link, | |
1457 | .symlink = ubifs_symlink, | |
1458 | .unlink = ubifs_unlink, | |
1459 | .mkdir = ubifs_mkdir, | |
1460 | .rmdir = ubifs_rmdir, | |
1461 | .mknod = ubifs_mknod, | |
390975ac | 1462 | .rename = ubifs_rename, |
1e51764a AB |
1463 | .setattr = ubifs_setattr, |
1464 | .getattr = ubifs_getattr, | |
1e51764a | 1465 | .listxattr = ubifs_listxattr, |
8c1c5f26 DY |
1466 | #ifdef CONFIG_UBIFS_ATIME_SUPPORT |
1467 | .update_time = ubifs_update_time, | |
1468 | #endif | |
474b9370 | 1469 | .tmpfile = ubifs_tmpfile, |
1e51764a AB |
1470 | }; |
1471 | ||
e8b81566 | 1472 | const struct file_operations ubifs_dir_operations = { |
01122e06 | 1473 | .llseek = generic_file_llseek, |
1e51764a AB |
1474 | .release = ubifs_dir_release, |
1475 | .read = generic_read_dir, | |
c51da20c | 1476 | .iterate_shared = ubifs_readdir, |
1e51764a AB |
1477 | .fsync = ubifs_fsync, |
1478 | .unlocked_ioctl = ubifs_ioctl, | |
ba40e6a3 | 1479 | .open = ubifs_dir_open, |
1e51764a AB |
1480 | #ifdef CONFIG_COMPAT |
1481 | .compat_ioctl = ubifs_compat_ioctl, | |
1482 | #endif | |
1483 | }; |