namei.c 18.9 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
	if (f2fs_may_inline(inode))
		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
C
Chao Yu 已提交
61 62 63
	if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode))
		set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);

64
	trace_f2fs_new_inode(inode, 0);
65 66 67 68 69 70 71
	mark_inode_dirty(inode);
	return inode;

out:
	clear_nlink(inode);
	unlock_new_inode(inode);
fail:
72
	trace_f2fs_new_inode(inode, err);
73
	make_bad_inode(inode);
74 75 76 77 78 79 80 81
	iput(inode);
	if (nid_free)
		alloc_nid_failed(sbi, ino);
	return ERR_PTR(err);
}

static int is_multimedia_file(const unsigned char *s, const char *sub)
{
82 83
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
84 85

	if (sublen > slen)
86
		return 0;
87

88
	return !strncasecmp(s + slen - sublen, sub, sublen);
89 90
}

J
Jaegeuk Kim 已提交
91
/*
92 93
 * Set multimedia files as cold files for hot/cold data separation
 */
94
static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
95 96 97 98 99 100 101
		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++) {
102
		if (is_multimedia_file(name, extlist[i])) {
103
			file_set_cold(inode);
104 105 106 107 108 109 110 111
			break;
		}
	}
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
112
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
113 114
	struct inode *inode;
	nid_t ino = 0;
115
	int err;
116

117 118
	f2fs_balance_fs(sbi);

119 120 121 122 123
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
124
		set_cold_files(sbi, inode, dentry->d_name.name);
125 126 127 128 129 130

	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;

131
	f2fs_lock_op(sbi);
132 133 134
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
135
	f2fs_unlock_op(sbi);
136 137 138

	alloc_nid_done(sbi, ino);

139
	stat_inc_inline_inode(inode);
140
	d_instantiate(dentry, inode);
141
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
142 143 144

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
145 146
	return 0;
out:
147
	handle_failed_inode(inode);
148 149 150 151 152 153
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
154
	struct inode *inode = d_inode(old_dentry);
155
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
156
	int err;
157

158 159
	f2fs_balance_fs(sbi);

160
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
161
	ihold(inode);
162 163

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
164
	f2fs_lock_op(sbi);
165 166 167
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
168
	f2fs_unlock_op(sbi);
169 170

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
171 172 173

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
174 175 176 177
	return 0;
out:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
	iput(inode);
178
	f2fs_unlock_op(sbi);
179 180 181 182 183 184
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
185
	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
186 187
	if (!ino)
		return ERR_PTR(-ENOENT);
188
	return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
189 190
}

191 192 193 194 195 196 197 198 199 200 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
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;
}

229 230 231 232 233 234
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 已提交
235
	nid_t ino;
236

237
	if (dentry->d_name.len > F2FS_NAME_LEN)
238 239 240
		return ERR_PTR(-ENAMETOOLONG);

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
J
Jaegeuk Kim 已提交
241 242 243 244 245 246
	if (!de)
		return d_splice_alias(inode, dentry);

	ino = le32_to_cpu(de->ino);
	f2fs_dentry_kunmap(dir, page);
	f2fs_put_page(page, 0);
247

J
Jaegeuk Kim 已提交
248 249 250
	inode = f2fs_iget(dir->i_sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
251

J
Jaegeuk Kim 已提交
252 253
	if (f2fs_has_inline_dots(inode)) {
		int err;
254

J
Jaegeuk Kim 已提交
255 256 257 258
		err = __recover_dot_dentries(inode, dir->i_ino);
		if (err) {
			iget_failed(inode);
			return ERR_PTR(err);
259
		}
260 261 262 263 264 265
	}
	return d_splice_alias(inode, dentry);
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
266
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
267
	struct inode *inode = d_inode(dentry);
268 269 270 271
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

272
	trace_f2fs_unlink_enter(dir, dentry);
273 274
	f2fs_balance_fs(sbi);

275 276 277 278
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (!de)
		goto fail;

279
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
280
	err = acquire_orphan_inode(sbi);
281
	if (err) {
282
		f2fs_unlock_op(sbi);
283
		f2fs_dentry_kunmap(dir, page);
284 285 286
		f2fs_put_page(page, 0);
		goto fail;
	}
287
	f2fs_delete_entry(de, page, dir, inode);
288
	f2fs_unlock_op(sbi);
289

A
arter97 已提交
290
	/* In order to evict this inode, we set it dirty */
291
	mark_inode_dirty(inode);
J
Jaegeuk Kim 已提交
292 293 294

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
295
fail:
296
	trace_f2fs_unlink_exit(inode, err);
297 298 299
	return err;
}

