blob: 9ad451ac2cecc7d36219329d34de86d1ba501fec [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +09003 * fs/f2fs/namei.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kim57397d82012-11-02 17:11:10 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/pagemap.h>
11#include <linux/sched.h>
12#include <linux/ctype.h>
Chao Yu50732df2014-06-19 16:23:19 +080013#include <linux/dcache.h>
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -070014#include <linux/namei.h>
Chao Yu0abd6752017-07-09 00:13:07 +080015#include <linux/quotaops.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090016
17#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090018#include "node.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090019#include "xattr.h"
20#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090021#include <trace/events/f2fs.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090022
23static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
24{
Jaegeuk Kim40813632014-09-02 15:31:18 -070025 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090026 nid_t ino;
27 struct inode *inode;
28 bool nid_free = false;
Chao Yu6afc6622017-09-06 21:59:50 +080029 int xattr_size = 0;
Gu Zhenge4795562013-09-27 18:08:30 +080030 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090031
Jaegeuk Kima014e032014-06-20 21:44:02 -070032 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090033 if (!inode)
34 return ERR_PTR(-ENOMEM);
35
Gu Zhenge4795562013-09-27 18:08:30 +080036 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +080037 if (!f2fs_alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080038 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090039 err = -ENOSPC;
40 goto fail;
41 }
Gu Zhenge4795562013-09-27 18:08:30 +080042 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090043
Chao Yu0abd6752017-07-09 00:13:07 +080044 nid_free = true;
45
Chao Yu70ff5df2014-05-08 17:09:30 +080046 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090047
48 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090049 inode->i_blocks = 0;
Deepa Dinamani95582b02018-05-08 19:36:02 -070050 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Arnd Bergmann24b81df2018-06-20 10:02:19 +020051 F2FS_I(inode)->i_crtime = inode->i_mtime;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090052 inode->i_generation = sbi->s_next_generation++;
53
Chao Yu1c41e6802018-05-07 20:28:52 +080054 if (S_ISDIR(inode->i_mode))
55 F2FS_I(inode)->i_current_depth = 1;
56
Jaegeuk Kim57397d82012-11-02 17:11:10 +090057 err = insert_inode_locked(inode);
58 if (err) {
59 err = -EINVAL;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070060 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090061 }
Chao Yu622f28a2014-09-24 18:19:10 +080062
Chao Yu5c571322017-07-26 00:01:41 +080063 if (f2fs_sb_has_project_quota(sbi->sb) &&
Chao Yu59c84402018-04-03 15:08:17 +080064 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
Chao Yu5c571322017-07-26 00:01:41 +080065 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
66 else
67 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
68 F2FS_DEF_PROJID);
69
Chao Yu0abd6752017-07-09 00:13:07 +080070 err = dquot_initialize(inode);
71 if (err)
72 goto fail_drop;
73
74 err = dquot_alloc_inode(inode);
75 if (err)
76 goto fail_drop;
77
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090078 set_inode_flag(inode, FI_NEW_INODE);
79
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070080 /* If the directory encrypted, then we should encrypt the inode. */
Sheng Yongff62af22018-03-15 18:51:42 +080081 if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
82 f2fs_may_encrypt(inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070083 f2fs_set_encrypted_inode(inode);
84
Chao Yu7a2af762017-07-19 00:19:06 +080085 if (f2fs_sb_has_extra_attr(sbi->sb)) {
86 set_inode_flag(inode, FI_EXTRA_ATTR);
87 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
88 }
89
Jaegeuk Kim91942322016-05-20 10:13:22 -070090 if (test_opt(sbi, INLINE_XATTR))
91 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yu6afc6622017-09-06 21:59:50 +080092
Chao Yufba48a82015-12-23 17:51:35 +080093 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070094 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070095 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070096 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080097
Chao Yu6afc6622017-09-06 21:59:50 +080098 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
99 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
100 if (f2fs_has_inline_xattr(inode))
Chao Yu63189b72018-03-08 14:22:56 +0800101 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
Chao Yu6afc6622017-09-06 21:59:50 +0800102 /* Otherwise, will be 0 */
103 } else if (f2fs_has_inline_xattr(inode) ||
104 f2fs_has_inline_dentry(inode)) {
105 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
106 }
107 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
108
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700109 f2fs_init_extent_tree(inode, NULL);
110
Chao Yud5e8f6c2015-07-15 17:28:53 +0800111 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -0700112 stat_inc_inline_inode(inode);
113 stat_inc_inline_dir(inode);
114
Chao Yu5c571322017-07-26 00:01:41 +0800115 F2FS_I(inode)->i_flags =
116 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
117
Chao Yu11f50202017-08-30 18:04:47 +0800118 if (S_ISDIR(inode->i_mode))
Chao Yu59c84402018-04-03 15:08:17 +0800119 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
Chao Yu11f50202017-08-30 18:04:47 +0800120
Chao Yu59c84402018-04-03 15:08:17 +0800121 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800122 set_inode_flag(inode, FI_PROJ_INHERIT);
123
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900124 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900125 return inode;
126
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900127fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900128 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900129 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900130 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700131 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700132 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900133 return ERR_PTR(err);
Chao Yu0abd6752017-07-09 00:13:07 +0800134fail_drop:
135 trace_f2fs_new_inode(inode, err);
136 dquot_drop(inode);
137 inode->i_flags |= S_NOQUOTA;
138 if (nid_free)
139 set_inode_flag(inode, FI_FREE_NID);
140 clear_nlink(inode);
141 unlock_new_inode(inode);
142 iput(inode);
143 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900144}
145
Chao Yub6a06cb2018-02-28 17:07:27 +0800146static int is_extension_exist(const unsigned char *s, const char *sub)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900147{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200148 size_t slen = strlen(s);
149 size_t sublen = strlen(sub);
Chao Yu7732c262016-09-05 12:28:27 +0800150 int i;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900151
Chao Yu741a7be2015-07-06 20:30:40 +0800152 /*
153 * filename format of multimedia file should be defined as:
Chao Yu7732c262016-09-05 12:28:27 +0800154 * "filename + '.' + extension + (optional: '.' + temp extension)".
Chao Yu741a7be2015-07-06 20:30:40 +0800155 */
156 if (slen < sublen + 2)
157 return 0;
158
Chao Yu7732c262016-09-05 12:28:27 +0800159 for (i = 1; i < slen - sublen; i++) {
160 if (s[i] != '.')
161 continue;
162 if (!strncasecmp(s + i + 1, sub, sublen))
163 return 1;
164 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165
Chao Yu7732c262016-09-05 12:28:27 +0800166 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900167}
168
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900169/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900170 * Set multimedia files as cold files for hot/cold data separation
171 */
Chao Yub6a06cb2018-02-28 17:07:27 +0800172static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900173 const unsigned char *name)
174{
Chao Yu846ae672018-02-26 22:04:13 +0800175 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800176 int i, cold_count, hot_count;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900177
Chao Yu846ae672018-02-26 22:04:13 +0800178 down_read(&sbi->sb_lock);
179
Chao Yub6a06cb2018-02-28 17:07:27 +0800180 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
181 hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800182
Chao Yub6a06cb2018-02-28 17:07:27 +0800183 for (i = 0; i < cold_count + hot_count; i++) {
184 if (!is_extension_exist(name, extlist[i]))
185 continue;
186 if (i < cold_count)
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900187 file_set_cold(inode);
Chao Yub6a06cb2018-02-28 17:07:27 +0800188 else
189 file_set_hot(inode);
190 break;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900191 }
Chao Yu846ae672018-02-26 22:04:13 +0800192
193 up_read(&sbi->sb_lock);
194}
195
Chao Yu4d57b862018-05-30 00:20:41 +0800196int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
Chao Yub6a06cb2018-02-28 17:07:27 +0800197 bool hot, bool set)
Chao Yu846ae672018-02-26 22:04:13 +0800198{
199 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800200 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
201 int hot_count = sbi->raw_super->hot_ext_count;
202 int total_count = cold_count + hot_count;
203 int start, count;
Chao Yu846ae672018-02-26 22:04:13 +0800204 int i;
205
Chao Yub6a06cb2018-02-28 17:07:27 +0800206 if (set) {
207 if (total_count == F2FS_MAX_EXTENSION)
208 return -EINVAL;
209 } else {
210 if (!hot && !cold_count)
211 return -EINVAL;
212 if (hot && !hot_count)
213 return -EINVAL;
214 }
215
216 if (hot) {
217 start = cold_count;
218 count = total_count;
219 } else {
220 start = 0;
221 count = cold_count;
222 }
223
224 for (i = start; i < count; i++) {
Chao Yu846ae672018-02-26 22:04:13 +0800225 if (strcmp(name, extlist[i]))
226 continue;
227
228 if (set)
229 return -EINVAL;
230
231 memcpy(extlist[i], extlist[i + 1],
Chao Yub6a06cb2018-02-28 17:07:27 +0800232 F2FS_EXTENSION_LEN * (total_count - i - 1));
233 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
234 if (hot)
235 sbi->raw_super->hot_ext_count = hot_count - 1;
236 else
237 sbi->raw_super->extension_count =
238 cpu_to_le32(cold_count - 1);
Chao Yu846ae672018-02-26 22:04:13 +0800239 return 0;
240 }
241
242 if (!set)
243 return -EINVAL;
244
Chao Yub6a06cb2018-02-28 17:07:27 +0800245 if (hot) {
Guenter Roeckb1385472018-07-01 13:57:06 -0700246 memcpy(extlist[count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800247 sbi->raw_super->hot_ext_count = hot_count + 1;
248 } else {
249 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
Chao Yu846ae672018-02-26 22:04:13 +0800250
Chao Yub6a06cb2018-02-28 17:07:27 +0800251 memcpy(buf, &extlist[cold_count],
252 F2FS_EXTENSION_LEN * hot_count);
253 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
Guenter Roeckb1385472018-07-01 13:57:06 -0700254 memcpy(extlist[cold_count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800255 memcpy(&extlist[cold_count + 1], buf,
256 F2FS_EXTENSION_LEN * hot_count);
257 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
258 }
Chao Yu846ae672018-02-26 22:04:13 +0800259 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900260}
261
262static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
263 bool excl)
264{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700265 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900266 struct inode *inode;
267 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800268 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900269
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200270 if (unlikely(f2fs_cp_error(sbi)))
271 return -EIO;
272
Chao Yu0abd6752017-07-09 00:13:07 +0800273 err = dquot_initialize(dir);
274 if (err)
275 return err;
276
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900277 inode = f2fs_new_inode(dir, mode);
278 if (IS_ERR(inode))
279 return PTR_ERR(inode);
280
281 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Chao Yub6a06cb2018-02-28 17:07:27 +0800282 set_file_temperature(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900283
284 inode->i_op = &f2fs_file_inode_operations;
285 inode->i_fop = &f2fs_file_operations;
286 inode->i_mapping->a_ops = &f2fs_dblock_aops;
287 ino = inode->i_ino;
288
Gu Zhenge4795562013-09-27 18:08:30 +0800289 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900290 err = f2fs_add_link(dentry, inode);
291 if (err)
292 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700293 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900294
Chao Yu4d57b862018-05-30 00:20:41 +0800295 f2fs_alloc_nid_done(sbi, ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900296
Al Viro1e2e5472018-05-04 08:23:01 -0400297 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800298
299 if (IS_DIRSYNC(dir))
300 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700301
302 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900303 return 0;
304out:
Chao Yu4d57b862018-05-30 00:20:41 +0800305 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900306 return err;
307}
308
309static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
310 struct dentry *dentry)
311{
David Howells2b0143b2015-03-17 22:25:59 +0000312 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700313 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800314 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900315
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200316 if (unlikely(f2fs_cp_error(sbi)))
317 return -EIO;
318
Eric Biggersb05157e2017-11-29 12:35:29 -0800319 err = fscrypt_prepare_link(old_dentry, dir, dentry);
320 if (err)
321 return err;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700322
Chao Yu5c571322017-07-26 00:01:41 +0800323 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
324 (!projid_eq(F2FS_I(dir)->i_projid,
325 F2FS_I(old_dentry->d_inode)->i_projid)))
326 return -EXDEV;
327
Chao Yu0abd6752017-07-09 00:13:07 +0800328 err = dquot_initialize(dir);
329 if (err)
330 return err;
331
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800332 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900333
Deepa Dinamani078cd822016-09-14 07:48:04 -0700334 inode->i_ctime = current_time(inode);
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900335 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900336
Jaegeuk Kim91942322016-05-20 10:13:22 -0700337 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800338 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900339 err = f2fs_add_link(dentry, inode);
340 if (err)
341 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700342 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900343
344 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800345
346 if (IS_DIRSYNC(dir))
347 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900348 return 0;
349out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700350 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700352 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900353 return err;
354}
355
356struct dentry *f2fs_get_parent(struct dentry *child)
357{
358 struct qstr dotdot = QSTR_INIT("..", 2);
Chao Yu91246c22016-07-19 08:27:47 +0800359 struct page *page;
360 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
361 if (!ino) {
362 if (IS_ERR(page))
363 return ERR_CAST(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900364 return ERR_PTR(-ENOENT);
Chao Yu91246c22016-07-19 08:27:47 +0800365 }
Al Virofc640052016-04-10 01:33:30 -0400366 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900367}
368
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700369static int __recover_dot_dentries(struct inode *dir, nid_t pino)
370{
371 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
372 struct qstr dot = QSTR_INIT(".", 1);
373 struct qstr dotdot = QSTR_INIT("..", 2);
374 struct f2fs_dir_entry *de;
375 struct page *page;
376 int err = 0;
377
Chao Yu4e0d8362015-12-30 17:40:31 +0800378 if (f2fs_readonly(sbi->sb)) {
379 f2fs_msg(sbi->sb, KERN_INFO,
380 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
381 "in readonly mountpoint", dir->i_ino, pino);
382 return 0;
383 }
384
Chao Yua6d3a472017-07-24 17:12:06 +0800385 err = dquot_initialize(dir);
386 if (err)
387 return err;
388
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800389 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800390
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700391 f2fs_lock_op(sbi);
392
393 de = f2fs_find_entry(dir, &dot, &page);
394 if (de) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700395 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700396 } else if (IS_ERR(page)) {
397 err = PTR_ERR(page);
398 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700399 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800400 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700401 if (err)
402 goto out;
403 }
404
405 de = f2fs_find_entry(dir, &dotdot, &page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800406 if (de)
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700407 f2fs_put_page(page, 0);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800408 else if (IS_ERR(page))
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700409 err = PTR_ERR(page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800410 else
Chao Yu4d57b862018-05-30 00:20:41 +0800411 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700412out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700413 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700414 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700415
416 f2fs_unlock_op(sbi);
417 return err;
418}
419
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900420static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
421 unsigned int flags)
422{
423 struct inode *inode = NULL;
424 struct f2fs_dir_entry *de;
425 struct page *page;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800426 struct dentry *new;
427 nid_t ino = -1;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700428 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000429 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900430
Chao Yu0c5e36d2017-10-17 17:33:41 +0800431 trace_f2fs_lookup_start(dir, dentry, flags);
432
Eric Biggers55899d72017-11-29 12:35:31 -0800433 err = fscrypt_prepare_lookup(dir, dentry, flags);
434 if (err)
435 goto out;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800436
Chao Yu0c5e36d2017-10-17 17:33:41 +0800437 if (dentry->d_name.len > F2FS_NAME_LEN) {
438 err = -ENAMETOOLONG;
439 goto out;
440 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900441
442 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700443 if (!de) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800444 if (IS_ERR(page)) {
445 err = PTR_ERR(page);
446 goto out;
447 }
448 goto out_splice;
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700449 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900450
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700451 ino = le32_to_cpu(de->ino);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700452 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700453
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700454 inode = f2fs_iget(dir->i_sb, ino);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800455 if (IS_ERR(inode)) {
456 err = PTR_ERR(inode);
457 goto out;
458 }
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700459
Liu Xue8c2b1432016-02-26 06:39:23 +0000460 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
461 err = __recover_dot_dentries(dir, root_ino);
462 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800463 goto out_iput;
Liu Xue8c2b1432016-02-26 06:39:23 +0000464 }
465
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700466 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700467 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700468 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800469 goto out_iput;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900470 }
Dan Carpenter07fe8d42016-12-16 11:18:15 +0300471 if (f2fs_encrypted_inode(dir) &&
472 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
473 !fscrypt_has_permitted_context(dir, inode)) {
Eric Biggersfaac7fd2017-04-07 10:58:39 -0700474 f2fs_msg(inode->i_sb, KERN_WARNING,
475 "Inconsistent encryption contexts: %lu/%lu",
476 dir->i_ino, inode->i_ino);
477 err = -EPERM;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800478 goto out_iput;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800479 }
Chao Yu0c5e36d2017-10-17 17:33:41 +0800480out_splice:
481 new = d_splice_alias(inode, dentry);
482 if (IS_ERR(new))
483 err = PTR_ERR(new);
484 trace_f2fs_lookup_end(dir, dentry, ino, err);
485 return new;
486out_iput:
Chao Yud7267322016-03-10 22:24:23 +0800487 iput(inode);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800488out:
489 trace_f2fs_lookup_end(dir, dentry, ino, err);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700490 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900491}
492
493static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
494{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700495 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000496 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900497 struct f2fs_dir_entry *de;
498 struct page *page;
499 int err = -ENOENT;
500
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900501 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900502
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200503 if (unlikely(f2fs_cp_error(sbi)))
504 return -EIO;
505
Chao Yu0abd6752017-07-09 00:13:07 +0800506 err = dquot_initialize(dir);
507 if (err)
508 return err;
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200509 err = dquot_initialize(inode);
510 if (err)
511 return err;
Chao Yu0abd6752017-07-09 00:13:07 +0800512
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900513 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800514 if (!de) {
515 if (IS_ERR(page))
516 err = PTR_ERR(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900517 goto fail;
Chao Yu91246c22016-07-19 08:27:47 +0800518 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900519
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800520 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800521
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900522 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800523 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900524 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900525 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900526 f2fs_put_page(page, 0);
527 goto fail;
528 }
Chao Yudbeacf02014-09-24 18:17:04 +0800529 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800530 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900531
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800532 if (IS_DIRSYNC(dir))
533 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900534fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900535 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900536 return err;
537}
538
Al Viro6b255392015-11-17 10:20:54 -0500539static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500540 struct inode *inode,
541 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700542{
Al Virofceef392015-12-29 15:58:39 -0500543 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400544 if (!IS_ERR(link) && !*link) {
545 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500546 do_delayed_call(done);
547 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400548 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700549 }
Al Viro680baac2015-05-02 13:32:22 -0400550 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700551}
552
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900553static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
554 const char *symname)
555{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700556 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900557 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700558 size_t len = strlen(symname);
Eric Biggers393c0382018-01-11 23:26:49 -0500559 struct fscrypt_str disk_link;
Gu Zhenge4795562013-09-27 18:08:30 +0800560 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900561
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200562 if (unlikely(f2fs_cp_error(sbi)))
563 return -EIO;
564
Eric Biggers393c0382018-01-11 23:26:49 -0500565 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
566 &disk_link);
567 if (err)
568 return err;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700569
Chao Yu0abd6752017-07-09 00:13:07 +0800570 err = dquot_initialize(dir);
571 if (err)
572 return err;
573
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900574 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
575 if (IS_ERR(inode))
576 return PTR_ERR(inode);
577
Eric Biggers393c0382018-01-11 23:26:49 -0500578 if (IS_ENCRYPTED(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700579 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
580 else
581 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500582 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900583 inode->i_mapping->a_ops = &f2fs_dblock_aops;
584
Gu Zhenge4795562013-09-27 18:08:30 +0800585 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900586 err = f2fs_add_link(dentry, inode);
587 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800588 goto out_f2fs_handle_failed_inode;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700589 f2fs_unlock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800590 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900591
Eric Biggers393c0382018-01-11 23:26:49 -0500592 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
593 if (err)
594 goto err_out;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700595
Chao Yu922ec352016-02-15 17:54:26 +0800596 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700597
598err_out:
Al Viro1e2e5472018-05-04 08:23:01 -0400599 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800600
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700601 /*
602 * Let's flush symlink data in order to avoid broken symlink as much as
603 * possible. Nevertheless, fsyncing is the best way, but there is no
604 * way to get a file descriptor in order to flush that.
605 *
606 * Note that, it needs to do dir->fsync to make this recoverable.
607 * If the symlink path is stored into inline_data, there is no
608 * performance regression.
609 */
Chao Yua6be0142015-10-22 18:23:08 +0800610 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800611 filemap_write_and_wait_range(inode->i_mapping, 0,
612 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700613
Chao Yua6be0142015-10-22 18:23:08 +0800614 if (IS_DIRSYNC(dir))
615 f2fs_sync_fs(sbi->sb, 1);
616 } else {
617 f2fs_unlink(dir, dentry);
618 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700619
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700620 f2fs_balance_fs(sbi, true);
Eric Biggers393c0382018-01-11 23:26:49 -0500621 goto out_free_encrypted_link;
622
Chao Yu4d57b862018-05-30 00:20:41 +0800623out_f2fs_handle_failed_inode:
624 f2fs_handle_failed_inode(inode);
Eric Biggers393c0382018-01-11 23:26:49 -0500625out_free_encrypted_link:
626 if (disk_link.name != (unsigned char *)symname)
627 kfree(disk_link.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900628 return err;
629}
630
631static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
632{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700633 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900634 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800635 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900636
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200637 if (unlikely(f2fs_cp_error(sbi)))
638 return -EIO;
639
Chao Yu0abd6752017-07-09 00:13:07 +0800640 err = dquot_initialize(dir);
641 if (err)
642 return err;
643
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900644 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900645 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900646 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900647
648 inode->i_op = &f2fs_dir_inode_operations;
649 inode->i_fop = &f2fs_dir_operations;
650 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800651 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900652
Jaegeuk Kim91942322016-05-20 10:13:22 -0700653 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800654 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900655 err = f2fs_add_link(dentry, inode);
656 if (err)
657 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700658 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900659
Chao Yu4d57b862018-05-30 00:20:41 +0800660 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900661
Al Viro1e2e5472018-05-04 08:23:01 -0400662 d_instantiate_new(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900663
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800664 if (IS_DIRSYNC(dir))
665 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700666
667 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900668 return 0;
669
670out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700671 clear_inode_flag(inode, FI_INC_LINK);
Chao Yu4d57b862018-05-30 00:20:41 +0800672 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900673 return err;
674}
675
676static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
677{
David Howells2b0143b2015-03-17 22:25:59 +0000678 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900679 if (f2fs_empty_dir(inode))
680 return f2fs_unlink(dir, dentry);
681 return -ENOTEMPTY;
682}
683
684static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
685 umode_t mode, dev_t rdev)
686{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700687 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900688 struct inode *inode;
689 int err = 0;
690
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200691 if (unlikely(f2fs_cp_error(sbi)))
692 return -EIO;
693
Chao Yu0abd6752017-07-09 00:13:07 +0800694 err = dquot_initialize(dir);
695 if (err)
696 return err;
697
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900698 inode = f2fs_new_inode(dir, mode);
699 if (IS_ERR(inode))
700 return PTR_ERR(inode);
701
702 init_special_inode(inode, inode->i_mode, rdev);
703 inode->i_op = &f2fs_special_inode_operations;
704
Gu Zhenge4795562013-09-27 18:08:30 +0800705 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900706 err = f2fs_add_link(dentry, inode);
707 if (err)
708 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700709 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900710
Chao Yu4d57b862018-05-30 00:20:41 +0800711 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800712
Al Viro1e2e5472018-05-04 08:23:01 -0400713 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800714
715 if (IS_DIRSYNC(dir))
716 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700717
718 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900719 return 0;
720out:
Chao Yu4d57b862018-05-30 00:20:41 +0800721 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900722 return err;
723}
724
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800725static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
726 umode_t mode, struct inode **whiteout)
727{
728 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
729 struct inode *inode;
730 int err;
731
Chao Yu0abd6752017-07-09 00:13:07 +0800732 err = dquot_initialize(dir);
733 if (err)
734 return err;
735
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800736 inode = f2fs_new_inode(dir, mode);
737 if (IS_ERR(inode))
738 return PTR_ERR(inode);
739
740 if (whiteout) {
741 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
742 inode->i_op = &f2fs_special_inode_operations;
743 } else {
744 inode->i_op = &f2fs_file_inode_operations;
745 inode->i_fop = &f2fs_file_operations;
746 inode->i_mapping->a_ops = &f2fs_dblock_aops;
747 }
748
749 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800750 err = f2fs_acquire_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800751 if (err)
752 goto out;
753
754 err = f2fs_do_tmpfile(inode, dir);
755 if (err)
756 goto release_out;
757
758 /*
759 * add this non-linked tmpfile to orphan list, in this way we could
760 * remove all unused data of tmpfile after abnormal power-off.
761 */
Chao Yu4d57b862018-05-30 00:20:41 +0800762 f2fs_add_orphan_inode(inode);
763 f2fs_alloc_nid_done(sbi, inode->i_ino);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800764
765 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700766 f2fs_i_links_write(inode, false);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800767 *whiteout = inode;
768 } else {
769 d_tmpfile(dentry, inode);
770 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700771 /* link_count was changed by d_tmpfile as well. */
772 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800773 unlock_new_inode(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700774
775 f2fs_balance_fs(sbi, true);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800776 return 0;
777
778release_out:
Chao Yu4d57b862018-05-30 00:20:41 +0800779 f2fs_release_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800780out:
Chao Yu4d57b862018-05-30 00:20:41 +0800781 f2fs_handle_failed_inode(inode);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800782 return err;
783}
784
785static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
786{
Sheng Yongff62af22018-03-15 18:51:42 +0800787 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
788
789 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200790 return -EIO;
791
Sheng Yongff62af22018-03-15 18:51:42 +0800792 if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700793 int err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700794 if (err)
795 return err;
796 }
797
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800798 return __f2fs_tmpfile(dir, dentry, mode, NULL);
799}
800
801static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
802{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200803 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
804 return -EIO;
805
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800806 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
807}
808
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900809static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800810 struct inode *new_dir, struct dentry *new_dentry,
811 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900812{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700813 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000814 struct inode *old_inode = d_inode(old_dentry);
815 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800816 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900817 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800818 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900819 struct f2fs_dir_entry *old_dir_entry = NULL;
820 struct f2fs_dir_entry *old_entry;
821 struct f2fs_dir_entry *new_entry;
Chao Yu993a0492016-02-17 16:47:05 +0800822 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800823 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900824
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200825 if (unlikely(f2fs_cp_error(sbi)))
826 return -EIO;
827
Chao Yu5c571322017-07-26 00:01:41 +0800828 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
829 (!projid_eq(F2FS_I(new_dir)->i_projid,
830 F2FS_I(old_dentry->d_inode)->i_projid)))
831 return -EXDEV;
832
Chao Yu0abd6752017-07-09 00:13:07 +0800833 err = dquot_initialize(old_dir);
834 if (err)
835 goto out;
836
837 err = dquot_initialize(new_dir);
838 if (err)
839 goto out;
840
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200841 if (new_inode) {
842 err = dquot_initialize(new_inode);
843 if (err)
844 goto out;
845 }
846
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900847 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +0800848 if (!old_entry) {
849 if (IS_ERR(old_page))
850 err = PTR_ERR(old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900851 goto out;
Chao Yu91246c22016-07-19 08:27:47 +0800852 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900853
854 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900855 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700856 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800857 if (IS_ERR(old_dir_page))
858 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900859 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700860 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900861 }
862
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800863 if (flags & RENAME_WHITEOUT) {
864 err = f2fs_create_whiteout(old_dir, &whiteout);
865 if (err)
866 goto out_dir;
867 }
868
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900869 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900870
871 err = -ENOTEMPTY;
872 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800873 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900874
875 err = -ENOENT;
876 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
877 &new_page);
Chao Yu91246c22016-07-19 08:27:47 +0800878 if (!new_entry) {
879 if (IS_ERR(new_page))
880 err = PTR_ERR(new_page);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800881 goto out_whiteout;
Chao Yu91246c22016-07-19 08:27:47 +0800882 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900883
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800884 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800885
Chao Yu12560102014-06-24 14:16:24 +0800886 f2fs_lock_op(sbi);
887
Chao Yu4d57b862018-05-30 00:20:41 +0800888 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900889 if (err)
890 goto put_out_dir;
891
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900892 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
893
Deepa Dinamani078cd822016-09-14 07:48:04 -0700894 new_inode->i_ctime = current_time(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900895 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900896 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700897 f2fs_i_links_write(new_inode, false);
898 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900899 up_write(&F2FS_I(new_inode)->i_sem);
900
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900901 if (!new_inode->i_nlink)
Chao Yu4d57b862018-05-30 00:20:41 +0800902 f2fs_add_orphan_inode(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900903 else
Chao Yu4d57b862018-05-30 00:20:41 +0800904 f2fs_release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900905 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800906 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800907
Chao Yu12560102014-06-24 14:16:24 +0800908 f2fs_lock_op(sbi);
909
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900910 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800911 if (err) {
912 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800913 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800914 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900915
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700916 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700917 f2fs_i_links_write(new_dir, true);
Chao Yu993a0492016-02-17 16:47:05 +0800918
919 /*
920 * old entry and new entry can locate in the same inline
921 * dentry in inode, when attaching new entry in inline dentry,
922 * it could force inline dentry conversion, after that,
923 * old_entry and old_page will point to wrong address, in
924 * order to avoid this, let's do the check and update here.
925 */
926 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
927 f2fs_put_page(old_page, 0);
928 old_page = NULL;
929
930 old_entry = f2fs_find_entry(old_dir,
931 &old_dentry->d_name, &old_page);
932 if (!old_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800933 err = -ENOENT;
934 if (IS_ERR(old_page))
935 err = PTR_ERR(old_page);
Chao Yu993a0492016-02-17 16:47:05 +0800936 f2fs_unlock_op(sbi);
937 goto out_whiteout;
938 }
939 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900940 }
941
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900942 down_write(&F2FS_I(old_inode)->i_sem);
Sheng Yongb855bf02017-06-26 10:41:36 +0800943 if (!old_dir_entry || whiteout)
944 file_lost_pino(old_inode);
945 else
946 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900947 up_write(&F2FS_I(old_inode)->i_sem);
948
Deepa Dinamani078cd822016-09-14 07:48:04 -0700949 old_inode->i_ctime = current_time(old_inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700950 f2fs_mark_inode_dirty_sync(old_inode, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900951
Chao Yudbeacf02014-09-24 18:17:04 +0800952 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900953
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800954 if (whiteout) {
955 whiteout->i_state |= I_LINKABLE;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700956 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800957 err = f2fs_add_link(old_dentry, whiteout);
958 if (err)
959 goto put_out_dir;
960 whiteout->i_state &= ~I_LINKABLE;
961 iput(whiteout);
962 }
963
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900964 if (old_dir_entry) {
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800965 if (old_dir != new_dir && !whiteout)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900966 f2fs_set_link(old_inode, old_dir_entry,
967 old_dir_page, new_dir);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800968 else
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900969 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700970 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900971 }
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600972 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800973 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600974 if (S_ISDIR(old_inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +0800975 f2fs_add_ino_entry(sbi, old_inode->i_ino,
976 TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600977 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900978
Gu Zhenge4795562013-09-27 18:08:30 +0800979 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800980
981 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
982 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900983 return 0;
984
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900985put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800986 f2fs_unlock_op(sbi);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800987 if (new_page)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800988 f2fs_put_page(new_page, 0);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800989out_whiteout:
990 if (whiteout)
991 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900992out_dir:
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800993 if (old_dir_entry)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900994 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900995out_old:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900996 f2fs_put_page(old_page, 0);
997out:
998 return err;
999}
1000
Chao Yu32f9bc22014-07-12 19:13:54 +08001001static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1002 struct inode *new_dir, struct dentry *new_dentry)
1003{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001004 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +00001005 struct inode *old_inode = d_inode(old_dentry);
1006 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +08001007 struct page *old_dir_page, *new_dir_page;
1008 struct page *old_page, *new_page;
1009 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1010 struct f2fs_dir_entry *old_entry, *new_entry;
1011 int old_nlink = 0, new_nlink = 0;
1012 int err = -ENOENT;
1013
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001014 if (unlikely(f2fs_cp_error(sbi)))
1015 return -EIO;
1016
Chao Yu5c571322017-07-26 00:01:41 +08001017 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1018 !projid_eq(F2FS_I(new_dir)->i_projid,
1019 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1020 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1021 !projid_eq(F2FS_I(old_dir)->i_projid,
1022 F2FS_I(new_dentry->d_inode)->i_projid)))
1023 return -EXDEV;
1024
Chao Yu0abd6752017-07-09 00:13:07 +08001025 err = dquot_initialize(old_dir);
1026 if (err)
1027 goto out;
1028
1029 err = dquot_initialize(new_dir);
1030 if (err)
1031 goto out;
1032
Chao Yu32f9bc22014-07-12 19:13:54 +08001033 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +08001034 if (!old_entry) {
1035 if (IS_ERR(old_page))
1036 err = PTR_ERR(old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001037 goto out;
Chao Yu91246c22016-07-19 08:27:47 +08001038 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001039
1040 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001041 if (!new_entry) {
1042 if (IS_ERR(new_page))
1043 err = PTR_ERR(new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001044 goto out_old;
Chao Yu91246c22016-07-19 08:27:47 +08001045 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001046
1047 /* prepare for updating ".." directory entry info later */
1048 if (old_dir != new_dir) {
1049 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001050 old_dir_entry = f2fs_parent_dir(old_inode,
1051 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001052 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001053 if (IS_ERR(old_dir_page))
1054 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001055 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001056 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001057 }
1058
1059 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001060 new_dir_entry = f2fs_parent_dir(new_inode,
1061 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001062 if (!new_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001063 if (IS_ERR(new_dir_page))
1064 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001065 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001066 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001067 }
1068 }
1069
1070 /*
1071 * If cross rename between file and directory those are not
1072 * in the same directory, we will inc nlink of file's parent
1073 * later, so we should check upper boundary of its nlink.
1074 */
1075 if ((!old_dir_entry || !new_dir_entry) &&
1076 old_dir_entry != new_dir_entry) {
1077 old_nlink = old_dir_entry ? -1 : 1;
1078 new_nlink = -old_nlink;
1079 err = -EMLINK;
Kinglong Meee2f0e962017-03-04 21:48:28 +08001080 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1081 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
Chao Yu32f9bc22014-07-12 19:13:54 +08001082 goto out_new_dir;
1083 }
1084
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001085 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001086
Chao Yu32f9bc22014-07-12 19:13:54 +08001087 f2fs_lock_op(sbi);
1088
Chao Yu32f9bc22014-07-12 19:13:54 +08001089 /* update ".." directory entry info of old dentry */
1090 if (old_dir_entry)
1091 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1092
1093 /* update ".." directory entry info of new dentry */
1094 if (new_dir_entry)
1095 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1096
1097 /* update directory entry info of old dir inode */
1098 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1099
1100 down_write(&F2FS_I(old_inode)->i_sem);
1101 file_lost_pino(old_inode);
1102 up_write(&F2FS_I(old_inode)->i_sem);
1103
Deepa Dinamani078cd822016-09-14 07:48:04 -07001104 old_dir->i_ctime = current_time(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001105 if (old_nlink) {
1106 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001107 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001108 up_write(&F2FS_I(old_dir)->i_sem);
1109 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001110 f2fs_mark_inode_dirty_sync(old_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001111
1112 /* update directory entry info of new dir inode */
1113 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1114
1115 down_write(&F2FS_I(new_inode)->i_sem);
1116 file_lost_pino(new_inode);
1117 up_write(&F2FS_I(new_inode)->i_sem);
1118
Deepa Dinamani078cd822016-09-14 07:48:04 -07001119 new_dir->i_ctime = current_time(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001120 if (new_nlink) {
1121 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001122 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001123 up_write(&F2FS_I(new_dir)->i_sem);
1124 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001125 f2fs_mark_inode_dirty_sync(new_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001126
Chao Yu63189b72018-03-08 14:22:56 +08001127 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001128 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1129 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Junling Zheng93cf93f2018-03-07 12:07:49 +08001130 }
Jaegeuk Kim0a007b972017-12-28 08:09:44 -08001131
Chao Yu32f9bc22014-07-12 19:13:54 +08001132 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001133
1134 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1135 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +08001136 return 0;
Chao Yu32f9bc22014-07-12 19:13:54 +08001137out_new_dir:
1138 if (new_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001139 f2fs_put_page(new_dir_page, 0);
1140 }
1141out_old_dir:
1142 if (old_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001143 f2fs_put_page(old_dir_page, 0);
1144 }
1145out_new:
Chao Yu32f9bc22014-07-12 19:13:54 +08001146 f2fs_put_page(new_page, 0);
1147out_old:
Chao Yu32f9bc22014-07-12 19:13:54 +08001148 f2fs_put_page(old_page, 0);
1149out:
1150 return err;
1151}
1152
1153static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1154 struct inode *new_dir, struct dentry *new_dentry,
1155 unsigned int flags)
1156{
Eric Biggers2e45b072017-11-29 12:35:30 -08001157 int err;
1158
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001159 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +08001160 return -EINVAL;
1161
Eric Biggers2e45b072017-11-29 12:35:30 -08001162 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1163 flags);
1164 if (err)
1165 return err;
1166
Chao Yu32f9bc22014-07-12 19:13:54 +08001167 if (flags & RENAME_EXCHANGE) {
1168 return f2fs_cross_rename(old_dir, old_dentry,
1169 new_dir, new_dentry);
1170 }
1171 /*
1172 * VFS has already handled the new dentry existence case,
1173 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1174 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001175 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +08001176}
1177
Al Viro6b255392015-11-17 10:20:54 -05001178static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001179 struct inode *inode,
1180 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001181{
Eric Biggersf2329cb2018-01-11 23:26:49 -05001182 struct page *page;
1183 const char *target;
Chao Yu50732df2014-06-19 16:23:19 +08001184
Al Viro6b255392015-11-17 10:20:54 -05001185 if (!dentry)
1186 return ERR_PTR(-ECHILD);
1187
Eric Biggersf2329cb2018-01-11 23:26:49 -05001188 page = read_mapping_page(inode->i_mapping, 0, NULL);
1189 if (IS_ERR(page))
1190 return ERR_CAST(page);
Chao Yu50732df2014-06-19 16:23:19 +08001191
Eric Biggersf2329cb2018-01-11 23:26:49 -05001192 target = fscrypt_get_symlink(inode, page_address(page),
1193 inode->i_sb->s_blocksize, done);
1194 put_page(page);
1195 return target;
Chao Yu50732df2014-06-19 16:23:19 +08001196}
1197
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001198const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001199 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001200 .getattr = f2fs_getattr,
1201 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001202#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001203 .listxattr = f2fs_listxattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001204#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001205};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001206
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001207const struct inode_operations f2fs_dir_inode_operations = {
1208 .create = f2fs_create,
1209 .lookup = f2fs_lookup,
1210 .link = f2fs_link,
1211 .unlink = f2fs_unlink,
1212 .symlink = f2fs_symlink,
1213 .mkdir = f2fs_mkdir,
1214 .rmdir = f2fs_rmdir,
1215 .mknod = f2fs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001216 .rename = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001217 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001218 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001219 .setattr = f2fs_setattr,
1220 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001221 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001222#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001223 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001224#endif
1225};
1226
1227const struct inode_operations f2fs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001228 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001229 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001230 .setattr = f2fs_setattr,
1231#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001232 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001233#endif
1234};
1235
1236const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001237 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001238 .setattr = f2fs_setattr,
1239 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001240 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001241#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001242 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001243#endif
1244};