dir.c 22.8 KB
Newer Older
C
Chao Yu 已提交
1
// SPDX-License-Identifier: GPL-2.0
J
Jaegeuk Kim 已提交
2
/*
J
Jaegeuk Kim 已提交
3 4 5 6 7 8 9
 * fs/f2fs/dir.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 */
#include <linux/fs.h>
#include <linux/f2fs_fs.h>
C
Chao Yu 已提交
10
#include <linux/sched/signal.h>
J
Jaegeuk Kim 已提交
11
#include "f2fs.h"
12
#include "node.h"
J
Jaegeuk Kim 已提交
13
#include "acl.h"
14
#include "xattr.h"
C
Chao Yu 已提交
15
#include <trace/events/f2fs.h>
J
Jaegeuk Kim 已提交
16 17 18

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

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

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

C
Chao Yu 已提交
39
static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
J
Jaegeuk Kim 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	[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,
};

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

C
Chao Yu 已提交
65
unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
66 67 68 69 70 71
{
	if (de->file_type < F2FS_FT_MAX)
		return f2fs_filetype_table[de->file_type];
	return DT_UNKNOWN;
}

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

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

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

94
	dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
95

96
	make_dentry_ptr_block(NULL, &d, dentry_blk);
C
Chao Yu 已提交
97
	de = f2fs_find_target_dentry(fname, namehash, max_slots, &d);
98 99 100 101 102 103
	if (de)
		*res_page = dentry_page;

	return de;
}

C
Chao Yu 已提交
104
struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
105 106
			f2fs_hash_t namehash, int *max_slots,
			struct f2fs_dentry_ptr *d)
J
Jaegeuk Kim 已提交
107 108
{
	struct f2fs_dir_entry *de;
109 110
	unsigned long bit_pos = 0;
	int max_len = 0;
J
Jaegeuk Kim 已提交
111

112 113 114 115
	if (max_slots)
		*max_slots = 0;
	while (bit_pos < d->max) {
		if (!test_bit_le(bit_pos, d->bitmap)) {
116
			bit_pos++;
117
			max_len++;
118 119
			continue;
		}
120

121
		de = &d->dentry[bit_pos];
122

123 124 125 126 127
		if (unlikely(!de->name_len)) {
			bit_pos++;
			continue;
		}

128 129 130
		if (de->hash_code == namehash &&
		    fscrypt_match_name(fname, d->filename[bit_pos],
				       le16_to_cpu(de->name_len)))
131
			goto found;
132

133
		if (max_slots && max_len > *max_slots)
134
			*max_slots = max_len;
135
		max_len = 0;
136

137
		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
J
Jaegeuk Kim 已提交
138 139 140 141
	}

	de = NULL;
found:
142
	if (max_slots && max_len > *max_slots)
143
		*max_slots = max_len;
J
Jaegeuk Kim 已提交
144 145 146 147
	return de;
}

static struct f2fs_dir_entry *find_in_level(struct inode *dir,
148
					unsigned int level,
149
					struct fscrypt_name *fname,
150
					struct page **res_page)
J
Jaegeuk Kim 已提交
151
{
152 153
	struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
	int s = GET_DENTRY_SLOTS(name.len);
J
Jaegeuk Kim 已提交
154 155 156 157 158
	unsigned int nbucket, nblock;
	unsigned int bidx, end_block;
	struct page *dentry_page;
	struct f2fs_dir_entry *de = NULL;
	bool room = false;
159
	int max_slots;
160
	f2fs_hash_t namehash = f2fs_dentry_hash(&name, fname);
J
Jaegeuk Kim 已提交
161

162
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
163 164
	nblock = bucket_blocks(level);

165 166
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
					le32_to_cpu(namehash) % nbucket);
