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
= current_time(inode
);
54 inode
->i_generation
= sbi
->s_next_generation
++;
56 err
= insert_inode_locked(inode
);
62 if (f2fs_sb_has_project_quota(sbi
->sb
) &&
63 (F2FS_I(dir
)->i_flags
& FS_PROJINHERIT_FL
))
64 F2FS_I(inode
)->i_projid
= F2FS_I(dir
)->i_projid
;
66 F2FS_I(inode
)->i_projid
= make_kprojid(&init_user_ns
,
69 err
= dquot_initialize(inode
);
73 err
= dquot_alloc_inode(inode
);
77 /* If the directory encrypted, then we should encrypt the inode. */
78 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
))
79 f2fs_set_encrypted_inode(inode
);
81 set_inode_flag(inode
, FI_NEW_INODE
);
83 if (f2fs_sb_has_extra_attr(sbi
->sb
)) {
84 set_inode_flag(inode
, FI_EXTRA_ATTR
);
85 F2FS_I(inode
)->i_extra_isize
= F2FS_TOTAL_EXTRA_ATTR_SIZE
;
88 if (test_opt(sbi
, INLINE_XATTR
))
89 set_inode_flag(inode
, FI_INLINE_XATTR
);
91 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
92 set_inode_flag(inode
, FI_INLINE_DATA
);
93 if (f2fs_may_inline_dentry(inode
))
94 set_inode_flag(inode
, FI_INLINE_DENTRY
);
96 if (f2fs_sb_has_flexible_inline_xattr(sbi
->sb
)) {
97 f2fs_bug_on(sbi
, !f2fs_has_extra_attr(inode
));
98 if (f2fs_has_inline_xattr(inode
))
99 xattr_size
= sbi
->inline_xattr_size
;
100 /* Otherwise, will be 0 */
101 } else if (f2fs_has_inline_xattr(inode
) ||
102 f2fs_has_inline_dentry(inode
)) {
103 xattr_size
= DEFAULT_INLINE_XATTR_ADDRS
;
105 F2FS_I(inode
)->i_inline_xattr_size
= xattr_size
;
107 f2fs_init_extent_tree(inode
, NULL
);
109 stat_inc_inline_xattr(inode
);
110 stat_inc_inline_inode(inode
);
111 stat_inc_inline_dir(inode
);
113 F2FS_I(inode
)->i_flags
=
114 f2fs_mask_flags(mode
, F2FS_I(dir
)->i_flags
& F2FS_FL_INHERITED
);
116 if (S_ISDIR(inode
->i_mode
))
117 F2FS_I(inode
)->i_flags
|= FS_INDEX_FL
;
119 if (F2FS_I(inode
)->i_flags
& FS_PROJINHERIT_FL
)
120 set_inode_flag(inode
, FI_PROJ_INHERIT
);
122 trace_f2fs_new_inode(inode
, 0);
126 trace_f2fs_new_inode(inode
, err
);
127 make_bad_inode(inode
);
129 set_inode_flag(inode
, FI_FREE_NID
);
133 trace_f2fs_new_inode(inode
, err
);
135 inode
->i_flags
|= S_NOQUOTA
;
137 set_inode_flag(inode
, FI_FREE_NID
);
139 unlock_new_inode(inode
);
144 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
146 size_t slen
= strlen(s
);
147 size_t sublen
= strlen(sub
);
151 * filename format of multimedia file should be defined as:
152 * "filename + '.' + extension + (optional: '.' + temp extension)".
154 if (slen
< sublen
+ 2)
157 for (i
= 1; i
< slen
- sublen
; i
++) {
160 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
168 * Set multimedia files as cold files for hot/cold data separation
170 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
171 const unsigned char *name
)
174 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
176 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
177 for (i
= 0; i
< count
; i
++) {
178 if (is_multimedia_file(name
, extlist
[i
])) {
179 file_set_cold(inode
);
185 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
188 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
193 if (unlikely(f2fs_cp_error(sbi
)))
196 err
= dquot_initialize(dir
);
200 inode
= f2fs_new_inode(dir
, mode
);
202 return PTR_ERR(inode
);
204 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
205 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
207 inode
->i_op
= &f2fs_file_inode_operations
;
208 inode
->i_fop
= &f2fs_file_operations
;
209 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
213 err
= f2fs_add_link(dentry
, inode
);
218 alloc_nid_done(sbi
, ino
);
220 d_instantiate(dentry
, inode
);
221 unlock_new_inode(inode
);
224 f2fs_sync_fs(sbi
->sb
, 1);
226 f2fs_balance_fs(sbi
, true);
229 handle_failed_inode(inode
);
233 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
234 struct dentry
*dentry
)
236 struct inode
*inode
= d_inode(old_dentry
);
237 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
240 if (unlikely(f2fs_cp_error(sbi
)))
243 if (f2fs_encrypted_inode(dir
) &&
244 !fscrypt_has_permitted_context(dir
, inode
))
247 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
248 (!projid_eq(F2FS_I(dir
)->i_projid
,
249 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
252 err
= dquot_initialize(dir
);
256 f2fs_balance_fs(sbi
, true);
258 inode
->i_ctime
= current_time(inode
);
261 set_inode_flag(inode
, FI_INC_LINK
);
263 err
= f2fs_add_link(dentry
, inode
);
268 d_instantiate(dentry
, inode
);
271 f2fs_sync_fs(sbi
->sb
, 1);
274 clear_inode_flag(inode
, FI_INC_LINK
);
280 struct dentry
*f2fs_get_parent(struct dentry
*child
)
282 struct qstr dotdot
= QSTR_INIT("..", 2);
284 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
287 return ERR_CAST(page
);
288 return ERR_PTR(-ENOENT
);
290 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
293 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
295 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
296 struct qstr dot
= QSTR_INIT(".", 1);
297 struct qstr dotdot
= QSTR_INIT("..", 2);
298 struct f2fs_dir_entry
*de
;
302 if (f2fs_readonly(sbi
->sb
)) {
303 f2fs_msg(sbi
->sb
, KERN_INFO
,
304 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
305 "in readonly mountpoint", dir
->i_ino
, pino
);
309 err
= dquot_initialize(dir
);
313 f2fs_balance_fs(sbi
, true);
317 de
= f2fs_find_entry(dir
, &dot
, &page
);
319 f2fs_dentry_kunmap(dir
, page
);
320 f2fs_put_page(page
, 0);
321 } else if (IS_ERR(page
)) {
325 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
330 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
332 f2fs_dentry_kunmap(dir
, page
);
333 f2fs_put_page(page
, 0);
334 } else if (IS_ERR(page
)) {
337 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
341 clear_inode_flag(dir
, FI_INLINE_DOTS
);
347 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
350 struct inode
*inode
= NULL
;
351 struct f2fs_dir_entry
*de
;
356 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
358 trace_f2fs_lookup_start(dir
, dentry
, flags
);
360 if (f2fs_encrypted_inode(dir
)) {
361 err
= fscrypt_get_encryption_info(dir
);
364 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
365 * created while the directory was encrypted and we
366 * don't have access to the key.
368 if (fscrypt_has_encryption_key(dir
))
369 fscrypt_set_encrypted_dentry(dentry
);
370 fscrypt_set_d_op(dentry
);
371 if (err
&& err
!= -ENOKEY
)
375 if (dentry
->d_name
.len
> F2FS_NAME_LEN
) {
380 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
389 ino
= le32_to_cpu(de
->ino
);
390 f2fs_dentry_kunmap(dir
, page
);
391 f2fs_put_page(page
, 0);
393 inode
= f2fs_iget(dir
->i_sb
, ino
);
395 err
= PTR_ERR(inode
);
399 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
400 err
= __recover_dot_dentries(dir
, root_ino
);
405 if (f2fs_has_inline_dots(inode
)) {
406 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
410 if (f2fs_encrypted_inode(dir
) &&
411 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
412 !fscrypt_has_permitted_context(dir
, inode
)) {
413 f2fs_msg(inode
->i_sb
, KERN_WARNING
,
414 "Inconsistent encryption contexts: %lu/%lu",
415 dir
->i_ino
, inode
->i_ino
);
420 new = d_splice_alias(inode
, dentry
);
423 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
428 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
432 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
434 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
435 struct inode
*inode
= d_inode(dentry
);
436 struct f2fs_dir_entry
*de
;
440 trace_f2fs_unlink_enter(dir
, dentry
);
442 if (unlikely(f2fs_cp_error(sbi
)))
445 err
= dquot_initialize(dir
);
448 err
= dquot_initialize(inode
);
452 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
459 f2fs_balance_fs(sbi
, true);
462 err
= acquire_orphan_inode(sbi
);
465 f2fs_dentry_kunmap(dir
, page
);
466 f2fs_put_page(page
, 0);
469 f2fs_delete_entry(de
, page
, dir
, inode
);
473 f2fs_sync_fs(sbi
->sb
, 1);
475 trace_f2fs_unlink_exit(inode
, err
);
479 static const char *f2fs_follow_link(struct dentry
*dentry
, void **cookie
)
481 const char *link
= page_follow_link_light(dentry
, cookie
);
482 if (!IS_ERR(link
) && !*link
) {
483 /* this is broken symlink case */
484 page_put_link(NULL
, *cookie
);
485 link
= ERR_PTR(-ENOENT
);
490 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
493 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
495 size_t len
= strlen(symname
);
496 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
497 struct fscrypt_symlink_data
*sd
= NULL
;
500 if (unlikely(f2fs_cp_error(sbi
)))
503 if (f2fs_encrypted_inode(dir
)) {
504 err
= fscrypt_get_encryption_info(dir
);
508 if (!fscrypt_has_encryption_key(dir
))
511 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
512 sizeof(struct fscrypt_symlink_data
));
515 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
516 return -ENAMETOOLONG
;
518 err
= dquot_initialize(dir
);
522 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
524 return PTR_ERR(inode
);
526 if (f2fs_encrypted_inode(inode
))
527 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
529 inode
->i_op
= &f2fs_symlink_inode_operations
;
530 inode_nohighmem(inode
);
531 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
534 err
= f2fs_add_link(dentry
, inode
);
538 alloc_nid_done(sbi
, inode
->i_ino
);
540 if (f2fs_encrypted_inode(inode
)) {
541 struct qstr istr
= QSTR_INIT(symname
, len
);
542 struct fscrypt_str ostr
;
544 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
550 err
= fscrypt_get_encryption_info(inode
);
554 if (!fscrypt_has_encryption_key(inode
)) {
559 ostr
.name
= sd
->encrypted_path
;
560 ostr
.len
= disk_link
.len
;
561 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
565 sd
->len
= cpu_to_le16(ostr
.len
);
566 disk_link
.name
= (char *)sd
;
569 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
572 d_instantiate(dentry
, inode
);
573 unlock_new_inode(inode
);
576 * Let's flush symlink data in order to avoid broken symlink as much as
577 * possible. Nevertheless, fsyncing is the best way, but there is no
578 * way to get a file descriptor in order to flush that.
580 * Note that, it needs to do dir->fsync to make this recoverable.
581 * If the symlink path is stored into inline_data, there is no
582 * performance regression.
585 filemap_write_and_wait_range(inode
->i_mapping
, 0,
589 f2fs_sync_fs(sbi
->sb
, 1);
591 f2fs_unlink(dir
, dentry
);
596 f2fs_balance_fs(sbi
, true);
599 handle_failed_inode(inode
);
603 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
605 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
609 if (unlikely(f2fs_cp_error(sbi
)))
612 err
= dquot_initialize(dir
);
616 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
618 return PTR_ERR(inode
);
620 inode
->i_op
= &f2fs_dir_inode_operations
;
621 inode
->i_fop
= &f2fs_dir_operations
;
622 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
623 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
625 set_inode_flag(inode
, FI_INC_LINK
);
627 err
= f2fs_add_link(dentry
, inode
);
632 alloc_nid_done(sbi
, inode
->i_ino
);
634 d_instantiate(dentry
, inode
);
635 unlock_new_inode(inode
);
638 f2fs_sync_fs(sbi
->sb
, 1);
640 f2fs_balance_fs(sbi
, true);
644 clear_inode_flag(inode
, FI_INC_LINK
);
645 handle_failed_inode(inode
);
649 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
651 struct inode
*inode
= d_inode(dentry
);
652 if (f2fs_empty_dir(inode
))
653 return f2fs_unlink(dir
, dentry
);
657 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
658 umode_t mode
, dev_t rdev
)
660 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
664 if (unlikely(f2fs_cp_error(sbi
)))
667 err
= dquot_initialize(dir
);
671 inode
= f2fs_new_inode(dir
, mode
);
673 return PTR_ERR(inode
);
675 init_special_inode(inode
, inode
->i_mode
, rdev
);
676 inode
->i_op
= &f2fs_special_inode_operations
;
679 err
= f2fs_add_link(dentry
, inode
);
684 alloc_nid_done(sbi
, inode
->i_ino
);
686 d_instantiate(dentry
, inode
);
687 unlock_new_inode(inode
);
690 f2fs_sync_fs(sbi
->sb
, 1);
692 f2fs_balance_fs(sbi
, true);
695 handle_failed_inode(inode
);
699 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
700 umode_t mode
, struct inode
**whiteout
)
702 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
706 err
= dquot_initialize(dir
);
710 inode
= f2fs_new_inode(dir
, mode
);
712 return PTR_ERR(inode
);
715 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
716 inode
->i_op
= &f2fs_special_inode_operations
;
718 inode
->i_op
= &f2fs_file_inode_operations
;
719 inode
->i_fop
= &f2fs_file_operations
;
720 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
724 err
= acquire_orphan_inode(sbi
);
728 err
= f2fs_do_tmpfile(inode
, dir
);
733 * add this non-linked tmpfile to orphan list, in this way we could
734 * remove all unused data of tmpfile after abnormal power-off.
736 add_orphan_inode(inode
);
737 alloc_nid_done(sbi
, inode
->i_ino
);
740 f2fs_i_links_write(inode
, false);
743 d_tmpfile(dentry
, inode
);
745 /* link_count was changed by d_tmpfile as well. */
747 unlock_new_inode(inode
);
749 f2fs_balance_fs(sbi
, true);
753 release_orphan_inode(sbi
);
755 handle_failed_inode(inode
);
759 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
761 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
764 if (f2fs_encrypted_inode(dir
)) {
765 int err
= fscrypt_get_encryption_info(dir
);
770 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
773 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
775 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
778 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
781 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
782 struct inode
*new_dir
, struct dentry
*new_dentry
,
785 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
786 struct inode
*old_inode
= d_inode(old_dentry
);
787 struct inode
*new_inode
= d_inode(new_dentry
);
788 struct inode
*whiteout
= NULL
;
789 struct page
*old_dir_page
;
790 struct page
*old_page
, *new_page
= NULL
;
791 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
792 struct f2fs_dir_entry
*old_entry
;
793 struct f2fs_dir_entry
*new_entry
;
794 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
797 if (unlikely(f2fs_cp_error(sbi
)))
800 if ((f2fs_encrypted_inode(old_dir
) &&
801 !fscrypt_has_encryption_key(old_dir
)) ||
802 (f2fs_encrypted_inode(new_dir
) &&
803 !fscrypt_has_encryption_key(new_dir
)))
806 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
807 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
812 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
813 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
814 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
817 err
= dquot_initialize(old_dir
);
821 err
= dquot_initialize(new_dir
);
826 err
= dquot_initialize(new_inode
);
831 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
833 if (IS_ERR(old_page
))
834 err
= PTR_ERR(old_page
);
838 if (S_ISDIR(old_inode
->i_mode
)) {
839 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
840 if (!old_dir_entry
) {
841 if (IS_ERR(old_dir_page
))
842 err
= PTR_ERR(old_dir_page
);
847 if (flags
& RENAME_WHITEOUT
) {
848 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
856 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
860 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
863 if (IS_ERR(new_page
))
864 err
= PTR_ERR(new_page
);
868 f2fs_balance_fs(sbi
, true);
872 err
= acquire_orphan_inode(sbi
);
876 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
878 new_inode
->i_ctime
= current_time(new_inode
);
879 down_write(&F2FS_I(new_inode
)->i_sem
);
881 f2fs_i_links_write(new_inode
, false);
882 f2fs_i_links_write(new_inode
, false);
883 up_write(&F2FS_I(new_inode
)->i_sem
);
885 if (!new_inode
->i_nlink
)
886 add_orphan_inode(new_inode
);
888 release_orphan_inode(sbi
);
890 f2fs_balance_fs(sbi
, true);
894 err
= f2fs_add_link(new_dentry
, old_inode
);
901 f2fs_i_links_write(new_dir
, true);
904 * old entry and new entry can locate in the same inline
905 * dentry in inode, when attaching new entry in inline dentry,
906 * it could force inline dentry conversion, after that,
907 * old_entry and old_page will point to wrong address, in
908 * order to avoid this, let's do the check and update here.
910 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
911 f2fs_put_page(old_page
, 0);
914 old_entry
= f2fs_find_entry(old_dir
,
915 &old_dentry
->d_name
, &old_page
);
918 if (IS_ERR(old_page
))
919 err
= PTR_ERR(old_page
);
926 down_write(&F2FS_I(old_inode
)->i_sem
);
927 if (!old_dir_entry
|| whiteout
)
928 file_lost_pino(old_inode
);
930 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
931 up_write(&F2FS_I(old_inode
)->i_sem
);
933 old_inode
->i_ctime
= current_time(old_inode
);
934 f2fs_mark_inode_dirty_sync(old_inode
, false);
936 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
939 whiteout
->i_state
|= I_LINKABLE
;
940 set_inode_flag(whiteout
, FI_INC_LINK
);
941 err
= f2fs_add_link(old_dentry
, whiteout
);
944 whiteout
->i_state
&= ~I_LINKABLE
;
949 if (old_dir
!= new_dir
&& !whiteout
) {
950 f2fs_set_link(old_inode
, old_dir_entry
,
951 old_dir_page
, new_dir
);
953 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
954 f2fs_put_page(old_dir_page
, 0);
956 f2fs_i_links_write(old_dir
, false);
961 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
962 f2fs_sync_fs(sbi
->sb
, 1);
968 f2fs_dentry_kunmap(new_dir
, new_page
);
969 f2fs_put_page(new_page
, 0);
976 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
977 f2fs_put_page(old_dir_page
, 0);
980 f2fs_dentry_kunmap(old_dir
, old_page
);
981 f2fs_put_page(old_page
, 0);
986 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
987 struct inode
*new_dir
, struct dentry
*new_dentry
)
989 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
990 struct inode
*old_inode
= d_inode(old_dentry
);
991 struct inode
*new_inode
= d_inode(new_dentry
);
992 struct page
*old_dir_page
, *new_dir_page
;
993 struct page
*old_page
, *new_page
;
994 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
995 struct f2fs_dir_entry
*old_entry
, *new_entry
;
996 int old_nlink
= 0, new_nlink
= 0;
999 if (unlikely(f2fs_cp_error(sbi
)))
1002 if ((f2fs_encrypted_inode(old_dir
) &&
1003 !fscrypt_has_encryption_key(old_dir
)) ||
1004 (f2fs_encrypted_inode(new_dir
) &&
1005 !fscrypt_has_encryption_key(new_dir
)))
1008 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
1009 (old_dir
!= new_dir
) &&
1010 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
1011 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
1014 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1015 !projid_eq(F2FS_I(new_dir
)->i_projid
,
1016 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
1017 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1018 !projid_eq(F2FS_I(old_dir
)->i_projid
,
1019 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
1022 err
= dquot_initialize(old_dir
);
1026 err
= dquot_initialize(new_dir
);
1030 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
1032 if (IS_ERR(old_page
))
1033 err
= PTR_ERR(old_page
);
1037 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
1039 if (IS_ERR(new_page
))
1040 err
= PTR_ERR(new_page
);
1044 /* prepare for updating ".." directory entry info later */
1045 if (old_dir
!= new_dir
) {
1046 if (S_ISDIR(old_inode
->i_mode
)) {
1047 old_dir_entry
= f2fs_parent_dir(old_inode
,
1049 if (!old_dir_entry
) {
1050 if (IS_ERR(old_dir_page
))
1051 err
= PTR_ERR(old_dir_page
);
1056 if (S_ISDIR(new_inode
->i_mode
)) {
1057 new_dir_entry
= f2fs_parent_dir(new_inode
,
1059 if (!new_dir_entry
) {
1060 if (IS_ERR(new_dir_page
))
1061 err
= PTR_ERR(new_dir_page
);
1068 * If cross rename between file and directory those are not
1069 * in the same directory, we will inc nlink of file's parent
1070 * later, so we should check upper boundary of its nlink.
1072 if ((!old_dir_entry
|| !new_dir_entry
) &&
1073 old_dir_entry
!= new_dir_entry
) {
1074 old_nlink
= old_dir_entry
? -1 : 1;
1075 new_nlink
= -old_nlink
;
1077 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1078 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1082 f2fs_balance_fs(sbi
, true);
1086 /* update ".." directory entry info of old dentry */
1088 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1090 /* update ".." directory entry info of new dentry */
1092 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1094 /* update directory entry info of old dir inode */
1095 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1097 down_write(&F2FS_I(old_inode
)->i_sem
);
1098 file_lost_pino(old_inode
);
1099 up_write(&F2FS_I(old_inode
)->i_sem
);
1101 old_dir
->i_ctime
= current_time(old_dir
);
1103 down_write(&F2FS_I(old_dir
)->i_sem
);
1104 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1105 up_write(&F2FS_I(old_dir
)->i_sem
);
1107 f2fs_mark_inode_dirty_sync(old_dir
, false);
1109 /* update directory entry info of new dir inode */
1110 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1112 down_write(&F2FS_I(new_inode
)->i_sem
);
1113 file_lost_pino(new_inode
);
1114 up_write(&F2FS_I(new_inode
)->i_sem
);
1116 new_dir
->i_ctime
= current_time(new_dir
);
1118 down_write(&F2FS_I(new_dir
)->i_sem
);
1119 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1120 up_write(&F2FS_I(new_dir
)->i_sem
);
1122 f2fs_mark_inode_dirty_sync(new_dir
, false);
1124 f2fs_unlock_op(sbi
);
1126 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1127 f2fs_sync_fs(sbi
->sb
, 1);
1130 if (new_dir_entry
) {
1131 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
1132 f2fs_put_page(new_dir_page
, 0);
1135 if (old_dir_entry
) {
1136 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
1137 f2fs_put_page(old_dir_page
, 0);
1140 f2fs_dentry_kunmap(new_dir
, new_page
);
1141 f2fs_put_page(new_page
, 0);
1143 f2fs_dentry_kunmap(old_dir
, old_page
);
1144 f2fs_put_page(old_page
, 0);
1149 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1150 struct inode
*new_dir
, struct dentry
*new_dentry
,
1153 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1156 if (flags
& RENAME_EXCHANGE
) {
1157 return f2fs_cross_rename(old_dir
, old_dentry
,
1158 new_dir
, new_dentry
);
1161 * VFS has already handled the new dentry existence case,
1162 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1164 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1167 static const char *f2fs_encrypted_follow_link(struct dentry
*dentry
, void **cookie
)
1169 struct page
*cpage
= NULL
;
1170 char *caddr
, *paddr
= NULL
;
1171 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1172 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1173 struct fscrypt_symlink_data
*sd
;
1174 struct inode
*inode
= d_inode(dentry
);
1175 u32 max_size
= inode
->i_sb
->s_blocksize
;
1178 res
= fscrypt_get_encryption_info(inode
);
1180 return ERR_PTR(res
);
1182 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1184 return ERR_CAST(cpage
);
1185 caddr
= page_address(cpage
);
1187 /* Symlink is encrypted */
1188 sd
= (struct fscrypt_symlink_data
*)caddr
;
1189 cstr
.name
= sd
->encrypted_path
;
1190 cstr
.len
= le16_to_cpu(sd
->len
);
1192 /* this is broken symlink case */
1193 if (unlikely(cstr
.len
== 0)) {
1198 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1199 /* Symlink data on the disk is corrupted */
1203 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1207 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1211 /* this is broken symlink case */
1212 if (unlikely(pstr
.name
[0] == 0)) {
1219 /* Null-terminate the name */
1220 paddr
[pstr
.len
] = '\0';
1223 return *cookie
= paddr
;
1225 fscrypt_fname_free_buffer(&pstr
);
1227 return ERR_PTR(res
);
1230 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1231 .readlink
= generic_readlink
,
1232 .follow_link
= f2fs_encrypted_follow_link
,
1233 .put_link
= kfree_put_link
,
1234 .getattr
= f2fs_getattr
,
1235 .setattr
= f2fs_setattr
,
1236 #ifdef CONFIG_F2FS_FS_XATTR
1237 .setxattr
= generic_setxattr
,
1238 .getxattr
= generic_getxattr
,
1239 .listxattr
= f2fs_listxattr
,
1240 .removexattr
= generic_removexattr
,
1244 const struct inode_operations f2fs_dir_inode_operations
= {
1245 .create
= f2fs_create
,
1246 .lookup
= f2fs_lookup
,
1248 .unlink
= f2fs_unlink
,
1249 .symlink
= f2fs_symlink
,
1250 .mkdir
= f2fs_mkdir
,
1251 .rmdir
= f2fs_rmdir
,
1252 .mknod
= f2fs_mknod
,
1253 .rename2
= f2fs_rename2
,
1254 .tmpfile
= f2fs_tmpfile
,
1255 .getattr
= f2fs_getattr
,
1256 .setattr
= f2fs_setattr
,
1257 .get_acl
= f2fs_get_acl
,
1258 .set_acl
= f2fs_set_acl
,
1259 #ifdef CONFIG_F2FS_FS_XATTR
1260 .setxattr
= generic_setxattr
,
1261 .getxattr
= generic_getxattr
,
1262 .listxattr
= f2fs_listxattr
,
1263 .removexattr
= generic_removexattr
,
1267 const struct inode_operations f2fs_symlink_inode_operations
= {
1268 .readlink
= generic_readlink
,
1269 .follow_link
= f2fs_follow_link
,
1270 .put_link
= page_put_link
,
1271 .getattr
= f2fs_getattr
,
1272 .setattr
= f2fs_setattr
,
1273 #ifdef CONFIG_F2FS_FS_XATTR
1274 .setxattr
= generic_setxattr
,
1275 .getxattr
= generic_getxattr
,
1276 .listxattr
= f2fs_listxattr
,
1277 .removexattr
= generic_removexattr
,
1281 const struct inode_operations f2fs_special_inode_operations
= {
1282 .getattr
= f2fs_getattr
,
1283 .setattr
= f2fs_setattr
,
1284 .get_acl
= f2fs_get_acl
,
1285 .set_acl
= f2fs_set_acl
,
1286 #ifdef CONFIG_F2FS_FS_XATTR
1287 .setxattr
= generic_setxattr
,
1288 .getxattr
= generic_getxattr
,
1289 .listxattr
= f2fs_listxattr
,
1290 .removexattr
= generic_removexattr
,