namei.c 29.7 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
	f2fs_set_inode_flags(inode);

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

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

146
static inline int is_extension_exist(const unsigned char *s, const char *sub)
147
{
148 149
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
150
	int i;
151

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

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

166
	return 0;
167 168
}

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

	down_read(&sbi->sb_lock);

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

C
Chao Yu 已提交
183
	for (i = 0; i < cold_count + hot_count; i++) {
184 185
		if (is_extension_exist(name, extlist[i]))
			break;
186
	}
187 188

	up_read(&sbi->sb_lock);
189 190 191 192 193 194 195 196

	if (i == cold_count + hot_count)
		return;

	if (i < cold_count)
		file_set_cold(inode);
	else
		file_set_hot(inode);
197 198
}

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

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

		if (set)
			return -EINVAL;

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

	if (!set)
		return -EINVAL;

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

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

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

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

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

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

	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;

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

C
Chao Yu 已提交
301
	f2fs_alloc_nid_done(sbi, ino);
302

303
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
304 305 306

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
307 308

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

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

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

328 329 330
	err = fscrypt_prepare_link(old_dentry, dir, dentry);
	if (err)
		return err;
331

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

J
Jaegeuk Kim 已提交
341
	f2fs_balance_fs(sbi, true);
342

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

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

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

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

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

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

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

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

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

399 400 401 402 403
	f2fs_lock_op(sbi);

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

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

	f2fs_unlock_op(sbi);
	return err;
}

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

C
Chao Yu 已提交
440 441 442 443 444 445
	trace_f2fs_lookup_start(dir, dentry, flags);

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

447 448 449 450 451 452 453 454
	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 已提交
455
	if (!de) {
C
Chao Yu 已提交
456 457 458 459 460
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto out;
		}
		goto out_splice;
J
Jaegeuk Kim 已提交
461
	}
462

J
Jaegeuk Kim 已提交
463 464
	ino = le32_to_cpu(de->ino);
	f2fs_put_page(page, 0);
465

J
Jaegeuk Kim 已提交
466
	inode = f2fs_iget(dir->i_sb, ino);
C
Chao Yu 已提交
467 468 469 470
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out;
	}
471

472 473 474
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
C
Chao Yu 已提交
475
			goto out_iput;
476 477
	}

478
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
479
		err = __recover_dot_dentries(inode, dir->i_ino);
480
		if (err)
C
Chao Yu 已提交
481
			goto out_iput;
482
	}
483
	if (IS_ENCRYPTED(dir) &&
D
Dan Carpenter 已提交
484 485
	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
	    !fscrypt_has_permitted_context(dir, inode)) {
486 487
		f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
			  dir->i_ino, inode->i_ino);
488
		err = -EPERM;
C
Chao Yu 已提交
489
		goto out_iput;
490
	}
C
Chao Yu 已提交
491
out_splice:
492 493 494 495 496 497 498 499 500 501 502
#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 已提交
503
	new = d_splice_alias(inode, dentry);
504
	err = PTR_ERR_OR_ZERO(new);
C
Chao Yu 已提交
505 506 507
	trace_f2fs_lookup_end(dir, dentry, ino, err);
	return new;
out_iput:
508
	iput(inode);
C
Chao Yu 已提交
509 510
out:
	trace_f2fs_lookup_end(dir, dentry, ino, err);
511
	return ERR_PTR(err);
512 513 514 515
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
516
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
517
	struct inode *inode = d_inode(dentry);
518 519 520 521
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

522
	trace_f2fs_unlink_enter(dir, dentry);
523

524 525 526
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

C
Chao Yu 已提交
527
	err = dquot_initialize(dir);
J
Jaegeuk Kim 已提交
528 529 530
	if (err)
		return err;
	err = dquot_initialize(inode);
C
Chao Yu 已提交
531 532 533
	if (err)
		return err;

534
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
535 536 537
	if (!de) {
		if (IS_ERR(page))
			err = PTR_ERR(page);
538
		goto fail;
539
	}
540

J
Jaegeuk Kim 已提交
541
	f2fs_balance_fs(sbi, true);
542

