blob: 1a49b881bac021cefa53df578108a2a6972c3961 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +09002 * fs/f2fs/namei.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
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.
10 */
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>
16
17#include "f2fs.h"
18#include "xattr.h"
19#include "acl.h"
20
21static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
22{
23 struct super_block *sb = dir->i_sb;
24 struct f2fs_sb_info *sbi = F2FS_SB(sb);
25 nid_t ino;
26 struct inode *inode;
27 bool nid_free = false;
28 int err;
29
30 inode = new_inode(sb);
31 if (!inode)
32 return ERR_PTR(-ENOMEM);
33
34 mutex_lock_op(sbi, NODE_NEW);
35 if (!alloc_nid(sbi, &ino)) {
36 mutex_unlock_op(sbi, NODE_NEW);
37 err = -ENOSPC;
38 goto fail;
39 }
40 mutex_unlock_op(sbi, NODE_NEW);
41
42 inode->i_uid = current_fsuid();
43
44 if (dir->i_mode & S_ISGID) {
45 inode->i_gid = dir->i_gid;
46 if (S_ISDIR(mode))
47 mode |= S_ISGID;
48 } else {
49 inode->i_gid = current_fsgid();
50 }
51
52 inode->i_ino = ino;
53 inode->i_mode = mode;
54 inode->i_blocks = 0;
55 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
56 inode->i_generation = sbi->s_next_generation++;
57
58 err = insert_inode_locked(inode);
59 if (err) {
60 err = -EINVAL;
61 nid_free = true;
62 goto out;
63 }
64
65 mark_inode_dirty(inode);
66 return inode;
67
68out:
69 clear_nlink(inode);
70 unlock_new_inode(inode);
71fail:
72 iput(inode);
73 if (nid_free)
74 alloc_nid_failed(sbi, ino);
75 return ERR_PTR(err);
76}
77
78static int is_multimedia_file(const unsigned char *s, const char *sub)
79{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020080 size_t slen = strlen(s);
81 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090082 int ret;
83
84 if (sublen > slen)
85 return 1;
86
87 ret = memcmp(s + slen - sublen, sub, sublen);
88 if (ret) { /* compare upper case */
89 int i;
90 char upper_sub[8];
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);
94 }
95
96 return ret;
97}
98
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090099/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900100 * Set multimedia files as cold files for hot/cold data separation
101 */
102static inline void set_cold_file(struct f2fs_sb_info *sbi, struct inode *inode,
103 const unsigned char *name)
104{
105 int i;
106 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
107
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;
112 break;
113 }
114 }
115}
116
117static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
118 bool excl)
119{
120 struct super_block *sb = dir->i_sb;
121 struct f2fs_sb_info *sbi = F2FS_SB(sb);
122 struct inode *inode;
123 nid_t ino = 0;
124 int err;
125
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900126 f2fs_balance_fs(sbi);
127
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900128 inode = f2fs_new_inode(dir, mode);
129 if (IS_ERR(inode))
130 return PTR_ERR(inode);
131
132 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
133 set_cold_file(sbi, inode, dentry->d_name.name);
134
135 inode->i_op = &f2fs_file_inode_operations;
136 inode->i_fop = &f2fs_file_operations;
137 inode->i_mapping->a_ops = &f2fs_dblock_aops;
138 ino = inode->i_ino;
139
140 err = f2fs_add_link(dentry, inode);
141 if (err)
142 goto out;
143
144 alloc_nid_done(sbi, ino);
145
146 if (!sbi->por_doing)
147 d_instantiate(dentry, inode);
148 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900149 return 0;
150out:
151 clear_nlink(inode);
152 unlock_new_inode(inode);
153 iput(inode);
154 alloc_nid_failed(sbi, ino);
155 return err;
156}
157
158static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
159 struct dentry *dentry)
160{
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);
164 int err;
165
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900166 f2fs_balance_fs(sbi);
167
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900168 inode->i_ctime = CURRENT_TIME;
169 atomic_inc(&inode->i_count);
170
171 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
172 err = f2fs_add_link(dentry, inode);
173 if (err)
174 goto out;
175
176 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900177 return 0;
178out:
179 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
180 iput(inode);
181 return err;
182}
183
184struct dentry *f2fs_get_parent(struct dentry *child)
185{
186 struct qstr dotdot = QSTR_INIT("..", 2);
187 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
188 if (!ino)
189 return ERR_PTR(-ENOENT);
190 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
191}
192
193static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
194 unsigned int flags)
195{
196 struct inode *inode = NULL;
197 struct f2fs_dir_entry *de;
198 struct page *page;
199
200 if (dentry->d_name.len > F2FS_MAX_NAME_LEN)
201 return ERR_PTR(-ENAMETOOLONG);
202
203 de = f2fs_find_entry(dir, &dentry->d_name, &page);
204 if (de) {
205 nid_t ino = le32_to_cpu(de->ino);
206 kunmap(page);
207 f2fs_put_page(page, 0);
208
209 inode = f2fs_iget(dir->i_sb, ino);
210 if (IS_ERR(inode))
211 return ERR_CAST(inode);
212 }
213
214 return d_splice_alias(inode, dentry);
215}
216
217static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
218{
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;
223 struct page *page;
224 int err = -ENOENT;
225
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900226 f2fs_balance_fs(sbi);
227
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900228 de = f2fs_find_entry(dir, &dentry->d_name, &page);
229 if (!de)
230 goto fail;
231
232 err = check_orphan_space(sbi);
233 if (err) {
234 kunmap(page);
235 f2fs_put_page(page, 0);
236 goto fail;
237 }
238
239 f2fs_delete_entry(de, page, inode);
240
241 /* In order to evict this inode, we set it dirty */
242 mark_inode_dirty(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900243fail:
244 return err;
245}
246
247static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
248 const char *symname)
249{
250 struct super_block *sb = dir->i_sb;
251 struct f2fs_sb_info *sbi = F2FS_SB(sb);
252 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200253 size_t symlen = strlen(symname) + 1;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254 int err;
255
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900256 f2fs_balance_fs(sbi);
257
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900258 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
259 if (IS_ERR(inode))
260 return PTR_ERR(inode);
261
262 inode->i_op = &f2fs_symlink_inode_operations;
263 inode->i_mapping->a_ops = &f2fs_dblock_aops;
264
265 err = f2fs_add_link(dentry, inode);
266 if (err)
267 goto out;
268
269 err = page_symlink(inode, symname, symlen);
270 alloc_nid_done(sbi, inode->i_ino);
271
272 d_instantiate(dentry, inode);
273 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900274 return err;
275out:
276 clear_nlink(inode);
277 unlock_new_inode(inode);
278 iput(inode);
279 alloc_nid_failed(sbi, inode->i_ino);
280 return err;
281}
282
283static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
284{
285 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
286 struct inode *inode;
287 int err;
288
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900289 f2fs_balance_fs(sbi);
290
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900291 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900292 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900293 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900294
295 inode->i_op = &f2fs_dir_inode_operations;
296 inode->i_fop = &f2fs_dir_operations;
297 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Namjae Jeona0d42532012-12-08 14:54:18 +0900298 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900299
300 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
301 err = f2fs_add_link(dentry, inode);
302 if (err)
303 goto out_fail;
304
305 alloc_nid_done(sbi, inode->i_ino);
306
307 d_instantiate(dentry, inode);
308 unlock_new_inode(inode);
309
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900310 return 0;
311
312out_fail:
313 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
314 clear_nlink(inode);
315 unlock_new_inode(inode);
316 iput(inode);
317 alloc_nid_failed(sbi, inode->i_ino);
318 return err;
319}
320
321static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
322{
323 struct inode *inode = dentry->d_inode;
324 if (f2fs_empty_dir(inode))
325 return f2fs_unlink(dir, dentry);
326 return -ENOTEMPTY;
327}
328
329static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
330 umode_t mode, dev_t rdev)
331{
332 struct super_block *sb = dir->i_sb;
333 struct f2fs_sb_info *sbi = F2FS_SB(sb);
334 struct inode *inode;
335 int err = 0;
336
337 if (!new_valid_dev(rdev))
338 return -EINVAL;
339
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900340 f2fs_balance_fs(sbi);
341
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900342 inode = f2fs_new_inode(dir, mode);
343 if (IS_ERR(inode))
344 return PTR_ERR(inode);
345
346 init_special_inode(inode, inode->i_mode, rdev);
347 inode->i_op = &f2fs_special_inode_operations;
348
349 err = f2fs_add_link(dentry, inode);
350 if (err)
351 goto out;
352
353 alloc_nid_done(sbi, inode->i_ino);
354 d_instantiate(dentry, inode);
355 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900356 return 0;
357out:
358 clear_nlink(inode);
359 unlock_new_inode(inode);
360 iput(inode);
361 alloc_nid_failed(sbi, inode->i_ino);
362 return err;
363}
364
365static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
366 struct inode *new_dir, struct dentry *new_dentry)
367{
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;
377 int err = -ENOENT;
378
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900379 f2fs_balance_fs(sbi);
380
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900381 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
382 if (!old_entry)
383 goto out;
384
385 if (S_ISDIR(old_inode->i_mode)) {
386 err = -EIO;
387 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
388 if (!old_dir_entry)
389 goto out_old;
390 }
391
392 mutex_lock_op(sbi, RENAME);
393
394 if (new_inode) {
395 struct page *new_page;
396
397 err = -ENOTEMPTY;
398 if (old_dir_entry && !f2fs_empty_dir(new_inode))
399 goto out_dir;
400
401 err = -ENOENT;
402 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
403 &new_page);
404 if (!new_entry)
405 goto out_dir;
406
407 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
408
409 new_inode->i_ctime = CURRENT_TIME;
410 if (old_dir_entry)
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);
416 } else {
417 err = f2fs_add_link(new_dentry, old_inode);
418 if (err)
419 goto out_dir;
420
421 if (old_dir_entry) {
422 inc_nlink(new_dir);
423 f2fs_write_inode(new_dir, NULL);
424 }
425 }
426
427 old_inode->i_ctime = CURRENT_TIME;
428 set_inode_flag(F2FS_I(old_inode), FI_NEED_CP);
429 mark_inode_dirty(old_inode);
430
431 f2fs_delete_entry(old_entry, old_page, NULL);
432
433 if (old_dir_entry) {
434 if (old_dir != new_dir) {
435 f2fs_set_link(old_inode, old_dir_entry,
436 old_dir_page, new_dir);
437 } else {
438 kunmap(old_dir_page);
439 f2fs_put_page(old_dir_page, 0);
440 }
441 drop_nlink(old_dir);
442 f2fs_write_inode(old_dir, NULL);
443 }
444
445 mutex_unlock_op(sbi, RENAME);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900446 return 0;
447
448out_dir:
449 if (old_dir_entry) {
450 kunmap(old_dir_page);
451 f2fs_put_page(old_dir_page, 0);
452 }
453 mutex_unlock_op(sbi, RENAME);
454out_old:
455 kunmap(old_page);
456 f2fs_put_page(old_page, 0);
457out:
458 return err;
459}
460
461const struct inode_operations f2fs_dir_inode_operations = {
462 .create = f2fs_create,
463 .lookup = f2fs_lookup,
464 .link = f2fs_link,
465 .unlink = f2fs_unlink,
466 .symlink = f2fs_symlink,
467 .mkdir = f2fs_mkdir,
468 .rmdir = f2fs_rmdir,
469 .mknod = f2fs_mknod,
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,
478#endif
479};
480
481const 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,
491#endif
492};
493
494const 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,
502#endif
503};