blob: fcc8c26c0fe85fafe6f05a17e95daafdce73ffa3 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +09002 * fs/f2fs/namei.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/pagemap.h>
14#include <linux/sched.h>
15#include <linux/ctype.h>
Chao Yu50732df2014-06-19 16:23:19 +080016#include <linux/dcache.h>
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -070017#include <linux/namei.h>
Jaegeuk Kim57397d82012-11-02 17:11:10 +090018
19#include "f2fs.h"
Jaegeuk Kim953a3e22013-03-21 15:21:57 +090020#include "node.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;
Gu Zhenge4795562013-09-27 18:08:30 +080031 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090032
Jaegeuk Kima014e032014-06-20 21:44:02 -070033 inode = new_inode(dir->i_sb);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090034 if (!inode)
35 return ERR_PTR(-ENOMEM);
36
Gu Zhenge4795562013-09-27 18:08:30 +080037 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090038 if (!alloc_nid(sbi, &ino)) {
Gu Zhenge4795562013-09-27 18:08:30 +080039 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090040 err = -ENOSPC;
41 goto fail;
42 }
Gu Zhenge4795562013-09-27 18:08:30 +080043 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090044
Chao Yu70ff5df2014-05-08 17:09:30 +080045 inode_init_owner(inode, dir, mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090046
47 inode->i_ino = ino;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090048 inode->i_blocks = 0;
49 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
50 inode->i_generation = sbi->s_next_generation++;
51
52 err = insert_inode_locked(inode);
53 if (err) {
54 err = -EINVAL;
55 nid_free = true;
Jaegeuk Kima21c20f2015-08-16 12:38:15 -070056 goto fail;
Jaegeuk Kim57397d82012-11-02 17:11:10 +090057 }
Chao Yu622f28a2014-09-24 18:19:10 +080058
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070059 /* If the directory encrypted, then we should encrypt the inode. */
60 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
61 f2fs_set_encrypted_inode(inode);
62
Chao Yufba48a82015-12-23 17:51:35 +080063 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070064 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070065 if (f2fs_may_inline_dentry(inode))
Chao Yu622f28a2014-09-24 18:19:10 +080066 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
67
Jaegeuk Kim3e72f722015-06-19 17:53:26 -070068 f2fs_init_extent_tree(inode, NULL);
69
Chao Yud5e8f6c2015-07-15 17:28:53 +080070 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -070071 stat_inc_inline_inode(inode);
72 stat_inc_inline_dir(inode);
73
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090074 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090075 mark_inode_dirty(inode);
76 return inode;
77
Jaegeuk Kim57397d82012-11-02 17:11:10 +090078fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090079 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090080 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090081 if (nid_free)
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -070082 set_inode_flag(F2FS_I(inode), FI_FREE_NID);
83 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090084 return ERR_PTR(err);
85}
86
87static int is_multimedia_file(const unsigned char *s, const char *sub)
88{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020089 size_t slen = strlen(s);
90 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090091
Chao Yu741a7be2015-07-06 20:30:40 +080092 /*
93 * filename format of multimedia file should be defined as:
94 * "filename + '.' + extension".
95 */
96 if (slen < sublen + 2)
97 return 0;
98
99 if (s[slen - sublen - 1] != '.')
Namjae Jeone66509f02013-04-20 01:27:21 +0900100 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900101
Gu Zheng749ebfd2013-08-22 18:11:25 +0800102 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900103}
104
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900105/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900106 * Set multimedia files as cold files for hot/cold data separation
107 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900108static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900109 const unsigned char *name)
110{
111 int i;
112 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
113
114 int count = le32_to_cpu(sbi->raw_super->extension_count);
115 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900116 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900117 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900118 break;
119 }
120 }
121}
122
123static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
124 bool excl)
125{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700126 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900127 struct inode *inode;
128 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800129 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900130
131 inode = f2fs_new_inode(dir, mode);
132 if (IS_ERR(inode))
133 return PTR_ERR(inode);
134
135 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900136 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900137
138 inode->i_op = &f2fs_file_inode_operations;
139 inode->i_fop = &f2fs_file_operations;
140 inode->i_mapping->a_ops = &f2fs_dblock_aops;
141 ino = inode->i_ino;
142
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800143 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800144
Gu Zhenge4795562013-09-27 18:08:30 +0800145 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900146 err = f2fs_add_link(dentry, inode);
147 if (err)
148 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700149 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900150
151 alloc_nid_done(sbi, ino);
152
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900153 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900154 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800155
156 if (IS_DIRSYNC(dir))
157 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900158 return 0;
159out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700160 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900161 return err;
162}
163
164static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
165 struct dentry *dentry)
166{
David Howells2b0143b2015-03-17 22:25:59 +0000167 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700168 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800169 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900170
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700171 if (f2fs_encrypted_inode(dir) &&
172 !f2fs_is_child_context_consistent_with_parent(dir, inode))
173 return -EPERM;
174
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800175 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900176
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900177 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900178 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900179
180 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800181 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900182 err = f2fs_add_link(dentry, inode);
183 if (err)
184 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700185 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900186
187 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800188
189 if (IS_DIRSYNC(dir))
190 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900191 return 0;
192out:
193 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
194 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700195 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900196 return err;
197}
198
199struct dentry *f2fs_get_parent(struct dentry *child)
200{
201 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +0000202 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900203 if (!ino)
204 return ERR_PTR(-ENOENT);
David Howells2b0143b2015-03-17 22:25:59 +0000205 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900206}
207
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700208static int __recover_dot_dentries(struct inode *dir, nid_t pino)
209{
210 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
211 struct qstr dot = QSTR_INIT(".", 1);
212 struct qstr dotdot = QSTR_INIT("..", 2);
213 struct f2fs_dir_entry *de;
214 struct page *page;
215 int err = 0;
216
Chao Yu4e0d8362015-12-30 17:40:31 +0800217 if (f2fs_readonly(sbi->sb)) {
218 f2fs_msg(sbi->sb, KERN_INFO,
219 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
220 "in readonly mountpoint", dir->i_ino, pino);
221 return 0;
222 }
223
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800224 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800225
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700226 f2fs_lock_op(sbi);
227
228 de = f2fs_find_entry(dir, &dot, &page);
229 if (de) {
230 f2fs_dentry_kunmap(dir, page);
231 f2fs_put_page(page, 0);
232 } else {
233 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
234 if (err)
235 goto out;
236 }
237
238 de = f2fs_find_entry(dir, &dotdot, &page);
239 if (de) {
240 f2fs_dentry_kunmap(dir, page);
241 f2fs_put_page(page, 0);
242 } else {
243 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
244 }
245out:
246 if (!err) {
247 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
248 mark_inode_dirty(dir);
249 }
250
251 f2fs_unlock_op(sbi);
252 return err;
253}
254
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900255static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
256 unsigned int flags)
257{
258 struct inode *inode = NULL;
259 struct f2fs_dir_entry *de;
260 struct page *page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700261 nid_t ino;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700262 int err = 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900263
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900264 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900265 return ERR_PTR(-ENAMETOOLONG);
266
267 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700268 if (!de)
269 return d_splice_alias(inode, dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900270
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700271 ino = le32_to_cpu(de->ino);
272 f2fs_dentry_kunmap(dir, page);
273 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700274
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700275 inode = f2fs_iget(dir->i_sb, ino);
276 if (IS_ERR(inode))
277 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700278
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700279 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700280 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700281 if (err)
282 goto err_out;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900283 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900284 return d_splice_alias(inode, dentry);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700285
286err_out:
287 iget_failed(inode);
288 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900289}
290
291static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
292{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700293 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000294 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900295 struct f2fs_dir_entry *de;
296 struct page *page;
297 int err = -ENOENT;
298
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900299 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900300
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900301 de = f2fs_find_entry(dir, &dentry->d_name, &page);
302 if (!de)
303 goto fail;
304
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800305 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800306
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900307 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900308 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900309 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900310 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800311 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900312 f2fs_put_page(page, 0);
313 goto fail;
314 }
Chao Yudbeacf02014-09-24 18:17:04 +0800315 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800316 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900317
arter97e1c42042014-08-06 23:22:50 +0900318 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900319 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800320
321 if (IS_DIRSYNC(dir))
322 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900323fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900324 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900325 return err;
326}
327
Al Viro6b255392015-11-17 10:20:54 -0500328static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500329 struct inode *inode,
330 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700331{
Al Virofceef392015-12-29 15:58:39 -0500332 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400333 if (!IS_ERR(link) && !*link) {
334 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500335 do_delayed_call(done);
336 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400337 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700338 }
Al Viro680baac2015-05-02 13:32:22 -0400339 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700340}
341
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900342static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
343 const char *symname)
344{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700345 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700347 size_t len = strlen(symname);
Chao Yu922ec352016-02-15 17:54:26 +0800348 struct f2fs_str disk_link = FSTR_INIT((char *)symname, len + 1);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700349 struct f2fs_encrypted_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800350 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351
Chao Yu922ec352016-02-15 17:54:26 +0800352 if (f2fs_encrypted_inode(dir)) {
353 err = f2fs_get_encryption_info(dir);
354 if (err)
355 return err;
356
357 if (!f2fs_encrypted_inode(dir))
358 return -EPERM;
359
360 disk_link.len = (f2fs_fname_encrypted_size(dir, len) +
361 sizeof(struct f2fs_encrypted_symlink_data));
362 }
363
364 if (disk_link.len > dir->i_sb->s_blocksize)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700365 return -ENAMETOOLONG;
366
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900367 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
368 if (IS_ERR(inode))
369 return PTR_ERR(inode);
370
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700371 if (f2fs_encrypted_inode(inode))
372 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
373 else
374 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500375 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900376 inode->i_mapping->a_ops = &f2fs_dblock_aops;
377
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800378 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800379
Gu Zhenge4795562013-09-27 18:08:30 +0800380 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900381 err = f2fs_add_link(dentry, inode);
382 if (err)
383 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700384 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900385 alloc_nid_done(sbi, inode->i_ino);
386
Chao Yu922ec352016-02-15 17:54:26 +0800387 if (f2fs_encrypted_inode(inode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700388 struct qstr istr = QSTR_INIT(symname, len);
Chao Yu922ec352016-02-15 17:54:26 +0800389 struct f2fs_str ostr;
390
391 sd = kzalloc(disk_link.len, GFP_NOFS);
392 if (!sd) {
393 err = -ENOMEM;
394 goto err_out;
395 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700396
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700397 err = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700398 if (err)
399 goto err_out;
400
Chao Yu922ec352016-02-15 17:54:26 +0800401 if (!f2fs_encrypted_inode(inode)) {
402 err = -EPERM;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700403 goto err_out;
Chao Yu922ec352016-02-15 17:54:26 +0800404 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700405
Chao Yu922ec352016-02-15 17:54:26 +0800406 ostr.name = sd->encrypted_path;
407 ostr.len = disk_link.len;
408 err = f2fs_fname_usr_to_disk(inode, &istr, &ostr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700409 if (err < 0)
410 goto err_out;
411
Chao Yu922ec352016-02-15 17:54:26 +0800412 sd->len = cpu_to_le16(ostr.len);
413 disk_link.name = (char *)sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700414 }
415
Chao Yu922ec352016-02-15 17:54:26 +0800416 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700417
418err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900419 d_instantiate(dentry, inode);
420 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800421
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700422 /*
423 * Let's flush symlink data in order to avoid broken symlink as much as
424 * possible. Nevertheless, fsyncing is the best way, but there is no
425 * way to get a file descriptor in order to flush that.
426 *
427 * Note that, it needs to do dir->fsync to make this recoverable.
428 * If the symlink path is stored into inline_data, there is no
429 * performance regression.
430 */
Chao Yua6be0142015-10-22 18:23:08 +0800431 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800432 filemap_write_and_wait_range(inode->i_mapping, 0,
433 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700434
Chao Yua6be0142015-10-22 18:23:08 +0800435 if (IS_DIRSYNC(dir))
436 f2fs_sync_fs(sbi->sb, 1);
437 } else {
438 f2fs_unlink(dir, dentry);
439 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700440
441 kfree(sd);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900442 return err;
443out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700444 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900445 return err;
446}
447
448static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
449{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700450 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900451 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800452 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900453
454 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900455 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900456 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900457
458 inode->i_op = &f2fs_dir_inode_operations;
459 inode->i_fop = &f2fs_dir_operations;
460 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800461 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900462
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800463 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800464
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900465 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800466 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900467 err = f2fs_add_link(dentry, inode);
468 if (err)
469 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700470 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900471
472 alloc_nid_done(sbi, inode->i_ino);
473
474 d_instantiate(dentry, inode);
475 unlock_new_inode(inode);
476
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800477 if (IS_DIRSYNC(dir))
478 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900479 return 0;
480
481out_fail:
482 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700483 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900484 return err;
485}
486
487static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
488{
David Howells2b0143b2015-03-17 22:25:59 +0000489 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900490 if (f2fs_empty_dir(inode))
491 return f2fs_unlink(dir, dentry);
492 return -ENOTEMPTY;
493}
494
495static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
496 umode_t mode, dev_t rdev)
497{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700498 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900499 struct inode *inode;
500 int err = 0;
501
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900502 inode = f2fs_new_inode(dir, mode);
503 if (IS_ERR(inode))
504 return PTR_ERR(inode);
505
506 init_special_inode(inode, inode->i_mode, rdev);
507 inode->i_op = &f2fs_special_inode_operations;
508
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800509 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800510
Gu Zhenge4795562013-09-27 18:08:30 +0800511 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900512 err = f2fs_add_link(dentry, inode);
513 if (err)
514 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700515 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900516
517 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800518
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900519 d_instantiate(dentry, inode);
520 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800521
522 if (IS_DIRSYNC(dir))
523 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900524 return 0;
525out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700526 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900527 return err;
528}
529
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800530static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
531 umode_t mode, struct inode **whiteout)
532{
533 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
534 struct inode *inode;
535 int err;
536
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800537 inode = f2fs_new_inode(dir, mode);
538 if (IS_ERR(inode))
539 return PTR_ERR(inode);
540
541 if (whiteout) {
542 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
543 inode->i_op = &f2fs_special_inode_operations;
544 } else {
545 inode->i_op = &f2fs_file_inode_operations;
546 inode->i_fop = &f2fs_file_operations;
547 inode->i_mapping->a_ops = &f2fs_dblock_aops;
548 }
549
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800550 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800551
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800552 f2fs_lock_op(sbi);
553 err = acquire_orphan_inode(sbi);
554 if (err)
555 goto out;
556
557 err = f2fs_do_tmpfile(inode, dir);
558 if (err)
559 goto release_out;
560
561 /*
562 * add this non-linked tmpfile to orphan list, in this way we could
563 * remove all unused data of tmpfile after abnormal power-off.
564 */
565 add_orphan_inode(sbi, inode->i_ino);
566 f2fs_unlock_op(sbi);
567
568 alloc_nid_done(sbi, inode->i_ino);
569
570 if (whiteout) {
571 inode_dec_link_count(inode);
572 *whiteout = inode;
573 } else {
574 d_tmpfile(dentry, inode);
575 }
576 unlock_new_inode(inode);
577 return 0;
578
579release_out:
580 release_orphan_inode(sbi);
581out:
582 handle_failed_inode(inode);
583 return err;
584}
585
586static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
587{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700588 if (f2fs_encrypted_inode(dir)) {
589 int err = f2fs_get_encryption_info(dir);
590 if (err)
591 return err;
592 }
593
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800594 return __f2fs_tmpfile(dir, dentry, mode, NULL);
595}
596
597static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
598{
599 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
600}
601
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900602static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800603 struct inode *new_dir, struct dentry *new_dentry,
604 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900605{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700606 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000607 struct inode *old_inode = d_inode(old_dentry);
608 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800609 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900610 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800611 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900612 struct f2fs_dir_entry *old_dir_entry = NULL;
613 struct f2fs_dir_entry *old_entry;
614 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800615 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900616
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700617 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
618 !f2fs_is_child_context_consistent_with_parent(new_dir,
619 old_inode)) {
620 err = -EPERM;
621 goto out;
622 }
623
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900624 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
625 if (!old_entry)
626 goto out;
627
628 if (S_ISDIR(old_inode->i_mode)) {
629 err = -EIO;
630 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
631 if (!old_dir_entry)
632 goto out_old;
633 }
634
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800635 if (flags & RENAME_WHITEOUT) {
636 err = f2fs_create_whiteout(old_dir, &whiteout);
637 if (err)
638 goto out_dir;
639 }
640
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900641 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900642
643 err = -ENOTEMPTY;
644 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800645 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900646
647 err = -ENOENT;
648 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
649 &new_page);
650 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800651 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900652
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800653 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800654
Chao Yu12560102014-06-24 14:16:24 +0800655 f2fs_lock_op(sbi);
656
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900657 err = acquire_orphan_inode(sbi);
658 if (err)
659 goto put_out_dir;
660
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700661 if (update_dent_inode(old_inode, new_inode,
662 &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900663 release_orphan_inode(sbi);
664 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900665 }
666
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900667 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
668
669 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900670 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900671 if (old_dir_entry)
672 drop_nlink(new_inode);
673 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900674 up_write(&F2FS_I(new_inode)->i_sem);
675
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900676 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900677
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900678 if (!new_inode->i_nlink)
679 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900680 else
681 release_orphan_inode(sbi);
682
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900683 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900684 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900685 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800686 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800687
Chao Yu12560102014-06-24 14:16:24 +0800688 f2fs_lock_op(sbi);
689
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900690 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800691 if (err) {
692 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800693 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800694 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900695
696 if (old_dir_entry) {
697 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900698 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900699 }
700 }
701
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900702 down_write(&F2FS_I(old_inode)->i_sem);
703 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700704 if (new_inode && file_enc_name(new_inode))
705 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900706 up_write(&F2FS_I(old_inode)->i_sem);
707
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900708 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900709 mark_inode_dirty(old_inode);
710
Chao Yudbeacf02014-09-24 18:17:04 +0800711 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900712
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800713 if (whiteout) {
714 whiteout->i_state |= I_LINKABLE;
715 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
716 err = f2fs_add_link(old_dentry, whiteout);
717 if (err)
718 goto put_out_dir;
719 whiteout->i_state &= ~I_LINKABLE;
720 iput(whiteout);
721 }
722
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900723 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800724 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900725 f2fs_set_link(old_inode, old_dir_entry,
726 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900727 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900728 } else {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800729 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900730 f2fs_put_page(old_dir_page, 0);
731 }
732 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900733 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900734 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900735 }
736
Gu Zhenge4795562013-09-27 18:08:30 +0800737 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800738
739 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
740 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741 return 0;
742
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900743put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800744 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800745 if (new_page) {
746 f2fs_dentry_kunmap(new_dir, new_page);
747 f2fs_put_page(new_page, 0);
748 }
749out_whiteout:
750 if (whiteout)
751 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900752out_dir:
753 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800754 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900755 f2fs_put_page(old_dir_page, 0);
756 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900757out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800758 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900759 f2fs_put_page(old_page, 0);
760out:
761 return err;
762}
763
Chao Yu32f9bc22014-07-12 19:13:54 +0800764static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
765 struct inode *new_dir, struct dentry *new_dentry)
766{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700767 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000768 struct inode *old_inode = d_inode(old_dentry);
769 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800770 struct page *old_dir_page, *new_dir_page;
771 struct page *old_page, *new_page;
772 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
773 struct f2fs_dir_entry *old_entry, *new_entry;
774 int old_nlink = 0, new_nlink = 0;
775 int err = -ENOENT;
776
Chao Yud3baf7c2015-05-25 18:07:02 +0800777 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
778 (old_dir != new_dir) &&
779 (!f2fs_is_child_context_consistent_with_parent(new_dir,
780 old_inode) ||
781 !f2fs_is_child_context_consistent_with_parent(old_dir,
782 new_inode)))
783 return -EPERM;
784
Chao Yu32f9bc22014-07-12 19:13:54 +0800785 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
786 if (!old_entry)
787 goto out;
788
789 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
790 if (!new_entry)
791 goto out_old;
792
793 /* prepare for updating ".." directory entry info later */
794 if (old_dir != new_dir) {
795 if (S_ISDIR(old_inode->i_mode)) {
796 err = -EIO;
797 old_dir_entry = f2fs_parent_dir(old_inode,
798 &old_dir_page);
799 if (!old_dir_entry)
800 goto out_new;
801 }
802
803 if (S_ISDIR(new_inode->i_mode)) {
804 err = -EIO;
805 new_dir_entry = f2fs_parent_dir(new_inode,
806 &new_dir_page);
807 if (!new_dir_entry)
808 goto out_old_dir;
809 }
810 }
811
812 /*
813 * If cross rename between file and directory those are not
814 * in the same directory, we will inc nlink of file's parent
815 * later, so we should check upper boundary of its nlink.
816 */
817 if ((!old_dir_entry || !new_dir_entry) &&
818 old_dir_entry != new_dir_entry) {
819 old_nlink = old_dir_entry ? -1 : 1;
820 new_nlink = -old_nlink;
821 err = -EMLINK;
822 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
823 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
824 goto out_new_dir;
825 }
826
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800827 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800828
Chao Yu32f9bc22014-07-12 19:13:54 +0800829 f2fs_lock_op(sbi);
830
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700831 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800832 if (err)
833 goto out_unlock;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700834 if (file_enc_name(new_inode))
835 file_set_enc_name(old_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800836
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700837 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800838 if (err)
839 goto out_undo;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700840 if (file_enc_name(old_inode))
841 file_set_enc_name(new_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800842
843 /* update ".." directory entry info of old dentry */
844 if (old_dir_entry)
845 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
846
847 /* update ".." directory entry info of new dentry */
848 if (new_dir_entry)
849 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
850
851 /* update directory entry info of old dir inode */
852 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
853
854 down_write(&F2FS_I(old_inode)->i_sem);
855 file_lost_pino(old_inode);
856 up_write(&F2FS_I(old_inode)->i_sem);
857
858 update_inode_page(old_inode);
859
860 old_dir->i_ctime = CURRENT_TIME;
861 if (old_nlink) {
862 down_write(&F2FS_I(old_dir)->i_sem);
863 if (old_nlink < 0)
864 drop_nlink(old_dir);
865 else
866 inc_nlink(old_dir);
867 up_write(&F2FS_I(old_dir)->i_sem);
868 }
869 mark_inode_dirty(old_dir);
870 update_inode_page(old_dir);
871
872 /* update directory entry info of new dir inode */
873 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
874
875 down_write(&F2FS_I(new_inode)->i_sem);
876 file_lost_pino(new_inode);
877 up_write(&F2FS_I(new_inode)->i_sem);
878
879 update_inode_page(new_inode);
880
881 new_dir->i_ctime = CURRENT_TIME;
882 if (new_nlink) {
883 down_write(&F2FS_I(new_dir)->i_sem);
884 if (new_nlink < 0)
885 drop_nlink(new_dir);
886 else
887 inc_nlink(new_dir);
888 up_write(&F2FS_I(new_dir)->i_sem);
889 }
890 mark_inode_dirty(new_dir);
891 update_inode_page(new_dir);
892
893 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800894
895 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
896 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800897 return 0;
898out_undo:
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700899 /*
900 * Still we may fail to recover name info of f2fs_inode here
901 * Drop it, once its name is set as encrypted
902 */
903 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800904out_unlock:
905 f2fs_unlock_op(sbi);
906out_new_dir:
907 if (new_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800908 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800909 f2fs_put_page(new_dir_page, 0);
910 }
911out_old_dir:
912 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800913 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800914 f2fs_put_page(old_dir_page, 0);
915 }
916out_new:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800917 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800918 f2fs_put_page(new_page, 0);
919out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800920 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800921 f2fs_put_page(old_page, 0);
922out:
923 return err;
924}
925
926static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
927 struct inode *new_dir, struct dentry *new_dentry,
928 unsigned int flags)
929{
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800930 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +0800931 return -EINVAL;
932
933 if (flags & RENAME_EXCHANGE) {
934 return f2fs_cross_rename(old_dir, old_dentry,
935 new_dir, new_dentry);
936 }
937 /*
938 * VFS has already handled the new dentry existence case,
939 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
940 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800941 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +0800942}
943
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700944#ifdef CONFIG_F2FS_FS_ENCRYPTION
Al Viro6b255392015-11-17 10:20:54 -0500945static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500946 struct inode *inode,
947 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +0800948{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700949 struct page *cpage = NULL;
950 char *caddr, *paddr = NULL;
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -0800951 struct f2fs_str cstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700952 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700953 struct f2fs_encrypted_symlink_data *sd;
954 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
955 u32 max_size = inode->i_sb->s_blocksize;
956 int res;
Chao Yu50732df2014-06-19 16:23:19 +0800957
Al Viro6b255392015-11-17 10:20:54 -0500958 if (!dentry)
959 return ERR_PTR(-ECHILD);
960
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700961 res = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700962 if (res)
963 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800964
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700965 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
966 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700967 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -0500968 caddr = page_address(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700969 caddr[size] = 0;
Chao Yu50732df2014-06-19 16:23:19 +0800970
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700971 /* Symlink is encrypted */
972 sd = (struct f2fs_encrypted_symlink_data *)caddr;
Chao Yu922ec352016-02-15 17:54:26 +0800973 cstr.name = sd->encrypted_path;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700974 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -0800975
976 /* this is broken symlink case */
977 if (unlikely(cstr.len == 0)) {
978 res = -ENOENT;
979 goto errout;
980 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700981
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700982 /* this is broken symlink case */
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -0800983 if (unlikely(cstr.name[0] == 0)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700984 res = -ENOENT;
985 goto errout;
986 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700987
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700988 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
989 max_size) {
990 /* Symlink data on the disk is corrupted */
991 res = -EIO;
992 goto errout;
993 }
994 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
995 if (res)
996 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +0800997
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700998 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
999 if (res < 0)
1000 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +08001001
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001002 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001003
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001004 /* Null-terminate the name */
1005 paddr[res] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001006
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001007 page_cache_release(cpage);
Al Virofceef392015-12-29 15:58:39 -05001008 set_delayed_call(done, kfree_link, paddr);
1009 return paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001010errout:
1011 f2fs_fname_crypto_free_buffer(&pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001012 page_cache_release(cpage);
1013 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001014}
1015
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001016const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1017 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001018 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001019 .getattr = f2fs_getattr,
1020 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001021#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001022 .setxattr = generic_setxattr,
1023 .getxattr = generic_getxattr,
1024 .listxattr = f2fs_listxattr,
1025 .removexattr = generic_removexattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001026#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001027};
1028#endif
1029
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001030const struct inode_operations f2fs_dir_inode_operations = {
1031 .create = f2fs_create,
1032 .lookup = f2fs_lookup,
1033 .link = f2fs_link,
1034 .unlink = f2fs_unlink,
1035 .symlink = f2fs_symlink,
1036 .mkdir = f2fs_mkdir,
1037 .rmdir = f2fs_rmdir,
1038 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001039 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001040 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001041 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001042 .setattr = f2fs_setattr,
1043 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001044 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001045#ifdef CONFIG_F2FS_FS_XATTR
1046 .setxattr = generic_setxattr,
1047 .getxattr = generic_getxattr,
1048 .listxattr = f2fs_listxattr,
1049 .removexattr = generic_removexattr,
1050#endif
1051};
1052
1053const struct inode_operations f2fs_symlink_inode_operations = {
1054 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001055 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001056 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001057 .setattr = f2fs_setattr,
1058#ifdef CONFIG_F2FS_FS_XATTR
1059 .setxattr = generic_setxattr,
1060 .getxattr = generic_getxattr,
1061 .listxattr = f2fs_listxattr,
1062 .removexattr = generic_removexattr,
1063#endif
1064};
1065
1066const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001067 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001068 .setattr = f2fs_setattr,
1069 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001070 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001071#ifdef CONFIG_F2FS_FS_XATTR
1072 .setxattr = generic_setxattr,
1073 .getxattr = generic_getxattr,
1074 .listxattr = f2fs_listxattr,
1075 .removexattr = generic_removexattr,
1076#endif
1077};