namei.c 27.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>
C
Chao Yu 已提交
18
#include <linux/quotaops.h>
19 20

#include "f2fs.h"
21
#include "node.h"
22 23
#include "xattr.h"
#include "acl.h"
24
#include <trace/events/f2fs.h>
25 26 27

static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
{
28
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
29 30 31
	nid_t ino;
	struct inode *inode;
	bool nid_free = false;
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);
39
	if (!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 54 55 56 57
	inode->i_generation = sbi->s_next_generation++;

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

C
Chao Yu 已提交
61 62 63 64 65 66 67 68
	err = dquot_initialize(inode);
	if (err)
		goto fail_drop;

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

69 70 71 72
	/* If the directory encrypted, then we should encrypt the inode. */
	if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
		f2fs_set_encrypted_inode(inode);

73 74
	set_inode_flag(inode, FI_NEW_INODE);

75 76 77 78 79
	if (f2fs_sb_has_extra_attr(sbi->sb)) {
		set_inode_flag(inode, FI_EXTRA_ATTR);
		F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
	}

80 81
	if (test_opt(sbi, INLINE_XATTR))
		set_inode_flag(inode, FI_INLINE_XATTR);
82
	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
83
		set_inode_flag(inode, FI_INLINE_DATA);
84
	if (f2fs_may_inline_dentry(inode))
85
		set_inode_flag(inode, FI_INLINE_DENTRY);
C
Chao Yu 已提交
86

J
Jaegeuk Kim 已提交
87 88
	f2fs_init_extent_tree(inode, NULL);

C
Chao Yu 已提交
89
	stat_inc_inline_xattr(inode);
90 91 92
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

93
	trace_f2fs_new_inode(inode, 0);
94 95 96
	return inode;

fail:
97
	trace_f2fs_new_inode(inode, err);
98
	make_bad_inode(inode);
99
	if (nid_free)
100
		set_inode_flag(inode, FI_FREE_NID);
101
	iput(inode);
102
	return ERR_PTR(err);
C
Chao Yu 已提交
103 104 105 106 107 108 109 110 111 112
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);
113 114 115 116
}

static int is_multimedia_file(const unsigned char *s, const char *sub)
{
117 118
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
119
	int i;
120

C
Chao Yu 已提交
121 122
	/*
	 * filename format of multimedia file should be defined as:
123
	 * "filename + '.' + extension + (optional: '.' + temp extension)".
C
Chao Yu 已提交
124 125 126 127
	 */
	if (slen < sublen + 2)
		return 0;

128 129 130 131 132 133
	for (i = 1; i < slen - sublen; i++) {
		if (s[i] != '.')
			continue;
		if (!strncasecmp(s + i + 1, sub, sublen))
			return 1;
	}
134

135
	return 0;
136 137
}

J
Jaegeuk Kim 已提交
138
/*
139 140
 * Set multimedia files as cold files for hot/cold data separation
 */
141
static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
142 143 144 145 146 147 148
		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++) {
149
		if (is_multimedia_file(name, extlist[i])) {
150
			file_set_cold(inode);
151 152 153 154 155 156 157 158
			break;
		}
	}
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
159
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
160 161
	struct inode *inode;
	nid_t ino = 0;
162
	int err;
163

C
Chao Yu 已提交
164 165 166 167
	err = dquot_initialize(dir);
	if (err)
		return err;

168 169 170 171 172
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
173
		set_cold_files(sbi, inode, dentry->d_name.name);
174 175 176 177 178 179

	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;

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

	alloc_nid_done(sbi, ino);

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

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
193 194

	f2fs_balance_fs(sbi, true);
195 196
	return 0;
out:
197
	handle_failed_inode(inode);
198 199 200 201 202 203
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
204
	struct inode *inode = d_inode(old_dentry);
205
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
206
	int err;
207

208
	if (f2fs_encrypted_inode(dir) &&
209
			!fscrypt_has_permitted_context(dir, inode))
210 211
		return -EPERM;

C
Chao Yu 已提交
212 213 214 215
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
216
	f2fs_balance_fs(sbi, true);
217

218
	inode->i_ctime = current_time(inode);
J
Jaegeuk Kim 已提交
219
	ihold(inode);
220

221
	set_inode_flag(inode, FI_INC_LINK);
222
	f2fs_lock_op(sbi);
