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;
68 struct exfat_chain dir, clu;
69 struct exfat_uni_name uni_name;
70 struct exfat_dentry *ep;
71 struct super_block *sb = inode->i_sb;
72 struct exfat_sb_info *sbi = EXFAT_SB(sb);
73 struct exfat_inode_info *ei = EXFAT_I(inode);
74 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
75 struct buffer_head *bh;
77 /* check if the given file ID is opened */
78 if (ei->type != TYPE_DIR)
82 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
84 exfat_chain_set(&dir, ei->start_clu,
85 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
87 dentries_per_clu = sbi->dentries_per_clu;
88 dentries_per_clu_bits = ilog2(dentries_per_clu);
90 clu_offset = dentry >> dentries_per_clu_bits;
91 exfat_chain_dup(&clu, &dir);
93 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
94 clu.dir += clu_offset;
95 clu.size -= clu_offset;
97 /* hint_information */
98 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
99 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
100 clu_offset -= ei->hint_bmap.off;
101 clu.dir = ei->hint_bmap.clu;
104 while (clu_offset > 0) {
105 if (exfat_get_next_cluster(sb, &(clu.dir)))
112 while (clu.dir != EXFAT_EOF_CLUSTER) {
113 i = dentry & (dentries_per_clu - 1);
115 for ( ; i < dentries_per_clu; i++, dentry++) {
116 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
120 type = exfat_get_entry_type(ep);
121 if (type == TYPE_UNUSED) {
126 if (type != TYPE_FILE && type != TYPE_DIR) {
131 num_ext = ep->dentry.file.num_ext;
132 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
133 exfat_get_entry_time(sbi, &dir_entry->crtime,
134 ep->dentry.file.create_tz,
135 ep->dentry.file.create_time,
136 ep->dentry.file.create_date,
137 ep->dentry.file.create_time_cs);
138 exfat_get_entry_time(sbi, &dir_entry->mtime,
139 ep->dentry.file.modify_tz,
140 ep->dentry.file.modify_time,
141 ep->dentry.file.modify_date,
142 ep->dentry.file.modify_time_cs);
143 exfat_get_entry_time(sbi, &dir_entry->atime,
144 ep->dentry.file.access_tz,
145 ep->dentry.file.access_time,
146 ep->dentry.file.access_date,
149 *uni_name.name = 0x0;
150 exfat_get_uniname_from_ext_entry(sb, &clu, i,
152 exfat_utf16_to_nls(sb, &uni_name,
153 dir_entry->namebuf.lfn,
154 dir_entry->namebuf.lfnbuf_len);
157 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
161 le64_to_cpu(ep->dentry.stream.valid_size);
162 dir_entry->entry = dentry;
165 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
166 ei->hint_bmap.clu = clu.dir;
168 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
172 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
176 clu.dir = EXFAT_EOF_CLUSTER;
178 if (exfat_get_next_cluster(sb, &(clu.dir)))
183 dir_entry->namebuf.lfn[0] = '\0';
184 *cpos = EXFAT_DEN_TO_B(dentry);
188 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
194 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
196 nb->lfn = __getname();
199 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
203 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
209 exfat_init_namebuf(nb);
212 /* skip iterating emit_dots when dir is empty */
213 #define ITER_POS_FILLED_DOTS (2)
214 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
216 struct inode *inode = filp->f_path.dentry->d_inode;
217 struct super_block *sb = inode->i_sb;
219 struct exfat_dir_entry de;
220 struct exfat_dentry_namebuf *nb = &(de.namebuf);
221 struct exfat_inode_info *ei = EXFAT_I(inode);
224 int err = 0, fake_offset = 0;
226 exfat_init_namebuf(nb);
227 mutex_lock(&EXFAT_SB(sb)->s_lock);
230 if (!dir_emit_dots(filp, ctx))
233 if (ctx->pos == ITER_POS_FILLED_DOTS) {
238 if (cpos & (DENTRY_SIZE - 1)) {
243 /* name buffer should be allocated before use */
244 err = exfat_alloc_namebuf(nb);
248 if (cpos >= i_size_read(inode))
251 err = exfat_readdir(inode, &cpos, &de);
254 * At least we tried to read a sector. Move cpos to next sector
255 * position (should be aligned).
258 cpos += 1 << (sb->s_blocksize_bits);
259 cpos &= ~(sb->s_blocksize - 1);
269 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
270 tmp = exfat_iget(sb, i_pos);
275 inum = iunique(sb, EXFAT_ROOT_INO);
279 * Before calling dir_emit(), sb_lock should be released.
280 * Because page fault can occur in dir_emit() when the size
281 * of buffer given from user is larger than one page size.
283 mutex_unlock(&EXFAT_SB(sb)->s_lock);
284 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
285 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
287 mutex_lock(&EXFAT_SB(sb)->s_lock);
292 if (!cpos && fake_offset)
293 cpos = ITER_POS_FILLED_DOTS;
296 mutex_unlock(&EXFAT_SB(sb)->s_lock);
299 * To improve performance, free namebuf after unlock sb_lock.
300 * If namebuf is not allocated, this function do nothing
302 exfat_free_namebuf(nb);
306 const struct file_operations exfat_dir_operations = {
307 .llseek = generic_file_llseek,
308 .read = generic_read_dir,
309 .iterate = exfat_iterate,
310 .unlocked_ioctl = exfat_ioctl,
312 .compat_ioctl = exfat_compat_ioctl,
314 .fsync = exfat_file_fsync,
317 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
321 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
323 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
327 return exfat_zeroed_cluster(inode, clu->dir);
330 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
334 len = p_uniname->name_len;
338 /* 1 file entry + 1 stream entry + name entries */
339 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
342 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
344 if (ep->type == EXFAT_UNUSED)
346 if (IS_EXFAT_DELETED(ep->type))
348 if (ep->type == EXFAT_INVAL)
350 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
351 if (ep->type == EXFAT_BITMAP)
353 if (ep->type == EXFAT_UPCASE)
355 if (ep->type == EXFAT_VOLUME)
357 if (ep->type == EXFAT_FILE) {
358 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
362 return TYPE_CRITICAL_PRI;
364 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
365 if (ep->type == EXFAT_GUID)
367 if (ep->type == EXFAT_PADDING)
369 if (ep->type == EXFAT_ACLTAB)
371 return TYPE_BENIGN_PRI;
373 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
374 if (ep->type == EXFAT_STREAM)
376 if (ep->type == EXFAT_NAME)
378 if (ep->type == EXFAT_ACL)
380 return TYPE_CRITICAL_SEC;
382 return TYPE_BENIGN_SEC;
385 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
387 if (type == TYPE_UNUSED) {
388 ep->type = EXFAT_UNUSED;
389 } else if (type == TYPE_DELETED) {
390 ep->type &= EXFAT_DELETE;
391 } else if (type == TYPE_STREAM) {
392 ep->type = EXFAT_STREAM;
393 } else if (type == TYPE_EXTEND) {
394 ep->type = EXFAT_NAME;
395 } else if (type == TYPE_BITMAP) {
396 ep->type = EXFAT_BITMAP;
397 } else if (type == TYPE_UPCASE) {
398 ep->type = EXFAT_UPCASE;
399 } else if (type == TYPE_VOLUME) {
400 ep->type = EXFAT_VOLUME;
401 } else if (type == TYPE_DIR) {
402 ep->type = EXFAT_FILE;
403 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
404 } else if (type == TYPE_FILE) {
405 ep->type = EXFAT_FILE;
406 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
410 static void exfat_init_stream_entry(struct exfat_dentry *ep,
411 unsigned char flags, unsigned int start_clu,
412 unsigned long long size)
414 exfat_set_entry_type(ep, TYPE_STREAM);
415 ep->dentry.stream.flags = flags;
416 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
417 ep->dentry.stream.valid_size = cpu_to_le64(size);
418 ep->dentry.stream.size = cpu_to_le64(size);
421 static void exfat_init_name_entry(struct exfat_dentry *ep,
422 unsigned short *uniname)
426 exfat_set_entry_type(ep, TYPE_EXTEND);
427 ep->dentry.name.flags = 0x0;
429 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
430 if (*uniname != 0x0) {
431 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
434 ep->dentry.name.unicode_0_14[i] = 0x0;
439 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
440 int entry, unsigned int type, unsigned int start_clu,
441 unsigned long long size)
443 struct super_block *sb = inode->i_sb;
444 struct exfat_sb_info *sbi = EXFAT_SB(sb);
445 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, §or);
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, §or);
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;
499 struct exfat_dentry *ep, *fep;
500 struct buffer_head *fbh, *bh;
502 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or);
506 num_entries = fep->dentry.file.num_ext + 1;
507 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
509 for (i = 1; i < num_entries; i++) {
510 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
515 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
520 fep->dentry.file.checksum = cpu_to_le16(chksum);
521 exfat_update_bh(fbh, IS_DIRSYNC(inode));
527 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
528 int entry, int num_entries, struct exfat_uni_name *p_uniname)
530 struct super_block *sb = inode->i_sb;
533 unsigned short *uniname = p_uniname->name;
534 struct exfat_dentry *ep;
535 struct buffer_head *bh;
536 int sync = IS_DIRSYNC(inode);
538 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
542 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
543 exfat_update_bh(bh, sync);
546 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
550 ep->dentry.stream.name_len = p_uniname->name_len;
551 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
552 exfat_update_bh(bh, sync);
555 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
556 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
560 exfat_init_name_entry(ep, uniname);
561 exfat_update_bh(bh, sync);
563 uniname += EXFAT_FILE_NAME_LEN;
566 exfat_update_dir_chksum(inode, p_dir, entry);
570 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
571 int entry, int order, int num_entries)
573 struct super_block *sb = inode->i_sb;
576 struct exfat_dentry *ep;
577 struct buffer_head *bh;
579 for (i = order; i < num_entries; i++) {
580 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
584 exfat_set_entry_type(ep, TYPE_DELETED);
585 exfat_update_bh(bh, IS_DIRSYNC(inode));
592 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
594 int chksum_type = CS_DIR_ENTRY, i;
595 unsigned short chksum = 0;
596 struct exfat_dentry *ep;
598 for (i = 0; i < es->num_entries; i++) {
599 ep = exfat_get_dentry_cached(es, i);
600 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
602 chksum_type = CS_DEFAULT;
604 ep = exfat_get_dentry_cached(es, 0);
605 ep->dentry.file.checksum = cpu_to_le16(chksum);
609 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
614 err = exfat_update_bhs(es->bh, es->num_bh, sync);
616 for (i = 0; i < es->num_bh; i++)
625 static int exfat_walk_fat_chain(struct super_block *sb,
626 struct exfat_chain *p_dir, unsigned int byte_offset,
629 struct exfat_sb_info *sbi = EXFAT_SB(sb);
630 unsigned int clu_offset;
631 unsigned int cur_clu;
633 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
634 cur_clu = p_dir->dir;
636 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
637 cur_clu += clu_offset;
639 while (clu_offset > 0) {
640 if (exfat_get_next_cluster(sb, &cur_clu))
642 if (cur_clu == EXFAT_EOF_CLUSTER) {
644 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
646 EXFAT_B_TO_DEN(byte_offset));
657 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
658 int entry, sector_t *sector, int *offset)
661 unsigned int off, clu = 0;
662 struct exfat_sb_info *sbi = EXFAT_SB(sb);
664 off = EXFAT_DEN_TO_B(entry);
666 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
670 /* byte offset in cluster */
671 off = EXFAT_CLU_OFFSET(off, sbi);
673 /* byte offset in sector */
674 *offset = EXFAT_BLK_OFFSET(off, sb);
676 /* sector offset in cluster */
677 *sector = EXFAT_B_TO_BLK(off, sb);
678 *sector += exfat_cluster_to_sector(sbi, clu);
682 #define EXFAT_MAX_RA_SIZE (128*1024)
683 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
685 struct exfat_sb_info *sbi = EXFAT_SB(sb);
686 struct buffer_head *bh;
687 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
688 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
689 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
690 unsigned int ra_count = min(adj_ra_count, max_ra_count);
692 /* Read-ahead is not required */
693 if (sbi->sect_per_clus == 1)
696 if (sec < sbi->data_start_sector) {
697 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
698 (unsigned long long)sec, sbi->data_start_sector);
702 /* Not sector aligned with ra_count, resize ra_count to page size */
703 if ((sec - sbi->data_start_sector) & (ra_count - 1))
704 ra_count = page_ra_count;
706 bh = sb_find_get_block(sb, sec);
707 if (!bh || !buffer_uptodate(bh)) {
710 for (i = 0; i < ra_count; i++)
711 sb_breadahead(sb, (sector_t)(sec + i));
717 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
718 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
721 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
725 if (p_dir->dir == DIR_DELETED) {
726 exfat_err(sb, "abnormal access to deleted dentry");
730 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
733 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
734 !(entry & (dentries_per_page - 1)))
735 exfat_dir_readahead(sb, sec);
737 *bh = sb_bread(sb, sec);
743 return (struct exfat_dentry *)((*bh)->b_data + off);
746 enum exfat_validate_dentry_mode {
748 ES_MODE_GET_FILE_ENTRY,
749 ES_MODE_GET_STRM_ENTRY,
750 ES_MODE_GET_NAME_ENTRY,
751 ES_MODE_GET_CRITICAL_SEC_ENTRY,
754 static bool exfat_validate_entry(unsigned int type,
755 enum exfat_validate_dentry_mode *mode)
757 if (type == TYPE_UNUSED || type == TYPE_DELETED)
761 case ES_MODE_STARTED:
762 if (type != TYPE_FILE && type != TYPE_DIR)
764 *mode = ES_MODE_GET_FILE_ENTRY;
766 case ES_MODE_GET_FILE_ENTRY:
767 if (type != TYPE_STREAM)
769 *mode = ES_MODE_GET_STRM_ENTRY;
771 case ES_MODE_GET_STRM_ENTRY:
772 if (type != TYPE_EXTEND)
774 *mode = ES_MODE_GET_NAME_ENTRY;
776 case ES_MODE_GET_NAME_ENTRY:
777 if (type == TYPE_STREAM)
779 if (type != TYPE_EXTEND) {
780 if (!(type & TYPE_CRITICAL_SEC))
782 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
785 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
786 if (type == TYPE_EXTEND || type == TYPE_STREAM)
788 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
797 struct exfat_dentry *exfat_get_dentry_cached(
798 struct exfat_entry_set_cache *es, int num)
800 int off = es->start_off + num * DENTRY_SIZE;
801 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
802 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
804 return (struct exfat_dentry *)p;
808 * Returns a set of dentries for a file or dir.
810 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
811 * User should call exfat_get_dentry_set() after setting 'modified' to apply
812 * changes made in this entry set to the real device.
815 * sb+p_dir+entry: indicates a file/dir
816 * type: specifies how many dentries should be included.
818 * pointer of entry set on success,
821 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
822 struct exfat_chain *p_dir, int entry, unsigned int type)
825 unsigned int off, byte_offset, clu = 0;
827 struct exfat_sb_info *sbi = EXFAT_SB(sb);
828 struct exfat_entry_set_cache *es;
829 struct exfat_dentry *ep;
831 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
832 struct buffer_head *bh;
834 if (p_dir->dir == DIR_DELETED) {
835 exfat_err(sb, "access to deleted dentry");
839 byte_offset = EXFAT_DEN_TO_B(entry);
840 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
844 es = kzalloc(sizeof(*es), GFP_KERNEL);
848 es->modified = false;
850 /* byte offset in cluster */
851 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
853 /* byte offset in sector */
854 off = EXFAT_BLK_OFFSET(byte_offset, sb);
857 /* sector offset in cluster */
858 sec = EXFAT_B_TO_BLK(byte_offset, sb);
859 sec += exfat_cluster_to_sector(sbi, clu);
861 bh = sb_bread(sb, sec);
864 es->bh[es->num_bh++] = bh;
866 ep = exfat_get_dentry_cached(es, 0);
867 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
870 num_entries = type == ES_ALL_ENTRIES ?
871 ep->dentry.file.num_ext + 1 : type;
872 es->num_entries = num_entries;
874 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
875 for (i = 1; i < num_bh; i++) {
876 /* get the next sector */
877 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
878 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
880 else if (exfat_get_next_cluster(sb, &clu))
882 sec = exfat_cluster_to_sector(sbi, clu);
887 bh = sb_bread(sb, sec);
890 es->bh[es->num_bh++] = bh;
893 /* validiate cached dentries */
894 for (i = 1; i < num_entries; i++) {
895 ep = exfat_get_dentry_cached(es, i);
896 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
902 exfat_free_dentry_set(es, false);
914 * @ei: inode info of parent directory
915 * @p_dir: directory structure of parent directory
916 * @num_entries:entry size of p_uniname
917 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
918 * for traversing cluster chain.
920 * >= 0: file directory entry position where the name exists
921 * -ENOENT: entry with the name does not exist
924 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
925 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
926 int num_entries, unsigned int type, struct exfat_hint *hint_opt)
928 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
929 int order, step, name_len = 0;
930 int dentries_per_clu, num_empty = 0;
931 unsigned int entry_type;
932 unsigned short *uniname = NULL;
933 struct exfat_chain clu;
934 struct exfat_hint *hint_stat = &ei->hint_stat;
935 struct exfat_hint_femp candi_empty;
936 struct exfat_sb_info *sbi = EXFAT_SB(sb);
938 dentries_per_clu = sbi->dentries_per_clu;
940 exfat_chain_dup(&clu, p_dir);
942 if (hint_stat->eidx) {
943 clu.dir = hint_stat->clu;
944 dentry = hint_stat->eidx;
948 candi_empty.eidx = EXFAT_HINT_NONE;
951 step = DIRENT_STEP_FILE;
952 while (clu.dir != EXFAT_EOF_CLUSTER) {
953 i = dentry & (dentries_per_clu - 1);
954 for (; i < dentries_per_clu; i++, dentry++) {
955 struct exfat_dentry *ep;
956 struct buffer_head *bh;
958 if (rewind && dentry == end_eidx)
961 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
965 entry_type = exfat_get_entry_type(ep);
967 if (entry_type == TYPE_UNUSED ||
968 entry_type == TYPE_DELETED) {
969 step = DIRENT_STEP_FILE;
972 if (candi_empty.eidx == EXFAT_HINT_NONE &&
974 exfat_chain_set(&candi_empty.cur,
975 clu.dir, clu.size, clu.flags);
978 if (candi_empty.eidx == EXFAT_HINT_NONE &&
979 num_empty >= num_entries) {
981 dentry - (num_empty - 1);
982 WARN_ON(candi_empty.eidx < 0);
983 candi_empty.count = num_empty;
985 if (ei->hint_femp.eidx ==
989 ei->hint_femp = candi_empty;
993 if (entry_type == TYPE_UNUSED)
999 candi_empty.eidx = EXFAT_HINT_NONE;
1001 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1002 step = DIRENT_STEP_FILE;
1003 hint_opt->clu = clu.dir;
1005 if (type == TYPE_ALL || type == entry_type) {
1006 num_ext = ep->dentry.file.num_ext;
1007 step = DIRENT_STEP_STRM;
1013 if (entry_type == TYPE_STREAM) {
1016 if (step != DIRENT_STEP_STRM) {
1017 step = DIRENT_STEP_FILE;
1021 step = DIRENT_STEP_FILE;
1022 name_hash = le16_to_cpu(
1023 ep->dentry.stream.name_hash);
1024 if (p_uniname->name_hash == name_hash &&
1025 p_uniname->name_len ==
1026 ep->dentry.stream.name_len) {
1027 step = DIRENT_STEP_NAME;
1036 if (entry_type == TYPE_EXTEND) {
1037 unsigned short entry_uniname[16], unichar;
1039 if (step != DIRENT_STEP_NAME) {
1040 step = DIRENT_STEP_FILE;
1045 uniname = p_uniname->name;
1047 uniname += EXFAT_FILE_NAME_LEN;
1049 len = exfat_extract_uni_name(ep, entry_uniname);
1052 unichar = *(uniname+len);
1053 *(uniname+len) = 0x0;
1055 if (exfat_uniname_ncmp(sb, uniname,
1056 entry_uniname, len)) {
1057 step = DIRENT_STEP_FILE;
1058 } else if (p_uniname->name_len == name_len) {
1059 if (order == num_ext)
1061 step = DIRENT_STEP_SECD;
1064 *(uniname+len) = unichar;
1069 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1070 if (step == DIRENT_STEP_SECD) {
1071 if (++order == num_ext)
1076 step = DIRENT_STEP_FILE;
1079 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1083 clu.dir = EXFAT_EOF_CLUSTER;
1085 if (exfat_get_next_cluster(sb, &clu.dir))
1092 * We started at not 0 index,so we should try to find target
1093 * from 0 index to the index we started at.
1095 if (!rewind && end_eidx) {
1098 clu.dir = p_dir->dir;
1099 /* reset empty hint */
1101 candi_empty.eidx = EXFAT_HINT_NONE;
1105 /* initialized hint_stat */
1106 hint_stat->clu = p_dir->dir;
1107 hint_stat->eidx = 0;
1111 /* next dentry we'll find is out of this cluster */
1112 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1115 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1119 clu.dir = EXFAT_EOF_CLUSTER;
1121 ret = exfat_get_next_cluster(sb, &clu.dir);
1124 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1125 /* just initialized hint_stat */
1126 hint_stat->clu = p_dir->dir;
1127 hint_stat->eidx = 0;
1128 return (dentry - num_ext);
1132 hint_stat->clu = clu.dir;
1133 hint_stat->eidx = dentry + 1;
1134 return dentry - num_ext;
1137 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1138 int entry, struct exfat_dentry *ep)
1142 struct exfat_dentry *ext_ep;
1143 struct buffer_head *bh;
1145 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1146 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1150 type = exfat_get_entry_type(ext_ep);
1152 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1160 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1163 int dentries_per_clu;
1164 unsigned int entry_type;
1165 struct exfat_chain clu;
1166 struct exfat_dentry *ep;
1167 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1168 struct buffer_head *bh;
1170 dentries_per_clu = sbi->dentries_per_clu;
1172 exfat_chain_dup(&clu, p_dir);
1174 while (clu.dir != EXFAT_EOF_CLUSTER) {
1175 for (i = 0; i < dentries_per_clu; i++) {
1176 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1179 entry_type = exfat_get_entry_type(ep);
1182 if (entry_type == TYPE_UNUSED)
1184 if (entry_type != TYPE_DIR)
1189 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1193 clu.dir = EXFAT_EOF_CLUSTER;
1195 if (exfat_get_next_cluster(sb, &(clu.dir)))