namei.c 30.9 KB
Newer Older
C
Chao Yu 已提交
1
// SPDX-License-Identifier: GPL-2.0
J
Jaegeuk Kim 已提交
2
/*
3 4 5 6 7 8 9 10 11 12
 * fs/f2fs/namei.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 */
#include <linux/fs.h>
#include <linux/f2fs_fs.h>
#include <linux/pagemap.h>
#include <linux/sched.h>
#include <linux/ctype.h>
13
#include <linux/random.h>
C
Chao Yu 已提交
14
#include <linux/dcache.h>
15
#include <linux/namei.h>
C
Chao Yu 已提交
16
#include <linux/quotaops.h>
17 18

#include "f2fs.h"
19
#include "node.h"
D
Daniel Rosenberg 已提交
20
#include "segment.h"
21 22
#include "xattr.h"
#include "acl.h"
23
#include <trace/events/f2fs.h>
24 25 26

static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
{
27
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
28 29 30
	nid_t ino;
	struct inode *inode;
	bool nid_free = false;
C
Chao Yu 已提交
31
	int xattr_size = 0;
32
	int err;
33

34
	inode = new_inode(dir->i_sb);
35 36 37
	if (!inode)
		return ERR_PTR(-ENOMEM);

38
	f2fs_lock_op(sbi);
C
Chao Yu 已提交
39
	if (!f2fs_alloc_nid(sbi, &ino)) {
40
		f2fs_unlock_op(sbi);
41 42 43
		err = -ENOSPC;
		goto fail;
	}
44
	f2fs_unlock_op(sbi);
45

C
Chao Yu 已提交
46 47
	nid_free = true;

48
	inode_init_owner(inode, dir, mode);
49 50 51

	inode->i_ino = ino;
	inode->i_blocks = 0;
52
	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
53
	F2FS_I(inode)->i_crtime = inode->i_mtime;
54
	inode->i_generation = prandom_u32();
55

56 57 58
	if (S_ISDIR(inode->i_mode))
		F2FS_I(inode)->i_current_depth = 1;

59 60 61
	err = insert_inode_locked(inode);
	if (err) {
		err = -EINVAL;
62
		goto fail;
63
	}
C
Chao Yu 已提交
64

65
	if (f2fs_sb_has_project_quota(sbi) &&
66
		(F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
C
Chao Yu 已提交
67 68 69 70 71
		F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
	else
		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
							F2FS_DEF_PROJID);

C
Chao Yu 已提交
72 73 74 75
	err = dquot_initialize(inode);
	if (err)
		goto fail_drop;

76 77
	set_inode_flag(inode, FI_NEW_INODE);

78
	/* If the directory encrypted, then we should encrypt the inode. */
79
	if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
80
				f2fs_may_encrypt(inode))
81 82
		f2fs_set_encrypted_inode(inode);

83
	if (f2fs_sb_has_extra_attr(sbi)) {
84 85 86 87
		set_inode_flag(inode, FI_EXTRA_ATTR);
		F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
	}

88 89
	if (test_opt(sbi, INLINE_XATTR))
		set_inode_flag(inode, FI_INLINE_XATTR);
C
Chao Yu 已提交
90

91
	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
92
		set_inode_flag(inode, FI_INLINE_DATA);
93
	if (f2fs_may_inline_dentry(inode))
94
		set_inode_flag(inode, FI_INLINE_DENTRY);
C
Chao Yu 已提交
95

96
	if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
C
Chao Yu 已提交
97 98
		f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
		if (f2fs_has_inline_xattr(inode))
99
			xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
C
Chao Yu 已提交
100 101 102 103 104 105 106
		/* Otherwise, will be 0 */
	} else if (f2fs_has_inline_xattr(inode) ||
				f2fs_has_inline_dentry(inode)) {
		xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
	}
	F2FS_I(inode)->i_inline_xattr_size = xattr_size;

J
Jaegeuk Kim 已提交
107 108
	f2fs_init_extent_tree(inode, NULL);

C
Chao Yu 已提交
109
	stat_inc_inline_xattr(inode);
110 111 112
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

C
Chao Yu 已提交
113 114 115
	F2FS_I(inode)->i_flags =
		f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);

C
Chao Yu 已提交
116
	if (S_ISDIR(inode->i_mode))
117
		F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
C
Chao Yu 已提交
118

119
	if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
C
Chao Yu 已提交
120 121
		set_inode_flag(inode, FI_PROJ_INHERIT);

C
Chao Yu 已提交
122 123 124 125 126 127 128
	if (f2fs_sb_has_compression(sbi)) {
		/* Inherit the compression flag in directory */
		if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
					f2fs_may_compress(inode))
			set_compress_context(inode);
	}

C
Chao Yu 已提交
129 130
	f2fs_set_inode_flags(inode);

131
	trace_f2fs_new_inode(inode, 0);
132 133 134
	return inode;

fail:
135
	trace_f2fs_new_inode(inode, err);
136
	make_bad_inode(inode);
