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
;
50 inode
->i_generation
= sbi
->s_next_generation
++;
52 err
= insert_inode_locked(inode
);
59 if (f2fs_may_inline(inode
))
60 set_inode_flag(F2FS_I(inode
), FI_INLINE_DATA
);
61 if (test_opt(sbi
, INLINE_DENTRY
) && S_ISDIR(inode
->i_mode
))
62 set_inode_flag(F2FS_I(inode
), FI_INLINE_DENTRY
);
64 trace_f2fs_new_inode(inode
, 0);
65 mark_inode_dirty(inode
);
70 unlock_new_inode(inode
);
72 trace_f2fs_new_inode(inode
, err
);
73 make_bad_inode(inode
);
76 alloc_nid_failed(sbi
, ino
);
80 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
82 size_t slen
= strlen(s
);
83 size_t sublen
= strlen(sub
);
88 return !strncasecmp(s
+ slen
- sublen
, sub
, sublen
);
92 * Set multimedia files as cold files for hot/cold data separation
94 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
95 const unsigned char *name
)
98 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
100 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
101 for (i
= 0; i
< count
; i
++) {
102 if (is_multimedia_file(name
, extlist
[i
])) {
103 file_set_cold(inode
);
109 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
112 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
117 f2fs_balance_fs(sbi
);
119 inode
= f2fs_new_inode(dir
, mode
);
121 return PTR_ERR(inode
);
123 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
124 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
126 inode
->i_op
= &f2fs_file_inode_operations
;
127 inode
->i_fop
= &f2fs_file_operations
;
128 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
132 err
= f2fs_add_link(dentry
, inode
);
137 alloc_nid_done(sbi
, ino
);
139 stat_inc_inline_inode(inode
);
140 d_instantiate(dentry
, inode
);
141 unlock_new_inode(inode
);
144 f2fs_sync_fs(sbi
->sb
, 1);
147 handle_failed_inode(inode
);
151 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
152 struct dentry
*dentry
)
154 struct inode
*inode
= d_inode(old_dentry
);
155 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
158 f2fs_balance_fs(sbi
);
160 inode
->i_ctime
= CURRENT_TIME
;
163 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
165 err
= f2fs_add_link(dentry
, inode
);
170 d_instantiate(dentry
, inode
);
173 f2fs_sync_fs(sbi
->sb
, 1);
176 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
182 struct dentry
*f2fs_get_parent(struct dentry
*child
)
184 struct qstr dotdot
= QSTR_INIT("..", 2);
185 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
);
187 return ERR_PTR(-ENOENT
);
188 return d_obtain_alias(f2fs_iget(d_inode(child
)->i_sb
, ino
));
191 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
193 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
194 struct qstr dot
= QSTR_INIT(".", 1);
195 struct qstr dotdot
= QSTR_INIT("..", 2);
196 struct f2fs_dir_entry
*de
;
202 de
= f2fs_find_entry(dir
, &dot
, &page
);
204 f2fs_dentry_kunmap(dir
, page
);
205 f2fs_put_page(page
, 0);
207 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
212 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
214 f2fs_dentry_kunmap(dir
, page
);
215 f2fs_put_page(page
, 0);
217 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
221 clear_inode_flag(F2FS_I(dir
), FI_INLINE_DOTS
);
222 mark_inode_dirty(dir
);
229 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
232 struct inode
*inode
= NULL
;
233 struct f2fs_dir_entry
*de
;
236 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
237 return ERR_PTR(-ENAMETOOLONG
);
239 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
241 nid_t ino
= le32_to_cpu(de
->ino
);
242 f2fs_dentry_kunmap(dir
, page
);
243 f2fs_put_page(page
, 0);
245 inode
= f2fs_iget(dir
->i_sb
, ino
);
247 return ERR_CAST(inode
);
249 if (f2fs_has_inline_dots(inode
)) {
252 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
260 return d_splice_alias(inode
, dentry
);
263 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
265 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
266 struct inode
*inode
= d_inode(dentry
);
267 struct f2fs_dir_entry
*de
;
271 trace_f2fs_unlink_enter(dir
, dentry
);
272 f2fs_balance_fs(sbi
);
274 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
279 err
= acquire_orphan_inode(sbi
);
282 f2fs_dentry_kunmap(dir
, page
);
283 f2fs_put_page(page
, 0);
286 f2fs_delete_entry(de
, page
, dir
, inode
);
289 /* In order to evict this inode, we set it dirty */
290 mark_inode_dirty(inode
);
293 f2fs_sync_fs(sbi
->sb
, 1);
295 trace_f2fs_unlink_exit(inode
, err
);
299 static void *f2fs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
301 struct page
*page
= page_follow_link_light(dentry
, nd
);
303 if (IS_ERR_OR_NULL(page
))
306 /* this is broken symlink case */
307 if (*nd_get_link(nd
) == 0) {
308 page_put_link(dentry
, nd
, page
);
309 return ERR_PTR(-ENOENT
);
314 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
317 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
319 size_t symlen
= strlen(symname
) + 1;
322 f2fs_balance_fs(sbi
);
324 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
326 return PTR_ERR(inode
);
328 inode
->i_op
= &f2fs_symlink_inode_operations
;
329 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
332 err
= f2fs_add_link(dentry
, inode
);
337 err
= page_symlink(inode
, symname
, symlen
);
338 alloc_nid_done(sbi
, inode
->i_ino
);
340 d_instantiate(dentry
, inode
);
341 unlock_new_inode(inode
);
344 * Let's flush symlink data in order to avoid broken symlink as much as
345 * possible. Nevertheless, fsyncing is the best way, but there is no
346 * way to get a file descriptor in order to flush that.
348 * Note that, it needs to do dir->fsync to make this recoverable.
349 * If the symlink path is stored into inline_data, there is no
350 * performance regression.
352 filemap_write_and_wait_range(inode
->i_mapping
, 0, symlen
- 1);
355 f2fs_sync_fs(sbi
->sb
, 1);
358 handle_failed_inode(inode
);
362 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
364 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
368 f2fs_balance_fs(sbi
);
370 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
372 return PTR_ERR(inode
);
374 inode
->i_op
= &f2fs_dir_inode_operations
;
375 inode
->i_fop
= &f2fs_dir_operations
;
376 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
377 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
379 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
381 err
= f2fs_add_link(dentry
, inode
);
386 stat_inc_inline_dir(inode
);
387 alloc_nid_done(sbi
, inode
->i_ino
);
389 d_instantiate(dentry
, inode
);
390 unlock_new_inode(inode
);
393 f2fs_sync_fs(sbi
->sb
, 1);
397 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
398 handle_failed_inode(inode
);
402 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
404 struct inode
*inode
= d_inode(dentry
);
405 if (f2fs_empty_dir(inode
))
406 return f2fs_unlink(dir
, dentry
);
410 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
411 umode_t mode
, dev_t rdev
)
413 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
417 if (!new_valid_dev(rdev
))
420 f2fs_balance_fs(sbi
);
422 inode
= f2fs_new_inode(dir
, mode
);
424 return PTR_ERR(inode
);
426 init_special_inode(inode
, inode
->i_mode
, rdev
);
427 inode
->i_op
= &f2fs_special_inode_operations
;
430 err
= f2fs_add_link(dentry
, inode
);
435 alloc_nid_done(sbi
, inode
->i_ino
);
437 d_instantiate(dentry
, inode
);
438 unlock_new_inode(inode
);
441 f2fs_sync_fs(sbi
->sb
, 1);
444 handle_failed_inode(inode
);
448 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
449 struct inode
*new_dir
, struct dentry
*new_dentry
)
451 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
452 struct inode
*old_inode
= d_inode(old_dentry
);
453 struct inode
*new_inode
= d_inode(new_dentry
);
454 struct page
*old_dir_page
;
455 struct page
*old_page
, *new_page
;
456 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
457 struct f2fs_dir_entry
*old_entry
;
458 struct f2fs_dir_entry
*new_entry
;
461 f2fs_balance_fs(sbi
);
463 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
467 if (S_ISDIR(old_inode
->i_mode
)) {
469 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
477 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
481 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
488 err
= acquire_orphan_inode(sbi
);
492 if (update_dent_inode(old_inode
, &new_dentry
->d_name
)) {
493 release_orphan_inode(sbi
);
497 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
499 new_inode
->i_ctime
= CURRENT_TIME
;
500 down_write(&F2FS_I(new_inode
)->i_sem
);
502 drop_nlink(new_inode
);
503 drop_nlink(new_inode
);
504 up_write(&F2FS_I(new_inode
)->i_sem
);
506 mark_inode_dirty(new_inode
);
508 if (!new_inode
->i_nlink
)
509 add_orphan_inode(sbi
, new_inode
->i_ino
);
511 release_orphan_inode(sbi
);
513 update_inode_page(old_inode
);
514 update_inode_page(new_inode
);
518 err
= f2fs_add_link(new_dentry
, old_inode
);
526 update_inode_page(new_dir
);
530 down_write(&F2FS_I(old_inode
)->i_sem
);
531 file_lost_pino(old_inode
);
532 up_write(&F2FS_I(old_inode
)->i_sem
);
534 old_inode
->i_ctime
= CURRENT_TIME
;
535 mark_inode_dirty(old_inode
);
537 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
540 if (old_dir
!= new_dir
) {
541 f2fs_set_link(old_inode
, old_dir_entry
,
542 old_dir_page
, new_dir
);
543 update_inode_page(old_inode
);
545 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
546 f2fs_put_page(old_dir_page
, 0);
549 mark_inode_dirty(old_dir
);
550 update_inode_page(old_dir
);
555 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
556 f2fs_sync_fs(sbi
->sb
, 1);
561 f2fs_dentry_kunmap(new_dir
, new_page
);
562 f2fs_put_page(new_page
, 0);
565 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
566 f2fs_put_page(old_dir_page
, 0);
569 f2fs_dentry_kunmap(old_dir
, old_page
);
570 f2fs_put_page(old_page
, 0);
575 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
576 struct inode
*new_dir
, struct dentry
*new_dentry
)
578 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
579 struct inode
*old_inode
= d_inode(old_dentry
);
580 struct inode
*new_inode
= d_inode(new_dentry
);
581 struct page
*old_dir_page
, *new_dir_page
;
582 struct page
*old_page
, *new_page
;
583 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
584 struct f2fs_dir_entry
*old_entry
, *new_entry
;
585 int old_nlink
= 0, new_nlink
= 0;
588 f2fs_balance_fs(sbi
);
590 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
594 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
598 /* prepare for updating ".." directory entry info later */
599 if (old_dir
!= new_dir
) {
600 if (S_ISDIR(old_inode
->i_mode
)) {
602 old_dir_entry
= f2fs_parent_dir(old_inode
,
608 if (S_ISDIR(new_inode
->i_mode
)) {
610 new_dir_entry
= f2fs_parent_dir(new_inode
,
618 * If cross rename between file and directory those are not
619 * in the same directory, we will inc nlink of file's parent
620 * later, so we should check upper boundary of its nlink.
622 if ((!old_dir_entry
|| !new_dir_entry
) &&
623 old_dir_entry
!= new_dir_entry
) {
624 old_nlink
= old_dir_entry
? -1 : 1;
625 new_nlink
= -old_nlink
;
627 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
628 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
634 err
= update_dent_inode(old_inode
, &new_dentry
->d_name
);
638 err
= update_dent_inode(new_inode
, &old_dentry
->d_name
);
642 /* update ".." directory entry info of old dentry */
644 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
646 /* update ".." directory entry info of new dentry */
648 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
650 /* update directory entry info of old dir inode */
651 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
653 down_write(&F2FS_I(old_inode
)->i_sem
);
654 file_lost_pino(old_inode
);
655 up_write(&F2FS_I(old_inode
)->i_sem
);
657 update_inode_page(old_inode
);
659 old_dir
->i_ctime
= CURRENT_TIME
;
661 down_write(&F2FS_I(old_dir
)->i_sem
);
666 up_write(&F2FS_I(old_dir
)->i_sem
);
668 mark_inode_dirty(old_dir
);
669 update_inode_page(old_dir
);
671 /* update directory entry info of new dir inode */
672 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
674 down_write(&F2FS_I(new_inode
)->i_sem
);
675 file_lost_pino(new_inode
);
676 up_write(&F2FS_I(new_inode
)->i_sem
);
678 update_inode_page(new_inode
);
680 new_dir
->i_ctime
= CURRENT_TIME
;
682 down_write(&F2FS_I(new_dir
)->i_sem
);
687 up_write(&F2FS_I(new_dir
)->i_sem
);
689 mark_inode_dirty(new_dir
);
690 update_inode_page(new_dir
);
694 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
695 f2fs_sync_fs(sbi
->sb
, 1);
698 /* Still we may fail to recover name info of f2fs_inode here */
699 update_dent_inode(old_inode
, &old_dentry
->d_name
);
704 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
705 f2fs_put_page(new_dir_page
, 0);
709 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
710 f2fs_put_page(old_dir_page
, 0);
713 f2fs_dentry_kunmap(new_dir
, new_page
);
714 f2fs_put_page(new_page
, 0);
716 f2fs_dentry_kunmap(old_dir
, old_page
);
717 f2fs_put_page(old_page
, 0);
722 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
723 struct inode
*new_dir
, struct dentry
*new_dentry
,
726 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
))
729 if (flags
& RENAME_EXCHANGE
) {
730 return f2fs_cross_rename(old_dir
, old_dentry
,
731 new_dir
, new_dentry
);
734 * VFS has already handled the new dentry existence case,
735 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
737 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
740 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
742 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
746 inode
= f2fs_new_inode(dir
, mode
);
748 return PTR_ERR(inode
);
750 inode
->i_op
= &f2fs_file_inode_operations
;
751 inode
->i_fop
= &f2fs_file_operations
;
752 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
755 err
= acquire_orphan_inode(sbi
);
759 err
= f2fs_do_tmpfile(inode
, dir
);
764 * add this non-linked tmpfile to orphan list, in this way we could
765 * remove all unused data of tmpfile after abnormal power-off.
767 add_orphan_inode(sbi
, inode
->i_ino
);
770 alloc_nid_done(sbi
, inode
->i_ino
);
772 stat_inc_inline_inode(inode
);
773 d_tmpfile(dentry
, inode
);
774 unlock_new_inode(inode
);
778 release_orphan_inode(sbi
);
780 handle_failed_inode(inode
);
784 const struct inode_operations f2fs_dir_inode_operations
= {
785 .create
= f2fs_create
,
786 .lookup
= f2fs_lookup
,
788 .unlink
= f2fs_unlink
,
789 .symlink
= f2fs_symlink
,
793 .rename2
= f2fs_rename2
,
794 .tmpfile
= f2fs_tmpfile
,
795 .getattr
= f2fs_getattr
,
796 .setattr
= f2fs_setattr
,
797 .get_acl
= f2fs_get_acl
,
798 .set_acl
= f2fs_set_acl
,
799 #ifdef CONFIG_F2FS_FS_XATTR
800 .setxattr
= generic_setxattr
,
801 .getxattr
= generic_getxattr
,
802 .listxattr
= f2fs_listxattr
,
803 .removexattr
= generic_removexattr
,
807 const struct inode_operations f2fs_symlink_inode_operations
= {
808 .readlink
= generic_readlink
,
809 .follow_link
= f2fs_follow_link
,
810 .put_link
= page_put_link
,
811 .getattr
= f2fs_getattr
,
812 .setattr
= f2fs_setattr
,
813 #ifdef CONFIG_F2FS_FS_XATTR
814 .setxattr
= generic_setxattr
,
815 .getxattr
= generic_getxattr
,
816 .listxattr
= f2fs_listxattr
,
817 .removexattr
= generic_removexattr
,
821 const struct inode_operations f2fs_special_inode_operations
= {
822 .getattr
= f2fs_getattr
,
823 .setattr
= f2fs_setattr
,
824 .get_acl
= f2fs_get_acl
,
825 .set_acl
= f2fs_set_acl
,
826 #ifdef CONFIG_F2FS_FS_XATTR
827 .setxattr
= generic_setxattr
,
828 .getxattr
= generic_getxattr
,
829 .listxattr
= f2fs_listxattr
,
830 .removexattr
= generic_removexattr
,