namei.c 11.7 KB
Newer Older
J
Jaegeuk Kim 已提交
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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>

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

static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
{
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	nid_t ino;
	struct inode *inode;
	bool nid_free = false;
30
	int err, ilock;
31 32 33 34 35

	inode = new_inode(sb);
	if (!inode)
		return ERR_PTR(-ENOMEM);

36
	ilock = mutex_lock_op(sbi);
37
	if (!alloc_nid(sbi, &ino)) {
38
		mutex_unlock_op(sbi, ilock);
39 40 41
		err = -ENOSPC;
		goto fail;
	}
42
	mutex_unlock_op(sbi, ilock);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

	inode->i_uid = current_fsuid();

	if (dir->i_mode & S_ISGID) {
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(mode))
			mode |= S_ISGID;
	} else {
		inode->i_gid = current_fsgid();
	}

	inode->i_ino = ino;
	inode->i_mode = mode;
	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;
	}
66
	trace_f2fs_new_inode(inode, 0);
67 68 69 70 71 72 73
	mark_inode_dirty(inode);
	return inode;

out:
	clear_nlink(inode);
	unlock_new_inode(inode);
fail:
74
	trace_f2fs_new_inode(inode, err);
75
	make_bad_inode(inode);
76 77 78 79 80 81 82 83
	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)
{
84 85
	size_t slen = strlen(s);
	size_t sublen = strlen(sub);
86 87

	if (sublen > slen)
88
		return 0;
89

90
	return !strncasecmp(s + slen - sublen, sub, sublen);
91 92
}

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

static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
						bool excl)
{
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	struct inode *inode;
	nid_t ino = 0;
118
	int err, ilock;
119

120 121
	f2fs_balance_fs(sbi);

122 123 124 125 126
	inode = f2fs_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);

	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
127
		set_cold_files(sbi, inode, dentry->d_name.name);
128 129 130 131 132 133

	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;

134
	ilock = mutex_lock_op(sbi);
135
	err = f2fs_add_link(dentry, inode);
136
	mutex_unlock_op(sbi, ilock);
137 138 139 140 141
	if (err)
		goto out;

	alloc_nid_done(sbi, ino);

142
	d_instantiate(dentry, inode);
143 144 145 146 147
	unlock_new_inode(inode);
	return 0;
out:
	clear_nlink(inode);
	unlock_new_inode(inode);
148
	make_bad_inode(inode);
149 150 151 152 153 154 155 156 157 158 159
	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;
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
160
	int err, ilock;
161

162 163
	f2fs_balance_fs(sbi);

164
	inode->i_ctime = CURRENT_TIME;
J
Jaegeuk Kim 已提交
165
	ihold(inode);
166 167

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
168
	ilock = mutex_lock_op(sbi);
169
	err = f2fs_add_link(dentry, inode);
170
	mutex_unlock_op(sbi, ilock);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	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;

198
	if (dentry->d_name.len > F2FS_NAME_LEN)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		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);
	}

	return d_splice_alias(inode, dentry);
}

static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
{
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	struct inode *inode = dentry->d_inode;
	struct f2fs_dir_entry *de;
	struct page *page;
	int err = -ENOENT;
223
	int ilock;
224

225
	trace_f2fs_unlink_enter(dir, dentry);
226 227
	f2fs_balance_fs(sbi);

228 229 230 231
	de = f2fs_find_entry(dir, &dentry->d_name, &page);
	if (!de)
		goto fail;

J
Jaegeuk Kim 已提交
232
	err = acquire_orphan_inode(sbi);
233 234 235 236 237 238
	if (err) {
		kunmap(page);
		f2fs_put_page(page, 0);
		goto fail;
	}

239
	ilock = mutex_lock_op(sbi);
240
	f2fs_delete_entry(de, page, inode);
241
	mutex_unlock_op(sbi, ilock);
242 243 244 245

	/* In order to evict this inode,  we set it dirty */
	mark_inode_dirty(inode);
fail:
246
	trace_f2fs_unlink_exit(inode, err);
247 248 249 250 251 252 253 254 255
	return err;
}

static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
					const char *symname)
{
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	struct inode *inode;
256
	size_t symlen = strlen(symname) + 1;
257
	int err, ilock;
258

259 260
	f2fs_balance_fs(sbi);

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

268
	ilock = mutex_lock_op(sbi);
269
	err = f2fs_add_link(dentry, inode);
270
	mutex_unlock_op(sbi, ilock);
271 272 273 274 275 276 277 278 279 280 281 282
	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);
283
	make_bad_inode(inode);
284 285 286 287 288 289 290 291 292
	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;
293
	int err, ilock;
294

295 296
	f2fs_balance_fs(sbi);

