Skip to contentSkip to conversation

treewide: add missing newline characters

PATCH New David Timber 1 participant 6 messages Nov 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 treewide: add missing newline characters

Add missing newline characters in error messages.

---
 defrag/defrag.c | 2 +-
 lib/libexfat.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
defrag/defrag.c +1 -1
index 0ac671f..00c60fc 100644
@@ -193,7 +193,7 @@ static int add_virtual_node(struct cluster_info_set *set, uint32_t data)
 		new_size = (offset + index + VIRT_SPACE_ALIGN) * sizeof(uint32_t);
 		new_next_clus = realloc(set->next_clus, new_size);
 		if (new_next_clus == NULL) {
-			exfat_err("memory run out in %s", __func__);
+			exfat_err("memory run out in %s\n", __func__);
 			return -ENOMEM;
 		}
 		set->next_clus = new_next_clus;
lib/libexfat.c +1 -1
index af96884..c242c1c 100644
@@ -1990,7 +1990,7 @@ int exfat_root_clus_count(struct exfat *exfat)
 		exfat_bitmap_set(exfat->alloc_bitmap, clus);
 
 		if (exfat_get_inode_next_clus(exfat, node, clus, &next)) {
-			exfat_err("ERROR: failed to read the fat entry of root");
+			exfat_err("ERROR: failed to read the fat entry of root\n");
 			return -EIO;
 		}
 
Signature · 1 line
2.56.0
#1 lore raw

PATCH 2/6 lib: add unaligned memory access test

To ensure that exfatprogs is supported on CPUs without unaligned memory access support, add the unaligned memory access "spike" test in suite0000-dump-arch.c to log the result in CI. On MIPS and SPARC, it should say:

Unaligned access: CRASH(3)

Also, remove unnecessary linking of libexfat.a.

---
 lib/Makefile.am                 |  1 -
 lib/tests/suite0000-dump-arch.c | 80 ++++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)
lib/Makefile.am +0 -1
index 484f0b2..7810e9f 100644
@@ -7,7 +7,6 @@ check_PROGRAMS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite000
 TESTS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite0001-parse
 
 tests_suite0000_dump_arch_SOURCES = tests/suite0000-dump-arch.c
-tests_suite0000_dump_arch_LDADD = libexfat.a
 
 tests_suite0000_bitmap_SOURCES = tests/suite0000-bitmap.c
 tests_suite0000_bitmap_LDADD = libexfat.a
lib/tests/suite0000-dump-arch.c +79 -1

103 lines not on this page · raw message

Signature · 1 line
2.56.0
#2 lore raw

PATCH 3/6 lib: relocate crc.c from mkfs to lib

In reply to David Timber

Relocate crc functions from mkfs to lib for testing and use in other parts of the project.

No functional change.

---
 include/libexfat.h  | 3 +++
 lib/Makefile.am     | 2 +-
 {mkfs => lib}/crc.c | 1 -
 mkfs/Makefile.am    | 2 +-
 mkfs/mkfs.h         | 3 ---
 5 files changed, 5 insertions(+), 6 deletions(-)
 rename {mkfs => lib}/crc.c (99%)
include/libexfat.h +3 -0
index 3749086..5e4a0f6 100644
@@ -505,6 +505,9 @@ void exfat_put_mbr_partition(const struct exfat_blk_dev *bd, void *dst,
 		const uint8_t ptype, uint32_t chs_limit);
 void exfat_put_bootstrap_code(const char *user_msg, void *dst, unsigned int code_offset);
 
+/* crc.c */
+uint32_t exfat_efi_crc32(const void *buf, size_t len);
+
 /*
  * Misc
  */
lib/Makefile.am +1 -1
index 7810e9f..c6993cc 100644
@@ -1,7 +1,7 @@
 AM_CFLAGS = -Wall -include $(top_builddir)/config.h -I$(top_srcdir)/include -fno-common
 noinst_LIBRARIES = libexfat.a
 
-libexfat_a_SOURCES = libexfat.c exfat_fs.c exfat_dir.c utils.c
+libexfat_a_SOURCES = libexfat.c exfat_fs.c exfat_dir.c utils.c crc.c
 
 check_PROGRAMS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite0001-parse
 TESTS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite0001-parse
lib/crc.c renamed from mkfs/crc.c +0 -1
similarity index 99%
rename from mkfs/crc.c
rename to lib/crc.c
index 0423ecb..8a08d41 100644
@@ -4,7 +4,6 @@
  */
 #include "exfat_ondisk.h"
 #include "libexfat.h"
