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 AB |
220 | |
221 | if (dentry->d_name.len > UBIFS_MAX_NLEN) | |
222 | return ERR_PTR(-ENAMETOOLONG); | |
223 | ||
224 | dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); | |
225 | if (!dent) | |
226 | return ERR_PTR(-ENOMEM); | |
227 | ||
228 | dent_key_init(c, &key, dir->i_ino, &dentry->d_name); | |
229 | ||
230 | err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name); | |
231 | if (err) { | |
720b499c | 232 | if (err == -ENOENT) { |
1e51764a AB |
233 | dbg_gen("not found"); |
234 | goto done; | |
235 | } | |
236 | goto out; | |
237 | } | |
238 | ||
bb2615d4 | 239 | if (dbg_check_name(c, dent, &dentry->d_name)) { |
1e51764a AB |
240 | err = -EINVAL; |
241 | goto out; | |
242 | } | |
243 | ||
244 | inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum)); | |
245 | if (IS_ERR(inode)) { | |
246 | /* | |
247 | * This should not happen. Probably the file-system needs | |
248 | * checking. | |
249 | */ | |
250 | err = PTR_ERR(inode); | |
235c362b | 251 | ubifs_err(c, "dead directory entry '%pd', error %d", |
4cb2a01d | 252 | dentry, err); |
1e51764a AB |
253 | ubifs_ro_mode(c, err); |
254 | goto out; | |
255 | } | |
256 | ||
257 | done: | |
258 | kfree(dent); | |
259 | /* | |
260 | * Note, d_splice_alias() would be required instead if we supported | |
261 | * NFS. | |
262 | */ | |
263 | d_add(dentry, inode); | |
264 | return NULL; | |
265 | ||
266 | out: | |
267 | kfree(dent); | |
268 | return ERR_PTR(err); | |
269 | } | |
270 | ||
4acdaf27 | 271 | static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 272 | bool excl) |
1e51764a AB |
273 | { |
274 | struct inode *inode; | |
275 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
276 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
277 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
278 | .dirtied_ino = 1 }; | |
279 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
280 | ||
281 | /* | |
282 | * Budget request settings: new inode, new direntry, changing the | |
283 | * parent directory inode. | |
284 | */ | |
285 | ||
4cb2a01d AV |
286 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
287 | dentry, mode, dir->i_ino); | |
1e51764a AB |
288 | |
289 | err = ubifs_budget_space(c, &req); | |
290 | if (err) | |
291 | return err; | |
292 | ||
293 | inode = ubifs_new_inode(c, dir, mode); | |
294 | if (IS_ERR(inode)) { | |
295 | err = PTR_ERR(inode); | |
296 | goto out_budg; | |
297 | } | |
298 | ||
d7f0b70d SN |
299 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
300 | if (err) | |
9401a795 | 301 | goto out_inode; |
d7f0b70d | 302 | |
1e51764a AB |
303 | mutex_lock(&dir_ui->ui_mutex); |
304 | dir->i_size += sz_change; | |
305 | dir_ui->ui_size = dir->i_size; | |
306 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
307 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
308 | if (err) | |
309 | goto out_cancel; | |
310 | mutex_unlock(&dir_ui->ui_mutex); | |
311 | ||
312 | ubifs_release_budget(c, &req); | |
313 | insert_inode_hash(inode); | |
314 | d_instantiate(dentry, inode); | |
315 | return 0; | |
316 | ||
317 | out_cancel: | |
318 | dir->i_size -= sz_change; | |
319 | dir_ui->ui_size = dir->i_size; | |
320 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 321 | out_inode: |
1e51764a AB |
322 | make_bad_inode(inode); |
323 | iput(inode); | |
324 | out_budg: | |
325 | ubifs_release_budget(c, &req); | |
235c362b | 326 | ubifs_err(c, "cannot create regular file, error %d", err); |
1e51764a AB |
327 | return err; |
328 | } | |
329 | ||
9e0a1fff RW |
330 | static int do_tmpfile(struct inode *dir, struct dentry *dentry, |
331 | umode_t mode, struct inode **whiteout) | |
474b9370 RW |
332 | { |
333 | struct inode *inode; | |
334 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
335 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1}; | |
336 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1 }; | |
337 | struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir); | |
338 | int err, instantiated = 0; | |
339 | ||
340 | /* | |
341 | * Budget request settings: new dirty inode, new direntry, | |
342 | * budget for dirtied inode will be released via writeback. | |
343 | */ | |
344 | ||
345 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", | |
346 | dentry, mode, dir->i_ino); | |
347 | ||
348 | err = ubifs_budget_space(c, &req); | |
349 | if (err) | |
350 | return err; | |
351 | ||
352 | err = ubifs_budget_space(c, &ino_req); | |
353 | if (err) { | |
354 | ubifs_release_budget(c, &req); | |
355 | return err; | |
356 | } | |
357 | ||
358 | inode = ubifs_new_inode(c, dir, mode); | |
359 | if (IS_ERR(inode)) { | |
360 | err = PTR_ERR(inode); | |
361 | goto out_budg; | |
362 | } | |
363 | ui = ubifs_inode(inode); | |
364 | ||
9e0a1fff RW |
365 | if (whiteout) { |
366 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
367 | ubifs_assert(inode->i_op == &ubifs_file_inode_operations); | |
368 | } | |
369 | ||
474b9370 RW |
370 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
371 | if (err) | |
372 | goto out_inode; | |
373 | ||
374 | mutex_lock(&ui->ui_mutex); | |
375 | insert_inode_hash(inode); | |
9e0a1fff RW |
376 | |
377 | if (whiteout) { | |
378 | mark_inode_dirty(inode); | |
379 | drop_nlink(inode); | |
380 | *whiteout = inode; | |
381 | } else { | |
382 | d_tmpfile(dentry, inode); | |
383 | } | |
474b9370 | 384 | ubifs_assert(ui->dirty); |
9e0a1fff | 385 | |
474b9370 RW |
386 | instantiated = 1; |
387 | mutex_unlock(&ui->ui_mutex); | |
388 | ||
389 | mutex_lock(&dir_ui->ui_mutex); | |
390 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
391 | if (err) | |
392 | goto out_cancel; | |
393 | mutex_unlock(&dir_ui->ui_mutex); | |
394 | ||
395 | ubifs_release_budget(c, &req); | |
396 | ||
397 | return 0; | |
398 | ||
399 | out_cancel: | |
400 | mutex_unlock(&dir_ui->ui_mutex); | |
401 | out_inode: | |
402 | make_bad_inode(inode); | |
403 | if (!instantiated) | |
404 | iput(inode); | |
405 | out_budg: | |
406 | ubifs_release_budget(c, &req); | |
407 | if (!instantiated) | |
408 | ubifs_release_budget(c, &ino_req); | |
409 | ubifs_err(c, "cannot create temporary file, error %d", err); | |
410 | return err; | |
411 | } | |
412 | ||
9e0a1fff RW |
413 | static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry, |
414 | umode_t mode) | |
415 | { | |
416 | return do_tmpfile(dir, dentry, mode, NULL); | |
417 | } | |
418 | ||
1e51764a AB |
419 | /** |
420 | * vfs_dent_type - get VFS directory entry type. | |
421 | * @type: UBIFS directory entry type | |
422 | * | |
423 | * This function converts UBIFS directory entry type into VFS directory entry | |
424 | * type. | |
425 | */ | |
426 | static unsigned int vfs_dent_type(uint8_t type) | |
427 | { | |
428 | switch (type) { | |
429 | case UBIFS_ITYPE_REG: | |
430 | return DT_REG; | |
431 | case UBIFS_ITYPE_DIR: | |
432 | return DT_DIR; | |
433 | case UBIFS_ITYPE_LNK: | |
434 | return DT_LNK; | |
435 | case UBIFS_ITYPE_BLK: | |
436 | return DT_BLK; | |
437 | case UBIFS_ITYPE_CHR: | |
438 | return DT_CHR; | |
439 | case UBIFS_ITYPE_FIFO: | |
440 | return DT_FIFO; | |
441 | case UBIFS_ITYPE_SOCK: | |
442 | return DT_SOCK; | |
443 | default: | |
444 | BUG(); | |
445 | } | |
446 | return 0; | |
447 | } | |
448 | ||
449 | /* | |
450 | * The classical Unix view for directory is that it is a linear array of | |
451 | * (name, inode number) entries. Linux/VFS assumes this model as well. | |
452 | * Particularly, 'readdir()' call wants us to return a directory entry offset | |
453 | * which later may be used to continue 'readdir()'ing the directory or to | |
454 | * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this | |
455 | * model because directory entries are identified by keys, which may collide. | |
456 | * | |
457 | * UBIFS uses directory entry hash value for directory offsets, so | |
458 | * 'seekdir()'/'telldir()' may not always work because of possible key | |
459 | * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work | |
460 | * properly by means of saving full directory entry name in the private field | |
461 | * of the file description object. | |
462 | * | |
463 | * This means that UBIFS cannot support NFS which requires full | |
464 | * 'seekdir()'/'telldir()' support. | |
465 | */ | |
01122e06 | 466 | static int ubifs_readdir(struct file *file, struct dir_context *ctx) |
1e51764a | 467 | { |
c83ed4c9 | 468 | int err = 0; |
1e51764a AB |
469 | struct qstr nm; |
470 | union ubifs_key key; | |
471 | struct ubifs_dent_node *dent; | |
496ad9aa | 472 | struct inode *dir = file_inode(file); |
1e51764a AB |
473 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
474 | ||
01122e06 | 475 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos); |
1e51764a | 476 | |
01122e06 | 477 | if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2) |
1e51764a AB |
478 | /* |
479 | * The directory was seek'ed to a senseless position or there | |
480 | * are no more entries. | |
481 | */ | |
482 | return 0; | |
483 | ||
605c912b AB |
484 | if (file->f_version == 0) { |
485 | /* | |
486 | * The file was seek'ed, which means that @file->private_data | |
487 | * is now invalid. This may also be just the first | |
488 | * 'ubifs_readdir()' invocation, in which case | |
489 | * @file->private_data is NULL, and the below code is | |
490 | * basically a no-op. | |
491 | */ | |
492 | kfree(file->private_data); | |
493 | file->private_data = NULL; | |
494 | } | |
495 | ||
496 | /* | |
497 | * 'generic_file_llseek()' unconditionally sets @file->f_version to | |
498 | * zero, and we use this for detecting whether the file was seek'ed. | |
499 | */ | |
500 | file->f_version = 1; | |
501 | ||
1e51764a | 502 | /* File positions 0 and 1 correspond to "." and ".." */ |
01122e06 | 503 | if (ctx->pos < 2) { |
1e51764a | 504 | ubifs_assert(!file->private_data); |
01122e06 | 505 | if (!dir_emit_dots(file, ctx)) |
1e51764a AB |
506 | return 0; |
507 | ||
508 | /* Find the first entry in TNC and save it */ | |
509 | lowest_dent_key(c, &key, dir->i_ino); | |
510 | nm.name = NULL; | |
511 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
512 | if (IS_ERR(dent)) { | |
513 | err = PTR_ERR(dent); | |
514 | goto out; | |
515 | } | |
516 | ||
01122e06 | 517 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
518 | file->private_data = dent; |
519 | } | |
520 | ||
521 | dent = file->private_data; | |
522 | if (!dent) { | |
523 | /* | |
524 | * The directory was seek'ed to and is now readdir'ed. | |
01122e06 | 525 | * Find the entry corresponding to @ctx->pos or the closest one. |
1e51764a | 526 | */ |
01122e06 | 527 | dent_key_init_hash(c, &key, dir->i_ino, ctx->pos); |
1e51764a AB |
528 | nm.name = NULL; |
529 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
530 | if (IS_ERR(dent)) { | |
531 | err = PTR_ERR(dent); | |
532 | goto out; | |
533 | } | |
01122e06 | 534 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
535 | file->private_data = dent; |
536 | } | |
537 | ||
538 | while (1) { | |
539 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", | |
7424bac8 | 540 | dent->name, (unsigned long long)le64_to_cpu(dent->inum), |
1e51764a | 541 | key_hash_flash(c, &dent->key)); |
0ecb9529 HH |
542 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > |
543 | ubifs_inode(dir)->creat_sqnum); | |
1e51764a AB |
544 | |
545 | nm.len = le16_to_cpu(dent->nlen); | |
01122e06 | 546 | if (!dir_emit(ctx, dent->name, nm.len, |
1e51764a | 547 | le64_to_cpu(dent->inum), |
01122e06 | 548 | vfs_dent_type(dent->type))) |
1e51764a AB |
549 | return 0; |
550 | ||
551 | /* Switch to the next entry */ | |
552 | key_read(c, &dent->key, &key); | |
553 | nm.name = dent->name; | |
554 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
555 | if (IS_ERR(dent)) { | |
556 | err = PTR_ERR(dent); | |
557 | goto out; | |
558 | } | |
559 | ||
560 | kfree(file->private_data); | |
01122e06 | 561 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
562 | file->private_data = dent; |
563 | cond_resched(); | |
564 | } | |
565 | ||
566 | out: | |
aeeb14f7 RW |
567 | kfree(file->private_data); |
568 | file->private_data = NULL; | |
569 | ||
c83ed4c9 | 570 | if (err != -ENOENT) |
235c362b | 571 | ubifs_err(c, "cannot find next direntry, error %d", err); |
a00052a2 RW |
572 | else |
573 | /* | |
574 | * -ENOENT is a non-fatal error in this context, the TNC uses | |
575 | * it to indicate that the cursor moved past the current directory | |
576 | * and readdir() has to stop. | |
577 | */ | |
578 | err = 0; | |
579 | ||
1e51764a | 580 | |
605c912b | 581 | /* 2 is a special value indicating that there are no more direntries */ |
01122e06 | 582 | ctx->pos = 2; |
c83ed4c9 | 583 | return err; |
1e51764a AB |
584 | } |
585 | ||
1e51764a AB |
586 | /* Free saved readdir() state when the directory is closed */ |
587 | static int ubifs_dir_release(struct inode *dir, struct file *file) | |
588 | { | |
589 | kfree(file->private_data); | |
590 | file->private_data = NULL; | |
591 | return 0; | |
592 | } | |
593 | ||
594 | /** | |
82c1593c | 595 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
1e51764a AB |
596 | * @inode1: first inode |
597 | * @inode2: second inode | |
82c1593c AB |
598 | * |
599 | * We do not implement any tricks to guarantee strict lock ordering, because | |
600 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
601 | * wrapper function. | |
1e51764a AB |
602 | */ |
603 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) | |
604 | { | |
82c1593c AB |
605 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
606 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1e51764a AB |
607 | } |
608 | ||
609 | /** | |
82c1593c | 610 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
1e51764a AB |
611 | * @inode1: first inode |
612 | * @inode2: second inode | |
613 | */ | |
614 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) | |
615 | { | |
1e51764a | 616 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
82c1593c | 617 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
1e51764a AB |
618 | } |
619 | ||
620 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, | |
621 | struct dentry *dentry) | |
622 | { | |
623 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 624 | struct inode *inode = d_inode(old_dentry); |
1e51764a AB |
625 | struct ubifs_inode *ui = ubifs_inode(inode); |
626 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
627 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
628 | struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2, | |
dab4b4d2 | 629 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
1e51764a AB |
630 | |
631 | /* | |
632 | * Budget request settings: new direntry, changing the target inode, | |
633 | * changing the parent inode. | |
634 | */ | |
635 | ||
4cb2a01d AV |
636 | dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu", |
637 | dentry, inode->i_ino, | |
1e51764a | 638 | inode->i_nlink, dir->i_ino); |
5955102c AV |
639 | ubifs_assert(inode_is_locked(dir)); |
640 | ubifs_assert(inode_is_locked(inode)); | |
8b3884a8 | 641 | |
d808efb4 | 642 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
643 | if (err) |
644 | return err; | |
645 | ||
646 | err = ubifs_budget_space(c, &req); | |
647 | if (err) | |
648 | return err; | |
649 | ||
650 | lock_2_inodes(dir, inode); | |
651 | inc_nlink(inode); | |
7de9c6ee | 652 | ihold(inode); |
1e51764a AB |
653 | inode->i_ctime = ubifs_current_time(inode); |
654 | dir->i_size += sz_change; | |
655 | dir_ui->ui_size = dir->i_size; | |
656 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
657 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
658 | if (err) | |
659 | goto out_cancel; | |
660 | unlock_2_inodes(dir, inode); | |
661 | ||
662 | ubifs_release_budget(c, &req); | |
663 | d_instantiate(dentry, inode); | |
664 | return 0; | |
665 | ||
666 | out_cancel: | |
667 | dir->i_size -= sz_change; | |
668 | dir_ui->ui_size = dir->i_size; | |
669 | drop_nlink(inode); | |
670 | unlock_2_inodes(dir, inode); | |
671 | ubifs_release_budget(c, &req); | |
672 | iput(inode); | |
673 | return err; | |
674 | } | |
675 | ||
676 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) | |
677 | { | |
678 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 679 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
680 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
681 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
682 | int err, budgeted = 1; | |
683 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
c43be108 | 684 | unsigned int saved_nlink = inode->i_nlink; |
1e51764a AB |
685 | |
686 | /* | |
687 | * Budget request settings: deletion direntry, deletion inode (+1 for | |
688 | * @dirtied_ino), changing the parent directory inode. If budgeting | |
689 | * fails, go ahead anyway because we have extra space reserved for | |
690 | * deletions. | |
691 | */ | |
692 | ||
4cb2a01d AV |
693 | dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu", |
694 | dentry, inode->i_ino, | |
1e51764a | 695 | inode->i_nlink, dir->i_ino); |
5955102c AV |
696 | ubifs_assert(inode_is_locked(dir)); |
697 | ubifs_assert(inode_is_locked(inode)); | |
d808efb4 | 698 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
699 | if (err) |
700 | return err; | |
701 | ||
702 | err = ubifs_budget_space(c, &req); | |
703 | if (err) { | |
704 | if (err != -ENOSPC) | |
705 | return err; | |
1e51764a AB |
706 | budgeted = 0; |
707 | } | |
708 | ||
709 | lock_2_inodes(dir, inode); | |
710 | inode->i_ctime = ubifs_current_time(dir); | |
711 | drop_nlink(inode); | |
712 | dir->i_size -= sz_change; | |
713 | dir_ui->ui_size = dir->i_size; | |
714 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
715 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
716 | if (err) | |
717 | goto out_cancel; | |
718 | unlock_2_inodes(dir, inode); | |
719 | ||
720 | if (budgeted) | |
721 | ubifs_release_budget(c, &req); | |
722 | else { | |
723 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 724 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
725 | smp_wmb(); |
726 | } | |
727 | return 0; | |
728 | ||
729 | out_cancel: | |
730 | dir->i_size += sz_change; | |
731 | dir_ui->ui_size = dir->i_size; | |
c43be108 | 732 | set_nlink(inode, saved_nlink); |
1e51764a AB |
733 | unlock_2_inodes(dir, inode); |
734 | if (budgeted) | |
735 | ubifs_release_budget(c, &req); | |
736 | return err; | |
737 | } | |
738 | ||
739 | /** | |
740 | * check_dir_empty - check if a directory is empty or not. | |
1e51764a AB |
741 | * @dir: VFS inode object of the directory to check |
742 | * | |
743 | * This function checks if directory @dir is empty. Returns zero if the | |
744 | * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes | |
745 | * in case of of errors. | |
746 | */ | |
f6337d84 | 747 | int ubifs_check_dir_empty(struct inode *dir) |
1e51764a | 748 | { |
f6337d84 | 749 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
1e51764a AB |
750 | struct qstr nm = { .name = NULL }; |
751 | struct ubifs_dent_node *dent; | |
752 | union ubifs_key key; | |
753 | int err; | |
754 | ||
755 | lowest_dent_key(c, &key, dir->i_ino); | |
756 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
757 | if (IS_ERR(dent)) { | |
758 | err = PTR_ERR(dent); | |
759 | if (err == -ENOENT) | |
760 | err = 0; | |
761 | } else { | |
762 | kfree(dent); | |
763 | err = -ENOTEMPTY; | |
764 | } | |
765 | return err; | |
766 | } | |
767 | ||
768 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) | |
769 | { | |
770 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 771 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
772 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); |
773 | int err, budgeted = 1; | |
774 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
775 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
776 | ||
777 | /* | |
778 | * Budget request settings: deletion direntry, deletion inode and | |
779 | * changing the parent inode. If budgeting fails, go ahead anyway | |
780 | * because we have extra space reserved for deletions. | |
781 | */ | |
782 | ||
4cb2a01d AV |
783 | dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry, |
784 | inode->i_ino, dir->i_ino); | |
5955102c AV |
785 | ubifs_assert(inode_is_locked(dir)); |
786 | ubifs_assert(inode_is_locked(inode)); | |
f6337d84 | 787 | err = ubifs_check_dir_empty(d_inode(dentry)); |
1e51764a AB |
788 | if (err) |
789 | return err; | |
790 | ||
791 | err = ubifs_budget_space(c, &req); | |
792 | if (err) { | |
793 | if (err != -ENOSPC) | |
794 | return err; | |
795 | budgeted = 0; | |
796 | } | |
797 | ||
798 | lock_2_inodes(dir, inode); | |
799 | inode->i_ctime = ubifs_current_time(dir); | |
800 | clear_nlink(inode); | |
801 | drop_nlink(dir); | |
802 | dir->i_size -= sz_change; | |
803 | dir_ui->ui_size = dir->i_size; | |
804 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
805 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
806 | if (err) | |
807 | goto out_cancel; | |
808 | unlock_2_inodes(dir, inode); | |
809 | ||
810 | if (budgeted) | |
811 | ubifs_release_budget(c, &req); | |
812 | else { | |
813 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 814 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
815 | smp_wmb(); |
816 | } | |
817 | return 0; | |
818 | ||
819 | out_cancel: | |
820 | dir->i_size += sz_change; | |
821 | dir_ui->ui_size = dir->i_size; | |
822 | inc_nlink(dir); | |
c43be108 | 823 | set_nlink(inode, 2); |
1e51764a AB |
824 | unlock_2_inodes(dir, inode); |
825 | if (budgeted) | |
826 | ubifs_release_budget(c, &req); | |
827 | return err; | |
828 | } | |
829 | ||
18bb1db3 | 830 | static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1e51764a AB |
831 | { |
832 | struct inode *inode; | |
833 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
834 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
835 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
182854b4 | 836 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
1e51764a AB |
837 | |
838 | /* | |
839 | * Budget request settings: new inode, new direntry and changing parent | |
840 | * directory inode. | |
841 | */ | |
842 | ||
4cb2a01d AV |
843 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
844 | dentry, mode, dir->i_ino); | |
1e51764a AB |
845 | |
846 | err = ubifs_budget_space(c, &req); | |
847 | if (err) | |
848 | return err; | |
849 | ||
850 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); | |
851 | if (IS_ERR(inode)) { | |
852 | err = PTR_ERR(inode); | |
853 | goto out_budg; | |
854 | } | |
855 | ||
d7f0b70d SN |
856 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
857 | if (err) | |
9401a795 | 858 | goto out_inode; |
d7f0b70d | 859 | |
1e51764a AB |
860 | mutex_lock(&dir_ui->ui_mutex); |
861 | insert_inode_hash(inode); | |
862 | inc_nlink(inode); | |
863 | inc_nlink(dir); | |
864 | dir->i_size += sz_change; | |
865 | dir_ui->ui_size = dir->i_size; | |
866 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
867 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
868 | if (err) { | |
235c362b | 869 | ubifs_err(c, "cannot create directory, error %d", err); |
1e51764a AB |
870 | goto out_cancel; |
871 | } | |
872 | mutex_unlock(&dir_ui->ui_mutex); | |
873 | ||
874 | ubifs_release_budget(c, &req); | |
875 | d_instantiate(dentry, inode); | |
876 | return 0; | |
877 | ||
878 | out_cancel: | |
879 | dir->i_size -= sz_change; | |
880 | dir_ui->ui_size = dir->i_size; | |
881 | drop_nlink(dir); | |
882 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 883 | out_inode: |
1e51764a AB |
884 | make_bad_inode(inode); |
885 | iput(inode); | |
886 | out_budg: | |
887 | ubifs_release_budget(c, &req); | |
888 | return err; | |
889 | } | |
890 | ||
891 | static int ubifs_mknod(struct inode *dir, struct dentry *dentry, | |
1a67aafb | 892 | umode_t mode, dev_t rdev) |
1e51764a AB |
893 | { |
894 | struct inode *inode; | |
895 | struct ubifs_inode *ui; | |
896 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
897 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
898 | union ubifs_dev_desc *dev = NULL; | |
899 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
900 | int err, devlen = 0; | |
901 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
902 | .new_ino_d = ALIGN(devlen, 8), |
903 | .dirtied_ino = 1 }; | |
1e51764a AB |
904 | |
905 | /* | |
906 | * Budget request settings: new inode, new direntry and changing parent | |
907 | * directory inode. | |
908 | */ | |
909 | ||
4cb2a01d | 910 | dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino); |
1e51764a | 911 | |
1e51764a AB |
912 | if (S_ISBLK(mode) || S_ISCHR(mode)) { |
913 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); | |
914 | if (!dev) | |
915 | return -ENOMEM; | |
916 | devlen = ubifs_encode_dev(dev, rdev); | |
917 | } | |
918 | ||
919 | err = ubifs_budget_space(c, &req); | |
920 | if (err) { | |
921 | kfree(dev); | |
922 | return err; | |
923 | } | |
924 | ||
925 | inode = ubifs_new_inode(c, dir, mode); | |
926 | if (IS_ERR(inode)) { | |
927 | kfree(dev); | |
928 | err = PTR_ERR(inode); | |
929 | goto out_budg; | |
930 | } | |
931 | ||
932 | init_special_inode(inode, inode->i_mode, rdev); | |
933 | inode->i_size = ubifs_inode(inode)->ui_size = devlen; | |
934 | ui = ubifs_inode(inode); | |
935 | ui->data = dev; | |
936 | ui->data_len = devlen; | |
937 | ||
d7f0b70d SN |
938 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
939 | if (err) | |
9401a795 | 940 | goto out_inode; |
d7f0b70d | 941 | |
1e51764a AB |
942 | mutex_lock(&dir_ui->ui_mutex); |
943 | dir->i_size += sz_change; | |
944 | dir_ui->ui_size = dir->i_size; | |
945 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
946 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
947 | if (err) | |
948 | goto out_cancel; | |
949 | mutex_unlock(&dir_ui->ui_mutex); | |
950 | ||
951 | ubifs_release_budget(c, &req); | |
952 | insert_inode_hash(inode); | |
953 | d_instantiate(dentry, inode); | |
954 | return 0; | |
955 | ||
956 | out_cancel: | |
957 | dir->i_size -= sz_change; | |
958 | dir_ui->ui_size = dir->i_size; | |
959 | mutex_unlock(&dir_ui->ui_mutex); | |
9401a795 | 960 | out_inode: |
1e51764a AB |
961 | make_bad_inode(inode); |
962 | iput(inode); | |
963 | out_budg: | |
964 | ubifs_release_budget(c, &req); | |
965 | return err; | |
966 | } | |
967 | ||
968 | static int ubifs_symlink(struct inode *dir, struct dentry *dentry, | |
969 | const char *symname) | |
970 | { | |
971 | struct inode *inode; | |
972 | struct ubifs_inode *ui; | |
973 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
974 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
975 | int err, len = strlen(symname); | |
976 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
977 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
978 | .new_ino_d = ALIGN(len, 8), |
979 | .dirtied_ino = 1 }; | |
1e51764a AB |
980 | |
981 | /* | |
982 | * Budget request settings: new inode, new direntry and changing parent | |
983 | * directory inode. | |
984 | */ | |
985 | ||
4cb2a01d AV |
986 | dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry, |
987 | symname, dir->i_ino); | |
1e51764a AB |
988 | |
989 | if (len > UBIFS_MAX_INO_DATA) | |
990 | return -ENAMETOOLONG; | |
991 | ||
992 | err = ubifs_budget_space(c, &req); | |
993 | if (err) | |
994 | return err; | |
995 | ||
996 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); | |
997 | if (IS_ERR(inode)) { | |
998 | err = PTR_ERR(inode); | |
999 | goto out_budg; | |
1000 | } | |
1001 | ||
1002 | ui = ubifs_inode(inode); | |
1003 | ui->data = kmalloc(len + 1, GFP_NOFS); | |
1004 | if (!ui->data) { | |
1005 | err = -ENOMEM; | |
1006 | goto out_inode; | |
1007 | } | |
1008 | ||
1009 | memcpy(ui->data, symname, len); | |
1010 | ((char *)ui->data)[len] = '\0'; | |
0f301bd3 | 1011 | inode->i_link = ui->data; |
1e51764a AB |
1012 | /* |
1013 | * The terminating zero byte is not written to the flash media and it | |
1014 | * is put just to make later in-memory string processing simpler. Thus, | |
1015 | * data length is @len, not @len + %1. | |
1016 | */ | |
1017 | ui->data_len = len; | |
1018 | inode->i_size = ubifs_inode(inode)->ui_size = len; | |
1019 | ||
d7f0b70d SN |
1020 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
1021 | if (err) | |
9401a795 | 1022 | goto out_inode; |
d7f0b70d | 1023 | |
1e51764a AB |
1024 | mutex_lock(&dir_ui->ui_mutex); |
1025 | dir->i_size += sz_change; | |
1026 | dir_ui->ui_size = dir->i_size; | |
1027 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
1028 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
1029 | if (err) | |
1030 | goto out_cancel; | |
1031 | mutex_unlock(&dir_ui->ui_mutex); | |
1032 | ||
1033 | ubifs_release_budget(c, &req); | |
1034 | insert_inode_hash(inode); | |
1035 | d_instantiate(dentry, inode); | |
1036 | return 0; | |
1037 | ||
1038 | out_cancel: | |
1039 | dir->i_size -= sz_change; | |
1040 | dir_ui->ui_size = dir->i_size; | |
1041 | mutex_unlock(&dir_ui->ui_mutex); | |
1042 | out_inode: | |
1043 | make_bad_inode(inode); | |
1044 | iput(inode); | |
1045 | out_budg: | |
1046 | ubifs_release_budget(c, &req); | |
1047 | return err; | |
1048 | } | |
1049 | ||
1050 | /** | |
9e0a1fff | 1051 | * lock_4_inodes - a wrapper for locking three UBIFS inodes. |
1e51764a AB |
1052 | * @inode1: first inode |
1053 | * @inode2: second inode | |
1054 | * @inode3: third inode | |
9e0a1fff | 1055 | * @inode4: fouth inode |
1e51764a | 1056 | * |
82c1593c | 1057 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
9e0a1fff | 1058 | * @inode2 whereas @inode3 and @inode4 may be %NULL. |
82c1593c AB |
1059 | * |
1060 | * We do not implement any tricks to guarantee strict lock ordering, because | |
1061 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
1062 | * wrapper function. | |
1e51764a | 1063 | */ |
9e0a1fff RW |
1064 | static void lock_4_inodes(struct inode *inode1, struct inode *inode2, |
1065 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1066 | { |
82c1593c AB |
1067 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
1068 | if (inode2 != inode1) | |
1069 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1070 | if (inode3) | |
1071 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); | |
9e0a1fff RW |
1072 | if (inode4) |
1073 | mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4); | |
1e51764a AB |
1074 | } |
1075 | ||
1076 | /** | |
9e0a1fff | 1077 | * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
1e51764a AB |
1078 | * @inode1: first inode |
1079 | * @inode2: second inode | |
1080 | * @inode3: third inode | |
9e0a1fff | 1081 | * @inode4: fouth inode |
1e51764a | 1082 | */ |
9e0a1fff RW |
1083 | static void unlock_4_inodes(struct inode *inode1, struct inode *inode2, |
1084 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1085 | { |
9e0a1fff RW |
1086 | if (inode4) |
1087 | mutex_unlock(&ubifs_inode(inode4)->ui_mutex); | |
1e51764a AB |
1088 | if (inode3) |
1089 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); | |
82c1593c AB |
1090 | if (inode1 != inode2) |
1091 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | |
1092 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | |
1e51764a AB |
1093 | } |
1094 | ||
390975ac RW |
1095 | static int do_rename(struct inode *old_dir, struct dentry *old_dentry, |
1096 | struct inode *new_dir, struct dentry *new_dentry, | |
1097 | unsigned int flags) | |
1e51764a AB |
1098 | { |
1099 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
2b0143b5 DH |
1100 | struct inode *old_inode = d_inode(old_dentry); |
1101 | struct inode *new_inode = d_inode(new_dentry); | |
9e0a1fff | 1102 | struct inode *whiteout = NULL; |
1e51764a | 1103 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); |
9e0a1fff | 1104 | struct ubifs_inode *whiteout_ui = NULL; |
1e51764a AB |
1105 | int err, release, sync = 0, move = (new_dir != old_dir); |
1106 | int is_dir = S_ISDIR(old_inode->i_mode); | |
1107 | int unlink = !!new_inode; | |
1108 | int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len); | |
1109 | int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len); | |
1110 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, | |
1111 | .dirtied_ino = 3 }; | |
1112 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1, | |
dab4b4d2 | 1113 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
1e51764a | 1114 | struct timespec time; |
782759b9 | 1115 | unsigned int uninitialized_var(saved_nlink); |
1e51764a | 1116 | |
f03b8ad8 MS |
1117 | if (flags & ~RENAME_NOREPLACE) |
1118 | return -EINVAL; | |
1119 | ||
1e51764a AB |
1120 | /* |
1121 | * Budget request settings: deletion direntry, new direntry, removing | |
1122 | * the old inode, and changing old and new parent directory inodes. | |
1123 | * | |
1124 | * However, this operation also marks the target inode as dirty and | |
1125 | * does not write it, so we allocate budget for the target inode | |
1126 | * separately. | |
1127 | */ | |
1128 | ||
9e0a1fff | 1129 | dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x", |
4cb2a01d | 1130 | old_dentry, old_inode->i_ino, old_dir->i_ino, |
9e0a1fff RW |
1131 | new_dentry, new_dir->i_ino, flags); |
1132 | ||
82c1593c | 1133 | if (unlink) |
5955102c | 1134 | ubifs_assert(inode_is_locked(new_inode)); |
82c1593c | 1135 | |
1e51764a | 1136 | if (unlink && is_dir) { |
f6337d84 | 1137 | err = ubifs_check_dir_empty(new_inode); |
1e51764a AB |
1138 | if (err) |
1139 | return err; | |
1140 | } | |
1141 | ||
1142 | err = ubifs_budget_space(c, &req); | |
1143 | if (err) | |
1144 | return err; | |
1145 | err = ubifs_budget_space(c, &ino_req); | |
1146 | if (err) { | |
1147 | ubifs_release_budget(c, &req); | |
1148 | return err; | |
1149 | } | |
1150 | ||
9e0a1fff RW |
1151 | if (flags & RENAME_WHITEOUT) { |
1152 | union ubifs_dev_desc *dev = NULL; | |
1153 | ||
1154 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); | |
1155 | if (!dev) { | |
1156 | ubifs_release_budget(c, &req); | |
1157 | ubifs_release_budget(c, &ino_req); | |
1158 | return -ENOMEM; | |
1159 | } | |
1160 | ||
1161 | err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout); | |
1162 | if (err) { | |
1163 | ubifs_release_budget(c, &req); | |
1164 | ubifs_release_budget(c, &ino_req); | |
1165 | kfree(dev); | |
1166 | return err; | |
1167 | } | |
1168 | ||
1169 | whiteout->i_state |= I_LINKABLE; | |
1170 | whiteout_ui = ubifs_inode(whiteout); | |
1171 | whiteout_ui->data = dev; | |
1172 | whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); | |
1173 | ubifs_assert(!whiteout_ui->dirty); | |
1174 | } | |
1175 | ||
1176 | lock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
1e51764a AB |
1177 | |
1178 | /* | |
1179 | * Like most other Unix systems, set the @i_ctime for inodes on a | |
1180 | * rename. | |
1181 | */ | |
1182 | time = ubifs_current_time(old_dir); | |
1183 | old_inode->i_ctime = time; | |
1184 | ||
1185 | /* We must adjust parent link count when renaming directories */ | |
1186 | if (is_dir) { | |
1187 | if (move) { | |
1188 | /* | |
1189 | * @old_dir loses a link because we are moving | |
1190 | * @old_inode to a different directory. | |
1191 | */ | |
1192 | drop_nlink(old_dir); | |
1193 | /* | |
1194 | * @new_dir only gains a link if we are not also | |
1195 | * overwriting an existing directory. | |
1196 | */ | |
1197 | if (!unlink) | |
1198 | inc_nlink(new_dir); | |
1199 | } else { | |
1200 | /* | |
1201 | * @old_inode is not moving to a different directory, | |
1202 | * but @old_dir still loses a link if we are | |
1203 | * overwriting an existing directory. | |
1204 | */ | |
1205 | if (unlink) | |
1206 | drop_nlink(old_dir); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | old_dir->i_size -= old_sz; | |
1211 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1212 | old_dir->i_mtime = old_dir->i_ctime = time; | |
1213 | new_dir->i_mtime = new_dir->i_ctime = time; | |
1214 | ||
1215 | /* | |
1216 | * And finally, if we unlinked a direntry which happened to have the | |
1217 | * same name as the moved direntry, we have to decrement @i_nlink of | |
1218 | * the unlinked inode and change its ctime. | |
1219 | */ | |
1220 | if (unlink) { | |
1221 | /* | |
1222 | * Directories cannot have hard-links, so if this is a | |
c43be108 | 1223 | * directory, just clear @i_nlink. |
1e51764a | 1224 | */ |
c43be108 | 1225 | saved_nlink = new_inode->i_nlink; |
1e51764a | 1226 | if (is_dir) |
c43be108 AB |
1227 | clear_nlink(new_inode); |
1228 | else | |
1e51764a AB |
1229 | drop_nlink(new_inode); |
1230 | new_inode->i_ctime = time; | |
1e51764a AB |
1231 | } else { |
1232 | new_dir->i_size += new_sz; | |
1233 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1234 | } | |
1235 | ||
1236 | /* | |
1237 | * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode | |
1238 | * is dirty, because this will be done later on at the end of | |
1239 | * 'ubifs_rename()'. | |
1240 | */ | |
1241 | if (IS_SYNC(old_inode)) { | |
1242 | sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); | |
1243 | if (unlink && IS_SYNC(new_inode)) | |
1244 | sync = 1; | |
1245 | } | |
9e0a1fff RW |
1246 | |
1247 | if (whiteout) { | |
1248 | struct ubifs_budget_req wht_req = { .dirtied_ino = 1, | |
1249 | .dirtied_ino_d = \ | |
1250 | ALIGN(ubifs_inode(whiteout)->data_len, 8) }; | |
1251 | ||
1252 | err = ubifs_budget_space(c, &wht_req); | |
1253 | if (err) { | |
1254 | ubifs_release_budget(c, &req); | |
1255 | ubifs_release_budget(c, &ino_req); | |
1256 | kfree(whiteout_ui->data); | |
1257 | whiteout_ui->data_len = 0; | |
1258 | iput(whiteout); | |
1259 | return err; | |
1260 | } | |
1261 | ||
1262 | inc_nlink(whiteout); | |
1263 | mark_inode_dirty(whiteout); | |
1264 | whiteout->i_state &= ~I_LINKABLE; | |
1265 | iput(whiteout); | |
1266 | } | |
1267 | ||
1268 | err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, whiteout, | |
1e51764a AB |
1269 | sync); |
1270 | if (err) | |
1271 | goto out_cancel; | |
1272 | ||
9e0a1fff | 1273 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
1e51764a AB |
1274 | ubifs_release_budget(c, &req); |
1275 | ||
1276 | mutex_lock(&old_inode_ui->ui_mutex); | |
1277 | release = old_inode_ui->dirty; | |
1278 | mark_inode_dirty_sync(old_inode); | |
1279 | mutex_unlock(&old_inode_ui->ui_mutex); | |
1280 | ||
1281 | if (release) | |
1282 | ubifs_release_budget(c, &ino_req); | |
1283 | if (IS_SYNC(old_inode)) | |
a9185b41 | 1284 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
1e51764a AB |
1285 | return err; |
1286 | ||
1287 | out_cancel: | |
1288 | if (unlink) { | |
c43be108 | 1289 | set_nlink(new_inode, saved_nlink); |
1e51764a AB |
1290 | } else { |
1291 | new_dir->i_size -= new_sz; | |
1292 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1293 | } | |
1294 | old_dir->i_size += old_sz; | |
1295 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1296 | if (is_dir) { | |
1297 | if (move) { | |
1298 | inc_nlink(old_dir); | |
1299 | if (!unlink) | |
1300 | drop_nlink(new_dir); | |
1301 | } else { | |
1302 | if (unlink) | |
1303 | inc_nlink(old_dir); | |
1304 | } | |
1305 | } | |
9e0a1fff RW |
1306 | if (whiteout) { |
1307 | drop_nlink(whiteout); | |
1308 | iput(whiteout); | |
1309 | } | |
1310 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
1e51764a AB |
1311 | ubifs_release_budget(c, &ino_req); |
1312 | ubifs_release_budget(c, &req); | |
1313 | return err; | |
1314 | } | |
1315 | ||
9ec64962 RW |
1316 | static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry, |
1317 | struct inode *new_dir, struct dentry *new_dentry) | |
1318 | { | |
1319 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
1320 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, | |
1321 | .dirtied_ino = 2 }; | |
1322 | int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); | |
1323 | struct inode *fst_inode = d_inode(old_dentry); | |
1324 | struct inode *snd_inode = d_inode(new_dentry); | |
1325 | struct timespec time; | |
1326 | int err; | |
1327 | ||
1328 | ubifs_assert(fst_inode && snd_inode); | |
1329 | ||
1330 | lock_4_inodes(old_dir, new_dir, NULL, NULL); | |
1331 | ||
1332 | time = ubifs_current_time(old_dir); | |
1333 | fst_inode->i_ctime = time; | |
1334 | snd_inode->i_ctime = time; | |
1335 | old_dir->i_mtime = old_dir->i_ctime = time; | |
1336 | new_dir->i_mtime = new_dir->i_ctime = time; | |
1337 | ||
1338 | if (old_dir != new_dir) { | |
1339 | if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) { | |
1340 | inc_nlink(new_dir); | |
1341 | drop_nlink(old_dir); | |
1342 | } | |
1343 | else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) { | |
1344 | drop_nlink(new_dir); | |
1345 | inc_nlink(old_dir); | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry, | |
1350 | sync); | |
1351 | ||
1352 | unlock_4_inodes(old_dir, new_dir, NULL, NULL); | |
1353 | ubifs_release_budget(c, &req); | |
1354 | ||
1355 | return err; | |
1356 | } | |
1357 | ||
390975ac | 1358 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9ec64962 RW |
1359 | struct inode *new_dir, struct dentry *new_dentry, |
1360 | unsigned int flags) | |
1361 | { | |
1362 | if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE)) | |
1363 | return -EINVAL; | |
1364 | ||
1365 | ubifs_assert(inode_is_locked(old_dir)); | |
1366 | ubifs_assert(inode_is_locked(new_dir)); | |
1367 | ||
1368 | if (flags & RENAME_EXCHANGE) | |
1369 | return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry); | |
1370 | ||
390975ac | 1371 | return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
9ec64962 RW |
1372 | } |
1373 | ||
1e51764a AB |
1374 | int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
1375 | struct kstat *stat) | |
1376 | { | |
1377 | loff_t size; | |
2b0143b5 | 1378 | struct inode *inode = d_inode(dentry); |
1e51764a AB |
1379 | struct ubifs_inode *ui = ubifs_inode(inode); |
1380 | ||
1381 | mutex_lock(&ui->ui_mutex); | |
6d42e7e9 | 1382 | generic_fillattr(inode, stat); |
1e51764a AB |
1383 | stat->blksize = UBIFS_BLOCK_SIZE; |
1384 | stat->size = ui->ui_size; | |
1385 | ||
1386 | /* | |
1387 | * Unfortunately, the 'stat()' system call was designed for block | |
1388 | * device based file systems, and it is not appropriate for UBIFS, | |
1389 | * because UBIFS does not have notion of "block". For example, it is | |
1390 | * difficult to tell how many block a directory takes - it actually | |
1391 | * takes less than 300 bytes, but we have to round it to block size, | |
1392 | * which introduces large mistake. This makes utilities like 'du' to | |
1393 | * report completely senseless numbers. This is the reason why UBIFS | |
1394 | * goes the same way as JFFS2 - it reports zero blocks for everything | |
1395 | * but regular files, which makes more sense than reporting completely | |
1396 | * wrong sizes. | |
1397 | */ | |
1398 | if (S_ISREG(inode->i_mode)) { | |
1399 | size = ui->xattr_size; | |
1400 | size += stat->size; | |
1401 | size = ALIGN(size, UBIFS_BLOCK_SIZE); | |
1402 | /* | |
1403 | * Note, user-space expects 512-byte blocks count irrespectively | |
1404 | * of what was reported in @stat->size. | |
1405 | */ | |
1406 | stat->blocks = size >> 9; | |
1407 | } else | |
1408 | stat->blocks = 0; | |
1409 | mutex_unlock(&ui->ui_mutex); | |
1410 | return 0; | |
1411 | } | |
1412 | ||
e8b81566 | 1413 | const struct inode_operations ubifs_dir_inode_operations = { |
1e51764a AB |
1414 | .lookup = ubifs_lookup, |
1415 | .create = ubifs_create, | |
1416 | .link = ubifs_link, | |
1417 | .symlink = ubifs_symlink, | |
1418 | .unlink = ubifs_unlink, | |
1419 | .mkdir = ubifs_mkdir, | |
1420 | .rmdir = ubifs_rmdir, | |
1421 | .mknod = ubifs_mknod, | |
390975ac | 1422 | .rename = ubifs_rename, |
1e51764a AB |
1423 | .setattr = ubifs_setattr, |
1424 | .getattr = ubifs_getattr, | |
1e51764a | 1425 | .listxattr = ubifs_listxattr, |
8c1c5f26 DY |
1426 | #ifdef CONFIG_UBIFS_ATIME_SUPPORT |
1427 | .update_time = ubifs_update_time, | |
1428 | #endif | |
474b9370 | 1429 | .tmpfile = ubifs_tmpfile, |
1e51764a AB |
1430 | }; |
1431 | ||
e8b81566 | 1432 | const struct file_operations ubifs_dir_operations = { |
01122e06 | 1433 | .llseek = generic_file_llseek, |
1e51764a AB |
1434 | .release = ubifs_dir_release, |
1435 | .read = generic_read_dir, | |
c51da20c | 1436 | .iterate_shared = ubifs_readdir, |
1e51764a AB |
1437 | .fsync = ubifs_fsync, |
1438 | .unlocked_ioctl = ubifs_ioctl, | |
1439 | #ifdef CONFIG_COMPAT | |
1440 | .compat_ioctl = ubifs_compat_ioctl, | |
1441 | #endif | |
1442 | }; |