dir.c 21.7 KB
Newer Older
J
Jaegeuk Kim 已提交
1
/*
J
Jaegeuk Kim 已提交
2 3 4 5 6 7 8 9 10 11 12 13
 * fs/f2fs/dir.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 "f2fs.h"
14
#include "node.h"
J
Jaegeuk Kim 已提交
15
#include "acl.h"
16
#include "xattr.h"
J
Jaegeuk Kim 已提交
17 18 19

static unsigned long dir_blocks(struct inode *inode)
{
20 21
	return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
							>> PAGE_SHIFT;
J
Jaegeuk Kim 已提交
22 23
}

24
static unsigned int dir_buckets(unsigned int level, int dir_level)
J
Jaegeuk Kim 已提交
25
{
26
	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
27
		return 1 << (level + dir_level);
J
Jaegeuk Kim 已提交
28
	else
29
		return MAX_DIR_BUCKETS;
J
Jaegeuk Kim 已提交
30 31 32 33 34 35 36 37 38 39
}

static unsigned int bucket_blocks(unsigned int level)
{
	if (level < MAX_DIR_HASH_DEPTH / 2)
		return 2;
	else
		return 4;
}

C
Chao Yu 已提交
40
static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
J
Jaegeuk Kim 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	[F2FS_FT_UNKNOWN]	= DT_UNKNOWN,
	[F2FS_FT_REG_FILE]	= DT_REG,
	[F2FS_FT_DIR]		= DT_DIR,
	[F2FS_FT_CHRDEV]	= DT_CHR,
	[F2FS_FT_BLKDEV]	= DT_BLK,
	[F2FS_FT_FIFO]		= DT_FIFO,
	[F2FS_FT_SOCK]		= DT_SOCK,
	[F2FS_FT_SYMLINK]	= DT_LNK,
};

static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
	[S_IFREG >> S_SHIFT]	= F2FS_FT_REG_FILE,
	[S_IFDIR >> S_SHIFT]	= F2FS_FT_DIR,
	[S_IFCHR >> S_SHIFT]	= F2FS_FT_CHRDEV,
	[S_IFBLK >> S_SHIFT]	= F2FS_FT_BLKDEV,
	[S_IFIFO >> S_SHIFT]	= F2FS_FT_FIFO,
	[S_IFSOCK >> S_SHIFT]	= F2FS_FT_SOCK,
	[S_IFLNK >> S_SHIFT]	= F2FS_FT_SYMLINK,
};

61
void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
J
Jaegeuk Kim 已提交
62 63 64 65
{
	de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
}

66 67 68 69 70 71 72
unsigned char get_de_type(struct f2fs_dir_entry *de)
{
	if (de->file_type < F2FS_FT_MAX)
		return f2fs_filetype_table[de->file_type];
	return DT_UNKNOWN;
}

73 74
static unsigned long dir_block_index(unsigned int level,
				int dir_level, unsigned int idx)
J
Jaegeuk Kim 已提交
75 76 77 78 79
{
	unsigned long i;
	unsigned long bidx = 0;

	for (i = 0; i < level; i++)
80
		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
J
Jaegeuk Kim 已提交
81 82 83 84 85
	bidx += idx * bucket_blocks(level);
	return bidx;
}

static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
86
				struct fscrypt_name *fname,
87 88
				f2fs_hash_t namehash,
				int *max_slots,
89 90 91 92
				struct page **res_page)
{
	struct f2fs_dentry_block *dentry_blk;
	struct f2fs_dir_entry *de;
93
	struct f2fs_dentry_ptr d;
94 95

	dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page);
96

97
	make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
98
	de = find_target_dentry(fname, namehash, max_slots, &d);
99 100 101 102 103 104 105 106
	if (de)
		*res_page = dentry_page;
	else
		kunmap(dentry_page);

	return de;
}

107
struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
108 109
			f2fs_hash_t namehash, int *max_slots,
			struct f2fs_dentry_ptr *d)
J
Jaegeuk Kim 已提交
110 111
{
	struct f2fs_dir_entry *de;
112 113
	unsigned long bit_pos = 0;
	int max_len = 0;
114 115
	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
	struct fscrypt_str *name = &fname->disk_name;
J
Jaegeuk Kim 已提交
116

117 118 119 120
	if (max_slots)
		*max_slots = 0;
	while (bit_pos < d->max) {
		if (!test_bit_le(bit_pos, d->bitmap)) {
121
			bit_pos++;
122
			max_len++;
123 124
			continue;
		}
125

126
		de = &d->dentry[bit_pos];
127

128 129 130 131 132
		if (unlikely(!de->name_len)) {
			bit_pos++;
			continue;
		}

133 134 135 136 137 138
		/* encrypted case */
		de_name.name = d->filename[bit_pos];
		de_name.len = le16_to_cpu(de->name_len);

		/* show encrypted name */
		if (fname->hash) {
E
Eric Biggers 已提交
139
			if (de->hash_code == cpu_to_le32(fname->hash))
140 141 142 143
				goto found;
		} else if (de_name.len == name->len &&
			de->hash_code == namehash &&
			!memcmp(de_name.name, name->name, name->len))
