Skip to contentSkip to conversation

fat: set buffer uptodate before dirtying in vfat_update_dotdot_de()

RFC PATCH syzbot 2 participants 2 messages Sep 22 – Sep 24 VFAT/FAT/msdos filesystem

lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

During a directory rename operation (such as rename or rename-exchange), vfat_update_dotdot_de() is invoked to update the ".." directory entry to point to the new parent directory. In vfat_update_dotdot_de(), the buffer head is dirtied via mmb_mark_buffer_dirty(), and if synchronous directory operations are enabled (such as when mounted with MS_SYNCHRONOUS where IS_DIRSYNC() is true), sync_dirty_buffer() is immediately called to write the buffer to disk. If writing the buffer fails due to an I/O error on the underlying block device, the block layer completion handler clears the BH_Uptodate flag on the buffer head, and sync_dirty_buffer() returns an error.

When vfat_rename_exchange() or vfat_rename() encounters an error during the rename sequence, it jumps to an error rollback path to restore the original ".." entry by calling vfat_update_dotdot_de() again with the same buffer head. When vfat_update_dotdot_de() calls mmb_mark_buffer_dirty(), mark_buffer_dirty() checks whether the buffer is uptodate and triggers a warning because BH_Uptodate was cleared by the earlier write error:

!buffer_uptodate(bh)

WARNING: fs/buffer.c:991 at mark_buffer_dirty+0x299/0x410 fs/buffer.c:991
Call Trace:
 <TASK>
 mmb_mark_buffer_dirty+0x2c/0x1f0 fs/buffer.c:624
 vfat_update_dotdot_de+0x90/0x1c0 fs/fat/namei_vfat.c:917
 vfat_rename_exchange fs/fat/namei_vfat.c:1140 [inline]
 vfat_rename2+0xe3e/0x1c90 fs/fat/namei_vfat.c:1171
 vfs_rename+0xbd9/0xef0 fs/namei.c:6208
 filename_renameat2+0x533/0x9a0 fs/namei.c:6326
 do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
 </TASK>

The in-memory data in the buffer head remains valid because it was originally read successfully from disk and only modified in memory; the I/O failure only indicates that writing it to the media failed. Fix this by calling set_buffer_uptodate() in vfat_update_dotdot_de() prior to marking the buffer dirty. This ensures that BH_Uptodate is set before mark_buffer_dirty() is called, preventing the spurious warning during error rollback.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by Gemini:gemini-3.8-flash Gemini:gemini-3.1-pro-preview syzbot
Reported-by [email protected]

To: "OGAWA Hirofumi" <[email protected]>
To: <[email protected]>
To: "Linus Torvalds" <[email protected]>

---

fs/fat/namei_vfat.c +6 -0
index da3e89c0b..b84228e7f 100644
@@ -914,6 +914,12 @@ static int vfat_update_dotdot_de(struct inode *dir, struct inode *inode,
 				 struct msdos_dir_entry *dotdot_de)
 {
 	fat_set_start(dotdot_de, MSDOS_I(dir)->i_logstart);
+	/*
+	 * If a previous sync failed, the block layer's end-io handler clears
+	 * BH_Uptodate. Set it uptodate before marking dirty to prevent
+	 * mark_buffer_dirty() from warning during error rollback.
+	 */
+	set_buffer_uptodate(dotdot_bh);
 	mmb_mark_buffer_dirty(dotdot_bh, &MSDOS_I(inode)->i_metadata_bhs);
 	if (IS_DIRSYNC(dir))
 		return sync_dirty_buffer(dotdot_bh);

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].
#1 lore raw
Quoted 69 lines · syzbot

During a directory rename operation (such as rename or rename-exchange), vfat_update_dotdot_de() is invoked to update the ".." directory entry to point to the new parent directory. In vfat_update_dotdot_de(), the buffer head is dirtied via mmb_mark_buffer_dirty(), and if synchronous directory operations are enabled (such as when mounted with MS_SYNCHRONOUS where IS_DIRSYNC() is true), sync_dirty_buffer() is immediately called to write the buffer to disk. If writing the buffer fails due to an I/O error on the underlying block device, the block layer completion handler clears the BH_Uptodate flag on the buffer head, and sync_dirty_buffer() returns an error.

