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;
37 unsigned int uni_len = 0, len;
39 err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
44 * First entry : file entry
45 * Second entry : stream-extension entry
46 * Third entry : first file-name entry
47 * So, the index of first file-name dentry should start from 2.
49 for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
50 struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
52 /* end of name entry */
53 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
56 len = exfat_extract_uni_name(ep, uniname);
58 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
60 uniname += EXFAT_FILE_NAME_LEN;
63 exfat_put_dentry_set(&es, false);
67 /* read a directory entry from the opened directory */
68 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
70 int i, dentries_per_clu, num_ext, err;
71 unsigned int type, clu_offset, max_dentries;
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 = EXFAT_B_TO_DEN(*cpos) & 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 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
93 (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
95 clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
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);
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 err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
161 exfat_utf16_to_nls(sb, &uni_name,
162 dir_entry->namebuf.lfn,
163 dir_entry->namebuf.lfnbuf_len);
166 ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
170 le64_to_cpu(ep->dentry.stream.valid_size);
171 dir_entry->entry = dentry;
174 ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
175 ei->hint_bmap.clu = clu.dir;
177 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
181 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
185 clu.dir = EXFAT_EOF_CLUSTER;
187 if (exfat_get_next_cluster(sb, &(clu.dir)))
192 dir_entry->namebuf.lfn[0] = '\0';
193 *cpos = EXFAT_DEN_TO_B(dentry);
197 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
203 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
205 nb->lfn = __getname();
208 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
212 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
218 exfat_init_namebuf(nb);
222 * Before calling dir_emit*(), sbi->s_lock should be released
223 * because page fault can occur in dir_emit*().
225 #define ITER_POS_FILLED_DOTS (2)
226 static int exfat_iterate(struct file *file, struct dir_context *ctx)
228 struct inode *inode = file_inode(file);
229 struct super_block *sb = inode->i_sb;
231 struct exfat_dir_entry de;
232 struct exfat_dentry_namebuf *nb = &(de.namebuf);
233 struct exfat_inode_info *ei = EXFAT_I(inode);
236 int err = 0, fake_offset = 0;
238 exfat_init_namebuf(nb);
241 if (!dir_emit_dots(file, ctx))
244 if (ctx->pos == ITER_POS_FILLED_DOTS) {
249 cpos = round_up(cpos, DENTRY_SIZE);
251 /* name buffer should be allocated before use */
252 err = exfat_alloc_namebuf(nb);
256 mutex_lock(&EXFAT_SB(sb)->s_lock);
258 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
261 err = exfat_readdir(inode, &cpos, &de);
264 * At least we tried to read a sector.
265 * Move cpos to next sector position (should be aligned).
268 cpos += 1 << (sb->s_blocksize_bits);
269 cpos &= ~(sb->s_blocksize - 1);
279 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
280 tmp = exfat_iget(sb, i_pos);
285 inum = iunique(sb, EXFAT_ROOT_INO);
288 mutex_unlock(&EXFAT_SB(sb)->s_lock);
289 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
290 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
296 if (!cpos && fake_offset)
297 cpos = ITER_POS_FILLED_DOTS;
299 mutex_unlock(&EXFAT_SB(sb)->s_lock);
302 * To improve performance, free namebuf after unlock sb_lock.
303 * If namebuf is not allocated, this function do nothing
305 exfat_free_namebuf(nb);
309 WRAP_DIR_ITER(exfat_iterate) // FIXME!
310 const struct file_operations exfat_dir_operations = {
311 .llseek = generic_file_llseek,
312 .read = generic_read_dir,
313 .iterate_shared = shared_exfat_iterate,
314 .unlocked_ioctl = exfat_ioctl,
316 .compat_ioctl = exfat_compat_ioctl,
318 .fsync = exfat_file_fsync,
321 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
325 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
327 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
331 return exfat_zeroed_cluster(inode, clu->dir);
334 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
338 len = p_uniname->name_len;
342 /* 1 file entry + 1 stream entry + name entries */
343 return ES_ENTRY_NUM(len);
346 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
348 if (ep->type == EXFAT_UNUSED)
350 if (IS_EXFAT_DELETED(ep->type))
352 if (ep->type == EXFAT_INVAL)
354 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
355 if (ep->type == EXFAT_BITMAP)
357 if (ep->type == EXFAT_UPCASE)
359 if (ep->type == EXFAT_VOLUME)
361 if (ep->type == EXFAT_FILE) {
362 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
366 return TYPE_CRITICAL_PRI;
368 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
369 if (ep->type == EXFAT_GUID)
371 if (ep->type == EXFAT_PADDING)
373 if (ep->type == EXFAT_ACLTAB)
375 return TYPE_BENIGN_PRI;
377 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
378 if (ep->type == EXFAT_STREAM)
380 if (ep->type == EXFAT_NAME)
382 if (ep->type == EXFAT_ACL)
384 return TYPE_CRITICAL_SEC;
387 if (ep->type == EXFAT_VENDOR_EXT)
388 return TYPE_VENDOR_EXT;
389 if (ep->type == EXFAT_VENDOR_ALLOC)
390 return TYPE_VENDOR_ALLOC;
392 return TYPE_BENIGN_SEC;
395 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
397 if (type == TYPE_UNUSED) {
398 ep->type = EXFAT_UNUSED;
399 } else if (type == TYPE_DELETED) {
400 ep->type &= EXFAT_DELETE;
401 } else if (type == TYPE_STREAM) {
402 ep->type = EXFAT_STREAM;
403 } else if (type == TYPE_EXTEND) {
404 ep->type = EXFAT_NAME;
405 } else if (type == TYPE_BITMAP) {
406 ep->type = EXFAT_BITMAP;
407 } else if (type == TYPE_UPCASE) {
408 ep->type = EXFAT_UPCASE;
409 } else if (type == TYPE_VOLUME) {
410 ep->type = EXFAT_VOLUME;
411 } else if (type == TYPE_DIR) {
412 ep->type = EXFAT_FILE;
413 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
414 } else if (type == TYPE_FILE) {
415 ep->type = EXFAT_FILE;
416 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
420 static void exfat_init_stream_entry(struct exfat_dentry *ep,
421 unsigned char flags, unsigned int start_clu,
422 unsigned long long size)
424 exfat_set_entry_type(ep, TYPE_STREAM);
425 ep->dentry.stream.flags = flags;
426 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
427 ep->dentry.stream.valid_size = cpu_to_le64(size);
428 ep->dentry.stream.size = cpu_to_le64(size);
431 static void exfat_init_name_entry(struct exfat_dentry *ep,
432 unsigned short *uniname)
436 exfat_set_entry_type(ep, TYPE_EXTEND);
437 ep->dentry.name.flags = 0x0;
439 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
440 if (*uniname != 0x0) {
441 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
444 ep->dentry.name.unicode_0_14[i] = 0x0;
449 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
450 int entry, unsigned int type, unsigned int start_clu,
451 unsigned long long size)
453 struct super_block *sb = inode->i_sb;
454 struct exfat_sb_info *sbi = EXFAT_SB(sb);
455 struct timespec64 ts = current_time(inode);
456 struct exfat_dentry *ep;
457 struct buffer_head *bh;
460 * We cannot use exfat_get_dentry_set here because file ep is not
463 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
467 exfat_set_entry_type(ep, type);
468 exfat_set_entry_time(sbi, &ts,
469 &ep->dentry.file.create_tz,
470 &ep->dentry.file.create_time,
471 &ep->dentry.file.create_date,
472 &ep->dentry.file.create_time_cs);
473 exfat_set_entry_time(sbi, &ts,
474 &ep->dentry.file.modify_tz,
475 &ep->dentry.file.modify_time,
476 &ep->dentry.file.modify_date,
477 &ep->dentry.file.modify_time_cs);
478 exfat_set_entry_time(sbi, &ts,
479 &ep->dentry.file.access_tz,
480 &ep->dentry.file.access_time,
481 &ep->dentry.file.access_date,
484 exfat_update_bh(bh, IS_DIRSYNC(inode));
487 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
491 exfat_init_stream_entry(ep,
492 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
494 exfat_update_bh(bh, IS_DIRSYNC(inode));
500 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
503 struct super_block *sb = inode->i_sb;
507 struct exfat_dentry *ep, *fep;
508 struct buffer_head *fbh, *bh;
510 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
514 num_entries = fep->dentry.file.num_ext + 1;
515 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
517 for (i = 1; i < num_entries; i++) {
518 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
523 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
528 fep->dentry.file.checksum = cpu_to_le16(chksum);
529 exfat_update_bh(fbh, IS_DIRSYNC(inode));
535 static void exfat_free_benign_secondary_clusters(struct inode *inode,
536 struct exfat_dentry *ep)
538 struct super_block *sb = inode->i_sb;
539 struct exfat_chain dir;
540 unsigned int start_clu =
541 le32_to_cpu(ep->dentry.generic_secondary.start_clu);
542 u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
543 unsigned char flags = ep->dentry.generic_secondary.flags;
545 if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
548 exfat_chain_set(&dir, start_clu,
549 EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
551 exfat_free_cluster(inode, &dir);
554 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
555 int entry, int num_entries, struct exfat_uni_name *p_uniname)
557 struct super_block *sb = inode->i_sb;
559 unsigned short *uniname = p_uniname->name;
560 struct exfat_dentry *ep;
561 struct buffer_head *bh;
562 int sync = IS_DIRSYNC(inode);
564 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
568 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
569 exfat_update_bh(bh, sync);
572 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
576 ep->dentry.stream.name_len = p_uniname->name_len;
577 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
578 exfat_update_bh(bh, sync);
581 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
582 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
586 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
587 exfat_free_benign_secondary_clusters(inode, ep);
589 exfat_init_name_entry(ep, uniname);
590 exfat_update_bh(bh, sync);
592 uniname += EXFAT_FILE_NAME_LEN;
595 exfat_update_dir_chksum(inode, p_dir, entry);
599 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
600 int entry, int order, int num_entries)
602 struct super_block *sb = inode->i_sb;
604 struct exfat_dentry *ep;
605 struct buffer_head *bh;
607 for (i = order; i < num_entries; i++) {
608 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
612 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
613 exfat_free_benign_secondary_clusters(inode, ep);
615 exfat_set_entry_type(ep, TYPE_DELETED);
616 exfat_update_bh(bh, IS_DIRSYNC(inode));
623 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
625 int chksum_type = CS_DIR_ENTRY, i;
626 unsigned short chksum = 0;
627 struct exfat_dentry *ep;
629 for (i = ES_IDX_FILE; i < es->num_entries; i++) {
630 ep = exfat_get_dentry_cached(es, i);
631 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
633 chksum_type = CS_DEFAULT;
635 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
636 ep->dentry.file.checksum = cpu_to_le16(chksum);
640 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
645 err = exfat_update_bhs(es->bh, es->num_bh, sync);
647 for (i = 0; i < es->num_bh; i++)
653 if (IS_DYNAMIC_ES(es))
659 static int exfat_walk_fat_chain(struct super_block *sb,
660 struct exfat_chain *p_dir, unsigned int byte_offset,
663 struct exfat_sb_info *sbi = EXFAT_SB(sb);
664 unsigned int clu_offset;
665 unsigned int cur_clu;
667 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
668 cur_clu = p_dir->dir;
670 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
671 cur_clu += clu_offset;
673 while (clu_offset > 0) {
674 if (exfat_get_next_cluster(sb, &cur_clu))
676 if (cur_clu == EXFAT_EOF_CLUSTER) {
678 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
680 EXFAT_B_TO_DEN(byte_offset));
691 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
692 int entry, sector_t *sector, int *offset)
695 unsigned int off, clu = 0;
696 struct exfat_sb_info *sbi = EXFAT_SB(sb);
698 off = EXFAT_DEN_TO_B(entry);
700 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
704 /* byte offset in cluster */
705 off = EXFAT_CLU_OFFSET(off, sbi);
707 /* byte offset in sector */
708 *offset = EXFAT_BLK_OFFSET(off, sb);
710 /* sector offset in cluster */
711 *sector = EXFAT_B_TO_BLK(off, sb);
712 *sector += exfat_cluster_to_sector(sbi, clu);
716 #define EXFAT_MAX_RA_SIZE (128*1024)
717 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
719 struct exfat_sb_info *sbi = EXFAT_SB(sb);
720 struct buffer_head *bh;
721 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
722 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
723 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
724 unsigned int ra_count = min(adj_ra_count, max_ra_count);
726 /* Read-ahead is not required */
727 if (sbi->sect_per_clus == 1)
730 if (sec < sbi->data_start_sector) {
731 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
732 (unsigned long long)sec, sbi->data_start_sector);
736 /* Not sector aligned with ra_count, resize ra_count to page size */
737 if ((sec - sbi->data_start_sector) & (ra_count - 1))
738 ra_count = page_ra_count;
740 bh = sb_find_get_block(sb, sec);
741 if (!bh || !buffer_uptodate(bh)) {
744 for (i = 0; i < ra_count; i++)
745 sb_breadahead(sb, (sector_t)(sec + i));
751 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
752 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
754 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
758 if (p_dir->dir == DIR_DELETED) {
759 exfat_err(sb, "abnormal access to deleted dentry");
763 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
766 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
767 !(entry & (dentries_per_page - 1)))
768 exfat_dir_readahead(sb, sec);
770 *bh = sb_bread(sb, sec);
774 return (struct exfat_dentry *)((*bh)->b_data + off);
777 enum exfat_validate_dentry_mode {
779 ES_MODE_GET_FILE_ENTRY,
780 ES_MODE_GET_STRM_ENTRY,
781 ES_MODE_GET_NAME_ENTRY,
782 ES_MODE_GET_CRITICAL_SEC_ENTRY,
783 ES_MODE_GET_BENIGN_SEC_ENTRY,
786 static bool exfat_validate_entry(unsigned int type,
787 enum exfat_validate_dentry_mode *mode)
789 if (type == TYPE_UNUSED || type == TYPE_DELETED)
793 case ES_MODE_STARTED:
794 if (type != TYPE_FILE && type != TYPE_DIR)
796 *mode = ES_MODE_GET_FILE_ENTRY;
798 case ES_MODE_GET_FILE_ENTRY:
799 if (type != TYPE_STREAM)
801 *mode = ES_MODE_GET_STRM_ENTRY;
803 case ES_MODE_GET_STRM_ENTRY:
804 if (type != TYPE_EXTEND)
806 *mode = ES_MODE_GET_NAME_ENTRY;
808 case ES_MODE_GET_NAME_ENTRY:
809 if (type & TYPE_BENIGN_SEC)
810 *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
811 else if (type != TYPE_EXTEND)
814 case ES_MODE_GET_BENIGN_SEC_ENTRY:
815 /* Assume unreconized benign secondary entry */
816 if (!(type & TYPE_BENIGN_SEC))
826 struct exfat_dentry *exfat_get_dentry_cached(
827 struct exfat_entry_set_cache *es, int num)
829 int off = es->start_off + num * DENTRY_SIZE;
830 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
831 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
833 return (struct exfat_dentry *)p;
837 * Returns a set of dentries for a file or dir.
839 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
840 * User should call exfat_get_dentry_set() after setting 'modified' to apply
841 * changes made in this entry set to the real device.
844 * sb+p_dir+entry: indicates a file/dir
845 * type: specifies how many dentries should be included.
847 * pointer of entry set on success,
850 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
851 struct super_block *sb, struct exfat_chain *p_dir, int entry,
857 struct exfat_sb_info *sbi = EXFAT_SB(sb);
858 struct exfat_dentry *ep;
860 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
861 struct buffer_head *bh;
863 if (p_dir->dir == DIR_DELETED) {
864 exfat_err(sb, "access to deleted dentry");
868 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
872 memset(es, 0, sizeof(*es));
874 es->modified = false;
878 bh = sb_bread(sb, sec);
881 es->bh[es->num_bh++] = bh;
883 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
884 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
887 num_entries = type == ES_ALL_ENTRIES ?
888 ep->dentry.file.num_ext + 1 : type;
889 es->num_entries = num_entries;
891 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
892 if (num_bh > ARRAY_SIZE(es->__bh)) {
893 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
901 for (i = 1; i < num_bh; i++) {
902 /* get the next sector */
903 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
904 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
906 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
908 else if (exfat_get_next_cluster(sb, &clu))
910 sec = exfat_cluster_to_sector(sbi, clu);
915 bh = sb_bread(sb, sec);
918 es->bh[es->num_bh++] = bh;
921 /* validate cached dentries */
922 for (i = ES_IDX_STREAM; i < num_entries; i++) {
923 ep = exfat_get_dentry_cached(es, i);
924 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
930 exfat_put_dentry_set(es, false);
934 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
936 hint_femp->eidx = EXFAT_HINT_NONE;
937 hint_femp->count = 0;
940 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
941 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
942 int dentry, int num_entries, int entry_type)
944 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
945 ei->hint_femp.eidx > dentry) {
946 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
948 if (candi_empty->count == 0) {
949 candi_empty->cur = *clu;
950 candi_empty->eidx = dentry;
953 if (entry_type == TYPE_UNUSED)
954 candi_empty->count += total_entries - dentry;
956 candi_empty->count++;
958 if (candi_empty->count == num_entries ||
959 candi_empty->count + candi_empty->eidx == total_entries)
960 ei->hint_femp = *candi_empty;
972 * @ei: inode info of parent directory
973 * @p_dir: directory structure of parent directory
974 * @num_entries:entry size of p_uniname
975 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
976 * for traversing cluster chain.
978 * >= 0: file directory entry position where the name exists
979 * -ENOENT: entry with the name does not exist
982 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
983 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
984 struct exfat_hint *hint_opt)
986 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
987 int order, step, name_len = 0;
988 int dentries_per_clu;
989 unsigned int entry_type;
990 unsigned short *uniname = NULL;
991 struct exfat_chain clu;
992 struct exfat_hint *hint_stat = &ei->hint_stat;
993 struct exfat_hint_femp candi_empty;
994 struct exfat_sb_info *sbi = EXFAT_SB(sb);
995 int num_entries = exfat_calc_num_entries(p_uniname);
1000 dentries_per_clu = sbi->dentries_per_clu;
1002 exfat_chain_dup(&clu, p_dir);
1004 if (hint_stat->eidx) {
1005 clu.dir = hint_stat->clu;
1006 dentry = hint_stat->eidx;
1010 exfat_reset_empty_hint(&ei->hint_femp);
1014 step = DIRENT_STEP_FILE;
1015 exfat_reset_empty_hint(&candi_empty);
1017 while (clu.dir != EXFAT_EOF_CLUSTER) {
1018 i = dentry & (dentries_per_clu - 1);
1019 for (; i < dentries_per_clu; i++, dentry++) {
1020 struct exfat_dentry *ep;
1021 struct buffer_head *bh;
1023 if (rewind && dentry == end_eidx)
1026 ep = exfat_get_dentry(sb, &clu, i, &bh);
1030 entry_type = exfat_get_entry_type(ep);
1032 if (entry_type == TYPE_UNUSED ||
1033 entry_type == TYPE_DELETED) {
1034 step = DIRENT_STEP_FILE;
1036 exfat_set_empty_hint(ei, &candi_empty, &clu,
1037 dentry, num_entries,
1041 if (entry_type == TYPE_UNUSED)
1046 exfat_reset_empty_hint(&candi_empty);
1048 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1049 step = DIRENT_STEP_FILE;
1050 hint_opt->clu = clu.dir;
1052 num_ext = ep->dentry.file.num_ext;
1053 step = DIRENT_STEP_STRM;
1058 if (entry_type == TYPE_STREAM) {
1061 if (step != DIRENT_STEP_STRM) {
1062 step = DIRENT_STEP_FILE;
1066 step = DIRENT_STEP_FILE;
1067 name_hash = le16_to_cpu(
1068 ep->dentry.stream.name_hash);
1069 if (p_uniname->name_hash == name_hash &&
1070 p_uniname->name_len ==
1071 ep->dentry.stream.name_len) {
1072 step = DIRENT_STEP_NAME;
1081 if (entry_type == TYPE_EXTEND) {
1082 unsigned short entry_uniname[16], unichar;
1084 if (step != DIRENT_STEP_NAME ||
1085 name_len >= MAX_NAME_LENGTH) {
1086 step = DIRENT_STEP_FILE;
1091 uniname = p_uniname->name;
1093 uniname += EXFAT_FILE_NAME_LEN;
1095 len = exfat_extract_uni_name(ep, entry_uniname);
1098 unichar = *(uniname+len);
1099 *(uniname+len) = 0x0;
1101 if (exfat_uniname_ncmp(sb, uniname,
1102 entry_uniname, len)) {
1103 step = DIRENT_STEP_FILE;
1104 } else if (p_uniname->name_len == name_len) {
1105 if (order == num_ext)
1107 step = DIRENT_STEP_SECD;
1110 *(uniname+len) = unichar;
1115 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1116 if (step == DIRENT_STEP_SECD) {
1117 if (++order == num_ext)
1122 step = DIRENT_STEP_FILE;
1125 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1129 clu.dir = EXFAT_EOF_CLUSTER;
1131 if (exfat_get_next_cluster(sb, &clu.dir))
1138 * We started at not 0 index,so we should try to find target
1139 * from 0 index to the index we started at.
1141 if (!rewind && end_eidx) {
1144 clu.dir = p_dir->dir;
1149 * set the EXFAT_EOF_CLUSTER flag to avoid search
1150 * from the beginning again when allocated a new cluster
1152 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1153 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1154 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1155 ei->hint_femp.count = 0;
1158 /* initialized hint_stat */
1159 hint_stat->clu = p_dir->dir;
1160 hint_stat->eidx = 0;
1164 /* next dentry we'll find is out of this cluster */
1165 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1168 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1172 clu.dir = EXFAT_EOF_CLUSTER;
1174 ret = exfat_get_next_cluster(sb, &clu.dir);
1177 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1178 /* just initialized hint_stat */
1179 hint_stat->clu = p_dir->dir;
1180 hint_stat->eidx = 0;
1181 return (dentry - num_ext);
1185 hint_stat->clu = clu.dir;
1186 hint_stat->eidx = dentry + 1;
1187 return dentry - num_ext;
1190 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1191 int entry, struct exfat_dentry *ep)
1195 struct exfat_dentry *ext_ep;
1196 struct buffer_head *bh;
1198 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1199 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1203 type = exfat_get_entry_type(ext_ep);
1205 if (type & TYPE_CRITICAL_SEC || type & TYPE_BENIGN_SEC)
1211 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1214 int dentries_per_clu;
1215 unsigned int entry_type;
1216 struct exfat_chain clu;
1217 struct exfat_dentry *ep;
1218 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1219 struct buffer_head *bh;
1221 dentries_per_clu = sbi->dentries_per_clu;
1223 exfat_chain_dup(&clu, p_dir);
1225 while (clu.dir != EXFAT_EOF_CLUSTER) {
1226 for (i = 0; i < dentries_per_clu; i++) {
1227 ep = exfat_get_dentry(sb, &clu, i, &bh);
1230 entry_type = exfat_get_entry_type(ep);
1233 if (entry_type == TYPE_UNUSED)
1235 if (entry_type != TYPE_DIR)
1240 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1244 clu.dir = EXFAT_EOF_CLUSTER;
1246 if (exfat_get_next_cluster(sb, &(clu.dir)))