Skip to contentSkip to conversation

libexfat, fsck: add volume flags definitions

PATCH Applied David Timber 3 participants 8 messages Sep 1 – Sep 3 exFAT file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libexfat, fsck: add volume flags definitions

Add the rest of volume flags(ACTIVE_FAT, DIRTY, MEDIA_FAILURE, CLEAR_TO_ZERO) from the exFAT volume format specs for cleaner code and upcoming features.

Use VOL_DIRTY instead of the magic value(0x02) in fsck.c.

No functional change.

---
 fsck/fsck.c            | 4 ++--
 include/exfat_ondisk.h | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)
fsck/fsck.c +2 -2
index f1934d0..5447154 100644
@@ -370,9 +370,9 @@ static int exfat_mark_volume_dirty(struct exfat *exfat, bool dirty)
 
 	flags = le16_to_cpu(exfat->bs->bsx.vol_flags);
 	if (dirty)
-		flags |= 0x02;
+		flags |= VOL_DIRTY;
 	else
-		flags &= ~0x02;
+		flags &= ~VOL_DIRTY;
 
 	exfat->bs->bsx.vol_flags = cpu_to_le16(flags);
 	if (!exfat_write_full(exfat->blk_dev->dev_fd, exfat->bs, sizeof(struct pbr), 0)) {
include/exfat_ondisk.h +6 -1
index dde84d6..fdb29de 100644
@@ -38,8 +38,13 @@
 
 #define PBR_SIGNATURE		0xAA55
 
-#define VOL_CLEAN		0x0000
+/* Volume flags masks */
+
+#define VOL_ACTIVE_FAT		0x0001
 #define VOL_DIRTY		0x0002
+#define VOL_MEDIA_FAILURE	0x0004
+#define VOL_CLEAR_TO_ZERO	0x0008
+#define VOL_RESERVED		0xFFF0
 
 #define DENTRY_SIZE		32 /* directory entry size */
 #define DENTRY_SIZE_BITS	5
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/2 dump: show all fields of exFAT volume header

Print the rest of the values in the volume header

- fs_version: as 2-byte raw hex values
- vol_flags: the four bit flags as attribute strings, the rest of the reverve bytes as 2-byte hex values
- num_fats
- phy_drv_no: as 1-byte hex value
- perc_in_use
- reserved2: as 7-byte hex values

Especially, being able to inspect vol_flags without mounting the volume or through hexdump should be incredibily useful. Other niche fields that are almost never used like num_fats, fs_version and ACTIVE_FAT should be useful for catching weird non-standard exFAT implementations in the wild.

---
 dump/dump.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
dump/dump.c +29 -6
index d55d561..efa892a 100644
@@ -86,6 +86,17 @@ static int exfat_read_dentry(struct exfat *exfat, struct exfat_inode *inode,
 	return 0;
 }
 
+#define VOLUME_FLAGS_STR_SIZE (46)
+
+static void volume_flags_tostr(const uint16_t vol_flags, const size_t size, char *out)
+{
+	snprintf(out, size, "%s%s%s%s",
+		vol_flags & VOL_ACTIVE_FAT ?	"ACTIVE_FAT " :		"",
+		vol_flags & VOL_DIRTY ?		"DIRTY " :		"",
+		vol_flags & VOL_MEDIA_FAILURE ?	"MEDIA_FAILURE " :	"",
+		vol_flags & VOL_CLEAR_TO_ZERO ?	"CLEAR_TO_ZERO " :	"");
+}
+
 static int exfat_show_fs_info(struct exfat *exfat)
 {
 	struct pbr *ppbr;
@@ -98,6 +109,8 @@ static int exfat_show_fs_info(struct exfat *exfat)
 	int ret;
 	char *volume_label;
 	off_t off;
+	char vol_flags_str[VOLUME_FLAGS_STR_SIZE];
+	uint16_t vol_flags;
 
 	ppbr = exfat->bs;
 	if (memcmp(ppbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
@@ -120,6 +133,10 @@ static int exfat_show_fs_info(struct exfat *exfat)
 		return -EINVAL;
 	}
 
+	vol_flags = le16_to_cpu(pbsx->vol_flags);
+	vol_flags_str[0] = 0;
+	volume_flags_tostr(vol_flags, sizeof(vol_flags_str), vol_flags_str);
+
 	bd->sector_size_bits = pbsx->sect_size_bits;
 	bd->sector_size = 1 << pbsx->sect_size_bits;
 
@@ -128,18 +145,24 @@ static int exfat_show_fs_info(struct exfat *exfat)
 	root_clu = le32_to_cpu(pbsx->root_cluster);
 
 	exfat_info("-------------- Dump Boot sector region --------------\n");
-	dump_field("Volume Length(sectors)", "%" PRIu64,
-			le64_to_cpu(pbsx->vol_length));
-	dump_field("FAT Offset(sector offset)", "%u",
-			le32_to_cpu(pbsx->fat_offset));
-	dump_field("FAT Length(sectors)", "%u",
-			le32_to_cpu(pbsx->fat_length));
+	dump_field("Volume Length(sectors)", "%" PRIu64, le64_to_cpu(pbsx->vol_length));
+	dump_field("FAT Offset(sector offset)", "%u", le32_to_cpu(pbsx->fat_offset));
+	dump_field("FAT Length(sectors)", "%u", le32_to_cpu(pbsx->fat_length));
 	dump_field("Cluster Heap Offset (sector offset)", "%u", clu_offset);
 	dump_field("Cluster Count", "%u", total_clus);
 	dump_field("Root Cluster (cluster offset)", "%u", root_clu);
 	dump_field("Volume Serial", "0x%x", le32_to_cpu(pbsx->vol_serial));
+	dump_field("Version", "%02x%02x", pbsx->fs_version[0], pbsx->fs_version[1]);
+	dump_field("Volume Flags", "%s%04x", vol_flags_str, vol_flags & VOL_RESERVED);
 	dump_field("Bytes per Sector", "%u", 1 << pbsx->sect_size_bits);
 	dump_field("Sectors per Cluster", "%u", 1 << pbsx->sect_per_clus_bits);
+	dump_field("Number of FATs", "%u", pbsx->num_fats);
+	dump_field("Drive Select (advisory)", "%02x", pbsx->phy_drv_no);
+	dump_field("Percent in Use (advisory)", "%u", pbsx->perc_in_use);
+	dump_field("Reserved", "%02x%02x%02x%02x%02x%02x%02x",
+			pbsx->reserved2[0], pbsx->reserved2[1], pbsx->reserved2[2],
+			pbsx->reserved2[3], pbsx->reserved2[4], pbsx->reserved2[5],
+			pbsx->reserved2[6]);
 
 	bd->cluster_size =
 		1 << (pbsx->sect_per_clus_bits + pbsx->sect_size_bits);
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 2/2] dump: show all fields of exFAT volume header

Hi David,

2026년 9월 1일 (화) 오후 5:42, David Timber <[email protected]>님이 작성:

Quoted 67 lines

Print the rest of the values in the volume header

- fs_version: as 2-byte raw hex values
- vol_flags: the four bit flags as attribute strings, the rest of the reverve bytes as 2-byte hex values
- num_fats
- phy_drv_no: as 1-byte hex value
- perc_in_use
- reserved2: as 7-byte hex values

Especially, being able to inspect vol_flags without mounting the volume or through hexdump should be incredibily useful. Other niche fields that are almost never used like num_fats, fs_version and ACTIVE_FAT should be useful for catching weird non-standard exFAT implementations in the wild.

---
 dump/dump.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
dump/dump.c +17 -0
index d55d561..efa892a 100644
@@ -86,6 +86,17 @@ static int exfat_read_dentry(struct exfat *exfat, struct exfat_inode *inode,
        return 0;
 }
 
+#define VOLUME_FLAGS_STR_SIZE (46)
+
+static void volume_flags_tostr(const uint16_t vol_flags, const size_t size, char *out)
+{
+       snprintf(out, size, "%s%s%s%s",
+               vol_flags & VOL_ACTIVE_FAT ?    "ACTIVE_FAT " :         "",
+               vol_flags & VOL_DIRTY ?         "DIRTY " :              "",
+               vol_flags & VOL_MEDIA_FAILURE ? "MEDIA_FAILURE " :      "",
+               vol_flags & VOL_CLEAR_TO_ZERO ? "CLEAR_TO_ZERO " :      "");
+}
+
 static int exfat_show_fs_info(struct exfat *exfat)
 {
        struct pbr *ppbr;
@@ -98,6 +109,8 @@ static int exfat_show_fs_info(struct exfat *exfat)
        int ret;
        char *volume_label;
        off_t off;
+       char vol_flags_str[VOLUME_FLAGS_STR_SIZE];
+       uint16_t vol_flags;
 
        ppbr = exfat->bs;
        if (memcmp(ppbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
@@ -120,6 +133,10 @@ static int exfat_show_fs_info(struct exfat *exfat)
                return -EINVAL;
        }
 
+       vol_flags = le16_to_cpu(pbsx->vol_flags);
+       vol_flags_str[0] = 0;
+       volume_flags_tostr(vol_flags, sizeof(vol_flags_str), vol_flags_str);
+
        bd->sector_size_bits = pbsx->sect_size_bits;
        bd->sector_size = 1 << pbsx->sect_size_bits;
 
@@ -128,18 +145,24 @@ static int exfat_show_fs_info(struct exfat *exfat)
        root_clu = le32_to_cpu(pbsx->root_cluster);
 

dump/dump.c static int exfat_show_fs_info(struct exfat *exfat) · line 150

Quoted 10 lines
dump/dump.c +3 -6
        exfat_info("-------------- Dump Boot sector region --------------\n");
-       dump_field("Volume Length(sectors)", "%" PRIu64,
-                       le64_to_cpu(pbsx->vol_length));
-       dump_field("FAT Offset(sector offset)", "%u",
-                       le32_to_cpu(pbsx->fat_offset));
-       dump_field("FAT Length(sectors)", "%u",
-                       le32_to_cpu(pbsx->fat_length));
+       dump_field("Volume Length(sectors)", "%" PRIu64, le64_to_cpu(pbsx->vol_length));
+       dump_field("FAT Offset(sector offset)", "%u", le32_to_cpu(pbsx->fat_offset));
+       dump_field("FAT Length(sectors)", "%u", le32_to_cpu(pbsx->fat_length));

I'm a bit concerned that adding this in the middle of the existing output might break programs that parse dump.exfat output.

However, I think that they will parse it line by line.

dump/dump.c static int exfat_show_fs_info(struct exfat *exfat) · line 161

Quoted 11 lines
dump/dump.c +5 -0
        dump_field("Cluster Heap Offset (sector offset)", "%u", clu_offset);
        dump_field("Cluster Count", "%u", total_clus);
        dump_field("Root Cluster (cluster offset)", "%u", root_clu);
        dump_field("Volume Serial", "0x%x", le32_to_cpu(pbsx->vol_serial));
+       dump_field("Version", "%02x%02x", pbsx->fs_version[0], pbsx->fs_version[1]);
+       dump_field("Volume Flags", "%s%04x", vol_flags_str, vol_flags & VOL_RESERVED);
        dump_field("Bytes per Sector", "%u", 1 << pbsx->sect_size_bits);
        dump_field("Sectors per Cluster", "%u", 1 << pbsx->sect_per_clus_bits);
+       dump_field("Number of FATs", "%u", pbsx->num_fats);
+       dump_field("Drive Select (advisory)", "%02x", pbsx->phy_drv_no);
+       dump_field("Percent in Use (advisory)", "%u", pbsx->perc_in_use);

How about printing "unknown" if perc_in_use is 0xff?

Quoted 10 lines
dump/dump.c +4 -1
+       dump_field("Reserved", "%02x%02x%02x%02x%02x%02x%02x",
+                       pbsx->reserved2[0], pbsx->reserved2[1], pbsx->reserved2[2],
+                       pbsx->reserved2[3], pbsx->reserved2[4], pbsx->reserved2[5],
+                       pbsx->reserved2[6]);
 
        bd->cluster_size =
                1 << (pbsx->sect_per_clus_bits + pbsx->sect_size_bits);
--

2.55.0

Signature · 2 lines
Thanks,
Hyunchul
#3 lore raw

Re: [PATCH 2/2] dump: show all fields of exFAT volume header

On 9/2/26 01:06, Hyunchul Lee wrote:
[...]

Quoted 5 lines

I'm a bit concerned that adding this in the middle of the existing output might break programs that parse dump.exfat output.

However, I think that they will parse it line by line.

That crossed my mind as well and that is my assumption too.

I thought about adding the -v option and printing the extra fields with the increased verbosity. Other than the volume flags(especially MEDIA_FAILURE - this is a big one) which has legitimate use cases, the other boilerplate fields are not really that useful. I think that's why the original author(s) didn't bother printing all the fields.

I still don't know if we should add -v or not.

Quoted 11 lines

dump/dump.c static int exfat_show_fs_info(struct exfat *exfat) · line 161

Quoted 11 lines
        dump_field("Cluster Heap Offset (sector offset)", "%u", clu_offset);
        dump_field("Cluster Count", "%u", total_clus);
        dump_field("Root Cluster (cluster offset)", "%u", root_clu);
        dump_field("Volume Serial", "0x%x", le32_to_cpu(pbsx->vol_serial));
dump/dump.c +5 -0
+       dump_field("Version", "%02x%02x", pbsx->fs_version[0], pbsx->fs_version[1]);
+       dump_field("Volume Flags", "%s%04x", vol_flags_str, vol_flags & VOL_RESERVED);
        dump_field("Bytes per Sector", "%u", 1 << pbsx->sect_size_bits);
        dump_field("Sectors per Cluster", "%u", 1 << pbsx->sect_per_clus_bits);
+       dump_field("Number of FATs", "%u", pbsx->num_fats);
+       dump_field("Drive Select (advisory)", "%02x", pbsx->phy_drv_no);
+       dump_field("Percent in Use (advisory)", "%u", pbsx->perc_in_use);
Quoted 1 line

How about printing "unknown" if perc_in_use is 0xff?

Yeah. That was dumb. I'll change it to something like this. Interpreted decimal value if in range, raw hex value otherwise:

    #define PERC_IN_USE_STR 13
    char perc_in_use[PERC_IN_USE_STR];

    perc_in_use[0] = 0;

    if (0 <= x && x <= 100)
        snprintf(..., "%u", x);
    else
        snprintf(..., "unknown (%02x)", x);

Davo

#4 lore raw

Re: [PATCH 2/2] dump: show all fields of exFAT volume header

Quoted 43 lines · David Timber

On 9/2/26 01:06, Hyunchul Lee wrote:
[...]

Quoted 5 lines

I'm a bit concerned that adding this in the middle of the existing output might break programs that parse dump.exfat output.

However, I think that they will parse it line by line.

That crossed my mind as well and that is my assumption too.

I thought about adding the -v option and printing the extra fields with the increased verbosity. Other than the volume flags(especially MEDIA_FAILURE - this is a big one) which has legitimate use cases, the other boilerplate fields are not really that useful. I think that's why the original author(s) didn't bother printing all the fields.

I still don't know if we should add -v or not.

Quoted 11 lines

dump/dump.c static int exfat_show_fs_info(struct exfat *exfat) · line 161

Quoted 11 lines
        dump_field("Cluster Heap Offset (sector offset)", "%u", clu_offset);
        dump_field("Cluster Count", "%u", total_clus);
        dump_field("Root Cluster (cluster offset)", "%u", root_clu);
        dump_field("Volume Serial", "0x%x", le32_to_cpu(pbsx->vol_serial));
dump/dump.c +5 -0
+       dump_field("Version", "%02x%02x", pbsx->fs_version[0], pbsx->fs_version[1]);
+       dump_field("Volume Flags", "%s%04x", vol_flags_str, vol_flags & VOL_RESERVED);
        dump_field("Bytes per Sector", "%u", 1 << pbsx->sect_size_bits);
        dump_field("Sectors per Cluster", "%u", 1 << pbsx->sect_per_clus_bits);
+       dump_field("Number of FATs", "%u", pbsx->num_fats);
+       dump_field("Drive Select (advisory)", "%02x", pbsx->phy_drv_no);
+       dump_field("Percent in Use (advisory)", "%u", pbsx->perc_in_use);
Quoted 1 line

How about printing "unknown" if perc_in_use is 0xff?

Yeah. That was dumb. I'll change it to something like this. Interpreted decimal value if in range, raw hex value otherwise:

    #define PERC_IN_USE_STR 13
    char perc_in_use[PERC_IN_USE_STR];

    perc_in_use[0] = 0;

    if (0 <= x && x <= 100)
        snprintf(..., "%u", x);
    else
        snprintf(..., "unknown (%02x)", x);
Quoted 1 line

Davo

Oh, no. The Linux kernel exfat driver doesn't set it at all so it's always stuck at 0. mkfs.exfat doesn't properly set it either. I think mkfs.exfat should set it to 0xFF if it doesn't bother setting it properly.

Will follow up.

Davo

#5 lore raw

Re: [PATCH 2/2] dump: show all fields of exFAT volume header

In reply to David Timber
Quoted 9 lines · David Timber

On 9/2/26 01:06, Hyunchul Lee wrote:
[...]

Quoted 5 lines

I'm a bit concerned that adding this in the middle of the existing output might break programs that parse dump.exfat output.

However, I think that they will parse it line by line.

That crossed my mind as well and that is my assumption too.

Quoted 7 lines

I thought about adding the -v option and printing the extra fields with the increased verbosity. Other than the volume flags(especially MEDIA_FAILURE - this is a big one) which has legitimate use cases, the other boilerplate fields are not really that useful. I think that's why the original author(s) didn't bother printing all the fields.

I still don't know if we should add -v or not.

I think the option -v would be useful if it reports difference between Main and Backup boot regions. This would help determine whether the Main boot region can be restored by the Backup boot region.

Quoted 28 lines
Quoted 12 lines

dump/dump.c static int exfat_show_fs_info(struct exfat *exfat) · line 161

Quoted 12 lines
        dump_field("Cluster Heap Offset (sector offset)", "%u", clu_offset);
        dump_field("Cluster Count", "%u", total_clus);
        dump_field("Root Cluster (cluster offset)", "%u", root_clu);
        dump_field("Volume Serial", "0x%x", le32_to_cpu(pbsx->vol_serial));
dump/dump.c +5 -0
+       dump_field("Version", "%02x%02x", pbsx->fs_version[0], pbsx->fs_version[1]);
+       dump_field("Volume Flags", "%s%04x", vol_flags_str, vol_flags & VOL_RESERVED);
        dump_field("Bytes per Sector", "%u", 1 << pbsx->sect_size_bits);
        dump_field("Sectors per Cluster", "%u", 1 << pbsx->sect_per_clus_bits);
+       dump_field("Number of FATs", "%u", pbsx->num_fats);
+       dump_field("Drive Select (advisory)", "%02x", pbsx->phy_drv_no);
+       dump_field("Percent in Use (advisory)", "%u", pbsx->perc_in_use);
Quoted 1 line

How about printing "unknown" if perc_in_use is 0xff?

Yeah. That was dumb. I'll change it to something like this. Interpreted decimal value if in range, raw hex value otherwise:

    #define PERC_IN_USE_STR 13
    char perc_in_use[PERC_IN_USE_STR];

    perc_in_use[0] = 0;

    if (0 <= x && x <= 100)
        snprintf(..., "%u", x);
    else
        snprintf(..., "unknown (%02x)", x);

Davo

Signature · 2 lines
Thanks,
Hyunchul
Reviewed-by on v1 1/2
#6 lore raw
In reply to David Timber

2026년 9월 1일 (화) 오후 5:42, David Timber <[email protected]>님이 작성:

Quoted 5 lines

Add the rest of volume flags(ACTIVE_FAT, DIRTY, MEDIA_FAILURE, CLEAR_TO_ZERO) from the exFAT volume format specs for cleaner code and upcoming features.

Quoted 8 lines

Use VOL_DIRTY instead of the magic value(0x02) in fsck.c.

No functional change.

---
 fsck/fsck.c            | 4 ++--
 include/exfat_ondisk.h | 7 ++++++-

Looks good to me.

Quoted 40 lines
 2 files changed, 8 insertions(+), 3 deletions(-)
fsck/fsck.c +2 -2
index f1934d0..5447154 100644
@@ -370,9 +370,9 @@ static int exfat_mark_volume_dirty(struct exfat *exfat, bool dirty)
 
        flags = le16_to_cpu(exfat->bs->bsx.vol_flags);
        if (dirty)
-               flags |= 0x02;
+               flags |= VOL_DIRTY;
        else
-               flags &= ~0x02;
+               flags &= ~VOL_DIRTY;
 
        exfat->bs->bsx.vol_flags = cpu_to_le16(flags);
        if (!exfat_write_full(exfat->blk_dev->dev_fd, exfat->bs, sizeof(struct pbr), 0)) {
include/exfat_ondisk.h +6 -2
index dde84d6..fdb29de 100644
@@ -38,8 +38,13 @@
 
 #define PBR_SIGNATURE          0xAA55
 
-#define VOL_CLEAN              0x0000
+/* Volume flags masks */
+
+#define VOL_ACTIVE_FAT         0x0001
 #define VOL_DIRTY              0x0002
+#define VOL_MEDIA_FAILURE      0x0004
+#define VOL_CLEAR_TO_ZERO      0x0008
+#define VOL_RESERVED           0xFFF0
 
 #define DENTRY_SIZE            32 /* directory entry size */
 #define DENTRY_SIZE_BITS       5
--

2.55.0

Signature · 2 lines
Thanks,
Hyunchul
maintainer Applied
#7 lore raw
In reply to David Timber
Quoted 10 lines · David Timber

Add the rest of volume flags(ACTIVE_FAT, DIRTY, MEDIA_FAILURE, CLEAR_TO_ZERO) from the exFAT volume format specs for cleaner code and upcoming features.

Use VOL_DIRTY instead of the magic value(0x02) in fsck.c.

No functional change.

Applied it to #exfat-next.
Thanks!