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
=
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
;
152 ui
->flags
= inherit_flags(dir
, mode
);
153 ubifs_set_inode_flags(inode
);
155 ui
->compr_type
= c
->default_compr
;
157 ui
->compr_type
= UBIFS_COMPR_NONE
;
158 ui
->synced_i_size
= 0;
160 spin_lock(&c
->cnt_lock
);
161 /* Inode number overflow is currently not supported */
162 if (c
->highest_inum
>= INUM_WARN_WATERMARK
) {
163 if (c
->highest_inum
>= INUM_WATERMARK
) {
164 spin_unlock(&c
->cnt_lock
);
165 ubifs_err(c
, "out of inode numbers");
166 make_bad_inode(inode
);
168 return ERR_PTR(-EINVAL
);
170 ubifs_warn(c
, "running out of inode numbers (current %lu, max %u)",
171 (unsigned long)c
->highest_inum
, INUM_WATERMARK
);
174 inode
->i_ino
= ++c
->highest_inum
;
176 * The creation sequence number remains with this inode for its
177 * lifetime. All nodes for this inode have a greater sequence number,
178 * and so it is possible to distinguish obsolete nodes belonging to a
179 * previous incarnation of the same inode number - for example, for the
180 * purpose of rebuilding the index.
182 ui
->creat_sqnum
= ++c
->max_sqnum
;
183 spin_unlock(&c
->cnt_lock
);
186 err
= fscrypt_inherit_context(dir
, inode
, &encrypted
, true);
188 ubifs_err(c
, "fscrypt_inherit_context failed: %i", err
);
189 make_bad_inode(inode
);
198 static int dbg_check_name(const struct ubifs_info
*c
,
199 const struct ubifs_dent_node
*dent
,
200 const struct fscrypt_name
*nm
)
202 if (!dbg_is_chk_gen(c
))
204 if (le16_to_cpu(dent
->nlen
) != fname_len(nm
))
206 if (memcmp(dent
->name
, fname_name(nm
), fname_len(nm
)))
211 static struct dentry
*ubifs_lookup(struct inode
*dir
, struct dentry
*dentry
,
216 struct inode
*inode
= NULL
;
217 struct ubifs_dent_node
*dent
;
218 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
219 struct fscrypt_name nm
;
221 dbg_gen("'%pd' in dir ino %lu", dentry
, dir
->i_ino
);
223 if (ubifs_crypt_is_encrypted(dir
)) {
224 err
= fscrypt_get_encryption_info(dir
);
227 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
228 * created while the directory was encrypted and we
229 * have access to the key.
231 if (fscrypt_has_encryption_key(dir
))
232 fscrypt_set_encrypted_dentry(dentry
);
233 fscrypt_set_d_op(dentry
);
234 if (err
&& err
!= -ENOKEY
)
238 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
242 if (fname_len(&nm
) > UBIFS_MAX_NLEN
) {
247 dent
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
254 ubifs_assert(fname_len(&nm
) == 0);
255 ubifs_assert(fname_name(&nm
) == NULL
);
256 if (nm
.hash
& ~UBIFS_S_KEY_HASH_MASK
)
257 goto done
; /* ENOENT */
258 dent_key_init_hash(c
, &key
, dir
->i_ino
, nm
.hash
);
259 err
= ubifs_tnc_lookup_dh(c
, &key
, dent
, nm
.minor_hash
);
261 dent_key_init(c
, &key
, dir
->i_ino
, &nm
);
262 err
= ubifs_tnc_lookup_nm(c
, &key
, dent
, &nm
);
266 if (err
== -ENOENT
) {
267 dbg_gen("not found");
273 if (dbg_check_name(c
, dent
, &nm
)) {
278 inode
= ubifs_iget(dir
->i_sb
, le64_to_cpu(dent
->inum
));
281 * This should not happen. Probably the file-system needs
284 err
= PTR_ERR(inode
);
285 ubifs_err(c
, "dead directory entry '%pd', error %d",
287 ubifs_ro_mode(c
, err
);
291 if (ubifs_crypt_is_encrypted(dir
) &&
292 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
293 !fscrypt_has_permitted_context(dir
, inode
)) {
294 ubifs_warn(c
, "Inconsistent encryption contexts: %lu/%lu",
295 dir
->i_ino
, inode
->i_ino
);
302 fscrypt_free_filename(&nm
);
304 * Note, d_splice_alias() would be required instead if we supported
307 d_add(dentry
, inode
);
315 fscrypt_free_filename(&nm
);
319 static int ubifs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
323 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
324 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
326 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
327 struct fscrypt_name nm
;
331 * Budget request settings: new inode, new direntry, changing the
332 * parent directory inode.
335 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
336 dentry
, mode
, dir
->i_ino
);
338 err
= ubifs_budget_space(c
, &req
);
342 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
346 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
348 inode
= ubifs_new_inode(c
, dir
, mode
);
350 err
= PTR_ERR(inode
);
354 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
358 mutex_lock(&dir_ui
->ui_mutex
);
359 dir
->i_size
+= sz_change
;
360 dir_ui
->ui_size
= dir
->i_size
;
361 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
362 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
365 mutex_unlock(&dir_ui
->ui_mutex
);
367 ubifs_release_budget(c
, &req
);
368 fscrypt_free_filename(&nm
);
369 insert_inode_hash(inode
);
370 d_instantiate(dentry
, inode
);
374 dir
->i_size
-= sz_change
;
375 dir_ui
->ui_size
= dir
->i_size
;
376 mutex_unlock(&dir_ui
->ui_mutex
);
378 make_bad_inode(inode
);
381 fscrypt_free_filename(&nm
);
383 ubifs_release_budget(c
, &req
);
384 ubifs_err(c
, "cannot create regular file, error %d", err
);
388 static int do_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
389 umode_t mode
, struct inode
**whiteout
)
392 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
393 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1};
394 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1 };
395 struct ubifs_inode
*ui
, *dir_ui
= ubifs_inode(dir
);
396 int err
, instantiated
= 0;
397 struct fscrypt_name nm
;
400 * Budget request settings: new dirty inode, new direntry,
401 * budget for dirtied inode will be released via writeback.
404 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
405 dentry
, mode
, dir
->i_ino
);
407 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
411 err
= ubifs_budget_space(c
, &req
);
413 fscrypt_free_filename(&nm
);
417 err
= ubifs_budget_space(c
, &ino_req
);
419 ubifs_release_budget(c
, &req
);
420 fscrypt_free_filename(&nm
);
424 inode
= ubifs_new_inode(c
, dir
, mode
);
426 err
= PTR_ERR(inode
);
429 ui
= ubifs_inode(inode
);
432 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
433 ubifs_assert(inode
->i_op
== &ubifs_file_inode_operations
);
436 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
440 mutex_lock(&ui
->ui_mutex
);
441 insert_inode_hash(inode
);
444 mark_inode_dirty(inode
);
448 d_tmpfile(dentry
, inode
);
450 ubifs_assert(ui
->dirty
);
453 mutex_unlock(&ui
->ui_mutex
);
455 mutex_lock(&dir_ui
->ui_mutex
);
456 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
459 mutex_unlock(&dir_ui
->ui_mutex
);
461 ubifs_release_budget(c
, &req
);
466 mutex_unlock(&dir_ui
->ui_mutex
);
468 make_bad_inode(inode
);
472 ubifs_release_budget(c
, &req
);
474 ubifs_release_budget(c
, &ino_req
);
475 fscrypt_free_filename(&nm
);
476 ubifs_err(c
, "cannot create temporary file, error %d", err
);
480 static int ubifs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
483 return do_tmpfile(dir
, dentry
, mode
, NULL
);
487 * vfs_dent_type - get VFS directory entry type.
488 * @type: UBIFS directory entry type
490 * This function converts UBIFS directory entry type into VFS directory entry
493 static unsigned int vfs_dent_type(uint8_t type
)
496 case UBIFS_ITYPE_REG
:
498 case UBIFS_ITYPE_DIR
:
500 case UBIFS_ITYPE_LNK
:
502 case UBIFS_ITYPE_BLK
:
504 case UBIFS_ITYPE_CHR
:
506 case UBIFS_ITYPE_FIFO
:
508 case UBIFS_ITYPE_SOCK
:
517 * The classical Unix view for directory is that it is a linear array of
518 * (name, inode number) entries. Linux/VFS assumes this model as well.
519 * Particularly, 'readdir()' call wants us to return a directory entry offset
520 * which later may be used to continue 'readdir()'ing the directory or to
521 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
522 * model because directory entries are identified by keys, which may collide.
524 * UBIFS uses directory entry hash value for directory offsets, so
525 * 'seekdir()'/'telldir()' may not always work because of possible key
526 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
527 * properly by means of saving full directory entry name in the private field
528 * of the file description object.
530 * This means that UBIFS cannot support NFS which requires full
531 * 'seekdir()'/'telldir()' support.
533 static int ubifs_readdir(struct file
*file
, struct dir_context
*ctx
)
535 int fstr_real_len
= 0, err
= 0;
536 struct fscrypt_name nm
;
537 struct fscrypt_str fstr
= {0};
539 struct ubifs_dent_node
*dent
;
540 struct inode
*dir
= file_inode(file
);
541 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
542 bool encrypted
= ubifs_crypt_is_encrypted(dir
);
544 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, ctx
->pos
);
546 if (ctx
->pos
> UBIFS_S_KEY_HASH_MASK
|| ctx
->pos
== 2)
548 * The directory was seek'ed to a senseless position or there
549 * are no more entries.
554 err
= fscrypt_get_encryption_info(dir
);
555 if (err
&& err
!= -ENOKEY
)
558 err
= fscrypt_fname_alloc_buffer(dir
, UBIFS_MAX_NLEN
, &fstr
);
562 fstr_real_len
= fstr
.len
;
565 if (file
->f_version
== 0) {
567 * The file was seek'ed, which means that @file->private_data
568 * is now invalid. This may also be just the first
569 * 'ubifs_readdir()' invocation, in which case
570 * @file->private_data is NULL, and the below code is
573 kfree(file
->private_data
);
574 file
->private_data
= NULL
;
578 * 'generic_file_llseek()' unconditionally sets @file->f_version to
579 * zero, and we use this for detecting whether the file was seek'ed.
583 /* File positions 0 and 1 correspond to "." and ".." */
585 ubifs_assert(!file
->private_data
);
586 if (!dir_emit_dots(file
, ctx
)) {
588 fscrypt_fname_free_buffer(&fstr
);
592 /* Find the first entry in TNC and save it */
593 lowest_dent_key(c
, &key
, dir
->i_ino
);
595 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
601 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
602 file
->private_data
= dent
;
605 dent
= file
->private_data
;
608 * The directory was seek'ed to and is now readdir'ed.
609 * Find the entry corresponding to @ctx->pos or the closest one.
611 dent_key_init_hash(c
, &key
, dir
->i_ino
, ctx
->pos
);
613 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
618 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
619 file
->private_data
= dent
;
623 dbg_gen("ino %llu, new f_pos %#x",
624 (unsigned long long)le64_to_cpu(dent
->inum
),
625 key_hash_flash(c
, &dent
->key
));
626 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) >
627 ubifs_inode(dir
)->creat_sqnum
);
629 fname_len(&nm
) = le16_to_cpu(dent
->nlen
);
630 fname_name(&nm
) = dent
->name
;
633 fstr
.len
= fstr_real_len
;
635 err
= fscrypt_fname_disk_to_usr(dir
, key_hash_flash(c
,
637 le32_to_cpu(dent
->cookie
),
638 &nm
.disk_name
, &fstr
);
642 fstr
.len
= fname_len(&nm
);
643 fstr
.name
= fname_name(&nm
);
646 if (!dir_emit(ctx
, fstr
.name
, fstr
.len
,
647 le64_to_cpu(dent
->inum
),
648 vfs_dent_type(dent
->type
))) {
650 fscrypt_fname_free_buffer(&fstr
);
654 /* Switch to the next entry */
655 key_read(c
, &dent
->key
, &key
);
656 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
662 kfree(file
->private_data
);
663 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
664 file
->private_data
= dent
;
669 kfree(file
->private_data
);
670 file
->private_data
= NULL
;
673 fscrypt_fname_free_buffer(&fstr
);
676 ubifs_err(c
, "cannot find next direntry, error %d", err
);
679 * -ENOENT is a non-fatal error in this context, the TNC uses
680 * it to indicate that the cursor moved past the current directory
681 * and readdir() has to stop.
686 /* 2 is a special value indicating that there are no more direntries */
691 /* Free saved readdir() state when the directory is closed */
692 static int ubifs_dir_release(struct inode
*dir
, struct file
*file
)
694 kfree(file
->private_data
);
695 file
->private_data
= NULL
;
700 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
701 * @inode1: first inode
702 * @inode2: second inode
704 * We do not implement any tricks to guarantee strict lock ordering, because
705 * VFS has already done it for us on the @i_mutex. So this is just a simple
708 static void lock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
710 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
711 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
715 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
716 * @inode1: first inode
717 * @inode2: second inode
719 static void unlock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
721 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
722 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
725 static int ubifs_link(struct dentry
*old_dentry
, struct inode
*dir
,
726 struct dentry
*dentry
)
728 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
729 struct inode
*inode
= d_inode(old_dentry
);
730 struct ubifs_inode
*ui
= ubifs_inode(inode
);
731 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
732 int err
, sz_change
= CALC_DENT_SIZE(dentry
->d_name
.len
);
733 struct ubifs_budget_req req
= { .new_dent
= 1, .dirtied_ino
= 2,
734 .dirtied_ino_d
= ALIGN(ui
->data_len
, 8) };
735 struct fscrypt_name nm
;
738 * Budget request settings: new direntry, changing the target inode,
739 * changing the parent inode.
742 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
743 dentry
, inode
->i_ino
,
744 inode
->i_nlink
, dir
->i_ino
);
745 ubifs_assert(inode_is_locked(dir
));
746 ubifs_assert(inode_is_locked(inode
));
748 if (ubifs_crypt_is_encrypted(dir
) &&
749 !fscrypt_has_permitted_context(dir
, inode
))
752 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
756 err
= dbg_check_synced_i_size(c
, inode
);
760 err
= ubifs_budget_space(c
, &req
);
764 lock_2_inodes(dir
, inode
);
766 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
767 if (inode
->i_nlink
== 0)
768 ubifs_delete_orphan(c
, inode
->i_ino
);
772 inode
->i_ctime
= current_time(inode
);
773 dir
->i_size
+= sz_change
;
774 dir_ui
->ui_size
= dir
->i_size
;
775 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
776 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
779 unlock_2_inodes(dir
, inode
);
781 ubifs_release_budget(c
, &req
);
782 d_instantiate(dentry
, inode
);
783 fscrypt_free_filename(&nm
);
787 dir
->i_size
-= sz_change
;
788 dir_ui
->ui_size
= dir
->i_size
;
790 if (inode
->i_nlink
== 0)
791 ubifs_add_orphan(c
, inode
->i_ino
);
792 unlock_2_inodes(dir
, inode
);
793 ubifs_release_budget(c
, &req
);
796 fscrypt_free_filename(&nm
);
800 static int ubifs_unlink(struct inode
*dir
, struct dentry
*dentry
)
802 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
803 struct inode
*inode
= d_inode(dentry
);
804 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
805 int err
, sz_change
, budgeted
= 1;
806 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
807 unsigned int saved_nlink
= inode
->i_nlink
;
808 struct fscrypt_name nm
;
811 * Budget request settings: deletion direntry, deletion inode (+1 for
812 * @dirtied_ino), changing the parent directory inode. If budgeting
813 * fails, go ahead anyway because we have extra space reserved for
817 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
818 dentry
, inode
->i_ino
,
819 inode
->i_nlink
, dir
->i_ino
);
821 if (ubifs_crypt_is_encrypted(dir
)) {
822 err
= fscrypt_get_encryption_info(dir
);
823 if (err
&& err
!= -ENOKEY
)
827 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
831 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
833 ubifs_assert(inode_is_locked(dir
));
834 ubifs_assert(inode_is_locked(inode
));
835 err
= dbg_check_synced_i_size(c
, inode
);
839 err
= ubifs_budget_space(c
, &req
);
846 lock_2_inodes(dir
, inode
);
847 inode
->i_ctime
= current_time(dir
);
849 dir
->i_size
-= sz_change
;
850 dir_ui
->ui_size
= dir
->i_size
;
851 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
852 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
855 unlock_2_inodes(dir
, inode
);
858 ubifs_release_budget(c
, &req
);
860 /* We've deleted something - clean the "no space" flags */
861 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
864 fscrypt_free_filename(&nm
);
868 dir
->i_size
+= sz_change
;
869 dir_ui
->ui_size
= dir
->i_size
;
870 set_nlink(inode
, saved_nlink
);
871 unlock_2_inodes(dir
, inode
);
873 ubifs_release_budget(c
, &req
);
875 fscrypt_free_filename(&nm
);
880 * check_dir_empty - check if a directory is empty or not.
881 * @dir: VFS inode object of the directory to check
883 * This function checks if directory @dir is empty. Returns zero if the
884 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
885 * in case of of errors.
887 int ubifs_check_dir_empty(struct inode
*dir
)
889 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
890 struct fscrypt_name nm
= { 0 };
891 struct ubifs_dent_node
*dent
;
895 lowest_dent_key(c
, &key
, dir
->i_ino
);
896 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
908 static int ubifs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
910 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
911 struct inode
*inode
= d_inode(dentry
);
912 int err
, sz_change
, budgeted
= 1;
913 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
914 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
915 struct fscrypt_name nm
;
918 * Budget request settings: deletion direntry, deletion inode and
919 * changing the parent inode. If budgeting fails, go ahead anyway
920 * because we have extra space reserved for deletions.
923 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry
,
924 inode
->i_ino
, dir
->i_ino
);
925 ubifs_assert(inode_is_locked(dir
));
926 ubifs_assert(inode_is_locked(inode
));
927 err
= ubifs_check_dir_empty(d_inode(dentry
));
931 if (ubifs_crypt_is_encrypted(dir
)) {
932 err
= fscrypt_get_encryption_info(dir
);
933 if (err
&& err
!= -ENOKEY
)
937 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
941 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
943 err
= ubifs_budget_space(c
, &req
);
950 lock_2_inodes(dir
, inode
);
951 inode
->i_ctime
= current_time(dir
);
954 dir
->i_size
-= sz_change
;
955 dir_ui
->ui_size
= dir
->i_size
;
956 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
957 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
960 unlock_2_inodes(dir
, inode
);
963 ubifs_release_budget(c
, &req
);
965 /* We've deleted something - clean the "no space" flags */
966 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
969 fscrypt_free_filename(&nm
);
973 dir
->i_size
+= sz_change
;
974 dir_ui
->ui_size
= dir
->i_size
;
977 unlock_2_inodes(dir
, inode
);
979 ubifs_release_budget(c
, &req
);
981 fscrypt_free_filename(&nm
);
985 static int ubifs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
988 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
989 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
991 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1 };
992 struct fscrypt_name nm
;
995 * Budget request settings: new inode, new direntry and changing parent
999 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
1000 dentry
, mode
, dir
->i_ino
);
1002 err
= ubifs_budget_space(c
, &req
);
1006 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1010 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1012 inode
= ubifs_new_inode(c
, dir
, S_IFDIR
| mode
);
1013 if (IS_ERR(inode
)) {
1014 err
= PTR_ERR(inode
);
1018 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1022 mutex_lock(&dir_ui
->ui_mutex
);
1023 insert_inode_hash(inode
);
1026 dir
->i_size
+= sz_change
;
1027 dir_ui
->ui_size
= dir
->i_size
;
1028 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1029 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
1031 ubifs_err(c
, "cannot create directory, error %d", err
);
1034 mutex_unlock(&dir_ui
->ui_mutex
);
1036 ubifs_release_budget(c
, &req
);
1037 d_instantiate(dentry
, inode
);
1038 fscrypt_free_filename(&nm
);
1042 dir
->i_size
-= sz_change
;
1043 dir_ui
->ui_size
= dir
->i_size
;
1045 mutex_unlock(&dir_ui
->ui_mutex
);
1047 make_bad_inode(inode
);
1050 fscrypt_free_filename(&nm
);
1052 ubifs_release_budget(c
, &req
);
1056 static int ubifs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1057 umode_t mode
, dev_t rdev
)
1059 struct inode
*inode
;
1060 struct ubifs_inode
*ui
;
1061 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1062 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1063 union ubifs_dev_desc
*dev
= NULL
;
1065 int err
, devlen
= 0;
1066 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1068 struct fscrypt_name nm
;
1071 * Budget request settings: new inode, new direntry and changing parent
1075 dbg_gen("dent '%pd' in dir ino %lu", dentry
, dir
->i_ino
);
1077 if (S_ISBLK(mode
) || S_ISCHR(mode
)) {
1078 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1081 devlen
= ubifs_encode_dev(dev
, rdev
);
1084 req
.new_ino_d
= ALIGN(devlen
, 8);
1085 err
= ubifs_budget_space(c
, &req
);
1091 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1097 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1099 inode
= ubifs_new_inode(c
, dir
, mode
);
1100 if (IS_ERR(inode
)) {
1102 err
= PTR_ERR(inode
);
1106 init_special_inode(inode
, inode
->i_mode
, rdev
);
1107 inode
->i_size
= ubifs_inode(inode
)->ui_size
= devlen
;
1108 ui
= ubifs_inode(inode
);
1110 ui
->data_len
= devlen
;
1112 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1116 mutex_lock(&dir_ui
->ui_mutex
);
1117 dir
->i_size
+= sz_change
;
1118 dir_ui
->ui_size
= dir
->i_size
;
1119 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1120 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
1123 mutex_unlock(&dir_ui
->ui_mutex
);
1125 ubifs_release_budget(c
, &req
);
1126 insert_inode_hash(inode
);
1127 d_instantiate(dentry
, inode
);
1128 fscrypt_free_filename(&nm
);
1132 dir
->i_size
-= sz_change
;
1133 dir_ui
->ui_size
= dir
->i_size
;
1134 mutex_unlock(&dir_ui
->ui_mutex
);
1136 make_bad_inode(inode
);
1139 fscrypt_free_filename(&nm
);
1141 ubifs_release_budget(c
, &req
);
1145 static int ubifs_symlink(struct inode
*dir
, struct dentry
*dentry
,
1146 const char *symname
)
1148 struct inode
*inode
;
1149 struct ubifs_inode
*ui
;
1150 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1151 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1152 int err
, sz_change
, len
= strlen(symname
);
1153 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
1154 struct fscrypt_symlink_data
*sd
= NULL
;
1155 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1156 .new_ino_d
= ALIGN(len
, 8),
1158 struct fscrypt_name nm
;
1160 if (ubifs_crypt_is_encrypted(dir
)) {
1161 err
= fscrypt_get_encryption_info(dir
);
1165 if (!fscrypt_has_encryption_key(dir
)) {
1170 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
1171 sizeof(struct fscrypt_symlink_data
));
1175 * Budget request settings: new inode, new direntry and changing parent
1179 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry
,
1180 symname
, dir
->i_ino
);
1182 if (disk_link
.len
> UBIFS_MAX_INO_DATA
)
1183 return -ENAMETOOLONG
;
1185 err
= ubifs_budget_space(c
, &req
);
1189 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1193 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1195 inode
= ubifs_new_inode(c
, dir
, S_IFLNK
| S_IRWXUGO
);
1196 if (IS_ERR(inode
)) {
1197 err
= PTR_ERR(inode
);
1201 ui
= ubifs_inode(inode
);
1202 ui
->data
= kmalloc(disk_link
.len
, GFP_NOFS
);
1208 if (ubifs_crypt_is_encrypted(dir
)) {
1209 struct qstr istr
= QSTR_INIT(symname
, len
);
1210 struct fscrypt_str ostr
;
1212 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
1218 ostr
.name
= sd
->encrypted_path
;
1219 ostr
.len
= disk_link
.len
;
1221 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
1225 sd
->len
= cpu_to_le16(ostr
.len
);
1226 disk_link
.name
= (char *)sd
;
1228 inode
->i_link
= ui
->data
;
1231 memcpy(ui
->data
, disk_link
.name
, disk_link
.len
);
1232 ((char *)ui
->data
)[disk_link
.len
- 1] = '\0';
1235 * The terminating zero byte is not written to the flash media and it
1236 * is put just to make later in-memory string processing simpler. Thus,
1237 * data length is @len, not @len + %1.
1239 ui
->data_len
= disk_link
.len
- 1;
1240 inode
->i_size
= ubifs_inode(inode
)->ui_size
= disk_link
.len
- 1;
1242 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1246 mutex_lock(&dir_ui
->ui_mutex
);
1247 dir
->i_size
+= sz_change
;
1248 dir_ui
->ui_size
= dir
->i_size
;
1249 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1250 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
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
);
1265 make_bad_inode(inode
);
1268 fscrypt_free_filename(&nm
);
1270 ubifs_release_budget(c
, &req
);
1276 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1277 * @inode1: first inode
1278 * @inode2: second inode
1279 * @inode3: third inode
1280 * @inode4: fouth inode
1282 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1283 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1285 * We do not implement any tricks to guarantee strict lock ordering, because
1286 * VFS has already done it for us on the @i_mutex. So this is just a simple
1289 static void lock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1290 struct inode
*inode3
, struct inode
*inode4
)
1292 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
1293 if (inode2
!= inode1
)
1294 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
1296 mutex_lock_nested(&ubifs_inode(inode3
)->ui_mutex
, WB_MUTEX_3
);
1298 mutex_lock_nested(&ubifs_inode(inode4
)->ui_mutex
, WB_MUTEX_4
);
1302 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1303 * @inode1: first inode
1304 * @inode2: second inode
1305 * @inode3: third inode
1306 * @inode4: fouth inode
1308 static void unlock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1309 struct inode
*inode3
, struct inode
*inode4
)
1312 mutex_unlock(&ubifs_inode(inode4
)->ui_mutex
);
1314 mutex_unlock(&ubifs_inode(inode3
)->ui_mutex
);
1315 if (inode1
!= inode2
)
1316 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
1317 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
1320 static int do_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1321 struct inode
*new_dir
, struct dentry
*new_dentry
,
1324 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1325 struct inode
*old_inode
= d_inode(old_dentry
);
1326 struct inode
*new_inode
= d_inode(new_dentry
);
1327 struct inode
*whiteout
= NULL
;
1328 struct ubifs_inode
*old_inode_ui
= ubifs_inode(old_inode
);
1329 struct ubifs_inode
*whiteout_ui
= NULL
;
1330 int err
, release
, sync
= 0, move
= (new_dir
!= old_dir
);
1331 int is_dir
= S_ISDIR(old_inode
->i_mode
);
1332 int unlink
= !!new_inode
, new_sz
, old_sz
;
1333 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1335 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1,
1336 .dirtied_ino_d
= ALIGN(old_inode_ui
->data_len
, 8) };
1337 struct timespec time
;
1338 unsigned int uninitialized_var(saved_nlink
);
1339 struct fscrypt_name old_nm
, new_nm
;
1342 * Budget request settings: deletion direntry, new direntry, removing
1343 * the old inode, and changing old and new parent directory inodes.
1345 * However, this operation also marks the target inode as dirty and
1346 * does not write it, so we allocate budget for the target inode
1350 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1351 old_dentry
, old_inode
->i_ino
, old_dir
->i_ino
,
1352 new_dentry
, new_dir
->i_ino
, flags
);
1355 ubifs_assert(inode_is_locked(new_inode
));
1357 if (old_dir
!= new_dir
) {
1358 if (ubifs_crypt_is_encrypted(new_dir
) &&
1359 !fscrypt_has_permitted_context(new_dir
, old_inode
))
1363 if (unlink
&& is_dir
) {
1364 err
= ubifs_check_dir_empty(new_inode
);
1369 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &old_nm
);
1373 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &new_nm
);
1375 fscrypt_free_filename(&old_nm
);
1379 new_sz
= CALC_DENT_SIZE(fname_len(&new_nm
));
1380 old_sz
= CALC_DENT_SIZE(fname_len(&old_nm
));
1382 err
= ubifs_budget_space(c
, &req
);
1384 fscrypt_free_filename(&old_nm
);
1385 fscrypt_free_filename(&new_nm
);
1388 err
= ubifs_budget_space(c
, &ino_req
);
1390 fscrypt_free_filename(&old_nm
);
1391 fscrypt_free_filename(&new_nm
);
1392 ubifs_release_budget(c
, &req
);
1396 if (flags
& RENAME_WHITEOUT
) {
1397 union ubifs_dev_desc
*dev
= NULL
;
1399 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1405 err
= do_tmpfile(old_dir
, old_dentry
, S_IFCHR
| WHITEOUT_MODE
, &whiteout
);
1411 whiteout
->i_state
|= I_LINKABLE
;
1412 whiteout_ui
= ubifs_inode(whiteout
);
1413 whiteout_ui
->data
= dev
;
1414 whiteout_ui
->data_len
= ubifs_encode_dev(dev
, MKDEV(0, 0));
1415 ubifs_assert(!whiteout_ui
->dirty
);
1418 lock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1421 * Like most other Unix systems, set the @i_ctime for inodes on a
1424 time
= current_time(old_dir
);
1425 old_inode
->i_ctime
= time
;
1427 /* We must adjust parent link count when renaming directories */
1431 * @old_dir loses a link because we are moving
1432 * @old_inode to a different directory.
1434 drop_nlink(old_dir
);
1436 * @new_dir only gains a link if we are not also
1437 * overwriting an existing directory.
1443 * @old_inode is not moving to a different directory,
1444 * but @old_dir still loses a link if we are
1445 * overwriting an existing directory.
1448 drop_nlink(old_dir
);
1452 old_dir
->i_size
-= old_sz
;
1453 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1454 old_dir
->i_mtime
= old_dir
->i_ctime
= time
;
1455 new_dir
->i_mtime
= new_dir
->i_ctime
= time
;
1458 * And finally, if we unlinked a direntry which happened to have the
1459 * same name as the moved direntry, we have to decrement @i_nlink of
1460 * the unlinked inode and change its ctime.
1464 * Directories cannot have hard-links, so if this is a
1465 * directory, just clear @i_nlink.
1467 saved_nlink
= new_inode
->i_nlink
;
1469 clear_nlink(new_inode
);
1471 drop_nlink(new_inode
);
1472 new_inode
->i_ctime
= time
;
1474 new_dir
->i_size
+= new_sz
;
1475 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1479 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1480 * is dirty, because this will be done later on at the end of
1483 if (IS_SYNC(old_inode
)) {
1484 sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1485 if (unlink
&& IS_SYNC(new_inode
))
1490 struct ubifs_budget_req wht_req
= { .dirtied_ino
= 1,
1492 ALIGN(ubifs_inode(whiteout
)->data_len
, 8) };
1494 err
= ubifs_budget_space(c
, &wht_req
);
1496 kfree(whiteout_ui
->data
);
1497 whiteout_ui
->data_len
= 0;
1502 inc_nlink(whiteout
);
1503 mark_inode_dirty(whiteout
);
1504 whiteout
->i_state
&= ~I_LINKABLE
;
1508 err
= ubifs_jnl_rename(c
, old_dir
, old_inode
, &old_nm
, new_dir
,
1509 new_inode
, &new_nm
, whiteout
, sync
);
1513 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1514 ubifs_release_budget(c
, &req
);
1516 mutex_lock(&old_inode_ui
->ui_mutex
);
1517 release
= old_inode_ui
->dirty
;
1518 mark_inode_dirty_sync(old_inode
);
1519 mutex_unlock(&old_inode_ui
->ui_mutex
);
1522 ubifs_release_budget(c
, &ino_req
);
1523 if (IS_SYNC(old_inode
))
1524 err
= old_inode
->i_sb
->s_op
->write_inode(old_inode
, NULL
);
1526 fscrypt_free_filename(&old_nm
);
1527 fscrypt_free_filename(&new_nm
);
1532 set_nlink(new_inode
, saved_nlink
);
1534 new_dir
->i_size
-= new_sz
;
1535 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1537 old_dir
->i_size
+= old_sz
;
1538 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1543 drop_nlink(new_dir
);
1550 drop_nlink(whiteout
);
1553 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1555 ubifs_release_budget(c
, &ino_req
);
1556 ubifs_release_budget(c
, &req
);
1557 fscrypt_free_filename(&old_nm
);
1558 fscrypt_free_filename(&new_nm
);
1562 static int ubifs_xrename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1563 struct inode
*new_dir
, struct dentry
*new_dentry
)
1565 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1566 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1568 int sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1569 struct inode
*fst_inode
= d_inode(old_dentry
);
1570 struct inode
*snd_inode
= d_inode(new_dentry
);
1571 struct timespec time
;
1573 struct fscrypt_name fst_nm
, snd_nm
;
1575 ubifs_assert(fst_inode
&& snd_inode
);
1577 if ((ubifs_crypt_is_encrypted(old_dir
) ||
1578 ubifs_crypt_is_encrypted(new_dir
)) &&
1579 (old_dir
!= new_dir
) &&
1580 (!fscrypt_has_permitted_context(new_dir
, fst_inode
) ||
1581 !fscrypt_has_permitted_context(old_dir
, snd_inode
)))
1584 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &fst_nm
);
1588 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &snd_nm
);
1590 fscrypt_free_filename(&fst_nm
);
1594 lock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1596 time
= current_time(old_dir
);
1597 fst_inode
->i_ctime
= time
;
1598 snd_inode
->i_ctime
= time
;
1599 old_dir
->i_mtime
= old_dir
->i_ctime
= time
;
1600 new_dir
->i_mtime
= new_dir
->i_ctime
= time
;
1602 if (old_dir
!= new_dir
) {
1603 if (S_ISDIR(fst_inode
->i_mode
) && !S_ISDIR(snd_inode
->i_mode
)) {
1605 drop_nlink(old_dir
);
1607 else if (!S_ISDIR(fst_inode
->i_mode
) && S_ISDIR(snd_inode
->i_mode
)) {
1608 drop_nlink(new_dir
);
1613 err
= ubifs_jnl_xrename(c
, old_dir
, fst_inode
, &fst_nm
, new_dir
,
1614 snd_inode
, &snd_nm
, sync
);
1616 unlock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1617 ubifs_release_budget(c
, &req
);
1619 fscrypt_free_filename(&fst_nm
);
1620 fscrypt_free_filename(&snd_nm
);
1624 static int ubifs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1625 struct inode
*new_dir
, struct dentry
*new_dentry
,
1628 if (flags
& ~(RENAME_NOREPLACE
| RENAME_WHITEOUT
| RENAME_EXCHANGE
))
1631 ubifs_assert(inode_is_locked(old_dir
));
1632 ubifs_assert(inode_is_locked(new_dir
));
1634 if (flags
& RENAME_EXCHANGE
)
1635 return ubifs_xrename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1637 return do_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1640 int ubifs_getattr(const struct path
*path
, struct kstat
*stat
,
1641 u32 request_mask
, unsigned int flags
)
1644 struct inode
*inode
= d_inode(path
->dentry
);
1645 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1647 mutex_lock(&ui
->ui_mutex
);
1649 if (ui
->flags
& UBIFS_APPEND_FL
)
1650 stat
->attributes
|= STATX_ATTR_APPEND
;
1651 if (ui
->flags
& UBIFS_COMPR_FL
)
1652 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
1653 if (ui
->flags
& UBIFS_CRYPT_FL
)
1654 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
1655 if (ui
->flags
& UBIFS_IMMUTABLE_FL
)
1656 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
1658 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
1659 STATX_ATTR_COMPRESSED
|
1660 STATX_ATTR_ENCRYPTED
|
1661 STATX_ATTR_IMMUTABLE
);
1663 generic_fillattr(inode
, stat
);
1664 stat
->blksize
= UBIFS_BLOCK_SIZE
;
1665 stat
->size
= ui
->ui_size
;
1668 * Unfortunately, the 'stat()' system call was designed for block
1669 * device based file systems, and it is not appropriate for UBIFS,
1670 * because UBIFS does not have notion of "block". For example, it is
1671 * difficult to tell how many block a directory takes - it actually
1672 * takes less than 300 bytes, but we have to round it to block size,
1673 * which introduces large mistake. This makes utilities like 'du' to
1674 * report completely senseless numbers. This is the reason why UBIFS
1675 * goes the same way as JFFS2 - it reports zero blocks for everything
1676 * but regular files, which makes more sense than reporting completely
1679 if (S_ISREG(inode
->i_mode
)) {
1680 size
= ui
->xattr_size
;
1682 size
= ALIGN(size
, UBIFS_BLOCK_SIZE
);
1684 * Note, user-space expects 512-byte blocks count irrespectively
1685 * of what was reported in @stat->size.
1687 stat
->blocks
= size
>> 9;
1690 mutex_unlock(&ui
->ui_mutex
);
1694 static int ubifs_dir_open(struct inode
*dir
, struct file
*file
)
1696 if (ubifs_crypt_is_encrypted(dir
))
1697 return fscrypt_get_encryption_info(dir
) ? -EACCES
: 0;
1702 const struct inode_operations ubifs_dir_inode_operations
= {
1703 .lookup
= ubifs_lookup
,
1704 .create
= ubifs_create
,
1706 .symlink
= ubifs_symlink
,
1707 .unlink
= ubifs_unlink
,
1708 .mkdir
= ubifs_mkdir
,
1709 .rmdir
= ubifs_rmdir
,
1710 .mknod
= ubifs_mknod
,
1711 .rename
= ubifs_rename
,
1712 .setattr
= ubifs_setattr
,
1713 .getattr
= ubifs_getattr
,
1714 .listxattr
= ubifs_listxattr
,
1715 #ifdef CONFIG_UBIFS_ATIME_SUPPORT
1716 .update_time
= ubifs_update_time
,
1718 .tmpfile
= ubifs_tmpfile
,
1721 const struct file_operations ubifs_dir_operations
= {
1722 .llseek
= generic_file_llseek
,
1723 .release
= ubifs_dir_release
,
1724 .read
= generic_read_dir
,
1725 .iterate_shared
= ubifs_readdir
,
1726 .fsync
= ubifs_fsync
,
1727 .unlocked_ioctl
= ubifs_ioctl
,
1728 .open
= ubifs_dir_open
,
1729 #ifdef CONFIG_COMPAT
1730 .compat_ioctl
= ubifs_compat_ioctl
,