dir.c 27.1 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>
11
#include <linux/unicode.h>
J
Jaegeuk Kim 已提交
12
#include "f2fs.h"
13
#include "node.h"
J
Jaegeuk Kim 已提交
14
#include "acl.h"
15
#include "xattr.h"
C
Chao Yu 已提交
16
#include <trace/events/f2fs.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,
};

C
Chao Yu 已提交
61
static 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];
}

C
Chao Yu 已提交
66
unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
67 68 69 70 71 72
{
	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
	bidx += idx * bucket_blocks(level);
	return bidx;
}

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

96
	dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
97

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

	return de;
}

106 107 108 109 110 111 112 113 114
#ifdef CONFIG_UNICODE
/*
 * Test whether a case-insensitive directory entry matches the filename
 * being searched for.
 *
 * Returns: 0 if the directory entry matches, more than 0 if it
 * doesn't match or less than zero on error.
 */
int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,
115
				const struct qstr *entry, bool quick)
116 117 118 119 120
{
	const struct f2fs_sb_info *sbi = F2FS_SB(parent->i_sb);
	const struct unicode_map *um = sbi->s_encoding;
	int ret;

121 122 123 124 125
	if (quick)
		ret = utf8_strncasecmp_folded(um, name, entry);
	else
		ret = utf8_strncasecmp(um, name, entry);

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	if (ret < 0) {
		/* Handle invalid character sequence as either an error
		 * or as an opaque byte sequence.
		 */
		if (f2fs_has_strict_mode(sbi))
			return -EINVAL;

		if (name->len != entry->len)
			return 1;

		return !!memcmp(name->name, entry->name, name->len);
	}

	return ret;
}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

static void f2fs_fname_setup_ci_filename(struct inode *dir,
					const struct qstr *iname,
					struct fscrypt_str *cf_name)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

	if (!IS_CASEFOLDED(dir)) {
		cf_name->name = NULL;
		return;
	}

	cf_name->name = f2fs_kmalloc(sbi, F2FS_NAME_LEN, GFP_NOFS);
	if (!cf_name->name)
		return;

	cf_name->len = utf8_casefold(sbi->s_encoding,
					iname, cf_name->name,
					F2FS_NAME_LEN);
	if ((int)cf_name->len <= 0) {
		kvfree(cf_name->name);
		cf_name->name = NULL;
	}
}
165 166
#endif

167 168 169
static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d,
					struct f2fs_dir_entry *de,
					struct fscrypt_name *fname,
170
					struct fscrypt_str *cf_str,
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
					unsigned long bit_pos,
					f2fs_hash_t namehash)
{
#ifdef CONFIG_UNICODE
	struct inode *parent = d->inode;
	struct f2fs_sb_info *sbi = F2FS_I_SB(parent);
	struct qstr entry;
#endif

	if (de->hash_code != namehash)
		return false;

#ifdef CONFIG_UNICODE
	entry.name = d->filename[bit_pos];
	entry.len = de->name_len;

187 188 189 190 191 192 193 194 195
	if (sbi->s_encoding && IS_CASEFOLDED(parent)) {
		if (cf_str->name) {
			struct qstr cf = {.name = cf_str->name,
					  .len = cf_str->len};
			return !f2fs_ci_compare(parent, &cf, &entry, true);
		}
		return !f2fs_ci_compare(parent, fname->usr_fname, &entry,
					false);
	}
196 197 198 199 200 201 202
#endif
	if (fscrypt_match_name(fname, d->filename[bit_pos],
				le16_to_cpu(de->name_len)))
		return true;
	return false;
}

C
Chao Yu 已提交
203
struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
204 205
			f2fs_hash_t namehash, int *max_slots,
			struct f2fs_dentry_ptr *d)
