1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
8 #include <linux/buffer_head.h>
10 #include "exfat_raw.h"
13 static int exfat_extract_uni_name(struct exfat_dentry *ep,
14 unsigned short *uniname)
18 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
19 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
31 static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
32 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
35 struct exfat_entry_set_cache *es;
37 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
42 * First entry : file entry
43 * Second entry : stream-extension entry
44 * Third entry : first file-name entry
45 * So, the index of first file-name dentry should start from 2.
47 for (i = 2; i < es->num_entries; i++) {
48 struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
50 /* end of name entry */
51 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
54 exfat_extract_uni_name(ep, uniname);
55 uniname += EXFAT_FILE_NAME_LEN;
58 exfat_free_dentry_set(es, false);
61 /* read a directory entry from the opened directory */
62 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
64 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
65 unsigned int type, clu_offset, max_dentries;
67 struct exfat_chain dir, clu;
68 struct exfat_uni_name uni_name;
69 struct exfat_dentry *ep;
70 struct super_block *sb = inode->i_sb;
71 struct exfat_sb_info *sbi = EXFAT_SB(sb);
72 struct exfat_inode_info *ei = EXFAT_I(inode);
73 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
74 struct buffer_head *bh;
76 /* check if the given file ID is opened */
77 if (ei->type != TYPE_DIR)
81 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
83 exfat_chain_set(&dir, ei->start_clu,
84 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
86 dentries_per_clu = sbi->dentries_per_clu;
87 dentries_per_clu_bits = ilog2(dentries_per_clu);
88 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
89 (u64)sbi->num_clusters << dentries_per_clu_bits);
91 clu_offset = dentry >> dentries_per_clu_bits;
92 exfat_chain_dup(&clu, &dir);
94 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
95 clu.dir += clu_offset;
96 clu.size -= clu_offset;
98 /* hint_information */
99 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
100 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
101 clu_offset -= ei->hint_bmap.off;
102 clu.dir = ei->hint_bmap.clu;
105 while (clu_offset > 0) {
106 if (exfat_get_next_cluster(sb, &(clu.dir)))
113 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
114 i = dentry & (dentries_per_clu - 1);
116 for ( ; i < dentries_per_clu; i++, dentry++) {
117 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
121 type = exfat_get_entry_type(ep);
122 if (type == TYPE_UNUSED) {
127 if (type != TYPE_FILE && type != TYPE_DIR) {
132 num_ext = ep->dentry.file.num_ext;
133 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
134 exfat_get_entry_time(sbi, &dir_entry->crtime,
135 ep->dentry.file.create_tz,
136 ep->dentry.file.create_time,
137 ep->dentry.file.create_date,
138 ep->dentry.file.create_time_cs);
139 exfat_get_entry_time(sbi, &dir_entry->mtime,
140 ep->dentry.file.modify_tz,
141 ep->dentry.file.modify_time,
142 ep->dentry.file.modify_date,
143 ep->dentry.file.modify_time_cs);
144 exfat_get_entry_time(sbi, &dir_entry->atime,
145 ep->dentry.file.access_tz,
146 ep->dentry.file.access_time,
147 ep->dentry.file.access_date,
150 *uni_name.name = 0x0;
151 exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
153 exfat_utf16_to_nls(sb, &uni_name,
154 dir_entry->namebuf.lfn,
155 dir_entry->namebuf.lfnbuf_len);
158 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
162 le64_to_cpu(ep->dentry.stream.valid_size);
163 dir_entry->entry = dentry;
166 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
167 ei->hint_bmap.clu = clu.dir;
169 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
173 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
177 clu.dir = EXFAT_EOF_CLUSTER;
179 if (exfat_get_next_cluster(sb, &(clu.dir)))
184 dir_entry->namebuf.lfn[0] = '\0';
185 *cpos = EXFAT_DEN_TO_B(dentry);
189 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
195 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
197 nb->lfn = __getname();
200 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
204 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
210 exfat_init_namebuf(nb);
213 /* skip iterating emit_dots when dir is empty */
214 #define ITER_POS_FILLED_DOTS (2)
215 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
217 struct inode *inode = filp->f_path.dentry->d_inode;
218 struct super_block *sb = inode->i_sb;
220 struct exfat_dir_entry de;
221 struct exfat_dentry_namebuf *nb = &(de.namebuf);
222 struct exfat_inode_info *ei = EXFAT_I(inode);
225 int err = 0, fake_offset = 0;
227 exfat_init_namebuf(nb);
228 mutex_lock(&EXFAT_SB(sb)->s_lock);
231 if (!dir_emit_dots(filp, ctx))
234 if (ctx->pos == ITER_POS_FILLED_DOTS) {
239 if (cpos & (DENTRY_SIZE - 1)) {
244 /* name buffer should be allocated before use */
245 err = exfat_alloc_namebuf(nb);
249 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
252 err = exfat_readdir(inode, &cpos, &de);
255 * At least we tried to read a sector. Move cpos to next sector
256 * position (should be aligned).
259 cpos += 1 << (sb->s_blocksize_bits);
260 cpos &= ~(sb->s_blocksize - 1);
270 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
271 tmp = exfat_iget(sb, i_pos);
276 inum = iunique(sb, EXFAT_ROOT_INO);
280 * Before calling dir_emit(), sb_lock should be released.
281 * Because page fault can occur in dir_emit() when the size
282 * of buffer given from user is larger than one page size.
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))
288 mutex_lock(&EXFAT_SB(sb)->s_lock);
293 if (!cpos && fake_offset)
294 cpos = ITER_POS_FILLED_DOTS;
297 mutex_unlock(&EXFAT_SB(sb)->s_lock);
300 * To improve performance, free namebuf after unlock sb_lock.
301 * If namebuf is not allocated, this function do nothing
303 exfat_free_namebuf(nb);
307 const struct file_operations exfat_dir_operations = {
308 .llseek = generic_file_llseek,
309 .read = generic_read_dir,
310 .iterate = exfat_iterate,
311 .fsync = exfat_file_fsync,
314 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
318 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
320 ret = exfat_alloc_cluster(inode, 1, clu);
324 return exfat_zeroed_cluster(inode, clu->dir);
327 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
331 len = p_uniname->name_len;
335 /* 1 file entry + 1 stream entry + name entries */
336 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
339 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
341 if (ep->type == EXFAT_UNUSED)
343 if (IS_EXFAT_DELETED(ep->type))
345 if (ep->type == EXFAT_INVAL)
347 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
348 if (ep->type == EXFAT_BITMAP)
350 if (ep->type == EXFAT_UPCASE)
352 if (ep->type == EXFAT_VOLUME)
354 if (ep->type == EXFAT_FILE) {
355 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
359 return TYPE_CRITICAL_PRI;
361 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
362 if (ep->type == EXFAT_GUID)
364 if (ep->type == EXFAT_PADDING)
366 if (ep->type == EXFAT_ACLTAB)
368 return TYPE_BENIGN_PRI;
370 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
371 if (ep->type == EXFAT_STREAM)
373 if (ep->type == EXFAT_NAME)
375 if (ep->type == EXFAT_ACL)
377 return TYPE_CRITICAL_SEC;
379 return TYPE_BENIGN_SEC;
382 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
384 if (type == TYPE_UNUSED) {
385 ep->type = EXFAT_UNUSED;
386 } else if (type == TYPE_DELETED) {
387 ep->type &= EXFAT_DELETE;
388 } else if (type == TYPE_STREAM) {
389 ep->type = EXFAT_STREAM;
390 } else if (type == TYPE_EXTEND) {
391 ep->type = EXFAT_NAME;
392 } else if (type == TYPE_BITMAP) {
393 ep->type = EXFAT_BITMAP;
394 } else if (type == TYPE_UPCASE) {
395 ep->type = EXFAT_UPCASE;
396 } else if (type == TYPE_VOLUME) {
397 ep->type = EXFAT_VOLUME;
398 } else if (type == TYPE_DIR) {
399 ep->type = EXFAT_FILE;
400 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
401 } else if (type == TYPE_FILE) {
402 ep->type = EXFAT_FILE;
403 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
407 static void exfat_init_stream_entry(struct exfat_dentry *ep,
408 unsigned char flags, unsigned int start_clu,
409 unsigned long long size)
411 exfat_set_entry_type(ep, TYPE_STREAM);
412 ep->dentry.stream.flags = flags;
413 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
414 ep->dentry.stream.valid_size = cpu_to_le64(size);
415 ep->dentry.stream.size = cpu_to_le64(size);
418 static void exfat_init_name_entry(struct exfat_dentry *ep,
419 unsigned short *uniname)
423 exfat_set_entry_type(ep, TYPE_EXTEND);
424 ep->dentry.name.flags = 0x0;
426 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
427 if (*uniname != 0x0) {
428 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
431 ep->dentry.name.unicode_0_14[i] = 0x0;
436 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
437 int entry, unsigned int type, unsigned int start_clu,
438 unsigned long long size)
440 struct super_block *sb = inode->i_sb;
441 struct exfat_sb_info *sbi = EXFAT_SB(sb);
442 struct timespec64 ts = current_time(inode);
444 struct exfat_dentry *ep;
445 struct buffer_head *bh;
448 * We cannot use exfat_get_dentry_set here because file ep is not
451 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
455 exfat_set_entry_type(ep, type);
456 exfat_set_entry_time(sbi, &ts,
457 &ep->dentry.file.create_tz,
458 &ep->dentry.file.create_time,
459 &ep->dentry.file.create_date,
460 &ep->dentry.file.create_time_cs);
461 exfat_set_entry_time(sbi, &ts,
462 &ep->dentry.file.modify_tz,
463 &ep->dentry.file.modify_time,
464 &ep->dentry.file.modify_date,
465 &ep->dentry.file.modify_time_cs);
466 exfat_set_entry_time(sbi, &ts,
467 &ep->dentry.file.access_tz,
468 &ep->dentry.file.access_time,
469 &ep->dentry.file.access_date,
472 exfat_update_bh(bh, IS_DIRSYNC(inode));
475 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
479 exfat_init_stream_entry(ep,
480 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
482 exfat_update_bh(bh, IS_DIRSYNC(inode));
488 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
491 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, §or);
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, NULL);
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;
530 unsigned short *uniname = p_uniname->name;
531 struct exfat_dentry *ep;
532 struct buffer_head *bh;
533 int sync = IS_DIRSYNC(inode);
535 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
539 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
540 exfat_update_bh(bh, sync);
543 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
547 ep->dentry.stream.name_len = p_uniname->name_len;
548 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
549 exfat_update_bh(bh, sync);
552 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
553 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
557 exfat_init_name_entry(ep, uniname);
558 exfat_update_bh(bh, sync);
560 uniname += EXFAT_FILE_NAME_LEN;
563 exfat_update_dir_chksum(inode, p_dir, entry);
567 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
568 int entry, int order, int num_entries)
570 struct super_block *sb = inode->i_sb;
573 struct exfat_dentry *ep;
574 struct buffer_head *bh;
576 for (i = order; i < num_entries; i++) {
577 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
581 exfat_set_entry_type(ep, TYPE_DELETED);
582 exfat_update_bh(bh, IS_DIRSYNC(inode));
589 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
591 int chksum_type = CS_DIR_ENTRY, i;
592 unsigned short chksum = 0;
593 struct exfat_dentry *ep;
595 for (i = 0; i < es->num_entries; i++) {
596 ep = exfat_get_dentry_cached(es, i);
597 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
599 chksum_type = CS_DEFAULT;
601 ep = exfat_get_dentry_cached(es, 0);
602 ep->dentry.file.checksum = cpu_to_le16(chksum);
606 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
611 err = exfat_update_bhs(es->bh, es->num_bh, sync);
613 for (i = 0; i < es->num_bh; i++)
622 static int exfat_walk_fat_chain(struct super_block *sb,
623 struct exfat_chain *p_dir, unsigned int byte_offset,
626 struct exfat_sb_info *sbi = EXFAT_SB(sb);
627 unsigned int clu_offset;
628 unsigned int cur_clu;
630 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
631 cur_clu = p_dir->dir;
633 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
634 cur_clu += clu_offset;
636 while (clu_offset > 0) {
637 if (exfat_get_next_cluster(sb, &cur_clu))
639 if (cur_clu == EXFAT_EOF_CLUSTER) {
641 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
643 EXFAT_B_TO_DEN(byte_offset));
654 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
655 int entry, sector_t *sector, int *offset)
658 unsigned int off, clu = 0;
659 struct exfat_sb_info *sbi = EXFAT_SB(sb);
661 off = EXFAT_DEN_TO_B(entry);
663 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
667 /* byte offset in cluster */
668 off = EXFAT_CLU_OFFSET(off, sbi);
670 /* byte offset in sector */
671 *offset = EXFAT_BLK_OFFSET(off, sb);
673 /* sector offset in cluster */
674 *sector = EXFAT_B_TO_BLK(off, sb);
675 *sector += exfat_cluster_to_sector(sbi, clu);
679 #define EXFAT_MAX_RA_SIZE (128*1024)
680 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
682 struct exfat_sb_info *sbi = EXFAT_SB(sb);
683 struct buffer_head *bh;
684 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
685 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
686 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
687 unsigned int ra_count = min(adj_ra_count, max_ra_count);
689 /* Read-ahead is not required */
690 if (sbi->sect_per_clus == 1)
693 if (sec < sbi->data_start_sector) {
694 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
695 (unsigned long long)sec, sbi->data_start_sector);
699 /* Not sector aligned with ra_count, resize ra_count to page size */
700 if ((sec - sbi->data_start_sector) & (ra_count - 1))
701 ra_count = page_ra_count;
703 bh = sb_find_get_block(sb, sec);
704 if (!bh || !buffer_uptodate(bh)) {
707 for (i = 0; i < ra_count; i++)
708 sb_breadahead(sb, (sector_t)(sec + i));
714 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
715 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
718 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
722 if (p_dir->dir == DIR_DELETED) {
723 exfat_err(sb, "abnormal access to deleted dentry");
727 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
730 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
731 !(entry & (dentries_per_page - 1)))
732 exfat_dir_readahead(sb, sec);
734 *bh = sb_bread(sb, sec);
740 return (struct exfat_dentry *)((*bh)->b_data + off);
743 enum exfat_validate_dentry_mode {
745 ES_MODE_GET_FILE_ENTRY,
746 ES_MODE_GET_STRM_ENTRY,
747 ES_MODE_GET_NAME_ENTRY,
748 ES_MODE_GET_CRITICAL_SEC_ENTRY,
751 static bool exfat_validate_entry(unsigned int type,
752 enum exfat_validate_dentry_mode *mode)
754 if (type == TYPE_UNUSED || type == TYPE_DELETED)
758 case ES_MODE_STARTED:
759 if (type != TYPE_FILE && type != TYPE_DIR)
761 *mode = ES_MODE_GET_FILE_ENTRY;
763 case ES_MODE_GET_FILE_ENTRY:
764 if (type != TYPE_STREAM)
766 *mode = ES_MODE_GET_STRM_ENTRY;
768 case ES_MODE_GET_STRM_ENTRY:
769 if (type != TYPE_EXTEND)
771 *mode = ES_MODE_GET_NAME_ENTRY;
773 case ES_MODE_GET_NAME_ENTRY:
774 if (type == TYPE_STREAM)
776 if (type != TYPE_EXTEND) {
777 if (!(type & TYPE_CRITICAL_SEC))
779 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
782 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
783 if (type == TYPE_EXTEND || type == TYPE_STREAM)
785 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
794 struct exfat_dentry *exfat_get_dentry_cached(
795 struct exfat_entry_set_cache *es, int num)
797 int off = es->start_off + num * DENTRY_SIZE;
798 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
799 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
801 return (struct exfat_dentry *)p;
805 * Returns a set of dentries for a file or dir.
807 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
808 * User should call exfat_get_dentry_set() after setting 'modified' to apply
809 * changes made in this entry set to the real device.
812 * sb+p_dir+entry: indicates a file/dir
813 * type: specifies how many dentries should be included.
815 * pointer of entry set on success,
818 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
819 struct exfat_chain *p_dir, int entry, unsigned int type)
822 unsigned int off, byte_offset, clu = 0;
824 struct exfat_sb_info *sbi = EXFAT_SB(sb);
825 struct exfat_entry_set_cache *es;
826 struct exfat_dentry *ep;
828 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
829 struct buffer_head *bh;
831 if (p_dir->dir == DIR_DELETED) {
832 exfat_err(sb, "access to deleted dentry");
836 byte_offset = EXFAT_DEN_TO_B(entry);
837 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
841 es = kzalloc(sizeof(*es), GFP_KERNEL);
845 es->modified = false;
847 /* byte offset in cluster */
848 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
850 /* byte offset in sector */
851 off = EXFAT_BLK_OFFSET(byte_offset, sb);
854 /* sector offset in cluster */
855 sec = EXFAT_B_TO_BLK(byte_offset, sb);
856 sec += exfat_cluster_to_sector(sbi, clu);
858 bh = sb_bread(sb, sec);
861 es->bh[es->num_bh++] = bh;
863 ep = exfat_get_dentry_cached(es, 0);
864 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
867 num_entries = type == ES_ALL_ENTRIES ?
868 ep->dentry.file.num_ext + 1 : type;
869 es->num_entries = num_entries;
871 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
872 for (i = 1; i < num_bh; i++) {
873 /* get the next sector */
874 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
875 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
877 else if (exfat_get_next_cluster(sb, &clu))
879 sec = exfat_cluster_to_sector(sbi, clu);
884 bh = sb_bread(sb, sec);
887 es->bh[es->num_bh++] = bh;
890 /* validiate cached dentries */
891 for (i = 1; i < num_entries; i++) {
892 ep = exfat_get_dentry_cached(es, i);
893 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
899 exfat_free_dentry_set(es, false);
912 * >= 0 : return dir entiry position with the name in dir
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)
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, NULL);
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 if (type == TYPE_ALL || type == entry_type) {
996 num_ext = ep->dentry.file.num_ext;
997 step = DIRENT_STEP_STRM;
1003 if (entry_type == TYPE_STREAM) {
1006 if (step != DIRENT_STEP_STRM) {
1007 step = DIRENT_STEP_FILE;
1011 step = DIRENT_STEP_FILE;
1012 name_hash = le16_to_cpu(
1013 ep->dentry.stream.name_hash);
1014 if (p_uniname->name_hash == name_hash &&
1015 p_uniname->name_len ==
1016 ep->dentry.stream.name_len) {
1017 step = DIRENT_STEP_NAME;
1026 if (entry_type == TYPE_EXTEND) {
1027 unsigned short entry_uniname[16], unichar;
1029 if (step != DIRENT_STEP_NAME) {
1030 step = DIRENT_STEP_FILE;
1035 uniname = p_uniname->name;
1037 uniname += EXFAT_FILE_NAME_LEN;
1039 len = exfat_extract_uni_name(ep, entry_uniname);
1042 unichar = *(uniname+len);
1043 *(uniname+len) = 0x0;
1045 if (exfat_uniname_ncmp(sb, uniname,
1046 entry_uniname, len)) {
1047 step = DIRENT_STEP_FILE;
1048 } else if (p_uniname->name_len == name_len) {
1049 if (order == num_ext)
1051 step = DIRENT_STEP_SECD;
1054 *(uniname+len) = unichar;
1059 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1060 if (step == DIRENT_STEP_SECD) {
1061 if (++order == num_ext)
1066 step = DIRENT_STEP_FILE;
1069 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1073 clu.dir = EXFAT_EOF_CLUSTER;
1075 if (exfat_get_next_cluster(sb, &clu.dir))
1082 * We started at not 0 index,so we should try to find target
1083 * from 0 index to the index we started at.
1085 if (!rewind && end_eidx) {
1088 clu.dir = p_dir->dir;
1089 /* reset empty hint */
1091 candi_empty.eidx = EXFAT_HINT_NONE;
1095 /* initialized hint_stat */
1096 hint_stat->clu = p_dir->dir;
1097 hint_stat->eidx = 0;
1101 /* next dentry we'll find is out of this cluster */
1102 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1105 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1109 clu.dir = EXFAT_EOF_CLUSTER;
1111 ret = exfat_get_next_cluster(sb, &clu.dir);
1114 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1115 /* just initialized hint_stat */
1116 hint_stat->clu = p_dir->dir;
1117 hint_stat->eidx = 0;
1118 return (dentry - num_ext);
1122 hint_stat->clu = clu.dir;
1123 hint_stat->eidx = dentry + 1;
1124 return dentry - num_ext;
1127 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1128 int entry, struct exfat_dentry *ep)
1132 struct exfat_dentry *ext_ep;
1133 struct buffer_head *bh;
1135 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1136 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1140 type = exfat_get_entry_type(ext_ep);
1142 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1150 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1153 int dentries_per_clu;
1154 unsigned int entry_type;
1155 struct exfat_chain clu;
1156 struct exfat_dentry *ep;
1157 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1158 struct buffer_head *bh;
1160 dentries_per_clu = sbi->dentries_per_clu;
1162 exfat_chain_dup(&clu, p_dir);
1164 while (clu.dir != EXFAT_EOF_CLUSTER) {
1165 for (i = 0; i < dentries_per_clu; i++) {
1166 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1169 entry_type = exfat_get_entry_type(ep);
1172 if (entry_type == TYPE_UNUSED)
1174 if (entry_type != TYPE_DIR)
1179 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1183 clu.dir = EXFAT_EOF_CLUSTER;
1185 if (exfat_get_next_cluster(sb, &(clu.dir)))