J
Jaegeuk Kim 已提交
167 168 169 170
	end_block = bidx + nblock;

	for (; bidx < end_block; bidx++) {
		/* no need to allocate new dentry pages to all the indices */
C
Chao Yu 已提交
171
		dentry_page = f2fs_find_data_page(dir, bidx);
J
Jaegeuk Kim 已提交
172
		if (IS_ERR(dentry_page)) {
173 174 175 176 177 178 179
			if (PTR_ERR(dentry_page) == -ENOENT) {
				room = true;
				continue;
			} else {
				*res_page = dentry_page;
				break;
			}
J
Jaegeuk Kim 已提交
180 181
		}

182 183
		de = find_in_block(dentry_page, fname, namehash, &max_slots,
								res_page);
J
Jaegeuk Kim 已提交
184 185 186 187 188 189 190 191
		if (de)
			break;

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

192 193 194
	if (!de && room && F2FS_I(dir)->chash != namehash) {
		F2FS_I(dir)->chash = namehash;
		F2FS_I(dir)->clevel = level;
J
Jaegeuk Kim 已提交
195 196 197 198 199
	}

	return de;
}

200 201
struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
			struct fscrypt_name *fname, struct page **res_page)
J
Jaegeuk Kim 已提交
202 203 204 205 206
{
	unsigned long npages = dir_blocks(dir);
	struct f2fs_dir_entry *de = NULL;
	unsigned int max_depth;
	unsigned int level;
207 208

	if (f2fs_has_inline_dentry(dir)) {
209
		*res_page = NULL;
C
Chao Yu 已提交
210
		de = f2fs_find_in_inline_dir(dir, fname, res_page);
211 212
		goto out;
	}
C
Chao Yu 已提交
213

214 215
	if (npages == 0) {
		*res_page = NULL;
216
		goto out;
217
	}
J
Jaegeuk Kim 已提交
218 219

	max_depth = F2FS_I(dir)->i_current_depth;
220 221 222 223 224
	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;
225
		f2fs_i_depth_write(dir, max_depth);
226
	}
J
Jaegeuk Kim 已提交
227 228

	for (level = 0; level < max_depth; level++) {
229
		*res_page = NULL;
230
		de = find_in_level(dir, level, fname, res_page);
231
		if (de || IS_ERR(*res_page))
J
Jaegeuk Kim 已提交
232 233
			break;
	}
234
out:
235 236 237
	/* This is to increase the speed of f2fs_create */
	if (!de)
		F2FS_I(dir)->task = current;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	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) {
256 257 258 259
		if (err == -ENOENT)
			*res_page = NULL;
		else
			*res_page = ERR_PTR(err);
260 261 262 263 264
		return NULL;
	}

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

265
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
266 267 268 269 270
	return de;
}

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

S
Sheng Yong 已提交
273
	return f2fs_find_entry(dir, &dotdot, p);
J
Jaegeuk Kim 已提交
274 275
}

276
ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
277
							struct page **page)
J
Jaegeuk Kim 已提交
278 279 280 281
{
	ino_t res = 0;
	struct f2fs_dir_entry *de;

282
	de = f2fs_find_entry(dir, qstr, page);
J
Jaegeuk Kim 已提交
283 284
	if (de) {
		res = le32_to_cpu(de->ino);
285
		f2fs_put_page(*page, 0);
J
Jaegeuk Kim 已提交
286 287 288 289 290 291 292 293
	}

	return res;
}

void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
		struct page *page, struct inode *inode)
{
J
Jaegeuk Kim 已提交
294
	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
J
Jaegeuk Kim 已提交
295
	lock_page(page);
296
	f2fs_wait_on_page_writeback(page, type, true, true);
J
Jaegeuk Kim 已提交
297
	de->ino = cpu_to_le32(inode->i_ino);
298
	set_de_type(de, inode->i_mode);
J
Jaegeuk Kim 已提交
299
	set_page_dirty(page);
300

301
	dir->i_mtime = dir->i_ctime = current_time(dir);
302
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
303 304 305
	f2fs_put_page(page, 1);
}

306
static void init_dent_inode(const struct qstr *name, struct page *ipage)
J
Jaegeuk Kim 已提交
307
{
308
	struct f2fs_inode *ri;
J
Jaegeuk Kim 已提交
309

310
	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
311

312
	/* copy name info. to this inode page */
313 314 315
	ri = F2FS_INODE(ipage);
	ri->i_namelen = cpu_to_le32(name->len);
	memcpy(ri->i_name, name->name, name->len);
J
Jaegeuk Kim 已提交
316 317 318
	set_page_dirty(ipage);
}

