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_dentry *ep;
36 struct exfat_entry_set_cache *es;
38 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
42 if (es->num_entries < 3)
48 * First entry : file entry
49 * Second entry : stream-extension entry
50 * Third entry : first file-name entry
51 * So, the index of first file-name dentry should start from 2.
53 for (i = 2; i < es->num_entries; i++, ep++) {
54 /* end of name entry */
55 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
58 exfat_extract_uni_name(ep, uniname);
59 uniname += EXFAT_FILE_NAME_LEN;
66 /* read a directory entry from the opened directory */
67 static int exfat_readdir(struct inode *inode, struct exfat_dir_entry *dir_entry)
69 int i, dentries_per_clu, dentries_per_clu_bits = 0;
70 unsigned int type, clu_offset;
72 struct exfat_chain dir, clu;
73 struct exfat_uni_name uni_name;
74 struct exfat_dentry *ep;
75 struct super_block *sb = inode->i_sb;
76 struct exfat_sb_info *sbi = EXFAT_SB(sb);
77 struct exfat_inode_info *ei = EXFAT_I(inode);
78 unsigned int dentry = ei->rwoffset & 0xFFFFFFFF;
79 struct buffer_head *bh;
81 /* check if the given file ID is opened */
82 if (ei->type != TYPE_DIR)
86 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
88 exfat_chain_set(&dir, ei->start_clu,
89 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
91 dentries_per_clu = sbi->dentries_per_clu;
92 dentries_per_clu_bits = ilog2(dentries_per_clu);
94 clu_offset = dentry >> dentries_per_clu_bits;
95 exfat_chain_dup(&clu, &dir);
97 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
98 clu.dir += clu_offset;
99 clu.size -= clu_offset;
101 /* hint_information */
102 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
103 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
104 clu_offset -= ei->hint_bmap.off;
105 clu.dir = ei->hint_bmap.clu;
108 while (clu_offset > 0) {
109 if (exfat_get_next_cluster(sb, &(clu.dir)))
116 while (clu.dir != EXFAT_EOF_CLUSTER) {
117 i = dentry & (dentries_per_clu - 1);
119 for ( ; i < dentries_per_clu; i++, dentry++) {
120 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
124 type = exfat_get_entry_type(ep);
125 if (type == TYPE_UNUSED) {
130 if (type != TYPE_FILE && type != TYPE_DIR) {
135 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
136 exfat_get_entry_time(sbi, &dir_entry->crtime,
137 ep->dentry.file.create_tz,
138 ep->dentry.file.create_time,
139 ep->dentry.file.create_date,
140 ep->dentry.file.create_time_ms);
141 exfat_get_entry_time(sbi, &dir_entry->mtime,
142 ep->dentry.file.modify_tz,
143 ep->dentry.file.modify_time,
144 ep->dentry.file.modify_date,
145 ep->dentry.file.modify_time_ms);
146 exfat_get_entry_time(sbi, &dir_entry->atime,
147 ep->dentry.file.access_tz,
148 ep->dentry.file.access_time,
149 ep->dentry.file.access_date,
152 *uni_name.name = 0x0;
153 exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
155 exfat_utf16_to_nls(sb, &uni_name,
156 dir_entry->namebuf.lfn,
157 dir_entry->namebuf.lfnbuf_len);
160 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
164 le64_to_cpu(ep->dentry.stream.valid_size);
167 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
168 ei->hint_bmap.clu = clu.dir;
170 ei->rwoffset = ++dentry;
174 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
178 clu.dir = EXFAT_EOF_CLUSTER;
180 if (exfat_get_next_cluster(sb, &(clu.dir)))
185 dir_entry->namebuf.lfn[0] = '\0';
186 ei->rwoffset = dentry;
190 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
196 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
198 nb->lfn = __getname();
201 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
205 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
211 exfat_init_namebuf(nb);
214 /* skip iterating emit_dots when dir is empty */
215 #define ITER_POS_FILLED_DOTS (2)
216 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
218 struct inode *inode = filp->f_path.dentry->d_inode;
219 struct super_block *sb = inode->i_sb;
221 struct exfat_dir_entry de;
222 struct exfat_dentry_namebuf *nb = &(de.namebuf);
223 struct exfat_inode_info *ei = EXFAT_I(inode);
226 int err = 0, fake_offset = 0;
228 exfat_init_namebuf(nb);
229 mutex_lock(&EXFAT_SB(sb)->s_lock);
232 if (!dir_emit_dots(filp, ctx))
235 if (ctx->pos == ITER_POS_FILLED_DOTS) {
240 if (cpos & (DENTRY_SIZE - 1)) {
245 /* name buffer should be allocated before use */
246 err = exfat_alloc_namebuf(nb);
250 ei->rwoffset = EXFAT_B_TO_DEN(cpos);
252 if (cpos >= i_size_read(inode))
255 err = exfat_readdir(inode, &de);
258 * At least we tried to read a sector. Move cpos to next sector
259 * position (should be aligned).
262 cpos += 1 << (sb->s_blocksize_bits);
263 cpos &= ~(sb->s_blocksize - 1);
270 cpos = EXFAT_DEN_TO_B(ei->rwoffset);
275 i_pos = ((loff_t)ei->start_clu << 32) |
276 ((ei->rwoffset - 1) & 0xffffffff);
277 tmp = exfat_iget(sb, i_pos);
282 inum = iunique(sb, EXFAT_ROOT_INO);
286 * Before calling dir_emit(), sb_lock should be released.
287 * Because page fault can occur in dir_emit() when the size
288 * of buffer given from user is larger than one page size.
290 mutex_unlock(&EXFAT_SB(sb)->s_lock);
291 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
292 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
294 mutex_lock(&EXFAT_SB(sb)->s_lock);
299 if (!cpos && fake_offset)
300 cpos = ITER_POS_FILLED_DOTS;
303 mutex_unlock(&EXFAT_SB(sb)->s_lock);
306 * To improve performance, free namebuf after unlock sb_lock.
307 * If namebuf is not allocated, this function do nothing
309 exfat_free_namebuf(nb);
313 const struct file_operations exfat_dir_operations = {
314 .llseek = generic_file_llseek,
315 .read = generic_read_dir,
316 .iterate = exfat_iterate,
317 .fsync = generic_file_fsync,
320 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
324 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
326 ret = exfat_alloc_cluster(inode, 1, clu);
330 return exfat_zeroed_cluster(inode, clu->dir);
333 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
337 len = p_uniname->name_len;
341 /* 1 file entry + 1 stream entry + name entries */
342 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
345 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
347 if (ep->type == EXFAT_UNUSED)
349 if (IS_EXFAT_DELETED(ep->type))
351 if (ep->type == EXFAT_INVAL)
353 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
354 if (ep->type == EXFAT_BITMAP)
356 if (ep->type == EXFAT_UPCASE)
358 if (ep->type == EXFAT_VOLUME)
360 if (ep->type == EXFAT_FILE) {
361 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
365 return TYPE_CRITICAL_PRI;
367 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
368 if (ep->type == EXFAT_GUID)
370 if (ep->type == EXFAT_PADDING)
372 if (ep->type == EXFAT_ACLTAB)
374 return TYPE_BENIGN_PRI;
376 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
377 if (ep->type == EXFAT_STREAM)
379 if (ep->type == EXFAT_NAME)
381 if (ep->type == EXFAT_ACL)
383 return TYPE_CRITICAL_SEC;
385 return TYPE_BENIGN_SEC;
388 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
390 if (type == TYPE_UNUSED) {
391 ep->type = EXFAT_UNUSED;
392 } else if (type == TYPE_DELETED) {
393 ep->type &= EXFAT_DELETE;
394 } else if (type == TYPE_STREAM) {
395 ep->type = EXFAT_STREAM;
396 } else if (type == TYPE_EXTEND) {
397 ep->type = EXFAT_NAME;
398 } else if (type == TYPE_BITMAP) {
399 ep->type = EXFAT_BITMAP;
400 } else if (type == TYPE_UPCASE) {
401 ep->type = EXFAT_UPCASE;
402 } else if (type == TYPE_VOLUME) {
403 ep->type = EXFAT_VOLUME;
404 } else if (type == TYPE_DIR) {
405 ep->type = EXFAT_FILE;
406 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
407 } else if (type == TYPE_FILE) {
408 ep->type = EXFAT_FILE;
409 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
413 static void exfat_init_stream_entry(struct exfat_dentry *ep,
414 unsigned char flags, unsigned int start_clu,
415 unsigned long long size)
417 exfat_set_entry_type(ep, TYPE_STREAM);
418 ep->dentry.stream.flags = flags;
419 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
420 ep->dentry.stream.valid_size = cpu_to_le64(size);
421 ep->dentry.stream.size = cpu_to_le64(size);
424 static void exfat_init_name_entry(struct exfat_dentry *ep,
425 unsigned short *uniname)
429 exfat_set_entry_type(ep, TYPE_EXTEND);
430 ep->dentry.name.flags = 0x0;
432 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
433 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
440 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
441 int entry, unsigned int type, unsigned int start_clu,
442 unsigned long long size)
444 struct super_block *sb = inode->i_sb;
445 struct exfat_sb_info *sbi = EXFAT_SB(sb);
446 struct timespec64 ts = current_time(inode);
448 struct exfat_dentry *ep;
449 struct buffer_head *bh;
452 * We cannot use exfat_get_dentry_set here because file ep is not
455 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
459 exfat_set_entry_type(ep, type);
460 exfat_set_entry_time(sbi, &ts,
461 &ep->dentry.file.create_tz,
462 &ep->dentry.file.create_time,
463 &ep->dentry.file.create_date,
464 &ep->dentry.file.create_time_ms);
465 exfat_set_entry_time(sbi, &ts,
466 &ep->dentry.file.modify_tz,
467 &ep->dentry.file.modify_time,
468 &ep->dentry.file.modify_date,
469 &ep->dentry.file.modify_time_ms);
470 exfat_set_entry_time(sbi, &ts,
471 &ep->dentry.file.access_tz,
472 &ep->dentry.file.access_time,
473 &ep->dentry.file.access_date,
476 exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
479 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
483 exfat_init_stream_entry(ep,
484 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
486 exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
492 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
495 struct super_block *sb = inode->i_sb;
499 unsigned short chksum;
500 struct exfat_dentry *ep, *fep;
501 struct buffer_head *fbh, *bh;
503 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or);
507 num_entries = fep->dentry.file.num_ext + 1;
508 chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
510 for (i = 1; i < num_entries; i++) {
511 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
516 chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
521 fep->dentry.file.checksum = cpu_to_le16(chksum);
522 exfat_update_bh(sb, fbh, IS_DIRSYNC(inode));
528 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
529 int entry, int num_entries, struct exfat_uni_name *p_uniname)
531 struct super_block *sb = inode->i_sb;
534 unsigned short *uniname = p_uniname->name;
535 struct exfat_dentry *ep;
536 struct buffer_head *bh;
537 int sync = IS_DIRSYNC(inode);
539 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
543 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
544 exfat_update_bh(sb, bh, sync);
547 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
551 ep->dentry.stream.name_len = p_uniname->name_len;
552 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
553 exfat_update_bh(sb, bh, sync);
556 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
557 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
561 exfat_init_name_entry(ep, uniname);
562 exfat_update_bh(sb, bh, sync);
564 uniname += EXFAT_FILE_NAME_LEN;
567 exfat_update_dir_chksum(inode, p_dir, entry);
571 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
572 int entry, int order, int num_entries)
574 struct super_block *sb = inode->i_sb;
577 struct exfat_dentry *ep;
578 struct buffer_head *bh;
580 for (i = order; i < num_entries; i++) {
581 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
585 exfat_set_entry_type(ep, TYPE_DELETED);
586 exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
593 int exfat_update_dir_chksum_with_entry_set(struct super_block *sb,
594 struct exfat_entry_set_cache *es, int sync)
596 struct exfat_sb_info *sbi = EXFAT_SB(sb);
597 struct buffer_head *bh;
598 sector_t sec = es->sector;
599 unsigned int off = es->offset;
600 int chksum_type = CS_DIR_ENTRY, i, num_entries = es->num_entries;
601 unsigned int buf_off = (off - es->offset);
602 unsigned int remaining_byte_in_sector, copy_entries, clu;
603 unsigned short chksum = 0;
605 for (i = 0; i < num_entries; i++) {
606 chksum = exfat_calc_chksum_2byte(&es->entries[i], DENTRY_SIZE,
607 chksum, chksum_type);
608 chksum_type = CS_DEFAULT;
611 es->entries[0].dentry.file.checksum = cpu_to_le16(chksum);
613 while (num_entries) {
614 /* write per sector base */
615 remaining_byte_in_sector = (1 << sb->s_blocksize_bits) - off;
616 copy_entries = min_t(int,
617 EXFAT_B_TO_DEN(remaining_byte_in_sector),
619 bh = sb_bread(sb, sec);
622 memcpy(bh->b_data + off,
623 (unsigned char *)&es->entries[0] + buf_off,
624 EXFAT_DEN_TO_B(copy_entries));
625 exfat_update_bh(sb, bh, sync);
627 num_entries -= copy_entries;
630 /* get next sector */
631 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
632 clu = exfat_sector_to_cluster(sbi, sec);
633 if (es->alloc_flag == ALLOC_NO_FAT_CHAIN)
635 else if (exfat_get_next_cluster(sb, &clu))
637 sec = exfat_cluster_to_sector(sbi, clu);
642 buf_off += EXFAT_DEN_TO_B(copy_entries);
651 static int exfat_walk_fat_chain(struct super_block *sb,
652 struct exfat_chain *p_dir, unsigned int byte_offset,
655 struct exfat_sb_info *sbi = EXFAT_SB(sb);
656 unsigned int clu_offset;
657 unsigned int cur_clu;
659 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
660 cur_clu = p_dir->dir;
662 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
663 cur_clu += clu_offset;
665 while (clu_offset > 0) {
666 if (exfat_get_next_cluster(sb, &cur_clu))
668 if (cur_clu == EXFAT_EOF_CLUSTER) {
670 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
672 EXFAT_B_TO_DEN(byte_offset));
683 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
684 int entry, sector_t *sector, int *offset)
687 unsigned int off, clu = 0;
688 struct exfat_sb_info *sbi = EXFAT_SB(sb);
690 off = EXFAT_DEN_TO_B(entry);
692 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
696 /* byte offset in cluster */
697 off = EXFAT_CLU_OFFSET(off, sbi);
699 /* byte offset in sector */
700 *offset = EXFAT_BLK_OFFSET(off, sb);
702 /* sector offset in cluster */
703 *sector = EXFAT_B_TO_BLK(off, sb);
704 *sector += exfat_cluster_to_sector(sbi, clu);
708 #define EXFAT_MAX_RA_SIZE (128*1024)
709 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
711 struct exfat_sb_info *sbi = EXFAT_SB(sb);
712 struct buffer_head *bh;
713 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
714 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
715 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
716 unsigned int ra_count = min(adj_ra_count, max_ra_count);
718 /* Read-ahead is not required */
719 if (sbi->sect_per_clus == 1)
722 if (sec < sbi->data_start_sector) {
723 exfat_msg(sb, KERN_ERR,
724 "requested sector is invalid(sect:%llu, root:%llu)",
725 (unsigned long long)sec, sbi->data_start_sector);
729 /* Not sector aligned with ra_count, resize ra_count to page size */
730 if ((sec - sbi->data_start_sector) & (ra_count - 1))
731 ra_count = page_ra_count;
733 bh = sb_find_get_block(sb, sec);
734 if (!bh || !buffer_uptodate(bh)) {
737 for (i = 0; i < ra_count; i++)
738 sb_breadahead(sb, (sector_t)(sec + i));
744 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
745 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
748 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
752 if (p_dir->dir == DIR_DELETED) {
753 exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry\n");
757 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
760 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
761 !(entry & (dentries_per_page - 1)))
762 exfat_dir_readahead(sb, sec);
764 *bh = sb_bread(sb, sec);
770 return (struct exfat_dentry *)((*bh)->b_data + off);
773 enum exfat_validate_dentry_mode {
775 ES_MODE_GET_FILE_ENTRY,
776 ES_MODE_GET_STRM_ENTRY,
777 ES_MODE_GET_NAME_ENTRY,
778 ES_MODE_GET_CRITICAL_SEC_ENTRY,
781 static bool exfat_validate_entry(unsigned int type,
782 enum exfat_validate_dentry_mode *mode)
784 if (type == TYPE_UNUSED || type == TYPE_DELETED)
788 case ES_MODE_STARTED:
789 if (type != TYPE_FILE && type != TYPE_DIR)
791 *mode = ES_MODE_GET_FILE_ENTRY;
793 case ES_MODE_GET_FILE_ENTRY:
794 if (type != TYPE_STREAM)
796 *mode = ES_MODE_GET_STRM_ENTRY;
798 case ES_MODE_GET_STRM_ENTRY:
799 if (type != TYPE_EXTEND)
801 *mode = ES_MODE_GET_NAME_ENTRY;
803 case ES_MODE_GET_NAME_ENTRY:
804 if (type == TYPE_STREAM)
806 if (type != TYPE_EXTEND) {
807 if (!(type & TYPE_CRITICAL_SEC))
809 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
812 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
813 if (type == TYPE_EXTEND || type == TYPE_STREAM)
815 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
825 * Returns a set of dentries for a file or dir.
827 * Note that this is a copy (dump) of dentries so that user should
828 * call write_entry_set() to apply changes made in this entry set
829 * to the real device.
832 * sb+p_dir+entry: indicates a file/dir
833 * type: specifies how many dentries should be included.
835 * file_ep: will point the first dentry(= file dentry) on success
837 * pointer of entry set on success,
840 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
841 struct exfat_chain *p_dir, int entry, unsigned int type,
842 struct exfat_dentry **file_ep)
845 unsigned int off, byte_offset, clu = 0;
846 unsigned int entry_type;
848 struct exfat_sb_info *sbi = EXFAT_SB(sb);
849 struct exfat_entry_set_cache *es;
850 struct exfat_dentry *ep, *pos;
851 unsigned char num_entries;
852 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
853 struct buffer_head *bh;
855 if (p_dir->dir == DIR_DELETED) {
856 exfat_msg(sb, KERN_ERR, "access to deleted dentry\n");
860 byte_offset = EXFAT_DEN_TO_B(entry);
861 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
865 /* byte offset in cluster */
866 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
868 /* byte offset in sector */
869 off = EXFAT_BLK_OFFSET(byte_offset, sb);
871 /* sector offset in cluster */
872 sec = EXFAT_B_TO_BLK(byte_offset, sb);
873 sec += exfat_cluster_to_sector(sbi, clu);
875 bh = sb_bread(sb, sec);
879 ep = (struct exfat_dentry *)(bh->b_data + off);
880 entry_type = exfat_get_entry_type(ep);
882 if (entry_type != TYPE_FILE && entry_type != TYPE_DIR)
885 num_entries = type == ES_ALL_ENTRIES ?
886 ep->dentry.file.num_ext + 1 : type;
887 es = kmalloc(struct_size(es, entries, num_entries), GFP_KERNEL);
891 es->num_entries = num_entries;
894 es->alloc_flag = p_dir->flags;
896 pos = &es->entries[0];
898 while (num_entries) {
899 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
903 memcpy(pos, ep, sizeof(struct exfat_dentry));
905 if (--num_entries == 0)
908 if (((off + DENTRY_SIZE) & (sb->s_blocksize - 1)) <
909 (off & (sb->s_blocksize - 1))) {
910 /* get the next sector */
911 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
912 if (es->alloc_flag == ALLOC_NO_FAT_CHAIN)
914 else if (exfat_get_next_cluster(sb, &clu))
916 sec = exfat_cluster_to_sector(sbi, clu);
922 bh = sb_bread(sb, sec);
926 ep = (struct exfat_dentry *)bh->b_data;
935 *file_ep = &es->entries[0];
955 * >= 0 : return dir entiry position with the name in dir
956 * -EEXIST : (root dir, ".") it is the root dir itself
957 * -ENOENT : entry with the name does not exist
960 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
961 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
962 int num_entries, unsigned int type)
964 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
965 int order, step, name_len = 0;
966 int dentries_per_clu, num_empty = 0;
967 unsigned int entry_type;
968 unsigned short *uniname = NULL;
969 struct exfat_chain clu;
970 struct exfat_hint *hint_stat = &ei->hint_stat;
971 struct exfat_hint_femp candi_empty;
972 struct exfat_sb_info *sbi = EXFAT_SB(sb);
974 dentries_per_clu = sbi->dentries_per_clu;
976 exfat_chain_dup(&clu, p_dir);
978 if (hint_stat->eidx) {
979 clu.dir = hint_stat->clu;
980 dentry = hint_stat->eidx;
984 candi_empty.eidx = EXFAT_HINT_NONE;
987 step = DIRENT_STEP_FILE;
988 while (clu.dir != EXFAT_EOF_CLUSTER) {
989 i = dentry & (dentries_per_clu - 1);
990 for (; i < dentries_per_clu; i++, dentry++) {
991 struct exfat_dentry *ep;
992 struct buffer_head *bh;
994 if (rewind && dentry == end_eidx)
997 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1001 entry_type = exfat_get_entry_type(ep);
1003 if (entry_type == TYPE_UNUSED ||
1004 entry_type == TYPE_DELETED) {
1005 step = DIRENT_STEP_FILE;
1008 if (candi_empty.eidx == EXFAT_HINT_NONE &&
1010 exfat_chain_set(&candi_empty.cur,
1011 clu.dir, clu.size, clu.flags);
1014 if (candi_empty.eidx == EXFAT_HINT_NONE &&
1015 num_empty >= num_entries) {
1017 dentry - (num_empty - 1);
1018 WARN_ON(candi_empty.eidx < 0);
1019 candi_empty.count = num_empty;
1021 if (ei->hint_femp.eidx ==
1024 ei->hint_femp.eidx) {
1025 memcpy(&ei->hint_femp,
1027 sizeof(candi_empty));
1032 if (entry_type == TYPE_UNUSED)
1038 candi_empty.eidx = EXFAT_HINT_NONE;
1040 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1041 step = DIRENT_STEP_FILE;
1042 if (type == TYPE_ALL || type == entry_type) {
1043 num_ext = ep->dentry.file.num_ext;
1044 step = DIRENT_STEP_STRM;
1050 if (entry_type == TYPE_STREAM) {
1051 unsigned short name_hash;
1053 if (step != DIRENT_STEP_STRM) {
1054 step = DIRENT_STEP_FILE;
1058 step = DIRENT_STEP_FILE;
1059 name_hash = le16_to_cpu(
1060 ep->dentry.stream.name_hash);
1061 if (p_uniname->name_hash == name_hash &&
1062 p_uniname->name_len ==
1063 ep->dentry.stream.name_len) {
1064 step = DIRENT_STEP_NAME;
1073 if (entry_type == TYPE_EXTEND) {
1074 unsigned short entry_uniname[16], unichar;
1076 if (step != DIRENT_STEP_NAME) {
1077 step = DIRENT_STEP_FILE;
1082 uniname = p_uniname->name;
1084 uniname += EXFAT_FILE_NAME_LEN;
1086 len = exfat_extract_uni_name(ep, entry_uniname);
1089 unichar = *(uniname+len);
1090 *(uniname+len) = 0x0;
1092 if (exfat_uniname_ncmp(sb, uniname,
1093 entry_uniname, len)) {
1094 step = DIRENT_STEP_FILE;
1095 } else if (p_uniname->name_len == name_len) {
1096 if (order == num_ext)
1098 step = DIRENT_STEP_SECD;
1101 *(uniname+len) = unichar;
1106 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1107 if (step == DIRENT_STEP_SECD) {
1108 if (++order == num_ext)
1113 step = DIRENT_STEP_FILE;
1116 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1120 clu.dir = EXFAT_EOF_CLUSTER;
1122 if (exfat_get_next_cluster(sb, &clu.dir))
1129 * We started at not 0 index,so we should try to find target
1130 * from 0 index to the index we started at.
1132 if (!rewind && end_eidx) {
1135 clu.dir = p_dir->dir;
1136 /* reset empty hint */
1138 candi_empty.eidx = EXFAT_HINT_NONE;
1142 /* initialized hint_stat */
1143 hint_stat->clu = p_dir->dir;
1144 hint_stat->eidx = 0;
1148 /* next dentry we'll find is out of this cluster */
1149 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1152 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1156 clu.dir = EXFAT_EOF_CLUSTER;
1158 ret = exfat_get_next_cluster(sb, &clu.dir);
1161 if (ret || clu.dir != EXFAT_EOF_CLUSTER) {
1162 /* just initialized hint_stat */
1163 hint_stat->clu = p_dir->dir;
1164 hint_stat->eidx = 0;
1165 return (dentry - num_ext);
1169 hint_stat->clu = clu.dir;
1170 hint_stat->eidx = dentry + 1;
1171 return dentry - num_ext;
1174 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1175 int entry, struct exfat_dentry *ep)
1179 struct exfat_dentry *ext_ep;
1180 struct buffer_head *bh;
1182 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1183 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1187 type = exfat_get_entry_type(ext_ep);
1189 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1197 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1200 int dentries_per_clu;
1201 unsigned int entry_type;
1202 struct exfat_chain clu;
1203 struct exfat_dentry *ep;
1204 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1205 struct buffer_head *bh;
1207 dentries_per_clu = sbi->dentries_per_clu;
1209 exfat_chain_dup(&clu, p_dir);
1211 while (clu.dir != EXFAT_EOF_CLUSTER) {
1212 for (i = 0; i < dentries_per_clu; i++) {
1213 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1216 entry_type = exfat_get_entry_type(ep);
1219 if (entry_type == TYPE_UNUSED)
1221 if (entry_type != TYPE_DIR)
1226 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1230 clu.dir = EXFAT_EOF_CLUSTER;
1232 if (exfat_get_next_cluster(sb, &(clu.dir)))