namei.c 28.8 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>
C
Chao Yu 已提交
13
#include <linux/dcache.h>
14
#include <linux/namei.h>
C
Chao Yu 已提交
15
#include <linux/quotaops.h>
16 17

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

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

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

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

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

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

	inode->i_ino = ino;
	inode->i_blocks = 0;
51
	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
52
	F2FS_I(inode)->i_crtime = inode->i_mtime;
53 54
	inode->i_generation = sbi->s_next_generation++;

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

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

C
Chao Yu 已提交
64
	if (f2fs_sb_has_project_quota(sbi->sb) &&
65
		(F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
C
Chao Yu 已提交
66 67 68 69 70
		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 已提交
71 72 73 74 75 76 77 78
	err = dquot_initialize(inode);
	if (err)
		goto fail_drop;

	err = dquot_alloc_inode(inode);
	if (err)
		goto fail_drop;

79 80
	set_inode_flag(inode, FI_NEW_INODE);

81
	/* If the directory encrypted, then we should encrypt the inode. */
82 83
	if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
				f2fs_may_encrypt(inode))
84 85
		f2fs_set_encrypted_inode(inode);

86 87 88 89 90
	if (f2fs_sb_has_extra_attr(sbi->sb)) {
		set_inode_flag(inode, FI_EXTRA_ATTR);
		F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
	}

91 92
	if (test_opt(sbi, INLINE_XATTR))
		set_inode_flag(inode, FI_INLINE_XATTR);
C
Chao Yu 已提交
93

94
	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
95
		set_inode_flag(inode, FI_INLINE_DATA);
96
	if (f2fs_may_inline_dentry(inode))
97
		set_inode_flag(inode, FI_INLINE_DENTRY);
C
Chao Yu 已提交
98

C
Chao Yu 已提交
99 100 101
	if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
		f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
		if (f2fs_has_inline_xattr(inode))
102
			xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
C
Chao Yu 已提交
103 104 105 106 107 108 109
		/* 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 已提交
110 111
	f2fs_init_extent_tree(inode, NULL);

C
Chao Yu 已提交
112
	stat_inc_inline_xattr(inode);
113 114 115
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

C
Chao Yu 已提交
116 117 118
	F2FS_I(inode)->i_flags =
		f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);

C
Chao Yu 已提交
119
	if (S_ISDIR(inode->i_mode))
120
		F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
C
Chao Yu 已提交
121

122
	if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
C
Chao Yu 已提交
123 124
		set_inode_flag(inode, FI_PROJ_INHERIT);

125
	trace_f2fs_new_inode(inode, 0);
126 127 128
	return inode;

fail:
129
	trace_f2fs_new_inode(inode, err);
130
	make_bad_inode(inode);
131
	if (nid_free)
132
		set_inode_flag(inode, FI_FREE_NID);
133
	iput(inode);
134
	return ERR_PTR(err);
C
Chao Yu 已提交
135 136 137 138 139 140 141 142 143 144
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);
145 146
}

C
Chao Yu 已提交
147
static int is_extension_exist(const unsigned char *s, const char *sub)
148
{
149 150
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
151
	int i;
152

C
Chao Yu 已提交
153 154
	/*
	 * filename format of multimedia file should be defined as:
155
	 * "filename + '.' + extension + (optional: '.' + temp extension)".
C
Chao Yu 已提交
156 157 158 159
	 */
	if (slen < sublen + 2)
		return 0;

160 161 162 163 164 165
	for (i = 1; i < slen - sublen; i++) {
		if (s[i] != '.')
			continue;
		if (!strncasecmp(s + i + 1, sub, sublen))
			return 1;
	}
166

167
	return 0;
168 169
}

J
Jaegeuk Kim 已提交
170
/*
171 172
 * Set multimedia files as cold files for hot/cold data separation
 */
