Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * fs/f2fs/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *             http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/f2fs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/unicode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "f2fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "node.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <trace/events/f2fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) extern struct kmem_cache *f2fs_cf_name_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) static unsigned long dir_blocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 							>> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static unsigned int dir_buckets(unsigned int level, int dir_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 		return 1 << (level + dir_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 		return MAX_DIR_BUCKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) static unsigned int bucket_blocks(unsigned int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	if (level < MAX_DIR_HASH_DEPTH / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 		return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	[F2FS_FT_UNKNOWN]	= DT_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	[F2FS_FT_REG_FILE]	= DT_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	[F2FS_FT_DIR]		= DT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	[F2FS_FT_CHRDEV]	= DT_CHR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	[F2FS_FT_BLKDEV]	= DT_BLK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	[F2FS_FT_FIFO]		= DT_FIFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	[F2FS_FT_SOCK]		= DT_SOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	[F2FS_FT_SYMLINK]	= DT_LNK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	[S_IFREG >> S_SHIFT]	= F2FS_FT_REG_FILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	[S_IFDIR >> S_SHIFT]	= F2FS_FT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	[S_IFCHR >> S_SHIFT]	= F2FS_FT_CHRDEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	[S_IFBLK >> S_SHIFT]	= F2FS_FT_BLKDEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	[S_IFIFO >> S_SHIFT]	= F2FS_FT_FIFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	[S_IFSOCK >> S_SHIFT]	= F2FS_FT_SOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	[S_IFLNK >> S_SHIFT]	= F2FS_FT_SYMLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	if (de->file_type < F2FS_FT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		return f2fs_filetype_table[de->file_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	return DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) int f2fs_init_casefolded_name(const struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			      struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	if (IS_CASEFOLDED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		fname->cf_name.name = kmem_cache_alloc(f2fs_cf_name_slab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 								GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		if (!fname->cf_name.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		fname->cf_name.len = utf8_casefold(sb->s_encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 						   fname->usr_fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 						   fname->cf_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 						   F2FS_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		if ((int)fname->cf_name.len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 			kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 			fname->cf_name.name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 			if (sb_has_strict_encoding(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			/* fall back to treating name as opaque byte sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) static int __f2fs_setup_filename(const struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 				 const struct fscrypt_name *crypt_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 				 struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	memset(fname, 0, sizeof(*fname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	fname->usr_fname = crypt_name->usr_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	fname->disk_name = crypt_name->disk_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	fname->crypto_buf = crypt_name->crypto_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	if (crypt_name->is_nokey_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		/* hash was decoded from the no-key name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		fname->hash = cpu_to_le32(crypt_name->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		err = f2fs_init_casefolded_name(dir, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			f2fs_free_filename(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		f2fs_hash_filename(dir, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * Prepare to search for @iname in @dir.  This is similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * fscrypt_setup_filename(), but this also handles computing the casefolded name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * and the f2fs dirhash if needed, then packing all the information about this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * filename up into a 'struct f2fs_filename'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			int lookup, struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	struct fscrypt_name crypt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return __f2fs_setup_filename(dir, &crypt_name, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * Prepare to look up @dentry in @dir.  This is similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * and the f2fs dirhash if needed, then packing all the information about this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * filename up into a 'struct f2fs_filename'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	struct fscrypt_name crypt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	return __f2fs_setup_filename(dir, &crypt_name, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) void f2fs_free_filename(struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	kfree(fname->crypto_buf.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	fname->crypto_buf.name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	if (fname->cf_name.name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		fname->cf_name.name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static unsigned long dir_block_index(unsigned int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				int dir_level, unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	unsigned long bidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	for (i = 0; i < level; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	bidx += idx * bucket_blocks(level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	return bidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static struct f2fs_dir_entry *find_in_block(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 				struct page *dentry_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 				const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 				int *max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	struct f2fs_dentry_block *dentry_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	struct f2fs_dentry_ptr d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	make_dentry_ptr_block(dir, &d, dentry_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	return f2fs_find_target_dentry(&d, fname, max_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * Test whether a case-insensitive directory entry matches the filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * being searched for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * Returns 1 for a match, 0 for no match, and -errno on an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			       const u8 *de_name, u32 de_name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	const struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	const struct unicode_map *um = sb->s_encoding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	struct qstr entry = QSTR_INIT(de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		const struct fscrypt_str encrypted_name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			FSTR_INIT((u8 *)de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		if (!decrypted_name.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 						&decrypted_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		entry.name = decrypted_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		entry.len = decrypted_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	res = utf8_strncasecmp_folded(um, name, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	 * In strict mode, ignore invalid names.  In non-strict mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	 * fall back to treating them as opaque byte sequences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (res < 0 && !sb_has_strict_encoding(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		res = name->len == entry.len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 				memcmp(name->name, entry.name, name->len) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		/* utf8_strncasecmp_folded returns 0 on match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		res = (res == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	kfree(decrypted_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) #endif /* CONFIG_UNICODE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) static inline int f2fs_match_name(const struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 				   const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 				   const u8 *de_name, u32 de_name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	struct fscrypt_name f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	if (fname->cf_name.name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	f.usr_fname = fname->usr_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	f.disk_name = fname->disk_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	f.crypto_buf = fname->crypto_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	return fscrypt_match_name(&f, de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			const struct f2fs_filename *fname, int *max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct f2fs_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	unsigned long bit_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	int max_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		*max_slots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	while (bit_pos < d->max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if (!test_bit_le(bit_pos, d->bitmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			bit_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			max_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		de = &d->dentry[bit_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		if (unlikely(!de->name_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			bit_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if (de->hash_code == fname->hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			res = f2fs_match_name(d->inode, fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 					      d->filename[bit_pos],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 					      le16_to_cpu(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 				return ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 				goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		if (max_slots && max_len > *max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 			*max_slots = max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		max_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	if (max_slots && max_len > *max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		*max_slots = max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) static struct f2fs_dir_entry *find_in_level(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 					unsigned int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 					const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 					struct page **res_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	int s = GET_DENTRY_SLOTS(fname->disk_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	unsigned int nbucket, nblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	unsigned int bidx, end_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	struct page *dentry_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	struct f2fs_dir_entry *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	bool room = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	int max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	nblock = bucket_blocks(level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			       le32_to_cpu(fname->hash) % nbucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	end_block = bidx + nblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	for (; bidx < end_block; bidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		/* no need to allocate new dentry pages to all the indices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		dentry_page = f2fs_find_data_page(dir, bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		if (IS_ERR(dentry_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			if (PTR_ERR(dentry_page) == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 				room = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				*res_page = dentry_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		de = find_in_block(dir, dentry_page, fname, &max_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		if (IS_ERR(de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			*res_page = ERR_CAST(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		} else if (de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			*res_page = dentry_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		if (max_slots >= s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			room = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		f2fs_put_page(dentry_page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	if (!de && room && F2FS_I(dir)->chash != fname->hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		F2FS_I(dir)->chash = fname->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		F2FS_I(dir)->clevel = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 					 const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 					 struct page **res_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	unsigned long npages = dir_blocks(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	struct f2fs_dir_entry *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	unsigned int max_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	unsigned int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	*res_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (f2fs_has_inline_dentry(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		de = f2fs_find_in_inline_dir(dir, fname, res_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (npages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	max_depth = F2FS_I(dir)->i_current_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			  dir->i_ino, max_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		max_depth = MAX_DIR_HASH_DEPTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		f2fs_i_depth_write(dir, max_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	for (level = 0; level < max_depth; level++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		de = find_in_level(dir, level, fname, res_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		if (de || IS_ERR(*res_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	/* This is to increase the speed of f2fs_create */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (!de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		F2FS_I(dir)->task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  * Find an entry in the specified directory with the wanted name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * It returns the page where the entry was found (as a parameter - res_page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * and the entry itself. Page is returned mapped and unlocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * Entry is guaranteed to be valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			const struct qstr *child, struct page **res_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct f2fs_dir_entry *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	struct f2fs_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	err = f2fs_setup_filename(dir, child, 1, &fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			*res_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			*res_page = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	de = __f2fs_find_entry(dir, &fname, res_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	f2fs_free_filename(&fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	struct qstr dotdot = QSTR_INIT("..", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	return f2fs_find_entry(dir, &dotdot, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 							struct page **page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	ino_t res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	struct f2fs_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	de = f2fs_find_entry(dir, qstr, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		res = le32_to_cpu(de->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		f2fs_put_page(*page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		struct page *page, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	f2fs_wait_on_page_writeback(page, type, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	de->ino = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	set_de_type(de, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	f2fs_mark_inode_dirty_sync(dir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static void init_dent_inode(struct inode *dir, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			    const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			    struct page *ipage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	struct f2fs_inode *ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	if (!fname) /* tmpfile case? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	/* copy name info. to this inode page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	ri = F2FS_INODE(ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	ri->i_namelen = cpu_to_le32(fname->disk_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		file_set_enc_name(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		 * Roll-forward recovery doesn't have encryption keys available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		 * so it can't compute the dirhash for encrypted+casefolded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		 * filenames.  Append it to i_name if possible.  Else, disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		 * roll-forward recovery of the dentry (i.e., make fsync'ing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		 * file force a checkpoint) by setting LOST_PINO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		if (IS_CASEFOLDED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			    F2FS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 				put_unaligned(fname->hash, (f2fs_hash_t *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 					&ri->i_name[fname->disk_name.len]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 				file_lost_pino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	set_page_dirty(ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 					struct f2fs_dentry_ptr *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	struct fscrypt_str dot = FSTR_INIT(".", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	struct fscrypt_str dotdot = FSTR_INIT("..", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	/* update dirent of "." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	/* update dirent of ".." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static int make_empty_dir(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		struct inode *parent, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	struct page *dentry_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct f2fs_dentry_block *dentry_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	struct f2fs_dentry_ptr d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	if (f2fs_has_inline_dentry(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		return f2fs_make_empty_inline_dir(inode, parent, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	if (IS_ERR(dentry_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		return PTR_ERR(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	dentry_blk = page_address(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	make_dentry_ptr_block(NULL, &d, dentry_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	f2fs_do_make_empty_dir(inode, parent, &d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	set_page_dirty(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	f2fs_put_page(dentry_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			const struct f2fs_filename *fname, struct page *dpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		page = f2fs_new_inode_page(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			/* in order to handle error case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			err = make_empty_dir(inode, dir, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 				lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 				goto put_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		err = f2fs_init_acl(inode, dir, page, dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			goto put_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		err = f2fs_init_security(inode, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 					 fname ? fname->usr_fname : NULL, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			goto put_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			err = fscrypt_set_context(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				goto put_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	init_dent_inode(dir, inode, fname, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 * This file should be checkpointed during fsync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	 * We lost i_pino from now on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (is_inode_flag_set(inode, FI_INC_LINK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (!S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			file_lost_pino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		 * If link the tmpfile to alias through linkat path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		 * we should remove this inode from orphan list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		f2fs_i_links_write(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) put_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	f2fs_update_inode(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 						unsigned int current_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			f2fs_i_links_write(dir, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		clear_inode_flag(inode, FI_NEW_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	f2fs_mark_inode_dirty_sync(dir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (F2FS_I(dir)->i_current_depth != current_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		f2fs_i_depth_write(dir, current_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		clear_inode_flag(inode, FI_INC_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	int bit_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	int zero_start, zero_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (zero_start >= max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		return max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (zero_end - zero_start >= slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		return zero_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	bit_start = zero_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (zero_end + 1 >= max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		return max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			  const struct f2fs_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	struct f2fs_dentry_ptr d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	unsigned int bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	return bit_pos < d.max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			unsigned int bit_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct f2fs_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	int slots = GET_DENTRY_SLOTS(name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	de = &d->dentry[bit_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	de->hash_code = name_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	de->name_len = cpu_to_le16(name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	memcpy(d->filename[bit_pos], name->name, name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	de->ino = cpu_to_le32(ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	set_de_type(de, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	for (i = 0; i < slots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		__set_bit_le(bit_pos + i, (void *)d->bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		/* avoid wrong garbage data for readdir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			(de + i)->name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			   struct inode *inode, nid_t ino, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	unsigned int bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	unsigned int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	unsigned int current_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	unsigned long bidx, block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	unsigned int nbucket, nblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	struct page *dentry_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	struct f2fs_dentry_block *dentry_blk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct f2fs_dentry_ptr d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	int slots, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	slots = GET_DENTRY_SLOTS(fname->disk_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	current_depth = F2FS_I(dir)->i_current_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (F2FS_I(dir)->chash == fname->hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		level = F2FS_I(dir)->clevel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		F2FS_I(dir)->chash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) start:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	/* Increase the depth, if required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (level == current_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		++current_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	nblock = bucket_blocks(level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 				(le32_to_cpu(fname->hash) % nbucket));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (IS_ERR(dentry_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			return PTR_ERR(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		dentry_blk = page_address(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 						slots, NR_DENTRY_IN_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		if (bit_pos < NR_DENTRY_IN_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			goto add_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		f2fs_put_page(dentry_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	/* Move to next level to find the empty slot for new dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	++level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	goto start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) add_dentry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		f2fs_down_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	make_dentry_ptr_block(NULL, &d, dentry_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			   bit_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	set_page_dirty(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		f2fs_i_pino_write(inode, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		/* synchronize inode page's data from inode cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		if (is_inode_flag_set(inode, FI_NEW_INODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			f2fs_update_inode(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	f2fs_update_parent_metadata(dir, inode, current_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		f2fs_up_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	f2fs_put_page(dentry_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		    struct inode *inode, nid_t ino, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	int err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (f2fs_has_inline_dentry(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  * f2fs_unlock_op().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 				struct inode *inode, nid_t ino, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct f2fs_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct f2fs_dir_entry *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	err = f2fs_setup_filename(dir, name, 0, &fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	 * An immature stackable filesystem shows a race condition between lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	 * and create. If we have same task when doing lookup and create, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	 * definitely fine as expected by VFS normally. Otherwise, let's just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	 * verify on-disk dentry one more time, which guarantees filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	 * consistency more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (current != F2FS_I(dir)->task) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		de = __f2fs_find_entry(dir, &fname, &page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		F2FS_I(dir)->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		f2fs_put_page(page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	} else if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	f2fs_free_filename(&fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	f2fs_down_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	clear_inode_flag(inode, FI_NEW_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	f2fs_up_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	f2fs_down_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		f2fs_i_links_write(dir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	f2fs_i_links_write(inode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		f2fs_i_links_write(inode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		f2fs_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	f2fs_up_write(&F2FS_I(inode)->i_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		f2fs_add_orphan_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		f2fs_release_orphan_inode(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * It only removes the dentry from the dentry page, corresponding name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * entry in name page does not need to be touched during deletion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 					struct inode *dir, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct	f2fs_dentry_block *dentry_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	unsigned int bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	if (f2fs_has_inline_dentry(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		return f2fs_delete_inline_entry(dentry, page, dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	f2fs_wait_on_page_writeback(page, DATA, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	dentry_blk = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	bit_pos = dentry - dentry_blk->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	for (i = 0; i < slots; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	/* Let's check and deallocate this dentry page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			NR_DENTRY_IN_BLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (bit_pos == NR_DENTRY_IN_BLOCK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		f2fs_clear_page_cache_dirty_tag(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		clear_page_private_gcing(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		inode_dec_dirty_pages(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		f2fs_remove_dirty_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		set_page_private(page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	f2fs_mark_inode_dirty_sync(dir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		f2fs_drop_nlink(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) bool f2fs_empty_dir(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	unsigned long bidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	struct page *dentry_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	unsigned int bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	struct f2fs_dentry_block *dentry_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	unsigned long nblock = dir_blocks(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (f2fs_has_inline_dentry(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return f2fs_empty_inline_dir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	for (bidx = 0; bidx < nblock; bidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		if (IS_ERR(dentry_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			if (PTR_ERR(dentry_page) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		dentry_blk = page_address(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (bidx == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			bit_pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			bit_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 						NR_DENTRY_IN_BLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 						bit_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		f2fs_put_page(dentry_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		if (bit_pos < NR_DENTRY_IN_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			unsigned int start_pos, struct fscrypt_str *fstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	unsigned char d_type = DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	unsigned int bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct f2fs_dir_entry *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	bool readdir_ra = sbi->readdir_ra == 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	bool found_valid_dirent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	bit_pos = ((unsigned long)ctx->pos % d->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (readdir_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	while (bit_pos < d->max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		if (bit_pos >= d->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		de = &d->dentry[bit_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		if (de->name_len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			if (found_valid_dirent || !bit_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 				printk_ratelimited(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 					"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 					KERN_WARNING, sbi->sb->s_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 					le32_to_cpu(de->ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 				set_sbi_flag(sbi, SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			bit_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			ctx->pos = start_pos + bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		d_type = f2fs_get_de_type(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		de_name.name = d->filename[bit_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		de_name.len = le16_to_cpu(de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		/* check memory boundary before moving forward */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		if (unlikely(bit_pos > d->max ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				  __func__, le16_to_cpu(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			set_sbi_flag(sbi, SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		if (IS_ENCRYPTED(d->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			int save_len = fstr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			err = fscrypt_fname_disk_to_usr(d->inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 						(u32)le32_to_cpu(de->hash_code),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 						0, &de_name, fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			de_name = *fstr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			fstr->len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		if (!dir_emit(ctx, de_name.name, de_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 					le32_to_cpu(de->ino), d_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		if (readdir_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		ctx->pos = start_pos + bit_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		found_valid_dirent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (readdir_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static int f2fs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	unsigned long npages = dir_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	struct f2fs_dentry_block *dentry_blk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct page *dentry_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	struct file_ra_state *ra = &file->f_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	loff_t start_pos = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	struct f2fs_dentry_ptr d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		err = fscrypt_prepare_readdir(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (f2fs_has_inline_dentry(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		err = f2fs_read_inline_dir(file, ctx, &fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		/* allow readdir() to be interrupted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		if (fatal_signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		/* readahead for multi pages of dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		if (npages - n > 1 && !ra_has_index(ra, n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			page_cache_sync_readahead(inode->i_mapping, ra, file, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		dentry_page = f2fs_find_data_page(inode, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		if (IS_ERR(dentry_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 			err = PTR_ERR(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		dentry_blk = page_address(dentry_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		make_dentry_ptr_block(inode, &d, dentry_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		err = f2fs_fill_dentries(ctx, &d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				n * NR_DENTRY_IN_BLOCK, &fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			f2fs_put_page(dentry_page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		f2fs_put_page(dentry_page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	fscrypt_fname_free_buffer(&fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return err < 0 ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) const struct file_operations f2fs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	.iterate_shared	= f2fs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	.fsync		= f2fs_sync_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	.unlocked_ioctl	= f2fs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	.compat_ioctl   = f2fs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) };