J
Jaegeuk Kim 已提交
206 207
{
	struct f2fs_dir_entry *de;
208
	struct fscrypt_str cf_str = { .name = NULL, .len = 0 };
209 210
	unsigned long bit_pos = 0;
	int max_len = 0;
J
Jaegeuk Kim 已提交
211

212 213 214 215
#ifdef CONFIG_UNICODE
	f2fs_fname_setup_ci_filename(d->inode, fname->usr_fname, &cf_str);
#endif

216 217 218 219
	if (max_slots)
		*max_slots = 0;
	while (bit_pos < d->max) {
		if (!test_bit_le(bit_pos, d->bitmap)) {
220
			bit_pos++;
221
			max_len++;
222 223
			continue;
		}
224

225
		de = &d->dentry[bit_pos];
226

227 228 229 230 231
		if (unlikely(!de->name_len)) {
			bit_pos++;
			continue;
		}

232
		if (f2fs_match_name(d, de, fname, &cf_str, bit_pos, namehash))
233
			goto found;
234

235
		if (max_slots && max_len > *max_slots)
236
			*max_slots = max_len;
237
		max_len = 0;
238

239
		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
J
Jaegeuk Kim 已提交
240 241 242 243
	}

	de = NULL;
found:
244
	if (max_slots && max_len > *max_slots)
245
		*max_slots = max_len;
246 247 248 249

#ifdef CONFIG_UNICODE
	kvfree(cf_str.name);
#endif
J
Jaegeuk Kim 已提交
250 251 252 253
	return de;
}

static struct f2fs_dir_entry *find_in_level(struct inode *dir,
254
					unsigned int level,
255
					struct fscrypt_name *fname,
256
					struct page **res_page)
J
Jaegeuk Kim 已提交
257
{
258 259
	struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
	int s = GET_DENTRY_SLOTS(name.len);
J
Jaegeuk Kim 已提交
260 261 262 263 264
	unsigned int nbucket, nblock;
	unsigned int bidx, end_block;
	struct page *dentry_page;
	struct f2fs_dir_entry *de = NULL;
	bool room = false;
265
	int max_slots;
266
	f2fs_hash_t namehash = f2fs_dentry_hash(dir, &name, fname);
J
Jaegeuk Kim 已提交
267

268
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
269 270
	nblock = bucket_blocks(level);

271 272
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
					le32_to_cpu(namehash) % nbucket);
J
Jaegeuk Kim 已提交
273 274 275 276
	end_block = bidx + nblock;

	for (; bidx < end_block; bidx++) {
		/* no need to allocate new dentry pages to all the indices */
C
Chao Yu 已提交
277
		dentry_page = f2fs_find_data_page(dir, bidx);
J
Jaegeuk Kim 已提交
278
		if (IS_ERR(dentry_page)) {
279 280 281 282 283 284 285
			if (PTR_ERR(dentry_page) == -ENOENT) {
				room = true;
				continue;
			} else {
				*res_page = dentry_page;
				break;
			}
J
Jaegeuk Kim 已提交
286 287
		}

288 289
		de = find_in_block(dir, dentry_page, fname, namehash,
							&max_slots, res_page);
J
Jaegeuk Kim 已提交
290 291 292 293 294 295 296 297
		if (de)
			break;

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

298 299 300
	if (!de && room && F2FS_I(dir)->chash != namehash) {
		F2FS_I(dir)->chash = namehash;
		F2FS_I(dir)->clevel = level;
J
Jaegeuk Kim 已提交
301 302 303 304 305
	}

	return de;
}

306 307
struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
			struct fscrypt_name *fname, struct page **res_page)
J
Jaegeuk Kim 已提交
308 309 310 311 312
{
	unsigned long npages = dir_blocks(dir);
	struct f2fs_dir_entry *de = NULL;
	unsigned int max_depth;
	unsigned int level;
313 314

	if (f2fs_has_inline_dentry(dir)) {
315
		*res_page = NULL;
C
Chao Yu 已提交
316
		de = f2fs_find_in_inline_dir(dir, fname, res_page);
317 318
		goto out;
	}
C
Chao Yu 已提交
319

320 321
	if (npages == 0) {
		*res_page = NULL;
322
		goto out;
323
	}
J
Jaegeuk Kim 已提交
324 325

	max_depth = F2FS_I(dir)->i_current_depth;
326
	if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
327 328
		f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
			  dir->i_ino, max_depth);
329
		max_depth = MAX_DIR_HASH_DEPTH;
