1 // SPDX-License-Identifier: GPL-2.0-only
2 /* * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
7 * Authors: Artem Bityutskiy (Битюцкий Артём)
13 * This file implements directory operations.
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.
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.
34 * inherit_flags - inherit flags of the parent inode.
36 * @mode: new inode mode flags
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.
45 * This function returns the inherited flags.
47 static int inherit_flags(const struct inode *dir, umode_t mode)
50 const struct ubifs_inode *ui = ubifs_inode(dir);
52 if (!S_ISDIR(dir->i_mode))
54 * The parent is not a directory, which means that an extended
55 * attribute inode is being created. No flags.
59 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
61 /* The "DIRSYNC" flag only applies to directories */
62 flags &= ~UBIFS_DIRSYNC_FL;
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
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
76 struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
81 struct ubifs_inode *ui;
82 bool encrypted = false;
84 inode = new_inode(c->vfs_sb);
85 ui = ubifs_inode(inode);
87 return ERR_PTR(-ENOMEM);
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.
95 inode->i_flags |= S_NOCMTIME;
97 inode_init_owner(inode, dir, mode);
98 inode->i_mtime = inode->i_atime = inode->i_ctime =
100 inode->i_mapping->nrpages = 0;
102 err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
104 ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
108 switch (mode & S_IFMT) {
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;
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;
120 inode->i_op = &ubifs_symlink_inode_operations;
126 inode->i_op = &ubifs_file_inode_operations;
132 ui->flags = inherit_flags(dir, mode);
133 ubifs_set_inode_flags(inode);
135 ui->compr_type = c->default_compr;
137 ui->compr_type = UBIFS_COMPR_NONE;
138 ui->synced_i_size = 0;
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);
145 ubifs_err(c, "out of inode numbers");
149 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
150 (unsigned long)c->highest_inum, INUM_WATERMARK);
153 inode->i_ino = ++c->highest_inum;
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.
161 ui->creat_sqnum = ++c->max_sqnum;
162 spin_unlock(&c->cnt_lock);
165 err = fscrypt_set_context(inode, NULL);
167 ubifs_err(c, "fscrypt_set_context failed: %i", err);
175 make_bad_inode(inode);
180 static int dbg_check_name(const struct ubifs_info *c,
181 const struct ubifs_dent_node *dent,
182 const struct fscrypt_name *nm)
184 if (!dbg_is_chk_gen(c))
186 if (le16_to_cpu(dent->nlen) != fname_len(nm))
188 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
193 static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
198 struct inode *inode = NULL;
199 struct ubifs_dent_node *dent = NULL;
200 struct ubifs_info *c = dir->i_sb->s_fs_info;
201 struct fscrypt_name nm;
203 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
205 err = fscrypt_prepare_lookup(dir, dentry, &nm);
207 return d_splice_alias(NULL, dentry);
211 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
212 inode = ERR_PTR(-ENAMETOOLONG);
216 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
218 inode = ERR_PTR(-ENOMEM);
222 if (fname_name(&nm) == NULL) {
223 if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
224 goto done; /* ENOENT */
225 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
226 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
228 dent_key_init(c, &key, dir->i_ino, &nm);
229 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
234 dbg_gen("not found");
236 inode = ERR_PTR(err);
240 if (dbg_check_name(c, dent, &nm)) {
241 inode = ERR_PTR(-EINVAL);
245 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
248 * This should not happen. Probably the file-system needs
251 err = PTR_ERR(inode);
252 ubifs_err(c, "dead directory entry '%pd', error %d",
254 ubifs_ro_mode(c, err);
258 if (IS_ENCRYPTED(dir) &&
259 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
260 !fscrypt_has_permitted_context(dir, inode)) {
261 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
262 dir->i_ino, inode->i_ino);
264 inode = ERR_PTR(-EPERM);
269 fscrypt_free_filename(&nm);
270 return d_splice_alias(inode, dentry);
273 static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
277 struct ubifs_info *c = dir->i_sb->s_fs_info;
278 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
280 struct ubifs_inode *dir_ui = ubifs_inode(dir);
281 struct fscrypt_name nm;
285 * Budget request settings: new inode, new direntry, changing the
286 * parent directory inode.
289 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
290 dentry, mode, dir->i_ino);
292 err = ubifs_budget_space(c, &req);
296 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
300 sz_change = CALC_DENT_SIZE(fname_len(&nm));
302 inode = ubifs_new_inode(c, dir, mode);
304 err = PTR_ERR(inode);
308 err = ubifs_init_security(dir, inode, &dentry->d_name);
312 mutex_lock(&dir_ui->ui_mutex);
313 dir->i_size += sz_change;
314 dir_ui->ui_size = dir->i_size;
315 dir->i_mtime = dir->i_ctime = inode->i_ctime;
316 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
319 mutex_unlock(&dir_ui->ui_mutex);
321 ubifs_release_budget(c, &req);
322 fscrypt_free_filename(&nm);
323 insert_inode_hash(inode);
324 d_instantiate(dentry, inode);
328 dir->i_size -= sz_change;
329 dir_ui->ui_size = dir->i_size;
330 mutex_unlock(&dir_ui->ui_mutex);
332 make_bad_inode(inode);
335 fscrypt_free_filename(&nm);
337 ubifs_release_budget(c, &req);
338 ubifs_err(c, "cannot create regular file, error %d", err);
342 static int do_tmpfile(struct inode *dir, struct dentry *dentry,
343 umode_t mode, struct inode **whiteout)
346 struct ubifs_info *c = dir->i_sb->s_fs_info;
347 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
348 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
349 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
350 int err, instantiated = 0;
351 struct fscrypt_name nm;
354 * Budget request settings: new dirty inode, new direntry,
355 * budget for dirtied inode will be released via writeback.
358 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
359 dentry, mode, dir->i_ino);
361 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
365 err = ubifs_budget_space(c, &req);
367 fscrypt_free_filename(&nm);
371 err = ubifs_budget_space(c, &ino_req);
373 ubifs_release_budget(c, &req);
374 fscrypt_free_filename(&nm);
378 inode = ubifs_new_inode(c, dir, mode);
380 err = PTR_ERR(inode);
383 ui = ubifs_inode(inode);
386 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
387 ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
390 err = ubifs_init_security(dir, inode, &dentry->d_name);
394 mutex_lock(&ui->ui_mutex);
395 insert_inode_hash(inode);
398 mark_inode_dirty(inode);
402 d_tmpfile(dentry, inode);
404 ubifs_assert(c, ui->dirty);
407 mutex_unlock(&ui->ui_mutex);
409 mutex_lock(&dir_ui->ui_mutex);
410 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
413 mutex_unlock(&dir_ui->ui_mutex);
415 ubifs_release_budget(c, &req);
420 mutex_unlock(&dir_ui->ui_mutex);
422 make_bad_inode(inode);
426 ubifs_release_budget(c, &req);
428 ubifs_release_budget(c, &ino_req);
429 fscrypt_free_filename(&nm);
430 ubifs_err(c, "cannot create temporary file, error %d", err);
434 static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
437 return do_tmpfile(dir, dentry, mode, NULL);
441 * vfs_dent_type - get VFS directory entry type.
442 * @type: UBIFS directory entry type
444 * This function converts UBIFS directory entry type into VFS directory entry
447 static unsigned int vfs_dent_type(uint8_t type)
450 case UBIFS_ITYPE_REG:
452 case UBIFS_ITYPE_DIR:
454 case UBIFS_ITYPE_LNK:
456 case UBIFS_ITYPE_BLK:
458 case UBIFS_ITYPE_CHR:
460 case UBIFS_ITYPE_FIFO:
462 case UBIFS_ITYPE_SOCK:
471 * The classical Unix view for directory is that it is a linear array of
472 * (name, inode number) entries. Linux/VFS assumes this model as well.
473 * Particularly, 'readdir()' call wants us to return a directory entry offset
474 * which later may be used to continue 'readdir()'ing the directory or to
475 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
476 * model because directory entries are identified by keys, which may collide.
478 * UBIFS uses directory entry hash value for directory offsets, so
479 * 'seekdir()'/'telldir()' may not always work because of possible key
480 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
481 * properly by means of saving full directory entry name in the private field
482 * of the file description object.
484 * This means that UBIFS cannot support NFS which requires full
485 * 'seekdir()'/'telldir()' support.
487 static int ubifs_readdir(struct file *file, struct dir_context *ctx)
489 int fstr_real_len = 0, err = 0;
490 struct fscrypt_name nm;
491 struct fscrypt_str fstr = {0};
493 struct ubifs_dent_node *dent;
494 struct inode *dir = file_inode(file);
495 struct ubifs_info *c = dir->i_sb->s_fs_info;
496 bool encrypted = IS_ENCRYPTED(dir);
498 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
500 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
502 * The directory was seek'ed to a senseless position or there
503 * are no more entries.
508 err = fscrypt_get_encryption_info(dir);
512 err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
516 fstr_real_len = fstr.len;
519 if (file->f_version == 0) {
521 * The file was seek'ed, which means that @file->private_data
522 * is now invalid. This may also be just the first
523 * 'ubifs_readdir()' invocation, in which case
524 * @file->private_data is NULL, and the below code is
527 kfree(file->private_data);
528 file->private_data = NULL;
532 * 'generic_file_llseek()' unconditionally sets @file->f_version to
533 * zero, and we use this for detecting whether the file was seek'ed.
537 /* File positions 0 and 1 correspond to "." and ".." */
539 ubifs_assert(c, !file->private_data);
540 if (!dir_emit_dots(file, ctx)) {
542 fscrypt_fname_free_buffer(&fstr);
546 /* Find the first entry in TNC and save it */
547 lowest_dent_key(c, &key, dir->i_ino);
549 dent = ubifs_tnc_next_ent(c, &key, &nm);
555 ctx->pos = key_hash_flash(c, &dent->key);
556 file->private_data = dent;
559 dent = file->private_data;
562 * The directory was seek'ed to and is now readdir'ed.
563 * Find the entry corresponding to @ctx->pos or the closest one.
565 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
567 dent = ubifs_tnc_next_ent(c, &key, &nm);
572 ctx->pos = key_hash_flash(c, &dent->key);
573 file->private_data = dent;
577 dbg_gen("ino %llu, new f_pos %#x",
578 (unsigned long long)le64_to_cpu(dent->inum),
579 key_hash_flash(c, &dent->key));
580 ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
581 ubifs_inode(dir)->creat_sqnum);
583 fname_len(&nm) = le16_to_cpu(dent->nlen);
584 fname_name(&nm) = dent->name;
587 fstr.len = fstr_real_len;
589 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
591 le32_to_cpu(dent->cookie),
592 &nm.disk_name, &fstr);
596 fstr.len = fname_len(&nm);
597 fstr.name = fname_name(&nm);
600 if (!dir_emit(ctx, fstr.name, fstr.len,
601 le64_to_cpu(dent->inum),
602 vfs_dent_type(dent->type))) {
604 fscrypt_fname_free_buffer(&fstr);
608 /* Switch to the next entry */
609 key_read(c, &dent->key, &key);
610 dent = ubifs_tnc_next_ent(c, &key, &nm);
616 kfree(file->private_data);
617 ctx->pos = key_hash_flash(c, &dent->key);
618 file->private_data = dent;
623 kfree(file->private_data);
624 file->private_data = NULL;
627 fscrypt_fname_free_buffer(&fstr);
630 ubifs_err(c, "cannot find next direntry, error %d", err);
633 * -ENOENT is a non-fatal error in this context, the TNC uses
634 * it to indicate that the cursor moved past the current directory
635 * and readdir() has to stop.
640 /* 2 is a special value indicating that there are no more direntries */
645 /* Free saved readdir() state when the directory is closed */
646 static int ubifs_dir_release(struct inode *dir, struct file *file)
648 kfree(file->private_data);
649 file->private_data = NULL;
654 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
655 * @inode1: first inode
656 * @inode2: second inode
658 * We do not implement any tricks to guarantee strict lock ordering, because
659 * VFS has already done it for us on the @i_mutex. So this is just a simple
662 static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
664 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
665 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
669 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
670 * @inode1: first inode
671 * @inode2: second inode
673 static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
675 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
676 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
679 static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
680 struct dentry *dentry)
682 struct ubifs_info *c = dir->i_sb->s_fs_info;
683 struct inode *inode = d_inode(old_dentry);
684 struct ubifs_inode *ui = ubifs_inode(inode);
685 struct ubifs_inode *dir_ui = ubifs_inode(dir);
686 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
687 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
688 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
689 struct fscrypt_name nm;
692 * Budget request settings: new direntry, changing the target inode,
693 * changing the parent inode.
696 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
697 dentry, inode->i_ino,
698 inode->i_nlink, dir->i_ino);
699 ubifs_assert(c, inode_is_locked(dir));
700 ubifs_assert(c, inode_is_locked(inode));
702 err = fscrypt_prepare_link(old_dentry, dir, dentry);
706 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
710 err = dbg_check_synced_i_size(c, inode);
714 err = ubifs_budget_space(c, &req);
718 lock_2_inodes(dir, inode);
720 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
721 if (inode->i_nlink == 0)
722 ubifs_delete_orphan(c, inode->i_ino);
726 inode->i_ctime = current_time(inode);
727 dir->i_size += sz_change;
728 dir_ui->ui_size = dir->i_size;
729 dir->i_mtime = dir->i_ctime = inode->i_ctime;
730 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
733 unlock_2_inodes(dir, inode);
735 ubifs_release_budget(c, &req);
736 d_instantiate(dentry, inode);
737 fscrypt_free_filename(&nm);
741 dir->i_size -= sz_change;
742 dir_ui->ui_size = dir->i_size;
744 if (inode->i_nlink == 0)
745 ubifs_add_orphan(c, inode->i_ino);
746 unlock_2_inodes(dir, inode);
747 ubifs_release_budget(c, &req);
750 fscrypt_free_filename(&nm);
754 static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
756 struct ubifs_info *c = dir->i_sb->s_fs_info;
757 struct inode *inode = d_inode(dentry);
758 struct ubifs_inode *dir_ui = ubifs_inode(dir);
759 int err, sz_change, budgeted = 1;
760 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
761 unsigned int saved_nlink = inode->i_nlink;
762 struct fscrypt_name nm;
765 * Budget request settings: deletion direntry, deletion inode (+1 for
766 * @dirtied_ino), changing the parent directory inode. If budgeting
767 * fails, go ahead anyway because we have extra space reserved for
771 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
772 dentry, inode->i_ino,
773 inode->i_nlink, dir->i_ino);
775 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
779 err = ubifs_purge_xattrs(inode);
783 sz_change = CALC_DENT_SIZE(fname_len(&nm));
785 ubifs_assert(c, inode_is_locked(dir));
786 ubifs_assert(c, inode_is_locked(inode));
787 err = dbg_check_synced_i_size(c, inode);
791 err = ubifs_budget_space(c, &req);
798 lock_2_inodes(dir, inode);
799 inode->i_ctime = current_time(dir);
801 dir->i_size -= sz_change;
802 dir_ui->ui_size = dir->i_size;
803 dir->i_mtime = dir->i_ctime = inode->i_ctime;
804 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
807 unlock_2_inodes(dir, inode);
810 ubifs_release_budget(c, &req);
812 /* We've deleted something - clean the "no space" flags */
813 c->bi.nospace = c->bi.nospace_rp = 0;
816 fscrypt_free_filename(&nm);
820 dir->i_size += sz_change;
821 dir_ui->ui_size = dir->i_size;
822 set_nlink(inode, saved_nlink);
823 unlock_2_inodes(dir, inode);
825 ubifs_release_budget(c, &req);
827 fscrypt_free_filename(&nm);
832 * check_dir_empty - check if a directory is empty or not.
833 * @dir: VFS inode object of the directory to check
835 * This function checks if directory @dir is empty. Returns zero if the
836 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
837 * in case of of errors.
839 int ubifs_check_dir_empty(struct inode *dir)
841 struct ubifs_info *c = dir->i_sb->s_fs_info;
842 struct fscrypt_name nm = { 0 };
843 struct ubifs_dent_node *dent;
847 lowest_dent_key(c, &key, dir->i_ino);
848 dent = ubifs_tnc_next_ent(c, &key, &nm);
860 static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
862 struct ubifs_info *c = dir->i_sb->s_fs_info;
863 struct inode *inode = d_inode(dentry);
864 int err, sz_change, budgeted = 1;
865 struct ubifs_inode *dir_ui = ubifs_inode(dir);
866 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
867 struct fscrypt_name nm;
870 * Budget request settings: deletion direntry, deletion inode and
871 * changing the parent inode. If budgeting fails, go ahead anyway
872 * because we have extra space reserved for deletions.
875 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
876 inode->i_ino, dir->i_ino);
877 ubifs_assert(c, inode_is_locked(dir));
878 ubifs_assert(c, inode_is_locked(inode));
879 err = ubifs_check_dir_empty(d_inode(dentry));
883 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
887 err = ubifs_purge_xattrs(inode);
891 sz_change = CALC_DENT_SIZE(fname_len(&nm));
893 err = ubifs_budget_space(c, &req);
900 lock_2_inodes(dir, inode);
901 inode->i_ctime = current_time(dir);
904 dir->i_size -= sz_change;
905 dir_ui->ui_size = dir->i_size;
906 dir->i_mtime = dir->i_ctime = inode->i_ctime;
907 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
910 unlock_2_inodes(dir, inode);
913 ubifs_release_budget(c, &req);
915 /* We've deleted something - clean the "no space" flags */
916 c->bi.nospace = c->bi.nospace_rp = 0;
919 fscrypt_free_filename(&nm);
923 dir->i_size += sz_change;
924 dir_ui->ui_size = dir->i_size;
927 unlock_2_inodes(dir, inode);
929 ubifs_release_budget(c, &req);
931 fscrypt_free_filename(&nm);
935 static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
938 struct ubifs_inode *dir_ui = ubifs_inode(dir);
939 struct ubifs_info *c = dir->i_sb->s_fs_info;
941 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
942 struct fscrypt_name nm;
945 * Budget request settings: new inode, new direntry and changing parent
949 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
950 dentry, mode, dir->i_ino);
952 err = ubifs_budget_space(c, &req);
956 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
960 sz_change = CALC_DENT_SIZE(fname_len(&nm));
962 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
964 err = PTR_ERR(inode);
968 err = ubifs_init_security(dir, inode, &dentry->d_name);
972 mutex_lock(&dir_ui->ui_mutex);
973 insert_inode_hash(inode);
976 dir->i_size += sz_change;
977 dir_ui->ui_size = dir->i_size;
978 dir->i_mtime = dir->i_ctime = inode->i_ctime;
979 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
981 ubifs_err(c, "cannot create directory, error %d", err);
984 mutex_unlock(&dir_ui->ui_mutex);
986 ubifs_release_budget(c, &req);
987 d_instantiate(dentry, inode);
988 fscrypt_free_filename(&nm);
992 dir->i_size -= sz_change;
993 dir_ui->ui_size = dir->i_size;
995 mutex_unlock(&dir_ui->ui_mutex);
997 make_bad_inode(inode);
1000 fscrypt_free_filename(&nm);
1002 ubifs_release_budget(c, &req);
1006 static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
1007 umode_t mode, dev_t rdev)
1009 struct inode *inode;
1010 struct ubifs_inode *ui;
1011 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1012 struct ubifs_info *c = dir->i_sb->s_fs_info;
1013 union ubifs_dev_desc *dev = NULL;
1015 int err, devlen = 0;
1016 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1018 struct fscrypt_name nm;
1021 * Budget request settings: new inode, new direntry and changing parent
1025 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1027 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1028 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1031 devlen = ubifs_encode_dev(dev, rdev);
1034 req.new_ino_d = ALIGN(devlen, 8);
1035 err = ubifs_budget_space(c, &req);
1041 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1047 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1049 inode = ubifs_new_inode(c, dir, mode);
1050 if (IS_ERR(inode)) {
1052 err = PTR_ERR(inode);
1056 init_special_inode(inode, inode->i_mode, rdev);
1057 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1058 ui = ubifs_inode(inode);
1060 ui->data_len = devlen;
1062 err = ubifs_init_security(dir, inode, &dentry->d_name);
1066 mutex_lock(&dir_ui->ui_mutex);
1067 dir->i_size += sz_change;
1068 dir_ui->ui_size = dir->i_size;
1069 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1070 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1073 mutex_unlock(&dir_ui->ui_mutex);
1075 ubifs_release_budget(c, &req);
1076 insert_inode_hash(inode);
1077 d_instantiate(dentry, inode);
1078 fscrypt_free_filename(&nm);
1082 dir->i_size -= sz_change;
1083 dir_ui->ui_size = dir->i_size;
1084 mutex_unlock(&dir_ui->ui_mutex);
1086 make_bad_inode(inode);
1089 fscrypt_free_filename(&nm);
1091 ubifs_release_budget(c, &req);
1095 static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1096 const char *symname)
1098 struct inode *inode;
1099 struct ubifs_inode *ui;
1100 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1101 struct ubifs_info *c = dir->i_sb->s_fs_info;
1102 int err, sz_change, len = strlen(symname);
1103 struct fscrypt_str disk_link;
1104 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1105 .new_ino_d = ALIGN(len, 8),
1107 struct fscrypt_name nm;
1109 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1110 symname, dir->i_ino);
1112 err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
1118 * Budget request settings: new inode, new direntry and changing parent
1121 err = ubifs_budget_space(c, &req);
1125 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1129 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1131 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1132 if (IS_ERR(inode)) {
1133 err = PTR_ERR(inode);
1137 ui = ubifs_inode(inode);
1138 ui->data = kmalloc(disk_link.len, GFP_NOFS);
1144 if (IS_ENCRYPTED(inode)) {
1145 disk_link.name = ui->data; /* encrypt directly into ui->data */
1146 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
1150 memcpy(ui->data, disk_link.name, disk_link.len);
1151 inode->i_link = ui->data;
1155 * The terminating zero byte is not written to the flash media and it
1156 * is put just to make later in-memory string processing simpler. Thus,
1157 * data length is @disk_link.len - 1, not @disk_link.len.
1159 ui->data_len = disk_link.len - 1;
1160 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1162 err = ubifs_init_security(dir, inode, &dentry->d_name);
1166 mutex_lock(&dir_ui->ui_mutex);
1167 dir->i_size += sz_change;
1168 dir_ui->ui_size = dir->i_size;
1169 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1170 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1173 mutex_unlock(&dir_ui->ui_mutex);
1175 insert_inode_hash(inode);
1176 d_instantiate(dentry, inode);
1181 dir->i_size -= sz_change;
1182 dir_ui->ui_size = dir->i_size;
1183 mutex_unlock(&dir_ui->ui_mutex);
1185 make_bad_inode(inode);
1188 fscrypt_free_filename(&nm);
1190 ubifs_release_budget(c, &req);
1195 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1196 * @inode1: first inode
1197 * @inode2: second inode
1198 * @inode3: third inode
1199 * @inode4: fouth inode
1201 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1202 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1204 * We do not implement any tricks to guarantee strict lock ordering, because
1205 * VFS has already done it for us on the @i_mutex. So this is just a simple
1208 static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1209 struct inode *inode3, struct inode *inode4)
1211 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1212 if (inode2 != inode1)
1213 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1215 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
1217 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1221 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1222 * @inode1: first inode
1223 * @inode2: second inode
1224 * @inode3: third inode
1225 * @inode4: fouth inode
1227 static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1228 struct inode *inode3, struct inode *inode4)
1231 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1233 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
1234 if (inode1 != inode2)
1235 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1236 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1239 static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1240 struct inode *new_dir, struct dentry *new_dentry,
1243 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1244 struct inode *old_inode = d_inode(old_dentry);
1245 struct inode *new_inode = d_inode(new_dentry);
1246 struct inode *whiteout = NULL;
1247 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1248 struct ubifs_inode *whiteout_ui = NULL;
1249 int err, release, sync = 0, move = (new_dir != old_dir);
1250 int is_dir = S_ISDIR(old_inode->i_mode);
1251 int unlink = !!new_inode, new_sz, old_sz;
1252 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1254 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1255 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1256 struct timespec64 time;
1257 unsigned int saved_nlink;
1258 struct fscrypt_name old_nm, new_nm;
1261 * Budget request settings: deletion direntry, new direntry, removing
1262 * the old inode, and changing old and new parent directory inodes.
1264 * However, this operation also marks the target inode as dirty and
1265 * does not write it, so we allocate budget for the target inode
1269 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1270 old_dentry, old_inode->i_ino, old_dir->i_ino,
1271 new_dentry, new_dir->i_ino, flags);
1274 ubifs_assert(c, inode_is_locked(new_inode));
1276 err = ubifs_purge_xattrs(new_inode);
1281 if (unlink && is_dir) {
1282 err = ubifs_check_dir_empty(new_inode);
1287 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1291 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1293 fscrypt_free_filename(&old_nm);
1297 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1298 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1300 err = ubifs_budget_space(c, &req);
1302 fscrypt_free_filename(&old_nm);
1303 fscrypt_free_filename(&new_nm);
1306 err = ubifs_budget_space(c, &ino_req);
1308 fscrypt_free_filename(&old_nm);
1309 fscrypt_free_filename(&new_nm);
1310 ubifs_release_budget(c, &req);
1314 if (flags & RENAME_WHITEOUT) {
1315 union ubifs_dev_desc *dev = NULL;
1317 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1323 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1329 whiteout->i_state |= I_LINKABLE;
1330 whiteout_ui = ubifs_inode(whiteout);
1331 whiteout_ui->data = dev;
1332 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1333 ubifs_assert(c, !whiteout_ui->dirty);
1336 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1339 * Like most other Unix systems, set the @i_ctime for inodes on a
1342 time = current_time(old_dir);
1343 old_inode->i_ctime = time;
1345 /* We must adjust parent link count when renaming directories */
1349 * @old_dir loses a link because we are moving
1350 * @old_inode to a different directory.
1352 drop_nlink(old_dir);
1354 * @new_dir only gains a link if we are not also
1355 * overwriting an existing directory.
1361 * @old_inode is not moving to a different directory,
1362 * but @old_dir still loses a link if we are
1363 * overwriting an existing directory.
1366 drop_nlink(old_dir);
1370 old_dir->i_size -= old_sz;
1371 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1372 old_dir->i_mtime = old_dir->i_ctime = time;
1373 new_dir->i_mtime = new_dir->i_ctime = time;
1376 * And finally, if we unlinked a direntry which happened to have the
1377 * same name as the moved direntry, we have to decrement @i_nlink of
1378 * the unlinked inode and change its ctime.
1382 * Directories cannot have hard-links, so if this is a
1383 * directory, just clear @i_nlink.
1385 saved_nlink = new_inode->i_nlink;
1387 clear_nlink(new_inode);
1389 drop_nlink(new_inode);
1390 new_inode->i_ctime = time;
1392 new_dir->i_size += new_sz;
1393 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1397 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1398 * is dirty, because this will be done later on at the end of
1401 if (IS_SYNC(old_inode)) {
1402 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1403 if (unlink && IS_SYNC(new_inode))
1408 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1410 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1412 err = ubifs_budget_space(c, &wht_req);
1414 kfree(whiteout_ui->data);
1415 whiteout_ui->data_len = 0;
1420 inc_nlink(whiteout);
1421 mark_inode_dirty(whiteout);
1422 whiteout->i_state &= ~I_LINKABLE;
1426 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1427 new_inode, &new_nm, whiteout, sync);
1431 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1432 ubifs_release_budget(c, &req);
1434 mutex_lock(&old_inode_ui->ui_mutex);
1435 release = old_inode_ui->dirty;
1436 mark_inode_dirty_sync(old_inode);
1437 mutex_unlock(&old_inode_ui->ui_mutex);
1440 ubifs_release_budget(c, &ino_req);
1441 if (IS_SYNC(old_inode))
1442 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1444 fscrypt_free_filename(&old_nm);
1445 fscrypt_free_filename(&new_nm);
1450 set_nlink(new_inode, saved_nlink);
1452 new_dir->i_size -= new_sz;
1453 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1455 old_dir->i_size += old_sz;
1456 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1461 drop_nlink(new_dir);
1468 drop_nlink(whiteout);
1471 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1473 ubifs_release_budget(c, &ino_req);
1474 ubifs_release_budget(c, &req);
1475 fscrypt_free_filename(&old_nm);
1476 fscrypt_free_filename(&new_nm);
1480 static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1481 struct inode *new_dir, struct dentry *new_dentry)
1483 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1484 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1486 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1487 struct inode *fst_inode = d_inode(old_dentry);
1488 struct inode *snd_inode = d_inode(new_dentry);
1489 struct timespec64 time;
1491 struct fscrypt_name fst_nm, snd_nm;
1493 ubifs_assert(c, fst_inode && snd_inode);
1495 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1499 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1501 fscrypt_free_filename(&fst_nm);
1505 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1507 time = current_time(old_dir);
1508 fst_inode->i_ctime = time;
1509 snd_inode->i_ctime = time;
1510 old_dir->i_mtime = old_dir->i_ctime = time;
1511 new_dir->i_mtime = new_dir->i_ctime = time;
1513 if (old_dir != new_dir) {
1514 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1516 drop_nlink(old_dir);
1518 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1519 drop_nlink(new_dir);
1524 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1525 snd_inode, &snd_nm, sync);
1527 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1528 ubifs_release_budget(c, &req);
1530 fscrypt_free_filename(&fst_nm);
1531 fscrypt_free_filename(&snd_nm);
1535 static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1536 struct inode *new_dir, struct dentry *new_dentry,
1540 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1542 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1545 ubifs_assert(c, inode_is_locked(old_dir));
1546 ubifs_assert(c, inode_is_locked(new_dir));
1548 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1553 if (flags & RENAME_EXCHANGE)
1554 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1556 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1559 int ubifs_getattr(const struct path *path, struct kstat *stat,
1560 u32 request_mask, unsigned int flags)
1563 struct inode *inode = d_inode(path->dentry);
1564 struct ubifs_inode *ui = ubifs_inode(inode);
1566 mutex_lock(&ui->ui_mutex);
1568 if (ui->flags & UBIFS_APPEND_FL)
1569 stat->attributes |= STATX_ATTR_APPEND;
1570 if (ui->flags & UBIFS_COMPR_FL)
1571 stat->attributes |= STATX_ATTR_COMPRESSED;
1572 if (ui->flags & UBIFS_CRYPT_FL)
1573 stat->attributes |= STATX_ATTR_ENCRYPTED;
1574 if (ui->flags & UBIFS_IMMUTABLE_FL)
1575 stat->attributes |= STATX_ATTR_IMMUTABLE;
1577 stat->attributes_mask |= (STATX_ATTR_APPEND |
1578 STATX_ATTR_COMPRESSED |
1579 STATX_ATTR_ENCRYPTED |
1580 STATX_ATTR_IMMUTABLE);
1582 generic_fillattr(inode, stat);
1583 stat->blksize = UBIFS_BLOCK_SIZE;
1584 stat->size = ui->ui_size;
1587 * Unfortunately, the 'stat()' system call was designed for block
1588 * device based file systems, and it is not appropriate for UBIFS,
1589 * because UBIFS does not have notion of "block". For example, it is
1590 * difficult to tell how many block a directory takes - it actually
1591 * takes less than 300 bytes, but we have to round it to block size,
1592 * which introduces large mistake. This makes utilities like 'du' to
1593 * report completely senseless numbers. This is the reason why UBIFS
1594 * goes the same way as JFFS2 - it reports zero blocks for everything
1595 * but regular files, which makes more sense than reporting completely
1598 if (S_ISREG(inode->i_mode)) {
1599 size = ui->xattr_size;
1601 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1603 * Note, user-space expects 512-byte blocks count irrespectively
1604 * of what was reported in @stat->size.
1606 stat->blocks = size >> 9;
1609 mutex_unlock(&ui->ui_mutex);
1613 static int ubifs_dir_open(struct inode *dir, struct file *file)
1615 if (IS_ENCRYPTED(dir))
1616 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1621 const struct inode_operations ubifs_dir_inode_operations = {
1622 .lookup = ubifs_lookup,
1623 .create = ubifs_create,
1625 .symlink = ubifs_symlink,
1626 .unlink = ubifs_unlink,
1627 .mkdir = ubifs_mkdir,
1628 .rmdir = ubifs_rmdir,
1629 .mknod = ubifs_mknod,
1630 .rename = ubifs_rename,
1631 .setattr = ubifs_setattr,
1632 .getattr = ubifs_getattr,
1633 #ifdef CONFIG_UBIFS_FS_XATTR
1634 .listxattr = ubifs_listxattr,
1636 .update_time = ubifs_update_time,
1637 .tmpfile = ubifs_tmpfile,
1640 const struct file_operations ubifs_dir_operations = {
1641 .llseek = generic_file_llseek,
1642 .release = ubifs_dir_release,
1643 .read = generic_read_dir,
1644 .iterate_shared = ubifs_readdir,
1645 .fsync = ubifs_fsync,
1646 .unlocked_ioctl = ubifs_ioctl,
1647 .open = ubifs_dir_open,
1648 #ifdef CONFIG_COMPAT
1649 .compat_ioctl = ubifs_compat_ioctl,