[PATCH v1 5/6] exfat: introduce exfat_chain_advance helper

From: Chi Zhiling

Date: Tue Mar 31 2026 - 05:20:58 EST


From: Chi Zhiling <chizhiling@xxxxxxxxxx>

Introduce exfat_chain_advance() to walk a exfat_chain structure by a
given step, updating both ->dir and ->size fields atomically. This
helper handles both ALLOC_NO_FAT_CHAIN and ALLOC_FAT_CHAIN modes with
proper boundary checking.

Signed-off-by: Chi Zhiling <chizhiling@xxxxxxxxxx>
---
fs/exfat/exfat_fs.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h
index 530459ab9acc..d7f88a326cf0 100644
--- a/fs/exfat/exfat_fs.h
+++ b/fs/exfat/exfat_fs.h
@@ -552,6 +552,32 @@ int exfat_read_volume_label(struct super_block *sb,
int exfat_write_volume_label(struct super_block *sb,
struct exfat_uni_name *label);

+static inline int exfat_chain_advance(struct super_block *sb,
+ struct exfat_chain *chain, unsigned int step)
+{
+ if (chain->flags == ALLOC_NO_FAT_CHAIN) {
+ if (chain->size > step) {
+ chain->dir += step;
+ chain->size -= step;
+ } else if (chain->size == step) {
+ chain->dir = EXFAT_EOF_CLUSTER;
+ chain->size = 0;
+ } else {
+ return -EIO;
+ }
+ } else {
+ if (exfat_fat_walk(sb, &chain->dir, step, ALLOC_FAT_CHAIN))
+ return -EIO;
+ if (chain->size > step) {
+ chain->size -= step;
+ } else {
+ chain->size = 0;
+ WARN_ON_ONCE(chain->dir != EXFAT_EOF_CLUSTER);
+ }
+ }
+ return 0;
+}
+
/* inode.c */
extern const struct inode_operations exfat_file_inode_operations;
void exfat_sync_inode(struct inode *inode);
--
2.43.0