144 145
			goto found;

146
		if (max_slots && max_len > *max_slots)
147
			*max_slots = max_len;
148
		max_len = 0;
149

150
		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
J
Jaegeuk Kim 已提交
151 152 153 154
	}

	de = NULL;
found:
155
	if (max_slots && max_len > *max_slots)
156
		*max_slots = max_len;
J
Jaegeuk Kim 已提交
157 158 159 160
	return de;
}

static struct f2fs_dir_entry *find_in_level(struct inode *dir,
161
					unsigned int level,
162
					struct fscrypt_name *fname,
163
					struct page **res_page)
J
Jaegeuk Kim 已提交
164
{
165 166
	struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
	int s = GET_DENTRY_SLOTS(name.len);
J
Jaegeuk Kim 已提交
167 168 169 170 171
	unsigned int nbucket, nblock;
	unsigned int bidx, end_block;
	struct page *dentry_page;
	struct f2fs_dir_entry *de = NULL;
	bool room = false;
172
	int max_slots;
173 174
	f2fs_hash_t namehash;

175 176 177 178
	if(fname->hash)
		namehash = cpu_to_le32(fname->hash);
	else
		namehash = f2fs_dentry_hash(&name);
J
Jaegeuk Kim 已提交
179

180
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
181 182
	nblock = bucket_blocks(level);

183 184
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
					le32_to_cpu(namehash) % nbucket);
J
Jaegeuk Kim 已提交
185 186 187 188
	end_block = bidx + nblock;

	for (; bidx < end_block; bidx++) {
		/* no need to allocate new dentry pages to all the indices */
189
		dentry_page = find_data_page(dir, bidx);
J
Jaegeuk Kim 已提交
190
		if (IS_ERR(dentry_page)) {
191 192 193 194 195 196 197
			if (PTR_ERR(dentry_page) == -ENOENT) {
				room = true;
				continue;
			} else {
				*res_page = dentry_page;
				break;
			}
J
Jaegeuk Kim 已提交
198 199
		}

200 201
		de = find_in_block(dentry_page, fname, namehash, &max_slots,
								res_page);
J
Jaegeuk Kim 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
		if (de)
			break;

		if (max_slots >= s)
			room = true;
		f2fs_put_page(dentry_page, 0);
	}

	if (!de && room && F2FS_I(dir)->chash != namehash) {
		F2FS_I(dir)->chash = namehash;
		F2FS_I(dir)->clevel = level;
	}

	return de;
}

218 219
struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
			struct fscrypt_name *fname, struct page **res_page)
J
Jaegeuk Kim 已提交
220 221 222 223 224
{
	unsigned long npages = dir_blocks(dir);
	struct f2fs_dir_entry *de = NULL;
	unsigned int max_depth;
	unsigned int level;
225 226

	if (f2fs_has_inline_dentry(dir)) {
227
		*res_page = NULL;
228
		de = find_in_inline_dir(dir, fname, res_page);
229 230
		goto out;
	}
C
Chao Yu 已提交
231

232 233
	if (npages == 0) {
		*res_page = NULL;
234
		goto out;
235
	}
J
Jaegeuk Kim 已提交
236 237

	max_depth = F2FS_I(dir)->i_current_depth;
238 239 240 241 242
	if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
		f2fs_msg(F2FS_I_SB(dir)->sb, KERN_WARNING,
				"Corrupted max_depth of %lu: %u",
				dir->i_ino, max_depth);
		max_depth = MAX_DIR_HASH_DEPTH;
243
		f2fs_i_depth_write(dir, max_depth);
244
	}