C
Chao Yu 已提交
173
static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
174 175
		const unsigned char *name)
{
176
	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
C
Chao Yu 已提交
177
	int i, cold_count, hot_count;
178 179 180

	down_read(&sbi->sb_lock);

C
Chao Yu 已提交
181 182
	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
	hot_count = sbi->raw_super->hot_ext_count;
183

C
Chao Yu 已提交
184 185 186 187
	for (i = 0; i < cold_count + hot_count; i++) {
		if (!is_extension_exist(name, extlist[i]))
			continue;
		if (i < cold_count)
188
			file_set_cold(inode);
C
Chao Yu 已提交
189 190 191
		else
			file_set_hot(inode);
		break;
192
	}
193 194 195 196

	up_read(&sbi->sb_lock);
}

C
Chao Yu 已提交
197
int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
C
Chao Yu 已提交
198
							bool hot, bool set)
199 200
{
	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
C
Chao Yu 已提交
201 202 203 204
	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;
205 206
	int i;

C
Chao Yu 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	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++) {
226 227 228 229 230 231 232
		if (strcmp(name, extlist[i]))
			continue;

		if (set)
			return -EINVAL;

		memcpy(extlist[i], extlist[i + 1],
C
Chao Yu 已提交
233 234 235 236 237 238 239
				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);
240 241 242 243 244 245
		return 0;
	}

	if (!set)
		return -EINVAL;

C
Chao Yu 已提交
246
	if (hot) {
247
		memcpy(extlist[count], name, strlen(name));
C
Chao Yu 已提交
248 249 250 251 252 253 254
		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);
255
		memcpy(extlist[cold_count], name, strlen(name));
C
Chao Yu 已提交
256 257 258 259
		memcpy(&extlist[cold_count + 1], buf,
				F2FS_EXTENSION_LEN * hot_count);
		sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
	}
260
	return 0;
261 262 263 264 265
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
266
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
267 268
	struct inode *inode;
	nid_t ino = 0;
269
	int err;
270

271 272
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
273 274 275
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
276

C
Chao Yu 已提交
277 278 279 280
	err = dquot_initialize(dir);
	if (err)
		return err;

281 282 283 284 285
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
C
Chao Yu 已提交
286
		set_file_temperature(sbi, inode, dentry->d_name.name);
287 288 289 290 291 292

	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;

293
	f2fs_lock_op(sbi);
294 295 296
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
297
	f2fs_unlock_op(sbi);
298

C
Chao Yu 已提交
299
	f2fs_alloc_nid_done(sbi, ino);
300

301
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
302 303 304

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
305 306

	f2fs_balance_fs(sbi, true);
307 308
	return 0;
out:
C
Chao Yu 已提交
309
	f2fs_handle_failed_inode(inode);
310 311 312 313 314 315
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
316
	struct inode *inode = d_inode(old_dentry);
317
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
318
	int err;
319

320 321
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
322 323 324
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
325

326 327 328
	err = fscrypt_prepare_link(old_dentry, dir, dentry);
	if (err)
		return err;
329

C
Chao Yu 已提交
330 331 332 333 334
	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 已提交
335 336 337 338
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
339
	f2fs_balance_fs(sbi, true);
340

341
	inode->i_ctime = current_time(inode);
J
Jaegeuk Kim 已提交
342
	ihold(inode);
343

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

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
352 353 354

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
355 356
	return 0;
out:
357
	clear_inode_flag(inode, FI_INC_LINK);
358
	iput(inode);
359
	f2fs_unlock_op(sbi);
360 361 362 363 364 365
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
366 367 368 369 370
	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);
371
		return ERR_PTR(-ENOENT);
372
	}
373
	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
374 375
}

376 377 378 379 380 381 382 383 384
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;

385 386 387 388 389 390 391
	if (f2fs_readonly(sbi->sb)) {
		f2fs_msg(sbi->sb, KERN_INFO,
			"skip recovering inline_dots inode (ino:%lu, pino:%u) "
			"in readonly mountpoint", dir->i_ino, pino);
		return 0;
	}

392 393 394 395
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
396
	f2fs_balance_fs(sbi, true);
397

