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 (!f2fs_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 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->sb) &&
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 = dquot_initialize(inode);
77 err = dquot_alloc_inode(inode);
81 set_inode_flag(inode, FI_NEW_INODE);
83 /* If the directory encrypted, then we should encrypt the inode. */
84 if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
85 f2fs_may_encrypt(inode))
86 f2fs_set_encrypted_inode(inode);
88 if (f2fs_sb_has_extra_attr(sbi->sb)) {
89 set_inode_flag(inode, FI_EXTRA_ATTR);
90 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
93 if (test_opt(sbi, INLINE_XATTR))
94 set_inode_flag(inode, FI_INLINE_XATTR);
96 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
97 set_inode_flag(inode, FI_INLINE_DATA);
98 if (f2fs_may_inline_dentry(inode))
99 set_inode_flag(inode, FI_INLINE_DENTRY);
101 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
102 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
103 if (f2fs_has_inline_xattr(inode))
104 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
105 /* Otherwise, will be 0 */
106 } else if (f2fs_has_inline_xattr(inode) ||
107 f2fs_has_inline_dentry(inode)) {
108 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
110 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
112 f2fs_init_extent_tree(inode, NULL);
114 stat_inc_inline_xattr(inode);
115 stat_inc_inline_inode(inode);
116 stat_inc_inline_dir(inode);
118 F2FS_I(inode)->i_flags =
119 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
121 if (S_ISDIR(inode->i_mode))
122 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
124 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
125 set_inode_flag(inode, FI_PROJ_INHERIT);
127 trace_f2fs_new_inode(inode, 0);
131 trace_f2fs_new_inode(inode, err);
132 make_bad_inode(inode);
134 set_inode_flag(inode, FI_FREE_NID);
138 trace_f2fs_new_inode(inode, err);
140 inode->i_flags |= S_NOQUOTA;
142 set_inode_flag(inode, FI_FREE_NID);
144 unlock_new_inode(inode);
149 static int is_extension_exist(const unsigned char *s, const char *sub)
151 size_t slen = strlen(s);
152 size_t sublen = strlen(sub);
156 * filename format of multimedia file should be defined as:
157 * "filename + '.' + extension + (optional: '.' + temp extension)".
159 if (slen < sublen + 2)
162 for (i = 1; i < slen - sublen; i++) {
165 if (!strncasecmp(s + i + 1, sub, sublen))
173 * Set multimedia files as cold files for hot/cold data separation
175 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
176 const unsigned char *name)
178 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
179 int i, cold_count, hot_count;
181 down_read(&sbi->sb_lock);
183 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
184 hot_count = sbi->raw_super->hot_ext_count;
186 for (i = 0; i < cold_count + hot_count; i++) {
187 if (!is_extension_exist(name, extlist[i]))
190 file_set_cold(inode);
196 up_read(&sbi->sb_lock);
199 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
202 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
203 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
204 int hot_count = sbi->raw_super->hot_ext_count;
205 int total_count = cold_count + hot_count;
210 if (total_count == F2FS_MAX_EXTENSION)
213 if (!hot && !cold_count)
215 if (hot && !hot_count)
227 for (i = start; i < count; i++) {
228 if (strcmp(name, extlist[i]))
234 memcpy(extlist[i], extlist[i + 1],
235 F2FS_EXTENSION_LEN * (total_count - i - 1));
236 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
238 sbi->raw_super->hot_ext_count = hot_count - 1;
240 sbi->raw_super->extension_count =
241 cpu_to_le32(cold_count - 1);
249 strncpy(extlist[count], name, strlen(name));
250 sbi->raw_super->hot_ext_count = hot_count + 1;
252 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
254 memcpy(buf, &extlist[cold_count],
255 F2FS_EXTENSION_LEN * hot_count);
256 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
257 strncpy(extlist[cold_count], name, strlen(name));
258 memcpy(&extlist[cold_count + 1], buf,
259 F2FS_EXTENSION_LEN * hot_count);
260 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
265 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
268 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
273 if (unlikely(f2fs_cp_error(sbi)))
276 err = dquot_initialize(dir);
280 inode = f2fs_new_inode(dir, mode);
282 return PTR_ERR(inode);
284 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
285 set_file_temperature(sbi, inode, dentry->d_name.name);
287 inode->i_op = &f2fs_file_inode_operations;
288 inode->i_fop = &f2fs_file_operations;
289 inode->i_mapping->a_ops = &f2fs_dblock_aops;
293 err = f2fs_add_link(dentry, inode);
298 f2fs_alloc_nid_done(sbi, ino);
300 d_instantiate_new(dentry, inode);
303 f2fs_sync_fs(sbi->sb, 1);
305 f2fs_balance_fs(sbi, true);
308 f2fs_handle_failed_inode(inode);
312 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
313 struct dentry *dentry)
315 struct inode *inode = d_inode(old_dentry);
316 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
319 if (unlikely(f2fs_cp_error(sbi)))
322 err = fscrypt_prepare_link(old_dentry, dir, dentry);
326 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
327 (!projid_eq(F2FS_I(dir)->i_projid,
328 F2FS_I(old_dentry->d_inode)->i_projid)))
331 err = dquot_initialize(dir);
335 f2fs_balance_fs(sbi, true);
337 inode->i_ctime = current_time(inode);
340 set_inode_flag(inode, FI_INC_LINK);
342 err = f2fs_add_link(dentry, inode);
347 d_instantiate(dentry, inode);
350 f2fs_sync_fs(sbi->sb, 1);
353 clear_inode_flag(inode, FI_INC_LINK);
359 struct dentry *f2fs_get_parent(struct dentry *child)
361 struct qstr dotdot = QSTR_INIT("..", 2);
363 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
366 return ERR_CAST(page);
367 return ERR_PTR(-ENOENT);
369 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
372 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
374 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
375 struct qstr dot = QSTR_INIT(".", 1);
376 struct qstr dotdot = QSTR_INIT("..", 2);
377 struct f2fs_dir_entry *de;
381 if (f2fs_readonly(sbi->sb)) {
382 f2fs_msg(sbi->sb, KERN_INFO,
383 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
384 "in readonly mountpoint", dir->i_ino, pino);
388 err = dquot_initialize(dir);
392 f2fs_balance_fs(sbi, true);
396 de = f2fs_find_entry(dir, &dot, &page);
398 f2fs_put_page(page, 0);
399 } else if (IS_ERR(page)) {
403 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
408 de = f2fs_find_entry(dir, &dotdot, &page);
410 f2fs_put_page(page, 0);
411 else if (IS_ERR(page))
414 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
417 clear_inode_flag(dir, FI_INLINE_DOTS);
423 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
426 struct inode *inode = NULL;
427 struct f2fs_dir_entry *de;
432 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
434 trace_f2fs_lookup_start(dir, dentry, flags);
436 err = fscrypt_prepare_lookup(dir, dentry, flags);
440 if (dentry->d_name.len > F2FS_NAME_LEN) {
445 de = f2fs_find_entry(dir, &dentry->d_name, &page);
454 ino = le32_to_cpu(de->ino);
455 f2fs_put_page(page, 0);
457 inode = f2fs_iget(dir->i_sb, ino);
459 err = PTR_ERR(inode);
463 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
464 err = __recover_dot_dentries(dir, root_ino);
469 if (f2fs_has_inline_dots(inode)) {
470 err = __recover_dot_dentries(inode, dir->i_ino);
474 if (f2fs_encrypted_inode(dir) &&
475 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
476 !fscrypt_has_permitted_context(dir, inode)) {
477 f2fs_msg(inode->i_sb, KERN_WARNING,
478 "Inconsistent encryption contexts: %lu/%lu",
479 dir->i_ino, inode->i_ino);
484 new = d_splice_alias(inode, dentry);
487 trace_f2fs_lookup_end(dir, dentry, ino, err);
492 trace_f2fs_lookup_end(dir, dentry, ino, err);
496 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
498 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
499 struct inode *inode = d_inode(dentry);
500 struct f2fs_dir_entry *de;
504 trace_f2fs_unlink_enter(dir, dentry);
506 if (unlikely(f2fs_cp_error(sbi)))
509 err = dquot_initialize(dir);
512 err = dquot_initialize(inode);
516 de = f2fs_find_entry(dir, &dentry->d_name, &page);
523 f2fs_balance_fs(sbi, true);
526 err = f2fs_acquire_orphan_inode(sbi);
529 f2fs_put_page(page, 0);
532 f2fs_delete_entry(de, page, dir, inode);
536 f2fs_sync_fs(sbi->sb, 1);
538 trace_f2fs_unlink_exit(inode, err);
542 static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
544 const char *link = page_follow_link_light(dentry, cookie);
545 if (!IS_ERR(link) && !*link) {
546 /* this is broken symlink case */
547 page_put_link(NULL, *cookie);
548 link = ERR_PTR(-ENOENT);
553 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
556 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
558 size_t len = strlen(symname);
559 struct fscrypt_str disk_link;
562 if (unlikely(f2fs_cp_error(sbi)))
565 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
570 err = dquot_initialize(dir);
574 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
576 return PTR_ERR(inode);
578 if (IS_ENCRYPTED(inode))
579 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
581 inode->i_op = &f2fs_symlink_inode_operations;
582 inode_nohighmem(inode);
583 inode->i_mapping->a_ops = &f2fs_dblock_aops;
586 err = f2fs_add_link(dentry, inode);
588 goto out_f2fs_handle_failed_inode;
590 f2fs_alloc_nid_done(sbi, inode->i_ino);
592 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
596 err = page_symlink(inode, disk_link.name, disk_link.len);
599 d_instantiate_new(dentry, inode);
602 * Let's flush symlink data in order to avoid broken symlink as much as
603 * possible. Nevertheless, fsyncing is the best way, but there is no
604 * way to get a file descriptor in order to flush that.
606 * Note that, it needs to do dir->fsync to make this recoverable.
607 * If the symlink path is stored into inline_data, there is no
608 * performance regression.
611 filemap_write_and_wait_range(inode->i_mapping, 0,
615 f2fs_sync_fs(sbi->sb, 1);
617 f2fs_unlink(dir, dentry);
620 f2fs_balance_fs(sbi, true);
621 goto out_free_encrypted_link;
623 out_f2fs_handle_failed_inode:
624 f2fs_handle_failed_inode(inode);
625 out_free_encrypted_link:
626 if (disk_link.name != (unsigned char *)symname)
627 kfree(disk_link.name);
631 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
633 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
637 if (unlikely(f2fs_cp_error(sbi)))
640 err = dquot_initialize(dir);
644 inode = f2fs_new_inode(dir, S_IFDIR | mode);
646 return PTR_ERR(inode);
648 inode->i_op = &f2fs_dir_inode_operations;
649 inode->i_fop = &f2fs_dir_operations;
650 inode->i_mapping->a_ops = &f2fs_dblock_aops;
651 inode_nohighmem(inode);
653 set_inode_flag(inode, FI_INC_LINK);
655 err = f2fs_add_link(dentry, inode);
660 f2fs_alloc_nid_done(sbi, inode->i_ino);
662 d_instantiate_new(dentry, inode);
665 f2fs_sync_fs(sbi->sb, 1);
667 f2fs_balance_fs(sbi, true);
671 clear_inode_flag(inode, FI_INC_LINK);
672 f2fs_handle_failed_inode(inode);
676 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
678 struct inode *inode = d_inode(dentry);
679 if (f2fs_empty_dir(inode))
680 return f2fs_unlink(dir, dentry);
684 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
685 umode_t mode, dev_t rdev)
687 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
691 if (unlikely(f2fs_cp_error(sbi)))
694 err = dquot_initialize(dir);
698 inode = f2fs_new_inode(dir, mode);
700 return PTR_ERR(inode);
702 init_special_inode(inode, inode->i_mode, rdev);
703 inode->i_op = &f2fs_special_inode_operations;
706 err = f2fs_add_link(dentry, inode);
711 f2fs_alloc_nid_done(sbi, inode->i_ino);
713 d_instantiate_new(dentry, inode);
716 f2fs_sync_fs(sbi->sb, 1);
718 f2fs_balance_fs(sbi, true);
721 f2fs_handle_failed_inode(inode);
725 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
726 umode_t mode, struct inode **whiteout)
728 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
732 err = dquot_initialize(dir);
736 inode = f2fs_new_inode(dir, mode);
738 return PTR_ERR(inode);
741 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
742 inode->i_op = &f2fs_special_inode_operations;
744 inode->i_op = &f2fs_file_inode_operations;
745 inode->i_fop = &f2fs_file_operations;
746 inode->i_mapping->a_ops = &f2fs_dblock_aops;
750 err = f2fs_acquire_orphan_inode(sbi);
754 err = f2fs_do_tmpfile(inode, dir);
759 * add this non-linked tmpfile to orphan list, in this way we could
760 * remove all unused data of tmpfile after abnormal power-off.
762 f2fs_add_orphan_inode(inode);
763 f2fs_alloc_nid_done(sbi, inode->i_ino);
766 f2fs_i_links_write(inode, false);
769 d_tmpfile(dentry, inode);
771 /* link_count was changed by d_tmpfile as well. */
773 unlock_new_inode(inode);
775 f2fs_balance_fs(sbi, true);
779 f2fs_release_orphan_inode(sbi);
781 f2fs_handle_failed_inode(inode);
785 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
787 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
789 if (unlikely(f2fs_cp_error(sbi)))
792 if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
793 int err = fscrypt_get_encryption_info(dir);
798 return __f2fs_tmpfile(dir, dentry, mode, NULL);
801 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
803 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
806 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
809 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
810 struct inode *new_dir, struct dentry *new_dentry,
813 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
814 struct inode *old_inode = d_inode(old_dentry);
815 struct inode *new_inode = d_inode(new_dentry);
816 struct inode *whiteout = NULL;
817 struct page *old_dir_page;
818 struct page *old_page, *new_page = NULL;
819 struct f2fs_dir_entry *old_dir_entry = NULL;
820 struct f2fs_dir_entry *old_entry;
821 struct f2fs_dir_entry *new_entry;
822 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
825 if (unlikely(f2fs_cp_error(sbi)))
828 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
829 (!projid_eq(F2FS_I(new_dir)->i_projid,
830 F2FS_I(old_dentry->d_inode)->i_projid)))
833 err = dquot_initialize(old_dir);
837 err = dquot_initialize(new_dir);
842 err = dquot_initialize(new_inode);
847 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
849 if (IS_ERR(old_page))
850 err = PTR_ERR(old_page);
854 if (S_ISDIR(old_inode->i_mode)) {
855 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
856 if (!old_dir_entry) {
857 if (IS_ERR(old_dir_page))
858 err = PTR_ERR(old_dir_page);
863 if (flags & RENAME_WHITEOUT) {
864 err = f2fs_create_whiteout(old_dir, &whiteout);
872 if (old_dir_entry && !f2fs_empty_dir(new_inode))
876 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
879 if (IS_ERR(new_page))
880 err = PTR_ERR(new_page);
884 f2fs_balance_fs(sbi, true);
888 err = f2fs_acquire_orphan_inode(sbi);
892 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
894 new_inode->i_ctime = current_time(new_inode);
895 down_write(&F2FS_I(new_inode)->i_sem);
897 f2fs_i_links_write(new_inode, false);
898 f2fs_i_links_write(new_inode, false);
899 up_write(&F2FS_I(new_inode)->i_sem);
901 if (!new_inode->i_nlink)
902 f2fs_add_orphan_inode(new_inode);
904 f2fs_release_orphan_inode(sbi);
906 f2fs_balance_fs(sbi, true);
910 err = f2fs_add_link(new_dentry, old_inode);
917 f2fs_i_links_write(new_dir, true);
920 * old entry and new entry can locate in the same inline
921 * dentry in inode, when attaching new entry in inline dentry,
922 * it could force inline dentry conversion, after that,
923 * old_entry and old_page will point to wrong address, in
924 * order to avoid this, let's do the check and update here.
926 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
927 f2fs_put_page(old_page, 0);
930 old_entry = f2fs_find_entry(old_dir,
931 &old_dentry->d_name, &old_page);
934 if (IS_ERR(old_page))
935 err = PTR_ERR(old_page);
942 down_write(&F2FS_I(old_inode)->i_sem);
943 if (!old_dir_entry || whiteout)
944 file_lost_pino(old_inode);
946 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
947 up_write(&F2FS_I(old_inode)->i_sem);
949 old_inode->i_ctime = current_time(old_inode);
950 f2fs_mark_inode_dirty_sync(old_inode, false);
952 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
955 whiteout->i_state |= I_LINKABLE;
956 set_inode_flag(whiteout, FI_INC_LINK);
957 err = f2fs_add_link(old_dentry, whiteout);
960 whiteout->i_state &= ~I_LINKABLE;
965 if (old_dir != new_dir && !whiteout)
966 f2fs_set_link(old_inode, old_dir_entry,
967 old_dir_page, new_dir);
969 f2fs_put_page(old_dir_page, 0);
970 f2fs_i_links_write(old_dir, false);
972 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
973 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
974 if (S_ISDIR(old_inode->i_mode))
975 f2fs_add_ino_entry(sbi, old_inode->i_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 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1129 f2fs_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_follow_link(struct dentry *dentry, void **cookie)
1180 struct inode *inode = d_inode(dentry);
1185 return ERR_PTR(-ECHILD);
1187 page = read_mapping_page(inode->i_mapping, 0, NULL);
1189 return ERR_CAST(page);
1191 target = fscrypt_get_symlink(inode, page_address(page),
1192 inode->i_sb->s_blocksize);
1194 return *cookie = target;
1197 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1198 .readlink = generic_readlink,
1199 .follow_link = f2fs_encrypted_follow_link,
1200 .put_link = kfree_put_link,
1201 .getattr = f2fs_getattr,
1202 .setattr = f2fs_setattr,
1203 #ifdef CONFIG_F2FS_FS_XATTR
1204 .setxattr = generic_setxattr,
1205 .getxattr = generic_getxattr,
1206 .listxattr = f2fs_listxattr,
1207 .removexattr = generic_removexattr,
1211 const struct inode_operations f2fs_dir_inode_operations = {
1212 .create = f2fs_create,
1213 .lookup = f2fs_lookup,
1215 .unlink = f2fs_unlink,
1216 .symlink = f2fs_symlink,
1217 .mkdir = f2fs_mkdir,
1218 .rmdir = f2fs_rmdir,
1219 .mknod = f2fs_mknod,
1220 .rename2 = f2fs_rename2,
1221 .tmpfile = f2fs_tmpfile,
1222 .getattr = f2fs_getattr,
1223 .setattr = f2fs_setattr,
1224 .get_acl = f2fs_get_acl,
1225 .set_acl = f2fs_set_acl,
1226 #ifdef CONFIG_F2FS_FS_XATTR
1227 .setxattr = generic_setxattr,
1228 .getxattr = generic_getxattr,
1229 .listxattr = f2fs_listxattr,
1230 .removexattr = generic_removexattr,
1234 const struct inode_operations f2fs_symlink_inode_operations = {
1235 .readlink = generic_readlink,
1236 .follow_link = f2fs_follow_link,
1237 .put_link = page_put_link,
1238 .getattr = f2fs_getattr,
1239 .setattr = f2fs_setattr,
1240 #ifdef CONFIG_F2FS_FS_XATTR
1241 .setxattr = generic_setxattr,
1242 .getxattr = generic_getxattr,
1243 .listxattr = f2fs_listxattr,
1244 .removexattr = generic_removexattr,
1248 const struct inode_operations f2fs_special_inode_operations = {
1249 .getattr = f2fs_getattr,
1250 .setattr = f2fs_setattr,
1251 .get_acl = f2fs_get_acl,
1252 .set_acl = f2fs_set_acl,
1253 #ifdef CONFIG_F2FS_FS_XATTR
1254 .setxattr = generic_setxattr,
1255 .getxattr = generic_getxattr,
1256 .listxattr = f2fs_listxattr,
1257 .removexattr = generic_removexattr,