blob: e910d2ebe0c67484f1b8270452dcdea00dd61de6 [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>
Chao Yu0abd6752017-07-09 00:13:07 +080018#include <linux/quotaops.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090019
20#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090021#include "node.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090022#include "xattr.h"
23#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090025
26static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
27{
Jaegeuk Kim40813632014-09-02 15:31:18 -070028 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090029 nid_t ino;
30 struct inode *inode;
31 bool nid_free = false;
Chao Yu6afc6622017-09-06 21:59:50 +080032 int xattr_size = 0;
Gu Zhenge4795562013-09-27 18:08:30 +080033 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090034
Jaegeuk Kima014e032014-06-20 21:44:02 -070035 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090036 if (!inode)
37 return ERR_PTR(-ENOMEM);
38
Gu Zhenge4795562013-09-27 18:08:30 +080039 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090040 if (!alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080041 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090042 err = -ENOSPC;
43 goto fail;
44 }
Gu Zhenge4795562013-09-27 18:08:30 +080045 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090046
Chao Yu0abd6752017-07-09 00:13:07 +080047 nid_free = true;
48
Chao Yu70ff5df2014-05-08 17:09:30 +080049 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090050
51 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090052 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -070053 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090054 inode->i_generation = sbi->s_next_generation++;
55
56 err = insert_inode_locked(inode);
57 if (err) {
58 err = -EINVAL;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070059 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090060 }
Chao Yu622f28a2014-09-24 18:19:10 +080061
Chao Yu5c571322017-07-26 00:01:41 +080062 if (f2fs_sb_has_project_quota(sbi->sb) &&
63 (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
64 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
65 else
66 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
67 F2FS_DEF_PROJID);
68
Chao Yu0abd6752017-07-09 00:13:07 +080069 err = dquot_initialize(inode);
70 if (err)
71 goto fail_drop;
72
73 err = dquot_alloc_inode(inode);
74 if (err)
75 goto fail_drop;
76
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070077 /* If the directory encrypted, then we should encrypt the inode. */
78 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
79 f2fs_set_encrypted_inode(inode);
80
Jaegeuk Kim91942322016-05-20 10:13:22 -070081 set_inode_flag(inode, FI_NEW_INODE);
82
Chao Yu7a2af762017-07-19 00:19:06 +080083 if (f2fs_sb_has_extra_attr(sbi->sb)) {
84 set_inode_flag(inode, FI_EXTRA_ATTR);
85 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
86 }
87
Jaegeuk Kim91942322016-05-20 10:13:22 -070088 if (test_opt(sbi, INLINE_XATTR))
89 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yu6afc6622017-09-06 21:59:50 +080090
Chao Yufba48a82015-12-23 17:51:35 +080091 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070092 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070093 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070094 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080095
Chao Yu6afc6622017-09-06 21:59:50 +080096 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
97 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
98 if (f2fs_has_inline_xattr(inode))
99 xattr_size = sbi->inline_xattr_size;
100 /* Otherwise, will be 0 */
101 } else if (f2fs_has_inline_xattr(inode) ||
102 f2fs_has_inline_dentry(inode)) {
103 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
104 }
105 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
106
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700107 f2fs_init_extent_tree(inode, NULL);
108
Chao Yud5e8f6c2015-07-15 17:28:53 +0800109 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -0700110 stat_inc_inline_inode(inode);
111 stat_inc_inline_dir(inode);
112
Chao Yu5c571322017-07-26 00:01:41 +0800113 F2FS_I(inode)->i_flags =
114 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
115
Chao Yu11f50202017-08-30 18:04:47 +0800116 if (S_ISDIR(inode->i_mode))
117 F2FS_I(inode)->i_flags |= FS_INDEX_FL;
118
Chao Yu5c571322017-07-26 00:01:41 +0800119 if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
120 set_inode_flag(inode, FI_PROJ_INHERIT);
121
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900122 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900123 return inode;
124
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900125fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900126 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900127 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900128 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700129 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700130 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900131 return ERR_PTR(err);
Chao Yu0abd6752017-07-09 00:13:07 +0800132fail_drop:
133 trace_f2fs_new_inode(inode, err);
134 dquot_drop(inode);
135 inode->i_flags |= S_NOQUOTA;
136 if (nid_free)
137 set_inode_flag(inode, FI_FREE_NID);
138 clear_nlink(inode);
139 unlock_new_inode(inode);
140 iput(inode);
141 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900142}
143
144static int is_multimedia_file(const unsigned char *s, const char *sub)
145{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200146 size_t slen = strlen(s);
147 size_t sublen = strlen(sub);
Chao Yu7732c262016-09-05 12:28:27 +0800148 int i;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900149
Chao Yu741a7be2015-07-06 20:30:40 +0800150 /*
151 * filename format of multimedia file should be defined as:
Chao Yu7732c262016-09-05 12:28:27 +0800152 * "filename + '.' + extension + (optional: '.' + temp extension)".
Chao Yu741a7be2015-07-06 20:30:40 +0800153 */
154 if (slen < sublen + 2)
155 return 0;
156
Chao Yu7732c262016-09-05 12:28:27 +0800157 for (i = 1; i < slen - sublen; i++) {
158 if (s[i] != '.')
159 continue;
160 if (!strncasecmp(s + i + 1, sub, sublen))
161 return 1;
162 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900163
Chao Yu7732c262016-09-05 12:28:27 +0800164 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165}
166
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900167/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900168 * Set multimedia files as cold files for hot/cold data separation
169 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900170static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900171 const unsigned char *name)
172{
173 int i;
174 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
175
176 int count = le32_to_cpu(sbi->raw_super->extension_count);
177 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900178 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900179 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900180 break;
181 }
182 }
183}
184
185static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
186 bool excl)
187{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700188 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900189 struct inode *inode;
190 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800191 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900192
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200193 if (unlikely(f2fs_cp_error(sbi)))
194 return -EIO;
195
Chao Yu0abd6752017-07-09 00:13:07 +0800196 err = dquot_initialize(dir);
197 if (err)
198 return err;
199
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900200 inode = f2fs_new_inode(dir, mode);
201 if (IS_ERR(inode))
202 return PTR_ERR(inode);
203
204 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900205 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900206
207 inode->i_op = &f2fs_file_inode_operations;
208 inode->i_fop = &f2fs_file_operations;
209 inode->i_mapping->a_ops = &f2fs_dblock_aops;
210 ino = inode->i_ino;
211
Gu Zhenge4795562013-09-27 18:08:30 +0800212 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900213 err = f2fs_add_link(dentry, inode);
214 if (err)
215 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700216 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900217
218 alloc_nid_done(sbi, ino);
219
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900220 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900221 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800222
223 if (IS_DIRSYNC(dir))
224 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700225
226 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900227 return 0;
228out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700229 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900230 return err;
231}
232
233static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
234 struct dentry *dentry)
235{
David Howells2b0143b2015-03-17 22:25:59 +0000236 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700237 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800238 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900239
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200240 if (unlikely(f2fs_cp_error(sbi)))
241 return -EIO;
242
Eric Biggersb05157e2017-11-29 12:35:29 -0800243 err = fscrypt_prepare_link(old_dentry, dir, dentry);
244 if (err)
245 return err;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700246
Chao Yu5c571322017-07-26 00:01:41 +0800247 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
248 (!projid_eq(F2FS_I(dir)->i_projid,
249 F2FS_I(old_dentry->d_inode)->i_projid)))
250 return -EXDEV;
251
Chao Yu0abd6752017-07-09 00:13:07 +0800252 err = dquot_initialize(dir);
253 if (err)
254 return err;
255
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800256 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900257
Deepa Dinamani078cd822016-09-14 07:48:04 -0700258 inode->i_ctime = current_time(inode);
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900259 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900260
Jaegeuk Kim91942322016-05-20 10:13:22 -0700261 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800262 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900263 err = f2fs_add_link(dentry, inode);
264 if (err)
265 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700266 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900267
268 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800269
270 if (IS_DIRSYNC(dir))
271 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900272 return 0;
273out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700274 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900275 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700276 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900277 return err;
278}
279
280struct dentry *f2fs_get_parent(struct dentry *child)
281{
282 struct qstr dotdot = QSTR_INIT("..", 2);
Chao Yu91246c22016-07-19 08:27:47 +0800283 struct page *page;
284 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
285 if (!ino) {
286 if (IS_ERR(page))
287 return ERR_CAST(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900288 return ERR_PTR(-ENOENT);
Chao Yu91246c22016-07-19 08:27:47 +0800289 }
Al Virofc640052016-04-10 01:33:30 -0400290 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900291}
292
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700293static int __recover_dot_dentries(struct inode *dir, nid_t pino)
294{
295 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
296 struct qstr dot = QSTR_INIT(".", 1);
297 struct qstr dotdot = QSTR_INIT("..", 2);
298 struct f2fs_dir_entry *de;
299 struct page *page;
300 int err = 0;
301
Chao Yu4e0d8362015-12-30 17:40:31 +0800302 if (f2fs_readonly(sbi->sb)) {
303 f2fs_msg(sbi->sb, KERN_INFO,
304 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
305 "in readonly mountpoint", dir->i_ino, pino);
306 return 0;
307 }
308
Chao Yua6d3a472017-07-24 17:12:06 +0800309 err = dquot_initialize(dir);
310 if (err)
311 return err;
312
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800313 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800314
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700315 f2fs_lock_op(sbi);
316
317 de = f2fs_find_entry(dir, &dot, &page);
318 if (de) {
319 f2fs_dentry_kunmap(dir, page);
320 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700321 } else if (IS_ERR(page)) {
322 err = PTR_ERR(page);
323 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700324 } else {
325 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
326 if (err)
327 goto out;
328 }
329
330 de = f2fs_find_entry(dir, &dotdot, &page);
331 if (de) {
332 f2fs_dentry_kunmap(dir, page);
333 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700334 } else if (IS_ERR(page)) {
335 err = PTR_ERR(page);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700336 } else {
337 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
338 }
339out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700340 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700341 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700342
343 f2fs_unlock_op(sbi);
344 return err;
345}
346
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900347static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
348 unsigned int flags)
349{
350 struct inode *inode = NULL;
351 struct f2fs_dir_entry *de;
352 struct page *page;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800353 struct dentry *new;
354 nid_t ino = -1;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700355 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000356 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900357
Chao Yu0c5e36d2017-10-17 17:33:41 +0800358 trace_f2fs_lookup_start(dir, dentry, flags);
359
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800360 if (f2fs_encrypted_inode(dir)) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800361 err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800362
363 /*
364 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
365 * created while the directory was encrypted and we
366 * don't have access to the key.
367 */
368 if (fscrypt_has_encryption_key(dir))
369 fscrypt_set_encrypted_dentry(dentry);
370 fscrypt_set_d_op(dentry);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800371 if (err && err != -ENOKEY)
372 goto out;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800373 }
374
Chao Yu0c5e36d2017-10-17 17:33:41 +0800375 if (dentry->d_name.len > F2FS_NAME_LEN) {
376 err = -ENAMETOOLONG;
377 goto out;
378 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900379
380 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700381 if (!de) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800382 if (IS_ERR(page)) {
383 err = PTR_ERR(page);
384 goto out;
385 }
386 goto out_splice;
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700387 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900388
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700389 ino = le32_to_cpu(de->ino);
390 f2fs_dentry_kunmap(dir, page);
391 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700392
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700393 inode = f2fs_iget(dir->i_sb, ino);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800394 if (IS_ERR(inode)) {
395 err = PTR_ERR(inode);
396 goto out;
397 }
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700398
Liu Xue8c2b1432016-02-26 06:39:23 +0000399 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
400 err = __recover_dot_dentries(dir, root_ino);
401 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800402 goto out_iput;
Liu Xue8c2b1432016-02-26 06:39:23 +0000403 }
404
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700405 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700406 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700407 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800408 goto out_iput;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900409 }
Dan Carpenter07fe8d42016-12-16 11:18:15 +0300410 if (f2fs_encrypted_inode(dir) &&
411 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
412 !fscrypt_has_permitted_context(dir, inode)) {
Eric Biggersfaac7fd2017-04-07 10:58:39 -0700413 f2fs_msg(inode->i_sb, KERN_WARNING,
414 "Inconsistent encryption contexts: %lu/%lu",
415 dir->i_ino, inode->i_ino);
416 err = -EPERM;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800417 goto out_iput;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800418 }
Chao Yu0c5e36d2017-10-17 17:33:41 +0800419out_splice:
420 new = d_splice_alias(inode, dentry);
421 if (IS_ERR(new))
422 err = PTR_ERR(new);
423 trace_f2fs_lookup_end(dir, dentry, ino, err);
424 return new;
425out_iput:
Chao Yud7267322016-03-10 22:24:23 +0800426 iput(inode);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800427out:
428 trace_f2fs_lookup_end(dir, dentry, ino, err);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700429 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900430}
431
432static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
433{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700434 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000435 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900436 struct f2fs_dir_entry *de;
437 struct page *page;
438 int err = -ENOENT;
439
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900440 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900441
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200442 if (unlikely(f2fs_cp_error(sbi)))
443 return -EIO;
444
Chao Yu0abd6752017-07-09 00:13:07 +0800445 err = dquot_initialize(dir);
446 if (err)
447 return err;
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200448 err = dquot_initialize(inode);
449 if (err)
450 return err;
Chao Yu0abd6752017-07-09 00:13:07 +0800451
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900452 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800453 if (!de) {
454 if (IS_ERR(page))
455 err = PTR_ERR(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900456 goto fail;
Chao Yu91246c22016-07-19 08:27:47 +0800457 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900458
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800459 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800460
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900461 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900462 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900463 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900464 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800465 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900466 f2fs_put_page(page, 0);
467 goto fail;
468 }
Chao Yudbeacf02014-09-24 18:17:04 +0800469 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800470 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900471
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800472 if (IS_DIRSYNC(dir))
473 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900474fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900475 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900476 return err;
477}
478
Al Viro6b255392015-11-17 10:20:54 -0500479static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500480 struct inode *inode,
481 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700482{
Al Virofceef392015-12-29 15:58:39 -0500483 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400484 if (!IS_ERR(link) && !*link) {
485 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500486 do_delayed_call(done);
487 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400488 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700489 }
Al Viro680baac2015-05-02 13:32:22 -0400490 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700491}
492
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900493static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
494 const char *symname)
495{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700496 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900497 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700498 size_t len = strlen(symname);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700499 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
500 struct fscrypt_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800501 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900502
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200503 if (unlikely(f2fs_cp_error(sbi)))
504 return -EIO;
505
Chao Yu922ec352016-02-15 17:54:26 +0800506 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700507 err = fscrypt_get_encryption_info(dir);
Chao Yu922ec352016-02-15 17:54:26 +0800508 if (err)
509 return err;
510
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700511 if (!fscrypt_has_encryption_key(dir))
Eric Biggers54475f52016-12-05 11:12:44 -0800512 return -ENOKEY;
Chao Yu922ec352016-02-15 17:54:26 +0800513
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700514 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
515 sizeof(struct fscrypt_symlink_data));
Chao Yu922ec352016-02-15 17:54:26 +0800516 }
517
518 if (disk_link.len > dir->i_sb->s_blocksize)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700519 return -ENAMETOOLONG;
520
Chao Yu0abd6752017-07-09 00:13:07 +0800521 err = dquot_initialize(dir);
522 if (err)
523 return err;
524
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900525 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
526 if (IS_ERR(inode))
527 return PTR_ERR(inode);
528
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700529 if (f2fs_encrypted_inode(inode))
530 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
531 else
532 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500533 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900534 inode->i_mapping->a_ops = &f2fs_dblock_aops;
535
Gu Zhenge4795562013-09-27 18:08:30 +0800536 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900537 err = f2fs_add_link(dentry, inode);
538 if (err)
539 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700540 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900541 alloc_nid_done(sbi, inode->i_ino);
542
Chao Yu922ec352016-02-15 17:54:26 +0800543 if (f2fs_encrypted_inode(inode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700544 struct qstr istr = QSTR_INIT(symname, len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700545 struct fscrypt_str ostr;
Chao Yu922ec352016-02-15 17:54:26 +0800546
547 sd = kzalloc(disk_link.len, GFP_NOFS);
548 if (!sd) {
549 err = -ENOMEM;
550 goto err_out;
551 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700552
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700553 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700554 if (err)
555 goto err_out;
556
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700557 if (!fscrypt_has_encryption_key(inode)) {
Eric Biggers54475f52016-12-05 11:12:44 -0800558 err = -ENOKEY;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700559 goto err_out;
Chao Yu922ec352016-02-15 17:54:26 +0800560 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700561
Chao Yu922ec352016-02-15 17:54:26 +0800562 ostr.name = sd->encrypted_path;
563 ostr.len = disk_link.len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700564 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400565 if (err)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700566 goto err_out;
567
Chao Yu922ec352016-02-15 17:54:26 +0800568 sd->len = cpu_to_le16(ostr.len);
569 disk_link.name = (char *)sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700570 }
571
Chao Yu922ec352016-02-15 17:54:26 +0800572 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700573
574err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900575 d_instantiate(dentry, inode);
576 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800577
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700578 /*
579 * Let's flush symlink data in order to avoid broken symlink as much as
580 * possible. Nevertheless, fsyncing is the best way, but there is no
581 * way to get a file descriptor in order to flush that.
582 *
583 * Note that, it needs to do dir->fsync to make this recoverable.
584 * If the symlink path is stored into inline_data, there is no
585 * performance regression.
586 */
Chao Yua6be0142015-10-22 18:23:08 +0800587 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800588 filemap_write_and_wait_range(inode->i_mapping, 0,
589 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700590
Chao Yua6be0142015-10-22 18:23:08 +0800591 if (IS_DIRSYNC(dir))
592 f2fs_sync_fs(sbi->sb, 1);
593 } else {
594 f2fs_unlink(dir, dentry);
595 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700596
597 kfree(sd);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700598
599 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900600 return err;
601out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700602 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900603 return err;
604}
605
606static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
607{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700608 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900609 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800610 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900611
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200612 if (unlikely(f2fs_cp_error(sbi)))
613 return -EIO;
614
Chao Yu0abd6752017-07-09 00:13:07 +0800615 err = dquot_initialize(dir);
616 if (err)
617 return err;
618
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900619 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900620 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900621 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900622
623 inode->i_op = &f2fs_dir_inode_operations;
624 inode->i_fop = &f2fs_dir_operations;
625 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800626 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900627
Jaegeuk Kim91942322016-05-20 10:13:22 -0700628 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800629 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900630 err = f2fs_add_link(dentry, inode);
631 if (err)
632 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700633 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900634
635 alloc_nid_done(sbi, inode->i_ino);
636
637 d_instantiate(dentry, inode);
638 unlock_new_inode(inode);
639
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800640 if (IS_DIRSYNC(dir))
641 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700642
643 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900644 return 0;
645
646out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700647 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700648 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900649 return err;
650}
651
652static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
653{
David Howells2b0143b2015-03-17 22:25:59 +0000654 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900655 if (f2fs_empty_dir(inode))
656 return f2fs_unlink(dir, dentry);
657 return -ENOTEMPTY;
658}
659
660static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
661 umode_t mode, dev_t rdev)
662{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700663 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900664 struct inode *inode;
665 int err = 0;
666
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200667 if (unlikely(f2fs_cp_error(sbi)))
668 return -EIO;
669
Chao Yu0abd6752017-07-09 00:13:07 +0800670 err = dquot_initialize(dir);
671 if (err)
672 return err;
673
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900674 inode = f2fs_new_inode(dir, mode);
675 if (IS_ERR(inode))
676 return PTR_ERR(inode);
677
678 init_special_inode(inode, inode->i_mode, rdev);
679 inode->i_op = &f2fs_special_inode_operations;
680
Gu Zhenge4795562013-09-27 18:08:30 +0800681 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900682 err = f2fs_add_link(dentry, inode);
683 if (err)
684 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700685 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900686
687 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800688
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900689 d_instantiate(dentry, inode);
690 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800691
692 if (IS_DIRSYNC(dir))
693 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700694
695 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900696 return 0;
697out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700698 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900699 return err;
700}
701
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800702static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
703 umode_t mode, struct inode **whiteout)
704{
705 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
706 struct inode *inode;
707 int err;
708
Chao Yu0abd6752017-07-09 00:13:07 +0800709 err = dquot_initialize(dir);
710 if (err)
711 return err;
712
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800713 inode = f2fs_new_inode(dir, mode);
714 if (IS_ERR(inode))
715 return PTR_ERR(inode);
716
717 if (whiteout) {
718 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
719 inode->i_op = &f2fs_special_inode_operations;
720 } else {
721 inode->i_op = &f2fs_file_inode_operations;
722 inode->i_fop = &f2fs_file_operations;
723 inode->i_mapping->a_ops = &f2fs_dblock_aops;
724 }
725
726 f2fs_lock_op(sbi);
727 err = acquire_orphan_inode(sbi);
728 if (err)
729 goto out;
730
731 err = f2fs_do_tmpfile(inode, dir);
732 if (err)
733 goto release_out;
734
735 /*
736 * add this non-linked tmpfile to orphan list, in this way we could
737 * remove all unused data of tmpfile after abnormal power-off.
738 */
Jaegeuk Kim67c37582016-06-13 18:27:02 -0700739 add_orphan_inode(inode);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800740 alloc_nid_done(sbi, inode->i_ino);
741
742 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700743 f2fs_i_links_write(inode, false);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800744 *whiteout = inode;
745 } else {
746 d_tmpfile(dentry, inode);
747 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700748 /* link_count was changed by d_tmpfile as well. */
749 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800750 unlock_new_inode(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700751
752 f2fs_balance_fs(sbi, true);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800753 return 0;
754
755release_out:
756 release_orphan_inode(sbi);
757out:
758 handle_failed_inode(inode);
759 return err;
760}
761
762static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
763{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200764 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
765 return -EIO;
766
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700767 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700768 int err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700769 if (err)
770 return err;
771 }
772
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800773 return __f2fs_tmpfile(dir, dentry, mode, NULL);
774}
775
776static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
777{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200778 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
779 return -EIO;
780
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800781 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
782}
783
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900784static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800785 struct inode *new_dir, struct dentry *new_dentry,
786 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900787{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700788 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000789 struct inode *old_inode = d_inode(old_dentry);
790 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800791 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900792 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800793 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900794 struct f2fs_dir_entry *old_dir_entry = NULL;
795 struct f2fs_dir_entry *old_entry;
796 struct f2fs_dir_entry *new_entry;
Chao Yu993a0492016-02-17 16:47:05 +0800797 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800798 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900799
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200800 if (unlikely(f2fs_cp_error(sbi)))
801 return -EIO;
802
Chao Yu5c571322017-07-26 00:01:41 +0800803 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
804 (!projid_eq(F2FS_I(new_dir)->i_projid,
805 F2FS_I(old_dentry->d_inode)->i_projid)))
806 return -EXDEV;
807
Chao Yu0abd6752017-07-09 00:13:07 +0800808 err = dquot_initialize(old_dir);
809 if (err)
810 goto out;
811
812 err = dquot_initialize(new_dir);
813 if (err)
814 goto out;
815
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200816 if (new_inode) {
817 err = dquot_initialize(new_inode);
818 if (err)
819 goto out;
820 }
821
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900822 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +0800823 if (!old_entry) {
824 if (IS_ERR(old_page))
825 err = PTR_ERR(old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900826 goto out;
Chao Yu91246c22016-07-19 08:27:47 +0800827 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900828
829 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900830 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700831 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800832 if (IS_ERR(old_dir_page))
833 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900834 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700835 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900836 }
837
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800838 if (flags & RENAME_WHITEOUT) {
839 err = f2fs_create_whiteout(old_dir, &whiteout);
840 if (err)
841 goto out_dir;
842 }
843
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900844 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900845
846 err = -ENOTEMPTY;
847 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800848 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900849
850 err = -ENOENT;
851 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
852 &new_page);
Chao Yu91246c22016-07-19 08:27:47 +0800853 if (!new_entry) {
854 if (IS_ERR(new_page))
855 err = PTR_ERR(new_page);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800856 goto out_whiteout;
Chao Yu91246c22016-07-19 08:27:47 +0800857 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900858
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800859 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800860
Chao Yu12560102014-06-24 14:16:24 +0800861 f2fs_lock_op(sbi);
862
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900863 err = acquire_orphan_inode(sbi);
864 if (err)
865 goto put_out_dir;
866
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900867 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
868
Deepa Dinamani078cd822016-09-14 07:48:04 -0700869 new_inode->i_ctime = current_time(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900870 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900871 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700872 f2fs_i_links_write(new_inode, false);
873 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900874 up_write(&F2FS_I(new_inode)->i_sem);
875
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900876 if (!new_inode->i_nlink)
Jaegeuk Kim67c37582016-06-13 18:27:02 -0700877 add_orphan_inode(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900878 else
879 release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900880 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800881 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800882
Chao Yu12560102014-06-24 14:16:24 +0800883 f2fs_lock_op(sbi);
884
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900885 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800886 if (err) {
887 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800888 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800889 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900890
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700891 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700892 f2fs_i_links_write(new_dir, true);
Chao Yu993a0492016-02-17 16:47:05 +0800893
894 /*
895 * old entry and new entry can locate in the same inline
896 * dentry in inode, when attaching new entry in inline dentry,
897 * it could force inline dentry conversion, after that,
898 * old_entry and old_page will point to wrong address, in
899 * order to avoid this, let's do the check and update here.
900 */
901 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
902 f2fs_put_page(old_page, 0);
903 old_page = NULL;
904
905 old_entry = f2fs_find_entry(old_dir,
906 &old_dentry->d_name, &old_page);
907 if (!old_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800908 err = -ENOENT;
909 if (IS_ERR(old_page))
910 err = PTR_ERR(old_page);
Chao Yu993a0492016-02-17 16:47:05 +0800911 f2fs_unlock_op(sbi);
912 goto out_whiteout;
913 }
914 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900915 }
916
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900917 down_write(&F2FS_I(old_inode)->i_sem);
Sheng Yongb855bf02017-06-26 10:41:36 +0800918 if (!old_dir_entry || whiteout)
919 file_lost_pino(old_inode);
920 else
921 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900922 up_write(&F2FS_I(old_inode)->i_sem);
923
Deepa Dinamani078cd822016-09-14 07:48:04 -0700924 old_inode->i_ctime = current_time(old_inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700925 f2fs_mark_inode_dirty_sync(old_inode, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900926
Chao Yudbeacf02014-09-24 18:17:04 +0800927 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900928
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800929 if (whiteout) {
930 whiteout->i_state |= I_LINKABLE;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700931 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800932 err = f2fs_add_link(old_dentry, whiteout);
933 if (err)
934 goto put_out_dir;
935 whiteout->i_state &= ~I_LINKABLE;
936 iput(whiteout);
937 }
938
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900939 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800940 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900941 f2fs_set_link(old_inode, old_dir_entry,
942 old_dir_page, new_dir);
943 } else {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800944 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900945 f2fs_put_page(old_dir_page, 0);
946 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700947 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900948 }
949
Gu Zhenge4795562013-09-27 18:08:30 +0800950 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800951
952 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
953 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900954 return 0;
955
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900956put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800957 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800958 if (new_page) {
959 f2fs_dentry_kunmap(new_dir, new_page);
960 f2fs_put_page(new_page, 0);
961 }
962out_whiteout:
963 if (whiteout)
964 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900965out_dir:
966 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800967 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900968 f2fs_put_page(old_dir_page, 0);
969 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900970out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800971 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900972 f2fs_put_page(old_page, 0);
973out:
974 return err;
975}
976
Chao Yu32f9bc22014-07-12 19:13:54 +0800977static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
978 struct inode *new_dir, struct dentry *new_dentry)
979{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700980 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000981 struct inode *old_inode = d_inode(old_dentry);
982 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800983 struct page *old_dir_page, *new_dir_page;
984 struct page *old_page, *new_page;
985 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
986 struct f2fs_dir_entry *old_entry, *new_entry;
987 int old_nlink = 0, new_nlink = 0;
988 int err = -ENOENT;
989
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200990 if (unlikely(f2fs_cp_error(sbi)))
991 return -EIO;
992
Chao Yu5c571322017-07-26 00:01:41 +0800993 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
994 !projid_eq(F2FS_I(new_dir)->i_projid,
995 F2FS_I(old_dentry->d_inode)->i_projid)) ||
996 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
997 !projid_eq(F2FS_I(old_dir)->i_projid,
998 F2FS_I(new_dentry->d_inode)->i_projid)))
999 return -EXDEV;
1000
Chao Yu0abd6752017-07-09 00:13:07 +08001001 err = dquot_initialize(old_dir);
1002 if (err)
1003 goto out;
1004
1005 err = dquot_initialize(new_dir);
1006 if (err)
1007 goto out;
1008
Chao Yu32f9bc22014-07-12 19:13:54 +08001009 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +08001010 if (!old_entry) {
1011 if (IS_ERR(old_page))
1012 err = PTR_ERR(old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001013 goto out;
Chao Yu91246c22016-07-19 08:27:47 +08001014 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001015
1016 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001017 if (!new_entry) {
1018 if (IS_ERR(new_page))
1019 err = PTR_ERR(new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001020 goto out_old;
Chao Yu91246c22016-07-19 08:27:47 +08001021 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001022
1023 /* prepare for updating ".." directory entry info later */
1024 if (old_dir != new_dir) {
1025 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001026 old_dir_entry = f2fs_parent_dir(old_inode,
1027 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001028 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001029 if (IS_ERR(old_dir_page))
1030 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001031 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001032 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001033 }
1034
1035 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001036 new_dir_entry = f2fs_parent_dir(new_inode,
1037 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001038 if (!new_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001039 if (IS_ERR(new_dir_page))
1040 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001041 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001042 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001043 }
1044 }
1045
1046 /*
1047 * If cross rename between file and directory those are not
1048 * in the same directory, we will inc nlink of file's parent
1049 * later, so we should check upper boundary of its nlink.
1050 */
1051 if ((!old_dir_entry || !new_dir_entry) &&
1052 old_dir_entry != new_dir_entry) {
1053 old_nlink = old_dir_entry ? -1 : 1;
1054 new_nlink = -old_nlink;
1055 err = -EMLINK;
Kinglong Meee2f0e962017-03-04 21:48:28 +08001056 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1057 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
Chao Yu32f9bc22014-07-12 19:13:54 +08001058 goto out_new_dir;
1059 }
1060
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001061 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001062
Chao Yu32f9bc22014-07-12 19:13:54 +08001063 f2fs_lock_op(sbi);
1064
Chao Yu32f9bc22014-07-12 19:13:54 +08001065 /* update ".." directory entry info of old dentry */
1066 if (old_dir_entry)
1067 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1068
1069 /* update ".." directory entry info of new dentry */
1070 if (new_dir_entry)
1071 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1072
1073 /* update directory entry info of old dir inode */
1074 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1075
1076 down_write(&F2FS_I(old_inode)->i_sem);
1077 file_lost_pino(old_inode);
1078 up_write(&F2FS_I(old_inode)->i_sem);
1079
Deepa Dinamani078cd822016-09-14 07:48:04 -07001080 old_dir->i_ctime = current_time(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001081 if (old_nlink) {
1082 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001083 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001084 up_write(&F2FS_I(old_dir)->i_sem);
1085 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001086 f2fs_mark_inode_dirty_sync(old_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001087
1088 /* update directory entry info of new dir inode */
1089 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1090
1091 down_write(&F2FS_I(new_inode)->i_sem);
1092 file_lost_pino(new_inode);
1093 up_write(&F2FS_I(new_inode)->i_sem);
1094
Deepa Dinamani078cd822016-09-14 07:48:04 -07001095 new_dir->i_ctime = current_time(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001096 if (new_nlink) {
1097 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001098 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001099 up_write(&F2FS_I(new_dir)->i_sem);
1100 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001101 f2fs_mark_inode_dirty_sync(new_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001102
1103 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001104
1105 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1106 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +08001107 return 0;
Chao Yu32f9bc22014-07-12 19:13:54 +08001108out_new_dir:
1109 if (new_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -08001110 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001111 f2fs_put_page(new_dir_page, 0);
1112 }
1113out_old_dir:
1114 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -08001115 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001116 f2fs_put_page(old_dir_page, 0);
1117 }
1118out_new:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -08001119 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001120 f2fs_put_page(new_page, 0);
1121out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -08001122 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001123 f2fs_put_page(old_page, 0);
1124out:
1125 return err;
1126}
1127
1128static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1129 struct inode *new_dir, struct dentry *new_dentry,
1130 unsigned int flags)
1131{
Eric Biggers2e45b072017-11-29 12:35:30 -08001132 int err;
1133
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001134 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +08001135 return -EINVAL;
1136
Eric Biggers2e45b072017-11-29 12:35:30 -08001137 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1138 flags);
1139 if (err)
1140 return err;
1141
Chao Yu32f9bc22014-07-12 19:13:54 +08001142 if (flags & RENAME_EXCHANGE) {
1143 return f2fs_cross_rename(old_dir, old_dentry,
1144 new_dir, new_dentry);
1145 }
1146 /*
1147 * VFS has already handled the new dentry existence case,
1148 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1149 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001150 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +08001151}
1152
Al Viro6b255392015-11-17 10:20:54 -05001153static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001154 struct inode *inode,
1155 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001156{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001157 struct page *cpage = NULL;
1158 char *caddr, *paddr = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001159 struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
1160 struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
1161 struct fscrypt_symlink_data *sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001162 u32 max_size = inode->i_sb->s_blocksize;
1163 int res;
Chao Yu50732df2014-06-19 16:23:19 +08001164
Al Viro6b255392015-11-17 10:20:54 -05001165 if (!dentry)
1166 return ERR_PTR(-ECHILD);
1167
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001168 res = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001169 if (res)
1170 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001171
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001172 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1173 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -07001174 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -05001175 caddr = page_address(cpage);
Chao Yu50732df2014-06-19 16:23:19 +08001176
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001177 /* Symlink is encrypted */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001178 sd = (struct fscrypt_symlink_data *)caddr;
Chao Yu922ec352016-02-15 17:54:26 +08001179 cstr.name = sd->encrypted_path;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001180 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -08001181
1182 /* this is broken symlink case */
1183 if (unlikely(cstr.len == 0)) {
1184 res = -ENOENT;
1185 goto errout;
1186 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001187
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001188 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001189 /* Symlink data on the disk is corrupted */
1190 res = -EIO;
1191 goto errout;
1192 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001193 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001194 if (res)
1195 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +08001196
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001197 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001198 if (res)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001199 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +08001200
Jaegeuk Kimc90e09f2016-03-30 13:13:16 -07001201 /* this is broken symlink case */
1202 if (unlikely(pstr.name[0] == 0)) {
1203 res = -ENOENT;
1204 goto errout;
1205 }
1206
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001207 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001208
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001209 /* Null-terminate the name */
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001210 paddr[pstr.len] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001211
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001212 put_page(cpage);
Al Virofceef392015-12-29 15:58:39 -05001213 set_delayed_call(done, kfree_link, paddr);
1214 return paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001215errout:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001216 fscrypt_fname_free_buffer(&pstr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001217 put_page(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001218 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001219}
1220
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001221const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001222 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001223 .getattr = f2fs_getattr,
1224 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001225#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001226 .listxattr = f2fs_listxattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001227#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001228};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001229
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001230const struct inode_operations f2fs_dir_inode_operations = {
1231 .create = f2fs_create,
1232 .lookup = f2fs_lookup,
1233 .link = f2fs_link,
1234 .unlink = f2fs_unlink,
1235 .symlink = f2fs_symlink,
1236 .mkdir = f2fs_mkdir,
1237 .rmdir = f2fs_rmdir,
1238 .mknod = f2fs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001239 .rename = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001240 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001241 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001242 .setattr = f2fs_setattr,
1243 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001244 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001245#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001246 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001247#endif
1248};
1249
1250const struct inode_operations f2fs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001251 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001252 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001253 .setattr = f2fs_setattr,
1254#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001255 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001256#endif
1257};
1258
1259const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001260 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001261 .setattr = f2fs_setattr,
1262 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001263 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001264#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001265 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001266#endif
1267};