blob: 8055e30eb14d0bcb87fefad84dbe9062deab932a [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 }
Chao Yu622f28a2014-09-24 18:19:10 +080057
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070058 if (f2fs_may_inline(inode))
59 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Chao Yu622f28a2014-09-24 18:19:10 +080060 if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode))
61 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
62
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090063 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090064 mark_inode_dirty(inode);
65 return inode;
66
67out:
68 clear_nlink(inode);
69 unlock_new_inode(inode);
70fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090071 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090072 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090073 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
84 if (sublen > slen)
Namjae Jeone66509f02013-04-20 01:27:21 +090085 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090086
Gu Zheng749ebfd2013-08-22 18:11:25 +080087 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090088}
89
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090090/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +090091 * Set multimedia files as cold files for hot/cold data separation
92 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090093static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +090094 const unsigned char *name)
95{
96 int i;
97 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
98
99 int count = le32_to_cpu(sbi->raw_super->extension_count);
100 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900101 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900102 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900103 break;
104 }
105 }
106}
107
108static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
109 bool excl)
110{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700111 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900112 struct inode *inode;
113 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800114 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900115
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900116 f2fs_balance_fs(sbi);
117
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900118 inode = f2fs_new_inode(dir, mode);
119 if (IS_ERR(inode))
120 return PTR_ERR(inode);
121
122 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900123 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900124
125 inode->i_op = &f2fs_file_inode_operations;
126 inode->i_fop = &f2fs_file_operations;
127 inode->i_mapping->a_ops = &f2fs_dblock_aops;
128 ino = inode->i_ino;
129
Gu Zhenge4795562013-09-27 18:08:30 +0800130 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900131 err = f2fs_add_link(dentry, inode);
132 if (err)
133 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700134 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900135
136 alloc_nid_done(sbi, ino);
137
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700138 stat_inc_inline_inode(inode);
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900139 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900140 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800141
142 if (IS_DIRSYNC(dir))
143 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900144 return 0;
145out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700146 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900147 return err;
148}
149
150static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
151 struct dentry *dentry)
152{
153 struct inode *inode = old_dentry->d_inode;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700154 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800155 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900156
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900157 f2fs_balance_fs(sbi);
158
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900159 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900160 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900161
162 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800163 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900164 err = f2fs_add_link(dentry, inode);
165 if (err)
166 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700167 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900168
169 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800170
171 if (IS_DIRSYNC(dir))
172 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900173 return 0;
174out:
175 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
176 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700177 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900178 return err;
179}
180
181struct dentry *f2fs_get_parent(struct dentry *child)
182{
183 struct qstr dotdot = QSTR_INIT("..", 2);
184 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
185 if (!ino)
186 return ERR_PTR(-ENOENT);
187 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
188}
189
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700190static int __recover_dot_dentries(struct inode *dir, nid_t pino)
191{
192 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
193 struct qstr dot = QSTR_INIT(".", 1);
194 struct qstr dotdot = QSTR_INIT("..", 2);
195 struct f2fs_dir_entry *de;
196 struct page *page;
197 int err = 0;
198
199 f2fs_lock_op(sbi);
200
201 de = f2fs_find_entry(dir, &dot, &page);
202 if (de) {
203 f2fs_dentry_kunmap(dir, page);
204 f2fs_put_page(page, 0);
205 } else {
206 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
207 if (err)
208 goto out;
209 }
210
211 de = f2fs_find_entry(dir, &dotdot, &page);
212 if (de) {
213 f2fs_dentry_kunmap(dir, page);
214 f2fs_put_page(page, 0);
215 } else {
216 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
217 }
218out:
219 if (!err) {
220 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
221 mark_inode_dirty(dir);
222 }
223
224 f2fs_unlock_op(sbi);
225 return err;
226}
227
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900228static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
229 unsigned int flags)
230{
231 struct inode *inode = NULL;
232 struct f2fs_dir_entry *de;
233 struct page *page;
234
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900235 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900236 return ERR_PTR(-ENAMETOOLONG);
237
238 de = f2fs_find_entry(dir, &dentry->d_name, &page);
239 if (de) {
240 nid_t ino = le32_to_cpu(de->ino);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800241 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900242 f2fs_put_page(page, 0);
243
244 inode = f2fs_iget(dir->i_sb, ino);
245 if (IS_ERR(inode))
246 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700247
248 if (f2fs_has_inline_dots(inode)) {
249 int err;
250
251 err = __recover_dot_dentries(inode, dir->i_ino);
252 if (err) {
253 iget_failed(inode);
254 return ERR_PTR(err);
255 }
256 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900257 }
258
259 return d_splice_alias(inode, dentry);
260}
261
262static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
263{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700264 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900265 struct inode *inode = dentry->d_inode;
266 struct f2fs_dir_entry *de;
267 struct page *page;
268 int err = -ENOENT;
269
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900270 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900271 f2fs_balance_fs(sbi);
272
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900273 de = f2fs_find_entry(dir, &dentry->d_name, &page);
274 if (!de)
275 goto fail;
276
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900277 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900278 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900279 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900280 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800281 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900282 f2fs_put_page(page, 0);
283 goto fail;
284 }
Chao Yudbeacf02014-09-24 18:17:04 +0800285 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800286 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900287
arter97e1c42042014-08-06 23:22:50 +0900288 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900289 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800290
291 if (IS_DIRSYNC(dir))
292 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900293fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900294 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900295 return err;
296}
297
298static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
299 const char *symname)
300{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700301 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900302 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200303 size_t symlen = strlen(symname) + 1;
Gu Zhenge4795562013-09-27 18:08:30 +0800304 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900305
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900306 f2fs_balance_fs(sbi);
307
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900308 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
309 if (IS_ERR(inode))
310 return PTR_ERR(inode);
311
312 inode->i_op = &f2fs_symlink_inode_operations;
313 inode->i_mapping->a_ops = &f2fs_dblock_aops;
314
Gu Zhenge4795562013-09-27 18:08:30 +0800315 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900316 err = f2fs_add_link(dentry, inode);
317 if (err)
318 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700319 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900320
321 err = page_symlink(inode, symname, symlen);
322 alloc_nid_done(sbi, inode->i_ino);
323
324 d_instantiate(dentry, inode);
325 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800326
327 if (IS_DIRSYNC(dir))
328 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900329 return err;
330out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700331 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900332 return err;
333}
334
335static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
336{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700337 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900338 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800339 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900340
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900341 f2fs_balance_fs(sbi);
342
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900343 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900344 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900345 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346
347 inode->i_op = &f2fs_dir_inode_operations;
348 inode->i_fop = &f2fs_dir_operations;
349 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800350 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351
352 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800353 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900354 err = f2fs_add_link(dentry, inode);
355 if (err)
356 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700357 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900358
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700359 stat_inc_inline_dir(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900360 alloc_nid_done(sbi, inode->i_ino);
361
362 d_instantiate(dentry, inode);
363 unlock_new_inode(inode);
364
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800365 if (IS_DIRSYNC(dir))
366 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900367 return 0;
368
369out_fail:
370 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700371 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900372 return err;
373}
374
375static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
376{
377 struct inode *inode = dentry->d_inode;
378 if (f2fs_empty_dir(inode))
379 return f2fs_unlink(dir, dentry);
380 return -ENOTEMPTY;
381}
382
383static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
384 umode_t mode, dev_t rdev)
385{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700386 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900387 struct inode *inode;
388 int err = 0;
389
390 if (!new_valid_dev(rdev))
391 return -EINVAL;
392
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900393 f2fs_balance_fs(sbi);
394
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900395 inode = f2fs_new_inode(dir, mode);
396 if (IS_ERR(inode))
397 return PTR_ERR(inode);
398
399 init_special_inode(inode, inode->i_mode, rdev);
400 inode->i_op = &f2fs_special_inode_operations;
401
Gu Zhenge4795562013-09-27 18:08:30 +0800402 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900403 err = f2fs_add_link(dentry, inode);
404 if (err)
405 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700406 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900407
408 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800409
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900410 d_instantiate(dentry, inode);
411 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800412
413 if (IS_DIRSYNC(dir))
414 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900415 return 0;
416out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700417 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900418 return err;
419}
420
421static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
422 struct inode *new_dir, struct dentry *new_dentry)
423{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700424 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900425 struct inode *old_inode = old_dentry->d_inode;
426 struct inode *new_inode = new_dentry->d_inode;
427 struct page *old_dir_page;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900428 struct page *old_page, *new_page;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900429 struct f2fs_dir_entry *old_dir_entry = NULL;
430 struct f2fs_dir_entry *old_entry;
431 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800432 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900433
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900434 f2fs_balance_fs(sbi);
435
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900436 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
437 if (!old_entry)
438 goto out;
439
440 if (S_ISDIR(old_inode->i_mode)) {
441 err = -EIO;
442 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
443 if (!old_dir_entry)
444 goto out_old;
445 }
446
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900447 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900448
449 err = -ENOTEMPTY;
450 if (old_dir_entry && !f2fs_empty_dir(new_inode))
451 goto out_dir;
452
453 err = -ENOENT;
454 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
455 &new_page);
456 if (!new_entry)
457 goto out_dir;
458
Chao Yu12560102014-06-24 14:16:24 +0800459 f2fs_lock_op(sbi);
460
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900461 err = acquire_orphan_inode(sbi);
462 if (err)
463 goto put_out_dir;
464
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900465 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900466 release_orphan_inode(sbi);
467 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900468 }
469
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900470 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
471
472 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900473 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900474 if (old_dir_entry)
475 drop_nlink(new_inode);
476 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900477 up_write(&F2FS_I(new_inode)->i_sem);
478
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900479 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900480
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900481 if (!new_inode->i_nlink)
482 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900483 else
484 release_orphan_inode(sbi);
485
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900486 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900487 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900488 } else {
Chao Yu12560102014-06-24 14:16:24 +0800489 f2fs_lock_op(sbi);
490
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900491 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800492 if (err) {
493 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900494 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +0800495 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900496
497 if (old_dir_entry) {
498 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900499 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900500 }
501 }
502
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900503 down_write(&F2FS_I(old_inode)->i_sem);
504 file_lost_pino(old_inode);
505 up_write(&F2FS_I(old_inode)->i_sem);
506
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900507 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900508 mark_inode_dirty(old_inode);
509
Chao Yudbeacf02014-09-24 18:17:04 +0800510 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900511
512 if (old_dir_entry) {
513 if (old_dir != new_dir) {
514 f2fs_set_link(old_inode, old_dir_entry,
515 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900516 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900517 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800518 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900519 f2fs_put_page(old_dir_page, 0);
520 }
521 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900522 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900523 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900524 }
525
Gu Zhenge4795562013-09-27 18:08:30 +0800526 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800527
528 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
529 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900530 return 0;
531
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900532put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800533 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800534 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yudd4d9612014-06-24 14:13:13 +0800535 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900536out_dir:
537 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800538 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900539 f2fs_put_page(old_dir_page, 0);
540 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900541out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800542 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900543 f2fs_put_page(old_page, 0);
544out:
545 return err;
546}
547
Chao Yu32f9bc22014-07-12 19:13:54 +0800548static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
549 struct inode *new_dir, struct dentry *new_dentry)
550{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700551 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +0800552 struct inode *old_inode = old_dentry->d_inode;
553 struct inode *new_inode = new_dentry->d_inode;
554 struct page *old_dir_page, *new_dir_page;
555 struct page *old_page, *new_page;
556 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
557 struct f2fs_dir_entry *old_entry, *new_entry;
558 int old_nlink = 0, new_nlink = 0;
559 int err = -ENOENT;
560
561 f2fs_balance_fs(sbi);
562
563 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
564 if (!old_entry)
565 goto out;
566
567 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
568 if (!new_entry)
569 goto out_old;
570
571 /* prepare for updating ".." directory entry info later */
572 if (old_dir != new_dir) {
573 if (S_ISDIR(old_inode->i_mode)) {
574 err = -EIO;
575 old_dir_entry = f2fs_parent_dir(old_inode,
576 &old_dir_page);
577 if (!old_dir_entry)
578 goto out_new;
579 }
580
581 if (S_ISDIR(new_inode->i_mode)) {
582 err = -EIO;
583 new_dir_entry = f2fs_parent_dir(new_inode,
584 &new_dir_page);
585 if (!new_dir_entry)
586 goto out_old_dir;
587 }
588 }
589
590 /*
591 * If cross rename between file and directory those are not
592 * in the same directory, we will inc nlink of file's parent
593 * later, so we should check upper boundary of its nlink.
594 */
595 if ((!old_dir_entry || !new_dir_entry) &&
596 old_dir_entry != new_dir_entry) {
597 old_nlink = old_dir_entry ? -1 : 1;
598 new_nlink = -old_nlink;
599 err = -EMLINK;
600 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
601 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
602 goto out_new_dir;
603 }
604
605 f2fs_lock_op(sbi);
606
607 err = update_dent_inode(old_inode, &new_dentry->d_name);
608 if (err)
609 goto out_unlock;
610
611 err = update_dent_inode(new_inode, &old_dentry->d_name);
612 if (err)
613 goto out_undo;
614
615 /* update ".." directory entry info of old dentry */
616 if (old_dir_entry)
617 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
618
619 /* update ".." directory entry info of new dentry */
620 if (new_dir_entry)
621 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
622
623 /* update directory entry info of old dir inode */
624 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
625
626 down_write(&F2FS_I(old_inode)->i_sem);
627 file_lost_pino(old_inode);
628 up_write(&F2FS_I(old_inode)->i_sem);
629
630 update_inode_page(old_inode);
631
632 old_dir->i_ctime = CURRENT_TIME;
633 if (old_nlink) {
634 down_write(&F2FS_I(old_dir)->i_sem);
635 if (old_nlink < 0)
636 drop_nlink(old_dir);
637 else
638 inc_nlink(old_dir);
639 up_write(&F2FS_I(old_dir)->i_sem);
640 }
641 mark_inode_dirty(old_dir);
642 update_inode_page(old_dir);
643
644 /* update directory entry info of new dir inode */
645 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
646
647 down_write(&F2FS_I(new_inode)->i_sem);
648 file_lost_pino(new_inode);
649 up_write(&F2FS_I(new_inode)->i_sem);
650
651 update_inode_page(new_inode);
652
653 new_dir->i_ctime = CURRENT_TIME;
654 if (new_nlink) {
655 down_write(&F2FS_I(new_dir)->i_sem);
656 if (new_nlink < 0)
657 drop_nlink(new_dir);
658 else
659 inc_nlink(new_dir);
660 up_write(&F2FS_I(new_dir)->i_sem);
661 }
662 mark_inode_dirty(new_dir);
663 update_inode_page(new_dir);
664
665 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800666
667 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
668 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800669 return 0;
670out_undo:
671 /* Still we may fail to recover name info of f2fs_inode here */
672 update_dent_inode(old_inode, &old_dentry->d_name);
673out_unlock:
674 f2fs_unlock_op(sbi);
675out_new_dir:
676 if (new_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800677 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800678 f2fs_put_page(new_dir_page, 0);
679 }
680out_old_dir:
681 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800682 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800683 f2fs_put_page(old_dir_page, 0);
684 }
685out_new:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800686 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800687 f2fs_put_page(new_page, 0);
688out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800689 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800690 f2fs_put_page(old_page, 0);
691out:
692 return err;
693}
694
695static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
696 struct inode *new_dir, struct dentry *new_dentry,
697 unsigned int flags)
698{
699 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
700 return -EINVAL;
701
702 if (flags & RENAME_EXCHANGE) {
703 return f2fs_cross_rename(old_dir, old_dentry,
704 new_dir, new_dentry);
705 }
706 /*
707 * VFS has already handled the new dentry existence case,
708 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
709 */
710 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
711}
712
Chao Yu50732df2014-06-19 16:23:19 +0800713static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
714{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700715 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Chao Yu50732df2014-06-19 16:23:19 +0800716 struct inode *inode;
Chao Yu50732df2014-06-19 16:23:19 +0800717 int err;
718
719 inode = f2fs_new_inode(dir, mode);
720 if (IS_ERR(inode))
721 return PTR_ERR(inode);
722
723 inode->i_op = &f2fs_file_inode_operations;
724 inode->i_fop = &f2fs_file_operations;
725 inode->i_mapping->a_ops = &f2fs_dblock_aops;
726
727 f2fs_lock_op(sbi);
728 err = acquire_orphan_inode(sbi);
729 if (err)
730 goto out;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700731
732 err = f2fs_do_tmpfile(inode, dir);
733 if (err)
734 goto release_out;
735
Chao Yu50732df2014-06-19 16:23:19 +0800736 /*
737 * add this non-linked tmpfile to orphan list, in this way we could
738 * remove all unused data of tmpfile after abnormal power-off.
739 */
740 add_orphan_inode(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800741 f2fs_unlock_op(sbi);
742
743 alloc_nid_done(sbi, inode->i_ino);
Chao Yu3c0d84d2015-02-16 16:20:27 +0800744
745 stat_inc_inline_inode(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800746 d_tmpfile(dentry, inode);
747 unlock_new_inode(inode);
748 return 0;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700749
750release_out:
751 release_orphan_inode(sbi);
Chao Yu50732df2014-06-19 16:23:19 +0800752out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700753 handle_failed_inode(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800754 return err;
755}
756
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900757const struct inode_operations f2fs_dir_inode_operations = {
758 .create = f2fs_create,
759 .lookup = f2fs_lookup,
760 .link = f2fs_link,
761 .unlink = f2fs_unlink,
762 .symlink = f2fs_symlink,
763 .mkdir = f2fs_mkdir,
764 .rmdir = f2fs_rmdir,
765 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +0800766 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +0800767 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900768 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900769 .setattr = f2fs_setattr,
770 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800771 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900772#ifdef CONFIG_F2FS_FS_XATTR
773 .setxattr = generic_setxattr,
774 .getxattr = generic_getxattr,
775 .listxattr = f2fs_listxattr,
776 .removexattr = generic_removexattr,
777#endif
778};
779
780const struct inode_operations f2fs_symlink_inode_operations = {
781 .readlink = generic_readlink,
782 .follow_link = page_follow_link_light,
783 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900784 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900785 .setattr = f2fs_setattr,
786#ifdef CONFIG_F2FS_FS_XATTR
787 .setxattr = generic_setxattr,
788 .getxattr = generic_getxattr,
789 .listxattr = f2fs_listxattr,
790 .removexattr = generic_removexattr,
791#endif
792};
793
794const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900795 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900796 .setattr = f2fs_setattr,
797 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800798 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900799#ifdef CONFIG_F2FS_FS_XATTR
800 .setxattr = generic_setxattr,
801 .getxattr = generic_getxattr,
802 .listxattr = f2fs_listxattr,
803 .removexattr = generic_removexattr,
804#endif
805};