137
	if (nid_free)
138
		set_inode_flag(inode, FI_FREE_NID);
139
	iput(inode);
140
	return ERR_PTR(err);
C
Chao Yu 已提交
141 142 143 144 145 146 147 148 149 150
fail_drop:
	trace_f2fs_new_inode(inode, err);
	dquot_drop(inode);
	inode->i_flags |= S_NOQUOTA;
	if (nid_free)
		set_inode_flag(inode, FI_FREE_NID);
	clear_nlink(inode);
	unlock_new_inode(inode);
	iput(inode);
	return ERR_PTR(err);
151 152
}

153
static inline int is_extension_exist(const unsigned char *s, const char *sub)
154
{
155 156
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
157
	int i;
158

C
Chao Yu 已提交
159 160 161
	if (sublen == 1 && *sub == '*')
		return 1;

C
Chao Yu 已提交
162 163
	/*
	 * filename format of multimedia file should be defined as:
164
	 * "filename + '.' + extension + (optional: '.' + temp extension)".
C
Chao Yu 已提交
165 166 167 168
	 */
	if (slen < sublen + 2)
		return 0;

169 170 171 172 173 174
	for (i = 1; i < slen - sublen; i++) {
		if (s[i] != '.')
			continue;
		if (!strncasecmp(s + i + 1, sub, sublen))
			return 1;
	}
175

176
	return 0;
177 178
}

J
Jaegeuk Kim 已提交
179
/*
C
Chao Yu 已提交
180
 * Set file's temperature for hot/cold data separation
181
 */
C
Chao Yu 已提交
182
static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
183 184
		const unsigned char *name)
{
185
	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
C
Chao Yu 已提交
186
	int i, cold_count, hot_count;
187 188 189

	down_read(&sbi->sb_lock);

C
Chao Yu 已提交
190 191
	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
	hot_count = sbi->raw_super->hot_ext_count;
192

C
Chao Yu 已提交
193
	for (i = 0; i < cold_count + hot_count; i++) {
194 195
		if (is_extension_exist(name, extlist[i]))
			break;
196
	}
197 198

	up_read(&sbi->sb_lock);
199 200 201 202 203 204 205 206

	if (i == cold_count + hot_count)
		return;

	if (i < cold_count)
		file_set_cold(inode);
	else
		file_set_hot(inode);
207 208
}

C
Chao Yu 已提交
209
int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
C
Chao Yu 已提交
210
							bool hot, bool set)
211 212
{
	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
C
Chao Yu 已提交
213 214 215 216
	int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
	int hot_count = sbi->raw_super->hot_ext_count;
	int total_count = cold_count + hot_count;
	int start, count;
217 218
	int i;

C
Chao Yu 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	if (set) {
		if (total_count == F2FS_MAX_EXTENSION)
			return -EINVAL;
	} else {
		if (!hot && !cold_count)
			return -EINVAL;
		if (hot && !hot_count)
			return -EINVAL;
	}

	if (hot) {
		start = cold_count;
		count = total_count;
	} else {
		start = 0;
		count = cold_count;
	}

	for (i = start; i < count; i++) {
238 239 240 241 242 243 244
		if (strcmp(name, extlist[i]))
			continue;

		if (set)
			return -EINVAL;

		memcpy(extlist[i], extlist[i + 1],
C
Chao Yu 已提交
245 246 247 248 249 250 251
				F2FS_EXTENSION_LEN * (total_count - i - 1));
		memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
		if (hot)
			sbi->raw_super->hot_ext_count = hot_count - 1;
		else
			sbi->raw_super->extension_count =
						cpu_to_le32(cold_count - 1);
252 253 254 255 256 257
		return 0;
	}

	if (!set)
		return -EINVAL;

C
Chao Yu 已提交
258
	if (hot) {
259
		memcpy(extlist[count], name, strlen(name));
C
Chao Yu 已提交
260 261 262 263 264 265 266
		sbi->raw_super->hot_ext_count = hot_count + 1;
	} else {
		char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];

		memcpy(buf, &extlist[cold_count],
				F2FS_EXTENSION_LEN * hot_count);
		memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
267
		memcpy(extlist[cold_count], name, strlen(name));
C
Chao Yu 已提交
268 269 270 271
		memcpy(&extlist[cold_count + 1], buf,
				F2FS_EXTENSION_LEN * hot_count);
		sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
	}
272
	return 0;
273 274
}

C
Chao Yu 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
						const unsigned char *name)
{
	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
	unsigned char (*ext)[F2FS_EXTENSION_LEN];
	unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
	int i, cold_count, hot_count;

	if (!f2fs_sb_has_compression(sbi) ||
			is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
			F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
			!f2fs_may_compress(inode))
		return;

	down_read(&sbi->sb_lock);

	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
	hot_count = sbi->raw_super->hot_ext_count;

	for (i = cold_count; i < cold_count + hot_count; i++) {
		if (is_extension_exist(name, extlist[i])) {
			up_read(&sbi->sb_lock);
			return;
		}
	}

	up_read(&sbi->sb_lock);

	ext = F2FS_OPTION(sbi).extensions;

	for (i = 0; i < ext_cnt; i++) {
		if (!is_extension_exist(name, ext[i]))
			continue;

		set_compress_context(inode);
		return;
	}
}

