blob: 1994d2e449d6c4b9869abfa00b2bde0fd4f47df7 [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>
Chao Yu50732df2014-06-19 16:23:19 +080016#include <linux/dcache.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090017
18#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090019#include "node.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090020#include "xattr.h"
21#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090022#include <trace/events/f2fs.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090023
24static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
25{
26 struct super_block *sb = dir->i_sb;
27 struct f2fs_sb_info *sbi = F2FS_SB(sb);
28 nid_t ino;
29 struct inode *inode;
30 bool nid_free = false;
Gu Zhenge4795562013-09-27 18:08:30 +080031 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090032
33 inode = new_inode(sb);
34 if (!inode)
35 return ERR_PTR(-ENOMEM);
36
Gu Zhenge4795562013-09-27 18:08:30 +080037 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090038 if (!alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080039 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090040 err = -ENOSPC;
41 goto fail;
42 }
Gu Zhenge4795562013-09-27 18:08:30 +080043 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090044
Chao Yu70ff5df2014-05-08 17:09:30 +080045 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090046
47 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090048 inode->i_blocks = 0;
49 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
50 inode->i_generation = sbi->s_next_generation++;
51
52 err = insert_inode_locked(inode);
53 if (err) {
54 err = -EINVAL;
55 nid_free = true;
56 goto out;
57 }
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090058 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090059 mark_inode_dirty(inode);
60 return inode;
61
62out:
63 clear_nlink(inode);
64 unlock_new_inode(inode);
65fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090066 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090067 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090068 iput(inode);
69 if (nid_free)
70 alloc_nid_failed(sbi, ino);
71 return ERR_PTR(err);
72}
73
74static int is_multimedia_file(const unsigned char *s, const char *sub)
75{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020076 size_t slen = strlen(s);
77 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090078
79 if (sublen > slen)
Namjae Jeone66509f02013-04-20 01:27:21 +090080 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090081
Gu Zheng749ebfd2013-08-22 18:11:25 +080082 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090083}
84
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090085/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +090086 * Set multimedia files as cold files for hot/cold data separation
87 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090088static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +090089 const unsigned char *name)
90{
91 int i;
92 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
93
94 int count = le32_to_cpu(sbi->raw_super->extension_count);
95 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +090096 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +090097 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090098 break;
99 }
100 }
101}
102
103static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
104 bool excl)
105{
106 struct super_block *sb = dir->i_sb;
107 struct f2fs_sb_info *sbi = F2FS_SB(sb);
108 struct inode *inode;
109 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800110 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900111
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900112 f2fs_balance_fs(sbi);
113
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900114 inode = f2fs_new_inode(dir, mode);
115 if (IS_ERR(inode))
116 return PTR_ERR(inode);
117
118 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900119 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900120
121 inode->i_op = &f2fs_file_inode_operations;
122 inode->i_fop = &f2fs_file_operations;
123 inode->i_mapping->a_ops = &f2fs_dblock_aops;
124 ino = inode->i_ino;
125
Gu Zhenge4795562013-09-27 18:08:30 +0800126 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900127 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800128 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900129 if (err)
130 goto out;
131
132 alloc_nid_done(sbi, ino);
133
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900134 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900135 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900136 return 0;
137out:
138 clear_nlink(inode);
139 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900140 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900141 iput(inode);
142 alloc_nid_failed(sbi, ino);
143 return err;
144}
145
146static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
147 struct dentry *dentry)
148{
149 struct inode *inode = old_dentry->d_inode;
150 struct super_block *sb = dir->i_sb;
151 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Gu Zhenge4795562013-09-27 18:08:30 +0800152 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900153
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900154 f2fs_balance_fs(sbi);
155
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900156 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900157 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158
159 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800160 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900161 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800162 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900163 if (err)
164 goto out;
165
166 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900167 return 0;
168out:
169 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
170 iput(inode);
171 return err;
172}
173
174struct dentry *f2fs_get_parent(struct dentry *child)
175{
176 struct qstr dotdot = QSTR_INIT("..", 2);
177 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
178 if (!ino)
179 return ERR_PTR(-ENOENT);
180 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
181}
182
183static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
184 unsigned int flags)
185{
186 struct inode *inode = NULL;
187 struct f2fs_dir_entry *de;
188 struct page *page;
189
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900190 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900191 return ERR_PTR(-ENAMETOOLONG);
192
193 de = f2fs_find_entry(dir, &dentry->d_name, &page);
194 if (de) {
195 nid_t ino = le32_to_cpu(de->ino);
196 kunmap(page);
197 f2fs_put_page(page, 0);
198
199 inode = f2fs_iget(dir->i_sb, ino);
200 if (IS_ERR(inode))
201 return ERR_CAST(inode);
Chao Yu48b230a2014-04-07 11:18:34 +0800202
203 stat_inc_inline_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900204 }
205
206 return d_splice_alias(inode, dentry);
207}
208
209static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
210{
211 struct super_block *sb = dir->i_sb;
212 struct f2fs_sb_info *sbi = F2FS_SB(sb);
213 struct inode *inode = dentry->d_inode;
214 struct f2fs_dir_entry *de;
215 struct page *page;
216 int err = -ENOENT;
217
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900218 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900219 f2fs_balance_fs(sbi);
220
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900221 de = f2fs_find_entry(dir, &dentry->d_name, &page);
222 if (!de)
223 goto fail;
224
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900225 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900226 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900227 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900228 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900229 kunmap(page);
230 f2fs_put_page(page, 0);
231 goto fail;
232 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900233 f2fs_delete_entry(de, page, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800234 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900235
236 /* In order to evict this inode, we set it dirty */
237 mark_inode_dirty(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900238fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900239 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900240 return err;
241}
242
243static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
244 const char *symname)
245{
246 struct super_block *sb = dir->i_sb;
247 struct f2fs_sb_info *sbi = F2FS_SB(sb);
248 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200249 size_t symlen = strlen(symname) + 1;
Gu Zhenge4795562013-09-27 18:08:30 +0800250 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900251
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900252 f2fs_balance_fs(sbi);
253
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
255 if (IS_ERR(inode))
256 return PTR_ERR(inode);
257
258 inode->i_op = &f2fs_symlink_inode_operations;
259 inode->i_mapping->a_ops = &f2fs_dblock_aops;
260
Gu Zhenge4795562013-09-27 18:08:30 +0800261 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900262 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800263 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900264 if (err)
265 goto out;
266
267 err = page_symlink(inode, symname, symlen);
268 alloc_nid_done(sbi, inode->i_ino);
269
270 d_instantiate(dentry, inode);
271 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900272 return err;
273out:
274 clear_nlink(inode);
275 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900276 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900277 iput(inode);
278 alloc_nid_failed(sbi, inode->i_ino);
279 return err;
280}
281
282static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
283{
284 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
285 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800286 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900287
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900288 f2fs_balance_fs(sbi);
289
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900290 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900291 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900292 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900293
294 inode->i_op = &f2fs_dir_inode_operations;
295 inode->i_fop = &f2fs_dir_operations;
296 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Namjae Jeona0d42532012-12-08 14:54:18 +0900297 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900298
299 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800300 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900301 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800302 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900303 if (err)
304 goto out_fail;
305
306 alloc_nid_done(sbi, inode->i_ino);
307
308 d_instantiate(dentry, inode);
309 unlock_new_inode(inode);
310
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900311 return 0;
312
313out_fail:
314 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
315 clear_nlink(inode);
316 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900317 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900318 iput(inode);
319 alloc_nid_failed(sbi, inode->i_ino);
320 return err;
321}
322
323static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
324{
325 struct inode *inode = dentry->d_inode;
326 if (f2fs_empty_dir(inode))
327 return f2fs_unlink(dir, dentry);
328 return -ENOTEMPTY;
329}
330
331static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
332 umode_t mode, dev_t rdev)
333{
334 struct super_block *sb = dir->i_sb;
335 struct f2fs_sb_info *sbi = F2FS_SB(sb);
336 struct inode *inode;
337 int err = 0;
338
339 if (!new_valid_dev(rdev))
340 return -EINVAL;
341
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900342 f2fs_balance_fs(sbi);
343
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900344 inode = f2fs_new_inode(dir, mode);
345 if (IS_ERR(inode))
346 return PTR_ERR(inode);
347
348 init_special_inode(inode, inode->i_mode, rdev);
349 inode->i_op = &f2fs_special_inode_operations;
350
Gu Zhenge4795562013-09-27 18:08:30 +0800351 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900352 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800353 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900354 if (err)
355 goto out;
356
357 alloc_nid_done(sbi, inode->i_ino);
358 d_instantiate(dentry, inode);
359 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900360 return 0;
361out:
362 clear_nlink(inode);
363 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900364 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900365 iput(inode);
366 alloc_nid_failed(sbi, inode->i_ino);
367 return err;
368}
369
370static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
371 struct inode *new_dir, struct dentry *new_dentry)
372{
373 struct super_block *sb = old_dir->i_sb;
374 struct f2fs_sb_info *sbi = F2FS_SB(sb);
375 struct inode *old_inode = old_dentry->d_inode;
376 struct inode *new_inode = new_dentry->d_inode;
377 struct page *old_dir_page;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900378 struct page *old_page, *new_page;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900379 struct f2fs_dir_entry *old_dir_entry = NULL;
380 struct f2fs_dir_entry *old_entry;
381 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800382 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900383
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900384 f2fs_balance_fs(sbi);
385
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900386 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
387 if (!old_entry)
388 goto out;
389
390 if (S_ISDIR(old_inode->i_mode)) {
391 err = -EIO;
392 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
393 if (!old_dir_entry)
394 goto out_old;
395 }
396
Gu Zhenge4795562013-09-27 18:08:30 +0800397 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900398
399 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900400
401 err = -ENOTEMPTY;
402 if (old_dir_entry && !f2fs_empty_dir(new_inode))
403 goto out_dir;
404
405 err = -ENOENT;
406 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
407 &new_page);
408 if (!new_entry)
409 goto out_dir;
410
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900411 err = acquire_orphan_inode(sbi);
412 if (err)
413 goto put_out_dir;
414
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900415 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900416 release_orphan_inode(sbi);
417 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900418 }
419
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900420 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
421
422 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900423 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424 if (old_dir_entry)
425 drop_nlink(new_inode);
426 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900427 up_write(&F2FS_I(new_inode)->i_sem);
428
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900429 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900430
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900431 if (!new_inode->i_nlink)
432 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900433 else
434 release_orphan_inode(sbi);
435
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900436 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900437 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900438 } else {
439 err = f2fs_add_link(new_dentry, old_inode);
440 if (err)
441 goto out_dir;
442
443 if (old_dir_entry) {
444 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900445 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900446 }
447 }
448
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900449 down_write(&F2FS_I(old_inode)->i_sem);
450 file_lost_pino(old_inode);
451 up_write(&F2FS_I(old_inode)->i_sem);
452
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900453 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900454 mark_inode_dirty(old_inode);
455
456 f2fs_delete_entry(old_entry, old_page, NULL);
457
458 if (old_dir_entry) {
459 if (old_dir != new_dir) {
460 f2fs_set_link(old_inode, old_dir_entry,
461 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900462 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900463 } else {
464 kunmap(old_dir_page);
465 f2fs_put_page(old_dir_page, 0);
466 }
467 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900468 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900469 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900470 }
471
Gu Zhenge4795562013-09-27 18:08:30 +0800472 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900473 return 0;
474
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900475put_out_dir:
Chao Yudd4d9612014-06-24 14:13:13 +0800476 kunmap(new_page);
477 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900478out_dir:
479 if (old_dir_entry) {
480 kunmap(old_dir_page);
481 f2fs_put_page(old_dir_page, 0);
482 }
Gu Zhenge4795562013-09-27 18:08:30 +0800483 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900484out_old:
485 kunmap(old_page);
486 f2fs_put_page(old_page, 0);
487out:
488 return err;
489}
490
Chao Yu50732df2014-06-19 16:23:19 +0800491static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
492{
493 struct super_block *sb = dir->i_sb;
494 struct f2fs_sb_info *sbi = F2FS_SB(sb);
495 struct inode *inode;
Chao Yu50732df2014-06-19 16:23:19 +0800496 int err;
497
498 inode = f2fs_new_inode(dir, mode);
499 if (IS_ERR(inode))
500 return PTR_ERR(inode);
501
502 inode->i_op = &f2fs_file_inode_operations;
503 inode->i_fop = &f2fs_file_operations;
504 inode->i_mapping->a_ops = &f2fs_dblock_aops;
505
506 f2fs_lock_op(sbi);
507 err = acquire_orphan_inode(sbi);
508 if (err)
509 goto out;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700510
511 err = f2fs_do_tmpfile(inode, dir);
512 if (err)
513 goto release_out;
514
Chao Yu50732df2014-06-19 16:23:19 +0800515 /*
516 * add this non-linked tmpfile to orphan list, in this way we could
517 * remove all unused data of tmpfile after abnormal power-off.
518 */
519 add_orphan_inode(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800520 f2fs_unlock_op(sbi);
521
522 alloc_nid_done(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800523 d_tmpfile(dentry, inode);
524 unlock_new_inode(inode);
525 return 0;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700526
527release_out:
528 release_orphan_inode(sbi);
Chao Yu50732df2014-06-19 16:23:19 +0800529out:
530 f2fs_unlock_op(sbi);
531 clear_nlink(inode);
532 unlock_new_inode(inode);
533 make_bad_inode(inode);
534 iput(inode);
535 alloc_nid_failed(sbi, inode->i_ino);
536 return err;
537}
538
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900539const struct inode_operations f2fs_dir_inode_operations = {
540 .create = f2fs_create,
541 .lookup = f2fs_lookup,
542 .link = f2fs_link,
543 .unlink = f2fs_unlink,
544 .symlink = f2fs_symlink,
545 .mkdir = f2fs_mkdir,
546 .rmdir = f2fs_rmdir,
547 .mknod = f2fs_mknod,
548 .rename = f2fs_rename,
Chao Yu50732df2014-06-19 16:23:19 +0800549 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900550 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900551 .setattr = f2fs_setattr,
552 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800553 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900554#ifdef CONFIG_F2FS_FS_XATTR
555 .setxattr = generic_setxattr,
556 .getxattr = generic_getxattr,
557 .listxattr = f2fs_listxattr,
558 .removexattr = generic_removexattr,
559#endif
560};
561
562const struct inode_operations f2fs_symlink_inode_operations = {
563 .readlink = generic_readlink,
564 .follow_link = page_follow_link_light,
565 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900566 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900567 .setattr = f2fs_setattr,
568#ifdef CONFIG_F2FS_FS_XATTR
569 .setxattr = generic_setxattr,
570 .getxattr = generic_getxattr,
571 .listxattr = f2fs_listxattr,
572 .removexattr = generic_removexattr,
573#endif
574};
575
576const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900577 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900578 .setattr = f2fs_setattr,
579 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800580 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900581#ifdef CONFIG_F2FS_FS_XATTR
582 .setxattr = generic_setxattr,
583 .getxattr = generic_getxattr,
584 .listxattr = f2fs_listxattr,
585 .removexattr = generic_removexattr,
586#endif
587};