300 301
static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
302
	struct page *page = page_follow_link_light(dentry, nd);
303

304
	if (IS_ERR_OR_NULL(page))
305 306 307 308
		return page;

	/* this is broken symlink case */
	if (*nd_get_link(nd) == 0) {
309
		page_put_link(dentry, nd, page);
310 311 312 313 314
		return ERR_PTR(-ENOENT);
	}
	return page;
}

315 316 317
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
318
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
319
	struct inode *inode;
320
	size_t symlen = strlen(symname) + 1;
321
	int err;
322

323 324
	f2fs_balance_fs(sbi);

325 326 327 328 329 330 331
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	inode->i_op = &f2fs_symlink_inode_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

332
	f2fs_lock_op(sbi);
333 334 335
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
336
	f2fs_unlock_op(sbi);
337 338 339 340 341 342

	err = page_symlink(inode, symname, symlen);
	alloc_nid_done(sbi, inode->i_ino);

	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
343

344 345 346 347 348 349 350 351 352 353 354
	/*
	 * 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.
	 */
	filemap_write_and_wait_range(inode->i_mapping, 0, symlen - 1);

J
Jaegeuk Kim 已提交
355 356
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
357 358
	return err;
out:
359
	handle_failed_inode(inode);
360 361 362 363 364
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
365
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
366
	struct inode *inode;
367
	int err;
368

369 370
	f2fs_balance_fs(sbi);

371 372
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
373
		return PTR_ERR(inode);
374 375 376 377

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
378
	mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
379 380

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
381
	f2fs_lock_op(sbi);
382 383 384
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
385
	f2fs_unlock_op(sbi);
386

387
	stat_inc_inline_dir(inode);
388 389 390 391 392
	alloc_nid_done(sbi, inode->i_ino);

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

J
Jaegeuk Kim 已提交
393 394
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
395 396 397 398
	return 0;

out_fail:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
399
	handle_failed_inode(inode);
400 401 402 403 404
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
405
	struct inode *inode = d_inode(dentry);
406 407 408 409 410 411 412 413
	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)
{
414
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
415 416 417 418 419 420
	struct inode *inode;
	int err = 0;

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

421 422
	f2fs_balance_fs(sbi);

423 424 425 426 427 428 429
	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;

430
	f2fs_lock_op(sbi);
431 432 433
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
434
	f2fs_unlock_op(sbi);
435 436

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

438 439
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
440 441 442

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
443 444
	return 0;
out:
445
	handle_failed_inode(inode);
446 447 448 449 450 451
	return err;
}

static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
			struct inode *new_dir, struct dentry *new_dentry)
{
452
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
453 454
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
455
	struct page *old_dir_page;
J
Jaegeuk Kim 已提交
456
	struct page *old_page, *new_page;
457 458 459
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
460
	int err = -ENOENT;
461

462 463
	f2fs_balance_fs(sbi);

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	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;
	}

	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
			goto out_dir;

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

487 488
		f2fs_lock_op(sbi);

J
Jaegeuk Kim 已提交
489 490 491 492
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

493
		if (update_dent_inode(old_inode, &new_dentry->d_name)) {
J
Jaegeuk Kim 已提交
494 495
			release_orphan_inode(sbi);
			goto put_out_dir;
496 497
		}

498 499 500
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
501
		down_write(&F2FS_I(new_inode)->i_sem);
502 503 504
		if (old_dir_entry)
			drop_nlink(new_inode);
		drop_nlink(new_inode);
505 506
		up_write(&F2FS_I(new_inode)->i_sem);

507
		mark_inode_dirty(new_inode);
J
Jaegeuk Kim 已提交
508

509 510
		if (!new_inode->i_nlink)
			add_orphan_inode(sbi, new_inode->i_ino);
J
Jaegeuk Kim 已提交
511 512 513
		else
			release_orphan_inode(sbi);

514
		update_inode_page(old_inode);
515
		update_inode_page(new_inode);
