nilfs2: count new blocks that a short copy leaves dirty
Subsystems (2)
PATCH v2 1/1 nilfs2: count new blocks that a short copy leaves dirty
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 <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/fAfter 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/fCount the buffers that block_write_end() is going to dirty, including the zero-filled new ones, before calling generic_write_end().
---
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/[email protected]/
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(-)
fs/nilfs2/inode.c +1 -2
@@ -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);
fs/nilfs2/page.c +41 -0
47 lines not on this page · raw message
fs/nilfs2/page.h +4 -0
@@ -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
Signature · 1 line
2.53.0
Quoted 91 lines · Hengyu Liang
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'sfsync()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/f256
umount /mnt
mount -t nilfs2/dev/vda/mnt
wc -c/mnt/fBefore commit
665575cff098("filemap: move prefaulting out of hot write path"), the result is:write: 67518464 fsync: ok 67518464 /mnt/fAfter that commit, the result is:
write: 268435456
fsync: No space left on device
wc:/mnt/f: No such file or directoryWith this change, the result is:
write: 70217728 fsync: ok 70217728 /mnt/fCount the buffers that
block_write_end()is going to dirty, including the zero-filled new ones, before callinggeneric_write_end().Fixes 05fe58fdc10d ("nilfs2: inode operations")Signed-off-by Hengyu Liang <[email protected]>---
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
665575cff098made it easier to hit. No code change.
v1: https://lore.kernel.org/linux-nilfs/[email protected]/
Quoted 4 lines
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_NONEpage): the dirty block count is right, and a nearly full volume returns ENOSPC instead of accepting more than the free space.
Hi Viacheslav,
This v2 patch looks good to me. Could you please apply it directly to your tree?
Thanks,
Ryusuke Konishi
Quoted 92 lines
fs/nilfs2/inode.c | 3 +-- fs/nilfs2/page.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/nilfs2/page.h | 4 ++++ 3 files changed, 46 insertions(+), 2 deletions(-)fs/nilfs2/inode.c +1
-2@@ -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);fs/nilfs2/page.c +41
-0@@ -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: * fs/nilfs2/page.h +4
-0@@ -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
Quoted 238 lines · Ryusuke Konishi
Quoted 115 lines · Hengyu Liang
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. Commit665575cff098("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'sfsync()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_tlen =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/f256
umount /mnt
mount -t nilfs2/dev/vda/mnt
wc -c/mnt/fBefore commit
665575cff098("filemap: move prefaulting out of hot write
path"), the result is:write: 67518464
fsync: ok
67518464/mnt/fAfter that commit, the result is:
write: 268435456
fsync: No space left on device
wc:/mnt/f: No such file or directoryWith this change, the result is:
write: 70217728
fsync: ok
70217728/mnt/fCount the buffers that
block_write_end()is going to dirty, including
the zero-filled new ones, before callinggeneric_write_end().Fixes 05fe58fdc10d ("nilfs2: inode operations")Signed-off-by Hengyu Liang <[email protected]>---
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 and665575cff098made it easier to hit. No code change. v1:
https://lore.kernel.org/linux-nilfs/[email protected]/Quoted 6 lines
Tested on nilfs2 for-next with KASAN and lockdep, also with the short
copy from Ryusuke's reply (1 KiB blocks, source running into aPROT_NONEpage): the dirty block count is right, and a nearly full volume returns ENOSPC instead of accepting more than the free space.Hi Viacheslav,
This v2 patch looks good to me. Could you please apply it directly to
your tree?Acked-by Ryusuke Konishi <[email protected]>Thanks,
Ryusuke KonishiQuoted 104 lines
fs/nilfs2/inode.c | 3 +--
fs/nilfs2/page.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/nilfs2/page.h | 4 ++++3 files changed, 46 insertions(+), 2 deletions(-)fs/nilfs2/inode.c +1
-2@@ -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);fs/nilfs2/page.c +2
-0@@ -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 thatblock_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)fs/nilfs2/page.c +17
-0+{ + 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;fs/nilfs2/page.c +2
-0+} +/*
* NILFS2 needsclear_page_dirty()in the following two cases:
*fs/nilfs2/page.h +0
-0@@ -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 intnilfs_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 longnilfs_find_uncommitted_extent(struct inode *inode, sector_t start_blk, sector_t *blkoff);
Quoted 4 lines
Quoted 4 lines
base-commit:
8fef1a9dec7bf58a29bc8a7ee6909ab629401cd3--
2.53.0
Applied.
Thanks,
Slava.