330
		f2fs_i_depth_write(dir, max_depth);
331
	}
J
Jaegeuk Kim 已提交
332 333

	for (level = 0; level < max_depth; level++) {
334
		*res_page = NULL;
335
		de = find_in_level(dir, level, fname, res_page);
336
		if (de || IS_ERR(*res_page))
J
Jaegeuk Kim 已提交
337 338
			break;
	}
339
out:
340 341 342
	/* This is to increase the speed of f2fs_create */
	if (!de)
		F2FS_I(dir)->task = current;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	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;

359 360 361 362 363 364 365 366
#ifdef CONFIG_UNICODE
	if (f2fs_has_strict_mode(F2FS_I_SB(dir)) && IS_CASEFOLDED(dir) &&
			utf8_validate(F2FS_I_SB(dir)->s_encoding, child)) {
		*res_page = ERR_PTR(-EINVAL);
		return NULL;
	}
#endif

367 368
	err = fscrypt_setup_filename(dir, child, 1, &fname);
	if (err) {
369 370 371 372
		if (err == -ENOENT)
			*res_page = NULL;
		else
			*res_page = ERR_PTR(err);
373 374 375 376 377
		return NULL;
	}

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

378
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
379 380 381 382 383
	return de;
}

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

S
Sheng Yong 已提交
386
	return f2fs_find_entry(dir, &dotdot, p);
J
Jaegeuk Kim 已提交
387 388
}

389
ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
390
							struct page **page)
J
Jaegeuk Kim 已提交
391 392 393 394
{
	ino_t res = 0;
	struct f2fs_dir_entry *de;

395
	de = f2fs_find_entry(dir, qstr, page);
J
Jaegeuk Kim 已提交
396 397
	if (de) {
		res = le32_to_cpu(de->ino);
398
		f2fs_put_page(*page, 0);
J
Jaegeuk Kim 已提交
399 400 401 402 403 404 405 406
	}

	return res;
}

void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
		struct page *page, struct inode *inode)
{
J
Jaegeuk Kim 已提交
407
	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
J
Jaegeuk Kim 已提交
408
	lock_page(page);
409
	f2fs_wait_on_page_writeback(page, type, true, true);
J
Jaegeuk Kim 已提交
410
	de->ino = cpu_to_le32(inode->i_ino);
411
	set_de_type(de, inode->i_mode);
J
Jaegeuk Kim 已提交
412
	set_page_dirty(page);
413

414
	dir->i_mtime = dir->i_ctime = current_time(dir);
415
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
416 417 418
	f2fs_put_page(page, 1);
}

419
static void init_dent_inode(const struct qstr *name, struct page *ipage)
J
Jaegeuk Kim 已提交
420
{
421
	struct f2fs_inode *ri;
J
Jaegeuk Kim 已提交
422

423
	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
424

425
	/* copy name info. to this inode page */
426 427 428
	ri = F2FS_INODE(ipage);
	ri->i_namelen = cpu_to_le32(name->len);
	memcpy(ri->i_name, name->name, name->len);
J
Jaegeuk Kim 已提交
429 430 431
	set_page_dirty(ipage);
}

C
Chao Yu 已提交
432
void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
433 434
					struct f2fs_dentry_ptr *d)
{
435 436
	struct qstr dot = QSTR_INIT(".", 1);
	struct qstr dotdot = QSTR_INIT("..", 2);
437

438 439
	/* update dirent of "." */
	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
440

441 442
	/* update dirent of ".." */
	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
443 444
}

445 446
static int make_empty_dir(struct inode *inode,
		struct inode *parent, struct page *page)
447 448 449
{
	struct page *dentry_page;
	struct f2fs_dentry_block *dentry_blk;
450
	struct f2fs_dentry_ptr d;
451

C
Chao Yu 已提交
452
	if (f2fs_has_inline_dentry(inode))
C
Chao Yu 已提交
453
		return f2fs_make_empty_inline_dir(inode, parent, page);
C
Chao Yu 已提交
454

C
Chao Yu 已提交
455
	dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
456 457 458
	if (IS_ERR(dentry_page))
		return PTR_ERR(dentry_page);

459
	dentry_blk = page_address(dentry_page);
460

461
	make_dentry_ptr_block(NULL, &d, dentry_blk);
C
Chao Yu 已提交
462
	f2fs_do_make_empty_dir(inode, parent, &d);
463 464 465 466 467 468

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

C
Chao Yu 已提交
469
struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
470 471
			const struct qstr *new_name, const struct qstr *orig_name,
			struct page *dpage)
