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>
22 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
24 struct super_block *sb = dir->i_sb;
25 struct f2fs_sb_info *sbi = F2FS_SB(sb);
28 bool nid_free = false;
31 inode = new_inode(sb);
33 return ERR_PTR(-ENOMEM);
35 mutex_lock_op(sbi, NODE_NEW);
36 if (!alloc_nid(sbi, &ino)) {
37 mutex_unlock_op(sbi, NODE_NEW);
41 mutex_unlock_op(sbi, NODE_NEW);
43 inode->i_uid = current_fsuid();
45 if (dir->i_mode & S_ISGID) {
46 inode->i_gid = dir->i_gid;
50 inode->i_gid = current_fsgid();
56 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
57 inode->i_generation = sbi->s_next_generation++;
59 err = insert_inode_locked(inode);
66 mark_inode_dirty(inode);
71 unlock_new_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);
88 ret = memcmp(s + slen - sublen, sub, sublen);
89 if (ret) { /* compare upper case */
92 for (i = 0; i < sublen && i < sizeof(upper_sub); i++)
93 upper_sub[i] = toupper(sub[i]);
94 return memcmp(s + slen - sublen, upper_sub, sublen);
101 * Set multimedia files as cold files for hot/cold data separation
103 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
104 const unsigned char *name)
107 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
109 int count = le32_to_cpu(sbi->raw_super->extension_count);
110 for (i = 0; i < count; i++) {
111 if (!is_multimedia_file(name, extlist[i])) {
112 set_cold_file(inode);
118 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
121 struct super_block *sb = dir->i_sb;
122 struct f2fs_sb_info *sbi = F2FS_SB(sb);
127 f2fs_balance_fs(sbi);
129 inode = f2fs_new_inode(dir, mode);
131 return PTR_ERR(inode);
133 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
134 set_cold_files(sbi, inode, dentry->d_name.name);
136 inode->i_op = &f2fs_file_inode_operations;
137 inode->i_fop = &f2fs_file_operations;
138 inode->i_mapping->a_ops = &f2fs_dblock_aops;
141 err = f2fs_add_link(dentry, inode);
145 alloc_nid_done(sbi, ino);
148 d_instantiate(dentry, inode);
149 unlock_new_inode(inode);
153 unlock_new_inode(inode);
155 alloc_nid_failed(sbi, ino);
159 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
160 struct dentry *dentry)
162 struct inode *inode = old_dentry->d_inode;
163 struct super_block *sb = dir->i_sb;
164 struct f2fs_sb_info *sbi = F2FS_SB(sb);
167 f2fs_balance_fs(sbi);
169 inode->i_ctime = CURRENT_TIME;
170 atomic_inc(&inode->i_count);
172 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
173 err = f2fs_add_link(dentry, inode);
178 * This file should be checkpointed during fsync.
179 * We lost i_pino from now on.
183 d_instantiate(dentry, inode);
186 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
191 struct dentry *f2fs_get_parent(struct dentry *child)
193 struct qstr dotdot = QSTR_INIT("..", 2);
194 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
196 return ERR_PTR(-ENOENT);
197 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
200 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
203 struct inode *inode = NULL;
204 struct f2fs_dir_entry *de;
207 if (dentry->d_name.len > F2FS_NAME_LEN)
208 return ERR_PTR(-ENAMETOOLONG);
210 de = f2fs_find_entry(dir, &dentry->d_name, &page);
212 nid_t ino = le32_to_cpu(de->ino);
214 f2fs_put_page(page, 0);
216 inode = f2fs_iget(dir->i_sb, ino);
218 return ERR_CAST(inode);
221 return d_splice_alias(inode, dentry);
224 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
226 struct super_block *sb = dir->i_sb;
227 struct f2fs_sb_info *sbi = F2FS_SB(sb);
228 struct inode *inode = dentry->d_inode;
229 struct f2fs_dir_entry *de;
233 f2fs_balance_fs(sbi);
235 de = f2fs_find_entry(dir, &dentry->d_name, &page);
239 err = check_orphan_space(sbi);
242 f2fs_put_page(page, 0);
246 f2fs_delete_entry(de, page, inode);
248 /* In order to evict this inode, we set it dirty */
249 mark_inode_dirty(inode);
254 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
257 struct super_block *sb = dir->i_sb;
258 struct f2fs_sb_info *sbi = F2FS_SB(sb);
260 size_t symlen = strlen(symname) + 1;
263 f2fs_balance_fs(sbi);
265 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
267 return PTR_ERR(inode);
269 inode->i_op = &f2fs_symlink_inode_operations;
270 inode->i_mapping->a_ops = &f2fs_dblock_aops;
272 err = f2fs_add_link(dentry, inode);
276 err = page_symlink(inode, symname, symlen);
277 alloc_nid_done(sbi, inode->i_ino);
279 d_instantiate(dentry, inode);
280 unlock_new_inode(inode);
284 unlock_new_inode(inode);
286 alloc_nid_failed(sbi, inode->i_ino);
290 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
292 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
296 f2fs_balance_fs(sbi);
298 inode = f2fs_new_inode(dir, S_IFDIR | mode);
300 return PTR_ERR(inode);
302 inode->i_op = &f2fs_dir_inode_operations;
303 inode->i_fop = &f2fs_dir_operations;
304 inode->i_mapping->a_ops = &f2fs_dblock_aops;
305 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
307 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
308 err = f2fs_add_link(dentry, inode);
312 alloc_nid_done(sbi, inode->i_ino);
314 d_instantiate(dentry, inode);
315 unlock_new_inode(inode);
320 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
322 unlock_new_inode(inode);
324 alloc_nid_failed(sbi, inode->i_ino);
328 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
330 struct inode *inode = dentry->d_inode;
331 if (f2fs_empty_dir(inode))
332 return f2fs_unlink(dir, dentry);
336 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
337 umode_t mode, dev_t rdev)
339 struct super_block *sb = dir->i_sb;
340 struct f2fs_sb_info *sbi = F2FS_SB(sb);
344 if (!new_valid_dev(rdev))
347 f2fs_balance_fs(sbi);
349 inode = f2fs_new_inode(dir, mode);
351 return PTR_ERR(inode);
353 init_special_inode(inode, inode->i_mode, rdev);
354 inode->i_op = &f2fs_special_inode_operations;
356 err = f2fs_add_link(dentry, inode);
360 alloc_nid_done(sbi, inode->i_ino);
361 d_instantiate(dentry, inode);
362 unlock_new_inode(inode);
366 unlock_new_inode(inode);
368 alloc_nid_failed(sbi, inode->i_ino);
372 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
373 struct inode *new_dir, struct dentry *new_dentry)
375 struct super_block *sb = old_dir->i_sb;
376 struct f2fs_sb_info *sbi = F2FS_SB(sb);
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;
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);
399 mutex_lock_op(sbi, RENAME);
402 struct page *new_page;
405 if (old_dir_entry && !f2fs_empty_dir(new_inode))
409 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
414 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
416 new_inode->i_ctime = CURRENT_TIME;
418 drop_nlink(new_inode);
419 drop_nlink(new_inode);
420 if (!new_inode->i_nlink)
421 add_orphan_inode(sbi, new_inode->i_ino);
422 f2fs_write_inode(new_inode, NULL);
424 err = f2fs_add_link(new_dentry, old_inode);
430 f2fs_write_inode(new_dir, NULL);
434 old_inode->i_ctime = CURRENT_TIME;
435 mark_inode_dirty(old_inode);
437 f2fs_delete_entry(old_entry, old_page, NULL);
440 if (old_dir != new_dir) {
441 f2fs_set_link(old_inode, old_dir_entry,
442 old_dir_page, new_dir);
444 kunmap(old_dir_page);
445 f2fs_put_page(old_dir_page, 0);
448 f2fs_write_inode(old_dir, NULL);
451 mutex_unlock_op(sbi, RENAME);
456 kunmap(old_dir_page);
457 f2fs_put_page(old_dir_page, 0);
459 mutex_unlock_op(sbi, RENAME);
462 f2fs_put_page(old_page, 0);
467 const struct inode_operations f2fs_dir_inode_operations = {
468 .create = f2fs_create,
469 .lookup = f2fs_lookup,
471 .unlink = f2fs_unlink,
472 .symlink = f2fs_symlink,
476 .rename = f2fs_rename,
477 .setattr = f2fs_setattr,
478 .get_acl = f2fs_get_acl,
479 #ifdef CONFIG_F2FS_FS_XATTR
480 .setxattr = generic_setxattr,
481 .getxattr = generic_getxattr,
482 .listxattr = f2fs_listxattr,
483 .removexattr = generic_removexattr,
487 const struct inode_operations f2fs_symlink_inode_operations = {
488 .readlink = generic_readlink,
489 .follow_link = page_follow_link_light,
490 .put_link = page_put_link,
491 .setattr = f2fs_setattr,
492 #ifdef CONFIG_F2FS_FS_XATTR
493 .setxattr = generic_setxattr,
494 .getxattr = generic_getxattr,
495 .listxattr = f2fs_listxattr,
496 .removexattr = generic_removexattr,
500 const struct inode_operations f2fs_special_inode_operations = {
501 .setattr = f2fs_setattr,
502 .get_acl = f2fs_get_acl,
503 #ifdef CONFIG_F2FS_FS_XATTR
504 .setxattr = generic_setxattr,
505 .getxattr = generic_getxattr,
506 .listxattr = f2fs_listxattr,
507 .removexattr = generic_removexattr,