398 399 400 401 402
	f2fs_lock_op(sbi);

	de = f2fs_find_entry(dir, &dot, &page);
	if (de) {
		f2fs_put_page(page, 0);
403 404 405
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto out;
406
	} else {
C
Chao Yu 已提交
407
		err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
408 409 410 411 412
		if (err)
			goto out;
	}

	de = f2fs_find_entry(dir, &dotdot, &page);
413
	if (de)
414
		f2fs_put_page(page, 0);
415
	else if (IS_ERR(page))
416
		err = PTR_ERR(page);
417
	else
C
Chao Yu 已提交
418
		err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
419
out:
420
	if (!err)
421
		clear_inode_flag(dir, FI_INLINE_DOTS);
422 423 424 425 426

	f2fs_unlock_op(sbi);
	return err;
}

427 428 429 430 431 432
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 已提交
433 434
	struct dentry *new;
	nid_t ino = -1;
435
	int err = 0;
436
	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
437

C
Chao Yu 已提交
438 439
	trace_f2fs_lookup_start(dir, dentry, flags);

440 441 442
	err = fscrypt_prepare_lookup(dir, dentry, flags);
	if (err)
		goto out;
443

C
Chao Yu 已提交
444 445 446 447
	if (dentry->d_name.len > F2FS_NAME_LEN) {
		err = -ENAMETOOLONG;
		goto out;
	}
448 449

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
J
Jaegeuk Kim 已提交
450
	if (!de) {
C
Chao Yu 已提交
451 452 453 454 455
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto out;
		}
		goto out_splice;
J
Jaegeuk Kim 已提交
456
	}
457

J
Jaegeuk Kim 已提交
458 459
	ino = le32_to_cpu(de->ino);
	f2fs_put_page(page, 0);
460

J
Jaegeuk Kim 已提交
461
	inode = f2fs_iget(dir->i_sb, ino);
C
Chao Yu 已提交
462 463 464 465
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out;
	}
466

467 468 469
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
C
Chao Yu 已提交
470
			goto out_iput;
471 472
	}

473
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
474
		err = __recover_dot_dentries(inode, dir->i_ino);
475
		if (err)
C
Chao Yu 已提交
476
			goto out_iput;
477
	}
D
Dan Carpenter 已提交
478 479 480
	if (f2fs_encrypted_inode(dir) &&
	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
	    !fscrypt_has_permitted_context(dir, inode)) {
481 482 483 484
		f2fs_msg(inode->i_sb, KERN_WARNING,
			 "Inconsistent encryption contexts: %lu/%lu",
			 dir->i_ino, inode->i_ino);
		err = -EPERM;
C
Chao Yu 已提交
485
		goto out_iput;
486
	}
C
Chao Yu 已提交
487 488 489 490 491 492 493
out_splice:
	new = d_splice_alias(inode, dentry);
	if (IS_ERR(new))
		err = PTR_ERR(new);
	trace_f2fs_lookup_end(dir, dentry, ino, err);
	return new;
out_iput:
494
	iput(inode);
C
Chao Yu 已提交
495 496
out:
	trace_f2fs_lookup_end(dir, dentry, ino, err);
497
	return ERR_PTR(err);
498 499 500 501
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
502
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
503
	struct inode *inode = d_inode(dentry);
504 505 506 507
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

508
	trace_f2fs_unlink_enter(dir, dentry);
509

510 511 512
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

C
Chao Yu 已提交
513
	err = dquot_initialize(dir);
J
Jaegeuk Kim 已提交
514 515 516
	if (err)
		return err;
	err = dquot_initialize(inode);
C
Chao Yu 已提交
517 518 519
	if (err)
		return err;

520
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
521 522 523
	if (!de) {
		if (IS_ERR(page))
			err = PTR_ERR(page);
524
		goto fail;
525
	}
526

J
Jaegeuk Kim 已提交
527
	f2fs_balance_fs(sbi, true);
528

529
	f2fs_lock_op(sbi);
C
Chao Yu 已提交
530
	err = f2fs_acquire_orphan_inode(sbi);