314 315 316
static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
317
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
318 319
	struct inode *inode;
	nid_t ino = 0;
320
	int err;
321

322 323
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
324 325
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
326

C
Chao Yu 已提交
327 328 329 330
	err = dquot_initialize(dir);
	if (err)
		return err;

331 332 333 334 335
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
C
Chao Yu 已提交
336
		set_file_temperature(sbi, inode, dentry->d_name.name);
337

C
Chao Yu 已提交
338 339
	set_compress_inode(sbi, inode, dentry->d_name.name);

340 341 342 343 344
	inode->i_op = &f2fs_file_inode_operations;
	inode->i_fop = &f2fs_file_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
	ino = inode->i_ino;

345
	f2fs_lock_op(sbi);
346 347 348
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
349
	f2fs_unlock_op(sbi);
350

C
Chao Yu 已提交
351
	f2fs_alloc_nid_done(sbi, ino);
352

353
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
354 355 356

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
357 358

	f2fs_balance_fs(sbi, true);
359 360
	return 0;
out:
C
Chao Yu 已提交
361
	f2fs_handle_failed_inode(inode);
362 363 364 365 366 367
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
368
	struct inode *inode = d_inode(old_dentry);
369
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
370
	int err;
371

372 373
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
374 375
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
376

377 378 379
	err = fscrypt_prepare_link(old_dentry, dir, dentry);
	if (err)
		return err;
380

C
Chao Yu 已提交
381 382 383 384 385
	if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
			(!projid_eq(F2FS_I(dir)->i_projid,
			F2FS_I(old_dentry->d_inode)->i_projid)))
		return -EXDEV;

C
Chao Yu 已提交
386 387 388 389
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
390
	f2fs_balance_fs(sbi, true);
391

392
	inode->i_ctime = current_time(inode);
J
Jaegeuk Kim 已提交
393
	ihold(inode);
394

395
	set_inode_flag(inode, FI_INC_LINK);
396
	f2fs_lock_op(sbi);
397 398 399
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
400
	f2fs_unlock_op(sbi);
401 402

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
403 404 405

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
406 407
	return 0;
out:
408
	clear_inode_flag(inode, FI_INC_LINK);
409
	iput(inode);
410
	f2fs_unlock_op(sbi);
411 412 413 414 415 416
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
417 418 419 420 421
	struct page *page;
	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
	if (!ino) {
		if (IS_ERR(page))
			return ERR_CAST(page);
422
		return ERR_PTR(-ENOENT);
423
	}
424
	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
425 426
}

427 428 429 430 431 432 433 434 435
static int __recover_dot_dentries(struct inode *dir, nid_t pino)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
	struct qstr dot = QSTR_INIT(".", 1);
	struct qstr dotdot = QSTR_INIT("..", 2);
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = 0;

436
	if (f2fs_readonly(sbi->sb)) {
437 438
		f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
			  dir->i_ino, pino);
439 440 441
		return 0;
	}

442 443 444 445
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
446
	f2fs_balance_fs(sbi, true);
447

448 449 450 451 452
	f2fs_lock_op(sbi);

	de = f2fs_find_entry(dir, &dot, &page);
	if (de) {
		f2fs_put_page(page, 0);
453 454 455
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto out;
456
	} else {
C
Chao Yu 已提交
457
		err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
458 459 460 461 462
		if (err)
			goto out;
	}

	de = f2fs_find_entry(dir, &dotdot, &page);
463
	if (de)
464
		f2fs_put_page(page, 0);
465
	else if (IS_ERR(page))
466
		err = PTR_ERR(page);
467
	else
C
Chao Yu 已提交
468
		err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
469
out:
470
	if (!err)
471
		clear_inode_flag(dir, FI_INLINE_DOTS);
472 473 474 475 476

	f2fs_unlock_op(sbi);
	return err;
}

477 478 479 480 481 482
static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
		unsigned int flags)
{
	struct inode *inode = NULL;
	struct f2fs_dir_entry *de;
	struct page *page;
C
Chao Yu 已提交
483 484
	struct dentry *new;
	nid_t ino = -1;
485
	int err = 0;
486
	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
487
	struct fscrypt_name fname;
488

C
Chao Yu 已提交
489 490 491 492 493 494
	trace_f2fs_lookup_start(dir, dentry, flags);

	if (dentry->d_name.len > F2FS_NAME_LEN) {
		err = -ENAMETOOLONG;
		goto out;
	}
495

496 497 498 499 500 501 502 503
	err = fscrypt_prepare_lookup(dir, dentry, &fname);
	if (err == -ENOENT)
		goto out_splice;
	if (err)
		goto out;
	de = __f2fs_find_entry(dir, &fname, &page);
	fscrypt_free_filename(&fname);

J
Jaegeuk Kim 已提交
504
	if (!de) {
C
Chao Yu 已提交
505 506 507 508 509
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto out;
		}
		goto out_splice;
J
Jaegeuk Kim 已提交
510
	}
