namei.c 25.6 KB
Newer Older
J
Jaegeuk Kim 已提交
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * fs/f2fs/namei.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/fs.h>
#include <linux/f2fs_fs.h>
#include <linux/pagemap.h>
#include <linux/sched.h>
#include <linux/ctype.h>
C
Chao Yu 已提交
16
#include <linux/dcache.h>
17
#include <linux/namei.h>
18 19

#include "f2fs.h"
20
#include "node.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;
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);
38
	if (!alloc_nid(sbi, &ino)) {
39
		f2fs_unlock_op(sbi);
40 41 42
		err = -ENOSPC;
		goto fail;
	}
43
	f2fs_unlock_op(sbi);
44

45
	inode_init_owner(inode, dir, mode);
46 47 48 49 50 51 52 53 54 55

	inode->i_ino = ino;
	inode->i_blocks = 0;
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
	inode->i_generation = sbi->s_next_generation++;

	err = insert_inode_locked(inode);
	if (err) {
		err = -EINVAL;
		nid_free = true;
56
		goto fail;
57
	}
C
Chao Yu 已提交
58

59 60 61 62
	/* If the directory encrypted, then we should encrypt the inode. */
	if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
		f2fs_set_encrypted_inode(inode);

63
	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
64
		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
65
	if (f2fs_may_inline_dentry(inode))
C
Chao Yu 已提交
66 67
		set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);

J
Jaegeuk Kim 已提交
68 69
	f2fs_init_extent_tree(inode, NULL);

C
Chao Yu 已提交
70
	stat_inc_inline_xattr(inode);
71 72 73
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

74
	trace_f2fs_new_inode(inode, 0);
75 76 77 78
	mark_inode_dirty(inode);
	return inode;

fail:
79
	trace_f2fs_new_inode(inode, err);
80
	make_bad_inode(inode);
81
	if (nid_free)
82 83
		set_inode_flag(F2FS_I(inode), FI_FREE_NID);
	iput(inode);
84 85 86 87 88
	return ERR_PTR(err);
}

static int is_multimedia_file(const unsigned char *s, const char *sub)
{
89 90
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
91

C
Chao Yu 已提交
92 93 94 95 96 97 98 99
	/*
	 * filename format of multimedia file should be defined as:
	 * "filename + '.' + extension".
	 */
	if (slen < sublen + 2)
		return 0;

	if (s[slen - sublen - 1] != '.')
100
		return 0;
101

102
	return !strncasecmp(s + slen - sublen, sub, sublen);
103 104
}

J
Jaegeuk Kim 已提交
105
/*
106 107
 * Set multimedia files as cold files for hot/cold data separation
 */
108
static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
109 110 111 112 113 114 115
		const unsigned char *name)
{
	int i;
	__u8 (*extlist)[8] = sbi->raw_super->extension_list;

	int count = le32_to_cpu(sbi->raw_super->extension_count);
	for (i = 0; i < count; i++) {
116
		if (is_multimedia_file(name, extlist[i])) {
117
			file_set_cold(inode);
118 119 120 121 122 123 124 125
			break;
		}
	}
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
126
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
127 128
	struct inode *inode;
	nid_t ino = 0;
129
	int err;
130 131 132 133 134 135

	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
136
		set_cold_files(sbi, inode, dentry->d_name.name);
137 138 139 140 141 142

	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;

J
Jaegeuk Kim 已提交
143
	f2fs_balance_fs(sbi, true);
144

145
	f2fs_lock_op(sbi);
146 147 148
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
149
	f2fs_unlock_op(sbi);
150 151 152

	alloc_nid_done(sbi, ino);

153
	d_instantiate(dentry, inode);
154
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
155 156 157

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
158 159
	return 0;
out:
160
	handle_failed_inode(inode);
161 162 163 164 165 166
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
167
	struct inode *inode = d_inode(old_dentry);
168
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
169
	int err;
170

171 172 173 174
	if (f2fs_encrypted_inode(dir) &&
		!f2fs_is_child_context_consistent_with_parent(dir, inode))
		return -EPERM;

J
Jaegeuk Kim 已提交
175
	f2fs_balance_fs(sbi, true);
176

177
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
178
	ihold(inode);
179 180

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
181
	f2fs_lock_op(sbi);
182 183 184
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
185
	f2fs_unlock_op(sbi);
186 187

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
188 189 190

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
191 192 193 194
	return 0;
out:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
	iput(inode);
195
	f2fs_unlock_op(sbi);
196 197 198 199 200 201
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
202
	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
203 204
	if (!ino)
		return ERR_PTR(-ENOENT);
205
	return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
206 207
}