543
	f2fs_lock_op(sbi);
C
Chao Yu 已提交
544
	err = f2fs_acquire_orphan_inode(sbi);
545
	if (err) {
546
		f2fs_unlock_op(sbi);
547 548 549
		f2fs_put_page(page, 0);
		goto fail;
	}
550
	f2fs_delete_entry(de, page, dir, inode);
551 552 553 554 555 556 557 558 559 560
#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
561
	f2fs_unlock_op(sbi);
562

J
Jaegeuk Kim 已提交
563 564
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
565
fail:
566
	trace_f2fs_unlink_exit(inode, err);
567 568 569
	return err;
}

570
static const char *f2fs_get_link(struct dentry *dentry,
571 572
				 struct inode *inode,
				 struct delayed_call *done)
573
{
574
	const char *link = page_get_link(dentry, inode, done);
575 576
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
577 578
		do_delayed_call(done);
		clear_delayed_call(done);
579
		link = ERR_PTR(-ENOENT);
580
	}
581
	return link;
582 583
}

584 585 586
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
587
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
588
	struct inode *inode;
589
	size_t len = strlen(symname);
590
	struct fscrypt_str disk_link;
591
	int err;
592

593 594
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
595 596 597
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
598

599 600 601 602
	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
				      &disk_link);
	if (err)
		return err;
603

C
Chao Yu 已提交
604 605 606 607
	err = dquot_initialize(dir);
	if (err)
		return err;

608 609 610 611
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

612
	if (IS_ENCRYPTED(inode))
613 614 615
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
616
	inode_nohighmem(inode);
617 618
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

619
	f2fs_lock_op(sbi);
620 621
	err = f2fs_add_link(dentry, inode);
	if (err)
C
Chao Yu 已提交
622
		goto out_f2fs_handle_failed_inode;
623
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
624
	f2fs_alloc_nid_done(sbi, inode->i_ino);
625

626 627 628
	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
	if (err)
		goto err_out;
629

630
	err = page_symlink(inode, disk_link.name, disk_link.len);
631 632

err_out:
633
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
634

635 636 637 638 639 640 641 642 643
	/*
	 * 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 已提交
644
	if (!err) {
645 646
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
647

C
Chao Yu 已提交
648 649 650 651 652
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
653

654
	f2fs_balance_fs(sbi, true);
655 656
	goto out_free_encrypted_link;

C
Chao Yu 已提交
657 658
out_f2fs_handle_failed_inode:
	f2fs_handle_failed_inode(inode);
659 660
out_free_encrypted_link:
	if (disk_link.name != (unsigned char *)symname)
661
		kvfree(disk_link.name);
662 663 664 665 666
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
667
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
668
	struct inode *inode;
669
	int err;
670

671 672 673
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;

C
Chao Yu 已提交
674 675 676 677
	err = dquot_initialize(dir);
	if (err)
		return err;

678 679
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
680
		return PTR_ERR(inode);
681 682 683 684

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

687
	set_inode_flag(inode, FI_INC_LINK);
688
	f2fs_lock_op(sbi);
689 690 691
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
692
	f2fs_unlock_op(sbi);
693

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

696
	d_instantiate_new(dentry, inode);
697

J
Jaegeuk Kim 已提交
698 699
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
700 701

	f2fs_balance_fs(sbi, true);
702 703 704
	return 0;

out_fail:
705
	clear_inode_flag(inode, FI_INC_LINK);
C
Chao Yu 已提交
706
	f2fs_handle_failed_inode(inode);
707 708 709 710 711
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
712
	struct inode *inode = d_inode(dentry);
713 714 715 716 717 718 719 720
	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)
{
721
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
722 723 724
	struct inode *inode;
	int err = 0;

725 726
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
727 728 729
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
730

C
Chao Yu 已提交
731 732 733 734
	err = dquot_initialize(dir);
	if (err)
		return err;

735 736 737 738 739 740 741
	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;

742
	f2fs_lock_op(sbi);
743 744 745
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
746
	f2fs_unlock_op(sbi);
747

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

750
	d_instantiate_new(dentry, inode);
J
Jaegeuk Kim 已提交
751 752 753

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
754 755

	f2fs_balance_fs(sbi, true);
756 757
	return 0;
out:
C
Chao Yu 已提交
758
	f2fs_handle_failed_inode(inode);
759 760 761
	return err;
}

C
Chao Yu 已提交
762 763 764 765 766 767 768
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 已提交
769 770 771 772
	err = dquot_initialize(dir);
	if (err)
		return err;

C
Chao Yu 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786
	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 已提交
787
	err = f2fs_acquire_orphan_inode(sbi);
C
Chao Yu 已提交
788 789 790 791 792 793 794 795 796 797 798
	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 已提交
799 800
	f2fs_add_orphan_inode(inode);
	f2fs_alloc_nid_done(sbi, inode->i_ino);
C
Chao Yu 已提交
801 802

	if (whiteout) {
803
		f2fs_i_links_write(inode, false);
C
Chao Yu 已提交
804 805 806 807
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
808 809
	/* link_count was changed by d_tmpfile as well. */
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
810
	unlock_new_inode(inode);
