blob: 0d2526e5aa11401016a985681bff7036bbe54e48 [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{
Jaegeuk Kim40813632014-09-02 15:31:18 -070026 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090027 nid_t ino;
28 struct inode *inode;
29 bool nid_free = false;
Gu Zhenge4795562013-09-27 18:08:30 +080030 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090031
Jaegeuk Kima014e032014-06-20 21:44:02 -070032 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090033 if (!inode)
34 return ERR_PTR(-ENOMEM);
35
Gu Zhenge4795562013-09-27 18:08:30 +080036 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090037 if (!alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080038 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090039 err = -ENOSPC;
40 goto fail;
41 }
Gu Zhenge4795562013-09-27 18:08:30 +080042 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090043
Chao Yu70ff5df2014-05-08 17:09:30 +080044 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090045
46 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090047 inode->i_blocks = 0;
48 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
49 inode->i_generation = sbi->s_next_generation++;
50
51 err = insert_inode_locked(inode);
52 if (err) {
53 err = -EINVAL;
54 nid_free = true;
55 goto out;
56 }
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090057 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090058 mark_inode_dirty(inode);
59 return inode;
60
61out:
62 clear_nlink(inode);
63 unlock_new_inode(inode);
64fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090065 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090066 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090067 iput(inode);
68 if (nid_free)
69 alloc_nid_failed(sbi, ino);
70 return ERR_PTR(err);
71}
72
73static int is_multimedia_file(const unsigned char *s, const char *sub)
74{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020075 size_t slen = strlen(s);
76 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090077
78 if (sublen > slen)
Namjae Jeone66509f02013-04-20 01:27:21 +090079 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090080
Gu Zheng749ebfd2013-08-22 18:11:25 +080081 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090082}
83
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090084/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +090085 * Set multimedia files as cold files for hot/cold data separation
86 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090087static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +090088 const unsigned char *name)
89{
90 int i;
91 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
92
93 int count = le32_to_cpu(sbi->raw_super->extension_count);
94 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +090095 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +090096 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090097 break;
98 }
99 }
100}
101
102static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
103 bool excl)
104{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700105 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900106 struct inode *inode;
107 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800108 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900109
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900110 f2fs_balance_fs(sbi);
111
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900112 inode = f2fs_new_inode(dir, mode);
113 if (IS_ERR(inode))
114 return PTR_ERR(inode);
115
116 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900117 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900118
119 inode->i_op = &f2fs_file_inode_operations;
120 inode->i_fop = &f2fs_file_operations;
121 inode->i_mapping->a_ops = &f2fs_dblock_aops;
122 ino = inode->i_ino;
123
Gu Zhenge4795562013-09-27 18:08:30 +0800124 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900125 err = f2fs_add_link(dentry, inode);
126 if (err)
127 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700128 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900129
130 alloc_nid_done(sbi, ino);
131
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900132 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900133 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900134 return 0;
135out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700136 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900137 return err;
138}
139
140static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
141 struct dentry *dentry)
142{
143 struct inode *inode = old_dentry->d_inode;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700144 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800145 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900146
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900147 f2fs_balance_fs(sbi);
148
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900149 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900150 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900151
152 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800153 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900154 err = f2fs_add_link(dentry, inode);
155 if (err)
156 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700157 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158
159 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900160 return 0;
161out:
162 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
163 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700164 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165 return err;
166}
167
168struct dentry *f2fs_get_parent(struct dentry *child)
169{
170 struct qstr dotdot = QSTR_INIT("..", 2);
171 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
172 if (!ino)
173 return ERR_PTR(-ENOENT);
174 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
175}
176
177static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
178 unsigned int flags)
179{
180 struct inode *inode = NULL;
181 struct f2fs_dir_entry *de;
182 struct page *page;
183
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900184 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900185 return ERR_PTR(-ENAMETOOLONG);
186
187 de = f2fs_find_entry(dir, &dentry->d_name, &page);
188 if (de) {
189 nid_t ino = le32_to_cpu(de->ino);
190 kunmap(page);
191 f2fs_put_page(page, 0);
192
193 inode = f2fs_iget(dir->i_sb, ino);
194 if (IS_ERR(inode))
195 return ERR_CAST(inode);
Chao Yu48b230a2014-04-07 11:18:34 +0800196
197 stat_inc_inline_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900198 }
199
200 return d_splice_alias(inode, dentry);
201}
202
203static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
204{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700205 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900206 struct inode *inode = dentry->d_inode;
207 struct f2fs_dir_entry *de;
208 struct page *page;
209 int err = -ENOENT;
210
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900211 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900212 f2fs_balance_fs(sbi);
213
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900214 de = f2fs_find_entry(dir, &dentry->d_name, &page);
215 if (!de)
216 goto fail;
217
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900218 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900219 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900220 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900221 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900222 kunmap(page);
223 f2fs_put_page(page, 0);
224 goto fail;
225 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900226 f2fs_delete_entry(de, page, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800227 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900228
arter97e1c42042014-08-06 23:22:50 +0900229 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900230 mark_inode_dirty(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900231fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900232 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900233 return err;
234}
235
236static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
237 const char *symname)
238{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700239 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900240 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200241 size_t symlen = strlen(symname) + 1;
Gu Zhenge4795562013-09-27 18:08:30 +0800242 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900243
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900244 f2fs_balance_fs(sbi);
245
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900246 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
247 if (IS_ERR(inode))
248 return PTR_ERR(inode);
249
250 inode->i_op = &f2fs_symlink_inode_operations;
251 inode->i_mapping->a_ops = &f2fs_dblock_aops;
252
Gu Zhenge4795562013-09-27 18:08:30 +0800253 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254 err = f2fs_add_link(dentry, inode);
255 if (err)
256 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700257 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900258
259 err = page_symlink(inode, symname, symlen);
260 alloc_nid_done(sbi, inode->i_ino);
261
262 d_instantiate(dentry, inode);
263 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900264 return err;
265out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700266 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900267 return err;
268}
269
270static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
271{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700272 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900273 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800274 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900275
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900276 f2fs_balance_fs(sbi);
277
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900278 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900279 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900280 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900281
282 inode->i_op = &f2fs_dir_inode_operations;
283 inode->i_fop = &f2fs_dir_operations;
284 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Namjae Jeona0d42532012-12-08 14:54:18 +0900285 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900286
287 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800288 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900289 err = f2fs_add_link(dentry, inode);
290 if (err)
291 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700292 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900293
294 alloc_nid_done(sbi, inode->i_ino);
295
296 d_instantiate(dentry, inode);
297 unlock_new_inode(inode);
298
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900299 return 0;
300
301out_fail:
302 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700303 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900304 return err;
305}
306
307static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
308{
309 struct inode *inode = dentry->d_inode;
310 if (f2fs_empty_dir(inode))
311 return f2fs_unlink(dir, dentry);
312 return -ENOTEMPTY;
313}
314
315static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
316 umode_t mode, dev_t rdev)
317{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700318 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900319 struct inode *inode;
320 int err = 0;
321
322 if (!new_valid_dev(rdev))
323 return -EINVAL;
324
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900325 f2fs_balance_fs(sbi);
326
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900327 inode = f2fs_new_inode(dir, mode);
328 if (IS_ERR(inode))
329 return PTR_ERR(inode);
330
331 init_special_inode(inode, inode->i_mode, rdev);
332 inode->i_op = &f2fs_special_inode_operations;
333
Gu Zhenge4795562013-09-27 18:08:30 +0800334 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900335 err = f2fs_add_link(dentry, inode);
336 if (err)
337 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700338 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900339
340 alloc_nid_done(sbi, inode->i_ino);
341 d_instantiate(dentry, inode);
342 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900343 return 0;
344out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700345 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346 return err;
347}
348
349static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
350 struct inode *new_dir, struct dentry *new_dentry)
351{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700352 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900353 struct inode *old_inode = old_dentry->d_inode;
354 struct inode *new_inode = new_dentry->d_inode;
355 struct page *old_dir_page;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900356 struct page *old_page, *new_page;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900357 struct f2fs_dir_entry *old_dir_entry = NULL;
358 struct f2fs_dir_entry *old_entry;
359 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800360 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900362 f2fs_balance_fs(sbi);
363
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900364 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
365 if (!old_entry)
366 goto out;
367
368 if (S_ISDIR(old_inode->i_mode)) {
369 err = -EIO;
370 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
371 if (!old_dir_entry)
372 goto out_old;
373 }
374
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900375 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900376
377 err = -ENOTEMPTY;
378 if (old_dir_entry && !f2fs_empty_dir(new_inode))
379 goto out_dir;
380
381 err = -ENOENT;
382 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
383 &new_page);
384 if (!new_entry)
385 goto out_dir;
386
Chao Yu12560102014-06-24 14:16:24 +0800387 f2fs_lock_op(sbi);
388
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900389 err = acquire_orphan_inode(sbi);
390 if (err)
391 goto put_out_dir;
392
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900393 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900394 release_orphan_inode(sbi);
395 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900396 }
397
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900398 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
399
400 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900401 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900402 if (old_dir_entry)
403 drop_nlink(new_inode);
404 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900405 up_write(&F2FS_I(new_inode)->i_sem);
406
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900407 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900408
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900409 if (!new_inode->i_nlink)
410 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900411 else
412 release_orphan_inode(sbi);
413
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900414 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900415 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900416 } else {
Chao Yu12560102014-06-24 14:16:24 +0800417 f2fs_lock_op(sbi);
418
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900419 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800420 if (err) {
421 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900422 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +0800423 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424
425 if (old_dir_entry) {
426 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900427 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900428 }
429 }
430
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900431 down_write(&F2FS_I(old_inode)->i_sem);
432 file_lost_pino(old_inode);
433 up_write(&F2FS_I(old_inode)->i_sem);
434
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900435 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900436 mark_inode_dirty(old_inode);
437
438 f2fs_delete_entry(old_entry, old_page, NULL);
439
440 if (old_dir_entry) {
441 if (old_dir != new_dir) {
442 f2fs_set_link(old_inode, old_dir_entry,
443 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900444 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900445 } else {
446 kunmap(old_dir_page);
447 f2fs_put_page(old_dir_page, 0);
448 }
449 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900450 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900451 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900452 }
453
Gu Zhenge4795562013-09-27 18:08:30 +0800454 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900455 return 0;
456
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900457put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800458 f2fs_unlock_op(sbi);
Chao Yudd4d9612014-06-24 14:13:13 +0800459 kunmap(new_page);
460 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900461out_dir:
462 if (old_dir_entry) {
463 kunmap(old_dir_page);
464 f2fs_put_page(old_dir_page, 0);
465 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900466out_old:
467 kunmap(old_page);
468 f2fs_put_page(old_page, 0);
469out:
470 return err;
471}
472
Chao Yu32f9bc22014-07-12 19:13:54 +0800473static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
474 struct inode *new_dir, struct dentry *new_dentry)
475{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700476 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +0800477 struct inode *old_inode = old_dentry->d_inode;
478 struct inode *new_inode = new_dentry->d_inode;
479 struct page *old_dir_page, *new_dir_page;
480 struct page *old_page, *new_page;
481 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
482 struct f2fs_dir_entry *old_entry, *new_entry;
483 int old_nlink = 0, new_nlink = 0;
484 int err = -ENOENT;
485
486 f2fs_balance_fs(sbi);
487
488 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
489 if (!old_entry)
490 goto out;
491
492 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
493 if (!new_entry)
494 goto out_old;
495
496 /* prepare for updating ".." directory entry info later */
497 if (old_dir != new_dir) {
498 if (S_ISDIR(old_inode->i_mode)) {
499 err = -EIO;
500 old_dir_entry = f2fs_parent_dir(old_inode,
501 &old_dir_page);
502 if (!old_dir_entry)
503 goto out_new;
504 }
505
506 if (S_ISDIR(new_inode->i_mode)) {
507 err = -EIO;
508 new_dir_entry = f2fs_parent_dir(new_inode,
509 &new_dir_page);
510 if (!new_dir_entry)
511 goto out_old_dir;
512 }
513 }
514
515 /*
516 * If cross rename between file and directory those are not
517 * in the same directory, we will inc nlink of file's parent
518 * later, so we should check upper boundary of its nlink.
519 */
520 if ((!old_dir_entry || !new_dir_entry) &&
521 old_dir_entry != new_dir_entry) {
522 old_nlink = old_dir_entry ? -1 : 1;
523 new_nlink = -old_nlink;
524 err = -EMLINK;
525 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
526 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
527 goto out_new_dir;
528 }
529
530 f2fs_lock_op(sbi);
531
532 err = update_dent_inode(old_inode, &new_dentry->d_name);
533 if (err)
534 goto out_unlock;
535
536 err = update_dent_inode(new_inode, &old_dentry->d_name);
537 if (err)
538 goto out_undo;
539
540 /* update ".." directory entry info of old dentry */
541 if (old_dir_entry)
542 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
543
544 /* update ".." directory entry info of new dentry */
545 if (new_dir_entry)
546 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
547
548 /* update directory entry info of old dir inode */
549 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
550
551 down_write(&F2FS_I(old_inode)->i_sem);
552 file_lost_pino(old_inode);
553 up_write(&F2FS_I(old_inode)->i_sem);
554
555 update_inode_page(old_inode);
556
557 old_dir->i_ctime = CURRENT_TIME;
558 if (old_nlink) {
559 down_write(&F2FS_I(old_dir)->i_sem);
560 if (old_nlink < 0)
561 drop_nlink(old_dir);
562 else
563 inc_nlink(old_dir);
564 up_write(&F2FS_I(old_dir)->i_sem);
565 }
566 mark_inode_dirty(old_dir);
567 update_inode_page(old_dir);
568
569 /* update directory entry info of new dir inode */
570 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
571
572 down_write(&F2FS_I(new_inode)->i_sem);
573 file_lost_pino(new_inode);
574 up_write(&F2FS_I(new_inode)->i_sem);
575
576 update_inode_page(new_inode);
577
578 new_dir->i_ctime = CURRENT_TIME;
579 if (new_nlink) {
580 down_write(&F2FS_I(new_dir)->i_sem);
581 if (new_nlink < 0)
582 drop_nlink(new_dir);
583 else
584 inc_nlink(new_dir);
585 up_write(&F2FS_I(new_dir)->i_sem);
586 }
587 mark_inode_dirty(new_dir);
588 update_inode_page(new_dir);
589
590 f2fs_unlock_op(sbi);
591 return 0;
592out_undo:
593 /* Still we may fail to recover name info of f2fs_inode here */
594 update_dent_inode(old_inode, &old_dentry->d_name);
595out_unlock:
596 f2fs_unlock_op(sbi);
597out_new_dir:
598 if (new_dir_entry) {
599 kunmap(new_dir_page);
600 f2fs_put_page(new_dir_page, 0);
601 }
602out_old_dir:
603 if (old_dir_entry) {
604 kunmap(old_dir_page);
605 f2fs_put_page(old_dir_page, 0);
606 }
607out_new:
608 kunmap(new_page);
609 f2fs_put_page(new_page, 0);
610out_old:
611 kunmap(old_page);
612 f2fs_put_page(old_page, 0);
613out:
614 return err;
615}
616
617static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
618 struct inode *new_dir, struct dentry *new_dentry,
619 unsigned int flags)
620{
621 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
622 return -EINVAL;
623
624 if (flags & RENAME_EXCHANGE) {
625 return f2fs_cross_rename(old_dir, old_dentry,
626 new_dir, new_dentry);
627 }
628 /*
629 * VFS has already handled the new dentry existence case,
630 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
631 */
632 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
633}
634
Chao Yu50732df2014-06-19 16:23:19 +0800635static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
636{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700637 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Chao Yu50732df2014-06-19 16:23:19 +0800638 struct inode *inode;
Chao Yu50732df2014-06-19 16:23:19 +0800639 int err;
640
641 inode = f2fs_new_inode(dir, mode);
642 if (IS_ERR(inode))
643 return PTR_ERR(inode);
644
645 inode->i_op = &f2fs_file_inode_operations;
646 inode->i_fop = &f2fs_file_operations;
647 inode->i_mapping->a_ops = &f2fs_dblock_aops;
648
649 f2fs_lock_op(sbi);
650 err = acquire_orphan_inode(sbi);
651 if (err)
652 goto out;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700653
654 err = f2fs_do_tmpfile(inode, dir);
655 if (err)
656 goto release_out;
657
Chao Yu50732df2014-06-19 16:23:19 +0800658 /*
659 * add this non-linked tmpfile to orphan list, in this way we could
660 * remove all unused data of tmpfile after abnormal power-off.
661 */
662 add_orphan_inode(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800663 f2fs_unlock_op(sbi);
664
665 alloc_nid_done(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800666 d_tmpfile(dentry, inode);
667 unlock_new_inode(inode);
668 return 0;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700669
670release_out:
671 release_orphan_inode(sbi);
Chao Yu50732df2014-06-19 16:23:19 +0800672out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700673 handle_failed_inode(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800674 return err;
675}
676
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900677const struct inode_operations f2fs_dir_inode_operations = {
678 .create = f2fs_create,
679 .lookup = f2fs_lookup,
680 .link = f2fs_link,
681 .unlink = f2fs_unlink,
682 .symlink = f2fs_symlink,
683 .mkdir = f2fs_mkdir,
684 .rmdir = f2fs_rmdir,
685 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +0800686 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +0800687 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900688 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900689 .setattr = f2fs_setattr,
690 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800691 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900692#ifdef CONFIG_F2FS_FS_XATTR
693 .setxattr = generic_setxattr,
694 .getxattr = generic_getxattr,
695 .listxattr = f2fs_listxattr,
696 .removexattr = generic_removexattr,
697#endif
698};
699
700const struct inode_operations f2fs_symlink_inode_operations = {
701 .readlink = generic_readlink,
702 .follow_link = page_follow_link_light,
703 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900704 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900705 .setattr = f2fs_setattr,
706#ifdef CONFIG_F2FS_FS_XATTR
707 .setxattr = generic_setxattr,
708 .getxattr = generic_getxattr,
709 .listxattr = f2fs_listxattr,
710 .removexattr = generic_removexattr,
711#endif
712};
713
714const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900715 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900716 .setattr = f2fs_setattr,
717 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800718 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900719#ifdef CONFIG_F2FS_FS_XATTR
720 .setxattr = generic_setxattr,
721 .getxattr = generic_getxattr,
722 .listxattr = f2fs_listxattr,
723 .removexattr = generic_removexattr,
724#endif
725};