namei.c 24.0 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 56 57

	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;
		goto out;
	}
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 (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);

70 71 72
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

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

out:
	clear_nlink(inode);
	unlock_new_inode(inode);
fail:
81
	trace_f2fs_new_inode(inode, err);
82
	make_bad_inode(inode);
83
	if (nid_free)
84 85
		set_inode_flag(F2FS_I(inode), FI_FREE_NID);
	iput(inode);
86 87 88 89 90
	return ERR_PTR(err);
}

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

	if (sublen > slen)
95
		return 0;
96

97
	return !strncasecmp(s + slen - sublen, sub, sublen);
98 99
}

J
Jaegeuk Kim 已提交
100
/*
101 102
 * Set multimedia files as cold files for hot/cold data separation
 */
103
static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
104 105 106 107 108 109 110
		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++) {
111
		if (is_multimedia_file(name, extlist[i])) {
112
			file_set_cold(inode);
113 114 115 116 117 118 119 120
			break;
		}
	}
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
121
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
122 123
	struct inode *inode;
	nid_t ino = 0;
124
	int err;
125

126 127
	f2fs_balance_fs(sbi);

128 129 130 131 132
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
133
		set_cold_files(sbi, inode, dentry->d_name.name);
134 135 136 137 138 139

	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;

140
	f2fs_lock_op(sbi);
141 142 143
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
144
	f2fs_unlock_op(sbi);
145 146 147

	alloc_nid_done(sbi, ino);

148
	d_instantiate(dentry, inode);
149
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
150 151 152

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
153 154
	return 0;
out:
155
	handle_failed_inode(inode);
156 157 158 159 160 161
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
162
	struct inode *inode = d_inode(old_dentry);
163
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
164
	int err;
165

166 167 168 169
	if (f2fs_encrypted_inode(dir) &&
		!f2fs_is_child_context_consistent_with_parent(dir, inode))
		return -EPERM;

170 171
	f2fs_balance_fs(sbi);

172
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
173
	ihold(inode);
174 175

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
176
	f2fs_lock_op(sbi);
177 178 179
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
180
	f2fs_unlock_op(sbi);
181 182

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
183 184 185

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
186 187 188 189
	return 0;
out:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
	iput(inode);
190
	f2fs_unlock_op(sbi);
191 192 193 194 195 196
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
197
	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
198 199
	if (!ino)
		return ERR_PTR(-ENOENT);
200
	return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
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;

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

241 242 243 244 245 246
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 已提交
247
	nid_t ino;
248
	int err = 0;
249

250
	if (dentry->d_name.len > F2FS_NAME_LEN)
251 252 253
		return ERR_PTR(-ENAMETOOLONG);

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

J
Jaegeuk Kim 已提交
257 258 259
	ino = le32_to_cpu(de->ino);
	f2fs_dentry_kunmap(dir, page);
	f2fs_put_page(page, 0);
260

J
Jaegeuk Kim 已提交
261 262 263
	inode = f2fs_iget(dir->i_sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
264

265
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
266
		err = __recover_dot_dentries(inode, dir->i_ino);
267 268
		if (err)
			goto err_out;
269 270
	}
	return d_splice_alias(inode, dentry);
271 272 273 274

err_out:
	iget_failed(inode);
	return ERR_PTR(err);
275 276 277 278
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
279
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
280
	struct inode *inode = d_inode(dentry);
281 282 283 284
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

285
	trace_f2fs_unlink_enter(dir, dentry);
286 287
	f2fs_balance_fs(sbi);

288 289 290 291
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (!de)
		goto fail;

292
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
293
	err = acquire_orphan_inode(sbi);
294
	if (err) {
295
		f2fs_unlock_op(sbi);
296
		f2fs_dentry_kunmap(dir, page);
297 298 299
		f2fs_put_page(page, 0);
		goto fail;
	}
300
	f2fs_delete_entry(de, page, dir, inode);
301
	f2fs_unlock_op(sbi);
302

A
arter97 已提交
303
	/* In order to evict this inode, we set it dirty */
