blob: c3e8a901d47ac27476b6a49dd9dfd173412b8882 [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;
Chao Yu6afc6622017-09-06 21:59:50 +080031 int xattr_size = 0;
Gu Zhenge4795562013-09-27 18:08:30 +080032 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090033
Jaegeuk Kima014e032014-06-20 21:44:02 -070034 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090035 if (!inode)
36 return ERR_PTR(-ENOMEM);
37
Gu Zhenge4795562013-09-27 18:08:30 +080038 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +080039 if (!f2fs_alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080040 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090041 err = -ENOSPC;
42 goto fail;
43 }
Gu Zhenge4795562013-09-27 18:08:30 +080044 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090045
Chao Yu0abd6752017-07-09 00:13:07 +080046 nid_free = true;
47
Chao Yu70ff5df2014-05-08 17:09:30 +080048 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090049
50 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090051 inode->i_blocks = 0;
Deepa Dinamani95582b02018-05-08 19:36:02 -070052 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Arnd Bergmann24b81df2018-06-20 10:02:19 +020053 F2FS_I(inode)->i_crtime = inode->i_mtime;
Jaegeuk Kim428e3bc2019-02-25 09:46:45 -080054 inode->i_generation = prandom_u32();
Jaegeuk Kim57397d82012-11-02 17:11:10 +090055
Chao Yu1c41e6802018-05-07 20:28:52 +080056 if (S_ISDIR(inode->i_mode))
57 F2FS_I(inode)->i_current_depth = 1;
58
Jaegeuk Kim57397d82012-11-02 17:11:10 +090059 err = insert_inode_locked(inode);
60 if (err) {
61 err = -EINVAL;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070062 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090063 }
Chao Yu622f28a2014-09-24 18:19:10 +080064
Chao Yu7beb01f2018-10-24 18:34:26 +080065 if (f2fs_sb_has_project_quota(sbi) &&
Chao Yu59c84402018-04-03 15:08:17 +080066 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
Chao Yu5c571322017-07-26 00:01:41 +080067 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
68 else
69 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
70 F2FS_DEF_PROJID);
71
Chao Yu0abd6752017-07-09 00:13:07 +080072 err = dquot_initialize(inode);
73 if (err)
74 goto fail_drop;
75
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090076 set_inode_flag(inode, FI_NEW_INODE);
77
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070078 /* If the directory encrypted, then we should encrypt the inode. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +053079 if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
Sheng Yongff62af22018-03-15 18:51:42 +080080 f2fs_may_encrypt(inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070081 f2fs_set_encrypted_inode(inode);
82
Chao Yu7beb01f2018-10-24 18:34:26 +080083 if (f2fs_sb_has_extra_attr(sbi)) {
Chao Yu7a2af762017-07-19 00:19:06 +080084 set_inode_flag(inode, FI_EXTRA_ATTR);
85 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
86 }
87
Jaegeuk Kim91942322016-05-20 10:13:22 -070088 if (test_opt(sbi, INLINE_XATTR))
89 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yu6afc6622017-09-06 21:59:50 +080090
Chao Yufba48a82015-12-23 17:51:35 +080091 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070092 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070093 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070094 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080095
Chao Yu7beb01f2018-10-24 18:34:26 +080096 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
Chao Yu6afc6622017-09-06 21:59:50 +080097 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
98 if (f2fs_has_inline_xattr(inode))
Chao Yu63189b72018-03-08 14:22:56 +080099 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
Chao Yu6afc6622017-09-06 21:59:50 +0800100 /* Otherwise, will be 0 */
101 } else if (f2fs_has_inline_xattr(inode) ||
102 f2fs_has_inline_dentry(inode)) {
103 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
104 }
105 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
106
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700107 f2fs_init_extent_tree(inode, NULL);
108
Chao Yud5e8f6c2015-07-15 17:28:53 +0800109 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -0700110 stat_inc_inline_inode(inode);
111 stat_inc_inline_dir(inode);
112
Chao Yu5c571322017-07-26 00:01:41 +0800113 F2FS_I(inode)->i_flags =
114 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
115
Chao Yu11f50202017-08-30 18:04:47 +0800116 if (S_ISDIR(inode->i_mode))
Chao Yu59c84402018-04-03 15:08:17 +0800117 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
Chao Yu11f50202017-08-30 18:04:47 +0800118
Chao Yu59c84402018-04-03 15:08:17 +0800119 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800120 set_inode_flag(inode, FI_PROJ_INHERIT);
121
Chao Yu9149a5e2018-10-07 19:06:15 +0800122 f2fs_set_inode_flags(inode);
123
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900124 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900125 return inode;
126
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900127fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +0900128 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900129 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900130 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700131 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700132 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900133 return ERR_PTR(err);
Chao Yu0abd6752017-07-09 00:13:07 +0800134fail_drop:
135 trace_f2fs_new_inode(inode, err);
136 dquot_drop(inode);
137 inode->i_flags |= S_NOQUOTA;
138 if (nid_free)
139 set_inode_flag(inode, FI_FREE_NID);
140 clear_nlink(inode);
141 unlock_new_inode(inode);
142 iput(inode);
143 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900144}
145
Chao Yub6a06cb2018-02-28 17:07:27 +0800146static int is_extension_exist(const unsigned char *s, const char *sub)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900147{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200148 size_t slen = strlen(s);
149 size_t sublen = strlen(sub);
Chao Yu7732c262016-09-05 12:28:27 +0800150 int i;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900151
Chao Yu741a7be2015-07-06 20:30:40 +0800152 /*
153 * filename format of multimedia file should be defined as:
Chao Yu7732c262016-09-05 12:28:27 +0800154 * "filename + '.' + extension + (optional: '.' + temp extension)".
Chao Yu741a7be2015-07-06 20:30:40 +0800155 */
156 if (slen < sublen + 2)
157 return 0;
158
Chao Yu7732c262016-09-05 12:28:27 +0800159 for (i = 1; i < slen - sublen; i++) {
160 if (s[i] != '.')
161 continue;
162 if (!strncasecmp(s + i + 1, sub, sublen))
163 return 1;
164 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900165
Chao Yu7732c262016-09-05 12:28:27 +0800166 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900167}
168
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900169/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900170 * Set multimedia files as cold files for hot/cold data separation
171 */
Chao Yub6a06cb2018-02-28 17:07:27 +0800172static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900173 const unsigned char *name)
174{
Chao Yu846ae672018-02-26 22:04:13 +0800175 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800176 int i, cold_count, hot_count;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900177
Chao Yu846ae672018-02-26 22:04:13 +0800178 down_read(&sbi->sb_lock);
179
Chao Yub6a06cb2018-02-28 17:07:27 +0800180 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
181 hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800182
Chao Yub6a06cb2018-02-28 17:07:27 +0800183 for (i = 0; i < cold_count + hot_count; i++) {
Chao Yued15ba12018-10-04 11:15:18 +0800184 if (is_extension_exist(name, extlist[i]))
185 break;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186 }
Chao Yu846ae672018-02-26 22:04:13 +0800187
188 up_read(&sbi->sb_lock);
Chao Yued15ba12018-10-04 11:15:18 +0800189
190 if (i == cold_count + hot_count)
191 return;
192
193 if (i < cold_count)
194 file_set_cold(inode);
195 else
196 file_set_hot(inode);
Chao Yu846ae672018-02-26 22:04:13 +0800197}
198
Chao Yu4d57b862018-05-30 00:20:41 +0800199int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
Chao Yub6a06cb2018-02-28 17:07:27 +0800200 bool hot, bool set)
Chao Yu846ae672018-02-26 22:04:13 +0800201{
202 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800203 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
204 int hot_count = sbi->raw_super->hot_ext_count;
205 int total_count = cold_count + hot_count;
206 int start, count;
Chao Yu846ae672018-02-26 22:04:13 +0800207 int i;
208
Chao Yub6a06cb2018-02-28 17:07:27 +0800209 if (set) {
210 if (total_count == F2FS_MAX_EXTENSION)
211 return -EINVAL;
212 } else {
213 if (!hot && !cold_count)
214 return -EINVAL;
215 if (hot && !hot_count)
216 return -EINVAL;
217 }
218
219 if (hot) {
220 start = cold_count;
221 count = total_count;
222 } else {
223 start = 0;
224 count = cold_count;
225 }
226
227 for (i = start; i < count; i++) {
Chao Yu846ae672018-02-26 22:04:13 +0800228 if (strcmp(name, extlist[i]))
229 continue;
230
231 if (set)
232 return -EINVAL;
233
234 memcpy(extlist[i], extlist[i + 1],
Chao Yub6a06cb2018-02-28 17:07:27 +0800235 F2FS_EXTENSION_LEN * (total_count - i - 1));
236 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
237 if (hot)
238 sbi->raw_super->hot_ext_count = hot_count - 1;
239 else
240 sbi->raw_super->extension_count =
241 cpu_to_le32(cold_count - 1);
Chao Yu846ae672018-02-26 22:04:13 +0800242 return 0;
243 }
244
245 if (!set)
246 return -EINVAL;
247
Chao Yub6a06cb2018-02-28 17:07:27 +0800248 if (hot) {
Guenter Roeckb1385472018-07-01 13:57:06 -0700249 memcpy(extlist[count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800250 sbi->raw_super->hot_ext_count = hot_count + 1;
251 } else {
252 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
Chao Yu846ae672018-02-26 22:04:13 +0800253
Chao Yub6a06cb2018-02-28 17:07:27 +0800254 memcpy(buf, &extlist[cold_count],
255 F2FS_EXTENSION_LEN * hot_count);
256 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
Guenter Roeckb1385472018-07-01 13:57:06 -0700257 memcpy(extlist[cold_count], name, strlen(name));
Chao Yub6a06cb2018-02-28 17:07:27 +0800258 memcpy(&extlist[cold_count + 1], buf,
259 F2FS_EXTENSION_LEN * hot_count);
260 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
261 }
Chao Yu846ae672018-02-26 22:04:13 +0800262 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900263}
264
265static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
266 bool excl)
267{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700268 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900269 struct inode *inode;
270 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800271 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900272
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200273 if (unlikely(f2fs_cp_error(sbi)))
274 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700275 err = f2fs_is_checkpoint_ready(sbi);
276 if (err)
277 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200278
Chao Yu0abd6752017-07-09 00:13:07 +0800279 err = dquot_initialize(dir);
280 if (err)
281 return err;
282
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900283 inode = f2fs_new_inode(dir, mode);
284 if (IS_ERR(inode))
285 return PTR_ERR(inode);
286
287 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Chao Yub6a06cb2018-02-28 17:07:27 +0800288 set_file_temperature(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900289
290 inode->i_op = &f2fs_file_inode_operations;
291 inode->i_fop = &f2fs_file_operations;
292 inode->i_mapping->a_ops = &f2fs_dblock_aops;
293 ino = inode->i_ino;
294
Gu Zhenge4795562013-09-27 18:08:30 +0800295 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900296 err = f2fs_add_link(dentry, inode);
297 if (err)
298 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700299 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900300
Chao Yu4d57b862018-05-30 00:20:41 +0800301 f2fs_alloc_nid_done(sbi, ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900302
Al Viro1e2e5472018-05-04 08:23:01 -0400303 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800304
305 if (IS_DIRSYNC(dir))
306 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700307
308 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900309 return 0;
310out:
Chao Yu4d57b862018-05-30 00:20:41 +0800311 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900312 return err;
313}
314
315static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
316 struct dentry *dentry)
317{
David Howells2b0143b2015-03-17 22:25:59 +0000318 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700319 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800320 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900321
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200322 if (unlikely(f2fs_cp_error(sbi)))
323 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700324 err = f2fs_is_checkpoint_ready(sbi);
325 if (err)
326 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200327
Eric Biggersb05157e2017-11-29 12:35:29 -0800328 err = fscrypt_prepare_link(old_dentry, dir, dentry);
329 if (err)
330 return err;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700331
Chao Yu5c571322017-07-26 00:01:41 +0800332 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
333 (!projid_eq(F2FS_I(dir)->i_projid,
334 F2FS_I(old_dentry->d_inode)->i_projid)))
335 return -EXDEV;
336
Chao Yu0abd6752017-07-09 00:13:07 +0800337 err = dquot_initialize(dir);
338 if (err)
339 return err;
340
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800341 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900342
Deepa Dinamani078cd822016-09-14 07:48:04 -0700343 inode->i_ctime = current_time(inode);
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900344 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900345
Jaegeuk Kim91942322016-05-20 10:13:22 -0700346 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800347 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900348 err = f2fs_add_link(dentry, inode);
349 if (err)
350 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700351 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900352
353 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800354
355 if (IS_DIRSYNC(dir))
356 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900357 return 0;
358out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700359 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900360 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700361 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900362 return err;
363}
364
365struct dentry *f2fs_get_parent(struct dentry *child)
366{
367 struct qstr dotdot = QSTR_INIT("..", 2);
Chao Yu91246c22016-07-19 08:27:47 +0800368 struct page *page;
369 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
370 if (!ino) {
371 if (IS_ERR(page))
372 return ERR_CAST(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900373 return ERR_PTR(-ENOENT);
Chao Yu91246c22016-07-19 08:27:47 +0800374 }
Al Virofc640052016-04-10 01:33:30 -0400375 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900376}
377
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700378static int __recover_dot_dentries(struct inode *dir, nid_t pino)
379{
380 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
381 struct qstr dot = QSTR_INIT(".", 1);
382 struct qstr dotdot = QSTR_INIT("..", 2);
383 struct f2fs_dir_entry *de;
384 struct page *page;
385 int err = 0;
386
Chao Yu4e0d8362015-12-30 17:40:31 +0800387 if (f2fs_readonly(sbi->sb)) {
388 f2fs_msg(sbi->sb, KERN_INFO,
389 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
390 "in readonly mountpoint", dir->i_ino, pino);
391 return 0;
392 }
393
Chao Yua6d3a472017-07-24 17:12:06 +0800394 err = dquot_initialize(dir);
395 if (err)
396 return err;
397
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800398 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800399
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700400 f2fs_lock_op(sbi);
401
402 de = f2fs_find_entry(dir, &dot, &page);
403 if (de) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700404 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700405 } else if (IS_ERR(page)) {
406 err = PTR_ERR(page);
407 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700408 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800409 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700410 if (err)
411 goto out;
412 }
413
414 de = f2fs_find_entry(dir, &dotdot, &page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800415 if (de)
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700416 f2fs_put_page(page, 0);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800417 else if (IS_ERR(page))
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700418 err = PTR_ERR(page);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800419 else
Chao Yu4d57b862018-05-30 00:20:41 +0800420 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700421out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700422 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700423 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700424
425 f2fs_unlock_op(sbi);
426 return err;
427}
428
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900429static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
430 unsigned int flags)
431{
432 struct inode *inode = NULL;
433 struct f2fs_dir_entry *de;
434 struct page *page;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800435 struct dentry *new;
436 nid_t ino = -1;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700437 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000438 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Eric Biggersb01531d2019-03-20 11:39:13 -0700439 struct fscrypt_name fname;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900440
Chao Yu0c5e36d2017-10-17 17:33:41 +0800441 trace_f2fs_lookup_start(dir, dentry, flags);
442
Chao Yu0c5e36d2017-10-17 17:33:41 +0800443 if (dentry->d_name.len > F2FS_NAME_LEN) {
444 err = -ENAMETOOLONG;
445 goto out;
446 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900447
Eric Biggersb01531d2019-03-20 11:39:13 -0700448 err = fscrypt_prepare_lookup(dir, dentry, &fname);
449 if (err == -ENOENT)
450 goto out_splice;
451 if (err)
452 goto out;
453 de = __f2fs_find_entry(dir, &fname, &page);
454 fscrypt_free_filename(&fname);
455
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700456 if (!de) {
Chao Yu0c5e36d2017-10-17 17:33:41 +0800457 if (IS_ERR(page)) {
458 err = PTR_ERR(page);
459 goto out;
460 }
461 goto out_splice;
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700462 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900463
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700464 ino = le32_to_cpu(de->ino);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700465 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700466
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700467 inode = f2fs_iget(dir->i_sb, ino);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800468 if (IS_ERR(inode)) {
469 err = PTR_ERR(inode);
470 goto out;
471 }
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700472
Liu Xue8c2b1432016-02-26 06:39:23 +0000473 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
474 err = __recover_dot_dentries(dir, root_ino);
475 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800476 goto out_iput;
Liu Xue8c2b1432016-02-26 06:39:23 +0000477 }
478
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700479 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700480 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700481 if (err)
Chao Yu0c5e36d2017-10-17 17:33:41 +0800482 goto out_iput;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900483 }
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530484 if (IS_ENCRYPTED(dir) &&
Dan Carpenter07fe8d42016-12-16 11:18:15 +0300485 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
486 !fscrypt_has_permitted_context(dir, inode)) {
Eric Biggersfaac7fd2017-04-07 10:58:39 -0700487 f2fs_msg(inode->i_sb, KERN_WARNING,
488 "Inconsistent encryption contexts: %lu/%lu",
489 dir->i_ino, inode->i_ino);
490 err = -EPERM;
Chao Yu0c5e36d2017-10-17 17:33:41 +0800491 goto out_iput;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800492 }
Chao Yu0c5e36d2017-10-17 17:33:41 +0800493out_splice:
494 new = d_splice_alias(inode, dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -0700495 err = PTR_ERR_OR_ZERO(new);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800496 trace_f2fs_lookup_end(dir, dentry, ino, err);
497 return new;
498out_iput:
Chao Yud7267322016-03-10 22:24:23 +0800499 iput(inode);
Chao Yu0c5e36d2017-10-17 17:33:41 +0800500out:
501 trace_f2fs_lookup_end(dir, dentry, ino, err);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700502 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900503}
504
505static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
506{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700507 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000508 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900509 struct f2fs_dir_entry *de;
510 struct page *page;
511 int err = -ENOENT;
512
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900513 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900514
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200515 if (unlikely(f2fs_cp_error(sbi)))
516 return -EIO;
517
Chao Yu0abd6752017-07-09 00:13:07 +0800518 err = dquot_initialize(dir);
519 if (err)
520 return err;
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200521 err = dquot_initialize(inode);
522 if (err)
523 return err;
Chao Yu0abd6752017-07-09 00:13:07 +0800524
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900525 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Chao Yu91246c22016-07-19 08:27:47 +0800526 if (!de) {
527 if (IS_ERR(page))
528 err = PTR_ERR(page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900529 goto fail;
Chao Yu91246c22016-07-19 08:27:47 +0800530 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900531
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800532 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800533
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900534 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800535 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900536 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900537 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900538 f2fs_put_page(page, 0);
539 goto fail;
540 }
Chao Yudbeacf02014-09-24 18:17:04 +0800541 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800542 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900543
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800544 if (IS_DIRSYNC(dir))
545 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900546fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900547 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900548 return err;
549}
550
Al Viro6b255392015-11-17 10:20:54 -0500551static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500552 struct inode *inode,
553 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700554{
Al Virofceef392015-12-29 15:58:39 -0500555 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400556 if (!IS_ERR(link) && !*link) {
557 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500558 do_delayed_call(done);
559 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400560 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700561 }
Al Viro680baac2015-05-02 13:32:22 -0400562 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700563}
564
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900565static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
566 const char *symname)
567{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700568 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900569 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700570 size_t len = strlen(symname);
Eric Biggers393c0382018-01-11 23:26:49 -0500571 struct fscrypt_str disk_link;
Gu Zhenge4795562013-09-27 18:08:30 +0800572 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900573
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200574 if (unlikely(f2fs_cp_error(sbi)))
575 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700576 err = f2fs_is_checkpoint_ready(sbi);
577 if (err)
578 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200579
Eric Biggers393c0382018-01-11 23:26:49 -0500580 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
581 &disk_link);
582 if (err)
583 return err;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700584
Chao Yu0abd6752017-07-09 00:13:07 +0800585 err = dquot_initialize(dir);
586 if (err)
587 return err;
588
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900589 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
590 if (IS_ERR(inode))
591 return PTR_ERR(inode);
592
Eric Biggers393c0382018-01-11 23:26:49 -0500593 if (IS_ENCRYPTED(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700594 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
595 else
596 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500597 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900598 inode->i_mapping->a_ops = &f2fs_dblock_aops;
599
Gu Zhenge4795562013-09-27 18:08:30 +0800600 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900601 err = f2fs_add_link(dentry, inode);
602 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800603 goto out_f2fs_handle_failed_inode;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700604 f2fs_unlock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800605 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900606
Eric Biggers393c0382018-01-11 23:26:49 -0500607 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
608 if (err)
609 goto err_out;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700610
Chao Yu922ec352016-02-15 17:54:26 +0800611 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700612
613err_out:
Al Viro1e2e5472018-05-04 08:23:01 -0400614 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800615
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700616 /*
617 * Let's flush symlink data in order to avoid broken symlink as much as
618 * possible. Nevertheless, fsyncing is the best way, but there is no
619 * way to get a file descriptor in order to flush that.
620 *
621 * Note that, it needs to do dir->fsync to make this recoverable.
622 * If the symlink path is stored into inline_data, there is no
623 * performance regression.
624 */
Chao Yua6be0142015-10-22 18:23:08 +0800625 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800626 filemap_write_and_wait_range(inode->i_mapping, 0,
627 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700628
Chao Yua6be0142015-10-22 18:23:08 +0800629 if (IS_DIRSYNC(dir))
630 f2fs_sync_fs(sbi->sb, 1);
631 } else {
632 f2fs_unlink(dir, dentry);
633 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700634
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700635 f2fs_balance_fs(sbi, true);
Eric Biggers393c0382018-01-11 23:26:49 -0500636 goto out_free_encrypted_link;
637
Chao Yu4d57b862018-05-30 00:20:41 +0800638out_f2fs_handle_failed_inode:
639 f2fs_handle_failed_inode(inode);
Eric Biggers393c0382018-01-11 23:26:49 -0500640out_free_encrypted_link:
641 if (disk_link.name != (unsigned char *)symname)
Jaegeuk Kim52225952018-12-13 18:38:33 -0800642 kvfree(disk_link.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900643 return err;
644}
645
646static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
647{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700648 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900649 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800650 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900651
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200652 if (unlikely(f2fs_cp_error(sbi)))
653 return -EIO;
654
Chao Yu0abd6752017-07-09 00:13:07 +0800655 err = dquot_initialize(dir);
656 if (err)
657 return err;
658
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900659 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900660 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900661 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900662
663 inode->i_op = &f2fs_dir_inode_operations;
664 inode->i_fop = &f2fs_dir_operations;
665 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800666 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900667
Jaegeuk Kim91942322016-05-20 10:13:22 -0700668 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800669 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900670 err = f2fs_add_link(dentry, inode);
671 if (err)
672 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700673 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900674
Chao Yu4d57b862018-05-30 00:20:41 +0800675 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900676
Al Viro1e2e5472018-05-04 08:23:01 -0400677 d_instantiate_new(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900678
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800679 if (IS_DIRSYNC(dir))
680 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700681
682 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900683 return 0;
684
685out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700686 clear_inode_flag(inode, FI_INC_LINK);
Chao Yu4d57b862018-05-30 00:20:41 +0800687 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900688 return err;
689}
690
691static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
692{
David Howells2b0143b2015-03-17 22:25:59 +0000693 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900694 if (f2fs_empty_dir(inode))
695 return f2fs_unlink(dir, dentry);
696 return -ENOTEMPTY;
697}
698
699static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
700 umode_t mode, dev_t rdev)
701{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700702 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900703 struct inode *inode;
704 int err = 0;
705
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200706 if (unlikely(f2fs_cp_error(sbi)))
707 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700708 err = f2fs_is_checkpoint_ready(sbi);
709 if (err)
710 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200711
Chao Yu0abd6752017-07-09 00:13:07 +0800712 err = dquot_initialize(dir);
713 if (err)
714 return err;
715
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900716 inode = f2fs_new_inode(dir, mode);
717 if (IS_ERR(inode))
718 return PTR_ERR(inode);
719
720 init_special_inode(inode, inode->i_mode, rdev);
721 inode->i_op = &f2fs_special_inode_operations;
722
Gu Zhenge4795562013-09-27 18:08:30 +0800723 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900724 err = f2fs_add_link(dentry, inode);
725 if (err)
726 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700727 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900728
Chao Yu4d57b862018-05-30 00:20:41 +0800729 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800730
Al Viro1e2e5472018-05-04 08:23:01 -0400731 d_instantiate_new(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800732
733 if (IS_DIRSYNC(dir))
734 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700735
736 f2fs_balance_fs(sbi, true);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900737 return 0;
738out:
Chao Yu4d57b862018-05-30 00:20:41 +0800739 f2fs_handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900740 return err;
741}
742
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800743static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
744 umode_t mode, struct inode **whiteout)
745{
746 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
747 struct inode *inode;
748 int err;
749
Chao Yu0abd6752017-07-09 00:13:07 +0800750 err = dquot_initialize(dir);
751 if (err)
752 return err;
753
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800754 inode = f2fs_new_inode(dir, mode);
755 if (IS_ERR(inode))
756 return PTR_ERR(inode);
757
758 if (whiteout) {
759 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
760 inode->i_op = &f2fs_special_inode_operations;
761 } else {
762 inode->i_op = &f2fs_file_inode_operations;
763 inode->i_fop = &f2fs_file_operations;
764 inode->i_mapping->a_ops = &f2fs_dblock_aops;
765 }
766
767 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800768 err = f2fs_acquire_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800769 if (err)
770 goto out;
771
772 err = f2fs_do_tmpfile(inode, dir);
773 if (err)
774 goto release_out;
775
776 /*
777 * add this non-linked tmpfile to orphan list, in this way we could
778 * remove all unused data of tmpfile after abnormal power-off.
779 */
Chao Yu4d57b862018-05-30 00:20:41 +0800780 f2fs_add_orphan_inode(inode);
781 f2fs_alloc_nid_done(sbi, inode->i_ino);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800782
783 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700784 f2fs_i_links_write(inode, false);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800785 *whiteout = inode;
786 } else {
787 d_tmpfile(dentry, inode);
788 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700789 /* link_count was changed by d_tmpfile as well. */
790 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800791 unlock_new_inode(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700792
793 f2fs_balance_fs(sbi, true);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800794 return 0;
795
796release_out:
Chao Yu4d57b862018-05-30 00:20:41 +0800797 f2fs_release_orphan_inode(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800798out:
Chao Yu4d57b862018-05-30 00:20:41 +0800799 f2fs_handle_failed_inode(inode);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800800 return err;
801}
802
803static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
804{
Sheng Yongff62af22018-03-15 18:51:42 +0800805 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
806
807 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200808 return -EIO;
809
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530810 if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700811 int err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700812 if (err)
813 return err;
814 }
815
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800816 return __f2fs_tmpfile(dir, dentry, mode, NULL);
817}
818
819static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
820{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200821 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
822 return -EIO;
823
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800824 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
825}
826
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900827static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800828 struct inode *new_dir, struct dentry *new_dentry,
829 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900830{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700831 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000832 struct inode *old_inode = d_inode(old_dentry);
833 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800834 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900835 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800836 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900837 struct f2fs_dir_entry *old_dir_entry = NULL;
838 struct f2fs_dir_entry *old_entry;
839 struct f2fs_dir_entry *new_entry;
Chao Yu993a0492016-02-17 16:47:05 +0800840 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700841 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900842
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200843 if (unlikely(f2fs_cp_error(sbi)))
844 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700845 err = f2fs_is_checkpoint_ready(sbi);
846 if (err)
847 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200848
Chao Yu5c571322017-07-26 00:01:41 +0800849 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
850 (!projid_eq(F2FS_I(new_dir)->i_projid,
851 F2FS_I(old_dentry->d_inode)->i_projid)))
852 return -EXDEV;
853
Chao Yu0abd6752017-07-09 00:13:07 +0800854 err = dquot_initialize(old_dir);
855 if (err)
856 goto out;
857
858 err = dquot_initialize(new_dir);
859 if (err)
860 goto out;
861
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200862 if (new_inode) {
863 err = dquot_initialize(new_inode);
864 if (err)
865 goto out;
866 }
867
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -0700868 err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900869 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +0800870 if (!old_entry) {
871 if (IS_ERR(old_page))
872 err = PTR_ERR(old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900873 goto out;
Chao Yu91246c22016-07-19 08:27:47 +0800874 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900875
876 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900877 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700878 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800879 if (IS_ERR(old_dir_page))
880 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900881 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700882 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900883 }
884
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800885 if (flags & RENAME_WHITEOUT) {
886 err = f2fs_create_whiteout(old_dir, &whiteout);
887 if (err)
888 goto out_dir;
889 }
890
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900891 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900892
893 err = -ENOTEMPTY;
894 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800895 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900896
897 err = -ENOENT;
898 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
899 &new_page);
Chao Yu91246c22016-07-19 08:27:47 +0800900 if (!new_entry) {
901 if (IS_ERR(new_page))
902 err = PTR_ERR(new_page);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800903 goto out_whiteout;
Chao Yu91246c22016-07-19 08:27:47 +0800904 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900905
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800906 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800907
Chao Yu12560102014-06-24 14:16:24 +0800908 f2fs_lock_op(sbi);
909
Chao Yu4d57b862018-05-30 00:20:41 +0800910 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900911 if (err)
912 goto put_out_dir;
913
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900914 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
915
Deepa Dinamani078cd822016-09-14 07:48:04 -0700916 new_inode->i_ctime = current_time(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900917 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900918 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700919 f2fs_i_links_write(new_inode, false);
920 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900921 up_write(&F2FS_I(new_inode)->i_sem);
922
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900923 if (!new_inode->i_nlink)
Chao Yu4d57b862018-05-30 00:20:41 +0800924 f2fs_add_orphan_inode(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900925 else
Chao Yu4d57b862018-05-30 00:20:41 +0800926 f2fs_release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900927 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800928 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800929
Chao Yu12560102014-06-24 14:16:24 +0800930 f2fs_lock_op(sbi);
931
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900932 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800933 if (err) {
934 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800935 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800936 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900937
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700938 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700939 f2fs_i_links_write(new_dir, true);
Chao Yu993a0492016-02-17 16:47:05 +0800940
941 /*
942 * old entry and new entry can locate in the same inline
943 * dentry in inode, when attaching new entry in inline dentry,
944 * it could force inline dentry conversion, after that,
945 * old_entry and old_page will point to wrong address, in
946 * order to avoid this, let's do the check and update here.
947 */
948 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
949 f2fs_put_page(old_page, 0);
950 old_page = NULL;
951
952 old_entry = f2fs_find_entry(old_dir,
953 &old_dentry->d_name, &old_page);
954 if (!old_entry) {
Chao Yu91246c22016-07-19 08:27:47 +0800955 err = -ENOENT;
956 if (IS_ERR(old_page))
957 err = PTR_ERR(old_page);
Chao Yu993a0492016-02-17 16:47:05 +0800958 f2fs_unlock_op(sbi);
959 goto out_whiteout;
960 }
961 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900962 }
963
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900964 down_write(&F2FS_I(old_inode)->i_sem);
Sheng Yongb855bf02017-06-26 10:41:36 +0800965 if (!old_dir_entry || whiteout)
966 file_lost_pino(old_inode);
967 else
968 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900969 up_write(&F2FS_I(old_inode)->i_sem);
970
Deepa Dinamani078cd822016-09-14 07:48:04 -0700971 old_inode->i_ctime = current_time(old_inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700972 f2fs_mark_inode_dirty_sync(old_inode, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900973
Chao Yudbeacf02014-09-24 18:17:04 +0800974 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900975
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800976 if (whiteout) {
977 whiteout->i_state |= I_LINKABLE;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700978 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800979 err = f2fs_add_link(old_dentry, whiteout);
980 if (err)
981 goto put_out_dir;
982 whiteout->i_state &= ~I_LINKABLE;
983 iput(whiteout);
984 }
985
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900986 if (old_dir_entry) {
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800987 if (old_dir != new_dir && !whiteout)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900988 f2fs_set_link(old_inode, old_dir_entry,
989 old_dir_page, new_dir);
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800990 else
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900991 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700992 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900993 }
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600994 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800995 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600996 if (S_ISDIR(old_inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +0800997 f2fs_add_ino_entry(sbi, old_inode->i_ino,
998 TRANS_DIR_INO);
Jaegeuk Kimade990f2018-04-24 22:43:01 -0600999 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001000
Gu Zhenge4795562013-09-27 18:08:30 +08001001 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001002
1003 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1004 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301005
1006 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001007 return 0;
1008
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +09001009put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +08001010 f2fs_unlock_op(sbi);
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001011 if (new_page)
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001012 f2fs_put_page(new_page, 0);
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001013out_whiteout:
1014 if (whiteout)
1015 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001016out_dir:
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001017 if (old_dir_entry)
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001018 f2fs_put_page(old_dir_page, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001019out_old:
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001020 f2fs_put_page(old_page, 0);
1021out:
1022 return err;
1023}
1024
Chao Yu32f9bc22014-07-12 19:13:54 +08001025static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1026 struct inode *new_dir, struct dentry *new_dentry)
1027{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001028 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +00001029 struct inode *old_inode = d_inode(old_dentry);
1030 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +08001031 struct page *old_dir_page, *new_dir_page;
1032 struct page *old_page, *new_page;
1033 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1034 struct f2fs_dir_entry *old_entry, *new_entry;
1035 int old_nlink = 0, new_nlink = 0;
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001036 int err;
Chao Yu32f9bc22014-07-12 19:13:54 +08001037
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001038 if (unlikely(f2fs_cp_error(sbi)))
1039 return -EIO;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001040 err = f2fs_is_checkpoint_ready(sbi);
1041 if (err)
1042 return err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001043
Chao Yu5c571322017-07-26 00:01:41 +08001044 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1045 !projid_eq(F2FS_I(new_dir)->i_projid,
1046 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1047 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1048 !projid_eq(F2FS_I(old_dir)->i_projid,
1049 F2FS_I(new_dentry->d_inode)->i_projid)))
1050 return -EXDEV;
1051
Chao Yu0abd6752017-07-09 00:13:07 +08001052 err = dquot_initialize(old_dir);
1053 if (err)
1054 goto out;
1055
1056 err = dquot_initialize(new_dir);
1057 if (err)
1058 goto out;
1059
Jaegeuk Kimd83d0f52018-09-17 13:25:04 -07001060 err = -ENOENT;
Chao Yu32f9bc22014-07-12 19:13:54 +08001061 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Chao Yu91246c22016-07-19 08:27:47 +08001062 if (!old_entry) {
1063 if (IS_ERR(old_page))
1064 err = PTR_ERR(old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001065 goto out;
Chao Yu91246c22016-07-19 08:27:47 +08001066 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001067
1068 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Chao Yu91246c22016-07-19 08:27:47 +08001069 if (!new_entry) {
1070 if (IS_ERR(new_page))
1071 err = PTR_ERR(new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001072 goto out_old;
Chao Yu91246c22016-07-19 08:27:47 +08001073 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001074
1075 /* prepare for updating ".." directory entry info later */
1076 if (old_dir != new_dir) {
1077 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001078 old_dir_entry = f2fs_parent_dir(old_inode,
1079 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001080 if (!old_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001081 if (IS_ERR(old_dir_page))
1082 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001083 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001084 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001085 }
1086
1087 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001088 new_dir_entry = f2fs_parent_dir(new_inode,
1089 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001090 if (!new_dir_entry) {
Chao Yu91246c22016-07-19 08:27:47 +08001091 if (IS_ERR(new_dir_page))
1092 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +08001093 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -07001094 }
Chao Yu32f9bc22014-07-12 19:13:54 +08001095 }
1096 }
1097
1098 /*
1099 * If cross rename between file and directory those are not
1100 * in the same directory, we will inc nlink of file's parent
1101 * later, so we should check upper boundary of its nlink.
1102 */
1103 if ((!old_dir_entry || !new_dir_entry) &&
1104 old_dir_entry != new_dir_entry) {
1105 old_nlink = old_dir_entry ? -1 : 1;
1106 new_nlink = -old_nlink;
1107 err = -EMLINK;
Kinglong Meee2f0e962017-03-04 21:48:28 +08001108 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1109 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
Chao Yu32f9bc22014-07-12 19:13:54 +08001110 goto out_new_dir;
1111 }
1112
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001113 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -08001114
Chao Yu32f9bc22014-07-12 19:13:54 +08001115 f2fs_lock_op(sbi);
1116
Chao Yu32f9bc22014-07-12 19:13:54 +08001117 /* update ".." directory entry info of old dentry */
1118 if (old_dir_entry)
1119 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1120
1121 /* update ".." directory entry info of new dentry */
1122 if (new_dir_entry)
1123 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1124
1125 /* update directory entry info of old dir inode */
1126 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1127
1128 down_write(&F2FS_I(old_inode)->i_sem);
1129 file_lost_pino(old_inode);
1130 up_write(&F2FS_I(old_inode)->i_sem);
1131
Deepa Dinamani078cd822016-09-14 07:48:04 -07001132 old_dir->i_ctime = current_time(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001133 if (old_nlink) {
1134 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001135 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001136 up_write(&F2FS_I(old_dir)->i_sem);
1137 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001138 f2fs_mark_inode_dirty_sync(old_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001139
1140 /* update directory entry info of new dir inode */
1141 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1142
1143 down_write(&F2FS_I(new_inode)->i_sem);
1144 file_lost_pino(new_inode);
1145 up_write(&F2FS_I(new_inode)->i_sem);
1146
Deepa Dinamani078cd822016-09-14 07:48:04 -07001147 new_dir->i_ctime = current_time(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +08001148 if (new_nlink) {
1149 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001150 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +08001151 up_write(&F2FS_I(new_dir)->i_sem);
1152 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001153 f2fs_mark_inode_dirty_sync(new_dir, false);
Chao Yu32f9bc22014-07-12 19:13:54 +08001154
Chao Yu63189b72018-03-08 14:22:56 +08001155 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001156 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1157 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
Junling Zheng93cf93f2018-03-07 12:07:49 +08001158 }
Jaegeuk Kim0a007b972017-12-28 08:09:44 -08001159
Chao Yu32f9bc22014-07-12 19:13:54 +08001160 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -08001161
1162 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1163 f2fs_sync_fs(sbi->sb, 1);
Sahitya Tummala63903982018-10-05 10:47:40 +05301164
1165 f2fs_update_time(sbi, REQ_TIME);
Chao Yu32f9bc22014-07-12 19:13:54 +08001166 return 0;
Chao Yu32f9bc22014-07-12 19:13:54 +08001167out_new_dir:
1168 if (new_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001169 f2fs_put_page(new_dir_page, 0);
1170 }
1171out_old_dir:
1172 if (old_dir_entry) {
Chao Yu32f9bc22014-07-12 19:13:54 +08001173 f2fs_put_page(old_dir_page, 0);
1174 }
1175out_new:
Chao Yu32f9bc22014-07-12 19:13:54 +08001176 f2fs_put_page(new_page, 0);
1177out_old:
Chao Yu32f9bc22014-07-12 19:13:54 +08001178 f2fs_put_page(old_page, 0);
1179out:
1180 return err;
1181}
1182
1183static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1184 struct inode *new_dir, struct dentry *new_dentry,
1185 unsigned int flags)
1186{
Eric Biggers2e45b072017-11-29 12:35:30 -08001187 int err;
1188
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001189 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +08001190 return -EINVAL;
1191
Eric Biggers2e45b072017-11-29 12:35:30 -08001192 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1193 flags);
1194 if (err)
1195 return err;
1196
Chao Yu32f9bc22014-07-12 19:13:54 +08001197 if (flags & RENAME_EXCHANGE) {
1198 return f2fs_cross_rename(old_dir, old_dentry,
1199 new_dir, new_dentry);
1200 }
1201 /*
1202 * VFS has already handled the new dentry existence case,
1203 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1204 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +08001205 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +08001206}
1207
Al Viro6b255392015-11-17 10:20:54 -05001208static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001209 struct inode *inode,
1210 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +08001211{
Eric Biggersf2329cb2018-01-11 23:26:49 -05001212 struct page *page;
1213 const char *target;
Chao Yu50732df2014-06-19 16:23:19 +08001214
Al Viro6b255392015-11-17 10:20:54 -05001215 if (!dentry)
1216 return ERR_PTR(-ECHILD);
1217
Eric Biggersf2329cb2018-01-11 23:26:49 -05001218 page = read_mapping_page(inode->i_mapping, 0, NULL);
1219 if (IS_ERR(page))
1220 return ERR_CAST(page);
Chao Yu50732df2014-06-19 16:23:19 +08001221
Eric Biggersf2329cb2018-01-11 23:26:49 -05001222 target = fscrypt_get_symlink(inode, page_address(page),
1223 inode->i_sb->s_blocksize, done);
1224 put_page(page);
1225 return target;
Chao Yu50732df2014-06-19 16:23:19 +08001226}
1227
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001228const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001229 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001230 .getattr = f2fs_getattr,
1231 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001232#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001233 .listxattr = f2fs_listxattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001234#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001235};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001236
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001237const struct inode_operations f2fs_dir_inode_operations = {
1238 .create = f2fs_create,
1239 .lookup = f2fs_lookup,
1240 .link = f2fs_link,
1241 .unlink = f2fs_unlink,
1242 .symlink = f2fs_symlink,
1243 .mkdir = f2fs_mkdir,
1244 .rmdir = f2fs_rmdir,
1245 .mknod = f2fs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001246 .rename = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001247 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001248 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001249 .setattr = f2fs_setattr,
1250 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001251 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001252#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001253 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001254#endif
1255};
1256
1257const struct inode_operations f2fs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001258 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001259 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001260 .setattr = f2fs_setattr,
1261#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001262 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001263#endif
1264};
1265
1266const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001267 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001268 .setattr = f2fs_setattr,
1269 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001270 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001271#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001272 .listxattr = f2fs_listxattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001273#endif
1274};