1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
7 #include <linux/compat.h>
9 #include <linux/buffer_head.h>
11 #include "exfat_raw.h"
14 static int exfat_extract_uni_name(struct exfat_dentry *ep,
15 unsigned short *uniname)
19 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
20 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
32 static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
33 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
36 struct exfat_entry_set_cache es;
38 if (exfat_get_dentry_set(&es, 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 = ES_IDX_FIRST_FILENAME; 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_put_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, num_ext;
65 unsigned int type, clu_offset, max_dentries;
66 struct exfat_chain dir, clu;
67 struct exfat_uni_name uni_name;
68 struct exfat_dentry *ep;
69 struct super_block *sb = inode->i_sb;
70 struct exfat_sb_info *sbi = EXFAT_SB(sb);
71 struct exfat_inode_info *ei = EXFAT_I(inode);
72 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
73 struct buffer_head *bh;
75 /* check if the given file ID is opened */
76 if (ei->type != TYPE_DIR)
80 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
82 exfat_chain_set(&dir, ei->start_clu,
83 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
85 dentries_per_clu = sbi->dentries_per_clu;
86 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
87 (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
89 clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
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 && dentry < max_dentries) {
112 i = dentry & (dentries_per_clu - 1);
114 for ( ; i < dentries_per_clu; i++, dentry++) {
115 ep = exfat_get_dentry(sb, &clu, i, &bh);
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, &clu, i,
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);
160 le64_to_cpu(ep->dentry.stream.valid_size);
161 dir_entry->entry = dentry;
164 ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
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 *file, struct dir_context *ctx)
215 struct inode *inode = file_inode(file);
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(file, 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 (ei->flags == ALLOC_NO_FAT_CHAIN && 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 .unlocked_ioctl = exfat_ioctl,
311 .compat_ioctl = exfat_compat_ioctl,
313 .fsync = exfat_file_fsync,
316 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
320 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
322 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
326 return exfat_zeroed_cluster(inode, clu->dir);
329 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
333 len = p_uniname->name_len;
337 /* 1 file entry + 1 stream entry + name entries */
338 return ES_ENTRY_NUM(len);
341 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
343 if (ep->type == EXFAT_UNUSED)
345 if (IS_EXFAT_DELETED(ep->type))
347 if (ep->type == EXFAT_INVAL)
349 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
350 if (ep->type == EXFAT_BITMAP)
352 if (ep->type == EXFAT_UPCASE)
354 if (ep->type == EXFAT_VOLUME)
356 if (ep->type == EXFAT_FILE) {
357 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
361 return TYPE_CRITICAL_PRI;
363 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
364 if (ep->type == EXFAT_GUID)
366 if (ep->type == EXFAT_PADDING)
368 if (ep->type == EXFAT_ACLTAB)
370 return TYPE_BENIGN_PRI;
372 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
373 if (ep->type == EXFAT_STREAM)
375 if (ep->type == EXFAT_NAME)
377 if (ep->type == EXFAT_ACL)
379 return TYPE_CRITICAL_SEC;
381 return TYPE_BENIGN_SEC;
384 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
386 if (type == TYPE_UNUSED) {
387 ep->type = EXFAT_UNUSED;
388 } else if (type == TYPE_DELETED) {
389 ep->type &= EXFAT_DELETE;
390 } else if (type == TYPE_STREAM) {
391 ep->type = EXFAT_STREAM;
392 } else if (type == TYPE_EXTEND) {
393 ep->type = EXFAT_NAME;
394 } else if (type == TYPE_BITMAP) {
395 ep->type = EXFAT_BITMAP;
396 } else if (type == TYPE_UPCASE) {
397 ep->type = EXFAT_UPCASE;
398 } else if (type == TYPE_VOLUME) {
399 ep->type = EXFAT_VOLUME;
400 } else if (type == TYPE_DIR) {
401 ep->type = EXFAT_FILE;
402 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
403 } else if (type == TYPE_FILE) {
404 ep->type = EXFAT_FILE;
405 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
409 static void exfat_init_stream_entry(struct exfat_dentry *ep,
410 unsigned char flags, unsigned int start_clu,
411 unsigned long long size)
413 exfat_set_entry_type(ep, TYPE_STREAM);
414 ep->dentry.stream.flags = flags;
415 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
416 ep->dentry.stream.valid_size = cpu_to_le64(size);
417 ep->dentry.stream.size = cpu_to_le64(size);
420 static void exfat_init_name_entry(struct exfat_dentry *ep,
421 unsigned short *uniname)
425 exfat_set_entry_type(ep, TYPE_EXTEND);
426 ep->dentry.name.flags = 0x0;
428 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
429 if (*uniname != 0x0) {
430 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
433 ep->dentry.name.unicode_0_14[i] = 0x0;
438 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
439 int entry, unsigned int type, unsigned int start_clu,
440 unsigned long long size)
442 struct super_block *sb = inode->i_sb;
443 struct exfat_sb_info *sbi = EXFAT_SB(sb);
444 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);
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);
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;
496 struct exfat_dentry *ep, *fep;
497 struct buffer_head *fbh, *bh;
499 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
503 num_entries = fep->dentry.file.num_ext + 1;
504 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
506 for (i = 1; i < num_entries; i++) {
507 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
512 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
517 fep->dentry.file.checksum = cpu_to_le16(chksum);
518 exfat_update_bh(fbh, IS_DIRSYNC(inode));
524 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
525 int entry, int num_entries, struct exfat_uni_name *p_uniname)
527 struct super_block *sb = inode->i_sb;
529 unsigned short *uniname = p_uniname->name;
530 struct exfat_dentry *ep;
531 struct buffer_head *bh;
532 int sync = IS_DIRSYNC(inode);
534 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
538 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
539 exfat_update_bh(bh, sync);
542 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
546 ep->dentry.stream.name_len = p_uniname->name_len;
547 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
548 exfat_update_bh(bh, sync);
551 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
552 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
556 exfat_init_name_entry(ep, uniname);
557 exfat_update_bh(bh, sync);
559 uniname += EXFAT_FILE_NAME_LEN;
562 exfat_update_dir_chksum(inode, p_dir, entry);
566 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
567 int entry, int order, int num_entries)
569 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);
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 = ES_IDX_FILE; 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, ES_IDX_FILE);
600 ep->dentry.file.checksum = cpu_to_le16(chksum);
604 int exfat_put_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))
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 static 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)
718 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
722 if (p_dir->dir == DIR_DELETED) {
723 exfat_err(sb, "abnormal access to deleted dentry");
727 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
730 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
731 !(entry & (dentries_per_page - 1)))
732 exfat_dir_readahead(sb, sec);
734 *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 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
817 struct super_block *sb, struct exfat_chain *p_dir, int entry,
823 struct exfat_sb_info *sbi = EXFAT_SB(sb);
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 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
838 memset(es, 0, sizeof(*es));
840 es->modified = false;
844 bh = sb_bread(sb, sec);
847 es->bh[es->num_bh++] = bh;
849 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
850 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
853 num_entries = type == ES_ALL_ENTRIES ?
854 ep->dentry.file.num_ext + 1 : type;
855 es->num_entries = num_entries;
857 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
858 if (num_bh > ARRAY_SIZE(es->__bh)) {
859 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
867 for (i = 1; i < num_bh; i++) {
868 /* get the next sector */
869 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
870 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
872 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
874 else if (exfat_get_next_cluster(sb, &clu))
876 sec = exfat_cluster_to_sector(sbi, clu);
881 bh = sb_bread(sb, sec);
884 es->bh[es->num_bh++] = bh;
887 /* validate cached dentries */
888 for (i = ES_IDX_STREAM; i < num_entries; i++) {
889 ep = exfat_get_dentry_cached(es, i);
890 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
896 exfat_put_dentry_set(es, false);
900 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
902 hint_femp->eidx = EXFAT_HINT_NONE;
903 hint_femp->count = 0;
906 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
907 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
908 int dentry, int num_entries, int entry_type)
910 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
911 ei->hint_femp.eidx > dentry) {
912 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
914 if (candi_empty->count == 0) {
915 candi_empty->cur = *clu;
916 candi_empty->eidx = dentry;
919 if (entry_type == TYPE_UNUSED)
920 candi_empty->count += total_entries - dentry;
922 candi_empty->count++;
924 if (candi_empty->count == num_entries ||
925 candi_empty->count + candi_empty->eidx == total_entries)
926 ei->hint_femp = *candi_empty;
938 * @ei: inode info of parent directory
939 * @p_dir: directory structure of parent directory
940 * @num_entries:entry size of p_uniname
941 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
942 * for traversing cluster chain.
944 * >= 0: file directory entry position where the name exists
945 * -ENOENT: entry with the name does not exist
948 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
949 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
950 struct exfat_hint *hint_opt)
952 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
953 int order, step, name_len = 0;
954 int dentries_per_clu;
955 unsigned int entry_type;
956 unsigned short *uniname = NULL;
957 struct exfat_chain clu;
958 struct exfat_hint *hint_stat = &ei->hint_stat;
959 struct exfat_hint_femp candi_empty;
960 struct exfat_sb_info *sbi = EXFAT_SB(sb);
961 int num_entries = exfat_calc_num_entries(p_uniname);
966 dentries_per_clu = sbi->dentries_per_clu;
968 exfat_chain_dup(&clu, p_dir);
970 if (hint_stat->eidx) {
971 clu.dir = hint_stat->clu;
972 dentry = hint_stat->eidx;
976 exfat_reset_empty_hint(&ei->hint_femp);
980 step = DIRENT_STEP_FILE;
981 exfat_reset_empty_hint(&candi_empty);
983 while (clu.dir != EXFAT_EOF_CLUSTER) {
984 i = dentry & (dentries_per_clu - 1);
985 for (; i < dentries_per_clu; i++, dentry++) {
986 struct exfat_dentry *ep;
987 struct buffer_head *bh;
989 if (rewind && dentry == end_eidx)
992 ep = exfat_get_dentry(sb, &clu, i, &bh);
996 entry_type = exfat_get_entry_type(ep);
998 if (entry_type == TYPE_UNUSED ||
999 entry_type == TYPE_DELETED) {
1000 step = DIRENT_STEP_FILE;
1002 exfat_set_empty_hint(ei, &candi_empty, &clu,
1003 dentry, num_entries,
1007 if (entry_type == TYPE_UNUSED)
1012 exfat_reset_empty_hint(&candi_empty);
1014 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1015 step = DIRENT_STEP_FILE;
1016 hint_opt->clu = clu.dir;
1018 num_ext = ep->dentry.file.num_ext;
1019 step = DIRENT_STEP_STRM;
1024 if (entry_type == TYPE_STREAM) {
1027 if (step != DIRENT_STEP_STRM) {
1028 step = DIRENT_STEP_FILE;
1032 step = DIRENT_STEP_FILE;
1033 name_hash = le16_to_cpu(
1034 ep->dentry.stream.name_hash);
1035 if (p_uniname->name_hash == name_hash &&
1036 p_uniname->name_len ==
1037 ep->dentry.stream.name_len) {
1038 step = DIRENT_STEP_NAME;
1047 if (entry_type == TYPE_EXTEND) {
1048 unsigned short entry_uniname[16], unichar;
1050 if (step != DIRENT_STEP_NAME) {
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;
1114 * set the EXFAT_EOF_CLUSTER flag to avoid search
1115 * from the beginning again when allocated a new cluster
1117 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1118 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1119 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1120 ei->hint_femp.count = 0;
1123 /* initialized hint_stat */
1124 hint_stat->clu = p_dir->dir;
1125 hint_stat->eidx = 0;
1129 /* next dentry we'll find is out of this cluster */
1130 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1133 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1137 clu.dir = EXFAT_EOF_CLUSTER;
1139 ret = exfat_get_next_cluster(sb, &clu.dir);
1142 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1143 /* just initialized hint_stat */
1144 hint_stat->clu = p_dir->dir;
1145 hint_stat->eidx = 0;
1146 return (dentry - num_ext);
1150 hint_stat->clu = clu.dir;
1151 hint_stat->eidx = dentry + 1;
1152 return dentry - num_ext;
1155 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1156 int entry, struct exfat_dentry *ep)
1160 struct exfat_dentry *ext_ep;
1161 struct buffer_head *bh;
1163 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1164 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1168 type = exfat_get_entry_type(ext_ep);
1170 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1178 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1181 int dentries_per_clu;
1182 unsigned int entry_type;
1183 struct exfat_chain clu;
1184 struct exfat_dentry *ep;
1185 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1186 struct buffer_head *bh;
1188 dentries_per_clu = sbi->dentries_per_clu;
1190 exfat_chain_dup(&clu, p_dir);
1192 while (clu.dir != EXFAT_EOF_CLUSTER) {
1193 for (i = 0; i < dentries_per_clu; i++) {
1194 ep = exfat_get_dentry(sb, &clu, i, &bh);
1197 entry_type = exfat_get_entry_type(ep);
1200 if (entry_type == TYPE_UNUSED)
1202 if (entry_type != TYPE_DIR)
1207 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1211 clu.dir = EXFAT_EOF_CLUSTER;
1213 if (exfat_get_next_cluster(sb, &(clu.dir)))