namei.c 12.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 18

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

static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
{
26
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
27 28 29
	nid_t ino;
	struct inode *inode;
	bool nid_free = false;
30
	int err;
31

32
	inode = new_inode(dir->i_sb);
33 34 35
	if (!inode)
		return ERR_PTR(-ENOMEM);

36
	f2fs_lock_op(sbi);
37
	if (!alloc_nid(sbi, &ino)) {
38
		f2fs_unlock_op(sbi);
39 40 41
		err = -ENOSPC;
		goto fail;
	}
42
	f2fs_unlock_op(sbi);
43

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

	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;
	}
57
	trace_f2fs_new_inode(inode, 0);
58 59 60 61 62 63 64
	mark_inode_dirty(inode);
	return inode;

out:
	clear_nlink(inode);
	unlock_new_inode(inode);
fail:
65
	trace_f2fs_new_inode(inode, err);
66
	make_bad_inode(inode);
67 68 69 70 71 72 73 74
	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)
{
75 76
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
77 78

	if (sublen > slen)
79
		return 0;
80

81
	return !strncasecmp(s + slen - sublen, sub, sublen);
82 83
}

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

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
105
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
106 107
	struct inode *inode;
	nid_t ino = 0;
108
	int err;
109

110 111
	f2fs_balance_fs(sbi);

112 113 114 115 116
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
117
		set_cold_files(sbi, inode, dentry->d_name.name);
118 119 120 121 122 123

	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;

124
	f2fs_lock_op(sbi);
125
	err = f2fs_add_link(dentry, inode);
126
	f2fs_unlock_op(sbi);
127 128 129 130 131
	if (err)
		goto out;

	alloc_nid_done(sbi, ino);

132
	d_instantiate(dentry, inode);
133 134 135 136 137
	unlock_new_inode(inode);
	return 0;
out:
	clear_nlink(inode);
	unlock_new_inode(inode);
138
	make_bad_inode(inode);
139 140 141 142 143 144 145 146 147
	iput(inode);
	alloc_nid_failed(sbi, ino);
	return err;
}

static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
	struct inode *inode = old_dentry->d_inode;
148
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
149
	int err;
150

151 152
	f2fs_balance_fs(sbi);

153
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
154
	ihold(inode);
155 156

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
157
	f2fs_lock_op(sbi);
158
	err = f2fs_add_link(dentry, inode);
159
	f2fs_unlock_op(sbi);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	if (err)
		goto out;

	d_instantiate(dentry, inode);
	return 0;
out:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
	iput(inode);
	return err;
}

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

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;

187
	if (dentry->d_name.len > F2FS_NAME_LEN)
188 189 190 191 192 193 194 195 196 197 198
		return ERR_PTR(-ENAMETOOLONG);

	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (de) {
		nid_t ino = le32_to_cpu(de->ino);
		kunmap(page);
		f2fs_put_page(page, 0);

		inode = f2fs_iget(dir->i_sb, ino);
		if (IS_ERR(inode))
			return ERR_CAST(inode);
199 200

		stat_inc_inline_inode(inode);
201 202 203 204 205 206 207
	}

	return d_splice_alias(inode, dentry);
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
208
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
209 210 211 212 213
	struct inode *inode = dentry->d_inode;
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;

214
	trace_f2fs_unlink_enter(dir, dentry);
215 216
	f2fs_balance_fs(sbi);

217 218 219 220
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (!de)
		goto fail;

221
	f2fs_lock_op(sbi);
J
Jaegeuk Kim 已提交
222
	err = acquire_orphan_inode(sbi);
223
	if (err) {
224
		f2fs_unlock_op(sbi);
225 226 227 228 229
		kunmap(page);
		f2fs_put_page(page, 0);
		goto fail;
	}
	f2fs_delete_entry(de, page, inode);
230
	f2fs_unlock_op(sbi);
231 232 233 234

	/* In order to evict this inode,  we set it dirty */
	mark_inode_dirty(inode);
fail:
235
	trace_f2fs_unlink_exit(inode, err);
236 237 238 239 240 241
	return err;
}

static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
242
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
243
	struct inode *inode;
244
	size_t symlen = strlen(symname) + 1;
245
	int err;
246

247 248
	f2fs_balance_fs(sbi);

249 250 251 252 253 254 255
	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;

256
	f2fs_lock_op(sbi);
257
	err = f2fs_add_link(dentry, inode);
258
	f2fs_unlock_op(sbi);
259 260 261 262 263 264 265 266 267 268 269 270
	if (err)
		goto out;

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

	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
	return err;
out:
	clear_nlink(inode);
	unlock_new_inode(inode);
271
	make_bad_inode(inode);
272 273 274 275 276 277 278 279 280
	iput(inode);
	alloc_nid_failed(sbi, inode->i_ino);
	return err;
}

static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
	struct inode *inode;
281
	int err;
282

283 284
	f2fs_balance_fs(sbi);

285 286
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
287
		return PTR_ERR(inode);
288 289 290 291

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
292
	mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
293 294

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
295
	f2fs_lock_op(sbi);
296
	err = f2fs_add_link(dentry, inode);
297
	f2fs_unlock_op(sbi);
298 299 300 301 302 303 304 305 306 307 308 309 310 311
	if (err)
		goto out_fail;

	alloc_nid_done(sbi, inode->i_ino);

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

	return 0;

out_fail:
	clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
	clear_nlink(inode);
	unlock_new_inode(inode);
312
	make_bad_inode(inode);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	iput(inode);
	alloc_nid_failed(sbi, inode->i_ino);
	return err;
}