511

J
Jaegeuk Kim 已提交
512 513
	ino = le32_to_cpu(de->ino);
	f2fs_put_page(page, 0);
514

J
Jaegeuk Kim 已提交
515
	inode = f2fs_iget(dir->i_sb, ino);
C
Chao Yu 已提交
516 517 518 519
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out;
	}
520

521 522 523
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
C
Chao Yu 已提交
524
			goto out_iput;
525 526
	}

527
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
528
		err = __recover_dot_dentries(inode, dir->i_ino);
529
		if (err)
C
Chao Yu 已提交
530
			goto out_iput;
531
	}
532
	if (IS_ENCRYPTED(dir) &&
D
Dan Carpenter 已提交
533 534
	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
	    !fscrypt_has_permitted_context(dir, inode)) {
535 536
		f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
			  dir->i_ino, inode->i_ino);
537
		err = -EPERM;
C
Chao Yu 已提交
538
		goto out_iput;
539
	}
C
Chao Yu 已提交
540
out_splice:
541 542 543 544 545 546 547 548 549 550 551
#ifdef CONFIG_UNICODE
	if (!inode && IS_CASEFOLDED(dir)) {
		/* Eventually we want to call d_add_ci(dentry, NULL)
		 * for negative dentries in the encoding case as
		 * well.  For now, prevent the negative dentry
		 * from being cached.
		 */
		trace_f2fs_lookup_end(dir, dentry, ino, err);
		return NULL;
	}
#endif
C
Chao Yu 已提交
552
	new = d_splice_alias(inode, dentry);
553
	err = PTR_ERR_OR_ZERO(new);
C
Chao Yu 已提交
554 555 556
	trace_f2fs_lookup_end(dir, dentry, ino, err);
	return new;
out_iput:
557
	iput(inode);
C
Chao Yu 已提交
558 559
out:
	trace_f2fs_lookup_end(dir, dentry, ino, err);
560
	return ERR_PTR(err);
561 562 563 564
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
565
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
566
	struct inode *inode = d_inode(dentry);
567 568 569 570
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

571
	trace_f2fs_unlink_enter(dir, dentry);
572

573 574 575
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

C
Chao Yu 已提交
576
	err = dquot_initialize(dir);
J
Jaegeuk Kim 已提交
577 578 579
	if (err)
		return err;
	err = dquot_initialize(inode);
C
Chao Yu 已提交
580 581 582
	if (err)
		return err;

583
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
584 585 586
	if (!de) {
		if (IS_ERR(page))
			err = PTR_ERR(page);
587
		goto fail;
588
	}
589

J
Jaegeuk Kim 已提交
590
	f2fs_balance_fs(sbi, true);
591

592
	f2fs_lock_op(sbi);
C
Chao Yu 已提交
593
	err = f2fs_acquire_orphan_inode(sbi);
594
	if (err) {
595
		f2fs_unlock_op(sbi);
596 597 598
		f2fs_put_page(page, 0);
		goto fail;
	}
599
	f2fs_delete_entry(de, page, dir, inode);
600 601 602 603 604 605 606 607 608 609
#ifdef CONFIG_UNICODE
	/* VFS negative dentries are incompatible with Encoding and
	 * Case-insensitiveness. Eventually we'll want avoid
	 * invalidating the dentries here, alongside with returning the
	 * negative dentries at f2fs_lookup(), when it is  better
	 * supported by the VFS for the CI case.
	 */
	if (IS_CASEFOLDED(dir))
		d_invalidate(dentry);
#endif
610
	f2fs_unlock_op(sbi);
611

J
Jaegeuk Kim 已提交
612 613
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
614
fail:
615
	trace_f2fs_unlink_exit(inode, err);
616 617 618
	return err;
}

619
static const char *f2fs_get_link(struct dentry *dentry,
620 621
				 struct inode *inode,
				 struct delayed_call *done)
622
{
623
	const char *link = page_get_link(dentry, inode, done);
624 625
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
626 627
		do_delayed_call(done);
		clear_delayed_call(done);
628
		link = ERR_PTR(-ENOENT);
629
	}
630
	return link;
631 632
}

633 634 635
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
636
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
637
	struct inode *inode;
638
	size_t len = strlen(symname);
639
	struct fscrypt_str disk_link;
640
	int err;
641

642 643
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
644 645
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
646

647 648 649 650
	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
				      &disk_link);
	if (err)
		return err;
651

C
Chao Yu 已提交
652 653 654 655
	err = dquot_initialize(dir);
	if (err)
		return err;

656 657 658 659
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

660
	if (IS_ENCRYPTED(inode))
661 662 663
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
664
	inode_nohighmem(inode);
665 666
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

667
	f2fs_lock_op(sbi);
