exfat: update percent_in_use field when updating volume flags

Currently, the exfat fs implementation does not modify the
percent_in_use in the boot sector header at all. Although the field is
only an advisory field for situations where bit counting is not
feasible, not updating the field at all can cause confusion if the
the value is actually exposed to the user.

Calculate the value using integer arithmetic only when the dirty flag is
cleared. If the dirty flag is set, set to 0xFF.

It is set to 0xFF when mounting under the assumption that the number of
used clusters will change - this is basically what is recommended in the
SD Association's specs.

When clearing the dirty flag, if overridden by
sbi->vol_flags_persistent, set to 0xFF. This is intentional because if
there are orphan clusters, the calculated value would be incorrect and
correcting the fs error is fsck.exfat's job.

Signed-off-by: David Timber <dxdt@dev.snart.me>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
This commit is contained in:
David Timber 2026-09-05 17:10:42 +00:00 committed by Namjae Jeon
parent 7d390ca06f
commit d090f9f99a

View File

@ -18,6 +18,7 @@
#include <linux/nls.h>
#include <linux/buffer_head.h>
#include <linux/magic.h>
#include <linux/math64.h>
#include "exfat_raw.h"
#include "exfat_fs.h"
@ -69,16 +70,28 @@ static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
return 0;
}
static inline __u8 exfat_calc_perc_in_use(const struct exfat_sb_info *sbi)
{
return (__u8)mul_u64_u32_div(sbi->used_clusters, 100,
EXFAT_DATA_CLUSTER_COUNT(sbi));
}
static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
__u8 new_piu;
/* retain persistent-flags */
new_flags |= sbi->vol_flags_persistent;
if (new_flags & VOLUME_DIRTY)
new_piu = 0xFF;
else
new_piu = exfat_calc_perc_in_use(sbi);
/* flags are not changed */
if (sbi->vol_flags == new_flags)
if (sbi->vol_flags == new_flags && new_piu == p_boot->percent_in_use)
return 0;
sbi->vol_flags = new_flags;
@ -90,6 +103,7 @@ static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)
return 0;
p_boot->vol_flags = cpu_to_le16(new_flags);
p_boot->percent_in_use = new_piu;
set_buffer_uptodate(sbi->boot_bh);
mark_buffer_dirty(sbi->boot_bh);