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>
22 #include <trace/events/f2fs.h>
24 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
26 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
29 bool nid_free = false;
32 inode = new_inode(dir->i_sb);
34 return ERR_PTR(-ENOMEM);
37 if (!alloc_nid(sbi, &ino)) {
44 inode_init_owner(inode, dir, mode);
48 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
49 inode->i_generation = sbi->s_next_generation++;
51 err = insert_inode_locked(inode);
58 if (f2fs_may_inline(inode))
59 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
60 if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode))
61 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
63 trace_f2fs_new_inode(inode, 0);
64 mark_inode_dirty(inode);
69 unlock_new_inode(inode);
71 trace_f2fs_new_inode(inode, err);
72 make_bad_inode(inode);
75 alloc_nid_failed(sbi, ino);
79 static int is_multimedia_file(const unsigned char *s, const char *sub)
81 size_t slen = strlen(s);
82 size_t sublen = strlen(sub);
87 return !strncasecmp(s + slen - sublen, sub, sublen);
91 * Set multimedia files as cold files for hot/cold data separation
93 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
94 const unsigned char *name)
97 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
99 int count = le32_to_cpu(sbi->raw_super->extension_count);
100 for (i = 0; i < count; i++) {
101 if (is_multimedia_file(name, extlist[i])) {
102 file_set_cold(inode);
108 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
111 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
116 f2fs_balance_fs(sbi);
118 inode = f2fs_new_inode(dir, mode);
120 return PTR_ERR(inode);
122 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
123 set_cold_files(sbi, inode, dentry->d_name.name);
125 inode->i_op = &f2fs_file_inode_operations;
126 inode->i_fop = &f2fs_file_operations;
127 inode->i_mapping->a_ops = &f2fs_dblock_aops;
131 err = f2fs_add_link(dentry, inode);
136 alloc_nid_done(sbi, ino);
138 stat_inc_inline_inode(inode);
139 d_instantiate(dentry, inode);
140 unlock_new_inode(inode);
143 f2fs_sync_fs(sbi->sb, 1);
146 handle_failed_inode(inode);
150 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
151 struct dentry *dentry)
153 struct inode *inode = old_dentry->d_inode;
154 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
157 f2fs_balance_fs(sbi);
159 inode->i_ctime = CURRENT_TIME;
162 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
164 err = f2fs_add_link(dentry, inode);
169 d_instantiate(dentry, inode);
172 f2fs_sync_fs(sbi->sb, 1);
175 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
181 struct dentry *f2fs_get_parent(struct dentry *child)
183 struct qstr dotdot = QSTR_INIT("..", 2);
184 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
186 return ERR_PTR(-ENOENT);
187 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
190 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
192 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
193 struct qstr dot = QSTR_INIT(".", 1);
194 struct qstr dotdot = QSTR_INIT("..", 2);
195 struct f2fs_dir_entry *de;
201 de = f2fs_find_entry(dir, &dot, &page);
203 f2fs_dentry_kunmap(dir, page);
204 f2fs_put_page(page, 0);
206 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
211 de = f2fs_find_entry(dir, &dotdot, &page);
213 f2fs_dentry_kunmap(dir, page);
214 f2fs_put_page(page, 0);
216 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
220 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
221 mark_inode_dirty(dir);
228 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
231 struct inode *inode = NULL;
232 struct f2fs_dir_entry *de;
235 if (dentry->d_name.len > F2FS_NAME_LEN)
236 return ERR_PTR(-ENAMETOOLONG);
238 de = f2fs_find_entry(dir, &dentry->d_name, &page);
240 nid_t ino = le32_to_cpu(de->ino);
241 f2fs_dentry_kunmap(dir, page);
242 f2fs_put_page(page, 0);
244 inode = f2fs_iget(dir->i_sb, ino);
246 return ERR_CAST(inode);
248 if (f2fs_has_inline_dots(inode)) {
251 err = __recover_dot_dentries(inode, dir->i_ino);
259 return d_splice_alias(inode, dentry);
262 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
264 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
265 struct inode *inode = dentry->d_inode;
266 struct f2fs_dir_entry *de;
270 trace_f2fs_unlink_enter(dir, dentry);
271 f2fs_balance_fs(sbi);
273 de = f2fs_find_entry(dir, &dentry->d_name, &page);
278 err = acquire_orphan_inode(sbi);
281 f2fs_dentry_kunmap(dir, page);
282 f2fs_put_page(page, 0);
285 f2fs_delete_entry(de, page, dir, inode);
288 /* In order to evict this inode, we set it dirty */
289 mark_inode_dirty(inode);
292 f2fs_sync_fs(sbi->sb, 1);
294 trace_f2fs_unlink_exit(inode, err);
298 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
301 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
303 size_t symlen = strlen(symname) + 1;
306 f2fs_balance_fs(sbi);
308 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
310 return PTR_ERR(inode);
312 inode->i_op = &f2fs_symlink_inode_operations;
313 inode->i_mapping->a_ops = &f2fs_dblock_aops;
316 err = f2fs_add_link(dentry, inode);
321 err = page_symlink(inode, symname, symlen);
322 alloc_nid_done(sbi, inode->i_ino);
324 d_instantiate(dentry, inode);
325 unlock_new_inode(inode);
328 * Let's flush symlink data in order to avoid broken symlink as much as
329 * possible. Nevertheless, fsyncing is the best way, but there is no
330 * way to get a file descriptor in order to flush that.
332 * Note that, it needs to do dir->fsync to make this recoverable.
333 * If the symlink path is stored into inline_data, there is no
334 * performance regression.
336 filemap_write_and_wait_range(inode->i_mapping, 0, symlen - 1);
339 f2fs_sync_fs(sbi->sb, 1);
342 handle_failed_inode(inode);
346 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
348 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
352 f2fs_balance_fs(sbi);
354 inode = f2fs_new_inode(dir, S_IFDIR | mode);
356 return PTR_ERR(inode);
358 inode->i_op = &f2fs_dir_inode_operations;
359 inode->i_fop = &f2fs_dir_operations;
360 inode->i_mapping->a_ops = &f2fs_dblock_aops;
361 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
363 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
365 err = f2fs_add_link(dentry, inode);
370 stat_inc_inline_dir(inode);
371 alloc_nid_done(sbi, inode->i_ino);
373 d_instantiate(dentry, inode);
374 unlock_new_inode(inode);
377 f2fs_sync_fs(sbi->sb, 1);
381 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
382 handle_failed_inode(inode);
386 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
388 struct inode *inode = dentry->d_inode;
389 if (f2fs_empty_dir(inode))
390 return f2fs_unlink(dir, dentry);
394 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
395 umode_t mode, dev_t rdev)
397 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
401 if (!new_valid_dev(rdev))
404 f2fs_balance_fs(sbi);
406 inode = f2fs_new_inode(dir, mode);
408 return PTR_ERR(inode);
410 init_special_inode(inode, inode->i_mode, rdev);
411 inode->i_op = &f2fs_special_inode_operations;
414 err = f2fs_add_link(dentry, inode);
419 alloc_nid_done(sbi, inode->i_ino);
421 d_instantiate(dentry, inode);
422 unlock_new_inode(inode);
425 f2fs_sync_fs(sbi->sb, 1);
428 handle_failed_inode(inode);
432 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
433 struct inode *new_dir, struct dentry *new_dentry)
435 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
436 struct inode *old_inode = old_dentry->d_inode;
437 struct inode *new_inode = new_dentry->d_inode;
438 struct page *old_dir_page;
439 struct page *old_page, *new_page;
440 struct f2fs_dir_entry *old_dir_entry = NULL;
441 struct f2fs_dir_entry *old_entry;
442 struct f2fs_dir_entry *new_entry;
445 f2fs_balance_fs(sbi);
447 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
451 if (S_ISDIR(old_inode->i_mode)) {
453 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
461 if (old_dir_entry && !f2fs_empty_dir(new_inode))
465 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
472 err = acquire_orphan_inode(sbi);
476 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
477 release_orphan_inode(sbi);
481 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
483 new_inode->i_ctime = CURRENT_TIME;
484 down_write(&F2FS_I(new_inode)->i_sem);
486 drop_nlink(new_inode);
487 drop_nlink(new_inode);
488 up_write(&F2FS_I(new_inode)->i_sem);
490 mark_inode_dirty(new_inode);
492 if (!new_inode->i_nlink)
493 add_orphan_inode(sbi, new_inode->i_ino);
495 release_orphan_inode(sbi);
497 update_inode_page(old_inode);
498 update_inode_page(new_inode);
502 err = f2fs_add_link(new_dentry, old_inode);
510 update_inode_page(new_dir);
514 down_write(&F2FS_I(old_inode)->i_sem);
515 file_lost_pino(old_inode);
516 up_write(&F2FS_I(old_inode)->i_sem);
518 old_inode->i_ctime = CURRENT_TIME;
519 mark_inode_dirty(old_inode);
521 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
524 if (old_dir != new_dir) {
525 f2fs_set_link(old_inode, old_dir_entry,
526 old_dir_page, new_dir);
527 update_inode_page(old_inode);
529 f2fs_dentry_kunmap(old_inode, old_dir_page);
530 f2fs_put_page(old_dir_page, 0);
533 mark_inode_dirty(old_dir);
534 update_inode_page(old_dir);
539 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
540 f2fs_sync_fs(sbi->sb, 1);
545 f2fs_dentry_kunmap(new_dir, new_page);
546 f2fs_put_page(new_page, 0);
549 f2fs_dentry_kunmap(old_inode, old_dir_page);
550 f2fs_put_page(old_dir_page, 0);
553 f2fs_dentry_kunmap(old_dir, old_page);
554 f2fs_put_page(old_page, 0);
559 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
560 struct inode *new_dir, struct dentry *new_dentry)
562 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
563 struct inode *old_inode = old_dentry->d_inode;
564 struct inode *new_inode = new_dentry->d_inode;
565 struct page *old_dir_page, *new_dir_page;
566 struct page *old_page, *new_page;
567 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
568 struct f2fs_dir_entry *old_entry, *new_entry;
569 int old_nlink = 0, new_nlink = 0;
572 f2fs_balance_fs(sbi);
574 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
578 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
582 /* prepare for updating ".." directory entry info later */
583 if (old_dir != new_dir) {
584 if (S_ISDIR(old_inode->i_mode)) {
586 old_dir_entry = f2fs_parent_dir(old_inode,
592 if (S_ISDIR(new_inode->i_mode)) {
594 new_dir_entry = f2fs_parent_dir(new_inode,
602 * If cross rename between file and directory those are not
603 * in the same directory, we will inc nlink of file's parent
604 * later, so we should check upper boundary of its nlink.
606 if ((!old_dir_entry || !new_dir_entry) &&
607 old_dir_entry != new_dir_entry) {
608 old_nlink = old_dir_entry ? -1 : 1;
609 new_nlink = -old_nlink;
611 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
612 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
618 err = update_dent_inode(old_inode, &new_dentry->d_name);
622 err = update_dent_inode(new_inode, &old_dentry->d_name);
626 /* update ".." directory entry info of old dentry */
628 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
630 /* update ".." directory entry info of new dentry */
632 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
634 /* update directory entry info of old dir inode */
635 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
637 down_write(&F2FS_I(old_inode)->i_sem);
638 file_lost_pino(old_inode);
639 up_write(&F2FS_I(old_inode)->i_sem);
641 update_inode_page(old_inode);
643 old_dir->i_ctime = CURRENT_TIME;
645 down_write(&F2FS_I(old_dir)->i_sem);
650 up_write(&F2FS_I(old_dir)->i_sem);
652 mark_inode_dirty(old_dir);
653 update_inode_page(old_dir);
655 /* update directory entry info of new dir inode */
656 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
658 down_write(&F2FS_I(new_inode)->i_sem);
659 file_lost_pino(new_inode);
660 up_write(&F2FS_I(new_inode)->i_sem);
662 update_inode_page(new_inode);
664 new_dir->i_ctime = CURRENT_TIME;
666 down_write(&F2FS_I(new_dir)->i_sem);
671 up_write(&F2FS_I(new_dir)->i_sem);
673 mark_inode_dirty(new_dir);
674 update_inode_page(new_dir);
678 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
679 f2fs_sync_fs(sbi->sb, 1);
682 /* Still we may fail to recover name info of f2fs_inode here */
683 update_dent_inode(old_inode, &old_dentry->d_name);
688 f2fs_dentry_kunmap(new_inode, new_dir_page);
689 f2fs_put_page(new_dir_page, 0);
693 f2fs_dentry_kunmap(old_inode, old_dir_page);
694 f2fs_put_page(old_dir_page, 0);
697 f2fs_dentry_kunmap(new_dir, new_page);
698 f2fs_put_page(new_page, 0);
700 f2fs_dentry_kunmap(old_dir, old_page);
701 f2fs_put_page(old_page, 0);
706 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
707 struct inode *new_dir, struct dentry *new_dentry,
710 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
713 if (flags & RENAME_EXCHANGE) {
714 return f2fs_cross_rename(old_dir, old_dentry,
715 new_dir, new_dentry);
718 * VFS has already handled the new dentry existence case,
719 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
721 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
724 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
726 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
730 inode = f2fs_new_inode(dir, mode);
732 return PTR_ERR(inode);
734 inode->i_op = &f2fs_file_inode_operations;
735 inode->i_fop = &f2fs_file_operations;
736 inode->i_mapping->a_ops = &f2fs_dblock_aops;
739 err = acquire_orphan_inode(sbi);
743 err = f2fs_do_tmpfile(inode, dir);
748 * add this non-linked tmpfile to orphan list, in this way we could
749 * remove all unused data of tmpfile after abnormal power-off.
751 add_orphan_inode(sbi, inode->i_ino);
754 alloc_nid_done(sbi, inode->i_ino);
756 stat_inc_inline_inode(inode);
757 d_tmpfile(dentry, inode);
758 unlock_new_inode(inode);
762 release_orphan_inode(sbi);
764 handle_failed_inode(inode);
768 const struct inode_operations f2fs_dir_inode_operations = {
769 .create = f2fs_create,
770 .lookup = f2fs_lookup,
772 .unlink = f2fs_unlink,
773 .symlink = f2fs_symlink,
777 .rename2 = f2fs_rename2,
778 .tmpfile = f2fs_tmpfile,
779 .getattr = f2fs_getattr,
780 .setattr = f2fs_setattr,
781 .get_acl = f2fs_get_acl,
782 .set_acl = f2fs_set_acl,
783 #ifdef CONFIG_F2FS_FS_XATTR
784 .setxattr = generic_setxattr,
785 .getxattr = generic_getxattr,
786 .listxattr = f2fs_listxattr,
787 .removexattr = generic_removexattr,
791 const struct inode_operations f2fs_symlink_inode_operations = {
792 .readlink = generic_readlink,
793 .follow_link = page_follow_link_light,
794 .put_link = page_put_link,
795 .getattr = f2fs_getattr,
796 .setattr = f2fs_setattr,
797 #ifdef CONFIG_F2FS_FS_XATTR
798 .setxattr = generic_setxattr,
799 .getxattr = generic_getxattr,
800 .listxattr = f2fs_listxattr,
801 .removexattr = generic_removexattr,
805 const struct inode_operations f2fs_special_inode_operations = {
806 .getattr = f2fs_getattr,
807 .setattr = f2fs_setattr,
808 .get_acl = f2fs_get_acl,
809 .set_acl = f2fs_set_acl,
810 #ifdef CONFIG_F2FS_FS_XATTR
811 .setxattr = generic_setxattr,
812 .getxattr = generic_getxattr,
813 .listxattr = f2fs_listxattr,
814 .removexattr = generic_removexattr,