208 209 210 211 212 213 214 215 216
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;

217 218 219 220 221 222 223
	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;
	}

J
Jaegeuk Kim 已提交
224
	f2fs_balance_fs(sbi, true);
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	f2fs_lock_op(sbi);

	de = f2fs_find_entry(dir, &dot, &page);
	if (de) {
		f2fs_dentry_kunmap(dir, page);
		f2fs_put_page(page, 0);
	} else {
		err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
		if (err)
			goto out;
	}

	de = f2fs_find_entry(dir, &dotdot, &page);
	if (de) {
		f2fs_dentry_kunmap(dir, page);
		f2fs_put_page(page, 0);
	} else {
		err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
	}
out:
	if (!err) {
		clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
		mark_inode_dirty(dir);
	}

	f2fs_unlock_op(sbi);
	return err;
}

255 256 257 258 259 260
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;
J
Jaegeuk Kim 已提交
261
	nid_t ino;
262
	int err = 0;
263
	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
264

265
	if (dentry->d_name.len > F2FS_NAME_LEN)
266 267 268
		return ERR_PTR(-ENAMETOOLONG);

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
J
Jaegeuk Kim 已提交
269 270
	if (!de)
		return d_splice_alias(inode, dentry);
271

J
Jaegeuk Kim 已提交
272 273 274
	ino = le32_to_cpu(de->ino);
	f2fs_dentry_kunmap(dir, page);
	f2fs_put_page(page, 0);
275

J
Jaegeuk Kim 已提交
276 277 278
	inode = f2fs_iget(dir->i_sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
279

280 281 282 283 284 285
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
			goto err_out;
	}

286
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
287
		err = __recover_dot_dentries(inode, dir->i_ino);
288 289
		if (err)
			goto err_out;
290 291
	}
	return d_splice_alias(inode, dentry);
292 293 294 295

err_out:
	iget_failed(inode);
	return ERR_PTR(err);
296 297 298 299
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
300
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
301
	struct inode *inode = d_inode(dentry);
302 303 304 305
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

306
	trace_f2fs_unlink_enter(dir, dentry);
307

308 309 310 311
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (!de)
		goto fail;

J
Jaegeuk Kim 已提交
312
	f2fs_balance_fs(sbi, true);
313

314
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
315
	err = acquire_orphan_inode(sbi);
316
	if (err) {
317
		f2fs_unlock_op(sbi);
318
		f2fs_dentry_kunmap(dir, page);
319 320 321
		f2fs_put_page(page, 0);
		goto fail;
	}
322
	f2fs_delete_entry(de, page, dir, inode);
323
	f2fs_unlock_op(sbi);
324

A
arter97 已提交
325
	/* In order to evict this inode, we set it dirty */
326
	mark_inode_dirty(inode);
J
Jaegeuk Kim 已提交
327 328 329

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
330
fail:
331
	trace_f2fs_unlink_exit(inode, err);
332 333 334
	return err;
}

335
static const char *f2fs_get_link(struct dentry *dentry,
336 337
				 struct inode *inode,
				 struct delayed_call *done)
338
{
339
	const char *link = page_get_link(dentry, inode, done);
340 341
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
342 343
		do_delayed_call(done);
		clear_delayed_call(done);
344
		link = ERR_PTR(-ENOENT);
345
	}
346
	return link;
347 348
}

349 350 351
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
352
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
353
	struct inode *inode;
354
	size_t len = strlen(symname);
355
	struct f2fs_str disk_link = FSTR_INIT((char *)symname, len + 1);
356
	struct f2fs_encrypted_symlink_data *sd = NULL;
357
	int err;
