Merge tag 'blackfin-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/realm...
[linux-2.6.git] / fs / f2fs / namei.c
blob64c07169df050b4b358186540031365bd67f4b56
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, ilock;
32 inode = new_inode(sb);
33 if (!inode)
34 return ERR_PTR(-ENOMEM);
36 ilock = mutex_lock_op(sbi);
37 if (!alloc_nid(sbi, &ino)) {
38 mutex_unlock_op(sbi, ilock);
39 err = -ENOSPC;
40 goto fail;
42 mutex_unlock_op(sbi, ilock);
44 inode->i_uid = current_fsuid();
46 if (dir->i_mode & S_ISGID) {
47 inode->i_gid = dir->i_gid;
48 if (S_ISDIR(mode))
49 mode |= S_ISGID;
50 } else {
51 inode->i_gid = current_fsgid();
54 inode->i_ino = ino;
55 inode->i_mode = mode;
56 inode->i_blocks = 0;
57 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
58 inode->i_generation = sbi->s_next_generation++;
60 err = insert_inode_locked(inode);
61 if (err) {
62 err = -EINVAL;
63 nid_free = true;
64 goto out;
66 trace_f2fs_new_inode(inode, 0);
67 mark_inode_dirty(inode);
68 return inode;
70 out:
71 clear_nlink(inode);
72 unlock_new_inode(inode);
73 fail:
74 trace_f2fs_new_inode(inode, err);
75 make_bad_inode(inode);
76 iput(inode);
77 if (nid_free)
78 alloc_nid_failed(sbi, ino);
79 return ERR_PTR(err);
82 static int is_multimedia_file(const unsigned char *s, const char *sub)
84 size_t slen = strlen(s);
85 size_t sublen = strlen(sub);
86 int ret;
88 if (sublen > slen)
89 return 0;
91 ret = memcmp(s + slen - sublen, sub, sublen);
92 if (ret) { /* compare upper case */
93 int i;
94 char upper_sub[8];
95 for (i = 0; i < sublen && i < sizeof(upper_sub); i++)
96 upper_sub[i] = toupper(sub[i]);
97 return !memcmp(s + slen - sublen, upper_sub, sublen);
100 return !ret;
104 * Set multimedia files as cold files for hot/cold data separation
106 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
107 const unsigned char *name)
109 int i;
110 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
112 int count = le32_to_cpu(sbi->raw_super->extension_count);
113 for (i = 0; i < count; i++) {
114 if (is_multimedia_file(name, extlist[i])) {
115 file_set_cold(inode);
116 break;
121 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
122 bool excl)
124 struct super_block *sb = dir->i_sb;
125 struct f2fs_sb_info *sbi = F2FS_SB(sb);
126 struct inode *inode;
127 nid_t ino = 0;
128 int err, ilock;
130 f2fs_balance_fs(sbi);
132 inode = f2fs_new_inode(dir, mode);
133 if (IS_ERR(inode))
134 return PTR_ERR(inode);
136 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
137 set_cold_files(sbi, inode, dentry->d_name.name);
139 inode->i_op = &f2fs_file_inode_operations;
140 inode->i_fop = &f2fs_file_operations;
141 inode->i_mapping->a_ops = &f2fs_dblock_aops;
142 ino = inode->i_ino;
144 ilock = mutex_lock_op(sbi);
145 err = f2fs_add_link(dentry, inode);
146 mutex_unlock_op(sbi, ilock);
147 if (err)
148 goto out;
150 alloc_nid_done(sbi, ino);
152 d_instantiate(dentry, inode);
153 unlock_new_inode(inode);
154 return 0;
155 out:
156 clear_nlink(inode);
157 unlock_new_inode(inode);
158 make_bad_inode(inode);
159 iput(inode);
160 alloc_nid_failed(sbi, ino);
161 return err;
164 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
165 struct dentry *dentry)
167 struct inode *inode = old_dentry->d_inode;
168 struct super_block *sb = dir->i_sb;
169 struct f2fs_sb_info *sbi = F2FS_SB(sb);
170 int err, ilock;
172 f2fs_balance_fs(sbi);
174 inode->i_ctime = CURRENT_TIME;
175 ihold(inode);
177 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
178 ilock = mutex_lock_op(sbi);
179 err = f2fs_add_link(dentry, inode);
180 mutex_unlock_op(sbi, ilock);
181 if (err)
182 goto out;
184 d_instantiate(dentry, inode);
185 return 0;
186 out:
187 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
188 iput(inode);
189 return err;
192 struct dentry *f2fs_get_parent(struct dentry *child)
194 struct qstr dotdot = QSTR_INIT("..", 2);
195 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
196 if (!ino)
197 return ERR_PTR(-ENOENT);
198 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
201 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
202 unsigned int flags)
204 struct inode *inode = NULL;
205 struct f2fs_dir_entry *de;
206 struct page *page;
208 if (dentry->d_name.len > F2FS_NAME_LEN)
209 return ERR_PTR(-ENAMETOOLONG);
211 de = f2fs_find_entry(dir, &dentry->d_name, &page);
212 if (de) {
213 nid_t ino = le32_to_cpu(de->ino);
214 kunmap(page);
215 f2fs_put_page(page, 0);
217 inode = f2fs_iget(dir->i_sb, ino);
218 if (IS_ERR(inode))
219 return ERR_CAST(inode);
222 return d_splice_alias(inode, dentry);
225 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
227 struct super_block *sb = dir->i_sb;
228 struct f2fs_sb_info *sbi = F2FS_SB(sb);
229 struct inode *inode = dentry->d_inode;
230 struct f2fs_dir_entry *de;
231 struct page *page;
232 int err = -ENOENT;
233 int ilock;
235 trace_f2fs_unlink_enter(dir, dentry);
236 f2fs_balance_fs(sbi);
238 de = f2fs_find_entry(dir, &dentry->d_name, &page);
239 if (!de)
240 goto fail;
242 err = check_orphan_space(sbi);
243 if (err) {
244 kunmap(page);
245 f2fs_put_page(page, 0);
246 goto fail;
249 ilock = mutex_lock_op(sbi);
250 f2fs_delete_entry(de, page, inode);
251 mutex_unlock_op(sbi, ilock);
253 /* In order to evict this inode, we set it dirty */
254 mark_inode_dirty(inode);
255 fail:
256 trace_f2fs_unlink_exit(inode, err);
257 return err;
260 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
261 const char *symname)
263 struct super_block *sb = dir->i_sb;
264 struct f2fs_sb_info *sbi = F2FS_SB(sb);
265 struct inode *inode;
266 size_t symlen = strlen(symname) + 1;
267 int err, ilock;
269 f2fs_balance_fs(sbi);
271 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
272 if (IS_ERR(inode))
273 return PTR_ERR(inode);
275 inode->i_op = &f2fs_symlink_inode_operations;
276 inode->i_mapping->a_ops = &f2fs_dblock_aops;
278 ilock = mutex_lock_op(sbi);
279 err = f2fs_add_link(dentry, inode);
280 mutex_unlock_op(sbi, ilock);
281 if (err)
282 goto out;
284 err = page_symlink(inode, symname, symlen);
285 alloc_nid_done(sbi, inode->i_ino);
287 d_instantiate(dentry, inode);
288 unlock_new_inode(inode);
289 return err;
290 out:
291 clear_nlink(inode);
292 unlock_new_inode(inode);
293 make_bad_inode(inode);
294 iput(inode);
295 alloc_nid_failed(sbi, inode->i_ino);
296 return err;
299 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
301 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
302 struct inode *inode;
303 int err, ilock;
305 f2fs_balance_fs(sbi);
307 inode = f2fs_new_inode(dir, S_IFDIR | mode);
308 if (IS_ERR(inode))
309 return PTR_ERR(inode);
311 inode->i_op = &f2fs_dir_inode_operations;
312 inode->i_fop = &f2fs_dir_operations;
313 inode->i_mapping->a_ops = &f2fs_dblock_aops;
314 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
316 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
317 ilock = mutex_lock_op(sbi);
318 err = f2fs_add_link(dentry, inode);
319 mutex_unlock_op(sbi, ilock);
320 if (err)
321 goto out_fail;
323 alloc_nid_done(sbi, inode->i_ino);
325 d_instantiate(dentry, inode);
326 unlock_new_inode(inode);
328 return 0;
330 out_fail:
331 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
332 clear_nlink(inode);
333 unlock_new_inode(inode);
334 make_bad_inode(inode);
335 iput(inode);
336 alloc_nid_failed(sbi, inode->i_ino);
337 return err;
340 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
342 struct inode *inode = dentry->d_inode;
343 if (f2fs_empty_dir(inode))
344 return f2fs_unlink(dir, dentry);
345 return -ENOTEMPTY;
348 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
349 umode_t mode, dev_t rdev)
351 struct super_block *sb = dir->i_sb;
352 struct f2fs_sb_info *sbi = F2FS_SB(sb);
353 struct inode *inode;
354 int err = 0;
355 int ilock;
357 if (!new_valid_dev(rdev))
358 return -EINVAL;
360 f2fs_balance_fs(sbi);
362 inode = f2fs_new_inode(dir, mode);
363 if (IS_ERR(inode))
364 return PTR_ERR(inode);
366 init_special_inode(inode, inode->i_mode, rdev);
367 inode->i_op = &f2fs_special_inode_operations;
369 ilock = mutex_lock_op(sbi);
370 err = f2fs_add_link(dentry, inode);
371 mutex_unlock_op(sbi, ilock);
372 if (err)
373 goto out;
375 alloc_nid_done(sbi, inode->i_ino);
376 d_instantiate(dentry, inode);
377 unlock_new_inode(inode);
378 return 0;
379 out:
380 clear_nlink(inode);
381 unlock_new_inode(inode);
382 make_bad_inode(inode);
383 iput(inode);
384 alloc_nid_failed(sbi, inode->i_ino);
385 return err;
388 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
389 struct inode *new_dir, struct dentry *new_dentry)
391 struct super_block *sb = old_dir->i_sb;
392 struct f2fs_sb_info *sbi = F2FS_SB(sb);
393 struct inode *old_inode = old_dentry->d_inode;
394 struct inode *new_inode = new_dentry->d_inode;
395 struct page *old_dir_page;
396 struct page *old_page;
397 struct f2fs_dir_entry *old_dir_entry = NULL;
398 struct f2fs_dir_entry *old_entry;
399 struct f2fs_dir_entry *new_entry;
400 int err = -ENOENT, ilock = -1;
402 f2fs_balance_fs(sbi);
404 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
405 if (!old_entry)
406 goto out;
408 if (S_ISDIR(old_inode->i_mode)) {
409 err = -EIO;
410 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
411 if (!old_dir_entry)
412 goto out_old;
415 ilock = mutex_lock_op(sbi);
417 if (new_inode) {
418 struct page *new_page;
420 err = -ENOTEMPTY;
421 if (old_dir_entry && !f2fs_empty_dir(new_inode))
422 goto out_dir;
424 err = -ENOENT;
425 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
426 &new_page);
427 if (!new_entry)
428 goto out_dir;
430 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
432 new_inode->i_ctime = CURRENT_TIME;
433 if (old_dir_entry)
434 drop_nlink(new_inode);
435 drop_nlink(new_inode);
436 if (!new_inode->i_nlink)
437 add_orphan_inode(sbi, new_inode->i_ino);
438 update_inode_page(new_inode);
439 } else {
440 err = f2fs_add_link(new_dentry, old_inode);
441 if (err)
442 goto out_dir;
444 if (old_dir_entry) {
445 inc_nlink(new_dir);
446 update_inode_page(new_dir);
450 old_inode->i_ctime = CURRENT_TIME;
451 mark_inode_dirty(old_inode);
453 f2fs_delete_entry(old_entry, old_page, NULL);
455 if (old_dir_entry) {
456 if (old_dir != new_dir) {
457 f2fs_set_link(old_inode, old_dir_entry,
458 old_dir_page, new_dir);
459 } else {
460 kunmap(old_dir_page);
461 f2fs_put_page(old_dir_page, 0);
463 drop_nlink(old_dir);
464 update_inode_page(old_dir);
467 mutex_unlock_op(sbi, ilock);
468 return 0;
470 out_dir:
471 if (old_dir_entry) {
472 kunmap(old_dir_page);
473 f2fs_put_page(old_dir_page, 0);
475 mutex_unlock_op(sbi, ilock);
476 out_old:
477 kunmap(old_page);
478 f2fs_put_page(old_page, 0);
479 out:
480 return err;
483 const struct inode_operations f2fs_dir_inode_operations = {
484 .create = f2fs_create,
485 .lookup = f2fs_lookup,
486 .link = f2fs_link,
487 .unlink = f2fs_unlink,
488 .symlink = f2fs_symlink,
489 .mkdir = f2fs_mkdir,
490 .rmdir = f2fs_rmdir,
491 .mknod = f2fs_mknod,
492 .rename = f2fs_rename,
493 .getattr = f2fs_getattr,
494 .setattr = f2fs_setattr,
495 .get_acl = f2fs_get_acl,
496 #ifdef CONFIG_F2FS_FS_XATTR
497 .setxattr = generic_setxattr,
498 .getxattr = generic_getxattr,
499 .listxattr = f2fs_listxattr,
500 .removexattr = generic_removexattr,
501 #endif
504 const struct inode_operations f2fs_symlink_inode_operations = {
505 .readlink = generic_readlink,
506 .follow_link = page_follow_link_light,
507 .put_link = page_put_link,
508 .getattr = f2fs_getattr,
509 .setattr = f2fs_setattr,
510 #ifdef CONFIG_F2FS_FS_XATTR
511 .setxattr = generic_setxattr,
512 .getxattr = generic_getxattr,
513 .listxattr = f2fs_listxattr,
514 .removexattr = generic_removexattr,
515 #endif
518 const struct inode_operations f2fs_special_inode_operations = {
519 .getattr = f2fs_getattr,
520 .setattr = f2fs_setattr,
521 .get_acl = f2fs_get_acl,
522 #ifdef CONFIG_F2FS_FS_XATTR
523 .setxattr = generic_setxattr,
524 .getxattr = generic_getxattr,
525 .listxattr = f2fs_listxattr,
526 .removexattr = generic_removexattr,
527 #endif