668 669
	err = f2fs_add_link(dentry, inode);
	if (err)
C
Chao Yu 已提交
670
		goto out_f2fs_handle_failed_inode;
671
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
672
	f2fs_alloc_nid_done(sbi, inode->i_ino);
673

674 675 676
	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
	if (err)
		goto err_out;
677

678
	err = page_symlink(inode, disk_link.name, disk_link.len);
679 680

err_out:
681
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
682

683 684 685 686 687 688 689 690 691
	/*
	 * Let's flush symlink data in order to avoid broken symlink as much as
	 * possible. Nevertheless, fsyncing is the best way, but there is no
	 * way to get a file descriptor in order to flush that.
	 *
	 * Note that, it needs to do dir->fsync to make this recoverable.
	 * If the symlink path is stored into inline_data, there is no
	 * performance regression.
	 */
C
Chao Yu 已提交
692
	if (!err) {
693 694
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
695

C
Chao Yu 已提交
696 697 698 699 700
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
701

702
	f2fs_balance_fs(sbi, true);
703 704
	goto out_free_encrypted_link;

C
Chao Yu 已提交
705 706
out_f2fs_handle_failed_inode:
	f2fs_handle_failed_inode(inode);
707 708
out_free_encrypted_link:
	if (disk_link.name != (unsigned char *)symname)
709
		kvfree(disk_link.name);
710 711 712 713 714
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
715
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
716
	struct inode *inode;
717
	int err;
718

719 720 721
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

C
Chao Yu 已提交
722 723 724 725
	err = dquot_initialize(dir);
	if (err)
		return err;

726 727
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
728
		return PTR_ERR(inode);
729 730 731 732

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
733
	inode_nohighmem(inode);
734

735
	set_inode_flag(inode, FI_INC_LINK);
736
	f2fs_lock_op(sbi);
737 738 739
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
740
	f2fs_unlock_op(sbi);
741

C
Chao Yu 已提交
742
	f2fs_alloc_nid_done(sbi, inode->i_ino);
743

744
	d_instantiate_new(dentry, inode);
745

J
Jaegeuk Kim 已提交
746 747
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
748 749

	f2fs_balance_fs(sbi, true);
750 751 752
	return 0;

out_fail:
753
	clear_inode_flag(inode, FI_INC_LINK);
C
Chao Yu 已提交
754
	f2fs_handle_failed_inode(inode);
755 756 757 758 759
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
760
	struct inode *inode = d_inode(dentry);
761 762 763 764 765 766 767 768
	if (f2fs_empty_dir(inode))
		return f2fs_unlink(dir, dentry);
	return -ENOTEMPTY;
}

static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
				umode_t mode, dev_t rdev)
{
769
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
770 771 772
	struct inode *inode;
	int err = 0;

773 774
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
775 776
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
777

C
Chao Yu 已提交
778 779 780 781
	err = dquot_initialize(dir);
	if (err)
		return err;

782 783 784 785 786 787 788
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	init_special_inode(inode, inode->i_mode, rdev);
	inode->i_op = &f2fs_special_inode_operations;

789
	f2fs_lock_op(sbi);
790 791 792
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
793
	f2fs_unlock_op(sbi);
794

C
Chao Yu 已提交
795
	f2fs_alloc_nid_done(sbi, inode->i_ino);
J
Jaegeuk Kim 已提交
796

797
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
798 799 800

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
801 802

	f2fs_balance_fs(sbi, true);
803 804
	return 0;
out:
C
Chao Yu 已提交
805
	f2fs_handle_failed_inode(inode);
806 807 808
	return err;
}

C
Chao Yu 已提交
809 810 811 812 813 814 815
static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
					umode_t mode, struct inode **whiteout)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
	struct inode *inode;
	int err;

C
Chao Yu 已提交
816 817 818 819
	err = dquot_initialize(dir);
	if (err)
		return err;

C
Chao Yu 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (whiteout) {
		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
		inode->i_op = &f2fs_special_inode_operations;
	} else {
		inode->i_op = &f2fs_file_inode_operations;
		inode->i_fop = &f2fs_file_operations;
		inode->i_mapping->a_ops = &f2fs_dblock_aops;
	}

	f2fs_lock_op(sbi);
C
Chao Yu 已提交
834
	err = f2fs_acquire_orphan_inode(sbi);
C
Chao Yu 已提交
835 836 837 838 839 840 841 842 843 844 845
	if (err)
		goto out;

	err = f2fs_do_tmpfile(inode, dir);
	if (err)
		goto release_out;

	/*
	 * add this non-linked tmpfile to orphan list, in this way we could
	 * remove all unused data of tmpfile after abnormal power-off.
	 */
C
Chao Yu 已提交
846 847
	f2fs_add_orphan_inode(inode);
	f2fs_alloc_nid_done(sbi, inode->i_ino);
