blob: e149c8c66a71b16fb0026e2497f8184ce9b1335c [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
Chao Yu4a67d9b2021-05-18 17:54:58 +0800156static inline int is_extension_exist(const unsigned char *s, const char *sub,
157 bool tmp_ext)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200159 size_t slen = strlen(s);
160 size_t sublen = strlen(sub);
Chao Yu7732c262016-09-05 12:28:27 +0800161 int i;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900162
Chao Yu4c8ff702019-11-01 18:07:14 +0800163 if (sublen == 1 && *sub == '*')
164 return 1;
165
Chao Yu741a7be2015-07-06 20:30:40 +0800166 /*
167 * filename format of multimedia file should be defined as:
Chao Yu7732c262016-09-05 12:28:27 +0800168 * "filename + '.' + extension + (optional: '.' + temp extension)".
Chao Yu741a7be2015-07-06 20:30:40 +0800169 */
170 if (slen < sublen + 2)
171 return 0;
172
Chao Yu4a67d9b2021-05-18 17:54:58 +0800173 if (!tmp_ext) {
174 /* file has no temp extension */
175 if (s[slen - sublen - 1] != '.')
176 return 0;
177 return !strncasecmp(s + slen - sublen, sub, sublen);
178 }
179
Chao Yu7732c262016-09-05 12:28:27 +0800180 for (i = 1; i < slen - sublen; i++) {
181 if (s[i] != '.')
182 continue;
183 if (!strncasecmp(s + i + 1, sub, sublen))
184 return 1;
185 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186
Chao Yu7732c262016-09-05 12:28:27 +0800187 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900188}
189
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900190/*
Chao Yu7a88ddb2020-02-27 19:30:05 +0800191 * Set file's temperature for hot/cold data separation
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900192 */
Chao Yub6a06cb2018-02-28 17:07:27 +0800193static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900194 const unsigned char *name)
195{
Chao Yu846ae672018-02-26 22:04:13 +0800196 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800197 int i, cold_count, hot_count;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900198
Chao Yu846ae672018-02-26 22:04:13 +0800199 down_read(&sbi->sb_lock);
200
Chao Yub6a06cb2018-02-28 17:07:27 +0800201 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
202 hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800203
Chao Yub6a06cb2018-02-28 17:07:27 +0800204 for (i = 0; i < cold_count + hot_count; i++) {
Chao Yu4a67d9b2021-05-18 17:54:58 +0800205 if (is_extension_exist(name, extlist[i], true))
Chao Yued15ba12018-10-04 11:15:18 +0800206 break;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900207 }
Chao Yu846ae672018-02-26 22:04:13 +0800208
209 up_read(&sbi->sb_lock);
Chao Yued15ba12018-10-04 11:15:18 +0800210
211 if (i == cold_count + hot_count)
212 return;
213
214 if (i < cold_count)
215 file_set_cold(inode);
216 else
217 file_set_hot(inode);
Chao Yu846ae672018-02-26 22:04:13 +0800218}
219
Chao Yu4d57b862018-05-30 00:20:41 +0800220int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
Chao Yub6a06cb2018-02-28 17:07:27 +0800221 bool hot, bool set)
Chao Yu846ae672018-02-26 22:04:13 +0800222{
223 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800224 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
225 int hot_count = sbi->raw_super->hot_ext_count;
226 int total_count = cold_count + hot_count;
227 int start, count;
Chao Yu846ae672018-02-26 22:04:13 +0800228 int i;
229
Chao Yub6a06cb2018-02-28 17:07:27 +0800230 if (set) {
231 if (total_count == F2FS_MAX_EXTENSION)
232 return -EINVAL;
233 } else {
234 if (!hot && !cold_count)
235 return -EINVAL;
236 if (hot && !hot_count)
237 return -EINVAL;
238 }
239
240 if (hot) {
241 start = cold_count;
242 count = total_count;
243 } else {
244 start = 0;
245 count = cold_count;
246 }
247
248 for (i = start; i < count; i++) {
Chao Yu846ae672018-02-26 22:04:13 +0800249 if (strcmp(name, extlist[i]))
250 continue;
251
252 if (set)
253 return -EINVAL;
254
255 memcpy(extlist[i], extlist[i + 1],
Chao Yub6a06cb2018-02-28 17:07:27 +0800256 F2FS_EXTENSION_LEN * (total_count - i - 1));
257 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
258 if (hot)
259 sbi->raw_super->hot_ext_count = hot_count - 1;
260 else
261 sbi->raw_super->extension_count =
262 cpu_to_le32(cold_count - 1);
Chao Yu846ae672018-02-26 22:04:13 +0800263 return 0;
264 }
265
266 if (!set)
267 return -EINVAL;
268
Chao Yub6a06cb2018-02-28 17:07:27 +0800269 if (hot) {
Guenter Roeckb1385472018-07-01 13:57:06 -0700270 memcpy(extlist[count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800271 sbi->raw_super->hot_ext_count = hot_count + 1;
272 } else {
273 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
Chao Yu846ae672018-02-26 22:04:13 +0800274
Chao Yub6a06cb2018-02-28 17:07:27 +0800275 memcpy(buf, &extlist[cold_count],
276 F2FS_EXTENSION_LEN * hot_count);
277 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
Guenter Roeckb1385472018-07-01 13:57:06 -0700278 memcpy(extlist[cold_count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800279 memcpy(&extlist[cold_count + 1], buf,
280 F2FS_EXTENSION_LEN * hot_count);
281 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
282 }
Chao Yu846ae672018-02-26 22:04:13 +0800283 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900284}
285
Chao Yu4c8ff702019-11-01 18:07:14 +0800286static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
287 const unsigned char *name)
288{
289 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Fengnan Chang151b1982021-06-08 19:15:08 +0800290 unsigned char (*noext)[F2FS_EXTENSION_LEN] = F2FS_OPTION(sbi).noextensions;
291 unsigned char (*ext)[F2FS_EXTENSION_LEN] = F2FS_OPTION(sbi).extensions;
292 unsigned char ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
293 unsigned char noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
Chao Yu4c8ff702019-11-01 18:07:14 +0800294 int i, cold_count, hot_count;
295
296 if (!f2fs_sb_has_compression(sbi) ||
Chao Yu4c8ff702019-11-01 18:07:14 +0800297 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
Fengnan Chang151b1982021-06-08 19:15:08 +0800298 !f2fs_may_compress(inode) ||
299 (!ext_cnt && !noext_cnt))
Chao Yu4c8ff702019-11-01 18:07:14 +0800300 return;
301
302 down_read(&sbi->sb_lock);
303
304 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
305 hot_count = sbi->raw_super->hot_ext_count;
306
307 for (i = cold_count; i < cold_count + hot_count; i++) {
Chao Yu4a67d9b2021-05-18 17:54:58 +0800308 if (is_extension_exist(name, extlist[i], false)) {
Chao Yu4c8ff702019-11-01 18:07:14 +0800309 up_read(&sbi->sb_lock);
310 return;
311 }
312 }
313
314 up_read(&sbi->sb_lock);
315
Fengnan Chang151b1982021-06-08 19:15:08 +0800316 for (i = 0; i < noext_cnt; i++) {
317 if (is_extension_exist(name, noext[i], false)) {
318 f2fs_disable_compressed_file(inode);
319 return;
320 }
321 }
322
323 if (is_inode_flag_set(inode, FI_COMPRESSED_FILE))
324 return;
Chao Yu4c8ff702019-11-01 18:07:14 +0800325
326 for (i = 0; i < ext_cnt; i++) {
Chao Yu4a67d9b2021-05-18 17:54:58 +0800327 if (!is_extension_exist(name, ext[i], false))
Chao Yu4c8ff702019-11-01 18:07:14 +0800328 continue;
329
330 set_compress_context(inode);
331 return;
332 }
333}
334
Christian Brauner549c7292021-01-21 14:19:43 +0100335static int f2fs_create(struct user_namespace *mnt_userns, struct inode *dir,
336 struct dentry *dentry, umode_t mode, bool excl)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900337{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700338 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900339 struct inode *inode;
340 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800341 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900342
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200343 if (unlikely(f2fs_cp_error(sbi)))
344 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800345 if (!f2fs_is_checkpoint_ready(sbi))
346 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200347
Chao Yu0abd6752017-07-09 00:13:07 +0800348 err = dquot_initialize(dir);
349 if (err)
350 return err;
351
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900352 inode = f2fs_new_inode(dir, mode);
353 if (IS_ERR(inode))
354 return PTR_ERR(inode);
355
356 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Chao Yub6a06cb2018-02-28 17:07:27 +0800357 set_file_temperature(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900358
Chao Yu4c8ff702019-11-01 18:07:14 +0800359 set_compress_inode(sbi, inode, dentry->d_name.name);
360
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361 inode->i_op = &f2fs_file_inode_operations;
362 inode->i_fop = &f2fs_file_operations;
363 inode->i_mapping->a_ops = &f2fs_dblock_aops;
364 ino = inode->i_ino;
365
Gu Zhenge4795562013-09-27 18:08:30 +0800366 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900367 err = f2fs_add_link(dentry, inode);
368 if (err)
369 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700370 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900371
Chao Yu4d57b862018-05-30 00:20:41 +0800372 f2fs_alloc_nid_done(sbi, ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900373
Al Viro1e2e5472018-05-04 08:23:01 -0400374 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800375
376 if (IS_DIRSYNC(dir))
377 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700378
379 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900380 return 0;
381out:
Chao Yu4d57b862018-05-30 00:20:41 +0800382 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900383 return err;
384}
385
386static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
387 struct dentry *dentry)
388{
David Howells2b0143b2015-03-17 22:25:59 +0000389 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700390 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800391 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900392
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200393 if (unlikely(f2fs_cp_error(sbi)))
394 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800395 if (!f2fs_is_checkpoint_ready(sbi))
396 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200397
Eric Biggersb05157e2017-11-29 12:35:29 -0800398 err = fscrypt_prepare_link(old_dentry, dir, dentry);
399 if (err)
400 return err;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700401
Chao Yu5c571322017-07-26 00:01:41 +0800402 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
403 (!projid_eq(F2FS_I(dir)->i_projid,
404 F2FS_I(old_dentry->d_inode)->i_projid)))
405 return -EXDEV;
406
Chao Yu0abd6752017-07-09 00:13:07 +0800407 err = dquot_initialize(dir);
408 if (err)
409 return err;
410
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800411 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900412
Deepa Dinamani078cd822016-09-14 07:48:04 -0700413 inode->i_ctime = current_time(inode);
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900414 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900415
Jaegeuk Kim91942322016-05-20 10:13:22 -0700416 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800417 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900418 err = f2fs_add_link(dentry, inode);
419 if (err)
420 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700421 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900422
423 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800424
425 if (IS_DIRSYNC(dir))
426 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900427 return 0;
428out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700429 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900430 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700431 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900432 return err;
433}
434
435struct dentry *f2fs_get_parent(struct dentry *child)
436{
Chao Yu91246c22016-07-19 08:27:47 +0800437 struct page *page;
Al Viro80e5d1ff52021-04-15 19:46:50 -0400438 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page);
Yi Zhuang5f029c02021-04-06 09:47:35 +0800439
Chao Yu91246c22016-07-19 08:27:47 +0800440 if (!ino) {
441 if (IS_ERR(page))
442 return ERR_CAST(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900443 return ERR_PTR(-ENOENT);
Chao Yu91246c22016-07-19 08:27:47 +0800444 }
Al Virofc640052016-04-10 01:33:30 -0400445 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900446}
447
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700448static int __recover_dot_dentries(struct inode *dir, nid_t pino)
449{
450 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
451 struct qstr dot = QSTR_INIT(".", 1);
452 struct qstr dotdot = QSTR_INIT("..", 2);
453 struct f2fs_dir_entry *de;
454 struct page *page;
455 int err = 0;
456
Chao Yu4e0d8362015-12-30 17:40:31 +0800457 if (f2fs_readonly(sbi->sb)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800458 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
459 dir->i_ino, pino);
Chao Yu4e0d8362015-12-30 17:40:31 +0800460 return 0;
461 }
462
Chao Yua6d3a472017-07-24 17:12:06 +0800463 err = dquot_initialize(dir);
464 if (err)
465 return err;
466
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800467 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800468
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700469 f2fs_lock_op(sbi);
470
471 de = f2fs_find_entry(dir, &dot, &page);
472 if (de) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700473 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700474 } else if (IS_ERR(page)) {
475 err = PTR_ERR(page);
476 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700477 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800478 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700479 if (err)
480 goto out;
481 }
482
483 de = f2fs_find_entry(dir, &dotdot, &page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800484 if (de)
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700485 f2fs_put_page(page, 0);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800486 else if (IS_ERR(page))
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700487 err = PTR_ERR(page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800488 else
Chao Yu4d57b862018-05-30 00:20:41 +0800489 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700490out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700491 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700492 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700493
494 f2fs_unlock_op(sbi);
495 return err;
496}
497
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900498static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
499 unsigned int flags)
500{
501 struct inode *inode = NULL;
502 struct f2fs_dir_entry *de;
503 struct page *page;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800504 struct dentry *new;
505 nid_t ino = -1;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700506 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000507 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Eric Biggers43c780b2020-05-07 00:59:04 -0700508 struct f2fs_filename fname;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900509
Chao Yu0c5e36d2017-10-17 17:33:41 +0800510 trace_f2fs_lookup_start(dir, dentry, flags);
511
Chao Yu0c5e36d2017-10-17 17:33:41 +0800512 if (dentry->d_name.len > F2FS_NAME_LEN) {
513 err = -ENAMETOOLONG;
514 goto out;
515 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900516
Eric Biggers43c780b2020-05-07 00:59:04 -0700517 err = f2fs_prepare_lookup(dir, dentry, &fname);
Daniel Rosenbergbb9cd912020-11-19 06:09:03 +0000518 generic_set_encrypted_ci_d_ops(dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -0700519 if (err == -ENOENT)
520 goto out_splice;
521 if (err)
522 goto out;
523 de = __f2fs_find_entry(dir, &fname, &page);
Eric Biggers43c780b2020-05-07 00:59:04 -0700524 f2fs_free_filename(&fname);
Eric Biggersb01531d2019-03-20 11:39:13 -0700525
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700526 if (!de) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800527 if (IS_ERR(page)) {
528 err = PTR_ERR(page);
529 goto out;
530 }
Chao Yu84597b12020-05-27 18:27:51 +0800531 err = -ENOENT;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800532 goto out_splice;
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700533 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900534
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700535 ino = le32_to_cpu(de->ino);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700536 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700537
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700538 inode = f2fs_iget(dir->i_sb, ino);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800539 if (IS_ERR(inode)) {
540 err = PTR_ERR(inode);
541 goto out;
542 }
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700543
Liu Xue8c2b1432016-02-26 06:39:23 +0000544 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
545 err = __recover_dot_dentries(dir, root_ino);
546 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800547 goto out_iput;
Liu Xue8c2b1432016-02-26 06:39:23 +0000548 }
549
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700550 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700551 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700552 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800553 goto out_iput;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900554 }
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530555 if (IS_ENCRYPTED(dir) &&
Dan Carpenter07fe8d42016-12-16 11:18:15 +0300556 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
557 !fscrypt_has_permitted_context(dir, inode)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800558 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
559 dir->i_ino, inode->i_ino);
Eric Biggersfaac7fd2017-04-07 10:58:39 -0700560 err = -EPERM;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800561 goto out_iput;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800562 }
Chao Yu0c5e36d2017-10-17 17:33:41 +0800563out_splice:
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700564#ifdef CONFIG_UNICODE
565 if (!inode && IS_CASEFOLDED(dir)) {
566 /* Eventually we want to call d_add_ci(dentry, NULL)
567 * for negative dentries in the encoding case as
568 * well. For now, prevent the negative dentry
569 * from being cached.
570 */
571 trace_f2fs_lookup_end(dir, dentry, ino, err);
572 return NULL;
573 }
574#endif
Chao Yu0c5e36d2017-10-17 17:33:41 +0800575 new = d_splice_alias(inode, dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -0700576 err = PTR_ERR_OR_ZERO(new);
Chao Yu84597b12020-05-27 18:27:51 +0800577 trace_f2fs_lookup_end(dir, dentry, ino, !new ? -ENOENT : err);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800578 return new;
579out_iput:
Chao Yud7267322016-03-10 22:24:23 +0800580 iput(inode);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800581out:
582 trace_f2fs_lookup_end(dir, dentry, ino, err);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700583 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900584}
585
586static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
587{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700588 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000589 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900590 struct f2fs_dir_entry *de;
591 struct page *page;
Colin Ian Kingdeaf1602020-04-20 23:00:57 +0100592 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900593
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900594 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900595
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800596 if (unlikely(f2fs_cp_error(sbi))) {
597 err = -EIO;
598 goto fail;
599 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200600
Chao Yu0abd6752017-07-09 00:13:07 +0800601 err = dquot_initialize(dir);
602 if (err)
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800603 goto fail;
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200604 err = dquot_initialize(inode);
605 if (err)
Lihong Kou9a99c17d2020-06-20 10:12:17 +0800606 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +0800607
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900608 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800609 if (!de) {
610 if (IS_ERR(page))
611 err = PTR_ERR(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900612 goto fail;
Chao Yu91246c22016-07-19 08:27:47 +0800613 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900614
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800615 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800616
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900617 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800618 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900619 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900620 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900621 f2fs_put_page(page, 0);
622 goto fail;
623 }
Chao Yudbeacf02014-09-24 18:17:04 +0800624 f2fs_delete_entry(de, page, dir, inode);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700625#ifdef CONFIG_UNICODE
626 /* VFS negative dentries are incompatible with Encoding and
627 * Case-insensitiveness. Eventually we'll want avoid
628 * invalidating the dentries here, alongside with returning the
Jack Qiua87aff12020-07-24 16:55:28 +0800629 * negative dentries at f2fs_lookup(), when it is better
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700630 * supported by the VFS for the CI case.
631 */
632 if (IS_CASEFOLDED(dir))
633 d_invalidate(dentry);
634#endif
Gu Zhenge4795562013-09-27 18:08:30 +0800635 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900636
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800637 if (IS_DIRSYNC(dir))
638 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900639fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900640 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900641 return err;
642}
643
Al Viro6b255392015-11-17 10:20:54 -0500644static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500645 struct inode *inode,
646 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700647{
Al Virofceef392015-12-29 15:58:39 -0500648 const char *link = page_get_link(dentry, inode, done);
Yi Zhuang5f029c02021-04-06 09:47:35 +0800649
Al Viro680baac2015-05-02 13:32:22 -0400650 if (!IS_ERR(link) && !*link) {
651 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500652 do_delayed_call(done);
653 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400654 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700655 }
Al Viro680baac2015-05-02 13:32:22 -0400656 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700657}
658
Christian Brauner549c7292021-01-21 14:19:43 +0100659static int f2fs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
660 struct dentry *dentry, const char *symname)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900661{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700662 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900663 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700664 size_t len = strlen(symname);
Eric Biggers393c0382018-01-11 23:26:49 -0500665 struct fscrypt_str disk_link;
Gu Zhenge4795562013-09-27 18:08:30 +0800666 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900667
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200668 if (unlikely(f2fs_cp_error(sbi)))
669 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800670 if (!f2fs_is_checkpoint_ready(sbi))
671 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200672
Eric Biggers393c0382018-01-11 23:26:49 -0500673 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
674 &disk_link);
675 if (err)
676 return err;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700677
Chao Yu0abd6752017-07-09 00:13:07 +0800678 err = dquot_initialize(dir);
679 if (err)
680 return err;
681
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900682 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
683 if (IS_ERR(inode))
684 return PTR_ERR(inode);
685
Eric Biggers393c0382018-01-11 23:26:49 -0500686 if (IS_ENCRYPTED(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700687 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
688 else
689 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500690 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900691 inode->i_mapping->a_ops = &f2fs_dblock_aops;
692
Gu Zhenge4795562013-09-27 18:08:30 +0800693 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900694 err = f2fs_add_link(dentry, inode);
695 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800696 goto out_f2fs_handle_failed_inode;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700697 f2fs_unlock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800698 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900699
Eric Biggers393c0382018-01-11 23:26:49 -0500700 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
701 if (err)
702 goto err_out;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700703
Chao Yu922ec352016-02-15 17:54:26 +0800704 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700705
706err_out:
Al Viro1e2e5472018-05-04 08:23:01 -0400707 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800708
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700709 /*
710 * Let's flush symlink data in order to avoid broken symlink as much as
711 * possible. Nevertheless, fsyncing is the best way, but there is no
712 * way to get a file descriptor in order to flush that.
713 *
714 * Note that, it needs to do dir->fsync to make this recoverable.
715 * If the symlink path is stored into inline_data, there is no
716 * performance regression.
717 */
Chao Yua6be0142015-10-22 18:23:08 +0800718 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800719 filemap_write_and_wait_range(inode->i_mapping, 0,
720 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700721
Chao Yua6be0142015-10-22 18:23:08 +0800722 if (IS_DIRSYNC(dir))
723 f2fs_sync_fs(sbi->sb, 1);
724 } else {
725 f2fs_unlink(dir, dentry);
726 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700727
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700728 f2fs_balance_fs(sbi, true);
Eric Biggers393c0382018-01-11 23:26:49 -0500729 goto out_free_encrypted_link;
730
Chao Yu4d57b862018-05-30 00:20:41 +0800731out_f2fs_handle_failed_inode:
732 f2fs_handle_failed_inode(inode);
Eric Biggers393c0382018-01-11 23:26:49 -0500733out_free_encrypted_link:
734 if (disk_link.name != (unsigned char *)symname)
Chao Yuc8eb7022020-09-14 16:47:00 +0800735 kfree(disk_link.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900736 return err;
737}
738
Christian Brauner549c7292021-01-21 14:19:43 +0100739static int f2fs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
740 struct dentry *dentry, umode_t mode)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700742 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900743 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800744 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900745
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200746 if (unlikely(f2fs_cp_error(sbi)))
747 return -EIO;
748
Chao Yu0abd6752017-07-09 00:13:07 +0800749 err = dquot_initialize(dir);
750 if (err)
751 return err;
752
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900753 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900754 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900755 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900756
757 inode->i_op = &f2fs_dir_inode_operations;
758 inode->i_fop = &f2fs_dir_operations;
759 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800760 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900761
Jaegeuk Kim91942322016-05-20 10:13:22 -0700762 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800763 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900764 err = f2fs_add_link(dentry, inode);
765 if (err)
766 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700767 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900768
Chao Yu4d57b862018-05-30 00:20:41 +0800769 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900770
Al Viro1e2e5472018-05-04 08:23:01 -0400771 d_instantiate_new(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900772
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800773 if (IS_DIRSYNC(dir))
774 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700775
776 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900777 return 0;
778
779out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700780 clear_inode_flag(inode, FI_INC_LINK);
Chao Yu4d57b862018-05-30 00:20:41 +0800781 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900782 return err;
783}
784
785static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
786{
David Howells2b0143b2015-03-17 22:25:59 +0000787 struct inode *inode = d_inode(dentry);
Yi Zhuang5f029c02021-04-06 09:47:35 +0800788
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900789 if (f2fs_empty_dir(inode))
790 return f2fs_unlink(dir, dentry);
791 return -ENOTEMPTY;
792}
793
Christian Brauner549c7292021-01-21 14:19:43 +0100794static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
795 struct dentry *dentry, umode_t mode, dev_t rdev)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900796{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700797 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900798 struct inode *inode;
799 int err = 0;
800
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200801 if (unlikely(f2fs_cp_error(sbi)))
802 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800803 if (!f2fs_is_checkpoint_ready(sbi))
804 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200805
Chao Yu0abd6752017-07-09 00:13:07 +0800806 err = dquot_initialize(dir);
807 if (err)
808 return err;
809
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900810 inode = f2fs_new_inode(dir, mode);
811 if (IS_ERR(inode))
812 return PTR_ERR(inode);
813
814 init_special_inode(inode, inode->i_mode, rdev);
815 inode->i_op = &f2fs_special_inode_operations;
816
Gu Zhenge4795562013-09-27 18:08:30 +0800817 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900818 err = f2fs_add_link(dentry, inode);
819 if (err)
820 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700821 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900822
Chao Yu4d57b862018-05-30 00:20:41 +0800823 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800824
Al Viro1e2e5472018-05-04 08:23:01 -0400825 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800826
827 if (IS_DIRSYNC(dir))
828 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700829
830 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900831 return 0;
832out:
Chao Yu4d57b862018-05-30 00:20:41 +0800833 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900834 return err;
835}
836
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800837static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
838 umode_t mode, struct inode **whiteout)
839{
840 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
841 struct inode *inode;
842 int err;
843
Chao Yu0abd6752017-07-09 00:13:07 +0800844 err = dquot_initialize(dir);
845 if (err)
846 return err;
847
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800848 inode = f2fs_new_inode(dir, mode);
849 if (IS_ERR(inode))
850 return PTR_ERR(inode);
851
852 if (whiteout) {
853 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
854 inode->i_op = &f2fs_special_inode_operations;
855 } else {
856 inode->i_op = &f2fs_file_inode_operations;
857 inode->i_fop = &f2fs_file_operations;
858 inode->i_mapping->a_ops = &f2fs_dblock_aops;
859 }
860
861 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800862 err = f2fs_acquire_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800863 if (err)
864 goto out;
865
866 err = f2fs_do_tmpfile(inode, dir);
867 if (err)
868 goto release_out;
869
870 /*
871 * add this non-linked tmpfile to orphan list, in this way we could
872 * remove all unused data of tmpfile after abnormal power-off.
873 */
Chao Yu4d57b862018-05-30 00:20:41 +0800874 f2fs_add_orphan_inode(inode);
875 f2fs_alloc_nid_done(sbi, inode->i_ino);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800876
877 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700878 f2fs_i_links_write(inode, false);
Chao Yu46085f32021-01-12 09:55:09 +0800879
880 spin_lock(&inode->i_lock);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800881 inode->i_state |= I_LINKABLE;
Chao Yu46085f32021-01-12 09:55:09 +0800882 spin_unlock(&inode->i_lock);
883
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800884 *whiteout = inode;
885 } else {
886 d_tmpfile(dentry, inode);
887 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700888 /* link_count was changed by d_tmpfile as well. */
889 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800890 unlock_new_inode(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700891
892 f2fs_balance_fs(sbi, true);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800893 return 0;
894
895release_out:
Chao Yu4d57b862018-05-30 00:20:41 +0800896 f2fs_release_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800897out:
Chao Yu4d57b862018-05-30 00:20:41 +0800898 f2fs_handle_failed_inode(inode);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800899 return err;
900}
901
Christian Brauner549c7292021-01-21 14:19:43 +0100902static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
903 struct dentry *dentry, umode_t mode)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800904{
Sheng Yongff62af22018-03-15 18:51:42 +0800905 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
906
907 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200908 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800909 if (!f2fs_is_checkpoint_ready(sbi))
910 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200911
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800912 return __f2fs_tmpfile(dir, dentry, mode, NULL);
913}
914
915static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
916{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200917 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
918 return -EIO;
919
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800920 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
921}
922
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900923static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800924 struct inode *new_dir, struct dentry *new_dentry,
925 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900926{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700927 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000928 struct inode *old_inode = d_inode(old_dentry);
929 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800930 struct inode *whiteout = NULL;
Jaegeuk Kim762e4db2019-12-11 15:10:47 -0800931 struct page *old_dir_page = NULL;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800932 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900933 struct f2fs_dir_entry *old_dir_entry = NULL;
934 struct f2fs_dir_entry *old_entry;
935 struct f2fs_dir_entry *new_entry;
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700936 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900937
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200938 if (unlikely(f2fs_cp_error(sbi)))
939 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800940 if (!f2fs_is_checkpoint_ready(sbi))
941 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200942
Chao Yu5c571322017-07-26 00:01:41 +0800943 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
944 (!projid_eq(F2FS_I(new_dir)->i_projid,
945 F2FS_I(old_dentry->d_inode)->i_projid)))
946 return -EXDEV;
947
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800948 /*
949 * If new_inode is null, the below renaming flow will
950 * add a link in old_dir which can conver inline_dir.
951 * After then, if we failed to get the entry due to other
952 * reasons like ENOMEM, we had to remove the new entry.
953 * Instead of adding such the error handling routine, let's
954 * simply convert first here.
955 */
956 if (old_dir == new_dir && !new_inode) {
957 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
958 if (err)
959 return err;
960 }
961
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -0800962 if (flags & RENAME_WHITEOUT) {
963 err = f2fs_create_whiteout(old_dir, &whiteout);
964 if (err)
965 return err;
966 }
967
Chao Yu0abd6752017-07-09 00:13:07 +0800968 err = dquot_initialize(old_dir);
969 if (err)
970 goto out;
971
972 err = dquot_initialize(new_dir);
973 if (err)
974 goto out;
975
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200976 if (new_inode) {
977 err = dquot_initialize(new_inode);
978 if (err)
979 goto out;
980 }
981
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700982 err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900983 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +0800984 if (!old_entry) {
985 if (IS_ERR(old_page))
986 err = PTR_ERR(old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900987 goto out;
Chao Yu91246c22016-07-19 08:27:47 +0800988 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900989
990 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900991 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700992 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800993 if (IS_ERR(old_dir_page))
994 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900995 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700996 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900997 }
998
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900999 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001000
1001 err = -ENOTEMPTY;
1002 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001003 goto out_dir;
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001004
1005 err = -ENOENT;
1006 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1007 &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001008 if (!new_entry) {
1009 if (IS_ERR(new_page))
1010 err = PTR_ERR(new_page);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001011 goto out_dir;
Chao Yu91246c22016-07-19 08:27:47 +08001012 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001013
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001014 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001015
Chao Yu12560102014-06-24 14:16:24 +08001016 f2fs_lock_op(sbi);
1017
Chao Yu4d57b862018-05-30 00:20:41 +08001018 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001019 if (err)
1020 goto put_out_dir;
1021
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001022 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001023 new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001024
Deepa Dinamani078cd822016-09-14 07:48:04 -07001025 new_inode->i_ctime = current_time(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +09001026 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001027 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -07001028 f2fs_i_links_write(new_inode, false);
1029 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +09001030 up_write(&F2FS_I(new_inode)->i_sem);
1031
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001032 if (!new_inode->i_nlink)
Chao Yu4d57b862018-05-30 00:20:41 +08001033 f2fs_add_orphan_inode(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001034 else
Chao Yu4d57b862018-05-30 00:20:41 +08001035 f2fs_release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001036 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001037 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001038
Chao Yu12560102014-06-24 14:16:24 +08001039 f2fs_lock_op(sbi);
1040
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001041 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +08001042 if (err) {
1043 f2fs_unlock_op(sbi);
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001044 goto out_dir;
Chao Yu12560102014-06-24 14:16:24 +08001045 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001046
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001047 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -07001048 f2fs_i_links_write(new_dir, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001049 }
1050
Jaegeuk Kimb2c08292014-06-30 18:09:55 +09001051 down_write(&F2FS_I(old_inode)->i_sem);
Sheng Yongb855bf02017-06-26 10:41:36 +08001052 if (!old_dir_entry || whiteout)
1053 file_lost_pino(old_inode);
1054 else
Chao Yu2a606372019-11-07 14:12:05 +08001055 /* adjust dir's i_pino to pass fsck check */
1056 f2fs_i_pino_write(old_inode, new_dir->i_ino);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +09001057 up_write(&F2FS_I(old_inode)->i_sem);
1058
Deepa Dinamani078cd822016-09-14 07:48:04 -07001059 old_inode->i_ctime = current_time(old_inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001060 f2fs_mark_inode_dirty_sync(old_inode, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001061
Chao Yudbeacf02014-09-24 18:17:04 +08001062 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001063 old_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001064
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001065 if (whiteout) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001066 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001067 err = f2fs_add_link(old_dentry, whiteout);
1068 if (err)
1069 goto put_out_dir;
Chao Yu46085f32021-01-12 09:55:09 +08001070
1071 spin_lock(&whiteout->i_lock);
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001072 whiteout->i_state &= ~I_LINKABLE;
Chao Yu46085f32021-01-12 09:55:09 +08001073 spin_unlock(&whiteout->i_lock);
1074
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001075 iput(whiteout);
1076 }
1077
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001078 if (old_dir_entry) {
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001079 if (old_dir != new_dir && !whiteout)
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001080 f2fs_set_link(old_inode, old_dir_entry,
1081 old_dir_page, new_dir);
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001082 else
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001083 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001084 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001085 }
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001086 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001087 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001088 if (S_ISDIR(old_inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001089 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1090 TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -06001091 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001092
Gu Zhenge4795562013-09-27 18:08:30 +08001093 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001094
1095 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1096 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301097
1098 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001099 return 0;
1100
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001101put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +08001102 f2fs_unlock_op(sbi);
Jaegeuk Kim762e4db2019-12-11 15:10:47 -08001103 f2fs_put_page(new_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001104out_dir:
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001105 if (old_dir_entry)
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001106 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001107out_old:
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001108 f2fs_put_page(old_page, 0);
1109out:
Jaegeuk Kim5b1dbb02019-12-06 16:59:58 -08001110 if (whiteout)
1111 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001112 return err;
1113}
1114
Chao Yu32f9bc22014-07-12 19:13:54 +08001115static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1116 struct inode *new_dir, struct dentry *new_dentry)
1117{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001118 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +00001119 struct inode *old_inode = d_inode(old_dentry);
1120 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +08001121 struct page *old_dir_page, *new_dir_page;
1122 struct page *old_page, *new_page;
1123 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1124 struct f2fs_dir_entry *old_entry, *new_entry;
1125 int old_nlink = 0, new_nlink = 0;
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001126 int err;
Chao Yu32f9bc22014-07-12 19:13:54 +08001127
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001128 if (unlikely(f2fs_cp_error(sbi)))
1129 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001130 if (!f2fs_is_checkpoint_ready(sbi))
1131 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001132
Chao Yu5c571322017-07-26 00:01:41 +08001133 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1134 !projid_eq(F2FS_I(new_dir)->i_projid,
1135 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1136 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1137 !projid_eq(F2FS_I(old_dir)->i_projid,
1138 F2FS_I(new_dentry->d_inode)->i_projid)))
1139 return -EXDEV;
1140
Chao Yu0abd6752017-07-09 00:13:07 +08001141 err = dquot_initialize(old_dir);
1142 if (err)
1143 goto out;
1144
1145 err = dquot_initialize(new_dir);
1146 if (err)
1147 goto out;
1148
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001149 err = -ENOENT;
Chao Yu32f9bc22014-07-12 19:13:54 +08001150 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +08001151 if (!old_entry) {
1152 if (IS_ERR(old_page))
1153 err = PTR_ERR(old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001154 goto out;
Chao Yu91246c22016-07-19 08:27:47 +08001155 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001156
1157 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001158 if (!new_entry) {
1159 if (IS_ERR(new_page))
1160 err = PTR_ERR(new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001161 goto out_old;
Chao Yu91246c22016-07-19 08:27:47 +08001162 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001163
1164 /* prepare for updating ".." directory entry info later */
1165 if (old_dir != new_dir) {
1166 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001167 old_dir_entry = f2fs_parent_dir(old_inode,
1168 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001169 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001170 if (IS_ERR(old_dir_page))
1171 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001172 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001173 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001174 }
1175
1176 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001177 new_dir_entry = f2fs_parent_dir(new_inode,
1178 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001179 if (!new_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001180 if (IS_ERR(new_dir_page))
1181 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001182 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001183 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001184 }
1185 }
1186
1187 /*
1188 * If cross rename between file and directory those are not
1189 * in the same directory, we will inc nlink of file's parent
1190 * later, so we should check upper boundary of its nlink.
1191 */
1192 if ((!old_dir_entry || !new_dir_entry) &&
1193 old_dir_entry != new_dir_entry) {
1194 old_nlink = old_dir_entry ? -1 : 1;
1195 new_nlink = -old_nlink;
1196 err = -EMLINK;
Kinglong Meee2f0e962017-03-04 21:48:28 +08001197 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1198 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
Chao Yu32f9bc22014-07-12 19:13:54 +08001199 goto out_new_dir;
1200 }
1201
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001202 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001203
Chao Yu32f9bc22014-07-12 19:13:54 +08001204 f2fs_lock_op(sbi);
1205
Chao Yu32f9bc22014-07-12 19:13:54 +08001206 /* update ".." directory entry info of old dentry */
1207 if (old_dir_entry)
1208 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1209
1210 /* update ".." directory entry info of new dentry */
1211 if (new_dir_entry)
1212 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1213
1214 /* update directory entry info of old dir inode */
1215 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1216
1217 down_write(&F2FS_I(old_inode)->i_sem);
Chao Yu2a606372019-11-07 14:12:05 +08001218 if (!old_dir_entry)
1219 file_lost_pino(old_inode);
1220 else
1221 /* adjust dir's i_pino to pass fsck check */
1222 f2fs_i_pino_write(old_inode, new_dir->i_ino);
Chao Yu32f9bc22014-07-12 19:13:54 +08001223 up_write(&F2FS_I(old_inode)->i_sem);
1224
Deepa Dinamani078cd822016-09-14 07:48:04 -07001225 old_dir->i_ctime = current_time(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001226 if (old_nlink) {
1227 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001228 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001229 up_write(&F2FS_I(old_dir)->i_sem);
1230 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001231 f2fs_mark_inode_dirty_sync(old_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001232
1233 /* update directory entry info of new dir inode */
1234 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1235
1236 down_write(&F2FS_I(new_inode)->i_sem);
Chao Yu2a606372019-11-07 14:12:05 +08001237 if (!new_dir_entry)
1238 file_lost_pino(new_inode);
1239 else
1240 /* adjust dir's i_pino to pass fsck check */
1241 f2fs_i_pino_write(new_inode, old_dir->i_ino);
Chao Yu32f9bc22014-07-12 19:13:54 +08001242 up_write(&F2FS_I(new_inode)->i_sem);
1243
Deepa Dinamani078cd822016-09-14 07:48:04 -07001244 new_dir->i_ctime = current_time(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001245 if (new_nlink) {
1246 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001247 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001248 up_write(&F2FS_I(new_dir)->i_sem);
1249 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001250 f2fs_mark_inode_dirty_sync(new_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001251
Chao Yu63189b72018-03-08 14:22:56 +08001252 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001253 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1254 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Junling Zheng93cf93f2018-03-07 12:07:49 +08001255 }
Jaegeuk Kim0a007b972017-12-28 08:09:44 -08001256
Chao Yu32f9bc22014-07-12 19:13:54 +08001257 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001258
1259 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1260 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301261
1262 f2fs_update_time(sbi, REQ_TIME);
Chao Yu32f9bc22014-07-12 19:13:54 +08001263 return 0;
Chao Yu32f9bc22014-07-12 19:13:54 +08001264out_new_dir:
1265 if (new_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001266 f2fs_put_page(new_dir_page, 0);
1267 }
1268out_old_dir:
1269 if (old_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001270 f2fs_put_page(old_dir_page, 0);
1271 }
1272out_new:
Chao Yu32f9bc22014-07-12 19:13:54 +08001273 f2fs_put_page(new_page, 0);
1274out_old:
Chao Yu32f9bc22014-07-12 19:13:54 +08001275 f2fs_put_page(old_page, 0);
1276out:
1277 return err;
1278}
1279
Christian Brauner549c7292021-01-21 14:19:43 +01001280static int f2fs_rename2(struct user_namespace *mnt_userns,
1281 struct inode *old_dir, struct dentry *old_dentry,
Chao Yu32f9bc22014-07-12 19:13:54 +08001282 struct inode *new_dir, struct dentry *new_dentry,
1283 unsigned int flags)
1284{
Eric Biggers2e45b072017-11-29 12:35:30 -08001285 int err;
1286
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001287 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +08001288 return -EINVAL;
1289
Eric Biggers2e45b072017-11-29 12:35:30 -08001290 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1291 flags);
1292 if (err)
1293 return err;
1294
Chao Yu32f9bc22014-07-12 19:13:54 +08001295 if (flags & RENAME_EXCHANGE) {
1296 return f2fs_cross_rename(old_dir, old_dentry,
1297 new_dir, new_dentry);
1298 }
1299 /*
1300 * VFS has already handled the new dentry existence case,
1301 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1302 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001303 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +08001304}
1305
Al Viro6b255392015-11-17 10:20:54 -05001306static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001307 struct inode *inode,
1308 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001309{
Eric Biggersf2329cb2018-01-11 23:26:49 -05001310 struct page *page;
1311 const char *target;
Chao Yu50732df2014-06-19 16:23:19 +08001312
Al Viro6b255392015-11-17 10:20:54 -05001313 if (!dentry)
1314 return ERR_PTR(-ECHILD);
1315
Eric Biggersf2329cb2018-01-11 23:26:49 -05001316 page = read_mapping_page(inode->i_mapping, 0, NULL);
1317 if (IS_ERR(page))
1318 return ERR_CAST(page);
Chao Yu50732df2014-06-19 16:23:19 +08001319
Eric Biggersf2329cb2018-01-11 23:26:49 -05001320 target = fscrypt_get_symlink(inode, page_address(page),
1321 inode->i_sb->s_blocksize, done);
1322 put_page(page);
1323 return target;
Chao Yu50732df2014-06-19 16:23:19 +08001324}
1325
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001326const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
Jack Qiua87aff12020-07-24 16:55:28 +08001327 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001328 .getattr = f2fs_getattr,
1329 .setattr = f2fs_setattr,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001330 .listxattr = f2fs_listxattr,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001331};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001332
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001333const struct inode_operations f2fs_dir_inode_operations = {
1334 .create = f2fs_create,
1335 .lookup = f2fs_lookup,
1336 .link = f2fs_link,
1337 .unlink = f2fs_unlink,
1338 .symlink = f2fs_symlink,
1339 .mkdir = f2fs_mkdir,
1340 .rmdir = f2fs_rmdir,
1341 .mknod = f2fs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001342 .rename = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001343 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001344 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001345 .setattr = f2fs_setattr,
1346 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001347 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001348 .listxattr = f2fs_listxattr,
Chao Yu7975f342019-07-22 18:03:50 +08001349 .fiemap = f2fs_fiemap,
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02001350 .fileattr_get = f2fs_fileattr_get,
1351 .fileattr_set = f2fs_fileattr_set,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001352};
1353
1354const struct inode_operations f2fs_symlink_inode_operations = {
Jack Qiua87aff12020-07-24 16:55:28 +08001355 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001356 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001357 .setattr = f2fs_setattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001358 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001359};
1360
1361const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001362 .getattr = f2fs_getattr,
Jack Qiua87aff12020-07-24 16:55:28 +08001363 .setattr = f2fs_setattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001364 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001365 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001366 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001367};