J
Jaegeuk Kim 已提交
245 246

	for (level = 0; level < max_depth; level++) {
247
		*res_page = NULL;
248
		de = find_in_level(dir, level, fname, res_page);
249
		if (de || IS_ERR(*res_page))
J
Jaegeuk Kim 已提交
250 251
			break;
	}
252
out:
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	return de;
}

/*
 * Find an entry in the specified directory with the wanted name.
 * It returns the page where the entry was found (as a parameter - res_page),
 * and the entry itself. Page is returned mapped and unlocked.
 * Entry is guaranteed to be valid.
 */
struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
			const struct qstr *child, struct page **res_page)
{
	struct f2fs_dir_entry *de = NULL;
	struct fscrypt_name fname;
	int err;

	err = fscrypt_setup_filename(dir, child, 1, &fname);
	if (err) {
		*res_page = ERR_PTR(err);
		return NULL;
	}

	de = __f2fs_find_entry(dir, &fname, res_page);

277
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
278 279 280 281 282
	return de;
}

struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
{
S
Sheng Yong 已提交
283
	struct qstr dotdot = QSTR_INIT("..", 2);
C
Chao Yu 已提交
284

S
Sheng Yong 已提交
285
	return f2fs_find_entry(dir, &dotdot, p);
J
Jaegeuk Kim 已提交
286 287
}

288
ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
289
							struct page **page)
J
Jaegeuk Kim 已提交
290 291 292 293
{
	ino_t res = 0;
	struct f2fs_dir_entry *de;

294
	de = f2fs_find_entry(dir, qstr, page);
J
Jaegeuk Kim 已提交
295 296
	if (de) {
		res = le32_to_cpu(de->ino);
297 298
		f2fs_dentry_kunmap(dir, *page);
		f2fs_put_page(*page, 0);
J
Jaegeuk Kim 已提交
299 300 301 302 303 304 305 306
	}

	return res;
}

void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
		struct page *page, struct inode *inode)
{
J
Jaegeuk Kim 已提交
307
	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
J
Jaegeuk Kim 已提交
308
	lock_page(page);
309
	f2fs_wait_on_page_writeback(page, type, true);
J
Jaegeuk Kim 已提交
310
	de->ino = cpu_to_le32(inode->i_ino);
311
	set_de_type(de, inode->i_mode);
312
	f2fs_dentry_kunmap(dir, page);
J
Jaegeuk Kim 已提交
313
	set_page_dirty(page);
314

315
	dir->i_mtime = dir->i_ctime = current_time(dir);
316
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
317 318 319
	f2fs_put_page(page, 1);
}

320
static void init_dent_inode(const struct qstr *name, struct page *ipage)
J
Jaegeuk Kim 已提交
321
{
322
	struct f2fs_inode *ri;
J
Jaegeuk Kim 已提交
323

324
	f2fs_wait_on_page_writeback(ipage, NODE, true);
325

326
	/* copy name info. to this inode page */
327 328 329
	ri = F2FS_INODE(ipage);
	ri->i_namelen = cpu_to_le32(name->len);
	memcpy(ri->i_name, name->name, name->len);
J
Jaegeuk Kim 已提交
330 331 332
	set_page_dirty(ipage);
}

333 334
int update_dent_inode(struct inode *inode, struct inode *to,
					const struct qstr *name)
335 336 337
{
	struct page *page;

338 339 340
	if (file_enc_name(to))
		return 0;

341
	page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
342 343 344 345 346 347 348 349 350
	if (IS_ERR(page))
		return PTR_ERR(page);

	init_dent_inode(name, page);
	f2fs_put_page(page, 1);

	return 0;
}

351 352 353
void do_make_empty_dir(struct inode *inode, struct inode *parent,
					struct f2fs_dentry_ptr *d)
{
354 355
	struct qstr dot = QSTR_INIT(".", 1);
	struct qstr dotdot = QSTR_INIT("..", 2);
356

357 358
	/* update dirent of "." */
	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
359

360 361
	/* update dirent of ".." */
	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
362 363
}