static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	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)
{
329
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
330 331 332 333 334 335
	struct inode *inode;
	int err = 0;

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

336 337
	f2fs_balance_fs(sbi);

338 339 340 341 342 343 344
	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;

345
	f2fs_lock_op(sbi);
346
	err = f2fs_add_link(dentry, inode);
347
	f2fs_unlock_op(sbi);
348 349 350 351 352 353 354 355 356 357
	if (err)
		goto out;

	alloc_nid_done(sbi, inode->i_ino);
	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
	return 0;
out:
	clear_nlink(inode);
	unlock_new_inode(inode);
358
	make_bad_inode(inode);
359 360 361 362 363 364 365 366
	iput(inode);
	alloc_nid_failed(sbi, inode->i_ino);
	return err;
}

static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
			struct inode *new_dir, struct dentry *new_dentry)
{
367
	struct f2fs_sb_info *sbi = F2FS_SB(old_dir->i_sb);
368 369 370
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
	struct page *old_dir_page;
J
Jaegeuk Kim 已提交
371
	struct page *old_page, *new_page;
372 373 374
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
375
	int err = -ENOENT;
376

377 378
	f2fs_balance_fs(sbi);

379 380 381 382 383 384 385 386 387 388 389
	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;
	}

390
	f2fs_lock_op(sbi);
391 392 393 394 395 396 397 398 399 400 401 402 403

	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;

J
Jaegeuk Kim 已提交
404 405 406 407
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

408
		if (update_dent_inode(old_inode, &new_dentry->d_name)) {
J
Jaegeuk Kim 已提交
409 410
			release_orphan_inode(sbi);
			goto put_out_dir;
411 412
		}

413 414 415
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
416
		down_write(&F2FS_I(new_inode)->i_sem);
417 418 419
		if (old_dir_entry)
			drop_nlink(new_inode);
		drop_nlink(new_inode);
420 421
		up_write(&F2FS_I(new_inode)->i_sem);

422
		mark_inode_dirty(new_inode);
J
Jaegeuk Kim 已提交
423

424 425
		if (!new_inode->i_nlink)
			add_orphan_inode(sbi, new_inode->i_ino);
J
Jaegeuk Kim 已提交
426 427 428
		else
			release_orphan_inode(sbi);

429
		update_inode_page(old_inode);
430
		update_inode_page(new_inode);
431 432 433 434 435 436 437
	} else {
		err = f2fs_add_link(new_dentry, old_inode);
		if (err)
			goto out_dir;

		if (old_dir_entry) {
			inc_nlink(new_dir);
438
			update_inode_page(new_dir);
439 440 441
		}
	}

442 443 444 445
	down_write(&F2FS_I(old_inode)->i_sem);
	file_lost_pino(old_inode);
	up_write(&F2FS_I(old_inode)->i_sem);

446 447 448 449 450 451 452 453 454
	old_inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(old_inode);

	f2fs_delete_entry(old_entry, old_page, NULL);

	if (old_dir_entry) {
		if (old_dir != new_dir) {
			f2fs_set_link(old_inode, old_dir_entry,
						old_dir_page, new_dir);
455
			update_inode_page(old_inode);
456 457 458 459 460
		} else {
			kunmap(old_dir_page);
			f2fs_put_page(old_dir_page, 0);
		}
		drop_nlink(old_dir);
461
		mark_inode_dirty(old_dir);
462
		update_inode_page(old_dir);
463 464
	}

465
	f2fs_unlock_op(sbi);
466 467
	return 0;

J
Jaegeuk Kim 已提交
468
put_out_dir:
469 470
	kunmap(new_page);
	f2fs_put_page(new_page, 0);
471 472 473 474 475
out_dir:
	if (old_dir_entry) {
		kunmap(old_dir_page);
		f2fs_put_page(old_dir_page, 0);
	}
476
	f2fs_unlock_op(sbi);
477 478 479 480 481 482 483
out_old:
	kunmap(old_page);
	f2fs_put_page(old_page, 0);
out:
	return err;
}

C
Chao Yu 已提交
484 485
static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
486
	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
C
Chao Yu 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	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;
502 503 504 505 506

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

C
Chao Yu 已提交
507 508 509 510 511 512 513 514 515 516 517
	/*
	 * 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);
	d_tmpfile(dentry, inode);
	unlock_new_inode(inode);
	return 0;
518 519 520

release_out:
	release_orphan_inode(sbi);
C
Chao Yu 已提交
521 522 523 524 525 526 527 528 529 530
out:
	f2fs_unlock_op(sbi);
	clear_nlink(inode);
	unlock_new_inode(inode);
	make_bad_inode(inode);
	iput(inode);
	alloc_nid_failed(sbi, inode->i_ino);
	return err;
}

531 532 533 534 535 536 537 538 539 540
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,
	.rename		= f2fs_rename,
C
Chao Yu 已提交
541
	.tmpfile	= f2fs_tmpfile,
542
	.getattr	= f2fs_getattr,
543 544
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
545
	.set_acl	= f2fs_set_acl,
546 547 548 549 550 551 552 553 554 555 556 557
#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,
	.follow_link    = page_follow_link_light,
	.put_link       = page_put_link,
558
	.getattr	= f2fs_getattr,
559 560 561 562 563 564 565 566 567 568
	.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 = {
569
	.getattr	= f2fs_getattr,
570 571
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
572
	.set_acl	= f2fs_set_acl,
573 574 575 576 577 578 579
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};