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 (f2fs_may_encrypt(dir
, inode
))
79 f2fs_set_encrypted_inode(inode
);
81 if (f2fs_sb_has_extra_attr(sbi
)) {
82 set_inode_flag(inode
, FI_EXTRA_ATTR
);
83 F2FS_I(inode
)->i_extra_isize
= F2FS_TOTAL_EXTRA_ATTR_SIZE
;
86 if (test_opt(sbi
, INLINE_XATTR
))
87 set_inode_flag(inode
, FI_INLINE_XATTR
);
89 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
90 set_inode_flag(inode
, FI_INLINE_DATA
);
91 if (f2fs_may_inline_dentry(inode
))
92 set_inode_flag(inode
, FI_INLINE_DENTRY
);
94 if (f2fs_sb_has_flexible_inline_xattr(sbi
)) {
95 f2fs_bug_on(sbi
, !f2fs_has_extra_attr(inode
));
96 if (f2fs_has_inline_xattr(inode
))
97 xattr_size
= F2FS_OPTION(sbi
).inline_xattr_size
;
98 /* Otherwise, will be 0 */
99 } else if (f2fs_has_inline_xattr(inode
) ||
100 f2fs_has_inline_dentry(inode
)) {
101 xattr_size
= DEFAULT_INLINE_XATTR_ADDRS
;
103 F2FS_I(inode
)->i_inline_xattr_size
= xattr_size
;
105 f2fs_init_extent_tree(inode
, NULL
);
107 stat_inc_inline_xattr(inode
);
108 stat_inc_inline_inode(inode
);
109 stat_inc_inline_dir(inode
);
111 F2FS_I(inode
)->i_flags
=
112 f2fs_mask_flags(mode
, F2FS_I(dir
)->i_flags
& F2FS_FL_INHERITED
);
114 if (S_ISDIR(inode
->i_mode
))
115 F2FS_I(inode
)->i_flags
|= F2FS_INDEX_FL
;
117 if (F2FS_I(inode
)->i_flags
& F2FS_PROJINHERIT_FL
)
118 set_inode_flag(inode
, FI_PROJ_INHERIT
);
120 if (f2fs_sb_has_compression(sbi
)) {
121 /* Inherit the compression flag in directory */
122 if ((F2FS_I(dir
)->i_flags
& F2FS_COMPR_FL
) &&
123 f2fs_may_compress(inode
))
124 set_compress_context(inode
);
127 f2fs_set_inode_flags(inode
);
129 trace_f2fs_new_inode(inode
, 0);
133 trace_f2fs_new_inode(inode
, err
);
134 make_bad_inode(inode
);
136 set_inode_flag(inode
, FI_FREE_NID
);
140 trace_f2fs_new_inode(inode
, err
);
142 inode
->i_flags
|= S_NOQUOTA
;
144 set_inode_flag(inode
, FI_FREE_NID
);
146 unlock_new_inode(inode
);
151 static inline int is_extension_exist(const unsigned char *s
, const char *sub
)
153 size_t slen
= strlen(s
);
154 size_t sublen
= strlen(sub
);
157 if (sublen
== 1 && *sub
== '*')
161 * filename format of multimedia file should be defined as:
162 * "filename + '.' + extension + (optional: '.' + temp extension)".
164 if (slen
< sublen
+ 2)
167 for (i
= 1; i
< slen
- sublen
; i
++) {
170 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
178 * Set file's temperature for hot/cold data separation
180 static inline void set_file_temperature(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
181 const unsigned char *name
)
183 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
184 int i
, cold_count
, hot_count
;
186 down_read(&sbi
->sb_lock
);
188 cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
189 hot_count
= sbi
->raw_super
->hot_ext_count
;
191 for (i
= 0; i
< cold_count
+ hot_count
; i
++) {
192 if (is_extension_exist(name
, extlist
[i
]))
196 up_read(&sbi
->sb_lock
);
198 if (i
== cold_count
+ hot_count
)
202 file_set_cold(inode
);
207 int f2fs_update_extension_list(struct f2fs_sb_info
*sbi
, const char *name
,
210 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
211 int cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
212 int hot_count
= sbi
->raw_super
->hot_ext_count
;
213 int total_count
= cold_count
+ hot_count
;
218 if (total_count
== F2FS_MAX_EXTENSION
)
221 if (!hot
&& !cold_count
)
223 if (hot
&& !hot_count
)
235 for (i
= start
; i
< count
; i
++) {
236 if (strcmp(name
, extlist
[i
]))
242 memcpy(extlist
[i
], extlist
[i
+ 1],
243 F2FS_EXTENSION_LEN
* (total_count
- i
- 1));
244 memset(extlist
[total_count
- 1], 0, F2FS_EXTENSION_LEN
);
246 sbi
->raw_super
->hot_ext_count
= hot_count
- 1;
248 sbi
->raw_super
->extension_count
=
249 cpu_to_le32(cold_count
- 1);
257 memcpy(extlist
[count
], name
, strlen(name
));
258 sbi
->raw_super
->hot_ext_count
= hot_count
+ 1;
260 char buf
[F2FS_MAX_EXTENSION
][F2FS_EXTENSION_LEN
];
262 memcpy(buf
, &extlist
[cold_count
],
263 F2FS_EXTENSION_LEN
* hot_count
);
264 memset(extlist
[cold_count
], 0, F2FS_EXTENSION_LEN
);
265 memcpy(extlist
[cold_count
], name
, strlen(name
));
266 memcpy(&extlist
[cold_count
+ 1], buf
,
267 F2FS_EXTENSION_LEN
* hot_count
);
268 sbi
->raw_super
->extension_count
= cpu_to_le32(cold_count
+ 1);
273 static void set_compress_inode(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
274 const unsigned char *name
)
276 __u8 (*extlist
)[F2FS_EXTENSION_LEN
] = sbi
->raw_super
->extension_list
;
277 unsigned char (*ext
)[F2FS_EXTENSION_LEN
];
278 unsigned int ext_cnt
= F2FS_OPTION(sbi
).compress_ext_cnt
;
279 int i
, cold_count
, hot_count
;
281 if (!f2fs_sb_has_compression(sbi
) ||
282 is_inode_flag_set(inode
, FI_COMPRESSED_FILE
) ||
283 F2FS_I(inode
)->i_flags
& F2FS_NOCOMP_FL
||
284 !f2fs_may_compress(inode
))
287 down_read(&sbi
->sb_lock
);
289 cold_count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
290 hot_count
= sbi
->raw_super
->hot_ext_count
;
292 for (i
= cold_count
; i
< cold_count
+ hot_count
; i
++) {
293 if (is_extension_exist(name
, extlist
[i
])) {
294 up_read(&sbi
->sb_lock
);
299 up_read(&sbi
->sb_lock
);
301 ext
= F2FS_OPTION(sbi
).extensions
;
303 for (i
= 0; i
< ext_cnt
; i
++) {
304 if (!is_extension_exist(name
, ext
[i
]))
307 set_compress_context(inode
);
312 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
315 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
320 if (unlikely(f2fs_cp_error(sbi
)))
322 if (!f2fs_is_checkpoint_ready(sbi
))
325 err
= dquot_initialize(dir
);
329 inode
= f2fs_new_inode(dir
, mode
);
331 return PTR_ERR(inode
);
333 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
334 set_file_temperature(sbi
, inode
, dentry
->d_name
.name
);
336 set_compress_inode(sbi
, inode
, dentry
->d_name
.name
);
338 inode
->i_op
= &f2fs_file_inode_operations
;
339 inode
->i_fop
= &f2fs_file_operations
;
340 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
344 err
= f2fs_add_link(dentry
, inode
);
349 f2fs_alloc_nid_done(sbi
, ino
);
351 d_instantiate_new(dentry
, inode
);
354 f2fs_sync_fs(sbi
->sb
, 1);
356 f2fs_balance_fs(sbi
, true);
359 f2fs_handle_failed_inode(inode
);
363 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
364 struct dentry
*dentry
)
366 struct inode
*inode
= d_inode(old_dentry
);
367 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
370 if (unlikely(f2fs_cp_error(sbi
)))
372 if (!f2fs_is_checkpoint_ready(sbi
))
375 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
379 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
380 (!projid_eq(F2FS_I(dir
)->i_projid
,
381 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
384 err
= dquot_initialize(dir
);
388 f2fs_balance_fs(sbi
, true);
390 inode
->i_ctime
= current_time(inode
);
393 set_inode_flag(inode
, FI_INC_LINK
);
395 err
= f2fs_add_link(dentry
, inode
);
400 d_instantiate(dentry
, inode
);
403 f2fs_sync_fs(sbi
->sb
, 1);
406 clear_inode_flag(inode
, FI_INC_LINK
);
412 struct dentry
*f2fs_get_parent(struct dentry
*child
)
414 struct qstr dotdot
= QSTR_INIT("..", 2);
416 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
419 return ERR_CAST(page
);
420 return ERR_PTR(-ENOENT
);
422 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
425 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
427 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
428 struct qstr dot
= QSTR_INIT(".", 1);
429 struct qstr dotdot
= QSTR_INIT("..", 2);
430 struct f2fs_dir_entry
*de
;
434 if (f2fs_readonly(sbi
->sb
)) {
435 f2fs_info(sbi
, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
440 err
= dquot_initialize(dir
);
444 f2fs_balance_fs(sbi
, true);
448 de
= f2fs_find_entry(dir
, &dot
, &page
);
450 f2fs_put_page(page
, 0);
451 } else if (IS_ERR(page
)) {
455 err
= f2fs_do_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
460 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
462 f2fs_put_page(page
, 0);
463 else if (IS_ERR(page
))
466 err
= f2fs_do_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
469 clear_inode_flag(dir
, FI_INLINE_DOTS
);
475 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
478 struct inode
*inode
= NULL
;
479 struct f2fs_dir_entry
*de
;
484 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
485 struct fscrypt_name fname
;
487 trace_f2fs_lookup_start(dir
, dentry
, flags
);
489 if (dentry
->d_name
.len
> F2FS_NAME_LEN
) {
494 err
= fscrypt_prepare_lookup(dir
, dentry
, &fname
);
499 de
= __f2fs_find_entry(dir
, &fname
, &page
);
500 fscrypt_free_filename(&fname
);
510 ino
= le32_to_cpu(de
->ino
);
511 f2fs_put_page(page
, 0);
513 inode
= f2fs_iget(dir
->i_sb
, ino
);
515 err
= PTR_ERR(inode
);
519 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
520 err
= __recover_dot_dentries(dir
, root_ino
);
525 if (f2fs_has_inline_dots(inode
)) {
526 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
530 if (IS_ENCRYPTED(dir
) &&
531 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
532 !fscrypt_has_permitted_context(dir
, inode
)) {
533 f2fs_warn(F2FS_I_SB(inode
), "Inconsistent encryption contexts: %lu/%lu",
534 dir
->i_ino
, inode
->i_ino
);
539 #ifdef CONFIG_UNICODE
540 if (!inode
&& IS_CASEFOLDED(dir
)) {
541 /* Eventually we want to call d_add_ci(dentry, NULL)
542 * for negative dentries in the encoding case as
543 * well. For now, prevent the negative dentry
546 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
550 new = d_splice_alias(inode
, dentry
);
551 err
= PTR_ERR_OR_ZERO(new);
552 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
557 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
561 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
563 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
564 struct inode
*inode
= d_inode(dentry
);
565 struct f2fs_dir_entry
*de
;
569 trace_f2fs_unlink_enter(dir
, dentry
);
571 if (unlikely(f2fs_cp_error(sbi
)))
574 err
= dquot_initialize(dir
);
577 err
= dquot_initialize(inode
);
581 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
588 f2fs_balance_fs(sbi
, true);
591 err
= f2fs_acquire_orphan_inode(sbi
);
594 f2fs_put_page(page
, 0);
597 f2fs_delete_entry(de
, page
, dir
, inode
);
598 #ifdef CONFIG_UNICODE
599 /* VFS negative dentries are incompatible with Encoding and
600 * Case-insensitiveness. Eventually we'll want avoid
601 * invalidating the dentries here, alongside with returning the
602 * negative dentries at f2fs_lookup(), when it is better
603 * supported by the VFS for the CI case.
605 if (IS_CASEFOLDED(dir
))
606 d_invalidate(dentry
);
611 f2fs_sync_fs(sbi
->sb
, 1);
613 trace_f2fs_unlink_exit(inode
, err
);
617 static const char *f2fs_get_link(struct dentry
*dentry
,
619 struct delayed_call
*done
)
621 const char *link
= page_get_link(dentry
, inode
, done
);
622 if (!IS_ERR(link
) && !*link
) {
623 /* this is broken symlink case */
624 do_delayed_call(done
);
625 clear_delayed_call(done
);
626 link
= ERR_PTR(-ENOENT
);
631 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
634 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
636 size_t len
= strlen(symname
);
637 struct fscrypt_str disk_link
;
640 if (unlikely(f2fs_cp_error(sbi
)))
642 if (!f2fs_is_checkpoint_ready(sbi
))
645 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
650 err
= dquot_initialize(dir
);
654 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
656 return PTR_ERR(inode
);
658 if (IS_ENCRYPTED(inode
))
659 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
661 inode
->i_op
= &f2fs_symlink_inode_operations
;
662 inode_nohighmem(inode
);
663 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
666 err
= f2fs_add_link(dentry
, inode
);
668 goto out_f2fs_handle_failed_inode
;
670 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
672 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
676 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
679 d_instantiate_new(dentry
, inode
);
682 * Let's flush symlink data in order to avoid broken symlink as much as
683 * possible. Nevertheless, fsyncing is the best way, but there is no
684 * way to get a file descriptor in order to flush that.
686 * Note that, it needs to do dir->fsync to make this recoverable.
687 * If the symlink path is stored into inline_data, there is no
688 * performance regression.
691 filemap_write_and_wait_range(inode
->i_mapping
, 0,
695 f2fs_sync_fs(sbi
->sb
, 1);
697 f2fs_unlink(dir
, dentry
);
700 f2fs_balance_fs(sbi
, true);
701 goto out_free_encrypted_link
;
703 out_f2fs_handle_failed_inode
:
704 f2fs_handle_failed_inode(inode
);
705 out_free_encrypted_link
:
706 if (disk_link
.name
!= (unsigned char *)symname
)
707 kvfree(disk_link
.name
);
711 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
713 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
717 if (unlikely(f2fs_cp_error(sbi
)))
720 err
= dquot_initialize(dir
);
724 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
726 return PTR_ERR(inode
);
728 inode
->i_op
= &f2fs_dir_inode_operations
;
729 inode
->i_fop
= &f2fs_dir_operations
;
730 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
731 inode_nohighmem(inode
);
733 set_inode_flag(inode
, FI_INC_LINK
);
735 err
= f2fs_add_link(dentry
, inode
);
740 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
742 d_instantiate_new(dentry
, inode
);
745 f2fs_sync_fs(sbi
->sb
, 1);
747 f2fs_balance_fs(sbi
, true);
751 clear_inode_flag(inode
, FI_INC_LINK
);
752 f2fs_handle_failed_inode(inode
);
756 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
758 struct inode
*inode
= d_inode(dentry
);
759 if (f2fs_empty_dir(inode
))
760 return f2fs_unlink(dir
, dentry
);
764 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
765 umode_t mode
, dev_t rdev
)
767 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
771 if (unlikely(f2fs_cp_error(sbi
)))
773 if (!f2fs_is_checkpoint_ready(sbi
))
776 err
= dquot_initialize(dir
);
780 inode
= f2fs_new_inode(dir
, mode
);
782 return PTR_ERR(inode
);
784 init_special_inode(inode
, inode
->i_mode
, rdev
);
785 inode
->i_op
= &f2fs_special_inode_operations
;
788 err
= f2fs_add_link(dentry
, inode
);
793 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
795 d_instantiate_new(dentry
, inode
);
798 f2fs_sync_fs(sbi
->sb
, 1);
800 f2fs_balance_fs(sbi
, true);
803 f2fs_handle_failed_inode(inode
);
807 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
808 umode_t mode
, struct inode
**whiteout
)
810 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
814 err
= dquot_initialize(dir
);
818 inode
= f2fs_new_inode(dir
, mode
);
820 return PTR_ERR(inode
);
823 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
824 inode
->i_op
= &f2fs_special_inode_operations
;
826 inode
->i_op
= &f2fs_file_inode_operations
;
827 inode
->i_fop
= &f2fs_file_operations
;
828 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
832 err
= f2fs_acquire_orphan_inode(sbi
);
836 err
= f2fs_do_tmpfile(inode
, dir
);
841 * add this non-linked tmpfile to orphan list, in this way we could
842 * remove all unused data of tmpfile after abnormal power-off.
844 f2fs_add_orphan_inode(inode
);
845 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
848 f2fs_i_links_write(inode
, false);
849 inode
->i_state
|= I_LINKABLE
;
852 d_tmpfile(dentry
, inode
);
854 /* link_count was changed by d_tmpfile as well. */
856 unlock_new_inode(inode
);
858 f2fs_balance_fs(sbi
, true);
862 f2fs_release_orphan_inode(sbi
);
864 f2fs_handle_failed_inode(inode
);
868 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
870 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
872 if (unlikely(f2fs_cp_error(sbi
)))
874 if (!f2fs_is_checkpoint_ready(sbi
))
877 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
880 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
882 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
885 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
888 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
889 struct inode
*new_dir
, struct dentry
*new_dentry
,
892 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
893 struct inode
*old_inode
= d_inode(old_dentry
);
894 struct inode
*new_inode
= d_inode(new_dentry
);
895 struct inode
*whiteout
= NULL
;
896 struct page
*old_dir_page
= NULL
;
897 struct page
*old_page
, *new_page
= NULL
;
898 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
899 struct f2fs_dir_entry
*old_entry
;
900 struct f2fs_dir_entry
*new_entry
;
903 if (unlikely(f2fs_cp_error(sbi
)))
905 if (!f2fs_is_checkpoint_ready(sbi
))
908 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
909 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
910 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
914 * If new_inode is null, the below renaming flow will
915 * add a link in old_dir which can conver inline_dir.
916 * After then, if we failed to get the entry due to other
917 * reasons like ENOMEM, we had to remove the new entry.
918 * Instead of adding such the error handling routine, let's
919 * simply convert first here.
921 if (old_dir
== new_dir
&& !new_inode
) {
922 err
= f2fs_try_convert_inline_dir(old_dir
, new_dentry
);
927 if (flags
& RENAME_WHITEOUT
) {
928 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
933 err
= dquot_initialize(old_dir
);
937 err
= dquot_initialize(new_dir
);
942 err
= dquot_initialize(new_inode
);
948 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
950 if (IS_ERR(old_page
))
951 err
= PTR_ERR(old_page
);
955 if (S_ISDIR(old_inode
->i_mode
)) {
956 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
957 if (!old_dir_entry
) {
958 if (IS_ERR(old_dir_page
))
959 err
= PTR_ERR(old_dir_page
);
967 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
971 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
974 if (IS_ERR(new_page
))
975 err
= PTR_ERR(new_page
);
979 f2fs_balance_fs(sbi
, true);
983 err
= f2fs_acquire_orphan_inode(sbi
);
987 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
990 new_inode
->i_ctime
= current_time(new_inode
);
991 down_write(&F2FS_I(new_inode
)->i_sem
);
993 f2fs_i_links_write(new_inode
, false);
994 f2fs_i_links_write(new_inode
, false);
995 up_write(&F2FS_I(new_inode
)->i_sem
);
997 if (!new_inode
->i_nlink
)
998 f2fs_add_orphan_inode(new_inode
);
1000 f2fs_release_orphan_inode(sbi
);
1002 f2fs_balance_fs(sbi
, true);
1006 err
= f2fs_add_link(new_dentry
, old_inode
);
1008 f2fs_unlock_op(sbi
);
1013 f2fs_i_links_write(new_dir
, true);
1016 down_write(&F2FS_I(old_inode
)->i_sem
);
1017 if (!old_dir_entry
|| whiteout
)
1018 file_lost_pino(old_inode
);
1020 /* adjust dir's i_pino to pass fsck check */
1021 f2fs_i_pino_write(old_inode
, new_dir
->i_ino
);
1022 up_write(&F2FS_I(old_inode
)->i_sem
);
1024 old_inode
->i_ctime
= current_time(old_inode
);
1025 f2fs_mark_inode_dirty_sync(old_inode
, false);
1027 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
1031 set_inode_flag(whiteout
, FI_INC_LINK
);
1032 err
= f2fs_add_link(old_dentry
, whiteout
);
1035 whiteout
->i_state
&= ~I_LINKABLE
;
1039 if (old_dir_entry
) {
1040 if (old_dir
!= new_dir
&& !whiteout
)
1041 f2fs_set_link(old_inode
, old_dir_entry
,
1042 old_dir_page
, new_dir
);
1044 f2fs_put_page(old_dir_page
, 0);
1045 f2fs_i_links_write(old_dir
, false);
1047 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1048 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1049 if (S_ISDIR(old_inode
->i_mode
))
1050 f2fs_add_ino_entry(sbi
, old_inode
->i_ino
,
1054 f2fs_unlock_op(sbi
);
1056 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1057 f2fs_sync_fs(sbi
->sb
, 1);
1059 f2fs_update_time(sbi
, REQ_TIME
);
1063 f2fs_unlock_op(sbi
);
1064 f2fs_put_page(new_page
, 0);
1067 f2fs_put_page(old_dir_page
, 0);
1069 f2fs_put_page(old_page
, 0);
1076 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1077 struct inode
*new_dir
, struct dentry
*new_dentry
)
1079 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
1080 struct inode
*old_inode
= d_inode(old_dentry
);
1081 struct inode
*new_inode
= d_inode(new_dentry
);
1082 struct page
*old_dir_page
, *new_dir_page
;
1083 struct page
*old_page
, *new_page
;
1084 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
1085 struct f2fs_dir_entry
*old_entry
, *new_entry
;
1086 int old_nlink
= 0, new_nlink
= 0;
1089 if (unlikely(f2fs_cp_error(sbi
)))
1091 if (!f2fs_is_checkpoint_ready(sbi
))
1094 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1095 !projid_eq(F2FS_I(new_dir
)->i_projid
,
1096 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
1097 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1098 !projid_eq(F2FS_I(old_dir
)->i_projid
,
1099 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
1102 err
= dquot_initialize(old_dir
);
1106 err
= dquot_initialize(new_dir
);
1111 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
1113 if (IS_ERR(old_page
))
1114 err
= PTR_ERR(old_page
);
1118 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
1120 if (IS_ERR(new_page
))
1121 err
= PTR_ERR(new_page
);
1125 /* prepare for updating ".." directory entry info later */
1126 if (old_dir
!= new_dir
) {
1127 if (S_ISDIR(old_inode
->i_mode
)) {
1128 old_dir_entry
= f2fs_parent_dir(old_inode
,
1130 if (!old_dir_entry
) {
1131 if (IS_ERR(old_dir_page
))
1132 err
= PTR_ERR(old_dir_page
);
1137 if (S_ISDIR(new_inode
->i_mode
)) {
1138 new_dir_entry
= f2fs_parent_dir(new_inode
,
1140 if (!new_dir_entry
) {
1141 if (IS_ERR(new_dir_page
))
1142 err
= PTR_ERR(new_dir_page
);
1149 * If cross rename between file and directory those are not
1150 * in the same directory, we will inc nlink of file's parent
1151 * later, so we should check upper boundary of its nlink.
1153 if ((!old_dir_entry
|| !new_dir_entry
) &&
1154 old_dir_entry
!= new_dir_entry
) {
1155 old_nlink
= old_dir_entry
? -1 : 1;
1156 new_nlink
= -old_nlink
;
1158 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1159 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1163 f2fs_balance_fs(sbi
, true);
1167 /* update ".." directory entry info of old dentry */
1169 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1171 /* update ".." directory entry info of new dentry */
1173 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1175 /* update directory entry info of old dir inode */
1176 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1178 down_write(&F2FS_I(old_inode
)->i_sem
);
1180 file_lost_pino(old_inode
);
1182 /* adjust dir's i_pino to pass fsck check */
1183 f2fs_i_pino_write(old_inode
, new_dir
->i_ino
);
1184 up_write(&F2FS_I(old_inode
)->i_sem
);
1186 old_dir
->i_ctime
= current_time(old_dir
);
1188 down_write(&F2FS_I(old_dir
)->i_sem
);
1189 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1190 up_write(&F2FS_I(old_dir
)->i_sem
);
1192 f2fs_mark_inode_dirty_sync(old_dir
, false);
1194 /* update directory entry info of new dir inode */
1195 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1197 down_write(&F2FS_I(new_inode
)->i_sem
);
1199 file_lost_pino(new_inode
);
1201 /* adjust dir's i_pino to pass fsck check */
1202 f2fs_i_pino_write(new_inode
, old_dir
->i_ino
);
1203 up_write(&F2FS_I(new_inode
)->i_sem
);
1205 new_dir
->i_ctime
= current_time(new_dir
);
1207 down_write(&F2FS_I(new_dir
)->i_sem
);
1208 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1209 up_write(&F2FS_I(new_dir
)->i_sem
);
1211 f2fs_mark_inode_dirty_sync(new_dir
, false);
1213 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1214 f2fs_add_ino_entry(sbi
, old_dir
->i_ino
, TRANS_DIR_INO
);
1215 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1218 f2fs_unlock_op(sbi
);
1220 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1221 f2fs_sync_fs(sbi
->sb
, 1);
1223 f2fs_update_time(sbi
, REQ_TIME
);
1226 if (new_dir_entry
) {
1227 f2fs_put_page(new_dir_page
, 0);
1230 if (old_dir_entry
) {
1231 f2fs_put_page(old_dir_page
, 0);
1234 f2fs_put_page(new_page
, 0);
1236 f2fs_put_page(old_page
, 0);
1241 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1242 struct inode
*new_dir
, struct dentry
*new_dentry
,
1247 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1250 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
1255 if (flags
& RENAME_EXCHANGE
) {
1256 return f2fs_cross_rename(old_dir
, old_dentry
,
1257 new_dir
, new_dentry
);
1260 * VFS has already handled the new dentry existence case,
1261 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1263 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1266 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1267 struct inode
*inode
,
1268 struct delayed_call
*done
)
1274 return ERR_PTR(-ECHILD
);
1276 page
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1278 return ERR_CAST(page
);
1280 target
= fscrypt_get_symlink(inode
, page_address(page
),
1281 inode
->i_sb
->s_blocksize
, done
);
1286 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1287 .get_link
= f2fs_encrypted_get_link
,
1288 .getattr
= f2fs_getattr
,
1289 .setattr
= f2fs_setattr
,
1290 #ifdef CONFIG_F2FS_FS_XATTR
1291 .listxattr
= f2fs_listxattr
,
1295 const struct inode_operations f2fs_dir_inode_operations
= {
1296 .create
= f2fs_create
,
1297 .lookup
= f2fs_lookup
,
1299 .unlink
= f2fs_unlink
,
1300 .symlink
= f2fs_symlink
,
1301 .mkdir
= f2fs_mkdir
,
1302 .rmdir
= f2fs_rmdir
,
1303 .mknod
= f2fs_mknod
,
1304 .rename
= f2fs_rename2
,
1305 .tmpfile
= f2fs_tmpfile
,
1306 .getattr
= f2fs_getattr
,
1307 .setattr
= f2fs_setattr
,
1308 .get_acl
= f2fs_get_acl
,
1309 .set_acl
= f2fs_set_acl
,
1310 #ifdef CONFIG_F2FS_FS_XATTR
1311 .listxattr
= f2fs_listxattr
,
1313 .fiemap
= f2fs_fiemap
,
1316 const struct inode_operations f2fs_symlink_inode_operations
= {
1317 .get_link
= f2fs_get_link
,
1318 .getattr
= f2fs_getattr
,
1319 .setattr
= f2fs_setattr
,
1320 #ifdef CONFIG_F2FS_FS_XATTR
1321 .listxattr
= f2fs_listxattr
,
1325 const struct inode_operations f2fs_special_inode_operations
= {
1326 .getattr
= f2fs_getattr
,
1327 .setattr
= f2fs_setattr
,
1328 .get_acl
= f2fs_get_acl
,
1329 .set_acl
= f2fs_set_acl
,
1330 #ifdef CONFIG_F2FS_FS_XATTR
1331 .listxattr
= f2fs_listxattr
,