364 365
static int make_empty_dir(struct inode *inode,
		struct inode *parent, struct page *page)
366 367 368
{
	struct page *dentry_page;
	struct f2fs_dentry_block *dentry_blk;
369
	struct f2fs_dentry_ptr d;
370

C
Chao Yu 已提交
371 372 373
	if (f2fs_has_inline_dentry(inode))
		return make_empty_inline_dir(inode, parent, page);

374
	dentry_page = get_new_data_page(inode, page, 0, true);
375 376 377
	if (IS_ERR(dentry_page))
		return PTR_ERR(dentry_page);

G
Gu Zheng 已提交
378
	dentry_blk = kmap_atomic(dentry_page);
379

380
	make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
381
	do_make_empty_dir(inode, parent, &d);
382

G
Gu Zheng 已提交
383
	kunmap_atomic(dentry_blk);
384 385 386 387 388 389

	set_page_dirty(dentry_page);
	f2fs_put_page(dentry_page, 1);
	return 0;
}

390
struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
391 392
			const struct qstr *new_name, const struct qstr *orig_name,
			struct page *dpage)
J
Jaegeuk Kim 已提交
393
{
394 395 396
	struct page *page;
	int err;

397
	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
398
		page = new_inode_page(inode);
399 400
		if (IS_ERR(page))
			return page;
J
Jaegeuk Kim 已提交
401 402

		if (S_ISDIR(inode->i_mode)) {
J
Jaegeuk Kim 已提交
403 404
			/* in order to handle error case */
			get_page(page);
405
			err = make_empty_dir(inode, dir, page);
J
Jaegeuk Kim 已提交
406 407 408 409 410
			if (err) {
				lock_page(page);
				goto put_error;
			}
			put_page(page);
J
Jaegeuk Kim 已提交
411 412
		}

413
		err = f2fs_init_acl(inode, dir, page, dpage);
414
		if (err)
415
			goto put_error;
416

417
		err = f2fs_init_security(inode, dir, orig_name, page);
418
		if (err)
419
			goto put_error;
420 421

		if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
422
			err = fscrypt_inherit_context(dir, inode, page, false);
423 424 425
			if (err)
				goto put_error;
		}
J
Jaegeuk Kim 已提交
426
	} else {
427
		page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
428 429 430 431
		if (IS_ERR(page))
			return page;

		set_cold_node(inode, page);
J
Jaegeuk Kim 已提交
432
	}
433

434 435
	if (new_name)
		init_dent_inode(new_name, page);
436

437 438 439 440
	/*
	 * This file should be checkpointed during fsync.
	 * We lost i_pino from now on.
	 */
441
	if (is_inode_flag_set(inode, FI_INC_LINK)) {
442
		file_lost_pino(inode);
C
Chao Yu 已提交
443 444 445 446 447
		/*
		 * If link the tmpfile to alias through linkat path,
		 * we should remove this inode from orphan list.
		 */
		if (inode->i_nlink == 0)
448
			remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
449
		f2fs_i_links_write(inode, true);
J
Jaegeuk Kim 已提交
450
	}
451 452
	return page;

453
put_error:
J
Jaegeuk Kim 已提交
454 455 456
	clear_nlink(inode);
	update_inode(inode, page);
	f2fs_put_page(page, 1);
457
	return ERR_PTR(err);
J
Jaegeuk Kim 已提交
458 459
}

460
void update_parent_metadata(struct inode *dir, struct inode *inode,
J
Jaegeuk Kim 已提交
461 462
						unsigned int current_depth)
{
463
	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
464
		if (S_ISDIR(inode->i_mode))
465
			f2fs_i_links_write(dir, true);
466
		clear_inode_flag(inode, FI_NEW_INODE);
J
Jaegeuk Kim 已提交
467
	}
468
	dir->i_mtime = dir->i_ctime = current_time(dir);
469
	f2fs_mark_inode_dirty_sync(dir, false);
470

471
	if (F2FS_I(dir)->i_current_depth != current_depth)
472
		f2fs_i_depth_write(dir, current_depth);