304
	mark_inode_dirty(inode);
J
Jaegeuk Kim 已提交
305 306 307

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
308
fail:
309
	trace_f2fs_unlink_exit(inode, err);
310 311 312
	return err;
}

313
static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
314
{
315
	const char *link = page_follow_link_light(dentry, cookie);
316 317
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
318
		page_put_link(NULL, *cookie);
319
		link = ERR_PTR(-ENOENT);
320
	}
321
	return link;
322 323
}

324 325 326
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
327
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
328
	struct inode *inode;
329 330 331 332 333
	size_t len = strlen(symname);
	size_t p_len;
	char *p_str;
	struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
	struct f2fs_encrypted_symlink_data *sd = NULL;
334
	int err;
335

336 337 338
	if (len > dir->i_sb->s_blocksize)
		return -ENAMETOOLONG;

339 340
	f2fs_balance_fs(sbi);

341 342 343 344
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

345 346 347 348
	if (f2fs_encrypted_inode(inode))
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
349 350
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

351
	f2fs_lock_op(sbi);
352 353 354
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
355
	f2fs_unlock_op(sbi);
356 357
	alloc_nid_done(sbi, inode->i_ino);

358 359 360
	if (f2fs_encrypted_inode(dir)) {
		struct qstr istr = QSTR_INIT(symname, len);

361
		err = f2fs_get_encryption_info(inode);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		if (err)
			goto err_out;

		err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
		if (err)
			goto err_out;

		err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
		if (err < 0)
			goto err_out;

		p_len = encrypted_symlink_data_len(disk_link.len) + 1;

		if (p_len > dir->i_sb->s_blocksize) {
			err = -ENAMETOOLONG;
			goto err_out;
		}

		sd = kzalloc(p_len, GFP_NOFS);
		if (!sd) {
			err = -ENOMEM;
			goto err_out;
		}
		memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
		sd->len = cpu_to_le16(disk_link.len);
		p_str = (char *)sd;
	} else {
		p_len = len + 1;
		p_str = (char *)symname;
	}

	err = page_symlink(inode, p_str, p_len);

err_out:
396 397
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
398

399 400 401 402 403 404 405 406 407
	/*
	 * 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.
	 */
408 409
	if (!err)
		filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
410

J
Jaegeuk Kim 已提交
411 412
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
413 414 415

	kfree(sd);
	f2fs_fname_crypto_free_buffer(&disk_link);
416 417
	return err;
out:
418
	handle_failed_inode(inode);
419 420 421 422 423
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
424
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
425
	struct inode *inode;
426
	int err;
427

428 429
	f2fs_balance_fs(sbi);

430 431
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
432
		return PTR_ERR(inode);
433 434 435 436

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
437
	mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
438 439

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
440
	f2fs_lock_op(sbi);
441 442 443
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
444
	f2fs_unlock_op(sbi);
445 446 447 448 449 450

	alloc_nid_done(sbi, inode->i_ino);

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

J
Jaegeuk Kim 已提交
451 452
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
453 454 455 456
	return 0;

out_fail:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
457
	handle_failed_inode(inode);
458 459 460 461 462
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
463
	struct inode *inode = d_inode(dentry);
464 465 466 467 468 469 470 471
	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)
{
472
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
473 474 475 476 477 478
	struct inode *inode;
	int err = 0;

	if (!new_valid_dev(rdev))
		return -EINVAL;

479 480
	f2fs_balance_fs(sbi);

481 482 483 484 485 486 487
	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;

488
	f2fs_lock_op(sbi);
489 490 491
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
492
	f2fs_unlock_op(sbi);
493 494

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

496 497
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
498 499 500

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
501 502
	return 0;
out:
503
	handle_failed_inode(inode);
504 505 506
	return err;
}

C
Chao Yu 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
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;

	if (!whiteout)
		f2fs_balance_fs(sbi);

	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);
	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)
{
566 567 568 569 570 571
	if (f2fs_encrypted_inode(dir)) {
		int err = f2fs_get_encryption_info(dir);
		if (err)
			return err;
	}

C
Chao Yu 已提交
572 573 574 575 576 577 578 579
	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);
}