358

359 360 361 362 363 364 365 366 367 368 369 370 371
	if (f2fs_encrypted_inode(dir)) {
		err = f2fs_get_encryption_info(dir);
		if (err)
			return err;

		if (!f2fs_encrypted_inode(dir))
			return -EPERM;

		disk_link.len = (f2fs_fname_encrypted_size(dir, len) +
				sizeof(struct f2fs_encrypted_symlink_data));
	}

	if (disk_link.len > dir->i_sb->s_blocksize)
372 373
		return -ENAMETOOLONG;

374 375 376 377
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

378 379 380 381
	if (f2fs_encrypted_inode(inode))
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
382
	inode_nohighmem(inode);
383 384
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

J
Jaegeuk Kim 已提交
385
	f2fs_balance_fs(sbi, true);
386

387
	f2fs_lock_op(sbi);
388 389 390
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
391
	f2fs_unlock_op(sbi);
392 393
	alloc_nid_done(sbi, inode->i_ino);

394
	if (f2fs_encrypted_inode(inode)) {
395
		struct qstr istr = QSTR_INIT(symname, len);
396
		struct f2fs_str ostr;
397

398 399 400
		sd = kzalloc(disk_link.len, GFP_NOFS);
		if (!sd) {
			err = -ENOMEM;
401
			goto err_out;
402
		}
403

404
		err = f2fs_get_encryption_info(inode);
405 406 407
		if (err)
			goto err_out;

408 409
		if (!f2fs_encrypted_inode(inode)) {
			err = -EPERM;
410 411 412
			goto err_out;
		}

413 414 415 416
		ostr.name = sd->encrypted_path;
		ostr.len = disk_link.len;
		err = f2fs_fname_usr_to_disk(inode, &istr, &ostr);
		if (err < 0)
417
			goto err_out;
418 419 420

		sd->len = cpu_to_le16(ostr.len);
		disk_link.name = (char *)sd;
421 422
	}

423
	err = page_symlink(inode, disk_link.name, disk_link.len);
424 425

err_out:
426 427
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
428

429 430 431 432 433 434 435 436 437
	/*
	 * 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 已提交
438
	if (!err) {
439 440
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
441

C
Chao Yu 已提交
442 443 444 445 446
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
447 448

	kfree(sd);
449 450
	return err;
out:
451
	handle_failed_inode(inode);
452 453 454 455 456
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
457
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
458
	struct inode *inode;
459
	int err;
460 461 462

	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
463
		return PTR_ERR(inode);
464 465 466 467

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
468
	mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
469

J
Jaegeuk Kim 已提交
470
	f2fs_balance_fs(sbi, true);
471

472
	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
473
	f2fs_lock_op(sbi);
474 475 476
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
477
	f2fs_unlock_op(sbi);
478 479 480 481 482 483

	alloc_nid_done(sbi, inode->i_ino);

	d_instantiate(dentry, inode);
	unlock_new_inode(inode);

J
Jaegeuk Kim 已提交
484 485
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
486 487 488 489
	return 0;

out_fail:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
490
	handle_failed_inode(inode);
491 492 493 494 495
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
496
	struct inode *inode = d_inode(dentry);
497 498 499 500 501 502 503 504
	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)
{
505
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
506 507 508 509 510 511 512 513 514 515
	struct inode *inode;
	int err = 0;

	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;

J
Jaegeuk Kim 已提交
516
	f2fs_balance_fs(sbi, true);
517

518
	f2fs_lock_op(sbi);
519 520 521
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
522
	f2fs_unlock_op(sbi);
523 524

	alloc_nid_done(sbi, inode->i_ino);
J
Jaegeuk Kim 已提交
525

526 527
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
528 529 530

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
531 532
	return 0;
out:
533
	handle_failed_inode(inode);
534 535 536
	return err;
}

C
Chao Yu 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
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;

	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;
	}

J
Jaegeuk Kim 已提交
557
	f2fs_balance_fs(sbi, true);
558

C
Chao Yu 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	f2fs_lock_op(sbi);
	err = acquire_orphan_inode(sbi);
	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.
	 */
	add_orphan_inode(sbi, inode->i_ino);
	f2fs_unlock_op(sbi);

	alloc_nid_done(sbi, inode->i_ino);

	if (whiteout) {
		inode_dec_link_count(inode);
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
	unlock_new_inode(inode);
	return 0;

release_out:
	release_orphan_inode(sbi);
out:
	handle_failed_inode(inode);
	return err;
}

