aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2026-11-11 21:22:00 +0100
committerJakob Unterwurzacher2026-11-11 21:22:00 +0100
commit851188164274de96cd581c3da1b5e847a42e2948 (patch)
treec1d33d2b5086e9f5bfad8abddb5c6261068ff029
parent0363a138f8125604d75d9ca84be4092482b622df (diff)
fusefrontend: -sharedstorage: always check for file holes on write
Write() skips writePadHole() when a write directly follows the previous one (isConsecutiveWrite), because such a write cannot create a hole. That decision is based on process-local state. With -sharedstorage, another mount may have shrunk the file in between, so the old partial last block was never padded and ended up as a short block in the middle of the file, making it unreadable (EIO). Disable the optimization with -sharedstorage. Add TestConsecutiveWriteAfterRemoteTruncate as a regression test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--internal/fusefrontend/file.go4
-rw-r--r--tests/cluster/cluster_test.go33
2 files changed, 36 insertions, 1 deletions
diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go
index fb97b9a..feb40b5 100644
--- a/internal/fusefrontend/file.go
+++ b/internal/fusefrontend/file.go
@@ -431,7 +431,9 @@ func (f *File) Write(ctx context.Context, data []byte, off int64) (uint32, sysca
// If the write creates a file hole, we have to zero-pad the last block.
// But if the write directly follows an earlier write, it cannot create a
// hole, and we can save one Stat() call.
- if !f.isConsecutiveWrite(off) {
+ // With -sharedstorage, another mount may have truncated the file since our
+ // last write, so we have to check every time.
+ if f.rootNode.args.SharedStorage || !f.isConsecutiveWrite(off) {
errno := f.writePadHole(off)
if errno != 0 {
return 0, errno
diff --git a/tests/cluster/cluster_test.go b/tests/cluster/cluster_test.go
index f08d3d0..813834e 100644
--- a/tests/cluster/cluster_test.go
+++ b/tests/cluster/cluster_test.go
@@ -288,3 +288,36 @@ func TestGrowAfterTruncateKeepsHeader(t *testing.T) {
t.Errorf("wrong content: got %q, want %q", buf, want)
}
}
+
+// Write() skips hole padding when a write directly follows the previous one,
+// based on process-local state. With -sharedstorage, another mount may have
+// shrunk the file in between, leaving the old partial last block unpadded
+// in the middle of the file, making it unreadable.
+func TestConsecutiveWriteAfterRemoteTruncate(t *testing.T) {
+ mnt1, mnt2 := mountTwice(t)
+ f1, err := os.Create(mnt1 + "/foo")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f1.Close()
+ if _, err := f1.WriteAt(bytes.Repeat([]byte("a"), 8192), 0); err != nil {
+ t.Fatal(err)
+ }
+ // mount2 shrinks the file so that the second block is partial
+ if err := os.Truncate(mnt2+"/foo", 5000); err != nil {
+ t.Fatal(err)
+ }
+ // mount1 continues writing where it left off, creating a hole
+ if _, err := f1.WriteAt(bytes.Repeat([]byte("b"), 4096), 8192); err != nil {
+ t.Fatal(err)
+ }
+ want := append(bytes.Repeat([]byte("a"), 5000), make([]byte, 8192-5000)...)
+ want = append(want, bytes.Repeat([]byte("b"), 4096)...)
+ have, err := os.ReadFile(mnt2 + "/foo")
+ if err != nil {
+ t.Fatalf("ReadFile: %v", err)
+ }
+ if !bytes.Equal(have, want) {
+ t.Errorf("wrong content: len(have)=%d, len(want)=%d", len(have), len(want))
+ }
+}