580
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
581 582
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
583
{
584
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
585 586
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
587
	struct inode *whiteout = NULL;
588
	struct page *old_dir_page;
C
Chao Yu 已提交
589
	struct page *old_page, *new_page = NULL;
590 591 592
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
593
	int err = -ENOENT;
594

595 596 597 598 599 600 601
	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;
	}

602 603
	f2fs_balance_fs(sbi);

604 605 606 607 608 609 610 611 612 613 614
	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 已提交
615 616 617 618 619 620
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

621 622 623 624
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
625
			goto out_whiteout;
626 627 628 629 630

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

633 634
		f2fs_lock_op(sbi);

J
Jaegeuk Kim 已提交
635 636 637 638
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

639 640
		if (update_dent_inode(old_inode, new_inode,
						&new_dentry->d_name)) {
J
Jaegeuk Kim 已提交
641 642
			release_orphan_inode(sbi);
			goto put_out_dir;
643 644
		}

645 646 647
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
648
		down_write(&F2FS_I(new_inode)->i_sem);
649 650 651
		if (old_dir_entry)
			drop_nlink(new_inode);
		drop_nlink(new_inode);
652 653
		up_write(&F2FS_I(new_inode)->i_sem);

654
		mark_inode_dirty(new_inode);
J
Jaegeuk Kim 已提交
655

656 657
		if (!new_inode->i_nlink)
			add_orphan_inode(sbi, new_inode->i_ino);
J
Jaegeuk Kim 已提交
658 659 660
		else
			release_orphan_inode(sbi);

661
		update_inode_page(old_inode);
662
		update_inode_page(new_inode);
663
	} else {
664 665
		f2fs_lock_op(sbi);

666
		err = f2fs_add_link(new_dentry, old_inode);
667 668
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
669
			goto out_whiteout;
670
		}
671 672 673

		if (old_dir_entry) {
			inc_nlink(new_dir);
674
			update_inode_page(new_dir);
675 676 677
		}
	}

678 679
	down_write(&F2FS_I(old_inode)->i_sem);
	file_lost_pino(old_inode);
680 681
	if (new_inode && file_enc_name(new_inode))
		file_set_enc_name(old_inode);
682 683
	up_write(&F2FS_I(old_inode)->i_sem);

684 685 686
	old_inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(old_inode);

687
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
688

C
Chao Yu 已提交
689 690 691 692 693 694 695 696 697 698
	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);
	}

699
	if (old_dir_entry) {
C
Chao Yu 已提交
700
		if (old_dir != new_dir && !whiteout) {
701 702
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
703
			update_inode_page(old_inode);
704
		} else {
705
			f2fs_dentry_kunmap(old_inode, old_dir_page);
706 707 708
			f2fs_put_page(old_dir_page, 0);
		}
		drop_nlink(old_dir);
709
		mark_inode_dirty(old_dir);
710
		update_inode_page(old_dir);
711 712
	}

713
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
714 715 716

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

J
Jaegeuk Kim 已提交
719
put_out_dir:
720
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
721 722 723 724 725 726 727
	if (new_page) {
		f2fs_dentry_kunmap(new_dir, new_page);
		f2fs_put_page(new_page, 0);
	}
out_whiteout:
	if (whiteout)
		iput(whiteout);
728 729
out_dir:
	if (old_dir_entry) {
730
		f2fs_dentry_kunmap(old_inode, old_dir_page);
731 732 733
		f2fs_put_page(old_dir_page, 0);
	}
out_old:
734
	f2fs_dentry_kunmap(old_dir, old_page);
735 736 737 738 739
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
740 741 742
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
743
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
744 745
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
746 747 748 749 750 751 752
	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;

753 754 755 756 757 758 759 760
	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 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	f2fs_balance_fs(sbi);

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

	f2fs_lock_op(sbi);

