Skip to content

Linux 7.0: fuse: write dirty folios back before a truncate without the writeback cache - #288

Merged
hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-resolute-7.0.0-14.14from
achhenderson:fix-redfs-7.0-wt-truncate-mmap
Nov 12, 2026
Merged

hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-resolute-7.0.0-14.14from
achhenderson:fix-redfs-7.0-wt-truncate-mmap

Conversation

@achhenderson

Copy link
Copy Markdown
Collaborator

Summary

Without the writeback cache, an ftruncate() to a smaller size is lost when the file has pages that were written through a shared mapping and not yet written back. The call returns 0, but the file keeps its old size and contents on every client:

fd = open(f, O_RDWR | O_CREAT | O_TRUNC);  pwrite(fd, "X" x 5121, 0);
m = mmap(fd, 5121, MAP_SHARED);            memset(m + 2047, 'Z', 3071);
ftruncate(fd, 0);                          /* returns 0 */
close(fd);                                 /* size is 5121 again */

The server's truncate invalidates this client's page cache from inside its SETATTR handler. fuse_reverse_inval_inode() launders the folios that are still dirty, and the server applies those writes after the truncate. fuse_wb_do_setattr() already writes back before the SETATTR for this reason; the path without the writeback cache did not, and the notify arrives there as well.

The change: fuse_wt_do_setattr() writes back and waits before a truncating SETATTR, in the block that already does so for a staged splice copy. It is one condition in fs/fuse/dir.c. open(O_TRUNC) is not affected and not changed.

Known gap, also in the commit message: a store through the mapping that races the truncate from another thread can still dirty a folio between the write-back and the reply.

This is not specific to 7.0. The 6.8 and 6.17 modules show the same wrong results, and the 6.17, 6.12 and 6.8 branches need the same change. Ports will follow once this one is reviewed.

Testing

Ubuntu 7.0.0-42-generic with redfs.ko built from this branch, RED release/2.5, mounts without the writeback cache:

  • A reproducer that runs 23 variants of the sequence above, 14 of them with dirty mapped folios at the truncate: 13, 10 and 14 wrong results in three runs before the change, none in eight runs after.
  • generic/029 fails before and passes after; generic/030 passes.
  • With the writeback cache nothing changes: no wrong results before or after.
  • The xfstests CI pass list (200 tests) without the writeback cache: no failure that is due to the change. What still fails also fails on 6.8 or failed in earlier runs without it.

… cache

Without the writeback cache, ftruncate() to a smaller size is lost when
the file has pages that were written through a shared mapping and not yet
written back. The call returns 0, but the file keeps its old size and
contents, including the mapped stores, on every client:

    fd = open(f, O_RDWR | O_CREAT | O_TRUNC);  pwrite(fd, "X" x 5121, 0);
    m = mmap(fd, 5121, MAP_SHARED);            memset(m + 2047, 'Z', 3071);
    ftruncate(fd, 0);                          /* returns 0 */
    close(fd);                                 /* size is 5121 again */

Truncating down and up again shows the old bytes where zeros belong, which
is how generic/029 fails. With msync() or fsync() before the truncate the
result is correct.

The server's truncate invalidates this client's page cache from inside its
SETATTR handler. fuse_reverse_inval_inode() launders the folios that are
still dirty, with i_size not yet changed, and the server applies those
writes after the truncate. A trace of redfs on 7.0.0-42 shows the two
WRITEs between SETATTR's send and its reply:

    SETATTR sent
      rfs_inval_0: fuse_reverse_inval_inode -> fuse_launder_folio
        WRITE pos 0 len 4096, i_size 5121
        WRITE pos 4096 len 1025, i_size 5121
    SETATTR done, error 0

"fuse: drop the nowrite freeze from setattr" removed the freeze that used
to hold such writes back until the reply, because the notify waited on
them. "fuse: flush dirty folios before a truncate under DLM" then made the
writeback-cache path write back before the SETATTR, on the assumption that
only that configuration is invalidated on truncate. The path without the
writeback cache got neither, and the notify arrives there as well.

Write back and wait in fuse_wt_do_setattr() before a truncating SETATTR,
together with the staged splice copy that is already written back at this
point. Without the writeback cache only a shared mapping or a staged
splice copy leaves dirty folios, so the call does nothing for a file that
is not mapped. As for the splice case, a writeback error is left in the
mapping for fuse_flush_common() and does not fail the truncate.

open(O_TRUNC) is not changed: the O_TRUNC variants of the reproducer pass
without this.

A store through the mapping that races the truncate from another thread
can still dirty a folio after the write-back and before the reply, and
that folio is laundered as before. Closing that needs page_mkwrite to be
held off across the request, as the DAX case does with the invalidate
lock.

Tested with RED release/2.5 at 497d797afb on Ubuntu 7.0.0-42-generic,
without the writeback cache. trunc_mmap.py runs 23 variants of the
sequence, 14 of which have dirty mapped folios at the truncate: 13, 10 and
14 wrong results in three runs before, none in eight runs after.
generic/029 fails before and passes after; generic/030 passes. With the
writeback cache nothing changes (no wrong results before or after).

Signed-off-by: Allison Henderson <allison.henderson@ddn.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants