Linux 3.16-rc2
[linux/fpc-iii.git] / fs / f2fs / namei.c
blob9138c32aa69864b3e2b1ee40f08135383b5918b1
1 /*
2 * fs/f2fs/namei.c
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.
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched.h>
15 #include <linux/ctype.h>
17 #include "f2fs.h"
18 #include "node.h"
19 #include "xattr.h"
20 #include "acl.h"
21 #include <trace/events/f2fs.h>
23 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
25 struct super_block *sb = dir->i_sb;
26 struct f2fs_sb_info *sbi = F2FS_SB(sb);
27 nid_t ino;
28 struct inode *inode;
29 bool nid_free = false;
30 int err;
32 inode = new_inode(sb);
33 if (!inode)
34 return ERR_PTR(-ENOMEM);
36 f2fs_lock_op(sbi);
37 if (!alloc_nid(sbi, &ino)) {
38 f2fs_unlock_op(sbi);
39 err = -ENOSPC;
40 goto fail;
42 f2fs_unlock_op(sbi);
44 inode_init_owner(inode, dir, mode);
46 inode->i_ino = ino;
47 inode->i_blocks = 0;
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);
52 if (err) {
53 err = -EINVAL;
54 nid_free = true;
55 goto out;
57 trace_f2fs_new_inode(inode, 0);
58 mark_inode_dirty(inode);
59 return inode;
61 out:
62 clear_nlink(inode);
63 unlock_new_inode(inode);
64 fail:
65 trace_f2fs_new_inode(inode, err);
66 make_bad_inode(inode);
67 iput(inode);
68 if (nid_free)
69 alloc_nid_failed(sbi, ino);
70 return ERR_PTR(err);
73 static int is_multimedia_file(const unsigned char *s, const char *sub)
75 size_t slen = strlen(s);
76 size_t sublen = strlen(sub);
78 if (sublen > slen)
79 return 0;
81 return !strncasecmp(s + slen - sublen, sub, sublen);
85 * Set multimedia files as cold files for hot/cold data separation
87 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
88 const unsigned char *name)
90 int i;
91 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
93 int count = le32_to_cpu(sbi->raw_super->extension_count);
94 for (i = 0; i < count; i++) {
95 if (is_multimedia_file(name, extlist[i])) {
96 file_set_cold(inode);
97 break;
102 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
103 bool excl)
105 struct super_block *sb = dir->i_sb;
106 struct f2fs_sb_info *sbi = F2FS_SB(sb);
107 struct inode *inode;
108 nid_t ino = 0;
109 int err;
111 f2fs_balance_fs(sbi);
113 inode = f2fs_new_inode(dir, mode);
114 if (IS_ERR(inode))
115 return PTR_ERR(inode);
117 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
118 set_cold_files(sbi, inode, dentry->d_name.name);
120 inode->i_op = &f2fs_file_inode_operations;
121 inode->i_fop = &f2fs_file_operations;
122 inode->i_mapping->a_ops = &f2fs_dblock_aops;
123 ino = inode->i_ino;
125 f2fs_lock_op(sbi);
126 err = f2fs_add_link(dentry, inode);
127 f2fs_unlock_op(sbi);
128 if (err)
129 goto out;
131 alloc_nid_done(sbi, ino);
133 d_instantiate(dentry, inode);
134 unlock_new_inode(inode);
135 return 0;
136 out:
137 clear_nlink(inode);
138 unlock_new_inode(inode);
139 make_bad_inode(inode);
140 iput(inode);
141 alloc_nid_failed(sbi, ino);
142 return err;
145 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
146 struct dentry *dentry)
148 struct inode *inode = old_dentry->d_inode;
149 struct super_block *sb = dir->i_sb;
150 struct f2fs_sb_info *sbi = F2FS_SB(sb);
151 int err;
153 f2fs_balance_fs(sbi);
155 inode->i_ctime = CURRENT_TIME;
156 ihold(inode);
158 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
159 f2fs_lock_op(sbi);
160 err = f2fs_add_link(dentry, inode);
161 f2fs_unlock_op(sbi);
162 if (err)
163 goto out;
165 d_instantiate(dentry, inode);
166 return 0;
167 out:
168 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
169 iput(inode);
170 return err;
173 struct dentry *f2fs_get_parent(struct dentry *child)
175 struct qstr dotdot = QSTR_INIT("..", 2);
176 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
177 if (!ino)
178 return ERR_PTR(-ENOENT);
179 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
182 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
183 unsigned int flags)
185 struct inode *inode = NULL;
186 struct f2fs_dir_entry *de;
187 struct page *page;
189 if (dentry->d_name.len > F2FS_NAME_LEN)
190 return ERR_PTR(-ENAMETOOLONG);
192 de = f2fs_find_entry(dir, &dentry->d_name, &page);
193 if (de) {
194 nid_t ino = le32_to_cpu(de->ino);
195 kunmap(page);
196 f2fs_put_page(page, 0);
198 inode = f2fs_iget(dir->i_sb, ino);
199 if (IS_ERR(inode))
200 return ERR_CAST(inode);
202 stat_inc_inline_inode(inode);
205 return d_splice_alias(inode, dentry);
208 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
210 struct super_block *sb = dir->i_sb;
211 struct f2fs_sb_info *sbi = F2FS_SB(sb);
212 struct inode *inode = dentry->d_inode;
213 struct f2fs_dir_entry *de;
214 struct page *page;
215 int err = -ENOENT;
217 trace_f2fs_unlink_enter(dir, dentry);
218 f2fs_balance_fs(sbi);
220 de = f2fs_find_entry(dir, &dentry->d_name, &page);
221 if (!de)
222 goto fail;
224 f2fs_lock_op(sbi);
225 err = acquire_orphan_inode(sbi);
226 if (err) {
227 f2fs_unlock_op(sbi);
228 kunmap(page);
229 f2fs_put_page(page, 0);
230 goto fail;
232 f2fs_delete_entry(de, page, inode);
233 f2fs_unlock_op(sbi);
235 /* In order to evict this inode, we set it dirty */
236 mark_inode_dirty(inode);
237 fail:
238 trace_f2fs_unlink_exit(inode, err);
239 return err;
242 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
243 const char *symname)
245 struct super_block *sb = dir->i_sb;
246 struct f2fs_sb_info *sbi = F2FS_SB(sb);
247 struct inode *inode;
248 size_t symlen = strlen(symname) + 1;
249 int err;
251 f2fs_balance_fs(sbi);
253 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
254 if (IS_ERR(inode))
255 return PTR_ERR(inode);
257 inode->i_op = &f2fs_symlink_inode_operations;
258 inode->i_mapping->a_ops = &f2fs_dblock_aops;
260 f2fs_lock_op(sbi);
261 err = f2fs_add_link(dentry, inode);
262 f2fs_unlock_op(sbi);
263 if (err)
264 goto out;
266 err = page_symlink(inode, symname, symlen);
267 alloc_nid_done(sbi, inode->i_ino);
269 d_instantiate(dentry, inode);
270 unlock_new_inode(inode);
271 return err;
272 out:
273 clear_nlink(inode);
274 unlock_new_inode(inode);
275 make_bad_inode(inode);
276 iput(inode);
277 alloc_nid_failed(sbi, inode->i_ino);
278 return err;
281 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
283 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
284 struct inode *inode;
285 int err;
287 f2fs_balance_fs(sbi);
289 inode = f2fs_new_inode(dir, S_IFDIR | mode);
290 if (IS_ERR(inode))
291 return PTR_ERR(inode);
293 inode->i_op = &f2fs_dir_inode_operations;
294 inode->i_fop = &f2fs_dir_operations;
295 inode->i_mapping->a_ops = &f2fs_dblock_aops;
296 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
298 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
299 f2fs_lock_op(sbi);
300 err = f2fs_add_link(dentry, inode);
301 f2fs_unlock_op(sbi);
302 if (err)
303 goto out_fail;
305 alloc_nid_done(sbi, inode->i_ino);
307 d_instantiate(dentry, inode);
308 unlock_new_inode(inode);
310 return 0;
312 out_fail:
313 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
314 clear_nlink(inode);
315 unlock_new_inode(inode);
316 make_bad_inode(inode);
317 iput(inode);
318 alloc_nid_failed(sbi, inode->i_ino);
319 return err;
322 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
324 struct inode *inode = dentry->d_inode;
325 if (f2fs_empty_dir(inode))
326 return f2fs_unlink(dir, dentry);
327 return -ENOTEMPTY;
330 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
331 umode_t mode, dev_t rdev)
333 struct super_block *sb = dir->i_sb;
334 struct f2fs_sb_info *sbi = F2FS_SB(sb);
335 struct inode *inode;
336 int err = 0;
338 if (!new_valid_dev(rdev))
339 return -EINVAL;
341 f2fs_balance_fs(sbi);
343 inode = f2fs_new_inode(dir, mode);
344 if (IS_ERR(inode))
345 return PTR_ERR(inode);
347 init_special_inode(inode, inode->i_mode, rdev);
348 inode->i_op = &f2fs_special_inode_operations;
350 f2fs_lock_op(sbi);
351 err = f2fs_add_link(dentry, inode);
352 f2fs_unlock_op(sbi);
353 if (err)
354 goto out;
356 alloc_nid_done(sbi, inode->i_ino);
357 d_instantiate(dentry, inode);
358 unlock_new_inode(inode);
359 return 0;
360 out:
361 clear_nlink(inode);
362 unlock_new_inode(inode);
363 make_bad_inode(inode);
364 iput(inode);
365 alloc_nid_failed(sbi, inode->i_ino);
366 return err;
369 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
370 struct inode *new_dir, struct dentry *new_dentry)
372 struct super_block *sb = old_dir->i_sb;
373 struct f2fs_sb_info *sbi = F2FS_SB(sb);
374 struct inode *old_inode = old_dentry->d_inode;
375 struct inode *new_inode = new_dentry->d_inode;
376 struct page *old_dir_page;
377 struct page *old_page, *new_page;
378 struct f2fs_dir_entry *old_dir_entry = NULL;
379 struct f2fs_dir_entry *old_entry;
380 struct f2fs_dir_entry *new_entry;
381 int err = -ENOENT;
383 f2fs_balance_fs(sbi);
385 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
386 if (!old_entry)
387 goto out;
389 if (S_ISDIR(old_inode->i_mode)) {
390 err = -EIO;
391 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
392 if (!old_dir_entry)
393 goto out_old;
396 f2fs_lock_op(sbi);
398 if (new_inode) {
400 err = -ENOTEMPTY;
401 if (old_dir_entry && !f2fs_empty_dir(new_inode))
402 goto out_dir;
404 err = -ENOENT;
405 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
406 &new_page);
407 if (!new_entry)
408 goto out_dir;
410 err = acquire_orphan_inode(sbi);
411 if (err)
412 goto put_out_dir;
414 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
415 release_orphan_inode(sbi);
416 goto put_out_dir;
419 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
420 down_write(&F2FS_I(old_inode)->i_sem);
421 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
422 up_write(&F2FS_I(old_inode)->i_sem);
424 new_inode->i_ctime = CURRENT_TIME;
425 down_write(&F2FS_I(new_inode)->i_sem);
426 if (old_dir_entry)
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);
435 else
436 release_orphan_inode(sbi);
438 update_inode_page(old_inode);
439 update_inode_page(new_inode);
440 } else {
441 err = f2fs_add_link(new_dentry, old_inode);
442 if (err)
443 goto out_dir;
445 if (old_dir_entry) {
446 inc_nlink(new_dir);
447 update_inode_page(new_dir);
451 old_inode->i_ctime = CURRENT_TIME;
452 mark_inode_dirty(old_inode);
454 f2fs_delete_entry(old_entry, old_page, NULL);
456 if (old_dir_entry) {
457 if (old_dir != new_dir) {
458 f2fs_set_link(old_inode, old_dir_entry,
459 old_dir_page, new_dir);
460 down_write(&F2FS_I(old_inode)->i_sem);
461 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
462 up_write(&F2FS_I(old_inode)->i_sem);
463 update_inode_page(old_inode);
464 } else {
465 kunmap(old_dir_page);
466 f2fs_put_page(old_dir_page, 0);
468 drop_nlink(old_dir);
469 mark_inode_dirty(old_dir);
470 update_inode_page(old_dir);
473 f2fs_unlock_op(sbi);
474 return 0;
476 put_out_dir:
477 f2fs_put_page(new_page, 1);
478 out_dir:
479 if (old_dir_entry) {
480 kunmap(old_dir_page);
481 f2fs_put_page(old_dir_page, 0);
483 f2fs_unlock_op(sbi);
484 out_old:
485 kunmap(old_page);
486 f2fs_put_page(old_page, 0);
487 out:
488 return err;
491 const struct inode_operations f2fs_dir_inode_operations = {
492 .create = f2fs_create,
493 .lookup = f2fs_lookup,
494 .link = f2fs_link,
495 .unlink = f2fs_unlink,
496 .symlink = f2fs_symlink,
497 .mkdir = f2fs_mkdir,
498 .rmdir = f2fs_rmdir,
499 .mknod = f2fs_mknod,
500 .rename = f2fs_rename,
501 .getattr = f2fs_getattr,
502 .setattr = f2fs_setattr,
503 .get_acl = f2fs_get_acl,
504 .set_acl = f2fs_set_acl,
505 #ifdef CONFIG_F2FS_FS_XATTR
506 .setxattr = generic_setxattr,
507 .getxattr = generic_getxattr,
508 .listxattr = f2fs_listxattr,
509 .removexattr = generic_removexattr,
510 #endif
513 const struct inode_operations f2fs_symlink_inode_operations = {
514 .readlink = generic_readlink,
515 .follow_link = page_follow_link_light,
516 .put_link = page_put_link,
517 .getattr = f2fs_getattr,
518 .setattr = f2fs_setattr,
519 #ifdef CONFIG_F2FS_FS_XATTR
520 .setxattr = generic_setxattr,
521 .getxattr = generic_getxattr,
522 .listxattr = f2fs_listxattr,
523 .removexattr = generic_removexattr,
524 #endif
527 const struct inode_operations f2fs_special_inode_operations = {
528 .getattr = f2fs_getattr,
529 .setattr = f2fs_setattr,
530 .get_acl = f2fs_get_acl,
531 .set_acl = f2fs_set_acl,
532 #ifdef CONFIG_F2FS_FS_XATTR
533 .setxattr = generic_setxattr,
534 .getxattr = generic_getxattr,
535 .listxattr = f2fs_listxattr,
536 .removexattr = generic_removexattr,
537 #endif