When vfat_rename_exchange() or vfat_rename() encounters an error during the rename sequence, it jumps to an error rollback path to restore the original ".." entry by calling vfat_update_dotdot_de() again with the same buffer head. When vfat_update_dotdot_de() calls mmb_mark_buffer_dirty(), mark_buffer_dirty() checks whether the buffer is uptodate and triggers a warning because BH_Uptodate was cleared by the earlier write error:

!buffer_uptodate(bh)

WARNING: fs/buffer.c:991 at mark_buffer_dirty+0x299/0x410 fs/buffer.c:991
Call Trace:
  <TASK>
  mmb_mark_buffer_dirty+0x2c/0x1f0 fs/buffer.c:624
  vfat_update_dotdot_de+0x90/0x1c0 fs/fat/namei_vfat.c:917
  vfat_rename_exchange fs/fat/namei_vfat.c:1140 [inline]
  vfat_rename2+0xe3e/0x1c90 fs/fat/namei_vfat.c:1171
  vfs_rename+0xbd9/0xef0 fs/namei.c:6208
  filename_renameat2+0x533/0x9a0 fs/namei.c:6326
  do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
  entry_SYSCALL_64_after_hwframe+0x77/0x7f
  </TASK>

The in-memory data in the buffer head remains valid because it was originally read successfully from disk and only modified in memory; the I/O failure only indicates that writing it to the media failed. Fix this by calling set_buffer_uptodate() in vfat_update_dotdot_de() prior to marking the buffer dirty. This ensures that BH_Uptodate is set before mark_buffer_dirty() is called, preventing the spurious warning during error rollback.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by Gemini:gemini-3.8-flash Gemini:gemini-3.1-pro-preview syzbot
Reported-by [email protected]

To: "OGAWA Hirofumi" <[email protected]>
To: <[email protected]>
To: "Linus Torvalds" <[email protected]>

---

fs/fat/namei_vfat.c +6 -0
index da3e89c0b..b84228e7f 100644
@@ -914,6 +914,12 @@ static int vfat_update_dotdot_de(struct inode *dir, struct inode *inode,
  				 struct msdos_dir_entry *dotdot_de)
  {
  	fat_set_start(dotdot_de, MSDOS_I(dir)->i_logstart);
+	/*
+	 * If a previous sync failed, the block layer's end-io handler clears
+	 * BH_Uptodate. Set it uptodate before marking dirty to prevent
+	 * mark_buffer_dirty() from warning during error rollback.
+	 */
+	set_buffer_uptodate(dotdot_bh);
  	mmb_mark_buffer_dirty(dotdot_bh, &MSDOS_I(inode)->i_metadata_bhs);
  	if (IS_DIRSYNC(dir))
  		return sync_dirty_buffer(dotdot_bh);
Quoted 1 line

base-commit: 93f51579e7df248780214094418f205253383cc5

In vfat_update_dotdot_de(), remove the unconditional set_buffer_uptodate(dotdot_bh). A failed synchronous write clears BH_Uptodate, and the rename rollback then warns when it dirties the same buffer. The current assignment can suppress that warning after one isolated failed write, but it claims that the whole buffer is valid after changing only two entry fields. It also races with I/O completion, which can clear the bit again. The buffer API requires serialization for this state change.

Keep the check in this shared VFAT helper. Take BH_Lock before testing buffer_uptodate(dotdot_bh). If the bit is clear after waiting for I/O, unlock and return -EIO before changing the .. entry or marking its buffer dirty. Keep the lock through fat_set_start() and mmb_mark_buffer_dirty(), then unlock before the existing sync_dirty_buffer() call. Preserve the existing buffer reference, rename cleanup, and first forward error. Do not duplicate the check in the rename rollback labels or expand this revision to the separate MSDOS path.

Update the subject so it describes validation rather than forcing the bit set. A suitable subject is fat: validate VFAT dotdot buffers before dirtying. Shorten the body to explain the failed write, the rollback warning, and how the locked state check avoids it. Remove the full stack trace and the broad claim that the held bytes are always valid after a failed write. Keep the existing Fixes, Reported-by, Closes, Link, and Gemini provenance tags.