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;
38 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
43 * First entry : file entry
44 * Second entry : stream-extension entry
45 * Third entry : first file-name entry
46 * So, the index of first file-name dentry should start from 2.
48 for (i = 2; i < es->num_entries; i++) {
49 struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
51 /* end of name entry */
52 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
55 exfat_extract_uni_name(ep, uniname);
56 uniname += EXFAT_FILE_NAME_LEN;
59 exfat_free_dentry_set(es, false);
62 /* read a directory entry from the opened directory */
63 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
65 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
66 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);
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, &clu, i,
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);
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 .unlocked_ioctl = exfat_ioctl,
313 .compat_ioctl = exfat_compat_ioctl,
315 .fsync = exfat_file_fsync,
318 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
322 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
324 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
328 return exfat_zeroed_cluster(inode, clu->dir);
331 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
335 len = p_uniname->name_len;
339 /* 1 file entry + 1 stream entry + name entries */
340 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
343 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
345 if (ep->type == EXFAT_UNUSED)
347 if (IS_EXFAT_DELETED(ep->type))
349 if (ep->type == EXFAT_INVAL)
351 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
352 if (ep->type == EXFAT_BITMAP)
354 if (ep->type == EXFAT_UPCASE)
356 if (ep->type == EXFAT_VOLUME)
358 if (ep->type == EXFAT_FILE) {
359 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
363 return TYPE_CRITICAL_PRI;
365 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
366 if (ep->type == EXFAT_GUID)
368 if (ep->type == EXFAT_PADDING)
370 if (ep->type == EXFAT_ACLTAB)
372 return TYPE_BENIGN_PRI;
374 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
375 if (ep->type == EXFAT_STREAM)
377 if (ep->type == EXFAT_NAME)
379 if (ep->type == EXFAT_ACL)
381 return TYPE_CRITICAL_SEC;
383 return TYPE_BENIGN_SEC;
386 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
388 if (type == TYPE_UNUSED) {
389 ep->type = EXFAT_UNUSED;
390 } else if (type == TYPE_DELETED) {
391 ep->type &= EXFAT_DELETE;
392 } else if (type == TYPE_STREAM) {
393 ep->type = EXFAT_STREAM;
394 } else if (type == TYPE_EXTEND) {
395 ep->type = EXFAT_NAME;
396 } else if (type == TYPE_BITMAP) {
397 ep->type = EXFAT_BITMAP;
398 } else if (type == TYPE_UPCASE) {
399 ep->type = EXFAT_UPCASE;
400 } else if (type == TYPE_VOLUME) {
401 ep->type = EXFAT_VOLUME;
402 } else if (type == TYPE_DIR) {
403 ep->type = EXFAT_FILE;
404 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
405 } else if (type == TYPE_FILE) {
406 ep->type = EXFAT_FILE;
407 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
411 static void exfat_init_stream_entry(struct exfat_dentry *ep,
412 unsigned char flags, unsigned int start_clu,
413 unsigned long long size)
415 exfat_set_entry_type(ep, TYPE_STREAM);
416 ep->dentry.stream.flags = flags;
417 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
418 ep->dentry.stream.valid_size = cpu_to_le64(size);
419 ep->dentry.stream.size = cpu_to_le64(size);
422 static void exfat_init_name_entry(struct exfat_dentry *ep,
423 unsigned short *uniname)
427 exfat_set_entry_type(ep, TYPE_EXTEND);
428 ep->dentry.name.flags = 0x0;
430 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
431 if (*uniname != 0x0) {
432 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
435 ep->dentry.name.unicode_0_14[i] = 0x0;
440 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
441 int entry, unsigned int type, unsigned int start_clu,
442 unsigned long long size)
444 struct super_block *sb = inode->i_sb;
445 struct exfat_sb_info *sbi = EXFAT_SB(sb);
446 struct timespec64 ts = current_time(inode);
447 struct exfat_dentry *ep;
448 struct buffer_head *bh;
451 * We cannot use exfat_get_dentry_set here because file ep is not
454 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
458 exfat_set_entry_type(ep, type);
459 exfat_set_entry_time(sbi, &ts,
460 &ep->dentry.file.create_tz,
461 &ep->dentry.file.create_time,
462 &ep->dentry.file.create_date,
463 &ep->dentry.file.create_time_cs);
464 exfat_set_entry_time(sbi, &ts,
465 &ep->dentry.file.modify_tz,
466 &ep->dentry.file.modify_time,
467 &ep->dentry.file.modify_date,
468 &ep->dentry.file.modify_time_cs);
469 exfat_set_entry_time(sbi, &ts,
470 &ep->dentry.file.access_tz,
471 &ep->dentry.file.access_time,
472 &ep->dentry.file.access_date,
475 exfat_update_bh(bh, IS_DIRSYNC(inode));
478 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
482 exfat_init_stream_entry(ep,
483 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
485 exfat_update_bh(bh, IS_DIRSYNC(inode));
491 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
494 struct super_block *sb = inode->i_sb;
498 struct exfat_dentry *ep, *fep;
499 struct buffer_head *fbh, *bh;
501 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
505 num_entries = fep->dentry.file.num_ext + 1;
506 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
508 for (i = 1; i < num_entries; i++) {
509 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
514 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
519 fep->dentry.file.checksum = cpu_to_le16(chksum);
520 exfat_update_bh(fbh, IS_DIRSYNC(inode));
526 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
527 int entry, int num_entries, struct exfat_uni_name *p_uniname)
529 struct super_block *sb = inode->i_sb;
531 unsigned short *uniname = p_uniname->name;
532 struct exfat_dentry *ep;
533 struct buffer_head *bh;
534 int sync = IS_DIRSYNC(inode);
536 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
540 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
541 exfat_update_bh(bh, sync);
544 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
548 ep->dentry.stream.name_len = p_uniname->name_len;
549 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
550 exfat_update_bh(bh, sync);
553 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
554 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
558 exfat_init_name_entry(ep, uniname);
559 exfat_update_bh(bh, sync);
561 uniname += EXFAT_FILE_NAME_LEN;
564 exfat_update_dir_chksum(inode, p_dir, entry);
568 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
569 int entry, int order, int num_entries)
571 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);
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 static 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)
717 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
721 if (p_dir->dir == DIR_DELETED) {
722 exfat_err(sb, "abnormal access to deleted dentry");
726 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
729 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
730 !(entry & (dentries_per_page - 1)))
731 exfat_dir_readahead(sb, sec);
733 *bh = sb_bread(sb, sec);
737 return (struct exfat_dentry *)((*bh)->b_data + off);
740 enum exfat_validate_dentry_mode {
742 ES_MODE_GET_FILE_ENTRY,
743 ES_MODE_GET_STRM_ENTRY,
744 ES_MODE_GET_NAME_ENTRY,
745 ES_MODE_GET_CRITICAL_SEC_ENTRY,
748 static bool exfat_validate_entry(unsigned int type,
749 enum exfat_validate_dentry_mode *mode)
751 if (type == TYPE_UNUSED || type == TYPE_DELETED)
755 case ES_MODE_STARTED:
756 if (type != TYPE_FILE && type != TYPE_DIR)
758 *mode = ES_MODE_GET_FILE_ENTRY;
760 case ES_MODE_GET_FILE_ENTRY:
761 if (type != TYPE_STREAM)
763 *mode = ES_MODE_GET_STRM_ENTRY;
765 case ES_MODE_GET_STRM_ENTRY:
766 if (type != TYPE_EXTEND)
768 *mode = ES_MODE_GET_NAME_ENTRY;
770 case ES_MODE_GET_NAME_ENTRY:
771 if (type == TYPE_STREAM)
773 if (type != TYPE_EXTEND) {
774 if (!(type & TYPE_CRITICAL_SEC))
776 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
779 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
780 if (type == TYPE_EXTEND || type == TYPE_STREAM)
782 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
791 struct exfat_dentry *exfat_get_dentry_cached(
792 struct exfat_entry_set_cache *es, int num)
794 int off = es->start_off + num * DENTRY_SIZE;
795 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
796 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
798 return (struct exfat_dentry *)p;
802 * Returns a set of dentries for a file or dir.
804 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
805 * User should call exfat_get_dentry_set() after setting 'modified' to apply
806 * changes made in this entry set to the real device.
809 * sb+p_dir+entry: indicates a file/dir
810 * type: specifies how many dentries should be included.
812 * pointer of entry set on success,
815 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
816 struct exfat_chain *p_dir, int entry, unsigned int type)
819 unsigned int off, byte_offset, clu = 0;
821 struct exfat_sb_info *sbi = EXFAT_SB(sb);
822 struct exfat_entry_set_cache *es;
823 struct exfat_dentry *ep;
825 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
826 struct buffer_head *bh;
828 if (p_dir->dir == DIR_DELETED) {
829 exfat_err(sb, "access to deleted dentry");
833 byte_offset = EXFAT_DEN_TO_B(entry);
834 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
838 es = kzalloc(sizeof(*es), GFP_KERNEL);
842 es->modified = false;
844 /* byte offset in cluster */
845 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
847 /* byte offset in sector */
848 off = EXFAT_BLK_OFFSET(byte_offset, sb);
851 /* sector offset in cluster */
852 sec = EXFAT_B_TO_BLK(byte_offset, sb);
853 sec += exfat_cluster_to_sector(sbi, clu);
855 bh = sb_bread(sb, sec);
858 es->bh[es->num_bh++] = bh;
860 ep = exfat_get_dentry_cached(es, 0);
861 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
864 num_entries = type == ES_ALL_ENTRIES ?
865 ep->dentry.file.num_ext + 1 : type;
866 es->num_entries = num_entries;
868 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
869 for (i = 1; i < num_bh; i++) {
870 /* get the next sector */
871 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
872 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
874 else if (exfat_get_next_cluster(sb, &clu))
876 sec = exfat_cluster_to_sector(sbi, clu);
881 bh = sb_bread(sb, sec);
884 es->bh[es->num_bh++] = bh;
887 /* validate cached dentries */
888 for (i = 1; i < num_entries; i++) {
889 ep = exfat_get_dentry_cached(es, i);
890 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
896 exfat_free_dentry_set(es, false);
908 * @ei: inode info of parent directory
909 * @p_dir: directory structure of parent directory
910 * @num_entries:entry size of p_uniname
911 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
912 * for traversing cluster chain.
914 * >= 0: file directory entry position where the name exists
915 * -ENOENT: entry with the name does not exist
918 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
919 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
920 int num_entries, unsigned int type, struct exfat_hint *hint_opt)
922 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
923 int order, step, name_len = 0;
924 int dentries_per_clu, num_empty = 0;
925 unsigned int entry_type;
926 unsigned short *uniname = NULL;
927 struct exfat_chain clu;
928 struct exfat_hint *hint_stat = &ei->hint_stat;
929 struct exfat_hint_femp candi_empty;
930 struct exfat_sb_info *sbi = EXFAT_SB(sb);
932 dentries_per_clu = sbi->dentries_per_clu;
934 exfat_chain_dup(&clu, p_dir);
936 if (hint_stat->eidx) {
937 clu.dir = hint_stat->clu;
938 dentry = hint_stat->eidx;
942 candi_empty.eidx = EXFAT_HINT_NONE;
945 step = DIRENT_STEP_FILE;
946 while (clu.dir != EXFAT_EOF_CLUSTER) {
947 i = dentry & (dentries_per_clu - 1);
948 for (; i < dentries_per_clu; i++, dentry++) {
949 struct exfat_dentry *ep;
950 struct buffer_head *bh;
952 if (rewind && dentry == end_eidx)
955 ep = exfat_get_dentry(sb, &clu, i, &bh);
959 entry_type = exfat_get_entry_type(ep);
961 if (entry_type == TYPE_UNUSED ||
962 entry_type == TYPE_DELETED) {
963 step = DIRENT_STEP_FILE;
966 if (candi_empty.eidx == EXFAT_HINT_NONE &&
968 exfat_chain_set(&candi_empty.cur,
969 clu.dir, clu.size, clu.flags);
972 if (candi_empty.eidx == EXFAT_HINT_NONE &&
973 num_empty >= num_entries) {
975 dentry - (num_empty - 1);
976 WARN_ON(candi_empty.eidx < 0);
977 candi_empty.count = num_empty;
979 if (ei->hint_femp.eidx ==
983 ei->hint_femp = candi_empty;
987 if (entry_type == TYPE_UNUSED)
993 candi_empty.eidx = EXFAT_HINT_NONE;
995 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
996 step = DIRENT_STEP_FILE;
997 hint_opt->clu = clu.dir;
999 if (type == TYPE_ALL || type == entry_type) {
1000 num_ext = ep->dentry.file.num_ext;
1001 step = DIRENT_STEP_STRM;
1007 if (entry_type == TYPE_STREAM) {
1010 if (step != DIRENT_STEP_STRM) {
1011 step = DIRENT_STEP_FILE;
1015 step = DIRENT_STEP_FILE;
1016 name_hash = le16_to_cpu(
1017 ep->dentry.stream.name_hash);
1018 if (p_uniname->name_hash == name_hash &&
1019 p_uniname->name_len ==
1020 ep->dentry.stream.name_len) {
1021 step = DIRENT_STEP_NAME;
1030 if (entry_type == TYPE_EXTEND) {
1031 unsigned short entry_uniname[16], unichar;
1033 if (step != DIRENT_STEP_NAME) {
1034 step = DIRENT_STEP_FILE;
1039 uniname = p_uniname->name;
1041 uniname += EXFAT_FILE_NAME_LEN;
1043 len = exfat_extract_uni_name(ep, entry_uniname);
1046 unichar = *(uniname+len);
1047 *(uniname+len) = 0x0;
1049 if (exfat_uniname_ncmp(sb, uniname,
1050 entry_uniname, len)) {
1051 step = DIRENT_STEP_FILE;
1052 } else if (p_uniname->name_len == name_len) {
1053 if (order == num_ext)
1055 step = DIRENT_STEP_SECD;
1058 *(uniname+len) = unichar;
1063 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1064 if (step == DIRENT_STEP_SECD) {
1065 if (++order == num_ext)
1070 step = DIRENT_STEP_FILE;
1073 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1077 clu.dir = EXFAT_EOF_CLUSTER;
1079 if (exfat_get_next_cluster(sb, &clu.dir))
1086 * We started at not 0 index,so we should try to find target
1087 * from 0 index to the index we started at.
1089 if (!rewind && end_eidx) {
1092 clu.dir = p_dir->dir;
1093 /* reset empty hint */
1095 candi_empty.eidx = EXFAT_HINT_NONE;
1099 /* initialized hint_stat */
1100 hint_stat->clu = p_dir->dir;
1101 hint_stat->eidx = 0;
1105 /* next dentry we'll find is out of this cluster */
1106 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1109 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1113 clu.dir = EXFAT_EOF_CLUSTER;
1115 ret = exfat_get_next_cluster(sb, &clu.dir);
1118 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1119 /* just initialized hint_stat */
1120 hint_stat->clu = p_dir->dir;
1121 hint_stat->eidx = 0;
1122 return (dentry - num_ext);
1126 hint_stat->clu = clu.dir;
1127 hint_stat->eidx = dentry + 1;
1128 return dentry - num_ext;
1131 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1132 int entry, struct exfat_dentry *ep)
1136 struct exfat_dentry *ext_ep;
1137 struct buffer_head *bh;
1139 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1140 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1144 type = exfat_get_entry_type(ext_ep);
1146 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1154 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1157 int dentries_per_clu;
1158 unsigned int entry_type;
1159 struct exfat_chain clu;
1160 struct exfat_dentry *ep;
1161 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1162 struct buffer_head *bh;
1164 dentries_per_clu = sbi->dentries_per_clu;
1166 exfat_chain_dup(&clu, p_dir);
1168 while (clu.dir != EXFAT_EOF_CLUSTER) {
1169 for (i = 0; i < dentries_per_clu; i++) {
1170 ep = exfat_get_dentry(sb, &clu, i, &bh);
1173 entry_type = exfat_get_entry_type(ep);
1176 if (entry_type == TYPE_UNUSED)
1178 if (entry_type != TYPE_DIR)
1183 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1187 clu.dir = EXFAT_EOF_CLUSTER;
1189 if (exfat_get_next_cluster(sb, &(clu.dir)))