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;
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);
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 num_ext = ep->dentry.file.num_ext;
131 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
132 exfat_get_entry_time(sbi, &dir_entry->crtime,
133 ep->dentry.file.create_tz,
134 ep->dentry.file.create_time,
135 ep->dentry.file.create_date,
136 ep->dentry.file.create_time_cs);
137 exfat_get_entry_time(sbi, &dir_entry->mtime,
138 ep->dentry.file.modify_tz,
139 ep->dentry.file.modify_time,
140 ep->dentry.file.modify_date,
141 ep->dentry.file.modify_time_cs);
142 exfat_get_entry_time(sbi, &dir_entry->atime,
143 ep->dentry.file.access_tz,
144 ep->dentry.file.access_time,
145 ep->dentry.file.access_date,
148 *uni_name.name = 0x0;
149 exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
151 exfat_utf16_to_nls(sb, &uni_name,
152 dir_entry->namebuf.lfn,
153 dir_entry->namebuf.lfnbuf_len);
156 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
160 le64_to_cpu(ep->dentry.stream.valid_size);
161 dir_entry->entry = dentry;
164 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
165 ei->hint_bmap.clu = clu.dir;
167 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
171 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
175 clu.dir = EXFAT_EOF_CLUSTER;
177 if (exfat_get_next_cluster(sb, &(clu.dir)))
182 dir_entry->namebuf.lfn[0] = '\0';
183 *cpos = EXFAT_DEN_TO_B(dentry);
187 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
193 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
195 nb->lfn = __getname();
198 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
202 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
208 exfat_init_namebuf(nb);
211 /* skip iterating emit_dots when dir is empty */
212 #define ITER_POS_FILLED_DOTS (2)
213 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
215 struct inode *inode = filp->f_path.dentry->d_inode;
216 struct super_block *sb = inode->i_sb;
218 struct exfat_dir_entry de;
219 struct exfat_dentry_namebuf *nb = &(de.namebuf);
220 struct exfat_inode_info *ei = EXFAT_I(inode);
223 int err = 0, fake_offset = 0;
225 exfat_init_namebuf(nb);
226 mutex_lock(&EXFAT_SB(sb)->s_lock);
229 if (!dir_emit_dots(filp, ctx))
232 if (ctx->pos == ITER_POS_FILLED_DOTS) {
237 if (cpos & (DENTRY_SIZE - 1)) {
242 /* name buffer should be allocated before use */
243 err = exfat_alloc_namebuf(nb);
247 if (cpos >= i_size_read(inode))
250 err = exfat_readdir(inode, &cpos, &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);
268 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
269 tmp = exfat_iget(sb, i_pos);
274 inum = iunique(sb, EXFAT_ROOT_INO);
278 * Before calling dir_emit(), sb_lock should be released.
279 * Because page fault can occur in dir_emit() when the size
280 * of buffer given from user is larger than one page size.
282 mutex_unlock(&EXFAT_SB(sb)->s_lock);
283 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
284 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
286 mutex_lock(&EXFAT_SB(sb)->s_lock);
291 if (!cpos && fake_offset)
292 cpos = ITER_POS_FILLED_DOTS;
295 mutex_unlock(&EXFAT_SB(sb)->s_lock);
298 * To improve performance, free namebuf after unlock sb_lock.
299 * If namebuf is not allocated, this function do nothing
301 exfat_free_namebuf(nb);
305 const struct file_operations exfat_dir_operations = {
306 .llseek = generic_file_llseek,
307 .read = generic_read_dir,
308 .iterate = exfat_iterate,
309 .fsync = exfat_file_fsync,
312 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
316 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
318 ret = exfat_alloc_cluster(inode, 1, clu);
322 return exfat_zeroed_cluster(inode, clu->dir);
325 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
329 len = p_uniname->name_len;
333 /* 1 file entry + 1 stream entry + name entries */
334 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
337 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
339 if (ep->type == EXFAT_UNUSED)
341 if (IS_EXFAT_DELETED(ep->type))
343 if (ep->type == EXFAT_INVAL)
345 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
346 if (ep->type == EXFAT_BITMAP)
348 if (ep->type == EXFAT_UPCASE)
350 if (ep->type == EXFAT_VOLUME)
352 if (ep->type == EXFAT_FILE) {
353 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
357 return TYPE_CRITICAL_PRI;
359 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
360 if (ep->type == EXFAT_GUID)
362 if (ep->type == EXFAT_PADDING)
364 if (ep->type == EXFAT_ACLTAB)
366 return TYPE_BENIGN_PRI;
368 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
369 if (ep->type == EXFAT_STREAM)
371 if (ep->type == EXFAT_NAME)
373 if (ep->type == EXFAT_ACL)
375 return TYPE_CRITICAL_SEC;
377 return TYPE_BENIGN_SEC;
380 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
382 if (type == TYPE_UNUSED) {
383 ep->type = EXFAT_UNUSED;
384 } else if (type == TYPE_DELETED) {
385 ep->type &= EXFAT_DELETE;
386 } else if (type == TYPE_STREAM) {
387 ep->type = EXFAT_STREAM;
388 } else if (type == TYPE_EXTEND) {
389 ep->type = EXFAT_NAME;
390 } else if (type == TYPE_BITMAP) {
391 ep->type = EXFAT_BITMAP;
392 } else if (type == TYPE_UPCASE) {
393 ep->type = EXFAT_UPCASE;
394 } else if (type == TYPE_VOLUME) {
395 ep->type = EXFAT_VOLUME;
396 } else if (type == TYPE_DIR) {
397 ep->type = EXFAT_FILE;
398 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
399 } else if (type == TYPE_FILE) {
400 ep->type = EXFAT_FILE;
401 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
405 static void exfat_init_stream_entry(struct exfat_dentry *ep,
406 unsigned char flags, unsigned int start_clu,
407 unsigned long long size)
409 exfat_set_entry_type(ep, TYPE_STREAM);
410 ep->dentry.stream.flags = flags;
411 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
412 ep->dentry.stream.valid_size = cpu_to_le64(size);
413 ep->dentry.stream.size = cpu_to_le64(size);
416 static void exfat_init_name_entry(struct exfat_dentry *ep,
417 unsigned short *uniname)
421 exfat_set_entry_type(ep, TYPE_EXTEND);
422 ep->dentry.name.flags = 0x0;
424 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
425 if (*uniname != 0x0) {
426 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
429 ep->dentry.name.unicode_0_14[i] = 0x0;
434 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
435 int entry, unsigned int type, unsigned int start_clu,
436 unsigned long long size)
438 struct super_block *sb = inode->i_sb;
439 struct exfat_sb_info *sbi = EXFAT_SB(sb);
440 struct timespec64 ts = current_time(inode);
442 struct exfat_dentry *ep;
443 struct buffer_head *bh;
446 * We cannot use exfat_get_dentry_set here because file ep is not
449 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
453 exfat_set_entry_type(ep, type);
454 exfat_set_entry_time(sbi, &ts,
455 &ep->dentry.file.create_tz,
456 &ep->dentry.file.create_time,
457 &ep->dentry.file.create_date,
458 &ep->dentry.file.create_time_cs);
459 exfat_set_entry_time(sbi, &ts,
460 &ep->dentry.file.modify_tz,
461 &ep->dentry.file.modify_time,
462 &ep->dentry.file.modify_date,
463 &ep->dentry.file.modify_time_cs);
464 exfat_set_entry_time(sbi, &ts,
465 &ep->dentry.file.access_tz,
466 &ep->dentry.file.access_time,
467 &ep->dentry.file.access_date,
470 exfat_update_bh(bh, IS_DIRSYNC(inode));
473 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
477 exfat_init_stream_entry(ep,
478 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
480 exfat_update_bh(bh, IS_DIRSYNC(inode));
486 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
489 struct super_block *sb = inode->i_sb;
494 struct exfat_dentry *ep, *fep;
495 struct buffer_head *fbh, *bh;
497 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or);
501 num_entries = fep->dentry.file.num_ext + 1;
502 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
504 for (i = 1; i < num_entries; i++) {
505 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
510 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
515 fep->dentry.file.checksum = cpu_to_le16(chksum);
516 exfat_update_bh(fbh, IS_DIRSYNC(inode));
522 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
523 int entry, int num_entries, struct exfat_uni_name *p_uniname)
525 struct super_block *sb = inode->i_sb;
528 unsigned short *uniname = p_uniname->name;
529 struct exfat_dentry *ep;
530 struct buffer_head *bh;
531 int sync = IS_DIRSYNC(inode);
533 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
537 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
538 exfat_update_bh(bh, sync);
541 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
545 ep->dentry.stream.name_len = p_uniname->name_len;
546 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
547 exfat_update_bh(bh, sync);
550 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
551 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
555 exfat_init_name_entry(ep, uniname);
556 exfat_update_bh(bh, sync);
558 uniname += EXFAT_FILE_NAME_LEN;
561 exfat_update_dir_chksum(inode, p_dir, entry);
565 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
566 int entry, int order, int num_entries)
568 struct super_block *sb = inode->i_sb;
571 struct exfat_dentry *ep;
572 struct buffer_head *bh;
574 for (i = order; i < num_entries; i++) {
575 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
579 exfat_set_entry_type(ep, TYPE_DELETED);
580 exfat_update_bh(bh, IS_DIRSYNC(inode));
587 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
589 int chksum_type = CS_DIR_ENTRY, i;
590 unsigned short chksum = 0;
591 struct exfat_dentry *ep;
593 for (i = 0; i < es->num_entries; i++) {
594 ep = exfat_get_dentry_cached(es, i);
595 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
597 chksum_type = CS_DEFAULT;
599 ep = exfat_get_dentry_cached(es, 0);
600 ep->dentry.file.checksum = cpu_to_le16(chksum);
604 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
609 err = exfat_update_bhs(es->bh, es->num_bh, sync);
611 for (i = 0; i < es->num_bh; i++)
620 static int exfat_walk_fat_chain(struct super_block *sb,
621 struct exfat_chain *p_dir, unsigned int byte_offset,
624 struct exfat_sb_info *sbi = EXFAT_SB(sb);
625 unsigned int clu_offset;
626 unsigned int cur_clu;
628 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
629 cur_clu = p_dir->dir;
631 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
632 cur_clu += clu_offset;
634 while (clu_offset > 0) {
635 if (exfat_get_next_cluster(sb, &cur_clu))
637 if (cur_clu == EXFAT_EOF_CLUSTER) {
639 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
641 EXFAT_B_TO_DEN(byte_offset));
652 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
653 int entry, sector_t *sector, int *offset)
656 unsigned int off, clu = 0;
657 struct exfat_sb_info *sbi = EXFAT_SB(sb);
659 off = EXFAT_DEN_TO_B(entry);
661 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
665 /* byte offset in cluster */
666 off = EXFAT_CLU_OFFSET(off, sbi);
668 /* byte offset in sector */
669 *offset = EXFAT_BLK_OFFSET(off, sb);
671 /* sector offset in cluster */
672 *sector = EXFAT_B_TO_BLK(off, sb);
673 *sector += exfat_cluster_to_sector(sbi, clu);
677 #define EXFAT_MAX_RA_SIZE (128*1024)
678 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
680 struct exfat_sb_info *sbi = EXFAT_SB(sb);
681 struct buffer_head *bh;
682 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
683 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
684 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
685 unsigned int ra_count = min(adj_ra_count, max_ra_count);
687 /* Read-ahead is not required */
688 if (sbi->sect_per_clus == 1)
691 if (sec < sbi->data_start_sector) {
692 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
693 (unsigned long long)sec, sbi->data_start_sector);
697 /* Not sector aligned with ra_count, resize ra_count to page size */
698 if ((sec - sbi->data_start_sector) & (ra_count - 1))
699 ra_count = page_ra_count;
701 bh = sb_find_get_block(sb, sec);
702 if (!bh || !buffer_uptodate(bh)) {
705 for (i = 0; i < ra_count; i++)
706 sb_breadahead(sb, (sector_t)(sec + i));
712 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
713 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
716 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
720 if (p_dir->dir == DIR_DELETED) {
721 exfat_err(sb, "abnormal access to deleted dentry");
725 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
728 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
729 !(entry & (dentries_per_page - 1)))
730 exfat_dir_readahead(sb, sec);
732 *bh = sb_bread(sb, sec);
738 return (struct exfat_dentry *)((*bh)->b_data + off);
741 enum exfat_validate_dentry_mode {
743 ES_MODE_GET_FILE_ENTRY,
744 ES_MODE_GET_STRM_ENTRY,
745 ES_MODE_GET_NAME_ENTRY,
746 ES_MODE_GET_CRITICAL_SEC_ENTRY,
749 static bool exfat_validate_entry(unsigned int type,
750 enum exfat_validate_dentry_mode *mode)
752 if (type == TYPE_UNUSED || type == TYPE_DELETED)
756 case ES_MODE_STARTED:
757 if (type != TYPE_FILE && type != TYPE_DIR)
759 *mode = ES_MODE_GET_FILE_ENTRY;
761 case ES_MODE_GET_FILE_ENTRY:
762 if (type != TYPE_STREAM)
764 *mode = ES_MODE_GET_STRM_ENTRY;
766 case ES_MODE_GET_STRM_ENTRY:
767 if (type != TYPE_EXTEND)
769 *mode = ES_MODE_GET_NAME_ENTRY;
771 case ES_MODE_GET_NAME_ENTRY:
772 if (type == TYPE_STREAM)
774 if (type != TYPE_EXTEND) {
775 if (!(type & TYPE_CRITICAL_SEC))
777 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
780 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
781 if (type == TYPE_EXTEND || type == TYPE_STREAM)
783 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
792 struct exfat_dentry *exfat_get_dentry_cached(
793 struct exfat_entry_set_cache *es, int num)
795 int off = es->start_off + num * DENTRY_SIZE;
796 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
797 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
799 return (struct exfat_dentry *)p;
803 * Returns a set of dentries for a file or dir.
805 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
806 * User should call exfat_get_dentry_set() after setting 'modified' to apply
807 * changes made in this entry set to the real device.
810 * sb+p_dir+entry: indicates a file/dir
811 * type: specifies how many dentries should be included.
813 * pointer of entry set on success,
816 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
817 struct exfat_chain *p_dir, int entry, unsigned int type)
820 unsigned int off, byte_offset, clu = 0;
822 struct exfat_sb_info *sbi = EXFAT_SB(sb);
823 struct exfat_entry_set_cache *es;
824 struct exfat_dentry *ep;
826 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
827 struct buffer_head *bh;
829 if (p_dir->dir == DIR_DELETED) {
830 exfat_err(sb, "access to deleted dentry");
834 byte_offset = EXFAT_DEN_TO_B(entry);
835 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
839 es = kzalloc(sizeof(*es), GFP_KERNEL);
843 es->modified = false;
845 /* byte offset in cluster */
846 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
848 /* byte offset in sector */
849 off = EXFAT_BLK_OFFSET(byte_offset, sb);
852 /* sector offset in cluster */
853 sec = EXFAT_B_TO_BLK(byte_offset, sb);
854 sec += exfat_cluster_to_sector(sbi, clu);
856 bh = sb_bread(sb, sec);
859 es->bh[es->num_bh++] = bh;
861 ep = exfat_get_dentry_cached(es, 0);
862 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
865 num_entries = type == ES_ALL_ENTRIES ?
866 ep->dentry.file.num_ext + 1 : type;
867 es->num_entries = num_entries;
869 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
870 for (i = 1; i < num_bh; i++) {
871 /* get the next sector */
872 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
873 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
875 else if (exfat_get_next_cluster(sb, &clu))
877 sec = exfat_cluster_to_sector(sbi, clu);
882 bh = sb_bread(sb, sec);
885 es->bh[es->num_bh++] = bh;
888 /* validiate cached dentries */
889 for (i = 1; i < num_entries; i++) {
890 ep = exfat_get_dentry_cached(es, i);
891 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
897 exfat_free_dentry_set(es, false);
910 * >= 0 : return dir entiry position with the name in dir
911 * -ENOENT : entry with the name does not exist
914 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
915 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
916 int num_entries, unsigned int type)
918 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
919 int order, step, name_len = 0;
920 int dentries_per_clu, num_empty = 0;
921 unsigned int entry_type;
922 unsigned short *uniname = NULL;
923 struct exfat_chain clu;
924 struct exfat_hint *hint_stat = &ei->hint_stat;
925 struct exfat_hint_femp candi_empty;
926 struct exfat_sb_info *sbi = EXFAT_SB(sb);
928 dentries_per_clu = sbi->dentries_per_clu;
930 exfat_chain_dup(&clu, p_dir);
932 if (hint_stat->eidx) {
933 clu.dir = hint_stat->clu;
934 dentry = hint_stat->eidx;
938 candi_empty.eidx = EXFAT_HINT_NONE;
941 step = DIRENT_STEP_FILE;
942 while (clu.dir != EXFAT_EOF_CLUSTER) {
943 i = dentry & (dentries_per_clu - 1);
944 for (; i < dentries_per_clu; i++, dentry++) {
945 struct exfat_dentry *ep;
946 struct buffer_head *bh;
948 if (rewind && dentry == end_eidx)
951 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
955 entry_type = exfat_get_entry_type(ep);
957 if (entry_type == TYPE_UNUSED ||
958 entry_type == TYPE_DELETED) {
959 step = DIRENT_STEP_FILE;
962 if (candi_empty.eidx == EXFAT_HINT_NONE &&
964 exfat_chain_set(&candi_empty.cur,
965 clu.dir, clu.size, clu.flags);
968 if (candi_empty.eidx == EXFAT_HINT_NONE &&
969 num_empty >= num_entries) {
971 dentry - (num_empty - 1);
972 WARN_ON(candi_empty.eidx < 0);
973 candi_empty.count = num_empty;
975 if (ei->hint_femp.eidx ==
979 ei->hint_femp = candi_empty;
983 if (entry_type == TYPE_UNUSED)
989 candi_empty.eidx = EXFAT_HINT_NONE;
991 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
992 step = DIRENT_STEP_FILE;
993 if (type == TYPE_ALL || type == entry_type) {
994 num_ext = ep->dentry.file.num_ext;
995 step = DIRENT_STEP_STRM;
1001 if (entry_type == TYPE_STREAM) {
1004 if (step != DIRENT_STEP_STRM) {
1005 step = DIRENT_STEP_FILE;
1009 step = DIRENT_STEP_FILE;
1010 name_hash = le16_to_cpu(
1011 ep->dentry.stream.name_hash);
1012 if (p_uniname->name_hash == name_hash &&
1013 p_uniname->name_len ==
1014 ep->dentry.stream.name_len) {
1015 step = DIRENT_STEP_NAME;
1024 if (entry_type == TYPE_EXTEND) {
1025 unsigned short entry_uniname[16], unichar;
1027 if (step != DIRENT_STEP_NAME) {
1028 step = DIRENT_STEP_FILE;
1033 uniname = p_uniname->name;
1035 uniname += EXFAT_FILE_NAME_LEN;
1037 len = exfat_extract_uni_name(ep, entry_uniname);
1040 unichar = *(uniname+len);
1041 *(uniname+len) = 0x0;
1043 if (exfat_uniname_ncmp(sb, uniname,
1044 entry_uniname, len)) {
1045 step = DIRENT_STEP_FILE;
1046 } else if (p_uniname->name_len == name_len) {
1047 if (order == num_ext)
1049 step = DIRENT_STEP_SECD;
1052 *(uniname+len) = unichar;
1057 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1058 if (step == DIRENT_STEP_SECD) {
1059 if (++order == num_ext)
1064 step = DIRENT_STEP_FILE;
1067 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1071 clu.dir = EXFAT_EOF_CLUSTER;
1073 if (exfat_get_next_cluster(sb, &clu.dir))
1080 * We started at not 0 index,so we should try to find target
1081 * from 0 index to the index we started at.
1083 if (!rewind && end_eidx) {
1086 clu.dir = p_dir->dir;
1087 /* reset empty hint */
1089 candi_empty.eidx = EXFAT_HINT_NONE;
1093 /* initialized hint_stat */
1094 hint_stat->clu = p_dir->dir;
1095 hint_stat->eidx = 0;
1099 /* next dentry we'll find is out of this cluster */
1100 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1103 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1107 clu.dir = EXFAT_EOF_CLUSTER;
1109 ret = exfat_get_next_cluster(sb, &clu.dir);
1112 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1113 /* just initialized hint_stat */
1114 hint_stat->clu = p_dir->dir;
1115 hint_stat->eidx = 0;
1116 return (dentry - num_ext);
1120 hint_stat->clu = clu.dir;
1121 hint_stat->eidx = dentry + 1;
1122 return dentry - num_ext;
1125 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1126 int entry, struct exfat_dentry *ep)
1130 struct exfat_dentry *ext_ep;
1131 struct buffer_head *bh;
1133 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1134 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1138 type = exfat_get_entry_type(ext_ep);
1140 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1148 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1151 int dentries_per_clu;
1152 unsigned int entry_type;
1153 struct exfat_chain clu;
1154 struct exfat_dentry *ep;
1155 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1156 struct buffer_head *bh;
1158 dentries_per_clu = sbi->dentries_per_clu;
1160 exfat_chain_dup(&clu, p_dir);
1162 while (clu.dir != EXFAT_EOF_CLUSTER) {
1163 for (i = 0; i < dentries_per_clu; i++) {
1164 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1167 entry_type = exfat_get_entry_type(ep);
1170 if (entry_type == TYPE_UNUSED)
1172 if (entry_type != TYPE_DIR)
1177 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1181 clu.dir = EXFAT_EOF_CLUSTER;
1183 if (exfat_get_next_cluster(sb, &(clu.dir)))