1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched.h>
12 #include <linux/ctype.h>
13 #include <linux/random.h>
14 #include <linux/dcache.h>
15 #include <linux/namei.h>
16 #include <linux/quotaops.h>
23 #include <trace/events/f2fs.h>
25 static struct inode
*f2fs_new_inode(struct inode
*dir
, umode_t mode
)
27 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
30 bool nid_free
= false;
34 inode
= new_inode(dir
->i_sb
);
36 return ERR_PTR(-ENOMEM
);
39 if (!f2fs_alloc_nid(sbi
, &ino
)) {
48 inode_init_owner(inode
, dir
, mode
);
52 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
53 F2FS_I(inode
)->i_crtime
= inode
->i_mtime
;
54 inode
->i_generation
= prandom_u32();
56 if (S_ISDIR(inode
->i_mode
))
57 F2FS_I(inode
)->i_current_depth
= 1;
59 err
= insert_inode_locked(inode
);
65 if (f2fs_sb_has_project_quota(sbi
) &&
66 (F2FS_I(dir
)->i_flags
& F2FS_PROJINHERIT_FL
))
67 F2FS_I(inode
)->i_projid
= F2FS_I(dir
)->i_projid
;
69 F2FS_I(inode
)->i_projid
= make_kprojid(&init_user_ns
,
72 err
= dquot_initialize(inode
);
76 set_inode_flag(inode
, FI_NEW_INODE
);
78 /* If the directory encrypted, then we should encrypt the inode. */
79 if ((IS_ENCRYPTED(dir
) || DUMMY_ENCRYPTION_ENABLED(sbi
)) &&
80 f2fs_may_encrypt(inode
))
81 f2fs_set_encrypted_inode(inode
);
83 if (f2fs_sb_has_extra_attr(sbi
)) {
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
)) {
97 f2fs_bug_on(sbi
, !f2fs_has_extra_attr(inode
));
98 if (f2fs_has_inline_xattr(inode
))
99 xattr_size
= F2FS_OPTION(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
|= F2FS_INDEX_FL
;
119 if (F2FS_I(inode
)->i_flags
& F2FS_PROJINHERIT_FL
)
120 set_inode_flag(inode
, FI_PROJ_INHERIT
);
122 f2fs_set_inode_flags(inode
);
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 inline 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
]))
188 up_read(&sbi
->sb_lock
);
190 if (i
== cold_count
+ hot_count
)
194 file_set_cold(inode
);
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 memcpy(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 memcpy(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
)))
275 if (!f2fs_is_checkpoint_ready(sbi
))
278 err
= dquot_initialize(dir
);
282 inode
= f2fs_new_inode(dir
, mode
);
284 return PTR_ERR(inode
);
286 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
287 set_file_temperature(sbi
, inode
, dentry
->d_name
.name
);
289 inode
->i_op
= &f2fs_file_inode_operations
;
290 inode
->i_fop
= &f2fs_file_operations
;
291 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
295 err
= f2fs_add_link(dentry
, inode
);
300 f2fs_alloc_nid_done(sbi
, ino
);
302 d_instantiate_new(dentry
, inode
);
305 f2fs_sync_fs(sbi
->sb
, 1);
307 f2fs_balance_fs(sbi
, true);
310 f2fs_handle_failed_inode(inode
);
314 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
315 struct dentry
*dentry
)
317 struct inode
*inode
= d_inode(old_dentry
);
318 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
321 if (unlikely(f2fs_cp_error(sbi
)))
323 if (!f2fs_is_checkpoint_ready(sbi
))
326 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
330 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
331 (!projid_eq(F2FS_I(dir
)->i_projid
,
332 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
335 err
= dquot_initialize(dir
);
339 f2fs_balance_fs(sbi
, true);
341 inode
->i_ctime
= current_time(inode
);
344 set_inode_flag(inode
, FI_INC_LINK
);
346 err
= f2fs_add_link(dentry
, inode
);
351 d_instantiate(dentry
, inode
);
354 f2fs_sync_fs(sbi
->sb
, 1);
357 clear_inode_flag(inode
, FI_INC_LINK
);
363 struct dentry
*f2fs_get_parent(struct dentry
*child
)
365 struct qstr dotdot
= QSTR_INIT("..", 2);
367 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
370 return ERR_CAST(page
);
371 return ERR_PTR(-ENOENT
);
373 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
376 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
378 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
379 struct qstr dot
= QSTR_INIT(".", 1);
380 struct qstr dotdot
= QSTR_INIT("..", 2);
381 struct f2fs_dir_entry
*de
;
385 if (f2fs_readonly(sbi
->sb
)) {
386 f2fs_info(sbi
, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
391 err
= dquot_initialize(dir
);
395 f2fs_balance_fs(sbi
, true);
399 de
= f2fs_find_entry(dir
, &dot
, &page
);
401 f2fs_put_page(page
, 0);
402 } else if (IS_ERR(page
)) {
406 err
= f2fs_do_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
411 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
413 f2fs_put_page(page
, 0);
414 else if (IS_ERR(page
))
417 err
= f2fs_do_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
420 clear_inode_flag(dir
, FI_INLINE_DOTS
);
426 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
429 struct inode
*inode
= NULL
;
430 struct f2fs_dir_entry
*de
;
435 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
436 struct fscrypt_name fname
;
438 trace_f2fs_lookup_start(dir
, dentry
, flags
);
440 if (dentry
->d_name
.len
> F2FS_NAME_LEN
) {
445 err
= fscrypt_prepare_lookup(dir
, dentry
, &fname
);
450 de
= __f2fs_find_entry(dir
, &fname
, &page
);
451 fscrypt_free_filename(&fname
);
461 ino
= le32_to_cpu(de
->ino
);
462 f2fs_put_page(page
, 0);
464 inode
= f2fs_iget(dir
->i_sb
, ino
);
466 err
= PTR_ERR(inode
);
470 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
471 err
= __recover_dot_dentries(dir
, root_ino
);
476 if (f2fs_has_inline_dots(inode
)) {
477 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
481 if (IS_ENCRYPTED(dir
) &&
482 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
483 !fscrypt_has_permitted_context(dir
, inode
)) {
484 f2fs_warn(F2FS_I_SB(inode
), "Inconsistent encryption contexts: %lu/%lu",
485 dir
->i_ino
, inode
->i_ino
);
490 #ifdef CONFIG_UNICODE
491 if (!inode
&& IS_CASEFOLDED(dir
)) {
492 /* Eventually we want to call d_add_ci(dentry, NULL)
493 * for negative dentries in the encoding case as
494 * well. For now, prevent the negative dentry
497 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
501 new = d_splice_alias(inode
, dentry
);
502 err
= PTR_ERR_OR_ZERO(new);
503 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
508 trace_f2fs_lookup_end(dir
, dentry
, ino
, err
);
512 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
514 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
515 struct inode
*inode
= d_inode(dentry
);
516 struct f2fs_dir_entry
*de
;
520 trace_f2fs_unlink_enter(dir
, dentry
);
522 if (unlikely(f2fs_cp_error(sbi
)))
525 err
= dquot_initialize(dir
);
528 err
= dquot_initialize(inode
);
532 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
539 f2fs_balance_fs(sbi
, true);
542 err
= f2fs_acquire_orphan_inode(sbi
);
545 f2fs_put_page(page
, 0);
548 f2fs_delete_entry(de
, page
, dir
, inode
);
549 #ifdef CONFIG_UNICODE
550 /* VFS negative dentries are incompatible with Encoding and
551 * Case-insensitiveness. Eventually we'll want avoid
552 * invalidating the dentries here, alongside with returning the
553 * negative dentries at f2fs_lookup(), when it is better
554 * supported by the VFS for the CI case.
556 if (IS_CASEFOLDED(dir
))
557 d_invalidate(dentry
);
562 f2fs_sync_fs(sbi
->sb
, 1);
564 trace_f2fs_unlink_exit(inode
, err
);
568 static const char *f2fs_get_link(struct dentry
*dentry
,
570 struct delayed_call
*done
)
572 const char *link
= page_get_link(dentry
, inode
, done
);
573 if (!IS_ERR(link
) && !*link
) {
574 /* this is broken symlink case */
575 do_delayed_call(done
);
576 clear_delayed_call(done
);
577 link
= ERR_PTR(-ENOENT
);
582 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
585 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
587 size_t len
= strlen(symname
);
588 struct fscrypt_str disk_link
;
591 if (unlikely(f2fs_cp_error(sbi
)))
593 if (!f2fs_is_checkpoint_ready(sbi
))
596 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
601 err
= dquot_initialize(dir
);
605 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
607 return PTR_ERR(inode
);
609 if (IS_ENCRYPTED(inode
))
610 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
612 inode
->i_op
= &f2fs_symlink_inode_operations
;
613 inode_nohighmem(inode
);
614 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
617 err
= f2fs_add_link(dentry
, inode
);
619 goto out_f2fs_handle_failed_inode
;
621 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
623 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
627 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
630 d_instantiate_new(dentry
, inode
);
633 * Let's flush symlink data in order to avoid broken symlink as much as
634 * possible. Nevertheless, fsyncing is the best way, but there is no
635 * way to get a file descriptor in order to flush that.
637 * Note that, it needs to do dir->fsync to make this recoverable.
638 * If the symlink path is stored into inline_data, there is no
639 * performance regression.
642 filemap_write_and_wait_range(inode
->i_mapping
, 0,
646 f2fs_sync_fs(sbi
->sb
, 1);
648 f2fs_unlink(dir
, dentry
);
651 f2fs_balance_fs(sbi
, true);
652 goto out_free_encrypted_link
;
654 out_f2fs_handle_failed_inode
:
655 f2fs_handle_failed_inode(inode
);
656 out_free_encrypted_link
:
657 if (disk_link
.name
!= (unsigned char *)symname
)
658 kvfree(disk_link
.name
);
662 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
664 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
668 if (unlikely(f2fs_cp_error(sbi
)))
671 err
= dquot_initialize(dir
);
675 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
677 return PTR_ERR(inode
);
679 inode
->i_op
= &f2fs_dir_inode_operations
;
680 inode
->i_fop
= &f2fs_dir_operations
;
681 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
682 inode_nohighmem(inode
);
684 set_inode_flag(inode
, FI_INC_LINK
);
686 err
= f2fs_add_link(dentry
, inode
);
691 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
693 d_instantiate_new(dentry
, inode
);
696 f2fs_sync_fs(sbi
->sb
, 1);
698 f2fs_balance_fs(sbi
, true);
702 clear_inode_flag(inode
, FI_INC_LINK
);
703 f2fs_handle_failed_inode(inode
);
707 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
709 struct inode
*inode
= d_inode(dentry
);
710 if (f2fs_empty_dir(inode
))
711 return f2fs_unlink(dir
, dentry
);
715 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
716 umode_t mode
, dev_t rdev
)
718 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
722 if (unlikely(f2fs_cp_error(sbi
)))
724 if (!f2fs_is_checkpoint_ready(sbi
))
727 err
= dquot_initialize(dir
);
731 inode
= f2fs_new_inode(dir
, mode
);
733 return PTR_ERR(inode
);
735 init_special_inode(inode
, inode
->i_mode
, rdev
);
736 inode
->i_op
= &f2fs_special_inode_operations
;
739 err
= f2fs_add_link(dentry
, inode
);
744 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
746 d_instantiate_new(dentry
, inode
);
749 f2fs_sync_fs(sbi
->sb
, 1);
751 f2fs_balance_fs(sbi
, true);
754 f2fs_handle_failed_inode(inode
);
758 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
759 umode_t mode
, struct inode
**whiteout
)
761 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
765 err
= dquot_initialize(dir
);
769 inode
= f2fs_new_inode(dir
, mode
);
771 return PTR_ERR(inode
);
774 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
775 inode
->i_op
= &f2fs_special_inode_operations
;
777 inode
->i_op
= &f2fs_file_inode_operations
;
778 inode
->i_fop
= &f2fs_file_operations
;
779 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
783 err
= f2fs_acquire_orphan_inode(sbi
);
787 err
= f2fs_do_tmpfile(inode
, dir
);
792 * add this non-linked tmpfile to orphan list, in this way we could
793 * remove all unused data of tmpfile after abnormal power-off.
795 f2fs_add_orphan_inode(inode
);
796 f2fs_alloc_nid_done(sbi
, inode
->i_ino
);
799 f2fs_i_links_write(inode
, false);
802 d_tmpfile(dentry
, inode
);
804 /* link_count was changed by d_tmpfile as well. */
806 unlock_new_inode(inode
);
808 f2fs_balance_fs(sbi
, true);
812 f2fs_release_orphan_inode(sbi
);
814 f2fs_handle_failed_inode(inode
);
818 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
820 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
822 if (unlikely(f2fs_cp_error(sbi
)))
824 if (!f2fs_is_checkpoint_ready(sbi
))
827 if (IS_ENCRYPTED(dir
) || DUMMY_ENCRYPTION_ENABLED(sbi
)) {
828 int err
= fscrypt_get_encryption_info(dir
);
833 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
836 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
838 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir
))))
841 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
844 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
845 struct inode
*new_dir
, struct dentry
*new_dentry
,
848 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
849 struct inode
*old_inode
= d_inode(old_dentry
);
850 struct inode
*new_inode
= d_inode(new_dentry
);
851 struct inode
*whiteout
= NULL
;
852 struct page
*old_dir_page
;
853 struct page
*old_page
, *new_page
= NULL
;
854 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
855 struct f2fs_dir_entry
*old_entry
;
856 struct f2fs_dir_entry
*new_entry
;
857 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
860 if (unlikely(f2fs_cp_error(sbi
)))
862 if (!f2fs_is_checkpoint_ready(sbi
))
865 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
866 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
867 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
870 err
= dquot_initialize(old_dir
);
874 err
= dquot_initialize(new_dir
);
879 err
= dquot_initialize(new_inode
);
885 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
887 if (IS_ERR(old_page
))
888 err
= PTR_ERR(old_page
);
892 if (S_ISDIR(old_inode
->i_mode
)) {
893 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
894 if (!old_dir_entry
) {
895 if (IS_ERR(old_dir_page
))
896 err
= PTR_ERR(old_dir_page
);
901 if (flags
& RENAME_WHITEOUT
) {
902 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
910 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
914 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
917 if (IS_ERR(new_page
))
918 err
= PTR_ERR(new_page
);
922 f2fs_balance_fs(sbi
, true);
926 err
= f2fs_acquire_orphan_inode(sbi
);
930 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
932 new_inode
->i_ctime
= current_time(new_inode
);
933 down_write(&F2FS_I(new_inode
)->i_sem
);
935 f2fs_i_links_write(new_inode
, false);
936 f2fs_i_links_write(new_inode
, false);
937 up_write(&F2FS_I(new_inode
)->i_sem
);
939 if (!new_inode
->i_nlink
)
940 f2fs_add_orphan_inode(new_inode
);
942 f2fs_release_orphan_inode(sbi
);
944 f2fs_balance_fs(sbi
, true);
948 err
= f2fs_add_link(new_dentry
, old_inode
);
955 f2fs_i_links_write(new_dir
, true);
958 * old entry and new entry can locate in the same inline
959 * dentry in inode, when attaching new entry in inline dentry,
960 * it could force inline dentry conversion, after that,
961 * old_entry and old_page will point to wrong address, in
962 * order to avoid this, let's do the check and update here.
964 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
965 f2fs_put_page(old_page
, 0);
968 old_entry
= f2fs_find_entry(old_dir
,
969 &old_dentry
->d_name
, &old_page
);
972 if (IS_ERR(old_page
))
973 err
= PTR_ERR(old_page
);
980 down_write(&F2FS_I(old_inode
)->i_sem
);
981 if (!old_dir_entry
|| whiteout
)
982 file_lost_pino(old_inode
);
984 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
985 up_write(&F2FS_I(old_inode
)->i_sem
);
987 old_inode
->i_ctime
= current_time(old_inode
);
988 f2fs_mark_inode_dirty_sync(old_inode
, false);
990 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
993 whiteout
->i_state
|= I_LINKABLE
;
994 set_inode_flag(whiteout
, FI_INC_LINK
);
995 err
= f2fs_add_link(old_dentry
, whiteout
);
998 whiteout
->i_state
&= ~I_LINKABLE
;
1002 if (old_dir_entry
) {
1003 if (old_dir
!= new_dir
&& !whiteout
)
1004 f2fs_set_link(old_inode
, old_dir_entry
,
1005 old_dir_page
, new_dir
);
1007 f2fs_put_page(old_dir_page
, 0);
1008 f2fs_i_links_write(old_dir
, false);
1010 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1011 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1012 if (S_ISDIR(old_inode
->i_mode
))
1013 f2fs_add_ino_entry(sbi
, old_inode
->i_ino
,
1017 f2fs_unlock_op(sbi
);
1019 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1020 f2fs_sync_fs(sbi
->sb
, 1);
1022 f2fs_update_time(sbi
, REQ_TIME
);
1026 f2fs_unlock_op(sbi
);
1028 f2fs_put_page(new_page
, 0);
1034 f2fs_put_page(old_dir_page
, 0);
1036 f2fs_put_page(old_page
, 0);
1041 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1042 struct inode
*new_dir
, struct dentry
*new_dentry
)
1044 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
1045 struct inode
*old_inode
= d_inode(old_dentry
);
1046 struct inode
*new_inode
= d_inode(new_dentry
);
1047 struct page
*old_dir_page
, *new_dir_page
;
1048 struct page
*old_page
, *new_page
;
1049 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
1050 struct f2fs_dir_entry
*old_entry
, *new_entry
;
1051 int old_nlink
= 0, new_nlink
= 0;
1054 if (unlikely(f2fs_cp_error(sbi
)))
1056 if (!f2fs_is_checkpoint_ready(sbi
))
1059 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1060 !projid_eq(F2FS_I(new_dir
)->i_projid
,
1061 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
1062 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
1063 !projid_eq(F2FS_I(old_dir
)->i_projid
,
1064 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
1067 err
= dquot_initialize(old_dir
);
1071 err
= dquot_initialize(new_dir
);
1076 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
1078 if (IS_ERR(old_page
))
1079 err
= PTR_ERR(old_page
);
1083 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
1085 if (IS_ERR(new_page
))
1086 err
= PTR_ERR(new_page
);
1090 /* prepare for updating ".." directory entry info later */
1091 if (old_dir
!= new_dir
) {
1092 if (S_ISDIR(old_inode
->i_mode
)) {
1093 old_dir_entry
= f2fs_parent_dir(old_inode
,
1095 if (!old_dir_entry
) {
1096 if (IS_ERR(old_dir_page
))
1097 err
= PTR_ERR(old_dir_page
);
1102 if (S_ISDIR(new_inode
->i_mode
)) {
1103 new_dir_entry
= f2fs_parent_dir(new_inode
,
1105 if (!new_dir_entry
) {
1106 if (IS_ERR(new_dir_page
))
1107 err
= PTR_ERR(new_dir_page
);
1114 * If cross rename between file and directory those are not
1115 * in the same directory, we will inc nlink of file's parent
1116 * later, so we should check upper boundary of its nlink.
1118 if ((!old_dir_entry
|| !new_dir_entry
) &&
1119 old_dir_entry
!= new_dir_entry
) {
1120 old_nlink
= old_dir_entry
? -1 : 1;
1121 new_nlink
= -old_nlink
;
1123 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1124 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1128 f2fs_balance_fs(sbi
, true);
1132 /* update ".." directory entry info of old dentry */
1134 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1136 /* update ".." directory entry info of new dentry */
1138 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1140 /* update directory entry info of old dir inode */
1141 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1143 down_write(&F2FS_I(old_inode
)->i_sem
);
1144 file_lost_pino(old_inode
);
1145 up_write(&F2FS_I(old_inode
)->i_sem
);
1147 old_dir
->i_ctime
= current_time(old_dir
);
1149 down_write(&F2FS_I(old_dir
)->i_sem
);
1150 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1151 up_write(&F2FS_I(old_dir
)->i_sem
);
1153 f2fs_mark_inode_dirty_sync(old_dir
, false);
1155 /* update directory entry info of new dir inode */
1156 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1158 down_write(&F2FS_I(new_inode
)->i_sem
);
1159 file_lost_pino(new_inode
);
1160 up_write(&F2FS_I(new_inode
)->i_sem
);
1162 new_dir
->i_ctime
= current_time(new_dir
);
1164 down_write(&F2FS_I(new_dir
)->i_sem
);
1165 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1166 up_write(&F2FS_I(new_dir
)->i_sem
);
1168 f2fs_mark_inode_dirty_sync(new_dir
, false);
1170 if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
) {
1171 f2fs_add_ino_entry(sbi
, old_dir
->i_ino
, TRANS_DIR_INO
);
1172 f2fs_add_ino_entry(sbi
, new_dir
->i_ino
, TRANS_DIR_INO
);
1175 f2fs_unlock_op(sbi
);
1177 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1178 f2fs_sync_fs(sbi
->sb
, 1);
1180 f2fs_update_time(sbi
, REQ_TIME
);
1183 if (new_dir_entry
) {
1184 f2fs_put_page(new_dir_page
, 0);
1187 if (old_dir_entry
) {
1188 f2fs_put_page(old_dir_page
, 0);
1191 f2fs_put_page(new_page
, 0);
1193 f2fs_put_page(old_page
, 0);
1198 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1199 struct inode
*new_dir
, struct dentry
*new_dentry
,
1204 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1207 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
1212 if (flags
& RENAME_EXCHANGE
) {
1213 return f2fs_cross_rename(old_dir
, old_dentry
,
1214 new_dir
, new_dentry
);
1217 * VFS has already handled the new dentry existence case,
1218 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1220 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1223 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1224 struct inode
*inode
,
1225 struct delayed_call
*done
)
1231 return ERR_PTR(-ECHILD
);
1233 page
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1235 return ERR_CAST(page
);
1237 target
= fscrypt_get_symlink(inode
, page_address(page
),
1238 inode
->i_sb
->s_blocksize
, done
);
1243 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1244 .get_link
= f2fs_encrypted_get_link
,
1245 .getattr
= f2fs_getattr
,
1246 .setattr
= f2fs_setattr
,
1247 #ifdef CONFIG_F2FS_FS_XATTR
1248 .listxattr
= f2fs_listxattr
,
1252 const struct inode_operations f2fs_dir_inode_operations
= {
1253 .create
= f2fs_create
,
1254 .lookup
= f2fs_lookup
,
1256 .unlink
= f2fs_unlink
,
1257 .symlink
= f2fs_symlink
,
1258 .mkdir
= f2fs_mkdir
,
1259 .rmdir
= f2fs_rmdir
,
1260 .mknod
= f2fs_mknod
,
1261 .rename
= f2fs_rename2
,
1262 .tmpfile
= f2fs_tmpfile
,
1263 .getattr
= f2fs_getattr
,
1264 .setattr
= f2fs_setattr
,
1265 .get_acl
= f2fs_get_acl
,
1266 .set_acl
= f2fs_set_acl
,
1267 #ifdef CONFIG_F2FS_FS_XATTR
1268 .listxattr
= f2fs_listxattr
,
1270 .fiemap
= f2fs_fiemap
,
1273 const struct inode_operations f2fs_symlink_inode_operations
= {
1274 .get_link
= f2fs_get_link
,
1275 .getattr
= f2fs_getattr
,
1276 .setattr
= f2fs_setattr
,
1277 #ifdef CONFIG_F2FS_FS_XATTR
1278 .listxattr
= f2fs_listxattr
,
1282 const struct inode_operations f2fs_special_inode_operations
= {
1283 .getattr
= f2fs_getattr
,
1284 .setattr
= f2fs_setattr
,
1285 .get_acl
= f2fs_get_acl
,
1286 .set_acl
= f2fs_set_acl
,
1287 #ifdef CONFIG_F2FS_FS_XATTR
1288 .listxattr
= f2fs_listxattr
,