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 struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
193 struct inode *inode = NULL;
194 struct f2fs_dir_entry *de;
197 if (dentry->d_name.len > F2FS_NAME_LEN)
198 return ERR_PTR(-ENAMETOOLONG);
200 de = f2fs_find_entry(dir, &dentry->d_name, &page);
202 nid_t ino = le32_to_cpu(de->ino);
203 f2fs_dentry_kunmap(dir, page);
204 f2fs_put_page(page, 0);
206 inode = f2fs_iget(dir->i_sb, ino);
208 return ERR_CAST(inode);
211 return d_splice_alias(inode, dentry);
214 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
216 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
217 struct inode *inode = dentry->d_inode;
218 struct f2fs_dir_entry *de;
222 trace_f2fs_unlink_enter(dir, dentry);
223 f2fs_balance_fs(sbi);
225 de = f2fs_find_entry(dir, &dentry->d_name, &page);
230 err = acquire_orphan_inode(sbi);
233 f2fs_dentry_kunmap(dir, page);
234 f2fs_put_page(page, 0);
237 f2fs_delete_entry(de, page, dir, inode);
240 /* In order to evict this inode, we set it dirty */
241 mark_inode_dirty(inode);
244 f2fs_sync_fs(sbi->sb, 1);
246 trace_f2fs_unlink_exit(inode, err);
250 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
253 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
255 size_t symlen = strlen(symname) + 1;
258 f2fs_balance_fs(sbi);
260 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
262 return PTR_ERR(inode);
264 inode->i_op = &f2fs_symlink_inode_operations;
265 inode->i_mapping->a_ops = &f2fs_dblock_aops;
268 err = f2fs_add_link(dentry, inode);
273 err = page_symlink(inode, symname, symlen);
274 alloc_nid_done(sbi, inode->i_ino);
276 d_instantiate(dentry, inode);
277 unlock_new_inode(inode);
280 f2fs_sync_fs(sbi->sb, 1);
283 handle_failed_inode(inode);
287 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
289 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
293 f2fs_balance_fs(sbi);
295 inode = f2fs_new_inode(dir, S_IFDIR | mode);
297 return PTR_ERR(inode);
299 inode->i_op = &f2fs_dir_inode_operations;
300 inode->i_fop = &f2fs_dir_operations;
301 inode->i_mapping->a_ops = &f2fs_dblock_aops;
302 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
304 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
306 err = f2fs_add_link(dentry, inode);
311 stat_inc_inline_dir(inode);
312 alloc_nid_done(sbi, inode->i_ino);
314 d_instantiate(dentry, inode);
315 unlock_new_inode(inode);
318 f2fs_sync_fs(sbi->sb, 1);
322 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
323 handle_failed_inode(inode);
327 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
329 struct inode *inode = dentry->d_inode;
330 if (f2fs_empty_dir(inode))
331 return f2fs_unlink(dir, dentry);
335 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
336 umode_t mode, dev_t rdev)
338 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
342 if (!new_valid_dev(rdev))
345 f2fs_balance_fs(sbi);
347 inode = f2fs_new_inode(dir, mode);
349 return PTR_ERR(inode);
351 init_special_inode(inode, inode->i_mode, rdev);
352 inode->i_op = &f2fs_special_inode_operations;
355 err = f2fs_add_link(dentry, inode);
360 alloc_nid_done(sbi, inode->i_ino);
362 d_instantiate(dentry, inode);
363 unlock_new_inode(inode);
366 f2fs_sync_fs(sbi->sb, 1);
369 handle_failed_inode(inode);
373 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
374 struct inode *new_dir, struct dentry *new_dentry)
376 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
377 struct inode *old_inode = old_dentry->d_inode;
378 struct inode *new_inode = new_dentry->d_inode;
379 struct page *old_dir_page;
380 struct page *old_page, *new_page;
381 struct f2fs_dir_entry *old_dir_entry = NULL;
382 struct f2fs_dir_entry *old_entry;
383 struct f2fs_dir_entry *new_entry;
386 f2fs_balance_fs(sbi);
388 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
392 if (S_ISDIR(old_inode->i_mode)) {
394 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
402 if (old_dir_entry && !f2fs_empty_dir(new_inode))
406 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
413 err = acquire_orphan_inode(sbi);
417 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
418 release_orphan_inode(sbi);
422 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
424 new_inode->i_ctime = CURRENT_TIME;
425 down_write(&F2FS_I(new_inode)->i_sem);
427 drop_nlink(new_inode);
428 drop_nlink(new_inode);
429 up_write(&F2FS_I(new_inode)->i_sem);
431 mark_inode_dirty(new_inode);
433 if (!new_inode->i_nlink)
434 add_orphan_inode(sbi, new_inode->i_ino);
436 release_orphan_inode(sbi);
438 update_inode_page(old_inode);
439 update_inode_page(new_inode);
443 err = f2fs_add_link(new_dentry, old_inode);
451 update_inode_page(new_dir);
455 down_write(&F2FS_I(old_inode)->i_sem);
456 file_lost_pino(old_inode);
457 up_write(&F2FS_I(old_inode)->i_sem);
459 old_inode->i_ctime = CURRENT_TIME;
460 mark_inode_dirty(old_inode);
462 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
465 if (old_dir != new_dir) {
466 f2fs_set_link(old_inode, old_dir_entry,
467 old_dir_page, new_dir);
468 update_inode_page(old_inode);
470 f2fs_dentry_kunmap(old_inode, old_dir_page);
471 f2fs_put_page(old_dir_page, 0);
474 mark_inode_dirty(old_dir);
475 update_inode_page(old_dir);
480 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
481 f2fs_sync_fs(sbi->sb, 1);
486 f2fs_dentry_kunmap(new_dir, new_page);
487 f2fs_put_page(new_page, 0);
490 f2fs_dentry_kunmap(old_inode, old_dir_page);
491 f2fs_put_page(old_dir_page, 0);
494 f2fs_dentry_kunmap(old_dir, old_page);
495 f2fs_put_page(old_page, 0);
500 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
501 struct inode *new_dir, struct dentry *new_dentry)
503 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
504 struct inode *old_inode = old_dentry->d_inode;
505 struct inode *new_inode = new_dentry->d_inode;
506 struct page *old_dir_page, *new_dir_page;
507 struct page *old_page, *new_page;
508 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
509 struct f2fs_dir_entry *old_entry, *new_entry;
510 int old_nlink = 0, new_nlink = 0;
513 f2fs_balance_fs(sbi);
515 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
519 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
523 /* prepare for updating ".." directory entry info later */
524 if (old_dir != new_dir) {
525 if (S_ISDIR(old_inode->i_mode)) {
527 old_dir_entry = f2fs_parent_dir(old_inode,
533 if (S_ISDIR(new_inode->i_mode)) {
535 new_dir_entry = f2fs_parent_dir(new_inode,
543 * If cross rename between file and directory those are not
544 * in the same directory, we will inc nlink of file's parent
545 * later, so we should check upper boundary of its nlink.
547 if ((!old_dir_entry || !new_dir_entry) &&
548 old_dir_entry != new_dir_entry) {
549 old_nlink = old_dir_entry ? -1 : 1;
550 new_nlink = -old_nlink;
552 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
553 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
559 err = update_dent_inode(old_inode, &new_dentry->d_name);
563 err = update_dent_inode(new_inode, &old_dentry->d_name);
567 /* update ".." directory entry info of old dentry */
569 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
571 /* update ".." directory entry info of new dentry */
573 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
575 /* update directory entry info of old dir inode */
576 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
578 down_write(&F2FS_I(old_inode)->i_sem);
579 file_lost_pino(old_inode);
580 up_write(&F2FS_I(old_inode)->i_sem);
582 update_inode_page(old_inode);
584 old_dir->i_ctime = CURRENT_TIME;
586 down_write(&F2FS_I(old_dir)->i_sem);
591 up_write(&F2FS_I(old_dir)->i_sem);
593 mark_inode_dirty(old_dir);
594 update_inode_page(old_dir);
596 /* update directory entry info of new dir inode */
597 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
599 down_write(&F2FS_I(new_inode)->i_sem);
600 file_lost_pino(new_inode);
601 up_write(&F2FS_I(new_inode)->i_sem);
603 update_inode_page(new_inode);
605 new_dir->i_ctime = CURRENT_TIME;
607 down_write(&F2FS_I(new_dir)->i_sem);
612 up_write(&F2FS_I(new_dir)->i_sem);
614 mark_inode_dirty(new_dir);
615 update_inode_page(new_dir);
619 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
620 f2fs_sync_fs(sbi->sb, 1);
623 /* Still we may fail to recover name info of f2fs_inode here */
624 update_dent_inode(old_inode, &old_dentry->d_name);
629 f2fs_dentry_kunmap(new_inode, new_dir_page);
630 f2fs_put_page(new_dir_page, 0);
634 f2fs_dentry_kunmap(old_inode, old_dir_page);
635 f2fs_put_page(old_dir_page, 0);
638 f2fs_dentry_kunmap(new_dir, new_page);
639 f2fs_put_page(new_page, 0);
641 f2fs_dentry_kunmap(old_dir, old_page);
642 f2fs_put_page(old_page, 0);
647 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
648 struct inode *new_dir, struct dentry *new_dentry,
651 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
654 if (flags & RENAME_EXCHANGE) {
655 return f2fs_cross_rename(old_dir, old_dentry,
656 new_dir, new_dentry);
659 * VFS has already handled the new dentry existence case,
660 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
662 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
665 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
667 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
671 inode = f2fs_new_inode(dir, mode);
673 return PTR_ERR(inode);
675 inode->i_op = &f2fs_file_inode_operations;
676 inode->i_fop = &f2fs_file_operations;
677 inode->i_mapping->a_ops = &f2fs_dblock_aops;
680 err = acquire_orphan_inode(sbi);
684 err = f2fs_do_tmpfile(inode, dir);
689 * add this non-linked tmpfile to orphan list, in this way we could
690 * remove all unused data of tmpfile after abnormal power-off.
692 add_orphan_inode(sbi, inode->i_ino);
695 alloc_nid_done(sbi, inode->i_ino);
696 d_tmpfile(dentry, inode);
697 unlock_new_inode(inode);
701 release_orphan_inode(sbi);
703 handle_failed_inode(inode);
707 const struct inode_operations f2fs_dir_inode_operations = {
708 .create = f2fs_create,
709 .lookup = f2fs_lookup,
711 .unlink = f2fs_unlink,
712 .symlink = f2fs_symlink,
716 .rename2 = f2fs_rename2,
717 .tmpfile = f2fs_tmpfile,
718 .getattr = f2fs_getattr,
719 .setattr = f2fs_setattr,
720 .get_acl = f2fs_get_acl,
721 .set_acl = f2fs_set_acl,
722 #ifdef CONFIG_F2FS_FS_XATTR
723 .setxattr = generic_setxattr,
724 .getxattr = generic_getxattr,
725 .listxattr = f2fs_listxattr,
726 .removexattr = generic_removexattr,
730 const struct inode_operations f2fs_symlink_inode_operations = {
731 .readlink = generic_readlink,
732 .follow_link = page_follow_link_light,
733 .put_link = page_put_link,
734 .getattr = f2fs_getattr,
735 .setattr = f2fs_setattr,
736 #ifdef CONFIG_F2FS_FS_XATTR
737 .setxattr = generic_setxattr,
738 .getxattr = generic_getxattr,
739 .listxattr = f2fs_listxattr,
740 .removexattr = generic_removexattr,
744 const struct inode_operations f2fs_special_inode_operations = {
745 .getattr = f2fs_getattr,
746 .setattr = f2fs_setattr,
747 .get_acl = f2fs_get_acl,
748 .set_acl = f2fs_set_acl,
749 #ifdef CONFIG_F2FS_FS_XATTR
750 .setxattr = generic_setxattr,
751 .getxattr = generic_getxattr,
752 .listxattr = f2fs_listxattr,
753 .removexattr = generic_removexattr,