J
Jaegeuk Kim 已提交
473

474 475
	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
		clear_inode_flag(inode, FI_INC_LINK);
J
Jaegeuk Kim 已提交
476 477
}

478
int room_for_filename(const void *bitmap, int slots, int max_slots)
J
Jaegeuk Kim 已提交
479 480 481 482
{
	int bit_start = 0;
	int zero_start, zero_end;
next:
483 484 485 486 487
	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
	if (zero_start >= max_slots)
		return max_slots;

	zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
J
Jaegeuk Kim 已提交
488 489 490 491 492
	if (zero_end - zero_start >= slots)
		return zero_start;

	bit_start = zero_end + 1;

493 494
	if (zero_end + 1 >= max_slots)
		return max_slots;
J
Jaegeuk Kim 已提交
495 496 497
	goto next;
}

498
void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
499 500 501 502 503 504 505 506 507 508 509
				const struct qstr *name, f2fs_hash_t name_hash,
				unsigned int bit_pos)
{
	struct f2fs_dir_entry *de;
	int slots = GET_DENTRY_SLOTS(name->len);
	int i;

	de = &d->dentry[bit_pos];
	de->hash_code = name_hash;
	de->name_len = cpu_to_le16(name->len);
	memcpy(d->filename[bit_pos], name->name, name->len);
510 511
	de->ino = cpu_to_le32(ino);
	set_de_type(de, mode);
512
	for (i = 0; i < slots; i++) {
513
		__set_bit_le(bit_pos + i, (void *)d->bitmap);
514 515 516 517
		/* avoid wrong garbage data for readdir */
		if (i)
			(de + i)->name_len = 0;
	}
518 519
}

520
int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
521
				const struct qstr *orig_name,
522
				struct inode *inode, nid_t ino, umode_t mode)
J
Jaegeuk Kim 已提交
523 524 525 526 527 528 529 530 531
{
	unsigned int bit_pos;
	unsigned int level;
	unsigned int current_depth;
	unsigned long bidx, block;
	f2fs_hash_t dentry_hash;
	unsigned int nbucket, nblock;
	struct page *dentry_page = NULL;
	struct f2fs_dentry_block *dentry_blk = NULL;
532
	struct f2fs_dentry_ptr d;
533
	struct page *page = NULL;
534
	int slots, err = 0;
C
Chao Yu 已提交
535

J
Jaegeuk Kim 已提交
536
	level = 0;
537 538
	slots = GET_DENTRY_SLOTS(new_name->len);
	dentry_hash = f2fs_dentry_hash(new_name);
539

J
Jaegeuk Kim 已提交
540 541 542 543 544 545 546
	current_depth = F2FS_I(dir)->i_current_depth;
	if (F2FS_I(dir)->chash == dentry_hash) {
		level = F2FS_I(dir)->clevel;
		F2FS_I(dir)->chash = 0;
	}

start:
J
Jaegeuk Kim 已提交
547
#ifdef CONFIG_F2FS_FAULT_INJECTION
548
	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
J
Jaegeuk Kim 已提交
549 550
		return -ENOSPC;
#endif
551 552
	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
		return -ENOSPC;
J
Jaegeuk Kim 已提交
553 554 555 556 557

	/* Increase the depth, if required */
	if (level == current_depth)
		++current_depth;

558
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
559 560
	nblock = bucket_blocks(level);

561 562
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
				(le32_to_cpu(dentry_hash) % nbucket));
J
Jaegeuk Kim 已提交
563 564

	for (block = bidx; block <= (bidx + nblock - 1); block++) {
565
		dentry_page = get_new_data_page(dir, NULL, block, true);
566 567
		if (IS_ERR(dentry_page))
			return PTR_ERR(dentry_page);
J
Jaegeuk Kim 已提交
568 569

		dentry_blk = kmap(dentry_page);
570 571
		bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
						slots, NR_DENTRY_IN_BLOCK);
J
Jaegeuk Kim 已提交
572 573 574 575 576 577 578 579 580 581 582
		if (bit_pos < NR_DENTRY_IN_BLOCK)
			goto add_dentry;

		kunmap(dentry_page);
		f2fs_put_page(dentry_page, 1);
	}

	/* Move to next level to find the empty slot for new dentry */
	++level;
	goto start;
