blob: 618829e8049c09de846972a5438e92fca19dbcc9 [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
Jaegeuk Kim91942322016-05-20 10:13:22 -070063 set_inode_flag(inode, FI_NEW_INODE);
64
65 if (test_opt(sbi, INLINE_XATTR))
66 set_inode_flag(inode, FI_INLINE_XATTR);
Chao Yufba48a82015-12-23 17:51:35 +080067 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070068 set_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070069 if (f2fs_may_inline_dentry(inode))
Jaegeuk Kim91942322016-05-20 10:13:22 -070070 set_inode_flag(inode, FI_INLINE_DENTRY);
Chao Yu622f28a2014-09-24 18:19:10 +080071
Jaegeuk Kim3e72f722015-06-19 17:53:26 -070072 f2fs_init_extent_tree(inode, NULL);
73
Chao Yud5e8f6c2015-07-15 17:28:53 +080074 stat_inc_inline_xattr(inode);
Jaegeuk Kim2fb2c952015-04-30 18:58:22 -070075 stat_inc_inline_inode(inode);
76 stat_inc_inline_dir(inode);
77
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090078 trace_f2fs_new_inode(inode, 0);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090079 return inode;
80
Jaegeuk Kim57397d82012-11-02 17:11:10 +090081fail:
Jaegeuk Kimd70b4f52013-04-25 13:24:33 +090082 trace_f2fs_new_inode(inode, err);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +090083 make_bad_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090084 if (nid_free)
Jaegeuk Kim91942322016-05-20 10:13:22 -070085 set_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -070086 iput(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090087 return ERR_PTR(err);
88}
89
90static int is_multimedia_file(const unsigned char *s, const char *sub)
91{
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020092 size_t slen = strlen(s);
93 size_t sublen = strlen(sub);
Jaegeuk Kim57397d82012-11-02 17:11:10 +090094
Chao Yu741a7be2015-07-06 20:30:40 +080095 /*
96 * filename format of multimedia file should be defined as:
97 * "filename + '.' + extension".
98 */
99 if (slen < sublen + 2)
100 return 0;
101
102 if (s[slen - sublen - 1] != '.')
Namjae Jeone66509f02013-04-20 01:27:21 +0900103 return 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900104
Gu Zheng749ebfd2013-08-22 18:11:25 +0800105 return !strncasecmp(s + slen - sublen, sub, sublen);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900106}
107
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900108/*
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900109 * Set multimedia files as cold files for hot/cold data separation
110 */
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900111static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900112 const unsigned char *name)
113{
114 int i;
115 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
116
117 int count = le32_to_cpu(sbi->raw_super->extension_count);
118 for (i = 0; i < count; i++) {
Namjae Jeone66509f02013-04-20 01:27:21 +0900119 if (is_multimedia_file(name, extlist[i])) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900120 file_set_cold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900121 break;
122 }
123 }
124}
125
126static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
127 bool excl)
128{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700129 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900130 struct inode *inode;
131 nid_t ino = 0;
Gu Zhenge4795562013-09-27 18:08:30 +0800132 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900133
134 inode = f2fs_new_inode(dir, mode);
135 if (IS_ERR(inode))
136 return PTR_ERR(inode);
137
138 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900139 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900140
141 inode->i_op = &f2fs_file_inode_operations;
142 inode->i_fop = &f2fs_file_operations;
143 inode->i_mapping->a_ops = &f2fs_dblock_aops;
144 ino = inode->i_ino;
145
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800146 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800147
Gu Zhenge4795562013-09-27 18:08:30 +0800148 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900149 err = f2fs_add_link(dentry, inode);
150 if (err)
151 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700152 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900153
154 alloc_nid_done(sbi, ino);
155
Jaegeuk Kim0a364af2013-05-16 08:57:43 +0900156 d_instantiate(dentry, inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900157 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800158
159 if (IS_DIRSYNC(dir))
160 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900161 return 0;
162out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700163 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900164 return err;
165}
166
167static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
168 struct dentry *dentry)
169{
David Howells2b0143b2015-03-17 22:25:59 +0000170 struct inode *inode = d_inode(old_dentry);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700171 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800172 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900173
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700174 if (f2fs_encrypted_inode(dir) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700175 !fscrypt_has_permitted_context(dir, inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700176 return -EPERM;
177
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800178 f2fs_balance_fs(sbi, true);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900179
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900180 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6f6fd832013-05-22 12:06:26 +0900181 ihold(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900182
Jaegeuk Kim91942322016-05-20 10:13:22 -0700183 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800184 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900185 err = f2fs_add_link(dentry, inode);
186 if (err)
187 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700188 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900189
190 d_instantiate(dentry, inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800191
192 if (IS_DIRSYNC(dir))
193 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900194 return 0;
195out:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700196 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900197 iput(inode);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700198 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900199 return err;
200}
201
202struct dentry *f2fs_get_parent(struct dentry *child)
203{
204 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +0000205 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900206 if (!ino)
207 return ERR_PTR(-ENOENT);
Al Virofc640052016-04-10 01:33:30 -0400208 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900209}
210
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700211static int __recover_dot_dentries(struct inode *dir, nid_t pino)
212{
213 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
214 struct qstr dot = QSTR_INIT(".", 1);
215 struct qstr dotdot = QSTR_INIT("..", 2);
216 struct f2fs_dir_entry *de;
217 struct page *page;
218 int err = 0;
219
Chao Yu4e0d8362015-12-30 17:40:31 +0800220 if (f2fs_readonly(sbi->sb)) {
221 f2fs_msg(sbi->sb, KERN_INFO,
222 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
223 "in readonly mountpoint", dir->i_ino, pino);
224 return 0;
225 }
226
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800227 f2fs_balance_fs(sbi, true);
Chao Yud5384172015-12-24 18:03:29 +0800228
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700229 f2fs_lock_op(sbi);
230
231 de = f2fs_find_entry(dir, &dot, &page);
232 if (de) {
233 f2fs_dentry_kunmap(dir, page);
234 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700235 } else if (IS_ERR(page)) {
236 err = PTR_ERR(page);
237 goto out;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700238 } else {
239 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
240 if (err)
241 goto out;
242 }
243
244 de = f2fs_find_entry(dir, &dotdot, &page);
245 if (de) {
246 f2fs_dentry_kunmap(dir, page);
247 f2fs_put_page(page, 0);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700248 } else if (IS_ERR(page)) {
249 err = PTR_ERR(page);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700250 } else {
251 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
252 }
253out:
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700254 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 clear_inode_flag(dir, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700256
257 f2fs_unlock_op(sbi);
258 return err;
259}
260
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900261static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
262 unsigned int flags)
263{
264 struct inode *inode = NULL;
265 struct f2fs_dir_entry *de;
266 struct page *page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700267 nid_t ino;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700268 int err = 0;
Liu Xue8c2b1432016-02-26 06:39:23 +0000269 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900270
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800271 if (f2fs_encrypted_inode(dir)) {
272 int res = fscrypt_get_encryption_info(dir);
273
274 /*
275 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
276 * created while the directory was encrypted and we
277 * don't have access to the key.
278 */
279 if (fscrypt_has_encryption_key(dir))
280 fscrypt_set_encrypted_dentry(dentry);
281 fscrypt_set_d_op(dentry);
282 if (res && res != -ENOKEY)
283 return ERR_PTR(res);
284 }
285
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900286 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900287 return ERR_PTR(-ENAMETOOLONG);
288
289 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700290 if (!de) {
291 if (IS_ERR(page))
292 return (struct dentry *)page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700293 return d_splice_alias(inode, dentry);
Jaegeuk Kimeb4246d2016-05-27 10:10:41 -0700294 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900295
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700296 ino = le32_to_cpu(de->ino);
297 f2fs_dentry_kunmap(dir, page);
298 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700299
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700300 inode = f2fs_iget(dir->i_sb, ino);
301 if (IS_ERR(inode))
302 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700303
Liu Xue8c2b1432016-02-26 06:39:23 +0000304 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
305 err = __recover_dot_dentries(dir, root_ino);
306 if (err)
307 goto err_out;
308 }
309
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700310 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700311 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700312 if (err)
313 goto err_out;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900314 }
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800315 if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
316 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
317 !fscrypt_has_permitted_context(dir, inode)) {
318 bool nokey = f2fs_encrypted_inode(inode) &&
319 !fscrypt_has_encryption_key(inode);
Chao Yud7267322016-03-10 22:24:23 +0800320 err = nokey ? -ENOKEY : -EPERM;
321 goto err_out;
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800322 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900323 return d_splice_alias(inode, dentry);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700324
325err_out:
Chao Yud7267322016-03-10 22:24:23 +0800326 iput(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700327 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900328}
329
330static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
331{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700332 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000333 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900334 struct f2fs_dir_entry *de;
335 struct page *page;
336 int err = -ENOENT;
337
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900338 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900339
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900340 de = f2fs_find_entry(dir, &dentry->d_name, &page);
341 if (!de)
342 goto fail;
343
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800344 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800345
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900346 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900347 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900348 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900349 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800350 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900351 f2fs_put_page(page, 0);
352 goto fail;
353 }
Chao Yudbeacf02014-09-24 18:17:04 +0800354 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800355 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900356
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800357 if (IS_DIRSYNC(dir))
358 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900359fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900360 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361 return err;
362}
363
Al Viro6b255392015-11-17 10:20:54 -0500364static const char *f2fs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500365 struct inode *inode,
366 struct delayed_call *done)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700367{
Al Virofceef392015-12-29 15:58:39 -0500368 const char *link = page_get_link(dentry, inode, done);
Al Viro680baac2015-05-02 13:32:22 -0400369 if (!IS_ERR(link) && !*link) {
370 /* this is broken symlink case */
Al Virofceef392015-12-29 15:58:39 -0500371 do_delayed_call(done);
372 clear_delayed_call(done);
Al Viro680baac2015-05-02 13:32:22 -0400373 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700374 }
Al Viro680baac2015-05-02 13:32:22 -0400375 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700376}
377
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900378static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
379 const char *symname)
380{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700381 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900382 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700383 size_t len = strlen(symname);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700384 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
385 struct fscrypt_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800386 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900387
Chao Yu922ec352016-02-15 17:54:26 +0800388 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700389 err = fscrypt_get_encryption_info(dir);
Chao Yu922ec352016-02-15 17:54:26 +0800390 if (err)
391 return err;
392
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700393 if (!fscrypt_has_encryption_key(dir))
Chao Yu922ec352016-02-15 17:54:26 +0800394 return -EPERM;
395
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700396 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
397 sizeof(struct fscrypt_symlink_data));
Chao Yu922ec352016-02-15 17:54:26 +0800398 }
399
400 if (disk_link.len > dir->i_sb->s_blocksize)
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700401 return -ENAMETOOLONG;
402
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900403 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
404 if (IS_ERR(inode))
405 return PTR_ERR(inode);
406
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700407 if (f2fs_encrypted_inode(inode))
408 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
409 else
410 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500411 inode_nohighmem(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900412 inode->i_mapping->a_ops = &f2fs_dblock_aops;
413
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800414 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800415
Gu Zhenge4795562013-09-27 18:08:30 +0800416 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900417 err = f2fs_add_link(dentry, inode);
418 if (err)
419 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700420 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900421 alloc_nid_done(sbi, inode->i_ino);
422
Chao Yu922ec352016-02-15 17:54:26 +0800423 if (f2fs_encrypted_inode(inode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700424 struct qstr istr = QSTR_INIT(symname, len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700425 struct fscrypt_str ostr;
Chao Yu922ec352016-02-15 17:54:26 +0800426
427 sd = kzalloc(disk_link.len, GFP_NOFS);
428 if (!sd) {
429 err = -ENOMEM;
430 goto err_out;
431 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700432
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700433 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700434 if (err)
435 goto err_out;
436
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700437 if (!fscrypt_has_encryption_key(inode)) {
Chao Yu922ec352016-02-15 17:54:26 +0800438 err = -EPERM;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700439 goto err_out;
Chao Yu922ec352016-02-15 17:54:26 +0800440 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700441
Chao Yu922ec352016-02-15 17:54:26 +0800442 ostr.name = sd->encrypted_path;
443 ostr.len = disk_link.len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700444 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700445 if (err < 0)
446 goto err_out;
447
Chao Yu922ec352016-02-15 17:54:26 +0800448 sd->len = cpu_to_le16(ostr.len);
449 disk_link.name = (char *)sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700450 }
451
Chao Yu922ec352016-02-15 17:54:26 +0800452 err = page_symlink(inode, disk_link.name, disk_link.len);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700453
454err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900455 d_instantiate(dentry, inode);
456 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800457
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700458 /*
459 * Let's flush symlink data in order to avoid broken symlink as much as
460 * possible. Nevertheless, fsyncing is the best way, but there is no
461 * way to get a file descriptor in order to flush that.
462 *
463 * Note that, it needs to do dir->fsync to make this recoverable.
464 * If the symlink path is stored into inline_data, there is no
465 * performance regression.
466 */
Chao Yua6be0142015-10-22 18:23:08 +0800467 if (!err) {
Chao Yu922ec352016-02-15 17:54:26 +0800468 filemap_write_and_wait_range(inode->i_mapping, 0,
469 disk_link.len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700470
Chao Yua6be0142015-10-22 18:23:08 +0800471 if (IS_DIRSYNC(dir))
472 f2fs_sync_fs(sbi->sb, 1);
473 } else {
474 f2fs_unlink(dir, dentry);
475 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700476
477 kfree(sd);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900478 return err;
479out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700480 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900481 return err;
482}
483
484static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
485{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700486 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900487 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800488 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900489
490 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900491 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900492 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900493
494 inode->i_op = &f2fs_dir_inode_operations;
495 inode->i_fop = &f2fs_dir_operations;
496 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800497 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900498
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800499 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800500
Jaegeuk Kim91942322016-05-20 10:13:22 -0700501 set_inode_flag(inode, FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800502 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900503 err = f2fs_add_link(dentry, inode);
504 if (err)
505 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700506 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900507
508 alloc_nid_done(sbi, inode->i_ino);
509
510 d_instantiate(dentry, inode);
511 unlock_new_inode(inode);
512
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800513 if (IS_DIRSYNC(dir))
514 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900515 return 0;
516
517out_fail:
Jaegeuk Kim91942322016-05-20 10:13:22 -0700518 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700519 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900520 return err;
521}
522
523static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
524{
David Howells2b0143b2015-03-17 22:25:59 +0000525 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900526 if (f2fs_empty_dir(inode))
527 return f2fs_unlink(dir, dentry);
528 return -ENOTEMPTY;
529}
530
531static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
532 umode_t mode, dev_t rdev)
533{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700534 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900535 struct inode *inode;
536 int err = 0;
537
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900538 inode = f2fs_new_inode(dir, mode);
539 if (IS_ERR(inode))
540 return PTR_ERR(inode);
541
542 init_special_inode(inode, inode->i_mode, rdev);
543 inode->i_op = &f2fs_special_inode_operations;
544
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800545 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800546
Gu Zhenge4795562013-09-27 18:08:30 +0800547 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900548 err = f2fs_add_link(dentry, inode);
549 if (err)
550 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700551 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900552
553 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800554
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900555 d_instantiate(dentry, inode);
556 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800557
558 if (IS_DIRSYNC(dir))
559 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900560 return 0;
561out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700562 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900563 return err;
564}
565
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800566static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
567 umode_t mode, struct inode **whiteout)
568{
569 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
570 struct inode *inode;
571 int err;
572
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800573 inode = f2fs_new_inode(dir, mode);
574 if (IS_ERR(inode))
575 return PTR_ERR(inode);
576
577 if (whiteout) {
578 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
579 inode->i_op = &f2fs_special_inode_operations;
580 } else {
581 inode->i_op = &f2fs_file_inode_operations;
582 inode->i_fop = &f2fs_file_operations;
583 inode->i_mapping->a_ops = &f2fs_dblock_aops;
584 }
585
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800586 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800587
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800588 f2fs_lock_op(sbi);
589 err = acquire_orphan_inode(sbi);
590 if (err)
591 goto out;
592
593 err = f2fs_do_tmpfile(inode, dir);
594 if (err)
595 goto release_out;
596
597 /*
598 * add this non-linked tmpfile to orphan list, in this way we could
599 * remove all unused data of tmpfile after abnormal power-off.
600 */
601 add_orphan_inode(sbi, inode->i_ino);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800602 alloc_nid_done(sbi, inode->i_ino);
603
604 if (whiteout) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700605 f2fs_i_links_write(inode, false);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800606 *whiteout = inode;
607 } else {
608 d_tmpfile(dentry, inode);
609 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700610 /* link_count was changed by d_tmpfile as well. */
611 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800612 unlock_new_inode(inode);
613 return 0;
614
615release_out:
616 release_orphan_inode(sbi);
617out:
618 handle_failed_inode(inode);
619 return err;
620}
621
622static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
623{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700624 if (f2fs_encrypted_inode(dir)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700625 int err = fscrypt_get_encryption_info(dir);
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700626 if (err)
627 return err;
628 }
629
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800630 return __f2fs_tmpfile(dir, dentry, mode, NULL);
631}
632
633static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
634{
635 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
636}
637
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900638static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800639 struct inode *new_dir, struct dentry *new_dentry,
640 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900641{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700642 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000643 struct inode *old_inode = d_inode(old_dentry);
644 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800645 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900646 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800647 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900648 struct f2fs_dir_entry *old_dir_entry = NULL;
649 struct f2fs_dir_entry *old_entry;
650 struct f2fs_dir_entry *new_entry;
Chao Yu993a0492016-02-17 16:47:05 +0800651 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
Gu Zhenge4795562013-09-27 18:08:30 +0800652 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900653
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700654 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700655 !fscrypt_has_permitted_context(new_dir, old_inode)) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700656 err = -EPERM;
657 goto out;
658 }
659
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900660 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
661 if (!old_entry)
662 goto out;
663
664 if (S_ISDIR(old_inode->i_mode)) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900665 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700666 if (!old_dir_entry) {
667 err = PTR_ERR(old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900668 goto out_old;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700669 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900670 }
671
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800672 if (flags & RENAME_WHITEOUT) {
673 err = f2fs_create_whiteout(old_dir, &whiteout);
674 if (err)
675 goto out_dir;
676 }
677
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900678 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900679
680 err = -ENOTEMPTY;
681 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800682 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900683
684 err = -ENOENT;
685 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
686 &new_page);
687 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800688 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900689
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800690 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800691
Chao Yu12560102014-06-24 14:16:24 +0800692 f2fs_lock_op(sbi);
693
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900694 err = acquire_orphan_inode(sbi);
695 if (err)
696 goto put_out_dir;
697
Chao Yu9def1e92016-02-17 16:45:44 +0800698 err = update_dent_inode(old_inode, new_inode,
699 &new_dentry->d_name);
700 if (err) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900701 release_orphan_inode(sbi);
702 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900703 }
704
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900705 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
706
707 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900708 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900709 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700710 f2fs_i_links_write(new_inode, false);
711 f2fs_i_links_write(new_inode, false);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900712 up_write(&F2FS_I(new_inode)->i_sem);
713
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900714 if (!new_inode->i_nlink)
715 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900716 else
717 release_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900718 } else {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800719 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800720
Chao Yu12560102014-06-24 14:16:24 +0800721 f2fs_lock_op(sbi);
722
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900723 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800724 if (err) {
725 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800726 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800727 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900728
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700729 if (old_dir_entry)
Jaegeuk Kima1961242016-05-20 09:43:20 -0700730 f2fs_i_links_write(new_dir, true);
Chao Yu993a0492016-02-17 16:47:05 +0800731
732 /*
733 * old entry and new entry can locate in the same inline
734 * dentry in inode, when attaching new entry in inline dentry,
735 * it could force inline dentry conversion, after that,
736 * old_entry and old_page will point to wrong address, in
737 * order to avoid this, let's do the check and update here.
738 */
739 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
740 f2fs_put_page(old_page, 0);
741 old_page = NULL;
742
743 old_entry = f2fs_find_entry(old_dir,
744 &old_dentry->d_name, &old_page);
745 if (!old_entry) {
746 err = -EIO;
747 f2fs_unlock_op(sbi);
748 goto out_whiteout;
749 }
750 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900751 }
752
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900753 down_write(&F2FS_I(old_inode)->i_sem);
754 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700755 if (new_inode && file_enc_name(new_inode))
756 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900757 up_write(&F2FS_I(old_inode)->i_sem);
758
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900759 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700760 mark_inode_dirty_sync(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900761
Chao Yudbeacf02014-09-24 18:17:04 +0800762 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900763
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800764 if (whiteout) {
765 whiteout->i_state |= I_LINKABLE;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700766 set_inode_flag(whiteout, FI_INC_LINK);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800767 err = f2fs_add_link(old_dentry, whiteout);
768 if (err)
769 goto put_out_dir;
770 whiteout->i_state &= ~I_LINKABLE;
771 iput(whiteout);
772 }
773
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900774 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800775 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900776 f2fs_set_link(old_inode, old_dir_entry,
777 old_dir_page, new_dir);
778 } else {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800779 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900780 f2fs_put_page(old_dir_page, 0);
781 }
Jaegeuk Kima1961242016-05-20 09:43:20 -0700782 f2fs_i_links_write(old_dir, false);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900783 }
784
Gu Zhenge4795562013-09-27 18:08:30 +0800785 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800786
787 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
788 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900789 return 0;
790
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900791put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800792 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800793 if (new_page) {
794 f2fs_dentry_kunmap(new_dir, new_page);
795 f2fs_put_page(new_page, 0);
796 }
797out_whiteout:
798 if (whiteout)
799 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900800out_dir:
801 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800802 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900803 f2fs_put_page(old_dir_page, 0);
804 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900805out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800806 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900807 f2fs_put_page(old_page, 0);
808out:
809 return err;
810}
811
Chao Yu32f9bc22014-07-12 19:13:54 +0800812static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
813 struct inode *new_dir, struct dentry *new_dentry)
814{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700815 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000816 struct inode *old_inode = d_inode(old_dentry);
817 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800818 struct page *old_dir_page, *new_dir_page;
819 struct page *old_page, *new_page;
820 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
821 struct f2fs_dir_entry *old_entry, *new_entry;
822 int old_nlink = 0, new_nlink = 0;
823 int err = -ENOENT;
824
Chao Yud3baf7c2015-05-25 18:07:02 +0800825 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700826 (old_dir != new_dir) &&
827 (!fscrypt_has_permitted_context(new_dir, old_inode) ||
828 !fscrypt_has_permitted_context(old_dir, new_inode)))
Chao Yud3baf7c2015-05-25 18:07:02 +0800829 return -EPERM;
830
Chao Yu32f9bc22014-07-12 19:13:54 +0800831 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
832 if (!old_entry)
833 goto out;
834
835 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
836 if (!new_entry)
837 goto out_old;
838
839 /* prepare for updating ".." directory entry info later */
840 if (old_dir != new_dir) {
841 if (S_ISDIR(old_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +0800842 old_dir_entry = f2fs_parent_dir(old_inode,
843 &old_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700844 if (!old_dir_entry) {
845 err = PTR_ERR(old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800846 goto out_new;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700847 }
Chao Yu32f9bc22014-07-12 19:13:54 +0800848 }
849
850 if (S_ISDIR(new_inode->i_mode)) {
Chao Yu32f9bc22014-07-12 19:13:54 +0800851 new_dir_entry = f2fs_parent_dir(new_inode,
852 &new_dir_page);
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700853 if (!new_dir_entry) {
854 err = PTR_ERR(new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800855 goto out_old_dir;
Jaegeuk Kim3e198862016-06-09 14:57:19 -0700856 }
Chao Yu32f9bc22014-07-12 19:13:54 +0800857 }
858 }
859
860 /*
861 * If cross rename between file and directory those are not
862 * in the same directory, we will inc nlink of file's parent
863 * later, so we should check upper boundary of its nlink.
864 */
865 if ((!old_dir_entry || !new_dir_entry) &&
866 old_dir_entry != new_dir_entry) {
867 old_nlink = old_dir_entry ? -1 : 1;
868 new_nlink = -old_nlink;
869 err = -EMLINK;
870 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
871 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
872 goto out_new_dir;
873 }
874
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800875 f2fs_balance_fs(sbi, true);
Jaegeuk Kim00623e62015-12-22 11:56:08 -0800876
Chao Yu32f9bc22014-07-12 19:13:54 +0800877 f2fs_lock_op(sbi);
878
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700879 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800880 if (err)
881 goto out_unlock;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700882 if (file_enc_name(new_inode))
883 file_set_enc_name(old_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800884
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700885 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800886 if (err)
887 goto out_undo;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700888 if (file_enc_name(old_inode))
889 file_set_enc_name(new_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800890
891 /* update ".." directory entry info of old dentry */
892 if (old_dir_entry)
893 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
894
895 /* update ".." directory entry info of new dentry */
896 if (new_dir_entry)
897 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
898
899 /* update directory entry info of old dir inode */
900 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
901
902 down_write(&F2FS_I(old_inode)->i_sem);
903 file_lost_pino(old_inode);
904 up_write(&F2FS_I(old_inode)->i_sem);
905
Chao Yu32f9bc22014-07-12 19:13:54 +0800906 old_dir->i_ctime = CURRENT_TIME;
907 if (old_nlink) {
908 down_write(&F2FS_I(old_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700909 f2fs_i_links_write(old_dir, old_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +0800910 up_write(&F2FS_I(old_dir)->i_sem);
911 }
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700912 mark_inode_dirty_sync(old_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +0800913
914 /* update directory entry info of new dir inode */
915 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
916
917 down_write(&F2FS_I(new_inode)->i_sem);
918 file_lost_pino(new_inode);
919 up_write(&F2FS_I(new_inode)->i_sem);
920
Chao Yu32f9bc22014-07-12 19:13:54 +0800921 new_dir->i_ctime = CURRENT_TIME;
922 if (new_nlink) {
923 down_write(&F2FS_I(new_dir)->i_sem);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700924 f2fs_i_links_write(new_dir, new_nlink > 0);
Chao Yu32f9bc22014-07-12 19:13:54 +0800925 up_write(&F2FS_I(new_dir)->i_sem);
926 }
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700927 mark_inode_dirty_sync(new_dir);
Chao Yu32f9bc22014-07-12 19:13:54 +0800928
929 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800930
931 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
932 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800933 return 0;
934out_undo:
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700935 /*
936 * Still we may fail to recover name info of f2fs_inode here
937 * Drop it, once its name is set as encrypted
938 */
939 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800940out_unlock:
941 f2fs_unlock_op(sbi);
942out_new_dir:
943 if (new_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800944 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800945 f2fs_put_page(new_dir_page, 0);
946 }
947out_old_dir:
948 if (old_dir_entry) {
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800949 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800950 f2fs_put_page(old_dir_page, 0);
951 }
952out_new:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800953 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800954 f2fs_put_page(new_page, 0);
955out_old:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800956 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800957 f2fs_put_page(old_page, 0);
958out:
959 return err;
960}
961
962static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
963 struct inode *new_dir, struct dentry *new_dentry,
964 unsigned int flags)
965{
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800966 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +0800967 return -EINVAL;
968
969 if (flags & RENAME_EXCHANGE) {
970 return f2fs_cross_rename(old_dir, old_dentry,
971 new_dir, new_dentry);
972 }
973 /*
974 * VFS has already handled the new dentry existence case,
975 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
976 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800977 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +0800978}
979
Al Viro6b255392015-11-17 10:20:54 -0500980static const char *f2fs_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500981 struct inode *inode,
982 struct delayed_call *done)
Chao Yu50732df2014-06-19 16:23:19 +0800983{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700984 struct page *cpage = NULL;
985 char *caddr, *paddr = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700986 struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
987 struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
988 struct fscrypt_symlink_data *sd;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700989 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
990 u32 max_size = inode->i_sb->s_blocksize;
991 int res;
Chao Yu50732df2014-06-19 16:23:19 +0800992
Al Viro6b255392015-11-17 10:20:54 -0500993 if (!dentry)
994 return ERR_PTR(-ECHILD);
995
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700996 res = fscrypt_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700997 if (res)
998 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800999
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001000 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1001 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -07001002 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -05001003 caddr = page_address(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001004 caddr[size] = 0;
Chao Yu50732df2014-06-19 16:23:19 +08001005
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001006 /* Symlink is encrypted */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001007 sd = (struct fscrypt_symlink_data *)caddr;
Chao Yu922ec352016-02-15 17:54:26 +08001008 cstr.name = sd->encrypted_path;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001009 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kim8dc0d6a2015-12-24 16:13:09 -08001010
1011 /* this is broken symlink case */
1012 if (unlikely(cstr.len == 0)) {
1013 res = -ENOENT;
1014 goto errout;
1015 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001016
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001017 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001018 /* Symlink data on the disk is corrupted */
1019 res = -EIO;
1020 goto errout;
1021 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001022 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001023 if (res)
1024 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +08001025
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001026 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001027 if (res < 0)
1028 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +08001029
Jaegeuk Kimc90e09f2016-03-30 13:13:16 -07001030 /* this is broken symlink case */
1031 if (unlikely(pstr.name[0] == 0)) {
1032 res = -ENOENT;
1033 goto errout;
1034 }
1035
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001036 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -07001037
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001038 /* Null-terminate the name */
1039 paddr[res] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 put_page(cpage);
Al Virofceef392015-12-29 15:58:39 -05001042 set_delayed_call(done, kfree_link, paddr);
1043 return paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001044errout:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001045 fscrypt_fname_free_buffer(&pstr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001046 put_page(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001047 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +08001048}
1049
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001050const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1051 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001052 .get_link = f2fs_encrypted_get_link,
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001053 .getattr = f2fs_getattr,
1054 .setattr = f2fs_setattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001055#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001056 .setxattr = generic_setxattr,
1057 .getxattr = generic_getxattr,
1058 .listxattr = f2fs_listxattr,
1059 .removexattr = generic_removexattr,
Chao Yu3a9e6432015-12-31 18:20:10 +08001060#endif
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001061};
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07001062
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001063const struct inode_operations f2fs_dir_inode_operations = {
1064 .create = f2fs_create,
1065 .lookup = f2fs_lookup,
1066 .link = f2fs_link,
1067 .unlink = f2fs_unlink,
1068 .symlink = f2fs_symlink,
1069 .mkdir = f2fs_mkdir,
1070 .rmdir = f2fs_rmdir,
1071 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001072 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001073 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001074 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001075 .setattr = f2fs_setattr,
1076 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001077 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001078#ifdef CONFIG_F2FS_FS_XATTR
1079 .setxattr = generic_setxattr,
1080 .getxattr = generic_getxattr,
1081 .listxattr = f2fs_listxattr,
1082 .removexattr = generic_removexattr,
1083#endif
1084};
1085
1086const struct inode_operations f2fs_symlink_inode_operations = {
1087 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001088 .get_link = f2fs_get_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001089 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001090 .setattr = f2fs_setattr,
1091#ifdef CONFIG_F2FS_FS_XATTR
1092 .setxattr = generic_setxattr,
1093 .getxattr = generic_getxattr,
1094 .listxattr = f2fs_listxattr,
1095 .removexattr = generic_removexattr,
1096#endif
1097};
1098
1099const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001100 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001101 .setattr = f2fs_setattr,
1102 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001103 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001104#ifdef CONFIG_F2FS_FS_XATTR
1105 .setxattr = generic_setxattr,
1106 .getxattr = generic_getxattr,
1107 .listxattr = f2fs_listxattr,
1108 .removexattr = generic_removexattr,
1109#endif
1110};