811 812

	f2fs_balance_fs(sbi, true);
C
Chao Yu 已提交
813 814 815
	return 0;

release_out:
C
Chao Yu 已提交
816
	f2fs_release_orphan_inode(sbi);
C
Chao Yu 已提交
817
out:
C
Chao Yu 已提交
818
	f2fs_handle_failed_inode(inode);
C
Chao Yu 已提交
819 820 821 822 823
	return err;
}

static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
824
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
825
	int ret;
826 827

	if (unlikely(f2fs_cp_error(sbi)))
828
		return -EIO;
829 830 831
	ret = f2fs_is_checkpoint_ready(sbi);
	if (ret)
		return ret;
832

833
	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
834
		int err = fscrypt_get_encryption_info(dir);
835 836 837 838
		if (err)
			return err;
	}

C
Chao Yu 已提交
839 840 841 842 843
	return __f2fs_tmpfile(dir, dentry, mode, NULL);
}

static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
{
844 845 846
	if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
		return -EIO;

C
Chao Yu 已提交
847 848 849
	return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
}

850
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
851 852
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
853
{
854
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
855 856
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
857
	struct inode *whiteout = NULL;
858
	struct page *old_dir_page;
C
Chao Yu 已提交
859
	struct page *old_page, *new_page = NULL;
860 861 862
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
863
	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
864
	int err;
865

866 867
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
868 869 870
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
871

C
Chao Yu 已提交
872 873 874 875 876
	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 已提交
877 878 879 880 881 882 883 884
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

J
Jaegeuk Kim 已提交
885 886 887 888 889 890
	if (new_inode) {
		err = dquot_initialize(new_inode);
		if (err)
			goto out;
	}

891
	err = -ENOENT;
892
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
893 894 895
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
896
		goto out;
897
	}
898 899 900

	if (S_ISDIR(old_inode->i_mode)) {
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
901
		if (!old_dir_entry) {
902 903
			if (IS_ERR(old_dir_page))
				err = PTR_ERR(old_dir_page);
904
			goto out_old;
905
		}
906 907
	}

C
Chao Yu 已提交
908 909 910 911 912 913
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

914 915 916 917
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
918
			goto out_whiteout;
919 920 921 922

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
923 924 925
		if (!new_entry) {
			if (IS_ERR(new_page))
				err = PTR_ERR(new_page);
C
Chao Yu 已提交
926
			goto out_whiteout;
927
		}
928

J
Jaegeuk Kim 已提交
929
		f2fs_balance_fs(sbi, true);
930

931 932
		f2fs_lock_op(sbi);

C
Chao Yu 已提交
933
		err = f2fs_acquire_orphan_inode(sbi);
J
Jaegeuk Kim 已提交
934 935 936
		if (err)
			goto put_out_dir;

937 938
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

939
		new_inode->i_ctime = current_time(new_inode);
940
		down_write(&F2FS_I(new_inode)->i_sem);
941
		if (old_dir_entry)
942 943
			f2fs_i_links_write(new_inode, false);
		f2fs_i_links_write(new_inode, false);
