1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched.h>
12 #include <linux/ctype.h>
13 #include <linux/random.h>
14 #include <linux/dcache.h>
15 #include <linux/namei.h>
16 #include <linux/quotaops.h>
23 #include <trace/events/f2fs.h>
25 static struct inode
*f2fs_new_inode(struct inode
*dir
, umode_t mode
)
27 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
30 bool nid_free
= false;
34 inode
= new_inode(dir
->i_sb
);
36 return ERR_PTR(-ENOMEM
);
39 if (!f2fs_alloc_nid(sbi
, &ino
)) {
48 inode_init_owner(inode
, dir
, mode
);
52 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
53 F2FS_I(inode
)->i_crtime
= inode
->i_mtime
;
54 inode
->i_generation
= prandom_u32();
56 if (S_ISDIR(inode
->i_mode
))
57 F2FS_I(inode
)->i_current_depth
= 1;
59 err
= insert_inode_locked(inode
);
65 if (f2fs_sb_has_project_quota(sbi
) &&
66 (F2FS_I(dir
)->i_flags
& F2FS_PROJINHERIT_FL
))
67 F2FS_I(inode
)->i_projid
= F2FS_I(dir
)->i_projid
;
69 F2FS_I(inode
)->i_projid
= make_kprojid(&init_user_ns
,
72 err
= dquot_initialize(inode
);
76 set_inode_flag(inode
, FI_NEW_INODE
);
78 /* If the directory encrypted, then we should encrypt the inode. */
79 if ((IS_ENCRYPTED(dir
) || DUMMY_ENCRYPTION_ENABLED(sbi
)) &&
80 f2fs_may_encrypt(inode
))
81 f2fs_set_encrypted_inode(inode
);
83 if (f2fs_sb_has_extra_attr(sbi
)) {
84 set_inode_flag(inode
, FI_EXTRA_ATTR
);
85 F2FS_I(inode
)->i_extra_isize
= F2FS_TOTAL_EXTRA_ATTR_SIZE
;
88 if (test_opt(sbi
, INLINE_XATTR
))
89 set_inode_flag(inode
, FI_INLINE_XATTR
);
91 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
92 set_inode_flag(inode
, FI_INLINE_DATA
);
93 if (f2fs_may_inline_dentry(inode
))
94 set_inode_flag(inode
, FI_INLINE_DENTRY
);
96 if (f2fs_sb_has_flexible_inline_xattr(sbi
)) {
97 f2fs_bug_on(sbi
, !f2fs_has_extra_attr(inode
));
98 if (f2fs_has_inline_xattr(inode
))
99 xattr_size
= F2FS_OPTION(sbi
).inline_xattr_size
;
100 /* Otherwise, will be 0 */
101 } else if (f2fs_has_inline_xattr(inode
) ||
102 f2fs_has_inline_dentry(inode
)) {
103 xattr_size
= DEFAULT_INLINE_XATTR_ADDRS
;
105 F2FS_I(inode
)->i_inline_xattr_size
= xattr_size
;
107 f2fs_init_extent_tree(inode
, NULL
);
109 stat_inc_inline_xattr(inode
);
110 stat_inc_inline_inode(inode
);
111 stat_inc_inline_dir(inode
);
113 F2FS_I(inode
)->i_flags
=
114 f2fs_mask_flags(mode
, F2FS_I(dir
)->i_flags
& F2FS_FL_INHERITED
);
116 if (S_ISDIR(inode
->i_mode
))
117 F2FS_I(inode
)->i_flags
|= F2FS_INDEX_FL
;
119 if (F2FS_I(inode
)->i_flags
& F2FS_PROJINHERIT_FL
)
120 set_inode_flag(inode
, FI_PROJ_INHERIT
);
122 if (f2fs_sb_has_compression(sbi
)) {
123 /* Inherit the compression flag in directory */
124 if ((F2FS_I(dir
)->i_flags
& F2FS_COMPR_FL
) &&
125 f2fs_may_compress(inode
))
126 set_compress_context(inode
);
129 f2fs_set_inode_flags(inode
);
131 trace_f2fs_new_inode(inode
, 0);
135 trace_f2fs_new_inode(inode
, err
);
136 make_bad_inode(inode
);
138 set_inode_flag(inode
, FI_FREE_NID
);
142 trace_f2fs_new_inode(inode
, err
);
144 inode
->i_flags
|= S_NOQUOTA
;
146 set_inode_flag(inode
, FI_FREE_NID
);
148 unlock_new_inode(inode
);
153 static inline int is_extension_exist(const unsigned char *s
, const char *sub
)
155 size_t slen
= strlen(s
);
156 size_t sublen
= strlen(sub
);
159 if (sublen
== 1 && *sub
== '*')
163 * filename format of multimedia file should be defined as:
164 * "filename + '.' + extension + (optional: '.' + temp extension)".
166 if (slen
< sublen
+ 2)
169 for (i
= 1; i
< slen
- sublen
; i
++) {
172 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
180 * Set multimedia files as cold files for hot/cold data separation
182 static inline void set_file_temperature(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
183 const unsigned char *name
)
185 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
186 int i
, cold_count
, hot_count
;
188 down_read(&sbi
->sb_lock
);
190 cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
191 hot_count
= sbi
->raw_super
->hot_ext_count
;
193 for (i
= 0; i
< cold_count
+ hot_count
; i
++) {
194 if (is_extension_exist(name
, extlist
[i
]))
198 up_read(&sbi
->sb_lock
);
200 if (i
== cold_count
+ hot_count
)
204 file_set_cold(inode
);
209 int f2fs_update_extension_list(struct f2fs_sb_info
*sbi
, const char *name
,
212 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
213 int cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
214 int hot_count
= sbi
->raw_super
->hot_ext_count
;
215 int total_count
= cold_count
+ hot_count
;
220 if (total_count
== F2FS_MAX_EXTENSION
)
223 if (!hot
&& !cold_count
)
225 if (hot
&& !hot_count
)
237 for (i
= start
; i
< count
; i
++) {
238 if (strcmp(name
, extlist
[i
]))
244 memcpy(extlist
[i
], extlist
[i
+ 1],
245 F2FS_EXTENSION_LEN
* (total_count
- i
- 1));
246 memset(extlist
[total_count
- 1], 0, F2FS_EXTENSION_LEN
);
248 sbi
->raw_super
->hot_ext_count
= hot_count
- 1;
250 sbi
->raw_super
->extension_count
=
251 cpu_to_le32(cold_count
- 1);
259 memcpy(extlist
[count
], name
, strlen(name
));
260 sbi
->raw_super
->hot_ext_count
= hot_count
+ 1;
262 char buf
[F2FS_MAX_EXTENSION
][F2FS_EXTENSION_LEN
];
264 memcpy(buf
, &extlist
[cold_count
],
265 F2FS_EXTENSION_LEN
* hot_count
);
266 memset(extlist
[cold_count
], 0, F2FS_EXTENSION_LEN
);
267 memcpy(extlist
[cold_count
], name
, strlen(name
));
268 memcpy(&extlist
[cold_count
+ 1], buf
,
269 F2FS_EXTENSION_LEN
* hot_count
);
270 sbi
->raw_super
->extension_count
= cpu_to_le32(cold_count
+ 1);
275 static void set_compress_inode(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
276 const unsigned char *name
)
278 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
279 unsigned char (*ext
)[F2FS_EXTENSION_LEN
];
280 unsigned int ext_cnt
= F2FS_OPTION(sbi
).compress_ext_cnt
;
281 int i
, cold_count
, hot_count
;
283 if (!f2fs_sb_has_compression(sbi
) ||
284 is_inode_flag_set(inode
, FI_COMPRESSED_FILE
) ||
285 F2FS_I(inode
)->i_flags
& F2FS_NOCOMP_FL
||
286 !f2fs_may_compress(inode
))
289 down_read(&sbi
->sb_lock
);
291 cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
292 hot_count
= sbi
->raw_super
->hot_ext_count
;
294 for (i
= cold_count
; i
< cold_count
+ hot_count
; i
++) {
295 if (is_extension_exist(name
, extlist
[i
])) {
296 up_read(&sbi
->sb_lock
);
301 up_read(&sbi
->sb_lock
);
303 ext
= F2FS_OPTION(sbi
).extensions
;
305 for (i
= 0; i
< ext_cnt
; i
++) {
306 if (!is_extension_exist(name
, ext
[i
]))
309 set_compress_context(inode
);
314 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
317 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
322 if (unlikely(f2fs_cp_error(sbi
)))
324 if (!f2fs_is_checkpoint_ready(sbi
))
327 err
= dquot_initialize(dir
);
331 inode
= f2fs_new_inode(dir
, mode
);
333 return PTR_ERR(inode
);
335 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
336 set_file_temperature(sbi
, inode
, dentry
->d_name
.name
);
338 set_compress_inode(sbi
, inode
, dentry
->d_name
.name
);
340 inode
->i_op
= &f2fs_file_inode_operations
;
341 inode
->i_fop
= &f2fs_file_operations
;
342 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
346 err
= f2fs_add_link(dentry
, inode
);
351 f2fs_alloc_nid_done(sbi
, ino
);
353 d_instantiate_new(dentry
, inode
);
356 f2fs_sync_fs(sbi
->sb
, 1);
358 f2fs_balance_fs(sbi
, true);
361 f2fs_handle_failed_inode(inode
);
365 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
366 struct dentry
*dentry
)
368 struct inode
*inode
= d_inode(old_dentry
);
369 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
372 if (unlikely(f2fs_cp_error(sbi
)))
374 if (!f2fs_is_checkpoint_ready(sbi
))
377 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
381 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
382 (!projid_eq(F2FS_I(dir
)->i_projid
,
383 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
386 err
= dquot_initialize(dir
);
390 f2fs_balance_fs(sbi
, true);
392 inode
->i_ctime
= current_time(inode
);
395 set_inode_flag(inode
, FI_INC_LINK
);
397 err
= f2fs_add_link(dentry
, inode
);
402 d_instantiate(dentry
, inode
);
405 f2fs_sync_fs(sbi
->sb
, 1);
408 clear_inode_flag(inode
, FI_INC_LINK
);
414 struct dentry
*f2fs_get_parent(struct dentry
*child
)
416 struct qstr dotdot
= QSTR_INIT("..", 2);
418 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
421 return ERR_CAST(page
);
422 return ERR_PTR(-ENOENT
);
424 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
427 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
429 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
430 struct qstr dot
= QSTR_INIT(".", 1);
431 struct qstr dotdot
= QSTR_INIT("..", 2);
432 struct f2fs_dir_entry
*de
;
436 if (f2fs_readonly(sbi
->sb
)) {
437 f2fs_info(sbi
, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
442 err
= dquot_initialize(dir
);
446 f2fs_balance_fs(sbi
, true);
450 de
= f2fs_find_entry(dir
, &dot
, &page
);
452 f2fs_put_page(page
, 0);
453 } else if (IS_ERR(page
)) {
457 err
= f2fs_do_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
462 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
464 f2fs_put_page(page
, 0);
465 else if (IS_ERR(page
))
468 err
= f2fs_do_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
471 clear_inode_flag(dir
, FI_INLINE_DOTS
);
477 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
480 struct inode
*inode
= NULL
;
481 struct f2fs_dir_entry
*de
;
486 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
487 struct fscrypt_name fname
;
489 trace_f2fs_lookup_start(dir
, dentry
, flags
);
491 if (dentry
->d_name
.len
> F2FS_NAME_LEN
) {
496 err
= fscrypt_prepare_lookup(dir
, dentry
, &fname
);
501 de
= __f2fs_find_entry(dir
, &fname
, &page
);
502 fscrypt_free_filename(&fname
);
512 ino
= le32_to_cpu(de
->ino
);
513 f2fs_put_page(page
, 0);
515 inode
= f2fs_iget(dir
->i_sb
, ino
);
517 err
= PTR_ERR(inode
);
521 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
522 err
= __recover_dot_dentries(dir
, root_ino
);
527 if (f2fs_has_inline_dots(inode
)) {
528 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
532 if (IS_ENCRYPTED(dir
) &&
533 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
534 !fscrypt_has_permitted_context(dir
, inode
)) {
535 f2fs_warn(F2FS_I_SB(inode
), "Inconsistent encryption contexts: %lu/%lu",
536 dir
->i_ino
, inode
->i_ino
);
541 #ifdef CONFIG_UNICODE
542 if (!inode
&& IS_CASEFOLDED(dir
)) {
543 /* Eventually we want to call d_add_ci(dentry, NULL)
544 * for negative dentries in the encoding case as
545 * well. For now, prevent the negative dentry
548 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
552 new = d_splice_alias(inode
, dentry
);
553 err
= PTR_ERR_OR_ZERO(new);
554 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
559 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
563 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
565 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
566 struct inode
*inode
= d_inode(dentry
);
567 struct f2fs_dir_entry
*de
;
571 trace_f2fs_unlink_enter(dir
, dentry
);
573 if (unlikely(f2fs_cp_error(sbi
)))
576 err
= dquot_initialize(dir
);
579 err
= dquot_initialize(inode
);
583 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
590 f2fs_balance_fs(sbi
, true);
593 err
= f2fs_acquire_orphan_inode(sbi
);
596 f2fs_put_page(page
, 0);
599 f2fs_delete_entry(de
, page
, dir
, inode
);
600 #ifdef CONFIG_UNICODE
601 /* VFS negative dentries are incompatible with Encoding and
602 * Case-insensitiveness. Eventually we'll want avoid
603 * invalidating the dentries here, alongside with returning the
604 * negative dentries at f2fs_lookup(), when it is better
605 * supported by the VFS for the CI case.
607 if (IS_CASEFOLDED(dir
))
608 d_invalidate(dentry
);
613 f2fs_sync_fs(sbi
->sb
, 1);
615 trace_f2fs_unlink_exit(inode
, err
);
619 static const char *f2fs_get_link(struct dentry
*dentry
,
621 struct delayed_call
*done
)
623 const char *link
= page_get_link(dentry
, inode
, done
);
624 if (!IS_ERR(link
) && !*link
) {
625 /* this is broken symlink case */
626 do_delayed_call(done
);
627 clear_delayed_call(done
);
628 link
= ERR_PTR(-ENOENT
);
633 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
636 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
638 size_t len
= strlen(symname
);
639 struct fscrypt_str disk_link
;
642 if (unlikely(f2fs_cp_error(sbi
)))
644 if (!f2fs_is_checkpoint_ready(sbi
))
647 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
652 err
= dquot_initialize(dir
);
656 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
658 return PTR_ERR(inode
);
660 if (IS_ENCRYPTED(inode
))
661 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
663 inode
->i_op
= &f2fs_symlink_inode_operations
;
664 inode_nohighmem(inode
);
665 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
668 err
= f2fs_add_link(dentry
, inode
);
670 goto out_f2fs_handle_failed_inode
;
672 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
674 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
678 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
681 d_instantiate_new(dentry
, inode
);
684 * Let's flush symlink data in order to avoid broken symlink as much as
685 * possible. Nevertheless, fsyncing is the best way, but there is no
686 * way to get a file descriptor in order to flush that.
688 * Note that, it needs to do dir->fsync to make this recoverable.
689 * If the symlink path is stored into inline_data, there is no
690 * performance regression.
693 filemap_write_and_wait_range(inode
->i_mapping
, 0,
697 f2fs_sync_fs(sbi
->sb
, 1);
699 f2fs_unlink(dir
, dentry
);
702 f2fs_balance_fs(sbi
, true);
703 goto out_free_encrypted_link
;
705 out_f2fs_handle_failed_inode
:
706 f2fs_handle_failed_inode(inode
);
707 out_free_encrypted_link
:
708 if (disk_link
.name
!= (unsigned char *)symname
)
709 kvfree(disk_link
.name
);
713 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
715 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
719 if (unlikely(f2fs_cp_error(sbi
)))
722 err
= dquot_initialize(dir
);
726 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
728 return PTR_ERR(inode
);
730 inode
->i_op
= &f2fs_dir_inode_operations
;
731 inode
->i_fop
= &f2fs_dir_operations
;
732 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
733 inode_nohighmem(inode
);
735 set_inode_flag(inode
, FI_INC_LINK
);
737 err
= f2fs_add_link(dentry
, inode
);
742 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
744 d_instantiate_new(dentry
, inode
);
747 f2fs_sync_fs(sbi
->sb
, 1);
749 f2fs_balance_fs(sbi
, true);
753 clear_inode_flag(inode
, FI_INC_LINK
);
754 f2fs_handle_failed_inode(inode
);
758 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
760 struct inode
*inode
= d_inode(dentry
);
761 if (f2fs_empty_dir(inode
))
762 return f2fs_unlink(dir
, dentry
);
766 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
767 umode_t mode
, dev_t rdev
)
769 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
773 if (unlikely(f2fs_cp_error(sbi
)))
775 if (!f2fs_is_checkpoint_ready(sbi
))
778 err
= dquot_initialize(dir
);
782 inode
= f2fs_new_inode(dir
, mode
);
784 return PTR_ERR(inode
);
786 init_special_inode(inode
, inode
->i_mode
, rdev
);
787 inode
->i_op
= &f2fs_special_inode_operations
;
790 err
= f2fs_add_link(dentry
, inode
);
795 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
797 d_instantiate_new(dentry
, inode
);
800 f2fs_sync_fs(sbi
->sb
, 1);
802 f2fs_balance_fs(sbi
, true);
805 f2fs_handle_failed_inode(inode
);
809 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
810 umode_t mode
, struct inode
**whiteout
)
812 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
816 err
= dquot_initialize(dir
);
820 inode
= f2fs_new_inode(dir
, mode
);
822 return PTR_ERR(inode
);
825 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
826 inode
->i_op
= &f2fs_special_inode_operations
;
828 inode
->i_op
= &f2fs_file_inode_operations
;
829 inode
->i_fop
= &f2fs_file_operations
;
830 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
834 err
= f2fs_acquire_orphan_inode(sbi
);
838 err
= f2fs_do_tmpfile(inode
, dir
);
843 * add this non-linked tmpfile to orphan list, in this way we could
844 * remove all unused data of tmpfile after abnormal power-off.
846 f2fs_add_orphan_inode(inode
);
847 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
850 f2fs_i_links_write(inode
, false);
851 inode
->i_state
|= I_LINKABLE
;
854 d_tmpfile(dentry
, inode
);
856 /* link_count was changed by d_tmpfile as well. */
858 unlock_new_inode(inode
);
860 f2fs_balance_fs(sbi
, true);
864 f2fs_release_orphan_inode(sbi
);
866 f2fs_handle_failed_inode(inode
);
870 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
872 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
874 if (unlikely(f2fs_cp_error(sbi
)))
876 if (!f2fs_is_checkpoint_ready(sbi
))
879 if (IS_ENCRYPTED(dir
) || DUMMY_ENCRYPTION_ENABLED(sbi
)) {
880 int err
= fscrypt_get_encryption_info(dir
);
885 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
888 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
890 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
893 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
896 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
897 struct inode
*new_dir
, struct dentry
*new_dentry
,
900 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
901 struct inode
*old_inode
= d_inode(old_dentry
);
902 struct inode
*new_inode
= d_inode(new_dentry
);
903 struct inode
*whiteout
= NULL
;
904 struct page
*old_dir_page
= NULL
;
905 struct page
*old_page
, *new_page
= NULL
;
906 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
907 struct f2fs_dir_entry
*old_entry
;
908 struct f2fs_dir_entry
*new_entry
;
911 if (unlikely(f2fs_cp_error(sbi
)))
913 if (!f2fs_is_checkpoint_ready(sbi
))
916 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
917 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
918 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
922 * If new_inode is null, the below renaming flow will
923 * add a link in old_dir which can conver inline_dir.
924 * After then, if we failed to get the entry due to other
925 * reasons like ENOMEM, we had to remove the new entry.
926 * Instead of adding such the error handling routine, let's
927 * simply convert first here.
929 if (old_dir
== new_dir
&& !new_inode
) {
930 err
= f2fs_try_convert_inline_dir(old_dir
, new_dentry
);
935 if (flags
& RENAME_WHITEOUT
) {
936 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
941 err
= dquot_initialize(old_dir
);
945 err
= dquot_initialize(new_dir
);
950 err
= dquot_initialize(new_inode
);
956 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
958 if (IS_ERR(old_page
))
959 err
= PTR_ERR(old_page
);
963 if (S_ISDIR(old_inode
->i_mode
)) {
964 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
965 if (!old_dir_entry
) {
966 if (IS_ERR(old_dir_page
))
967 err
= PTR_ERR(old_dir_page
);
975 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
979 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
982 if (IS_ERR(new_page
))
983 err
= PTR_ERR(new_page
);
987 f2fs_balance_fs(sbi
, true);
991 err
= f2fs_acquire_orphan_inode(sbi
);
995 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
998 new_inode
->i_ctime
= current_time(new_inode
);
999 down_write(&F2FS_I(new_inode
)->i_sem
);
1001 f2fs_i_links_write(new_inode
, false);
1002 f2fs_i_links_write(new_inode
, false);
1003 up_write(&F2FS_I(new_inode
)->i_sem
);
1005 if (!new_inode
->i_nlink
)
1006 f2fs_add_orphan_inode(new_inode
);
1008 f2fs_release_orphan_inode(sbi
);
1010 f2fs_balance_fs(sbi
, true);
1014 err
= f2fs_add_link(new_dentry
, old_inode
);
1016 f2fs_unlock_op(sbi
);
1021 f2fs_i_links_write(new_dir
, true);
1024 down_write(&F2FS_I(old_inode
)->i_sem
);
1025 if (!old_dir_entry
|| whiteout
)
1026 file_lost_pino(old_inode
);
1028 /* adjust dir's i_pino to pass fsck check */
1029 f2fs_i_pino_write(old_inode
, new_dir
->i_ino
);
1030 up_write(&F2FS_I(old_inode
)->i_sem
);
1032 old_inode
->i_ctime
= current_time(old_inode
);
1033 f2fs_mark_inode_dirty_sync(old_inode
, false);
1035 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
1039 set_inode_flag(whiteout
, FI_INC_LINK
);
1040 err
= f2fs_add_link(old_dentry
, whiteout
);
1043 whiteout
->i_state
&= ~I_LINKABLE
;
1047 if (old_dir_entry
) {
1048 if (old_dir
!= new_dir
&& !whiteout
)
1049 f2fs_set_link(old_inode
, old_dir_entry
,
1050 old_dir_page
, new_dir
);
1052 f2fs_put_page(old_dir_page
, 0);
1053 f2fs_i_links_write(old_dir
, false);
1055 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1056 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1057 if (S_ISDIR(old_inode
->i_mode
))
1058 f2fs_add_ino_entry(sbi
, old_inode
->i_ino
,
1062 f2fs_unlock_op(sbi
);
1064 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1065 f2fs_sync_fs(sbi
->sb
, 1);
1067 f2fs_update_time(sbi
, REQ_TIME
);
1071 f2fs_unlock_op(sbi
);
1072 f2fs_put_page(new_page
, 0);
1075 f2fs_put_page(old_dir_page
, 0);
1077 f2fs_put_page(old_page
, 0);
1084 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1085 struct inode
*new_dir
, struct dentry
*new_dentry
)
1087 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
1088 struct inode
*old_inode
= d_inode(old_dentry
);
1089 struct inode
*new_inode
= d_inode(new_dentry
);
1090 struct page
*old_dir_page
, *new_dir_page
;
1091 struct page
*old_page
, *new_page
;
1092 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
1093 struct f2fs_dir_entry
*old_entry
, *new_entry
;
1094 int old_nlink
= 0, new_nlink
= 0;
1097 if (unlikely(f2fs_cp_error(sbi
)))
1099 if (!f2fs_is_checkpoint_ready(sbi
))
1102 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1103 !projid_eq(F2FS_I(new_dir
)->i_projid
,
1104 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
1105 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1106 !projid_eq(F2FS_I(old_dir
)->i_projid
,
1107 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
1110 err
= dquot_initialize(old_dir
);
1114 err
= dquot_initialize(new_dir
);
1119 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
1121 if (IS_ERR(old_page
))
1122 err
= PTR_ERR(old_page
);
1126 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
1128 if (IS_ERR(new_page
))
1129 err
= PTR_ERR(new_page
);
1133 /* prepare for updating ".." directory entry info later */
1134 if (old_dir
!= new_dir
) {
1135 if (S_ISDIR(old_inode
->i_mode
)) {
1136 old_dir_entry
= f2fs_parent_dir(old_inode
,
1138 if (!old_dir_entry
) {
1139 if (IS_ERR(old_dir_page
))
1140 err
= PTR_ERR(old_dir_page
);
1145 if (S_ISDIR(new_inode
->i_mode
)) {
1146 new_dir_entry
= f2fs_parent_dir(new_inode
,
1148 if (!new_dir_entry
) {
1149 if (IS_ERR(new_dir_page
))
1150 err
= PTR_ERR(new_dir_page
);
1157 * If cross rename between file and directory those are not
1158 * in the same directory, we will inc nlink of file's parent
1159 * later, so we should check upper boundary of its nlink.
1161 if ((!old_dir_entry
|| !new_dir_entry
) &&
1162 old_dir_entry
!= new_dir_entry
) {
1163 old_nlink
= old_dir_entry
? -1 : 1;
1164 new_nlink
= -old_nlink
;
1166 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1167 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1171 f2fs_balance_fs(sbi
, true);
1175 /* update ".." directory entry info of old dentry */
1177 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1179 /* update ".." directory entry info of new dentry */
1181 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1183 /* update directory entry info of old dir inode */
1184 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1186 down_write(&F2FS_I(old_inode
)->i_sem
);
1188 file_lost_pino(old_inode
);
1190 /* adjust dir's i_pino to pass fsck check */
1191 f2fs_i_pino_write(old_inode
, new_dir
->i_ino
);
1192 up_write(&F2FS_I(old_inode
)->i_sem
);
1194 old_dir
->i_ctime
= current_time(old_dir
);
1196 down_write(&F2FS_I(old_dir
)->i_sem
);
1197 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1198 up_write(&F2FS_I(old_dir
)->i_sem
);
1200 f2fs_mark_inode_dirty_sync(old_dir
, false);
1202 /* update directory entry info of new dir inode */
1203 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1205 down_write(&F2FS_I(new_inode
)->i_sem
);
1207 file_lost_pino(new_inode
);
1209 /* adjust dir's i_pino to pass fsck check */
1210 f2fs_i_pino_write(new_inode
, old_dir
->i_ino
);
1211 up_write(&F2FS_I(new_inode
)->i_sem
);
1213 new_dir
->i_ctime
= current_time(new_dir
);
1215 down_write(&F2FS_I(new_dir
)->i_sem
);
1216 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1217 up_write(&F2FS_I(new_dir
)->i_sem
);
1219 f2fs_mark_inode_dirty_sync(new_dir
, false);
1221 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1222 f2fs_add_ino_entry(sbi
, old_dir
->i_ino
, TRANS_DIR_INO
);
1223 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1226 f2fs_unlock_op(sbi
);
1228 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1229 f2fs_sync_fs(sbi
->sb
, 1);
1231 f2fs_update_time(sbi
, REQ_TIME
);
1234 if (new_dir_entry
) {
1235 f2fs_put_page(new_dir_page
, 0);
1238 if (old_dir_entry
) {
1239 f2fs_put_page(old_dir_page
, 0);
1242 f2fs_put_page(new_page
, 0);
1244 f2fs_put_page(old_page
, 0);
1249 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1250 struct inode
*new_dir
, struct dentry
*new_dentry
,
1255 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1258 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
1263 if (flags
& RENAME_EXCHANGE
) {
1264 return f2fs_cross_rename(old_dir
, old_dentry
,
1265 new_dir
, new_dentry
);
1268 * VFS has already handled the new dentry existence case,
1269 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1271 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1274 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1275 struct inode
*inode
,
1276 struct delayed_call
*done
)
1282 return ERR_PTR(-ECHILD
);
1284 page
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1286 return ERR_CAST(page
);
1288 target
= fscrypt_get_symlink(inode
, page_address(page
),
1289 inode
->i_sb
->s_blocksize
, done
);
1294 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1295 .get_link
= f2fs_encrypted_get_link
,
1296 .getattr
= f2fs_getattr
,
1297 .setattr
= f2fs_setattr
,
1298 #ifdef CONFIG_F2FS_FS_XATTR
1299 .listxattr
= f2fs_listxattr
,
1303 const struct inode_operations f2fs_dir_inode_operations
= {
1304 .create
= f2fs_create
,
1305 .lookup
= f2fs_lookup
,
1307 .unlink
= f2fs_unlink
,
1308 .symlink
= f2fs_symlink
,
1309 .mkdir
= f2fs_mkdir
,
1310 .rmdir
= f2fs_rmdir
,
1311 .mknod
= f2fs_mknod
,
1312 .rename
= f2fs_rename2
,
1313 .tmpfile
= f2fs_tmpfile
,
1314 .getattr
= f2fs_getattr
,
1315 .setattr
= f2fs_setattr
,
1316 .get_acl
= f2fs_get_acl
,
1317 .set_acl
= f2fs_set_acl
,
1318 #ifdef CONFIG_F2FS_FS_XATTR
1319 .listxattr
= f2fs_listxattr
,
1321 .fiemap
= f2fs_fiemap
,
1324 const struct inode_operations f2fs_symlink_inode_operations
= {
1325 .get_link
= f2fs_get_link
,
1326 .getattr
= f2fs_getattr
,
1327 .setattr
= f2fs_setattr
,
1328 #ifdef CONFIG_F2FS_FS_XATTR
1329 .listxattr
= f2fs_listxattr
,
1333 const struct inode_operations f2fs_special_inode_operations
= {
1334 .getattr
= f2fs_getattr
,
1335 .setattr
= f2fs_setattr
,
1336 .get_acl
= f2fs_get_acl
,
1337 .set_acl
= f2fs_set_acl
,
1338 #ifdef CONFIG_F2FS_FS_XATTR
1339 .listxattr
= f2fs_listxattr
,