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
) && f2fs_may_encrypt(inode
))
82 f2fs_set_encrypted_inode(inode
);
84 if (f2fs_sb_has_extra_attr(sbi
->sb
)) {
85 set_inode_flag(inode
, FI_EXTRA_ATTR
);
86 F2FS_I(inode
)->i_extra_isize
= F2FS_TOTAL_EXTRA_ATTR_SIZE
;
89 if (test_opt(sbi
, INLINE_XATTR
))
90 set_inode_flag(inode
, FI_INLINE_XATTR
);
92 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
93 set_inode_flag(inode
, FI_INLINE_DATA
);
94 if (f2fs_may_inline_dentry(inode
))
95 set_inode_flag(inode
, FI_INLINE_DENTRY
);
97 if (f2fs_sb_has_flexible_inline_xattr(sbi
->sb
)) {
98 f2fs_bug_on(sbi
, !f2fs_has_extra_attr(inode
));
99 if (f2fs_has_inline_xattr(inode
))
100 xattr_size
= sbi
->inline_xattr_size
;
101 /* Otherwise, will be 0 */
102 } else if (f2fs_has_inline_xattr(inode
) ||
103 f2fs_has_inline_dentry(inode
)) {
104 xattr_size
= DEFAULT_INLINE_XATTR_ADDRS
;
106 F2FS_I(inode
)->i_inline_xattr_size
= xattr_size
;
108 f2fs_init_extent_tree(inode
, NULL
);
110 stat_inc_inline_xattr(inode
);
111 stat_inc_inline_inode(inode
);
112 stat_inc_inline_dir(inode
);
114 F2FS_I(inode
)->i_flags
=
115 f2fs_mask_flags(mode
, F2FS_I(dir
)->i_flags
& F2FS_FL_INHERITED
);
117 if (S_ISDIR(inode
->i_mode
))
118 F2FS_I(inode
)->i_flags
|= FS_INDEX_FL
;
120 if (F2FS_I(inode
)->i_flags
& FS_PROJINHERIT_FL
)
121 set_inode_flag(inode
, FI_PROJ_INHERIT
);
123 trace_f2fs_new_inode(inode
, 0);
127 trace_f2fs_new_inode(inode
, err
);
128 make_bad_inode(inode
);
130 set_inode_flag(inode
, FI_FREE_NID
);
134 trace_f2fs_new_inode(inode
, err
);
136 inode
->i_flags
|= S_NOQUOTA
;
138 set_inode_flag(inode
, FI_FREE_NID
);
140 unlock_new_inode(inode
);
145 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
147 size_t slen
= strlen(s
);
148 size_t sublen
= strlen(sub
);
152 * filename format of multimedia file should be defined as:
153 * "filename + '.' + extension + (optional: '.' + temp extension)".
155 if (slen
< sublen
+ 2)
158 for (i
= 1; i
< slen
- sublen
; i
++) {
161 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
169 * Set multimedia files as cold files for hot/cold data separation
171 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
172 const unsigned char *name
)
175 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
177 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
178 for (i
= 0; i
< count
; i
++) {
179 if (is_multimedia_file(name
, extlist
[i
])) {
180 file_set_cold(inode
);
186 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
189 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
194 if (unlikely(f2fs_cp_error(sbi
)))
197 err
= dquot_initialize(dir
);
201 inode
= f2fs_new_inode(dir
, mode
);
203 return PTR_ERR(inode
);
205 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
206 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
208 inode
->i_op
= &f2fs_file_inode_operations
;
209 inode
->i_fop
= &f2fs_file_operations
;
210 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
214 err
= f2fs_add_link(dentry
, inode
);
219 alloc_nid_done(sbi
, ino
);
221 d_instantiate(dentry
, inode
);
222 unlock_new_inode(inode
);
225 f2fs_sync_fs(sbi
->sb
, 1);
227 f2fs_balance_fs(sbi
, true);
230 handle_failed_inode(inode
);
234 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
235 struct dentry
*dentry
)
237 struct inode
*inode
= d_inode(old_dentry
);
238 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
241 if (unlikely(f2fs_cp_error(sbi
)))
244 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
248 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
249 (!projid_eq(F2FS_I(dir
)->i_projid
,
250 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
253 err
= dquot_initialize(dir
);
257 f2fs_balance_fs(sbi
, true);
259 inode
->i_ctime
= current_time(inode
);
262 set_inode_flag(inode
, FI_INC_LINK
);
264 err
= f2fs_add_link(dentry
, inode
);
269 d_instantiate(dentry
, inode
);
272 f2fs_sync_fs(sbi
->sb
, 1);
275 clear_inode_flag(inode
, FI_INC_LINK
);
281 struct dentry
*f2fs_get_parent(struct dentry
*child
)
283 struct qstr dotdot
= QSTR_INIT("..", 2);
285 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
288 return ERR_CAST(page
);
289 return ERR_PTR(-ENOENT
);
291 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
294 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
296 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
297 struct qstr dot
= QSTR_INIT(".", 1);
298 struct qstr dotdot
= QSTR_INIT("..", 2);
299 struct f2fs_dir_entry
*de
;
303 if (f2fs_readonly(sbi
->sb
)) {
304 f2fs_msg(sbi
->sb
, KERN_INFO
,
305 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
306 "in readonly mountpoint", dir
->i_ino
, pino
);
310 err
= dquot_initialize(dir
);
314 f2fs_balance_fs(sbi
, true);
318 de
= f2fs_find_entry(dir
, &dot
, &page
);
320 f2fs_dentry_kunmap(dir
, page
);
321 f2fs_put_page(page
, 0);
322 } else if (IS_ERR(page
)) {
326 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
331 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
333 f2fs_dentry_kunmap(dir
, page
);
334 f2fs_put_page(page
, 0);
335 } else if (IS_ERR(page
)) {
338 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
342 clear_inode_flag(dir
, FI_INLINE_DOTS
);
348 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
351 struct inode
*inode
= NULL
;
352 struct f2fs_dir_entry
*de
;
357 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
359 trace_f2fs_lookup_start(dir
, dentry
, flags
);
361 err
= fscrypt_prepare_lookup(dir
, dentry
, flags
);
365 if (dentry
->d_name
.len
> F2FS_NAME_LEN
) {
370 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
379 ino
= le32_to_cpu(de
->ino
);
380 f2fs_dentry_kunmap(dir
, page
);
381 f2fs_put_page(page
, 0);
383 inode
= f2fs_iget(dir
->i_sb
, ino
);
385 err
= PTR_ERR(inode
);
389 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
390 err
= __recover_dot_dentries(dir
, root_ino
);
395 if (f2fs_has_inline_dots(inode
)) {
396 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
400 if (f2fs_encrypted_inode(dir
) &&
401 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
402 !fscrypt_has_permitted_context(dir
, inode
)) {
403 f2fs_msg(inode
->i_sb
, KERN_WARNING
,
404 "Inconsistent encryption contexts: %lu/%lu",
405 dir
->i_ino
, inode
->i_ino
);
410 new = d_splice_alias(inode
, dentry
);
413 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
418 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
422 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
424 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
425 struct inode
*inode
= d_inode(dentry
);
426 struct f2fs_dir_entry
*de
;
430 trace_f2fs_unlink_enter(dir
, dentry
);
432 if (unlikely(f2fs_cp_error(sbi
)))
435 err
= dquot_initialize(dir
);
438 err
= dquot_initialize(inode
);
442 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
449 f2fs_balance_fs(sbi
, true);
452 err
= acquire_orphan_inode(sbi
);
455 f2fs_dentry_kunmap(dir
, page
);
456 f2fs_put_page(page
, 0);
459 f2fs_delete_entry(de
, page
, dir
, inode
);
463 f2fs_sync_fs(sbi
->sb
, 1);
465 trace_f2fs_unlink_exit(inode
, err
);
469 static const char *f2fs_get_link(struct dentry
*dentry
,
471 struct delayed_call
*done
)
473 const char *link
= page_get_link(dentry
, inode
, done
);
474 if (!IS_ERR(link
) && !*link
) {
475 /* this is broken symlink case */
476 do_delayed_call(done
);
477 clear_delayed_call(done
);
478 link
= ERR_PTR(-ENOENT
);
483 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
486 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
488 size_t len
= strlen(symname
);
489 struct fscrypt_str disk_link
;
492 if (unlikely(f2fs_cp_error(sbi
)))
495 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
500 err
= dquot_initialize(dir
);
504 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
506 return PTR_ERR(inode
);
508 if (IS_ENCRYPTED(inode
))
509 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
511 inode
->i_op
= &f2fs_symlink_inode_operations
;
512 inode_nohighmem(inode
);
513 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
516 err
= f2fs_add_link(dentry
, inode
);
518 goto out_handle_failed_inode
;
520 alloc_nid_done(sbi
, inode
->i_ino
);
522 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
526 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
529 d_instantiate(dentry
, inode
);
530 unlock_new_inode(inode
);
533 * Let's flush symlink data in order to avoid broken symlink as much as
534 * possible. Nevertheless, fsyncing is the best way, but there is no
535 * way to get a file descriptor in order to flush that.
537 * Note that, it needs to do dir->fsync to make this recoverable.
538 * If the symlink path is stored into inline_data, there is no
539 * performance regression.
542 filemap_write_and_wait_range(inode
->i_mapping
, 0,
546 f2fs_sync_fs(sbi
->sb
, 1);
548 f2fs_unlink(dir
, dentry
);
551 f2fs_balance_fs(sbi
, true);
552 goto out_free_encrypted_link
;
554 out_handle_failed_inode
:
555 handle_failed_inode(inode
);
556 out_free_encrypted_link
:
557 if (disk_link
.name
!= (unsigned char *)symname
)
558 kfree(disk_link
.name
);
562 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
564 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
568 if (unlikely(f2fs_cp_error(sbi
)))
571 err
= dquot_initialize(dir
);
575 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
577 return PTR_ERR(inode
);
579 inode
->i_op
= &f2fs_dir_inode_operations
;
580 inode
->i_fop
= &f2fs_dir_operations
;
581 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
582 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
584 set_inode_flag(inode
, FI_INC_LINK
);
586 err
= f2fs_add_link(dentry
, inode
);
591 alloc_nid_done(sbi
, inode
->i_ino
);
593 d_instantiate(dentry
, inode
);
594 unlock_new_inode(inode
);
597 f2fs_sync_fs(sbi
->sb
, 1);
599 f2fs_balance_fs(sbi
, true);
603 clear_inode_flag(inode
, FI_INC_LINK
);
604 handle_failed_inode(inode
);
608 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
610 struct inode
*inode
= d_inode(dentry
);
611 if (f2fs_empty_dir(inode
))
612 return f2fs_unlink(dir
, dentry
);
616 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
617 umode_t mode
, dev_t rdev
)
619 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
623 if (unlikely(f2fs_cp_error(sbi
)))
626 err
= dquot_initialize(dir
);
630 inode
= f2fs_new_inode(dir
, mode
);
632 return PTR_ERR(inode
);
634 init_special_inode(inode
, inode
->i_mode
, rdev
);
635 inode
->i_op
= &f2fs_special_inode_operations
;
638 err
= f2fs_add_link(dentry
, inode
);
643 alloc_nid_done(sbi
, inode
->i_ino
);
645 d_instantiate(dentry
, inode
);
646 unlock_new_inode(inode
);
649 f2fs_sync_fs(sbi
->sb
, 1);
651 f2fs_balance_fs(sbi
, true);
654 handle_failed_inode(inode
);
658 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
659 umode_t mode
, struct inode
**whiteout
)
661 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
665 err
= dquot_initialize(dir
);
669 inode
= f2fs_new_inode(dir
, mode
);
671 return PTR_ERR(inode
);
674 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
675 inode
->i_op
= &f2fs_special_inode_operations
;
677 inode
->i_op
= &f2fs_file_inode_operations
;
678 inode
->i_fop
= &f2fs_file_operations
;
679 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
683 err
= acquire_orphan_inode(sbi
);
687 err
= f2fs_do_tmpfile(inode
, dir
);
692 * add this non-linked tmpfile to orphan list, in this way we could
693 * remove all unused data of tmpfile after abnormal power-off.
695 add_orphan_inode(inode
);
696 alloc_nid_done(sbi
, inode
->i_ino
);
699 f2fs_i_links_write(inode
, false);
702 d_tmpfile(dentry
, inode
);
704 /* link_count was changed by d_tmpfile as well. */
706 unlock_new_inode(inode
);
708 f2fs_balance_fs(sbi
, true);
712 release_orphan_inode(sbi
);
714 handle_failed_inode(inode
);
718 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
720 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
723 if (f2fs_encrypted_inode(dir
)) {
724 int err
= fscrypt_get_encryption_info(dir
);
729 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
732 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
734 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
737 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
740 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
741 struct inode
*new_dir
, struct dentry
*new_dentry
,
744 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
745 struct inode
*old_inode
= d_inode(old_dentry
);
746 struct inode
*new_inode
= d_inode(new_dentry
);
747 struct inode
*whiteout
= NULL
;
748 struct page
*old_dir_page
;
749 struct page
*old_page
, *new_page
= NULL
;
750 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
751 struct f2fs_dir_entry
*old_entry
;
752 struct f2fs_dir_entry
*new_entry
;
753 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
756 if (unlikely(f2fs_cp_error(sbi
)))
759 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
760 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
761 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
764 err
= dquot_initialize(old_dir
);
768 err
= dquot_initialize(new_dir
);
773 err
= dquot_initialize(new_inode
);
778 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
780 if (IS_ERR(old_page
))
781 err
= PTR_ERR(old_page
);
785 if (S_ISDIR(old_inode
->i_mode
)) {
786 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
787 if (!old_dir_entry
) {
788 if (IS_ERR(old_dir_page
))
789 err
= PTR_ERR(old_dir_page
);
794 if (flags
& RENAME_WHITEOUT
) {
795 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
803 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
807 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
810 if (IS_ERR(new_page
))
811 err
= PTR_ERR(new_page
);
815 f2fs_balance_fs(sbi
, true);
819 err
= acquire_orphan_inode(sbi
);
823 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
825 new_inode
->i_ctime
= current_time(new_inode
);
826 down_write(&F2FS_I(new_inode
)->i_sem
);
828 f2fs_i_links_write(new_inode
, false);
829 f2fs_i_links_write(new_inode
, false);
830 up_write(&F2FS_I(new_inode
)->i_sem
);
832 if (!new_inode
->i_nlink
)
833 add_orphan_inode(new_inode
);
835 release_orphan_inode(sbi
);
837 f2fs_balance_fs(sbi
, true);
841 err
= f2fs_add_link(new_dentry
, old_inode
);
848 f2fs_i_links_write(new_dir
, true);
851 * old entry and new entry can locate in the same inline
852 * dentry in inode, when attaching new entry in inline dentry,
853 * it could force inline dentry conversion, after that,
854 * old_entry and old_page will point to wrong address, in
855 * order to avoid this, let's do the check and update here.
857 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
858 f2fs_put_page(old_page
, 0);
861 old_entry
= f2fs_find_entry(old_dir
,
862 &old_dentry
->d_name
, &old_page
);
865 if (IS_ERR(old_page
))
866 err
= PTR_ERR(old_page
);
873 down_write(&F2FS_I(old_inode
)->i_sem
);
874 if (!old_dir_entry
|| whiteout
)
875 file_lost_pino(old_inode
);
877 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
878 up_write(&F2FS_I(old_inode
)->i_sem
);
880 old_inode
->i_ctime
= current_time(old_inode
);
881 f2fs_mark_inode_dirty_sync(old_inode
, false);
883 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
886 whiteout
->i_state
|= I_LINKABLE
;
887 set_inode_flag(whiteout
, FI_INC_LINK
);
888 err
= f2fs_add_link(old_dentry
, whiteout
);
891 whiteout
->i_state
&= ~I_LINKABLE
;
896 if (old_dir
!= new_dir
&& !whiteout
) {
897 f2fs_set_link(old_inode
, old_dir_entry
,
898 old_dir_page
, new_dir
);
900 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
901 f2fs_put_page(old_dir_page
, 0);
903 f2fs_i_links_write(old_dir
, false);
905 add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
909 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
910 f2fs_sync_fs(sbi
->sb
, 1);
916 f2fs_dentry_kunmap(new_dir
, new_page
);
917 f2fs_put_page(new_page
, 0);
924 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
925 f2fs_put_page(old_dir_page
, 0);
928 f2fs_dentry_kunmap(old_dir
, old_page
);
929 f2fs_put_page(old_page
, 0);
934 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
935 struct inode
*new_dir
, struct dentry
*new_dentry
)
937 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
938 struct inode
*old_inode
= d_inode(old_dentry
);
939 struct inode
*new_inode
= d_inode(new_dentry
);
940 struct page
*old_dir_page
, *new_dir_page
;
941 struct page
*old_page
, *new_page
;
942 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
943 struct f2fs_dir_entry
*old_entry
, *new_entry
;
944 int old_nlink
= 0, new_nlink
= 0;
947 if (unlikely(f2fs_cp_error(sbi
)))
950 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
951 !projid_eq(F2FS_I(new_dir
)->i_projid
,
952 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
953 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
954 !projid_eq(F2FS_I(old_dir
)->i_projid
,
955 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
958 err
= dquot_initialize(old_dir
);
962 err
= dquot_initialize(new_dir
);
966 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
968 if (IS_ERR(old_page
))
969 err
= PTR_ERR(old_page
);
973 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
975 if (IS_ERR(new_page
))
976 err
= PTR_ERR(new_page
);
980 /* prepare for updating ".." directory entry info later */
981 if (old_dir
!= new_dir
) {
982 if (S_ISDIR(old_inode
->i_mode
)) {
983 old_dir_entry
= f2fs_parent_dir(old_inode
,
985 if (!old_dir_entry
) {
986 if (IS_ERR(old_dir_page
))
987 err
= PTR_ERR(old_dir_page
);
992 if (S_ISDIR(new_inode
->i_mode
)) {
993 new_dir_entry
= f2fs_parent_dir(new_inode
,
995 if (!new_dir_entry
) {
996 if (IS_ERR(new_dir_page
))
997 err
= PTR_ERR(new_dir_page
);
1004 * If cross rename between file and directory those are not
1005 * in the same directory, we will inc nlink of file's parent
1006 * later, so we should check upper boundary of its nlink.
1008 if ((!old_dir_entry
|| !new_dir_entry
) &&
1009 old_dir_entry
!= new_dir_entry
) {
1010 old_nlink
= old_dir_entry
? -1 : 1;
1011 new_nlink
= -old_nlink
;
1013 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1014 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1018 f2fs_balance_fs(sbi
, true);
1022 /* update ".." directory entry info of old dentry */
1024 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1026 /* update ".." directory entry info of new dentry */
1028 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1030 /* update directory entry info of old dir inode */
1031 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1033 down_write(&F2FS_I(old_inode
)->i_sem
);
1034 file_lost_pino(old_inode
);
1035 up_write(&F2FS_I(old_inode
)->i_sem
);
1037 old_dir
->i_ctime
= current_time(old_dir
);
1039 down_write(&F2FS_I(old_dir
)->i_sem
);
1040 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1041 up_write(&F2FS_I(old_dir
)->i_sem
);
1043 f2fs_mark_inode_dirty_sync(old_dir
, false);
1045 /* update directory entry info of new dir inode */
1046 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1048 down_write(&F2FS_I(new_inode
)->i_sem
);
1049 file_lost_pino(new_inode
);
1050 up_write(&F2FS_I(new_inode
)->i_sem
);
1052 new_dir
->i_ctime
= current_time(new_dir
);
1054 down_write(&F2FS_I(new_dir
)->i_sem
);
1055 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1056 up_write(&F2FS_I(new_dir
)->i_sem
);
1058 f2fs_mark_inode_dirty_sync(new_dir
, false);
1060 add_ino_entry(sbi
, old_dir
->i_ino
, TRANS_DIR_INO
);
1061 add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1063 f2fs_unlock_op(sbi
);
1065 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1066 f2fs_sync_fs(sbi
->sb
, 1);
1069 if (new_dir_entry
) {
1070 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
1071 f2fs_put_page(new_dir_page
, 0);
1074 if (old_dir_entry
) {
1075 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
1076 f2fs_put_page(old_dir_page
, 0);
1079 f2fs_dentry_kunmap(new_dir
, new_page
);
1080 f2fs_put_page(new_page
, 0);
1082 f2fs_dentry_kunmap(old_dir
, old_page
);
1083 f2fs_put_page(old_page
, 0);
1088 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1089 struct inode
*new_dir
, struct dentry
*new_dentry
,
1094 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1097 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
1102 if (flags
& RENAME_EXCHANGE
) {
1103 return f2fs_cross_rename(old_dir
, old_dentry
,
1104 new_dir
, new_dentry
);
1107 * VFS has already handled the new dentry existence case,
1108 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1110 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1113 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1114 struct inode
*inode
,
1115 struct delayed_call
*done
)
1121 return ERR_PTR(-ECHILD
);
1123 page
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1125 return ERR_CAST(page
);
1127 target
= fscrypt_get_symlink(inode
, page_address(page
),
1128 inode
->i_sb
->s_blocksize
, done
);
1133 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1134 .get_link
= f2fs_encrypted_get_link
,
1135 .getattr
= f2fs_getattr
,
1136 .setattr
= f2fs_setattr
,
1137 #ifdef CONFIG_F2FS_FS_XATTR
1138 .listxattr
= f2fs_listxattr
,
1142 const struct inode_operations f2fs_dir_inode_operations
= {
1143 .create
= f2fs_create
,
1144 .lookup
= f2fs_lookup
,
1146 .unlink
= f2fs_unlink
,
1147 .symlink
= f2fs_symlink
,
1148 .mkdir
= f2fs_mkdir
,
1149 .rmdir
= f2fs_rmdir
,
1150 .mknod
= f2fs_mknod
,
1151 .rename
= f2fs_rename2
,
1152 .tmpfile
= f2fs_tmpfile
,
1153 .getattr
= f2fs_getattr
,
1154 .setattr
= f2fs_setattr
,
1155 .get_acl
= f2fs_get_acl
,
1156 .set_acl
= f2fs_set_acl
,
1157 #ifdef CONFIG_F2FS_FS_XATTR
1158 .listxattr
= f2fs_listxattr
,
1162 const struct inode_operations f2fs_symlink_inode_operations
= {
1163 .get_link
= f2fs_get_link
,
1164 .getattr
= f2fs_getattr
,
1165 .setattr
= f2fs_setattr
,
1166 #ifdef CONFIG_F2FS_FS_XATTR
1167 .listxattr
= f2fs_listxattr
,
1171 const struct inode_operations f2fs_special_inode_operations
= {
1172 .getattr
= f2fs_getattr
,
1173 .setattr
= f2fs_setattr
,
1174 .get_acl
= f2fs_get_acl
,
1175 .set_acl
= f2fs_set_acl
,
1176 #ifdef CONFIG_F2FS_FS_XATTR
1177 .listxattr
= f2fs_listxattr
,