namei.c 26.5 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 64 65 66
	set_inode_flag(inode, FI_NEW_INODE);

	if (test_opt(sbi, INLINE_XATTR))
		set_inode_flag(inode, FI_INLINE_XATTR);
67
	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
68
		set_inode_flag(inode, FI_INLINE_DATA);
69
	if (f2fs_may_inline_dentry(inode))
70
		set_inode_flag(inode, FI_INLINE_DENTRY);
C
Chao Yu 已提交
71

J
Jaegeuk Kim 已提交
72 73
	f2fs_init_extent_tree(inode, NULL);

C
Chao Yu 已提交
74
	stat_inc_inline_xattr(inode);
75 76 77
	stat_inc_inline_inode(inode);
	stat_inc_inline_dir(inode);

78
	trace_f2fs_new_inode(inode, 0);
79 80 81
	return inode;

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

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

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

103 104 105 106 107 108
	for (i = 1; i < slen - sublen; i++) {
		if (s[i] != '.')
			continue;
		if (!strncasecmp(s + i + 1, sub, sublen))
			return 1;
	}
109

110
	return 0;
111 112
}

J
Jaegeuk Kim 已提交
113
/*
114 115
 * Set multimedia files as cold files for hot/cold data separation
 */
116
static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
117 118 119 120 121 122 123
		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++) {
124
		if (is_multimedia_file(name, extlist[i])) {
125
			file_set_cold(inode);
126 127 128 129 130 131 132 133
			break;
		}
	}
}

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
134
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
135 136
	struct inode *inode;
	nid_t ino = 0;
137
	int err;
138 139 140 141 142 143

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

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
144
		set_cold_files(sbi, inode, dentry->d_name.name);
145 146 147 148 149 150

	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 已提交
151
	f2fs_balance_fs(sbi, true);
152

153
	f2fs_lock_op(sbi);
154 155 156
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
157
	f2fs_unlock_op(sbi);
158 159 160

	alloc_nid_done(sbi, ino);

161
	d_instantiate(dentry, inode);
162
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
163 164 165

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
166 167
	return 0;
out:
168
	handle_failed_inode(inode);
169 170 171 172 173 174
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
175
	struct inode *inode = d_inode(old_dentry);
176
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
177
	int err;
178

179
	if (f2fs_encrypted_inode(dir) &&
180
			!fscrypt_has_permitted_context(dir, inode))
181 182
		return -EPERM;

J
Jaegeuk Kim 已提交
183
	f2fs_balance_fs(sbi, true);
184

185
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
186
	ihold(inode);
187

188
	set_inode_flag(inode, FI_INC_LINK);
189
	f2fs_lock_op(sbi);
190 191 192
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
193
	f2fs_unlock_op(sbi);
194 195

	d_instantiate(dentry, inode);
J
Jaegeuk Kim 已提交
196 197 198

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
199 200
	return 0;
out:
201
	clear_inode_flag(inode, FI_INC_LINK);
202
	iput(inode);
203
	f2fs_unlock_op(sbi);
204 205 206 207 208 209
	return err;
}

struct dentry *f2fs_get_parent(struct dentry *child)
{
	struct qstr dotdot = QSTR_INIT("..", 2);
210 211 212 213 214
	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);
215
		return ERR_PTR(-ENOENT);
216
	}
217
	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
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;

229 230 231 232 233 234 235
	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 已提交
236
	f2fs_balance_fs(sbi, true);
237

238 239 240 241 242 243
	f2fs_lock_op(sbi);

	de = f2fs_find_entry(dir, &dot, &page);
	if (de) {
		f2fs_dentry_kunmap(dir, page);
		f2fs_put_page(page, 0);
244 245 246
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto out;
247 248 249 250 251 252 253 254 255 256
	} 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);
257 258
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
259 260 261 262
	} else {
		err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
	}
out:
263
	if (!err)
264
		clear_inode_flag(dir, FI_INLINE_DOTS);
265 266 267 268 269

	f2fs_unlock_op(sbi);
	return err;
}

270 271 272 273 274 275
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 已提交
276
	nid_t ino;
277
	int err = 0;
278
	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	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);
	}

295
	if (dentry->d_name.len > F2FS_NAME_LEN)