944 945
		up_write(&F2FS_I(new_inode)->i_sem);

946
		if (!new_inode->i_nlink)
C
Chao Yu 已提交
947
			f2fs_add_orphan_inode(new_inode);
J
Jaegeuk Kim 已提交
948
		else
C
Chao Yu 已提交
949
			f2fs_release_orphan_inode(sbi);
950
	} else {
J
Jaegeuk Kim 已提交
951
		f2fs_balance_fs(sbi, true);
952

953 954
		f2fs_lock_op(sbi);

955
		err = f2fs_add_link(new_dentry, old_inode);
956 957
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
958
			goto out_whiteout;
959
		}
960

961
		if (old_dir_entry)
962
			f2fs_i_links_write(new_dir, true);
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

		/*
		 * 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) {
978 979 980
				err = -ENOENT;
				if (IS_ERR(old_page))
					err = PTR_ERR(old_page);
981 982 983 984
				f2fs_unlock_op(sbi);
				goto out_whiteout;
			}
		}
985 986
	}

987
	down_write(&F2FS_I(old_inode)->i_sem);
988 989 990 991
	if (!old_dir_entry || whiteout)
		file_lost_pino(old_inode);
	else
		F2FS_I(old_inode)->i_pino = new_dir->i_ino;
992 993
	up_write(&F2FS_I(old_inode)->i_sem);

994
	old_inode->i_ctime = current_time(old_inode);
995
	f2fs_mark_inode_dirty_sync(old_inode, false);
996

997
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
998

C
Chao Yu 已提交
999 1000
	if (whiteout) {
		whiteout->i_state |= I_LINKABLE;
1001
		set_inode_flag(whiteout, FI_INC_LINK);
C
Chao Yu 已提交
1002 1003 1004 1005 1006 1007 1008
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

1009
	if (old_dir_entry) {
1010
		if (old_dir != new_dir && !whiteout)
1011 1012
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
1013
		else
1014
			f2fs_put_page(old_dir_page, 0);
1015
		f2fs_i_links_write(old_dir, false);
1016
	}
1017
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
1018
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1019
		if (S_ISDIR(old_inode->i_mode))
C
Chao Yu 已提交
1020 1021
			f2fs_add_ino_entry(sbi, old_inode->i_ino,
							TRANS_DIR_INO);
1022
	}
1023

1024
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1025 1026 1027

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
1028 1029

	f2fs_update_time(sbi, REQ_TIME);
1030 1031
	return 0;

J
Jaegeuk Kim 已提交
1032
put_out_dir:
1033
	f2fs_unlock_op(sbi);
1034
	if (new_page)
C
Chao Yu 已提交
1035 1036 1037 1038
		f2fs_put_page(new_page, 0);
out_whiteout:
	if (whiteout)
		iput(whiteout);
1039
out_dir:
1040
	if (old_dir_entry)
1041 1042 1043 1044 1045 1046 1047
		f2fs_put_page(old_dir_page, 0);
out_old:
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
1048 1049 1050
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
1051
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1052 1053
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
1054 1055 1056 1057 1058
	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;
1059
	int err;
1060

1061 1062
	if (unlikely(f2fs_cp_error(sbi)))
		return -EIO;
D
Daniel Rosenberg 已提交
1063 1064 1065
	err = f2fs_is_checkpoint_ready(sbi);
	if (err)
		return err;
1066

C
Chao Yu 已提交
1067 1068 1069 1070 1071 1072 1073 1074
	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 已提交
1075 1076 1077 1078 1079 1080 1081 1082
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

1083
	err = -ENOENT;
C
Chao Yu 已提交
1084
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1085 1086 1087
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
C
Chao Yu 已提交
1088
		goto out;
1089
	}
C
Chao Yu 已提交
1090 1091

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1092 1093 1094
	if (!new_entry) {
		if (IS_ERR(new_page))
			err = PTR_ERR(new_page);
C
Chao Yu 已提交
1095
		goto out_old;
1096
	}
C
Chao Yu 已提交
1097 1098 1099 1100 1101 1102

	/* 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);
1103
			if (!old_dir_entry) {
1104 1105
				if (IS_ERR(old_dir_page))
					err = PTR_ERR(old_dir_page);
C
Chao Yu 已提交
1106
				goto out_new;
1107
			}
C
Chao Yu 已提交
1108 1109 1110 1111 1112
		}

		if (S_ISDIR(new_inode->i_mode)) {
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
1113
			if (!new_dir_entry) {
1114 1115
				if (IS_ERR(new_dir_page))
					err = PTR_ERR(new_dir_page);
C
Chao Yu 已提交
1116
				goto out_old_dir;
1117
			}
C
Chao Yu 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		}
	}

	/*
	 * 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;
1131 1132
		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
C
Chao Yu 已提交
1133 1134 1135
			goto out_new_dir;
	}

J
Jaegeuk Kim 已提交
1136
	f2fs_balance_fs(sbi, true);
1137

C
Chao Yu 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	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);

1155
	old_dir->i_ctime = current_time(old_dir);
C
Chao Yu 已提交
1156 1157
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
1158
		f2fs_i_links_write(old_dir, old_nlink > 0);
C
Chao Yu 已提交
1159 1160
		up_write(&F2FS_I(old_dir)->i_sem);
	}
1161
	f2fs_mark_inode_dirty_sync(old_dir, false);
C
Chao Yu 已提交
1162 1163 1164 1165 1166 1167 1168 1169

	/* 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);

1170
	new_dir->i_ctime = current_time(new_dir);
C
Chao Yu 已提交
1171 1172
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
1173
		f2fs_i_links_write(new_dir, new_nlink > 0);
C
Chao Yu 已提交
1174 1175
		up_write(&F2FS_I(new_dir)->i_sem);
	}
1176
	f2fs_mark_inode_dirty_sync(new_dir, false);
C
Chao Yu 已提交
1177

1178
	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
C
Chao Yu 已提交
1179 1180
		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1181
	}
1182

C
Chao Yu 已提交
1183
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1184 1185 1186

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
1187 1188

	f2fs_update_time(sbi, REQ_TIME);
C
Chao Yu 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	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)
{
1210 1211
	int err;

C
Chao Yu 已提交
1212
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
1213 1214
		return -EINVAL;

1215 1216 1217 1218 1219
	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
				     flags);
	if (err)
		return err;

C
Chao Yu 已提交
1220 1221 1222 1223 1224 1225 1226 1227
	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 已提交
1228
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
1229 1230
}

1231
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1232 1233
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
1234
{
1235 1236
	struct page *page;
	const char *target;
1237

1238 1239 1240
	if (!dentry)
		return ERR_PTR(-ECHILD);

1241 1242 1243
	page = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(page))
		return ERR_CAST(page);
1244

1245 1246 1247 1248
	target = fscrypt_get_symlink(inode, page_address(page),
				     inode->i_sb->s_blocksize, done);
	put_page(page);
	return target;
C
Chao Yu 已提交
1249 1250
}

1251
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1252
	.get_link       = f2fs_encrypted_get_link,
1253 1254
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1255
#ifdef CONFIG_F2FS_FS_XATTR
1256
	.listxattr	= f2fs_listxattr,
1257
#endif
1258 1259
};

1260 1261 1262 1263 1264 1265 1266 1267 1268
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,
1269
	.rename		= f2fs_rename2,
C
Chao Yu 已提交
1270
	.tmpfile	= f2fs_tmpfile,
1271
	.getattr	= f2fs_getattr,
1272 1273
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1274
	.set_acl	= f2fs_set_acl,
1275 1276 1277
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
1278
	.fiemap		= f2fs_fiemap,
1279 1280 1281
};

const struct inode_operations f2fs_symlink_inode_operations = {
1282
	.get_link       = f2fs_get_link,
1283
	.getattr	= f2fs_getattr,
1284 1285 1286 1287 1288 1289 1290
	.setattr	= f2fs_setattr,
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_special_inode_operations = {
1291
	.getattr	= f2fs_getattr,
1292 1293
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1294
	.set_acl	= f2fs_set_acl,
1295 1296 1297 1298
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};