C
Chao Yu 已提交
319
void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
320 321
					struct f2fs_dentry_ptr *d)
{
322 323
	struct qstr dot = QSTR_INIT(".", 1);
	struct qstr dotdot = QSTR_INIT("..", 2);
324

325 326
	/* update dirent of "." */
	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
327

328 329
	/* update dirent of ".." */
	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
330 331
}

332 333
static int make_empty_dir(struct inode *inode,
		struct inode *parent, struct page *page)
334 335 336
{
	struct page *dentry_page;
	struct f2fs_dentry_block *dentry_blk;
337
	struct f2fs_dentry_ptr d;
338

C
Chao Yu 已提交
339
	if (f2fs_has_inline_dentry(inode))
C
Chao Yu 已提交
340
		return f2fs_make_empty_inline_dir(inode, parent, page);
C
Chao Yu 已提交
341

C
Chao Yu 已提交
342
	dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
343 344 345
	if (IS_ERR(dentry_page))
		return PTR_ERR(dentry_page);

346
	dentry_blk = page_address(dentry_page);
347

348
	make_dentry_ptr_block(NULL, &d, dentry_blk);
C
Chao Yu 已提交
349
	f2fs_do_make_empty_dir(inode, parent, &d);
350 351 352 353 354 355

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

C
Chao Yu 已提交
356
struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
357 358
			const struct qstr *new_name, const struct qstr *orig_name,
			struct page *dpage)
J
Jaegeuk Kim 已提交
359
{
360
	struct page *page;
361
	int dummy_encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
362 363
	int err;

364
	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
C
Chao Yu 已提交
365
		page = f2fs_new_inode_page(inode);
366 367
		if (IS_ERR(page))
			return page;
J
Jaegeuk Kim 已提交
368 369

		if (S_ISDIR(inode->i_mode)) {
J
Jaegeuk Kim 已提交
370 371
			/* in order to handle error case */
			get_page(page);
372
			err = make_empty_dir(inode, dir, page);
J
Jaegeuk Kim 已提交
373 374 375 376 377
			if (err) {
				lock_page(page);
				goto put_error;
			}
			put_page(page);
J
Jaegeuk Kim 已提交
378 379
		}

380
		err = f2fs_init_acl(inode, dir, page, dpage);
381
		if (err)
382
			goto put_error;
383

384
		err = f2fs_init_security(inode, dir, orig_name, page);
385
		if (err)
386
			goto put_error;
387

388 389
		if ((f2fs_encrypted_inode(dir) || dummy_encrypt) &&
					f2fs_may_encrypt(inode)) {
390
			err = fscrypt_inherit_context(dir, inode, page, false);
391 392 393
			if (err)
				goto put_error;
		}
J
Jaegeuk Kim 已提交
394
	} else {
C
Chao Yu 已提交
395
		page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
396 397
		if (IS_ERR(page))
			return page;
J
Jaegeuk Kim 已提交
398
	}
399

400
	if (new_name) {
401
		init_dent_inode(new_name, page);
402 403 404
		if (f2fs_encrypted_inode(dir))
			file_set_enc_name(inode);
	}
405

406 407 408 409
	/*
	 * This file should be checkpointed during fsync.
	 * We lost i_pino from now on.
	 */
410
	if (is_inode_flag_set(inode, FI_INC_LINK)) {
411 412
		if (!S_ISDIR(inode->i_mode))
			file_lost_pino(inode);
C
Chao Yu 已提交
413 414 415 416 417
		/*
		 * If link the tmpfile to alias through linkat path,
		 * we should remove this inode from orphan list.
		 */
		if (inode->i_nlink == 0)
C
Chao Yu 已提交
418
			f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
419
		f2fs_i_links_write(inode, true);
J
Jaegeuk Kim 已提交
420
	}
421 422
	return page;

423
put_error:
J
Jaegeuk Kim 已提交
424
	clear_nlink(inode);
C
Chao Yu 已提交
425
	f2fs_update_inode(inode, page);
J
Jaegeuk Kim 已提交
426
	f2fs_put_page(page, 1);
427
	return ERR_PTR(err);
J
Jaegeuk Kim 已提交
428 429
}

