//go:build !freebsd package mount import ( "bytes" "context" "os" "path/filepath" "syscall" "testing" "github.com/seaweedfs/go-fuse/v2/fuse" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/protobuf/proto" ) // openSwapWriteHandle opens /dir/file for writing on a WFS with one writable // chunk slot, so a second chunk is buffered in the swap file under swapDir. // The file holds 256 'O' bytes on the fake filer. func openSwapWriteHandle(t *testing.T, swapDir string) (*WFS, *FileHandle, *g6ConditionalPeer, *fuse.OpenOut) { t.Helper() base := attributeAppendEntry(1) base.CommitTsNs = 100 peer := &g6ConditionalPeer{entry: proto.Clone(base).(*filer_pb.Entry)} peer.blobs = map[string][]byte{base.Chunks[0].GetFileIdString(): bytes.Repeat([]byte{'O'}, 256)} w := newInvalidateTestWFS(t) w.option.uniqueCacheDirForWrite = swapDir w.option.DefaultPermissions = true w.posixLocks = NewPosixLockTable() w.option.ConcurrentWriters = 1 w.concurrentWriters = util.NewLimitedConcurrentExecutor(1) startFakeFiler(t, w, peer) w.inodeToPath.Lookup("/dir", 1, true, false, 0, false) w.inodeToPath.Lookup("/dir/file", 1, false, false, 42, true) w.inodeToPath.MarkChildrenCached("/dir") if err := w.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", proto.Clone(base).(*filer_pb.Entry)), 1); err != nil { t.Fatal(err) } out := &fuse.OpenOut{} if code := w.Open(nil, &fuse.OpenIn{InHeader: fuse.InHeader{NodeId: 42}, Flags: syscall.O_WRONLY}, out); code != fuse.OK { t.Fatal(code) } fh := w.GetHandle(FileHandleId(out.Fh)) g6AppendUploader(t, fh, 100, peer) return w, fh, peer, out } // A write the dirty pages cannot buffer fails, and must not grow the file: the // next flush would publish that size with a hole where its data should be. A // write that buffers only its first chunk is a short write of those bytes, or // an error for kernel writeback, which would drop the rest silently. func TestFailedWriteDoesNotGrowFile(t *testing.T) { t.Run("short write", func(t *testing.T) { testFailedWriteDoesNotGrowFile(t, 0) }) t.Run("kernel writeback", func(t *testing.T) { testFailedWriteDoesNotGrowFile(t, fuse.WRITE_CACHE) }) } func testFailedWriteDoesNotGrowFile(t *testing.T, writeFlags uint32) { w, fh, peer, out := openSwapWriteHandle(t, t.TempDir()) // Chunks are 1024 bytes: the dirty pages keep the first chunk only. fh.dirtyPages.randomWriter = &refusingDirtyPages{DirtyPages: fh.dirtyPages.randomWriter, limit: 1024} write := func(offset uint64, size int, value byte) (uint32, fuse.Status) { return w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh, Offset: offset, Size: uint32(size), WriteFlags: writeFlags}, bytes.Repeat([]byte{value}, size)) } if _, code := write(256, 256, 'A'); code != fuse.OK { t.Fatalf("buffered write: %v", code) } if _, code := write(4096, 256, 'B'); code == fuse.OK { t.Fatal("a write the dirty pages refused succeeded") } if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 512 { t.Fatalf("file size after the failed write = %d, want 512", size) } // The first 24 bytes fit the first chunk; the dirty pages refuse the next. n, code := write(1000, 48, 'C') if writeFlags == 0 && (code != fuse.OK || n != 24) { t.Fatalf("boundary-crossing write = %d, %v; want a short write of 24", n, code) } if writeFlags != 0 && code == fuse.OK { t.Fatalf("boundary-crossing writeback = %d, OK; want an error", n) } if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 1024 { t.Fatalf("file size after the short write = %d, want 1024", size) } if code := w.Fsync(nil, &fuse.FsyncIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh}); code != fuse.OK { t.Fatal(code) } peer.mu.Lock() defer peer.mu.Unlock() want := append(bytes.Repeat([]byte{'O'}, 256), bytes.Repeat([]byte{'A'}, 256)...) want = append(want, make([]byte, 1000-512)...) want = append(want, bytes.Repeat([]byte{'C'}, 24)...) if got := g6StoredBytes(t, peer); peer.entry.Attributes.FileSize != 1024 || !bytes.Equal(got, want) { t.Fatalf("stored size %d, want 1024 with the original and buffered bytes", peer.entry.Attributes.FileSize) } } // A write that needs the swap file used to fail when the swap directory could // not be created, as on a full cache disk. It is buffered in memory instead, // and the flush stores it. func TestWriteBuffersInMemoryWhenTheSwapDirectoryIsUnusable(t *testing.T) { notADir := filepath.Join(t.TempDir(), "file") if err := os.WriteFile(notADir, nil, 0600); err != nil { t.Fatal(err) } w, _, peer, out := openSwapWriteHandle(t, filepath.Join(notADir, "swap")) write := func(offset uint64, size int, value byte) (uint32, fuse.Status) { return w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh, Offset: offset, Size: uint32(size)}, bytes.Repeat([]byte{value}, size)) } if _, code := write(256, 256, 'A'); code != fuse.OK { t.Fatalf("buffered write: %v", code) } // The only writable chunk slot is taken, so this write needs the swap file. if n, code := write(4096, 256, 'B'); code != fuse.OK || n != 256 { t.Fatalf("write needing the unusable swap file = %d, %v; want 256, OK", n, code) } if code := w.Fsync(nil, &fuse.FsyncIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh}); code != fuse.OK { t.Fatal(code) } peer.mu.Lock() defer peer.mu.Unlock() want := append(bytes.Repeat([]byte{'O'}, 256), bytes.Repeat([]byte{'A'}, 256)...) want = append(want, make([]byte, 4096-512)...) want = append(want, bytes.Repeat([]byte{'B'}, 256)...) if got := g6StoredBytes(t, peer); peer.entry.Attributes.FileSize != 4352 || !bytes.Equal(got, want) { t.Fatalf("stored size %d, want 4352 with every write", peer.entry.Attributes.FileSize) } } // Writes buffered in the swap file were acknowledged to the application. If the // swap file cannot be read back when they upload, fsync must fail like a failed // chunk upload and the filer must not get an entry that claims those bytes. func TestFsyncFailsWhenSwapFileCannotBeReadBack(t *testing.T) { swapDir := t.TempDir() w, _, peer, out := openSwapWriteHandle(t, swapDir) peer.mu.Lock() before := proto.Clone(peer.entry).(*filer_pb.Entry) peer.mu.Unlock() write := func(offset uint64, value byte) (uint32, fuse.Status) { return w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh, Offset: offset, Size: 256}, bytes.Repeat([]byte{value}, 256)) } if n, code := write(256, 'A'); n != 256 || code != fuse.OK { t.Fatalf("buffered write = %d, %v", n, code) } // The only writable chunk slot is taken, so this write goes to the swap file. if n, code := write(4096, 'B'); n != 256 || code != fuse.OK { t.Fatalf("swap file write = %d, %v", n, code) } swapFiles, err := os.ReadDir(swapDir) if err != nil || len(swapFiles) != 1 { t.Fatalf("swap dir = %v, %v; want one swap file", swapFiles, err) } // An outside cleanup cuts the swap file after the write was acknowledged. if err := os.Truncate(filepath.Join(swapDir, swapFiles[0].Name()), 0); err != nil { t.Fatal(err) } code := w.Fsync(nil, &fuse.FsyncIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh}) if code == fuse.OK { t.Fatal("fsync succeeded although acknowledged bytes could not be uploaded") } t.Logf("fsync = %v", code) if _, code := write(8192, 'C'); code == fuse.OK { t.Error("write after the failed upload succeeded; the handle can no longer flush cleanly") } peer.mu.Lock() defer peer.mu.Unlock() // The failed fsync publishes nothing: the filer keeps the entry and the 256 // bytes it had before the writes. if !proto.Equal(peer.entry, before) { t.Fatalf("a failed fsync changed the filer entry:\n got %v\nwant %v", peer.entry, before) } if stored := g6StoredBytes(t, peer); !bytes.Equal(stored, bytes.Repeat([]byte{'O'}, 256)) { t.Fatalf("filer content = %q, want the original 256 bytes", stored) } }