blob: f2ad7931e9492b179b389719d0918761979838e7 [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>
Jaegeuk Kim428e3bc2019-02-25 09:46:45 -080013#include <linux/random.h>
Chao Yu50732df2014-06-19 16:23:19 +080014#include <linux/dcache.h>
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -070015#include <linux/namei.h>
Chao Yu0abd6752017-07-09 00:13:07 +080016#include <linux/quotaops.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090017
18#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090019#include "node.h"
Daniel Rosenberg43549942018-08-20 19:21:43 -070020#include "segment.h"
Jaegeuk Kim57397d82012-11-02 17:11:10 +090021#include "xattr.h"
22#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090023#include <trace/events/f2fs.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090024
25static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
26{
Jaegeuk Kim40813632014-09-02 15:31:18 -070027 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090028 nid_t ino;
29 struct inode *inode;
30 bool nid_free = false;
Eric Biggerse075b692020-09-16 21:11:27 -070031 bool encrypt = 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);
Chao Yu4d57b862018-05-30 00:20:41 +080040 if (!f2fs_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
Christian Brauner21cb47b2021-01-21 14:19:25 +010049 inode_init_owner(&init_user_ns, 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 Dinamani95582b02018-05-08 19:36:02 -070053 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Arnd Bergmann24b81df2018-06-20 10:02:19 +020054 F2FS_I(inode)->i_crtime = inode->i_mtime;
Jaegeuk Kim428e3bc2019-02-25 09:46:45 -080055 inode->i_generation = prandom_u32();
Jaegeuk Kim57397d82012-11-02 17:11:10 +090056
Chao Yu1c41e6802018-05-07 20:28:52 +080057 if (S_ISDIR(inode->i_mode))
58 F2FS_I(inode)->i_current_depth = 1;
59
Jaegeuk Kim57397d82012-11-02 17:11:10 +090060 err = insert_inode_locked(inode);
61 if (err) {
62 err = -EINVAL;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070063 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090064 }
Chao Yu622f28a2014-09-24 18:19:10 +080065
Chao Yu7beb01f2018-10-24 18:34:26 +080066 if (f2fs_sb_has_project_quota(sbi) &&
Chao Yu59c84402018-04-03 15:08:17 +080067 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
Chao Yu5c571322017-07-26 00:01:41 +080068 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
69 else
70 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
71 F2FS_DEF_PROJID);
72
Eric Biggerse075b692020-09-16 21:11:27 -070073 err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
74 if (err)
75 goto fail_drop;
76
Chao Yu0abd6752017-07-09 00:13:07 +080077 err = dquot_initialize(inode);
78 if (err)
79 goto fail_drop;
80
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090081 set_inode_flag(inode, FI_NEW_INODE);
82
Eric Biggerse075b692020-09-16 21:11:27 -070083 if (encrypt)
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070084 f2fs_set_encrypted_inode(inode);
85
Chao Yu7beb01f2018-10-24 18:34:26 +080086 if (f2fs_sb_has_extra_attr(sbi)) {
Chao Yu7a2af762017-07-19 00:19:06 +080087 set_inode_flag(inode, FI_EXTRA_ATTR);
88 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
89 }
90
Jaegeuk Kim91942322016-05-20 10:13:22 -070091 if (test_opt(sbi, INLINE_XATTR))
92 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yu6afc6622017-09-06 21:59:50 +080093
Chao Yufba48a82015-12-23 17:51:35 +080094 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070095 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070096 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070097 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080098
Chao Yu7beb01f2018-10-24 18:34:26 +080099 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
Chao Yu6afc6622017-09-06 21:59:50 +0800100 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
101 if (f2fs_has_inline_xattr(inode))
Chao Yu63189b72018-03-08 14:22:56 +0800102 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
Chao Yu6afc6622017-09-06 21:59:50 +0800103 /* Otherwise, will be 0 */
104 } else if (f2fs_has_inline_xattr(inode) ||
105 f2fs_has_inline_dentry(inode)) {
106 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
107 }
108 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
109
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700110 f2fs_init_extent_tree(inode, NULL);
111
Chao Yud5e8f6c2015-07-15 17:28:53 +0800112 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -0700113 stat_inc_inline_inode(inode);
114 stat_inc_inline_dir(inode);
115
Chao Yu5c571322017-07-26 00:01:41 +0800116 F2FS_I(inode)->i_flags =
117 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
118
Chao Yu11f50202017-08-30 18:04:47 +0800119 if (S_ISDIR(inode->i_mode))
Chao Yu59c84402018-04-03 15:08:17 +0800120 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
Chao Yu11f50202017-08-30 18:04:47 +0800121
Chao Yu59c84402018-04-03 15:08:17 +0800122 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800123 set_inode_flag(inode, FI_PROJ_INHERIT);
124
Chao Yu4c8ff702019-11-01 18:07:14 +0800125 if (f2fs_sb_has_compression(sbi)) {
126 /* Inherit the compression flag in directory */
127 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
128 f2fs_may_compress(inode))
129 set_compress_context(inode);
130 }
131
Chao Yu9149a5e2018-10-07 19:06:15 +0800132 f2fs_set_inode_flags(inode);
133
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900134 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900135 return inode;
136
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900137fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900138 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900139 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900140 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700141 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700142 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900143 return ERR_PTR(err);
Chao Yu0abd6752017-07-09 00:13:07 +0800144fail_drop:
145 trace_f2fs_new_inode(inode, err);
146 dquot_drop(inode);
147 inode->i_flags |= S_NOQUOTA;
148 if (nid_free)
149 set_inode_flag(inode, FI_FREE_NID);
150 clear_nlink(inode);
151 unlock_new_inode(inode);
152 iput(inode);
153 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900154}
155
Park Ju Hyung5c533b12019-04-17 18:57:38 +0900156static inline int is_extension_exist(const unsigned char *s, const char *sub)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900157{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200158 size_t slen = strlen(s);
159 size_t sublen = strlen(sub);
Chao Yu7732c262016-09-05 12:28:27 +0800160 int i;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900161
Chao Yu4c8ff702019-11-01 18:07:14 +0800162 if (sublen == 1 && *sub == '*')
163 return 1;
164
Chao Yu741a7be2015-07-06 20:30:40 +0800165 /*
166 * filename format of multimedia file should be defined as:
Chao Yu7732c262016-09-05 12:28:27 +0800167 * "filename + '.' + extension + (optional: '.' + temp extension)".
Chao Yu741a7be2015-07-06 20:30:40 +0800168 */
169 if (slen < sublen + 2)
170 return 0;
171
Chao Yu7732c262016-09-05 12:28:27 +0800172 for (i = 1; i < slen - sublen; i++) {
173 if (s[i] != '.')
174 continue;
175 if (!strncasecmp(s + i + 1, sub, sublen))
176 return 1;
177 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900178
Chao Yu7732c262016-09-05 12:28:27 +0800179 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900180}
181
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900182/*
Chao Yu7a88ddb2020-02-27 19:30:05 +0800183 * Set file's temperature for hot/cold data separation
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900184 */
Chao Yub6a06cb2018-02-28 17:07:27 +0800185static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186 const unsigned char *name)
187{
Chao Yu846ae672018-02-26 22:04:13 +0800188 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800189 int i, cold_count, hot_count;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900190
Chao Yu846ae672018-02-26 22:04:13 +0800191 down_read(&sbi->sb_lock);
192
Chao Yub6a06cb2018-02-28 17:07:27 +0800193 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
194 hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800195
Chao Yub6a06cb2018-02-28 17:07:27 +0800196 for (i = 0; i < cold_count + hot_count; i++) {
Chao Yued15ba12018-10-04 11:15:18 +0800197 if (is_extension_exist(name, extlist[i]))
198 break;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900199 }
Chao Yu846ae672018-02-26 22:04:13 +0800200
201 up_read(&sbi->sb_lock);
Chao Yued15ba12018-10-04 11:15:18 +0800202
203 if (i == cold_count + hot_count)
204 return;
205
206 if (i < cold_count)
207 file_set_cold(inode);
208 else
209 file_set_hot(inode);
Chao Yu846ae672018-02-26 22:04:13 +0800210}
211
Chao Yu4d57b862018-05-30 00:20:41 +0800212int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
Chao Yub6a06cb2018-02-28 17:07:27 +0800213 bool hot, bool set)
Chao Yu846ae672018-02-26 22:04:13 +0800214{
215 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800216 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
217 int hot_count = sbi->raw_super->hot_ext_count;
218 int total_count = cold_count + hot_count;
219 int start, count;
Chao Yu846ae672018-02-26 22:04:13 +0800220 int i;
221
Chao Yub6a06cb2018-02-28 17:07:27 +0800222 if (set) {
223 if (total_count == F2FS_MAX_EXTENSION)
224 return -EINVAL;
225 } else {
226 if (!hot && !cold_count)
227 return -EINVAL;
228 if (hot && !hot_count)
229 return -EINVAL;
230 }
231
232 if (hot) {
233 start = cold_count;
234 count = total_count;
235 } else {
236 start = 0;
237 count = cold_count;
238 }
239
240 for (i = start; i < count; i++) {
Chao Yu846ae672018-02-26 22:04:13 +0800241 if (strcmp(name, extlist[i]))
242 continue;
243
244 if (set)
245 return -EINVAL;
246
247 memcpy(extlist[i], extlist[i + 1],
Chao Yub6a06cb2018-02-28 17:07:27 +0800248 F2FS_EXTENSION_LEN * (total_count - i - 1));
249 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
250 if (hot)
251 sbi->raw_super->hot_ext_count = hot_count - 1;
252 else
253 sbi->raw_super->extension_count =
254 cpu_to_le32(cold_count - 1);
Chao Yu846ae672018-02-26 22:04:13 +0800255 return 0;
256 }
257
258 if (!set)
259 return -EINVAL;
260
Chao Yub6a06cb2018-02-28 17:07:27 +0800261 if (hot) {
Guenter Roeckb1385472018-07-01 13:57:06 -0700262 memcpy(extlist[count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800263 sbi->raw_super->hot_ext_count = hot_count + 1;
264 } else {
265 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
Chao Yu846ae672018-02-26 22:04:13 +0800266
Chao Yub6a06cb2018-02-28 17:07:27 +0800267 memcpy(buf, &extlist[cold_count],
268 F2FS_EXTENSION_LEN * hot_count);
269 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
Guenter Roeckb1385472018-07-01 13:57:06 -0700270 memcpy(extlist[cold_count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800271 memcpy(&extlist[cold_count + 1], buf,
272 F2FS_EXTENSION_LEN * hot_count);
273 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
274 }
Chao Yu846ae672018-02-26 22:04:13 +0800275 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900276}
277
Chao Yu4c8ff702019-11-01 18:07:14 +0800278static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
279 const unsigned char *name)
280{
281 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
282 unsigned char (*ext)[F2FS_EXTENSION_LEN];
283 unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
284 int i, cold_count, hot_count;
285
286 if (!f2fs_sb_has_compression(sbi) ||
287 is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
288 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
289 !f2fs_may_compress(inode))
290 return;
291
292 down_read(&sbi->sb_lock);
293
294 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
295 hot_count = sbi->raw_super->hot_ext_count;
296
297 for (i = cold_count; i < cold_count + hot_count; i++) {
298 if (is_extension_exist(name, extlist[i])) {
299 up_read(&sbi->sb_lock);
300 return;
301 }
302 }
303
304 up_read(&sbi->sb_lock);
305
306 ext = F2FS_OPTION(sbi).extensions;
307
308 for (i = 0; i < ext_cnt; i++) {
309 if (!is_extension_exist(name, ext[i]))
310 continue;
311
312 set_compress_context(inode);
313 return;
314 }
315}
316
Christian Brauner549c7292021-01-21 14:19:43 +0100317static int f2fs_create(struct user_namespace *mnt_userns, struct inode *dir,
318 struct dentry *dentry, umode_t mode, bool excl)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900319{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700320 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900321 struct inode *inode;
322 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800323 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900324
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200325 if (unlikely(f2fs_cp_error(sbi)))
326 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800327 if (!f2fs_is_checkpoint_ready(sbi))
328 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200329
Chao Yu0abd6752017-07-09 00:13:07 +0800330 err = dquot_initialize(dir);
331 if (err)
332 return err;
333
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900334 inode = f2fs_new_inode(dir, mode);
335 if (IS_ERR(inode))
336 return PTR_ERR(inode);
337
338 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Chao Yub6a06cb2018-02-28 17:07:27 +0800339 set_file_temperature(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900340
Chao Yu4c8ff702019-11-01 18:07:14 +0800341 set_compress_inode(sbi, inode, dentry->d_name.name);
342
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900343 inode->i_op = &f2fs_file_inode_operations;
344 inode->i_fop = &f2fs_file_operations;
345 inode->i_mapping->a_ops = &f2fs_dblock_aops;
346 ino = inode->i_ino;
347
Gu Zhenge4795562013-09-27 18:08:30 +0800348 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900349 err = f2fs_add_link(dentry, inode);
350 if (err)
351 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700352 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900353
Chao Yu4d57b862018-05-30 00:20:41 +0800354 f2fs_alloc_nid_done(sbi, ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900355
Al Viro1e2e5472018-05-04 08:23:01 -0400356 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800357
358 if (IS_DIRSYNC(dir))
359 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700360
361 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900362 return 0;
363out:
Chao Yu4d57b862018-05-30 00:20:41 +0800364 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900365 return err;
366}
367
368static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
369 struct dentry *dentry)
370{
David Howells2b0143b2015-03-17 22:25:59 +0000371 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700372 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800373 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900374
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200375 if (unlikely(f2fs_cp_error(sbi)))
376 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800377 if (!f2fs_is_checkpoint_ready(sbi))
378 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200379
Eric Biggersb05157e2017-11-29 12:35:29 -0800380 err = fscrypt_prepare_link(old_dentry, dir, dentry);
381 if (err)
382 return err;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700383
Chao Yu5c571322017-07-26 00:01:41 +0800384 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
385 (!projid_eq(F2FS_I(dir)->i_projid,
386 F2FS_I(old_dentry->d_inode)->i_projid)))
387 return -EXDEV;
388
Chao Yu0abd6752017-07-09 00:13:07 +0800389 err = dquot_initialize(dir);
390 if (err)
391 return err;
392
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800393 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900394
Deepa Dinamani078cd822016-09-14 07:48:04 -0700395 inode->i_ctime = current_time(inode);
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900396 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900397
Jaegeuk Kim91942322016-05-20 10:13:22 -0700398 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800399 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900400 err = f2fs_add_link(dentry, inode);
401 if (err)
402 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700403 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900404
405 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800406
407 if (IS_DIRSYNC(dir))
408 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900409 return 0;
410out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700411 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900412 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700413 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900414 return err;
415}
416
417struct dentry *f2fs_get_parent(struct dentry *child)
418{
Chao Yu91246c22016-07-19 08:27:47 +0800419 struct page *page;
Al Viro80e5d1ff52021-04-15 19:46:50 -0400420 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800421 if (!ino) {
422 if (IS_ERR(page))
423 return ERR_CAST(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424 return ERR_PTR(-ENOENT);
Chao Yu91246c22016-07-19 08:27:47 +0800425 }
Al Virofc640052016-04-10 01:33:30 -0400426 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900427}
428
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700429static int __recover_dot_dentries(struct inode *dir, nid_t pino)
430{
431 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
432 struct qstr dot = QSTR_INIT(".", 1);
433 struct qstr dotdot = QSTR_INIT("..", 2);
434 struct f2fs_dir_entry *de;
435 struct page *page;
436 int err = 0;
437
Chao Yu4e0d8362015-12-30 17:40:31 +0800438 if (f2fs_readonly(sbi->sb)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800439 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
440 dir->i_ino, pino);
Chao Yu4e0d8362015-12-30 17:40:31 +0800441 return 0;
442 }
443
Chao Yua6d3a472017-07-24 17:12:06 +0800444 err = dquot_initialize(dir);
445 if (err)
446 return err;
447
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800448 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800449
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700450 f2fs_lock_op(sbi);
451
452 de = f2fs_find_entry(dir, &dot, &page);
453 if (de) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700454 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700455 } else if (IS_ERR(page)) {
456 err = PTR_ERR(page);
457 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700458 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800459 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700460 if (err)
461 goto out;
462 }
463
464 de = f2fs_find_entry(dir, &dotdot, &page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800465 if (de)
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700466 f2fs_put_page(page, 0);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800467 else if (IS_ERR(page))
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700468 err = PTR_ERR(page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800469 else
Chao Yu4d57b862018-05-30 00:20:41 +0800470 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700471out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700472 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700473 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700474
475 f2fs_unlock_op(sbi);
476 return err;
477}
478
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900479static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
480 unsigned int flags)
481{
482 struct inode *inode = NULL;
483 struct f2fs_dir_entry *de;
484 struct page *page;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800485 struct dentry *new;
486 nid_t ino = -1;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700487 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000488 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Eric Biggers43c780b2020-05-07 00:59:04 -0700489 struct f2fs_filename fname;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900490
Chao Yu0c5e36d2017-10-17 17:33:41 +0800491 trace_f2fs_lookup_start(dir, dentry, flags);
492
Chao Yu0c5e36d2017-10-17 17:33:41 +0800493 if (dentry->d_name.len > F2FS_NAME_LEN) {
494 err = -ENAMETOOLONG;
495 goto out;
496 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900497
Eric Biggers43c780b2020-05-07 00:59:04 -0700498 err = f2fs_prepare_lookup(dir, dentry, &fname);
Daniel Rosenbergbb9cd912020-11-19 06:09:03 +0000499 generic_set_encrypted_ci_d_ops(dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -0700500 if (err == -ENOENT)
501 goto out_splice;
502 if (err)
503 goto out;
504 de = __f2fs_find_entry(dir, &fname, &page);
Eric Biggers43c780b2020-05-07 00:59:04 -0700505 f2fs_free_filename(&fname);
Eric Biggersb01531d2019-03-20 11:39:13 -0700506
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700507 if (!de) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800508 if (IS_ERR(page)) {
509 err = PTR_ERR(page);
510 goto out;
511 }
Chao Yu84597b12020-05-27 18:27:51 +0800512 err = -ENOENT;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800513 goto out_splice;
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700514 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900515
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700516 ino = le32_to_cpu(de->ino);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700517 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700518
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700519 inode = f2fs_iget(dir->i_sb, ino);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800520 if (IS_ERR(inode)) {
521 err = PTR_ERR(inode);
522 goto out;
523 }
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700524
Liu Xue8c2b1432016-02-26 06:39:23 +0000525 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
526 err = __recover_dot_dentries(dir, root_ino);
527 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800528 goto out_iput;
Liu Xue8c2b1432016-02-26 06:39:23 +0000529 }
530
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700531 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700532 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700533 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800534 goto out_iput;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900535 }
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530536 if (IS_ENCRYPTED(dir) &&
Dan Carpenter07fe8d42016-12-16 11:18:15 +0300537 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
538 !fscrypt_has_permitted_context(dir, inode)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800539 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
540 dir->i_ino, inode->i_ino);
Eric Biggersfaac7fd2017-04-07 10:58:39 -0700541 err = -EPERM;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800542 goto out_iput;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800543 }
Chao Yu0c5e36d2017-10-17 17:33:41 +0800544out_splice:
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700545#ifdef CONFIG_UNICODE
546 if (!inode && IS_CASEFOLDED(dir)) {
547 /* Eventually we want to call d_add_ci(dentry, NULL)
548 * for negative dentries in the encoding case as
549 * well. For now, prevent the negative dentry
550 * from being cached.
551 */
552 trace_f2fs_lookup_end(dir, dentry, ino, err);
553 return NULL;
554 }
555#endif
Chao Yu0c5e36d2017-10-17 17:33:41 +0800556 new = d_splice_alias(inode, dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -0700557 err = PTR_ERR_OR_ZERO(new);
Chao Yu84597b12020-05-27 18:27:51 +0800558 trace_f2fs_lookup_end(dir, dentry, ino, !new ? -ENOENT : err);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800559 return new;
560out_iput:
Chao Yud7267322016-03-10 22:24:23 +0800561 iput(inode);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800562out:
563 trace_f2fs_lookup_end(dir, dentry, ino, err);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700564 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900565}
566
567static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
568{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700569 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000570 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900571 struct f2fs_dir_entry *de;
572 struct page *page;
Colin Ian Kingdeaf1602020-04-20 23:00:57 +0100573 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900574
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900575 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900576
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800577 if (unlikely(f2fs_cp_error(sbi))) {
578 err = -EIO;
579 goto fail;
580 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200581
Chao Yu0abd6752017-07-09 00:13:07 +0800582 err = dquot_initialize(dir);
583 if (err)
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800584 goto fail;
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200585 err = dquot_initialize(inode);
586 if (err)
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800587 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +0800588
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900589 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800590 if (!de) {
591 if (IS_ERR(page))
592 err = PTR_ERR(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900593 goto fail;
Chao Yu91246c22016-07-19 08:27:47 +0800594 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900595
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800596 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800597
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900598 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800599 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900600 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900601 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900602 f2fs_put_page(page, 0);
603 goto fail;
604 }
Chao Yudbeacf02014-09-24 18:17:04 +0800605 f2fs_delete_entry(de, page, dir, inode);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700606#ifdef CONFIG_UNICODE
607 /* VFS negative dentries are incompatible with Encoding and
608 * Case-insensitiveness. Eventually we'll want avoid
609 * invalidating the dentries here, alongside with returning the
Jack Qiua87aff12020-07-24 16:55:28 +0800610 * negative dentries at f2fs_lookup(), when it is better
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700611 * supported by the VFS for the CI case.
612 */
613 if (IS_CASEFOLDED(dir))
614 d_invalidate(dentry);
615#endif
Gu Zhenge4795562013-09-27 18:08:30 +0800616 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900617
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800618 if (IS_DIRSYNC(dir))
619 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900620fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900621 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900622 return err;
623}
624
Al Viro6b255392015-11-17 10:20:54 -0500625static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500626 struct inode *inode,
627 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700628{
Al Virofceef392015-12-29 15:58:39 -0500629 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400630 if (!IS_ERR(link) && !*link) {
631 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500632 do_delayed_call(done);
633 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400634 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700635 }
Al Viro680baac2015-05-02 13:32:22 -0400636 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700637}
638
Christian Brauner549c7292021-01-21 14:19:43 +0100639static int f2fs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
640 struct dentry *dentry, const char *symname)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900641{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700642 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900643 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700644 size_t len = strlen(symname);
Eric Biggers393c0382018-01-11 23:26:49 -0500645 struct fscrypt_str disk_link;
Gu Zhenge4795562013-09-27 18:08:30 +0800646 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900647
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200648 if (unlikely(f2fs_cp_error(sbi)))
649 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800650 if (!f2fs_is_checkpoint_ready(sbi))
651 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200652
Eric Biggers393c0382018-01-11 23:26:49 -0500653 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
654 &disk_link);
655 if (err)
656 return err;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700657
Chao Yu0abd6752017-07-09 00:13:07 +0800658 err = dquot_initialize(dir);
659 if (err)
660 return err;
661
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900662 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
663 if (IS_ERR(inode))
664 return PTR_ERR(inode);
665
Eric Biggers393c0382018-01-11 23:26:49 -0500666 if (IS_ENCRYPTED(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700667 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
668 else
669 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500670 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900671 inode->i_mapping->a_ops = &f2fs_dblock_aops;
672
Gu Zhenge4795562013-09-27 18:08:30 +0800673 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900674 err = f2fs_add_link(dentry, inode);
675 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800676 goto out_f2fs_handle_failed_inode;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700677 f2fs_unlock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800678 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900679
Eric Biggers393c0382018-01-11 23:26:49 -0500680 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
681 if (err)
682 goto err_out;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700683
Chao Yu922ec352016-02-15 17:54:26 +0800684 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700685
686err_out:
Al Viro1e2e5472018-05-04 08:23:01 -0400687 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800688
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700689 /*
690 * Let's flush symlink data in order to avoid broken symlink as much as
691 * possible. Nevertheless, fsyncing is the best way, but there is no
692 * way to get a file descriptor in order to flush that.
693 *
694 * Note that, it needs to do dir->fsync to make this recoverable.
695 * If the symlink path is stored into inline_data, there is no
696 * performance regression.
697 */
Chao Yua6be0142015-10-22 18:23:08 +0800698 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800699 filemap_write_and_wait_range(inode->i_mapping, 0,
700 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700701
Chao Yua6be0142015-10-22 18:23:08 +0800702 if (IS_DIRSYNC(dir))
703 f2fs_sync_fs(sbi->sb, 1);
704 } else {
705 f2fs_unlink(dir, dentry);
706 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700707
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700708 f2fs_balance_fs(sbi, true);
Eric Biggers393c0382018-01-11 23:26:49 -0500709 goto out_free_encrypted_link;
710
Chao Yu4d57b862018-05-30 00:20:41 +0800711out_f2fs_handle_failed_inode:
712 f2fs_handle_failed_inode(inode);
Eric Biggers393c0382018-01-11 23:26:49 -0500713out_free_encrypted_link:
714 if (disk_link.name != (unsigned char *)symname)
Chao Yuc8eb7022020-09-14 16:47:00 +0800715 kfree(disk_link.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900716 return err;
717}
718
Christian Brauner549c7292021-01-21 14:19:43 +0100719static int f2fs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
720 struct dentry *dentry, umode_t mode)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900721{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700722 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900723 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800724 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900725
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200726 if (unlikely(f2fs_cp_error(sbi)))
727 return -EIO;
728
Chao Yu0abd6752017-07-09 00:13:07 +0800729 err = dquot_initialize(dir);
730 if (err)
731 return err;
732
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900733 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900734 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900735 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900736
737 inode->i_op = &f2fs_dir_inode_operations;
738 inode->i_fop = &f2fs_dir_operations;
739 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800740 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741
Jaegeuk Kim91942322016-05-20 10:13:22 -0700742 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800743 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900744 err = f2fs_add_link(dentry, inode);
745 if (err)
746 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700747 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900748
Chao Yu4d57b862018-05-30 00:20:41 +0800749 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900750
Al Viro1e2e5472018-05-04 08:23:01 -0400751 d_instantiate_new(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900752
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800753 if (IS_DIRSYNC(dir))
754 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700755
756 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900757 return 0;
758
759out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700760 clear_inode_flag(inode, FI_INC_LINK);
Chao Yu4d57b862018-05-30 00:20:41 +0800761 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900762 return err;
763}
764
765static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
766{
David Howells2b0143b2015-03-17 22:25:59 +0000767 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900768 if (f2fs_empty_dir(inode))
769 return f2fs_unlink(dir, dentry);
770 return -ENOTEMPTY;
771}
772
Christian Brauner549c7292021-01-21 14:19:43 +0100773static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
774 struct dentry *dentry, umode_t mode, dev_t rdev)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900775{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700776 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900777 struct inode *inode;
778 int err = 0;
779
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200780 if (unlikely(f2fs_cp_error(sbi)))
781 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800782 if (!f2fs_is_checkpoint_ready(sbi))
783 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200784
Chao Yu0abd6752017-07-09 00:13:07 +0800785 err = dquot_initialize(dir);
786 if (err)
787 return err;
788
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900789 inode = f2fs_new_inode(dir, mode);
790 if (IS_ERR(inode))
791 return PTR_ERR(inode);
792
793 init_special_inode(inode, inode->i_mode, rdev);
794 inode->i_op = &f2fs_special_inode_operations;
795
Gu Zhenge4795562013-09-27 18:08:30 +0800796 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900797 err = f2fs_add_link(dentry, inode);
798 if (err)
799 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700800 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900801
Chao Yu4d57b862018-05-30 00:20:41 +0800802 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800803
Al Viro1e2e5472018-05-04 08:23:01 -0400804 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800805
806 if (IS_DIRSYNC(dir))
807 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700808
809 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900810 return 0;
811out:
Chao Yu4d57b862018-05-30 00:20:41 +0800812 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900813 return err;
814}
815
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800816static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
817 umode_t mode, struct inode **whiteout)
818{
819 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
820 struct inode *inode;
821 int err;
822
Chao Yu0abd6752017-07-09 00:13:07 +0800823 err = dquot_initialize(dir);
824 if (err)
825 return err;
826
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800827 inode = f2fs_new_inode(dir, mode);
828 if (IS_ERR(inode))
829 return PTR_ERR(inode);
830
831 if (whiteout) {
832 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
833 inode->i_op = &f2fs_special_inode_operations;
834 } else {
835 inode->i_op = &f2fs_file_inode_operations;
836 inode->i_fop = &f2fs_file_operations;
837 inode->i_mapping->a_ops = &f2fs_dblock_aops;
838 }
839
840 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800841 err = f2fs_acquire_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800842 if (err)
843 goto out;
844
845 err = f2fs_do_tmpfile(inode, dir);
846 if (err)
847 goto release_out;
848
849 /*
850 * add this non-linked tmpfile to orphan list, in this way we could
851 * remove all unused data of tmpfile after abnormal power-off.
852 */
Chao Yu4d57b862018-05-30 00:20:41 +0800853 f2fs_add_orphan_inode(inode);
854 f2fs_alloc_nid_done(sbi, inode->i_ino);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800855
856 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700857 f2fs_i_links_write(inode, false);
Chao Yu46085f32021-01-12 09:55:09 +0800858
859 spin_lock(&inode->i_lock);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800860 inode->i_state |= I_LINKABLE;
Chao Yu46085f32021-01-12 09:55:09 +0800861 spin_unlock(&inode->i_lock);
862
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800863 *whiteout = inode;
864 } else {
865 d_tmpfile(dentry, inode);
866 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700867 /* link_count was changed by d_tmpfile as well. */
868 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800869 unlock_new_inode(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700870
871 f2fs_balance_fs(sbi, true);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800872 return 0;
873
874release_out:
Chao Yu4d57b862018-05-30 00:20:41 +0800875 f2fs_release_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800876out:
Chao Yu4d57b862018-05-30 00:20:41 +0800877 f2fs_handle_failed_inode(inode);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800878 return err;
879}
880
Christian Brauner549c7292021-01-21 14:19:43 +0100881static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
882 struct dentry *dentry, umode_t mode)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800883{
Sheng Yongff62af22018-03-15 18:51:42 +0800884 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
885
886 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200887 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800888 if (!f2fs_is_checkpoint_ready(sbi))
889 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200890
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800891 return __f2fs_tmpfile(dir, dentry, mode, NULL);
892}
893
894static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
895{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200896 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
897 return -EIO;
898
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800899 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
900}
901
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900902static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800903 struct inode *new_dir, struct dentry *new_dentry,
904 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900905{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700906 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000907 struct inode *old_inode = d_inode(old_dentry);
908 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800909 struct inode *whiteout = NULL;
Jaegeuk Kim762e4db2019-12-11 15:10:47 -0800910 struct page *old_dir_page = NULL;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800911 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900912 struct f2fs_dir_entry *old_dir_entry = NULL;
913 struct f2fs_dir_entry *old_entry;
914 struct f2fs_dir_entry *new_entry;
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700915 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900916
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200917 if (unlikely(f2fs_cp_error(sbi)))
918 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800919 if (!f2fs_is_checkpoint_ready(sbi))
920 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200921
Chao Yu5c571322017-07-26 00:01:41 +0800922 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
923 (!projid_eq(F2FS_I(new_dir)->i_projid,
924 F2FS_I(old_dentry->d_inode)->i_projid)))
925 return -EXDEV;
926
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800927 /*
928 * If new_inode is null, the below renaming flow will
929 * add a link in old_dir which can conver inline_dir.
930 * After then, if we failed to get the entry due to other
931 * reasons like ENOMEM, we had to remove the new entry.
932 * Instead of adding such the error handling routine, let's
933 * simply convert first here.
934 */
935 if (old_dir == new_dir && !new_inode) {
936 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
937 if (err)
938 return err;
939 }
940
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800941 if (flags & RENAME_WHITEOUT) {
942 err = f2fs_create_whiteout(old_dir, &whiteout);
943 if (err)
944 return err;
945 }
946
Chao Yu0abd6752017-07-09 00:13:07 +0800947 err = dquot_initialize(old_dir);
948 if (err)
949 goto out;
950
951 err = dquot_initialize(new_dir);
952 if (err)
953 goto out;
954
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200955 if (new_inode) {
956 err = dquot_initialize(new_inode);
957 if (err)
958 goto out;
959 }
960
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700961 err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900962 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +0800963 if (!old_entry) {
964 if (IS_ERR(old_page))
965 err = PTR_ERR(old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900966 goto out;
Chao Yu91246c22016-07-19 08:27:47 +0800967 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900968
969 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900970 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700971 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800972 if (IS_ERR(old_dir_page))
973 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900974 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700975 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900976 }
977
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900978 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900979
980 err = -ENOTEMPTY;
981 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800982 goto out_dir;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900983
984 err = -ENOENT;
985 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
986 &new_page);
Chao Yu91246c22016-07-19 08:27:47 +0800987 if (!new_entry) {
988 if (IS_ERR(new_page))
989 err = PTR_ERR(new_page);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800990 goto out_dir;
Chao Yu91246c22016-07-19 08:27:47 +0800991 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900992
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800993 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800994
Chao Yu12560102014-06-24 14:16:24 +0800995 f2fs_lock_op(sbi);
996
Chao Yu4d57b862018-05-30 00:20:41 +0800997 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900998 if (err)
999 goto put_out_dir;
1000
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001001 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001002 new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001003
Deepa Dinamani078cd822016-09-14 07:48:04 -07001004 new_inode->i_ctime = current_time(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +09001005 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001006 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -07001007 f2fs_i_links_write(new_inode, false);
1008 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +09001009 up_write(&F2FS_I(new_inode)->i_sem);
1010
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001011 if (!new_inode->i_nlink)
Chao Yu4d57b862018-05-30 00:20:41 +08001012 f2fs_add_orphan_inode(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001013 else
Chao Yu4d57b862018-05-30 00:20:41 +08001014 f2fs_release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001015 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001016 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001017
Chao Yu12560102014-06-24 14:16:24 +08001018 f2fs_lock_op(sbi);
1019
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001020 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +08001021 if (err) {
1022 f2fs_unlock_op(sbi);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001023 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +08001024 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001025
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001026 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -07001027 f2fs_i_links_write(new_dir, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001028 }
1029
Jaegeuk Kimb2c08292014-06-30 18:09:55 +09001030 down_write(&F2FS_I(old_inode)->i_sem);
Sheng Yongb855bf02017-06-26 10:41:36 +08001031 if (!old_dir_entry || whiteout)
1032 file_lost_pino(old_inode);
1033 else
Chao Yu2a606372019-11-07 14:12:05 +08001034 /* adjust dir's i_pino to pass fsck check */
1035 f2fs_i_pino_write(old_inode, new_dir->i_ino);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +09001036 up_write(&F2FS_I(old_inode)->i_sem);
1037
Deepa Dinamani078cd822016-09-14 07:48:04 -07001038 old_inode->i_ctime = current_time(old_inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001039 f2fs_mark_inode_dirty_sync(old_inode, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001040
Chao Yudbeacf02014-09-24 18:17:04 +08001041 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001042 old_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001043
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001044 if (whiteout) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001045 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001046 err = f2fs_add_link(old_dentry, whiteout);
1047 if (err)
1048 goto put_out_dir;
Chao Yu46085f32021-01-12 09:55:09 +08001049
1050 spin_lock(&whiteout->i_lock);
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001051 whiteout->i_state &= ~I_LINKABLE;
Chao Yu46085f32021-01-12 09:55:09 +08001052 spin_unlock(&whiteout->i_lock);
1053
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001054 iput(whiteout);
1055 }
1056
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001057 if (old_dir_entry) {
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001058 if (old_dir != new_dir && !whiteout)
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001059 f2fs_set_link(old_inode, old_dir_entry,
1060 old_dir_page, new_dir);
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001061 else
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001062 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001063 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001064 }
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001065 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001066 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001067 if (S_ISDIR(old_inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001068 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1069 TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001070 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001071
Gu Zhenge4795562013-09-27 18:08:30 +08001072 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001073
1074 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1075 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301076
1077 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001078 return 0;
1079
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001080put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +08001081 f2fs_unlock_op(sbi);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001082 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001083out_dir:
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001084 if (old_dir_entry)
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001085 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001086out_old:
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001087 f2fs_put_page(old_page, 0);
1088out:
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001089 if (whiteout)
1090 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001091 return err;
1092}
1093
Chao Yu32f9bc22014-07-12 19:13:54 +08001094static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1095 struct inode *new_dir, struct dentry *new_dentry)
1096{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001097 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +00001098 struct inode *old_inode = d_inode(old_dentry);
1099 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +08001100 struct page *old_dir_page, *new_dir_page;
1101 struct page *old_page, *new_page;
1102 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1103 struct f2fs_dir_entry *old_entry, *new_entry;
1104 int old_nlink = 0, new_nlink = 0;
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001105 int err;
Chao Yu32f9bc22014-07-12 19:13:54 +08001106
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001107 if (unlikely(f2fs_cp_error(sbi)))
1108 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001109 if (!f2fs_is_checkpoint_ready(sbi))
1110 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001111
Chao Yu5c571322017-07-26 00:01:41 +08001112 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1113 !projid_eq(F2FS_I(new_dir)->i_projid,
1114 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1115 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1116 !projid_eq(F2FS_I(old_dir)->i_projid,
1117 F2FS_I(new_dentry->d_inode)->i_projid)))
1118 return -EXDEV;
1119
Chao Yu0abd6752017-07-09 00:13:07 +08001120 err = dquot_initialize(old_dir);
1121 if (err)
1122 goto out;
1123
1124 err = dquot_initialize(new_dir);
1125 if (err)
1126 goto out;
1127
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001128 err = -ENOENT;
Chao Yu32f9bc22014-07-12 19:13:54 +08001129 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +08001130 if (!old_entry) {
1131 if (IS_ERR(old_page))
1132 err = PTR_ERR(old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001133 goto out;
Chao Yu91246c22016-07-19 08:27:47 +08001134 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001135
1136 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001137 if (!new_entry) {
1138 if (IS_ERR(new_page))
1139 err = PTR_ERR(new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001140 goto out_old;
Chao Yu91246c22016-07-19 08:27:47 +08001141 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001142
1143 /* prepare for updating ".." directory entry info later */
1144 if (old_dir != new_dir) {
1145 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001146 old_dir_entry = f2fs_parent_dir(old_inode,
1147 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001148 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001149 if (IS_ERR(old_dir_page))
1150 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001151 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001152 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001153 }
1154
1155 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001156 new_dir_entry = f2fs_parent_dir(new_inode,
1157 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001158 if (!new_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001159 if (IS_ERR(new_dir_page))
1160 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001161 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001162 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001163 }
1164 }
1165
1166 /*
1167 * If cross rename between file and directory those are not
1168 * in the same directory, we will inc nlink of file's parent
1169 * later, so we should check upper boundary of its nlink.
1170 */
1171 if ((!old_dir_entry || !new_dir_entry) &&
1172 old_dir_entry != new_dir_entry) {
1173 old_nlink = old_dir_entry ? -1 : 1;
1174 new_nlink = -old_nlink;
1175 err = -EMLINK;
Kinglong Meee2f0e962017-03-04 21:48:28 +08001176 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1177 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
Chao Yu32f9bc22014-07-12 19:13:54 +08001178 goto out_new_dir;
1179 }
1180
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001181 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001182
Chao Yu32f9bc22014-07-12 19:13:54 +08001183 f2fs_lock_op(sbi);
1184
Chao Yu32f9bc22014-07-12 19:13:54 +08001185 /* update ".." directory entry info of old dentry */
1186 if (old_dir_entry)
1187 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1188
1189 /* update ".." directory entry info of new dentry */
1190 if (new_dir_entry)
1191 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1192
1193 /* update directory entry info of old dir inode */
1194 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1195
1196 down_write(&F2FS_I(old_inode)->i_sem);
Chao Yu2a606372019-11-07 14:12:05 +08001197 if (!old_dir_entry)
1198 file_lost_pino(old_inode);
1199 else
1200 /* adjust dir's i_pino to pass fsck check */
1201 f2fs_i_pino_write(old_inode, new_dir->i_ino);
Chao Yu32f9bc22014-07-12 19:13:54 +08001202 up_write(&F2FS_I(old_inode)->i_sem);
1203
Deepa Dinamani078cd822016-09-14 07:48:04 -07001204 old_dir->i_ctime = current_time(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001205 if (old_nlink) {
1206 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001207 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001208 up_write(&F2FS_I(old_dir)->i_sem);
1209 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001210 f2fs_mark_inode_dirty_sync(old_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001211
1212 /* update directory entry info of new dir inode */
1213 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1214
1215 down_write(&F2FS_I(new_inode)->i_sem);
Chao Yu2a606372019-11-07 14:12:05 +08001216 if (!new_dir_entry)
1217 file_lost_pino(new_inode);
1218 else
1219 /* adjust dir's i_pino to pass fsck check */
1220 f2fs_i_pino_write(new_inode, old_dir->i_ino);
Chao Yu32f9bc22014-07-12 19:13:54 +08001221 up_write(&F2FS_I(new_inode)->i_sem);
1222
Deepa Dinamani078cd822016-09-14 07:48:04 -07001223 new_dir->i_ctime = current_time(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001224 if (new_nlink) {
1225 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001226 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001227 up_write(&F2FS_I(new_dir)->i_sem);
1228 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001229 f2fs_mark_inode_dirty_sync(new_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001230
Chao Yu63189b72018-03-08 14:22:56 +08001231 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001232 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1233 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Junling Zheng93cf93f2018-03-07 12:07:49 +08001234 }
Jaegeuk Kim0a007b972017-12-28 08:09:44 -08001235
Chao Yu32f9bc22014-07-12 19:13:54 +08001236 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001237
1238 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1239 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301240
1241 f2fs_update_time(sbi, REQ_TIME);
Chao Yu32f9bc22014-07-12 19:13:54 +08001242 return 0;
Chao Yu32f9bc22014-07-12 19:13:54 +08001243out_new_dir:
1244 if (new_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001245 f2fs_put_page(new_dir_page, 0);
1246 }
1247out_old_dir:
1248 if (old_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001249 f2fs_put_page(old_dir_page, 0);
1250 }
1251out_new:
Chao Yu32f9bc22014-07-12 19:13:54 +08001252 f2fs_put_page(new_page, 0);
1253out_old:
Chao Yu32f9bc22014-07-12 19:13:54 +08001254 f2fs_put_page(old_page, 0);
1255out:
1256 return err;
1257}
1258
Christian Brauner549c7292021-01-21 14:19:43 +01001259static int f2fs_rename2(struct user_namespace *mnt_userns,
1260 struct inode *old_dir, struct dentry *old_dentry,
Chao Yu32f9bc22014-07-12 19:13:54 +08001261 struct inode *new_dir, struct dentry *new_dentry,
1262 unsigned int flags)
1263{
Eric Biggers2e45b072017-11-29 12:35:30 -08001264 int err;
1265
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001266 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +08001267 return -EINVAL;
1268
Eric Biggers2e45b072017-11-29 12:35:30 -08001269 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1270 flags);
1271 if (err)
1272 return err;
1273
Chao Yu32f9bc22014-07-12 19:13:54 +08001274 if (flags & RENAME_EXCHANGE) {
1275 return f2fs_cross_rename(old_dir, old_dentry,
1276 new_dir, new_dentry);
1277 }
1278 /*
1279 * VFS has already handled the new dentry existence case,
1280 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1281 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001282 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +08001283}
1284
Al Viro6b255392015-11-17 10:20:54 -05001285static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001286 struct inode *inode,
1287 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001288{
Eric Biggersf2329cb2018-01-11 23:26:49 -05001289 struct page *page;
1290 const char *target;
Chao Yu50732df2014-06-19 16:23:19 +08001291
Al Viro6b255392015-11-17 10:20:54 -05001292 if (!dentry)
1293 return ERR_PTR(-ECHILD);
1294
Eric Biggersf2329cb2018-01-11 23:26:49 -05001295 page = read_mapping_page(inode->i_mapping, 0, NULL);
1296 if (IS_ERR(page))
1297 return ERR_CAST(page);
Chao Yu50732df2014-06-19 16:23:19 +08001298
Eric Biggersf2329cb2018-01-11 23:26:49 -05001299 target = fscrypt_get_symlink(inode, page_address(page),
1300 inode->i_sb->s_blocksize, done);
1301 put_page(page);
1302 return target;
Chao Yu50732df2014-06-19 16:23:19 +08001303}
1304
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001305const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
Jack Qiua87aff12020-07-24 16:55:28 +08001306 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001307 .getattr = f2fs_getattr,
1308 .setattr = f2fs_setattr,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001309 .listxattr = f2fs_listxattr,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001310};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001311
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001312const struct inode_operations f2fs_dir_inode_operations = {
1313 .create = f2fs_create,
1314 .lookup = f2fs_lookup,
1315 .link = f2fs_link,
1316 .unlink = f2fs_unlink,
1317 .symlink = f2fs_symlink,
1318 .mkdir = f2fs_mkdir,
1319 .rmdir = f2fs_rmdir,
1320 .mknod = f2fs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001321 .rename = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001322 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001323 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001324 .setattr = f2fs_setattr,
1325 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001326 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001327 .listxattr = f2fs_listxattr,
Chao Yu7975f342019-07-22 18:03:50 +08001328 .fiemap = f2fs_fiemap,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001329};
1330
1331const struct inode_operations f2fs_symlink_inode_operations = {
Jack Qiua87aff12020-07-24 16:55:28 +08001332 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001333 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001334 .setattr = f2fs_setattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001335 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001336};
1337
1338const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001339 .getattr = f2fs_getattr,
Jack Qiua87aff12020-07-24 16:55:28 +08001340 .setattr = f2fs_setattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001341 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001342 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001343 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001344};