blob: e48b80c490905ac500958b9f7b0a74de38dc8463 [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 Kim01b960e2015-04-23 10:27:21 -070063 if (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
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900131 f2fs_balance_fs(sbi);
132
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900133 inode = f2fs_new_inode(dir, mode);
134 if (IS_ERR(inode))
135 return PTR_ERR(inode);
136
137 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900138 set_cold_files(sbi, inode, dentry->d_name.name);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900139
140 inode->i_op = &f2fs_file_inode_operations;
141 inode->i_fop = &f2fs_file_operations;
142 inode->i_mapping->a_ops = &f2fs_dblock_aops;
143 ino = inode->i_ino;
144
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 Kim1efef832012-12-19 16:25:21 +0900175 f2fs_balance_fs(sbi);
176
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
217 f2fs_lock_op(sbi);
218
219 de = f2fs_find_entry(dir, &dot, &page);
220 if (de) {
221 f2fs_dentry_kunmap(dir, page);
222 f2fs_put_page(page, 0);
223 } else {
224 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
225 if (err)
226 goto out;
227 }
228
229 de = f2fs_find_entry(dir, &dotdot, &page);
230 if (de) {
231 f2fs_dentry_kunmap(dir, page);
232 f2fs_put_page(page, 0);
233 } else {
234 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
235 }
236out:
237 if (!err) {
238 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
239 mark_inode_dirty(dir);
240 }
241
242 f2fs_unlock_op(sbi);
243 return err;
244}
245
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900246static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
247 unsigned int flags)
248{
249 struct inode *inode = NULL;
250 struct f2fs_dir_entry *de;
251 struct page *page;
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700252 nid_t ino;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700253 int err = 0;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900254
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900255 if (dentry->d_name.len > F2FS_NAME_LEN)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900256 return ERR_PTR(-ENAMETOOLONG);
257
258 de = f2fs_find_entry(dir, &dentry->d_name, &page);
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700259 if (!de)
260 return d_splice_alias(inode, dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900261
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700262 ino = le32_to_cpu(de->ino);
263 f2fs_dentry_kunmap(dir, page);
264 f2fs_put_page(page, 0);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700265
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700266 inode = f2fs_iget(dir->i_sb, ino);
267 if (IS_ERR(inode))
268 return ERR_CAST(inode);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700269
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700270 if (f2fs_has_inline_dots(inode)) {
Jaegeuk Kim06957e82015-04-22 11:40:27 -0700271 err = __recover_dot_dentries(inode, dir->i_ino);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700272 if (err)
273 goto err_out;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900274 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900275 return d_splice_alias(inode, dentry);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700276
277err_out:
278 iget_failed(inode);
279 return ERR_PTR(err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900280}
281
282static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
283{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700284 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000285 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900286 struct f2fs_dir_entry *de;
287 struct page *page;
288 int err = -ENOENT;
289
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900290 trace_f2fs_unlink_enter(dir, dentry);
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900291 f2fs_balance_fs(sbi);
292
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900293 de = f2fs_find_entry(dir, &dentry->d_name, &page);
294 if (!de)
295 goto fail;
296
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900297 f2fs_lock_op(sbi);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900298 err = acquire_orphan_inode(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900299 if (err) {
Jaegeuk Kimccaaca22013-10-08 10:19:28 +0900300 f2fs_unlock_op(sbi);
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800301 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900302 f2fs_put_page(page, 0);
303 goto fail;
304 }
Chao Yudbeacf02014-09-24 18:17:04 +0800305 f2fs_delete_entry(de, page, dir, inode);
Gu Zhenge4795562013-09-27 18:08:30 +0800306 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900307
arter97e1c42042014-08-06 23:22:50 +0900308 /* In order to evict this inode, we set it dirty */
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900309 mark_inode_dirty(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800310
311 if (IS_DIRSYNC(dir))
312 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900313fail:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900314 trace_f2fs_unlink_exit(inode, err);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900315 return err;
316}
317
Al Viro6e771372015-05-02 13:37:52 -0400318static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700319{
Al Viro6e771372015-05-02 13:37:52 -0400320 const char *link = page_follow_link_light(dentry, cookie);
Al Viro680baac2015-05-02 13:32:22 -0400321 if (!IS_ERR(link) && !*link) {
322 /* this is broken symlink case */
Al Viro5f2c4172015-05-07 11:14:26 -0400323 page_put_link(NULL, *cookie);
Al Viro680baac2015-05-02 13:32:22 -0400324 link = ERR_PTR(-ENOENT);
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700325 }
Al Viro680baac2015-05-02 13:32:22 -0400326 return link;
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -0700327}
328
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900329static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
330 const char *symname)
331{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700332 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900333 struct inode *inode;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700334 size_t len = strlen(symname);
335 size_t p_len;
336 char *p_str;
337 struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
338 struct f2fs_encrypted_symlink_data *sd = NULL;
Gu Zhenge4795562013-09-27 18:08:30 +0800339 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900340
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700341 if (len > dir->i_sb->s_blocksize)
342 return -ENAMETOOLONG;
343
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900344 f2fs_balance_fs(sbi);
345
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900346 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
347 if (IS_ERR(inode))
348 return PTR_ERR(inode);
349
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700350 if (f2fs_encrypted_inode(inode))
351 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
352 else
353 inode->i_op = &f2fs_symlink_inode_operations;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900354 inode->i_mapping->a_ops = &f2fs_dblock_aops;
355
Gu Zhenge4795562013-09-27 18:08:30 +0800356 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900357 err = f2fs_add_link(dentry, inode);
358 if (err)
359 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700360 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900361 alloc_nid_done(sbi, inode->i_ino);
362
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700363 if (f2fs_encrypted_inode(dir)) {
364 struct qstr istr = QSTR_INIT(symname, len);
365
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700366 err = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700367 if (err)
368 goto err_out;
369
370 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
371 if (err)
372 goto err_out;
373
374 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
375 if (err < 0)
376 goto err_out;
377
378 p_len = encrypted_symlink_data_len(disk_link.len) + 1;
379
380 if (p_len > dir->i_sb->s_blocksize) {
381 err = -ENAMETOOLONG;
382 goto err_out;
383 }
384
385 sd = kzalloc(p_len, GFP_NOFS);
386 if (!sd) {
387 err = -ENOMEM;
388 goto err_out;
389 }
390 memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
391 sd->len = cpu_to_le16(disk_link.len);
392 p_str = (char *)sd;
393 } else {
394 p_len = len + 1;
395 p_str = (char *)symname;
396 }
397
398 err = page_symlink(inode, p_str, p_len);
399
400err_out:
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900401 d_instantiate(dentry, inode);
402 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800403
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700404 /*
405 * Let's flush symlink data in order to avoid broken symlink as much as
406 * possible. Nevertheless, fsyncing is the best way, but there is no
407 * way to get a file descriptor in order to flush that.
408 *
409 * Note that, it needs to do dir->fsync to make this recoverable.
410 * If the symlink path is stored into inline_data, there is no
411 * performance regression.
412 */
Chao Yua6be0142015-10-22 18:23:08 +0800413 if (!err) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700414 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
Jaegeuk Kimd0cae972015-04-15 13:37:53 -0700415
Chao Yua6be0142015-10-22 18:23:08 +0800416 if (IS_DIRSYNC(dir))
417 f2fs_sync_fs(sbi->sb, 1);
418 } else {
419 f2fs_unlink(dir, dentry);
420 }
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700421
422 kfree(sd);
423 f2fs_fname_crypto_free_buffer(&disk_link);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900424 return err;
425out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700426 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900427 return err;
428}
429
430static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
431{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700432 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900433 struct inode *inode;
Gu Zhenge4795562013-09-27 18:08:30 +0800434 int err;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900435
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900436 f2fs_balance_fs(sbi);
437
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900438 inode = f2fs_new_inode(dir, S_IFDIR | mode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900439 if (IS_ERR(inode))
Namjae Jeon61412b62012-12-01 10:56:25 +0900440 return PTR_ERR(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900441
442 inode->i_op = &f2fs_dir_inode_operations;
443 inode->i_fop = &f2fs_dir_operations;
444 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Chao Yu560d4672015-02-07 17:36:15 +0800445 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900446
447 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
Gu Zhenge4795562013-09-27 18:08:30 +0800448 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900449 err = f2fs_add_link(dentry, inode);
450 if (err)
451 goto out_fail;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700452 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900453
454 alloc_nid_done(sbi, inode->i_ino);
455
456 d_instantiate(dentry, inode);
457 unlock_new_inode(inode);
458
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800459 if (IS_DIRSYNC(dir))
460 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900461 return 0;
462
463out_fail:
464 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700465 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900466 return err;
467}
468
469static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
470{
David Howells2b0143b2015-03-17 22:25:59 +0000471 struct inode *inode = d_inode(dentry);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900472 if (f2fs_empty_dir(inode))
473 return f2fs_unlink(dir, dentry);
474 return -ENOTEMPTY;
475}
476
477static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
478 umode_t mode, dev_t rdev)
479{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700480 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900481 struct inode *inode;
482 int err = 0;
483
484 if (!new_valid_dev(rdev))
485 return -EINVAL;
486
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900487 f2fs_balance_fs(sbi);
488
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900489 inode = f2fs_new_inode(dir, mode);
490 if (IS_ERR(inode))
491 return PTR_ERR(inode);
492
493 init_special_inode(inode, inode->i_mode, rdev);
494 inode->i_op = &f2fs_special_inode_operations;
495
Gu Zhenge4795562013-09-27 18:08:30 +0800496 f2fs_lock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900497 err = f2fs_add_link(dentry, inode);
498 if (err)
499 goto out;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700500 f2fs_unlock_op(sbi);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900501
502 alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800503
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900504 d_instantiate(dentry, inode);
505 unlock_new_inode(inode);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800506
507 if (IS_DIRSYNC(dir))
508 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900509 return 0;
510out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700511 handle_failed_inode(inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900512 return err;
513}
514
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800515static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
516 umode_t mode, struct inode **whiteout)
517{
518 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
519 struct inode *inode;
520 int err;
521
522 if (!whiteout)
523 f2fs_balance_fs(sbi);
524
525 inode = f2fs_new_inode(dir, mode);
526 if (IS_ERR(inode))
527 return PTR_ERR(inode);
528
529 if (whiteout) {
530 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
531 inode->i_op = &f2fs_special_inode_operations;
532 } else {
533 inode->i_op = &f2fs_file_inode_operations;
534 inode->i_fop = &f2fs_file_operations;
535 inode->i_mapping->a_ops = &f2fs_dblock_aops;
536 }
537
538 f2fs_lock_op(sbi);
539 err = acquire_orphan_inode(sbi);
540 if (err)
541 goto out;
542
543 err = f2fs_do_tmpfile(inode, dir);
544 if (err)
545 goto release_out;
546
547 /*
548 * add this non-linked tmpfile to orphan list, in this way we could
549 * remove all unused data of tmpfile after abnormal power-off.
550 */
551 add_orphan_inode(sbi, inode->i_ino);
552 f2fs_unlock_op(sbi);
553
554 alloc_nid_done(sbi, inode->i_ino);
555
556 if (whiteout) {
557 inode_dec_link_count(inode);
558 *whiteout = inode;
559 } else {
560 d_tmpfile(dentry, inode);
561 }
562 unlock_new_inode(inode);
563 return 0;
564
565release_out:
566 release_orphan_inode(sbi);
567out:
568 handle_failed_inode(inode);
569 return err;
570}
571
572static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
573{
Jaegeuk Kim304eecc2015-05-19 16:11:40 -0700574 if (f2fs_encrypted_inode(dir)) {
575 int err = f2fs_get_encryption_info(dir);
576 if (err)
577 return err;
578 }
579
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800580 return __f2fs_tmpfile(dir, dentry, mode, NULL);
581}
582
583static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
584{
585 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
586}
587
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900588static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800589 struct inode *new_dir, struct dentry *new_dentry,
590 unsigned int flags)
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900591{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700592 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000593 struct inode *old_inode = d_inode(old_dentry);
594 struct inode *new_inode = d_inode(new_dentry);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800595 struct inode *whiteout = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900596 struct page *old_dir_page;
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800597 struct page *old_page, *new_page = NULL;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900598 struct f2fs_dir_entry *old_dir_entry = NULL;
599 struct f2fs_dir_entry *old_entry;
600 struct f2fs_dir_entry *new_entry;
Gu Zhenge4795562013-09-27 18:08:30 +0800601 int err = -ENOENT;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900602
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700603 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
604 !f2fs_is_child_context_consistent_with_parent(new_dir,
605 old_inode)) {
606 err = -EPERM;
607 goto out;
608 }
609
Jaegeuk Kim1efef832012-12-19 16:25:21 +0900610 f2fs_balance_fs(sbi);
611
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900612 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
613 if (!old_entry)
614 goto out;
615
616 if (S_ISDIR(old_inode->i_mode)) {
617 err = -EIO;
618 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
619 if (!old_dir_entry)
620 goto out_old;
621 }
622
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800623 if (flags & RENAME_WHITEOUT) {
624 err = f2fs_create_whiteout(old_dir, &whiteout);
625 if (err)
626 goto out_dir;
627 }
628
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900629 if (new_inode) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900630
631 err = -ENOTEMPTY;
632 if (old_dir_entry && !f2fs_empty_dir(new_inode))
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800633 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900634
635 err = -ENOENT;
636 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
637 &new_page);
638 if (!new_entry)
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800639 goto out_whiteout;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900640
Chao Yu12560102014-06-24 14:16:24 +0800641 f2fs_lock_op(sbi);
642
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900643 err = acquire_orphan_inode(sbi);
644 if (err)
645 goto put_out_dir;
646
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700647 if (update_dent_inode(old_inode, new_inode,
648 &new_dentry->d_name)) {
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900649 release_orphan_inode(sbi);
650 goto put_out_dir;
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900651 }
652
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900653 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
654
655 new_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900656 down_write(&F2FS_I(new_inode)->i_sem);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900657 if (old_dir_entry)
658 drop_nlink(new_inode);
659 drop_nlink(new_inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900660 up_write(&F2FS_I(new_inode)->i_sem);
661
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900662 mark_inode_dirty(new_inode);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900663
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900664 if (!new_inode->i_nlink)
665 add_orphan_inode(sbi, new_inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900666 else
667 release_orphan_inode(sbi);
668
Jaegeuk Kim8cb82682013-08-26 21:25:31 +0900669 update_inode_page(old_inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900670 update_inode_page(new_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900671 } else {
Chao Yu12560102014-06-24 14:16:24 +0800672 f2fs_lock_op(sbi);
673
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900674 err = f2fs_add_link(new_dentry, old_inode);
Chao Yu12560102014-06-24 14:16:24 +0800675 if (err) {
676 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800677 goto out_whiteout;
Chao Yu12560102014-06-24 14:16:24 +0800678 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900679
680 if (old_dir_entry) {
681 inc_nlink(new_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900682 update_inode_page(new_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900683 }
684 }
685
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900686 down_write(&F2FS_I(old_inode)->i_sem);
687 file_lost_pino(old_inode);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700688 if (new_inode && file_enc_name(new_inode))
689 file_set_enc_name(old_inode);
Jaegeuk Kimb2c08292014-06-30 18:09:55 +0900690 up_write(&F2FS_I(old_inode)->i_sem);
691
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900692 old_inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900693 mark_inode_dirty(old_inode);
694
Chao Yudbeacf02014-09-24 18:17:04 +0800695 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900696
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800697 if (whiteout) {
698 whiteout->i_state |= I_LINKABLE;
699 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
700 err = f2fs_add_link(old_dentry, whiteout);
701 if (err)
702 goto put_out_dir;
703 whiteout->i_state &= ~I_LINKABLE;
704 iput(whiteout);
705 }
706
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900707 if (old_dir_entry) {
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800708 if (old_dir != new_dir && !whiteout) {
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900709 f2fs_set_link(old_inode, old_dir_entry,
710 old_dir_page, new_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900711 update_inode_page(old_inode);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900712 } else {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800713 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900714 f2fs_put_page(old_dir_page, 0);
715 }
716 drop_nlink(old_dir);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900717 mark_inode_dirty(old_dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900718 update_inode_page(old_dir);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900719 }
720
Gu Zhenge4795562013-09-27 18:08:30 +0800721 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800722
723 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
724 f2fs_sync_fs(sbi->sb, 1);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900725 return 0;
726
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900727put_out_dir:
Chao Yu12560102014-06-24 14:16:24 +0800728 f2fs_unlock_op(sbi);
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800729 if (new_page) {
730 f2fs_dentry_kunmap(new_dir, new_page);
731 f2fs_put_page(new_page, 0);
732 }
733out_whiteout:
734 if (whiteout)
735 iput(whiteout);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900736out_dir:
737 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800738 f2fs_dentry_kunmap(old_inode, old_dir_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900739 f2fs_put_page(old_dir_page, 0);
740 }
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900741out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800742 f2fs_dentry_kunmap(old_dir, old_page);
Jaegeuk Kim57397d82012-11-02 17:11:10 +0900743 f2fs_put_page(old_page, 0);
744out:
745 return err;
746}
747
Chao Yu32f9bc22014-07-12 19:13:54 +0800748static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
749 struct inode *new_dir, struct dentry *new_dentry)
750{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700751 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000752 struct inode *old_inode = d_inode(old_dentry);
753 struct inode *new_inode = d_inode(new_dentry);
Chao Yu32f9bc22014-07-12 19:13:54 +0800754 struct page *old_dir_page, *new_dir_page;
755 struct page *old_page, *new_page;
756 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
757 struct f2fs_dir_entry *old_entry, *new_entry;
758 int old_nlink = 0, new_nlink = 0;
759 int err = -ENOENT;
760
Chao Yud3baf7c2015-05-25 18:07:02 +0800761 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
762 (old_dir != new_dir) &&
763 (!f2fs_is_child_context_consistent_with_parent(new_dir,
764 old_inode) ||
765 !f2fs_is_child_context_consistent_with_parent(old_dir,
766 new_inode)))
767 return -EPERM;
768
Chao Yu32f9bc22014-07-12 19:13:54 +0800769 f2fs_balance_fs(sbi);
770
771 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
772 if (!old_entry)
773 goto out;
774
775 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
776 if (!new_entry)
777 goto out_old;
778
779 /* prepare for updating ".." directory entry info later */
780 if (old_dir != new_dir) {
781 if (S_ISDIR(old_inode->i_mode)) {
782 err = -EIO;
783 old_dir_entry = f2fs_parent_dir(old_inode,
784 &old_dir_page);
785 if (!old_dir_entry)
786 goto out_new;
787 }
788
789 if (S_ISDIR(new_inode->i_mode)) {
790 err = -EIO;
791 new_dir_entry = f2fs_parent_dir(new_inode,
792 &new_dir_page);
793 if (!new_dir_entry)
794 goto out_old_dir;
795 }
796 }
797
798 /*
799 * If cross rename between file and directory those are not
800 * in the same directory, we will inc nlink of file's parent
801 * later, so we should check upper boundary of its nlink.
802 */
803 if ((!old_dir_entry || !new_dir_entry) &&
804 old_dir_entry != new_dir_entry) {
805 old_nlink = old_dir_entry ? -1 : 1;
806 new_nlink = -old_nlink;
807 err = -EMLINK;
808 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
809 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
810 goto out_new_dir;
811 }
812
813 f2fs_lock_op(sbi);
814
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700815 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800816 if (err)
817 goto out_unlock;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700818 if (file_enc_name(new_inode))
819 file_set_enc_name(old_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800820
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700821 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800822 if (err)
823 goto out_undo;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700824 if (file_enc_name(old_inode))
825 file_set_enc_name(new_inode);
Chao Yu32f9bc22014-07-12 19:13:54 +0800826
827 /* update ".." directory entry info of old dentry */
828 if (old_dir_entry)
829 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
830
831 /* update ".." directory entry info of new dentry */
832 if (new_dir_entry)
833 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
834
835 /* update directory entry info of old dir inode */
836 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
837
838 down_write(&F2FS_I(old_inode)->i_sem);
839 file_lost_pino(old_inode);
840 up_write(&F2FS_I(old_inode)->i_sem);
841
842 update_inode_page(old_inode);
843
844 old_dir->i_ctime = CURRENT_TIME;
845 if (old_nlink) {
846 down_write(&F2FS_I(old_dir)->i_sem);
847 if (old_nlink < 0)
848 drop_nlink(old_dir);
849 else
850 inc_nlink(old_dir);
851 up_write(&F2FS_I(old_dir)->i_sem);
852 }
853 mark_inode_dirty(old_dir);
854 update_inode_page(old_dir);
855
856 /* update directory entry info of new dir inode */
857 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
858
859 down_write(&F2FS_I(new_inode)->i_sem);
860 file_lost_pino(new_inode);
861 up_write(&F2FS_I(new_inode)->i_sem);
862
863 update_inode_page(new_inode);
864
865 new_dir->i_ctime = CURRENT_TIME;
866 if (new_nlink) {
867 down_write(&F2FS_I(new_dir)->i_sem);
868 if (new_nlink < 0)
869 drop_nlink(new_dir);
870 else
871 inc_nlink(new_dir);
872 up_write(&F2FS_I(new_dir)->i_sem);
873 }
874 mark_inode_dirty(new_dir);
875 update_inode_page(new_dir);
876
877 f2fs_unlock_op(sbi);
Jaegeuk Kimb7e1d802014-11-09 22:15:31 -0800878
879 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
880 f2fs_sync_fs(sbi->sb, 1);
Chao Yu32f9bc22014-07-12 19:13:54 +0800881 return 0;
882out_undo:
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700883 /*
884 * Still we may fail to recover name info of f2fs_inode here
885 * Drop it, once its name is set as encrypted
886 */
887 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
Chao Yu32f9bc22014-07-12 19:13:54 +0800888out_unlock:
889 f2fs_unlock_op(sbi);
890out_new_dir:
891 if (new_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800892 f2fs_dentry_kunmap(new_inode, new_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800893 f2fs_put_page(new_dir_page, 0);
894 }
895out_old_dir:
896 if (old_dir_entry) {
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800897 f2fs_dentry_kunmap(old_inode, old_dir_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800898 f2fs_put_page(old_dir_page, 0);
899 }
900out_new:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800901 f2fs_dentry_kunmap(new_dir, new_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800902 f2fs_put_page(new_page, 0);
903out_old:
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -0800904 f2fs_dentry_kunmap(old_dir, old_page);
Chao Yu32f9bc22014-07-12 19:13:54 +0800905 f2fs_put_page(old_page, 0);
906out:
907 return err;
908}
909
910static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
911 struct inode *new_dir, struct dentry *new_dentry,
912 unsigned int flags)
913{
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800914 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Chao Yu32f9bc22014-07-12 19:13:54 +0800915 return -EINVAL;
916
917 if (flags & RENAME_EXCHANGE) {
918 return f2fs_cross_rename(old_dir, old_dentry,
919 new_dir, new_dentry);
920 }
921 /*
922 * VFS has already handled the new dentry existence case,
923 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
924 */
Chao Yu7e01e7ad2015-05-19 17:37:26 +0800925 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Chao Yu32f9bc22014-07-12 19:13:54 +0800926}
927
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700928#ifdef CONFIG_F2FS_FS_ENCRYPTION
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700929static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
Chao Yu50732df2014-06-19 16:23:19 +0800930{
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700931 struct page *cpage = NULL;
932 char *caddr, *paddr = NULL;
933 struct f2fs_str cstr;
934 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
935 struct inode *inode = d_inode(dentry);
936 struct f2fs_encrypted_symlink_data *sd;
937 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
938 u32 max_size = inode->i_sb->s_blocksize;
939 int res;
Chao Yu50732df2014-06-19 16:23:19 +0800940
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700941 res = f2fs_get_encryption_info(inode);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700942 if (res)
943 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800944
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700945 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
946 if (IS_ERR(cpage))
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700947 return ERR_CAST(cpage);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700948 caddr = kmap(cpage);
949 caddr[size] = 0;
Chao Yu50732df2014-06-19 16:23:19 +0800950
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700951 /* Symlink is encrypted */
952 sd = (struct f2fs_encrypted_symlink_data *)caddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700953 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700954 cstr.name = kmalloc(cstr.len, GFP_NOFS);
955 if (!cstr.name) {
956 res = -ENOMEM;
957 goto errout;
958 }
959 memcpy(cstr.name, sd->encrypted_path, cstr.len);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700960
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700961 /* this is broken symlink case */
962 if (cstr.name[0] == 0 && cstr.len == 0) {
963 res = -ENOENT;
964 goto errout;
965 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700966
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700967 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
968 max_size) {
969 /* Symlink data on the disk is corrupted */
970 res = -EIO;
971 goto errout;
972 }
973 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
974 if (res)
975 goto errout;
Chao Yu50732df2014-06-19 16:23:19 +0800976
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700977 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
978 if (res < 0)
979 goto errout;
Chao Yu3c0d84d2015-02-16 16:20:27 +0800980
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700981 kfree(cstr.name);
982
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700983 paddr = pstr.name;
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700984
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700985 /* Null-terminate the name */
986 paddr[res] = '\0';
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700987
988 kunmap(cpage);
989 page_cache_release(cpage);
Linus Torvaldscfcc0ad2015-06-24 20:38:29 -0700990 return *cookie = paddr;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700991errout:
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700992 kfree(cstr.name);
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700993 f2fs_fname_crypto_free_buffer(&pstr);
994 kunmap(cpage);
995 page_cache_release(cpage);
996 return ERR_PTR(res);
Chao Yu50732df2014-06-19 16:23:19 +0800997}
998
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700999const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1000 .readlink = generic_readlink,
1001 .follow_link = f2fs_encrypted_follow_link,
1002 .put_link = kfree_put_link,
1003 .getattr = f2fs_getattr,
1004 .setattr = f2fs_setattr,
1005 .setxattr = generic_setxattr,
1006 .getxattr = generic_getxattr,
1007 .listxattr = f2fs_listxattr,
1008 .removexattr = generic_removexattr,
1009};
1010#endif
1011
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001012const struct inode_operations f2fs_dir_inode_operations = {
1013 .create = f2fs_create,
1014 .lookup = f2fs_lookup,
1015 .link = f2fs_link,
1016 .unlink = f2fs_unlink,
1017 .symlink = f2fs_symlink,
1018 .mkdir = f2fs_mkdir,
1019 .rmdir = f2fs_rmdir,
1020 .mknod = f2fs_mknod,
Chao Yu32f9bc22014-07-12 19:13:54 +08001021 .rename2 = f2fs_rename2,
Chao Yu50732df2014-06-19 16:23:19 +08001022 .tmpfile = f2fs_tmpfile,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001023 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001024 .setattr = f2fs_setattr,
1025 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001026 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001027#ifdef CONFIG_F2FS_FS_XATTR
1028 .setxattr = generic_setxattr,
1029 .getxattr = generic_getxattr,
1030 .listxattr = f2fs_listxattr,
1031 .removexattr = generic_removexattr,
1032#endif
1033};
1034
1035const struct inode_operations f2fs_symlink_inode_operations = {
1036 .readlink = generic_readlink,
Jaegeuk Kimfeb7cbb2015-04-15 13:49:55 -07001037 .follow_link = f2fs_follow_link,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001038 .put_link = page_put_link,
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001039 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001040 .setattr = f2fs_setattr,
1041#ifdef CONFIG_F2FS_FS_XATTR
1042 .setxattr = generic_setxattr,
1043 .getxattr = generic_getxattr,
1044 .listxattr = f2fs_listxattr,
1045 .removexattr = generic_removexattr,
1046#endif
1047};
1048
1049const struct inode_operations f2fs_special_inode_operations = {
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +09001050 .getattr = f2fs_getattr,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001051 .setattr = f2fs_setattr,
1052 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001053 .set_acl = f2fs_set_acl,
Jaegeuk Kim57397d82012-11-02 17:11:10 +09001054#ifdef CONFIG_F2FS_FS_XATTR
1055 .setxattr = generic_setxattr,
1056 .getxattr = generic_getxattr,
1057 .listxattr = f2fs_listxattr,
1058 .removexattr = generic_removexattr,
1059#endif
1060};