297 298
	inode = f2fs_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
299
		return PTR_ERR(inode);
300 301 302 303

	inode->i_op = &f2fs_dir_inode_operations;
	inode->i_fop = &f2fs_dir_operations;
	inode->i_mapping->a_ops = &f2fs_dblock_aops;
304
	mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
305 306

	set_inode_flag(F2FS_I(inode), FI_INC_LINK);
307
	ilock = mutex_lock_op(sbi);
308
	err = f2fs_add_link(dentry, inode);
309
	mutex_unlock_op(sbi, ilock);
310 311 312 313 314 315 316 317 318 319 320 321 322 323
	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);
324
	make_bad_inode(inode);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	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)
{
	struct super_block *sb = dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	struct inode *inode;
	int err = 0;
345
	int ilock;
346 347 348 349

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

350 351
	f2fs_balance_fs(sbi);

352 353 354 355 356 357 358
	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;

359
	ilock = mutex_lock_op(sbi);
360
	err = f2fs_add_link(dentry, inode);
361
	mutex_unlock_op(sbi, ilock);
362 363 364 365 366 367 368 369 370 371
	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);
372
	make_bad_inode(inode);
373 374 375 376 377 378 379 380 381 382 383 384 385
	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)
{
	struct super_block *sb = old_dir->i_sb;
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
	struct page *old_dir_page;
J
Jaegeuk Kim 已提交
386
	struct page *old_page, *new_page;
387 388 389
	struct f2fs_dir_entry *old_dir_entry = NULL;
	struct f2fs_dir_entry *old_entry;
	struct f2fs_dir_entry *new_entry;
390
	int err = -ENOENT, ilock = -1;
391

392 393
	f2fs_balance_fs(sbi);

394 395 396 397 398 399 400 401 402 403 404
	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;
	}

405
	ilock = mutex_lock_op(sbi);
406 407 408 409 410 411 412 413 414 415 416 417 418

	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 已提交
419 420 421 422
		err = acquire_orphan_inode(sbi);
		if (err)
			goto put_out_dir;

423
		if (update_dent_inode(old_inode, &new_dentry->d_name)) {
J
Jaegeuk Kim 已提交
424 425
			release_orphan_inode(sbi);
			goto put_out_dir;
426 427
		}

428 429 430 431 432 433
		f2fs_set_link(new_dir, new_entry, new_page, old_inode);

		new_inode->i_ctime = CURRENT_TIME;
		if (old_dir_entry)
			drop_nlink(new_inode);
		drop_nlink(new_inode);
J
Jaegeuk Kim 已提交
434

435 436
		if (!new_inode->i_nlink)
			add_orphan_inode(sbi, new_inode->i_ino);
J
Jaegeuk Kim 已提交
437 438 439
		else
			release_orphan_inode(sbi);

440
		update_inode_page(old_inode);
441
		update_inode_page(new_inode);
442 443 444 445 446 447 448
	} else {
		err = f2fs_add_link(new_dentry, old_inode);
		if (err)
			goto out_dir;

		if (old_dir_entry) {
			inc_nlink(new_dir);
449
			update_inode_page(new_dir);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		}
	}

	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);
		} else {
			kunmap(old_dir_page);
			f2fs_put_page(old_dir_page, 0);
		}
		drop_nlink(old_dir);
467
		update_inode_page(old_dir);
468 469
	}

470
	mutex_unlock_op(sbi, ilock);
471 472
	return 0;

J
Jaegeuk Kim 已提交
473 474
put_out_dir:
	f2fs_put_page(new_page, 1);
475 476 477 478 479
out_dir:
	if (old_dir_entry) {
		kunmap(old_dir_page);
		f2fs_put_page(old_dir_page, 0);
	}
480
	mutex_unlock_op(sbi, ilock);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
out_old:
	kunmap(old_page);
	f2fs_put_page(old_page, 0);
out:
	return err;
}

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,
498
	.getattr	= f2fs_getattr,
499 500 501 502 503 504 505 506 507 508 509 510 511 512
	.setattr	= f2fs_setattr,
	.get_acl	= f2fs_get_acl,
#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,
513
	.getattr	= f2fs_getattr,
514 515 516 517 518 519 520 521 522 523
	.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 = {
524
	.getattr	= f2fs_getattr,
525 526 527 528 529 530 531 532 533
	.setattr        = f2fs_setattr,
	.get_acl	= f2fs_get_acl,
#ifdef CONFIG_F2FS_FS_XATTR
	.setxattr       = generic_setxattr,
	.getxattr       = generic_getxattr,
	.listxattr	= f2fs_listxattr,
	.removexattr    = generic_removexattr,
#endif
};