J
Jaegeuk Kim 已提交
472
{
473
	struct page *page;
474
	int dummy_encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
475 476
	int err;

477
	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
C
Chao Yu 已提交
478
		page = f2fs_new_inode_page(inode);
479 480
		if (IS_ERR(page))
			return page;
J
Jaegeuk Kim 已提交
481 482

		if (S_ISDIR(inode->i_mode)) {
J
Jaegeuk Kim 已提交
483 484
			/* in order to handle error case */
			get_page(page);
485
			err = make_empty_dir(inode, dir, page);
J
Jaegeuk Kim 已提交
486 487 488 489 490
			if (err) {
				lock_page(page);
				goto put_error;
			}
			put_page(page);
J
Jaegeuk Kim 已提交
491 492
		}

493
		err = f2fs_init_acl(inode, dir, page, dpage);
494
		if (err)
495
			goto put_error;
496

497
		err = f2fs_init_security(inode, dir, orig_name, page);
498
		if (err)
499
			goto put_error;
500

501
		if ((IS_ENCRYPTED(dir) || dummy_encrypt) &&
502
					f2fs_may_encrypt(inode)) {
503
			err = fscrypt_inherit_context(dir, inode, page, false);
504 505 506
			if (err)
				goto put_error;
		}
J
Jaegeuk Kim 已提交
507
	} else {
C
Chao Yu 已提交
508
		page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
509 510
		if (IS_ERR(page))
			return page;
J
Jaegeuk Kim 已提交
511
	}
512

513
	if (new_name) {
514
		init_dent_inode(new_name, page);
515
		if (IS_ENCRYPTED(dir))
516 517
			file_set_enc_name(inode);
	}
518

519 520 521 522
	/*
	 * This file should be checkpointed during fsync.
	 * We lost i_pino from now on.
	 */
523
	if (is_inode_flag_set(inode, FI_INC_LINK)) {
524 525
		if (!S_ISDIR(inode->i_mode))
			file_lost_pino(inode);
C
Chao Yu 已提交
526 527 528 529 530
		/*
		 * 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 已提交
531
			f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
532
		f2fs_i_links_write(inode, true);
J
Jaegeuk Kim 已提交
533
	}
534 535
	return page;

536
put_error:
J
Jaegeuk Kim 已提交
537
	clear_nlink(inode);
C
Chao Yu 已提交
538
	f2fs_update_inode(inode, page);
J
Jaegeuk Kim 已提交
539
	f2fs_put_page(page, 1);
540
	return ERR_PTR(err);
J
Jaegeuk Kim 已提交
541 542
}

C
Chao Yu 已提交
543
void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
J
Jaegeuk Kim 已提交
544 545
						unsigned int current_depth)
{
546
	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
547
		if (S_ISDIR(inode->i_mode))
548
			f2fs_i_links_write(dir, true);
549
		clear_inode_flag(inode, FI_NEW_INODE);
J
Jaegeuk Kim 已提交
550
	}
551
	dir->i_mtime = dir->i_ctime = current_time(dir);
552
	f2fs_mark_inode_dirty_sync(dir, false);
553

554
	if (F2FS_I(dir)->i_current_depth != current_depth)
555
		f2fs_i_depth_write(dir, current_depth);
J
Jaegeuk Kim 已提交
556

557 558
	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
		clear_inode_flag(inode, FI_INC_LINK);
J
Jaegeuk Kim 已提交
559 560
}

C
Chao Yu 已提交
561
int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
J
Jaegeuk Kim 已提交
562 563 564 565
{
	int bit_start = 0;
	int zero_start, zero_end;
next:
566 567 568 569 570
	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 已提交
571 572 573 574 575
	if (zero_end - zero_start >= slots)
		return zero_start;

	bit_start = zero_end + 1;

576 577
	if (zero_end + 1 >= max_slots)
		return max_slots;
J
Jaegeuk Kim 已提交
578 579 580
	goto next;
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594
bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
					struct fscrypt_name *fname)
{
	struct f2fs_dentry_ptr d;
	unsigned int bit_pos;
	int slots = GET_DENTRY_SLOTS(fname_len(fname));

	make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));

	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);

	return bit_pos < d.max;
}

595
void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
596 597 598 599 600 601 602 603 604 605 606
				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);
607 608
	de->ino = cpu_to_le32(ino);
	set_de_type(de, mode);
609
	for (i = 0; i < slots; i++) {
610
		__set_bit_le(bit_pos + i, (void *)d->bitmap);
611 612 613 614
		/* avoid wrong garbage data for readdir */
		if (i)
			(de + i)->name_len = 0;
	}
