Re: [PATCH] nilfs2: count new blocks that a short copy leaves dirty

From: Ryusuke Konishi

Date: Mon Nov 23 2026 - 12:05:20 EST


On Sun, Nov 22, 2026 at 10:03 PM Hengyu Liang wrote:
>
> Commit 665575cff098 ("filemap: move prefaulting out of hot write path")
> made generic_perform_write() fault in the source buffer only after a
> copy into the page cache has made no progress. A write from memory
> that is not present yet now first calls ->write_end() with copied == 0,
> and copies again after the memory has been faulted in.
>
> However, nilfs_write_end() adds only the clean buffers of the copied
> range to the count of dirty blocks, while block_write_end() zero-fills
> and dirties the newly allocated buffers of the rest of the range. The
> retry finds these buffers dirty, so the new blocks are never counted
> and nilfs_near_disk_full() does not stop the writer. As of now, a
> write from a fresh anonymous mapping is accepted far beyond the free
> space, fsync() fails with ENOSPC, and the file is gone after a remount,
> together with the other changes made since the last log was written.
> On a volume shared with another user, that user's fsync() fails with
> ENOSPC too, and the files and directories that user created after the
> write are lost the same way.
>
> The issue can be reproduced on a 160 MiB disk with this program:
>
> #include <errno.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/mman.h>
> #include <unistd.h>
>
> int main(int argc, char *argv[])
> {
> size_t len = atol(argv[2]) << 20;
> /* fresh anonymous memory: none of its pages is present yet */
> char *buf = mmap(NULL, len, PROT_READ,
> MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
>
> printf("write: %zd\n", write(fd, buf, len));
> printf("fsync: %s\n", fsync(fd) ? strerror(errno) : "ok");
> return 0;
> }
>
> and these commands:
>
> mkfs.nilfs2 -q /dev/vda
> mount -t nilfs2 /dev/vda /mnt
> ./cold /mnt/f 256
> umount /mnt
> mount -t nilfs2 /dev/vda /mnt
> wc -c /mnt/f
>
> Before commit 665575cff098 ("filemap: move prefaulting out of hot write
> path"), the result is:
>
> write: 67518464
> fsync: ok
> 67518464 /mnt/f
>
> After that commit, the result is:
>
> write: 268435456
> fsync: No space left on device
> wc: /mnt/f: No such file or directory
>
> With this patch, the result is:
>
> write: 70217728
> fsync: ok
> 70217728 /mnt/f
>
> Count the buffers that block_write_end() is going to dirty, including
> the zero-filled new ones, before calling generic_write_end().
>
> Fixes: 665575cff098 ("filemap: move prefaulting out of hot write path")
> Cc: stable@xxxxxxxxxxxxxxx # v6.14
> Signed-off-by: Hengyu Liang <hengyul@xxxxxxxxxx>
> ---
> I found this while comparing the behavior of different kernel versions.
> I used an LLM to analyze the problem and to draft the patch and this
> message, and I analyzed the result myself.
>
> Tested on nilfs2 for-next (8fef1a9dec7b) with KASAN and lockdep: after
> single writes (new, appended and overwritten blocks; cold, warm and
> unaligned sources; 4 KiB and 1 KiB blocks) dirty_data_blocks_count in
> sysfs grows by the number of blocks written, as on v6.8, and a nearly
> full volume stops such a write at the same size as on v6.8.
>
> A write into page cache pages that were read from a hole is not counted
> either, on v6.8 as well, because __block_write_begin_int() dirties the
> new buffers of an uptodate folio itself; this patch does not change that.

Hi Hengyu Liang,

Thank you for reporting the issue and proposing the patch.

Through reviewing your patch and testing with the reproducer, I
confirmed both the dirty block counting bug in nilfs_write_end() on
short copies and that your patch properly resolves it.

However, the short-copy handling in generic_write_end() and
block_write_end() was already present when nilfs2 was originally
introduced, and it appears that this bug in nilfs_write_end() -- which
skews the free space calculation and causes excessive consumption of
reserved segments -- has been latent since then.
I verified this locally on an older kernel using a reproducer that
intentionally triggers short copies via pwrite() from a buffer
straddling a readable page and a PROT_NONE page.

Could you please update the Fixes tag to point to the original nilfs2
implementation commit rather than 665575cff098, remove the kernel
version constraint from the Cc: stable tag, and revise the commit log
to state that the dirty-block counting bug has existed since the
original implementation, while commit 665575cff098 made it much easier
to reproduce?
Once updated, I will forward it to Viacheslav.

Thanks,
Ryusuke Konishi

>
> fs/nilfs2/inode.c | 3 +--
> fs/nilfs2/page.c | 41 +++++++++++++++++++++++++++++++++++++++++
> fs/nilfs2/page.h | 4 ++++
> 3 files changed, 46 insertions(+), 2 deletions(-)
>
> diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
> index b217eee57c36..dd0bed24972a 100644
> --- a/fs/nilfs2/inode.c
> +++ b/fs/nilfs2/inode.c
> @@ -249,8 +249,7 @@ static int nilfs_write_end(const struct kiocb *iocb,
> unsigned int nr_dirty;
> int err;
>
> - nr_dirty = nilfs_page_count_clean_buffers(folio, start,
> - start + copied);
> + nr_dirty = nilfs_page_count_buffers_to_dirty(folio, start, len, copied);
> copied = generic_write_end(iocb, mapping, pos, len, copied, folio,
> fsdata);
> nilfs_set_file_dirty(inode, nr_dirty);
> diff --git a/fs/nilfs2/page.c b/fs/nilfs2/page.c
> index 857926da9f21..0dec7b5b27c4 100644
> --- a/fs/nilfs2/page.c
> +++ b/fs/nilfs2/page.c
> @@ -481,6 +481,47 @@ unsigned int nilfs_page_count_clean_buffers(struct folio *folio,
> return nc;
> }
>
> +/**
> + * nilfs_page_count_buffers_to_dirty - count buffers a write will dirty
> + * @folio: folio being written to
> + * @from: offset of the write in @folio
> + * @len: number of bytes to be written
> + * @copied: number of bytes copied into @folio
> + *
> + * Count the clean buffers of @folio that block_write_end() is going to mark
> + * dirty: those overlapping the copied range and, after a short copy, the
> + * newly allocated ones in the rest of the range, which it zero-fills. As
> + * there, a short copy into a folio that is not uptodate counts as a
> + * zero-length write.
> + *
> + * Return: Number of buffers that the write changes from clean to dirty.
> + */
> +unsigned int nilfs_page_count_buffers_to_dirty(struct folio *folio,
> + unsigned int from,
> + unsigned int len,
> + unsigned int copied)
> +{
> + unsigned int to = from + len, end = from + copied;
> + unsigned int block_start, block_end;
> + struct buffer_head *bh, *head;
> + unsigned int nc = 0;
> +
> + if (copied < len && !folio_test_uptodate(folio))
> + end = from;
> +
> + for (bh = head = folio_buffers(folio), block_start = 0;
> + bh != head || !block_start;
> + block_start = block_end, bh = bh->b_this_page) {
> + block_end = block_start + bh->b_size;
> + if (buffer_dirty(bh))
> + continue;
> + if ((block_end > from && block_start < end) ||
> + (buffer_new(bh) && block_end > end && block_start < to))
> + nc++;
> + }
> + return nc;
> +}
> +
> /*
> * NILFS2 needs clear_page_dirty() in the following two cases:
> *
> diff --git a/fs/nilfs2/page.h b/fs/nilfs2/page.h
> index c3ba3468af5c..4ccd740966ec 100644
> --- a/fs/nilfs2/page.h
> +++ b/fs/nilfs2/page.h
> @@ -45,6 +45,10 @@ void nilfs_clear_folio_dirty(struct folio *folio, bool force);
> void nilfs_clear_dirty_pages(struct address_space *mapping, bool force);
> unsigned int nilfs_page_count_clean_buffers(struct folio *folio,
> unsigned int from, unsigned int to);
> +unsigned int nilfs_page_count_buffers_to_dirty(struct folio *folio,
> + unsigned int from,
> + unsigned int len,
> + unsigned int copied);
> unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
> sector_t start_blk,
> sector_t *blkoff);
>
> base-commit: 8fef1a9dec7bf58a29bc8a7ee6909ab629401cd3
> --
> 2.53.0
>