blob: 27b03776ffd21b8377dd645ba15591c1e783c19e [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 Kima014e032014-06-20 21:44:02 -070026 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
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 Kima014e032014-06-20 21:44:02 -0700105 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
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);
Gu Zhenge4795562013-09-27 18:08:30 +0800126 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900127 if (err)
128 goto out;
129
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:
136 clear_nlink(inode);
137 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900138 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900139 iput(inode);
140 alloc_nid_failed(sbi, ino);
141 return err;
142}
143
144static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
146{
147 struct inode *inode = old_dentry->d_inode;
Jaegeuk Kima014e032014-06-20 21:44:02 -0700148 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Gu Zhenge4795562013-09-27 18:08:30 +0800149 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900150
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900151 f2fs_balance_fs(sbi);
152
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900153 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900154 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900155
156 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800157 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800159 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900160 if (err)
161 goto out;
162
163 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900164 return 0;
165out:
166 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
167 iput(inode);
168 return err;
169}
170
171struct dentry *f2fs_get_parent(struct dentry *child)
172{
173 struct qstr dotdot = QSTR_INIT("..", 2);
174 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
175 if (!ino)
176 return ERR_PTR(-ENOENT);
177 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
178}
179
180static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
181 unsigned int flags)
182{
183 struct inode *inode = NULL;
184 struct f2fs_dir_entry *de;
185 struct page *page;
186
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900187 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900188 return ERR_PTR(-ENAMETOOLONG);
189
190 de = f2fs_find_entry(dir, &dentry->d_name, &page);
191 if (de) {
192 nid_t ino = le32_to_cpu(de->ino);
193 kunmap(page);
194 f2fs_put_page(page, 0);
195
196 inode = f2fs_iget(dir->i_sb, ino);
197 if (IS_ERR(inode))
198 return ERR_CAST(inode);
Chao Yu48b230a2014-04-07 11:18:34 +0800199
200 stat_inc_inline_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900201 }
202
203 return d_splice_alias(inode, dentry);
204}
205
206static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
207{
Jaegeuk Kima014e032014-06-20 21:44:02 -0700208 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900209 struct inode *inode = dentry->d_inode;
210 struct f2fs_dir_entry *de;
211 struct page *page;
212 int err = -ENOENT;
213
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900214 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900215 f2fs_balance_fs(sbi);
216
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900217 de = f2fs_find_entry(dir, &dentry->d_name, &page);
218 if (!de)
219 goto fail;
220
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900221 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900222 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900223 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900224 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900225 kunmap(page);
226 f2fs_put_page(page, 0);
227 goto fail;
228 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900229 f2fs_delete_entry(de, page, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800230 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900231
232 /* In order to evict this inode, we set it dirty */
233 mark_inode_dirty(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900234fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900235 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900236 return err;
237}
238
239static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
240 const char *symname)
241{
Jaegeuk Kima014e032014-06-20 21:44:02 -0700242 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900243 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200244 size_t symlen = strlen(symname) + 1;
Gu Zhenge4795562013-09-27 18:08:30 +0800245 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900246
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900247 f2fs_balance_fs(sbi);
248
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900249 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
250 if (IS_ERR(inode))
251 return PTR_ERR(inode);
252
253 inode->i_op = &f2fs_symlink_inode_operations;
254 inode->i_mapping->a_ops = &f2fs_dblock_aops;
255
Gu Zhenge4795562013-09-27 18:08:30 +0800256 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900257 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800258 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900259 if (err)
260 goto out;
261
262 err = page_symlink(inode, symname, symlen);
263 alloc_nid_done(sbi, inode->i_ino);
264
265 d_instantiate(dentry, inode);
266 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900267 return err;
268out:
269 clear_nlink(inode);
270 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900271 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900272 iput(inode);
273 alloc_nid_failed(sbi, inode->i_ino);
274 return err;
275}
276
277static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
278{
279 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
280 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800281 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900282
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900283 f2fs_balance_fs(sbi);
284
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900285 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900286 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900287 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900288
289 inode->i_op = &f2fs_dir_inode_operations;
290 inode->i_fop = &f2fs_dir_operations;
291 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Namjae Jeona0d42532012-12-08 14:54:18 +0900292 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900293
294 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800295 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900296 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800297 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900298 if (err)
299 goto out_fail;
300
301 alloc_nid_done(sbi, inode->i_ino);
302
303 d_instantiate(dentry, inode);
304 unlock_new_inode(inode);
305
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900306 return 0;
307
308out_fail:
309 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
310 clear_nlink(inode);
311 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900312 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900313 iput(inode);
314 alloc_nid_failed(sbi, inode->i_ino);
315 return err;
316}
317
318static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
319{
320 struct inode *inode = dentry->d_inode;
321 if (f2fs_empty_dir(inode))
322 return f2fs_unlink(dir, dentry);
323 return -ENOTEMPTY;
324}
325
326static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
327 umode_t mode, dev_t rdev)
328{
Jaegeuk Kima014e032014-06-20 21:44:02 -0700329 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900330 struct inode *inode;
331 int err = 0;
332
333 if (!new_valid_dev(rdev))
334 return -EINVAL;
335
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900336 f2fs_balance_fs(sbi);
337
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900338 inode = f2fs_new_inode(dir, mode);
339 if (IS_ERR(inode))
340 return PTR_ERR(inode);
341
342 init_special_inode(inode, inode->i_mode, rdev);
343 inode->i_op = &f2fs_special_inode_operations;
344
Gu Zhenge4795562013-09-27 18:08:30 +0800345 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346 err = f2fs_add_link(dentry, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800347 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900348 if (err)
349 goto out;
350
351 alloc_nid_done(sbi, inode->i_ino);
352 d_instantiate(dentry, inode);
353 unlock_new_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900354 return 0;
355out:
356 clear_nlink(inode);
357 unlock_new_inode(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900358 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900359 iput(inode);
360 alloc_nid_failed(sbi, inode->i_ino);
361 return err;
362}
363
364static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
365 struct inode *new_dir, struct dentry *new_dentry)
366{
Jaegeuk Kima014e032014-06-20 21:44:02 -0700367 struct f2fs_sb_info *sbi = F2FS_SB(old_dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900368 struct inode *old_inode = old_dentry->d_inode;
369 struct inode *new_inode = new_dentry->d_inode;
370 struct page *old_dir_page;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900371 struct page *old_page, *new_page;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900372 struct f2fs_dir_entry *old_dir_entry = NULL;
373 struct f2fs_dir_entry *old_entry;
374 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800375 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900376
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900377 f2fs_balance_fs(sbi);
378
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900379 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
380 if (!old_entry)
381 goto out;
382
383 if (S_ISDIR(old_inode->i_mode)) {
384 err = -EIO;
385 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
386 if (!old_dir_entry)
387 goto out_old;
388 }
389
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900390 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900391
392 err = -ENOTEMPTY;
393 if (old_dir_entry && !f2fs_empty_dir(new_inode))
394 goto out_dir;
395
396 err = -ENOENT;
397 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
398 &new_page);
399 if (!new_entry)
400 goto out_dir;
401
Chao Yu12560102014-06-24 14:16:24 +0800402 f2fs_lock_op(sbi);
403
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900404 err = acquire_orphan_inode(sbi);
405 if (err)
406 goto put_out_dir;
407
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900408 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900409 release_orphan_inode(sbi);
410 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900411 }
412
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900413 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
414
415 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900416 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900417 if (old_dir_entry)
418 drop_nlink(new_inode);
419 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900420 up_write(&F2FS_I(new_inode)->i_sem);
421
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900422 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900423
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424 if (!new_inode->i_nlink)
425 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900426 else
427 release_orphan_inode(sbi);
428
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900429 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900430 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900431 } else {
Chao Yu12560102014-06-24 14:16:24 +0800432 f2fs_lock_op(sbi);
433
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900434 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800435 if (err) {
436 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900437 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +0800438 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900439
440 if (old_dir_entry) {
441 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900442 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900443 }
444 }
445
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900446 down_write(&F2FS_I(old_inode)->i_sem);
447 file_lost_pino(old_inode);
448 up_write(&F2FS_I(old_inode)->i_sem);
449
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900450 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900451 mark_inode_dirty(old_inode);
452
453 f2fs_delete_entry(old_entry, old_page, NULL);
454
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);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900459 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900460 } else {
461 kunmap(old_dir_page);
462 f2fs_put_page(old_dir_page, 0);
463 }
464 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900465 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900466 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900467 }
468
Gu Zhenge4795562013-09-27 18:08:30 +0800469 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900470 return 0;
471
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900472put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800473 f2fs_unlock_op(sbi);
Chao Yudd4d9612014-06-24 14:13:13 +0800474 kunmap(new_page);
475 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900476out_dir:
477 if (old_dir_entry) {
478 kunmap(old_dir_page);
479 f2fs_put_page(old_dir_page, 0);
480 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900481out_old:
482 kunmap(old_page);
483 f2fs_put_page(old_page, 0);
484out:
485 return err;
486}
487
Chao Yu32f9bc22014-07-12 19:13:54 +0800488static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
489 struct inode *new_dir, struct dentry *new_dentry)
490{
491 struct super_block *sb = old_dir->i_sb;
492 struct f2fs_sb_info *sbi = F2FS_SB(sb);
493 struct inode *old_inode = old_dentry->d_inode;
494 struct inode *new_inode = new_dentry->d_inode;
495 struct page *old_dir_page, *new_dir_page;
496 struct page *old_page, *new_page;
497 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
498 struct f2fs_dir_entry *old_entry, *new_entry;
499 int old_nlink = 0, new_nlink = 0;
500 int err = -ENOENT;
501
502 f2fs_balance_fs(sbi);
503
504 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
505 if (!old_entry)
506 goto out;
507
508 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
509 if (!new_entry)
510 goto out_old;
511
512 /* prepare for updating ".." directory entry info later */
513 if (old_dir != new_dir) {
514 if (S_ISDIR(old_inode->i_mode)) {
515 err = -EIO;
516 old_dir_entry = f2fs_parent_dir(old_inode,
517 &old_dir_page);
518 if (!old_dir_entry)
519 goto out_new;
520 }
521
522 if (S_ISDIR(new_inode->i_mode)) {
523 err = -EIO;
524 new_dir_entry = f2fs_parent_dir(new_inode,
525 &new_dir_page);
526 if (!new_dir_entry)
527 goto out_old_dir;
528 }
529 }
530
531 /*
532 * If cross rename between file and directory those are not
533 * in the same directory, we will inc nlink of file's parent
534 * later, so we should check upper boundary of its nlink.
535 */
536 if ((!old_dir_entry || !new_dir_entry) &&
537 old_dir_entry != new_dir_entry) {
538 old_nlink = old_dir_entry ? -1 : 1;
539 new_nlink = -old_nlink;
540 err = -EMLINK;
541 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
542 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
543 goto out_new_dir;
544 }
545
546 f2fs_lock_op(sbi);
547
548 err = update_dent_inode(old_inode, &new_dentry->d_name);
549 if (err)
550 goto out_unlock;
551
552 err = update_dent_inode(new_inode, &old_dentry->d_name);
553 if (err)
554 goto out_undo;
555
556 /* update ".." directory entry info of old dentry */
557 if (old_dir_entry)
558 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
559
560 /* update ".." directory entry info of new dentry */
561 if (new_dir_entry)
562 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
563
564 /* update directory entry info of old dir inode */
565 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
566
567 down_write(&F2FS_I(old_inode)->i_sem);
568 file_lost_pino(old_inode);
569 up_write(&F2FS_I(old_inode)->i_sem);
570
571 update_inode_page(old_inode);
572
573 old_dir->i_ctime = CURRENT_TIME;
574 if (old_nlink) {
575 down_write(&F2FS_I(old_dir)->i_sem);
576 if (old_nlink < 0)
577 drop_nlink(old_dir);
578 else
579 inc_nlink(old_dir);
580 up_write(&F2FS_I(old_dir)->i_sem);
581 }
582 mark_inode_dirty(old_dir);
583 update_inode_page(old_dir);
584
585 /* update directory entry info of new dir inode */
586 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
587
588 down_write(&F2FS_I(new_inode)->i_sem);
589 file_lost_pino(new_inode);
590 up_write(&F2FS_I(new_inode)->i_sem);
591
592 update_inode_page(new_inode);
593
594 new_dir->i_ctime = CURRENT_TIME;
595 if (new_nlink) {
596 down_write(&F2FS_I(new_dir)->i_sem);
597 if (new_nlink < 0)
598 drop_nlink(new_dir);
599 else
600 inc_nlink(new_dir);
601 up_write(&F2FS_I(new_dir)->i_sem);
602 }
603 mark_inode_dirty(new_dir);
604 update_inode_page(new_dir);
605
606 f2fs_unlock_op(sbi);
607 return 0;
608out_undo:
609 /* Still we may fail to recover name info of f2fs_inode here */
610 update_dent_inode(old_inode, &old_dentry->d_name);
611out_unlock:
612 f2fs_unlock_op(sbi);
613out_new_dir:
614 if (new_dir_entry) {
615 kunmap(new_dir_page);
616 f2fs_put_page(new_dir_page, 0);
617 }
618out_old_dir:
619 if (old_dir_entry) {
620 kunmap(old_dir_page);
621 f2fs_put_page(old_dir_page, 0);
622 }
623out_new:
624 kunmap(new_page);
625 f2fs_put_page(new_page, 0);
626out_old:
627 kunmap(old_page);
628 f2fs_put_page(old_page, 0);
629out:
630 return err;
631}
632
633static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
634 struct inode *new_dir, struct dentry *new_dentry,
635 unsigned int flags)
636{
637 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
638 return -EINVAL;
639
640 if (flags & RENAME_EXCHANGE) {
641 return f2fs_cross_rename(old_dir, old_dentry,
642 new_dir, new_dentry);
643 }
644 /*
645 * VFS has already handled the new dentry existence case,
646 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
647 */
648 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
649}
650
Chao Yu50732df2014-06-19 16:23:19 +0800651static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
652{
Jaegeuk Kima014e032014-06-20 21:44:02 -0700653 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Chao Yu50732df2014-06-19 16:23:19 +0800654 struct inode *inode;
Chao Yu50732df2014-06-19 16:23:19 +0800655 int err;
656
657 inode = f2fs_new_inode(dir, mode);
658 if (IS_ERR(inode))
659 return PTR_ERR(inode);
660
661 inode->i_op = &f2fs_file_inode_operations;
662 inode->i_fop = &f2fs_file_operations;
663 inode->i_mapping->a_ops = &f2fs_dblock_aops;
664
665 f2fs_lock_op(sbi);
666 err = acquire_orphan_inode(sbi);
667 if (err)
668 goto out;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700669
670 err = f2fs_do_tmpfile(inode, dir);
671 if (err)
672 goto release_out;
673
Chao Yu50732df2014-06-19 16:23:19 +0800674 /*
675 * add this non-linked tmpfile to orphan list, in this way we could
676 * remove all unused data of tmpfile after abnormal power-off.
677 */
678 add_orphan_inode(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800679 f2fs_unlock_op(sbi);
680
681 alloc_nid_done(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800682 d_tmpfile(dentry, inode);
683 unlock_new_inode(inode);
684 return 0;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700685
686release_out:
687 release_orphan_inode(sbi);
Chao Yu50732df2014-06-19 16:23:19 +0800688out:
689 f2fs_unlock_op(sbi);
690 clear_nlink(inode);
691 unlock_new_inode(inode);
692 make_bad_inode(inode);
693 iput(inode);
694 alloc_nid_failed(sbi, inode->i_ino);
695 return err;
696}
697
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900698const struct inode_operations f2fs_dir_inode_operations = {
699 .create = f2fs_create,
700 .lookup = f2fs_lookup,
701 .link = f2fs_link,
702 .unlink = f2fs_unlink,
703 .symlink = f2fs_symlink,
704 .mkdir = f2fs_mkdir,
705 .rmdir = f2fs_rmdir,
706 .mknod = f2fs_mknod,
707 .rename = f2fs_rename,
Chao Yu32f9bc22014-07-12 19:13:54 +0800708 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +0800709 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900710 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900711 .setattr = f2fs_setattr,
712 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800713 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900714#ifdef CONFIG_F2FS_FS_XATTR
715 .setxattr = generic_setxattr,
716 .getxattr = generic_getxattr,
717 .listxattr = f2fs_listxattr,
718 .removexattr = generic_removexattr,
719#endif
720};
721
722const struct inode_operations f2fs_symlink_inode_operations = {
723 .readlink = generic_readlink,
724 .follow_link = page_follow_link_light,
725 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900726 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900727 .setattr = f2fs_setattr,
728#ifdef CONFIG_F2FS_FS_XATTR
729 .setxattr = generic_setxattr,
730 .getxattr = generic_getxattr,
731 .listxattr = f2fs_listxattr,
732 .removexattr = generic_removexattr,
733#endif
734};
735
736const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900737 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900738 .setattr = f2fs_setattr,
739 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800740 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741#ifdef CONFIG_F2FS_FS_XATTR
742 .setxattr = generic_setxattr,
743 .getxattr = generic_getxattr,
744 .listxattr = f2fs_listxattr,
745 .removexattr = generic_removexattr,
746#endif
747};