ima: don't allocate a copy of template_fmt in template_desc_init_fields()
[linux/fpc-iii.git] / fs / f2fs / namei.c
bloba6bdddc33ce2ae5458b734052c09ae5200e6c2b2
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);
421 new_inode->i_ctime = CURRENT_TIME;
422 down_write(&F2FS_I(new_inode)->i_sem);
423 if (old_dir_entry)
424 drop_nlink(new_inode);
425 drop_nlink(new_inode);
426 up_write(&F2FS_I(new_inode)->i_sem);
428 mark_inode_dirty(new_inode);
430 if (!new_inode->i_nlink)
431 add_orphan_inode(sbi, new_inode->i_ino);
432 else
433 release_orphan_inode(sbi);
435 update_inode_page(old_inode);
436 update_inode_page(new_inode);
437 } else {
438 err = f2fs_add_link(new_dentry, old_inode);
439 if (err)
440 goto out_dir;
442 if (old_dir_entry) {
443 inc_nlink(new_dir);
444 update_inode_page(new_dir);
448 down_write(&F2FS_I(old_inode)->i_sem);
449 file_lost_pino(old_inode);
450 up_write(&F2FS_I(old_inode)->i_sem);
452 old_inode->i_ctime = CURRENT_TIME;
453 mark_inode_dirty(old_inode);
455 f2fs_delete_entry(old_entry, old_page, NULL);
457 if (old_dir_entry) {
458 if (old_dir != new_dir) {
459 f2fs_set_link(old_inode, old_dir_entry,
460 old_dir_page, new_dir);
461 update_inode_page(old_inode);
462 } else {
463 kunmap(old_dir_page);
464 f2fs_put_page(old_dir_page, 0);
466 drop_nlink(old_dir);
467 mark_inode_dirty(old_dir);
468 update_inode_page(old_dir);
471 f2fs_unlock_op(sbi);
472 return 0;
474 put_out_dir:
475 kunmap(new_page);
476 f2fs_put_page(new_page, 0);
477 out_dir:
478 if (old_dir_entry) {
479 kunmap(old_dir_page);
480 f2fs_put_page(old_dir_page, 0);
482 f2fs_unlock_op(sbi);
483 out_old:
484 kunmap(old_page);
485 f2fs_put_page(old_page, 0);
486 out:
487 return err;
490 const struct inode_operations f2fs_dir_inode_operations = {
491 .create = f2fs_create,
492 .lookup = f2fs_lookup,
493 .link = f2fs_link,
494 .unlink = f2fs_unlink,
495 .symlink = f2fs_symlink,
496 .mkdir = f2fs_mkdir,
497 .rmdir = f2fs_rmdir,
498 .mknod = f2fs_mknod,
499 .rename = f2fs_rename,
500 .getattr = f2fs_getattr,
501 .setattr = f2fs_setattr,
502 .get_acl = f2fs_get_acl,
503 .set_acl = f2fs_set_acl,
504 #ifdef CONFIG_F2FS_FS_XATTR
505 .setxattr = generic_setxattr,
506 .getxattr = generic_getxattr,
507 .listxattr = f2fs_listxattr,
508 .removexattr = generic_removexattr,
509 #endif
512 const struct inode_operations f2fs_symlink_inode_operations = {
513 .readlink = generic_readlink,
514 .follow_link = page_follow_link_light,
515 .put_link = page_put_link,
516 .getattr = f2fs_getattr,
517 .setattr = f2fs_setattr,
518 #ifdef CONFIG_F2FS_FS_XATTR
519 .setxattr = generic_setxattr,
520 .getxattr = generic_getxattr,
521 .listxattr = f2fs_listxattr,
522 .removexattr = generic_removexattr,
523 #endif
526 const struct inode_operations f2fs_special_inode_operations = {
527 .getattr = f2fs_getattr,
528 .setattr = f2fs_setattr,
529 .get_acl = f2fs_get_acl,
530 .set_acl = f2fs_set_acl,
531 #ifdef CONFIG_F2FS_FS_XATTR
532 .setxattr = generic_setxattr,
533 .getxattr = generic_getxattr,
534 .listxattr = f2fs_listxattr,
535 .removexattr = generic_removexattr,
536 #endif