615 616
}

617
int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
618
				const struct qstr *orig_name,
619
				struct inode *inode, nid_t ino, umode_t mode)
J
Jaegeuk Kim 已提交
620 621 622 623 624 625 626 627 628
{
	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;
629
	struct f2fs_dentry_ptr d;
630
	struct page *page = NULL;
631
	int slots, err = 0;
C
Chao Yu 已提交
632

J
Jaegeuk Kim 已提交
633
	level = 0;
634
	slots = GET_DENTRY_SLOTS(new_name->len);
635
	dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
636

J
Jaegeuk Kim 已提交
637 638 639 640 641 642 643
	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:
644
	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
645
		f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
J
Jaegeuk Kim 已提交
646
		return -ENOSPC;
647
	}
648

649 650
	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
		return -ENOSPC;
J
Jaegeuk Kim 已提交
651 652 653 654 655

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

656
	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
J
Jaegeuk Kim 已提交
657 658
	nblock = bucket_blocks(level);

659 660
	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
				(le32_to_cpu(dentry_hash) % nbucket));
J
Jaegeuk Kim 已提交
661 662

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

667
		dentry_blk = page_address(dentry_page);
C
Chao Yu 已提交
668
		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
669
						slots, NR_DENTRY_IN_BLOCK);
J
Jaegeuk Kim 已提交
670 671 672 673 674 675 676 677 678 679
		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:
680
	f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
J
Jaegeuk Kim 已提交
681

682 683
	if (inode) {
		down_write(&F2FS_I(inode)->i_sem);
C
Chao Yu 已提交
684
		page = f2fs_init_inode_metadata(inode, dir, new_name,
685
						orig_name, NULL);
686 687 688 689
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto fail;
		}
690
	}
691

692
	make_dentry_ptr_block(NULL, &d, dentry_blk);
693
	f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
694

J
Jaegeuk Kim 已提交
695
	set_page_dirty(dentry_page);
696

697
	if (inode) {
698
		f2fs_i_pino_write(inode, dir->i_ino);
699 700 701 702 703

		/* synchronize inode page's data from inode cache */
		if (is_inode_flag_set(inode, FI_NEW_INODE))
			f2fs_update_inode(inode, page);

704 705
		f2fs_put_page(page, 1);
	}
706

C
Chao Yu 已提交
707
	f2fs_update_parent_metadata(dir, inode, current_depth);
J
Jaegeuk Kim 已提交
708
fail:
709 710
	if (inode)
		up_write(&F2FS_I(inode)->i_sem);
711

J
Jaegeuk Kim 已提交
712
	f2fs_put_page(dentry_page, 1);
713 714 715 716

	return err;
}

C
Chao Yu 已提交
717
int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
				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;
}

737 738 739 740
/*
 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
 * f2fs_unlock_op().
 */
