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 err = dbg_check_synced_i_size(c, inode);
646 err = ubifs_budget_space(c, &req);
650 lock_2_inodes(dir, inode);
653 inode->i_ctime = ubifs_current_time(inode);
654 dir->i_size += sz_change;
655 dir_ui->ui_size = dir->i_size;
656 dir->i_mtime = dir->i_ctime = inode->i_ctime;
657 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
660 unlock_2_inodes(dir, inode);
662 ubifs_release_budget(c, &req);
663 d_instantiate(dentry, inode);
667 dir->i_size -= sz_change;
668 dir_ui->ui_size = dir->i_size;
670 unlock_2_inodes(dir, inode);
671 ubifs_release_budget(c, &req);
676 static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
678 struct ubifs_info *c = dir->i_sb->s_fs_info;
679 struct inode *inode = d_inode(dentry);
680 struct ubifs_inode *dir_ui = ubifs_inode(dir);
681 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
682 int err, budgeted = 1;
683 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
684 unsigned int saved_nlink = inode->i_nlink;
687 * Budget request settings: deletion direntry, deletion inode (+1 for
688 * @dirtied_ino), changing the parent directory inode. If budgeting
689 * fails, go ahead anyway because we have extra space reserved for
693 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
694 dentry, inode->i_ino,
695 inode->i_nlink, dir->i_ino);
696 ubifs_assert(inode_is_locked(dir));
697 ubifs_assert(inode_is_locked(inode));
698 err = dbg_check_synced_i_size(c, inode);
702 err = ubifs_budget_space(c, &req);
709 lock_2_inodes(dir, inode);
710 inode->i_ctime = ubifs_current_time(dir);
712 dir->i_size -= sz_change;
713 dir_ui->ui_size = dir->i_size;
714 dir->i_mtime = dir->i_ctime = inode->i_ctime;
715 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
718 unlock_2_inodes(dir, inode);
721 ubifs_release_budget(c, &req);
723 /* We've deleted something - clean the "no space" flags */
724 c->bi.nospace = c->bi.nospace_rp = 0;
730 dir->i_size += sz_change;
731 dir_ui->ui_size = dir->i_size;
732 set_nlink(inode, saved_nlink);
733 unlock_2_inodes(dir, inode);
735 ubifs_release_budget(c, &req);
740 * check_dir_empty - check if a directory is empty or not.
741 * @dir: VFS inode object of the directory to check
743 * This function checks if directory @dir is empty. Returns zero if the
744 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
745 * in case of of errors.
747 int ubifs_check_dir_empty(struct inode *dir)
749 struct ubifs_info *c = dir->i_sb->s_fs_info;
750 struct qstr nm = { .name = NULL };
751 struct ubifs_dent_node *dent;
755 lowest_dent_key(c, &key, dir->i_ino);
756 dent = ubifs_tnc_next_ent(c, &key, &nm);
768 static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
770 struct ubifs_info *c = dir->i_sb->s_fs_info;
771 struct inode *inode = d_inode(dentry);
772 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
773 int err, budgeted = 1;
774 struct ubifs_inode *dir_ui = ubifs_inode(dir);
775 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
778 * Budget request settings: deletion direntry, deletion inode and
779 * changing the parent inode. If budgeting fails, go ahead anyway
780 * because we have extra space reserved for deletions.
783 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
784 inode->i_ino, dir->i_ino);
785 ubifs_assert(inode_is_locked(dir));
786 ubifs_assert(inode_is_locked(inode));
787 err = ubifs_check_dir_empty(d_inode(dentry));
791 err = ubifs_budget_space(c, &req);
798 lock_2_inodes(dir, inode);
799 inode->i_ctime = ubifs_current_time(dir);
802 dir->i_size -= sz_change;
803 dir_ui->ui_size = dir->i_size;
804 dir->i_mtime = dir->i_ctime = inode->i_ctime;
805 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
808 unlock_2_inodes(dir, inode);
811 ubifs_release_budget(c, &req);
813 /* We've deleted something - clean the "no space" flags */
814 c->bi.nospace = c->bi.nospace_rp = 0;
820 dir->i_size += sz_change;
821 dir_ui->ui_size = dir->i_size;
824 unlock_2_inodes(dir, inode);
826 ubifs_release_budget(c, &req);
830 static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
833 struct ubifs_inode *dir_ui = ubifs_inode(dir);
834 struct ubifs_info *c = dir->i_sb->s_fs_info;
835 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
836 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
839 * Budget request settings: new inode, new direntry and changing parent
843 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
844 dentry, mode, dir->i_ino);
846 err = ubifs_budget_space(c, &req);
850 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
852 err = PTR_ERR(inode);
856 err = ubifs_init_security(dir, inode, &dentry->d_name);
860 mutex_lock(&dir_ui->ui_mutex);
861 insert_inode_hash(inode);
864 dir->i_size += sz_change;
865 dir_ui->ui_size = dir->i_size;
866 dir->i_mtime = dir->i_ctime = inode->i_ctime;
867 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
869 ubifs_err(c, "cannot create directory, error %d", err);
872 mutex_unlock(&dir_ui->ui_mutex);
874 ubifs_release_budget(c, &req);
875 d_instantiate(dentry, inode);
879 dir->i_size -= sz_change;
880 dir_ui->ui_size = dir->i_size;
882 mutex_unlock(&dir_ui->ui_mutex);
884 make_bad_inode(inode);
887 ubifs_release_budget(c, &req);
891 static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
892 umode_t mode, dev_t rdev)
895 struct ubifs_inode *ui;
896 struct ubifs_inode *dir_ui = ubifs_inode(dir);
897 struct ubifs_info *c = dir->i_sb->s_fs_info;
898 union ubifs_dev_desc *dev = NULL;
899 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
901 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
902 .new_ino_d = ALIGN(devlen, 8),
906 * Budget request settings: new inode, new direntry and changing parent
910 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
912 if (S_ISBLK(mode) || S_ISCHR(mode)) {
913 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
916 devlen = ubifs_encode_dev(dev, rdev);
919 err = ubifs_budget_space(c, &req);
925 inode = ubifs_new_inode(c, dir, mode);
928 err = PTR_ERR(inode);
932 init_special_inode(inode, inode->i_mode, rdev);
933 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
934 ui = ubifs_inode(inode);
936 ui->data_len = devlen;
938 err = ubifs_init_security(dir, inode, &dentry->d_name);
942 mutex_lock(&dir_ui->ui_mutex);
943 dir->i_size += sz_change;
944 dir_ui->ui_size = dir->i_size;
945 dir->i_mtime = dir->i_ctime = inode->i_ctime;
946 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
949 mutex_unlock(&dir_ui->ui_mutex);
951 ubifs_release_budget(c, &req);
952 insert_inode_hash(inode);
953 d_instantiate(dentry, inode);
957 dir->i_size -= sz_change;
958 dir_ui->ui_size = dir->i_size;
959 mutex_unlock(&dir_ui->ui_mutex);
961 make_bad_inode(inode);
964 ubifs_release_budget(c, &req);
968 static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
972 struct ubifs_inode *ui;
973 struct ubifs_inode *dir_ui = ubifs_inode(dir);
974 struct ubifs_info *c = dir->i_sb->s_fs_info;
975 int err, len = strlen(symname);
976 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
977 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
978 .new_ino_d = ALIGN(len, 8),
982 * Budget request settings: new inode, new direntry and changing parent
986 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
987 symname, dir->i_ino);
989 if (len > UBIFS_MAX_INO_DATA)
990 return -ENAMETOOLONG;
992 err = ubifs_budget_space(c, &req);
996 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
998 err = PTR_ERR(inode);
1002 ui = ubifs_inode(inode);
1003 ui->data = kmalloc(len + 1, GFP_NOFS);
1009 memcpy(ui->data, symname, len);
1010 ((char *)ui->data)[len] = '\0';
1011 inode->i_link = ui->data;
1013 * The terminating zero byte is not written to the flash media and it
1014 * is put just to make later in-memory string processing simpler. Thus,
1015 * data length is @len, not @len + %1.
1018 inode->i_size = ubifs_inode(inode)->ui_size = len;
1020 err = ubifs_init_security(dir, inode, &dentry->d_name);
1024 mutex_lock(&dir_ui->ui_mutex);
1025 dir->i_size += sz_change;
1026 dir_ui->ui_size = dir->i_size;
1027 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1028 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
1031 mutex_unlock(&dir_ui->ui_mutex);
1033 ubifs_release_budget(c, &req);
1034 insert_inode_hash(inode);
1035 d_instantiate(dentry, inode);
1039 dir->i_size -= sz_change;
1040 dir_ui->ui_size = dir->i_size;
1041 mutex_unlock(&dir_ui->ui_mutex);
1043 make_bad_inode(inode);
1046 ubifs_release_budget(c, &req);
1051 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1052 * @inode1: first inode
1053 * @inode2: second inode
1054 * @inode3: third inode
1055 * @inode4: fouth inode
1057 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1058 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1060 * We do not implement any tricks to guarantee strict lock ordering, because
1061 * VFS has already done it for us on the @i_mutex. So this is just a simple
1064 static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1065 struct inode *inode3, struct inode *inode4)
1067 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1068 if (inode2 != inode1)
1069 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1071 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
1073 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1077 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1078 * @inode1: first inode
1079 * @inode2: second inode
1080 * @inode3: third inode
1081 * @inode4: fouth inode
1083 static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1084 struct inode *inode3, struct inode *inode4)
1087 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1089 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
1090 if (inode1 != inode2)
1091 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1092 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1095 static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1096 struct inode *new_dir, struct dentry *new_dentry,
1099 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1100 struct inode *old_inode = d_inode(old_dentry);
1101 struct inode *new_inode = d_inode(new_dentry);
1102 struct inode *whiteout = NULL;
1103 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1104 struct ubifs_inode *whiteout_ui = NULL;
1105 int err, release, sync = 0, move = (new_dir != old_dir);
1106 int is_dir = S_ISDIR(old_inode->i_mode);
1107 int unlink = !!new_inode;
1108 int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
1109 int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
1110 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1112 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1113 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1114 struct timespec time;
1115 unsigned int uninitialized_var(saved_nlink);
1117 if (flags & ~RENAME_NOREPLACE)
1121 * Budget request settings: deletion direntry, new direntry, removing
1122 * the old inode, and changing old and new parent directory inodes.
1124 * However, this operation also marks the target inode as dirty and
1125 * does not write it, so we allocate budget for the target inode
1129 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1130 old_dentry, old_inode->i_ino, old_dir->i_ino,
1131 new_dentry, new_dir->i_ino, flags);
1134 ubifs_assert(inode_is_locked(new_inode));
1136 if (unlink && is_dir) {
1137 err = ubifs_check_dir_empty(new_inode);
1142 err = ubifs_budget_space(c, &req);
1145 err = ubifs_budget_space(c, &ino_req);
1147 ubifs_release_budget(c, &req);
1151 if (flags & RENAME_WHITEOUT) {
1152 union ubifs_dev_desc *dev = NULL;
1154 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1156 ubifs_release_budget(c, &req);
1157 ubifs_release_budget(c, &ino_req);
1161 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1163 ubifs_release_budget(c, &req);
1164 ubifs_release_budget(c, &ino_req);
1169 whiteout->i_state |= I_LINKABLE;
1170 whiteout_ui = ubifs_inode(whiteout);
1171 whiteout_ui->data = dev;
1172 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1173 ubifs_assert(!whiteout_ui->dirty);
1176 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1179 * Like most other Unix systems, set the @i_ctime for inodes on a
1182 time = ubifs_current_time(old_dir);
1183 old_inode->i_ctime = time;
1185 /* We must adjust parent link count when renaming directories */
1189 * @old_dir loses a link because we are moving
1190 * @old_inode to a different directory.
1192 drop_nlink(old_dir);
1194 * @new_dir only gains a link if we are not also
1195 * overwriting an existing directory.
1201 * @old_inode is not moving to a different directory,
1202 * but @old_dir still loses a link if we are
1203 * overwriting an existing directory.
1206 drop_nlink(old_dir);
1210 old_dir->i_size -= old_sz;
1211 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1212 old_dir->i_mtime = old_dir->i_ctime = time;
1213 new_dir->i_mtime = new_dir->i_ctime = time;
1216 * And finally, if we unlinked a direntry which happened to have the
1217 * same name as the moved direntry, we have to decrement @i_nlink of
1218 * the unlinked inode and change its ctime.
1222 * Directories cannot have hard-links, so if this is a
1223 * directory, just clear @i_nlink.
1225 saved_nlink = new_inode->i_nlink;
1227 clear_nlink(new_inode);
1229 drop_nlink(new_inode);
1230 new_inode->i_ctime = time;
1232 new_dir->i_size += new_sz;
1233 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1237 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1238 * is dirty, because this will be done later on at the end of
1241 if (IS_SYNC(old_inode)) {
1242 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1243 if (unlink && IS_SYNC(new_inode))
1248 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1250 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1252 err = ubifs_budget_space(c, &wht_req);
1254 ubifs_release_budget(c, &req);
1255 ubifs_release_budget(c, &ino_req);
1256 kfree(whiteout_ui->data);
1257 whiteout_ui->data_len = 0;
1262 inc_nlink(whiteout);
1263 mark_inode_dirty(whiteout);
1264 whiteout->i_state &= ~I_LINKABLE;
1268 err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, whiteout,
1273 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1274 ubifs_release_budget(c, &req);
1276 mutex_lock(&old_inode_ui->ui_mutex);
1277 release = old_inode_ui->dirty;
1278 mark_inode_dirty_sync(old_inode);
1279 mutex_unlock(&old_inode_ui->ui_mutex);
1282 ubifs_release_budget(c, &ino_req);
1283 if (IS_SYNC(old_inode))
1284 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1289 set_nlink(new_inode, saved_nlink);
1291 new_dir->i_size -= new_sz;
1292 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1294 old_dir->i_size += old_sz;
1295 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1300 drop_nlink(new_dir);
1307 drop_nlink(whiteout);
1310 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1311 ubifs_release_budget(c, &ino_req);
1312 ubifs_release_budget(c, &req);
1316 static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1317 struct inode *new_dir, struct dentry *new_dentry)
1319 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1320 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1322 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1323 struct inode *fst_inode = d_inode(old_dentry);
1324 struct inode *snd_inode = d_inode(new_dentry);
1325 struct timespec time;
1328 ubifs_assert(fst_inode && snd_inode);
1330 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1332 time = ubifs_current_time(old_dir);
1333 fst_inode->i_ctime = time;
1334 snd_inode->i_ctime = time;
1335 old_dir->i_mtime = old_dir->i_ctime = time;
1336 new_dir->i_mtime = new_dir->i_ctime = time;
1338 if (old_dir != new_dir) {
1339 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1341 drop_nlink(old_dir);
1343 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1344 drop_nlink(new_dir);
1349 err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry,
1352 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1353 ubifs_release_budget(c, &req);
1358 static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1359 struct inode *new_dir, struct dentry *new_dentry,
1362 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1365 ubifs_assert(inode_is_locked(old_dir));
1366 ubifs_assert(inode_is_locked(new_dir));
1368 if (flags & RENAME_EXCHANGE)
1369 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1371 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1374 int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1378 struct inode *inode = d_inode(dentry);
1379 struct ubifs_inode *ui = ubifs_inode(inode);
1381 mutex_lock(&ui->ui_mutex);
1382 generic_fillattr(inode, stat);
1383 stat->blksize = UBIFS_BLOCK_SIZE;
1384 stat->size = ui->ui_size;
1387 * Unfortunately, the 'stat()' system call was designed for block
1388 * device based file systems, and it is not appropriate for UBIFS,
1389 * because UBIFS does not have notion of "block". For example, it is
1390 * difficult to tell how many block a directory takes - it actually
1391 * takes less than 300 bytes, but we have to round it to block size,
1392 * which introduces large mistake. This makes utilities like 'du' to
1393 * report completely senseless numbers. This is the reason why UBIFS
1394 * goes the same way as JFFS2 - it reports zero blocks for everything
1395 * but regular files, which makes more sense than reporting completely
1398 if (S_ISREG(inode->i_mode)) {
1399 size = ui->xattr_size;
1401 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1403 * Note, user-space expects 512-byte blocks count irrespectively
1404 * of what was reported in @stat->size.
1406 stat->blocks = size >> 9;
1409 mutex_unlock(&ui->ui_mutex);
1413 static int ubifs_dir_open(struct inode *dir, struct file *file)
1415 if (ubifs_crypt_is_encrypted(dir))
1416 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1421 const struct inode_operations ubifs_dir_inode_operations = {
1422 .lookup = ubifs_lookup,
1423 .create = ubifs_create,
1425 .symlink = ubifs_symlink,
1426 .unlink = ubifs_unlink,
1427 .mkdir = ubifs_mkdir,
1428 .rmdir = ubifs_rmdir,
1429 .mknod = ubifs_mknod,
1430 .rename = ubifs_rename,
1431 .setattr = ubifs_setattr,
1432 .getattr = ubifs_getattr,
1433 .listxattr = ubifs_listxattr,
1434 #ifdef CONFIG_UBIFS_ATIME_SUPPORT
1435 .update_time = ubifs_update_time,
1437 .tmpfile = ubifs_tmpfile,
1440 const struct file_operations ubifs_dir_operations = {
1441 .llseek = generic_file_llseek,
1442 .release = ubifs_dir_release,
1443 .read = generic_read_dir,
1444 .iterate_shared = ubifs_readdir,
1445 .fsync = ubifs_fsync,
1446 .unlocked_ioctl = ubifs_ioctl,
1447 .open = ubifs_dir_open,
1448 #ifdef CONFIG_COMPAT
1449 .compat_ioctl = ubifs_compat_ioctl,