mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] exfat: clean up new entry on add entry failure
@ 2026-07-29  2:25 Yichong Chen
  2026-07-29  9:10 ` Namjae Jeon
  0 siblings, 1 reply; 2+ messages in thread
From: Yichong Chen @ 2026-07-29  2:25 UTC (permalink / raw)
  To: linkinjeon; +Cc: sj1557.seo, yuezhang.mo, exfat, linux-kernel, Yichong Chen

exfat_add_entry() initializes a new directory entry set before writing it
with exfat_put_dentry_set().  If the write fails, mkdir/create returns an
error but a partially written entry may be left behind.

For non-zero-size directories, the failure also happens after a cluster
has been allocated for the new directory.  Clean up the new entry
best-effort, and free the newly allocated directory cluster only when the
cleanup writeback succeeds.  This avoids freeing a cluster that may still
be referenced by an on-disk entry if the cleanup fails.

Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
---
v2:
- Free the newly allocated directory cluster only after entry cleanup
  succeeds, as suggested by Namjae Jeon.

 fs/exfat/namei.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c
index 038bc6fed681..a4dc83b5949c 100644
--- a/fs/exfat/namei.c
+++ b/fs/exfat/namei.c
@@ -471,6 +471,7 @@ static int exfat_add_entry(struct inode *inode, const char *path,
 	struct exfat_entry_set_cache es;
 	int clu_size = 0;
 	unsigned int start_clu = EXFAT_FREE_CLUSTER;
+	bool dir_allocated = false;
 
 	ret = exfat_resolve_path(inode, path, &uniname);
 	if (ret)
@@ -497,6 +498,7 @@ static int exfat_add_entry(struct inode *inode, const char *path,
 		}
 		start_clu = clu.dir;
 		clu_size = sbi->cluster_size;
+		dir_allocated = true;
 	}
 
 	/* update the directory entry */
@@ -507,8 +509,21 @@ static int exfat_add_entry(struct inode *inode, const char *path,
 	exfat_init_ext_entry(&es, num_entries, &uniname, NULL, 0);
 
 	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
-	if (ret)
+	if (ret) {
+		int cleanup_ret;
+
+		cleanup_ret = exfat_get_dentry_set(&es, sb, &info->dir,
+						   dentry, ES_ALL_ENTRIES);
+		if (!cleanup_ret) {
+			exfat_remove_entries(inode, &es, ES_IDX_FILE, false);
+			cleanup_ret = exfat_put_dentry_set(&es,
+							   IS_DIRSYNC(inode));
+		}
+
+		if (!cleanup_ret && dir_allocated)
+			exfat_free_cluster(inode, &clu);
 		goto out;
+	}
 
 	info->entry = dentry;
 	info->flags = ALLOC_NO_FAT_CHAIN;
-- 
2.51.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] exfat: clean up new entry on add entry failure
  2026-07-29  2:25 [PATCH v2] exfat: clean up new entry on add entry failure Yichong Chen
@ 2026-07-29  9:10 ` Namjae Jeon
  0 siblings, 0 replies; 2+ messages in thread
From: Namjae Jeon @ 2026-07-29  9:10 UTC (permalink / raw)
  To: Yichong Chen; +Cc: sj1557.seo, yuezhang.mo, exfat, linux-kernel

On Wed, Jul 29, 2026 at 11:26 AM Yichong Chen <chenyichong@uniontech.com> wrote:
>
> exfat_add_entry() initializes a new directory entry set before writing it
> with exfat_put_dentry_set().  If the write fails, mkdir/create returns an
> error but a partially written entry may be left behind.
>
> For non-zero-size directories, the failure also happens after a cluster
> has been allocated for the new directory.  Clean up the new entry
> best-effort, and free the newly allocated directory cluster only when the
> cleanup writeback succeeds.  This avoids freeing a cluster that may still
> be referenced by an on-disk entry if the cleanup fails.
>
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
Applied it to #dev.
Thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-29  9:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-29  2:25 [PATCH v2] exfat: clean up new entry on add entry failure Yichong Chen
2026-07-29  9:10 ` Namjae Jeon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®