296 297 298
		return ERR_PTR(-ENAMETOOLONG);

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
J
Jaegeuk Kim 已提交
299 300 301
	if (!de) {
		if (IS_ERR(page))
			return (struct dentry *)page;
J
Jaegeuk Kim 已提交
302
		return d_splice_alias(inode, dentry);
J
Jaegeuk Kim 已提交
303
	}
304

J
Jaegeuk Kim 已提交
305 306 307
	ino = le32_to_cpu(de->ino);
	f2fs_dentry_kunmap(dir, page);
	f2fs_put_page(page, 0);
308

J
Jaegeuk Kim 已提交
309 310 311
	inode = f2fs_iget(dir->i_sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
312

313 314 315 316 317 318
	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
		err = __recover_dot_dentries(dir, root_ino);
		if (err)
			goto err_out;
	}

319
	if (f2fs_has_inline_dots(inode)) {
J
Jaegeuk Kim 已提交
320
		err = __recover_dot_dentries(inode, dir->i_ino);
321 322
		if (err)
			goto err_out;
323
	}
324 325 326 327 328
	if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
			(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
			!fscrypt_has_permitted_context(dir, inode)) {
		bool nokey = f2fs_encrypted_inode(inode) &&
			!fscrypt_has_encryption_key(inode);
329 330
		err = nokey ? -ENOKEY : -EPERM;
		goto err_out;
331
	}
332
	return d_splice_alias(inode, dentry);
333 334

err_out:
335
	iput(inode);
336
	return ERR_PTR(err);
337 338 339 340
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
341
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
342
	struct inode *inode = d_inode(dentry);
343 344 345 346
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

347
	trace_f2fs_unlink_enter(dir, dentry);
348

349
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
350 351 352
	if (!de) {
		if (IS_ERR(page))
			err = PTR_ERR(page);
353
		goto fail;
354
	}
355

J
Jaegeuk Kim 已提交
356
	f2fs_balance_fs(sbi, true);
357

358
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
359
	err = acquire_orphan_inode(sbi);
360
	if (err) {
361
		f2fs_unlock_op(sbi);
362
		f2fs_dentry_kunmap(dir, page);
363 364 365
		f2fs_put_page(page, 0);
		goto fail;
	}
366
	f2fs_delete_entry(de, page, dir, inode);
367
	f2fs_unlock_op(sbi);
368

J
Jaegeuk Kim 已提交
369 370
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
371
fail:
372
	trace_f2fs_unlink_exit(inode, err);
373 374 375
	return err;
}

376
static const char *f2fs_get_link(struct dentry *dentry,
377 378
				 struct inode *inode,
				 struct delayed_call *done)
379
{
380
	const char *link = page_get_link(dentry, inode, done);
381 382
	if (!IS_ERR(link) && !*link) {
		/* this is broken symlink case */
383 384
		do_delayed_call(done);
		clear_delayed_call(done);
385
		link = ERR_PTR(-ENOENT);
386
	}
387
	return link;
388 389
}

390 391 392
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
393
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
394
	struct inode *inode;
395
	size_t len = strlen(symname);
396 397
	struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
	struct fscrypt_symlink_data *sd = NULL;
398
	int err;
399

400
	if (f2fs_encrypted_inode(dir)) {
401
		err = fscrypt_get_encryption_info(dir);
402 403 404
		if (err)
			return err;

405
		if (!fscrypt_has_encryption_key(dir))
406 407
			return -EPERM;

408 409
		disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
				sizeof(struct fscrypt_symlink_data));
410 411 412
	}

	if (disk_link.len > dir->i_sb->s_blocksize)
413 414
		return -ENAMETOOLONG;

415 416 417 418
	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

419 420 421 422
	if (f2fs_encrypted_inode(inode))
		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
	else
		inode->i_op = &f2fs_symlink_inode_operations;
423
	inode_nohighmem(inode);
424 425
	inode->i_mapping->a_ops = &f2fs_dblock_aops;

J
Jaegeuk Kim 已提交
426
	f2fs_balance_fs(sbi, true);
427

428
	f2fs_lock_op(sbi);
429 430 431
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
432
	f2fs_unlock_op(sbi);
433 434
	alloc_nid_done(sbi, inode->i_ino);

