Skip to contentSkip to conversation

exfat: update percent_in_use, remove runtime integer division

PATCH Applied David Timber 2 participants 4 messages Sep 5 – Sep 7 exFAT file system

lore
Share & tools
lore
#0 lore raw

cover exfat: update percent_in_use, remove runtime integer division

Changes from RFC v0:

- use mul_u64_u32_div() for 32-bit arch without 64-bit integer division

mul_u64_u32_div() isn't so popular API, so I'm not sure it's available on all arch.

I don't have a beefy machine to test my patches for all arch... If you'd like to apply, I suggest waiting for the CI bots to finish testing. I believe all submitted patches that apply to the master branch are tested by the bots. I'll ping when the patches get a pass.

David Timber (2):
exfat: update percent_in_use field when updating volume flags
exfat: remove runtime integer division

 fs/exfat/dir.c      |  3 ++-
 fs/exfat/exfat_fs.h |  1 +
 fs/exfat/namei.c    |  4 ++--
 fs/exfat/super.c    | 20 +++++++++++++++++---
 4 files changed, 22 insertions(+), 6 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 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.

---
 fs/exfat/super.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
fs/exfat/super.c +15 -1
index a9ea36ba2693..2409e57e7370 100644
@@ -18,6 +18,7 @@
 #include <linux/nls.h>
 #include <linux/buffer_head.h>
 #include <linux/magic.h>
+#include <linux/math.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);
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/2 exfat: remove runtime integer division

In reply to David Timber

Replace the arithmetic ops that compiles to the use of actual integer division with bitwise ops. As the divisors involved are all power of two, integer division is not necessary.

No functional change.

---
 fs/exfat/dir.c      | 3 ++-
 fs/exfat/exfat_fs.h | 1 +
 fs/exfat/namei.c    | 4 ++--
 fs/exfat/super.c    | 4 ++--
 4 files changed, 7 insertions(+), 5 deletions(-)
fs/exfat/dir.c +2 -1
index fa9abedf4d84..46514b13bebd 100644
@@ -1309,7 +1309,8 @@ static int exfat_get_volume_label_dentry(struct super_block *sb,
 			es->bh = es->__bh;
 			es->bh[0] = bh;
 			es->num_bh = 1;
-			es->start_off = exfat_dentries_to_bytes(i) % sb->s_blocksize;
+			es->start_off = exfat_dentries_to_bytes(i) &
+					((u32)sb->s_blocksize - 1);
 
 			return 0;
 		}
fs/exfat/exfat_fs.h +1 -0
index a9131fe03302..41a2c7dfc479 100644
@@ -232,6 +232,7 @@ struct exfat_sb_info {
 	unsigned int num_FAT_sectors; /* num of FAT sectors */
 	unsigned int root_dir; /* root dir cluster */
 	unsigned int dentries_per_clu; /* num of dentries per cluster */
+	unsigned int dentries_per_clu_bits;
 	unsigned int vol_flags; /* volume flags */
 	unsigned int vol_flags_persistent; /* volume flags to retain */
 	struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
fs/exfat/namei.c +2 -2
index 80f60e80786e..3c5746fc57d9 100644
@@ -386,7 +386,7 @@ int exfat_find_empty_entry(struct inode *inode,
 	}
 
 	p_dir->dir = exfat_sector_to_cluster(sbi, es->bh[0]->b_blocknr);
-	p_dir->size -= dentry / sbi->dentries_per_clu;
+	p_dir->size -= dentry >> sbi->dentries_per_clu_bits;
 
 	return dentry & (sbi->dentries_per_clu - 1);
 }
@@ -638,7 +638,7 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 	/* adjust cdir to the optimized value */
 	cdir.dir = hint_opt.clu;
 	if (cdir.flags & ALLOC_NO_FAT_CHAIN)
-		cdir.size -= dentry / sbi->dentries_per_clu;
+		cdir.size -= dentry >> sbi->dentries_per_clu_bits;
 	dentry = hint_opt.eidx;
 
 	info->dir = cdir;
fs/exfat/super.c +2 -2
index 2409e57e7370..217d150652cf 100644
@@ -519,8 +519,8 @@ static int exfat_read_boot_sector(struct super_block *sb)
 		EXFAT_RESERVED_CLUSTERS;
 
 	sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
-	sbi->dentries_per_clu = 1 <<
-		(sbi->cluster_size_bits - DENTRY_SIZE_BITS);
+	sbi->dentries_per_clu_bits = sbi->cluster_size_bits - DENTRY_SIZE_BITS;
+	sbi->dentries_per_clu = 1 << sbi->dentries_per_clu_bits;
 
 	sbi->vol_flags = le16_to_cpu(p_boot->vol_flags);
 	sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE);
Signature · 1 line
2.55.0
maintainer Applied
#3 lore raw
In reply to David Timber
Quoted 15 lines · David Timber

Changes from RFC v0:

- use mul_u64_u32_div() for 32-bit arch without 64-bit integer division

mul_u64_u32_div() isn't so popular API, so I'm not sure it's available on all arch.

I don't have a beefy machine to test my patches for all arch... If you'd like to apply, I suggest waiting for the CI bots to finish testing. I believe all submitted patches that apply to the master branch are tested by the bots. I'll ping when the patches get a pass.

Quoted 3 lines

David Timber (2):
exfat: update percent_in_use field when updating volume flags
exfat: remove runtime integer division

Applied them to #dev.
Thanks!