nilfs_write_end() adds only the clean buffers of the copied range to the count of dirty blocks. After a short copy, however, block_write_end() also zero-fills and dirties the newly allocated buffers of the part that was not copied, which is the whole range when the folio is not uptodate. The write is then retried, finds these buffers dirty, and does not count them either. This skews the free space calculation: nilfs_near_disk_full() does not stop the writer in time, and the reserved segments are consumed. This has been the case since nilfs2 was merged, but it used to take an unusual short copy, for example from a source buffer that runs into an unreadable page. Commit 665575cff098 ("filemap: move prefaulting out of hot write path") made it much easier to hit: generic_perform_write() now faults in the source buffer only after a copy into the page cache has made no progress, so a write from memory that is not present yet first calls ->write_end() with copied == 0 for each page. 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 #include #include #include #include #include #include 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 change, 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: 05fe58fdc10d ("nilfs2: inode operations") Cc: stable@vger.kernel.org Signed-off-by: Hengyu Liang --- 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. v2: as Ryusuke asked, the Fixes tag now names the original implementation, the stable Cc has no version, and the message says the bug is old and 665575cff098 made it easier to hit. No code change. v1: https://lore.kernel.org/linux-nilfs/20261122130247.2069007-1-hengyul@cs.unc.edu/ Tested on nilfs2 for-next with KASAN and lockdep, also with the short copy from Ryusuke's reply (1 KiB blocks, source running into a PROT_NONE page): the dirty block count is right, and a nearly full volume returns ENOSPC instead of accepting more than the free space. 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