223 224 225
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
226
	f2fs_unlock_op(sbi);
227 228

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
229 230 231

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
232 233
	return 0;
out:
234
	clear_inode_flag(inode, FI_INC_LINK);
235
	iput(inode);
236
	f2fs_unlock_op(sbi);
237 238 239 240 241 242
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
243 244 245 246 247
	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);
248
		return ERR_PTR(-ENOENT);
249
	}
250
	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
251 252
}

253 254 255 256 257 258 259 260 261
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;

262 263 264 265 266 267 268
	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;
	}

269 270 271 272
	err = dquot_initialize(dir);
	if (err)
		return err;

J
Jaegeuk Kim 已提交
273
	f2fs_balance_fs(sbi, true);
274

275 276 277 278 279 280
	f2fs_lock_op(sbi);

	de = f2fs_find_entry(dir, &dot, &page);
	if (de) {
		f2fs_dentry_kunmap(dir, page);
		f2fs_put_page(page, 0);
281 282 283
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto out;
284 285 286 287 288 289 290 291 292 293
	} 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);
294 295
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
296 297 298 299
	} else {
		err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
	}
out:
300
	if (!err)
301
		clear_inode_flag(dir, FI_INLINE_DOTS);
302 303 304 305 306

	f2fs_unlock_op(sbi);
	return err;
}

307 308 309 310 311 312
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 已提交
313
	nid_t ino;
314
	int err = 0;
315
	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	if (f2fs_encrypted_inode(dir)) {
		int res = fscrypt_get_encryption_info(dir);

		/*
		 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
		 * created while the directory was encrypted and we
		 * don't have access to the key.
		 */
		if (fscrypt_has_encryption_key(dir))
			fscrypt_set_encrypted_dentry(dentry);
		fscrypt_set_d_op(dentry);
		if (res && res != -ENOKEY)
			return ERR_PTR(res);
	}

332
	if (dentry->d_name.len > F2FS_NAME_LEN)
333 334 335
		return ERR_PTR(-ENAMETOOLONG);

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
J
Jaegeuk Kim 已提交
336 337 338
	if (!de) {
		if (IS_ERR(page))
			return (struct dentry *)page;
J
Jaegeuk Kim 已提交
339
		return d_splice_alias(inode, dentry);
J
Jaegeuk Kim 已提交
340
	}
341

J
Jaegeuk Kim 已提交
342 343 344
	ino = le32_to_cpu(de->ino);
	f2fs_dentry_kunmap(dir, page);
	f2fs_put_page(page, 0);
345

J
Jaegeuk Kim 已提交
346 347 348
	inode = f2fs_iget(dir->i_sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
349

350 351 352 353 354 355
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
			goto err_out;
	}

356
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
357
		err = __recover_dot_dentries(inode, dir->i_ino);
358 359
		if (err)
			goto err_out;
360
	}
D
Dan Carpenter 已提交
361 362 363
	if (f2fs_encrypted_inode(dir) &&
	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
	    !fscrypt_has_permitted_context(dir, inode)) {
364 365 366 367
		f2fs_msg(inode->i_sb, KERN_WARNING,
			 "Inconsistent encryption contexts: %lu/%lu",
			 dir->i_ino, inode->i_ino);
		err = -EPERM;
368
		goto err_out;
369
	}
370
	return d_splice_alias(inode, dentry);
371 372

err_out:
373
	iput(inode);
374
	return ERR_PTR(err);
375 376 377 378
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
379
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
380
	struct inode *inode = d_inode(dentry);
381 382 383 384
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

385
	trace_f2fs_unlink_enter(dir, dentry);
386

C
Chao Yu 已提交
387 388 389 390
	err = dquot_initialize(dir);
	if (err)
		return err;

391
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
392 393 394
	if (!de) {
		if (IS_ERR(page))
			err = PTR_ERR(page);
395
		goto fail;
396
	}
397

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

400
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
401
	err = acquire_orphan_inode(sbi);
402
	if (err) {
403
		f2fs_unlock_op(sbi);
404
		f2fs_dentry_kunmap(dir, page);
405 406 407
		f2fs_put_page(page, 0);
		goto fail;
	}
408
	f2fs_delete_entry(de, page, dir, inode);
409
	f2fs_unlock_op(sbi);
410

