blob: cbf61a5f0e575a5d3ad37d02cff06a5133167af6 [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;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070056 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090057 }
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 Kim91942322016-05-20 10:13:22 -070063 set_inode_flag(inode, FI_NEW_INODE);
64
65 if (test_opt(sbi, INLINE_XATTR))
66 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yufba48a82015-12-23 17:51:35 +080067 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070068 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070069 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070070 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080071
Jaegeuk Kim3e72f722015-06-19 17:53:26 -070072 f2fs_init_extent_tree(inode, NULL);
73
Chao Yud5e8f6c2015-07-15 17:28:53 +080074 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -070075 stat_inc_inline_inode(inode);
76 stat_inc_inline_dir(inode);
77
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090078 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090079 mark_inode_dirty(inode);
80 return inode;
81
Jaegeuk Kim57397d82012-11-02 17:11:10 +090082fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090083 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090084 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090085 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -070086 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -070087 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090088 return ERR_PTR(err);
89}
90
91static int is_multimedia_file(const unsigned char *s, const char *sub)
92{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020093 size_t slen = strlen(s);
94 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090095
Chao Yu741a7be2015-07-06 20:30:40 +080096 /*
97 * filename format of multimedia file should be defined as:
98 * "filename + '.' + extension".
99 */
100 if (slen < sublen + 2)
101 return 0;
102
103 if (s[slen - sublen - 1] != '.')
Namjae Jeone66509f02013-04-20 01:27:21 +0900104 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900105
Gu Zheng749ebfd2013-08-22 18:11:25 +0800106 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900107}
108
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900109/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900110 * Set multimedia files as cold files for hot/cold data separation
111 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900112static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900113 const unsigned char *name)
114{
115 int i;
116 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
117
118 int count = le32_to_cpu(sbi->raw_super->extension_count);
119 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900120 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900121 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900122 break;
123 }
124 }
125}
126
127static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
128 bool excl)
129{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700130 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900131 struct inode *inode;
132 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800133 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900134
135 inode = f2fs_new_inode(dir, mode);
136 if (IS_ERR(inode))
137 return PTR_ERR(inode);
138
139 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900140 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900141
142 inode->i_op = &f2fs_file_inode_operations;
143 inode->i_fop = &f2fs_file_operations;
144 inode->i_mapping->a_ops = &f2fs_dblock_aops;
145 ino = inode->i_ino;
146
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800147 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800148
Gu Zhenge4795562013-09-27 18:08:30 +0800149 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900150 err = f2fs_add_link(dentry, inode);
151 if (err)
152 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700153 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900154
155 alloc_nid_done(sbi, ino);
156
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900157 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800159
160 if (IS_DIRSYNC(dir))
161 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900162 return 0;
163out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700164 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165 return err;
166}
167
168static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
169 struct dentry *dentry)
170{
David Howells2b0143b2015-03-17 22:25:59 +0000171 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700172 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800173 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900174
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700175 if (f2fs_encrypted_inode(dir) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700176 !fscrypt_has_permitted_context(dir, inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700177 return -EPERM;
178
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800179 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900180
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900181 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900182 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900183
Jaegeuk Kim91942322016-05-20 10:13:22 -0700184 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800185 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186 err = f2fs_add_link(dentry, inode);
187 if (err)
188 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700189 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900190
191 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800192
193 if (IS_DIRSYNC(dir))
194 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900195 return 0;
196out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700197 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900198 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700199 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900200 return err;
201}
202
203struct dentry *f2fs_get_parent(struct dentry *child)
204{
205 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +0000206 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900207 if (!ino)
208 return ERR_PTR(-ENOENT);
Al Virofc640052016-04-10 01:33:30 -0400209 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900210}
211
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700212static int __recover_dot_dentries(struct inode *dir, nid_t pino)
213{
214 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
215 struct qstr dot = QSTR_INIT(".", 1);
216 struct qstr dotdot = QSTR_INIT("..", 2);
217 struct f2fs_dir_entry *de;
218 struct page *page;
219 int err = 0;
220
Chao Yu4e0d8362015-12-30 17:40:31 +0800221 if (f2fs_readonly(sbi->sb)) {
222 f2fs_msg(sbi->sb, KERN_INFO,
223 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
224 "in readonly mountpoint", dir->i_ino, pino);
225 return 0;
226 }
227
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800228 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800229
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700230 f2fs_lock_op(sbi);
231
232 de = f2fs_find_entry(dir, &dot, &page);
233 if (de) {
234 f2fs_dentry_kunmap(dir, page);
235 f2fs_put_page(page, 0);
236 } else {
237 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
238 if (err)
239 goto out;
240 }
241
242 de = f2fs_find_entry(dir, &dotdot, &page);
243 if (de) {
244 f2fs_dentry_kunmap(dir, page);
245 f2fs_put_page(page, 0);
246 } else {
247 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
248 }
249out:
250 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700251 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700252 mark_inode_dirty(dir);
253 }
254
255 f2fs_unlock_op(sbi);
256 return err;
257}
258
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900259static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
260 unsigned int flags)
261{
262 struct inode *inode = NULL;
263 struct f2fs_dir_entry *de;
264 struct page *page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700265 nid_t ino;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700266 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000267 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900268
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800269 if (f2fs_encrypted_inode(dir)) {
270 int res = fscrypt_get_encryption_info(dir);
271
272 /*
273 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
274 * created while the directory was encrypted and we
275 * don't have access to the key.
276 */
277 if (fscrypt_has_encryption_key(dir))
278 fscrypt_set_encrypted_dentry(dentry);
279 fscrypt_set_d_op(dentry);
280 if (res && res != -ENOKEY)
281 return ERR_PTR(res);
282 }
283
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900284 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900285 return ERR_PTR(-ENAMETOOLONG);
286
287 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700288 if (!de)
289 return d_splice_alias(inode, dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900290
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700291 ino = le32_to_cpu(de->ino);
292 f2fs_dentry_kunmap(dir, page);
293 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700294
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700295 inode = f2fs_iget(dir->i_sb, ino);
296 if (IS_ERR(inode))
297 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700298
Liu Xue8c2b1432016-02-26 06:39:23 +0000299 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
300 err = __recover_dot_dentries(dir, root_ino);
301 if (err)
302 goto err_out;
303 }
304
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700305 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700306 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700307 if (err)
308 goto err_out;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900309 }
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800310 if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
311 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
312 !fscrypt_has_permitted_context(dir, inode)) {
313 bool nokey = f2fs_encrypted_inode(inode) &&
314 !fscrypt_has_encryption_key(inode);
Chao Yud7267322016-03-10 22:24:23 +0800315 err = nokey ? -ENOKEY : -EPERM;
316 goto err_out;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800317 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900318 return d_splice_alias(inode, dentry);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700319
320err_out:
Chao Yud7267322016-03-10 22:24:23 +0800321 iput(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700322 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900323}
324
325static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
326{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700327 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000328 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900329 struct f2fs_dir_entry *de;
330 struct page *page;
331 int err = -ENOENT;
332
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900333 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900334
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900335 de = f2fs_find_entry(dir, &dentry->d_name, &page);
336 if (!de)
337 goto fail;
338
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800339 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800340
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900341 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900342 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900343 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900344 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800345 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346 f2fs_put_page(page, 0);
347 goto fail;
348 }
Chao Yudbeacf02014-09-24 18:17:04 +0800349 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800350 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351
arter97e1c42042014-08-06 23:22:50 +0900352 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900353 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800354
355 if (IS_DIRSYNC(dir))
356 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900357fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900358 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900359 return err;
360}
361
Al Viro6b255392015-11-17 10:20:54 -0500362static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500363 struct inode *inode,
364 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700365{
Al Virofceef392015-12-29 15:58:39 -0500366 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400367 if (!IS_ERR(link) && !*link) {
368 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500369 do_delayed_call(done);
370 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400371 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700372 }
Al Viro680baac2015-05-02 13:32:22 -0400373 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700374}
375
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900376static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
377 const char *symname)
378{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700379 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900380 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700381 size_t len = strlen(symname);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700382 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
383 struct fscrypt_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800384 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900385
Chao Yu922ec352016-02-15 17:54:26 +0800386 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700387 err = fscrypt_get_encryption_info(dir);
Chao Yu922ec352016-02-15 17:54:26 +0800388 if (err)
389 return err;
390
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700391 if (!fscrypt_has_encryption_key(dir))
Chao Yu922ec352016-02-15 17:54:26 +0800392 return -EPERM;
393
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700394 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
395 sizeof(struct fscrypt_symlink_data));
Chao Yu922ec352016-02-15 17:54:26 +0800396 }
397
398 if (disk_link.len > dir->i_sb->s_blocksize)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700399 return -ENAMETOOLONG;
400
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900401 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
402 if (IS_ERR(inode))
403 return PTR_ERR(inode);
404
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700405 if (f2fs_encrypted_inode(inode))
406 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
407 else
408 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500409 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900410 inode->i_mapping->a_ops = &f2fs_dblock_aops;
411
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800412 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800413
Gu Zhenge4795562013-09-27 18:08:30 +0800414 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900415 err = f2fs_add_link(dentry, inode);
416 if (err)
417 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700418 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900419 alloc_nid_done(sbi, inode->i_ino);
420
Chao Yu922ec352016-02-15 17:54:26 +0800421 if (f2fs_encrypted_inode(inode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700422 struct qstr istr = QSTR_INIT(symname, len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700423 struct fscrypt_str ostr;
Chao Yu922ec352016-02-15 17:54:26 +0800424
425 sd = kzalloc(disk_link.len, GFP_NOFS);
426 if (!sd) {
427 err = -ENOMEM;
428 goto err_out;
429 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700430
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700431 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700432 if (err)
433 goto err_out;
434
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700435 if (!fscrypt_has_encryption_key(inode)) {
Chao Yu922ec352016-02-15 17:54:26 +0800436 err = -EPERM;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700437 goto err_out;
Chao Yu922ec352016-02-15 17:54:26 +0800438 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700439
Chao Yu922ec352016-02-15 17:54:26 +0800440 ostr.name = sd->encrypted_path;
441 ostr.len = disk_link.len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700442 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700443 if (err < 0)
444 goto err_out;
445
Chao Yu922ec352016-02-15 17:54:26 +0800446 sd->len = cpu_to_le16(ostr.len);
447 disk_link.name = (char *)sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700448 }
449
Chao Yu922ec352016-02-15 17:54:26 +0800450 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700451
452err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900453 d_instantiate(dentry, inode);
454 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800455
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700456 /*
457 * Let's flush symlink data in order to avoid broken symlink as much as
458 * possible. Nevertheless, fsyncing is the best way, but there is no
459 * way to get a file descriptor in order to flush that.
460 *
461 * Note that, it needs to do dir->fsync to make this recoverable.
462 * If the symlink path is stored into inline_data, there is no
463 * performance regression.
464 */
Chao Yua6be0142015-10-22 18:23:08 +0800465 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800466 filemap_write_and_wait_range(inode->i_mapping, 0,
467 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700468
Chao Yua6be0142015-10-22 18:23:08 +0800469 if (IS_DIRSYNC(dir))
470 f2fs_sync_fs(sbi->sb, 1);
471 } else {
472 f2fs_unlink(dir, dentry);
473 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700474
475 kfree(sd);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900476 return err;
477out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700478 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900479 return err;
480}
481
482static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
483{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700484 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900485 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800486 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900487
488 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900489 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900490 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900491
492 inode->i_op = &f2fs_dir_inode_operations;
493 inode->i_fop = &f2fs_dir_operations;
494 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800495 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900496
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800497 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800498
Jaegeuk Kim91942322016-05-20 10:13:22 -0700499 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800500 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900501 err = f2fs_add_link(dentry, inode);
502 if (err)
503 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700504 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900505
506 alloc_nid_done(sbi, inode->i_ino);
507
508 d_instantiate(dentry, inode);
509 unlock_new_inode(inode);
510
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800511 if (IS_DIRSYNC(dir))
512 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900513 return 0;
514
515out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700516 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700517 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900518 return err;
519}
520
521static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
522{
David Howells2b0143b2015-03-17 22:25:59 +0000523 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900524 if (f2fs_empty_dir(inode))
525 return f2fs_unlink(dir, dentry);
526 return -ENOTEMPTY;
527}
528
529static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
530 umode_t mode, dev_t rdev)
531{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700532 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900533 struct inode *inode;
534 int err = 0;
535
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900536 inode = f2fs_new_inode(dir, mode);
537 if (IS_ERR(inode))
538 return PTR_ERR(inode);
539
540 init_special_inode(inode, inode->i_mode, rdev);
541 inode->i_op = &f2fs_special_inode_operations;
542
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800543 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800544
Gu Zhenge4795562013-09-27 18:08:30 +0800545 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900546 err = f2fs_add_link(dentry, inode);
547 if (err)
548 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700549 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900550
551 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800552
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900553 d_instantiate(dentry, inode);
554 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800555
556 if (IS_DIRSYNC(dir))
557 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900558 return 0;
559out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700560 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900561 return err;
562}
563
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800564static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
565 umode_t mode, struct inode **whiteout)
566{
567 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
568 struct inode *inode;
569 int err;
570
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800571 inode = f2fs_new_inode(dir, mode);
572 if (IS_ERR(inode))
573 return PTR_ERR(inode);
574
575 if (whiteout) {
576 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
577 inode->i_op = &f2fs_special_inode_operations;
578 } else {
579 inode->i_op = &f2fs_file_inode_operations;
580 inode->i_fop = &f2fs_file_operations;
581 inode->i_mapping->a_ops = &f2fs_dblock_aops;
582 }
583
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800584 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800585
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800586 f2fs_lock_op(sbi);
587 err = acquire_orphan_inode(sbi);
588 if (err)
589 goto out;
590
591 err = f2fs_do_tmpfile(inode, dir);
592 if (err)
593 goto release_out;
594
595 /*
596 * add this non-linked tmpfile to orphan list, in this way we could
597 * remove all unused data of tmpfile after abnormal power-off.
598 */
599 add_orphan_inode(sbi, inode->i_ino);
600 f2fs_unlock_op(sbi);
601
602 alloc_nid_done(sbi, inode->i_ino);
603
604 if (whiteout) {
605 inode_dec_link_count(inode);
606 *whiteout = inode;
607 } else {
608 d_tmpfile(dentry, inode);
609 }
610 unlock_new_inode(inode);
611 return 0;
612
613release_out:
614 release_orphan_inode(sbi);
615out:
616 handle_failed_inode(inode);
617 return err;
618}
619
620static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
621{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700622 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700623 int err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700624 if (err)
625 return err;
626 }
627
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800628 return __f2fs_tmpfile(dir, dentry, mode, NULL);
629}
630
631static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
632{
633 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
634}
635
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900636static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800637 struct inode *new_dir, struct dentry *new_dentry,
638 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900639{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700640 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000641 struct inode *old_inode = d_inode(old_dentry);
642 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800643 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900644 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800645 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900646 struct f2fs_dir_entry *old_dir_entry = NULL;
647 struct f2fs_dir_entry *old_entry;
648 struct f2fs_dir_entry *new_entry;
Chao Yu993a0492016-02-17 16:47:05 +0800649 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800650 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900651
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700652 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700653 !fscrypt_has_permitted_context(new_dir, old_inode)) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700654 err = -EPERM;
655 goto out;
656 }
657
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900658 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
659 if (!old_entry)
660 goto out;
661
662 if (S_ISDIR(old_inode->i_mode)) {
663 err = -EIO;
664 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
665 if (!old_dir_entry)
666 goto out_old;
667 }
668
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800669 if (flags & RENAME_WHITEOUT) {
670 err = f2fs_create_whiteout(old_dir, &whiteout);
671 if (err)
672 goto out_dir;
673 }
674
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900675 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900676
677 err = -ENOTEMPTY;
678 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800679 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900680
681 err = -ENOENT;
682 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
683 &new_page);
684 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800685 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900686
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800687 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800688
Chao Yu12560102014-06-24 14:16:24 +0800689 f2fs_lock_op(sbi);
690
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900691 err = acquire_orphan_inode(sbi);
692 if (err)
693 goto put_out_dir;
694
Chao Yu9def1e92016-02-17 16:45:44 +0800695 err = update_dent_inode(old_inode, new_inode,
696 &new_dentry->d_name);
697 if (err) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900698 release_orphan_inode(sbi);
699 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900700 }
701
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900702 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
703
704 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900705 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900706 if (old_dir_entry)
707 drop_nlink(new_inode);
708 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900709 up_write(&F2FS_I(new_inode)->i_sem);
710
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900711 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900712
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900713 if (!new_inode->i_nlink)
714 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900715 else
716 release_orphan_inode(sbi);
717
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900718 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900719 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900720 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800721 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800722
Chao Yu12560102014-06-24 14:16:24 +0800723 f2fs_lock_op(sbi);
724
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900725 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800726 if (err) {
727 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800728 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800729 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900730
731 if (old_dir_entry) {
732 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900733 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900734 }
Chao Yu993a0492016-02-17 16:47:05 +0800735
736 /*
737 * old entry and new entry can locate in the same inline
738 * dentry in inode, when attaching new entry in inline dentry,
739 * it could force inline dentry conversion, after that,
740 * old_entry and old_page will point to wrong address, in
741 * order to avoid this, let's do the check and update here.
742 */
743 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
744 f2fs_put_page(old_page, 0);
745 old_page = NULL;
746
747 old_entry = f2fs_find_entry(old_dir,
748 &old_dentry->d_name, &old_page);
749 if (!old_entry) {
750 err = -EIO;
751 f2fs_unlock_op(sbi);
752 goto out_whiteout;
753 }
754 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900755 }
756
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900757 down_write(&F2FS_I(old_inode)->i_sem);
758 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700759 if (new_inode && file_enc_name(new_inode))
760 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900761 up_write(&F2FS_I(old_inode)->i_sem);
762
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900763 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900764 mark_inode_dirty(old_inode);
765
Chao Yudbeacf02014-09-24 18:17:04 +0800766 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900767
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800768 if (whiteout) {
769 whiteout->i_state |= I_LINKABLE;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700770 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800771 err = f2fs_add_link(old_dentry, whiteout);
772 if (err)
773 goto put_out_dir;
774 whiteout->i_state &= ~I_LINKABLE;
775 iput(whiteout);
776 }
777
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900778 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800779 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900780 f2fs_set_link(old_inode, old_dir_entry,
781 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900782 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900783 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800784 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900785 f2fs_put_page(old_dir_page, 0);
786 }
787 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900788 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900789 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900790 }
791
Gu Zhenge4795562013-09-27 18:08:30 +0800792 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800793
794 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
795 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900796 return 0;
797
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900798put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800799 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800800 if (new_page) {
801 f2fs_dentry_kunmap(new_dir, new_page);
802 f2fs_put_page(new_page, 0);
803 }
804out_whiteout:
805 if (whiteout)
806 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900807out_dir:
808 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800809 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900810 f2fs_put_page(old_dir_page, 0);
811 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900812out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800813 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900814 f2fs_put_page(old_page, 0);
815out:
816 return err;
817}
818
Chao Yu32f9bc22014-07-12 19:13:54 +0800819static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
820 struct inode *new_dir, struct dentry *new_dentry)
821{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700822 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000823 struct inode *old_inode = d_inode(old_dentry);
824 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800825 struct page *old_dir_page, *new_dir_page;
826 struct page *old_page, *new_page;
827 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
828 struct f2fs_dir_entry *old_entry, *new_entry;
829 int old_nlink = 0, new_nlink = 0;
830 int err = -ENOENT;
831
Chao Yud3baf7c2015-05-25 18:07:02 +0800832 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700833 (old_dir != new_dir) &&
834 (!fscrypt_has_permitted_context(new_dir, old_inode) ||
835 !fscrypt_has_permitted_context(old_dir, new_inode)))
Chao Yud3baf7c2015-05-25 18:07:02 +0800836 return -EPERM;
837
Chao Yu32f9bc22014-07-12 19:13:54 +0800838 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
839 if (!old_entry)
840 goto out;
841
842 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
843 if (!new_entry)
844 goto out_old;
845
846 /* prepare for updating ".." directory entry info later */
847 if (old_dir != new_dir) {
848 if (S_ISDIR(old_inode->i_mode)) {
849 err = -EIO;
850 old_dir_entry = f2fs_parent_dir(old_inode,
851 &old_dir_page);
852 if (!old_dir_entry)
853 goto out_new;
854 }
855
856 if (S_ISDIR(new_inode->i_mode)) {
857 err = -EIO;
858 new_dir_entry = f2fs_parent_dir(new_inode,
859 &new_dir_page);
860 if (!new_dir_entry)
861 goto out_old_dir;
862 }
863 }
864
865 /*
866 * If cross rename between file and directory those are not
867 * in the same directory, we will inc nlink of file's parent
868 * later, so we should check upper boundary of its nlink.
869 */
870 if ((!old_dir_entry || !new_dir_entry) &&
871 old_dir_entry != new_dir_entry) {
872 old_nlink = old_dir_entry ? -1 : 1;
873 new_nlink = -old_nlink;
874 err = -EMLINK;
875 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
876 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
877 goto out_new_dir;
878 }
879
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800880 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800881
Chao Yu32f9bc22014-07-12 19:13:54 +0800882 f2fs_lock_op(sbi);
883
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700884 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800885 if (err)
886 goto out_unlock;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700887 if (file_enc_name(new_inode))
888 file_set_enc_name(old_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800889
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700890 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800891 if (err)
892 goto out_undo;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700893 if (file_enc_name(old_inode))
894 file_set_enc_name(new_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800895
896 /* update ".." directory entry info of old dentry */
897 if (old_dir_entry)
898 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
899
900 /* update ".." directory entry info of new dentry */
901 if (new_dir_entry)
902 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
903
904 /* update directory entry info of old dir inode */
905 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
906
907 down_write(&F2FS_I(old_inode)->i_sem);
908 file_lost_pino(old_inode);
909 up_write(&F2FS_I(old_inode)->i_sem);
910
911 update_inode_page(old_inode);
912
913 old_dir->i_ctime = CURRENT_TIME;
914 if (old_nlink) {
915 down_write(&F2FS_I(old_dir)->i_sem);
916 if (old_nlink < 0)
917 drop_nlink(old_dir);
918 else
919 inc_nlink(old_dir);
920 up_write(&F2FS_I(old_dir)->i_sem);
921 }
922 mark_inode_dirty(old_dir);
923 update_inode_page(old_dir);
924
925 /* update directory entry info of new dir inode */
926 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
927
928 down_write(&F2FS_I(new_inode)->i_sem);
929 file_lost_pino(new_inode);
930 up_write(&F2FS_I(new_inode)->i_sem);
931
932 update_inode_page(new_inode);
933
934 new_dir->i_ctime = CURRENT_TIME;
935 if (new_nlink) {
936 down_write(&F2FS_I(new_dir)->i_sem);
937 if (new_nlink < 0)
938 drop_nlink(new_dir);
939 else
940 inc_nlink(new_dir);
941 up_write(&F2FS_I(new_dir)->i_sem);
942 }
943 mark_inode_dirty(new_dir);
944 update_inode_page(new_dir);
945
946 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800947
948 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
949 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800950 return 0;
951out_undo:
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700952 /*
953 * Still we may fail to recover name info of f2fs_inode here
954 * Drop it, once its name is set as encrypted
955 */
956 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800957out_unlock:
958 f2fs_unlock_op(sbi);
959out_new_dir:
960 if (new_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800961 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800962 f2fs_put_page(new_dir_page, 0);
963 }
964out_old_dir:
965 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800966 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800967 f2fs_put_page(old_dir_page, 0);
968 }
969out_new:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800970 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800971 f2fs_put_page(new_page, 0);
972out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800973 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800974 f2fs_put_page(old_page, 0);
975out:
976 return err;
977}
978
979static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
980 struct inode *new_dir, struct dentry *new_dentry,
981 unsigned int flags)
982{
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800983 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +0800984 return -EINVAL;
985
986 if (flags & RENAME_EXCHANGE) {
987 return f2fs_cross_rename(old_dir, old_dentry,
988 new_dir, new_dentry);
989 }
990 /*
991 * VFS has already handled the new dentry existence case,
992 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
993 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800994 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +0800995}
996
Al Viro6b255392015-11-17 10:20:54 -0500997static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500998 struct inode *inode,
999 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001000{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001001 struct page *cpage = NULL;
1002 char *caddr, *paddr = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001003 struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
1004 struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
1005 struct fscrypt_symlink_data *sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001006 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
1007 u32 max_size = inode->i_sb->s_blocksize;
1008 int res;
Chao Yu50732df2014-06-19 16:23:19 +08001009
Al Viro6b255392015-11-17 10:20:54 -05001010 if (!dentry)
1011 return ERR_PTR(-ECHILD);
1012
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001013 res = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001014 if (res)
1015 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001016
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001017 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1018 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -07001019 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -05001020 caddr = page_address(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001021 caddr[size] = 0;
Chao Yu50732df2014-06-19 16:23:19 +08001022
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001023 /* Symlink is encrypted */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001024 sd = (struct fscrypt_symlink_data *)caddr;
Chao Yu922ec352016-02-15 17:54:26 +08001025 cstr.name = sd->encrypted_path;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001026 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -08001027
1028 /* this is broken symlink case */
1029 if (unlikely(cstr.len == 0)) {
1030 res = -ENOENT;
1031 goto errout;
1032 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001033
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001034 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001035 /* Symlink data on the disk is corrupted */
1036 res = -EIO;
1037 goto errout;
1038 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001039 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001040 if (res)
1041 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +08001042
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001043 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001044 if (res < 0)
1045 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +08001046
Jaegeuk Kimc90e09f2016-03-30 13:13:16 -07001047 /* this is broken symlink case */
1048 if (unlikely(pstr.name[0] == 0)) {
1049 res = -ENOENT;
1050 goto errout;
1051 }
1052
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001053 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001054
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001055 /* Null-terminate the name */
1056 paddr[res] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001057
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001058 put_page(cpage);
Al Virofceef392015-12-29 15:58:39 -05001059 set_delayed_call(done, kfree_link, paddr);
1060 return paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001061errout:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001062 fscrypt_fname_free_buffer(&pstr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001063 put_page(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001064 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001065}
1066
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001067const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1068 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001069 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001070 .getattr = f2fs_getattr,
1071 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001072#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001073 .setxattr = generic_setxattr,
1074 .getxattr = generic_getxattr,
1075 .listxattr = f2fs_listxattr,
1076 .removexattr = generic_removexattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001077#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001078};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001079
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001080const struct inode_operations f2fs_dir_inode_operations = {
1081 .create = f2fs_create,
1082 .lookup = f2fs_lookup,
1083 .link = f2fs_link,
1084 .unlink = f2fs_unlink,
1085 .symlink = f2fs_symlink,
1086 .mkdir = f2fs_mkdir,
1087 .rmdir = f2fs_rmdir,
1088 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001089 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001090 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001091 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001092 .setattr = f2fs_setattr,
1093 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001094 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001095#ifdef CONFIG_F2FS_FS_XATTR
1096 .setxattr = generic_setxattr,
1097 .getxattr = generic_getxattr,
1098 .listxattr = f2fs_listxattr,
1099 .removexattr = generic_removexattr,
1100#endif
1101};
1102
1103const struct inode_operations f2fs_symlink_inode_operations = {
1104 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001105 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001106 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001107 .setattr = f2fs_setattr,
1108#ifdef CONFIG_F2FS_FS_XATTR
1109 .setxattr = generic_setxattr,
1110 .getxattr = generic_getxattr,
1111 .listxattr = f2fs_listxattr,
1112 .removexattr = generic_removexattr,
1113#endif
1114};
1115
1116const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001117 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001118 .setattr = f2fs_setattr,
1119 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001120 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001121#ifdef CONFIG_F2FS_FS_XATTR
1122 .setxattr = generic_setxattr,
1123 .getxattr = generic_getxattr,
1124 .listxattr = f2fs_listxattr,
1125 .removexattr = generic_removexattr,
1126#endif
1127};