C
Chao Yu 已提交
430
void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
J
Jaegeuk Kim 已提交
431 432
						unsigned int current_depth)
{
433
	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
434
		if (S_ISDIR(inode->i_mode))
435
			f2fs_i_links_write(dir, true);
436
		clear_inode_flag(inode, FI_NEW_INODE);
J
Jaegeuk Kim 已提交
437
	}
438
	dir->i_mtime = dir->i_ctime = current_time(dir);
439
	f2fs_mark_inode_dirty_sync(dir, false);
440

441
	if (F2FS_I(dir)->i_current_depth != current_depth)
442
		f2fs_i_depth_write(dir, current_depth);
J
Jaegeuk Kim 已提交
443

444 445
	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
		clear_inode_flag(inode, FI_INC_LINK);
J
Jaegeuk Kim 已提交
446 447
}

C
Chao Yu 已提交
448
int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
J
Jaegeuk Kim 已提交
449 450 451 452
{
	int bit_start = 0;
	int zero_start, zero_end;
next:
453 454 455 456 457
	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 已提交
458 459 460 461 462
	if (zero_end - zero_start >= slots)
		return zero_start;

	bit_start = zero_end + 1;

463 464
	if (zero_end + 1 >= max_slots)
		return max_slots;
J
Jaegeuk Kim 已提交
465 466 467
	goto next;
}

468
void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
469 470 471 472 473 474 475 476 477 478 479
				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);
480 481
	de->ino = cpu_to_le32(ino);
	set_de_type(de, mode);
482
	for (i = 0; i < slots; i++) {
483
		__set_bit_le(bit_pos + i, (void *)d->bitmap);
484 485 486 487
		/* avoid wrong garbage data for readdir */
		if (i)
			(de + i)->name_len = 0;
	}
488 489
}

490
int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
491
				const struct qstr *orig_name,
492
				struct inode *inode, nid_t ino, umode_t mode)
J
Jaegeuk Kim 已提交
493 494 495 496 497 498 499 500 501
{
	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;
502
	struct f2fs_dentry_ptr d;
503
	struct page *page = NULL;
504
	int slots, err = 0;
C
Chao Yu 已提交
505

J
Jaegeuk Kim 已提交
506
	level = 0;
507
	slots = GET_DENTRY_SLOTS(new_name->len);
508
	dentry_hash = f2fs_dentry_hash(new_name, NULL);
509

J
Jaegeuk Kim 已提交
510 511 512 513 514 515 516
	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:
517 518
	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
		f2fs_show_injection_info(FAULT_DIR_DEPTH);
J
Jaegeuk Kim 已提交
519
		return -ENOSPC;
520
	}
521

522 523
	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
		return -ENOSPC;
J
Jaegeuk Kim 已提交
524 525 526 527 528

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

529
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
530 531
	nblock = bucket_blocks(level);

532 533
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
				(le32_to_cpu(dentry_hash) % nbucket));
J
Jaegeuk Kim 已提交
534 535

	for (block = bidx; block <= (bidx + nblock - 1); block++) {
C
Chao Yu 已提交
536
		dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
537 538
		if (IS_ERR(dentry_page))
			return PTR_ERR(dentry_page);
J
Jaegeuk Kim 已提交
539

540
		dentry_blk = page_address(dentry_page);
C
Chao Yu 已提交
541
		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
542
						slots, NR_DENTRY_IN_BLOCK);
J
Jaegeuk Kim 已提交
543 544 545 546 547 548 549 550 551 552
		if (bit_pos < NR_DENTRY_IN_BLOCK)
			goto add_dentry;

		f2fs_put_page(dentry_page, 1);
	}

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

555 556
	if (inode) {
		down_write(&F2FS_I(inode)->i_sem);
C
Chao Yu 已提交
557
		page = f2fs_init_inode_metadata(inode, dir, new_name,
558
						orig_name, NULL);
559 560 561 562
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto fail;
		}
563
	}
564

565
	make_dentry_ptr_block(NULL, &d, dentry_blk);
566
	f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
567

J
Jaegeuk Kim 已提交
568
	set_page_dirty(dentry_page);
569

570
	if (inode) {
571
		f2fs_i_pino_write(inode, dir->i_ino);
572 573
		f2fs_put_page(page, 1);
	}
574

C
Chao Yu 已提交
575
	f2fs_update_parent_metadata(dir, inode, current_depth);