J
Jaegeuk Kim 已提交
411 412
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
413
fail:
414
	trace_f2fs_unlink_exit(inode, err);
415 416 417
	return err;
}

418
static const char *f2fs_get_link(struct dentry *dentry,
419 420
				 struct inode *inode,
				 struct delayed_call *done)
421
{
422
	const char *link = page_get_link(dentry, inode, done);
423 424
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
425 426
		do_delayed_call(done);
		clear_delayed_call(done);
427
		link = ERR_PTR(-ENOENT);
428
	}
429
	return link;
430 431
}

432 433 434
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
435
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
436
	struct inode *inode;
437
	size_t len = strlen(symname);
438 439
	struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
	struct fscrypt_symlink_data *sd = NULL;
440
	int err;
441

442
	if (f2fs_encrypted_inode(dir)) {
443
		err = fscrypt_get_encryption_info(dir);
444 445 446
		if (err)
			return err;

447
		if (!fscrypt_has_encryption_key(dir))
448
			return -ENOKEY;
449

450 451
		disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
				sizeof(struct fscrypt_symlink_data));
452 453 454
	}

	if (disk_link.len > dir->i_sb->s_blocksize)
455 456
		return -ENAMETOOLONG;

C
Chao Yu 已提交
457 458 459 460
	err = dquot_initialize(dir);
	if (err)
		return err;

461 462 463 464
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

465 466 467 468
	if (f2fs_encrypted_inode(inode))
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
469
	inode_nohighmem(inode);
470 471
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

472
	f2fs_lock_op(sbi);
473 474 475
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
476
	f2fs_unlock_op(sbi);
477 478
	alloc_nid_done(sbi, inode->i_ino);

479
	if (f2fs_encrypted_inode(inode)) {
480
		struct qstr istr = QSTR_INIT(symname, len);
481
		struct fscrypt_str ostr;
482

483 484 485
		sd = kzalloc(disk_link.len, GFP_NOFS);
		if (!sd) {
			err = -ENOMEM;
486
			goto err_out;
487
		}
488

489
		err = fscrypt_get_encryption_info(inode);
490 491 492
		if (err)
			goto err_out;

493
		if (!fscrypt_has_encryption_key(inode)) {
494
			err = -ENOKEY;
495 496 497
			goto err_out;
		}

498 499
		ostr.name = sd->encrypted_path;
		ostr.len = disk_link.len;
500
		err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
501
		if (err)
502
			goto err_out;
503 504 505

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

508
	err = page_symlink(inode, disk_link.name, disk_link.len);
509 510

err_out:
511 512
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
513

514 515 516 517 518 519 520 521 522
	/*
	 * 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 已提交
523
	if (!err) {
524 525
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
526

C
Chao Yu 已提交
527 528 529 530 531
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
532 533

	kfree(sd);
534 535

	f2fs_balance_fs(sbi, true);
536 537
	return err;
out:
538
	handle_failed_inode(inode);
539 540 541 542 543
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
544
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
545
	struct inode *inode;
546
	int err;
547

C
Chao Yu 已提交
548 549 550 551
	err = dquot_initialize(dir);
	if (err)
		return err;

552 553
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
554
		return PTR_ERR(inode);
555 556 557 558

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

561
	set_inode_flag(inode, FI_INC_LINK);
562
	f2fs_lock_op(sbi);
563 564 565
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
566
	f2fs_unlock_op(sbi);
567 568 569 570 571 572

	alloc_nid_done(sbi, inode->i_ino);

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

J
Jaegeuk Kim 已提交
573 574
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
575 576

	f2fs_balance_fs(sbi, true);
577 578 579
	return 0;

out_fail:
580
	clear_inode_flag(inode, FI_INC_LINK);
581
	handle_failed_inode(inode);
582 583 584 585 586
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
587
	struct inode *inode = d_inode(dentry);
588 589 590 591 592 593 594 595
	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)
{
596
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
597 598 599
	struct inode *inode;
	int err = 0;

C
Chao Yu 已提交
600 601 602 603
	err = dquot_initialize(dir);
	if (err)
		return err;

604 605 606 607 608 609 610
	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;

611
	f2fs_lock_op(sbi);
612 613 614
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
615
	f2fs_unlock_op(sbi);
616 617

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

619 620
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
621 622 623

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
624 625

	f2fs_balance_fs(sbi, true);
626 627
	return 0;
out:
628
	handle_failed_inode(inode);
629 630 631
	return err;
}

C
Chao Yu 已提交
632 633 634 635 636 637 638
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 已提交
639 640 641 642
	err = dquot_initialize(dir);
	if (err)
		return err;

C
Chao Yu 已提交
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
	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.
	 */
669
	add_orphan_inode(inode);
C
Chao Yu 已提交
670 671 672
	alloc_nid_done(sbi, inode->i_ino);

	if (whiteout) {
673
		f2fs_i_links_write(inode, false);
C
Chao Yu 已提交
674 675 676 677
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
678 679
	/* link_count was changed by d_tmpfile as well. */
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
680
	unlock_new_inode(inode);
681 682

	f2fs_balance_fs(sbi, true);
C
Chao Yu 已提交
683 684 685 686 687 688 689 690 691 692 693
	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)
{
694
	if (f2fs_encrypted_inode(dir)) {
695
		int err = fscrypt_get_encryption_info(dir);
696 697 698 699
		if (err)
			return err;
	}

C
Chao Yu 已提交
700 701 702 703 704 705 706 707
	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);
}

