blob: 841f6b486bd67fabeff0f0efbfb425aeaedc455e [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"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090018#include "node.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090019#include "xattr.h"
20#include "acl.h"
21
22static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
23{
24 struct super_block *sb = dir->i_sb;
25 struct f2fs_sb_info *sbi = F2FS_SB(sb);
26 nid_t ino;
27 struct inode *inode;
28 bool nid_free = false;
Jaegeuk Kim39936832012-11-22 16:21:29 +090029 int err, ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090030
31 inode = new_inode(sb);
32 if (!inode)
33 return ERR_PTR(-ENOMEM);
34
Jaegeuk Kim39936832012-11-22 16:21:29 +090035 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090036 if (!alloc_nid(sbi, &ino)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +090037 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090038 err = -ENOSPC;
39 goto fail;
40 }
Jaegeuk Kim39936832012-11-22 16:21:29 +090041 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090042
43 inode->i_uid = current_fsuid();
44
45 if (dir->i_mode & S_ISGID) {
46 inode->i_gid = dir->i_gid;
47 if (S_ISDIR(mode))
48 mode |= S_ISGID;
49 } else {
50 inode->i_gid = current_fsgid();
51 }
52
53 inode->i_ino = ino;
54 inode->i_mode = mode;
55 inode->i_blocks = 0;
56 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
57 inode->i_generation = sbi->s_next_generation++;
58
59 err = insert_inode_locked(inode);
60 if (err) {
61 err = -EINVAL;
62 nid_free = true;
63 goto out;
64 }
65
66 mark_inode_dirty(inode);
67 return inode;
68
69out:
70 clear_nlink(inode);
71 unlock_new_inode(inode);
72fail:
73 iput(inode);
74 if (nid_free)
75 alloc_nid_failed(sbi, ino);
76 return ERR_PTR(err);
77}
78
79static int is_multimedia_file(const unsigned char *s, const char *sub)
80{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020081 size_t slen = strlen(s);
82 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090083 int ret;
84
85 if (sublen > slen)
86 return 1;
87
88 ret = memcmp(s + slen - sublen, sub, sublen);
89 if (ret) { /* compare upper case */
90 int i;
91 char upper_sub[8];
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);
95 }
96
97 return ret;
98}
99
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900100/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900101 * Set multimedia files as cold files for hot/cold data separation
102 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900103static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900104 const unsigned char *name)
105{
106 int i;
107 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
108
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])) {
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900112 set_cold_file(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900113 break;
114 }
115 }
116}
117
118static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
119 bool excl)
120{
121 struct super_block *sb = dir->i_sb;
122 struct f2fs_sb_info *sbi = F2FS_SB(sb);
123 struct inode *inode;
124 nid_t ino = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900125 int err, ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900126
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900127 f2fs_balance_fs(sbi);
128
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900129 inode = f2fs_new_inode(dir, mode);
130 if (IS_ERR(inode))
131 return PTR_ERR(inode);
132
133 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900134 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900135
136 inode->i_op = &f2fs_file_inode_operations;
137 inode->i_fop = &f2fs_file_operations;
138 inode->i_mapping->a_ops = &f2fs_dblock_aops;
139 ino = inode->i_ino;
140
Jaegeuk Kim39936832012-11-22 16:21:29 +0900141 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900142 err = f2fs_add_link(dentry, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900143 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900144 if (err)
145 goto out;
146
147 alloc_nid_done(sbi, ino);
148
149 if (!sbi->por_doing)
150 d_instantiate(dentry, inode);
151 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900152 return 0;
153out:
154 clear_nlink(inode);
155 unlock_new_inode(inode);
156 iput(inode);
157 alloc_nid_failed(sbi, ino);
158 return err;
159}
160
161static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
162 struct dentry *dentry)
163{
164 struct inode *inode = old_dentry->d_inode;
165 struct super_block *sb = dir->i_sb;
166 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900167 int err, ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900168
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900169 f2fs_balance_fs(sbi);
170
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900171 inode->i_ctime = CURRENT_TIME;
172 atomic_inc(&inode->i_count);
173
174 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900175 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900176 err = f2fs_add_link(dentry, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900177 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900178 if (err)
179 goto out;
180
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900181 /*
182 * This file should be checkpointed during fsync.
183 * We lost i_pino from now on.
184 */
185 set_cp_file(inode);
186
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900187 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900188 return 0;
189out:
190 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
191 iput(inode);
192 return err;
193}
194
195struct dentry *f2fs_get_parent(struct dentry *child)
196{
197 struct qstr dotdot = QSTR_INIT("..", 2);
198 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
199 if (!ino)
200 return ERR_PTR(-ENOENT);
201 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
202}
203
204static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
205 unsigned int flags)
206{
207 struct inode *inode = NULL;
208 struct f2fs_dir_entry *de;
209 struct page *page;
210
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900211 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900212 return ERR_PTR(-ENAMETOOLONG);
213
214 de = f2fs_find_entry(dir, &dentry->d_name, &page);
215 if (de) {
216 nid_t ino = le32_to_cpu(de->ino);
217 kunmap(page);
218 f2fs_put_page(page, 0);
219
220 inode = f2fs_iget(dir->i_sb, ino);
221 if (IS_ERR(inode))
222 return ERR_CAST(inode);
223 }
224
225 return d_splice_alias(inode, dentry);
226}
227
228static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
229{
230 struct super_block *sb = dir->i_sb;
231 struct f2fs_sb_info *sbi = F2FS_SB(sb);
232 struct inode *inode = dentry->d_inode;
233 struct f2fs_dir_entry *de;
234 struct page *page;
235 int err = -ENOENT;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900236 int ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900237
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900238 f2fs_balance_fs(sbi);
239
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900240 de = f2fs_find_entry(dir, &dentry->d_name, &page);
241 if (!de)
242 goto fail;
243
244 err = check_orphan_space(sbi);
245 if (err) {
246 kunmap(page);
247 f2fs_put_page(page, 0);
248 goto fail;
249 }
250
Jaegeuk Kim39936832012-11-22 16:21:29 +0900251 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900252 f2fs_delete_entry(de, page, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900253 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254
255 /* In order to evict this inode, we set it dirty */
256 mark_inode_dirty(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900257fail:
258 return err;
259}
260
261static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
262 const char *symname)
263{
264 struct super_block *sb = dir->i_sb;
265 struct f2fs_sb_info *sbi = F2FS_SB(sb);
266 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200267 size_t symlen = strlen(symname) + 1;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900268 int err, ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900269
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900270 f2fs_balance_fs(sbi);
271
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900272 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
273 if (IS_ERR(inode))
274 return PTR_ERR(inode);
275
276 inode->i_op = &f2fs_symlink_inode_operations;
277 inode->i_mapping->a_ops = &f2fs_dblock_aops;
278
Jaegeuk Kim39936832012-11-22 16:21:29 +0900279 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900280 err = f2fs_add_link(dentry, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900281 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900282 if (err)
283 goto out;
284
285 err = page_symlink(inode, symname, symlen);
286 alloc_nid_done(sbi, inode->i_ino);
287
288 d_instantiate(dentry, inode);
289 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900290 return err;
291out:
292 clear_nlink(inode);
293 unlock_new_inode(inode);
294 iput(inode);
295 alloc_nid_failed(sbi, inode->i_ino);
296 return err;
297}
298
299static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
300{
301 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
302 struct inode *inode;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900303 int err, ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900304
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900305 f2fs_balance_fs(sbi);
306
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900307 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900308 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900309 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900310
311 inode->i_op = &f2fs_dir_inode_operations;
312 inode->i_fop = &f2fs_dir_operations;
313 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Namjae Jeona0d42532012-12-08 14:54:18 +0900314 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900315
316 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900317 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900318 err = f2fs_add_link(dentry, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900319 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900320 if (err)
321 goto out_fail;
322
323 alloc_nid_done(sbi, inode->i_ino);
324
325 d_instantiate(dentry, inode);
326 unlock_new_inode(inode);
327
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900328 return 0;
329
330out_fail:
331 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
332 clear_nlink(inode);
333 unlock_new_inode(inode);
334 iput(inode);
335 alloc_nid_failed(sbi, inode->i_ino);
336 return err;
337}
338
339static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
340{
341 struct inode *inode = dentry->d_inode;
342 if (f2fs_empty_dir(inode))
343 return f2fs_unlink(dir, dentry);
344 return -ENOTEMPTY;
345}
346
347static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
348 umode_t mode, dev_t rdev)
349{
350 struct super_block *sb = dir->i_sb;
351 struct f2fs_sb_info *sbi = F2FS_SB(sb);
352 struct inode *inode;
353 int err = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900354 int ilock;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900355
356 if (!new_valid_dev(rdev))
357 return -EINVAL;
358
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900359 f2fs_balance_fs(sbi);
360
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361 inode = f2fs_new_inode(dir, mode);
362 if (IS_ERR(inode))
363 return PTR_ERR(inode);
364
365 init_special_inode(inode, inode->i_mode, rdev);
366 inode->i_op = &f2fs_special_inode_operations;
367
Jaegeuk Kim39936832012-11-22 16:21:29 +0900368 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900369 err = f2fs_add_link(dentry, inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900370 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900371 if (err)
372 goto out;
373
374 alloc_nid_done(sbi, inode->i_ino);
375 d_instantiate(dentry, inode);
376 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900377 return 0;
378out:
379 clear_nlink(inode);
380 unlock_new_inode(inode);
381 iput(inode);
382 alloc_nid_failed(sbi, inode->i_ino);
383 return err;
384}
385
386static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
387 struct inode *new_dir, struct dentry *new_dentry)
388{
389 struct super_block *sb = old_dir->i_sb;
390 struct f2fs_sb_info *sbi = F2FS_SB(sb);
391 struct inode *old_inode = old_dentry->d_inode;
392 struct inode *new_inode = new_dentry->d_inode;
393 struct page *old_dir_page;
394 struct page *old_page;
395 struct f2fs_dir_entry *old_dir_entry = NULL;
396 struct f2fs_dir_entry *old_entry;
397 struct f2fs_dir_entry *new_entry;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900398 int err = -ENOENT, ilock = -1;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900399
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900400 f2fs_balance_fs(sbi);
401
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900402 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
403 if (!old_entry)
404 goto out;
405
406 if (S_ISDIR(old_inode->i_mode)) {
407 err = -EIO;
408 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
409 if (!old_dir_entry)
410 goto out_old;
411 }
412
Jaegeuk Kim39936832012-11-22 16:21:29 +0900413 ilock = mutex_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900414
415 if (new_inode) {
416 struct page *new_page;
417
418 err = -ENOTEMPTY;
419 if (old_dir_entry && !f2fs_empty_dir(new_inode))
420 goto out_dir;
421
422 err = -ENOENT;
423 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
424 &new_page);
425 if (!new_entry)
426 goto out_dir;
427
428 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
429
430 new_inode->i_ctime = CURRENT_TIME;
431 if (old_dir_entry)
432 drop_nlink(new_inode);
433 drop_nlink(new_inode);
434 if (!new_inode->i_nlink)
435 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900436 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900437 } else {
438 err = f2fs_add_link(new_dentry, old_inode);
439 if (err)
440 goto out_dir;
441
442 if (old_dir_entry) {
443 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900444 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900445 }
446 }
447
448 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900449 mark_inode_dirty(old_inode);
450
451 f2fs_delete_entry(old_entry, old_page, NULL);
452
453 if (old_dir_entry) {
454 if (old_dir != new_dir) {
455 f2fs_set_link(old_inode, old_dir_entry,
456 old_dir_page, new_dir);
457 } else {
458 kunmap(old_dir_page);
459 f2fs_put_page(old_dir_page, 0);
460 }
461 drop_nlink(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900462 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900463 }
464
Jaegeuk Kim39936832012-11-22 16:21:29 +0900465 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900466 return 0;
467
468out_dir:
469 if (old_dir_entry) {
470 kunmap(old_dir_page);
471 f2fs_put_page(old_dir_page, 0);
472 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900473 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900474out_old:
475 kunmap(old_page);
476 f2fs_put_page(old_page, 0);
477out:
478 return err;
479}
480
481const struct inode_operations f2fs_dir_inode_operations = {
482 .create = f2fs_create,
483 .lookup = f2fs_lookup,
484 .link = f2fs_link,
485 .unlink = f2fs_unlink,
486 .symlink = f2fs_symlink,
487 .mkdir = f2fs_mkdir,
488 .rmdir = f2fs_rmdir,
489 .mknod = f2fs_mknod,
490 .rename = f2fs_rename,
491 .setattr = f2fs_setattr,
492 .get_acl = f2fs_get_acl,
493#ifdef CONFIG_F2FS_FS_XATTR
494 .setxattr = generic_setxattr,
495 .getxattr = generic_getxattr,
496 .listxattr = f2fs_listxattr,
497 .removexattr = generic_removexattr,
498#endif
499};
500
501const struct inode_operations f2fs_symlink_inode_operations = {
502 .readlink = generic_readlink,
503 .follow_link = page_follow_link_light,
504 .put_link = page_put_link,
505 .setattr = f2fs_setattr,
506#ifdef CONFIG_F2FS_FS_XATTR
507 .setxattr = generic_setxattr,
508 .getxattr = generic_getxattr,
509 .listxattr = f2fs_listxattr,
510 .removexattr = generic_removexattr,
511#endif
512};
513
514const struct inode_operations f2fs_special_inode_operations = {
515 .setattr = f2fs_setattr,
516 .get_acl = f2fs_get_acl,
517#ifdef CONFIG_F2FS_FS_XATTR
518 .setxattr = generic_setxattr,
519 .getxattr = generic_getxattr,
520 .listxattr = f2fs_listxattr,
521 .removexattr = generic_removexattr,
522#endif
523};