C
Chao Yu 已提交
848 849

	if (whiteout) {
850
		f2fs_i_links_write(inode, false);
851
		inode->i_state |= I_LINKABLE;
C
Chao Yu 已提交
852 853 854 855
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
856 857
	/* link_count was changed by d_tmpfile as well. */
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
858
	unlock_new_inode(inode);
859 860

	f2fs_balance_fs(sbi, true);
C
Chao Yu 已提交
861 862 863
	return 0;

release_out:
C
Chao Yu 已提交
864
	f2fs_release_orphan_inode(sbi);
C
Chao Yu 已提交
865
out:
C
Chao Yu 已提交
866
	f2fs_handle_failed_inode(inode);
C
Chao Yu 已提交
867 868 869 870 871
	return err;
}

static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
872 873 874
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

	if (unlikely(f2fs_cp_error(sbi)))
875
		return -EIO;
876 877
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
878

879
	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
880
		int err = fscrypt_get_encryption_info(dir);
881 882 883 884
		if (err)
			return err;
	}

C
Chao Yu 已提交
885 886 887 888 889
	return __f2fs_tmpfile(dir, dentry, mode, NULL);
}

static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
{
890 891 892
	if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
		return -EIO;

C
Chao Yu 已提交
893 894 895
	return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
}

896
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
897 898
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
899
{
900
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
901 902
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
903
	struct inode *whiteout = NULL;
904
	struct page *old_dir_page = NULL;
C
Chao Yu 已提交
905
	struct page *old_page, *new_page = NULL;
906 907 908
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
909
	int err;
910

911 912
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
913 914
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
915

C
Chao Yu 已提交
916 917 918 919 920
	if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
			(!projid_eq(F2FS_I(new_dir)->i_projid,
			F2FS_I(old_dentry->d_inode)->i_projid)))
		return -EXDEV;

921 922 923 924 925 926 927 928 929 930 931 932 933 934
	/*
	 * If new_inode is null, the below renaming flow will
	 * add a link in old_dir which can conver inline_dir.
	 * After then, if we failed to get the entry due to other
	 * reasons like ENOMEM, we had to remove the new entry.
	 * Instead of adding such the error handling routine, let's
	 * simply convert first here.
	 */
	if (old_dir == new_dir && !new_inode) {
		err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
		if (err)
			return err;
	}

935 936 937 938 939 940
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			return err;
	}

C
Chao Yu 已提交
941 942 943 944 945 946 947 948
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

	err = dquot_initialize(new_dir);
	if (err)
		goto out;

J
Jaegeuk Kim 已提交
949 950 951 952 953 954
	if (new_inode) {
		err = dquot_initialize(new_inode);
		if (err)
			goto out;
	}

955
	err = -ENOENT;
956
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
957 958 959
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
960
		goto out;
961
	}
962 963 964

	if (S_ISDIR(old_inode->i_mode)) {
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
965
		if (!old_dir_entry) {
966 967
			if (IS_ERR(old_dir_page))
				err = PTR_ERR(old_dir_page);
968
			goto out_old;
969
		}
970 971 972 973 974 975
	}

	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
976
			goto out_dir;
977 978 979 980

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
981 982 983
		if (!new_entry) {
			if (IS_ERR(new_page))
				err = PTR_ERR(new_page);
984
			goto out_dir;
985
		}
986

J
Jaegeuk Kim 已提交
987
		f2fs_balance_fs(sbi, true);
988

989 990
		f2fs_lock_op(sbi);

C
Chao Yu 已提交
991
		err = f2fs_acquire_orphan_inode(sbi);
J
Jaegeuk Kim 已提交
992 993 994
		if (err)
			goto put_out_dir;

995
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
996
		new_page = NULL;
997

998
		new_inode->i_ctime = current_time(new_inode);
999
		down_write(&F2FS_I(new_inode)->i_sem);
1000
		if (old_dir_entry)
1001 1002
			f2fs_i_links_write(new_inode, false);
		f2fs_i_links_write(new_inode, false);
1003 1004
		up_write(&F2FS_I(new_inode)->i_sem);

1005
		if (!new_inode->i_nlink)
C
Chao Yu 已提交
1006
			f2fs_add_orphan_inode(new_inode);
J
Jaegeuk Kim 已提交
1007
		else
C
Chao Yu 已提交
1008
			f2fs_release_orphan_inode(sbi);
1009
	} else {
J
Jaegeuk Kim 已提交
1010
		f2fs_balance_fs(sbi, true);
1011

1012 1013
		f2fs_lock_op(sbi);

1014
		err = f2fs_add_link(new_dentry, old_inode);
1015 1016
		if (err) {
			f2fs_unlock_op(sbi);
1017
			goto out_dir;
1018
		}
1019

1020
		if (old_dir_entry)
1021
			f2fs_i_links_write(new_dir, true);
1022 1023
	}

1024
	down_write(&F2FS_I(old_inode)->i_sem);
1025 1026 1027
	if (!old_dir_entry || whiteout)
		file_lost_pino(old_inode);
	else
1028 1029
		/* adjust dir's i_pino to pass fsck check */
		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1030 1031
	up_write(&F2FS_I(old_inode)->i_sem);

