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, struct exfat_dir_entry *dir_entry)
64 int i, dentries_per_clu, dentries_per_clu_bits = 0;
65 unsigned int type, clu_offset;
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 = ei->rwoffset & 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);
89 clu_offset = dentry >> dentries_per_clu_bits;
90 exfat_chain_dup(&clu, &dir);
92 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
93 clu.dir += clu_offset;
94 clu.size -= clu_offset;
96 /* hint_information */
97 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
98 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
99 clu_offset -= ei->hint_bmap.off;
100 clu.dir = ei->hint_bmap.clu;
103 while (clu_offset > 0) {
104 if (exfat_get_next_cluster(sb, &(clu.dir)))
111 while (clu.dir != EXFAT_EOF_CLUSTER) {
112 i = dentry & (dentries_per_clu - 1);
114 for ( ; i < dentries_per_clu; i++, dentry++) {
115 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
119 type = exfat_get_entry_type(ep);
120 if (type == TYPE_UNUSED) {
125 if (type != TYPE_FILE && type != TYPE_DIR) {
130 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
131 exfat_get_entry_time(sbi, &dir_entry->crtime,
132 ep->dentry.file.create_tz,
133 ep->dentry.file.create_time,
134 ep->dentry.file.create_date,
135 ep->dentry.file.create_time_cs);
136 exfat_get_entry_time(sbi, &dir_entry->mtime,
137 ep->dentry.file.modify_tz,
138 ep->dentry.file.modify_time,
139 ep->dentry.file.modify_date,
140 ep->dentry.file.modify_time_cs);
141 exfat_get_entry_time(sbi, &dir_entry->atime,
142 ep->dentry.file.access_tz,
143 ep->dentry.file.access_time,
144 ep->dentry.file.access_date,
147 *uni_name.name = 0x0;
148 exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
150 exfat_utf16_to_nls(sb, &uni_name,
151 dir_entry->namebuf.lfn,
152 dir_entry->namebuf.lfnbuf_len);
155 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
159 le64_to_cpu(ep->dentry.stream.valid_size);
162 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
163 ei->hint_bmap.clu = clu.dir;
165 ei->rwoffset = ++dentry;
169 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
173 clu.dir = EXFAT_EOF_CLUSTER;
175 if (exfat_get_next_cluster(sb, &(clu.dir)))
180 dir_entry->namebuf.lfn[0] = '\0';
181 ei->rwoffset = dentry;
185 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
191 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
193 nb->lfn = __getname();
196 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
200 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
206 exfat_init_namebuf(nb);
209 /* skip iterating emit_dots when dir is empty */
210 #define ITER_POS_FILLED_DOTS (2)
211 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
213 struct inode *inode = filp->f_path.dentry->d_inode;
214 struct super_block *sb = inode->i_sb;
216 struct exfat_dir_entry de;
217 struct exfat_dentry_namebuf *nb = &(de.namebuf);
218 struct exfat_inode_info *ei = EXFAT_I(inode);
221 int err = 0, fake_offset = 0;
223 exfat_init_namebuf(nb);
224 mutex_lock(&EXFAT_SB(sb)->s_lock);
227 if (!dir_emit_dots(filp, ctx))
230 if (ctx->pos == ITER_POS_FILLED_DOTS) {
235 if (cpos & (DENTRY_SIZE - 1)) {
240 /* name buffer should be allocated before use */
241 err = exfat_alloc_namebuf(nb);
245 ei->rwoffset = EXFAT_B_TO_DEN(cpos);
247 if (cpos >= i_size_read(inode))
250 err = exfat_readdir(inode, &de);
253 * At least we tried to read a sector. Move cpos to next sector
254 * position (should be aligned).
257 cpos += 1 << (sb->s_blocksize_bits);
258 cpos &= ~(sb->s_blocksize - 1);
265 cpos = EXFAT_DEN_TO_B(ei->rwoffset);
270 i_pos = ((loff_t)ei->start_clu << 32) |
271 ((ei->rwoffset - 1) & 0xffffffff);
272 tmp = exfat_iget(sb, i_pos);
277 inum = iunique(sb, EXFAT_ROOT_INO);
281 * Before calling dir_emit(), sb_lock should be released.
282 * Because page fault can occur in dir_emit() when the size
283 * of buffer given from user is larger than one page size.
285 mutex_unlock(&EXFAT_SB(sb)->s_lock);
286 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
287 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
289 mutex_lock(&EXFAT_SB(sb)->s_lock);
294 if (!cpos && fake_offset)
295 cpos = ITER_POS_FILLED_DOTS;
298 mutex_unlock(&EXFAT_SB(sb)->s_lock);
301 * To improve performance, free namebuf after unlock sb_lock.
302 * If namebuf is not allocated, this function do nothing
304 exfat_free_namebuf(nb);
308 const struct file_operations exfat_dir_operations = {
309 .llseek = generic_file_llseek,
310 .read = generic_read_dir,
311 .iterate = exfat_iterate,
312 .fsync = exfat_file_fsync,
315 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
319 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
321 ret = exfat_alloc_cluster(inode, 1, clu);
325 return exfat_zeroed_cluster(inode, clu->dir);
328 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
332 len = p_uniname->name_len;
336 /* 1 file entry + 1 stream entry + name entries */
337 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
340 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
342 if (ep->type == EXFAT_UNUSED)
344 if (IS_EXFAT_DELETED(ep->type))
346 if (ep->type == EXFAT_INVAL)
348 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
349 if (ep->type == EXFAT_BITMAP)
351 if (ep->type == EXFAT_UPCASE)
353 if (ep->type == EXFAT_VOLUME)
355 if (ep->type == EXFAT_FILE) {
356 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
360 return TYPE_CRITICAL_PRI;
362 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
363 if (ep->type == EXFAT_GUID)
365 if (ep->type == EXFAT_PADDING)
367 if (ep->type == EXFAT_ACLTAB)
369 return TYPE_BENIGN_PRI;
371 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
372 if (ep->type == EXFAT_STREAM)
374 if (ep->type == EXFAT_NAME)
376 if (ep->type == EXFAT_ACL)
378 return TYPE_CRITICAL_SEC;
380 return TYPE_BENIGN_SEC;
383 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
385 if (type == TYPE_UNUSED) {
386 ep->type = EXFAT_UNUSED;
387 } else if (type == TYPE_DELETED) {
388 ep->type &= EXFAT_DELETE;
389 } else if (type == TYPE_STREAM) {
390 ep->type = EXFAT_STREAM;
391 } else if (type == TYPE_EXTEND) {
392 ep->type = EXFAT_NAME;
393 } else if (type == TYPE_BITMAP) {
394 ep->type = EXFAT_BITMAP;
395 } else if (type == TYPE_UPCASE) {
396 ep->type = EXFAT_UPCASE;
397 } else if (type == TYPE_VOLUME) {
398 ep->type = EXFAT_VOLUME;
399 } else if (type == TYPE_DIR) {
400 ep->type = EXFAT_FILE;
401 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
402 } else if (type == TYPE_FILE) {
403 ep->type = EXFAT_FILE;
404 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
408 static void exfat_init_stream_entry(struct exfat_dentry *ep,
409 unsigned char flags, unsigned int start_clu,
410 unsigned long long size)
412 exfat_set_entry_type(ep, TYPE_STREAM);
413 ep->dentry.stream.flags = flags;
414 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
415 ep->dentry.stream.valid_size = cpu_to_le64(size);
416 ep->dentry.stream.size = cpu_to_le64(size);
419 static void exfat_init_name_entry(struct exfat_dentry *ep,
420 unsigned short *uniname)
424 exfat_set_entry_type(ep, TYPE_EXTEND);
425 ep->dentry.name.flags = 0x0;
427 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
428 if (*uniname != 0x0) {
429 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
432 ep->dentry.name.unicode_0_14[i] = 0x0;
437 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
438 int entry, unsigned int type, unsigned int start_clu,
439 unsigned long long size)
441 struct super_block *sb = inode->i_sb;
442 struct exfat_sb_info *sbi = EXFAT_SB(sb);
443 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, §or);
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, §or);
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;
497 struct exfat_dentry *ep, *fep;
498 struct buffer_head *fbh, *bh;
500 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or);
504 num_entries = fep->dentry.file.num_ext + 1;
505 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
507 for (i = 1; i < num_entries; i++) {
508 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
513 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
518 fep->dentry.file.checksum = cpu_to_le16(chksum);
519 exfat_update_bh(fbh, IS_DIRSYNC(inode));
525 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
526 int entry, int num_entries, struct exfat_uni_name *p_uniname)
528 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, §or);
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, §or);
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, §or);
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;
574 struct exfat_dentry *ep;
575 struct buffer_head *bh;
577 for (i = order; i < num_entries; i++) {
578 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
582 exfat_set_entry_type(ep, TYPE_DELETED);
583 exfat_update_bh(bh, IS_DIRSYNC(inode));
590 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
592 int chksum_type = CS_DIR_ENTRY, i;
593 unsigned short chksum = 0;
594 struct exfat_dentry *ep;
596 for (i = 0; i < es->num_entries; i++) {
597 ep = exfat_get_dentry_cached(es, i);
598 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
600 chksum_type = CS_DEFAULT;
602 ep = exfat_get_dentry_cached(es, 0);
603 ep->dentry.file.checksum = cpu_to_le16(chksum);
607 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
612 err = exfat_update_bhs(es->bh, es->num_bh, sync);
614 for (i = 0; i < es->num_bh; i++)
623 static int exfat_walk_fat_chain(struct super_block *sb,
624 struct exfat_chain *p_dir, unsigned int byte_offset,
627 struct exfat_sb_info *sbi = EXFAT_SB(sb);
628 unsigned int clu_offset;
629 unsigned int cur_clu;
631 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
632 cur_clu = p_dir->dir;
634 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
635 cur_clu += clu_offset;
637 while (clu_offset > 0) {
638 if (exfat_get_next_cluster(sb, &cur_clu))
640 if (cur_clu == EXFAT_EOF_CLUSTER) {
642 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
644 EXFAT_B_TO_DEN(byte_offset));
655 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
656 int entry, sector_t *sector, int *offset)
659 unsigned int off, clu = 0;
660 struct exfat_sb_info *sbi = EXFAT_SB(sb);
662 off = EXFAT_DEN_TO_B(entry);
664 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
668 /* byte offset in cluster */
669 off = EXFAT_CLU_OFFSET(off, sbi);
671 /* byte offset in sector */
672 *offset = EXFAT_BLK_OFFSET(off, sb);
674 /* sector offset in cluster */
675 *sector = EXFAT_B_TO_BLK(off, sb);
676 *sector += exfat_cluster_to_sector(sbi, clu);
680 #define EXFAT_MAX_RA_SIZE (128*1024)
681 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
683 struct exfat_sb_info *sbi = EXFAT_SB(sb);
684 struct buffer_head *bh;
685 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
686 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
687 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
688 unsigned int ra_count = min(adj_ra_count, max_ra_count);
690 /* Read-ahead is not required */
691 if (sbi->sect_per_clus == 1)
694 if (sec < sbi->data_start_sector) {
695 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
696 (unsigned long long)sec, sbi->data_start_sector);
700 /* Not sector aligned with ra_count, resize ra_count to page size */
701 if ((sec - sbi->data_start_sector) & (ra_count - 1))
702 ra_count = page_ra_count;
704 bh = sb_find_get_block(sb, sec);
705 if (!bh || !buffer_uptodate(bh)) {
708 for (i = 0; i < ra_count; i++)
709 sb_breadahead(sb, (sector_t)(sec + i));
715 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
716 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
719 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
723 if (p_dir->dir == DIR_DELETED) {
724 exfat_err(sb, "abnormal access to deleted dentry");
728 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
731 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
732 !(entry & (dentries_per_page - 1)))
733 exfat_dir_readahead(sb, sec);
735 *bh = sb_bread(sb, sec);
741 return (struct exfat_dentry *)((*bh)->b_data + off);
744 enum exfat_validate_dentry_mode {
746 ES_MODE_GET_FILE_ENTRY,
747 ES_MODE_GET_STRM_ENTRY,
748 ES_MODE_GET_NAME_ENTRY,
749 ES_MODE_GET_CRITICAL_SEC_ENTRY,
752 static bool exfat_validate_entry(unsigned int type,
753 enum exfat_validate_dentry_mode *mode)
755 if (type == TYPE_UNUSED || type == TYPE_DELETED)
759 case ES_MODE_STARTED:
760 if (type != TYPE_FILE && type != TYPE_DIR)
762 *mode = ES_MODE_GET_FILE_ENTRY;
764 case ES_MODE_GET_FILE_ENTRY:
765 if (type != TYPE_STREAM)
767 *mode = ES_MODE_GET_STRM_ENTRY;
769 case ES_MODE_GET_STRM_ENTRY:
770 if (type != TYPE_EXTEND)
772 *mode = ES_MODE_GET_NAME_ENTRY;
774 case ES_MODE_GET_NAME_ENTRY:
775 if (type == TYPE_STREAM)
777 if (type != TYPE_EXTEND) {
778 if (!(type & TYPE_CRITICAL_SEC))
780 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
783 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
784 if (type == TYPE_EXTEND || type == TYPE_STREAM)
786 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
795 struct exfat_dentry *exfat_get_dentry_cached(
796 struct exfat_entry_set_cache *es, int num)
798 int off = es->start_off + num * DENTRY_SIZE;
799 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
800 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
802 return (struct exfat_dentry *)p;
806 * Returns a set of dentries for a file or dir.
808 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
809 * User should call exfat_get_dentry_set() after setting 'modified' to apply
810 * changes made in this entry set to the real device.
813 * sb+p_dir+entry: indicates a file/dir
814 * type: specifies how many dentries should be included.
816 * pointer of entry set on success,
819 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
820 struct exfat_chain *p_dir, int entry, unsigned int type)
823 unsigned int off, byte_offset, clu = 0;
825 struct exfat_sb_info *sbi = EXFAT_SB(sb);
826 struct exfat_entry_set_cache *es;
827 struct exfat_dentry *ep;
829 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
830 struct buffer_head *bh;
832 if (p_dir->dir == DIR_DELETED) {
833 exfat_err(sb, "access to deleted dentry");
837 byte_offset = EXFAT_DEN_TO_B(entry);
838 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
842 es = kzalloc(sizeof(*es), GFP_KERNEL);
846 es->modified = false;
848 /* byte offset in cluster */
849 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
851 /* byte offset in sector */
852 off = EXFAT_BLK_OFFSET(byte_offset, sb);
855 /* sector offset in cluster */
856 sec = EXFAT_B_TO_BLK(byte_offset, sb);
857 sec += exfat_cluster_to_sector(sbi, clu);
859 bh = sb_bread(sb, sec);
862 es->bh[es->num_bh++] = bh;
864 ep = exfat_get_dentry_cached(es, 0);
865 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
868 num_entries = type == ES_ALL_ENTRIES ?
869 ep->dentry.file.num_ext + 1 : type;
870 es->num_entries = num_entries;
872 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
873 for (i = 1; i < num_bh; i++) {
874 /* get the next sector */
875 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
876 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
878 else if (exfat_get_next_cluster(sb, &clu))
880 sec = exfat_cluster_to_sector(sbi, clu);
885 bh = sb_bread(sb, sec);
888 es->bh[es->num_bh++] = bh;
891 /* validiate cached dentries */
892 for (i = 1; i < num_entries; i++) {
893 ep = exfat_get_dentry_cached(es, i);
894 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
900 exfat_free_dentry_set(es, false);
913 * >= 0 : return dir entiry position with the name in dir
914 * -EEXIST : (root dir, ".") it is the root dir itself
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)
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, NULL);
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 ==
982 ei->hint_femp.eidx) {
983 memcpy(&ei->hint_femp,
985 sizeof(candi_empty));
990 if (entry_type == TYPE_UNUSED)
996 candi_empty.eidx = EXFAT_HINT_NONE;
998 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
999 step = DIRENT_STEP_FILE;
1000 if (type == TYPE_ALL || type == entry_type) {
1001 num_ext = ep->dentry.file.num_ext;
1002 step = DIRENT_STEP_STRM;
1008 if (entry_type == TYPE_STREAM) {
1011 if (step != DIRENT_STEP_STRM) {
1012 step = DIRENT_STEP_FILE;
1016 step = DIRENT_STEP_FILE;
1017 name_hash = le16_to_cpu(
1018 ep->dentry.stream.name_hash);
1019 if (p_uniname->name_hash == name_hash &&
1020 p_uniname->name_len ==
1021 ep->dentry.stream.name_len) {
1022 step = DIRENT_STEP_NAME;
1031 if (entry_type == TYPE_EXTEND) {
1032 unsigned short entry_uniname[16], unichar;
1034 if (step != DIRENT_STEP_NAME) {
1035 step = DIRENT_STEP_FILE;
1040 uniname = p_uniname->name;
1042 uniname += EXFAT_FILE_NAME_LEN;
1044 len = exfat_extract_uni_name(ep, entry_uniname);
1047 unichar = *(uniname+len);
1048 *(uniname+len) = 0x0;
1050 if (exfat_uniname_ncmp(sb, uniname,
1051 entry_uniname, len)) {
1052 step = DIRENT_STEP_FILE;
1053 } else if (p_uniname->name_len == name_len) {
1054 if (order == num_ext)
1056 step = DIRENT_STEP_SECD;
1059 *(uniname+len) = unichar;
1064 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1065 if (step == DIRENT_STEP_SECD) {
1066 if (++order == num_ext)
1071 step = DIRENT_STEP_FILE;
1074 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1078 clu.dir = EXFAT_EOF_CLUSTER;
1080 if (exfat_get_next_cluster(sb, &clu.dir))
1087 * We started at not 0 index,so we should try to find target
1088 * from 0 index to the index we started at.
1090 if (!rewind && end_eidx) {
1093 clu.dir = p_dir->dir;
1094 /* reset empty hint */
1096 candi_empty.eidx = EXFAT_HINT_NONE;
1100 /* initialized hint_stat */
1101 hint_stat->clu = p_dir->dir;
1102 hint_stat->eidx = 0;
1106 /* next dentry we'll find is out of this cluster */
1107 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1110 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1114 clu.dir = EXFAT_EOF_CLUSTER;
1116 ret = exfat_get_next_cluster(sb, &clu.dir);
1119 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1120 /* just initialized hint_stat */
1121 hint_stat->clu = p_dir->dir;
1122 hint_stat->eidx = 0;
1123 return (dentry - num_ext);
1127 hint_stat->clu = clu.dir;
1128 hint_stat->eidx = dentry + 1;
1129 return dentry - num_ext;
1132 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1133 int entry, struct exfat_dentry *ep)
1137 struct exfat_dentry *ext_ep;
1138 struct buffer_head *bh;
1140 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1141 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1145 type = exfat_get_entry_type(ext_ep);
1147 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1155 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1158 int dentries_per_clu;
1159 unsigned int entry_type;
1160 struct exfat_chain clu;
1161 struct exfat_dentry *ep;
1162 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1163 struct buffer_head *bh;
1165 dentries_per_clu = sbi->dentries_per_clu;
1167 exfat_chain_dup(&clu, p_dir);
1169 while (clu.dir != EXFAT_EOF_CLUSTER) {
1170 for (i = 0; i < dentries_per_clu; i++) {
1171 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1174 entry_type = exfat_get_entry_type(ep);
1177 if (entry_type == TYPE_UNUSED)
1179 if (entry_type != TYPE_DIR)
1184 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1188 clu.dir = EXFAT_EOF_CLUSTER;
1190 if (exfat_get_next_cluster(sb, &(clu.dir)))