J
Jaegeuk Kim 已提交
576
fail:
577 578
	if (inode)
		up_write(&F2FS_I(inode)->i_sem);
579

J
Jaegeuk Kim 已提交
580
	f2fs_put_page(dentry_page, 1);
581 582 583 584

	return err;
}

C
Chao Yu 已提交
585
int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
				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;
}

605 606 607 608
/*
 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
 * f2fs_unlock_op().
 */
C
Chao Yu 已提交
609
int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
610 611 612
				struct inode *inode, nid_t ino, umode_t mode)
{
	struct fscrypt_name fname;
613 614
	struct page *page = NULL;
	struct f2fs_dir_entry *de = NULL;
615 616 617 618 619 620
	int err;

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

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	/*
	 * An immature stakable filesystem shows a race condition between lookup
	 * and create. If we have same task when doing lookup and create, it's
	 * definitely fine as expected by VFS normally. Otherwise, let's just
	 * verify on-disk dentry one more time, which guarantees filesystem
	 * consistency more.
	 */
	if (current != F2FS_I(dir)->task) {
		de = __f2fs_find_entry(dir, &fname, &page);
		F2FS_I(dir)->task = NULL;
	}
	if (de) {
		f2fs_put_page(page, 0);
		err = -EEXIST;
	} else if (IS_ERR(page)) {
		err = PTR_ERR(page);
	} else {
C
Chao Yu 已提交
638
		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
639
	}
640
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
641 642 643
	return err;
}

644 645 646 647 648 649
int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
{
	struct page *page;
	int err = 0;

	down_write(&F2FS_I(inode)->i_sem);
C
Chao Yu 已提交
650
	page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
651 652 653 654 655 656
	if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto fail;
	}
	f2fs_put_page(page, 1);

657
	clear_inode_flag(inode, FI_NEW_INODE);
658
	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
659 660 661 662 663
fail:
	up_write(&F2FS_I(inode)->i_sem);
	return err;
}

J
Jaegeuk Kim 已提交
664
void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
665 666 667 668 669
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

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

670
	if (S_ISDIR(inode->i_mode))
671
		f2fs_i_links_write(dir, false);
672
	inode->i_ctime = current_time(inode);
673

674
	f2fs_i_links_write(inode, false);
675
	if (S_ISDIR(inode->i_mode)) {
676
		f2fs_i_links_write(inode, false);
677
		f2fs_i_size_write(inode, 0);
678 679 680 681
	}
	up_write(&F2FS_I(inode)->i_sem);

	if (inode->i_nlink == 0)
C
Chao Yu 已提交
682
		f2fs_add_orphan_inode(inode);
683
	else
C
Chao Yu 已提交
684
		f2fs_release_orphan_inode(sbi);
685 686
}

J
Jaegeuk Kim 已提交
687
/*
A
arter97 已提交
688
 * It only removes the dentry from the dentry page, corresponding name
J
Jaegeuk Kim 已提交
689 690 691
 * entry in name page does not need to be touched during deletion.
 */
void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
692
					struct inode *dir, struct inode *inode)
J
Jaegeuk Kim 已提交
693 694 695
{
	struct	f2fs_dentry_block *dentry_blk;
	unsigned int bit_pos;
696
	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
J
Jaegeuk Kim 已提交
697 698
	int i;

699 700
	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);

701
	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
C
Chao Yu 已提交
702
		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
703

C
Chao Yu 已提交
704 705 706
	if (f2fs_has_inline_dentry(dir))
		return f2fs_delete_inline_entry(dentry, page, dir, inode);

J
Jaegeuk Kim 已提交
707
	lock_page(page);
708
	f2fs_wait_on_page_writeback(page, DATA, true, true);
J
Jaegeuk Kim 已提交
709

G
Gu Zheng 已提交
710 711
	dentry_blk = page_address(page);
	bit_pos = dentry - dentry_blk->dentry;
J
Jaegeuk Kim 已提交
712
	for (i = 0; i < slots; i++)
J
Jaegeuk Kim 已提交
713
		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
J
Jaegeuk Kim 已提交
714 715 716 717 718 719 720

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

721
	dir->i_ctime = dir->i_mtime = current_time(dir);