static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
595 596 597 598 599 600
	if (f2fs_encrypted_inode(dir)) {
		int err = f2fs_get_encryption_info(dir);
		if (err)
			return err;
	}

C
Chao Yu 已提交
601 602 603 604 605 606 607 608
	return __f2fs_tmpfile(dir, dentry, mode, NULL);
}

static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
{
	return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
}

609
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
610 611
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
612
{
613
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
614 615
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
616
	struct inode *whiteout = NULL;
617
	struct page *old_dir_page;
C
Chao Yu 已提交
618
	struct page *old_page, *new_page = NULL;
619 620 621
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
622
	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
623
	int err = -ENOENT;
624

625 626 627 628 629 630 631
	if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
		!f2fs_is_child_context_consistent_with_parent(new_dir,
							old_inode)) {
		err = -EPERM;
		goto out;
	}

632 633 634 635 636 637 638 639 640 641 642
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
	if (!old_entry)
		goto out;

	if (S_ISDIR(old_inode->i_mode)) {
		err = -EIO;
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
		if (!old_dir_entry)
			goto out_old;
	}

C
Chao Yu 已提交
643 644 645 646 647 648
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

649 650 651 652
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
653
			goto out_whiteout;
654 655 656 657 658

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
		if (!new_entry)
C
Chao Yu 已提交
659
			goto out_whiteout;
660

J
Jaegeuk Kim 已提交
661
		f2fs_balance_fs(sbi, true);
662

663 664
		f2fs_lock_op(sbi);

J
Jaegeuk Kim 已提交
665 666 667 668
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

669 670 671
		err = update_dent_inode(old_inode, new_inode,
						&new_dentry->d_name);
		if (err) {
J
Jaegeuk Kim 已提交
672 673
			release_orphan_inode(sbi);
			goto put_out_dir;
674 675
		}

676 677 678
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
679
		down_write(&F2FS_I(new_inode)->i_sem);
680 681 682
		if (old_dir_entry)
			drop_nlink(new_inode);
		drop_nlink(new_inode);
683 684
		up_write(&F2FS_I(new_inode)->i_sem);

685
		mark_inode_dirty(new_inode);
J
Jaegeuk Kim 已提交
686

687 688
		if (!new_inode->i_nlink)
			add_orphan_inode(sbi, new_inode->i_ino);
J
Jaegeuk Kim 已提交
689 690 691
		else
			release_orphan_inode(sbi);

692
		update_inode_page(old_inode);
693
		update_inode_page(new_inode);
694
	} else {
J
Jaegeuk Kim 已提交
695
		f2fs_balance_fs(sbi, true);
696

697 698
		f2fs_lock_op(sbi);

699
		err = f2fs_add_link(new_dentry, old_inode);
700 701
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
702
			goto out_whiteout;
703
		}
