1 /* * This file is part of UBIFS.
3 * Copyright (C) 2006-2008 Nokia Corporation.
4 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
25 * This file implements directory operations.
27 * All FS operations in this file allocate budget before writing anything to the
28 * media. If they fail to allocate it, the error is returned. The only
29 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
30 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
31 * not what users are usually ready to get. UBIFS budgeting subsystem has some
32 * space reserved for these purposes.
34 * All operations in this file write all inodes which they change straight
35 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
36 * @i_size of the parent inode and writes the parent inode together with the
37 * target inode. This was done to simplify file-system recovery which would
38 * otherwise be very difficult to do. The only exception is rename which marks
39 * the re-named inode dirty (because its @i_ctime is updated) but does not
40 * write it, but just marks it as dirty.
46 * inherit_flags - inherit flags of the parent inode.
48 * @mode: new inode mode flags
50 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
51 * parent directory inode @dir. UBIFS inodes inherit the following flags:
52 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
53 * sub-directory basis;
54 * o %UBIFS_SYNC_FL - useful for the same reasons;
55 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
57 * This function returns the inherited flags.
59 static int inherit_flags(const struct inode *dir, umode_t mode)
62 const struct ubifs_inode *ui = ubifs_inode(dir);
64 if (!S_ISDIR(dir->i_mode))
66 * The parent is not a directory, which means that an extended
67 * attribute inode is being created. No flags.
71 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
73 /* The "DIRSYNC" flag only applies to directories */
74 flags &= ~UBIFS_DIRSYNC_FL;
79 * ubifs_new_inode - allocate new UBIFS inode object.
80 * @c: UBIFS file-system description object
81 * @dir: parent directory inode
82 * @mode: inode mode flags
84 * This function finds an unused inode number, allocates new inode and
85 * initializes it. Returns new inode in case of success and an error code in
88 struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
93 struct ubifs_inode *ui;
94 bool encrypted = false;
96 if (ubifs_crypt_is_encrypted(dir)) {
97 err = fscrypt_get_encryption_info(dir);
99 ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
103 if (!fscrypt_has_encryption_key(dir))
104 return ERR_PTR(-EPERM);
109 inode = new_inode(c->vfs_sb);
110 ui = ubifs_inode(inode);
112 return ERR_PTR(-ENOMEM);
115 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
116 * marking them dirty in file write path (see 'file_update_time()').
117 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
118 * to make budgeting work.
120 inode->i_flags |= S_NOCMTIME;
122 inode_init_owner(inode, dir, mode);
123 inode->i_mtime = inode->i_atime = inode->i_ctime =
124 ubifs_current_time(inode);
125 inode->i_mapping->nrpages = 0;
127 switch (mode & S_IFMT) {
129 inode->i_mapping->a_ops = &ubifs_file_address_operations;
130 inode->i_op = &ubifs_file_inode_operations;
131 inode->i_fop = &ubifs_file_operations;
134 inode->i_op = &ubifs_dir_inode_operations;
135 inode->i_fop = &ubifs_dir_operations;
136 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
139 inode->i_op = &ubifs_symlink_inode_operations;
145 inode->i_op = &ubifs_file_inode_operations;
151 ui->flags = inherit_flags(dir, mode);
152 ubifs_set_inode_flags(inode);
154 ui->compr_type = c->default_compr;
156 ui->compr_type = UBIFS_COMPR_NONE;
157 ui->synced_i_size = 0;
159 spin_lock(&c->cnt_lock);
160 /* Inode number overflow is currently not supported */
161 if (c->highest_inum >= INUM_WARN_WATERMARK) {
162 if (c->highest_inum >= INUM_WATERMARK) {
163 spin_unlock(&c->cnt_lock);
164 ubifs_err(c, "out of inode numbers");
165 make_bad_inode(inode);
167 return ERR_PTR(-EINVAL);
169 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
170 (unsigned long)c->highest_inum, INUM_WATERMARK);
173 inode->i_ino = ++c->highest_inum;
175 * The creation sequence number remains with this inode for its
176 * lifetime. All nodes for this inode have a greater sequence number,
177 * and so it is possible to distinguish obsolete nodes belonging to a
178 * previous incarnation of the same inode number - for example, for the
179 * purpose of rebuilding the index.
181 ui->creat_sqnum = ++c->max_sqnum;
182 spin_unlock(&c->cnt_lock);
185 err = fscrypt_inherit_context(dir, inode, &encrypted, true);
187 ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
188 make_bad_inode(inode);
197 static int dbg_check_name(const struct ubifs_info *c,
198 const struct ubifs_dent_node *dent,
199 const struct qstr *nm)
201 if (!dbg_is_chk_gen(c))
203 if (le16_to_cpu(dent->nlen) != nm->len)
205 if (memcmp(dent->name, nm->name, nm->len))
210 static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
215 struct inode *inode = NULL;
216 struct ubifs_dent_node *dent;
217 struct ubifs_info *c = dir->i_sb->s_fs_info;
219 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
221 if (dentry->d_name.len > UBIFS_MAX_NLEN)
222 return ERR_PTR(-ENAMETOOLONG);
224 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
226 return ERR_PTR(-ENOMEM);
228 dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
230 err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
232 if (err == -ENOENT) {
233 dbg_gen("not found");
239 if (dbg_check_name(c, dent, &dentry->d_name)) {
244 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
247 * This should not happen. Probably the file-system needs
250 err = PTR_ERR(inode);
251 ubifs_err(c, "dead directory entry '%pd', error %d",
253 ubifs_ro_mode(c, err);
260 * Note, d_splice_alias() would be required instead if we supported
263 d_add(dentry, inode);
271 static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
275 struct ubifs_info *c = dir->i_sb->s_fs_info;
276 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
277 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
279 struct ubifs_inode *dir_ui = ubifs_inode(dir);
282 * Budget request settings: new inode, new direntry, changing the
283 * parent directory inode.
286 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
287 dentry, mode, dir->i_ino);
289 err = ubifs_budget_space(c, &req);
293 inode = ubifs_new_inode(c, dir, mode);
295 err = PTR_ERR(inode);
299 err = ubifs_init_security(dir, inode, &dentry->d_name);
303 mutex_lock(&dir_ui->ui_mutex);
304 dir->i_size += sz_change;
305 dir_ui->ui_size = dir->i_size;
306 dir->i_mtime = dir->i_ctime = inode->i_ctime;
307 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
310 mutex_unlock(&dir_ui->ui_mutex);
312 ubifs_release_budget(c, &req);
313 insert_inode_hash(inode);
314 d_instantiate(dentry, inode);
318 dir->i_size -= sz_change;
319 dir_ui->ui_size = dir->i_size;
320 mutex_unlock(&dir_ui->ui_mutex);
322 make_bad_inode(inode);
325 ubifs_release_budget(c, &req);
326 ubifs_err(c, "cannot create regular file, error %d", err);
330 static int do_tmpfile(struct inode *dir, struct dentry *dentry,
331 umode_t mode, struct inode **whiteout)
334 struct ubifs_info *c = dir->i_sb->s_fs_info;
335 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
336 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
337 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
338 int err, instantiated = 0;
341 * Budget request settings: new dirty inode, new direntry,
342 * budget for dirtied inode will be released via writeback.
345 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
346 dentry, mode, dir->i_ino);
348 err = ubifs_budget_space(c, &req);
352 err = ubifs_budget_space(c, &ino_req);
354 ubifs_release_budget(c, &req);
358 inode = ubifs_new_inode(c, dir, mode);
360 err = PTR_ERR(inode);
363 ui = ubifs_inode(inode);
366 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
367 ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
370 err = ubifs_init_security(dir, inode, &dentry->d_name);
374 mutex_lock(&ui->ui_mutex);
375 insert_inode_hash(inode);
378 mark_inode_dirty(inode);
382 d_tmpfile(dentry, inode);
384 ubifs_assert(ui->dirty);
387 mutex_unlock(&ui->ui_mutex);
389 mutex_lock(&dir_ui->ui_mutex);
390 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
393 mutex_unlock(&dir_ui->ui_mutex);
395 ubifs_release_budget(c, &req);
400 mutex_unlock(&dir_ui->ui_mutex);
402 make_bad_inode(inode);
406 ubifs_release_budget(c, &req);
408 ubifs_release_budget(c, &ino_req);
409 ubifs_err(c, "cannot create temporary file, error %d", err);
413 static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
416 return do_tmpfile(dir, dentry, mode, NULL);
420 * vfs_dent_type - get VFS directory entry type.
421 * @type: UBIFS directory entry type
423 * This function converts UBIFS directory entry type into VFS directory entry
426 static unsigned int vfs_dent_type(uint8_t type)
429 case UBIFS_ITYPE_REG:
431 case UBIFS_ITYPE_DIR:
433 case UBIFS_ITYPE_LNK:
435 case UBIFS_ITYPE_BLK:
437 case UBIFS_ITYPE_CHR:
439 case UBIFS_ITYPE_FIFO:
441 case UBIFS_ITYPE_SOCK:
450 * The classical Unix view for directory is that it is a linear array of
451 * (name, inode number) entries. Linux/VFS assumes this model as well.
452 * Particularly, 'readdir()' call wants us to return a directory entry offset
453 * which later may be used to continue 'readdir()'ing the directory or to
454 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
455 * model because directory entries are identified by keys, which may collide.
457 * UBIFS uses directory entry hash value for directory offsets, so
458 * 'seekdir()'/'telldir()' may not always work because of possible key
459 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
460 * properly by means of saving full directory entry name in the private field
461 * of the file description object.
463 * This means that UBIFS cannot support NFS which requires full
464 * 'seekdir()'/'telldir()' support.
466 static int ubifs_readdir(struct file *file, struct dir_context *ctx)
471 struct ubifs_dent_node *dent;
472 struct inode *dir = file_inode(file);
473 struct ubifs_info *c = dir->i_sb->s_fs_info;
475 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
477 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
479 * The directory was seek'ed to a senseless position or there
480 * are no more entries.
484 if (file->f_version == 0) {
486 * The file was seek'ed, which means that @file->private_data
487 * is now invalid. This may also be just the first
488 * 'ubifs_readdir()' invocation, in which case
489 * @file->private_data is NULL, and the below code is
492 kfree(file->private_data);
493 file->private_data = NULL;
497 * 'generic_file_llseek()' unconditionally sets @file->f_version to
498 * zero, and we use this for detecting whether the file was seek'ed.
502 /* File positions 0 and 1 correspond to "." and ".." */
504 ubifs_assert(!file->private_data);
505 if (!dir_emit_dots(file, ctx))
508 /* Find the first entry in TNC and save it */
509 lowest_dent_key(c, &key, dir->i_ino);
511 dent = ubifs_tnc_next_ent(c, &key, &nm);
517 ctx->pos = key_hash_flash(c, &dent->key);
518 file->private_data = dent;
521 dent = file->private_data;
524 * The directory was seek'ed to and is now readdir'ed.
525 * Find the entry corresponding to @ctx->pos or the closest one.
527 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
529 dent = ubifs_tnc_next_ent(c, &key, &nm);
534 ctx->pos = key_hash_flash(c, &dent->key);
535 file->private_data = dent;
539 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
540 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
541 key_hash_flash(c, &dent->key));
542 ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
543 ubifs_inode(dir)->creat_sqnum);
545 nm.len = le16_to_cpu(dent->nlen);
546 if (!dir_emit(ctx, dent->name, nm.len,
547 le64_to_cpu(dent->inum),
548 vfs_dent_type(dent->type)))
551 /* Switch to the next entry */
552 key_read(c, &dent->key, &key);
553 nm.name = dent->name;
554 dent = ubifs_tnc_next_ent(c, &key, &nm);
560 kfree(file->private_data);
561 ctx->pos = key_hash_flash(c, &dent->key);
562 file->private_data = dent;
567 kfree(file->private_data);
568 file->private_data = NULL;
571 ubifs_err(c, "cannot find next direntry, error %d", err);
574 * -ENOENT is a non-fatal error in this context, the TNC uses
575 * it to indicate that the cursor moved past the current directory
576 * and readdir() has to stop.
581 /* 2 is a special value indicating that there are no more direntries */
586 /* Free saved readdir() state when the directory is closed */
587 static int ubifs_dir_release(struct inode *dir, struct file *file)
589 kfree(file->private_data);
590 file->private_data = NULL;
595 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
596 * @inode1: first inode
597 * @inode2: second inode
599 * We do not implement any tricks to guarantee strict lock ordering, because
600 * VFS has already done it for us on the @i_mutex. So this is just a simple
603 static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
605 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
606 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
610 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
611 * @inode1: first inode
612 * @inode2: second inode
614 static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
616 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
617 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
620 static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
621 struct dentry *dentry)
623 struct ubifs_info *c = dir->i_sb->s_fs_info;
624 struct inode *inode = d_inode(old_dentry);
625 struct ubifs_inode *ui = ubifs_inode(inode);
626 struct ubifs_inode *dir_ui = ubifs_inode(dir);
627 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
628 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
629 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
632 * Budget request settings: new direntry, changing the target inode,
633 * changing the parent inode.
636 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
637 dentry, inode->i_ino,
638 inode->i_nlink, dir->i_ino);
639 ubifs_assert(inode_is_locked(dir));
640 ubifs_assert(inode_is_locked(inode));
642 if (ubifs_crypt_is_encrypted(dir) &&
643 !fscrypt_has_permitted_context(dir, inode))
646 err = dbg_check_synced_i_size(c, inode);
650 err = ubifs_budget_space(c, &req);
654 lock_2_inodes(dir, inode);
657 inode->i_ctime = ubifs_current_time(inode);
658 dir->i_size += sz_change;
659 dir_ui->ui_size = dir->i_size;
660 dir->i_mtime = dir->i_ctime = inode->i_ctime;
661 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
664 unlock_2_inodes(dir, inode);
666 ubifs_release_budget(c, &req);
667 d_instantiate(dentry, inode);
671 dir->i_size -= sz_change;
672 dir_ui->ui_size = dir->i_size;
674 unlock_2_inodes(dir, inode);
675 ubifs_release_budget(c, &req);
680 static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
682 struct ubifs_info *c = dir->i_sb->s_fs_info;
683 struct inode *inode = d_inode(dentry);
684 struct ubifs_inode *dir_ui = ubifs_inode(dir);
685 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
686 int err, budgeted = 1;
687 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
688 unsigned int saved_nlink = inode->i_nlink;
691 * Budget request settings: deletion direntry, deletion inode (+1 for
692 * @dirtied_ino), changing the parent directory inode. If budgeting
693 * fails, go ahead anyway because we have extra space reserved for
697 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
698 dentry, inode->i_ino,
699 inode->i_nlink, dir->i_ino);
700 ubifs_assert(inode_is_locked(dir));
701 ubifs_assert(inode_is_locked(inode));
702 err = dbg_check_synced_i_size(c, inode);
706 err = ubifs_budget_space(c, &req);
713 lock_2_inodes(dir, inode);
714 inode->i_ctime = ubifs_current_time(dir);
716 dir->i_size -= sz_change;
717 dir_ui->ui_size = dir->i_size;
718 dir->i_mtime = dir->i_ctime = inode->i_ctime;
719 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
722 unlock_2_inodes(dir, inode);
725 ubifs_release_budget(c, &req);
727 /* We've deleted something - clean the "no space" flags */
728 c->bi.nospace = c->bi.nospace_rp = 0;
734 dir->i_size += sz_change;
735 dir_ui->ui_size = dir->i_size;
736 set_nlink(inode, saved_nlink);
737 unlock_2_inodes(dir, inode);
739 ubifs_release_budget(c, &req);
744 * check_dir_empty - check if a directory is empty or not.
745 * @dir: VFS inode object of the directory to check
747 * This function checks if directory @dir is empty. Returns zero if the
748 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
749 * in case of of errors.
751 int ubifs_check_dir_empty(struct inode *dir)
753 struct ubifs_info *c = dir->i_sb->s_fs_info;
754 struct qstr nm = { .name = NULL };
755 struct ubifs_dent_node *dent;
759 lowest_dent_key(c, &key, dir->i_ino);
760 dent = ubifs_tnc_next_ent(c, &key, &nm);
772 static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
774 struct ubifs_info *c = dir->i_sb->s_fs_info;
775 struct inode *inode = d_inode(dentry);
776 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
777 int err, budgeted = 1;
778 struct ubifs_inode *dir_ui = ubifs_inode(dir);
779 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
782 * Budget request settings: deletion direntry, deletion inode and
783 * changing the parent inode. If budgeting fails, go ahead anyway
784 * because we have extra space reserved for deletions.
787 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
788 inode->i_ino, dir->i_ino);
789 ubifs_assert(inode_is_locked(dir));
790 ubifs_assert(inode_is_locked(inode));
791 err = ubifs_check_dir_empty(d_inode(dentry));
795 err = ubifs_budget_space(c, &req);
802 lock_2_inodes(dir, inode);
803 inode->i_ctime = ubifs_current_time(dir);
806 dir->i_size -= sz_change;
807 dir_ui->ui_size = dir->i_size;
808 dir->i_mtime = dir->i_ctime = inode->i_ctime;
809 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
812 unlock_2_inodes(dir, inode);
815 ubifs_release_budget(c, &req);
817 /* We've deleted something - clean the "no space" flags */
818 c->bi.nospace = c->bi.nospace_rp = 0;
824 dir->i_size += sz_change;
825 dir_ui->ui_size = dir->i_size;
828 unlock_2_inodes(dir, inode);
830 ubifs_release_budget(c, &req);
834 static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
837 struct ubifs_inode *dir_ui = ubifs_inode(dir);
838 struct ubifs_info *c = dir->i_sb->s_fs_info;
839 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
840 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
843 * Budget request settings: new inode, new direntry and changing parent
847 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
848 dentry, mode, dir->i_ino);
850 err = ubifs_budget_space(c, &req);
854 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
856 err = PTR_ERR(inode);
860 err = ubifs_init_security(dir, inode, &dentry->d_name);
864 mutex_lock(&dir_ui->ui_mutex);
865 insert_inode_hash(inode);
868 dir->i_size += sz_change;
869 dir_ui->ui_size = dir->i_size;
870 dir->i_mtime = dir->i_ctime = inode->i_ctime;
871 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
873 ubifs_err(c, "cannot create directory, error %d", err);
876 mutex_unlock(&dir_ui->ui_mutex);
878 ubifs_release_budget(c, &req);
879 d_instantiate(dentry, inode);
883 dir->i_size -= sz_change;
884 dir_ui->ui_size = dir->i_size;
886 mutex_unlock(&dir_ui->ui_mutex);
888 make_bad_inode(inode);
891 ubifs_release_budget(c, &req);
895 static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
896 umode_t mode, dev_t rdev)
899 struct ubifs_inode *ui;
900 struct ubifs_inode *dir_ui = ubifs_inode(dir);
901 struct ubifs_info *c = dir->i_sb->s_fs_info;
902 union ubifs_dev_desc *dev = NULL;
903 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
905 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
906 .new_ino_d = ALIGN(devlen, 8),
910 * Budget request settings: new inode, new direntry and changing parent
914 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
916 if (S_ISBLK(mode) || S_ISCHR(mode)) {
917 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
920 devlen = ubifs_encode_dev(dev, rdev);
923 err = ubifs_budget_space(c, &req);
929 inode = ubifs_new_inode(c, dir, mode);
932 err = PTR_ERR(inode);
936 init_special_inode(inode, inode->i_mode, rdev);
937 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
938 ui = ubifs_inode(inode);
940 ui->data_len = devlen;
942 err = ubifs_init_security(dir, inode, &dentry->d_name);
946 mutex_lock(&dir_ui->ui_mutex);
947 dir->i_size += sz_change;
948 dir_ui->ui_size = dir->i_size;
949 dir->i_mtime = dir->i_ctime = inode->i_ctime;
950 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
953 mutex_unlock(&dir_ui->ui_mutex);
955 ubifs_release_budget(c, &req);
956 insert_inode_hash(inode);
957 d_instantiate(dentry, inode);
961 dir->i_size -= sz_change;
962 dir_ui->ui_size = dir->i_size;
963 mutex_unlock(&dir_ui->ui_mutex);
965 make_bad_inode(inode);
968 ubifs_release_budget(c, &req);
972 static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
976 struct ubifs_inode *ui;
977 struct ubifs_inode *dir_ui = ubifs_inode(dir);
978 struct ubifs_info *c = dir->i_sb->s_fs_info;
979 int err, len = strlen(symname);
980 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
981 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
982 .new_ino_d = ALIGN(len, 8),
986 * Budget request settings: new inode, new direntry and changing parent
990 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
991 symname, dir->i_ino);
993 if (len > UBIFS_MAX_INO_DATA)
994 return -ENAMETOOLONG;
996 err = ubifs_budget_space(c, &req);
1000 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1001 if (IS_ERR(inode)) {
1002 err = PTR_ERR(inode);
1006 ui = ubifs_inode(inode);
1007 ui->data = kmalloc(len + 1, GFP_NOFS);
1013 memcpy(ui->data, symname, len);
1014 ((char *)ui->data)[len] = '\0';
1015 inode->i_link = ui->data;
1017 * The terminating zero byte is not written to the flash media and it
1018 * is put just to make later in-memory string processing simpler. Thus,
1019 * data length is @len, not @len + %1.
1022 inode->i_size = ubifs_inode(inode)->ui_size = len;
1024 err = ubifs_init_security(dir, inode, &dentry->d_name);
1028 mutex_lock(&dir_ui->ui_mutex);
1029 dir->i_size += sz_change;
1030 dir_ui->ui_size = dir->i_size;
1031 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1032 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
1035 mutex_unlock(&dir_ui->ui_mutex);
1037 ubifs_release_budget(c, &req);
1038 insert_inode_hash(inode);
1039 d_instantiate(dentry, inode);
1043 dir->i_size -= sz_change;
1044 dir_ui->ui_size = dir->i_size;
1045 mutex_unlock(&dir_ui->ui_mutex);
1047 make_bad_inode(inode);
1050 ubifs_release_budget(c, &req);
1055 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1056 * @inode1: first inode
1057 * @inode2: second inode
1058 * @inode3: third inode
1059 * @inode4: fouth inode
1061 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1062 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1064 * We do not implement any tricks to guarantee strict lock ordering, because
1065 * VFS has already done it for us on the @i_mutex. So this is just a simple
1068 static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1069 struct inode *inode3, struct inode *inode4)
1071 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1072 if (inode2 != inode1)
1073 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1075 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
1077 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1081 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1082 * @inode1: first inode
1083 * @inode2: second inode
1084 * @inode3: third inode
1085 * @inode4: fouth inode
1087 static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1088 struct inode *inode3, struct inode *inode4)
1091 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1093 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
1094 if (inode1 != inode2)
1095 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1096 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1099 static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1100 struct inode *new_dir, struct dentry *new_dentry,
1103 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1104 struct inode *old_inode = d_inode(old_dentry);
1105 struct inode *new_inode = d_inode(new_dentry);
1106 struct inode *whiteout = NULL;
1107 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1108 struct ubifs_inode *whiteout_ui = NULL;
1109 int err, release, sync = 0, move = (new_dir != old_dir);
1110 int is_dir = S_ISDIR(old_inode->i_mode);
1111 int unlink = !!new_inode;
1112 int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
1113 int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
1114 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1116 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1117 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1118 struct timespec time;
1119 unsigned int uninitialized_var(saved_nlink);
1121 if (flags & ~RENAME_NOREPLACE)
1125 * Budget request settings: deletion direntry, new direntry, removing
1126 * the old inode, and changing old and new parent directory inodes.
1128 * However, this operation also marks the target inode as dirty and
1129 * does not write it, so we allocate budget for the target inode
1133 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1134 old_dentry, old_inode->i_ino, old_dir->i_ino,
1135 new_dentry, new_dir->i_ino, flags);
1138 ubifs_assert(inode_is_locked(new_inode));
1140 if (old_dir != new_dir) {
1141 if (ubifs_crypt_is_encrypted(new_dir) &&
1142 !fscrypt_has_permitted_context(new_dir, old_inode))
1146 if (unlink && is_dir) {
1147 err = ubifs_check_dir_empty(new_inode);
1152 err = ubifs_budget_space(c, &req);
1155 err = ubifs_budget_space(c, &ino_req);
1157 ubifs_release_budget(c, &req);
1161 if (flags & RENAME_WHITEOUT) {
1162 union ubifs_dev_desc *dev = NULL;
1164 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1166 ubifs_release_budget(c, &req);
1167 ubifs_release_budget(c, &ino_req);
1171 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1173 ubifs_release_budget(c, &req);
1174 ubifs_release_budget(c, &ino_req);
1179 whiteout->i_state |= I_LINKABLE;
1180 whiteout_ui = ubifs_inode(whiteout);
1181 whiteout_ui->data = dev;
1182 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1183 ubifs_assert(!whiteout_ui->dirty);
1186 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1189 * Like most other Unix systems, set the @i_ctime for inodes on a
1192 time = ubifs_current_time(old_dir);
1193 old_inode->i_ctime = time;
1195 /* We must adjust parent link count when renaming directories */
1199 * @old_dir loses a link because we are moving
1200 * @old_inode to a different directory.
1202 drop_nlink(old_dir);
1204 * @new_dir only gains a link if we are not also
1205 * overwriting an existing directory.
1211 * @old_inode is not moving to a different directory,
1212 * but @old_dir still loses a link if we are
1213 * overwriting an existing directory.
1216 drop_nlink(old_dir);
1220 old_dir->i_size -= old_sz;
1221 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1222 old_dir->i_mtime = old_dir->i_ctime = time;
1223 new_dir->i_mtime = new_dir->i_ctime = time;
1226 * And finally, if we unlinked a direntry which happened to have the
1227 * same name as the moved direntry, we have to decrement @i_nlink of
1228 * the unlinked inode and change its ctime.
1232 * Directories cannot have hard-links, so if this is a
1233 * directory, just clear @i_nlink.
1235 saved_nlink = new_inode->i_nlink;
1237 clear_nlink(new_inode);
1239 drop_nlink(new_inode);
1240 new_inode->i_ctime = time;
1242 new_dir->i_size += new_sz;
1243 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1247 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1248 * is dirty, because this will be done later on at the end of
1251 if (IS_SYNC(old_inode)) {
1252 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1253 if (unlink && IS_SYNC(new_inode))
1258 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1260 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1262 err = ubifs_budget_space(c, &wht_req);
1264 ubifs_release_budget(c, &req);
1265 ubifs_release_budget(c, &ino_req);
1266 kfree(whiteout_ui->data);
1267 whiteout_ui->data_len = 0;
1272 inc_nlink(whiteout);
1273 mark_inode_dirty(whiteout);
1274 whiteout->i_state &= ~I_LINKABLE;
1278 err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, whiteout,
1283 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1284 ubifs_release_budget(c, &req);
1286 mutex_lock(&old_inode_ui->ui_mutex);
1287 release = old_inode_ui->dirty;
1288 mark_inode_dirty_sync(old_inode);
1289 mutex_unlock(&old_inode_ui->ui_mutex);
1292 ubifs_release_budget(c, &ino_req);
1293 if (IS_SYNC(old_inode))
1294 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1299 set_nlink(new_inode, saved_nlink);
1301 new_dir->i_size -= new_sz;
1302 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1304 old_dir->i_size += old_sz;
1305 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1310 drop_nlink(new_dir);
1317 drop_nlink(whiteout);
1320 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1321 ubifs_release_budget(c, &ino_req);
1322 ubifs_release_budget(c, &req);
1326 static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1327 struct inode *new_dir, struct dentry *new_dentry)
1329 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1330 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1332 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1333 struct inode *fst_inode = d_inode(old_dentry);
1334 struct inode *snd_inode = d_inode(new_dentry);
1335 struct timespec time;
1338 ubifs_assert(fst_inode && snd_inode);
1340 if ((ubifs_crypt_is_encrypted(old_dir) ||
1341 ubifs_crypt_is_encrypted(new_dir)) &&
1342 (old_dir != new_dir) &&
1343 (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
1344 !fscrypt_has_permitted_context(old_dir, snd_inode)))
1347 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1349 time = ubifs_current_time(old_dir);
1350 fst_inode->i_ctime = time;
1351 snd_inode->i_ctime = time;
1352 old_dir->i_mtime = old_dir->i_ctime = time;
1353 new_dir->i_mtime = new_dir->i_ctime = time;
1355 if (old_dir != new_dir) {
1356 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1358 drop_nlink(old_dir);
1360 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1361 drop_nlink(new_dir);
1366 err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry,
1369 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1370 ubifs_release_budget(c, &req);
1375 static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1376 struct inode *new_dir, struct dentry *new_dentry,
1379 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1382 ubifs_assert(inode_is_locked(old_dir));
1383 ubifs_assert(inode_is_locked(new_dir));
1385 if (flags & RENAME_EXCHANGE)
1386 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1388 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1391 int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1395 struct inode *inode = d_inode(dentry);
1396 struct ubifs_inode *ui = ubifs_inode(inode);
1398 mutex_lock(&ui->ui_mutex);
1399 generic_fillattr(inode, stat);
1400 stat->blksize = UBIFS_BLOCK_SIZE;
1401 stat->size = ui->ui_size;
1404 * Unfortunately, the 'stat()' system call was designed for block
1405 * device based file systems, and it is not appropriate for UBIFS,
1406 * because UBIFS does not have notion of "block". For example, it is
1407 * difficult to tell how many block a directory takes - it actually
1408 * takes less than 300 bytes, but we have to round it to block size,
1409 * which introduces large mistake. This makes utilities like 'du' to
1410 * report completely senseless numbers. This is the reason why UBIFS
1411 * goes the same way as JFFS2 - it reports zero blocks for everything
1412 * but regular files, which makes more sense than reporting completely
1415 if (S_ISREG(inode->i_mode)) {
1416 size = ui->xattr_size;
1418 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1420 * Note, user-space expects 512-byte blocks count irrespectively
1421 * of what was reported in @stat->size.
1423 stat->blocks = size >> 9;
1426 mutex_unlock(&ui->ui_mutex);
1430 static int ubifs_dir_open(struct inode *dir, struct file *file)
1432 if (ubifs_crypt_is_encrypted(dir))
1433 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1438 const struct inode_operations ubifs_dir_inode_operations = {
1439 .lookup = ubifs_lookup,
1440 .create = ubifs_create,
1442 .symlink = ubifs_symlink,
1443 .unlink = ubifs_unlink,
1444 .mkdir = ubifs_mkdir,
1445 .rmdir = ubifs_rmdir,
1446 .mknod = ubifs_mknod,
1447 .rename = ubifs_rename,
1448 .setattr = ubifs_setattr,
1449 .getattr = ubifs_getattr,
1450 .listxattr = ubifs_listxattr,
1451 #ifdef CONFIG_UBIFS_ATIME_SUPPORT
1452 .update_time = ubifs_update_time,
1454 .tmpfile = ubifs_tmpfile,
1457 const struct file_operations ubifs_dir_operations = {
1458 .llseek = generic_file_llseek,
1459 .release = ubifs_dir_release,
1460 .read = generic_read_dir,
1461 .iterate_shared = ubifs_readdir,
1462 .fsync = ubifs_fsync,
1463 .unlocked_ioctl = ubifs_ioctl,
1464 .open = ubifs_dir_open,
1465 #ifdef CONFIG_COMPAT
1466 .compat_ioctl = ubifs_compat_ioctl,