add_dentry:
583
	f2fs_wait_on_page_writeback(dentry_page, DATA, true);
J
Jaegeuk Kim 已提交
584

585 586
	if (inode) {
		down_write(&F2FS_I(inode)->i_sem);
587 588
		page = init_inode_metadata(inode, dir, new_name,
						orig_name, NULL);
589 590 591 592
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto fail;
		}
593 594
		if (f2fs_encrypted_inode(dir))
			file_set_enc_name(inode);
595
	}
596

597
	make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
598
	f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
599

J
Jaegeuk Kim 已提交
600
	set_page_dirty(dentry_page);
601

602
	if (inode) {
603
		f2fs_i_pino_write(inode, dir->i_ino);
604 605
		f2fs_put_page(page, 1);
	}
606 607

	update_parent_metadata(dir, inode, current_depth);
J
Jaegeuk Kim 已提交
608
fail:
609 610
	if (inode)
		up_write(&F2FS_I(inode)->i_sem);
611

J
Jaegeuk Kim 已提交
612 613
	kunmap(dentry_page);
	f2fs_put_page(dentry_page, 1);
614 615 616 617

	return err;
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
				struct inode *inode, nid_t ino, umode_t mode)
{
	struct qstr new_name;
	int err = -EAGAIN;

	new_name.name = fname_name(fname);
	new_name.len = fname_len(fname);

	if (f2fs_has_inline_dentry(dir))
		err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
							inode, ino, mode);
	if (err == -EAGAIN)
		err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
							inode, ino, mode);

	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
	return err;
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651
/*
 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
 * f2fs_unlock_op().
 */
int __f2fs_add_link(struct inode *dir, const struct qstr *name,
				struct inode *inode, nid_t ino, umode_t mode)
{
	struct fscrypt_name fname;
	int err;

	err = fscrypt_setup_filename(dir, name, 0, &fname);
	if (err)
		return err;

652
	err = __f2fs_do_add_link(dir, &fname, inode, ino, mode);
653

654
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
655 656 657
	return err;
}

658 659 660 661 662 663
int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
{
	struct page *page;
	int err = 0;

	down_write(&F2FS_I(inode)->i_sem);
664
	page = init_inode_metadata(inode, dir, NULL, NULL, NULL);
665 666 667 668 669 670
	if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto fail;
	}
	f2fs_put_page(page, 1);

671
	clear_inode_flag(inode, FI_NEW_INODE);
672 673
fail:
	up_write(&F2FS_I(inode)->i_sem);
674
	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
675 676 677
	return err;
}

J
Jaegeuk Kim 已提交
678
void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
679 680 681 682 683
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

	down_write(&F2FS_I(inode)->i_sem);

684
	if (S_ISDIR(inode->i_mode))
685
		f2fs_i_links_write(dir, false);
686
	inode->i_ctime = current_time(inode);
687

688
	f2fs_i_links_write(inode, false);
689
	if (S_ISDIR(inode->i_mode)) {
690
		f2fs_i_links_write(inode, false);
691
		f2fs_i_size_write(inode, 0);
692 693 694 695
	}
	up_write(&F2FS_I(inode)->i_sem);

	if (inode->i_nlink == 0)
696
		add_orphan_inode(inode);
697 698 699 700
	else
		release_orphan_inode(sbi);
}

J
Jaegeuk Kim 已提交
701
/*
A
arter97 已提交
702
 * It only removes the dentry from the dentry page, corresponding name
J
Jaegeuk Kim 已提交
703 704 705
 * entry in name page does not need to be touched during deletion.
 */
void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
706
					struct inode *dir, struct inode *inode)
J
Jaegeuk Kim 已提交
707 708 709
{
	struct	f2fs_dentry_block *dentry_blk;
	unsigned int bit_pos;
710
	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
J
Jaegeuk Kim 已提交
711 712
	int i;

713 714
	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);

C
Chao Yu 已提交
715 716 717
	if (f2fs_has_inline_dentry(dir))
		return f2fs_delete_inline_entry(dentry, page, dir, inode);

J
Jaegeuk Kim 已提交
718
	lock_page(page);
