blob: fdbae21ee8fb2ca991c2e37e35b9fdc505eb291a [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 Kimfcc85a42015-04-21 20:39:58 -070059 /* If the directory encrypted, then we should encrypt the inode. */
60 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
61 f2fs_set_encrypted_inode(inode);
62
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070063 if (f2fs_may_inline_data(inode))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070064 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070065 if (f2fs_may_inline_dentry(inode))
Chao Yu622f28a2014-09-24 18:19:10 +080066 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
67
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -070068 stat_inc_inline_inode(inode);
69 stat_inc_inline_dir(inode);
70
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090071 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090072 mark_inode_dirty(inode);
73 return inode;
74
75out:
76 clear_nlink(inode);
77 unlock_new_inode(inode);
78fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090079 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090080 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090081 iput(inode);
82 if (nid_free)
83 alloc_nid_failed(sbi, ino);
84 return ERR_PTR(err);
85}
86
87static int is_multimedia_file(const unsigned char *s, const char *sub)
88{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020089 size_t slen = strlen(s);
90 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090091
92 if (sublen > slen)
Namjae Jeone66509f02013-04-20 01:27:21 +090093 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090094
Gu Zheng749ebfd2013-08-22 18:11:25 +080095 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090096}
97
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090098/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +090099 * Set multimedia files as cold files for hot/cold data separation
100 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900101static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900102 const unsigned char *name)
103{
104 int i;
105 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
106
107 int count = le32_to_cpu(sbi->raw_super->extension_count);
108 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900109 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900110 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900111 break;
112 }
113 }
114}
115
116static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
117 bool excl)
118{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700119 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900120 struct inode *inode;
121 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800122 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900123
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900124 f2fs_balance_fs(sbi);
125
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900126 inode = f2fs_new_inode(dir, mode);
127 if (IS_ERR(inode))
128 return PTR_ERR(inode);
129
130 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900131 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900132
133 inode->i_op = &f2fs_file_inode_operations;
134 inode->i_fop = &f2fs_file_operations;
135 inode->i_mapping->a_ops = &f2fs_dblock_aops;
136 ino = inode->i_ino;
137
Gu Zhenge4795562013-09-27 18:08:30 +0800138 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900139 err = f2fs_add_link(dentry, inode);
140 if (err)
141 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700142 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900143
144 alloc_nid_done(sbi, ino);
145
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900146 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900147 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800148
149 if (IS_DIRSYNC(dir))
150 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900151 return 0;
152out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700153 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900154 return err;
155}
156
157static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
158 struct dentry *dentry)
159{
David Howells2b0143b2015-03-17 22:25:59 +0000160 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700161 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800162 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900163
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700164 if (f2fs_encrypted_inode(dir) &&
165 !f2fs_is_child_context_consistent_with_parent(dir, inode))
166 return -EPERM;
167
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900168 f2fs_balance_fs(sbi);
169
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900170 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900171 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900172
173 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800174 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900175 err = f2fs_add_link(dentry, inode);
176 if (err)
177 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700178 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900179
180 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800181
182 if (IS_DIRSYNC(dir))
183 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900184 return 0;
185out:
186 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
187 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700188 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900189 return err;
190}
191
192struct dentry *f2fs_get_parent(struct dentry *child)
193{
194 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +0000195 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900196 if (!ino)
197 return ERR_PTR(-ENOENT);
David Howells2b0143b2015-03-17 22:25:59 +0000198 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900199}
200
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700201static int __recover_dot_dentries(struct inode *dir, nid_t pino)
202{
203 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
204 struct qstr dot = QSTR_INIT(".", 1);
205 struct qstr dotdot = QSTR_INIT("..", 2);
206 struct f2fs_dir_entry *de;
207 struct page *page;
208 int err = 0;
209
210 f2fs_lock_op(sbi);
211
212 de = f2fs_find_entry(dir, &dot, &page);
213 if (de) {
214 f2fs_dentry_kunmap(dir, page);
215 f2fs_put_page(page, 0);
216 } else {
217 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
218 if (err)
219 goto out;
220 }
221
222 de = f2fs_find_entry(dir, &dotdot, &page);
223 if (de) {
224 f2fs_dentry_kunmap(dir, page);
225 f2fs_put_page(page, 0);
226 } else {
227 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
228 }
229out:
230 if (!err) {
231 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
232 mark_inode_dirty(dir);
233 }
234
235 f2fs_unlock_op(sbi);
236 return err;
237}
238
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900239static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
240 unsigned int flags)
241{
242 struct inode *inode = NULL;
243 struct f2fs_dir_entry *de;
244 struct page *page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700245 nid_t ino;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700246 int err = 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900247
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900248 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900249 return ERR_PTR(-ENAMETOOLONG);
250
251 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700252 if (!de)
253 return d_splice_alias(inode, dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700255 ino = le32_to_cpu(de->ino);
256 f2fs_dentry_kunmap(dir, page);
257 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700258
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700259 inode = f2fs_iget(dir->i_sb, ino);
260 if (IS_ERR(inode))
261 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700262
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700263 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700264 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700265 if (err)
266 goto err_out;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900267 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900268 return d_splice_alias(inode, dentry);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700269
270err_out:
271 iget_failed(inode);
272 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900273}
274
275static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
276{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700277 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000278 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900279 struct f2fs_dir_entry *de;
280 struct page *page;
281 int err = -ENOENT;
282
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900283 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900284 f2fs_balance_fs(sbi);
285
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900286 de = f2fs_find_entry(dir, &dentry->d_name, &page);
287 if (!de)
288 goto fail;
289
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900290 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900291 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900292 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900293 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800294 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900295 f2fs_put_page(page, 0);
296 goto fail;
297 }
Chao Yudbeacf02014-09-24 18:17:04 +0800298 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800299 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900300
arter97e1c42042014-08-06 23:22:50 +0900301 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900302 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800303
304 if (IS_DIRSYNC(dir))
305 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900306fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900307 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900308 return err;
309}
310
Al Viro6e771372015-05-02 13:37:52 -0400311static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700312{
Al Viro6e771372015-05-02 13:37:52 -0400313 const char *link = page_follow_link_light(dentry, cookie);
Al Viro680baac2015-05-02 13:32:22 -0400314 if (!IS_ERR(link) && !*link) {
315 /* this is broken symlink case */
Al Viro5f2c4172015-05-07 11:14:26 -0400316 page_put_link(NULL, *cookie);
Al Viro680baac2015-05-02 13:32:22 -0400317 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700318 }
Al Viro680baac2015-05-02 13:32:22 -0400319 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700320}
321
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900322static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
323 const char *symname)
324{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700325 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900326 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700327 size_t len = strlen(symname);
328 size_t p_len;
329 char *p_str;
330 struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
331 struct f2fs_encrypted_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800332 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900333
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700334 if (len > dir->i_sb->s_blocksize)
335 return -ENAMETOOLONG;
336
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900337 f2fs_balance_fs(sbi);
338
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900339 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
340 if (IS_ERR(inode))
341 return PTR_ERR(inode);
342
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700343 if (f2fs_encrypted_inode(inode))
344 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
345 else
346 inode->i_op = &f2fs_symlink_inode_operations;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900347 inode->i_mapping->a_ops = &f2fs_dblock_aops;
348
Gu Zhenge4795562013-09-27 18:08:30 +0800349 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900350 err = f2fs_add_link(dentry, inode);
351 if (err)
352 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700353 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900354 alloc_nid_done(sbi, inode->i_ino);
355
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700356 if (f2fs_encrypted_inode(dir)) {
357 struct qstr istr = QSTR_INIT(symname, len);
358
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700359 err = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700360 if (err)
361 goto err_out;
362
363 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
364 if (err)
365 goto err_out;
366
367 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
368 if (err < 0)
369 goto err_out;
370
371 p_len = encrypted_symlink_data_len(disk_link.len) + 1;
372
373 if (p_len > dir->i_sb->s_blocksize) {
374 err = -ENAMETOOLONG;
375 goto err_out;
376 }
377
378 sd = kzalloc(p_len, GFP_NOFS);
379 if (!sd) {
380 err = -ENOMEM;
381 goto err_out;
382 }
383 memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
384 sd->len = cpu_to_le16(disk_link.len);
385 p_str = (char *)sd;
386 } else {
387 p_len = len + 1;
388 p_str = (char *)symname;
389 }
390
391 err = page_symlink(inode, p_str, p_len);
392
393err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900394 d_instantiate(dentry, inode);
395 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800396
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700397 /*
398 * Let's flush symlink data in order to avoid broken symlink as much as
399 * possible. Nevertheless, fsyncing is the best way, but there is no
400 * way to get a file descriptor in order to flush that.
401 *
402 * Note that, it needs to do dir->fsync to make this recoverable.
403 * If the symlink path is stored into inline_data, there is no
404 * performance regression.
405 */
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700406 if (!err)
407 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700408
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800409 if (IS_DIRSYNC(dir))
410 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700411
412 kfree(sd);
413 f2fs_fname_crypto_free_buffer(&disk_link);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900414 return err;
415out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700416 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900417 return err;
418}
419
420static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
421{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700422 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900423 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800424 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900425
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900426 f2fs_balance_fs(sbi);
427
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900428 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900429 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900430 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900431
432 inode->i_op = &f2fs_dir_inode_operations;
433 inode->i_fop = &f2fs_dir_operations;
434 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800435 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900436
437 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800438 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900439 err = f2fs_add_link(dentry, inode);
440 if (err)
441 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700442 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900443
444 alloc_nid_done(sbi, inode->i_ino);
445
446 d_instantiate(dentry, inode);
447 unlock_new_inode(inode);
448
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800449 if (IS_DIRSYNC(dir))
450 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900451 return 0;
452
453out_fail:
454 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700455 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900456 return err;
457}
458
459static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
460{
David Howells2b0143b2015-03-17 22:25:59 +0000461 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900462 if (f2fs_empty_dir(inode))
463 return f2fs_unlink(dir, dentry);
464 return -ENOTEMPTY;
465}
466
467static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
468 umode_t mode, dev_t rdev)
469{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700470 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900471 struct inode *inode;
472 int err = 0;
473
474 if (!new_valid_dev(rdev))
475 return -EINVAL;
476
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900477 f2fs_balance_fs(sbi);
478
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900479 inode = f2fs_new_inode(dir, mode);
480 if (IS_ERR(inode))
481 return PTR_ERR(inode);
482
483 init_special_inode(inode, inode->i_mode, rdev);
484 inode->i_op = &f2fs_special_inode_operations;
485
Gu Zhenge4795562013-09-27 18:08:30 +0800486 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900487 err = f2fs_add_link(dentry, inode);
488 if (err)
489 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700490 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900491
492 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800493
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900494 d_instantiate(dentry, inode);
495 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800496
497 if (IS_DIRSYNC(dir))
498 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900499 return 0;
500out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700501 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900502 return err;
503}
504
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800505static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
506 umode_t mode, struct inode **whiteout)
507{
508 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
509 struct inode *inode;
510 int err;
511
512 if (!whiteout)
513 f2fs_balance_fs(sbi);
514
515 inode = f2fs_new_inode(dir, mode);
516 if (IS_ERR(inode))
517 return PTR_ERR(inode);
518
519 if (whiteout) {
520 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
521 inode->i_op = &f2fs_special_inode_operations;
522 } else {
523 inode->i_op = &f2fs_file_inode_operations;
524 inode->i_fop = &f2fs_file_operations;
525 inode->i_mapping->a_ops = &f2fs_dblock_aops;
526 }
527
528 f2fs_lock_op(sbi);
529 err = acquire_orphan_inode(sbi);
530 if (err)
531 goto out;
532
533 err = f2fs_do_tmpfile(inode, dir);
534 if (err)
535 goto release_out;
536
537 /*
538 * add this non-linked tmpfile to orphan list, in this way we could
539 * remove all unused data of tmpfile after abnormal power-off.
540 */
541 add_orphan_inode(sbi, inode->i_ino);
542 f2fs_unlock_op(sbi);
543
544 alloc_nid_done(sbi, inode->i_ino);
545
546 if (whiteout) {
547 inode_dec_link_count(inode);
548 *whiteout = inode;
549 } else {
550 d_tmpfile(dentry, inode);
551 }
552 unlock_new_inode(inode);
553 return 0;
554
555release_out:
556 release_orphan_inode(sbi);
557out:
558 handle_failed_inode(inode);
559 return err;
560}
561
562static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
563{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700564 if (f2fs_encrypted_inode(dir)) {
565 int err = f2fs_get_encryption_info(dir);
566 if (err)
567 return err;
568 }
569
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800570 return __f2fs_tmpfile(dir, dentry, mode, NULL);
571}
572
573static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
574{
575 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
576}
577
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900578static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800579 struct inode *new_dir, struct dentry *new_dentry,
580 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900581{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700582 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000583 struct inode *old_inode = d_inode(old_dentry);
584 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800585 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900586 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800587 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900588 struct f2fs_dir_entry *old_dir_entry = NULL;
589 struct f2fs_dir_entry *old_entry;
590 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800591 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900592
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700593 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
594 !f2fs_is_child_context_consistent_with_parent(new_dir,
595 old_inode)) {
596 err = -EPERM;
597 goto out;
598 }
599
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900600 f2fs_balance_fs(sbi);
601
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900602 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
603 if (!old_entry)
604 goto out;
605
606 if (S_ISDIR(old_inode->i_mode)) {
607 err = -EIO;
608 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
609 if (!old_dir_entry)
610 goto out_old;
611 }
612
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800613 if (flags & RENAME_WHITEOUT) {
614 err = f2fs_create_whiteout(old_dir, &whiteout);
615 if (err)
616 goto out_dir;
617 }
618
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900619 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900620
621 err = -ENOTEMPTY;
622 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800623 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900624
625 err = -ENOENT;
626 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
627 &new_page);
628 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800629 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900630
Chao Yu12560102014-06-24 14:16:24 +0800631 f2fs_lock_op(sbi);
632
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900633 err = acquire_orphan_inode(sbi);
634 if (err)
635 goto put_out_dir;
636
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700637 if (update_dent_inode(old_inode, new_inode,
638 &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900639 release_orphan_inode(sbi);
640 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900641 }
642
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900643 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
644
645 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900646 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900647 if (old_dir_entry)
648 drop_nlink(new_inode);
649 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900650 up_write(&F2FS_I(new_inode)->i_sem);
651
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900652 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900653
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900654 if (!new_inode->i_nlink)
655 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900656 else
657 release_orphan_inode(sbi);
658
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900659 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900660 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900661 } else {
Chao Yu12560102014-06-24 14:16:24 +0800662 f2fs_lock_op(sbi);
663
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900664 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800665 if (err) {
666 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800667 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800668 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900669
670 if (old_dir_entry) {
671 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900672 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900673 }
674 }
675
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900676 down_write(&F2FS_I(old_inode)->i_sem);
677 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700678 if (new_inode && file_enc_name(new_inode))
679 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900680 up_write(&F2FS_I(old_inode)->i_sem);
681
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900682 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900683 mark_inode_dirty(old_inode);
684
Chao Yudbeacf02014-09-24 18:17:04 +0800685 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900686
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800687 if (whiteout) {
688 whiteout->i_state |= I_LINKABLE;
689 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
690 err = f2fs_add_link(old_dentry, whiteout);
691 if (err)
692 goto put_out_dir;
693 whiteout->i_state &= ~I_LINKABLE;
694 iput(whiteout);
695 }
696
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900697 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800698 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900699 f2fs_set_link(old_inode, old_dir_entry,
700 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900701 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900702 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800703 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900704 f2fs_put_page(old_dir_page, 0);
705 }
706 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900707 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900708 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900709 }
710
Gu Zhenge4795562013-09-27 18:08:30 +0800711 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800712
713 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
714 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900715 return 0;
716
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900717put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800718 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800719 if (new_page) {
720 f2fs_dentry_kunmap(new_dir, new_page);
721 f2fs_put_page(new_page, 0);
722 }
723out_whiteout:
724 if (whiteout)
725 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900726out_dir:
727 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800728 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900729 f2fs_put_page(old_dir_page, 0);
730 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900731out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800732 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900733 f2fs_put_page(old_page, 0);
734out:
735 return err;
736}
737
Chao Yu32f9bc22014-07-12 19:13:54 +0800738static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
739 struct inode *new_dir, struct dentry *new_dentry)
740{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700741 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000742 struct inode *old_inode = d_inode(old_dentry);
743 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800744 struct page *old_dir_page, *new_dir_page;
745 struct page *old_page, *new_page;
746 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
747 struct f2fs_dir_entry *old_entry, *new_entry;
748 int old_nlink = 0, new_nlink = 0;
749 int err = -ENOENT;
750
Chao Yud3baf7c2015-05-25 18:07:02 +0800751 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
752 (old_dir != new_dir) &&
753 (!f2fs_is_child_context_consistent_with_parent(new_dir,
754 old_inode) ||
755 !f2fs_is_child_context_consistent_with_parent(old_dir,
756 new_inode)))
757 return -EPERM;
758
Chao Yu32f9bc22014-07-12 19:13:54 +0800759 f2fs_balance_fs(sbi);
760
761 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
762 if (!old_entry)
763 goto out;
764
765 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
766 if (!new_entry)
767 goto out_old;
768
769 /* prepare for updating ".." directory entry info later */
770 if (old_dir != new_dir) {
771 if (S_ISDIR(old_inode->i_mode)) {
772 err = -EIO;
773 old_dir_entry = f2fs_parent_dir(old_inode,
774 &old_dir_page);
775 if (!old_dir_entry)
776 goto out_new;
777 }
778
779 if (S_ISDIR(new_inode->i_mode)) {
780 err = -EIO;
781 new_dir_entry = f2fs_parent_dir(new_inode,
782 &new_dir_page);
783 if (!new_dir_entry)
784 goto out_old_dir;
785 }
786 }
787
788 /*
789 * If cross rename between file and directory those are not
790 * in the same directory, we will inc nlink of file's parent
791 * later, so we should check upper boundary of its nlink.
792 */
793 if ((!old_dir_entry || !new_dir_entry) &&
794 old_dir_entry != new_dir_entry) {
795 old_nlink = old_dir_entry ? -1 : 1;
796 new_nlink = -old_nlink;
797 err = -EMLINK;
798 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
799 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
800 goto out_new_dir;
801 }
802
803 f2fs_lock_op(sbi);
804
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700805 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800806 if (err)
807 goto out_unlock;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700808 if (file_enc_name(new_inode))
809 file_set_enc_name(old_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800810
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700811 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800812 if (err)
813 goto out_undo;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700814 if (file_enc_name(old_inode))
815 file_set_enc_name(new_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800816
817 /* update ".." directory entry info of old dentry */
818 if (old_dir_entry)
819 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
820
821 /* update ".." directory entry info of new dentry */
822 if (new_dir_entry)
823 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
824
825 /* update directory entry info of old dir inode */
826 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
827
828 down_write(&F2FS_I(old_inode)->i_sem);
829 file_lost_pino(old_inode);
830 up_write(&F2FS_I(old_inode)->i_sem);
831
832 update_inode_page(old_inode);
833
834 old_dir->i_ctime = CURRENT_TIME;
835 if (old_nlink) {
836 down_write(&F2FS_I(old_dir)->i_sem);
837 if (old_nlink < 0)
838 drop_nlink(old_dir);
839 else
840 inc_nlink(old_dir);
841 up_write(&F2FS_I(old_dir)->i_sem);
842 }
843 mark_inode_dirty(old_dir);
844 update_inode_page(old_dir);
845
846 /* update directory entry info of new dir inode */
847 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
848
849 down_write(&F2FS_I(new_inode)->i_sem);
850 file_lost_pino(new_inode);
851 up_write(&F2FS_I(new_inode)->i_sem);
852
853 update_inode_page(new_inode);
854
855 new_dir->i_ctime = CURRENT_TIME;
856 if (new_nlink) {
857 down_write(&F2FS_I(new_dir)->i_sem);
858 if (new_nlink < 0)
859 drop_nlink(new_dir);
860 else
861 inc_nlink(new_dir);
862 up_write(&F2FS_I(new_dir)->i_sem);
863 }
864 mark_inode_dirty(new_dir);
865 update_inode_page(new_dir);
866
867 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800868
869 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
870 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800871 return 0;
872out_undo:
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700873 /*
874 * Still we may fail to recover name info of f2fs_inode here
875 * Drop it, once its name is set as encrypted
876 */
877 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800878out_unlock:
879 f2fs_unlock_op(sbi);
880out_new_dir:
881 if (new_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800882 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800883 f2fs_put_page(new_dir_page, 0);
884 }
885out_old_dir:
886 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800887 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800888 f2fs_put_page(old_dir_page, 0);
889 }
890out_new:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800891 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800892 f2fs_put_page(new_page, 0);
893out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800894 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800895 f2fs_put_page(old_page, 0);
896out:
897 return err;
898}
899
900static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
901 struct inode *new_dir, struct dentry *new_dentry,
902 unsigned int flags)
903{
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800904 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +0800905 return -EINVAL;
906
907 if (flags & RENAME_EXCHANGE) {
908 return f2fs_cross_rename(old_dir, old_dentry,
909 new_dir, new_dentry);
910 }
911 /*
912 * VFS has already handled the new dentry existence case,
913 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
914 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800915 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +0800916}
917
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700918#ifdef CONFIG_F2FS_FS_ENCRYPTION
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700919static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
Chao Yu50732df2014-06-19 16:23:19 +0800920{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700921 struct page *cpage = NULL;
922 char *caddr, *paddr = NULL;
923 struct f2fs_str cstr;
924 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
925 struct inode *inode = d_inode(dentry);
926 struct f2fs_encrypted_symlink_data *sd;
927 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
928 u32 max_size = inode->i_sb->s_blocksize;
929 int res;
Chao Yu50732df2014-06-19 16:23:19 +0800930
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700931 res = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700932 if (res)
933 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800934
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700935 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
936 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700937 return ERR_CAST(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700938 caddr = kmap(cpage);
939 caddr[size] = 0;
Chao Yu50732df2014-06-19 16:23:19 +0800940
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700941 /* Symlink is encrypted */
942 sd = (struct f2fs_encrypted_symlink_data *)caddr;
943 cstr.name = sd->encrypted_path;
944 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700945
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700946 /* this is broken symlink case */
947 if (cstr.name[0] == 0 && cstr.len == 0) {
948 res = -ENOENT;
949 goto errout;
950 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700951
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700952 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
953 max_size) {
954 /* Symlink data on the disk is corrupted */
955 res = -EIO;
956 goto errout;
957 }
958 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
959 if (res)
960 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +0800961
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700962 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
963 if (res < 0)
964 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +0800965
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700966 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700967
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700968 /* Null-terminate the name */
969 paddr[res] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700970
971 kunmap(cpage);
972 page_cache_release(cpage);
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700973 return *cookie = paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700974errout:
975 f2fs_fname_crypto_free_buffer(&pstr);
976 kunmap(cpage);
977 page_cache_release(cpage);
978 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800979}
980
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700981const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
982 .readlink = generic_readlink,
983 .follow_link = f2fs_encrypted_follow_link,
984 .put_link = kfree_put_link,
985 .getattr = f2fs_getattr,
986 .setattr = f2fs_setattr,
987 .setxattr = generic_setxattr,
988 .getxattr = generic_getxattr,
989 .listxattr = f2fs_listxattr,
990 .removexattr = generic_removexattr,
991};
992#endif
993
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900994const struct inode_operations f2fs_dir_inode_operations = {
995 .create = f2fs_create,
996 .lookup = f2fs_lookup,
997 .link = f2fs_link,
998 .unlink = f2fs_unlink,
999 .symlink = f2fs_symlink,
1000 .mkdir = f2fs_mkdir,
1001 .rmdir = f2fs_rmdir,
1002 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001003 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001004 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001005 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001006 .setattr = f2fs_setattr,
1007 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001008 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001009#ifdef CONFIG_F2FS_FS_XATTR
1010 .setxattr = generic_setxattr,
1011 .getxattr = generic_getxattr,
1012 .listxattr = f2fs_listxattr,
1013 .removexattr = generic_removexattr,
1014#endif
1015};
1016
1017const struct inode_operations f2fs_symlink_inode_operations = {
1018 .readlink = generic_readlink,
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -07001019 .follow_link = f2fs_follow_link,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001020 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001021 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001022 .setattr = f2fs_setattr,
1023#ifdef CONFIG_F2FS_FS_XATTR
1024 .setxattr = generic_setxattr,
1025 .getxattr = generic_getxattr,
1026 .listxattr = f2fs_listxattr,
1027 .removexattr = generic_removexattr,
1028#endif
1029};
1030
1031const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001032 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001033 .setattr = f2fs_setattr,
1034 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001035 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001036#ifdef CONFIG_F2FS_FS_XATTR
1037 .setxattr = generic_setxattr,
1038 .getxattr = generic_getxattr,
1039 .listxattr = f2fs_listxattr,
1040 .removexattr = generic_removexattr,
1041#endif
1042};