1032
	old_inode->i_ctime = current_time(old_inode);
1033
	f2fs_mark_inode_dirty_sync(old_inode, false);
1034

1035
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1036
	old_page = NULL;
1037

C
Chao Yu 已提交
1038
	if (whiteout) {
1039
		set_inode_flag(whiteout, FI_INC_LINK);
C
Chao Yu 已提交
1040 1041 1042 1043 1044 1045 1046
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

1047
	if (old_dir_entry) {
1048
		if (old_dir != new_dir && !whiteout)
1049 1050
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
1051
		else
1052
			f2fs_put_page(old_dir_page, 0);
1053
		f2fs_i_links_write(old_dir, false);
1054
	}
1055
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
1056
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1057
		if (S_ISDIR(old_inode->i_mode))
C
Chao Yu 已提交
1058 1059
			f2fs_add_ino_entry(sbi, old_inode->i_ino,
							TRANS_DIR_INO);
1060
	}
1061

1062
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1063 1064 1065

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
1066 1067

	f2fs_update_time(sbi, REQ_TIME);
1068 1069
	return 0;

J
Jaegeuk Kim 已提交
1070
put_out_dir:
1071
	f2fs_unlock_op(sbi);
1072
	f2fs_put_page(new_page, 0);
1073
out_dir:
1074
	if (old_dir_entry)
1075 1076 1077 1078
		f2fs_put_page(old_dir_page, 0);
out_old:
	f2fs_put_page(old_page, 0);
out:
1079 1080
	if (whiteout)
		iput(whiteout);
1081 1082 1083
	return err;
}

C
Chao Yu 已提交
1084 1085 1086
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
1087
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1088 1089
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
1090 1091 1092 1093 1094
	struct page *old_dir_page, *new_dir_page;
	struct page *old_page, *new_page;
	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry, *new_entry;
	int old_nlink = 0, new_nlink = 0;
1095
	int err;
1096

1097 1098
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
1099 1100
	if (!f2fs_is_checkpoint_ready(sbi))
		return -ENOSPC;
1101

C
Chao Yu 已提交
1102 1103 1104 1105 1106 1107 1108 1109
	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
			!projid_eq(F2FS_I(new_dir)->i_projid,
			F2FS_I(old_dentry->d_inode)->i_projid)) ||
	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
			!projid_eq(F2FS_I(old_dir)->i_projid,
			F2FS_I(new_dentry->d_inode)->i_projid)))
		return -EXDEV;

C
Chao Yu 已提交
1110 1111 1112 1113 1114 1115 1116 1117
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

	err = dquot_initialize(new_dir);
	if (err)
		goto out;

1118
	err = -ENOENT;
C
Chao Yu 已提交
1119
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1120 1121 1122
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
C
Chao Yu 已提交
1123
		goto out;
1124
	}
C
Chao Yu 已提交
1125 1126

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1127 1128 1129
	if (!new_entry) {
		if (IS_ERR(new_page))
			err = PTR_ERR(new_page);
C
Chao Yu 已提交
1130
		goto out_old;
1131
	}
C
Chao Yu 已提交
1132 1133 1134 1135 1136 1137

	/* prepare for updating ".." directory entry info later */
	if (old_dir != new_dir) {
		if (S_ISDIR(old_inode->i_mode)) {
			old_dir_entry = f2fs_parent_dir(old_inode,
							&old_dir_page);
1138
			if (!old_dir_entry) {
1139 1140
				if (IS_ERR(old_dir_page))
					err = PTR_ERR(old_dir_page);
C
Chao Yu 已提交
1141
				goto out_new;
1142
			}
C
Chao Yu 已提交
1143 1144 1145 1146 1147
		}

		if (S_ISDIR(new_inode->i_mode)) {
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
1148
			if (!new_dir_entry) {
1149 1150
				if (IS_ERR(new_dir_page))
					err = PTR_ERR(new_dir_page);
C
Chao Yu 已提交
1151
				goto out_old_dir;
1152
			}
C
Chao Yu 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
		}
	}

	/*
	 * If cross rename between file and directory those are not
	 * in the same directory, we will inc nlink of file's parent
	 * later, so we should check upper boundary of its nlink.
	 */
	if ((!old_dir_entry || !new_dir_entry) &&
				old_dir_entry != new_dir_entry) {
		old_nlink = old_dir_entry ? -1 : 1;
		new_nlink = -old_nlink;
		err = -EMLINK;
1166 1167
		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
C
Chao Yu 已提交
1168 1169 1170
			goto out_new_dir;
	}

J
Jaegeuk Kim 已提交
1171
	f2fs_balance_fs(sbi, true);
1172

C
Chao Yu 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	f2fs_lock_op(sbi);

	/* update ".." directory entry info of old dentry */
	if (old_dir_entry)
		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);

	/* update ".." directory entry info of new dentry */
	if (new_dir_entry)
		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);

	/* update directory entry info of old dir inode */
	f2fs_set_link(old_dir, old_entry, old_page, new_inode);

	down_write(&F2FS_I(old_inode)->i_sem);