722
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
723

724
	if (inode)
J
Jaegeuk Kim 已提交
725
		f2fs_drop_nlink(dir, inode);
J
Jaegeuk Kim 已提交
726

C
Chao Yu 已提交
727
	if (bit_pos == NR_DENTRY_IN_BLOCK &&
C
Chao Yu 已提交
728
		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
M
Matthew Wilcox 已提交
729
		f2fs_clear_page_cache_dirty_tag(page);
J
Jaegeuk Kim 已提交
730
		clear_page_dirty_for_io(page);
731
		ClearPagePrivate(page);
J
Jaegeuk Kim 已提交
732
		ClearPageUptodate(page);
C
Chao Yu 已提交
733
		clear_cold_data(page);
734
		inode_dec_dirty_pages(dir);
C
Chao Yu 已提交
735
		f2fs_remove_dirty_inode(dir);
J
Jaegeuk Kim 已提交
736
	}
737
	f2fs_put_page(page, 1);
J
Jaegeuk Kim 已提交
738 739 740 741 742 743 744
}

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

C
Chao Yu 已提交
748 749 750
	if (f2fs_has_inline_dentry(dir))
		return f2fs_empty_inline_dir(dir);

J
Jaegeuk Kim 已提交
751
	for (bidx = 0; bidx < nblock; bidx++) {
C
Chao Yu 已提交
752
		dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
J
Jaegeuk Kim 已提交
753 754 755 756 757 758 759
		if (IS_ERR(dentry_page)) {
			if (PTR_ERR(dentry_page) == -ENOENT)
				continue;
			else
				return false;
		}

760
		dentry_blk = page_address(dentry_page);
J
Jaegeuk Kim 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
		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);

		f2fs_put_page(dentry_page, 1);

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

C
Chao Yu 已提交
777
int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
778
			unsigned int start_pos, struct fscrypt_str *fstr)
