root/fs/ubifs/dir.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inherit_flags
  2. ubifs_new_inode
  3. dbg_check_name
  4. ubifs_lookup
  5. ubifs_create
  6. do_tmpfile
  7. ubifs_tmpfile
  8. vfs_dent_type
  9. ubifs_readdir
  10. ubifs_dir_release
  11. lock_2_inodes
  12. unlock_2_inodes
  13. ubifs_link
  14. ubifs_unlink
  15. ubifs_check_dir_empty
  16. ubifs_rmdir
  17. ubifs_mkdir
  18. ubifs_mknod
  19. ubifs_symlink
  20. lock_4_inodes
  21. unlock_4_inodes
  22. do_rename
  23. ubifs_xrename
  24. ubifs_rename
  25. ubifs_getattr
  26. ubifs_dir_open

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

/* [<][>][^][v][top][bottom][index][help] */