Skip to content

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

Merged
hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
achhenderson:fix-redfs-6.17-wt-truncate-mmap
Nov 18, 2026
Merged

hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
achhenderson:fix-redfs-6.17-wt-truncate-mmap

Conversation

@achhenderson

Copy link
Copy Markdown
Collaborator

Summary

6.17 port of #288 (407600f1ceec on the 7.0 branch).

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. The writeback cache with DLM already writes back before the SETATTR for this reason; the path without the writeback cache did not.

Adaptation: fuse_do_setattr() is not split into a write-through and a writeback function on this branch. The condition is added to its existing write-back of a staged splice copy without the writeback cache, which is the block the 7.0 commit changes in fuse_wt_do_setattr().

The known gap of the 7.0 commit applies here as well: a store through the mapping that races the truncate from another thread can still dirty a folio between the write-back and the reply.

Testing

Ubuntu 6.17.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: 12, 14 and 14 wrong results in three runs before the change, none in eight runs after.
  • generic/029 fails before and passes after (2 of 2 runs); generic/030 passes.
  • With the writeback cache nothing changes: no wrong results before or after.

… 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>
(cherry picked from commit 407600f)
[ahenderson: fuse_do_setattr() is not split into fuse_wt_do_setattr() and
  fuse_wb_do_setattr() here; the condition is added to its write-back of a
  staged splice copy without the writeback cache]

@hbirth hbirth left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a backport of an approved change

@hbirth
hbirth merged commit 332f76b into DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1 Nov 18, 2026
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