435
	if (f2fs_encrypted_inode(inode)) {
436
		struct qstr istr = QSTR_INIT(symname, len);
437
		struct fscrypt_str ostr;
438

439 440 441
		sd = kzalloc(disk_link.len, GFP_NOFS);
		if (!sd) {
			err = -ENOMEM;
442
			goto err_out;
443
		}
444

445
		err = fscrypt_get_encryption_info(inode);
446 447 448
		if (err)
			goto err_out;

449
		if (!fscrypt_has_encryption_key(inode)) {
450
			err = -EPERM;
451 452 453
			goto err_out;
		}

454 455
		ostr.name = sd->encrypted_path;
		ostr.len = disk_link.len;
456
		err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
457
		if (err < 0)
458
			goto err_out;
459 460 461

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

464
	err = page_symlink(inode, disk_link.name, disk_link.len);
465 466

err_out:
467 468
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
469

470 471 472 473 474 475 476 477 478
	/*
	 * 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 已提交
479
	if (!err) {
480 481
		filemap_write_and_wait_range(inode->i_mapping, 0,
							disk_link.len - 1);
482

C
Chao Yu 已提交
483 484 485 486 487
		if (IS_DIRSYNC(dir))
			f2fs_sync_fs(sbi->sb, 1);
	} else {
		f2fs_unlink(dir, dentry);
	}
488 489

	kfree(sd);
490 491
	return err;
out:
492
	handle_failed_inode(inode);
493 494 495 496 497
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
498
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
499
	struct inode *inode;
500
	int err;
501 502 503

	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
504
		return PTR_ERR(inode);
505 506 507 508

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

J
Jaegeuk Kim 已提交
511
	f2fs_balance_fs(sbi, true);
512

513
	set_inode_flag(inode, FI_INC_LINK);
514
	f2fs_lock_op(sbi);
515 516 517
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out_fail;
518
	f2fs_unlock_op(sbi);
519 520 521 522 523 524

	alloc_nid_done(sbi, inode->i_ino);

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

J
Jaegeuk Kim 已提交
525 526
	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
527 528 529
	return 0;

out_fail:
530
	clear_inode_flag(inode, FI_INC_LINK);
531
	handle_failed_inode(inode);
532 533 534 535 536
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
537
	struct inode *inode = d_inode(dentry);
538 539 540 541 542 543 544 545
	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)
{
546
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
547 548 549 550 551 552 553 554 555 556
	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 已提交
557
	f2fs_balance_fs(sbi, true);
558

559
	f2fs_lock_op(sbi);
560 561 562
	err = f2fs_add_link(dentry, inode);
	if (err)
		goto out;
563
	f2fs_unlock_op(sbi);
564 565

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

567 568
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
J
Jaegeuk Kim 已提交
569 570 571

	if (IS_DIRSYNC(dir))
		f2fs_sync_fs(sbi->sb, 1);
572 573
	return 0;
out:
574
	handle_failed_inode(inode);
575 576 577
	return err;
}

C
Chao Yu 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
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 已提交
598
	f2fs_balance_fs(sbi, true);
599

C
Chao Yu 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612
	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.
	 */
613
	add_orphan_inode(inode);
C
Chao Yu 已提交
614 615 616
	alloc_nid_done(sbi, inode->i_ino);

	if (whiteout) {
617
		f2fs_i_links_write(inode, false);
C
Chao Yu 已提交
618 619 620 621
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
622 623
	/* link_count was changed by d_tmpfile as well. */
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
624 625 626 627 628 629 630 631 632 633 634 635
	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)
{
636
	if (f2fs_encrypted_inode(dir)) {
637
		int err = fscrypt_get_encryption_info(dir);
638 639 640 641
		if (err)
			return err;
	}

C
Chao Yu 已提交
642 643 644 645 646 647 648 649
	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);
}

650
static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
C
Chao Yu 已提交
651 652
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
653
{
654
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
655 656
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
657
	struct inode *whiteout = NULL;
658
	struct page *old_dir_page;
C
Chao Yu 已提交
659
	struct page *old_page, *new_page = NULL;
660 661 662
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
663
	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
664
	int err = -ENOENT;
665

666
	if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
667
			!fscrypt_has_permitted_context(new_dir, old_inode)) {
668 669 670 671
		err = -EPERM;
		goto out;
	}

