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;
34 inode
= new_inode(dir
->i_sb
);
36 return ERR_PTR(-ENOMEM
);
39 if (!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 inode
->i_generation
= sbi
->s_next_generation
++;
55 err
= insert_inode_locked(inode
);
61 err
= dquot_initialize(inode
);
65 err
= dquot_alloc_inode(inode
);
69 /* If the directory encrypted, then we should encrypt the inode. */
70 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
))
71 f2fs_set_encrypted_inode(inode
);
73 set_inode_flag(inode
, FI_NEW_INODE
);
75 if (test_opt(sbi
, INLINE_XATTR
))
76 set_inode_flag(inode
, FI_INLINE_XATTR
);
77 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
78 set_inode_flag(inode
, FI_INLINE_DATA
);
79 if (f2fs_may_inline_dentry(inode
))
80 set_inode_flag(inode
, FI_INLINE_DENTRY
);
82 f2fs_init_extent_tree(inode
, NULL
);
84 stat_inc_inline_xattr(inode
);
85 stat_inc_inline_inode(inode
);
86 stat_inc_inline_dir(inode
);
88 trace_f2fs_new_inode(inode
, 0);
92 trace_f2fs_new_inode(inode
, err
);
93 make_bad_inode(inode
);
95 set_inode_flag(inode
, FI_FREE_NID
);
99 trace_f2fs_new_inode(inode
, err
);
101 inode
->i_flags
|= S_NOQUOTA
;
103 set_inode_flag(inode
, FI_FREE_NID
);
105 unlock_new_inode(inode
);
110 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
112 size_t slen
= strlen(s
);
113 size_t sublen
= strlen(sub
);
117 * filename format of multimedia file should be defined as:
118 * "filename + '.' + extension + (optional: '.' + temp extension)".
120 if (slen
< sublen
+ 2)
123 for (i
= 1; i
< slen
- sublen
; i
++) {
126 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
134 * Set multimedia files as cold files for hot/cold data separation
136 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
137 const unsigned char *name
)
140 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
142 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
143 for (i
= 0; i
< count
; i
++) {
144 if (is_multimedia_file(name
, extlist
[i
])) {
145 file_set_cold(inode
);
151 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
154 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
159 err
= dquot_initialize(dir
);
163 inode
= f2fs_new_inode(dir
, mode
);
165 return PTR_ERR(inode
);
167 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
168 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
170 inode
->i_op
= &f2fs_file_inode_operations
;
171 inode
->i_fop
= &f2fs_file_operations
;
172 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
176 err
= f2fs_add_link(dentry
, inode
);
181 alloc_nid_done(sbi
, ino
);
183 d_instantiate(dentry
, inode
);
184 unlock_new_inode(inode
);
187 f2fs_sync_fs(sbi
->sb
, 1);
189 f2fs_balance_fs(sbi
, true);
192 handle_failed_inode(inode
);
196 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
197 struct dentry
*dentry
)
199 struct inode
*inode
= d_inode(old_dentry
);
200 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
203 if (f2fs_encrypted_inode(dir
) &&
204 !fscrypt_has_permitted_context(dir
, inode
))
207 err
= dquot_initialize(dir
);
211 f2fs_balance_fs(sbi
, true);
213 inode
->i_ctime
= current_time(inode
);
216 set_inode_flag(inode
, FI_INC_LINK
);
218 err
= f2fs_add_link(dentry
, inode
);
223 d_instantiate(dentry
, inode
);
226 f2fs_sync_fs(sbi
->sb
, 1);
229 clear_inode_flag(inode
, FI_INC_LINK
);
235 struct dentry
*f2fs_get_parent(struct dentry
*child
)
237 struct qstr dotdot
= QSTR_INIT("..", 2);
239 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
242 return ERR_CAST(page
);
243 return ERR_PTR(-ENOENT
);
245 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
248 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
250 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
251 struct qstr dot
= QSTR_INIT(".", 1);
252 struct qstr dotdot
= QSTR_INIT("..", 2);
253 struct f2fs_dir_entry
*de
;
257 if (f2fs_readonly(sbi
->sb
)) {
258 f2fs_msg(sbi
->sb
, KERN_INFO
,
259 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
260 "in readonly mountpoint", dir
->i_ino
, pino
);
264 f2fs_balance_fs(sbi
, true);
268 de
= f2fs_find_entry(dir
, &dot
, &page
);
270 f2fs_dentry_kunmap(dir
, page
);
271 f2fs_put_page(page
, 0);
272 } else if (IS_ERR(page
)) {
276 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
281 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
283 f2fs_dentry_kunmap(dir
, page
);
284 f2fs_put_page(page
, 0);
285 } else if (IS_ERR(page
)) {
288 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
292 clear_inode_flag(dir
, FI_INLINE_DOTS
);
298 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
301 struct inode
*inode
= NULL
;
302 struct f2fs_dir_entry
*de
;
306 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
308 if (f2fs_encrypted_inode(dir
)) {
309 int res
= fscrypt_get_encryption_info(dir
);
312 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
313 * created while the directory was encrypted and we
314 * don't have access to the key.
316 if (fscrypt_has_encryption_key(dir
))
317 fscrypt_set_encrypted_dentry(dentry
);
318 fscrypt_set_d_op(dentry
);
319 if (res
&& res
!= -ENOKEY
)
323 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
324 return ERR_PTR(-ENAMETOOLONG
);
326 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
329 return (struct dentry
*)page
;
330 return d_splice_alias(inode
, dentry
);
333 ino
= le32_to_cpu(de
->ino
);
334 f2fs_dentry_kunmap(dir
, page
);
335 f2fs_put_page(page
, 0);
337 inode
= f2fs_iget(dir
->i_sb
, ino
);
339 return ERR_CAST(inode
);
341 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
342 err
= __recover_dot_dentries(dir
, root_ino
);
347 if (f2fs_has_inline_dots(inode
)) {
348 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
352 if (f2fs_encrypted_inode(dir
) &&
353 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
354 !fscrypt_has_permitted_context(dir
, inode
)) {
355 f2fs_msg(inode
->i_sb
, KERN_WARNING
,
356 "Inconsistent encryption contexts: %lu/%lu",
357 dir
->i_ino
, inode
->i_ino
);
361 return d_splice_alias(inode
, dentry
);
368 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
370 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
371 struct inode
*inode
= d_inode(dentry
);
372 struct f2fs_dir_entry
*de
;
376 trace_f2fs_unlink_enter(dir
, dentry
);
378 err
= dquot_initialize(dir
);
382 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
389 f2fs_balance_fs(sbi
, true);
392 err
= acquire_orphan_inode(sbi
);
395 f2fs_dentry_kunmap(dir
, page
);
396 f2fs_put_page(page
, 0);
399 f2fs_delete_entry(de
, page
, dir
, inode
);
403 f2fs_sync_fs(sbi
->sb
, 1);
405 trace_f2fs_unlink_exit(inode
, err
);
409 static const char *f2fs_get_link(struct dentry
*dentry
,
411 struct delayed_call
*done
)
413 const char *link
= page_get_link(dentry
, inode
, done
);
414 if (!IS_ERR(link
) && !*link
) {
415 /* this is broken symlink case */
416 do_delayed_call(done
);
417 clear_delayed_call(done
);
418 link
= ERR_PTR(-ENOENT
);
423 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
426 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
428 size_t len
= strlen(symname
);
429 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
430 struct fscrypt_symlink_data
*sd
= NULL
;
433 if (f2fs_encrypted_inode(dir
)) {
434 err
= fscrypt_get_encryption_info(dir
);
438 if (!fscrypt_has_encryption_key(dir
))
441 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
442 sizeof(struct fscrypt_symlink_data
));
445 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
446 return -ENAMETOOLONG
;
448 err
= dquot_initialize(dir
);
452 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
454 return PTR_ERR(inode
);
456 if (f2fs_encrypted_inode(inode
))
457 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
459 inode
->i_op
= &f2fs_symlink_inode_operations
;
460 inode_nohighmem(inode
);
461 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
464 err
= f2fs_add_link(dentry
, inode
);
468 alloc_nid_done(sbi
, inode
->i_ino
);
470 if (f2fs_encrypted_inode(inode
)) {
471 struct qstr istr
= QSTR_INIT(symname
, len
);
472 struct fscrypt_str ostr
;
474 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
480 err
= fscrypt_get_encryption_info(inode
);
484 if (!fscrypt_has_encryption_key(inode
)) {
489 ostr
.name
= sd
->encrypted_path
;
490 ostr
.len
= disk_link
.len
;
491 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
495 sd
->len
= cpu_to_le16(ostr
.len
);
496 disk_link
.name
= (char *)sd
;
499 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
502 d_instantiate(dentry
, inode
);
503 unlock_new_inode(inode
);
506 * Let's flush symlink data in order to avoid broken symlink as much as
507 * possible. Nevertheless, fsyncing is the best way, but there is no
508 * way to get a file descriptor in order to flush that.
510 * Note that, it needs to do dir->fsync to make this recoverable.
511 * If the symlink path is stored into inline_data, there is no
512 * performance regression.
515 filemap_write_and_wait_range(inode
->i_mapping
, 0,
519 f2fs_sync_fs(sbi
->sb
, 1);
521 f2fs_unlink(dir
, dentry
);
526 f2fs_balance_fs(sbi
, true);
529 handle_failed_inode(inode
);
533 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
535 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
539 err
= dquot_initialize(dir
);
543 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
545 return PTR_ERR(inode
);
547 inode
->i_op
= &f2fs_dir_inode_operations
;
548 inode
->i_fop
= &f2fs_dir_operations
;
549 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
550 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
552 set_inode_flag(inode
, FI_INC_LINK
);
554 err
= f2fs_add_link(dentry
, inode
);
559 alloc_nid_done(sbi
, inode
->i_ino
);
561 d_instantiate(dentry
, inode
);
562 unlock_new_inode(inode
);
565 f2fs_sync_fs(sbi
->sb
, 1);
567 f2fs_balance_fs(sbi
, true);
571 clear_inode_flag(inode
, FI_INC_LINK
);
572 handle_failed_inode(inode
);
576 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
578 struct inode
*inode
= d_inode(dentry
);
579 if (f2fs_empty_dir(inode
))
580 return f2fs_unlink(dir
, dentry
);
584 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
585 umode_t mode
, dev_t rdev
)
587 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
591 err
= dquot_initialize(dir
);
595 inode
= f2fs_new_inode(dir
, mode
);
597 return PTR_ERR(inode
);
599 init_special_inode(inode
, inode
->i_mode
, rdev
);
600 inode
->i_op
= &f2fs_special_inode_operations
;
603 err
= f2fs_add_link(dentry
, inode
);
608 alloc_nid_done(sbi
, inode
->i_ino
);
610 d_instantiate(dentry
, inode
);
611 unlock_new_inode(inode
);
614 f2fs_sync_fs(sbi
->sb
, 1);
616 f2fs_balance_fs(sbi
, true);
619 handle_failed_inode(inode
);
623 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
624 umode_t mode
, struct inode
**whiteout
)
626 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
630 err
= dquot_initialize(dir
);
634 inode
= f2fs_new_inode(dir
, mode
);
636 return PTR_ERR(inode
);
639 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
640 inode
->i_op
= &f2fs_special_inode_operations
;
642 inode
->i_op
= &f2fs_file_inode_operations
;
643 inode
->i_fop
= &f2fs_file_operations
;
644 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
648 err
= acquire_orphan_inode(sbi
);
652 err
= f2fs_do_tmpfile(inode
, dir
);
657 * add this non-linked tmpfile to orphan list, in this way we could
658 * remove all unused data of tmpfile after abnormal power-off.
660 add_orphan_inode(inode
);
661 alloc_nid_done(sbi
, inode
->i_ino
);
664 f2fs_i_links_write(inode
, false);
667 d_tmpfile(dentry
, inode
);
669 /* link_count was changed by d_tmpfile as well. */
671 unlock_new_inode(inode
);
673 f2fs_balance_fs(sbi
, true);
677 release_orphan_inode(sbi
);
679 handle_failed_inode(inode
);
683 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
685 if (f2fs_encrypted_inode(dir
)) {
686 int err
= fscrypt_get_encryption_info(dir
);
691 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
694 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
696 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
699 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
700 struct inode
*new_dir
, struct dentry
*new_dentry
,
703 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
704 struct inode
*old_inode
= d_inode(old_dentry
);
705 struct inode
*new_inode
= d_inode(new_dentry
);
706 struct inode
*whiteout
= NULL
;
707 struct page
*old_dir_page
;
708 struct page
*old_page
, *new_page
= NULL
;
709 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
710 struct f2fs_dir_entry
*old_entry
;
711 struct f2fs_dir_entry
*new_entry
;
712 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
715 if ((f2fs_encrypted_inode(old_dir
) &&
716 !fscrypt_has_encryption_key(old_dir
)) ||
717 (f2fs_encrypted_inode(new_dir
) &&
718 !fscrypt_has_encryption_key(new_dir
)))
721 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
722 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
727 err
= dquot_initialize(old_dir
);
731 err
= dquot_initialize(new_dir
);
735 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
737 if (IS_ERR(old_page
))
738 err
= PTR_ERR(old_page
);
742 if (S_ISDIR(old_inode
->i_mode
)) {
743 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
744 if (!old_dir_entry
) {
745 if (IS_ERR(old_dir_page
))
746 err
= PTR_ERR(old_dir_page
);
751 if (flags
& RENAME_WHITEOUT
) {
752 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
760 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
764 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
767 if (IS_ERR(new_page
))
768 err
= PTR_ERR(new_page
);
772 f2fs_balance_fs(sbi
, true);
776 err
= acquire_orphan_inode(sbi
);
780 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
782 new_inode
->i_ctime
= current_time(new_inode
);
783 down_write(&F2FS_I(new_inode
)->i_sem
);
785 f2fs_i_links_write(new_inode
, false);
786 f2fs_i_links_write(new_inode
, false);
787 up_write(&F2FS_I(new_inode
)->i_sem
);
789 if (!new_inode
->i_nlink
)
790 add_orphan_inode(new_inode
);
792 release_orphan_inode(sbi
);
794 f2fs_balance_fs(sbi
, true);
798 err
= f2fs_add_link(new_dentry
, old_inode
);
805 f2fs_i_links_write(new_dir
, true);
808 * old entry and new entry can locate in the same inline
809 * dentry in inode, when attaching new entry in inline dentry,
810 * it could force inline dentry conversion, after that,
811 * old_entry and old_page will point to wrong address, in
812 * order to avoid this, let's do the check and update here.
814 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
815 f2fs_put_page(old_page
, 0);
818 old_entry
= f2fs_find_entry(old_dir
,
819 &old_dentry
->d_name
, &old_page
);
822 if (IS_ERR(old_page
))
823 err
= PTR_ERR(old_page
);
830 down_write(&F2FS_I(old_inode
)->i_sem
);
831 if (!old_dir_entry
|| whiteout
)
832 file_lost_pino(old_inode
);
834 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
835 up_write(&F2FS_I(old_inode
)->i_sem
);
837 old_inode
->i_ctime
= current_time(old_inode
);
838 f2fs_mark_inode_dirty_sync(old_inode
, false);
840 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
843 whiteout
->i_state
|= I_LINKABLE
;
844 set_inode_flag(whiteout
, FI_INC_LINK
);
845 err
= f2fs_add_link(old_dentry
, whiteout
);
848 whiteout
->i_state
&= ~I_LINKABLE
;
853 if (old_dir
!= new_dir
&& !whiteout
) {
854 f2fs_set_link(old_inode
, old_dir_entry
,
855 old_dir_page
, new_dir
);
857 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
858 f2fs_put_page(old_dir_page
, 0);
860 f2fs_i_links_write(old_dir
, false);
865 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
866 f2fs_sync_fs(sbi
->sb
, 1);
872 f2fs_dentry_kunmap(new_dir
, new_page
);
873 f2fs_put_page(new_page
, 0);
880 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
881 f2fs_put_page(old_dir_page
, 0);
884 f2fs_dentry_kunmap(old_dir
, old_page
);
885 f2fs_put_page(old_page
, 0);
890 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
891 struct inode
*new_dir
, struct dentry
*new_dentry
)
893 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
894 struct inode
*old_inode
= d_inode(old_dentry
);
895 struct inode
*new_inode
= d_inode(new_dentry
);
896 struct page
*old_dir_page
, *new_dir_page
;
897 struct page
*old_page
, *new_page
;
898 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
899 struct f2fs_dir_entry
*old_entry
, *new_entry
;
900 int old_nlink
= 0, new_nlink
= 0;
903 if ((f2fs_encrypted_inode(old_dir
) &&
904 !fscrypt_has_encryption_key(old_dir
)) ||
905 (f2fs_encrypted_inode(new_dir
) &&
906 !fscrypt_has_encryption_key(new_dir
)))
909 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
910 (old_dir
!= new_dir
) &&
911 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
912 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
915 err
= dquot_initialize(old_dir
);
919 err
= dquot_initialize(new_dir
);
923 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
925 if (IS_ERR(old_page
))
926 err
= PTR_ERR(old_page
);
930 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
932 if (IS_ERR(new_page
))
933 err
= PTR_ERR(new_page
);
937 /* prepare for updating ".." directory entry info later */
938 if (old_dir
!= new_dir
) {
939 if (S_ISDIR(old_inode
->i_mode
)) {
940 old_dir_entry
= f2fs_parent_dir(old_inode
,
942 if (!old_dir_entry
) {
943 if (IS_ERR(old_dir_page
))
944 err
= PTR_ERR(old_dir_page
);
949 if (S_ISDIR(new_inode
->i_mode
)) {
950 new_dir_entry
= f2fs_parent_dir(new_inode
,
952 if (!new_dir_entry
) {
953 if (IS_ERR(new_dir_page
))
954 err
= PTR_ERR(new_dir_page
);
961 * If cross rename between file and directory those are not
962 * in the same directory, we will inc nlink of file's parent
963 * later, so we should check upper boundary of its nlink.
965 if ((!old_dir_entry
|| !new_dir_entry
) &&
966 old_dir_entry
!= new_dir_entry
) {
967 old_nlink
= old_dir_entry
? -1 : 1;
968 new_nlink
= -old_nlink
;
970 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
971 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
975 f2fs_balance_fs(sbi
, true);
979 /* update ".." directory entry info of old dentry */
981 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
983 /* update ".." directory entry info of new dentry */
985 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
987 /* update directory entry info of old dir inode */
988 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
990 down_write(&F2FS_I(old_inode
)->i_sem
);
991 file_lost_pino(old_inode
);
992 up_write(&F2FS_I(old_inode
)->i_sem
);
994 old_dir
->i_ctime
= current_time(old_dir
);
996 down_write(&F2FS_I(old_dir
)->i_sem
);
997 f2fs_i_links_write(old_dir
, old_nlink
> 0);
998 up_write(&F2FS_I(old_dir
)->i_sem
);
1000 f2fs_mark_inode_dirty_sync(old_dir
, false);
1002 /* update directory entry info of new dir inode */
1003 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1005 down_write(&F2FS_I(new_inode
)->i_sem
);
1006 file_lost_pino(new_inode
);
1007 up_write(&F2FS_I(new_inode
)->i_sem
);
1009 new_dir
->i_ctime
= current_time(new_dir
);
1011 down_write(&F2FS_I(new_dir
)->i_sem
);
1012 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1013 up_write(&F2FS_I(new_dir
)->i_sem
);
1015 f2fs_mark_inode_dirty_sync(new_dir
, false);
1017 f2fs_unlock_op(sbi
);
1019 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1020 f2fs_sync_fs(sbi
->sb
, 1);
1023 if (new_dir_entry
) {
1024 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
1025 f2fs_put_page(new_dir_page
, 0);
1028 if (old_dir_entry
) {
1029 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
1030 f2fs_put_page(old_dir_page
, 0);
1033 f2fs_dentry_kunmap(new_dir
, new_page
);
1034 f2fs_put_page(new_page
, 0);
1036 f2fs_dentry_kunmap(old_dir
, old_page
);
1037 f2fs_put_page(old_page
, 0);
1042 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1043 struct inode
*new_dir
, struct dentry
*new_dentry
,
1046 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1049 if (flags
& RENAME_EXCHANGE
) {
1050 return f2fs_cross_rename(old_dir
, old_dentry
,
1051 new_dir
, new_dentry
);
1054 * VFS has already handled the new dentry existence case,
1055 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1057 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1060 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1061 struct inode
*inode
,
1062 struct delayed_call
*done
)
1064 struct page
*cpage
= NULL
;
1065 char *caddr
, *paddr
= NULL
;
1066 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1067 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1068 struct fscrypt_symlink_data
*sd
;
1069 u32 max_size
= inode
->i_sb
->s_blocksize
;
1073 return ERR_PTR(-ECHILD
);
1075 res
= fscrypt_get_encryption_info(inode
);
1077 return ERR_PTR(res
);
1079 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1081 return ERR_CAST(cpage
);
1082 caddr
= page_address(cpage
);
1084 /* Symlink is encrypted */
1085 sd
= (struct fscrypt_symlink_data
*)caddr
;
1086 cstr
.name
= sd
->encrypted_path
;
1087 cstr
.len
= le16_to_cpu(sd
->len
);
1089 /* this is broken symlink case */
1090 if (unlikely(cstr
.len
== 0)) {
1095 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1096 /* Symlink data on the disk is corrupted */
1100 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1104 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1108 /* this is broken symlink case */
1109 if (unlikely(pstr
.name
[0] == 0)) {
1116 /* Null-terminate the name */
1117 paddr
[pstr
.len
] = '\0';
1120 set_delayed_call(done
, kfree_link
, paddr
);
1123 fscrypt_fname_free_buffer(&pstr
);
1125 return ERR_PTR(res
);
1128 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1129 .get_link
= f2fs_encrypted_get_link
,
1130 .getattr
= f2fs_getattr
,
1131 .setattr
= f2fs_setattr
,
1132 #ifdef CONFIG_F2FS_FS_XATTR
1133 .listxattr
= f2fs_listxattr
,
1137 const struct inode_operations f2fs_dir_inode_operations
= {
1138 .create
= f2fs_create
,
1139 .lookup
= f2fs_lookup
,
1141 .unlink
= f2fs_unlink
,
1142 .symlink
= f2fs_symlink
,
1143 .mkdir
= f2fs_mkdir
,
1144 .rmdir
= f2fs_rmdir
,
1145 .mknod
= f2fs_mknod
,
1146 .rename
= f2fs_rename2
,
1147 .tmpfile
= f2fs_tmpfile
,
1148 .getattr
= f2fs_getattr
,
1149 .setattr
= f2fs_setattr
,
1150 .get_acl
= f2fs_get_acl
,
1151 .set_acl
= f2fs_set_acl
,
1152 #ifdef CONFIG_F2FS_FS_XATTR
1153 .listxattr
= f2fs_listxattr
,
1157 const struct inode_operations f2fs_symlink_inode_operations
= {
1158 .get_link
= f2fs_get_link
,
1159 .getattr
= f2fs_getattr
,
1160 .setattr
= f2fs_setattr
,
1161 #ifdef CONFIG_F2FS_FS_XATTR
1162 .listxattr
= f2fs_listxattr
,
1166 const struct inode_operations f2fs_special_inode_operations
= {
1167 .getattr
= f2fs_getattr
,
1168 .setattr
= f2fs_setattr
,
1169 .get_acl
= f2fs_get_acl
,
1170 .set_acl
= f2fs_set_acl
,
1171 #ifdef CONFIG_F2FS_FS_XATTR
1172 .listxattr
= f2fs_listxattr
,