708
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
709 710
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
711
{
712
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
713 714
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
715
	struct inode *whiteout = NULL;
716
	struct page *old_dir_page;
C
Chao Yu 已提交
717
	struct page *old_page, *new_page = NULL;
718 719 720
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
721
	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
722
	int err = -ENOENT;
723

724 725 726 727 728 729
	if ((f2fs_encrypted_inode(old_dir) &&
			!fscrypt_has_encryption_key(old_dir)) ||
			(f2fs_encrypted_inode(new_dir) &&
			!fscrypt_has_encryption_key(new_dir)))
		return -ENOKEY;

730
	if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
731
			!fscrypt_has_permitted_context(new_dir, old_inode)) {
732 733 734 735
		err = -EPERM;
		goto out;
	}

C
Chao Yu 已提交
736 737 738 739 740 741 742 743
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

744
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
745 746 747
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
748
		goto out;
749
	}
750 751 752

	if (S_ISDIR(old_inode->i_mode)) {
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
753
		if (!old_dir_entry) {
754 755
			if (IS_ERR(old_dir_page))
				err = PTR_ERR(old_dir_page);
756
			goto out_old;
757
		}
758 759
	}

C
Chao Yu 已提交
760 761 762 763 764 765
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

766 767 768 769
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
770
			goto out_whiteout;
771 772 773 774

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
775 776 777
		if (!new_entry) {
			if (IS_ERR(new_page))
				err = PTR_ERR(new_page);
C
Chao Yu 已提交
778
			goto out_whiteout;
779
		}
780

J
Jaegeuk Kim 已提交
781
		f2fs_balance_fs(sbi, true);
782

783 784
		f2fs_lock_op(sbi);

J
Jaegeuk Kim 已提交
785 786 787 788
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

789 790
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

791
		new_inode->i_ctime = current_time(new_inode);
792
		down_write(&F2FS_I(new_inode)->i_sem);
793
		if (old_dir_entry)
794 795
			f2fs_i_links_write(new_inode, false);
		f2fs_i_links_write(new_inode, false);
796 797
		up_write(&F2FS_I(new_inode)->i_sem);

798
		if (!new_inode->i_nlink)
799
			add_orphan_inode(new_inode);
J
Jaegeuk Kim 已提交
800 801
		else
			release_orphan_inode(sbi);
802
	} else {
J
Jaegeuk Kim 已提交
803
		f2fs_balance_fs(sbi, true);
804

805 806
		f2fs_lock_op(sbi);

807
		err = f2fs_add_link(new_dentry, old_inode);
808 809
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
810
			goto out_whiteout;
811
		}
812

813
		if (old_dir_entry)
