1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
7 #include <linux/compat.h>
9 #include <linux/buffer_head.h>
11 #include "exfat_raw.h"
14 static int exfat_extract_uni_name(struct exfat_dentry *ep,
15 unsigned short *uniname)
19 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
20 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
32 static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
33 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
36 struct exfat_entry_set_cache *es;
37 unsigned int uni_len = 0, len;
39 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
44 * First entry : file entry
45 * Second entry : stream-extension entry
46 * Third entry : first file-name entry
47 * So, the index of first file-name dentry should start from 2.
49 for (i = 2; i < es->num_entries; i++) {
50 struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
52 /* end of name entry */
53 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
56 len = exfat_extract_uni_name(ep, uniname);
58 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
60 uniname += EXFAT_FILE_NAME_LEN;
63 exfat_free_dentry_set(es, false);
66 /* read a directory entry from the opened directory */
67 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
69 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
70 unsigned int type, clu_offset, max_dentries;
71 struct exfat_chain dir, clu;
72 struct exfat_uni_name uni_name;
73 struct exfat_dentry *ep;
74 struct super_block *sb = inode->i_sb;
75 struct exfat_sb_info *sbi = EXFAT_SB(sb);
76 struct exfat_inode_info *ei = EXFAT_I(inode);
77 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
78 struct buffer_head *bh;
80 /* check if the given file ID is opened */
81 if (ei->type != TYPE_DIR)
85 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
87 exfat_chain_set(&dir, ei->start_clu,
88 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
90 dentries_per_clu = sbi->dentries_per_clu;
91 dentries_per_clu_bits = ilog2(dentries_per_clu);
92 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
93 (u64)sbi->num_clusters << dentries_per_clu_bits);
95 clu_offset = dentry >> dentries_per_clu_bits;
96 exfat_chain_dup(&clu, &dir);
98 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
99 clu.dir += clu_offset;
100 clu.size -= clu_offset;
102 /* hint_information */
103 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
104 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
105 clu_offset -= ei->hint_bmap.off;
106 clu.dir = ei->hint_bmap.clu;
109 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
110 if (exfat_get_next_cluster(sb, &(clu.dir)))
117 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
118 i = dentry & (dentries_per_clu - 1);
120 for ( ; i < dentries_per_clu; i++, dentry++) {
121 ep = exfat_get_dentry(sb, &clu, i, &bh);
125 type = exfat_get_entry_type(ep);
126 if (type == TYPE_UNUSED) {
131 if (type != TYPE_FILE && type != TYPE_DIR) {
136 num_ext = ep->dentry.file.num_ext;
137 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
138 exfat_get_entry_time(sbi, &dir_entry->crtime,
139 ep->dentry.file.create_tz,
140 ep->dentry.file.create_time,
141 ep->dentry.file.create_date,
142 ep->dentry.file.create_time_cs);
143 exfat_get_entry_time(sbi, &dir_entry->mtime,
144 ep->dentry.file.modify_tz,
145 ep->dentry.file.modify_time,
146 ep->dentry.file.modify_date,
147 ep->dentry.file.modify_time_cs);
148 exfat_get_entry_time(sbi, &dir_entry->atime,
149 ep->dentry.file.access_tz,
150 ep->dentry.file.access_time,
151 ep->dentry.file.access_date,
154 *uni_name.name = 0x0;
155 exfat_get_uniname_from_ext_entry(sb, &clu, i,
157 exfat_utf16_to_nls(sb, &uni_name,
158 dir_entry->namebuf.lfn,
159 dir_entry->namebuf.lfnbuf_len);
162 ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
166 le64_to_cpu(ep->dentry.stream.valid_size);
167 dir_entry->entry = dentry;
170 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
171 ei->hint_bmap.clu = clu.dir;
173 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
177 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
181 clu.dir = EXFAT_EOF_CLUSTER;
183 if (exfat_get_next_cluster(sb, &(clu.dir)))
188 dir_entry->namebuf.lfn[0] = '\0';
189 *cpos = EXFAT_DEN_TO_B(dentry);
193 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
199 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
201 nb->lfn = __getname();
204 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
208 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
214 exfat_init_namebuf(nb);
218 * Before calling dir_emit*(), sbi->s_lock should be released
219 * because page fault can occur in dir_emit*().
221 #define ITER_POS_FILLED_DOTS (2)
222 static int exfat_iterate(struct file *file, struct dir_context *ctx)
224 struct inode *inode = file_inode(file);
225 struct super_block *sb = inode->i_sb;
227 struct exfat_dir_entry de;
228 struct exfat_dentry_namebuf *nb = &(de.namebuf);
229 struct exfat_inode_info *ei = EXFAT_I(inode);
232 int err = 0, fake_offset = 0;
234 exfat_init_namebuf(nb);
237 if (!dir_emit_dots(file, ctx))
240 if (ctx->pos == ITER_POS_FILLED_DOTS) {
245 cpos = round_up(cpos, DENTRY_SIZE);
247 /* name buffer should be allocated before use */
248 err = exfat_alloc_namebuf(nb);
252 mutex_lock(&EXFAT_SB(sb)->s_lock);
254 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
257 err = exfat_readdir(inode, &cpos, &de);
260 * At least we tried to read a sector.
261 * Move cpos to next sector position (should be aligned).
264 cpos += 1 << (sb->s_blocksize_bits);
265 cpos &= ~(sb->s_blocksize - 1);
275 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
276 tmp = exfat_iget(sb, i_pos);
281 inum = iunique(sb, EXFAT_ROOT_INO);
284 mutex_unlock(&EXFAT_SB(sb)->s_lock);
285 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
286 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
292 if (!cpos && fake_offset)
293 cpos = ITER_POS_FILLED_DOTS;
295 mutex_unlock(&EXFAT_SB(sb)->s_lock);
298 * To improve performance, free namebuf after unlock sb_lock.
299 * If namebuf is not allocated, this function do nothing
301 exfat_free_namebuf(nb);
305 const struct file_operations exfat_dir_operations = {
306 .llseek = generic_file_llseek,
307 .read = generic_read_dir,
308 .iterate = exfat_iterate,
309 .unlocked_ioctl = exfat_ioctl,
311 .compat_ioctl = exfat_compat_ioctl,
313 .fsync = exfat_file_fsync,
316 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
320 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
322 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
326 return exfat_zeroed_cluster(inode, clu->dir);
329 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
333 len = p_uniname->name_len;
337 /* 1 file entry + 1 stream entry + name entries */
338 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
341 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
343 if (ep->type == EXFAT_UNUSED)
345 if (IS_EXFAT_DELETED(ep->type))
347 if (ep->type == EXFAT_INVAL)
349 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
350 if (ep->type == EXFAT_BITMAP)
352 if (ep->type == EXFAT_UPCASE)
354 if (ep->type == EXFAT_VOLUME)
356 if (ep->type == EXFAT_FILE) {
357 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
361 return TYPE_CRITICAL_PRI;
363 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
364 if (ep->type == EXFAT_GUID)
366 if (ep->type == EXFAT_PADDING)
368 if (ep->type == EXFAT_ACLTAB)
370 return TYPE_BENIGN_PRI;
372 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
373 if (ep->type == EXFAT_STREAM)
375 if (ep->type == EXFAT_NAME)
377 if (ep->type == EXFAT_ACL)
379 return TYPE_CRITICAL_SEC;
381 return TYPE_BENIGN_SEC;
384 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
386 if (type == TYPE_UNUSED) {
387 ep->type = EXFAT_UNUSED;
388 } else if (type == TYPE_DELETED) {
389 ep->type &= EXFAT_DELETE;
390 } else if (type == TYPE_STREAM) {
391 ep->type = EXFAT_STREAM;
392 } else if (type == TYPE_EXTEND) {
393 ep->type = EXFAT_NAME;
394 } else if (type == TYPE_BITMAP) {
395 ep->type = EXFAT_BITMAP;
396 } else if (type == TYPE_UPCASE) {
397 ep->type = EXFAT_UPCASE;
398 } else if (type == TYPE_VOLUME) {
399 ep->type = EXFAT_VOLUME;
400 } else if (type == TYPE_DIR) {
401 ep->type = EXFAT_FILE;
402 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
403 } else if (type == TYPE_FILE) {
404 ep->type = EXFAT_FILE;
405 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
409 static void exfat_init_stream_entry(struct exfat_dentry *ep,
410 unsigned char flags, unsigned int start_clu,
411 unsigned long long size)
413 exfat_set_entry_type(ep, TYPE_STREAM);
414 ep->dentry.stream.flags = flags;
415 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
416 ep->dentry.stream.valid_size = cpu_to_le64(size);
417 ep->dentry.stream.size = cpu_to_le64(size);
420 static void exfat_init_name_entry(struct exfat_dentry *ep,
421 unsigned short *uniname)
425 exfat_set_entry_type(ep, TYPE_EXTEND);
426 ep->dentry.name.flags = 0x0;
428 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
429 if (*uniname != 0x0) {
430 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
433 ep->dentry.name.unicode_0_14[i] = 0x0;
438 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
439 int entry, unsigned int type, unsigned int start_clu,
440 unsigned long long size)
442 struct super_block *sb = inode->i_sb;
443 struct exfat_sb_info *sbi = EXFAT_SB(sb);
444 struct timespec64 ts = current_time(inode);
445 struct exfat_dentry *ep;
446 struct buffer_head *bh;
449 * We cannot use exfat_get_dentry_set here because file ep is not
452 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
456 exfat_set_entry_type(ep, type);
457 exfat_set_entry_time(sbi, &ts,
458 &ep->dentry.file.create_tz,
459 &ep->dentry.file.create_time,
460 &ep->dentry.file.create_date,
461 &ep->dentry.file.create_time_cs);
462 exfat_set_entry_time(sbi, &ts,
463 &ep->dentry.file.modify_tz,
464 &ep->dentry.file.modify_time,
465 &ep->dentry.file.modify_date,
466 &ep->dentry.file.modify_time_cs);
467 exfat_set_entry_time(sbi, &ts,
468 &ep->dentry.file.access_tz,
469 &ep->dentry.file.access_time,
470 &ep->dentry.file.access_date,
473 exfat_update_bh(bh, IS_DIRSYNC(inode));
476 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
480 exfat_init_stream_entry(ep,
481 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
483 exfat_update_bh(bh, IS_DIRSYNC(inode));
489 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
492 struct super_block *sb = inode->i_sb;
496 struct exfat_dentry *ep, *fep;
497 struct buffer_head *fbh, *bh;
499 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
503 num_entries = fep->dentry.file.num_ext + 1;
504 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
506 for (i = 1; i < num_entries; i++) {
507 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
512 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
517 fep->dentry.file.checksum = cpu_to_le16(chksum);
518 exfat_update_bh(fbh, IS_DIRSYNC(inode));
524 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
525 int entry, int num_entries, struct exfat_uni_name *p_uniname)
527 struct super_block *sb = inode->i_sb;
529 unsigned short *uniname = p_uniname->name;
530 struct exfat_dentry *ep;
531 struct buffer_head *bh;
532 int sync = IS_DIRSYNC(inode);
534 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
538 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
539 exfat_update_bh(bh, sync);
542 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
546 ep->dentry.stream.name_len = p_uniname->name_len;
547 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
548 exfat_update_bh(bh, sync);
551 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
552 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
556 exfat_init_name_entry(ep, uniname);
557 exfat_update_bh(bh, sync);
559 uniname += EXFAT_FILE_NAME_LEN;
562 exfat_update_dir_chksum(inode, p_dir, entry);
566 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
567 int entry, int order, int num_entries)
569 struct super_block *sb = inode->i_sb;
571 struct exfat_dentry *ep;
572 struct buffer_head *bh;
574 for (i = order; i < num_entries; i++) {
575 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
579 exfat_set_entry_type(ep, TYPE_DELETED);
580 exfat_update_bh(bh, IS_DIRSYNC(inode));
587 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
589 int chksum_type = CS_DIR_ENTRY, i;
590 unsigned short chksum = 0;
591 struct exfat_dentry *ep;
593 for (i = 0; i < es->num_entries; i++) {
594 ep = exfat_get_dentry_cached(es, i);
595 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
597 chksum_type = CS_DEFAULT;
599 ep = exfat_get_dentry_cached(es, 0);
600 ep->dentry.file.checksum = cpu_to_le16(chksum);
604 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
609 err = exfat_update_bhs(es->bh, es->num_bh, sync);
611 for (i = 0; i < es->num_bh; i++)
620 static int exfat_walk_fat_chain(struct super_block *sb,
621 struct exfat_chain *p_dir, unsigned int byte_offset,
624 struct exfat_sb_info *sbi = EXFAT_SB(sb);
625 unsigned int clu_offset;
626 unsigned int cur_clu;
628 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
629 cur_clu = p_dir->dir;
631 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
632 cur_clu += clu_offset;
634 while (clu_offset > 0) {
635 if (exfat_get_next_cluster(sb, &cur_clu))
637 if (cur_clu == EXFAT_EOF_CLUSTER) {
639 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
641 EXFAT_B_TO_DEN(byte_offset));
652 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
653 int entry, sector_t *sector, int *offset)
656 unsigned int off, clu = 0;
657 struct exfat_sb_info *sbi = EXFAT_SB(sb);
659 off = EXFAT_DEN_TO_B(entry);
661 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
665 /* byte offset in cluster */
666 off = EXFAT_CLU_OFFSET(off, sbi);
668 /* byte offset in sector */
669 *offset = EXFAT_BLK_OFFSET(off, sb);
671 /* sector offset in cluster */
672 *sector = EXFAT_B_TO_BLK(off, sb);
673 *sector += exfat_cluster_to_sector(sbi, clu);
677 #define EXFAT_MAX_RA_SIZE (128*1024)
678 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
680 struct exfat_sb_info *sbi = EXFAT_SB(sb);
681 struct buffer_head *bh;
682 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
683 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
684 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
685 unsigned int ra_count = min(adj_ra_count, max_ra_count);
687 /* Read-ahead is not required */
688 if (sbi->sect_per_clus == 1)
691 if (sec < sbi->data_start_sector) {
692 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
693 (unsigned long long)sec, sbi->data_start_sector);
697 /* Not sector aligned with ra_count, resize ra_count to page size */
698 if ((sec - sbi->data_start_sector) & (ra_count - 1))
699 ra_count = page_ra_count;
701 bh = sb_find_get_block(sb, sec);
702 if (!bh || !buffer_uptodate(bh)) {
705 for (i = 0; i < ra_count; i++)
706 sb_breadahead(sb, (sector_t)(sec + i));
712 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
713 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
715 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
719 if (p_dir->dir == DIR_DELETED) {
720 exfat_err(sb, "abnormal access to deleted dentry");
724 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
727 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
728 !(entry & (dentries_per_page - 1)))
729 exfat_dir_readahead(sb, sec);
731 *bh = sb_bread(sb, sec);
735 return (struct exfat_dentry *)((*bh)->b_data + off);
738 enum exfat_validate_dentry_mode {
740 ES_MODE_GET_FILE_ENTRY,
741 ES_MODE_GET_STRM_ENTRY,
742 ES_MODE_GET_NAME_ENTRY,
743 ES_MODE_GET_CRITICAL_SEC_ENTRY,
746 static bool exfat_validate_entry(unsigned int type,
747 enum exfat_validate_dentry_mode *mode)
749 if (type == TYPE_UNUSED || type == TYPE_DELETED)
753 case ES_MODE_STARTED:
754 if (type != TYPE_FILE && type != TYPE_DIR)
756 *mode = ES_MODE_GET_FILE_ENTRY;
758 case ES_MODE_GET_FILE_ENTRY:
759 if (type != TYPE_STREAM)
761 *mode = ES_MODE_GET_STRM_ENTRY;
763 case ES_MODE_GET_STRM_ENTRY:
764 if (type != TYPE_EXTEND)
766 *mode = ES_MODE_GET_NAME_ENTRY;
768 case ES_MODE_GET_NAME_ENTRY:
769 if (type == TYPE_STREAM)
771 if (type != TYPE_EXTEND) {
772 if (!(type & TYPE_CRITICAL_SEC))
774 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
777 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
778 if (type == TYPE_EXTEND || type == TYPE_STREAM)
780 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
789 struct exfat_dentry *exfat_get_dentry_cached(
790 struct exfat_entry_set_cache *es, int num)
792 int off = es->start_off + num * DENTRY_SIZE;
793 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
794 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
796 return (struct exfat_dentry *)p;
800 * Returns a set of dentries for a file or dir.
802 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
803 * User should call exfat_get_dentry_set() after setting 'modified' to apply
804 * changes made in this entry set to the real device.
807 * sb+p_dir+entry: indicates a file/dir
808 * type: specifies how many dentries should be included.
810 * pointer of entry set on success,
813 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
814 struct exfat_chain *p_dir, int entry, unsigned int type)
817 unsigned int off, byte_offset, clu = 0;
819 struct exfat_sb_info *sbi = EXFAT_SB(sb);
820 struct exfat_entry_set_cache *es;
821 struct exfat_dentry *ep;
823 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
824 struct buffer_head *bh;
826 if (p_dir->dir == DIR_DELETED) {
827 exfat_err(sb, "access to deleted dentry");
831 byte_offset = EXFAT_DEN_TO_B(entry);
832 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
836 es = kzalloc(sizeof(*es), GFP_KERNEL);
840 es->modified = false;
842 /* byte offset in cluster */
843 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
845 /* byte offset in sector */
846 off = EXFAT_BLK_OFFSET(byte_offset, sb);
849 /* sector offset in cluster */
850 sec = EXFAT_B_TO_BLK(byte_offset, sb);
851 sec += exfat_cluster_to_sector(sbi, clu);
853 bh = sb_bread(sb, sec);
856 es->bh[es->num_bh++] = bh;
858 ep = exfat_get_dentry_cached(es, 0);
859 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
862 num_entries = type == ES_ALL_ENTRIES ?
863 ep->dentry.file.num_ext + 1 : type;
864 es->num_entries = num_entries;
866 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
867 for (i = 1; i < num_bh; i++) {
868 /* get the next sector */
869 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
870 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
872 else if (exfat_get_next_cluster(sb, &clu))
874 sec = exfat_cluster_to_sector(sbi, clu);
879 bh = sb_bread(sb, sec);
882 es->bh[es->num_bh++] = bh;
885 /* validate cached dentries */
886 for (i = 1; i < num_entries; i++) {
887 ep = exfat_get_dentry_cached(es, i);
888 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
894 exfat_free_dentry_set(es, false);
906 * @ei: inode info of parent directory
907 * @p_dir: directory structure of parent directory
908 * @num_entries:entry size of p_uniname
909 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
910 * for traversing cluster chain.
912 * >= 0: file directory entry position where the name exists
913 * -ENOENT: entry with the name does not exist
916 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
917 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
918 int num_entries, unsigned int type, struct exfat_hint *hint_opt)
920 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
921 int order, step, name_len = 0;
922 int dentries_per_clu, num_empty = 0;
923 unsigned int entry_type;
924 unsigned short *uniname = NULL;
925 struct exfat_chain clu;
926 struct exfat_hint *hint_stat = &ei->hint_stat;
927 struct exfat_hint_femp candi_empty;
928 struct exfat_sb_info *sbi = EXFAT_SB(sb);
930 dentries_per_clu = sbi->dentries_per_clu;
932 exfat_chain_dup(&clu, p_dir);
934 if (hint_stat->eidx) {
935 clu.dir = hint_stat->clu;
936 dentry = hint_stat->eidx;
940 candi_empty.eidx = EXFAT_HINT_NONE;
943 step = DIRENT_STEP_FILE;
944 while (clu.dir != EXFAT_EOF_CLUSTER) {
945 i = dentry & (dentries_per_clu - 1);
946 for (; i < dentries_per_clu; i++, dentry++) {
947 struct exfat_dentry *ep;
948 struct buffer_head *bh;
950 if (rewind && dentry == end_eidx)
953 ep = exfat_get_dentry(sb, &clu, i, &bh);
957 entry_type = exfat_get_entry_type(ep);
959 if (entry_type == TYPE_UNUSED ||
960 entry_type == TYPE_DELETED) {
961 step = DIRENT_STEP_FILE;
964 if (candi_empty.eidx == EXFAT_HINT_NONE &&
966 exfat_chain_set(&candi_empty.cur,
967 clu.dir, clu.size, clu.flags);
970 if (candi_empty.eidx == EXFAT_HINT_NONE &&
971 num_empty >= num_entries) {
973 dentry - (num_empty - 1);
974 WARN_ON(candi_empty.eidx < 0);
975 candi_empty.count = num_empty;
977 if (ei->hint_femp.eidx ==
981 ei->hint_femp = candi_empty;
985 if (entry_type == TYPE_UNUSED)
991 candi_empty.eidx = EXFAT_HINT_NONE;
993 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
994 step = DIRENT_STEP_FILE;
995 hint_opt->clu = clu.dir;
997 if (type == TYPE_ALL || type == entry_type) {
998 num_ext = ep->dentry.file.num_ext;
999 step = DIRENT_STEP_STRM;
1005 if (entry_type == TYPE_STREAM) {
1008 if (step != DIRENT_STEP_STRM) {
1009 step = DIRENT_STEP_FILE;
1013 step = DIRENT_STEP_FILE;
1014 name_hash = le16_to_cpu(
1015 ep->dentry.stream.name_hash);
1016 if (p_uniname->name_hash == name_hash &&
1017 p_uniname->name_len ==
1018 ep->dentry.stream.name_len) {
1019 step = DIRENT_STEP_NAME;
1028 if (entry_type == TYPE_EXTEND) {
1029 unsigned short entry_uniname[16], unichar;
1031 if (step != DIRENT_STEP_NAME ||
1032 name_len >= MAX_NAME_LENGTH) {
1033 step = DIRENT_STEP_FILE;
1038 uniname = p_uniname->name;
1040 uniname += EXFAT_FILE_NAME_LEN;
1042 len = exfat_extract_uni_name(ep, entry_uniname);
1045 unichar = *(uniname+len);
1046 *(uniname+len) = 0x0;
1048 if (exfat_uniname_ncmp(sb, uniname,
1049 entry_uniname, len)) {
1050 step = DIRENT_STEP_FILE;
1051 } else if (p_uniname->name_len == name_len) {
1052 if (order == num_ext)
1054 step = DIRENT_STEP_SECD;
1057 *(uniname+len) = unichar;
1062 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1063 if (step == DIRENT_STEP_SECD) {
1064 if (++order == num_ext)
1069 step = DIRENT_STEP_FILE;
1072 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1076 clu.dir = EXFAT_EOF_CLUSTER;
1078 if (exfat_get_next_cluster(sb, &clu.dir))
1085 * We started at not 0 index,so we should try to find target
1086 * from 0 index to the index we started at.
1088 if (!rewind && end_eidx) {
1091 clu.dir = p_dir->dir;
1092 /* reset empty hint */
1094 candi_empty.eidx = EXFAT_HINT_NONE;
1098 /* initialized hint_stat */
1099 hint_stat->clu = p_dir->dir;
1100 hint_stat->eidx = 0;
1104 /* next dentry we'll find is out of this cluster */
1105 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1108 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1112 clu.dir = EXFAT_EOF_CLUSTER;
1114 ret = exfat_get_next_cluster(sb, &clu.dir);
1117 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1118 /* just initialized hint_stat */
1119 hint_stat->clu = p_dir->dir;
1120 hint_stat->eidx = 0;
1121 return (dentry - num_ext);
1125 hint_stat->clu = clu.dir;
1126 hint_stat->eidx = dentry + 1;
1127 return dentry - num_ext;
1130 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1131 int entry, struct exfat_dentry *ep)
1135 struct exfat_dentry *ext_ep;
1136 struct buffer_head *bh;
1138 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1139 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1143 type = exfat_get_entry_type(ext_ep);
1145 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1153 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1156 int dentries_per_clu;
1157 unsigned int entry_type;
1158 struct exfat_chain clu;
1159 struct exfat_dentry *ep;
1160 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1161 struct buffer_head *bh;
1163 dentries_per_clu = sbi->dentries_per_clu;
1165 exfat_chain_dup(&clu, p_dir);
1167 while (clu.dir != EXFAT_EOF_CLUSTER) {
1168 for (i = 0; i < dentries_per_clu; i++) {
1169 ep = exfat_get_dentry(sb, &clu, i, &bh);
1172 entry_type = exfat_get_entry_type(ep);
1175 if (entry_type == TYPE_UNUSED)
1177 if (entry_type != TYPE_DIR)
1182 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1186 clu.dir = EXFAT_EOF_CLUSTER;
1188 if (exfat_get_next_cluster(sb, &(clu.dir)))