4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched.h>
15 #include <linux/ctype.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18 #include <linux/quotaops.h>
24 #include <trace/events/f2fs.h>
26 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
28 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
31 bool nid_free = false;
35 inode = new_inode(dir->i_sb);
37 return ERR_PTR(-ENOMEM);
40 if (!alloc_nid(sbi, &ino)) {
49 inode_init_owner(inode, dir, mode);
53 inode->i_mtime = inode->i_atime = inode->i_ctime =
54 F2FS_I(inode)->i_crtime = current_time(inode);
55 inode->i_generation = sbi->s_next_generation++;
57 err = insert_inode_locked(inode);
63 if (f2fs_sb_has_project_quota(sbi->sb) &&
64 (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
65 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
67 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
70 err = dquot_initialize(inode);
74 err = dquot_alloc_inode(inode);
78 set_inode_flag(inode, FI_NEW_INODE);
80 /* If the directory encrypted, then we should encrypt the inode. */
81 if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
82 f2fs_may_encrypt(inode))
83 f2fs_set_encrypted_inode(inode);
85 if (f2fs_sb_has_extra_attr(sbi->sb)) {
86 set_inode_flag(inode, FI_EXTRA_ATTR);
87 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
90 if (test_opt(sbi, INLINE_XATTR))
91 set_inode_flag(inode, FI_INLINE_XATTR);
93 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
94 set_inode_flag(inode, FI_INLINE_DATA);
95 if (f2fs_may_inline_dentry(inode))
96 set_inode_flag(inode, FI_INLINE_DENTRY);
98 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
99 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
100 if (f2fs_has_inline_xattr(inode))
101 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
102 /* Otherwise, will be 0 */
103 } else if (f2fs_has_inline_xattr(inode) ||
104 f2fs_has_inline_dentry(inode)) {
105 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
107 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
109 f2fs_init_extent_tree(inode, NULL);
111 stat_inc_inline_xattr(inode);
112 stat_inc_inline_inode(inode);
113 stat_inc_inline_dir(inode);
115 F2FS_I(inode)->i_flags =
116 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
118 if (S_ISDIR(inode->i_mode))
119 F2FS_I(inode)->i_flags |= FS_INDEX_FL;
121 if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
122 set_inode_flag(inode, FI_PROJ_INHERIT);
124 trace_f2fs_new_inode(inode, 0);
128 trace_f2fs_new_inode(inode, err);
129 make_bad_inode(inode);
131 set_inode_flag(inode, FI_FREE_NID);
135 trace_f2fs_new_inode(inode, err);
137 inode->i_flags |= S_NOQUOTA;
139 set_inode_flag(inode, FI_FREE_NID);
141 unlock_new_inode(inode);
146 static int is_extension_exist(const unsigned char *s, const char *sub)
148 size_t slen = strlen(s);
149 size_t sublen = strlen(sub);
153 * filename format of multimedia file should be defined as:
154 * "filename + '.' + extension + (optional: '.' + temp extension)".
156 if (slen < sublen + 2)
159 for (i = 1; i < slen - sublen; i++) {
162 if (!strncasecmp(s + i + 1, sub, sublen))
170 * Set multimedia files as cold files for hot/cold data separation
172 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
173 const unsigned char *name)
175 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
176 int i, cold_count, hot_count;
178 down_read(&sbi->sb_lock);
180 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
181 hot_count = sbi->raw_super->hot_ext_count;
183 for (i = 0; i < cold_count + hot_count; i++) {
184 if (!is_extension_exist(name, extlist[i]))
187 file_set_cold(inode);
193 up_read(&sbi->sb_lock);
196 int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
199 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
200 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
201 int hot_count = sbi->raw_super->hot_ext_count;
202 int total_count = cold_count + hot_count;
207 if (total_count == F2FS_MAX_EXTENSION)
210 if (!hot && !cold_count)
212 if (hot && !hot_count)
224 for (i = start; i < count; i++) {
225 if (strcmp(name, extlist[i]))
231 memcpy(extlist[i], extlist[i + 1],
232 F2FS_EXTENSION_LEN * (total_count - i - 1));
233 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
235 sbi->raw_super->hot_ext_count = hot_count - 1;
237 sbi->raw_super->extension_count =
238 cpu_to_le32(cold_count - 1);
246 strncpy(extlist[count], name, strlen(name));
247 sbi->raw_super->hot_ext_count = hot_count + 1;
249 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
251 memcpy(buf, &extlist[cold_count],
252 F2FS_EXTENSION_LEN * hot_count);
253 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
254 strncpy(extlist[cold_count], name, strlen(name));
255 memcpy(&extlist[cold_count + 1], buf,
256 F2FS_EXTENSION_LEN * hot_count);
257 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
262 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
265 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
270 if (unlikely(f2fs_cp_error(sbi)))
273 err = dquot_initialize(dir);
277 inode = f2fs_new_inode(dir, mode);
279 return PTR_ERR(inode);
281 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
282 set_file_temperature(sbi, inode, dentry->d_name.name);
284 inode->i_op = &f2fs_file_inode_operations;
285 inode->i_fop = &f2fs_file_operations;
286 inode->i_mapping->a_ops = &f2fs_dblock_aops;
290 err = f2fs_add_link(dentry, inode);
295 alloc_nid_done(sbi, ino);
297 d_instantiate(dentry, inode);
298 unlock_new_inode(inode);
301 f2fs_sync_fs(sbi->sb, 1);
303 f2fs_balance_fs(sbi, true);
306 handle_failed_inode(inode);
310 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
311 struct dentry *dentry)
313 struct inode *inode = d_inode(old_dentry);
314 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
317 if (unlikely(f2fs_cp_error(sbi)))
320 err = fscrypt_prepare_link(old_dentry, dir, dentry);
324 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
325 (!projid_eq(F2FS_I(dir)->i_projid,
326 F2FS_I(old_dentry->d_inode)->i_projid)))
329 err = dquot_initialize(dir);
333 f2fs_balance_fs(sbi, true);
335 inode->i_ctime = current_time(inode);
338 set_inode_flag(inode, FI_INC_LINK);
340 err = f2fs_add_link(dentry, inode);
345 d_instantiate(dentry, inode);
348 f2fs_sync_fs(sbi->sb, 1);
351 clear_inode_flag(inode, FI_INC_LINK);
357 struct dentry *f2fs_get_parent(struct dentry *child)
359 struct qstr dotdot = QSTR_INIT("..", 2);
361 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
364 return ERR_CAST(page);
365 return ERR_PTR(-ENOENT);
367 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
370 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
372 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
373 struct qstr dot = QSTR_INIT(".", 1);
374 struct qstr dotdot = QSTR_INIT("..", 2);
375 struct f2fs_dir_entry *de;
379 if (f2fs_readonly(sbi->sb)) {
380 f2fs_msg(sbi->sb, KERN_INFO,
381 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
382 "in readonly mountpoint", dir->i_ino, pino);
386 err = dquot_initialize(dir);
390 f2fs_balance_fs(sbi, true);
394 de = f2fs_find_entry(dir, &dot, &page);
396 f2fs_put_page(page, 0);
397 } else if (IS_ERR(page)) {
401 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
406 de = f2fs_find_entry(dir, &dotdot, &page);
408 f2fs_put_page(page, 0);
409 else if (IS_ERR(page))
412 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
415 clear_inode_flag(dir, FI_INLINE_DOTS);
421 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
424 struct inode *inode = NULL;
425 struct f2fs_dir_entry *de;
430 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
432 trace_f2fs_lookup_start(dir, dentry, flags);
434 err = fscrypt_prepare_lookup(dir, dentry, flags);
438 if (dentry->d_name.len > F2FS_NAME_LEN) {
443 de = f2fs_find_entry(dir, &dentry->d_name, &page);
452 ino = le32_to_cpu(de->ino);
453 f2fs_put_page(page, 0);
455 inode = f2fs_iget(dir->i_sb, ino);
457 err = PTR_ERR(inode);
461 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
462 err = __recover_dot_dentries(dir, root_ino);
467 if (f2fs_has_inline_dots(inode)) {
468 err = __recover_dot_dentries(inode, dir->i_ino);
472 if (f2fs_encrypted_inode(dir) &&
473 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
474 !fscrypt_has_permitted_context(dir, inode)) {
475 f2fs_msg(inode->i_sb, KERN_WARNING,
476 "Inconsistent encryption contexts: %lu/%lu",
477 dir->i_ino, inode->i_ino);
482 new = d_splice_alias(inode, dentry);
485 trace_f2fs_lookup_end(dir, dentry, ino, err);
490 trace_f2fs_lookup_end(dir, dentry, ino, err);
494 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
496 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
497 struct inode *inode = d_inode(dentry);
498 struct f2fs_dir_entry *de;
502 trace_f2fs_unlink_enter(dir, dentry);
504 if (unlikely(f2fs_cp_error(sbi)))
507 err = dquot_initialize(dir);
510 err = dquot_initialize(inode);
514 de = f2fs_find_entry(dir, &dentry->d_name, &page);
521 f2fs_balance_fs(sbi, true);
524 err = acquire_orphan_inode(sbi);
527 f2fs_put_page(page, 0);
530 f2fs_delete_entry(de, page, dir, inode);
534 f2fs_sync_fs(sbi->sb, 1);
536 trace_f2fs_unlink_exit(inode, err);
540 static const char *f2fs_get_link(struct dentry *dentry,
542 struct delayed_call *done)
544 const char *link = page_get_link(dentry, inode, done);
545 if (!IS_ERR(link) && !*link) {
546 /* this is broken symlink case */
547 do_delayed_call(done);
548 clear_delayed_call(done);
549 link = ERR_PTR(-ENOENT);
554 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
557 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
559 size_t len = strlen(symname);
560 struct fscrypt_str disk_link;
563 if (unlikely(f2fs_cp_error(sbi)))
566 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
571 err = dquot_initialize(dir);
575 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
577 return PTR_ERR(inode);
579 if (IS_ENCRYPTED(inode))
580 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
582 inode->i_op = &f2fs_symlink_inode_operations;
583 inode_nohighmem(inode);
584 inode->i_mapping->a_ops = &f2fs_dblock_aops;
587 err = f2fs_add_link(dentry, inode);
589 goto out_handle_failed_inode;
591 alloc_nid_done(sbi, inode->i_ino);
593 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
597 err = page_symlink(inode, disk_link.name, disk_link.len);
600 d_instantiate(dentry, inode);
601 unlock_new_inode(inode);
604 * Let's flush symlink data in order to avoid broken symlink as much as
605 * possible. Nevertheless, fsyncing is the best way, but there is no
606 * way to get a file descriptor in order to flush that.
608 * Note that, it needs to do dir->fsync to make this recoverable.
609 * If the symlink path is stored into inline_data, there is no
610 * performance regression.
613 filemap_write_and_wait_range(inode->i_mapping, 0,
617 f2fs_sync_fs(sbi->sb, 1);
619 f2fs_unlink(dir, dentry);
622 f2fs_balance_fs(sbi, true);
623 goto out_free_encrypted_link;
625 out_handle_failed_inode:
626 handle_failed_inode(inode);
627 out_free_encrypted_link:
628 if (disk_link.name != (unsigned char *)symname)
629 kfree(disk_link.name);
633 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
635 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
639 if (unlikely(f2fs_cp_error(sbi)))
642 err = dquot_initialize(dir);
646 inode = f2fs_new_inode(dir, S_IFDIR | mode);
648 return PTR_ERR(inode);
650 inode->i_op = &f2fs_dir_inode_operations;
651 inode->i_fop = &f2fs_dir_operations;
652 inode->i_mapping->a_ops = &f2fs_dblock_aops;
653 inode_nohighmem(inode);
655 set_inode_flag(inode, FI_INC_LINK);
657 err = f2fs_add_link(dentry, inode);
662 alloc_nid_done(sbi, inode->i_ino);
664 d_instantiate(dentry, inode);
665 unlock_new_inode(inode);
668 f2fs_sync_fs(sbi->sb, 1);
670 f2fs_balance_fs(sbi, true);
674 clear_inode_flag(inode, FI_INC_LINK);
675 handle_failed_inode(inode);
679 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
681 struct inode *inode = d_inode(dentry);
682 if (f2fs_empty_dir(inode))
683 return f2fs_unlink(dir, dentry);
687 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
688 umode_t mode, dev_t rdev)
690 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
694 if (unlikely(f2fs_cp_error(sbi)))
697 err = dquot_initialize(dir);
701 inode = f2fs_new_inode(dir, mode);
703 return PTR_ERR(inode);
705 init_special_inode(inode, inode->i_mode, rdev);
706 inode->i_op = &f2fs_special_inode_operations;
709 err = f2fs_add_link(dentry, inode);
714 alloc_nid_done(sbi, inode->i_ino);
716 d_instantiate(dentry, inode);
717 unlock_new_inode(inode);
720 f2fs_sync_fs(sbi->sb, 1);
722 f2fs_balance_fs(sbi, true);
725 handle_failed_inode(inode);
729 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
730 umode_t mode, struct inode **whiteout)
732 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
736 err = dquot_initialize(dir);
740 inode = f2fs_new_inode(dir, mode);
742 return PTR_ERR(inode);
745 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
746 inode->i_op = &f2fs_special_inode_operations;
748 inode->i_op = &f2fs_file_inode_operations;
749 inode->i_fop = &f2fs_file_operations;
750 inode->i_mapping->a_ops = &f2fs_dblock_aops;
754 err = acquire_orphan_inode(sbi);
758 err = f2fs_do_tmpfile(inode, dir);
763 * add this non-linked tmpfile to orphan list, in this way we could
764 * remove all unused data of tmpfile after abnormal power-off.
766 add_orphan_inode(inode);
767 alloc_nid_done(sbi, inode->i_ino);
770 f2fs_i_links_write(inode, false);
773 d_tmpfile(dentry, inode);
775 /* link_count was changed by d_tmpfile as well. */
777 unlock_new_inode(inode);
779 f2fs_balance_fs(sbi, true);
783 release_orphan_inode(sbi);
785 handle_failed_inode(inode);
789 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
791 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
793 if (unlikely(f2fs_cp_error(sbi)))
796 if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
797 int err = fscrypt_get_encryption_info(dir);
802 return __f2fs_tmpfile(dir, dentry, mode, NULL);
805 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
807 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
810 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
813 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
814 struct inode *new_dir, struct dentry *new_dentry,
817 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
818 struct inode *old_inode = d_inode(old_dentry);
819 struct inode *new_inode = d_inode(new_dentry);
820 struct inode *whiteout = NULL;
821 struct page *old_dir_page;
822 struct page *old_page, *new_page = NULL;
823 struct f2fs_dir_entry *old_dir_entry = NULL;
824 struct f2fs_dir_entry *old_entry;
825 struct f2fs_dir_entry *new_entry;
826 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
829 if (unlikely(f2fs_cp_error(sbi)))
832 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
833 (!projid_eq(F2FS_I(new_dir)->i_projid,
834 F2FS_I(old_dentry->d_inode)->i_projid)))
837 err = dquot_initialize(old_dir);
841 err = dquot_initialize(new_dir);
846 err = dquot_initialize(new_inode);
851 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
853 if (IS_ERR(old_page))
854 err = PTR_ERR(old_page);
858 if (S_ISDIR(old_inode->i_mode)) {
859 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
860 if (!old_dir_entry) {
861 if (IS_ERR(old_dir_page))
862 err = PTR_ERR(old_dir_page);
867 if (flags & RENAME_WHITEOUT) {
868 err = f2fs_create_whiteout(old_dir, &whiteout);
876 if (old_dir_entry && !f2fs_empty_dir(new_inode))
880 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
883 if (IS_ERR(new_page))
884 err = PTR_ERR(new_page);
888 f2fs_balance_fs(sbi, true);
892 err = acquire_orphan_inode(sbi);
896 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
898 new_inode->i_ctime = current_time(new_inode);
899 down_write(&F2FS_I(new_inode)->i_sem);
901 f2fs_i_links_write(new_inode, false);
902 f2fs_i_links_write(new_inode, false);
903 up_write(&F2FS_I(new_inode)->i_sem);
905 if (!new_inode->i_nlink)
906 add_orphan_inode(new_inode);
908 release_orphan_inode(sbi);
910 f2fs_balance_fs(sbi, true);
914 err = f2fs_add_link(new_dentry, old_inode);
921 f2fs_i_links_write(new_dir, true);
924 * old entry and new entry can locate in the same inline
925 * dentry in inode, when attaching new entry in inline dentry,
926 * it could force inline dentry conversion, after that,
927 * old_entry and old_page will point to wrong address, in
928 * order to avoid this, let's do the check and update here.
930 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
931 f2fs_put_page(old_page, 0);
934 old_entry = f2fs_find_entry(old_dir,
935 &old_dentry->d_name, &old_page);
938 if (IS_ERR(old_page))
939 err = PTR_ERR(old_page);
946 down_write(&F2FS_I(old_inode)->i_sem);
947 if (!old_dir_entry || whiteout)
948 file_lost_pino(old_inode);
950 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
951 up_write(&F2FS_I(old_inode)->i_sem);
953 old_inode->i_ctime = current_time(old_inode);
954 f2fs_mark_inode_dirty_sync(old_inode, false);
956 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
959 whiteout->i_state |= I_LINKABLE;
960 set_inode_flag(whiteout, FI_INC_LINK);
961 err = f2fs_add_link(old_dentry, whiteout);
964 whiteout->i_state &= ~I_LINKABLE;
969 if (old_dir != new_dir && !whiteout)
970 f2fs_set_link(old_inode, old_dir_entry,
971 old_dir_page, new_dir);
973 f2fs_put_page(old_dir_page, 0);
974 f2fs_i_links_write(old_dir, false);
976 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
977 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
981 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
982 f2fs_sync_fs(sbi->sb, 1);
988 f2fs_put_page(new_page, 0);
994 f2fs_put_page(old_dir_page, 0);
996 f2fs_put_page(old_page, 0);
1001 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1002 struct inode *new_dir, struct dentry *new_dentry)
1004 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1005 struct inode *old_inode = d_inode(old_dentry);
1006 struct inode *new_inode = d_inode(new_dentry);
1007 struct page *old_dir_page, *new_dir_page;
1008 struct page *old_page, *new_page;
1009 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1010 struct f2fs_dir_entry *old_entry, *new_entry;
1011 int old_nlink = 0, new_nlink = 0;
1014 if (unlikely(f2fs_cp_error(sbi)))
1017 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1018 !projid_eq(F2FS_I(new_dir)->i_projid,
1019 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1020 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1021 !projid_eq(F2FS_I(old_dir)->i_projid,
1022 F2FS_I(new_dentry->d_inode)->i_projid)))
1025 err = dquot_initialize(old_dir);
1029 err = dquot_initialize(new_dir);
1033 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1035 if (IS_ERR(old_page))
1036 err = PTR_ERR(old_page);
1040 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1042 if (IS_ERR(new_page))
1043 err = PTR_ERR(new_page);
1047 /* prepare for updating ".." directory entry info later */
1048 if (old_dir != new_dir) {
1049 if (S_ISDIR(old_inode->i_mode)) {
1050 old_dir_entry = f2fs_parent_dir(old_inode,
1052 if (!old_dir_entry) {
1053 if (IS_ERR(old_dir_page))
1054 err = PTR_ERR(old_dir_page);
1059 if (S_ISDIR(new_inode->i_mode)) {
1060 new_dir_entry = f2fs_parent_dir(new_inode,
1062 if (!new_dir_entry) {
1063 if (IS_ERR(new_dir_page))
1064 err = PTR_ERR(new_dir_page);
1071 * If cross rename between file and directory those are not
1072 * in the same directory, we will inc nlink of file's parent
1073 * later, so we should check upper boundary of its nlink.
1075 if ((!old_dir_entry || !new_dir_entry) &&
1076 old_dir_entry != new_dir_entry) {
1077 old_nlink = old_dir_entry ? -1 : 1;
1078 new_nlink = -old_nlink;
1080 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1081 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1085 f2fs_balance_fs(sbi, true);
1089 /* update ".." directory entry info of old dentry */
1091 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1093 /* update ".." directory entry info of new dentry */
1095 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1097 /* update directory entry info of old dir inode */
1098 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1100 down_write(&F2FS_I(old_inode)->i_sem);
1101 file_lost_pino(old_inode);
1102 up_write(&F2FS_I(old_inode)->i_sem);
1104 old_dir->i_ctime = current_time(old_dir);
1106 down_write(&F2FS_I(old_dir)->i_sem);
1107 f2fs_i_links_write(old_dir, old_nlink > 0);
1108 up_write(&F2FS_I(old_dir)->i_sem);
1110 f2fs_mark_inode_dirty_sync(old_dir, false);
1112 /* update directory entry info of new dir inode */
1113 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1115 down_write(&F2FS_I(new_inode)->i_sem);
1116 file_lost_pino(new_inode);
1117 up_write(&F2FS_I(new_inode)->i_sem);
1119 new_dir->i_ctime = current_time(new_dir);
1121 down_write(&F2FS_I(new_dir)->i_sem);
1122 f2fs_i_links_write(new_dir, new_nlink > 0);
1123 up_write(&F2FS_I(new_dir)->i_sem);
1125 f2fs_mark_inode_dirty_sync(new_dir, false);
1127 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1128 add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1129 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1132 f2fs_unlock_op(sbi);
1134 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1135 f2fs_sync_fs(sbi->sb, 1);
1138 if (new_dir_entry) {
1139 f2fs_put_page(new_dir_page, 0);
1142 if (old_dir_entry) {
1143 f2fs_put_page(old_dir_page, 0);
1146 f2fs_put_page(new_page, 0);
1148 f2fs_put_page(old_page, 0);
1153 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1154 struct inode *new_dir, struct dentry *new_dentry,
1159 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1162 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1167 if (flags & RENAME_EXCHANGE) {
1168 return f2fs_cross_rename(old_dir, old_dentry,
1169 new_dir, new_dentry);
1172 * VFS has already handled the new dentry existence case,
1173 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1175 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1178 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1179 struct inode *inode,
1180 struct delayed_call *done)
1186 return ERR_PTR(-ECHILD);
1188 page = read_mapping_page(inode->i_mapping, 0, NULL);
1190 return ERR_CAST(page);
1192 target = fscrypt_get_symlink(inode, page_address(page),
1193 inode->i_sb->s_blocksize, done);
1198 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1199 .get_link = f2fs_encrypted_get_link,
1200 .getattr = f2fs_getattr,
1201 .setattr = f2fs_setattr,
1202 #ifdef CONFIG_F2FS_FS_XATTR
1203 .listxattr = f2fs_listxattr,
1207 const struct inode_operations f2fs_dir_inode_operations = {
1208 .create = f2fs_create,
1209 .lookup = f2fs_lookup,
1211 .unlink = f2fs_unlink,
1212 .symlink = f2fs_symlink,
1213 .mkdir = f2fs_mkdir,
1214 .rmdir = f2fs_rmdir,
1215 .mknod = f2fs_mknod,
1216 .rename = f2fs_rename2,
1217 .tmpfile = f2fs_tmpfile,
1218 .getattr = f2fs_getattr,
1219 .setattr = f2fs_setattr,
1220 .get_acl = f2fs_get_acl,
1221 .set_acl = f2fs_set_acl,
1222 #ifdef CONFIG_F2FS_FS_XATTR
1223 .listxattr = f2fs_listxattr,
1227 const struct inode_operations f2fs_symlink_inode_operations = {
1228 .get_link = f2fs_get_link,
1229 .getattr = f2fs_getattr,
1230 .setattr = f2fs_setattr,
1231 #ifdef CONFIG_F2FS_FS_XATTR
1232 .listxattr = f2fs_listxattr,
1236 const struct inode_operations f2fs_special_inode_operations = {
1237 .getattr = f2fs_getattr,
1238 .setattr = f2fs_setattr,
1239 .get_acl = f2fs_get_acl,
1240 .set_acl = f2fs_set_acl,
1241 #ifdef CONFIG_F2FS_FS_XATTR
1242 .listxattr = f2fs_listxattr,