C
Chao Yu 已提交
741
int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
742 743 744
				struct inode *inode, nid_t ino, umode_t mode)
{
	struct fscrypt_name fname;
745 746
	struct page *page = NULL;
	struct f2fs_dir_entry *de = NULL;
747 748 749 750 751 752
	int err;

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

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	/*
	 * 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 已提交
770
		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
771
	}
772
	fscrypt_free_filename(&fname);
J
Jaegeuk Kim 已提交
773 774 775
	return err;
}

776 777 778 779 780 781
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 已提交
782
	page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
783 784 785 786 787 788
	if (IS_ERR(page)) {
		err = PTR_ERR(page);
		goto fail;
	}
	f2fs_put_page(page, 1);

789
	clear_inode_flag(inode, FI_NEW_INODE);
790
	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
791 792 793 794 795
fail:
	up_write(&F2FS_I(inode)->i_sem);
	return err;
}

J
Jaegeuk Kim 已提交
796
void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
797 798 799 800 801
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);

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

802
	if (S_ISDIR(inode->i_mode))
803
		f2fs_i_links_write(dir, false);
804
	inode->i_ctime = current_time(inode);
805

806
	f2fs_i_links_write(inode, false);
807
	if (S_ISDIR(inode->i_mode)) {
808
		f2fs_i_links_write(inode, false);
809
		f2fs_i_size_write(inode, 0);
810 811 812 813
	}
	up_write(&F2FS_I(inode)->i_sem);

	if (inode->i_nlink == 0)
C
Chao Yu 已提交
814
		f2fs_add_orphan_inode(inode);
815
	else
C
Chao Yu 已提交
816
		f2fs_release_orphan_inode(sbi);
817 818
}

J
Jaegeuk Kim 已提交
819
/*
A
arter97 已提交
820
 * It only removes the dentry from the dentry page, corresponding name
J
Jaegeuk Kim 已提交
821 822 823
 * entry in name page does not need to be touched during deletion.
 */
void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
824
					struct inode *dir, struct inode *inode)
J
Jaegeuk Kim 已提交
825 826 827
{
	struct	f2fs_dentry_block *dentry_blk;
	unsigned int bit_pos;
828
	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
J
Jaegeuk Kim 已提交
829 830
	int i;

831 832
	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);

833
	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
C
Chao Yu 已提交
834
		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
835

C
Chao Yu 已提交
836 837 838
	if (f2fs_has_inline_dentry(dir))
		return f2fs_delete_inline_entry(dentry, page, dir, inode);

J
Jaegeuk Kim 已提交
839
	lock_page(page);
840
	f2fs_wait_on_page_writeback(page, DATA, true, true);
J
Jaegeuk Kim 已提交
841

G
Gu Zheng 已提交
842 843
	dentry_blk = page_address(page);
	bit_pos = dentry - dentry_blk->dentry;
J
Jaegeuk Kim 已提交
844
	for (i = 0; i < slots; i++)
J
Jaegeuk Kim 已提交
845
		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
J
Jaegeuk Kim 已提交
846 847 848 849 850 851 852

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

853
	dir->i_ctime = dir->i_mtime = current_time(dir);
854
	f2fs_mark_inode_dirty_sync(dir, false);
J
Jaegeuk Kim 已提交
855

856
	if (inode)
J
Jaegeuk Kim 已提交
857
		f2fs_drop_nlink(dir, inode);
J
Jaegeuk Kim 已提交
858

C
Chao Yu 已提交
859
	if (bit_pos == NR_DENTRY_IN_BLOCK &&
C
Chao Yu 已提交
860
		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
M
Matthew Wilcox 已提交
861
		f2fs_clear_page_cache_dirty_tag(page);
J
Jaegeuk Kim 已提交
862
		clear_page_dirty_for_io(page);
863
		f2fs_clear_page_private(page);
J
Jaegeuk Kim 已提交
864
		ClearPageUptodate(page);
C
Chao Yu 已提交
865
		clear_cold_data(page);
866
		inode_dec_dirty_pages(dir);
C
Chao Yu 已提交
867
		f2fs_remove_dirty_inode(dir);
J
Jaegeuk Kim 已提交
868
	}
869
	f2fs_put_page(page, 1);
J
Jaegeuk Kim 已提交
870 871 872 873 874 875 876
}

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

C
Chao Yu 已提交
880 881 882
	if (f2fs_has_inline_dentry(dir))
		return f2fs_empty_inline_dir(dir);

