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