531
	if (err) {
532
		f2fs_unlock_op(sbi);
533 534 535
		f2fs_put_page(page, 0);
		goto fail;
	}
536
	f2fs_delete_entry(de, page, dir, inode);
537
	f2fs_unlock_op(sbi);
538

J
Jaegeuk Kim 已提交
539 540
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
541
fail:
542
	trace_f2fs_unlink_exit(inode, err);
543 544 545
	return err;
}

546
static const char *f2fs_get_link(struct dentry *dentry,
547 548
				 struct inode *inode,
				 struct delayed_call *done)
549
{
550
	const char *link = page_get_link(dentry, inode, done);
551 552
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
553 554
		do_delayed_call(done);
		clear_delayed_call(done);
555
		link = ERR_PTR(-ENOENT);
556
	}
557
	return link;
558 559
}

560 561 562
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
563
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
564
	struct inode *inode;
565
	size_t len = strlen(symname);
566
	struct fscrypt_str disk_link;
567
	int err;
568

569 570
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
571 572 573
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
574

575 576 577 578
	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
				      &disk_link);
	if (err)
		return err;
579

C
Chao Yu 已提交
580 581 582 583
	err = dquot_initialize(dir);
	if (err)
		return err;

584 585 586 587
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

588
	if (IS_ENCRYPTED(inode))
589 590 591
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
592
	inode_nohighmem(inode);
593 594
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

595
	f2fs_lock_op(sbi);
596 597
	err = f2fs_add_link(dentry, inode);
	if (err)
C
Chao Yu 已提交
598
		goto out_f2fs_handle_failed_inode;
599
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
600
	f2fs_alloc_nid_done(sbi, inode->i_ino);
601

602 603 604
	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
	if (err)
		goto err_out;
605

606
	err = page_symlink(inode, disk_link.name, disk_link.len);
607 608

err_out:
609
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
610

611 612 613 614 615 616 617 618 619
	/*
	 * 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 已提交
620
	if (!err) {
621 622
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
623

C
Chao Yu 已提交
624 625 626 627 628
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
629

630
	f2fs_balance_fs(sbi, true);
631 632
	goto out_free_encrypted_link;

C
Chao Yu 已提交
633 634
out_f2fs_handle_failed_inode:
	f2fs_handle_failed_inode(inode);
635 636 637
out_free_encrypted_link:
	if (disk_link.name != (unsigned char *)symname)
		kfree(disk_link.name);
638 639 640 641 642
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
643
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
644
	struct inode *inode;
645
	int err;
646

647 648 649
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

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

654 655
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
656
		return PTR_ERR(inode);
657 658 659 660

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

663
	set_inode_flag(inode, FI_INC_LINK);
664
	f2fs_lock_op(sbi);
665 666 667
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
668
	f2fs_unlock_op(sbi);
669

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

672
	d_instantiate_new(dentry, inode);
673

J
Jaegeuk Kim 已提交
674 675
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
676 677

	f2fs_balance_fs(sbi, true);
678 679 680
	return 0;

out_fail:
681
	clear_inode_flag(inode, FI_INC_LINK);
C
Chao Yu 已提交
682
	f2fs_handle_failed_inode(inode);
683 684 685 686 687
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
688
	struct inode *inode = d_inode(dentry);
689 690 691 692 693 694 695 696
	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)
{
697
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
698 699 700
	struct inode *inode;
	int err = 0;

701 702
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
703 704 705
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
706

C
Chao Yu 已提交
707 708 709 710
	err = dquot_initialize(dir);
	if (err)
		return err;

711 712 713 714 715 716 717
	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;

718
	f2fs_lock_op(sbi);
719 720 721
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
722
	f2fs_unlock_op(sbi);
723

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

726
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
727 728 729

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
730 731

	f2fs_balance_fs(sbi, true);
732 733
	return 0;
out:
C
Chao Yu 已提交
734
	f2fs_handle_failed_inode(inode);
735 736 737
	return err;
}

C
Chao Yu 已提交
738 739 740 741 742 743 744
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 已提交
745 746 747 748
	err = dquot_initialize(dir);
	if (err)
		return err;

C
Chao Yu 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762
	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 已提交
763
	err = f2fs_acquire_orphan_inode(sbi);
C
Chao Yu 已提交
764 765 766 767 768 769 770 771 772 773 774
	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 已提交
775 776
	f2fs_add_orphan_inode(inode);
	f2fs_alloc_nid_done(sbi, inode->i_ino);
C
Chao Yu 已提交
777 778

	if (whiteout) {
779
		f2fs_i_links_write(inode, false);
C
Chao Yu 已提交
780 781 782 783
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
784 785
	/* link_count was changed by d_tmpfile as well. */
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
786
	unlock_new_inode(inode);