J
Jaegeuk Kim 已提交
883
	for (bidx = 0; bidx < nblock; bidx++) {
C
Chao Yu 已提交
884
		dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
J
Jaegeuk Kim 已提交
885 886 887 888 889 890 891
		if (IS_ERR(dentry_page)) {
			if (PTR_ERR(dentry_page) == -ENOENT)
				continue;
			else
				return false;
		}

892
		dentry_blk = page_address(dentry_page);
J
Jaegeuk Kim 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
		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 已提交
909
int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
910
			unsigned int start_pos, struct fscrypt_str *fstr)
911 912 913 914
{
	unsigned char d_type = DT_UNKNOWN;
	unsigned int bit_pos;
	struct f2fs_dir_entry *de = NULL;
915
	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
916
	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
C
Chao Yu 已提交
917 918 919
	struct blk_plug plug;
	bool readdir_ra = sbi->readdir_ra == 1;
	int err = 0;
920

921
	bit_pos = ((unsigned long)ctx->pos % d->max);
922

C
Chao Yu 已提交
923 924 925
	if (readdir_ra)
		blk_start_plug(&plug);

926 927 928
	while (bit_pos < d->max) {
		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
		if (bit_pos >= d->max)
929 930
			break;

931
		de = &d->dentry[bit_pos];
932 933 934
		if (de->name_len == 0) {
			bit_pos++;
			ctx->pos = start_pos + bit_pos;
935
			printk_ratelimited(
936 937 938
				"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
				KERN_WARNING, sbi->sb->s_id,
				le32_to_cpu(de->ino));
939
			set_sbi_flag(sbi, SBI_NEED_FSCK);
940 941 942
			continue;
		}

C
Chao Yu 已提交
943
		d_type = f2fs_get_de_type(de);
944 945 946 947

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

948 949
		/* check memory boundary before moving forward */
		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
950 951
		if (unlikely(bit_pos > d->max ||
				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
952 953
			f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
				  __func__, le16_to_cpu(de->name_len));
954
			set_sbi_flag(sbi, SBI_NEED_FSCK);
955
			err = -EFSCORRUPTED;
956 957 958
			goto out;
		}

959
		if (IS_ENCRYPTED(d->inode)) {
960 961
			int save_len = fstr->len;

962
			err = fscrypt_fname_disk_to_usr(d->inode,
C
Chao Yu 已提交
963 964
						(u32)le32_to_cpu(de->hash_code),
						0, &de_name, fstr);
965
			if (err)
C
Chao Yu 已提交
966
				goto out;
967 968 969

			de_name = *fstr;
			fstr->len = save_len;
970 971 972
		}

		if (!dir_emit(ctx, de_name.name, de_name.len,
C
Chao Yu 已提交
973 974 975 976
					le32_to_cpu(de->ino), d_type)) {
			err = 1;
			goto out;
		}
977

C
Chao Yu 已提交
978
		if (readdir_ra)
C
Chao Yu 已提交
979
			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
980

981 982
		ctx->pos = start_pos + bit_pos;
	}
C
Chao Yu 已提交
983 984 985 986
out:
	if (readdir_ra)
		blk_finish_plug(&plug);
	return err;
987 988
}