814
			f2fs_i_links_write(new_dir, true);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

		/*
		 * 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) {
830 831 832
				err = -ENOENT;
				if (IS_ERR(old_page))
					err = PTR_ERR(old_page);
833 834 835 836
				f2fs_unlock_op(sbi);
				goto out_whiteout;
			}
		}
837 838
	}

839
	down_write(&F2FS_I(old_inode)->i_sem);
840 841 842 843
	if (!old_dir_entry || whiteout)
		file_lost_pino(old_inode);
	else
		F2FS_I(old_inode)->i_pino = new_dir->i_ino;
844 845
	up_write(&F2FS_I(old_inode)->i_sem);

846
	old_inode->i_ctime = current_time(old_inode);
847
	f2fs_mark_inode_dirty_sync(old_inode, false);
848

849
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
850

C
Chao Yu 已提交
851 852
	if (whiteout) {
		whiteout->i_state |= I_LINKABLE;
853
		set_inode_flag(whiteout, FI_INC_LINK);
C
Chao Yu 已提交
854 855 856 857 858 859 860
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

861
	if (old_dir_entry) {
C
Chao Yu 已提交
862
		if (old_dir != new_dir && !whiteout) {
863 864 865
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
		} else {
866
			f2fs_dentry_kunmap(old_inode, old_dir_page);
867 868
			f2fs_put_page(old_dir_page, 0);
		}
869
		f2fs_i_links_write(old_dir, false);
870 871
	}

872
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
873 874 875

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

J
Jaegeuk Kim 已提交
878
put_out_dir:
879
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
880 881 882 883 884 885 886
	if (new_page) {
		f2fs_dentry_kunmap(new_dir, new_page);
		f2fs_put_page(new_page, 0);
	}
out_whiteout:
	if (whiteout)
		iput(whiteout);
887 888
out_dir:
	if (old_dir_entry) {
889
		f2fs_dentry_kunmap(old_inode, old_dir_page);
890 891 892
		f2fs_put_page(old_dir_page, 0);
	}
out_old:
893
	f2fs_dentry_kunmap(old_dir, old_page);
894 895 896 897 898
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
899 900 901
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
902
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
903 904
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
905 906 907 908 909 910
	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;
911 912 913 914 915 916

	if ((f2fs_encrypted_inode(old_dir) &&
			!fscrypt_has_encryption_key(old_dir)) ||
			(f2fs_encrypted_inode(new_dir) &&
			!fscrypt_has_encryption_key(new_dir)))
		return -ENOKEY;
C
Chao Yu 已提交
917

918
	if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
919 920 921
			(old_dir != new_dir) &&
			(!fscrypt_has_permitted_context(new_dir, old_inode) ||
			 !fscrypt_has_permitted_context(old_dir, new_inode)))
922 923
		return -EPERM;

C
Chao Yu 已提交
924 925 926 927 928 929 930 931
	err = dquot_initialize(old_dir);
	if (err)
		goto out;

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

C
Chao Yu 已提交
932
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
933 934 935
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
C
Chao Yu 已提交
936
		goto out;
937
	}
C
Chao Yu 已提交
938 939

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
940 941 942
	if (!new_entry) {
		if (IS_ERR(new_page))
			err = PTR_ERR(new_page);
C
Chao Yu 已提交
943
		goto out_old;
944
	}
C
Chao Yu 已提交
945 946 947 948 949 950

	/* 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);
951
			if (!old_dir_entry) {
952 953
				if (IS_ERR(old_dir_page))
					err = PTR_ERR(old_dir_page);
C
Chao Yu 已提交
954
				goto out_new;
955
			}
C
Chao Yu 已提交
956 957 958 959 960
		}

		if (S_ISDIR(new_inode->i_mode)) {
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
961
			if (!new_dir_entry) {
962 963
				if (IS_ERR(new_dir_page))
					err = PTR_ERR(new_dir_page);
C
Chao Yu 已提交
964
				goto out_old_dir;
965
			}
C
Chao Yu 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978
		}
	}

	/*
	 * 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;
979 980
		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
C
Chao Yu 已提交
981 982 983
			goto out_new_dir;
	}

J
Jaegeuk Kim 已提交
984
	f2fs_balance_fs(sbi, true);
985

C
Chao Yu 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	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);

1003
	old_dir->i_ctime = current_time(old_dir);
C
Chao Yu 已提交
1004 1005
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
1006
		f2fs_i_links_write(old_dir, old_nlink > 0);
C
Chao Yu 已提交
1007 1008
		up_write(&F2FS_I(old_dir)->i_sem);
	}
1009
	f2fs_mark_inode_dirty_sync(old_dir, false);
C
Chao Yu 已提交
1010 1011 1012 1013 1014 1015 1016 1017

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

1018
	new_dir->i_ctime = current_time(new_dir);
C
Chao Yu 已提交
1019 1020
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
1021
		f2fs_i_links_write(new_dir, new_nlink > 0);
C
Chao Yu 已提交
1022 1023
		up_write(&F2FS_I(new_dir)->i_sem);
	}
1024
	f2fs_mark_inode_dirty_sync(new_dir, false);
C
Chao Yu 已提交
1025 1026

	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
1027 1028 1029

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
1030 1031 1032
	return 0;
out_new_dir:
	if (new_dir_entry) {
1033
		f2fs_dentry_kunmap(new_inode, new_dir_page);
C
Chao Yu 已提交
1034 1035 1036 1037
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
1038
		f2fs_dentry_kunmap(old_inode, old_dir_page);
C
Chao Yu 已提交
1039 1040 1041
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
1042
	f2fs_dentry_kunmap(new_dir, new_page);
C
Chao Yu 已提交
1043 1044
	f2fs_put_page(new_page, 0);
out_old:
1045
	f2fs_dentry_kunmap(old_dir, old_page);
C
Chao Yu 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054
	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 已提交
1055
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		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 已提交
1066
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
1067 1068
}

1069
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1070 1071
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
1072
{
1073 1074
	struct page *cpage = NULL;
	char *caddr, *paddr = NULL;
1075 1076 1077
	struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
	struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
	struct fscrypt_symlink_data *sd;
1078 1079 1080
	u32 max_size = inode->i_sb->s_blocksize;
	int res;

1081 1082 1083
	if (!dentry)
		return ERR_PTR(-ECHILD);

1084
	res = fscrypt_get_encryption_info(inode);
1085 1086 1087 1088 1089
	if (res)
		return ERR_PTR(res);

	cpage = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(cpage))
1090
		return ERR_CAST(cpage);
1091
	caddr = page_address(cpage);
1092 1093

	/* Symlink is encrypted */