719
	f2fs_wait_on_page_writeback(page, DATA, true);
J
Jaegeuk Kim 已提交
720

G
Gu Zheng 已提交
721 722
	dentry_blk = page_address(page);
	bit_pos = dentry - dentry_blk->dentry;
J
Jaegeuk Kim 已提交
723
	for (i = 0; i < slots; i++)
724
		clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
J
Jaegeuk Kim 已提交
725 726 727 728 729 730 731 732

	/* Let's check and deallocate this dentry page */
	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
			NR_DENTRY_IN_BLOCK,
			0);
	kunmap(page); /* kunmap - pair of f2fs_find_entry */
	set_page_dirty(page);

733
	dir->i_ctime = dir->i_mtime = current_time(dir);
734
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
735

736
	if (inode)
J
Jaegeuk Kim 已提交
737
		f2fs_drop_nlink(dir, inode);
J
Jaegeuk Kim 已提交
738

C
Chao Yu 已提交
739 740
	if (bit_pos == NR_DENTRY_IN_BLOCK &&
			!truncate_hole(dir, page->index, page->index + 1)) {
J
Jaegeuk Kim 已提交
741
		clear_page_dirty_for_io(page);
742
		ClearPagePrivate(page);
J
Jaegeuk Kim 已提交
743
		ClearPageUptodate(page);
744
		inode_dec_dirty_pages(dir);
745
		remove_dirty_inode(dir);
J
Jaegeuk Kim 已提交
746
	}
747
	f2fs_put_page(page, 1);
J
Jaegeuk Kim 已提交
748 749 750 751 752 753 754
}

bool f2fs_empty_dir(struct inode *dir)
{
	unsigned long bidx;
	struct page *dentry_page;
	unsigned int bit_pos;
755
	struct f2fs_dentry_block *dentry_blk;
J
Jaegeuk Kim 已提交
756 757
	unsigned long nblock = dir_blocks(dir);

C
Chao Yu 已提交
758 759 760
	if (f2fs_has_inline_dentry(dir))
		return f2fs_empty_inline_dir(dir);

J
Jaegeuk Kim 已提交
761
	for (bidx = 0; bidx < nblock; bidx++) {
762
		dentry_page = get_lock_data_page(dir, bidx, false);
J
Jaegeuk Kim 已提交
763 764 765 766 767 768 769
		if (IS_ERR(dentry_page)) {
			if (PTR_ERR(dentry_page) == -ENOENT)
				continue;
			else
				return false;
		}

G
Gu Zheng 已提交
770
		dentry_blk = kmap_atomic(dentry_page);
J
Jaegeuk Kim 已提交
771 772 773 774 775 776 777
		if (bidx == 0)
			bit_pos = 2;
		else
			bit_pos = 0;
		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
						NR_DENTRY_IN_BLOCK,
						bit_pos);
G
Gu Zheng 已提交
778
		kunmap_atomic(dentry_blk);
J
Jaegeuk Kim 已提交
779 780 781 782 783 784 785 786 787

		f2fs_put_page(dentry_page, 1);

		if (bit_pos < NR_DENTRY_IN_BLOCK)
			return false;
	}
	return true;
}

C
Chao Yu 已提交
788
int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
789
			unsigned int start_pos, struct fscrypt_str *fstr)
790 791 792 793
{
	unsigned char d_type = DT_UNKNOWN;
	unsigned int bit_pos;
	struct f2fs_dir_entry *de = NULL;
794
	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
795

796
	bit_pos = ((unsigned long)ctx->pos % d->max);
797

798 799 800
	while (bit_pos < d->max) {
		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
		if (bit_pos >= d->max)
801 802
			break;

803
		de = &d->dentry[bit_pos];
804 805 806 807 808 809
		if (de->name_len == 0) {
			bit_pos++;
			ctx->pos = start_pos + bit_pos;
			continue;
		}

810
		d_type = get_de_type(de);
811 812 813 814 815 816

		de_name.name = d->filename[bit_pos];
		de_name.len = le16_to_cpu(de->name_len);

		if (f2fs_encrypted_inode(d->inode)) {
			int save_len = fstr->len;
817
			int err;
818

819
			err = fscrypt_fname_disk_to_usr(d->inode,
820 821
						(u32)de->hash_code, 0,
						&de_name, fstr);
822
			if (err)
C
Chao Yu 已提交
823
				return err;
824 825 826

			de_name = *fstr;
			fstr->len = save_len;
827 828 829
		}

		if (!dir_emit(ctx, de_name.name, de_name.len,
830
					le32_to_cpu(de->ino), d_type))
C
Chao Yu 已提交
831
			return 1;
832 833 834 835

		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
		ctx->pos = start_pos + bit_pos;
	}
C
Chao Yu 已提交
836
	return 0;
837 838
}