787 788

	f2fs_balance_fs(sbi, true);
C
Chao Yu 已提交
789 790 791
	return 0;

release_out:
C
Chao Yu 已提交
792
	f2fs_release_orphan_inode(sbi);
C
Chao Yu 已提交
793
out:
C
Chao Yu 已提交
794
	f2fs_handle_failed_inode(inode);
C
Chao Yu 已提交
795 796 797 798 799
	return err;
}

static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
800 801 802
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

	if (unlikely(f2fs_cp_error(sbi)))
803 804
		return -EIO;

805
	if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
806
		int err = fscrypt_get_encryption_info(dir);
807 808 809 810
		if (err)
			return err;
	}

C
Chao Yu 已提交
811 812 813 814 815
	return __f2fs_tmpfile(dir, dentry, mode, NULL);
}

static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
{
816 817 818
	if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
		return -EIO;

C
Chao Yu 已提交
819 820 821
	return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
}

822
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
823 824
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
825
{
826
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
827 828
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
829
	struct inode *whiteout = NULL;
830
	struct page *old_dir_page;
C
Chao Yu 已提交
831
	struct page *old_page, *new_page = NULL;
832 833 834
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
835
	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
836
	int err;
837

838 839
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
840 841 842
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
843

C
Chao Yu 已提交
844 845 846 847 848
	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;

C
Chao Yu 已提交
849 850 851 852 853 854 855 856
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

J
Jaegeuk Kim 已提交
857 858 859 860 861 862
	if (new_inode) {
		err = dquot_initialize(new_inode);
		if (err)
			goto out;
	}

863
	err = -ENOENT;
864
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
865 866 867
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
868
		goto out;
869
	}
870 871 872

	if (S_ISDIR(old_inode->i_mode)) {
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
873
		if (!old_dir_entry) {
874 875
			if (IS_ERR(old_dir_page))
				err = PTR_ERR(old_dir_page);
876
			goto out_old;
877
		}
878 879
	}

C
Chao Yu 已提交
880 881 882 883 884 885
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

886 887 888 889
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
890
			goto out_whiteout;
891 892 893 894

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
895 896 897
		if (!new_entry) {
			if (IS_ERR(new_page))
				err = PTR_ERR(new_page);
C
Chao Yu 已提交
898
			goto out_whiteout;
899
		}
900

J
Jaegeuk Kim 已提交
901
		f2fs_balance_fs(sbi, true);
902

903 904
		f2fs_lock_op(sbi);

C
Chao Yu 已提交
905
		err = f2fs_acquire_orphan_inode(sbi);
J
Jaegeuk Kim 已提交
906 907 908
		if (err)
			goto put_out_dir;

909 910
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

911
		new_inode->i_ctime = current_time(new_inode);
912
		down_write(&F2FS_I(new_inode)->i_sem);
913
		if (old_dir_entry)
914 915
			f2fs_i_links_write(new_inode, false);
		f2fs_i_links_write(new_inode, false);
916 917
		up_write(&F2FS_I(new_inode)->i_sem);

918
		if (!new_inode->i_nlink)
C
Chao Yu 已提交
919
			f2fs_add_orphan_inode(new_inode);
J
Jaegeuk Kim 已提交
920
		else