779 780 781 782
{
	unsigned char d_type = DT_UNKNOWN;
	unsigned int bit_pos;
	struct f2fs_dir_entry *de = NULL;
783
	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
784
	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
C
Chao Yu 已提交
785 786 787
	struct blk_plug plug;
	bool readdir_ra = sbi->readdir_ra == 1;
	int err = 0;
788

789
	bit_pos = ((unsigned long)ctx->pos % d->max);
790

C
Chao Yu 已提交
791 792 793
	if (readdir_ra)
		blk_start_plug(&plug);

794 795 796
	while (bit_pos < d->max) {
		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
		if (bit_pos >= d->max)
797 798
			break;

799
		de = &d->dentry[bit_pos];
800 801 802
		if (de->name_len == 0) {
			bit_pos++;
			ctx->pos = start_pos + bit_pos;
803 804 805 806
			printk_ratelimited(
				"%s, invalid namelen(0), ino:%u, run fsck to fix.",
				KERN_WARNING, le32_to_cpu(de->ino));
			set_sbi_flag(sbi, SBI_NEED_FSCK);
807 808 809
			continue;
		}

C
Chao Yu 已提交
810
		d_type = f2fs_get_de_type(de);
811 812 813 814

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

815 816
		/* check memory boundary before moving forward */
		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
817 818
		if (unlikely(bit_pos > d->max ||
				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
819 820 821 822 823 824 825 826
			f2fs_msg(sbi->sb, KERN_WARNING,
				"%s: corrupted namelen=%d, run fsck to fix.",
				__func__, le16_to_cpu(de->name_len));
			set_sbi_flag(sbi, SBI_NEED_FSCK);
			err = -EINVAL;
			goto out;
		}

827 828 829
		if (f2fs_encrypted_inode(d->inode)) {
			int save_len = fstr->len;

830
			err = fscrypt_fname_disk_to_usr(d->inode,
831 832
						(u32)de->hash_code, 0,
						&de_name, fstr);
833
			if (err)
C
Chao Yu 已提交
834
				goto out;
835 836 837

			de_name = *fstr;
			fstr->len = save_len;
838 839 840
		}

		if (!dir_emit(ctx, de_name.name, de_name.len,
C
Chao Yu 已提交
841 842 843 844
					le32_to_cpu(de->ino), d_type)) {
			err = 1;
			goto out;
		}
845

C
Chao Yu 已提交
846
		if (readdir_ra)
C
Chao Yu 已提交
847
			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
848

849 850
		ctx->pos = start_pos + bit_pos;
	}
C
Chao Yu 已提交
851 852 853 854
out:
	if (readdir_ra)
		blk_finish_plug(&plug);
	return err;
855 856
}

A
Al Viro 已提交
857
static int f2fs_readdir(struct file *file, struct dir_context *ctx)
J
Jaegeuk Kim 已提交
858
{
A
Al Viro 已提交
859
	struct inode *inode = file_inode(file);
J
Jaegeuk Kim 已提交
860 861 862
	unsigned long npages = dir_blocks(inode);
	struct f2fs_dentry_block *dentry_blk = NULL;
	struct page *dentry_page = NULL;
863
	struct file_ra_state *ra = &file->f_ra;
C
Chao Yu 已提交
864
	loff_t start_pos = ctx->pos;
A
Al Viro 已提交
865
	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
866
	struct f2fs_dentry_ptr d;
867
	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
868
	int err = 0;
J
Jaegeuk Kim 已提交
869

870
	if (f2fs_encrypted_inode(inode)) {
871
		err = fscrypt_get_encryption_info(inode);
872
		if (err && err != -ENOKEY)
C
Chao Yu 已提交
873
			goto out;
874

875
		err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
876
		if (err < 0)
C
Chao Yu 已提交
877
			goto out;
878 879 880 881
	}

	if (f2fs_has_inline_dentry(inode)) {
		err = f2fs_read_inline_dir(file, ctx, &fstr);
C
Chao Yu 已提交
882
		goto out_free;
883
	}
C
Chao Yu 已提交
884

885
	for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
C
Chao Yu 已提交
886 887 888 889 890 891 892 893

		/* allow readdir() to be interrupted */
		if (fatal_signal_pending(current)) {
			err = -ERESTARTSYS;
			goto out_free;
		}
		cond_resched();

894 895 896 897 898
		/* 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));

899
		dentry_page = f2fs_find_data_page(inode, n);
900 901
		if (IS_ERR(dentry_page)) {
			err = PTR_ERR(dentry_page);
C
Chao Yu 已提交
902 903
			if (err == -ENOENT) {
				err = 0;
904
				continue;
C
Chao Yu 已提交
905
			} else {
C
Chao Yu 已提交
906
				goto out_free;
C
Chao Yu 已提交
907
			}
908
		}
J
Jaegeuk Kim 已提交
909

910
		dentry_blk = page_address(dentry_page);
J
Jaegeuk Kim 已提交
911

912
		make_dentry_ptr_block(inode, &d, dentry_blk);
913

C
Chao Yu 已提交
914 915 916
		err = f2fs_fill_dentries(ctx, &d,
				n * NR_DENTRY_IN_BLOCK, &fstr);
		if (err) {
917
			f2fs_put_page(dentry_page, 0);
918 919
			break;
		}
920

921
		f2fs_put_page(dentry_page, 0);
J
Jaegeuk Kim 已提交
922
	}
C
Chao Yu 已提交
923
out_free:
924
	fscrypt_fname_free_buffer(&fstr);
C
Chao Yu 已提交
925 926
out:
	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
C
Chao Yu 已提交
927
	return err < 0 ? err : 0;
J
Jaegeuk Kim 已提交
928 929
}

930 931 932
static int f2fs_dir_open(struct inode *inode, struct file *filp)
{
	if (f2fs_encrypted_inode(inode))
933
		return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
934 935 936
	return 0;
}

J
Jaegeuk Kim 已提交
937 938 939
const struct file_operations f2fs_dir_operations = {
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
A
Al Viro 已提交
940
	.iterate_shared	= f2fs_readdir,
J
Jaegeuk Kim 已提交
941
	.fsync		= f2fs_sync_file,
942
	.open		= f2fs_dir_open,
J
Jaegeuk Kim 已提交
943
	.unlocked_ioctl	= f2fs_ioctl,
944 945 946
#ifdef CONFIG_COMPAT
	.compat_ioctl   = f2fs_compat_ioctl,
#endif
J
Jaegeuk Kim 已提交
947
};