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>
21 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
23 struct super_block *sb = dir->i_sb;
24 struct f2fs_sb_info *sbi = F2FS_SB(sb);
27 bool nid_free = false;
30 inode = new_inode(sb);
32 return ERR_PTR(-ENOMEM);
34 mutex_lock_op(sbi, NODE_NEW);
35 if (!alloc_nid(sbi, &ino)) {
36 mutex_unlock_op(sbi, NODE_NEW);
40 mutex_unlock_op(sbi, NODE_NEW);
42 inode->i_uid = current_fsuid();
44 if (dir->i_mode & S_ISGID) {
45 inode->i_gid = dir->i_gid;
49 inode->i_gid = current_fsgid();
55 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
56 inode->i_generation = sbi->s_next_generation++;
58 err = insert_inode_locked(inode);
65 mark_inode_dirty(inode);
70 unlock_new_inode(inode);
74 alloc_nid_failed(sbi, ino);
78 static int is_multimedia_file(const unsigned char *s, const char *sub)
80 size_t slen = strlen(s);
81 size_t sublen = strlen(sub);
87 ret = memcmp(s + slen - sublen, sub, sublen);
88 if (ret) { /* compare upper case */
91 for (i = 0; i < sublen && i < sizeof(upper_sub); i++)
92 upper_sub[i] = toupper(sub[i]);
93 return memcmp(s + slen - sublen, upper_sub, sublen);
100 * Set multimedia files as cold files for hot/cold data separation
102 static inline void set_cold_file(struct f2fs_sb_info *sbi, struct inode *inode,
103 const unsigned char *name)
106 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
108 int count = le32_to_cpu(sbi->raw_super->extension_count);
109 for (i = 0; i < count; i++) {
110 if (!is_multimedia_file(name, extlist[i])) {
111 F2FS_I(inode)->i_advise |= FADVISE_COLD_BIT;
117 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
120 struct super_block *sb = dir->i_sb;
121 struct f2fs_sb_info *sbi = F2FS_SB(sb);
126 f2fs_balance_fs(sbi);
128 inode = f2fs_new_inode(dir, mode);
130 return PTR_ERR(inode);
132 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
133 set_cold_file(sbi, inode, dentry->d_name.name);
135 inode->i_op = &f2fs_file_inode_operations;
136 inode->i_fop = &f2fs_file_operations;
137 inode->i_mapping->a_ops = &f2fs_dblock_aops;
140 err = f2fs_add_link(dentry, inode);
144 alloc_nid_done(sbi, ino);
147 d_instantiate(dentry, inode);
148 unlock_new_inode(inode);
152 unlock_new_inode(inode);
154 alloc_nid_failed(sbi, ino);
158 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
159 struct dentry *dentry)
161 struct inode *inode = old_dentry->d_inode;
162 struct super_block *sb = dir->i_sb;
163 struct f2fs_sb_info *sbi = F2FS_SB(sb);
166 f2fs_balance_fs(sbi);
168 inode->i_ctime = CURRENT_TIME;
169 atomic_inc(&inode->i_count);
171 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
172 err = f2fs_add_link(dentry, inode);
176 d_instantiate(dentry, inode);
179 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
184 struct dentry *f2fs_get_parent(struct dentry *child)
186 struct qstr dotdot = QSTR_INIT("..", 2);
187 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
189 return ERR_PTR(-ENOENT);
190 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
193 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
196 struct inode *inode = NULL;
197 struct f2fs_dir_entry *de;
200 if (dentry->d_name.len > F2FS_MAX_NAME_LEN)
201 return ERR_PTR(-ENAMETOOLONG);
203 de = f2fs_find_entry(dir, &dentry->d_name, &page);
205 nid_t ino = le32_to_cpu(de->ino);
207 f2fs_put_page(page, 0);
209 inode = f2fs_iget(dir->i_sb, ino);
211 return ERR_CAST(inode);
214 return d_splice_alias(inode, dentry);
217 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
219 struct super_block *sb = dir->i_sb;
220 struct f2fs_sb_info *sbi = F2FS_SB(sb);
221 struct inode *inode = dentry->d_inode;
222 struct f2fs_dir_entry *de;
226 f2fs_balance_fs(sbi);
228 de = f2fs_find_entry(dir, &dentry->d_name, &page);
232 err = check_orphan_space(sbi);
235 f2fs_put_page(page, 0);
239 f2fs_delete_entry(de, page, inode);
241 /* In order to evict this inode, we set it dirty */
242 mark_inode_dirty(inode);
247 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
250 struct super_block *sb = dir->i_sb;
251 struct f2fs_sb_info *sbi = F2FS_SB(sb);
253 size_t symlen = strlen(symname) + 1;
256 f2fs_balance_fs(sbi);
258 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
260 return PTR_ERR(inode);
262 inode->i_op = &f2fs_symlink_inode_operations;
263 inode->i_mapping->a_ops = &f2fs_dblock_aops;
265 err = f2fs_add_link(dentry, inode);
269 err = page_symlink(inode, symname, symlen);
270 alloc_nid_done(sbi, inode->i_ino);
272 d_instantiate(dentry, inode);
273 unlock_new_inode(inode);
277 unlock_new_inode(inode);
279 alloc_nid_failed(sbi, inode->i_ino);
283 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
285 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
289 f2fs_balance_fs(sbi);
291 inode = f2fs_new_inode(dir, S_IFDIR | mode);
293 return PTR_ERR(inode);
295 inode->i_op = &f2fs_dir_inode_operations;
296 inode->i_fop = &f2fs_dir_operations;
297 inode->i_mapping->a_ops = &f2fs_dblock_aops;
298 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
300 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
301 err = f2fs_add_link(dentry, inode);
305 alloc_nid_done(sbi, inode->i_ino);
307 d_instantiate(dentry, inode);
308 unlock_new_inode(inode);
313 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
315 unlock_new_inode(inode);
317 alloc_nid_failed(sbi, inode->i_ino);
321 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
323 struct inode *inode = dentry->d_inode;
324 if (f2fs_empty_dir(inode))
325 return f2fs_unlink(dir, dentry);
329 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
330 umode_t mode, dev_t rdev)
332 struct super_block *sb = dir->i_sb;
333 struct f2fs_sb_info *sbi = F2FS_SB(sb);
337 if (!new_valid_dev(rdev))
340 f2fs_balance_fs(sbi);
342 inode = f2fs_new_inode(dir, mode);
344 return PTR_ERR(inode);
346 init_special_inode(inode, inode->i_mode, rdev);
347 inode->i_op = &f2fs_special_inode_operations;
349 err = f2fs_add_link(dentry, inode);
353 alloc_nid_done(sbi, inode->i_ino);
354 d_instantiate(dentry, inode);
355 unlock_new_inode(inode);
359 unlock_new_inode(inode);
361 alloc_nid_failed(sbi, inode->i_ino);
365 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
366 struct inode *new_dir, struct dentry *new_dentry)
368 struct super_block *sb = old_dir->i_sb;
369 struct f2fs_sb_info *sbi = F2FS_SB(sb);
370 struct inode *old_inode = old_dentry->d_inode;
371 struct inode *new_inode = new_dentry->d_inode;
372 struct page *old_dir_page;
373 struct page *old_page;
374 struct f2fs_dir_entry *old_dir_entry = NULL;
375 struct f2fs_dir_entry *old_entry;
376 struct f2fs_dir_entry *new_entry;
379 f2fs_balance_fs(sbi);
381 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
385 if (S_ISDIR(old_inode->i_mode)) {
387 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
392 mutex_lock_op(sbi, RENAME);
395 struct page *new_page;
398 if (old_dir_entry && !f2fs_empty_dir(new_inode))
402 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
407 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
409 new_inode->i_ctime = CURRENT_TIME;
411 drop_nlink(new_inode);
412 drop_nlink(new_inode);
413 if (!new_inode->i_nlink)
414 add_orphan_inode(sbi, new_inode->i_ino);
415 f2fs_write_inode(new_inode, NULL);
417 err = f2fs_add_link(new_dentry, old_inode);
423 f2fs_write_inode(new_dir, NULL);
427 old_inode->i_ctime = CURRENT_TIME;
428 set_inode_flag(F2FS_I(old_inode), FI_NEED_CP);
429 mark_inode_dirty(old_inode);
431 f2fs_delete_entry(old_entry, old_page, NULL);
434 if (old_dir != new_dir) {
435 f2fs_set_link(old_inode, old_dir_entry,
436 old_dir_page, new_dir);
438 kunmap(old_dir_page);
439 f2fs_put_page(old_dir_page, 0);
442 f2fs_write_inode(old_dir, NULL);
445 mutex_unlock_op(sbi, RENAME);
450 kunmap(old_dir_page);
451 f2fs_put_page(old_dir_page, 0);
453 mutex_unlock_op(sbi, RENAME);
456 f2fs_put_page(old_page, 0);
461 const struct inode_operations f2fs_dir_inode_operations = {
462 .create = f2fs_create,
463 .lookup = f2fs_lookup,
465 .unlink = f2fs_unlink,
466 .symlink = f2fs_symlink,
470 .rename = f2fs_rename,
471 .setattr = f2fs_setattr,
472 .get_acl = f2fs_get_acl,
473 #ifdef CONFIG_F2FS_FS_XATTR
474 .setxattr = generic_setxattr,
475 .getxattr = generic_getxattr,
476 .listxattr = f2fs_listxattr,
477 .removexattr = generic_removexattr,
481 const struct inode_operations f2fs_symlink_inode_operations = {
482 .readlink = generic_readlink,
483 .follow_link = page_follow_link_light,
484 .put_link = page_put_link,
485 .setattr = f2fs_setattr,
486 #ifdef CONFIG_F2FS_FS_XATTR
487 .setxattr = generic_setxattr,
488 .getxattr = generic_getxattr,
489 .listxattr = f2fs_listxattr,
490 .removexattr = generic_removexattr,
494 const struct inode_operations f2fs_special_inode_operations = {
495 .setattr = f2fs_setattr,
496 .get_acl = f2fs_get_acl,
497 #ifdef CONFIG_F2FS_FS_XATTR
498 .setxattr = generic_setxattr,
499 .getxattr = generic_getxattr,
500 .listxattr = f2fs_listxattr,
501 .removexattr = generic_removexattr,