C
Chao Yu 已提交
921
			f2fs_release_orphan_inode(sbi);
922
	} else {
J
Jaegeuk Kim 已提交
923
		f2fs_balance_fs(sbi, true);
924

925 926
		f2fs_lock_op(sbi);

927
		err = f2fs_add_link(new_dentry, old_inode);
928 929
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
930
			goto out_whiteout;
931
		}
932

933
		if (old_dir_entry)
934
			f2fs_i_links_write(new_dir, true);
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

		/*
		 * old entry and new entry can locate in the same inline
		 * dentry in inode, when attaching new entry in inline dentry,
		 * it could force inline dentry conversion, after that,
		 * old_entry and old_page will point to wrong address, in
		 * order to avoid this, let's do the check and update here.
		 */
		if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
			f2fs_put_page(old_page, 0);
			old_page = NULL;

			old_entry = f2fs_find_entry(old_dir,
						&old_dentry->d_name, &old_page);
			if (!old_entry) {
950 951 952
				err = -ENOENT;
				if (IS_ERR(old_page))
					err = PTR_ERR(old_page);
953 954 955 956
				f2fs_unlock_op(sbi);
				goto out_whiteout;
			}
		}
957 958
	}

959
	down_write(&F2FS_I(old_inode)->i_sem);
960 961 962 963
	if (!old_dir_entry || whiteout)
		file_lost_pino(old_inode);
	else
		F2FS_I(old_inode)->i_pino = new_dir->i_ino;
964 965
	up_write(&F2FS_I(old_inode)->i_sem);

966
	old_inode->i_ctime = current_time(old_inode);
967
	f2fs_mark_inode_dirty_sync(old_inode, false);
968

969
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
970

C
Chao Yu 已提交
971 972
	if (whiteout) {
		whiteout->i_state |= I_LINKABLE;
973
		set_inode_flag(whiteout, FI_INC_LINK);
C
Chao Yu 已提交
974 975 976 977 978 979 980
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

981
	if (old_dir_entry) {
982
		if (old_dir != new_dir && !whiteout)
983 984
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
985
		else
986
			f2fs_put_page(old_dir_page, 0);
987
		f2fs_i_links_write(old_dir, false);
988
	}
989
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
990
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
991
		if (S_ISDIR(old_inode->i_mode))
C
Chao Yu 已提交
992 993
			f2fs_add_ino_entry(sbi, old_inode->i_ino,
							TRANS_DIR_INO);
994
	}
995

996
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
997 998 999

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
1000 1001
	return 0;

J
Jaegeuk Kim 已提交
1002
put_out_dir:
1003
	f2fs_unlock_op(sbi);
1004
	if (new_page)
C
Chao Yu 已提交
1005 1006 1007 1008
		f2fs_put_page(new_page, 0);
out_whiteout:
	if (whiteout)
		iput(whiteout);
1009
out_dir:
1010
	if (old_dir_entry)
1011 1012 1013 1014 1015 1016 1017
		f2fs_put_page(old_dir_page, 0);
out_old:
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
1018 1019 1020
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
1021
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1022 1023
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
1024 1025 1026 1027 1028
	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;
1029
	int err;
1030

1031 1032
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
1033 1034 1035
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
1036

C
Chao Yu 已提交
1037 1038 1039 1040 1041 1042 1043 1044
	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 已提交
1045 1046 1047 1048 1049 1050 1051 1052
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

1053
	err = -ENOENT;
C
Chao Yu 已提交
1054
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1055 1056 1057
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
C
Chao Yu 已提交
1058
		goto out;
1059
	}
C
Chao Yu 已提交
1060 1061

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1062 1063 1064
	if (!new_entry) {
		if (IS_ERR(new_page))
			err = PTR_ERR(new_page);
C
Chao Yu 已提交
1065
		goto out_old;
1066
	}
