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;
36 unsigned int uni_len = 0, len;
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 len = exfat_extract_uni_name(ep, uniname);
57 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
59 uniname += EXFAT_FILE_NAME_LEN;
62 exfat_free_dentry_set(es, false);
65 /* read a directory entry from the opened directory */
66 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
68 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
69 unsigned int type, clu_offset, max_dentries;
71 struct exfat_chain dir, clu;
72 struct exfat_uni_name uni_name;
73 struct exfat_dentry *ep;
74 struct super_block *sb = inode->i_sb;
75 struct exfat_sb_info *sbi = EXFAT_SB(sb);
76 struct exfat_inode_info *ei = EXFAT_I(inode);
77 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
78 struct buffer_head *bh;
80 /* check if the given file ID is opened */
81 if (ei->type != TYPE_DIR)
85 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
87 exfat_chain_set(&dir, ei->start_clu,
88 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
90 dentries_per_clu = sbi->dentries_per_clu;
91 dentries_per_clu_bits = ilog2(dentries_per_clu);
92 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
93 (u64)sbi->num_clusters << dentries_per_clu_bits);
95 clu_offset = dentry >> dentries_per_clu_bits;
96 exfat_chain_dup(&clu, &dir);
98 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
99 clu.dir += clu_offset;
100 clu.size -= clu_offset;
102 /* hint_information */
103 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
104 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
105 clu_offset -= ei->hint_bmap.off;
106 clu.dir = ei->hint_bmap.clu;
109 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
110 if (exfat_get_next_cluster(sb, &(clu.dir)))
117 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
118 i = dentry & (dentries_per_clu - 1);
120 for ( ; i < dentries_per_clu; i++, dentry++) {
121 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
125 type = exfat_get_entry_type(ep);
126 if (type == TYPE_UNUSED) {
131 if (type != TYPE_FILE && type != TYPE_DIR) {
136 num_ext = ep->dentry.file.num_ext;
137 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
138 exfat_get_entry_time(sbi, &dir_entry->crtime,
139 ep->dentry.file.create_tz,
140 ep->dentry.file.create_time,
141 ep->dentry.file.create_date,
142 ep->dentry.file.create_time_cs);
143 exfat_get_entry_time(sbi, &dir_entry->mtime,
144 ep->dentry.file.modify_tz,
145 ep->dentry.file.modify_time,
146 ep->dentry.file.modify_date,
147 ep->dentry.file.modify_time_cs);
148 exfat_get_entry_time(sbi, &dir_entry->atime,
149 ep->dentry.file.access_tz,
150 ep->dentry.file.access_time,
151 ep->dentry.file.access_date,
154 *uni_name.name = 0x0;
155 exfat_get_uniname_from_ext_entry(sb, &clu, i,
157 exfat_utf16_to_nls(sb, &uni_name,
158 dir_entry->namebuf.lfn,
159 dir_entry->namebuf.lfnbuf_len);
162 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
166 le64_to_cpu(ep->dentry.stream.valid_size);
167 dir_entry->entry = dentry;
170 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
171 ei->hint_bmap.clu = clu.dir;
173 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
177 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
181 clu.dir = EXFAT_EOF_CLUSTER;
183 if (exfat_get_next_cluster(sb, &(clu.dir)))
188 dir_entry->namebuf.lfn[0] = '\0';
189 *cpos = EXFAT_DEN_TO_B(dentry);
193 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
199 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
201 nb->lfn = __getname();
204 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
208 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
214 exfat_init_namebuf(nb);
218 * Before calling dir_emit*(), sbi->s_lock should be released
219 * because page fault can occur in dir_emit*().
221 #define ITER_POS_FILLED_DOTS (2)
222 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
224 struct inode *inode = filp->f_path.dentry->d_inode;
225 struct super_block *sb = inode->i_sb;
227 struct exfat_dir_entry de;
228 struct exfat_dentry_namebuf *nb = &(de.namebuf);
229 struct exfat_inode_info *ei = EXFAT_I(inode);
232 int err = 0, fake_offset = 0;
234 exfat_init_namebuf(nb);
237 if (!dir_emit_dots(filp, ctx))
240 if (ctx->pos == ITER_POS_FILLED_DOTS) {
245 cpos = round_up(cpos, DENTRY_SIZE);
247 /* name buffer should be allocated before use */
248 err = exfat_alloc_namebuf(nb);
252 mutex_lock(&EXFAT_SB(sb)->s_lock);
254 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
257 err = exfat_readdir(inode, &cpos, &de);
260 * At least we tried to read a sector.
261 * Move cpos to next sector position (should be aligned).
264 cpos += 1 << (sb->s_blocksize_bits);
265 cpos &= ~(sb->s_blocksize - 1);
275 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
276 tmp = exfat_iget(sb, i_pos);
281 inum = iunique(sb, EXFAT_ROOT_INO);
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))
292 if (!cpos && fake_offset)
293 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++)
617 if (IS_DYNAMIC_ES(es))
624 static int exfat_walk_fat_chain(struct super_block *sb,
625 struct exfat_chain *p_dir, unsigned int byte_offset,
628 struct exfat_sb_info *sbi = EXFAT_SB(sb);
629 unsigned int clu_offset;
630 unsigned int cur_clu;
632 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
633 cur_clu = p_dir->dir;
635 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
636 cur_clu += clu_offset;
638 while (clu_offset > 0) {
639 if (exfat_get_next_cluster(sb, &cur_clu))
641 if (cur_clu == EXFAT_EOF_CLUSTER) {
643 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
645 EXFAT_B_TO_DEN(byte_offset));
656 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
657 int entry, sector_t *sector, int *offset)
660 unsigned int off, clu = 0;
661 struct exfat_sb_info *sbi = EXFAT_SB(sb);
663 off = EXFAT_DEN_TO_B(entry);
665 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
669 /* byte offset in cluster */
670 off = EXFAT_CLU_OFFSET(off, sbi);
672 /* byte offset in sector */
673 *offset = EXFAT_BLK_OFFSET(off, sb);
675 /* sector offset in cluster */
676 *sector = EXFAT_B_TO_BLK(off, sb);
677 *sector += exfat_cluster_to_sector(sbi, clu);
681 #define EXFAT_MAX_RA_SIZE (128*1024)
682 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
684 struct exfat_sb_info *sbi = EXFAT_SB(sb);
685 struct buffer_head *bh;
686 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
687 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
688 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
689 unsigned int ra_count = min(adj_ra_count, max_ra_count);
691 /* Read-ahead is not required */
692 if (sbi->sect_per_clus == 1)
695 if (sec < sbi->data_start_sector) {
696 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
697 (unsigned long long)sec, sbi->data_start_sector);
701 /* Not sector aligned with ra_count, resize ra_count to page size */
702 if ((sec - sbi->data_start_sector) & (ra_count - 1))
703 ra_count = page_ra_count;
705 bh = sb_find_get_block(sb, sec);
706 if (!bh || !buffer_uptodate(bh)) {
709 for (i = 0; i < ra_count; i++)
710 sb_breadahead(sb, (sector_t)(sec + i));
716 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
717 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
720 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
724 if (p_dir->dir == DIR_DELETED) {
725 exfat_err(sb, "abnormal access to deleted dentry");
729 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
732 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
733 !(entry & (dentries_per_page - 1)))
734 exfat_dir_readahead(sb, sec);
736 *bh = sb_bread(sb, sec);
742 return (struct exfat_dentry *)((*bh)->b_data + off);
745 enum exfat_validate_dentry_mode {
747 ES_MODE_GET_FILE_ENTRY,
748 ES_MODE_GET_STRM_ENTRY,
749 ES_MODE_GET_NAME_ENTRY,
750 ES_MODE_GET_CRITICAL_SEC_ENTRY,
753 static bool exfat_validate_entry(unsigned int type,
754 enum exfat_validate_dentry_mode *mode)
756 if (type == TYPE_UNUSED || type == TYPE_DELETED)
760 case ES_MODE_STARTED:
761 if (type != TYPE_FILE && type != TYPE_DIR)
763 *mode = ES_MODE_GET_FILE_ENTRY;
765 case ES_MODE_GET_FILE_ENTRY:
766 if (type != TYPE_STREAM)
768 *mode = ES_MODE_GET_STRM_ENTRY;
770 case ES_MODE_GET_STRM_ENTRY:
771 if (type != TYPE_EXTEND)
773 *mode = ES_MODE_GET_NAME_ENTRY;
775 case ES_MODE_GET_NAME_ENTRY:
776 if (type == TYPE_STREAM)
778 if (type != TYPE_EXTEND) {
779 if (!(type & TYPE_CRITICAL_SEC))
781 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
784 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
785 if (type == TYPE_EXTEND || type == TYPE_STREAM)
787 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
796 struct exfat_dentry *exfat_get_dentry_cached(
797 struct exfat_entry_set_cache *es, int num)
799 int off = es->start_off + num * DENTRY_SIZE;
800 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
801 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
803 return (struct exfat_dentry *)p;
807 * Returns a set of dentries for a file or dir.
809 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
810 * User should call exfat_get_dentry_set() after setting 'modified' to apply
811 * changes made in this entry set to the real device.
814 * sb+p_dir+entry: indicates a file/dir
815 * type: specifies how many dentries should be included.
817 * pointer of entry set on success,
820 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
821 struct exfat_chain *p_dir, int entry, unsigned int type)
824 unsigned int off, byte_offset, clu = 0;
826 struct exfat_sb_info *sbi = EXFAT_SB(sb);
827 struct exfat_entry_set_cache *es;
828 struct exfat_dentry *ep;
830 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
831 struct buffer_head *bh;
833 if (p_dir->dir == DIR_DELETED) {
834 exfat_err(sb, "access to deleted dentry");
838 byte_offset = EXFAT_DEN_TO_B(entry);
839 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
843 es = kzalloc(sizeof(*es), GFP_KERNEL);
847 es->modified = false;
849 /* byte offset in cluster */
850 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
852 /* byte offset in sector */
853 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 if (num_bh > ARRAY_SIZE(es->__bh)) {
876 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
885 for (i = 1; i < num_bh; i++) {
886 /* get the next sector */
887 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
888 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
890 else if (exfat_get_next_cluster(sb, &clu))
892 sec = exfat_cluster_to_sector(sbi, clu);
897 bh = sb_bread(sb, sec);
900 es->bh[es->num_bh++] = bh;
903 /* validiate cached dentries */
904 for (i = 1; i < num_entries; i++) {
905 ep = exfat_get_dentry_cached(es, i);
906 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
912 exfat_free_dentry_set(es, false);
924 * @ei: inode info of parent directory
925 * @p_dir: directory structure of parent directory
926 * @num_entries:entry size of p_uniname
927 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
928 * for traversing cluster chain.
930 * >= 0: file directory entry position where the name exists
931 * -ENOENT: entry with the name does not exist
934 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
935 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
936 int num_entries, unsigned int type, struct exfat_hint *hint_opt)
938 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
939 int order, step, name_len = 0;
940 int dentries_per_clu, num_empty = 0;
941 unsigned int entry_type;
942 unsigned short *uniname = NULL;
943 struct exfat_chain clu;
944 struct exfat_hint *hint_stat = &ei->hint_stat;
945 struct exfat_hint_femp candi_empty;
946 struct exfat_sb_info *sbi = EXFAT_SB(sb);
948 dentries_per_clu = sbi->dentries_per_clu;
950 exfat_chain_dup(&clu, p_dir);
952 if (hint_stat->eidx) {
953 clu.dir = hint_stat->clu;
954 dentry = hint_stat->eidx;
958 candi_empty.eidx = EXFAT_HINT_NONE;
961 step = DIRENT_STEP_FILE;
962 while (clu.dir != EXFAT_EOF_CLUSTER) {
963 i = dentry & (dentries_per_clu - 1);
964 for (; i < dentries_per_clu; i++, dentry++) {
965 struct exfat_dentry *ep;
966 struct buffer_head *bh;
968 if (rewind && dentry == end_eidx)
971 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
975 entry_type = exfat_get_entry_type(ep);
977 if (entry_type == TYPE_UNUSED ||
978 entry_type == TYPE_DELETED) {
979 step = DIRENT_STEP_FILE;
982 if (candi_empty.eidx == EXFAT_HINT_NONE &&
984 exfat_chain_set(&candi_empty.cur,
985 clu.dir, clu.size, clu.flags);
988 if (candi_empty.eidx == EXFAT_HINT_NONE &&
989 num_empty >= num_entries) {
991 dentry - (num_empty - 1);
992 WARN_ON(candi_empty.eidx < 0);
993 candi_empty.count = num_empty;
995 if (ei->hint_femp.eidx ==
999 ei->hint_femp = candi_empty;
1003 if (entry_type == TYPE_UNUSED)
1009 candi_empty.eidx = EXFAT_HINT_NONE;
1011 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1012 step = DIRENT_STEP_FILE;
1013 hint_opt->clu = clu.dir;
1015 if (type == TYPE_ALL || type == entry_type) {
1016 num_ext = ep->dentry.file.num_ext;
1017 step = DIRENT_STEP_STRM;
1023 if (entry_type == TYPE_STREAM) {
1026 if (step != DIRENT_STEP_STRM) {
1027 step = DIRENT_STEP_FILE;
1031 step = DIRENT_STEP_FILE;
1032 name_hash = le16_to_cpu(
1033 ep->dentry.stream.name_hash);
1034 if (p_uniname->name_hash == name_hash &&
1035 p_uniname->name_len ==
1036 ep->dentry.stream.name_len) {
1037 step = DIRENT_STEP_NAME;
1046 if (entry_type == TYPE_EXTEND) {
1047 unsigned short entry_uniname[16], unichar;
1049 if (step != DIRENT_STEP_NAME ||
1050 name_len >= MAX_NAME_LENGTH) {
1051 step = DIRENT_STEP_FILE;
1056 uniname = p_uniname->name;
1058 uniname += EXFAT_FILE_NAME_LEN;
1060 len = exfat_extract_uni_name(ep, entry_uniname);
1063 unichar = *(uniname+len);
1064 *(uniname+len) = 0x0;
1066 if (exfat_uniname_ncmp(sb, uniname,
1067 entry_uniname, len)) {
1068 step = DIRENT_STEP_FILE;
1069 } else if (p_uniname->name_len == name_len) {
1070 if (order == num_ext)
1072 step = DIRENT_STEP_SECD;
1075 *(uniname+len) = unichar;
1080 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1081 if (step == DIRENT_STEP_SECD) {
1082 if (++order == num_ext)
1087 step = DIRENT_STEP_FILE;
1090 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1094 clu.dir = EXFAT_EOF_CLUSTER;
1096 if (exfat_get_next_cluster(sb, &clu.dir))
1103 * We started at not 0 index,so we should try to find target
1104 * from 0 index to the index we started at.
1106 if (!rewind && end_eidx) {
1109 clu.dir = p_dir->dir;
1110 /* reset empty hint */
1112 candi_empty.eidx = EXFAT_HINT_NONE;
1116 /* initialized hint_stat */
1117 hint_stat->clu = p_dir->dir;
1118 hint_stat->eidx = 0;
1122 /* next dentry we'll find is out of this cluster */
1123 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1126 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1130 clu.dir = EXFAT_EOF_CLUSTER;
1132 ret = exfat_get_next_cluster(sb, &clu.dir);
1135 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1136 /* just initialized hint_stat */
1137 hint_stat->clu = p_dir->dir;
1138 hint_stat->eidx = 0;
1139 return (dentry - num_ext);
1143 hint_stat->clu = clu.dir;
1144 hint_stat->eidx = dentry + 1;
1145 return dentry - num_ext;
1148 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1149 int entry, struct exfat_dentry *ep)
1153 struct exfat_dentry *ext_ep;
1154 struct buffer_head *bh;
1156 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1157 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1161 type = exfat_get_entry_type(ext_ep);
1163 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1171 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1174 int dentries_per_clu;
1175 unsigned int entry_type;
1176 struct exfat_chain clu;
1177 struct exfat_dentry *ep;
1178 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1179 struct buffer_head *bh;
1181 dentries_per_clu = sbi->dentries_per_clu;
1183 exfat_chain_dup(&clu, p_dir);
1185 while (clu.dir != EXFAT_EOF_CLUSTER) {
1186 for (i = 0; i < dentries_per_clu; i++) {
1187 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1190 entry_type = exfat_get_entry_type(ep);
1193 if (entry_type == TYPE_UNUSED)
1195 if (entry_type != TYPE_DIR)
1200 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1204 clu.dir = EXFAT_EOF_CLUSTER;
1206 if (exfat_get_next_cluster(sb, &(clu.dir)))