blob: 55d0d27dfdf2067b725070b14068849b21244323 [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
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700311static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd)
312{
Jaegeuk Kim7263b1b2015-04-22 11:03:48 -0700313 struct page *page = page_follow_link_light(dentry, nd);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700314
Jaegeuk Kim7263b1b2015-04-22 11:03:48 -0700315 if (IS_ERR_OR_NULL(page))
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700316 return page;
317
318 /* this is broken symlink case */
319 if (*nd_get_link(nd) == 0) {
Jaegeuk Kim7263b1b2015-04-22 11:03:48 -0700320 page_put_link(dentry, nd, page);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700321 return ERR_PTR(-ENOENT);
322 }
323 return page;
324}
325
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900326static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
327 const char *symname)
328{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700329 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900330 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700331 size_t len = strlen(symname);
332 size_t p_len;
333 char *p_str;
334 struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
335 struct f2fs_encrypted_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800336 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900337
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700338 if (len > dir->i_sb->s_blocksize)
339 return -ENAMETOOLONG;
340
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_IFLNK | S_IRWXUGO);
344 if (IS_ERR(inode))
345 return PTR_ERR(inode);
346
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700347 if (f2fs_encrypted_inode(inode))
348 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
349 else
350 inode->i_op = &f2fs_symlink_inode_operations;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351 inode->i_mapping->a_ops = &f2fs_dblock_aops;
352
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;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700357 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900358 alloc_nid_done(sbi, inode->i_ino);
359
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700360 if (f2fs_encrypted_inode(dir)) {
361 struct qstr istr = QSTR_INIT(symname, len);
362
363 err = f2fs_inherit_context(dir, inode, NULL);
364 if (err)
365 goto err_out;
366
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700367 err = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700368 if (err)
369 goto err_out;
370
371 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
372 if (err)
373 goto err_out;
374
375 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
376 if (err < 0)
377 goto err_out;
378
379 p_len = encrypted_symlink_data_len(disk_link.len) + 1;
380
381 if (p_len > dir->i_sb->s_blocksize) {
382 err = -ENAMETOOLONG;
383 goto err_out;
384 }
385
386 sd = kzalloc(p_len, GFP_NOFS);
387 if (!sd) {
388 err = -ENOMEM;
389 goto err_out;
390 }
391 memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
392 sd->len = cpu_to_le16(disk_link.len);
393 p_str = (char *)sd;
394 } else {
395 p_len = len + 1;
396 p_str = (char *)symname;
397 }
398
399 err = page_symlink(inode, p_str, p_len);
400
401err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900402 d_instantiate(dentry, inode);
403 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800404
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700405 /*
406 * Let's flush symlink data in order to avoid broken symlink as much as
407 * possible. Nevertheless, fsyncing is the best way, but there is no
408 * way to get a file descriptor in order to flush that.
409 *
410 * Note that, it needs to do dir->fsync to make this recoverable.
411 * If the symlink path is stored into inline_data, there is no
412 * performance regression.
413 */
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700414 if (!err)
415 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700416
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800417 if (IS_DIRSYNC(dir))
418 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700419
420 kfree(sd);
421 f2fs_fname_crypto_free_buffer(&disk_link);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900422 return err;
423out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700424 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900425 return err;
426}
427
428static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
429{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700430 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900431 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800432 int err;
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 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900437 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900438 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900439
440 inode->i_op = &f2fs_dir_inode_operations;
441 inode->i_fop = &f2fs_dir_operations;
442 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800443 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900444
445 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800446 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900447 err = f2fs_add_link(dentry, inode);
448 if (err)
449 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700450 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900451
452 alloc_nid_done(sbi, inode->i_ino);
453
454 d_instantiate(dentry, inode);
455 unlock_new_inode(inode);
456
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800457 if (IS_DIRSYNC(dir))
458 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900459 return 0;
460
461out_fail:
462 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700463 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900464 return err;
465}
466
467static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
468{
David Howells2b0143b2015-03-17 22:25:59 +0000469 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900470 if (f2fs_empty_dir(inode))
471 return f2fs_unlink(dir, dentry);
472 return -ENOTEMPTY;
473}
474
475static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
476 umode_t mode, dev_t rdev)
477{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700478 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900479 struct inode *inode;
480 int err = 0;
481
482 if (!new_valid_dev(rdev))
483 return -EINVAL;
484
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900485 f2fs_balance_fs(sbi);
486
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900487 inode = f2fs_new_inode(dir, mode);
488 if (IS_ERR(inode))
489 return PTR_ERR(inode);
490
491 init_special_inode(inode, inode->i_mode, rdev);
492 inode->i_op = &f2fs_special_inode_operations;
493
Gu Zhenge4795562013-09-27 18:08:30 +0800494 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900495 err = f2fs_add_link(dentry, inode);
496 if (err)
497 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700498 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900499
500 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800501
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900502 d_instantiate(dentry, inode);
503 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800504
505 if (IS_DIRSYNC(dir))
506 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900507 return 0;
508out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700509 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900510 return err;
511}
512
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800513static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
514 umode_t mode, struct inode **whiteout)
515{
516 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
517 struct inode *inode;
518 int err;
519
520 if (!whiteout)
521 f2fs_balance_fs(sbi);
522
523 inode = f2fs_new_inode(dir, mode);
524 if (IS_ERR(inode))
525 return PTR_ERR(inode);
526
527 if (whiteout) {
528 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
529 inode->i_op = &f2fs_special_inode_operations;
530 } else {
531 inode->i_op = &f2fs_file_inode_operations;
532 inode->i_fop = &f2fs_file_operations;
533 inode->i_mapping->a_ops = &f2fs_dblock_aops;
534 }
535
536 f2fs_lock_op(sbi);
537 err = acquire_orphan_inode(sbi);
538 if (err)
539 goto out;
540
541 err = f2fs_do_tmpfile(inode, dir);
542 if (err)
543 goto release_out;
544
545 /*
546 * add this non-linked tmpfile to orphan list, in this way we could
547 * remove all unused data of tmpfile after abnormal power-off.
548 */
549 add_orphan_inode(sbi, inode->i_ino);
550 f2fs_unlock_op(sbi);
551
552 alloc_nid_done(sbi, inode->i_ino);
553
554 if (whiteout) {
555 inode_dec_link_count(inode);
556 *whiteout = inode;
557 } else {
558 d_tmpfile(dentry, inode);
559 }
560 unlock_new_inode(inode);
561 return 0;
562
563release_out:
564 release_orphan_inode(sbi);
565out:
566 handle_failed_inode(inode);
567 return err;
568}
569
570static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
571{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700572 if (f2fs_encrypted_inode(dir)) {
573 int err = f2fs_get_encryption_info(dir);
574 if (err)
575 return err;
576 }
577
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800578 return __f2fs_tmpfile(dir, dentry, mode, NULL);
579}
580
581static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
582{
583 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
584}
585
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900586static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800587 struct inode *new_dir, struct dentry *new_dentry,
588 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900589{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700590 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000591 struct inode *old_inode = d_inode(old_dentry);
592 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800593 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900594 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800595 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900596 struct f2fs_dir_entry *old_dir_entry = NULL;
597 struct f2fs_dir_entry *old_entry;
598 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800599 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900600
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700601 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
602 !f2fs_is_child_context_consistent_with_parent(new_dir,
603 old_inode)) {
604 err = -EPERM;
605 goto out;
606 }
607
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900608 f2fs_balance_fs(sbi);
609
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900610 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
611 if (!old_entry)
612 goto out;
613
614 if (S_ISDIR(old_inode->i_mode)) {
615 err = -EIO;
616 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
617 if (!old_dir_entry)
618 goto out_old;
619 }
620
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800621 if (flags & RENAME_WHITEOUT) {
622 err = f2fs_create_whiteout(old_dir, &whiteout);
623 if (err)
624 goto out_dir;
625 }
626
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900627 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900628
629 err = -ENOTEMPTY;
630 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800631 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900632
633 err = -ENOENT;
634 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
635 &new_page);
636 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800637 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900638
Chao Yu12560102014-06-24 14:16:24 +0800639 f2fs_lock_op(sbi);
640
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900641 err = acquire_orphan_inode(sbi);
642 if (err)
643 goto put_out_dir;
644
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700645 if (update_dent_inode(old_inode, new_inode,
646 &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900647 release_orphan_inode(sbi);
648 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900649 }
650
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900651 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
652
653 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900654 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900655 if (old_dir_entry)
656 drop_nlink(new_inode);
657 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900658 up_write(&F2FS_I(new_inode)->i_sem);
659
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900660 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900661
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900662 if (!new_inode->i_nlink)
663 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900664 else
665 release_orphan_inode(sbi);
666
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900667 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900668 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900669 } else {
Chao Yu12560102014-06-24 14:16:24 +0800670 f2fs_lock_op(sbi);
671
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900672 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800673 if (err) {
674 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800675 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800676 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900677
678 if (old_dir_entry) {
679 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900680 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900681 }
682 }
683
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900684 down_write(&F2FS_I(old_inode)->i_sem);
685 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700686 if (new_inode && file_enc_name(new_inode))
687 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900688 up_write(&F2FS_I(old_inode)->i_sem);
689
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900690 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900691 mark_inode_dirty(old_inode);
692
Chao Yudbeacf02014-09-24 18:17:04 +0800693 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900694
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800695 if (whiteout) {
696 whiteout->i_state |= I_LINKABLE;
697 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
698 err = f2fs_add_link(old_dentry, whiteout);
699 if (err)
700 goto put_out_dir;
701 whiteout->i_state &= ~I_LINKABLE;
702 iput(whiteout);
703 }
704
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900705 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800706 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900707 f2fs_set_link(old_inode, old_dir_entry,
708 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900709 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900710 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800711 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900712 f2fs_put_page(old_dir_page, 0);
713 }
714 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900715 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900716 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900717 }
718
Gu Zhenge4795562013-09-27 18:08:30 +0800719 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800720
721 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
722 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900723 return 0;
724
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900725put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800726 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800727 if (new_page) {
728 f2fs_dentry_kunmap(new_dir, new_page);
729 f2fs_put_page(new_page, 0);
730 }
731out_whiteout:
732 if (whiteout)
733 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900734out_dir:
735 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800736 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900737 f2fs_put_page(old_dir_page, 0);
738 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900739out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800740 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741 f2fs_put_page(old_page, 0);
742out:
743 return err;
744}
745
Chao Yu32f9bc22014-07-12 19:13:54 +0800746static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
747 struct inode *new_dir, struct dentry *new_dentry)
748{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700749 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000750 struct inode *old_inode = d_inode(old_dentry);
751 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800752 struct page *old_dir_page, *new_dir_page;
753 struct page *old_page, *new_page;
754 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
755 struct f2fs_dir_entry *old_entry, *new_entry;
756 int old_nlink = 0, new_nlink = 0;
757 int err = -ENOENT;
758
759 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 Yu50732df2014-06-19 16:23:19 +0800916}
917
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700918#ifdef CONFIG_F2FS_FS_ENCRYPTION
919static void *f2fs_encrypted_follow_link(struct dentry *dentry,
920 struct nameidata *nd)
921{
922 struct page *cpage = NULL;
923 char *caddr, *paddr = NULL;
924 struct f2fs_str cstr;
925 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
926 struct inode *inode = d_inode(dentry);
927 struct f2fs_encrypted_symlink_data *sd;
928 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
929 u32 max_size = inode->i_sb->s_blocksize;
930 int res;
931
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700932 res = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700933 if (res)
934 return ERR_PTR(res);
935
936 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
937 if (IS_ERR(cpage))
938 return cpage;
939 caddr = kmap(cpage);
940 caddr[size] = 0;
941
942 /* Symlink is encrypted */
943 sd = (struct f2fs_encrypted_symlink_data *)caddr;
944 cstr.name = sd->encrypted_path;
945 cstr.len = le16_to_cpu(sd->len);
946
947 /* this is broken symlink case */
948 if (cstr.name[0] == 0 && cstr.len == 0) {
949 res = -ENOENT;
950 goto errout;
951 }
952
953 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
954 max_size) {
955 /* Symlink data on the disk is corrupted */
956 res = -EIO;
957 goto errout;
958 }
959 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
960 if (res)
961 goto errout;
962
963 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
964 if (res < 0)
965 goto errout;
966
967 paddr = pstr.name;
968
969 /* Null-terminate the name */
970 paddr[res] = '\0';
971 nd_set_link(nd, paddr);
972
973 kunmap(cpage);
974 page_cache_release(cpage);
975 return NULL;
976errout:
977 f2fs_fname_crypto_free_buffer(&pstr);
978 kunmap(cpage);
979 page_cache_release(cpage);
980 return ERR_PTR(res);
981}
982
983const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
984 .readlink = generic_readlink,
985 .follow_link = f2fs_encrypted_follow_link,
986 .put_link = kfree_put_link,
987 .getattr = f2fs_getattr,
988 .setattr = f2fs_setattr,
989 .setxattr = generic_setxattr,
990 .getxattr = generic_getxattr,
991 .listxattr = f2fs_listxattr,
992 .removexattr = generic_removexattr,
993};
994#endif
995
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900996const struct inode_operations f2fs_dir_inode_operations = {
997 .create = f2fs_create,
998 .lookup = f2fs_lookup,
999 .link = f2fs_link,
1000 .unlink = f2fs_unlink,
1001 .symlink = f2fs_symlink,
1002 .mkdir = f2fs_mkdir,
1003 .rmdir = f2fs_rmdir,
1004 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001005 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001006 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001007 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001008 .setattr = f2fs_setattr,
1009 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001010 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001011#ifdef CONFIG_F2FS_FS_XATTR
1012 .setxattr = generic_setxattr,
1013 .getxattr = generic_getxattr,
1014 .listxattr = f2fs_listxattr,
1015 .removexattr = generic_removexattr,
1016#endif
1017};
1018
1019const struct inode_operations f2fs_symlink_inode_operations = {
1020 .readlink = generic_readlink,
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -07001021 .follow_link = f2fs_follow_link,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001022 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001023 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001024 .setattr = f2fs_setattr,
1025#ifdef CONFIG_F2FS_FS_XATTR
1026 .setxattr = generic_setxattr,
1027 .getxattr = generic_getxattr,
1028 .listxattr = f2fs_listxattr,
1029 .removexattr = generic_removexattr,
1030#endif
1031};
1032
1033const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001034 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001035 .setattr = f2fs_setattr,
1036 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001037 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001038#ifdef CONFIG_F2FS_FS_XATTR
1039 .setxattr = generic_setxattr,
1040 .getxattr = generic_getxattr,
1041 .listxattr = f2fs_listxattr,
1042 .removexattr = generic_removexattr,
1043#endif
1044};