C
Chao Yu 已提交
1067 1068 1069 1070 1071 1072

	/* 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);
1073
			if (!old_dir_entry) {
1074 1075
				if (IS_ERR(old_dir_page))
					err = PTR_ERR(old_dir_page);
C
Chao Yu 已提交
1076
				goto out_new;
1077
			}
C
Chao Yu 已提交
1078 1079 1080 1081 1082
		}

		if (S_ISDIR(new_inode->i_mode)) {
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
1083
			if (!new_dir_entry) {
1084 1085
				if (IS_ERR(new_dir_page))
					err = PTR_ERR(new_dir_page);
C
Chao Yu 已提交
1086
				goto out_old_dir;
1087
			}
C
Chao Yu 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
		}
	}

	/*
	 * 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;
1101 1102
		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
C
Chao Yu 已提交
1103 1104 1105
			goto out_new_dir;
	}

J
Jaegeuk Kim 已提交
1106
	f2fs_balance_fs(sbi, true);
1107

C
Chao Yu 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	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);
	file_lost_pino(old_inode);
	up_write(&F2FS_I(old_inode)->i_sem);

1125
	old_dir->i_ctime = current_time(old_dir);
C
Chao Yu 已提交
1126 1127
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
1128
		f2fs_i_links_write(old_dir, old_nlink > 0);
C
Chao Yu 已提交
1129 1130
		up_write(&F2FS_I(old_dir)->i_sem);
	}
1131
	f2fs_mark_inode_dirty_sync(old_dir, false);
C
Chao Yu 已提交
1132 1133 1134 1135 1136 1137 1138 1139

	/* 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);
	file_lost_pino(new_inode);
	up_write(&F2FS_I(new_inode)->i_sem);

1140
	new_dir->i_ctime = current_time(new_dir);
C
Chao Yu 已提交
1141 1142
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
1143
		f2fs_i_links_write(new_dir, new_nlink > 0);
C
Chao Yu 已提交
1144 1145
		up_write(&F2FS_I(new_dir)->i_sem);
	}
1146
	f2fs_mark_inode_dirty_sync(new_dir, false);
C
Chao Yu 已提交
1147

1148
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
1149 1150
		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1151
	}
1152

C
Chao Yu 已提交
1153
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1154 1155 1156

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	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)
{
1178 1179
	int err;

C
Chao Yu 已提交
1180
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
1181 1182
		return -EINVAL;

1183 1184 1185 1186 1187
	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
				     flags);
	if (err)
		return err;

C
Chao Yu 已提交
1188 1189 1190 1191 1192 1193 1194 1195
	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 已提交
1196
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
1197 1198
}

1199
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1200 1201
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
1202
{
1203 1204
	struct page *page;
	const char *target;
1205

1206 1207 1208
	if (!dentry)
		return ERR_PTR(-ECHILD);

1209 1210 1211
	page = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(page))
		return ERR_CAST(page);
1212

1213 1214 1215 1216
	target = fscrypt_get_symlink(inode, page_address(page),
				     inode->i_sb->s_blocksize, done);
	put_page(page);
	return target;
C
Chao Yu 已提交
1217 1218
}

1219
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1220
	.get_link       = f2fs_encrypted_get_link,
1221 1222
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1223
#ifdef CONFIG_F2FS_FS_XATTR
1224
	.listxattr	= f2fs_listxattr,
1225
#endif
1226 1227
};

1228 1229 1230 1231 1232 1233 1234 1235 1236
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,
1237
	.rename		= f2fs_rename2,
C
Chao Yu 已提交
1238
	.tmpfile	= f2fs_tmpfile,
1239
	.getattr	= f2fs_getattr,
1240 1241
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1242
	.set_acl	= f2fs_set_acl,
1243 1244 1245 1246 1247 1248
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_symlink_inode_operations = {
1249
	.get_link       = f2fs_get_link,
1250
	.getattr	= f2fs_getattr,
1251 1252 1253 1254 1255 1256 1257
	.setattr	= f2fs_setattr,
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_special_inode_operations = {
1258
	.getattr	= f2fs_getattr,
1259 1260
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1261
	.set_acl	= f2fs_set_acl,
1262 1263 1264 1265
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};