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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "exfat_raw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "exfat_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) static int exfat_extract_uni_name(struct exfat_dentry *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 		unsigned short *uniname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 	int i, len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 		*uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 		if (*uniname == 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 			return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 		uniname++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 		len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	*uniname = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 		struct exfat_chain *p_dir, int entry, unsigned short *uniname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	struct exfat_entry_set_cache *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	if (!es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	 * First entry  : file entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	 * Second entry : stream-extension entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	 * Third entry  : first file-name entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	 * So, the index of first file-name dentry should start from 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	for (i = 2; i < es->num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		/* end of name entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		if (exfat_get_entry_type(ep) != TYPE_EXTEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		exfat_extract_uni_name(ep, uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		uniname += EXFAT_FILE_NAME_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	exfat_free_dentry_set(es, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) /* read a directory entry from the opened directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	unsigned int type, clu_offset, max_dentries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct exfat_chain dir, clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct exfat_uni_name uni_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	/* check if the given file ID is opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	if (ei->type != TYPE_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	if (ei->entry == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		exfat_chain_set(&dir, ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 			EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	dentries_per_clu = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	dentries_per_clu_bits = ilog2(dentries_per_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 					   (u64)sbi->num_clusters << dentries_per_clu_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	clu_offset = dentry >> dentries_per_clu_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	exfat_chain_dup(&clu, &dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		clu.dir += clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		clu.size -= clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		/* hint_information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		    ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			clu_offset -= ei->hint_bmap.off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			clu.dir = ei->hint_bmap.clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		while (clu_offset > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			if (exfat_get_next_cluster(sb, &(clu.dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			clu_offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		i = dentry & (dentries_per_clu - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		for ( ; i < dentries_per_clu; i++, dentry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 			ep = exfat_get_dentry(sb, &clu, i, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 			if (type == TYPE_UNUSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 			if (type != TYPE_FILE && type != TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 			num_ext = ep->dentry.file.num_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 			dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 			exfat_get_entry_time(sbi, &dir_entry->crtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 					ep->dentry.file.create_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 					ep->dentry.file.create_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 					ep->dentry.file.create_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 					ep->dentry.file.create_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 			exfat_get_entry_time(sbi, &dir_entry->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 					ep->dentry.file.modify_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 					ep->dentry.file.modify_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 					ep->dentry.file.modify_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 					ep->dentry.file.modify_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			exfat_get_entry_time(sbi, &dir_entry->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 					ep->dentry.file.access_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 					ep->dentry.file.access_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 					ep->dentry.file.access_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 					0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			*uni_name.name = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 			exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 				uni_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			exfat_utf16_to_nls(sb, &uni_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 				dir_entry->namebuf.lfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				dir_entry->namebuf.lfnbuf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			dir_entry->size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				le64_to_cpu(ep->dentry.stream.valid_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			dir_entry->entry = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			ei->hint_bmap.clu = clu.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			*cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 				clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 				clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			if (exfat_get_next_cluster(sb, &(clu.dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	dir_entry->namebuf.lfn[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	*cpos = EXFAT_DEN_TO_B(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	nb->lfn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	nb->lfnbuf_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	nb->lfn = __getname();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (!nb->lfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	if (!nb->lfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	__putname(nb->lfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	exfat_init_namebuf(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) /* skip iterating emit_dots when dir is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) #define ITER_POS_FILLED_DOTS    (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static int exfat_iterate(struct file *filp, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct inode *inode = filp->f_path.dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	struct inode *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct exfat_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct exfat_dentry_namebuf *nb = &(de.namebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	unsigned long inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	loff_t cpos, i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	int err = 0, fake_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	exfat_init_namebuf(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	cpos = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (!dir_emit_dots(filp, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (ctx->pos == ITER_POS_FILLED_DOTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		fake_offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (cpos & (DENTRY_SIZE - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	/* name buffer should be allocated before use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	err = exfat_alloc_namebuf(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) get_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	err = exfat_readdir(inode, &cpos, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		 * At least we tried to read a sector.  Move cpos to next sector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		 * position (should be aligned).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		if (err == -EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 			cpos += 1 << (sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			cpos &= ~(sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (!nb->lfn[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	i_pos = ((loff_t)ei->start_clu << 32) |	(de.entry & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	tmp = exfat_iget(sb, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		inum = tmp->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		iput(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		inum = iunique(sb, EXFAT_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	 * Before calling dir_emit(), sb_lock should be released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	 * Because page fault can occur in dir_emit() when the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	 * of buffer given from user is larger than one page size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			(de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		goto out_unlocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	ctx->pos = cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	goto get_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) end_of_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (!cpos && fake_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		cpos = ITER_POS_FILLED_DOTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	ctx->pos = cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) out_unlocked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	 * To improve performance, free namebuf after unlock sb_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	 * If namebuf is not allocated, this function do nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	exfat_free_namebuf(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) const struct file_operations exfat_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	.iterate	= exfat_iterate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	.fsync		= exfat_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	return exfat_zeroed_cluster(inode, clu->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	len = p_uniname->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	/* 1 file entry + 1 stream entry + name entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (ep->type == EXFAT_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		return TYPE_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (IS_EXFAT_DELETED(ep->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		return TYPE_DELETED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (ep->type == EXFAT_INVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return TYPE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		if (ep->type == EXFAT_BITMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			return TYPE_BITMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		if (ep->type == EXFAT_UPCASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			return TYPE_UPCASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		if (ep->type == EXFAT_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			return TYPE_VOLUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		if (ep->type == EXFAT_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				return TYPE_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			return TYPE_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		return TYPE_CRITICAL_PRI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	if (IS_EXFAT_BENIGN_PRI(ep->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		if (ep->type == EXFAT_GUID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			return TYPE_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		if (ep->type == EXFAT_PADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			return TYPE_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		if (ep->type == EXFAT_ACLTAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			return TYPE_ACLTAB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		return TYPE_BENIGN_PRI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		if (ep->type == EXFAT_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			return TYPE_STREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		if (ep->type == EXFAT_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			return TYPE_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		if (ep->type == EXFAT_ACL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			return TYPE_ACL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return TYPE_CRITICAL_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	return TYPE_BENIGN_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (type == TYPE_UNUSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		ep->type = EXFAT_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	} else if (type == TYPE_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		ep->type &= EXFAT_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	} else if (type == TYPE_STREAM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		ep->type = EXFAT_STREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	} else if (type == TYPE_EXTEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		ep->type = EXFAT_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	} else if (type == TYPE_BITMAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		ep->type = EXFAT_BITMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	} else if (type == TYPE_UPCASE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		ep->type = EXFAT_UPCASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	} else if (type == TYPE_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		ep->type = EXFAT_VOLUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	} else if (type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		ep->type = EXFAT_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	} else if (type == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		ep->type = EXFAT_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) static void exfat_init_stream_entry(struct exfat_dentry *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		unsigned char flags, unsigned int start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		unsigned long long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	exfat_set_entry_type(ep, TYPE_STREAM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	ep->dentry.stream.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	ep->dentry.stream.valid_size = cpu_to_le64(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	ep->dentry.stream.size = cpu_to_le64(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) static void exfat_init_name_entry(struct exfat_dentry *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		unsigned short *uniname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	exfat_set_entry_type(ep, TYPE_EXTEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	ep->dentry.name.flags = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		if (*uniname != 0x0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			uniname++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			ep->dentry.name.unicode_0_14[i] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		int entry, unsigned int type, unsigned int start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		unsigned long long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct timespec64 ts = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	 * We cannot use exfat_get_dentry_set here because file ep is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	 * initialized yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	exfat_set_entry_type(ep, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	exfat_set_entry_time(sbi, &ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			&ep->dentry.file.create_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			&ep->dentry.file.create_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			&ep->dentry.file.create_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			&ep->dentry.file.create_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	exfat_set_entry_time(sbi, &ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			&ep->dentry.file.modify_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			&ep->dentry.file.modify_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			&ep->dentry.file.modify_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			&ep->dentry.file.modify_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	exfat_set_entry_time(sbi, &ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			&ep->dentry.file.access_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			&ep->dentry.file.access_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			&ep->dentry.file.access_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	exfat_update_bh(bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	exfat_init_stream_entry(ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		(type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		start_clu, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	exfat_update_bh(bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		int entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int i, num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	u16 chksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	struct exfat_dentry *ep, *fep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	struct buffer_head *fbh, *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	fep = exfat_get_dentry(sb, p_dir, entry, &fbh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (!fep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	num_entries = fep->dentry.file.num_ext + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	for (i = 1; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			goto release_fbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 				CS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	fep->dentry.file.checksum = cpu_to_le16(chksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	exfat_update_bh(fbh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) release_fbh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	brelse(fbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		int entry, int num_entries, struct exfat_uni_name *p_uniname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	unsigned short *uniname = p_uniname->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	int sync = IS_DIRSYNC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	exfat_update_bh(bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	ep->dentry.stream.name_len = p_uniname->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	exfat_update_bh(bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		exfat_init_name_entry(ep, uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		exfat_update_bh(bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		uniname += EXFAT_FILE_NAME_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	exfat_update_dir_chksum(inode, p_dir, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		int entry, int order, int num_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	for (i = order; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		exfat_set_entry_type(ep, TYPE_DELETED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		exfat_update_bh(bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	int chksum_type = CS_DIR_ENTRY, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	unsigned short chksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	for (i = 0; i < es->num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		ep = exfat_get_dentry_cached(es, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 					     chksum_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		chksum_type = CS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	ep = exfat_get_dentry_cached(es, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	ep->dentry.file.checksum = cpu_to_le16(chksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	es->modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (es->modified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		err = exfat_update_bhs(es->bh, es->num_bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	for (i = 0; i < es->num_bh; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			bforget(es->bh[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			brelse(es->bh[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	kfree(es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static int exfat_walk_fat_chain(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		struct exfat_chain *p_dir, unsigned int byte_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		unsigned int *clu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	unsigned int clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	unsigned int cur_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	cur_clu = p_dir->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		cur_clu += clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		while (clu_offset > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			if (exfat_get_next_cluster(sb, &cur_clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			if (cur_clu == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 				exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 					"invalid dentry access beyond EOF (clu : %u, eidx : %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 					p_dir->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 					EXFAT_B_TO_DEN(byte_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			clu_offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	*clu = cur_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		int entry, sector_t *sector, int *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	unsigned int off, clu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	off = EXFAT_DEN_TO_B(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	/* byte offset in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	off = EXFAT_CLU_OFFSET(off, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	/* byte offset in sector    */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	*offset = EXFAT_BLK_OFFSET(off, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	/* sector offset in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	*sector = EXFAT_B_TO_BLK(off, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	*sector += exfat_cluster_to_sector(sbi, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) #define EXFAT_MAX_RA_SIZE     (128*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	unsigned int ra_count = min(adj_ra_count, max_ra_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	/* Read-ahead is not required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	if (sbi->sect_per_clus == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	if (sec < sbi->data_start_sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			  (unsigned long long)sec, sbi->data_start_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	/* Not sector aligned with ra_count, resize ra_count to page size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if ((sec - sbi->data_start_sector) & (ra_count - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		ra_count = page_ra_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	bh = sb_find_get_block(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (!bh || !buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		for (i = 0; i < ra_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			sb_breadahead(sb, (sector_t)(sec + i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		sector_t *sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	sector_t sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	if (p_dir->dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		exfat_err(sb, "abnormal access to deleted dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (p_dir->dir != EXFAT_FREE_CLUSTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			!(entry & (dentries_per_page - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		exfat_dir_readahead(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	*bh = sb_bread(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if (!*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if (sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		*sector = sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	return (struct exfat_dentry *)((*bh)->b_data + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) enum exfat_validate_dentry_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	ES_MODE_STARTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	ES_MODE_GET_FILE_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	ES_MODE_GET_STRM_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	ES_MODE_GET_NAME_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	ES_MODE_GET_CRITICAL_SEC_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) static bool exfat_validate_entry(unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		enum exfat_validate_dentry_mode *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (type == TYPE_UNUSED || type == TYPE_DELETED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	switch (*mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	case ES_MODE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		if  (type != TYPE_FILE && type != TYPE_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		*mode = ES_MODE_GET_FILE_ENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	case ES_MODE_GET_FILE_ENTRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		if (type != TYPE_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		*mode = ES_MODE_GET_STRM_ENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	case ES_MODE_GET_STRM_ENTRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (type != TYPE_EXTEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		*mode = ES_MODE_GET_NAME_ENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	case ES_MODE_GET_NAME_ENTRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		if (type == TYPE_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		if (type != TYPE_EXTEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			if (!(type & TYPE_CRITICAL_SEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			*mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	case ES_MODE_GET_CRITICAL_SEC_ENTRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) struct exfat_dentry *exfat_get_dentry_cached(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	struct exfat_entry_set_cache *es, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	int off = es->start_off + num * DENTRY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	return (struct exfat_dentry *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) }
^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)  * Returns a set of dentries for a file or dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  * User should call exfat_get_dentry_set() after setting 'modified' to apply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * changes made in this entry set to the real device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  *   sb+p_dir+entry: indicates a file/dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  *   type:  specifies how many dentries should be included.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  * return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  *   pointer of entry set on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  *   NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		struct exfat_chain *p_dir, int entry, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	int ret, i, num_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	unsigned int off, byte_offset, clu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	sector_t sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	struct exfat_entry_set_cache *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	int num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	if (p_dir->dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		exfat_err(sb, "access to deleted dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return NULL;
^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) 	byte_offset = EXFAT_DEN_TO_B(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	es = kzalloc(sizeof(*es), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (!es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	es->sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	es->modified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	/* byte offset in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	/* byte offset in sector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	off = EXFAT_BLK_OFFSET(byte_offset, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	es->start_off = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	/* sector offset in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	sec = EXFAT_B_TO_BLK(byte_offset, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	sec += exfat_cluster_to_sector(sbi, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	bh = sb_bread(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		goto free_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	es->bh[es->num_bh++] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	ep = exfat_get_dentry_cached(es, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		goto free_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	num_entries = type == ES_ALL_ENTRIES ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		ep->dentry.file.num_ext + 1 : type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	es->num_entries = num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	for (i = 1; i < num_bh; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		/* get the next sector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		if (exfat_is_last_sector_in_cluster(sbi, sec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 				clu++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			else if (exfat_get_next_cluster(sb, &clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				goto free_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			sec = exfat_cluster_to_sector(sbi, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		bh = sb_bread(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			goto free_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		es->bh[es->num_bh++] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	/* validiate cached dentries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	for (i = 1; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		ep = exfat_get_dentry_cached(es, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			goto free_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) free_es:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	exfat_free_dentry_set(es, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	DIRENT_STEP_FILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	DIRENT_STEP_STRM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	DIRENT_STEP_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	DIRENT_STEP_SECD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911)  * return values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912)  *   >= 0	: return dir entiry position with the name in dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913)  *   -ENOENT	: entry with the name does not exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  *   -EIO	: I/O error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		int num_entries, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	int order, step, name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	int dentries_per_clu, num_empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	unsigned int entry_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	unsigned short *uniname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct exfat_hint *hint_stat = &ei->hint_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct exfat_hint_femp candi_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	dentries_per_clu = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	exfat_chain_dup(&clu, p_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	if (hint_stat->eidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		clu.dir = hint_stat->clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		dentry = hint_stat->eidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		end_eidx = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	candi_empty.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) rewind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	order = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	while (clu.dir != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		i = dentry & (dentries_per_clu - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		for (; i < dentries_per_clu; i++, dentry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			if (rewind && dentry == end_eidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			entry_type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			if (entry_type == TYPE_UNUSED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			    entry_type == TYPE_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 				num_empty++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				if (candi_empty.eidx == EXFAT_HINT_NONE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 						num_empty == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 					exfat_chain_set(&candi_empty.cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 						clu.dir, clu.size, clu.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 				if (candi_empty.eidx == EXFAT_HINT_NONE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 						num_empty >= num_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 					candi_empty.eidx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 						dentry - (num_empty - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 					WARN_ON(candi_empty.eidx < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 					candi_empty.count = num_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 					if (ei->hint_femp.eidx ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 							EXFAT_HINT_NONE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 						candi_empty.eidx <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 							 ei->hint_femp.eidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 						ei->hint_femp = candi_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 				if (entry_type == TYPE_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 					goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			num_empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			candi_empty.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				if (type == TYPE_ALL || type == entry_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 					num_ext = ep->dentry.file.num_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 					step = DIRENT_STEP_STRM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			if (entry_type == TYPE_STREAM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				u16 name_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 				if (step != DIRENT_STEP_STRM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 					step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 					brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 				step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 				name_hash = le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 						ep->dentry.stream.name_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 				if (p_uniname->name_hash == name_hash &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 				    p_uniname->name_len ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 						ep->dentry.stream.name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 					step = DIRENT_STEP_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 					order = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 					name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			if (entry_type == TYPE_EXTEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 				unsigned short entry_uniname[16], unichar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 				if (step != DIRENT_STEP_NAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 					step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				if (++order == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 					uniname = p_uniname->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 					uniname += EXFAT_FILE_NAME_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				len = exfat_extract_uni_name(ep, entry_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				name_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				unichar = *(uniname+len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				*(uniname+len) = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				if (exfat_uniname_ncmp(sb, uniname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 					entry_uniname, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 					step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 				} else if (p_uniname->name_len == name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 					if (order == num_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 						goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 					step = DIRENT_STEP_SECD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				*(uniname+len) = unichar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			if (entry_type &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 				if (step == DIRENT_STEP_SECD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 					if (++order == num_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 						goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			step = DIRENT_STEP_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 				clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			if (exfat_get_next_cluster(sb, &clu.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 				return -EIO;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	 * We started at not 0 index,so we should try to find target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	 * from 0 index to the index we started at.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (!rewind && end_eidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		rewind = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		dentry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		clu.dir = p_dir->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		/* reset empty hint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		num_empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		candi_empty.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		goto rewind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	/* initialized hint_stat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	hint_stat->clu = p_dir->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	hint_stat->eidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	/* next dentry we'll find is out of this cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 				clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 				clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			ret = exfat_get_next_cluster(sb, &clu.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			/* just initialized hint_stat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			hint_stat->clu = p_dir->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			hint_stat->eidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			return (dentry - num_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	hint_stat->clu = clu.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	hint_stat->eidx = dentry + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	return dentry - num_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		int entry, struct exfat_dentry *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	int i, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	struct exfat_dentry *ext_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		if (!ext_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		type = exfat_get_entry_type(ext_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	int i, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	int dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	unsigned int entry_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	dentries_per_clu = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	exfat_chain_dup(&clu, p_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	while (clu.dir != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		for (i = 0; i < dentries_per_clu; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			entry_type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			if (entry_type == TYPE_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 				return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			if (entry_type != TYPE_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 				clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 				clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			if (exfat_get_next_cluster(sb, &(clu.dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }