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>
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;
33 inode
= new_inode(dir
->i_sb
);
35 return ERR_PTR(-ENOMEM
);
38 if (!alloc_nid(sbi
, &ino
)) {
45 inode_init_owner(inode
, dir
, mode
);
49 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
50 inode
->i_generation
= sbi
->s_next_generation
++;
52 err
= insert_inode_locked(inode
);
59 /* If the directory encrypted, then we should encrypt the inode. */
60 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
))
61 f2fs_set_encrypted_inode(inode
);
63 set_inode_flag(inode
, FI_NEW_INODE
);
65 if (test_opt(sbi
, INLINE_XATTR
))
66 set_inode_flag(inode
, FI_INLINE_XATTR
);
67 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
68 set_inode_flag(inode
, FI_INLINE_DATA
);
69 if (f2fs_may_inline_dentry(inode
))
70 set_inode_flag(inode
, FI_INLINE_DENTRY
);
72 f2fs_init_extent_tree(inode
, NULL
);
74 stat_inc_inline_xattr(inode
);
75 stat_inc_inline_inode(inode
);
76 stat_inc_inline_dir(inode
);
78 trace_f2fs_new_inode(inode
, 0);
82 trace_f2fs_new_inode(inode
, err
);
83 make_bad_inode(inode
);
85 set_inode_flag(inode
, FI_FREE_NID
);
90 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
92 size_t slen
= strlen(s
);
93 size_t sublen
= strlen(sub
);
97 * filename format of multimedia file should be defined as:
98 * "filename + '.' + extension + (optional: '.' + temp extension)".
100 if (slen
< sublen
+ 2)
103 for (i
= 1; i
< slen
- sublen
; i
++) {
106 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
114 * Set multimedia files as cold files for hot/cold data separation
116 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
117 const unsigned char *name
)
120 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
122 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
123 for (i
= 0; i
< count
; i
++) {
124 if (is_multimedia_file(name
, extlist
[i
])) {
125 file_set_cold(inode
);
131 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
134 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
139 inode
= f2fs_new_inode(dir
, mode
);
141 return PTR_ERR(inode
);
143 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
144 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
146 inode
->i_op
= &f2fs_file_inode_operations
;
147 inode
->i_fop
= &f2fs_file_operations
;
148 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
151 f2fs_balance_fs(sbi
, true);
154 err
= f2fs_add_link(dentry
, inode
);
159 alloc_nid_done(sbi
, ino
);
161 d_instantiate(dentry
, inode
);
162 unlock_new_inode(inode
);
165 f2fs_sync_fs(sbi
->sb
, 1);
168 handle_failed_inode(inode
);
172 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
173 struct dentry
*dentry
)
175 struct inode
*inode
= d_inode(old_dentry
);
176 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
179 if (f2fs_encrypted_inode(dir
) &&
180 !fscrypt_has_permitted_context(dir
, inode
))
183 f2fs_balance_fs(sbi
, true);
185 inode
->i_ctime
= current_time(inode
);
188 set_inode_flag(inode
, FI_INC_LINK
);
190 err
= f2fs_add_link(dentry
, inode
);
195 d_instantiate(dentry
, inode
);
198 f2fs_sync_fs(sbi
->sb
, 1);
201 clear_inode_flag(inode
, FI_INC_LINK
);
207 struct dentry
*f2fs_get_parent(struct dentry
*child
)
209 struct qstr dotdot
= QSTR_INIT("..", 2);
211 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
214 return ERR_CAST(page
);
215 return ERR_PTR(-ENOENT
);
217 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
220 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
222 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
223 struct qstr dot
= QSTR_INIT(".", 1);
224 struct qstr dotdot
= QSTR_INIT("..", 2);
225 struct f2fs_dir_entry
*de
;
229 if (f2fs_readonly(sbi
->sb
)) {
230 f2fs_msg(sbi
->sb
, KERN_INFO
,
231 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
232 "in readonly mountpoint", dir
->i_ino
, pino
);
236 f2fs_balance_fs(sbi
, true);
240 de
= f2fs_find_entry(dir
, &dot
, &page
);
242 f2fs_dentry_kunmap(dir
, page
);
243 f2fs_put_page(page
, 0);
244 } else if (IS_ERR(page
)) {
248 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
253 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
255 f2fs_dentry_kunmap(dir
, page
);
256 f2fs_put_page(page
, 0);
257 } else if (IS_ERR(page
)) {
260 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
264 clear_inode_flag(dir
, FI_INLINE_DOTS
);
270 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
273 struct inode
*inode
= NULL
;
274 struct f2fs_dir_entry
*de
;
278 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
280 if (f2fs_encrypted_inode(dir
)) {
281 int res
= fscrypt_get_encryption_info(dir
);
284 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
285 * created while the directory was encrypted and we
286 * don't have access to the key.
288 if (fscrypt_has_encryption_key(dir
))
289 fscrypt_set_encrypted_dentry(dentry
);
290 fscrypt_set_d_op(dentry
);
291 if (res
&& res
!= -ENOKEY
)
295 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
296 return ERR_PTR(-ENAMETOOLONG
);
298 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
301 return (struct dentry
*)page
;
302 return d_splice_alias(inode
, dentry
);
305 ino
= le32_to_cpu(de
->ino
);
306 f2fs_dentry_kunmap(dir
, page
);
307 f2fs_put_page(page
, 0);
309 inode
= f2fs_iget(dir
->i_sb
, ino
);
311 return ERR_CAST(inode
);
313 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
314 err
= __recover_dot_dentries(dir
, root_ino
);
319 if (f2fs_has_inline_dots(inode
)) {
320 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
324 if (!IS_ERR(inode
) && f2fs_encrypted_inode(dir
) &&
325 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
326 !fscrypt_has_permitted_context(dir
, inode
)) {
327 bool nokey
= f2fs_encrypted_inode(inode
) &&
328 !fscrypt_has_encryption_key(inode
);
329 err
= nokey
? -ENOKEY
: -EPERM
;
332 return d_splice_alias(inode
, dentry
);
339 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
341 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
342 struct inode
*inode
= d_inode(dentry
);
343 struct f2fs_dir_entry
*de
;
347 trace_f2fs_unlink_enter(dir
, dentry
);
349 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
356 f2fs_balance_fs(sbi
, true);
359 err
= acquire_orphan_inode(sbi
);
362 f2fs_dentry_kunmap(dir
, page
);
363 f2fs_put_page(page
, 0);
366 f2fs_delete_entry(de
, page
, dir
, inode
);
370 f2fs_sync_fs(sbi
->sb
, 1);
372 trace_f2fs_unlink_exit(inode
, err
);
376 static const char *f2fs_get_link(struct dentry
*dentry
,
378 struct delayed_call
*done
)
380 const char *link
= page_get_link(dentry
, inode
, done
);
381 if (!IS_ERR(link
) && !*link
) {
382 /* this is broken symlink case */
383 do_delayed_call(done
);
384 clear_delayed_call(done
);
385 link
= ERR_PTR(-ENOENT
);
390 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
393 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
395 size_t len
= strlen(symname
);
396 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
397 struct fscrypt_symlink_data
*sd
= NULL
;
400 if (f2fs_encrypted_inode(dir
)) {
401 err
= fscrypt_get_encryption_info(dir
);
405 if (!fscrypt_has_encryption_key(dir
))
408 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
409 sizeof(struct fscrypt_symlink_data
));
412 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
413 return -ENAMETOOLONG
;
415 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
417 return PTR_ERR(inode
);
419 if (f2fs_encrypted_inode(inode
))
420 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
422 inode
->i_op
= &f2fs_symlink_inode_operations
;
423 inode_nohighmem(inode
);
424 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
426 f2fs_balance_fs(sbi
, true);
429 err
= f2fs_add_link(dentry
, inode
);
433 alloc_nid_done(sbi
, inode
->i_ino
);
435 if (f2fs_encrypted_inode(inode
)) {
436 struct qstr istr
= QSTR_INIT(symname
, len
);
437 struct fscrypt_str ostr
;
439 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
445 err
= fscrypt_get_encryption_info(inode
);
449 if (!fscrypt_has_encryption_key(inode
)) {
454 ostr
.name
= sd
->encrypted_path
;
455 ostr
.len
= disk_link
.len
;
456 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
460 sd
->len
= cpu_to_le16(ostr
.len
);
461 disk_link
.name
= (char *)sd
;
464 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
467 d_instantiate(dentry
, inode
);
468 unlock_new_inode(inode
);
471 * Let's flush symlink data in order to avoid broken symlink as much as
472 * possible. Nevertheless, fsyncing is the best way, but there is no
473 * way to get a file descriptor in order to flush that.
475 * Note that, it needs to do dir->fsync to make this recoverable.
476 * If the symlink path is stored into inline_data, there is no
477 * performance regression.
480 filemap_write_and_wait_range(inode
->i_mapping
, 0,
484 f2fs_sync_fs(sbi
->sb
, 1);
486 f2fs_unlink(dir
, dentry
);
492 handle_failed_inode(inode
);
496 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
498 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
502 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
504 return PTR_ERR(inode
);
506 inode
->i_op
= &f2fs_dir_inode_operations
;
507 inode
->i_fop
= &f2fs_dir_operations
;
508 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
509 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
511 f2fs_balance_fs(sbi
, true);
513 set_inode_flag(inode
, FI_INC_LINK
);
515 err
= f2fs_add_link(dentry
, inode
);
520 alloc_nid_done(sbi
, inode
->i_ino
);
522 d_instantiate(dentry
, inode
);
523 unlock_new_inode(inode
);
526 f2fs_sync_fs(sbi
->sb
, 1);
530 clear_inode_flag(inode
, FI_INC_LINK
);
531 handle_failed_inode(inode
);
535 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
537 struct inode
*inode
= d_inode(dentry
);
538 if (f2fs_empty_dir(inode
))
539 return f2fs_unlink(dir
, dentry
);
543 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
544 umode_t mode
, dev_t rdev
)
546 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
550 inode
= f2fs_new_inode(dir
, mode
);
552 return PTR_ERR(inode
);
554 init_special_inode(inode
, inode
->i_mode
, rdev
);
555 inode
->i_op
= &f2fs_special_inode_operations
;
557 f2fs_balance_fs(sbi
, true);
560 err
= f2fs_add_link(dentry
, inode
);
565 alloc_nid_done(sbi
, inode
->i_ino
);
567 d_instantiate(dentry
, inode
);
568 unlock_new_inode(inode
);
571 f2fs_sync_fs(sbi
->sb
, 1);
574 handle_failed_inode(inode
);
578 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
579 umode_t mode
, struct inode
**whiteout
)
581 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
585 inode
= f2fs_new_inode(dir
, mode
);
587 return PTR_ERR(inode
);
590 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
591 inode
->i_op
= &f2fs_special_inode_operations
;
593 inode
->i_op
= &f2fs_file_inode_operations
;
594 inode
->i_fop
= &f2fs_file_operations
;
595 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
598 f2fs_balance_fs(sbi
, true);
601 err
= acquire_orphan_inode(sbi
);
605 err
= f2fs_do_tmpfile(inode
, dir
);
610 * add this non-linked tmpfile to orphan list, in this way we could
611 * remove all unused data of tmpfile after abnormal power-off.
613 add_orphan_inode(inode
);
614 alloc_nid_done(sbi
, inode
->i_ino
);
617 f2fs_i_links_write(inode
, false);
620 d_tmpfile(dentry
, inode
);
622 /* link_count was changed by d_tmpfile as well. */
624 unlock_new_inode(inode
);
628 release_orphan_inode(sbi
);
630 handle_failed_inode(inode
);
634 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
636 if (f2fs_encrypted_inode(dir
)) {
637 int err
= fscrypt_get_encryption_info(dir
);
642 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
645 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
647 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
650 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
651 struct inode
*new_dir
, struct dentry
*new_dentry
,
654 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
655 struct inode
*old_inode
= d_inode(old_dentry
);
656 struct inode
*new_inode
= d_inode(new_dentry
);
657 struct inode
*whiteout
= NULL
;
658 struct page
*old_dir_page
;
659 struct page
*old_page
, *new_page
= NULL
;
660 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
661 struct f2fs_dir_entry
*old_entry
;
662 struct f2fs_dir_entry
*new_entry
;
663 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
666 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
667 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
672 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
674 if (IS_ERR(old_page
))
675 err
= PTR_ERR(old_page
);
679 if (S_ISDIR(old_inode
->i_mode
)) {
680 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
681 if (!old_dir_entry
) {
682 if (IS_ERR(old_dir_page
))
683 err
= PTR_ERR(old_dir_page
);
688 if (flags
& RENAME_WHITEOUT
) {
689 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
697 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
701 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
704 if (IS_ERR(new_page
))
705 err
= PTR_ERR(new_page
);
709 f2fs_balance_fs(sbi
, true);
713 err
= acquire_orphan_inode(sbi
);
717 err
= update_dent_inode(old_inode
, new_inode
,
718 &new_dentry
->d_name
);
720 release_orphan_inode(sbi
);
724 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
726 new_inode
->i_ctime
= current_time(new_inode
);
727 down_write(&F2FS_I(new_inode
)->i_sem
);
729 f2fs_i_links_write(new_inode
, false);
730 f2fs_i_links_write(new_inode
, false);
731 up_write(&F2FS_I(new_inode
)->i_sem
);
733 if (!new_inode
->i_nlink
)
734 add_orphan_inode(new_inode
);
736 release_orphan_inode(sbi
);
738 f2fs_balance_fs(sbi
, true);
742 err
= f2fs_add_link(new_dentry
, old_inode
);
749 f2fs_i_links_write(new_dir
, true);
752 * old entry and new entry can locate in the same inline
753 * dentry in inode, when attaching new entry in inline dentry,
754 * it could force inline dentry conversion, after that,
755 * old_entry and old_page will point to wrong address, in
756 * order to avoid this, let's do the check and update here.
758 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
759 f2fs_put_page(old_page
, 0);
762 old_entry
= f2fs_find_entry(old_dir
,
763 &old_dentry
->d_name
, &old_page
);
766 if (IS_ERR(old_page
))
767 err
= PTR_ERR(old_page
);
774 down_write(&F2FS_I(old_inode
)->i_sem
);
775 file_lost_pino(old_inode
);
776 if (new_inode
&& file_enc_name(new_inode
))
777 file_set_enc_name(old_inode
);
778 up_write(&F2FS_I(old_inode
)->i_sem
);
780 old_inode
->i_ctime
= current_time(old_inode
);
781 f2fs_mark_inode_dirty_sync(old_inode
, false);
783 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
786 whiteout
->i_state
|= I_LINKABLE
;
787 set_inode_flag(whiteout
, FI_INC_LINK
);
788 err
= f2fs_add_link(old_dentry
, whiteout
);
791 whiteout
->i_state
&= ~I_LINKABLE
;
796 if (old_dir
!= new_dir
&& !whiteout
) {
797 f2fs_set_link(old_inode
, old_dir_entry
,
798 old_dir_page
, new_dir
);
800 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
801 f2fs_put_page(old_dir_page
, 0);
803 f2fs_i_links_write(old_dir
, false);
808 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
809 f2fs_sync_fs(sbi
->sb
, 1);
815 f2fs_dentry_kunmap(new_dir
, new_page
);
816 f2fs_put_page(new_page
, 0);
823 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
824 f2fs_put_page(old_dir_page
, 0);
827 f2fs_dentry_kunmap(old_dir
, old_page
);
828 f2fs_put_page(old_page
, 0);
833 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
834 struct inode
*new_dir
, struct dentry
*new_dentry
)
836 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
837 struct inode
*old_inode
= d_inode(old_dentry
);
838 struct inode
*new_inode
= d_inode(new_dentry
);
839 struct page
*old_dir_page
, *new_dir_page
;
840 struct page
*old_page
, *new_page
;
841 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
842 struct f2fs_dir_entry
*old_entry
, *new_entry
;
843 int old_nlink
= 0, new_nlink
= 0;
846 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
847 (old_dir
!= new_dir
) &&
848 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
849 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
852 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
854 if (IS_ERR(old_page
))
855 err
= PTR_ERR(old_page
);
859 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
861 if (IS_ERR(new_page
))
862 err
= PTR_ERR(new_page
);
866 /* prepare for updating ".." directory entry info later */
867 if (old_dir
!= new_dir
) {
868 if (S_ISDIR(old_inode
->i_mode
)) {
869 old_dir_entry
= f2fs_parent_dir(old_inode
,
871 if (!old_dir_entry
) {
872 if (IS_ERR(old_dir_page
))
873 err
= PTR_ERR(old_dir_page
);
878 if (S_ISDIR(new_inode
->i_mode
)) {
879 new_dir_entry
= f2fs_parent_dir(new_inode
,
881 if (!new_dir_entry
) {
882 if (IS_ERR(new_dir_page
))
883 err
= PTR_ERR(new_dir_page
);
890 * If cross rename between file and directory those are not
891 * in the same directory, we will inc nlink of file's parent
892 * later, so we should check upper boundary of its nlink.
894 if ((!old_dir_entry
|| !new_dir_entry
) &&
895 old_dir_entry
!= new_dir_entry
) {
896 old_nlink
= old_dir_entry
? -1 : 1;
897 new_nlink
= -old_nlink
;
899 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
900 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
904 f2fs_balance_fs(sbi
, true);
908 err
= update_dent_inode(old_inode
, new_inode
, &new_dentry
->d_name
);
911 if (file_enc_name(new_inode
))
912 file_set_enc_name(old_inode
);
914 err
= update_dent_inode(new_inode
, old_inode
, &old_dentry
->d_name
);
917 if (file_enc_name(old_inode
))
918 file_set_enc_name(new_inode
);
920 /* update ".." directory entry info of old dentry */
922 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
924 /* update ".." directory entry info of new dentry */
926 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
928 /* update directory entry info of old dir inode */
929 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
931 down_write(&F2FS_I(old_inode
)->i_sem
);
932 file_lost_pino(old_inode
);
933 up_write(&F2FS_I(old_inode
)->i_sem
);
935 old_dir
->i_ctime
= current_time(old_dir
);
937 down_write(&F2FS_I(old_dir
)->i_sem
);
938 f2fs_i_links_write(old_dir
, old_nlink
> 0);
939 up_write(&F2FS_I(old_dir
)->i_sem
);
941 f2fs_mark_inode_dirty_sync(old_dir
, false);
943 /* update directory entry info of new dir inode */
944 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
946 down_write(&F2FS_I(new_inode
)->i_sem
);
947 file_lost_pino(new_inode
);
948 up_write(&F2FS_I(new_inode
)->i_sem
);
950 new_dir
->i_ctime
= current_time(new_dir
);
952 down_write(&F2FS_I(new_dir
)->i_sem
);
953 f2fs_i_links_write(new_dir
, new_nlink
> 0);
954 up_write(&F2FS_I(new_dir
)->i_sem
);
956 f2fs_mark_inode_dirty_sync(new_dir
, false);
960 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
961 f2fs_sync_fs(sbi
->sb
, 1);
965 * Still we may fail to recover name info of f2fs_inode here
966 * Drop it, once its name is set as encrypted
968 update_dent_inode(old_inode
, old_inode
, &old_dentry
->d_name
);
973 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
974 f2fs_put_page(new_dir_page
, 0);
978 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
979 f2fs_put_page(old_dir_page
, 0);
982 f2fs_dentry_kunmap(new_dir
, new_page
);
983 f2fs_put_page(new_page
, 0);
985 f2fs_dentry_kunmap(old_dir
, old_page
);
986 f2fs_put_page(old_page
, 0);
991 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
992 struct inode
*new_dir
, struct dentry
*new_dentry
,
995 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
998 if (flags
& RENAME_EXCHANGE
) {
999 return f2fs_cross_rename(old_dir
, old_dentry
,
1000 new_dir
, new_dentry
);
1003 * VFS has already handled the new dentry existence case,
1004 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1006 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1009 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1010 struct inode
*inode
,
1011 struct delayed_call
*done
)
1013 struct page
*cpage
= NULL
;
1014 char *caddr
, *paddr
= NULL
;
1015 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1016 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1017 struct fscrypt_symlink_data
*sd
;
1018 u32 max_size
= inode
->i_sb
->s_blocksize
;
1022 return ERR_PTR(-ECHILD
);
1024 res
= fscrypt_get_encryption_info(inode
);
1026 return ERR_PTR(res
);
1028 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1030 return ERR_CAST(cpage
);
1031 caddr
= page_address(cpage
);
1033 /* Symlink is encrypted */
1034 sd
= (struct fscrypt_symlink_data
*)caddr
;
1035 cstr
.name
= sd
->encrypted_path
;
1036 cstr
.len
= le16_to_cpu(sd
->len
);
1038 /* this is broken symlink case */
1039 if (unlikely(cstr
.len
== 0)) {
1044 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1045 /* Symlink data on the disk is corrupted */
1049 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1053 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1057 /* this is broken symlink case */
1058 if (unlikely(pstr
.name
[0] == 0)) {
1065 /* Null-terminate the name */
1066 paddr
[pstr
.len
] = '\0';
1069 set_delayed_call(done
, kfree_link
, paddr
);
1072 fscrypt_fname_free_buffer(&pstr
);
1074 return ERR_PTR(res
);
1077 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1078 .get_link
= f2fs_encrypted_get_link
,
1079 .getattr
= f2fs_getattr
,
1080 .setattr
= f2fs_setattr
,
1081 #ifdef CONFIG_F2FS_FS_XATTR
1082 .listxattr
= f2fs_listxattr
,
1086 const struct inode_operations f2fs_dir_inode_operations
= {
1087 .create
= f2fs_create
,
1088 .lookup
= f2fs_lookup
,
1090 .unlink
= f2fs_unlink
,
1091 .symlink
= f2fs_symlink
,
1092 .mkdir
= f2fs_mkdir
,
1093 .rmdir
= f2fs_rmdir
,
1094 .mknod
= f2fs_mknod
,
1095 .rename
= f2fs_rename2
,
1096 .tmpfile
= f2fs_tmpfile
,
1097 .getattr
= f2fs_getattr
,
1098 .setattr
= f2fs_setattr
,
1099 .get_acl
= f2fs_get_acl
,
1100 .set_acl
= f2fs_set_acl
,
1101 #ifdef CONFIG_F2FS_FS_XATTR
1102 .listxattr
= f2fs_listxattr
,
1106 const struct inode_operations f2fs_symlink_inode_operations
= {
1107 .get_link
= f2fs_get_link
,
1108 .getattr
= f2fs_getattr
,
1109 .setattr
= f2fs_setattr
,
1110 #ifdef CONFIG_F2FS_FS_XATTR
1111 .listxattr
= f2fs_listxattr
,
1115 const struct inode_operations f2fs_special_inode_operations
= {
1116 .getattr
= f2fs_getattr
,
1117 .setattr
= f2fs_setattr
,
1118 .get_acl
= f2fs_get_acl
,
1119 .set_acl
= f2fs_set_acl
,
1120 #ifdef CONFIG_F2FS_FS_XATTR
1121 .listxattr
= f2fs_listxattr
,