807
	err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
C
Chao Yu 已提交
808 809
	if (err)
		goto out_unlock;
810 811
	if (file_enc_name(new_inode))
		file_set_enc_name(old_inode);
C
Chao Yu 已提交
812

813
	err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
C
Chao Yu 已提交
814 815
	if (err)
		goto out_undo;
816 817
	if (file_enc_name(old_inode))
		file_set_enc_name(new_inode);
C
Chao Yu 已提交
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 856 857 858 859 860 861 862 863 864 865 866 867 868 869

	/* 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 已提交
870 871 872

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
873 874
	return 0;
out_undo:
875 876 877 878 879
	/*
	 * 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 已提交
880 881 882 883
out_unlock:
	f2fs_unlock_op(sbi);
out_new_dir:
	if (new_dir_entry) {
884
		f2fs_dentry_kunmap(new_inode, new_dir_page);
C
Chao Yu 已提交
885 886 887 888
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
889
		f2fs_dentry_kunmap(old_inode, old_dir_page);
C
Chao Yu 已提交
890 891 892
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
893
	f2fs_dentry_kunmap(new_dir, new_page);
C
Chao Yu 已提交
894 895
	f2fs_put_page(new_page, 0);
out_old:
896
	f2fs_dentry_kunmap(old_dir, old_page);
C
Chao Yu 已提交
897 898 899 900 901 902 903 904 905
	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 已提交
906
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
907 908 909 910 911 912 913 914 915 916
		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 已提交
917
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
918 919
}

920
#ifdef CONFIG_F2FS_FS_ENCRYPTION
921
static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
C
Chao Yu 已提交
922
{
923 924 925 926 927 928 929 930 931 932
	struct page *cpage = NULL;
	char *caddr, *paddr = NULL;
	struct f2fs_str cstr;
	struct f2fs_str pstr = FSTR_INIT(NULL, 0);
	struct inode *inode = d_inode(dentry);
	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;

933
	res = f2fs_get_encryption_info(inode);
934 935 936 937 938
	if (res)
		return ERR_PTR(res);

	cpage = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(cpage))
939
		return ERR_CAST(cpage);
940 941 942 943 944 945 946 947 948 949 950 951 952
	caddr = kmap(cpage);
	caddr[size] = 0;

	/* Symlink is encrypted */
	sd = (struct f2fs_encrypted_symlink_data *)caddr;
	cstr.name = sd->encrypted_path;
	cstr.len = le16_to_cpu(sd->len);

	/* this is broken symlink case */
	if (cstr.name[0] == 0 && cstr.len == 0) {
		res = -ENOENT;
		goto errout;
	}
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	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';

	kunmap(cpage);
	page_cache_release(cpage);
975
	return *cookie = paddr;
976 977 978 979 980
errout:
	f2fs_fname_crypto_free_buffer(&pstr);
	kunmap(cpage);
	page_cache_release(cpage);
	return ERR_PTR(res);
C
Chao Yu 已提交
981 982
}

983 984 985 986 987 988 989 990 991 992 993 994 995
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
	.readlink       = generic_readlink,
	.follow_link    = f2fs_encrypted_follow_link,
	.put_link       = kfree_put_link,
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
	.setxattr	= generic_setxattr,
	.getxattr	= generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr	= generic_removexattr,
};
#endif

996 997 998 999 1000 1001 1002 1003 1004
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 已提交
1005
	.rename2	= f2fs_rename2,
C
Chao Yu 已提交
1006
	.tmpfile	= f2fs_tmpfile,
1007
	.getattr	= f2fs_getattr,
1008 1009
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1010
	.set_acl	= f2fs_set_acl,
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
#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,
1021
	.follow_link    = f2fs_follow_link,
1022
	.put_link       = page_put_link,
1023
	.getattr	= f2fs_getattr,
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	.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 = {
1034
	.getattr	= f2fs_getattr,
1035 1036
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1037
	.set_acl	= f2fs_set_acl,
1038 1039 1040 1041 1042 1043 1044
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};