Skip to contentSkip to conversation

exfat: preserve the next empty-entry hint after allocation

PATCH In review Yang Wen 2 participants 2 messages Jun 13

Subsystems (2)
lore
Share & tools
lore

Yang Wen

author
#0 lore raw

PATCH v2 1/1 exfat: preserve the next empty-entry hint after allocation

Advance and preserve the empty-entry hint when only part of a contiguous empty range is consumed.

This allows the next file creation to reuse the remaining range without scanning the directory again.

---
 fs/exfat/namei.c | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
fs/exfat/namei.c +37 -2
index b7d5e44ad38e..403e47bde9d6 100644
@@ -276,6 +276,33 @@ static int exfat_check_max_dentries(struct inode *inode)
 	return 0;
 }
 
+static void exfat_cache_next_empty_entry(struct super_block *sb,
+		struct exfat_inode_info *ei, struct exfat_hint_femp *hint_femp,
+		int dentry, int num_entries)
+{
+	int next, cur_clu, next_clu;
+	struct exfat_sb_info *sbi = EXFAT_SB(sb);
+	struct exfat_hint_femp next_hint;
+
+	if (hint_femp->eidx == EXFAT_HINT_NONE ||
+	    hint_femp->eidx != dentry ||
+	    hint_femp->count <= num_entries)
+		return;
+
+	next = dentry + num_entries;
+	next_hint = *hint_femp;
+	next_hint.eidx = next;
+	next_hint.count -= num_entries;
+
+	cur_clu = dentry / sbi->dentries_per_clu;
+	next_clu = next / sbi->dentries_per_clu;
+	if (next_clu > cur_clu &&
+	    exfat_chain_advance(sb, &next_hint.cur, next_clu - cur_clu))
+		return;
+
+	ei->hint_femp = next_hint;
+}
+
 /*
  * Find an empty directory entry set.
  *
@@ -317,8 +344,13 @@ int exfat_find_empty_entry(struct inode *inode,
 			exfat_bytes_to_cluster(sbi, i_size_read(inode)),
 			ei->flags);
 
-	while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
-					num_entries, es)) < 0) {
+	for (;;) {
+		prev_hint_femp = hint_femp;
+		dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
+				num_entries, es);
+		if (dentry >= 0)
+			break;
+
 		if (dentry != -ENOSPC)
 			return dentry;
 
@@ -385,6 +417,9 @@ int exfat_find_empty_entry(struct inode *inode,
 		inode->i_blocks += sbi->cluster_size >> 9;
 	}
 
+	exfat_cache_next_empty_entry(sb, ei, &prev_hint_femp, dentry,
+			num_entries);
+
 	p_dir->dir = exfat_sector_to_cluster(sbi, es->bh[0]->b_blocknr);
 	p_dir->size -= dentry / sbi->dentries_per_clu;
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · Yang Wen

Advance and preserve the empty-entry hint when only part of a contiguous empty range is consumed.

This allows the next file creation to reuse the remaining range without scanning the directory again.

a build error happens as shown below, but I have directly fixed it and applied it to #dev.
Thanks!

fs/exfat/namei.c: In function ‘exfat_find_empty_entry’:
fs/exfat/namei.c:348:17: error: ‘prev_hint_femp’ undeclared