704 705 706

		if (old_dir_entry) {
			inc_nlink(new_dir);
707
			update_inode_page(new_dir);
708
		}
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

		/*
		 * 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) {
				err = -EIO;
				f2fs_unlock_op(sbi);
				goto out_whiteout;
			}
		}
729 730
	}

731 732
	down_write(&F2FS_I(old_inode)->i_sem);
	file_lost_pino(old_inode);
733 734
	if (new_inode && file_enc_name(new_inode))
		file_set_enc_name(old_inode);
735 736
	up_write(&F2FS_I(old_inode)->i_sem);

737 738 739
	old_inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(old_inode);

740
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
741

C
Chao Yu 已提交
742 743 744 745 746 747 748 749 750 751
	if (whiteout) {
		whiteout->i_state |= I_LINKABLE;
		set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

752
	if (old_dir_entry) {
C
Chao Yu 已提交
753
		if (old_dir != new_dir && !whiteout) {
754 755
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
756
			update_inode_page(old_inode);
757
		} else {
758
			f2fs_dentry_kunmap(old_inode, old_dir_page);
759 760 761
			f2fs_put_page(old_dir_page, 0);
		}
		drop_nlink(old_dir);
762
		mark_inode_dirty(old_dir);
763
		update_inode_page(old_dir);
764 765
	}

766
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
767 768 769

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

J
Jaegeuk Kim 已提交
772
put_out_dir:
773
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
774 775 776 777 778 779 780
	if (new_page) {
		f2fs_dentry_kunmap(new_dir, new_page);
		f2fs_put_page(new_page, 0);
	}
out_whiteout:
	if (whiteout)
		iput(whiteout);
781 782
out_dir:
	if (old_dir_entry) {
783
		f2fs_dentry_kunmap(old_inode, old_dir_page);
784 785 786
		f2fs_put_page(old_dir_page, 0);
	}
out_old:
787
	f2fs_dentry_kunmap(old_dir, old_page);
788 789 790 791 792
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
793 794 795
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
796
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
797 798
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
799 800 801 802 803 804 805
	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;
	int err = -ENOENT;

806 807 808 809 810 811 812 813
	if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
		(old_dir != new_dir) &&
		(!f2fs_is_child_context_consistent_with_parent(new_dir,
								old_inode) ||
		!f2fs_is_child_context_consistent_with_parent(old_dir,
								new_inode)))
		return -EPERM;

C
Chao Yu 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
	if (!old_entry)
		goto out;

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
	if (!new_entry)
		goto out_old;

	/* prepare for updating ".." directory entry info later */
	if (old_dir != new_dir) {
		if (S_ISDIR(old_inode->i_mode)) {
			err = -EIO;
			old_dir_entry = f2fs_parent_dir(old_inode,
							&old_dir_page);
			if (!old_dir_entry)
				goto out_new;
		}

		if (S_ISDIR(new_inode->i_mode)) {
			err = -EIO;
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
			if (!new_dir_entry)
				goto out_old_dir;
		}
	}

	/*
	 * 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;
		if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
			(new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
			goto out_new_dir;
	}

J
Jaegeuk Kim 已提交
856
	f2fs_balance_fs(sbi, true);
857

C
Chao Yu 已提交
858 859
	f2fs_lock_op(sbi);

860
	err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
C
Chao Yu 已提交
861 862
	if (err)
		goto out_unlock;
863 864
	if (file_enc_name(new_inode))
		file_set_enc_name(old_inode);
C
Chao Yu 已提交
865

866
	err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
C
Chao Yu 已提交
867 868
	if (err)
		goto out_undo;
869 870
	if (file_enc_name(old_inode))
		file_set_enc_name(new_inode);
C
Chao Yu 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

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

	update_inode_page(old_inode);

	old_dir->i_ctime = CURRENT_TIME;
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
		if (old_nlink < 0)
			drop_nlink(old_dir);
		else
			inc_nlink(old_dir);
		up_write(&F2FS_I(old_dir)->i_sem);
	}
	mark_inode_dirty(old_dir);
	update_inode_page(old_dir);

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

	update_inode_page(new_inode);

	new_dir->i_ctime = CURRENT_TIME;
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
		if (new_nlink < 0)
			drop_nlink(new_dir);
		else
			inc_nlink(new_dir);
		up_write(&F2FS_I(new_dir)->i_sem);
	}
	mark_inode_dirty(new_dir);
	update_inode_page(new_dir);

	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
923 924 925

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
926 927
	return 0;
out_undo:
928 929 930 931 932
	/*
	 * Still we may fail to recover name info of f2fs_inode here
	 * Drop it, once its name is set as encrypted
	 */
	update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
C
Chao Yu 已提交
933 934 935 936
out_unlock:
	f2fs_unlock_op(sbi);