A
Al Viro 已提交
839
static int f2fs_readdir(struct file *file, struct dir_context *ctx)
J
Jaegeuk Kim 已提交
840
{
A
Al Viro 已提交
841
	struct inode *inode = file_inode(file);
J
Jaegeuk Kim 已提交
842 843 844
	unsigned long npages = dir_blocks(inode);
	struct f2fs_dentry_block *dentry_blk = NULL;
	struct page *dentry_page = NULL;
845
	struct file_ra_state *ra = &file->f_ra;
A
Al Viro 已提交
846
	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
847
	struct f2fs_dentry_ptr d;
848
	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
849
	int err = 0;
J
Jaegeuk Kim 已提交
850

851
	if (f2fs_encrypted_inode(inode)) {
852
		err = fscrypt_get_encryption_info(inode);
853
		if (err && err != -ENOKEY)
854 855
			return err;

856
		err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
857 858 859 860 861 862 863 864
		if (err < 0)
			return err;
	}

	if (f2fs_has_inline_dentry(inode)) {
		err = f2fs_read_inline_dir(file, ctx, &fstr);
		goto out;
	}
C
Chao Yu 已提交
865

866 867 868 869 870
	/* readahead for multi pages of dir */
	if (npages - n > 1 && !ra_has_index(ra, n))
		page_cache_sync_readahead(inode->i_mapping, ra, file, n,
				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));

C
Chris Fries 已提交
871
	for (; n < npages; n++) {
872
		dentry_page = get_lock_data_page(inode, n, false);
873 874
		if (IS_ERR(dentry_page)) {
			err = PTR_ERR(dentry_page);
C
Chao Yu 已提交
875 876
			if (err == -ENOENT) {
				err = 0;
877
				continue;
C
Chao Yu 已提交
878
			} else {
879
				goto out;
C
Chao Yu 已提交
880
			}
881
		}
J
Jaegeuk Kim 已提交
882 883

		dentry_blk = kmap(dentry_page);
J
Jaegeuk Kim 已提交
884

885
		make_dentry_ptr(inode, &d, (void *)dentry_blk, 1);
886

C
Chao Yu 已提交
887 888 889
		err = f2fs_fill_dentries(ctx, &d,
				n * NR_DENTRY_IN_BLOCK, &fstr);
		if (err) {
890 891 892 893
			kunmap(dentry_page);
			f2fs_put_page(dentry_page, 1);
			break;
		}
894

A
Al Viro 已提交
895
		ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
J
Jaegeuk Kim 已提交
896 897 898
		kunmap(dentry_page);
		f2fs_put_page(dentry_page, 1);
	}
899
out:
900
	fscrypt_fname_free_buffer(&fstr);
C
Chao Yu 已提交
901
	return err < 0 ? err : 0;
J
Jaegeuk Kim 已提交
902 903
}

904 905 906
static int f2fs_dir_open(struct inode *inode, struct file *filp)
{
	if (f2fs_encrypted_inode(inode))
907
		return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
908 909 910
	return 0;
}

J
Jaegeuk Kim 已提交
911 912 913
const struct file_operations f2fs_dir_operations = {
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
A
Al Viro 已提交
914
	.iterate_shared	= f2fs_readdir,
J
Jaegeuk Kim 已提交
915
	.fsync		= f2fs_sync_file,
916
	.open		= f2fs_dir_open,
J
Jaegeuk Kim 已提交
917
	.unlocked_ioctl	= f2fs_ioctl,
918 919 920
#ifdef CONFIG_COMPAT
	.compat_ioctl   = f2fs_compat_ioctl,
#endif
J
Jaegeuk Kim 已提交
921
};