Skip to contentSkip to conversation

ufs: write out moved buffers past i_size

PATCH New Showta Ishizaki 1 participant 1 message Nov 25 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ufs: write out moved buffers past i_size

When ufs_new_fragments() has to move a file's tail, ufs_change_blocknr() gives the buffers in the page cache their new block numbers and marks them dirty, and leaves them to writeback. This happens inside a write that has not moved i_size yet. If writeback gets to a folio which is wholly past i_size first, block_write_full_folio() drops it and nothing is written at the new place. The folio is clean afterwards with its buffers still dirty, so later writes to it do not reach the device either, and the file shows what the device held there before. In a folio which straddles i_size the buffers past it can be skipped too.

Write such buffers out when they are moved. The folio being written stays locked until i_size has moved, so it does not need it.

I ran fsstress with eight processes on UFS2 images made on NetBSD and on FreeBSD, 16 seeds each, and compared the files before umount and after mounting again. In 5 of the 32 runs a file had changed, all of them on the image from NetBSD, with 1K fragments. With this patch none did.

---

fs/ufs/balloc.c +18 -0
index 628edfde3..b18a512c9 100644
@@ -301,6 +301,24 @@ static void ufs_change_blocknr(struct inode *inode, sector_t beg,
 			bh->b_blocknr = newb + pos;
 			clean_bdev_bh_alias(bh);
 			mark_buffer_dirty(bh);
+			/*
+			 * We are inside a write that has not moved i_size
+			 * yet.  Writeback drops a folio wholly past i_size
+			 * without writing it and leaves it clean with its
+			 * buffers dirty, so that later writes do not reach
+			 * the device either.  It may skip the buffers past
+			 * i_size of one that straddles it, too.  The folio
+			 * being written stays locked until the write has
+			 * moved i_size, so only the others need this.
+			 */
+			if (folio != locked_folio &&
+			    (loff_t)(beg + pos) << inode->i_blkbits >=
+			    i_size_read(inode)) {
+				/* A folio's buffers carry no reference. */
+				get_bh(bh);
+				sync_dirty_buffer(bh);
+				put_bh(bh);
+			}
 			++j;
 			bh = bh->b_this_page;
 		} while (bh != head);

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5