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 int 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 err = exfat_get_dentry_set(&es, 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 = ES_IDX_FIRST_FILENAME; 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 exfat_extract_uni_name(ep, uniname);
56 uniname += EXFAT_FILE_NAME_LEN;
59 exfat_put_dentry_set(&es, false);
63 /* read a directory entry from the opened directory */
64 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
66 int i, dentries_per_clu, num_ext, err;
67 unsigned int type, clu_offset, max_dentries;
68 struct exfat_chain dir, clu;
69 struct exfat_uni_name uni_name;
70 struct exfat_dentry *ep;
71 struct super_block *sb = inode->i_sb;
72 struct exfat_sb_info *sbi = EXFAT_SB(sb);
73 struct exfat_inode_info *ei = EXFAT_I(inode);
74 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
75 struct buffer_head *bh;
77 /* check if the given file ID is opened */
78 if (ei->type != TYPE_DIR)
82 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
84 exfat_chain_set(&dir, ei->start_clu,
85 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
87 dentries_per_clu = sbi->dentries_per_clu;
88 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
89 (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
91 clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
92 exfat_chain_dup(&clu, &dir);
94 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
95 clu.dir += clu_offset;
96 clu.size -= clu_offset;
98 /* hint_information */
99 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
100 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
101 clu_offset -= ei->hint_bmap.off;
102 clu.dir = ei->hint_bmap.clu;
105 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
106 if (exfat_get_next_cluster(sb, &(clu.dir)))
113 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
114 i = dentry & (dentries_per_clu - 1);
116 for ( ; i < dentries_per_clu; i++, dentry++) {
117 ep = exfat_get_dentry(sb, &clu, i, &bh);
121 type = exfat_get_entry_type(ep);
122 if (type == TYPE_UNUSED) {
127 if (type != TYPE_FILE && type != TYPE_DIR) {
132 num_ext = ep->dentry.file.num_ext;
133 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
134 exfat_get_entry_time(sbi, &dir_entry->crtime,
135 ep->dentry.file.create_tz,
136 ep->dentry.file.create_time,
137 ep->dentry.file.create_date,
138 ep->dentry.file.create_time_cs);
139 exfat_get_entry_time(sbi, &dir_entry->mtime,
140 ep->dentry.file.modify_tz,
141 ep->dentry.file.modify_time,
142 ep->dentry.file.modify_date,
143 ep->dentry.file.modify_time_cs);
144 exfat_get_entry_time(sbi, &dir_entry->atime,
145 ep->dentry.file.access_tz,
146 ep->dentry.file.access_time,
147 ep->dentry.file.access_date,
150 *uni_name.name = 0x0;
151 err = 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);
166 le64_to_cpu(ep->dentry.stream.valid_size);
167 dir_entry->entry = dentry;
170 ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
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);
217 /* skip iterating emit_dots when dir is empty */
218 #define ITER_POS_FILLED_DOTS (2)
219 static int exfat_iterate(struct file *file, struct dir_context *ctx)
221 struct inode *inode = file_inode(file);
222 struct super_block *sb = inode->i_sb;
224 struct exfat_dir_entry de;
225 struct exfat_dentry_namebuf *nb = &(de.namebuf);
226 struct exfat_inode_info *ei = EXFAT_I(inode);
229 int err = 0, fake_offset = 0;
231 exfat_init_namebuf(nb);
232 mutex_lock(&EXFAT_SB(sb)->s_lock);
235 if (!dir_emit_dots(file, ctx))
238 if (ctx->pos == ITER_POS_FILLED_DOTS) {
243 cpos = round_up(cpos, DENTRY_SIZE);
245 /* name buffer should be allocated before use */
246 err = exfat_alloc_namebuf(nb);
250 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
253 err = exfat_readdir(inode, &cpos, &de);
256 * At least we tried to read a sector. Move cpos to next sector
257 * position (should be aligned).
260 cpos += 1 << (sb->s_blocksize_bits);
261 cpos &= ~(sb->s_blocksize - 1);
271 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
272 tmp = exfat_iget(sb, i_pos);
277 inum = iunique(sb, EXFAT_ROOT_INO);
281 * Before calling dir_emit(), sb_lock should be released.
282 * Because page fault can occur in dir_emit() when the size
283 * of buffer given from user is larger than one page size.
285 mutex_unlock(&EXFAT_SB(sb)->s_lock);
286 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
287 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
289 mutex_lock(&EXFAT_SB(sb)->s_lock);
294 if (!cpos && fake_offset)
295 cpos = ITER_POS_FILLED_DOTS;
298 mutex_unlock(&EXFAT_SB(sb)->s_lock);
301 * To improve performance, free namebuf after unlock sb_lock.
302 * If namebuf is not allocated, this function do nothing
304 exfat_free_namebuf(nb);
308 const struct file_operations exfat_dir_operations = {
309 .llseek = generic_file_llseek,
310 .read = generic_read_dir,
311 .iterate = exfat_iterate,
312 .unlocked_ioctl = exfat_ioctl,
314 .compat_ioctl = exfat_compat_ioctl,
316 .fsync = exfat_file_fsync,
319 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
323 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
325 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
329 return exfat_zeroed_cluster(inode, clu->dir);
332 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
336 len = p_uniname->name_len;
340 /* 1 file entry + 1 stream entry + name entries */
341 return ES_ENTRY_NUM(len);
344 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
346 if (ep->type == EXFAT_UNUSED)
348 if (IS_EXFAT_DELETED(ep->type))
350 if (ep->type == EXFAT_INVAL)
352 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
353 if (ep->type == EXFAT_BITMAP)
355 if (ep->type == EXFAT_UPCASE)
357 if (ep->type == EXFAT_VOLUME)
359 if (ep->type == EXFAT_FILE) {
360 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
364 return TYPE_CRITICAL_PRI;
366 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
367 if (ep->type == EXFAT_GUID)
369 if (ep->type == EXFAT_PADDING)
371 if (ep->type == EXFAT_ACLTAB)
373 return TYPE_BENIGN_PRI;
375 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
376 if (ep->type == EXFAT_STREAM)
378 if (ep->type == EXFAT_NAME)
380 if (ep->type == EXFAT_ACL)
382 return TYPE_CRITICAL_SEC;
385 if (ep->type == EXFAT_VENDOR_EXT)
386 return TYPE_VENDOR_EXT;
387 if (ep->type == EXFAT_VENDOR_ALLOC)
388 return TYPE_VENDOR_ALLOC;
390 return TYPE_BENIGN_SEC;
393 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
395 if (type == TYPE_UNUSED) {
396 ep->type = EXFAT_UNUSED;
397 } else if (type == TYPE_DELETED) {
398 ep->type &= EXFAT_DELETE;
399 } else if (type == TYPE_STREAM) {
400 ep->type = EXFAT_STREAM;
401 } else if (type == TYPE_EXTEND) {
402 ep->type = EXFAT_NAME;
403 } else if (type == TYPE_BITMAP) {
404 ep->type = EXFAT_BITMAP;
405 } else if (type == TYPE_UPCASE) {
406 ep->type = EXFAT_UPCASE;
407 } else if (type == TYPE_VOLUME) {
408 ep->type = EXFAT_VOLUME;
409 } else if (type == TYPE_DIR) {
410 ep->type = EXFAT_FILE;
411 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
412 } else if (type == TYPE_FILE) {
413 ep->type = EXFAT_FILE;
414 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
418 static void exfat_init_stream_entry(struct exfat_dentry *ep,
419 unsigned char flags, unsigned int start_clu,
420 unsigned long long size)
422 exfat_set_entry_type(ep, TYPE_STREAM);
423 ep->dentry.stream.flags = flags;
424 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
425 ep->dentry.stream.valid_size = cpu_to_le64(size);
426 ep->dentry.stream.size = cpu_to_le64(size);
429 static void exfat_init_name_entry(struct exfat_dentry *ep,
430 unsigned short *uniname)
434 exfat_set_entry_type(ep, TYPE_EXTEND);
435 ep->dentry.name.flags = 0x0;
437 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
438 if (*uniname != 0x0) {
439 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
442 ep->dentry.name.unicode_0_14[i] = 0x0;
447 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
448 int entry, unsigned int type, unsigned int start_clu,
449 unsigned long long size)
451 struct super_block *sb = inode->i_sb;
452 struct exfat_sb_info *sbi = EXFAT_SB(sb);
453 struct timespec64 ts = current_time(inode);
454 struct exfat_dentry *ep;
455 struct buffer_head *bh;
458 * We cannot use exfat_get_dentry_set here because file ep is not
461 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
465 exfat_set_entry_type(ep, type);
466 exfat_set_entry_time(sbi, &ts,
467 &ep->dentry.file.create_tz,
468 &ep->dentry.file.create_time,
469 &ep->dentry.file.create_date,
470 &ep->dentry.file.create_time_cs);
471 exfat_set_entry_time(sbi, &ts,
472 &ep->dentry.file.modify_tz,
473 &ep->dentry.file.modify_time,
474 &ep->dentry.file.modify_date,
475 &ep->dentry.file.modify_time_cs);
476 exfat_set_entry_time(sbi, &ts,
477 &ep->dentry.file.access_tz,
478 &ep->dentry.file.access_time,
479 &ep->dentry.file.access_date,
482 exfat_update_bh(bh, IS_DIRSYNC(inode));
485 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
489 exfat_init_stream_entry(ep,
490 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
492 exfat_update_bh(bh, IS_DIRSYNC(inode));
498 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
501 struct super_block *sb = inode->i_sb;
505 struct exfat_dentry *ep, *fep;
506 struct buffer_head *fbh, *bh;
508 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
512 num_entries = fep->dentry.file.num_ext + 1;
513 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
515 for (i = 1; i < num_entries; i++) {
516 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
521 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
526 fep->dentry.file.checksum = cpu_to_le16(chksum);
527 exfat_update_bh(fbh, IS_DIRSYNC(inode));
533 static void exfat_free_benign_secondary_clusters(struct inode *inode,
534 struct exfat_dentry *ep)
536 struct super_block *sb = inode->i_sb;
537 struct exfat_chain dir;
538 unsigned int start_clu =
539 le32_to_cpu(ep->dentry.generic_secondary.start_clu);
540 u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
541 unsigned char flags = ep->dentry.generic_secondary.flags;
543 if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
546 exfat_chain_set(&dir, start_clu,
547 EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
549 exfat_free_cluster(inode, &dir);
552 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
553 int entry, int num_entries, struct exfat_uni_name *p_uniname)
555 struct super_block *sb = inode->i_sb;
557 unsigned short *uniname = p_uniname->name;
558 struct exfat_dentry *ep;
559 struct buffer_head *bh;
560 int sync = IS_DIRSYNC(inode);
562 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
566 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
567 exfat_update_bh(bh, sync);
570 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
574 ep->dentry.stream.name_len = p_uniname->name_len;
575 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
576 exfat_update_bh(bh, sync);
579 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
580 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
584 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
585 exfat_free_benign_secondary_clusters(inode, ep);
587 exfat_init_name_entry(ep, uniname);
588 exfat_update_bh(bh, sync);
590 uniname += EXFAT_FILE_NAME_LEN;
593 exfat_update_dir_chksum(inode, p_dir, entry);
597 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
598 int entry, int order, int num_entries)
600 struct super_block *sb = inode->i_sb;
602 struct exfat_dentry *ep;
603 struct buffer_head *bh;
605 for (i = order; i < num_entries; i++) {
606 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
610 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
611 exfat_free_benign_secondary_clusters(inode, ep);
613 exfat_set_entry_type(ep, TYPE_DELETED);
614 exfat_update_bh(bh, IS_DIRSYNC(inode));
621 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
623 int chksum_type = CS_DIR_ENTRY, i;
624 unsigned short chksum = 0;
625 struct exfat_dentry *ep;
627 for (i = ES_IDX_FILE; i < es->num_entries; i++) {
628 ep = exfat_get_dentry_cached(es, i);
629 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
631 chksum_type = CS_DEFAULT;
633 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
634 ep->dentry.file.checksum = cpu_to_le16(chksum);
638 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
643 err = exfat_update_bhs(es->bh, es->num_bh, sync);
645 for (i = 0; i < es->num_bh; i++)
651 if (IS_DYNAMIC_ES(es))
657 static int exfat_walk_fat_chain(struct super_block *sb,
658 struct exfat_chain *p_dir, unsigned int byte_offset,
661 struct exfat_sb_info *sbi = EXFAT_SB(sb);
662 unsigned int clu_offset;
663 unsigned int cur_clu;
665 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
666 cur_clu = p_dir->dir;
668 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
669 cur_clu += clu_offset;
671 while (clu_offset > 0) {
672 if (exfat_get_next_cluster(sb, &cur_clu))
674 if (cur_clu == EXFAT_EOF_CLUSTER) {
676 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
678 EXFAT_B_TO_DEN(byte_offset));
689 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
690 int entry, sector_t *sector, int *offset)
693 unsigned int off, clu = 0;
694 struct exfat_sb_info *sbi = EXFAT_SB(sb);
696 off = EXFAT_DEN_TO_B(entry);
698 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
702 /* byte offset in cluster */
703 off = EXFAT_CLU_OFFSET(off, sbi);
705 /* byte offset in sector */
706 *offset = EXFAT_BLK_OFFSET(off, sb);
708 /* sector offset in cluster */
709 *sector = EXFAT_B_TO_BLK(off, sb);
710 *sector += exfat_cluster_to_sector(sbi, clu);
714 #define EXFAT_MAX_RA_SIZE (128*1024)
715 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
717 struct exfat_sb_info *sbi = EXFAT_SB(sb);
718 struct buffer_head *bh;
719 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
720 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
721 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
722 unsigned int ra_count = min(adj_ra_count, max_ra_count);
724 /* Read-ahead is not required */
725 if (sbi->sect_per_clus == 1)
728 if (sec < sbi->data_start_sector) {
729 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
730 (unsigned long long)sec, sbi->data_start_sector);
734 /* Not sector aligned with ra_count, resize ra_count to page size */
735 if ((sec - sbi->data_start_sector) & (ra_count - 1))
736 ra_count = page_ra_count;
738 bh = sb_find_get_block(sb, sec);
739 if (!bh || !buffer_uptodate(bh)) {
742 for (i = 0; i < ra_count; i++)
743 sb_breadahead(sb, (sector_t)(sec + i));
749 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
750 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
752 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
756 if (p_dir->dir == DIR_DELETED) {
757 exfat_err(sb, "abnormal access to deleted dentry");
761 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
764 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
765 !(entry & (dentries_per_page - 1)))
766 exfat_dir_readahead(sb, sec);
768 *bh = sb_bread(sb, sec);
772 return (struct exfat_dentry *)((*bh)->b_data + off);
775 enum exfat_validate_dentry_mode {
777 ES_MODE_GET_FILE_ENTRY,
778 ES_MODE_GET_STRM_ENTRY,
779 ES_MODE_GET_NAME_ENTRY,
780 ES_MODE_GET_CRITICAL_SEC_ENTRY,
781 ES_MODE_GET_BENIGN_SEC_ENTRY,
784 static bool exfat_validate_entry(unsigned int type,
785 enum exfat_validate_dentry_mode *mode)
787 if (type == TYPE_UNUSED || type == TYPE_DELETED)
791 case ES_MODE_STARTED:
792 if (type != TYPE_FILE && type != TYPE_DIR)
794 *mode = ES_MODE_GET_FILE_ENTRY;
796 case ES_MODE_GET_FILE_ENTRY:
797 if (type != TYPE_STREAM)
799 *mode = ES_MODE_GET_STRM_ENTRY;
801 case ES_MODE_GET_STRM_ENTRY:
802 if (type != TYPE_EXTEND)
804 *mode = ES_MODE_GET_NAME_ENTRY;
806 case ES_MODE_GET_NAME_ENTRY:
807 if (type & TYPE_BENIGN_SEC)
808 *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
809 else if (type != TYPE_EXTEND)
812 case ES_MODE_GET_BENIGN_SEC_ENTRY:
813 /* Assume unreconized benign secondary entry */
814 if (!(type & TYPE_BENIGN_SEC))
824 struct exfat_dentry *exfat_get_dentry_cached(
825 struct exfat_entry_set_cache *es, int num)
827 int off = es->start_off + num * DENTRY_SIZE;
828 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
829 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
831 return (struct exfat_dentry *)p;
835 * Returns a set of dentries for a file or dir.
837 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
838 * User should call exfat_get_dentry_set() after setting 'modified' to apply
839 * changes made in this entry set to the real device.
842 * sb+p_dir+entry: indicates a file/dir
843 * type: specifies how many dentries should be included.
845 * pointer of entry set on success,
848 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
849 struct super_block *sb, struct exfat_chain *p_dir, int entry,
855 struct exfat_sb_info *sbi = EXFAT_SB(sb);
856 struct exfat_dentry *ep;
858 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
859 struct buffer_head *bh;
861 if (p_dir->dir == DIR_DELETED) {
862 exfat_err(sb, "access to deleted dentry");
866 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
870 memset(es, 0, sizeof(*es));
872 es->modified = false;
876 bh = sb_bread(sb, sec);
879 es->bh[es->num_bh++] = bh;
881 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
882 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
885 num_entries = type == ES_ALL_ENTRIES ?
886 ep->dentry.file.num_ext + 1 : type;
887 es->num_entries = num_entries;
889 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
890 if (num_bh > ARRAY_SIZE(es->__bh)) {
891 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
899 for (i = 1; i < num_bh; i++) {
900 /* get the next sector */
901 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
902 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
904 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
906 else if (exfat_get_next_cluster(sb, &clu))
908 sec = exfat_cluster_to_sector(sbi, clu);
913 bh = sb_bread(sb, sec);
916 es->bh[es->num_bh++] = bh;
919 /* validate cached dentries */
920 for (i = ES_IDX_STREAM; i < num_entries; i++) {
921 ep = exfat_get_dentry_cached(es, i);
922 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
928 exfat_put_dentry_set(es, false);
932 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
934 hint_femp->eidx = EXFAT_HINT_NONE;
935 hint_femp->count = 0;
938 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
939 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
940 int dentry, int num_entries, int entry_type)
942 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
943 ei->hint_femp.eidx > dentry) {
944 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
946 if (candi_empty->count == 0) {
947 candi_empty->cur = *clu;
948 candi_empty->eidx = dentry;
951 if (entry_type == TYPE_UNUSED)
952 candi_empty->count += total_entries - dentry;
954 candi_empty->count++;
956 if (candi_empty->count == num_entries ||
957 candi_empty->count + candi_empty->eidx == total_entries)
958 ei->hint_femp = *candi_empty;
970 * @ei: inode info of parent directory
971 * @p_dir: directory structure of parent directory
972 * @num_entries:entry size of p_uniname
973 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
974 * for traversing cluster chain.
976 * >= 0: file directory entry position where the name exists
977 * -ENOENT: entry with the name does not exist
980 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
981 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
982 struct exfat_hint *hint_opt)
984 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
985 int order, step, name_len = 0;
986 int dentries_per_clu;
987 unsigned int entry_type;
988 unsigned short *uniname = NULL;
989 struct exfat_chain clu;
990 struct exfat_hint *hint_stat = &ei->hint_stat;
991 struct exfat_hint_femp candi_empty;
992 struct exfat_sb_info *sbi = EXFAT_SB(sb);
993 int num_entries = exfat_calc_num_entries(p_uniname);
998 dentries_per_clu = sbi->dentries_per_clu;
1000 exfat_chain_dup(&clu, p_dir);
1002 if (hint_stat->eidx) {
1003 clu.dir = hint_stat->clu;
1004 dentry = hint_stat->eidx;
1008 exfat_reset_empty_hint(&ei->hint_femp);
1012 step = DIRENT_STEP_FILE;
1013 exfat_reset_empty_hint(&candi_empty);
1015 while (clu.dir != EXFAT_EOF_CLUSTER) {
1016 i = dentry & (dentries_per_clu - 1);
1017 for (; i < dentries_per_clu; i++, dentry++) {
1018 struct exfat_dentry *ep;
1019 struct buffer_head *bh;
1021 if (rewind && dentry == end_eidx)
1024 ep = exfat_get_dentry(sb, &clu, i, &bh);
1028 entry_type = exfat_get_entry_type(ep);
1030 if (entry_type == TYPE_UNUSED ||
1031 entry_type == TYPE_DELETED) {
1032 step = DIRENT_STEP_FILE;
1034 exfat_set_empty_hint(ei, &candi_empty, &clu,
1035 dentry, num_entries,
1039 if (entry_type == TYPE_UNUSED)
1044 exfat_reset_empty_hint(&candi_empty);
1046 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1047 step = DIRENT_STEP_FILE;
1048 hint_opt->clu = clu.dir;
1050 num_ext = ep->dentry.file.num_ext;
1051 step = DIRENT_STEP_STRM;
1056 if (entry_type == TYPE_STREAM) {
1059 if (step != DIRENT_STEP_STRM) {
1060 step = DIRENT_STEP_FILE;
1064 step = DIRENT_STEP_FILE;
1065 name_hash = le16_to_cpu(
1066 ep->dentry.stream.name_hash);
1067 if (p_uniname->name_hash == name_hash &&
1068 p_uniname->name_len ==
1069 ep->dentry.stream.name_len) {
1070 step = DIRENT_STEP_NAME;
1079 if (entry_type == TYPE_EXTEND) {
1080 unsigned short entry_uniname[16], unichar;
1082 if (step != DIRENT_STEP_NAME) {
1083 step = DIRENT_STEP_FILE;
1088 uniname = p_uniname->name;
1090 uniname += EXFAT_FILE_NAME_LEN;
1092 len = exfat_extract_uni_name(ep, entry_uniname);
1095 unichar = *(uniname+len);
1096 *(uniname+len) = 0x0;
1098 if (exfat_uniname_ncmp(sb, uniname,
1099 entry_uniname, len)) {
1100 step = DIRENT_STEP_FILE;
1101 } else if (p_uniname->name_len == name_len) {
1102 if (order == num_ext)
1104 step = DIRENT_STEP_SECD;
1107 *(uniname+len) = unichar;
1112 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1113 if (step == DIRENT_STEP_SECD) {
1114 if (++order == num_ext)
1119 step = DIRENT_STEP_FILE;
1122 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1126 clu.dir = EXFAT_EOF_CLUSTER;
1128 if (exfat_get_next_cluster(sb, &clu.dir))
1135 * We started at not 0 index,so we should try to find target
1136 * from 0 index to the index we started at.
1138 if (!rewind && end_eidx) {
1141 clu.dir = p_dir->dir;
1146 * set the EXFAT_EOF_CLUSTER flag to avoid search
1147 * from the beginning again when allocated a new cluster
1149 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1150 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1151 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1152 ei->hint_femp.count = 0;
1155 /* initialized hint_stat */
1156 hint_stat->clu = p_dir->dir;
1157 hint_stat->eidx = 0;
1161 /* next dentry we'll find is out of this cluster */
1162 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1165 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1169 clu.dir = EXFAT_EOF_CLUSTER;
1171 ret = exfat_get_next_cluster(sb, &clu.dir);
1174 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1175 /* just initialized hint_stat */
1176 hint_stat->clu = p_dir->dir;
1177 hint_stat->eidx = 0;
1178 return (dentry - num_ext);
1182 hint_stat->clu = clu.dir;
1183 hint_stat->eidx = dentry + 1;
1184 return dentry - num_ext;
1187 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1188 int entry, struct exfat_dentry *ep)
1192 struct exfat_dentry *ext_ep;
1193 struct buffer_head *bh;
1195 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1196 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1200 type = exfat_get_entry_type(ext_ep);
1202 if (type & TYPE_CRITICAL_SEC || type & TYPE_BENIGN_SEC)
1208 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1211 int dentries_per_clu;
1212 unsigned int entry_type;
1213 struct exfat_chain clu;
1214 struct exfat_dentry *ep;
1215 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1216 struct buffer_head *bh;
1218 dentries_per_clu = sbi->dentries_per_clu;
1220 exfat_chain_dup(&clu, p_dir);
1222 while (clu.dir != EXFAT_EOF_CLUSTER) {
1223 for (i = 0; i < dentries_per_clu; i++) {
1224 ep = exfat_get_dentry(sb, &clu, i, &bh);
1227 entry_type = exfat_get_entry_type(ep);
1230 if (entry_type == TYPE_UNUSED)
1232 if (entry_type != TYPE_DIR)
1237 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1241 clu.dir = EXFAT_EOF_CLUSTER;
1243 if (exfat_get_next_cluster(sb, &(clu.dir)))