516
	} else {
517 518
		f2fs_lock_op(sbi);

519
		err = f2fs_add_link(new_dentry, old_inode);
520 521
		if (err) {
			f2fs_unlock_op(sbi);
522
			goto out_dir;
523
		}
524 525 526

		if (old_dir_entry) {
			inc_nlink(new_dir);
527
			update_inode_page(new_dir);
528 529 530
		}
	}

531 532 533 534
	down_write(&F2FS_I(old_inode)->i_sem);
	file_lost_pino(old_inode);
	up_write(&F2FS_I(old_inode)->i_sem);

535 536 537
	old_inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(old_inode);

538
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
539 540 541 542 543

	if (old_dir_entry) {
		if (old_dir != new_dir) {
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
544
			update_inode_page(old_inode);
545
		} else {
546
			f2fs_dentry_kunmap(old_inode, old_dir_page);
547 548 549
			f2fs_put_page(old_dir_page, 0);
		}
		drop_nlink(old_dir);
550
		mark_inode_dirty(old_dir);
551
		update_inode_page(old_dir);
552 553
	}

554
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
555 556 557

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

J
Jaegeuk Kim 已提交
560
put_out_dir:
561
	f2fs_unlock_op(sbi);
562
	f2fs_dentry_kunmap(new_dir, new_page);
563
	f2fs_put_page(new_page, 0);
564 565
out_dir:
	if (old_dir_entry) {
566
		f2fs_dentry_kunmap(old_inode, old_dir_page);
567 568 569
		f2fs_put_page(old_dir_page, 0);
	}
out_old:
570
	f2fs_dentry_kunmap(old_dir, old_page);
571 572 573 574 575
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
576 577 578
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
579
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
580 581
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	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;

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

	err = update_dent_inode(old_inode, &new_dentry->d_name);
	if (err)
		goto out_unlock;

	err = update_dent_inode(new_inode, &old_dentry->d_name);
	if (err)
		goto out_undo;

	/* 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 已提交
694 695 696

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
697 698 699 700 701 702 703 704
	return 0;
out_undo:
	/* Still we may fail to recover name info of f2fs_inode here */
	update_dent_inode(old_inode, &old_dentry->d_name);
out_unlock:
	f2fs_unlock_op(sbi);
out_new_dir:
	if (new_dir_entry) {
705
		f2fs_dentry_kunmap(new_inode, new_dir_page);
C
Chao Yu 已提交
706 707 708 709
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
710
		f2fs_dentry_kunmap(old_inode, old_dir_page);
C
Chao Yu 已提交
711 712 713
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
714
	f2fs_dentry_kunmap(new_dir, new_page);
C
Chao Yu 已提交
715 716
	f2fs_put_page(new_page, 0);
out_old:
717
	f2fs_dentry_kunmap(old_dir, old_page);
C
Chao Yu 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	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)
{
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
		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.
	 */
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
}

C
Chao Yu 已提交
741 742
static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
743
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
C
Chao Yu 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	struct inode *inode;
	int err;

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

	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;
759 760 761 762 763

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

C
Chao Yu 已提交
764 765 766 767 768 769 770 771
	/*
	 * 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);
772 773

	stat_inc_inline_inode(inode);
C
Chao Yu 已提交
774 775 776
	d_tmpfile(dentry, inode);
	unlock_new_inode(inode);
	return 0;
777 778 779

release_out:
	release_orphan_inode(sbi);
C
Chao Yu 已提交
780
out:
781
	handle_failed_inode(inode);
C
Chao Yu 已提交
782 783 784
	return err;
}

785 786 787 788 789 790 791 792 793
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 已提交
794
	.rename2	= f2fs_rename2,
C
Chao Yu 已提交
795
	.tmpfile	= f2fs_tmpfile,
796
	.getattr	= f2fs_getattr,
797 798
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
799
	.set_acl	= f2fs_set_acl,
800 801 802 803 804 805 806 807 808 809
#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,
810
	.follow_link    = f2fs_follow_link,
811
	.put_link       = page_put_link,
812
	.getattr	= f2fs_getattr,
813 814 815 816 817 818 819 820 821 822
	.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 = {
823
	.getattr	= f2fs_getattr,
824 825
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
826
	.set_acl	= f2fs_set_acl,
827 828 829 830 831 832 833
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};