out_new_dir:
	if (new_dir_entry) {
937
		f2fs_dentry_kunmap(new_inode, new_dir_page);
C
Chao Yu 已提交
938 939 940 941
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
942
		f2fs_dentry_kunmap(old_inode, old_dir_page);
C
Chao Yu 已提交
943 944 945
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
946
	f2fs_dentry_kunmap(new_dir, new_page);
C
Chao Yu 已提交
947 948
	f2fs_put_page(new_page, 0);
out_old:
949
	f2fs_dentry_kunmap(old_dir, old_page);
C
Chao Yu 已提交
950 951 952 953 954 955 956 957 958
	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)
{
C
Chao Yu 已提交
959
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
960 961 962 963 964 965 966 967 968 969
		return -EINVAL;

	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 已提交
970
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
971 972
}

973
#ifdef CONFIG_F2FS_FS_ENCRYPTION
974
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
975 976
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
977
{
978 979
	struct page *cpage = NULL;
	char *caddr, *paddr = NULL;
980
	struct f2fs_str cstr = FSTR_INIT(NULL, 0);
981 982 983 984 985 986
	struct f2fs_str pstr = FSTR_INIT(NULL, 0);
	struct f2fs_encrypted_symlink_data *sd;
	loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
	u32 max_size = inode->i_sb->s_blocksize;
	int res;

987 988 989
	if (!dentry)
		return ERR_PTR(-ECHILD);

990
	res = f2fs_get_encryption_info(inode);
991 992 993 994 995
	if (res)
		return ERR_PTR(res);

	cpage = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(cpage))
996
		return ERR_CAST(cpage);
997
	caddr = page_address(cpage);
998 999 1000 1001
	caddr[size] = 0;

	/* Symlink is encrypted */
	sd = (struct f2fs_encrypted_symlink_data *)caddr;
1002
	cstr.name = sd->encrypted_path;
1003
	cstr.len = le16_to_cpu(sd->len);
1004 1005 1006 1007 1008 1009

	/* this is broken symlink case */
	if (unlikely(cstr.len == 0)) {
		res = -ENOENT;
		goto errout;
	}
1010 1011

	/* this is broken symlink case */
1012
	if (unlikely(cstr.name[0] == 0)) {
1013 1014 1015
		res = -ENOENT;
		goto errout;
	}
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
								max_size) {
		/* Symlink data on the disk is corrupted */
		res = -EIO;
		goto errout;
	}
	res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
	if (res)
		goto errout;

	res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
	if (res < 0)
		goto errout;

	paddr = pstr.name;

	/* Null-terminate the name */
	paddr[res] = '\0';

	page_cache_release(cpage);
1037 1038
	set_delayed_call(done, kfree_link, paddr);
	return paddr;
1039 1040 1041 1042
errout:
	f2fs_fname_crypto_free_buffer(&pstr);
	page_cache_release(cpage);
	return ERR_PTR(res);
C
Chao Yu 已提交
1043 1044
}

1045 1046
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
	.readlink       = generic_readlink,
1047
	.get_link       = f2fs_encrypted_get_link,
1048 1049
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1050
#ifdef CONFIG_F2FS_FS_XATTR
1051 1052 1053 1054
	.setxattr	= generic_setxattr,
	.getxattr	= generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr	= generic_removexattr,
1055
#endif
1056 1057 1058
};
#endif

1059 1060 1061 1062 1063 1064 1065 1066 1067
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,
C
Chao Yu 已提交
1068
	.rename2	= f2fs_rename2,
C
Chao Yu 已提交
1069
	.tmpfile	= f2fs_tmpfile,
1070
	.getattr	= f2fs_getattr,
1071 1072
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1073
	.set_acl	= f2fs_set_acl,
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr	= generic_setxattr,
	.getxattr	= generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr	= generic_removexattr,
#endif
};

const struct inode_operations f2fs_symlink_inode_operations = {
	.readlink       = generic_readlink,
1084
	.get_link       = f2fs_get_link,
1085
	.getattr	= f2fs_getattr,
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	.setattr	= f2fs_setattr,
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr	= generic_setxattr,
	.getxattr	= generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr	= generic_removexattr,
#endif
};

const struct inode_operations f2fs_special_inode_operations = {
1096
	.getattr	= f2fs_getattr,
1097 1098
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1099
	.set_acl	= f2fs_set_acl,
1100 1101 1102 1103 1104 1105 1106
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};