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
71 * @is_xattr: whether the inode is xattr inode
73 * This function finds an unused inode number, allocates new inode and
74 * initializes it. Non-xattr new inode may be written with xattrs(selinux/
75 * encryption) before writing dentry, which could cause inconsistent problem
76 * when powercut happens between two operations. To deal with it, non-xattr
77 * new inode is initialized with zero-nlink and added into orphan list, caller
78 * should make sure that inode is relinked later, and make sure that orphan
79 * removing and journal writing into an committing atomic operation. Returns
80 * new inode in case of success and an error code in case of failure.
82 struct inode
*ubifs_new_inode(struct ubifs_info
*c
, struct inode
*dir
,
83 umode_t mode
, bool is_xattr
)
87 struct ubifs_inode
*ui
;
88 bool encrypted
= false;
90 inode
= new_inode(c
->vfs_sb
);
91 ui
= ubifs_inode(inode
);
93 return ERR_PTR(-ENOMEM
);
96 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
97 * marking them dirty in file write path (see 'file_update_time()').
98 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
99 * to make budgeting work.
101 inode
->i_flags
|= S_NOCMTIME
;
103 inode_init_owner(&nop_mnt_idmap
, inode
, dir
, mode
);
104 simple_inode_init_ts(inode
);
105 inode
->i_mapping
->nrpages
= 0;
108 err
= fscrypt_prepare_new_inode(dir
, inode
, &encrypted
);
110 ubifs_err(c
, "fscrypt_prepare_new_inode failed: %i", err
);
115 switch (mode
& S_IFMT
) {
117 inode
->i_mapping
->a_ops
= &ubifs_file_address_operations
;
118 inode
->i_op
= &ubifs_file_inode_operations
;
119 inode
->i_fop
= &ubifs_file_operations
;
122 inode
->i_op
= &ubifs_dir_inode_operations
;
123 inode
->i_fop
= &ubifs_dir_operations
;
124 inode
->i_size
= ui
->ui_size
= UBIFS_INO_NODE_SZ
;
127 inode
->i_op
= &ubifs_symlink_inode_operations
;
133 inode
->i_op
= &ubifs_file_inode_operations
;
139 ui
->flags
= inherit_flags(dir
, mode
);
140 ubifs_set_inode_flags(inode
);
142 ui
->compr_type
= c
->default_compr
;
144 ui
->compr_type
= UBIFS_COMPR_NONE
;
145 ui
->synced_i_size
= 0;
147 spin_lock(&c
->cnt_lock
);
148 /* Inode number overflow is currently not supported */
149 if (c
->highest_inum
>= INUM_WARN_WATERMARK
) {
150 if (c
->highest_inum
>= INUM_WATERMARK
) {
151 spin_unlock(&c
->cnt_lock
);
152 ubifs_err(c
, "out of inode numbers");
156 ubifs_warn(c
, "running out of inode numbers (current %lu, max %u)",
157 (unsigned long)c
->highest_inum
, INUM_WATERMARK
);
160 inode
->i_ino
= ++c
->highest_inum
;
162 * The creation sequence number remains with this inode for its
163 * lifetime. All nodes for this inode have a greater sequence number,
164 * and so it is possible to distinguish obsolete nodes belonging to a
165 * previous incarnation of the same inode number - for example, for the
166 * purpose of rebuilding the index.
168 ui
->creat_sqnum
= ++c
->max_sqnum
;
169 spin_unlock(&c
->cnt_lock
);
173 err
= ubifs_add_orphan(c
, inode
->i_ino
);
175 ubifs_err(c
, "ubifs_add_orphan failed: %i", err
);
178 down_read(&c
->commit_sem
);
179 ui
->del_cmtno
= c
->cmt_no
;
180 up_read(&c
->commit_sem
);
184 err
= fscrypt_set_context(inode
, NULL
);
188 ubifs_delete_orphan(c
, inode
->i_ino
);
190 ubifs_err(c
, "fscrypt_set_context failed: %i", err
);
198 make_bad_inode(inode
);
203 static int dbg_check_name(const struct ubifs_info
*c
,
204 const struct ubifs_dent_node
*dent
,
205 const struct fscrypt_name
*nm
)
207 if (!dbg_is_chk_gen(c
))
209 if (le16_to_cpu(dent
->nlen
) != fname_len(nm
))
211 if (memcmp(dent
->name
, fname_name(nm
), fname_len(nm
)))
216 static struct dentry
*ubifs_lookup(struct inode
*dir
, struct dentry
*dentry
,
221 struct inode
*inode
= NULL
;
222 struct ubifs_dent_node
*dent
= NULL
;
223 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
224 struct fscrypt_name nm
;
226 dbg_gen("'%pd' in dir ino %lu", dentry
, dir
->i_ino
);
228 err
= fscrypt_prepare_lookup(dir
, dentry
, &nm
);
230 return d_splice_alias(NULL
, dentry
);
234 if (fname_len(&nm
) > UBIFS_MAX_NLEN
) {
235 inode
= ERR_PTR(-ENAMETOOLONG
);
239 dent
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
241 inode
= ERR_PTR(-ENOMEM
);
245 if (fname_name(&nm
) == NULL
) {
246 if (nm
.hash
& ~UBIFS_S_KEY_HASH_MASK
)
247 goto done
; /* ENOENT */
248 dent_key_init_hash(c
, &key
, dir
->i_ino
, nm
.hash
);
249 err
= ubifs_tnc_lookup_dh(c
, &key
, dent
, nm
.minor_hash
);
251 dent_key_init(c
, &key
, dir
->i_ino
, &nm
);
252 err
= ubifs_tnc_lookup_nm(c
, &key
, dent
, &nm
);
257 dbg_gen("not found");
259 inode
= ERR_PTR(err
);
263 if (dbg_check_name(c
, dent
, &nm
)) {
264 inode
= ERR_PTR(-EINVAL
);
268 inode
= ubifs_iget(dir
->i_sb
, le64_to_cpu(dent
->inum
));
271 * This should not happen. Probably the file-system needs
274 err
= PTR_ERR(inode
);
275 ubifs_err(c
, "dead directory entry '%pd', error %d",
277 ubifs_ro_mode(c
, err
);
281 if (IS_ENCRYPTED(dir
) &&
282 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
283 !fscrypt_has_permitted_context(dir
, inode
)) {
284 ubifs_warn(c
, "Inconsistent encryption contexts: %lu/%lu",
285 dir
->i_ino
, inode
->i_ino
);
287 inode
= ERR_PTR(-EPERM
);
292 fscrypt_free_filename(&nm
);
293 return d_splice_alias(inode
, dentry
);
296 static int ubifs_prepare_create(struct inode
*dir
, struct dentry
*dentry
,
297 struct fscrypt_name
*nm
)
299 if (fscrypt_is_nokey_name(dentry
))
302 return fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, nm
);
305 static int ubifs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
306 struct dentry
*dentry
, umode_t mode
, bool excl
)
309 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
310 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
312 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
313 struct fscrypt_name nm
;
317 * Budget request settings: new inode, new direntry, changing the
318 * parent directory inode.
321 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
322 dentry
, mode
, dir
->i_ino
);
324 err
= ubifs_budget_space(c
, &req
);
328 err
= ubifs_prepare_create(dir
, dentry
, &nm
);
332 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
334 inode
= ubifs_new_inode(c
, dir
, mode
, false);
336 err
= PTR_ERR(inode
);
340 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
345 mutex_lock(&dir_ui
->ui_mutex
);
346 dir
->i_size
+= sz_change
;
347 dir_ui
->ui_size
= dir
->i_size
;
348 inode_set_mtime_to_ts(dir
,
349 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
350 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0, 1);
353 mutex_unlock(&dir_ui
->ui_mutex
);
355 ubifs_release_budget(c
, &req
);
356 fscrypt_free_filename(&nm
);
357 insert_inode_hash(inode
);
358 d_instantiate(dentry
, inode
);
362 dir
->i_size
-= sz_change
;
363 dir_ui
->ui_size
= dir
->i_size
;
364 mutex_unlock(&dir_ui
->ui_mutex
);
369 fscrypt_free_filename(&nm
);
371 ubifs_release_budget(c
, &req
);
372 ubifs_err(c
, "cannot create regular file, error %d", err
);
376 static struct inode
*create_whiteout(struct inode
*dir
, struct dentry
*dentry
)
379 umode_t mode
= S_IFCHR
| WHITEOUT_MODE
;
381 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
384 * Create an inode('nlink = 1') for whiteout without updating journal,
385 * let ubifs_jnl_rename() store it on flash to complete rename whiteout
389 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
390 dentry
, mode
, dir
->i_ino
);
392 inode
= ubifs_new_inode(c
, dir
, mode
, false);
394 err
= PTR_ERR(inode
);
398 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
399 ubifs_assert(c
, inode
->i_op
== &ubifs_file_inode_operations
);
401 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
405 /* The dir size is updated by do_rename. */
406 insert_inode_hash(inode
);
413 ubifs_err(c
, "cannot create whiteout file, error %d", err
);
418 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
419 * @inode1: first inode
420 * @inode2: second inode
422 * We do not implement any tricks to guarantee strict lock ordering, because
423 * VFS has already done it for us on the @i_mutex. So this is just a simple
426 static void lock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
428 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
429 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
433 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
434 * @inode1: first inode
435 * @inode2: second inode
437 static void unlock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
439 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
440 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
443 static int ubifs_tmpfile(struct mnt_idmap
*idmap
, struct inode
*dir
,
444 struct file
*file
, umode_t mode
)
446 struct dentry
*dentry
= file
->f_path
.dentry
;
448 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
449 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
451 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1 };
452 struct ubifs_inode
*ui
;
453 int err
, instantiated
= 0;
454 struct fscrypt_name nm
;
457 * Budget request settings: new inode, new direntry, changing the
458 * parent directory inode.
459 * Allocate budget separately for new dirtied inode, the budget will
460 * be released via writeback.
463 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
464 dentry
, mode
, dir
->i_ino
);
466 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
470 err
= ubifs_budget_space(c
, &req
);
472 fscrypt_free_filename(&nm
);
476 err
= ubifs_budget_space(c
, &ino_req
);
478 ubifs_release_budget(c
, &req
);
479 fscrypt_free_filename(&nm
);
483 inode
= ubifs_new_inode(c
, dir
, mode
, false);
485 err
= PTR_ERR(inode
);
488 ui
= ubifs_inode(inode
);
490 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
495 mutex_lock(&ui
->ui_mutex
);
496 insert_inode_hash(inode
);
497 d_tmpfile(file
, inode
);
498 ubifs_assert(c
, ui
->dirty
);
501 mutex_unlock(&ui
->ui_mutex
);
503 lock_2_inodes(dir
, inode
);
504 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0, 1);
507 unlock_2_inodes(dir
, inode
);
509 ubifs_release_budget(c
, &req
);
510 fscrypt_free_filename(&nm
);
512 return finish_open_simple(file
, 0);
515 unlock_2_inodes(dir
, inode
);
520 ubifs_release_budget(c
, &req
);
522 ubifs_release_budget(c
, &ino_req
);
523 fscrypt_free_filename(&nm
);
524 ubifs_err(c
, "cannot create temporary file, error %d", err
);
529 * vfs_dent_type - get VFS directory entry type.
530 * @type: UBIFS directory entry type
532 * This function converts UBIFS directory entry type into VFS directory entry
535 static unsigned int vfs_dent_type(uint8_t type
)
538 case UBIFS_ITYPE_REG
:
540 case UBIFS_ITYPE_DIR
:
542 case UBIFS_ITYPE_LNK
:
544 case UBIFS_ITYPE_BLK
:
546 case UBIFS_ITYPE_CHR
:
548 case UBIFS_ITYPE_FIFO
:
550 case UBIFS_ITYPE_SOCK
:
558 struct ubifs_dir_data
{
559 struct ubifs_dent_node
*dent
;
564 * The classical Unix view for directory is that it is a linear array of
565 * (name, inode number) entries. Linux/VFS assumes this model as well.
566 * Particularly, 'readdir()' call wants us to return a directory entry offset
567 * which later may be used to continue 'readdir()'ing the directory or to
568 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
569 * model because directory entries are identified by keys, which may collide.
571 * UBIFS uses directory entry hash value for directory offsets, so
572 * 'seekdir()'/'telldir()' may not always work because of possible key
573 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
574 * properly by means of saving full directory entry name in the private field
575 * of the file description object.
577 * This means that UBIFS cannot support NFS which requires full
578 * 'seekdir()'/'telldir()' support.
580 static int ubifs_readdir(struct file
*file
, struct dir_context
*ctx
)
582 int fstr_real_len
= 0, err
= 0;
583 struct fscrypt_name nm
;
584 struct fscrypt_str fstr
= {0};
586 struct ubifs_dent_node
*dent
;
587 struct inode
*dir
= file_inode(file
);
588 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
589 bool encrypted
= IS_ENCRYPTED(dir
);
590 struct ubifs_dir_data
*data
= file
->private_data
;
592 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, ctx
->pos
);
594 if (ctx
->pos
> UBIFS_S_KEY_HASH_MASK
|| ctx
->pos
== 2)
596 * The directory was seek'ed to a senseless position or there
597 * are no more entries.
602 err
= fscrypt_prepare_readdir(dir
);
606 err
= fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN
, &fstr
);
610 fstr_real_len
= fstr
.len
;
613 if (data
->cookie
== 0) {
615 * The file was seek'ed, which means that @data->dent
616 * is now invalid. This may also be just the first
617 * 'ubifs_readdir()' invocation, in which case
618 * @data->dent is NULL, and the below code is
626 * 'ubifs_dir_llseek()' sets @data->cookie to zero, and we use this
627 * for detecting whether the file was seek'ed.
631 /* File positions 0 and 1 correspond to "." and ".." */
633 ubifs_assert(c
, !data
->dent
);
634 if (!dir_emit_dots(file
, ctx
)) {
636 fscrypt_fname_free_buffer(&fstr
);
640 /* Find the first entry in TNC and save it */
641 lowest_dent_key(c
, &key
, dir
->i_ino
);
643 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
649 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
656 * The directory was seek'ed to and is now readdir'ed.
657 * Find the entry corresponding to @ctx->pos or the closest one.
659 dent_key_init_hash(c
, &key
, dir
->i_ino
, ctx
->pos
);
661 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
666 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
671 dbg_gen("ino %llu, new f_pos %#x",
672 (unsigned long long)le64_to_cpu(dent
->inum
),
673 key_hash_flash(c
, &dent
->key
));
674 ubifs_assert(c
, le64_to_cpu(dent
->ch
.sqnum
) >
675 ubifs_inode(dir
)->creat_sqnum
);
677 fname_len(&nm
) = le16_to_cpu(dent
->nlen
);
678 fname_name(&nm
) = dent
->name
;
681 fstr
.len
= fstr_real_len
;
683 err
= fscrypt_fname_disk_to_usr(dir
, key_hash_flash(c
,
685 le32_to_cpu(dent
->cookie
),
686 &nm
.disk_name
, &fstr
);
690 fstr
.len
= fname_len(&nm
);
691 fstr
.name
= fname_name(&nm
);
694 if (!dir_emit(ctx
, fstr
.name
, fstr
.len
,
695 le64_to_cpu(dent
->inum
),
696 vfs_dent_type(dent
->type
))) {
698 fscrypt_fname_free_buffer(&fstr
);
702 /* Switch to the next entry */
703 key_read(c
, &dent
->key
, &key
);
704 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
711 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
721 fscrypt_fname_free_buffer(&fstr
);
724 ubifs_err(c
, "cannot find next direntry, error %d", err
);
727 * -ENOENT is a non-fatal error in this context, the TNC uses
728 * it to indicate that the cursor moved past the current directory
729 * and readdir() has to stop.
734 /* 2 is a special value indicating that there are no more direntries */
739 /* Free saved readdir() state when the directory is closed */
740 static int ubifs_dir_release(struct inode
*dir
, struct file
*file
)
742 struct ubifs_dir_data
*data
= file
->private_data
;
746 file
->private_data
= NULL
;
750 static int ubifs_link(struct dentry
*old_dentry
, struct inode
*dir
,
751 struct dentry
*dentry
)
753 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
754 struct inode
*inode
= d_inode(old_dentry
);
755 struct ubifs_inode
*ui
= ubifs_inode(inode
);
756 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
758 struct ubifs_budget_req req
= { .new_dent
= 1, .dirtied_ino
= 2,
759 .dirtied_ino_d
= ALIGN(ui
->data_len
, 8) };
760 struct fscrypt_name nm
;
763 * Budget request settings: new direntry, changing the target inode,
764 * changing the parent inode.
767 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
768 dentry
, inode
->i_ino
,
769 inode
->i_nlink
, dir
->i_ino
);
770 ubifs_assert(c
, inode_is_locked(dir
));
771 ubifs_assert(c
, inode_is_locked(inode
));
773 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
777 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
781 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
783 err
= dbg_check_synced_i_size(c
, inode
);
787 err
= ubifs_budget_space(c
, &req
);
791 lock_2_inodes(dir
, inode
);
795 inode_set_ctime_current(inode
);
796 dir
->i_size
+= sz_change
;
797 dir_ui
->ui_size
= dir
->i_size
;
798 inode_set_mtime_to_ts(dir
,
799 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
800 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0, inode
->i_nlink
== 1);
803 unlock_2_inodes(dir
, inode
);
805 ubifs_release_budget(c
, &req
);
806 d_instantiate(dentry
, inode
);
807 fscrypt_free_filename(&nm
);
811 dir
->i_size
-= sz_change
;
812 dir_ui
->ui_size
= dir
->i_size
;
814 unlock_2_inodes(dir
, inode
);
815 ubifs_release_budget(c
, &req
);
818 fscrypt_free_filename(&nm
);
822 static int ubifs_unlink(struct inode
*dir
, struct dentry
*dentry
)
824 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
825 struct inode
*inode
= d_inode(dentry
);
826 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
827 int err
, sz_change
, budgeted
= 1;
828 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
829 unsigned int saved_nlink
= inode
->i_nlink
;
830 struct fscrypt_name nm
;
833 * Budget request settings: deletion direntry, deletion inode (+1 for
834 * @dirtied_ino), changing the parent directory inode. If budgeting
835 * fails, go ahead anyway because we have extra space reserved for
839 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
840 dentry
, inode
->i_ino
,
841 inode
->i_nlink
, dir
->i_ino
);
843 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
847 err
= ubifs_purge_xattrs(inode
);
851 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
853 ubifs_assert(c
, inode_is_locked(dir
));
854 ubifs_assert(c
, inode_is_locked(inode
));
855 err
= dbg_check_synced_i_size(c
, inode
);
859 err
= ubifs_budget_space(c
, &req
);
866 lock_2_inodes(dir
, inode
);
867 inode_set_ctime_current(inode
);
869 dir
->i_size
-= sz_change
;
870 dir_ui
->ui_size
= dir
->i_size
;
871 inode_set_mtime_to_ts(dir
,
872 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
873 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0, 0);
876 unlock_2_inodes(dir
, inode
);
879 ubifs_release_budget(c
, &req
);
881 /* We've deleted something - clean the "no space" flags */
882 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
885 fscrypt_free_filename(&nm
);
889 dir
->i_size
+= sz_change
;
890 dir_ui
->ui_size
= dir
->i_size
;
891 set_nlink(inode
, saved_nlink
);
892 unlock_2_inodes(dir
, inode
);
894 ubifs_release_budget(c
, &req
);
896 fscrypt_free_filename(&nm
);
901 * ubifs_check_dir_empty - check if a directory is empty or not.
902 * @dir: VFS inode object of the directory to check
904 * This function checks if directory @dir is empty. Returns zero if the
905 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
908 int ubifs_check_dir_empty(struct inode
*dir
)
910 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
911 struct fscrypt_name nm
= { 0 };
912 struct ubifs_dent_node
*dent
;
916 lowest_dent_key(c
, &key
, dir
->i_ino
);
917 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
929 static int ubifs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
931 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
932 struct inode
*inode
= d_inode(dentry
);
933 int err
, sz_change
, budgeted
= 1;
934 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
935 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
936 struct fscrypt_name nm
;
939 * Budget request settings: deletion direntry, deletion inode and
940 * changing the parent inode. If budgeting fails, go ahead anyway
941 * because we have extra space reserved for deletions.
944 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry
,
945 inode
->i_ino
, dir
->i_ino
);
946 ubifs_assert(c
, inode_is_locked(dir
));
947 ubifs_assert(c
, inode_is_locked(inode
));
948 err
= ubifs_check_dir_empty(d_inode(dentry
));
952 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
956 err
= ubifs_purge_xattrs(inode
);
960 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
962 err
= ubifs_budget_space(c
, &req
);
969 lock_2_inodes(dir
, inode
);
970 inode_set_ctime_current(inode
);
973 dir
->i_size
-= sz_change
;
974 dir_ui
->ui_size
= dir
->i_size
;
975 inode_set_mtime_to_ts(dir
,
976 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
977 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0, 0);
980 unlock_2_inodes(dir
, inode
);
983 ubifs_release_budget(c
, &req
);
985 /* We've deleted something - clean the "no space" flags */
986 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
989 fscrypt_free_filename(&nm
);
993 dir
->i_size
+= sz_change
;
994 dir_ui
->ui_size
= dir
->i_size
;
997 unlock_2_inodes(dir
, inode
);
999 ubifs_release_budget(c
, &req
);
1001 fscrypt_free_filename(&nm
);
1005 static int ubifs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
1006 struct dentry
*dentry
, umode_t mode
)
1008 struct inode
*inode
;
1009 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1010 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1012 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1014 struct fscrypt_name nm
;
1017 * Budget request settings: new inode, new direntry and changing parent
1021 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
1022 dentry
, mode
, dir
->i_ino
);
1024 err
= ubifs_budget_space(c
, &req
);
1028 err
= ubifs_prepare_create(dir
, dentry
, &nm
);
1032 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1034 inode
= ubifs_new_inode(c
, dir
, S_IFDIR
| mode
, false);
1035 if (IS_ERR(inode
)) {
1036 err
= PTR_ERR(inode
);
1040 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1044 set_nlink(inode
, 1);
1045 mutex_lock(&dir_ui
->ui_mutex
);
1046 insert_inode_hash(inode
);
1049 dir
->i_size
+= sz_change
;
1050 dir_ui
->ui_size
= dir
->i_size
;
1051 inode_set_mtime_to_ts(dir
,
1052 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
1053 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0, 1);
1055 ubifs_err(c
, "cannot create directory, error %d", err
);
1058 mutex_unlock(&dir_ui
->ui_mutex
);
1060 ubifs_release_budget(c
, &req
);
1061 d_instantiate(dentry
, inode
);
1062 fscrypt_free_filename(&nm
);
1066 dir
->i_size
-= sz_change
;
1067 dir_ui
->ui_size
= dir
->i_size
;
1069 mutex_unlock(&dir_ui
->ui_mutex
);
1070 set_nlink(inode
, 0);
1074 fscrypt_free_filename(&nm
);
1076 ubifs_release_budget(c
, &req
);
1080 static int ubifs_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
1081 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
1083 struct inode
*inode
;
1084 struct ubifs_inode
*ui
;
1085 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1086 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1087 union ubifs_dev_desc
*dev
= NULL
;
1089 int err
, devlen
= 0;
1090 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1092 struct fscrypt_name nm
;
1095 * Budget request settings: new inode, new direntry and changing parent
1099 dbg_gen("dent '%pd' in dir ino %lu", dentry
, dir
->i_ino
);
1101 if (S_ISBLK(mode
) || S_ISCHR(mode
)) {
1102 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1105 devlen
= ubifs_encode_dev(dev
, rdev
);
1108 req
.new_ino_d
= ALIGN(devlen
, 8);
1109 err
= ubifs_budget_space(c
, &req
);
1115 err
= ubifs_prepare_create(dir
, dentry
, &nm
);
1121 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1123 inode
= ubifs_new_inode(c
, dir
, mode
, false);
1124 if (IS_ERR(inode
)) {
1126 err
= PTR_ERR(inode
);
1130 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1136 init_special_inode(inode
, inode
->i_mode
, rdev
);
1137 inode
->i_size
= ubifs_inode(inode
)->ui_size
= devlen
;
1138 ui
= ubifs_inode(inode
);
1140 ui
->data_len
= devlen
;
1141 set_nlink(inode
, 1);
1143 mutex_lock(&dir_ui
->ui_mutex
);
1144 dir
->i_size
+= sz_change
;
1145 dir_ui
->ui_size
= dir
->i_size
;
1146 inode_set_mtime_to_ts(dir
,
1147 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
1148 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0, 1);
1151 mutex_unlock(&dir_ui
->ui_mutex
);
1153 ubifs_release_budget(c
, &req
);
1154 insert_inode_hash(inode
);
1155 d_instantiate(dentry
, inode
);
1156 fscrypt_free_filename(&nm
);
1160 dir
->i_size
-= sz_change
;
1161 dir_ui
->ui_size
= dir
->i_size
;
1162 mutex_unlock(&dir_ui
->ui_mutex
);
1163 set_nlink(inode
, 0);
1167 fscrypt_free_filename(&nm
);
1169 ubifs_release_budget(c
, &req
);
1173 static int ubifs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
1174 struct dentry
*dentry
, const char *symname
)
1176 struct inode
*inode
;
1177 struct ubifs_inode
*ui
;
1178 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1179 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1180 int err
, sz_change
, len
= strlen(symname
);
1181 struct fscrypt_str disk_link
;
1182 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1184 struct fscrypt_name nm
;
1186 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry
,
1187 symname
, dir
->i_ino
);
1189 err
= fscrypt_prepare_symlink(dir
, symname
, len
, UBIFS_MAX_INO_DATA
,
1195 * Budget request settings: new inode, new direntry and changing parent
1198 req
.new_ino_d
= ALIGN(disk_link
.len
- 1, 8);
1199 err
= ubifs_budget_space(c
, &req
);
1203 err
= ubifs_prepare_create(dir
, dentry
, &nm
);
1207 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1209 inode
= ubifs_new_inode(c
, dir
, S_IFLNK
| S_IRWXUGO
, false);
1210 if (IS_ERR(inode
)) {
1211 err
= PTR_ERR(inode
);
1215 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1219 ui
= ubifs_inode(inode
);
1220 ui
->data
= kmalloc(disk_link
.len
, GFP_NOFS
);
1226 if (IS_ENCRYPTED(inode
)) {
1227 disk_link
.name
= ui
->data
; /* encrypt directly into ui->data */
1228 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
1232 memcpy(ui
->data
, disk_link
.name
, disk_link
.len
);
1233 inode
->i_link
= ui
->data
;
1237 * The terminating zero byte is not written to the flash media and it
1238 * is put just to make later in-memory string processing simpler. Thus,
1239 * data length is @disk_link.len - 1, not @disk_link.len.
1241 ui
->data_len
= disk_link
.len
- 1;
1242 inode
->i_size
= ubifs_inode(inode
)->ui_size
= disk_link
.len
- 1;
1243 set_nlink(inode
, 1);
1245 mutex_lock(&dir_ui
->ui_mutex
);
1246 dir
->i_size
+= sz_change
;
1247 dir_ui
->ui_size
= dir
->i_size
;
1248 inode_set_mtime_to_ts(dir
,
1249 inode_set_ctime_to_ts(dir
, inode_get_ctime(inode
)));
1250 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0, 1);
1253 mutex_unlock(&dir_ui
->ui_mutex
);
1255 insert_inode_hash(inode
);
1256 d_instantiate(dentry
, inode
);
1261 dir
->i_size
-= sz_change
;
1262 dir_ui
->ui_size
= dir
->i_size
;
1263 mutex_unlock(&dir_ui
->ui_mutex
);
1264 set_nlink(inode
, 0);
1266 /* Free inode->i_link before inode is marked as bad. */
1267 fscrypt_free_inode(inode
);
1270 fscrypt_free_filename(&nm
);
1272 ubifs_release_budget(c
, &req
);
1277 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1278 * @inode1: first inode
1279 * @inode2: second inode
1280 * @inode3: third inode
1281 * @inode4: fourth inode
1283 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1284 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1286 * We do not implement any tricks to guarantee strict lock ordering, because
1287 * VFS has already done it for us on the @i_mutex. So this is just a simple
1290 static void lock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1291 struct inode
*inode3
, struct inode
*inode4
)
1293 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
1294 if (inode2
!= inode1
)
1295 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
1297 mutex_lock_nested(&ubifs_inode(inode3
)->ui_mutex
, WB_MUTEX_3
);
1299 mutex_lock_nested(&ubifs_inode(inode4
)->ui_mutex
, WB_MUTEX_4
);
1303 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1304 * @inode1: first inode
1305 * @inode2: second inode
1306 * @inode3: third inode
1307 * @inode4: fourth inode
1309 static void unlock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1310 struct inode
*inode3
, struct inode
*inode4
)
1313 mutex_unlock(&ubifs_inode(inode4
)->ui_mutex
);
1315 mutex_unlock(&ubifs_inode(inode3
)->ui_mutex
);
1316 if (inode1
!= inode2
)
1317 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
1318 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
1321 static int do_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1322 struct inode
*new_dir
, struct dentry
*new_dentry
,
1325 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1326 struct inode
*old_inode
= d_inode(old_dentry
);
1327 struct inode
*new_inode
= d_inode(new_dentry
);
1328 struct inode
*whiteout
= NULL
;
1329 struct ubifs_inode
*old_inode_ui
= ubifs_inode(old_inode
);
1330 struct ubifs_inode
*whiteout_ui
= NULL
;
1331 int err
, release
, sync
= 0, move
= (new_dir
!= old_dir
);
1332 int is_dir
= S_ISDIR(old_inode
->i_mode
);
1333 int unlink
= !!new_inode
, new_sz
, old_sz
;
1334 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1336 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1,
1337 .dirtied_ino_d
= ALIGN(old_inode_ui
->data_len
, 8) };
1338 struct ubifs_budget_req wht_req
;
1339 unsigned int saved_nlink
;
1340 struct fscrypt_name old_nm
, new_nm
;
1343 * Budget request settings:
1344 * req: deletion direntry, new direntry, removing the old inode,
1345 * and changing old and new parent directory inodes.
1347 * wht_req: new whiteout inode for RENAME_WHITEOUT.
1349 * ino_req: marks the target inode as dirty and does not write it.
1352 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1353 old_dentry
, old_inode
->i_ino
, old_dir
->i_ino
,
1354 new_dentry
, new_dir
->i_ino
, flags
);
1357 ubifs_assert(c
, inode_is_locked(new_inode
));
1359 /* Budget for old inode's data when its nlink > 1. */
1360 req
.dirtied_ino_d
= ALIGN(ubifs_inode(new_inode
)->data_len
, 8);
1361 err
= ubifs_purge_xattrs(new_inode
);
1366 if (unlink
&& is_dir
) {
1367 err
= ubifs_check_dir_empty(new_inode
);
1372 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &old_nm
);
1376 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &new_nm
);
1378 fscrypt_free_filename(&old_nm
);
1382 new_sz
= CALC_DENT_SIZE(fname_len(&new_nm
));
1383 old_sz
= CALC_DENT_SIZE(fname_len(&old_nm
));
1385 err
= ubifs_budget_space(c
, &req
);
1387 fscrypt_free_filename(&old_nm
);
1388 fscrypt_free_filename(&new_nm
);
1391 err
= ubifs_budget_space(c
, &ino_req
);
1393 fscrypt_free_filename(&old_nm
);
1394 fscrypt_free_filename(&new_nm
);
1395 ubifs_release_budget(c
, &req
);
1399 if (flags
& RENAME_WHITEOUT
) {
1400 union ubifs_dev_desc
*dev
= NULL
;
1402 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1409 * The whiteout inode without dentry is pinned in memory,
1410 * umount won't happen during rename process because we
1411 * got parent dentry.
1413 whiteout
= create_whiteout(old_dir
, old_dentry
);
1414 if (IS_ERR(whiteout
)) {
1415 err
= PTR_ERR(whiteout
);
1420 whiteout_ui
= ubifs_inode(whiteout
);
1421 whiteout_ui
->data
= dev
;
1422 whiteout_ui
->data_len
= ubifs_encode_dev(dev
, MKDEV(0, 0));
1423 ubifs_assert(c
, !whiteout_ui
->dirty
);
1425 memset(&wht_req
, 0, sizeof(struct ubifs_budget_req
));
1426 wht_req
.new_ino
= 1;
1427 wht_req
.new_ino_d
= ALIGN(whiteout_ui
->data_len
, 8);
1429 * To avoid deadlock between space budget (holds ui_mutex and
1430 * waits wb work) and writeback work(waits ui_mutex), do space
1431 * budget before ubifs inodes locked.
1433 err
= ubifs_budget_space(c
, &wht_req
);
1438 set_nlink(whiteout
, 1);
1440 /* Add the old_dentry size to the old_dir size. */
1441 old_sz
-= CALC_DENT_SIZE(fname_len(&old_nm
));
1444 lock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1447 * Like most other Unix systems, set the @i_ctime for inodes on a
1450 simple_rename_timestamp(old_dir
, old_dentry
, new_dir
, new_dentry
);
1452 /* We must adjust parent link count when renaming directories */
1456 * @old_dir loses a link because we are moving
1457 * @old_inode to a different directory.
1459 drop_nlink(old_dir
);
1461 * @new_dir only gains a link if we are not also
1462 * overwriting an existing directory.
1468 * @old_inode is not moving to a different directory,
1469 * but @old_dir still loses a link if we are
1470 * overwriting an existing directory.
1473 drop_nlink(old_dir
);
1477 old_dir
->i_size
-= old_sz
;
1478 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1481 * And finally, if we unlinked a direntry which happened to have the
1482 * same name as the moved direntry, we have to decrement @i_nlink of
1483 * the unlinked inode.
1487 * Directories cannot have hard-links, so if this is a
1488 * directory, just clear @i_nlink.
1490 saved_nlink
= new_inode
->i_nlink
;
1492 clear_nlink(new_inode
);
1494 drop_nlink(new_inode
);
1496 new_dir
->i_size
+= new_sz
;
1497 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1501 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1502 * is dirty, because this will be done later on at the end of
1505 if (IS_SYNC(old_inode
)) {
1506 sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1507 if (unlink
&& IS_SYNC(new_inode
))
1510 * S_SYNC flag of whiteout inherits from the old_dir, and we
1511 * have already checked the old dir inode. So there is no need
1512 * to check whiteout.
1516 err
= ubifs_jnl_rename(c
, old_dir
, old_inode
, &old_nm
, new_dir
,
1517 new_inode
, &new_nm
, whiteout
, sync
, !!whiteout
);
1521 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1522 ubifs_release_budget(c
, &req
);
1525 ubifs_release_budget(c
, &wht_req
);
1529 mutex_lock(&old_inode_ui
->ui_mutex
);
1530 release
= old_inode_ui
->dirty
;
1531 mark_inode_dirty_sync(old_inode
);
1532 mutex_unlock(&old_inode_ui
->ui_mutex
);
1535 ubifs_release_budget(c
, &ino_req
);
1536 if (IS_SYNC(old_inode
))
1538 * Rename finished here. Although old inode cannot be updated
1539 * on flash, old ctime is not a big problem, don't return err
1540 * code to userspace.
1542 old_inode
->i_sb
->s_op
->write_inode(old_inode
, NULL
);
1544 fscrypt_free_filename(&old_nm
);
1545 fscrypt_free_filename(&new_nm
);
1550 set_nlink(new_inode
, saved_nlink
);
1552 new_dir
->i_size
-= new_sz
;
1553 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1555 old_dir
->i_size
+= old_sz
;
1556 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1561 drop_nlink(new_dir
);
1567 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1569 ubifs_release_budget(c
, &wht_req
);
1570 set_nlink(whiteout
, 0);
1574 ubifs_release_budget(c
, &ino_req
);
1575 ubifs_release_budget(c
, &req
);
1576 fscrypt_free_filename(&old_nm
);
1577 fscrypt_free_filename(&new_nm
);
1581 static int ubifs_xrename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1582 struct inode
*new_dir
, struct dentry
*new_dentry
)
1584 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1585 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1587 int sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1588 struct inode
*fst_inode
= d_inode(old_dentry
);
1589 struct inode
*snd_inode
= d_inode(new_dentry
);
1591 struct fscrypt_name fst_nm
, snd_nm
;
1593 ubifs_assert(c
, fst_inode
&& snd_inode
);
1596 * Budget request settings: changing two direntries, changing the two
1597 * parent directory inodes.
1600 dbg_gen("dent '%pd' ino %lu in dir ino %lu exchange dent '%pd' ino %lu in dir ino %lu",
1601 old_dentry
, fst_inode
->i_ino
, old_dir
->i_ino
,
1602 new_dentry
, snd_inode
->i_ino
, new_dir
->i_ino
);
1604 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &fst_nm
);
1608 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &snd_nm
);
1610 fscrypt_free_filename(&fst_nm
);
1614 err
= ubifs_budget_space(c
, &req
);
1618 lock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1620 simple_rename_timestamp(old_dir
, old_dentry
, new_dir
, new_dentry
);
1622 if (old_dir
!= new_dir
) {
1623 if (S_ISDIR(fst_inode
->i_mode
) && !S_ISDIR(snd_inode
->i_mode
)) {
1625 drop_nlink(old_dir
);
1627 else if (!S_ISDIR(fst_inode
->i_mode
) && S_ISDIR(snd_inode
->i_mode
)) {
1628 drop_nlink(new_dir
);
1633 err
= ubifs_jnl_xrename(c
, old_dir
, fst_inode
, &fst_nm
, new_dir
,
1634 snd_inode
, &snd_nm
, sync
);
1636 unlock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1637 ubifs_release_budget(c
, &req
);
1640 fscrypt_free_filename(&fst_nm
);
1641 fscrypt_free_filename(&snd_nm
);
1645 static int ubifs_rename(struct mnt_idmap
*idmap
,
1646 struct inode
*old_dir
, struct dentry
*old_dentry
,
1647 struct inode
*new_dir
, struct dentry
*new_dentry
,
1651 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1653 if (flags
& ~(RENAME_NOREPLACE
| RENAME_WHITEOUT
| RENAME_EXCHANGE
))
1656 ubifs_assert(c
, inode_is_locked(old_dir
));
1657 ubifs_assert(c
, inode_is_locked(new_dir
));
1659 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
1664 if (flags
& RENAME_EXCHANGE
)
1665 return ubifs_xrename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1667 return do_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1670 int ubifs_getattr(struct mnt_idmap
*idmap
, const struct path
*path
,
1671 struct kstat
*stat
, u32 request_mask
, unsigned int flags
)
1674 struct inode
*inode
= d_inode(path
->dentry
);
1675 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1677 mutex_lock(&ui
->ui_mutex
);
1679 if (ui
->flags
& UBIFS_APPEND_FL
)
1680 stat
->attributes
|= STATX_ATTR_APPEND
;
1681 if (ui
->flags
& UBIFS_COMPR_FL
)
1682 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
1683 if (ui
->flags
& UBIFS_CRYPT_FL
)
1684 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
1685 if (ui
->flags
& UBIFS_IMMUTABLE_FL
)
1686 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
1688 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
1689 STATX_ATTR_COMPRESSED
|
1690 STATX_ATTR_ENCRYPTED
|
1691 STATX_ATTR_IMMUTABLE
);
1693 generic_fillattr(&nop_mnt_idmap
, request_mask
, inode
, stat
);
1694 stat
->blksize
= UBIFS_BLOCK_SIZE
;
1695 stat
->size
= ui
->ui_size
;
1698 * Unfortunately, the 'stat()' system call was designed for block
1699 * device based file systems, and it is not appropriate for UBIFS,
1700 * because UBIFS does not have notion of "block". For example, it is
1701 * difficult to tell how many block a directory takes - it actually
1702 * takes less than 300 bytes, but we have to round it to block size,
1703 * which introduces large mistake. This makes utilities like 'du' to
1704 * report completely senseless numbers. This is the reason why UBIFS
1705 * goes the same way as JFFS2 - it reports zero blocks for everything
1706 * but regular files, which makes more sense than reporting completely
1709 if (S_ISREG(inode
->i_mode
)) {
1710 size
= ui
->xattr_size
;
1712 size
= ALIGN(size
, UBIFS_BLOCK_SIZE
);
1714 * Note, user-space expects 512-byte blocks count irrespectively
1715 * of what was reported in @stat->size.
1717 stat
->blocks
= size
>> 9;
1720 mutex_unlock(&ui
->ui_mutex
);
1724 static int ubifs_dir_open(struct inode
*inode
, struct file
*file
)
1726 struct ubifs_dir_data
*data
;
1728 data
= kzalloc(sizeof(struct ubifs_dir_data
), GFP_KERNEL
);
1731 file
->private_data
= data
;
1735 static loff_t
ubifs_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
1737 struct ubifs_dir_data
*data
= file
->private_data
;
1739 return generic_llseek_cookie(file
, offset
, whence
, &data
->cookie
);
1742 const struct inode_operations ubifs_dir_inode_operations
= {
1743 .lookup
= ubifs_lookup
,
1744 .create
= ubifs_create
,
1746 .symlink
= ubifs_symlink
,
1747 .unlink
= ubifs_unlink
,
1748 .mkdir
= ubifs_mkdir
,
1749 .rmdir
= ubifs_rmdir
,
1750 .mknod
= ubifs_mknod
,
1751 .rename
= ubifs_rename
,
1752 .setattr
= ubifs_setattr
,
1753 .getattr
= ubifs_getattr
,
1754 .listxattr
= ubifs_listxattr
,
1755 .update_time
= ubifs_update_time
,
1756 .tmpfile
= ubifs_tmpfile
,
1757 .fileattr_get
= ubifs_fileattr_get
,
1758 .fileattr_set
= ubifs_fileattr_set
,
1761 const struct file_operations ubifs_dir_operations
= {
1762 .open
= ubifs_dir_open
,
1763 .llseek
= ubifs_dir_llseek
,
1764 .release
= ubifs_dir_release
,
1765 .read
= generic_read_dir
,
1766 .iterate_shared
= ubifs_readdir
,
1767 .fsync
= ubifs_fsync
,
1768 .unlocked_ioctl
= ubifs_ioctl
,
1769 #ifdef CONFIG_COMPAT
1770 .compat_ioctl
= ubifs_compat_ioctl
,