672
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
673 674 675
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
676
		goto out;
677
	}
678 679 680

	if (S_ISDIR(old_inode->i_mode)) {
		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
681
		if (!old_dir_entry) {
682 683
			if (IS_ERR(old_dir_page))
				err = PTR_ERR(old_dir_page);
684
			goto out_old;
685
		}
686 687
	}

C
Chao Yu 已提交
688 689 690 691 692 693
	if (flags & RENAME_WHITEOUT) {
		err = f2fs_create_whiteout(old_dir, &whiteout);
		if (err)
			goto out_dir;
	}

694 695 696 697
	if (new_inode) {

		err = -ENOTEMPTY;
		if (old_dir_entry && !f2fs_empty_dir(new_inode))
C
Chao Yu 已提交
698
			goto out_whiteout;
699 700 701 702

		err = -ENOENT;
		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
						&new_page);
703 704 705
		if (!new_entry) {
			if (IS_ERR(new_page))
				err = PTR_ERR(new_page);
C
Chao Yu 已提交
706
			goto out_whiteout;
707
		}
708

J
Jaegeuk Kim 已提交
709
		f2fs_balance_fs(sbi, true);
710

711 712
		f2fs_lock_op(sbi);

J
Jaegeuk Kim 已提交
713 714 715 716
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

717 718 719
		err = update_dent_inode(old_inode, new_inode,
						&new_dentry->d_name);
		if (err) {
J
Jaegeuk Kim 已提交
720 721
			release_orphan_inode(sbi);
			goto put_out_dir;
722 723
		}

724 725 726
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
727
		down_write(&F2FS_I(new_inode)->i_sem);
728
		if (old_dir_entry)
729 730
			f2fs_i_links_write(new_inode, false);
		f2fs_i_links_write(new_inode, false);
731 732
		up_write(&F2FS_I(new_inode)->i_sem);

733
		if (!new_inode->i_nlink)
734
			add_orphan_inode(new_inode);
J
Jaegeuk Kim 已提交
735 736
		else
			release_orphan_inode(sbi);