1094
	sd = (struct fscrypt_symlink_data *)caddr;
1095
	cstr.name = sd->encrypted_path;
1096
	cstr.len = le16_to_cpu(sd->len);
1097 1098 1099 1100 1101 1102

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

1104
	if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
1105 1106 1107 1108
		/* Symlink data on the disk is corrupted */
		res = -EIO;
		goto errout;
	}
1109
	res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1110 1111 1112
	if (res)
		goto errout;

1113
	res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1114
	if (res)
1115 1116
		goto errout;

1117 1118 1119 1120 1121 1122
	/* this is broken symlink case */
	if (unlikely(pstr.name[0] == 0)) {
		res = -ENOENT;
		goto errout;
	}

1123 1124 1125
	paddr = pstr.name;

	/* Null-terminate the name */
1126
	paddr[pstr.len] = '\0';
1127

1128
	put_page(cpage);
1129 1130
	set_delayed_call(done, kfree_link, paddr);
	return paddr;
1131
errout:
1132
	fscrypt_fname_free_buffer(&pstr);
1133
	put_page(cpage);
1134
	return ERR_PTR(res);
C
Chao Yu 已提交
1135 1136
}

1137
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1138
	.get_link       = f2fs_encrypted_get_link,
1139 1140
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1141
#ifdef CONFIG_F2FS_FS_XATTR
1142
	.listxattr	= f2fs_listxattr,
1143
#endif
1144 1145
};

1146 1147 1148 1149 1150 1151 1152 1153 1154
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,
1155
	.rename		= f2fs_rename2,
C
Chao Yu 已提交
1156
	.tmpfile	= f2fs_tmpfile,
1157
	.getattr	= f2fs_getattr,
1158 1159
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1160
	.set_acl	= f2fs_set_acl,
1161 1162 1163 1164 1165 1166
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_symlink_inode_operations = {
1167
	.get_link       = f2fs_get_link,
1168
	.getattr	= f2fs_getattr,
1169 1170 1171 1172 1173 1174 1175
	.setattr	= f2fs_setattr,
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};

const struct inode_operations f2fs_special_inode_operations = {
1176
	.getattr	= f2fs_getattr,
1177 1178
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1179
	.set_acl	= f2fs_set_acl,
1180 1181 1182 1183
#ifdef CONFIG_F2FS_FS_XATTR
	.listxattr	= f2fs_listxattr,
#endif
};