1187 1188 1189 1190 1191
	if (!old_dir_entry)
		file_lost_pino(old_inode);
	else
		/* adjust dir's i_pino to pass fsck check */
		f2fs_i_pino_write(old_inode, new_dir->i_ino);
C
Chao Yu 已提交
1192 1193
	up_write(&F2FS_I(old_inode)->i_sem);

1194
	old_dir->i_ctime = current_time(old_dir);
C
Chao Yu 已提交
1195 1196
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
1197
		f2fs_i_links_write(old_dir, old_nlink > 0);
C
Chao Yu 已提交
1198 1199
		up_write(&F2FS_I(old_dir)->i_sem);
	}
1200
	f2fs_mark_inode_dirty_sync(old_dir, false);
C
Chao Yu 已提交
1201 1202 1203 1204 1205

	/* update directory entry info of new dir inode */
	f2fs_set_link(new_dir, new_entry, new_page, old_inode);

	down_write(&F2FS_I(new_inode)->i_sem);
1206 1207 1208 1209 1210
	if (!new_dir_entry)
		file_lost_pino(new_inode);
	else
		/* adjust dir's i_pino to pass fsck check */
		f2fs_i_pino_write(new_inode, old_dir->i_ino);
C
Chao Yu 已提交
1211 1212
	up_write(&F2FS_I(new_inode)->i_sem);

1213
	new_dir->i_ctime = current_time(new_dir);
C
Chao Yu 已提交
1214 1215
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
1216
		f2fs_i_links_write(new_dir, new_nlink > 0);
C
Chao Yu 已提交
1217 1218
		up_write(&F2FS_I(new_dir)->i_sem);
	}
1219
	f2fs_mark_inode_dirty_sync(new_dir, false);
C
Chao Yu 已提交
1220

1221
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
1222 1223
		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1224
	}
1225

C
Chao Yu 已提交
1226
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1227 1228 1229

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
1230 1231

	f2fs_update_time(sbi, REQ_TIME);
C
Chao Yu 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	return 0;
out_new_dir:
	if (new_dir_entry) {
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
	f2fs_put_page(new_page, 0);
out_old:
	f2fs_put_page(old_page, 0);
out:
	return err;
}

static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
{
1253 1254
	int err;

C
Chao Yu 已提交
1255
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
1256 1257
		return -EINVAL;

1258 1259 1260 1261 1262
	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
				     flags);
	if (err)
		return err;

C
Chao Yu 已提交
1263 1264 1265 1266 1267 1268 1269 1270
	if (flags & RENAME_EXCHANGE) {
		return f2fs_cross_rename(old_dir, old_dentry,
					 new_dir, new_dentry);
	}
	/*
	 * VFS has already handled the new dentry existence case,
	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
	 */
C
Chao Yu 已提交
1271
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
1272 1273
}

1274
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1275 1276
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
1277
{
1278 1279
	struct page *page;
	const char *target;
1280

1281 1282 1283
	if (!dentry)
		return ERR_PTR(-ECHILD);

1284 1285 1286
	page = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(page))
		return ERR_CAST(page);
1287

1288 1289 1290 1291
	target = fscrypt_get_symlink(inode, page_address(page),
				     inode->i_sb->s_blocksize, done);
	put_page(page);
	return target;
C
Chao Yu 已提交
1292 1293
}

1294
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1295
	.get_link       = f2fs_encrypted_get_link,
1296 1297
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1298
#ifdef CONFIG_F2FS_FS_XATTR
1299
	.listxattr	= f2fs_listxattr,
1300
#endif
1301 1302
};

1303 1304 1305 1306 1307 1308 1309 1310 1311
const struct inode_operations f2fs_dir_inode_operations = {
	.create		= f2fs_create,
	.lookup		= f2fs_lookup,
	.link		= f2fs_link,
	.unlink		= f2fs_unlink,
	.symlink	= f2fs_symlink,
	.mkdir		= f2fs_mkdir,
	.rmdir		= f2fs_rmdir,
	.mknod		= f2fs_mknod,
1312
	.rename		= f2fs_rename2,
C
Chao Yu 已提交
1313
	.tmpfile	= f2fs_tmpfile,
1314
	.getattr	= f2fs_getattr,
1315 1316
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1317
	.set_acl	= f2fs_set_acl,
1318 1319 1320
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
1321
	.fiemap		= f2fs_fiemap,
1322 1323 1324
};

const struct inode_operations f2fs_symlink_inode_operations = {
1325
	.get_link       = f2fs_get_link,
1326
	.getattr	= f2fs_getattr,
1327 1328 1329 1330 1331 1332 1333
	.setattr	= f2fs_setattr,
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_special_inode_operations = {
1334
	.getattr	= f2fs_getattr,
1335 1336
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1337
	.set_acl	= f2fs_set_acl,
1338 1339 1340 1341
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};