-#include "mkfs.h"
 
 static const uint32_t crc32table_le[256] = {
 	/*-
mkfs/Makefile.am +1 -1
index 230b232..42426d8 100644
@@ -3,4 +3,4 @@ mkfs_exfat_LDADD = $(top_builddir)/lib/libexfat.a $(BLKID_LIBS)
 
 sbin_PROGRAMS = mkfs.exfat
 
-mkfs_exfat_SOURCES = mkfs.c upcase.c mkfs.h crc.c
+mkfs_exfat_SOURCES = mkfs.c upcase.c mkfs.h
mkfs/mkfs.h +0 -3
index 4eadc96..eafffbd 100644
@@ -140,9 +140,6 @@ bool exfat_write_mkfs_data_regions(struct exfat_blk_dev *bd,
 bool exfat_verify_mkfs_data_regions(struct exfat_blk_dev *bd,
 		const struct exfat_mkfs_data_region *r);
 
-/* crc.c */
-uint32_t exfat_efi_crc32(const void *buf, size_t len);
-
 bool exfat_open_rnddev(void);
 void exfat_close_rnddev(void);
 void exfat_gen_guid(void *out);
Signature · 1 line
2.56.0
#3 lore raw

PATCH 4/6 lib: rename and export crc32 functions

In reply to David Timber

Rename crc32_le_base() to exfat_crc32_le_base() and make it a library function for upcoming features. Rename exfat_efi_crc32() to exfat_calc_crc32() as it's for calculating IEEE 802-3 CRC32(0x04C11DB7) in general.

No functional change.

---
 include/libexfat.h | 3 ++-
 lib/crc.c          | 6 +++---
 mkfs/mkfs.c        | 6 +++---
 3 files changed, 8 insertions(+), 7 deletions(-)
include/libexfat.h +2 -1
index 5e4a0f6..cf44b07 100644
@@ -506,7 +506,8 @@ void exfat_put_mbr_partition(const struct exfat_blk_dev *bd, void *dst,
 void exfat_put_bootstrap_code(const char *user_msg, void *dst, unsigned int code_offset);
 
 /* crc.c */
-uint32_t exfat_efi_crc32(const void *buf, size_t len);
+uint32_t exfat_crc32_le_base(uint32_t crc, const void *m, size_t len);
+uint32_t exfat_calc_crc32(const void *buf, size_t len);
 
 /*
  * Misc
lib/crc.c +3 -3
index 8a08d41..5cf8f86 100644
@@ -56,7 +56,7 @@ static const uint32_t crc32table_le[256] = {
 };
 
 /* From Linux kernel lib/crc/crc32-main.c */
-static inline uint32_t crc32_le_base(uint32_t crc, const void *m, size_t len)
+uint32_t exfat_crc32_le_base(uint32_t crc, const void *m, size_t len)
 {
 	const uint8_t *p = m;
 
@@ -66,7 +66,7 @@ static inline uint32_t crc32_le_base(uint32_t crc, const void *m, size_t len)
 }
 
 /* From Linux kernel block/partitions/efi.c */
-uint32_t exfat_efi_crc32(const void *buf, size_t len)
+uint32_t exfat_calc_crc32(const void *buf, size_t len)
 {
-	return (crc32_le_base(~0L, buf, len) ^ ~0L);
+	return (exfat_crc32_le_base(~0L, buf, len) ^ ~0L);
 }
mkfs/mkfs.c +3 -3
index fe3e416..3371373 100644
@@ -278,7 +278,7 @@ static int build_gpt(struct exfat_blk_dev *bd, const struct exfat_user_input *ui
 	head_main->num_partition_entries = EXFAT_GPT_ENTRY_CNT;
 	head_main->sizeof_partition_entry = EXFAT_GPT_ENTRY_SIZE;
 	head_main->partition_entry_array_crc32 = 0;
-	head_main->partition_entry_array_crc32 = exfat_efi_crc32(arr, EXFAT_GPT_ENTRY_ARR_SIZE);
+	head_main->partition_entry_array_crc32 = exfat_calc_crc32(arr, EXFAT_GPT_ENTRY_ARR_SIZE);
 	exfat_print_gpt_header(exfat_debug, "main", head_main);
 	exfat_debug("GPT header partition_entry_array_crc32: 0x%08"PRIx32"\n",
 			head_main->partition_entry_array_crc32);
@@ -299,9 +299,9 @@ static int build_gpt(struct exfat_blk_dev *bd, const struct exfat_user_input *ui
 	gpt_header_to_le(head_main);
 	gpt_header_to_le(head_backup);
 	head_main->header_crc32 = 0;
-	head_main->header_crc32 = exfat_efi_crc32(head_main, sizeof(*head_main));
+	head_main->header_crc32 = exfat_calc_crc32(head_main, sizeof(*head_main));
 	head_backup->header_crc32 = 0;
-	head_backup->header_crc32 = exfat_efi_crc32(head_backup, sizeof(*head_backup));
+	head_backup->header_crc32 = exfat_calc_crc32(head_backup, sizeof(*head_backup));
 	exfat_debug("GPT main   header header_crc32: 0x%08"PRIx32"\n", head_main->header_crc32);
 	exfat_debug("GPT backup header header_crc32: 0x%08"PRIx32"\n", head_backup->header_crc32);
 	head_main->header_crc32 = cpu_to_le32(head_main->header_crc32);
Signature · 1 line
2.56.0
#4 lore raw

PATCH 5/6 lib: add CRC32 tests

In reply to David Timber

Add lib/tests/suite0002-crc for checking correctness of CRC32 calculation and benchmarking performance and memory footprint. This is in preparation for using CRC32 as duplicate filename handling.

No functional change.

Benchmark result on RPI 3(BCM2837):

	$ /usr/bin/time -v ./lib/tests/suite0002-crc
	=== IMG_XXXX.jpg, exFAT ===
	collisions:	9234 out of 10000 (92%)
	elapsed:	0.022618742s
	=== IMG_XXXX.jpg, CRC32 ===
	collisions:	0 out of 10000 (0%)
	elapsed:	0.029111054s
	=== IMG_YYYYMMDD_hhmmss.jpg, exFAT ===
	collisions:	49120 out of 65536 (74%)
	elapsed:	0.287861648s
	=== IMG_YYYYMMDD_hhmmss.jpg, CRC32 ===
	collisions:	1213 out of 65536 (1%)
	elapsed:	0.378736875s
	...
		Maximum resident set size (kbytes): 3924
---
 lib/Makefile.am           |  15 +-
 lib/tests/suite0002-crc.c | 299 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 312 insertions(+), 2 deletions(-)
 create mode 100644 lib/tests/suite0002-crc.c
lib/Makefile.am +13 -2
index c6993cc..e9a21db 100644
@@ -3,8 +3,16 @@ noinst_LIBRARIES = libexfat.a
 
 libexfat_a_SOURCES = libexfat.c exfat_fs.c exfat_dir.c utils.c crc.c
 
-check_PROGRAMS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite0001-parse
-TESTS = tests/suite0000-bitmap tests/suite0000-dump-arch tests/suite0001-parse
+check_PROGRAMS = \
+	tests/suite0000-bitmap \
+	tests/suite0000-dump-arch \
+	tests/suite0001-parse \
+	tests/suite0002-crc
+TESTS = \
+	tests/suite0000-bitmap \
+	tests/suite0000-dump-arch \
+	tests/suite0001-parse \
+	tests/suite0002-crc
 
 tests_suite0000_dump_arch_SOURCES = tests/suite0000-dump-arch.c
 
@@ -14,6 +22,9 @@ tests_suite0000_bitmap_LDADD = libexfat.a
 tests_suite0001_parse_SOURCES = tests/suite0001-parse.c
 tests_suite0001_parse_LDADD = libexfat.a
 
+tests_suite0002_crc_SOURCES = tests/suite0002-crc.c
+tests_suite0002_crc_LDADD = libexfat.a
+
 if ENABLE_TESTS
 noinst_PROGRAMS = $(check_PROGRAMS)
 endif
lib/tests/suite0002-crc.c new file +299 -0

299 lines not on this page · raw message

Signature · 1 line
2.56.0
#5 lore raw

PATCH 6/6 fsck: use CRC32 and tsearch() for duplicate filename detection

In reply to David Timber

Currently, the standard exFAT 16-bit file name hashing algorithm is used with a bitmap to detect duplicate file names. As the 16-bit address space is too small, the O(N) fallback by rewinding de_iter almost always happens when the directory being checked is well-populated.

Alongside the existing 16-bit hashing for integrity check, also calculate the CRC32 of file names using the facilities introduced in commit 0844be34e702 ("mkfs: add partition table creation support for Windows compatibility"). Use tsearch() and tfind() to map the CRC32 values to detect duplicate file names. CRC32 provides good enough distribution in most use cases.

At the cost of reducing the chance of O(N) fallback search to near zero, the use of binary tree increases memory footprint significantly. For checking a directory containing 65K files, about 4MB of RSS is consumed on 64-bit machines. The memory consumption will be much lower on 32-bit machines as the data is mostly made up of pointers.

Performance and memory footprint impact:

pre-patch:

	exfatprogs version : 1.4.3 (2026-08-14)
	/dev/mmcblk0p1: clean. directories 5, files 62819
		Command being timed: "fsck.exfat /dev/mmcblk0p1"
		Elapsed (wall clock) time (h:mm:ss or m:ss): 0:46.95
		Maximum resident set size (kbytes): 3324
		File system inputs: 23584

Patch applied:

	exfatprogs version : 1.4.4 (2026-11-10)
	/dev/mmcblk0p1: clean. directories 5, files 62819
		Command being timed: "./fsck/fsck.exfat /dev/mmcblk0p1"
		Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.56
		Maximum resident set size (kbytes): 4376
		File system inputs: 23584
---
 fsck/fsck.c         | 61 +++++++++++++++++++++++++++++++++++----------
 fsck/fsck.h         | 12 ++++++++-
 include/exfat_dir.h |  3 ++-
 lib/exfat_dir.c     | 13 ++++++++++
 4 files changed, 74 insertions(+), 15 deletions(-)
fsck/fsck.c +48 -13
index 4f22a0e..634248d 100644
@@ -4,6 +4,13 @@
  *   Copyright (C) 2020 Hyunchul Lee <[email protected]>
  */
 
+/* Enable tdestroy() */
+#ifdef __linux__
+#define _GNU_SOURCE
+#else
+#define _BSD_SOURCE
+#endif
+
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -14,6 +21,7 @@
 #include <locale.h>
 #include <assert.h>
 #include <fcntl.h>
+#include <search.h>
 
 #include "exfat_ondisk.h"
 #include "libexfat.h"
@@ -711,12 +719,30 @@ static int handle_duplicated_filename(struct exfat_de_iter *iter,
 			ER_DE_DUPLICATED_NAME, "filename is duplicated");
 }
 
+/* Util functions for name_hash_tree */
+
+static void hash_tree_freef(void *ptr)
+{
+	/* Do nothing: no memory is dynamically allocated on our end */
+	(void)ptr;
+}
+
+static int hash_tree_compf(const void *a, const void *b)
+{
+	if ((uintptr_t)a == (uintptr_t)b)
+		return 0;
+	if ((uintptr_t)a < (uintptr_t)b)
+		return -1;
+	return 1;
+}
+
 static int check_name_dentry_set(struct exfat_de_iter *iter,
 				 struct exfat_inode *inode)
 {
 	struct exfat_dentry *stream_de;
 	size_t name_len;
 	__u16 hash;
+	uint32_t hash_crc32;
 	int ret = 0;
 
 	exfat_de_iter_get(iter, 1, &stream_de);
@@ -746,6 +772,7 @@ static int check_name_dentry_set(struct exfat_de_iter *iter,
 	}
 
 	hash = exfat_calc_name_hash(iter->exfat, inode->name, (int)name_len);
+	hash_crc32 = exfat_calc_name_hash_crc32(iter->exfat, inode->name, (int)name_len);
 	if (cpu_to_le16(hash) != stream_de->stream_name_hash) {
 		if (repair_file_ask(iter, NULL, ER_DE_NAME_HASH,
 				    "the name hash of a file is wrong")) {
@@ -757,10 +784,18 @@ static int check_name_dentry_set(struct exfat_de_iter *iter,
 		}
 	}
 
-	if (BITMAP_GET(iter->name_hash_bitmap, hash)) {
+	if (tfind((void *)(uintptr_t)hash_crc32, iter->name_hash_tree, hash_tree_compf))
 		ret = handle_duplicated_filename(iter, inode);
-	} else
-		BITMAP_SET(iter->name_hash_bitmap, hash);
+	else {
+		void *inserted = tsearch((void *)(uintptr_t)hash_crc32,
+					 iter->name_hash_tree, hash_tree_compf);
+
+		if (inserted == NULL) {
+			exfat_err("failed to allocate memory\n");
+			/* Returning -ENOMEM here results in incorrect behaviour :( */
+			exit(FSCK_EXIT_OPERATION_ERROR);
+		}
+	}
 
 	return ret;
 }
@@ -1471,9 +1506,11 @@ static int read_children(struct exfat_fsck *fsck, struct exfat_inode *dir)
 	else if (ret)
 		return ret;
 
-	de_iter->name_hash_bitmap = fsck->name_hash_bitmap;
-	memset(fsck->name_hash_bitmap, 0,
-			EXFAT_BITMAP_SIZE(EXFAT_MAX_HASH_COUNT));
+	if (fsck->name_hash_tree != NULL) {
+		tdestroy(fsck->name_hash_tree, hash_tree_freef);
+		fsck->name_hash_tree = NULL;
+	}
+	de_iter->name_hash_tree = &fsck->name_hash_tree;
 
 	while (1) {
 		ret = exfat_de_iter_get(de_iter, 0, &dentry);
@@ -1612,12 +1649,6 @@ static int exfat_filesystem_check(struct exfat_fsck *fsck)
 		return -ENOENT;
 	}
 
-	fsck->name_hash_bitmap = malloc(EXFAT_BITMAP_SIZE(EXFAT_MAX_HASH_COUNT));
-	if (!fsck->name_hash_bitmap) {
-		exfat_err("failed to allocate name hash bitmap\n");
-		return -ENOMEM;
-	}
-
 	list_add(&exfat->root->list, &exfat->dir_list);
 
 	while (!list_empty(&exfat->dir_list)) {
@@ -1646,7 +1677,11 @@ static int exfat_filesystem_check(struct exfat_fsck *fsck)
 	}
 out:
 	exfat_free_dir_list(exfat);
-	free(fsck->name_hash_bitmap);
+	if (fsck->name_hash_tree != NULL) {
+		tdestroy(fsck->name_hash_tree, hash_tree_freef);
+		fsck->name_hash_tree = NULL;
+	}
+
 	return ret;
 }
 
fsck/fsck.h +11 -1
index 5ed758e..6e621e6 100644
@@ -34,7 +34,17 @@ struct exfat_fsck {
 	bool			dirty:1;
 	bool			dirty_fat:1;
 
-	char *name_hash_bitmap;
+	/*
+	 * btree of CRC32 of file names in the current subdirectory
+	 *
+	 * Unlike the traditional use, the key is the CRC32 value itself rather
+	 * than a pointer to a dynamically allocated memory. This is a hack that
+	 * depends on the fact that Linux(and most OSes) support starts from
+	 * 32-bit CPUs. In other words, void * type must be able to hold
+	 * uint32_t. This might not be always the case(eg. IBM S390), although
+	 * the hashing should work just fine with fewer bits.
+	 */
+	void *name_hash_tree;
 	struct progress_bar progress_bar;
 };
 
include/exfat_dir.h +2 -1
index 6edb072..57a2ebe 100644
@@ -28,7 +28,7 @@ struct exfat_de_iter {
 #define INVALID_NAME_NUM_MAX	9999999
 	unsigned int		invalid_name_num;
 
-	char *name_hash_bitmap;		/* bitmap of children's name hashes */
+	void **name_hash_tree;		/* btree of children's name hashes */
 };
 
 struct exfat_lookup_filter {
@@ -106,6 +106,7 @@ void exfat_calc_dentry_checksum(struct exfat_dentry *dentry,
 uint16_t calc_dentry_set_checksum(struct exfat_dentry *dset, int dcount);
 uint16_t exfat_calc_name_hash(struct exfat *exfat,
 			      __le16 *name, int len);
+uint32_t exfat_calc_name_hash_crc32(struct exfat *exfat, __le16 *name, int len);
 
 int exfat_find_free_cluster(struct exfat *exfat, int clu_count,
 		clus_t *new_clu);
lib/exfat_dir.c +13 -0
index 28f4b36..9b63b04 100644
@@ -657,6 +657,19 @@ uint16_t exfat_calc_name_hash(struct exfat *exfat,
 	return chksum;
 }
 
+uint32_t exfat_calc_name_hash_crc32(struct exfat *exfat, __le16 *name, int len)
+{
+	uint16_t ch;
+	uint32_t crc = ~0L;
+
+	while (len-- > 0) {
+		ch = exfat->upcase_table[le16_to_cpu(*name++)];
+		crc = exfat_crc32_le_base(crc, &ch, sizeof(ch));
+	}
+
+	return (crc ^ ~0L);
+}
+
 static void unix_time_to_exfat_time(time_t unix_time, __u8 *tz, __le16 *date,
 				    __le16 *time, __u8 *time_ms)
 {
Signature · 1 line
2.56.0