blob: 7e3794edae42ab5c656f2ae9fec69e7431476b35 [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 Kimfeb7cbb2015-04-15 13:49:55 -070017#include <linux/namei.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090018
19#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090020#include "node.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090021#include "xattr.h"
22#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090023#include <trace/events/f2fs.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090024
25static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
26{
Jaegeuk Kim40813632014-09-02 15:31:18 -070027 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090028 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
Jaegeuk Kima014e032014-06-20 21:44:02 -070033 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090034 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 }
Chao Yu622f28a2014-09-24 18:19:10 +080058
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070059 if (f2fs_may_inline(inode))
60 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Chao Yu622f28a2014-09-24 18:19:10 +080061 if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode))
62 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
63
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090064 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090065 mark_inode_dirty(inode);
66 return inode;
67
68out:
69 clear_nlink(inode);
70 unlock_new_inode(inode);
71fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090072 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090073 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090074 iput(inode);
75 if (nid_free)
76 alloc_nid_failed(sbi, ino);
77 return ERR_PTR(err);
78}
79
80static int is_multimedia_file(const unsigned char *s, const char *sub)
81{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020082 size_t slen = strlen(s);
83 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090084
85 if (sublen > slen)
Namjae Jeone66509f02013-04-20 01:27:21 +090086 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090087
Gu Zheng749ebfd2013-08-22 18:11:25 +080088 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090089}
90
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090091/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +090092 * Set multimedia files as cold files for hot/cold data separation
93 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090094static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +090095 const unsigned char *name)
96{
97 int i;
98 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
99
100 int count = le32_to_cpu(sbi->raw_super->extension_count);
101 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900102 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900103 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900104 break;
105 }
106 }
107}
108
109static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
110 bool excl)
111{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700112 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900113 struct inode *inode;
114 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800115 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900116
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900117 f2fs_balance_fs(sbi);
118
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900119 inode = f2fs_new_inode(dir, mode);
120 if (IS_ERR(inode))
121 return PTR_ERR(inode);
122
123 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900124 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900125
126 inode->i_op = &f2fs_file_inode_operations;
127 inode->i_fop = &f2fs_file_operations;
128 inode->i_mapping->a_ops = &f2fs_dblock_aops;
129 ino = inode->i_ino;
130
Gu Zhenge4795562013-09-27 18:08:30 +0800131 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900132 err = f2fs_add_link(dentry, inode);
133 if (err)
134 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700135 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900136
137 alloc_nid_done(sbi, ino);
138
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700139 stat_inc_inline_inode(inode);
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900140 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900141 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800142
143 if (IS_DIRSYNC(dir))
144 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900145 return 0;
146out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700147 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900148 return err;
149}
150
151static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
152 struct dentry *dentry)
153{
David Howells2b0143b2015-03-17 22:25:59 +0000154 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700155 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800156 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900157
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900158 f2fs_balance_fs(sbi);
159
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900160 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900161 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900162
163 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800164 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165 err = f2fs_add_link(dentry, inode);
166 if (err)
167 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700168 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900169
170 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800171
172 if (IS_DIRSYNC(dir))
173 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900174 return 0;
175out:
176 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
177 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700178 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900179 return err;
180}
181
182struct dentry *f2fs_get_parent(struct dentry *child)
183{
184 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +0000185 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186 if (!ino)
187 return ERR_PTR(-ENOENT);
David Howells2b0143b2015-03-17 22:25:59 +0000188 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900189}
190
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700191static int __recover_dot_dentries(struct inode *dir, nid_t pino)
192{
193 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
194 struct qstr dot = QSTR_INIT(".", 1);
195 struct qstr dotdot = QSTR_INIT("..", 2);
196 struct f2fs_dir_entry *de;
197 struct page *page;
198 int err = 0;
199
200 f2fs_lock_op(sbi);
201
202 de = f2fs_find_entry(dir, &dot, &page);
203 if (de) {
204 f2fs_dentry_kunmap(dir, page);
205 f2fs_put_page(page, 0);
206 } else {
207 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
208 if (err)
209 goto out;
210 }
211
212 de = f2fs_find_entry(dir, &dotdot, &page);
213 if (de) {
214 f2fs_dentry_kunmap(dir, page);
215 f2fs_put_page(page, 0);
216 } else {
217 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
218 }
219out:
220 if (!err) {
221 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
222 mark_inode_dirty(dir);
223 }
224
225 f2fs_unlock_op(sbi);
226 return err;
227}
228
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900229static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
230 unsigned int flags)
231{
232 struct inode *inode = NULL;
233 struct f2fs_dir_entry *de;
234 struct page *page;
235
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900236 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900237 return ERR_PTR(-ENAMETOOLONG);
238
239 de = f2fs_find_entry(dir, &dentry->d_name, &page);
240 if (de) {
241 nid_t ino = le32_to_cpu(de->ino);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800242 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900243 f2fs_put_page(page, 0);
244
245 inode = f2fs_iget(dir->i_sb, ino);
246 if (IS_ERR(inode))
247 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700248
249 if (f2fs_has_inline_dots(inode)) {
250 int err;
251
252 err = __recover_dot_dentries(inode, dir->i_ino);
253 if (err) {
254 iget_failed(inode);
255 return ERR_PTR(err);
256 }
257 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900258 }
259
260 return d_splice_alias(inode, dentry);
261}
262
263static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
264{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700265 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000266 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900267 struct f2fs_dir_entry *de;
268 struct page *page;
269 int err = -ENOENT;
270
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900271 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900272 f2fs_balance_fs(sbi);
273
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900274 de = f2fs_find_entry(dir, &dentry->d_name, &page);
275 if (!de)
276 goto fail;
277
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900278 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900279 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900280 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900281 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800282 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900283 f2fs_put_page(page, 0);
284 goto fail;
285 }
Chao Yudbeacf02014-09-24 18:17:04 +0800286 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800287 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900288
arter97e1c42042014-08-06 23:22:50 +0900289 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900290 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800291
292 if (IS_DIRSYNC(dir))
293 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900294fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900295 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900296 return err;
297}
298
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700299static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd)
300{
301 struct page *page;
302
303 page = page_follow_link_light(dentry, nd);
304 if (IS_ERR(page))
305 return page;
306
307 /* this is broken symlink case */
308 if (*nd_get_link(nd) == 0) {
309 kunmap(page);
310 page_cache_release(page);
311 return ERR_PTR(-ENOENT);
312 }
313 return page;
314}
315
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900316static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
317 const char *symname)
318{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700319 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900320 struct inode *inode;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200321 size_t symlen = strlen(symname) + 1;
Gu Zhenge4795562013-09-27 18:08:30 +0800322 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900323
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900324 f2fs_balance_fs(sbi);
325
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900326 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
327 if (IS_ERR(inode))
328 return PTR_ERR(inode);
329
330 inode->i_op = &f2fs_symlink_inode_operations;
331 inode->i_mapping->a_ops = &f2fs_dblock_aops;
332
Gu Zhenge4795562013-09-27 18:08:30 +0800333 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900334 err = f2fs_add_link(dentry, inode);
335 if (err)
336 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700337 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900338
339 err = page_symlink(inode, symname, symlen);
340 alloc_nid_done(sbi, inode->i_ino);
341
342 d_instantiate(dentry, inode);
343 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800344
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700345 /*
346 * Let's flush symlink data in order to avoid broken symlink as much as
347 * possible. Nevertheless, fsyncing is the best way, but there is no
348 * way to get a file descriptor in order to flush that.
349 *
350 * Note that, it needs to do dir->fsync to make this recoverable.
351 * If the symlink path is stored into inline_data, there is no
352 * performance regression.
353 */
354 filemap_write_and_wait_range(inode->i_mapping, 0, symlen - 1);
355
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800356 if (IS_DIRSYNC(dir))
357 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900358 return err;
359out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700360 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361 return err;
362}
363
364static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
365{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700366 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900367 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800368 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900369
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900370 f2fs_balance_fs(sbi);
371
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900372 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900373 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900374 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900375
376 inode->i_op = &f2fs_dir_inode_operations;
377 inode->i_fop = &f2fs_dir_operations;
378 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800379 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900380
381 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800382 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900383 err = f2fs_add_link(dentry, inode);
384 if (err)
385 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700386 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900387
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700388 stat_inc_inline_dir(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900389 alloc_nid_done(sbi, inode->i_ino);
390
391 d_instantiate(dentry, inode);
392 unlock_new_inode(inode);
393
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800394 if (IS_DIRSYNC(dir))
395 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900396 return 0;
397
398out_fail:
399 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700400 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900401 return err;
402}
403
404static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
405{
David Howells2b0143b2015-03-17 22:25:59 +0000406 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900407 if (f2fs_empty_dir(inode))
408 return f2fs_unlink(dir, dentry);
409 return -ENOTEMPTY;
410}
411
412static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
413 umode_t mode, dev_t rdev)
414{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700415 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900416 struct inode *inode;
417 int err = 0;
418
419 if (!new_valid_dev(rdev))
420 return -EINVAL;
421
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900422 f2fs_balance_fs(sbi);
423
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424 inode = f2fs_new_inode(dir, mode);
425 if (IS_ERR(inode))
426 return PTR_ERR(inode);
427
428 init_special_inode(inode, inode->i_mode, rdev);
429 inode->i_op = &f2fs_special_inode_operations;
430
Gu Zhenge4795562013-09-27 18:08:30 +0800431 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900432 err = f2fs_add_link(dentry, inode);
433 if (err)
434 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700435 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900436
437 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800438
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900439 d_instantiate(dentry, inode);
440 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800441
442 if (IS_DIRSYNC(dir))
443 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900444 return 0;
445out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700446 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900447 return err;
448}
449
450static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
451 struct inode *new_dir, struct dentry *new_dentry)
452{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700453 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000454 struct inode *old_inode = d_inode(old_dentry);
455 struct inode *new_inode = d_inode(new_dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900456 struct page *old_dir_page;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900457 struct page *old_page, *new_page;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900458 struct f2fs_dir_entry *old_dir_entry = NULL;
459 struct f2fs_dir_entry *old_entry;
460 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800461 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900462
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900463 f2fs_balance_fs(sbi);
464
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900465 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
466 if (!old_entry)
467 goto out;
468
469 if (S_ISDIR(old_inode->i_mode)) {
470 err = -EIO;
471 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
472 if (!old_dir_entry)
473 goto out_old;
474 }
475
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900476 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900477
478 err = -ENOTEMPTY;
479 if (old_dir_entry && !f2fs_empty_dir(new_inode))
480 goto out_dir;
481
482 err = -ENOENT;
483 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
484 &new_page);
485 if (!new_entry)
486 goto out_dir;
487
Chao Yu12560102014-06-24 14:16:24 +0800488 f2fs_lock_op(sbi);
489
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900490 err = acquire_orphan_inode(sbi);
491 if (err)
492 goto put_out_dir;
493
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900494 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900495 release_orphan_inode(sbi);
496 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900497 }
498
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900499 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
500
501 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900502 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900503 if (old_dir_entry)
504 drop_nlink(new_inode);
505 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900506 up_write(&F2FS_I(new_inode)->i_sem);
507
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900508 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900509
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900510 if (!new_inode->i_nlink)
511 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900512 else
513 release_orphan_inode(sbi);
514
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900515 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900516 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900517 } else {
Chao Yu12560102014-06-24 14:16:24 +0800518 f2fs_lock_op(sbi);
519
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900520 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800521 if (err) {
522 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900523 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +0800524 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900525
526 if (old_dir_entry) {
527 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900528 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900529 }
530 }
531
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900532 down_write(&F2FS_I(old_inode)->i_sem);
533 file_lost_pino(old_inode);
534 up_write(&F2FS_I(old_inode)->i_sem);
535
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900536 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900537 mark_inode_dirty(old_inode);
538
Chao Yudbeacf02014-09-24 18:17:04 +0800539 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900540
541 if (old_dir_entry) {
542 if (old_dir != new_dir) {
543 f2fs_set_link(old_inode, old_dir_entry,
544 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900545 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900546 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800547 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900548 f2fs_put_page(old_dir_page, 0);
549 }
550 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900551 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900552 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900553 }
554
Gu Zhenge4795562013-09-27 18:08:30 +0800555 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800556
557 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
558 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900559 return 0;
560
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900561put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800562 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800563 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yudd4d9612014-06-24 14:13:13 +0800564 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900565out_dir:
566 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800567 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900568 f2fs_put_page(old_dir_page, 0);
569 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900570out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800571 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900572 f2fs_put_page(old_page, 0);
573out:
574 return err;
575}
576
Chao Yu32f9bc22014-07-12 19:13:54 +0800577static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
578 struct inode *new_dir, struct dentry *new_dentry)
579{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700580 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000581 struct inode *old_inode = d_inode(old_dentry);
582 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800583 struct page *old_dir_page, *new_dir_page;
584 struct page *old_page, *new_page;
585 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
586 struct f2fs_dir_entry *old_entry, *new_entry;
587 int old_nlink = 0, new_nlink = 0;
588 int err = -ENOENT;
589
590 f2fs_balance_fs(sbi);
591
592 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
593 if (!old_entry)
594 goto out;
595
596 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
597 if (!new_entry)
598 goto out_old;
599
600 /* prepare for updating ".." directory entry info later */
601 if (old_dir != new_dir) {
602 if (S_ISDIR(old_inode->i_mode)) {
603 err = -EIO;
604 old_dir_entry = f2fs_parent_dir(old_inode,
605 &old_dir_page);
606 if (!old_dir_entry)
607 goto out_new;
608 }
609
610 if (S_ISDIR(new_inode->i_mode)) {
611 err = -EIO;
612 new_dir_entry = f2fs_parent_dir(new_inode,
613 &new_dir_page);
614 if (!new_dir_entry)
615 goto out_old_dir;
616 }
617 }
618
619 /*
620 * If cross rename between file and directory those are not
621 * in the same directory, we will inc nlink of file's parent
622 * later, so we should check upper boundary of its nlink.
623 */
624 if ((!old_dir_entry || !new_dir_entry) &&
625 old_dir_entry != new_dir_entry) {
626 old_nlink = old_dir_entry ? -1 : 1;
627 new_nlink = -old_nlink;
628 err = -EMLINK;
629 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
630 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
631 goto out_new_dir;
632 }
633
634 f2fs_lock_op(sbi);
635
636 err = update_dent_inode(old_inode, &new_dentry->d_name);
637 if (err)
638 goto out_unlock;
639
640 err = update_dent_inode(new_inode, &old_dentry->d_name);
641 if (err)
642 goto out_undo;
643
644 /* update ".." directory entry info of old dentry */
645 if (old_dir_entry)
646 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
647
648 /* update ".." directory entry info of new dentry */
649 if (new_dir_entry)
650 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
651
652 /* update directory entry info of old dir inode */
653 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
654
655 down_write(&F2FS_I(old_inode)->i_sem);
656 file_lost_pino(old_inode);
657 up_write(&F2FS_I(old_inode)->i_sem);
658
659 update_inode_page(old_inode);
660
661 old_dir->i_ctime = CURRENT_TIME;
662 if (old_nlink) {
663 down_write(&F2FS_I(old_dir)->i_sem);
664 if (old_nlink < 0)
665 drop_nlink(old_dir);
666 else
667 inc_nlink(old_dir);
668 up_write(&F2FS_I(old_dir)->i_sem);
669 }
670 mark_inode_dirty(old_dir);
671 update_inode_page(old_dir);
672
673 /* update directory entry info of new dir inode */
674 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
675
676 down_write(&F2FS_I(new_inode)->i_sem);
677 file_lost_pino(new_inode);
678 up_write(&F2FS_I(new_inode)->i_sem);
679
680 update_inode_page(new_inode);
681
682 new_dir->i_ctime = CURRENT_TIME;
683 if (new_nlink) {
684 down_write(&F2FS_I(new_dir)->i_sem);
685 if (new_nlink < 0)
686 drop_nlink(new_dir);
687 else
688 inc_nlink(new_dir);
689 up_write(&F2FS_I(new_dir)->i_sem);
690 }
691 mark_inode_dirty(new_dir);
692 update_inode_page(new_dir);
693
694 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800695
696 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
697 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800698 return 0;
699out_undo:
700 /* Still we may fail to recover name info of f2fs_inode here */
701 update_dent_inode(old_inode, &old_dentry->d_name);
702out_unlock:
703 f2fs_unlock_op(sbi);
704out_new_dir:
705 if (new_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800706 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800707 f2fs_put_page(new_dir_page, 0);
708 }
709out_old_dir:
710 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800711 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800712 f2fs_put_page(old_dir_page, 0);
713 }
714out_new:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800715 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800716 f2fs_put_page(new_page, 0);
717out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800718 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800719 f2fs_put_page(old_page, 0);
720out:
721 return err;
722}
723
724static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
725 struct inode *new_dir, struct dentry *new_dentry,
726 unsigned int flags)
727{
728 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
729 return -EINVAL;
730
731 if (flags & RENAME_EXCHANGE) {
732 return f2fs_cross_rename(old_dir, old_dentry,
733 new_dir, new_dentry);
734 }
735 /*
736 * VFS has already handled the new dentry existence case,
737 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
738 */
739 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
740}
741
Chao Yu50732df2014-06-19 16:23:19 +0800742static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
743{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700744 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Chao Yu50732df2014-06-19 16:23:19 +0800745 struct inode *inode;
Chao Yu50732df2014-06-19 16:23:19 +0800746 int err;
747
748 inode = f2fs_new_inode(dir, mode);
749 if (IS_ERR(inode))
750 return PTR_ERR(inode);
751
752 inode->i_op = &f2fs_file_inode_operations;
753 inode->i_fop = &f2fs_file_operations;
754 inode->i_mapping->a_ops = &f2fs_dblock_aops;
755
756 f2fs_lock_op(sbi);
757 err = acquire_orphan_inode(sbi);
758 if (err)
759 goto out;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700760
761 err = f2fs_do_tmpfile(inode, dir);
762 if (err)
763 goto release_out;
764
Chao Yu50732df2014-06-19 16:23:19 +0800765 /*
766 * add this non-linked tmpfile to orphan list, in this way we could
767 * remove all unused data of tmpfile after abnormal power-off.
768 */
769 add_orphan_inode(sbi, inode->i_ino);
Chao Yu50732df2014-06-19 16:23:19 +0800770 f2fs_unlock_op(sbi);
771
772 alloc_nid_done(sbi, inode->i_ino);
Chao Yu3c0d84d2015-02-16 16:20:27 +0800773
774 stat_inc_inline_inode(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800775 d_tmpfile(dentry, inode);
776 unlock_new_inode(inode);
777 return 0;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700778
779release_out:
780 release_orphan_inode(sbi);
Chao Yu50732df2014-06-19 16:23:19 +0800781out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700782 handle_failed_inode(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800783 return err;
784}
785
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900786const struct inode_operations f2fs_dir_inode_operations = {
787 .create = f2fs_create,
788 .lookup = f2fs_lookup,
789 .link = f2fs_link,
790 .unlink = f2fs_unlink,
791 .symlink = f2fs_symlink,
792 .mkdir = f2fs_mkdir,
793 .rmdir = f2fs_rmdir,
794 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +0800795 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +0800796 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900797 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900798 .setattr = f2fs_setattr,
799 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800800 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900801#ifdef CONFIG_F2FS_FS_XATTR
802 .setxattr = generic_setxattr,
803 .getxattr = generic_getxattr,
804 .listxattr = f2fs_listxattr,
805 .removexattr = generic_removexattr,
806#endif
807};
808
809const struct inode_operations f2fs_symlink_inode_operations = {
810 .readlink = generic_readlink,
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700811 .follow_link = f2fs_follow_link,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900812 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900813 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900814 .setattr = f2fs_setattr,
815#ifdef CONFIG_F2FS_FS_XATTR
816 .setxattr = generic_setxattr,
817 .getxattr = generic_getxattr,
818 .listxattr = f2fs_listxattr,
819 .removexattr = generic_removexattr,
820#endif
821};
822
823const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900824 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900825 .setattr = f2fs_setattr,
826 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800827 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900828#ifdef CONFIG_F2FS_FS_XATTR
829 .setxattr = generic_setxattr,
830 .getxattr = generic_getxattr,
831 .listxattr = f2fs_listxattr,
832 .removexattr = generic_removexattr,
833#endif
834};