737
	} else {
J
Jaegeuk Kim 已提交
738
		f2fs_balance_fs(sbi, true);
739

740 741
		f2fs_lock_op(sbi);

742
		err = f2fs_add_link(new_dentry, old_inode);
743 744
		if (err) {
			f2fs_unlock_op(sbi);
C
Chao Yu 已提交
745
			goto out_whiteout;
746
		}
747

748
		if (old_dir_entry)
749
			f2fs_i_links_write(new_dir, true);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

		/*
		 * 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) {
765 766 767
				err = -ENOENT;
				if (IS_ERR(old_page))
					err = PTR_ERR(old_page);
768 769 770 771
				f2fs_unlock_op(sbi);
				goto out_whiteout;
			}
		}
772 773
	}

774 775
	down_write(&F2FS_I(old_inode)->i_sem);
	file_lost_pino(old_inode);
776 777
	if (new_inode && file_enc_name(new_inode))
		file_set_enc_name(old_inode);
778 779
	up_write(&F2FS_I(old_inode)->i_sem);

780
	old_inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
781
	f2fs_mark_inode_dirty_sync(old_inode);
782

783
	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
784

C
Chao Yu 已提交
785 786
	if (whiteout) {
		whiteout->i_state |= I_LINKABLE;
787
		set_inode_flag(whiteout, FI_INC_LINK);
C
Chao Yu 已提交
788 789 790 791 792 793 794
		err = f2fs_add_link(old_dentry, whiteout);
		if (err)
			goto put_out_dir;
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

795
	if (old_dir_entry) {
C
Chao Yu 已提交
796
		if (old_dir != new_dir && !whiteout) {
797 798 799
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
		} else {
800
			f2fs_dentry_kunmap(old_inode, old_dir_page);
801 802
			f2fs_put_page(old_dir_page, 0);
		}
803
		f2fs_i_links_write(old_dir, false);
804 805
	}

806
	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
807 808 809

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

J
Jaegeuk Kim 已提交
812
put_out_dir:
813
	f2fs_unlock_op(sbi);
C
Chao Yu 已提交
814 815 816 817 818 819 820
	if (new_page) {
		f2fs_dentry_kunmap(new_dir, new_page);
		f2fs_put_page(new_page, 0);
	}
out_whiteout:
	if (whiteout)
		iput(whiteout);
821 822
out_dir:
	if (old_dir_entry) {
823
		f2fs_dentry_kunmap(old_inode, old_dir_page);
824 825 826
		f2fs_put_page(old_dir_page, 0);
	}
out_old:
827
	f2fs_dentry_kunmap(old_dir, old_page);
828 829 830 831 832
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
833 834 835
static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
			     struct inode *new_dir, struct dentry *new_dentry)
{
836
	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
837 838
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
C
Chao Yu 已提交
839 840 841 842 843 844 845
	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;

846
	if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
847 848 849
			(old_dir != new_dir) &&
			(!fscrypt_has_permitted_context(new_dir, old_inode) ||
			 !fscrypt_has_permitted_context(old_dir, new_inode)))
850 851
		return -EPERM;

C
Chao Yu 已提交
852
	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
853 854 855
	if (!old_entry) {
		if (IS_ERR(old_page))
			err = PTR_ERR(old_page);
C
Chao Yu 已提交
856
		goto out;
857
	}
C
Chao Yu 已提交
858 859

	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
860 861 862
	if (!new_entry) {
		if (IS_ERR(new_page))
			err = PTR_ERR(new_page);
C
Chao Yu 已提交
863
		goto out_old;
864
	}
C
Chao Yu 已提交
865 866 867 868 869 870

	/* 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);
871
			if (!old_dir_entry) {
872 873
				if (IS_ERR(old_dir_page))
					err = PTR_ERR(old_dir_page);
C
Chao Yu 已提交
874
				goto out_new;
875
			}
C
Chao Yu 已提交
876 877 878 879 880
		}

		if (S_ISDIR(new_inode->i_mode)) {
			new_dir_entry = f2fs_parent_dir(new_inode,
							&new_dir_page);
881
			if (!new_dir_entry) {
882 883
				if (IS_ERR(new_dir_page))
					err = PTR_ERR(new_dir_page);
C
Chao Yu 已提交
884
				goto out_old_dir;
885
			}
C
Chao Yu 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
		}
	}

	/*
	 * 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 已提交
904
	f2fs_balance_fs(sbi, true);
905

C
Chao Yu 已提交
906 907
	f2fs_lock_op(sbi);

908
	err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
C
Chao Yu 已提交
909 910
	if (err)
		goto out_unlock;
911 912
	if (file_enc_name(new_inode))
		file_set_enc_name(old_inode);
C
Chao Yu 已提交
913

914
	err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
C
Chao Yu 已提交
915 916
	if (err)
		goto out_undo;
917 918
	if (file_enc_name(old_inode))
		file_set_enc_name(new_inode);
C
Chao Yu 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

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

	old_dir->i_ctime = CURRENT_TIME;
	if (old_nlink) {
		down_write(&F2FS_I(old_dir)->i_sem);
938
		f2fs_i_links_write(old_dir, old_nlink > 0);
C
Chao Yu 已提交
939 940
		up_write(&F2FS_I(old_dir)->i_sem);
	}
J
Jaegeuk Kim 已提交
941
	f2fs_mark_inode_dirty_sync(old_dir);
C
Chao Yu 已提交
942 943 944 945 946 947 948 949 950 951 952

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

	new_dir->i_ctime = CURRENT_TIME;
	if (new_nlink) {
		down_write(&F2FS_I(new_dir)->i_sem);
953
		f2fs_i_links_write(new_dir, new_nlink > 0);
C
Chao Yu 已提交
954 955
		up_write(&F2FS_I(new_dir)->i_sem);
	}
J
Jaegeuk Kim 已提交
956
	f2fs_mark_inode_dirty_sync(new_dir);
C
Chao Yu 已提交
957 958

	f2fs_unlock_op(sbi);
J
Jaegeuk Kim 已提交
959 960 961

	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
		f2fs_sync_fs(sbi->sb, 1);
C
Chao Yu 已提交
962 963
	return 0;
out_undo:
964 965 966 967 968
	/*
	 * 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 已提交
969 970 971 972
out_unlock:
	f2fs_unlock_op(sbi);
out_new_dir:
	if (new_dir_entry) {
973
		f2fs_dentry_kunmap(new_inode, new_dir_page);
C
Chao Yu 已提交
974 975 976 977
		f2fs_put_page(new_dir_page, 0);
	}
out_old_dir:
	if (old_dir_entry) {
978
		f2fs_dentry_kunmap(old_inode, old_dir_page);
C
Chao Yu 已提交
979 980 981
		f2fs_put_page(old_dir_page, 0);
	}
out_new:
982
	f2fs_dentry_kunmap(new_dir, new_page);
C
Chao Yu 已提交
983 984
	f2fs_put_page(new_page, 0);
out_old:
985
	f2fs_dentry_kunmap(old_dir, old_page);
C
Chao Yu 已提交
986 987 988 989 990 991 992 993 994
	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 已提交
995
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
C
Chao Yu 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005
		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 已提交
1006
	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
C
Chao Yu 已提交
1007 1008
}

1009
static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1010 1011
					   struct inode *inode,
					   struct delayed_call *done)
C
Chao Yu 已提交
1012
{
1013 1014
	struct page *cpage = NULL;
	char *caddr, *paddr = NULL;
1015 1016 1017
	struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
	struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
	struct fscrypt_symlink_data *sd;
1018 1019 1020
	u32 max_size = inode->i_sb->s_blocksize;
	int res;

1021 1022 1023
	if (!dentry)
		return ERR_PTR(-ECHILD);

1024
	res = fscrypt_get_encryption_info(inode);
1025 1026 1027 1028 1029
	if (res)
		return ERR_PTR(res);

	cpage = read_mapping_page(inode->i_mapping, 0, NULL);
	if (IS_ERR(cpage))
1030
		return ERR_CAST(cpage);
1031
	caddr = page_address(cpage);
1032 1033

	/* Symlink is encrypted */
1034
	sd = (struct fscrypt_symlink_data *)caddr;
1035
	cstr.name = sd->encrypted_path;
1036
	cstr.len = le16_to_cpu(sd->len);
1037 1038 1039 1040 1041 1042

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

1044
	if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
1045 1046 1047 1048
		/* Symlink data on the disk is corrupted */
		res = -EIO;
		goto errout;
	}
