From 0363a138f8125604d75d9ca84be4092482b622df Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 11 Nov 2026 21:21:13 +0100 Subject: fusefrontend: -sharedstorage: don't replace the header when growing a truncated file With -sharedstorage, truncate to zero keeps the file header, because other mounts may have the file ID cached. But a following truncate that grows the file to a block-aligned size went through truncateGrowFile, which saw plaintext size 0 and unconditionally called createHeader(). This replaced the file ID (without taking the header lock), and everything the other mount wrote afterwards with its cached ID was unreadable. Factor the "read header or create it if the file is empty" logic out of doWrite into loadOrCreateFileID() and use it in truncateGrowFile with -sharedstorage. Add TestGrowAfterTruncateKeepsHeader as a regression test. Co-Authored-By: Claude Opus 5.5 --- internal/fusefrontend/file.go | 67 +++++++++++++++---------- internal/fusefrontend/file_allocate_truncate.go | 11 +++- tests/cluster/cluster_test.go | 62 +++++++++++++++++++++++ 3 files changed, 111 insertions(+), 29 deletions(-) diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go index 353029c..fb97b9a 100644 --- a/internal/fusefrontend/file.go +++ b/internal/fusefrontend/file.go @@ -284,34 +284,47 @@ func (f *File) Read(ctx context.Context, buf []byte, off int64) (resultData fuse // and by Truncate() to rewrite the last file block. // // Empty writes do nothing and are allowed. -func (f *File) doWrite(data []byte, off int64) (uint32, syscall.Errno) { - fileWasEmpty := false - // The caller has exclusively locked ContentLock, which blocks all other - // readers and writers. No need to take IDLock. - // - // If the file ID is not cached, read it from disk - if f.fileTableEntry.ID == nil { - if err := f.LockSharedStorage(unix.F_WRLCK, 0, contentenc.HeaderLen); err != nil { - tlog.Warn.Printf("ino%d: doWrite: LockSharedStorage(F_WRLCK, %d, %d) failed: %v", f.qIno.Ino, 0, contentenc.HeaderLen, err) - return 0, fs.ToErrno(err) - } - defer f.UnlockSharedStorage(0, contentenc.HeaderLen) +// loadOrCreateFileID makes sure f.fileTableEntry.ID is set. If it is not +// cached, the file ID is read from the on-disk header, or, if the file is +// empty, a new header is created. Returns created=true in the latter case. +// +// With -sharedstorage, this happens under a byte-range lock on the header, +// so concurrent mounts agree on a single header. +// +// The caller must hold ContentLock exclusively, which blocks all other +// readers and writers. No need to take IDLock. +func (f *File) loadOrCreateFileID() (created bool, errno syscall.Errno) { + if f.fileTableEntry.ID != nil { + return false, 0 + } + if err := f.LockSharedStorage(unix.F_WRLCK, 0, contentenc.HeaderLen); err != nil { + tlog.Warn.Printf("ino%d: loadOrCreateFileID: LockSharedStorage(F_WRLCK, %d, %d) failed: %v", f.qIno.Ino, 0, contentenc.HeaderLen, err) + return false, fs.ToErrno(err) + } + defer f.UnlockSharedStorage(0, contentenc.HeaderLen) + + fileID, err := f.readFileID() + // Write a new file header if the file is empty + if err == io.EOF { + fileID, err = f.createHeader() + created = true + } else if err != nil { + // Other errors mean readFileID() found a corrupt header + tlog.Warn.Printf("loadOrCreateFileID %d: corrupt header: %v", f.qIno.Ino, err) + return false, syscall.EIO + } + if err != nil { + return false, fs.ToErrno(err) + } + f.fileTableEntry.ID = fileID + return created, 0 +} - var err error - fileID, err := f.readFileID() - // Write a new file header if the file is empty - if err == io.EOF { - fileID, err = f.createHeader() - fileWasEmpty = true - } else if err != nil { - // Other errors mean readFileID() found a corrupt header - tlog.Warn.Printf("doWrite %d: corrupt header: %v", f.qIno.Ino, err) - return 0, syscall.EIO - } - if err != nil { - return 0, fs.ToErrno(err) - } - f.fileTableEntry.ID = fileID +func (f *File) doWrite(data []byte, off int64) (uint32, syscall.Errno) { + // If the file ID is not cached, read it from disk or create a new header + fileWasEmpty, errno := f.loadOrCreateFileID() + if errno != 0 { + return 0, errno } // Handle payload data dataBuf := bytes.NewBuffer(data) diff --git a/internal/fusefrontend/file_allocate_truncate.go b/internal/fusefrontend/file_allocate_truncate.go index f4a078c..b7b2651 100644 --- a/internal/fusefrontend/file_allocate_truncate.go +++ b/internal/fusefrontend/file_allocate_truncate.go @@ -241,8 +241,15 @@ func (f *File) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) syscall.Er // The new size is block-aligned. In this case we can do everything ourselves // and avoid the call to doWrite. if newPlainSz%f.rootNode.contentEnc.PlainBS() == 0 { - // The file was empty, so it did not have a header. Create one. - if oldPlainSz == 0 { + if oldPlainSz == 0 && f.rootNode.args.SharedStorage { + // With -sharedstorage, truncate to zero keeps the header, and other + // mounts may have the file ID cached. Reuse the existing header if + // there is one, and only create one if the file is really empty. + if _, errno := f.loadOrCreateFileID(); errno != 0 { + return errno + } + } else if oldPlainSz == 0 { + // The file was empty, so it did not have a header. Create one. id, err := f.createHeader() if err != nil { return fs.ToErrno(err) diff --git a/tests/cluster/cluster_test.go b/tests/cluster/cluster_test.go index 70a2a02..f08d3d0 100644 --- a/tests/cluster/cluster_test.go +++ b/tests/cluster/cluster_test.go @@ -226,3 +226,65 @@ func TestOpenTruncate(t *testing.T) { go writerThread(mnt2 + "/foo") wg.Wait() } + +// mountTwice creates a new filesystem and mounts it twice with -sharedstorage. +func mountTwice(t *testing.T) (mnt1 string, mnt2 string) { + cDir := test_helpers.InitFS(t) + mnt1 = cDir + ".mnt1" + mnt2 = cDir + ".mnt2" + test_helpers.MountOrFatal(t, cDir, mnt1, "-extpass=echo test", "-wpanic=0", "-sharedstorage") + t.Cleanup(func() { test_helpers.UnmountPanic(mnt1) }) + test_helpers.MountOrFatal(t, cDir, mnt2, "-extpass=echo test", "-wpanic=0", "-sharedstorage") + t.Cleanup(func() { test_helpers.UnmountPanic(mnt2) }) + return +} + +// With -sharedstorage, truncate to zero keeps the file header because other +// mounts may have the file ID cached. Growing the file to a block-aligned size +// afterwards used to create a new header, replacing the file ID the other +// mount was still using, corrupting everything it wrote afterwards. +func TestGrowAfterTruncateKeepsHeader(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([]byte("xxxxxxxx"), 0); err != nil { + t.Fatal(err) + } + f2, err := os.OpenFile(mnt2+"/foo", os.O_RDWR, 0) + if err != nil { + t.Fatal(err) + } + defer f2.Close() + // Make mount2 cache the file ID + if _, err := f2.ReadAt(make([]byte, 8), 0); err != nil { + t.Fatal(err) + } + // mount1: truncate to zero, then grow to exactly one block + if err := f1.Truncate(0); err != nil { + t.Fatal(err) + } + if err := f1.Truncate(4096); err != nil { + t.Fatal(err) + } + // mount2 writes using its cached file ID + want := []byte("hello") + if _, err := f2.WriteAt(want, 0); err != nil { + t.Fatal(err) + } + // A fresh open on mount1 (reads the file ID from disk) must see the data + f3, err := os.Open(mnt1 + "/foo") + if err != nil { + t.Fatal(err) + } + defer f3.Close() + buf := make([]byte, len(want)) + if _, err := f3.ReadAt(buf, 0); err != nil { + t.Fatalf("ReadAt: %v", err) + } + if !bytes.Equal(buf, want) { + t.Errorf("wrong content: got %q, want %q", buf, want) + } +} -- cgit v1.2.3