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 fscrypt_name
*nm
)
201 if (!dbg_is_chk_gen(c
))
203 if (le16_to_cpu(dent
->nlen
) != fname_len(nm
))
205 if (memcmp(dent
->name
, fname_name(nm
), fname_len(nm
)))
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
;
218 struct fscrypt_name nm
;
220 dbg_gen("'%pd' in dir ino %lu", dentry
, dir
->i_ino
);
222 if (ubifs_crypt_is_encrypted(dir
)) {
223 err
= fscrypt_get_encryption_info(dir
);
226 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
227 * created while the directory was encrypted and we
228 * have access to the key.
230 if (fscrypt_has_encryption_key(dir
))
231 fscrypt_set_encrypted_dentry(dentry
);
232 fscrypt_set_d_op(dentry
);
233 if (err
&& err
!= -ENOKEY
)
237 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
241 if (fname_len(&nm
) > UBIFS_MAX_NLEN
) {
246 dent
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
253 ubifs_assert(fname_len(&nm
) == 0);
254 ubifs_assert(fname_name(&nm
) == NULL
);
255 dent_key_init_hash(c
, &key
, dir
->i_ino
, nm
.hash
);
256 err
= ubifs_tnc_lookup_dh(c
, &key
, dent
, nm
.minor_hash
);
258 dent_key_init(c
, &key
, dir
->i_ino
, &nm
);
259 err
= ubifs_tnc_lookup_nm(c
, &key
, dent
, &nm
);
263 if (err
== -ENOENT
) {
264 dbg_gen("not found");
270 if (dbg_check_name(c
, dent
, &nm
)) {
275 inode
= ubifs_iget(dir
->i_sb
, le64_to_cpu(dent
->inum
));
278 * This should not happen. Probably the file-system needs
281 err
= PTR_ERR(inode
);
282 ubifs_err(c
, "dead directory entry '%pd', error %d",
284 ubifs_ro_mode(c
, err
);
290 fscrypt_free_filename(&nm
);
292 * Note, d_splice_alias() would be required instead if we supported
295 d_add(dentry
, inode
);
301 fscrypt_free_filename(&nm
);
305 static int ubifs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
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
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
332 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
334 inode
= ubifs_new_inode(c
, dir
, mode
);
336 err
= PTR_ERR(inode
);
340 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
344 mutex_lock(&dir_ui
->ui_mutex
);
345 dir
->i_size
+= sz_change
;
346 dir_ui
->ui_size
= dir
->i_size
;
347 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
348 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
351 mutex_unlock(&dir_ui
->ui_mutex
);
353 ubifs_release_budget(c
, &req
);
354 fscrypt_free_filename(&nm
);
355 insert_inode_hash(inode
);
356 d_instantiate(dentry
, inode
);
360 dir
->i_size
-= sz_change
;
361 dir_ui
->ui_size
= dir
->i_size
;
362 mutex_unlock(&dir_ui
->ui_mutex
);
364 make_bad_inode(inode
);
367 fscrypt_free_filename(&nm
);
369 ubifs_release_budget(c
, &req
);
370 ubifs_err(c
, "cannot create regular file, error %d", err
);
374 static int do_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
375 umode_t mode
, struct inode
**whiteout
)
378 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
379 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1};
380 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1 };
381 struct ubifs_inode
*ui
, *dir_ui
= ubifs_inode(dir
);
382 int err
, instantiated
= 0;
383 struct fscrypt_name nm
;
386 * Budget request settings: new dirty inode, new direntry,
387 * budget for dirtied inode will be released via writeback.
390 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
391 dentry
, mode
, dir
->i_ino
);
393 if (ubifs_crypt_is_encrypted(dir
)) {
394 err
= fscrypt_get_encryption_info(dir
);
398 if (!fscrypt_has_encryption_key(dir
)) {
403 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
407 err
= ubifs_budget_space(c
, &req
);
409 fscrypt_free_filename(&nm
);
413 err
= ubifs_budget_space(c
, &ino_req
);
415 ubifs_release_budget(c
, &req
);
416 fscrypt_free_filename(&nm
);
420 inode
= ubifs_new_inode(c
, dir
, mode
);
422 err
= PTR_ERR(inode
);
425 ui
= ubifs_inode(inode
);
428 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
429 ubifs_assert(inode
->i_op
== &ubifs_file_inode_operations
);
432 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
436 mutex_lock(&ui
->ui_mutex
);
437 insert_inode_hash(inode
);
440 mark_inode_dirty(inode
);
444 d_tmpfile(dentry
, inode
);
446 ubifs_assert(ui
->dirty
);
449 mutex_unlock(&ui
->ui_mutex
);
451 mutex_lock(&dir_ui
->ui_mutex
);
452 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
455 mutex_unlock(&dir_ui
->ui_mutex
);
457 ubifs_release_budget(c
, &req
);
462 mutex_unlock(&dir_ui
->ui_mutex
);
464 make_bad_inode(inode
);
468 ubifs_release_budget(c
, &req
);
470 ubifs_release_budget(c
, &ino_req
);
471 fscrypt_free_filename(&nm
);
472 ubifs_err(c
, "cannot create temporary file, error %d", err
);
476 static int ubifs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
479 return do_tmpfile(dir
, dentry
, mode
, NULL
);
483 * vfs_dent_type - get VFS directory entry type.
484 * @type: UBIFS directory entry type
486 * This function converts UBIFS directory entry type into VFS directory entry
489 static unsigned int vfs_dent_type(uint8_t type
)
492 case UBIFS_ITYPE_REG
:
494 case UBIFS_ITYPE_DIR
:
496 case UBIFS_ITYPE_LNK
:
498 case UBIFS_ITYPE_BLK
:
500 case UBIFS_ITYPE_CHR
:
502 case UBIFS_ITYPE_FIFO
:
504 case UBIFS_ITYPE_SOCK
:
513 * The classical Unix view for directory is that it is a linear array of
514 * (name, inode number) entries. Linux/VFS assumes this model as well.
515 * Particularly, 'readdir()' call wants us to return a directory entry offset
516 * which later may be used to continue 'readdir()'ing the directory or to
517 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
518 * model because directory entries are identified by keys, which may collide.
520 * UBIFS uses directory entry hash value for directory offsets, so
521 * 'seekdir()'/'telldir()' may not always work because of possible key
522 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
523 * properly by means of saving full directory entry name in the private field
524 * of the file description object.
526 * This means that UBIFS cannot support NFS which requires full
527 * 'seekdir()'/'telldir()' support.
529 static int ubifs_readdir(struct file
*file
, struct dir_context
*ctx
)
531 int fstr_real_len
= 0, err
= 0;
532 struct fscrypt_name nm
;
533 struct fscrypt_str fstr
= {0};
535 struct ubifs_dent_node
*dent
;
536 struct inode
*dir
= file_inode(file
);
537 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
538 bool encrypted
= ubifs_crypt_is_encrypted(dir
);
540 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, ctx
->pos
);
542 if (ctx
->pos
> UBIFS_S_KEY_HASH_MASK
|| ctx
->pos
== 2)
544 * The directory was seek'ed to a senseless position or there
545 * are no more entries.
550 err
= fscrypt_get_encryption_info(dir
);
551 if (err
&& err
!= -ENOKEY
)
554 err
= fscrypt_fname_alloc_buffer(dir
, UBIFS_MAX_NLEN
, &fstr
);
558 fstr_real_len
= fstr
.len
;
561 if (file
->f_version
== 0) {
563 * The file was seek'ed, which means that @file->private_data
564 * is now invalid. This may also be just the first
565 * 'ubifs_readdir()' invocation, in which case
566 * @file->private_data is NULL, and the below code is
569 kfree(file
->private_data
);
570 file
->private_data
= NULL
;
574 * 'generic_file_llseek()' unconditionally sets @file->f_version to
575 * zero, and we use this for detecting whether the file was seek'ed.
579 /* File positions 0 and 1 correspond to "." and ".." */
581 ubifs_assert(!file
->private_data
);
582 if (!dir_emit_dots(file
, ctx
)) {
584 fscrypt_fname_free_buffer(&fstr
);
588 /* Find the first entry in TNC and save it */
589 lowest_dent_key(c
, &key
, dir
->i_ino
);
591 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
597 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
598 file
->private_data
= dent
;
601 dent
= file
->private_data
;
604 * The directory was seek'ed to and is now readdir'ed.
605 * Find the entry corresponding to @ctx->pos or the closest one.
607 dent_key_init_hash(c
, &key
, dir
->i_ino
, ctx
->pos
);
609 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
614 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
615 file
->private_data
= dent
;
619 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
620 dent
->name
, (unsigned long long)le64_to_cpu(dent
->inum
),
621 key_hash_flash(c
, &dent
->key
));
622 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) >
623 ubifs_inode(dir
)->creat_sqnum
);
625 fname_len(&nm
) = le16_to_cpu(dent
->nlen
);
626 fname_name(&nm
) = dent
->name
;
629 fstr
.len
= fstr_real_len
;
631 err
= fscrypt_fname_disk_to_usr(dir
, key_hash_flash(c
,
633 le32_to_cpu(dent
->cookie
),
634 &nm
.disk_name
, &fstr
);
638 fstr
.len
= fname_len(&nm
);
639 fstr
.name
= fname_name(&nm
);
642 if (!dir_emit(ctx
, fstr
.name
, fstr
.len
,
643 le64_to_cpu(dent
->inum
),
644 vfs_dent_type(dent
->type
))) {
646 fscrypt_fname_free_buffer(&fstr
);
650 /* Switch to the next entry */
651 key_read(c
, &dent
->key
, &key
);
652 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
658 kfree(file
->private_data
);
659 ctx
->pos
= key_hash_flash(c
, &dent
->key
);
660 file
->private_data
= dent
;
665 kfree(file
->private_data
);
666 file
->private_data
= NULL
;
669 fscrypt_fname_free_buffer(&fstr
);
672 ubifs_err(c
, "cannot find next direntry, error %d", err
);
675 * -ENOENT is a non-fatal error in this context, the TNC uses
676 * it to indicate that the cursor moved past the current directory
677 * and readdir() has to stop.
682 /* 2 is a special value indicating that there are no more direntries */
687 /* Free saved readdir() state when the directory is closed */
688 static int ubifs_dir_release(struct inode
*dir
, struct file
*file
)
690 kfree(file
->private_data
);
691 file
->private_data
= NULL
;
696 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
697 * @inode1: first inode
698 * @inode2: second inode
700 * We do not implement any tricks to guarantee strict lock ordering, because
701 * VFS has already done it for us on the @i_mutex. So this is just a simple
704 static void lock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
706 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
707 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
711 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
712 * @inode1: first inode
713 * @inode2: second inode
715 static void unlock_2_inodes(struct inode
*inode1
, struct inode
*inode2
)
717 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
718 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
721 static int ubifs_link(struct dentry
*old_dentry
, struct inode
*dir
,
722 struct dentry
*dentry
)
724 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
725 struct inode
*inode
= d_inode(old_dentry
);
726 struct ubifs_inode
*ui
= ubifs_inode(inode
);
727 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
728 int err
, sz_change
= CALC_DENT_SIZE(dentry
->d_name
.len
);
729 struct ubifs_budget_req req
= { .new_dent
= 1, .dirtied_ino
= 2,
730 .dirtied_ino_d
= ALIGN(ui
->data_len
, 8) };
731 struct fscrypt_name nm
;
734 * Budget request settings: new direntry, changing the target inode,
735 * changing the parent inode.
738 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
739 dentry
, inode
->i_ino
,
740 inode
->i_nlink
, dir
->i_ino
);
741 ubifs_assert(inode_is_locked(dir
));
742 ubifs_assert(inode_is_locked(inode
));
744 if (ubifs_crypt_is_encrypted(dir
)) {
745 if (!fscrypt_has_permitted_context(dir
, inode
))
748 err
= fscrypt_get_encryption_info(inode
);
752 if (!fscrypt_has_encryption_key(inode
))
756 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
760 err
= dbg_check_synced_i_size(c
, inode
);
764 err
= ubifs_budget_space(c
, &req
);
768 lock_2_inodes(dir
, inode
);
771 inode
->i_ctime
= ubifs_current_time(inode
);
772 dir
->i_size
+= sz_change
;
773 dir_ui
->ui_size
= dir
->i_size
;
774 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
775 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
778 unlock_2_inodes(dir
, inode
);
780 ubifs_release_budget(c
, &req
);
781 d_instantiate(dentry
, inode
);
782 fscrypt_free_filename(&nm
);
786 dir
->i_size
-= sz_change
;
787 dir_ui
->ui_size
= dir
->i_size
;
789 unlock_2_inodes(dir
, inode
);
790 ubifs_release_budget(c
, &req
);
793 fscrypt_free_filename(&nm
);
797 static int ubifs_unlink(struct inode
*dir
, struct dentry
*dentry
)
799 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
800 struct inode
*inode
= d_inode(dentry
);
801 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
802 int err
, sz_change
, budgeted
= 1;
803 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
804 unsigned int saved_nlink
= inode
->i_nlink
;
805 struct fscrypt_name nm
;
808 * Budget request settings: deletion direntry, deletion inode (+1 for
809 * @dirtied_ino), changing the parent directory inode. If budgeting
810 * fails, go ahead anyway because we have extra space reserved for
814 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
815 dentry
, inode
->i_ino
,
816 inode
->i_nlink
, dir
->i_ino
);
818 if (ubifs_crypt_is_encrypted(dir
)) {
819 err
= fscrypt_get_encryption_info(dir
);
820 if (err
&& err
!= -ENOKEY
)
824 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
828 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
830 ubifs_assert(inode_is_locked(dir
));
831 ubifs_assert(inode_is_locked(inode
));
832 err
= dbg_check_synced_i_size(c
, inode
);
836 err
= ubifs_budget_space(c
, &req
);
843 lock_2_inodes(dir
, inode
);
844 inode
->i_ctime
= ubifs_current_time(dir
);
846 dir
->i_size
-= sz_change
;
847 dir_ui
->ui_size
= dir
->i_size
;
848 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
849 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
852 unlock_2_inodes(dir
, inode
);
855 ubifs_release_budget(c
, &req
);
857 /* We've deleted something - clean the "no space" flags */
858 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
861 fscrypt_free_filename(&nm
);
865 dir
->i_size
+= sz_change
;
866 dir_ui
->ui_size
= dir
->i_size
;
867 set_nlink(inode
, saved_nlink
);
868 unlock_2_inodes(dir
, inode
);
870 ubifs_release_budget(c
, &req
);
872 fscrypt_free_filename(&nm
);
877 * check_dir_empty - check if a directory is empty or not.
878 * @dir: VFS inode object of the directory to check
880 * This function checks if directory @dir is empty. Returns zero if the
881 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
882 * in case of of errors.
884 int ubifs_check_dir_empty(struct inode
*dir
)
886 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
887 struct fscrypt_name nm
= { 0 };
888 struct ubifs_dent_node
*dent
;
892 lowest_dent_key(c
, &key
, dir
->i_ino
);
893 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
905 static int ubifs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
907 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
908 struct inode
*inode
= d_inode(dentry
);
909 int err
, sz_change
, budgeted
= 1;
910 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
911 struct ubifs_budget_req req
= { .mod_dent
= 1, .dirtied_ino
= 2 };
912 struct fscrypt_name nm
;
915 * Budget request settings: deletion direntry, deletion inode and
916 * changing the parent inode. If budgeting fails, go ahead anyway
917 * because we have extra space reserved for deletions.
920 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry
,
921 inode
->i_ino
, dir
->i_ino
);
922 ubifs_assert(inode_is_locked(dir
));
923 ubifs_assert(inode_is_locked(inode
));
924 err
= ubifs_check_dir_empty(d_inode(dentry
));
928 if (ubifs_crypt_is_encrypted(dir
)) {
929 err
= fscrypt_get_encryption_info(dir
);
930 if (err
&& err
!= -ENOKEY
)
934 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, &nm
);
938 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
940 err
= ubifs_budget_space(c
, &req
);
947 lock_2_inodes(dir
, inode
);
948 inode
->i_ctime
= ubifs_current_time(dir
);
951 dir
->i_size
-= sz_change
;
952 dir_ui
->ui_size
= dir
->i_size
;
953 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
954 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 1, 0);
957 unlock_2_inodes(dir
, inode
);
960 ubifs_release_budget(c
, &req
);
962 /* We've deleted something - clean the "no space" flags */
963 c
->bi
.nospace
= c
->bi
.nospace_rp
= 0;
966 fscrypt_free_filename(&nm
);
970 dir
->i_size
+= sz_change
;
971 dir_ui
->ui_size
= dir
->i_size
;
974 unlock_2_inodes(dir
, inode
);
976 ubifs_release_budget(c
, &req
);
978 fscrypt_free_filename(&nm
);
982 static int ubifs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
985 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
986 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
988 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1 };
989 struct fscrypt_name nm
;
992 * Budget request settings: new inode, new direntry and changing parent
996 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
997 dentry
, mode
, dir
->i_ino
);
999 err
= ubifs_budget_space(c
, &req
);
1003 if (ubifs_crypt_is_encrypted(dir
)) {
1004 err
= fscrypt_get_encryption_info(dir
);
1008 if (!fscrypt_has_encryption_key(dir
)) {
1014 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1018 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1020 inode
= ubifs_new_inode(c
, dir
, S_IFDIR
| mode
);
1021 if (IS_ERR(inode
)) {
1022 err
= PTR_ERR(inode
);
1026 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1030 mutex_lock(&dir_ui
->ui_mutex
);
1031 insert_inode_hash(inode
);
1034 dir
->i_size
+= sz_change
;
1035 dir_ui
->ui_size
= dir
->i_size
;
1036 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1037 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
1039 ubifs_err(c
, "cannot create directory, error %d", err
);
1042 mutex_unlock(&dir_ui
->ui_mutex
);
1044 ubifs_release_budget(c
, &req
);
1045 d_instantiate(dentry
, inode
);
1046 fscrypt_free_filename(&nm
);
1050 dir
->i_size
-= sz_change
;
1051 dir_ui
->ui_size
= dir
->i_size
;
1053 mutex_unlock(&dir_ui
->ui_mutex
);
1055 make_bad_inode(inode
);
1058 fscrypt_free_filename(&nm
);
1060 ubifs_release_budget(c
, &req
);
1064 static int ubifs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1065 umode_t mode
, dev_t rdev
)
1067 struct inode
*inode
;
1068 struct ubifs_inode
*ui
;
1069 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1070 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1071 union ubifs_dev_desc
*dev
= NULL
;
1073 int err
, devlen
= 0;
1074 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1075 .new_ino_d
= ALIGN(devlen
, 8),
1077 struct fscrypt_name nm
;
1080 * Budget request settings: new inode, new direntry and changing parent
1084 dbg_gen("dent '%pd' in dir ino %lu", dentry
, dir
->i_ino
);
1086 if (S_ISBLK(mode
) || S_ISCHR(mode
)) {
1087 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1090 devlen
= ubifs_encode_dev(dev
, rdev
);
1093 err
= ubifs_budget_space(c
, &req
);
1099 if (ubifs_crypt_is_encrypted(dir
)) {
1100 err
= fscrypt_get_encryption_info(dir
);
1104 if (!fscrypt_has_encryption_key(dir
)) {
1110 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1114 sz_change
= CALC_DENT_SIZE(fname_len(&nm
));
1116 inode
= ubifs_new_inode(c
, dir
, mode
);
1117 if (IS_ERR(inode
)) {
1119 err
= PTR_ERR(inode
);
1123 init_special_inode(inode
, inode
->i_mode
, rdev
);
1124 inode
->i_size
= ubifs_inode(inode
)->ui_size
= devlen
;
1125 ui
= ubifs_inode(inode
);
1127 ui
->data_len
= devlen
;
1129 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1133 mutex_lock(&dir_ui
->ui_mutex
);
1134 dir
->i_size
+= sz_change
;
1135 dir_ui
->ui_size
= dir
->i_size
;
1136 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1137 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
1140 mutex_unlock(&dir_ui
->ui_mutex
);
1142 ubifs_release_budget(c
, &req
);
1143 insert_inode_hash(inode
);
1144 d_instantiate(dentry
, inode
);
1145 fscrypt_free_filename(&nm
);
1149 dir
->i_size
-= sz_change
;
1150 dir_ui
->ui_size
= dir
->i_size
;
1151 mutex_unlock(&dir_ui
->ui_mutex
);
1153 make_bad_inode(inode
);
1156 fscrypt_free_filename(&nm
);
1158 ubifs_release_budget(c
, &req
);
1162 static int ubifs_symlink(struct inode
*dir
, struct dentry
*dentry
,
1163 const char *symname
)
1165 struct inode
*inode
;
1166 struct ubifs_inode
*ui
;
1167 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
1168 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
1169 int err
, len
= strlen(symname
);
1170 int sz_change
= CALC_DENT_SIZE(len
);
1171 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
1172 struct fscrypt_symlink_data
*sd
= NULL
;
1173 struct ubifs_budget_req req
= { .new_ino
= 1, .new_dent
= 1,
1174 .new_ino_d
= ALIGN(len
, 8),
1176 struct fscrypt_name nm
;
1178 if (ubifs_crypt_is_encrypted(dir
)) {
1179 err
= fscrypt_get_encryption_info(dir
);
1183 if (!fscrypt_has_encryption_key(dir
)) {
1188 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
1189 sizeof(struct fscrypt_symlink_data
));
1193 * Budget request settings: new inode, new direntry and changing parent
1197 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry
,
1198 symname
, dir
->i_ino
);
1200 if (disk_link
.len
> UBIFS_MAX_INO_DATA
)
1201 return -ENAMETOOLONG
;
1203 err
= ubifs_budget_space(c
, &req
);
1207 err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 0, &nm
);
1211 inode
= ubifs_new_inode(c
, dir
, S_IFLNK
| S_IRWXUGO
);
1212 if (IS_ERR(inode
)) {
1213 err
= PTR_ERR(inode
);
1217 ui
= ubifs_inode(inode
);
1218 ui
->data
= kmalloc(disk_link
.len
, GFP_NOFS
);
1224 if (ubifs_crypt_is_encrypted(dir
)) {
1225 struct qstr istr
= QSTR_INIT(symname
, len
);
1226 struct fscrypt_str ostr
;
1228 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
1234 err
= fscrypt_get_encryption_info(inode
);
1240 if (!fscrypt_has_encryption_key(inode
)) {
1246 ostr
.name
= sd
->encrypted_path
;
1247 ostr
.len
= disk_link
.len
;
1249 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
1255 sd
->len
= cpu_to_le16(ostr
.len
);
1256 disk_link
.name
= (char *)sd
;
1258 inode
->i_link
= ui
->data
;
1261 memcpy(ui
->data
, disk_link
.name
, disk_link
.len
);
1262 ((char *)ui
->data
)[disk_link
.len
- 1] = '\0';
1265 * The terminating zero byte is not written to the flash media and it
1266 * is put just to make later in-memory string processing simpler. Thus,
1267 * data length is @len, not @len + %1.
1269 ui
->data_len
= disk_link
.len
- 1;
1270 inode
->i_size
= ubifs_inode(inode
)->ui_size
= disk_link
.len
- 1;
1272 err
= ubifs_init_security(dir
, inode
, &dentry
->d_name
);
1276 mutex_lock(&dir_ui
->ui_mutex
);
1277 dir
->i_size
+= sz_change
;
1278 dir_ui
->ui_size
= dir
->i_size
;
1279 dir
->i_mtime
= dir
->i_ctime
= inode
->i_ctime
;
1280 err
= ubifs_jnl_update(c
, dir
, &nm
, inode
, 0, 0);
1283 mutex_unlock(&dir_ui
->ui_mutex
);
1285 ubifs_release_budget(c
, &req
);
1286 insert_inode_hash(inode
);
1287 d_instantiate(dentry
, inode
);
1288 fscrypt_free_filename(&nm
);
1292 dir
->i_size
-= sz_change
;
1293 dir_ui
->ui_size
= dir
->i_size
;
1294 mutex_unlock(&dir_ui
->ui_mutex
);
1296 make_bad_inode(inode
);
1299 fscrypt_free_filename(&nm
);
1301 ubifs_release_budget(c
, &req
);
1306 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1307 * @inode1: first inode
1308 * @inode2: second inode
1309 * @inode3: third inode
1310 * @inode4: fouth inode
1312 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1313 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1315 * We do not implement any tricks to guarantee strict lock ordering, because
1316 * VFS has already done it for us on the @i_mutex. So this is just a simple
1319 static void lock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1320 struct inode
*inode3
, struct inode
*inode4
)
1322 mutex_lock_nested(&ubifs_inode(inode1
)->ui_mutex
, WB_MUTEX_1
);
1323 if (inode2
!= inode1
)
1324 mutex_lock_nested(&ubifs_inode(inode2
)->ui_mutex
, WB_MUTEX_2
);
1326 mutex_lock_nested(&ubifs_inode(inode3
)->ui_mutex
, WB_MUTEX_3
);
1328 mutex_lock_nested(&ubifs_inode(inode4
)->ui_mutex
, WB_MUTEX_4
);
1332 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1333 * @inode1: first inode
1334 * @inode2: second inode
1335 * @inode3: third inode
1336 * @inode4: fouth inode
1338 static void unlock_4_inodes(struct inode
*inode1
, struct inode
*inode2
,
1339 struct inode
*inode3
, struct inode
*inode4
)
1342 mutex_unlock(&ubifs_inode(inode4
)->ui_mutex
);
1344 mutex_unlock(&ubifs_inode(inode3
)->ui_mutex
);
1345 if (inode1
!= inode2
)
1346 mutex_unlock(&ubifs_inode(inode2
)->ui_mutex
);
1347 mutex_unlock(&ubifs_inode(inode1
)->ui_mutex
);
1350 static int do_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1351 struct inode
*new_dir
, struct dentry
*new_dentry
,
1354 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1355 struct inode
*old_inode
= d_inode(old_dentry
);
1356 struct inode
*new_inode
= d_inode(new_dentry
);
1357 struct inode
*whiteout
= NULL
;
1358 struct ubifs_inode
*old_inode_ui
= ubifs_inode(old_inode
);
1359 struct ubifs_inode
*whiteout_ui
= NULL
;
1360 int err
, release
, sync
= 0, move
= (new_dir
!= old_dir
);
1361 int is_dir
= S_ISDIR(old_inode
->i_mode
);
1362 int unlink
= !!new_inode
, new_sz
, old_sz
;
1363 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1365 struct ubifs_budget_req ino_req
= { .dirtied_ino
= 1,
1366 .dirtied_ino_d
= ALIGN(old_inode_ui
->data_len
, 8) };
1367 struct timespec time
;
1368 unsigned int uninitialized_var(saved_nlink
);
1369 struct fscrypt_name old_nm
, new_nm
;
1371 if (flags
& ~RENAME_NOREPLACE
)
1375 * Budget request settings: deletion direntry, new direntry, removing
1376 * the old inode, and changing old and new parent directory inodes.
1378 * However, this operation also marks the target inode as dirty and
1379 * does not write it, so we allocate budget for the target inode
1383 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1384 old_dentry
, old_inode
->i_ino
, old_dir
->i_ino
,
1385 new_dentry
, new_dir
->i_ino
, flags
);
1388 ubifs_assert(inode_is_locked(new_inode
));
1390 if (old_dir
!= new_dir
) {
1391 if (ubifs_crypt_is_encrypted(new_dir
) &&
1392 !fscrypt_has_permitted_context(new_dir
, old_inode
))
1396 if (unlink
&& is_dir
) {
1397 err
= ubifs_check_dir_empty(new_inode
);
1402 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &old_nm
);
1406 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &new_nm
);
1408 fscrypt_free_filename(&old_nm
);
1412 new_sz
= CALC_DENT_SIZE(fname_len(&new_nm
));
1413 old_sz
= CALC_DENT_SIZE(fname_len(&old_nm
));
1415 err
= ubifs_budget_space(c
, &req
);
1417 fscrypt_free_filename(&old_nm
);
1418 fscrypt_free_filename(&new_nm
);
1421 err
= ubifs_budget_space(c
, &ino_req
);
1423 fscrypt_free_filename(&old_nm
);
1424 fscrypt_free_filename(&new_nm
);
1425 ubifs_release_budget(c
, &req
);
1429 if (flags
& RENAME_WHITEOUT
) {
1430 union ubifs_dev_desc
*dev
= NULL
;
1432 dev
= kmalloc(sizeof(union ubifs_dev_desc
), GFP_NOFS
);
1434 ubifs_release_budget(c
, &req
);
1435 ubifs_release_budget(c
, &ino_req
);
1439 err
= do_tmpfile(old_dir
, old_dentry
, S_IFCHR
| WHITEOUT_MODE
, &whiteout
);
1441 ubifs_release_budget(c
, &req
);
1442 ubifs_release_budget(c
, &ino_req
);
1447 whiteout
->i_state
|= I_LINKABLE
;
1448 whiteout_ui
= ubifs_inode(whiteout
);
1449 whiteout_ui
->data
= dev
;
1450 whiteout_ui
->data_len
= ubifs_encode_dev(dev
, MKDEV(0, 0));
1451 ubifs_assert(!whiteout_ui
->dirty
);
1454 lock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1457 * Like most other Unix systems, set the @i_ctime for inodes on a
1460 time
= ubifs_current_time(old_dir
);
1461 old_inode
->i_ctime
= time
;
1463 /* We must adjust parent link count when renaming directories */
1467 * @old_dir loses a link because we are moving
1468 * @old_inode to a different directory.
1470 drop_nlink(old_dir
);
1472 * @new_dir only gains a link if we are not also
1473 * overwriting an existing directory.
1479 * @old_inode is not moving to a different directory,
1480 * but @old_dir still loses a link if we are
1481 * overwriting an existing directory.
1484 drop_nlink(old_dir
);
1488 old_dir
->i_size
-= old_sz
;
1489 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1490 old_dir
->i_mtime
= old_dir
->i_ctime
= time
;
1491 new_dir
->i_mtime
= new_dir
->i_ctime
= time
;
1494 * And finally, if we unlinked a direntry which happened to have the
1495 * same name as the moved direntry, we have to decrement @i_nlink of
1496 * the unlinked inode and change its ctime.
1500 * Directories cannot have hard-links, so if this is a
1501 * directory, just clear @i_nlink.
1503 saved_nlink
= new_inode
->i_nlink
;
1505 clear_nlink(new_inode
);
1507 drop_nlink(new_inode
);
1508 new_inode
->i_ctime
= time
;
1510 new_dir
->i_size
+= new_sz
;
1511 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1515 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1516 * is dirty, because this will be done later on at the end of
1519 if (IS_SYNC(old_inode
)) {
1520 sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1521 if (unlink
&& IS_SYNC(new_inode
))
1526 struct ubifs_budget_req wht_req
= { .dirtied_ino
= 1,
1528 ALIGN(ubifs_inode(whiteout
)->data_len
, 8) };
1530 err
= ubifs_budget_space(c
, &wht_req
);
1532 ubifs_release_budget(c
, &req
);
1533 ubifs_release_budget(c
, &ino_req
);
1534 kfree(whiteout_ui
->data
);
1535 whiteout_ui
->data_len
= 0;
1540 inc_nlink(whiteout
);
1541 mark_inode_dirty(whiteout
);
1542 whiteout
->i_state
&= ~I_LINKABLE
;
1546 err
= ubifs_jnl_rename(c
, old_dir
, old_inode
, &old_nm
, new_dir
,
1547 new_inode
, &new_nm
, whiteout
, sync
);
1551 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1552 ubifs_release_budget(c
, &req
);
1554 mutex_lock(&old_inode_ui
->ui_mutex
);
1555 release
= old_inode_ui
->dirty
;
1556 mark_inode_dirty_sync(old_inode
);
1557 mutex_unlock(&old_inode_ui
->ui_mutex
);
1560 ubifs_release_budget(c
, &ino_req
);
1561 if (IS_SYNC(old_inode
))
1562 err
= old_inode
->i_sb
->s_op
->write_inode(old_inode
, NULL
);
1564 fscrypt_free_filename(&old_nm
);
1565 fscrypt_free_filename(&new_nm
);
1570 set_nlink(new_inode
, saved_nlink
);
1572 new_dir
->i_size
-= new_sz
;
1573 ubifs_inode(new_dir
)->ui_size
= new_dir
->i_size
;
1575 old_dir
->i_size
+= old_sz
;
1576 ubifs_inode(old_dir
)->ui_size
= old_dir
->i_size
;
1581 drop_nlink(new_dir
);
1588 drop_nlink(whiteout
);
1591 unlock_4_inodes(old_dir
, new_dir
, new_inode
, whiteout
);
1592 ubifs_release_budget(c
, &ino_req
);
1593 ubifs_release_budget(c
, &req
);
1594 fscrypt_free_filename(&old_nm
);
1595 fscrypt_free_filename(&new_nm
);
1599 static int ubifs_xrename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1600 struct inode
*new_dir
, struct dentry
*new_dentry
)
1602 struct ubifs_info
*c
= old_dir
->i_sb
->s_fs_info
;
1603 struct ubifs_budget_req req
= { .new_dent
= 1, .mod_dent
= 1,
1605 int sync
= IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
);
1606 struct inode
*fst_inode
= d_inode(old_dentry
);
1607 struct inode
*snd_inode
= d_inode(new_dentry
);
1608 struct timespec time
;
1610 struct fscrypt_name fst_nm
, snd_nm
;
1612 ubifs_assert(fst_inode
&& snd_inode
);
1614 if ((ubifs_crypt_is_encrypted(old_dir
) ||
1615 ubifs_crypt_is_encrypted(new_dir
)) &&
1616 (old_dir
!= new_dir
) &&
1617 (!fscrypt_has_permitted_context(new_dir
, fst_inode
) ||
1618 !fscrypt_has_permitted_context(old_dir
, snd_inode
)))
1621 err
= fscrypt_setup_filename(old_dir
, &old_dentry
->d_name
, 0, &fst_nm
);
1625 err
= fscrypt_setup_filename(new_dir
, &new_dentry
->d_name
, 0, &snd_nm
);
1627 fscrypt_free_filename(&fst_nm
);
1631 lock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1633 time
= ubifs_current_time(old_dir
);
1634 fst_inode
->i_ctime
= time
;
1635 snd_inode
->i_ctime
= time
;
1636 old_dir
->i_mtime
= old_dir
->i_ctime
= time
;
1637 new_dir
->i_mtime
= new_dir
->i_ctime
= time
;
1639 if (old_dir
!= new_dir
) {
1640 if (S_ISDIR(fst_inode
->i_mode
) && !S_ISDIR(snd_inode
->i_mode
)) {
1642 drop_nlink(old_dir
);
1644 else if (!S_ISDIR(fst_inode
->i_mode
) && S_ISDIR(snd_inode
->i_mode
)) {
1645 drop_nlink(new_dir
);
1650 err
= ubifs_jnl_xrename(c
, old_dir
, fst_inode
, &fst_nm
, new_dir
,
1651 snd_inode
, &snd_nm
, sync
);
1653 unlock_4_inodes(old_dir
, new_dir
, NULL
, NULL
);
1654 ubifs_release_budget(c
, &req
);
1656 fscrypt_free_filename(&fst_nm
);
1657 fscrypt_free_filename(&snd_nm
);
1661 static int ubifs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1662 struct inode
*new_dir
, struct dentry
*new_dentry
,
1665 if (flags
& ~(RENAME_NOREPLACE
| RENAME_WHITEOUT
| RENAME_EXCHANGE
))
1668 ubifs_assert(inode_is_locked(old_dir
));
1669 ubifs_assert(inode_is_locked(new_dir
));
1671 if (flags
& RENAME_EXCHANGE
)
1672 return ubifs_xrename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1674 return do_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1677 int ubifs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1681 struct inode
*inode
= d_inode(dentry
);
1682 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1684 mutex_lock(&ui
->ui_mutex
);
1685 generic_fillattr(inode
, stat
);
1686 stat
->blksize
= UBIFS_BLOCK_SIZE
;
1687 stat
->size
= ui
->ui_size
;
1690 * Unfortunately, the 'stat()' system call was designed for block
1691 * device based file systems, and it is not appropriate for UBIFS,
1692 * because UBIFS does not have notion of "block". For example, it is
1693 * difficult to tell how many block a directory takes - it actually
1694 * takes less than 300 bytes, but we have to round it to block size,
1695 * which introduces large mistake. This makes utilities like 'du' to
1696 * report completely senseless numbers. This is the reason why UBIFS
1697 * goes the same way as JFFS2 - it reports zero blocks for everything
1698 * but regular files, which makes more sense than reporting completely
1701 if (S_ISREG(inode
->i_mode
)) {
1702 size
= ui
->xattr_size
;
1704 size
= ALIGN(size
, UBIFS_BLOCK_SIZE
);
1706 * Note, user-space expects 512-byte blocks count irrespectively
1707 * of what was reported in @stat->size.
1709 stat
->blocks
= size
>> 9;
1712 mutex_unlock(&ui
->ui_mutex
);
1716 static int ubifs_dir_open(struct inode
*dir
, struct file
*file
)
1718 if (ubifs_crypt_is_encrypted(dir
))
1719 return fscrypt_get_encryption_info(dir
) ? -EACCES
: 0;
1724 const struct inode_operations ubifs_dir_inode_operations
= {
1725 .lookup
= ubifs_lookup
,
1726 .create
= ubifs_create
,
1728 .symlink
= ubifs_symlink
,
1729 .unlink
= ubifs_unlink
,
1730 .mkdir
= ubifs_mkdir
,
1731 .rmdir
= ubifs_rmdir
,
1732 .mknod
= ubifs_mknod
,
1733 .rename
= ubifs_rename
,
1734 .setattr
= ubifs_setattr
,
1735 .getattr
= ubifs_getattr
,
1736 .listxattr
= ubifs_listxattr
,
1737 #ifdef CONFIG_UBIFS_ATIME_SUPPORT
1738 .update_time
= ubifs_update_time
,
1740 .tmpfile
= ubifs_tmpfile
,
1743 const struct file_operations ubifs_dir_operations
= {
1744 .llseek
= generic_file_llseek
,
1745 .release
= ubifs_dir_release
,
1746 .read
= generic_read_dir
,
1747 .iterate_shared
= ubifs_readdir
,
1748 .fsync
= ubifs_fsync
,
1749 .unlocked_ioctl
= ubifs_ioctl
,
1750 .open
= ubifs_dir_open
,
1751 #ifdef CONFIG_COMPAT
1752 .compat_ioctl
= ubifs_compat_ioctl
,