A
Al Viro 已提交
989
static int f2fs_readdir(struct file *file, struct dir_context *ctx)
J
Jaegeuk Kim 已提交
990
{
A
Al Viro 已提交
991
	struct inode *inode = file_inode(file);
J
Jaegeuk Kim 已提交
992 993 994
	unsigned long npages = dir_blocks(inode);
	struct f2fs_dentry_block *dentry_blk = NULL;
	struct page *dentry_page = NULL;
995
	struct file_ra_state *ra = &file->f_ra;
C
Chao Yu 已提交
996
	loff_t start_pos = ctx->pos;
A
Al Viro 已提交
997
	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
998
	struct f2fs_dentry_ptr d;
999
	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1000
	int err = 0;
J
Jaegeuk Kim 已提交
1001

1002
	if (IS_ENCRYPTED(inode)) {
1003
		err = fscrypt_get_encryption_info(inode);
1004
		if (err)
C
Chao Yu 已提交
1005
			goto out;
1006

1007
		err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
1008
		if (err < 0)
C
Chao Yu 已提交
1009
			goto out;
1010 1011 1012 1013
	}

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

1017
	for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
C
Chao Yu 已提交
1018 1019 1020 1021 1022 1023 1024 1025

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

1026 1027 1028 1029 1030
		/* 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));

1031
		dentry_page = f2fs_find_data_page(inode, n);
1032 1033
		if (IS_ERR(dentry_page)) {
			err = PTR_ERR(dentry_page);
C
Chao Yu 已提交
1034 1035
			if (err == -ENOENT) {
				err = 0;
1036
				continue;
C
Chao Yu 已提交
1037
			} else {
C
Chao Yu 已提交
1038
				goto out_free;
C
Chao Yu 已提交
1039
			}
1040
		}
J
Jaegeuk Kim 已提交
1041

1042
		dentry_blk = page_address(dentry_page);
J
Jaegeuk Kim 已提交
1043

1044
		make_dentry_ptr_block(inode, &d, dentry_blk);
1045

C
Chao Yu 已提交
1046 1047 1048
		err = f2fs_fill_dentries(ctx, &d,
				n * NR_DENTRY_IN_BLOCK, &fstr);
		if (err) {
1049
			f2fs_put_page(dentry_page, 0);
1050 1051
			break;
		}
1052

1053
		f2fs_put_page(dentry_page, 0);
J
Jaegeuk Kim 已提交
1054
	}
C
Chao Yu 已提交
1055
out_free:
1056
	fscrypt_fname_free_buffer(&fstr);
C
Chao Yu 已提交
1057 1058
out:
	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
C
Chao Yu 已提交
1059
	return err < 0 ? err : 0;
J
Jaegeuk Kim 已提交
1060 1061
}

1062 1063
static int f2fs_dir_open(struct inode *inode, struct file *filp)
{
1064
	if (IS_ENCRYPTED(inode))
1065
		return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
1066 1067 1068
	return 0;
}

J
Jaegeuk Kim 已提交
1069 1070 1071
const struct file_operations f2fs_dir_operations = {
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
A
Al Viro 已提交
1072
	.iterate_shared	= f2fs_readdir,
J
Jaegeuk Kim 已提交
1073
	.fsync		= f2fs_sync_file,
1074
	.open		= f2fs_dir_open,
J
Jaegeuk Kim 已提交
1075
	.unlocked_ioctl	= f2fs_ioctl,
1076 1077 1078
#ifdef CONFIG_COMPAT
	.compat_ioctl   = f2fs_compat_ioctl,
#endif
J
Jaegeuk Kim 已提交
1079
};
1080 1081 1082 1083 1084 1085

#ifdef CONFIG_UNICODE
static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
			  const char *str, const struct qstr *name)
{
	struct qstr qstr = {.name = str, .len = len };
1086 1087
	const struct dentry *parent = READ_ONCE(dentry->d_parent);
	const struct inode *inode = READ_ONCE(parent->d_inode);
1088

1089
	if (!inode || !IS_CASEFOLDED(inode)) {
1090 1091
		if (len != name->len)
			return -1;
1092
		return memcmp(str, name->name, len);
1093 1094
	}

1095
	return f2fs_ci_compare(inode, name, &qstr, false);
1096 1097 1098 1099 1100 1101
}

static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
{
	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
	const struct unicode_map *um = sbi->s_encoding;
1102
	const struct inode *inode = READ_ONCE(dentry->d_inode);
1103 1104 1105
	unsigned char *norm;
	int len, ret = 0;

1106
	if (!inode || !IS_CASEFOLDED(inode))
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		return 0;

	norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
	if (!norm)
		return -ENOMEM;

	len = utf8_casefold(um, str, norm, PATH_MAX);
	if (len < 0) {
		if (f2fs_has_strict_mode(sbi))
			ret = -EINVAL;
		goto out;
	}
	str->hash = full_name_hash(dentry, norm, len);
out:
	kvfree(norm);
	return ret;
}

const struct dentry_operations f2fs_dentry_ops = {
	.d_hash = f2fs_d_hash,
	.d_compare = f2fs_d_compare,
};
#endif