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;
35 inode = new_inode(dir->i_sb);
37 return ERR_PTR(-ENOMEM);
40 if (!f2fs_alloc_nid(sbi, &ino)) {
49 inode_init_owner(&init_user_ns, inode, dir, mode);
53 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
54 F2FS_I(inode)->i_crtime = inode->i_mtime;
55 inode->i_generation = prandom_u32();
57 if (S_ISDIR(inode->i_mode))
58 F2FS_I(inode)->i_current_depth = 1;
60 err = insert_inode_locked(inode);
66 if (f2fs_sb_has_project_quota(sbi) &&
67 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
68 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
70 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
73 err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
77 err = dquot_initialize(inode);
81 set_inode_flag(inode, FI_NEW_INODE);
84 f2fs_set_encrypted_inode(inode);
86 if (f2fs_sb_has_extra_attr(sbi)) {
87 set_inode_flag(inode, FI_EXTRA_ATTR);
88 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
91 if (test_opt(sbi, INLINE_XATTR))
92 set_inode_flag(inode, FI_INLINE_XATTR);
94 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
95 set_inode_flag(inode, FI_INLINE_DATA);
96 if (f2fs_may_inline_dentry(inode))
97 set_inode_flag(inode, FI_INLINE_DENTRY);
99 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
100 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
101 if (f2fs_has_inline_xattr(inode))
102 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
103 /* Otherwise, will be 0 */
104 } else if (f2fs_has_inline_xattr(inode) ||
105 f2fs_has_inline_dentry(inode)) {
106 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
108 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
110 f2fs_init_extent_tree(inode, NULL);
112 stat_inc_inline_xattr(inode);
113 stat_inc_inline_inode(inode);
114 stat_inc_inline_dir(inode);
116 F2FS_I(inode)->i_flags =
117 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
119 if (S_ISDIR(inode->i_mode))
120 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
122 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
123 set_inode_flag(inode, FI_PROJ_INHERIT);
125 if (f2fs_sb_has_compression(sbi)) {
126 /* Inherit the compression flag in directory */
127 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
128 f2fs_may_compress(inode))
129 set_compress_context(inode);
132 f2fs_set_inode_flags(inode);
134 trace_f2fs_new_inode(inode, 0);
138 trace_f2fs_new_inode(inode, err);
139 make_bad_inode(inode);
141 set_inode_flag(inode, FI_FREE_NID);
145 trace_f2fs_new_inode(inode, err);
147 inode->i_flags |= S_NOQUOTA;
149 set_inode_flag(inode, FI_FREE_NID);
151 unlock_new_inode(inode);
156 static inline int is_extension_exist(const unsigned char *s, const char *sub)
158 size_t slen = strlen(s);
159 size_t sublen = strlen(sub);
162 if (sublen == 1 && *sub == '*')
166 * filename format of multimedia file should be defined as:
167 * "filename + '.' + extension + (optional: '.' + temp extension)".
169 if (slen < sublen + 2)
172 for (i = 1; i < slen - sublen; i++) {
175 if (!strncasecmp(s + i + 1, sub, sublen))
183 * Set file's temperature for hot/cold data separation
185 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
186 const unsigned char *name)
188 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
189 int i, cold_count, hot_count;
191 down_read(&sbi->sb_lock);
193 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
194 hot_count = sbi->raw_super->hot_ext_count;
196 for (i = 0; i < cold_count + hot_count; i++) {
197 if (is_extension_exist(name, extlist[i]))
201 up_read(&sbi->sb_lock);
203 if (i == cold_count + hot_count)
207 file_set_cold(inode);
212 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
215 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
216 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
217 int hot_count = sbi->raw_super->hot_ext_count;
218 int total_count = cold_count + hot_count;
223 if (total_count == F2FS_MAX_EXTENSION)
226 if (!hot && !cold_count)
228 if (hot && !hot_count)
240 for (i = start; i < count; i++) {
241 if (strcmp(name, extlist[i]))
247 memcpy(extlist[i], extlist[i + 1],
248 F2FS_EXTENSION_LEN * (total_count - i - 1));
249 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
251 sbi->raw_super->hot_ext_count = hot_count - 1;
253 sbi->raw_super->extension_count =
254 cpu_to_le32(cold_count - 1);
262 memcpy(extlist[count], name, strlen(name));
263 sbi->raw_super->hot_ext_count = hot_count + 1;
265 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
267 memcpy(buf, &extlist[cold_count],
268 F2FS_EXTENSION_LEN * hot_count);
269 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
270 memcpy(extlist[cold_count], name, strlen(name));
271 memcpy(&extlist[cold_count + 1], buf,
272 F2FS_EXTENSION_LEN * hot_count);
273 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
278 static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
279 const unsigned char *name)
281 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
282 unsigned char (*ext)[F2FS_EXTENSION_LEN];
283 unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
284 int i, cold_count, hot_count;
286 if (!f2fs_sb_has_compression(sbi) ||
287 is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
288 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
289 !f2fs_may_compress(inode))
292 down_read(&sbi->sb_lock);
294 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
295 hot_count = sbi->raw_super->hot_ext_count;
297 for (i = cold_count; i < cold_count + hot_count; i++) {
298 if (is_extension_exist(name, extlist[i])) {
299 up_read(&sbi->sb_lock);
304 up_read(&sbi->sb_lock);
306 ext = F2FS_OPTION(sbi).extensions;
308 for (i = 0; i < ext_cnt; i++) {
309 if (!is_extension_exist(name, ext[i]))
312 set_compress_context(inode);
317 static int f2fs_create(struct user_namespace *mnt_userns, struct inode *dir,
318 struct dentry *dentry, umode_t mode, bool excl)
320 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
325 if (unlikely(f2fs_cp_error(sbi)))
327 if (!f2fs_is_checkpoint_ready(sbi))
330 err = dquot_initialize(dir);
334 inode = f2fs_new_inode(dir, mode);
336 return PTR_ERR(inode);
338 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
339 set_file_temperature(sbi, inode, dentry->d_name.name);
341 set_compress_inode(sbi, inode, dentry->d_name.name);
343 inode->i_op = &f2fs_file_inode_operations;
344 inode->i_fop = &f2fs_file_operations;
345 inode->i_mapping->a_ops = &f2fs_dblock_aops;
349 err = f2fs_add_link(dentry, inode);
354 f2fs_alloc_nid_done(sbi, ino);
356 d_instantiate_new(dentry, inode);
359 f2fs_sync_fs(sbi->sb, 1);
361 f2fs_balance_fs(sbi, true);
364 f2fs_handle_failed_inode(inode);
368 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
369 struct dentry *dentry)
371 struct inode *inode = d_inode(old_dentry);
372 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
375 if (unlikely(f2fs_cp_error(sbi)))
377 if (!f2fs_is_checkpoint_ready(sbi))
380 err = fscrypt_prepare_link(old_dentry, dir, dentry);
384 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
385 (!projid_eq(F2FS_I(dir)->i_projid,
386 F2FS_I(old_dentry->d_inode)->i_projid)))
389 err = dquot_initialize(dir);
393 f2fs_balance_fs(sbi, true);
395 inode->i_ctime = current_time(inode);
398 set_inode_flag(inode, FI_INC_LINK);
400 err = f2fs_add_link(dentry, inode);
405 d_instantiate(dentry, inode);
408 f2fs_sync_fs(sbi->sb, 1);
411 clear_inode_flag(inode, FI_INC_LINK);
417 struct dentry *f2fs_get_parent(struct dentry *child)
419 struct qstr dotdot = QSTR_INIT("..", 2);
421 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
424 return ERR_CAST(page);
425 return ERR_PTR(-ENOENT);
427 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
430 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
432 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
433 struct qstr dot = QSTR_INIT(".", 1);
434 struct qstr dotdot = QSTR_INIT("..", 2);
435 struct f2fs_dir_entry *de;
439 if (f2fs_readonly(sbi->sb)) {
440 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
445 err = dquot_initialize(dir);
449 f2fs_balance_fs(sbi, true);
453 de = f2fs_find_entry(dir, &dot, &page);
455 f2fs_put_page(page, 0);
456 } else if (IS_ERR(page)) {
460 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
465 de = f2fs_find_entry(dir, &dotdot, &page);
467 f2fs_put_page(page, 0);
468 else if (IS_ERR(page))
471 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
474 clear_inode_flag(dir, FI_INLINE_DOTS);
480 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
483 struct inode *inode = NULL;
484 struct f2fs_dir_entry *de;
489 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
490 struct f2fs_filename fname;
492 trace_f2fs_lookup_start(dir, dentry, flags);
494 if (dentry->d_name.len > F2FS_NAME_LEN) {
499 err = f2fs_prepare_lookup(dir, dentry, &fname);
500 generic_set_encrypted_ci_d_ops(dentry);
505 de = __f2fs_find_entry(dir, &fname, &page);
506 f2fs_free_filename(&fname);
517 ino = le32_to_cpu(de->ino);
518 f2fs_put_page(page, 0);
520 inode = f2fs_iget(dir->i_sb, ino);
522 err = PTR_ERR(inode);
526 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
527 err = __recover_dot_dentries(dir, root_ino);
532 if (f2fs_has_inline_dots(inode)) {
533 err = __recover_dot_dentries(inode, dir->i_ino);
537 if (IS_ENCRYPTED(dir) &&
538 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
539 !fscrypt_has_permitted_context(dir, inode)) {
540 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
541 dir->i_ino, inode->i_ino);
546 #ifdef CONFIG_UNICODE
547 if (!inode && IS_CASEFOLDED(dir)) {
548 /* Eventually we want to call d_add_ci(dentry, NULL)
549 * for negative dentries in the encoding case as
550 * well. For now, prevent the negative dentry
553 trace_f2fs_lookup_end(dir, dentry, ino, err);
557 new = d_splice_alias(inode, dentry);
558 err = PTR_ERR_OR_ZERO(new);
559 trace_f2fs_lookup_end(dir, dentry, ino, !new ? -ENOENT : err);
564 trace_f2fs_lookup_end(dir, dentry, ino, err);
568 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
570 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
571 struct inode *inode = d_inode(dentry);
572 struct f2fs_dir_entry *de;
576 trace_f2fs_unlink_enter(dir, dentry);
578 if (unlikely(f2fs_cp_error(sbi))) {
583 err = dquot_initialize(dir);
586 err = dquot_initialize(inode);
590 de = f2fs_find_entry(dir, &dentry->d_name, &page);
597 f2fs_balance_fs(sbi, true);
600 err = f2fs_acquire_orphan_inode(sbi);
603 f2fs_put_page(page, 0);
606 f2fs_delete_entry(de, page, dir, inode);
607 #ifdef CONFIG_UNICODE
608 /* VFS negative dentries are incompatible with Encoding and
609 * Case-insensitiveness. Eventually we'll want avoid
610 * invalidating the dentries here, alongside with returning the
611 * negative dentries at f2fs_lookup(), when it is better
612 * supported by the VFS for the CI case.
614 if (IS_CASEFOLDED(dir))
615 d_invalidate(dentry);
620 f2fs_sync_fs(sbi->sb, 1);
622 trace_f2fs_unlink_exit(inode, err);
626 static const char *f2fs_get_link(struct dentry *dentry,
628 struct delayed_call *done)
630 const char *link = page_get_link(dentry, inode, done);
631 if (!IS_ERR(link) && !*link) {
632 /* this is broken symlink case */
633 do_delayed_call(done);
634 clear_delayed_call(done);
635 link = ERR_PTR(-ENOENT);
640 static int f2fs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
641 struct dentry *dentry, const char *symname)
643 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
645 size_t len = strlen(symname);
646 struct fscrypt_str disk_link;
649 if (unlikely(f2fs_cp_error(sbi)))
651 if (!f2fs_is_checkpoint_ready(sbi))
654 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
659 err = dquot_initialize(dir);
663 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
665 return PTR_ERR(inode);
667 if (IS_ENCRYPTED(inode))
668 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
670 inode->i_op = &f2fs_symlink_inode_operations;
671 inode_nohighmem(inode);
672 inode->i_mapping->a_ops = &f2fs_dblock_aops;
675 err = f2fs_add_link(dentry, inode);
677 goto out_f2fs_handle_failed_inode;
679 f2fs_alloc_nid_done(sbi, inode->i_ino);
681 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
685 err = page_symlink(inode, disk_link.name, disk_link.len);
688 d_instantiate_new(dentry, inode);
691 * Let's flush symlink data in order to avoid broken symlink as much as
692 * possible. Nevertheless, fsyncing is the best way, but there is no
693 * way to get a file descriptor in order to flush that.
695 * Note that, it needs to do dir->fsync to make this recoverable.
696 * If the symlink path is stored into inline_data, there is no
697 * performance regression.
700 filemap_write_and_wait_range(inode->i_mapping, 0,
704 f2fs_sync_fs(sbi->sb, 1);
706 f2fs_unlink(dir, dentry);
709 f2fs_balance_fs(sbi, true);
710 goto out_free_encrypted_link;
712 out_f2fs_handle_failed_inode:
713 f2fs_handle_failed_inode(inode);
714 out_free_encrypted_link:
715 if (disk_link.name != (unsigned char *)symname)
716 kfree(disk_link.name);
720 static int f2fs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
721 struct dentry *dentry, umode_t mode)
723 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
727 if (unlikely(f2fs_cp_error(sbi)))
730 err = dquot_initialize(dir);
734 inode = f2fs_new_inode(dir, S_IFDIR | mode);
736 return PTR_ERR(inode);
738 inode->i_op = &f2fs_dir_inode_operations;
739 inode->i_fop = &f2fs_dir_operations;
740 inode->i_mapping->a_ops = &f2fs_dblock_aops;
741 inode_nohighmem(inode);
743 set_inode_flag(inode, FI_INC_LINK);
745 err = f2fs_add_link(dentry, inode);
750 f2fs_alloc_nid_done(sbi, inode->i_ino);
752 d_instantiate_new(dentry, inode);
755 f2fs_sync_fs(sbi->sb, 1);
757 f2fs_balance_fs(sbi, true);
761 clear_inode_flag(inode, FI_INC_LINK);
762 f2fs_handle_failed_inode(inode);
766 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
768 struct inode *inode = d_inode(dentry);
769 if (f2fs_empty_dir(inode))
770 return f2fs_unlink(dir, dentry);
774 static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
775 struct dentry *dentry, umode_t mode, dev_t rdev)
777 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
781 if (unlikely(f2fs_cp_error(sbi)))
783 if (!f2fs_is_checkpoint_ready(sbi))
786 err = dquot_initialize(dir);
790 inode = f2fs_new_inode(dir, mode);
792 return PTR_ERR(inode);
794 init_special_inode(inode, inode->i_mode, rdev);
795 inode->i_op = &f2fs_special_inode_operations;
798 err = f2fs_add_link(dentry, inode);
803 f2fs_alloc_nid_done(sbi, inode->i_ino);
805 d_instantiate_new(dentry, inode);
808 f2fs_sync_fs(sbi->sb, 1);
810 f2fs_balance_fs(sbi, true);
813 f2fs_handle_failed_inode(inode);
817 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
818 umode_t mode, struct inode **whiteout)
820 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
824 err = dquot_initialize(dir);
828 inode = f2fs_new_inode(dir, mode);
830 return PTR_ERR(inode);
833 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
834 inode->i_op = &f2fs_special_inode_operations;
836 inode->i_op = &f2fs_file_inode_operations;
837 inode->i_fop = &f2fs_file_operations;
838 inode->i_mapping->a_ops = &f2fs_dblock_aops;
842 err = f2fs_acquire_orphan_inode(sbi);
846 err = f2fs_do_tmpfile(inode, dir);
851 * add this non-linked tmpfile to orphan list, in this way we could
852 * remove all unused data of tmpfile after abnormal power-off.
854 f2fs_add_orphan_inode(inode);
855 f2fs_alloc_nid_done(sbi, inode->i_ino);
858 f2fs_i_links_write(inode, false);
860 spin_lock(&inode->i_lock);
861 inode->i_state |= I_LINKABLE;
862 spin_unlock(&inode->i_lock);
866 d_tmpfile(dentry, inode);
868 /* link_count was changed by d_tmpfile as well. */
870 unlock_new_inode(inode);
872 f2fs_balance_fs(sbi, true);
876 f2fs_release_orphan_inode(sbi);
878 f2fs_handle_failed_inode(inode);
882 static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
883 struct dentry *dentry, umode_t mode)
885 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
887 if (unlikely(f2fs_cp_error(sbi)))
889 if (!f2fs_is_checkpoint_ready(sbi))
892 return __f2fs_tmpfile(dir, dentry, mode, NULL);
895 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
897 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
900 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
903 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
904 struct inode *new_dir, struct dentry *new_dentry,
907 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
908 struct inode *old_inode = d_inode(old_dentry);
909 struct inode *new_inode = d_inode(new_dentry);
910 struct inode *whiteout = NULL;
911 struct page *old_dir_page = NULL;
912 struct page *old_page, *new_page = NULL;
913 struct f2fs_dir_entry *old_dir_entry = NULL;
914 struct f2fs_dir_entry *old_entry;
915 struct f2fs_dir_entry *new_entry;
918 if (unlikely(f2fs_cp_error(sbi)))
920 if (!f2fs_is_checkpoint_ready(sbi))
923 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
924 (!projid_eq(F2FS_I(new_dir)->i_projid,
925 F2FS_I(old_dentry->d_inode)->i_projid)))
929 * If new_inode is null, the below renaming flow will
930 * add a link in old_dir which can conver inline_dir.
931 * After then, if we failed to get the entry due to other
932 * reasons like ENOMEM, we had to remove the new entry.
933 * Instead of adding such the error handling routine, let's
934 * simply convert first here.
936 if (old_dir == new_dir && !new_inode) {
937 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
942 if (flags & RENAME_WHITEOUT) {
943 err = f2fs_create_whiteout(old_dir, &whiteout);
948 err = dquot_initialize(old_dir);
952 err = dquot_initialize(new_dir);
957 err = dquot_initialize(new_inode);
963 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
965 if (IS_ERR(old_page))
966 err = PTR_ERR(old_page);
970 if (S_ISDIR(old_inode->i_mode)) {
971 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
972 if (!old_dir_entry) {
973 if (IS_ERR(old_dir_page))
974 err = PTR_ERR(old_dir_page);
982 if (old_dir_entry && !f2fs_empty_dir(new_inode))
986 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
989 if (IS_ERR(new_page))
990 err = PTR_ERR(new_page);
994 f2fs_balance_fs(sbi, true);
998 err = f2fs_acquire_orphan_inode(sbi);
1002 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1005 new_inode->i_ctime = current_time(new_inode);
1006 down_write(&F2FS_I(new_inode)->i_sem);
1008 f2fs_i_links_write(new_inode, false);
1009 f2fs_i_links_write(new_inode, false);
1010 up_write(&F2FS_I(new_inode)->i_sem);
1012 if (!new_inode->i_nlink)
1013 f2fs_add_orphan_inode(new_inode);
1015 f2fs_release_orphan_inode(sbi);
1017 f2fs_balance_fs(sbi, true);
1021 err = f2fs_add_link(new_dentry, old_inode);
1023 f2fs_unlock_op(sbi);
1028 f2fs_i_links_write(new_dir, true);
1031 down_write(&F2FS_I(old_inode)->i_sem);
1032 if (!old_dir_entry || whiteout)
1033 file_lost_pino(old_inode);
1035 /* adjust dir's i_pino to pass fsck check */
1036 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1037 up_write(&F2FS_I(old_inode)->i_sem);
1039 old_inode->i_ctime = current_time(old_inode);
1040 f2fs_mark_inode_dirty_sync(old_inode, false);
1042 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1046 set_inode_flag(whiteout, FI_INC_LINK);
1047 err = f2fs_add_link(old_dentry, whiteout);
1051 spin_lock(&whiteout->i_lock);
1052 whiteout->i_state &= ~I_LINKABLE;
1053 spin_unlock(&whiteout->i_lock);
1058 if (old_dir_entry) {
1059 if (old_dir != new_dir && !whiteout)
1060 f2fs_set_link(old_inode, old_dir_entry,
1061 old_dir_page, new_dir);
1063 f2fs_put_page(old_dir_page, 0);
1064 f2fs_i_links_write(old_dir, false);
1066 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1067 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1068 if (S_ISDIR(old_inode->i_mode))
1069 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1073 f2fs_unlock_op(sbi);
1075 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1076 f2fs_sync_fs(sbi->sb, 1);
1078 f2fs_update_time(sbi, REQ_TIME);
1082 f2fs_unlock_op(sbi);
1083 f2fs_put_page(new_page, 0);
1086 f2fs_put_page(old_dir_page, 0);
1088 f2fs_put_page(old_page, 0);
1095 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1096 struct inode *new_dir, struct dentry *new_dentry)
1098 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1099 struct inode *old_inode = d_inode(old_dentry);
1100 struct inode *new_inode = d_inode(new_dentry);
1101 struct page *old_dir_page, *new_dir_page;
1102 struct page *old_page, *new_page;
1103 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1104 struct f2fs_dir_entry *old_entry, *new_entry;
1105 int old_nlink = 0, new_nlink = 0;
1108 if (unlikely(f2fs_cp_error(sbi)))
1110 if (!f2fs_is_checkpoint_ready(sbi))
1113 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1114 !projid_eq(F2FS_I(new_dir)->i_projid,
1115 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1116 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1117 !projid_eq(F2FS_I(old_dir)->i_projid,
1118 F2FS_I(new_dentry->d_inode)->i_projid)))
1121 err = dquot_initialize(old_dir);
1125 err = dquot_initialize(new_dir);
1130 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1132 if (IS_ERR(old_page))
1133 err = PTR_ERR(old_page);
1137 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1139 if (IS_ERR(new_page))
1140 err = PTR_ERR(new_page);
1144 /* prepare for updating ".." directory entry info later */
1145 if (old_dir != new_dir) {
1146 if (S_ISDIR(old_inode->i_mode)) {
1147 old_dir_entry = f2fs_parent_dir(old_inode,
1149 if (!old_dir_entry) {
1150 if (IS_ERR(old_dir_page))
1151 err = PTR_ERR(old_dir_page);
1156 if (S_ISDIR(new_inode->i_mode)) {
1157 new_dir_entry = f2fs_parent_dir(new_inode,
1159 if (!new_dir_entry) {
1160 if (IS_ERR(new_dir_page))
1161 err = PTR_ERR(new_dir_page);
1168 * If cross rename between file and directory those are not
1169 * in the same directory, we will inc nlink of file's parent
1170 * later, so we should check upper boundary of its nlink.
1172 if ((!old_dir_entry || !new_dir_entry) &&
1173 old_dir_entry != new_dir_entry) {
1174 old_nlink = old_dir_entry ? -1 : 1;
1175 new_nlink = -old_nlink;
1177 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1178 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1182 f2fs_balance_fs(sbi, true);
1186 /* update ".." directory entry info of old dentry */
1188 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1190 /* update ".." directory entry info of new dentry */
1192 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1194 /* update directory entry info of old dir inode */
1195 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1197 down_write(&F2FS_I(old_inode)->i_sem);
1199 file_lost_pino(old_inode);
1201 /* adjust dir's i_pino to pass fsck check */
1202 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1203 up_write(&F2FS_I(old_inode)->i_sem);
1205 old_dir->i_ctime = current_time(old_dir);
1207 down_write(&F2FS_I(old_dir)->i_sem);
1208 f2fs_i_links_write(old_dir, old_nlink > 0);
1209 up_write(&F2FS_I(old_dir)->i_sem);
1211 f2fs_mark_inode_dirty_sync(old_dir, false);
1213 /* update directory entry info of new dir inode */
1214 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1216 down_write(&F2FS_I(new_inode)->i_sem);
1218 file_lost_pino(new_inode);
1220 /* adjust dir's i_pino to pass fsck check */
1221 f2fs_i_pino_write(new_inode, old_dir->i_ino);
1222 up_write(&F2FS_I(new_inode)->i_sem);
1224 new_dir->i_ctime = current_time(new_dir);
1226 down_write(&F2FS_I(new_dir)->i_sem);
1227 f2fs_i_links_write(new_dir, new_nlink > 0);
1228 up_write(&F2FS_I(new_dir)->i_sem);
1230 f2fs_mark_inode_dirty_sync(new_dir, false);
1232 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1233 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1234 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1237 f2fs_unlock_op(sbi);
1239 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1240 f2fs_sync_fs(sbi->sb, 1);
1242 f2fs_update_time(sbi, REQ_TIME);
1245 if (new_dir_entry) {
1246 f2fs_put_page(new_dir_page, 0);
1249 if (old_dir_entry) {
1250 f2fs_put_page(old_dir_page, 0);
1253 f2fs_put_page(new_page, 0);
1255 f2fs_put_page(old_page, 0);
1260 static int f2fs_rename2(struct user_namespace *mnt_userns,
1261 struct inode *old_dir, struct dentry *old_dentry,
1262 struct inode *new_dir, struct dentry *new_dentry,
1267 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1270 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1275 if (flags & RENAME_EXCHANGE) {
1276 return f2fs_cross_rename(old_dir, old_dentry,
1277 new_dir, new_dentry);
1280 * VFS has already handled the new dentry existence case,
1281 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1283 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1286 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1287 struct inode *inode,
1288 struct delayed_call *done)
1294 return ERR_PTR(-ECHILD);
1296 page = read_mapping_page(inode->i_mapping, 0, NULL);
1298 return ERR_CAST(page);
1300 target = fscrypt_get_symlink(inode, page_address(page),
1301 inode->i_sb->s_blocksize, done);
1306 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1307 .get_link = f2fs_encrypted_get_link,
1308 .getattr = f2fs_getattr,
1309 .setattr = f2fs_setattr,
1310 .listxattr = f2fs_listxattr,
1313 const struct inode_operations f2fs_dir_inode_operations = {
1314 .create = f2fs_create,
1315 .lookup = f2fs_lookup,
1317 .unlink = f2fs_unlink,
1318 .symlink = f2fs_symlink,
1319 .mkdir = f2fs_mkdir,
1320 .rmdir = f2fs_rmdir,
1321 .mknod = f2fs_mknod,
1322 .rename = f2fs_rename2,
1323 .tmpfile = f2fs_tmpfile,
1324 .getattr = f2fs_getattr,
1325 .setattr = f2fs_setattr,
1326 .get_acl = f2fs_get_acl,
1327 .set_acl = f2fs_set_acl,
1328 .listxattr = f2fs_listxattr,
1329 .fiemap = f2fs_fiemap,
1330 .fileattr_get = f2fs_fileattr_get,
1331 .fileattr_set = f2fs_fileattr_set,
1334 const struct inode_operations f2fs_symlink_inode_operations = {
1335 .get_link = f2fs_get_link,
1336 .getattr = f2fs_getattr,
1337 .setattr = f2fs_setattr,
1338 .listxattr = f2fs_listxattr,
1341 const struct inode_operations f2fs_special_inode_operations = {
1342 .getattr = f2fs_getattr,
1343 .setattr = f2fs_setattr,
1344 .get_acl = f2fs_get_acl,
1345 .set_acl = f2fs_set_acl,
1346 .listxattr = f2fs_listxattr,