1049
	res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1050 1051 1052
	if (res)
		goto errout;

1053
	res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1054 1055 1056
	if (res < 0)
		goto errout;

1057 1058 1059 1060 1061 1062
	/* this is broken symlink case */
	if (unlikely(pstr.name[0] == 0)) {
		res = -ENOENT;
		goto errout;
	}

1063 1064 1065 1066 1067
	paddr = pstr.name;

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

1068
	put_page(cpage);
1069 1070
	set_delayed_call(done, kfree_link, paddr);
	return paddr;
1071
errout:
1072
	fscrypt_fname_free_buffer(&pstr);
1073
	put_page(cpage);
1074
	return ERR_PTR(res);
C
Chao Yu 已提交
1075 1076
}

1077 1078
const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
	.readlink       = generic_readlink,
1079
	.get_link       = f2fs_encrypted_get_link,
1080 1081
	.getattr	= f2fs_getattr,
	.setattr	= f2fs_setattr,
1082
#ifdef CONFIG_F2FS_FS_XATTR
1083 1084 1085 1086
	.setxattr	= generic_setxattr,
	.getxattr	= generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr	= generic_removexattr,
1087
#endif
1088 1089
};

1090 1091 1092 1093 1094 1095 1096 1097 1098
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 已提交
1099
	.rename2	= f2fs_rename2,
C
Chao Yu 已提交
1100
	.tmpfile	= f2fs_tmpfile,
1101
	.getattr	= f2fs_getattr,
1102 1103
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1104
	.set_acl	= f2fs_set_acl,
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
#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,
1115
	.get_link       = f2fs_get_link,
1116
	.getattr	= f2fs_getattr,
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	.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 = {
1127
	.getattr	= f2fs_getattr,
1128 1129
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
1130
	.set_acl	= f2fs_set_acl,
1131 1132 1133 1134 1135 1136 1137
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};