From dca5c5d0a56c7d6e9ab5b862c115dceaa7bf005d Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 13:27:23 -0600 Subject: [PATCH 1/2] perf: zero only what a read could see, not every new block Re-lands #160, which #162 reverted. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- internal/goclusterfs/fs.go | 175 ++++++++++++++++------- internal/goclusterfs/stale_bytes_test.go | 133 +++++++++++++++++ tests/test-manifest.yaml | 14 ++ 4 files changed, 268 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28110ef1..3bb3eb59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,7 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - A file unlinked while open, on this node or another, was freed at once: its descriptors read and wrote whatever file took the inode next. The file now keeps its inode and blocks until every node has closed it. A node that dies holding it lets go when a peer replays its journal. While other nodes are mounted, each node tracks up to 487 unlinked files that other nodes have not yet let go of. An unlink beyond that waits, and fails with EBUSY only when live nodes keep holding them for 20 s. - Opening or stat-ing a file that another caller had just unlinked failed with EISDIR, or showed the freed inode's empty attributes. It now fails with ESTALE, and the kernel looks the name up again. - An open with O_CREAT and no O_EXCL failed with EEXIST when another node or thread created the same name between the kernel's lookup and the create. It now opens that file, and truncates it with O_TRUNC. -- Reads no longer show bytes nobody wrote to the file. A partial write into a newly allocated block left the rest of that block as the disk held it, often a deleted file's data, readable as soon as the file covered it; such writes now zero the block first, which doubles the bytes written for new data until a later change zeroes only what can become readable. A truncate into the middle of a block left the old bytes after the cut, and growing the file later showed them; truncate now zeroes them before it shrinks the file. +- Reads no longer show bytes nobody wrote to the file. A partial write into a newly allocated block left the rest of that block as the disk held it, often a deleted file's data, readable as soon as the file covered it. A truncate into the middle of a block left the old bytes after the cut, and growing the file later showed them. A first write into a block now zeroes the parts of it below the end of the file that it does not cover, and a write, truncate or fallocate that grows the file zeroes the rest of its old last block first, copying it if a snapshot or clone shares it. Sequential writes write their data once. - A damaged block reserve record in a journal could make journal replay and `goclusterfs-fsck` allocate up to 32 GB. A reserve record now holds at most 1,048,576 blocks, and a larger `-block-reserve` acts as 1,048,576. - A damaged journal record that claimed a length within 4 KB of 4 GB crashed journal recovery with a panic. Recovery now treats it as corrupt. - A node registry slot with a zero checksum was accepted whatever it held, so a slot torn or zeroed at its end read as valid. Now only an all-zero slot, which no node has written, skips the checksum. diff --git a/internal/goclusterfs/fs.go b/internal/goclusterfs/fs.go index d20ec4e4..2a6ae355 100644 --- a/internal/goclusterfs/fs.go +++ b/internal/goclusterfs/fs.go @@ -8,7 +8,6 @@ package goclusterfs import ( "bytes" - "cmp" "context" "errors" "fmt" @@ -2037,14 +2036,26 @@ func retryOnTreeConflict(err error) error { return err } -func (f *FS) zeroBlock(physBlock uint64) error { - buf := storage.GetBuffer(BlockSize) +// zeroRange zeroes bytes [from, to) of a data block; both are multiples of the sector size. +func (f *FS) zeroRange(physBlock, from, to uint64) error { + if to <= from { + return nil + } + buf := storage.GetBuffer(int(to - from)) defer storage.PutBuffer(buf) clear(buf.Bytes()) - _, err := f.dev.WriteAt(buf.Bytes(), int64(f.dataOffset+physBlock*BlockSize)) + _, err := f.dev.WriteAt(buf.Bytes(), int64(f.dataOffset+physBlock*BlockSize+from)) return err } +// alignSector rounds n to a sector boundary, up or down. +func alignSector(n uint64, up bool) uint64 { + if up { + n += storage.SectorSize - 1 + } + return n &^ (storage.SectorSize - 1) +} + // testHookBeforeInodeCommit, when set by a test, runs before writeInternal's final // inode write; an error it returns is handled as if that write had failed. var testHookBeforeInodeCommit func() error @@ -2147,6 +2158,9 @@ func (f *File) allocateBlockLocked(ctx context.Context, ino *inode.Inode, blockN // Returns errRetryWrite if a concurrent modification is detected and the caller should retry. func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (int, error) { bytesWritten := 0 + requested, writeStart := len(data), offset + // writtenEnd is the end of the data this write has put on disk so far; oldEnd is the file's size it started from. + writtenEnd, oldEnd := uint64(0), uint64(0) // Track if we hold the inode lock lockHeld := false @@ -2238,6 +2252,12 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i if !lockHeld || f.fs.lockMgr == nil { return nil } + // Once the lock drops, another node's growth may zero the rest of the block the file ended in. Cover what + // this write put there; a write that started at a block boundary stays invisible until it completes. + if oldEnd%BlockSize != 0 && writtenEnd > ino.Size { + ino.Size = min(writtenEnd, (oldEnd/BlockSize+1)*BlockSize) + inoDirty = true + } if inoDirty { if err := logReserveUse(); err != nil { releaseInodeLock() @@ -2293,8 +2313,34 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i } } + // A write that grows the file holds the inode lock exclusively from its first data write through its size update, + // so another node's growth cannot zero bytes this write put past the end before its size covers them. + if f.fs.lockMgr != nil && offset+uint64(len(data)) > ino.Size { + if err := f.fs.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(f.inodeNum), lock.LockModeExclusive); err != nil { + logging.Debugf("Write inode %d: failed to acquire inode lock to grow the file: %v", f.inodeNum, err) + return 0, syscall.EIO + } + lockHeld = true + if ino, err = f.fs.inodeTbl.Read(f.inodeNum); err != nil { + return 0, syscall.EIO + } + } + oldEnd = ino.Size + segments, err := f.growthSegments(ino, data, offset) + if err != nil { + return 0, err + } + offset, data = segments[0].off, segments[0].data + // Regular extent-based write path - for bytesWritten < len(data) { + for { + if bytesWritten == len(data) { + if segments = segments[1:]; len(segments) == 0 { + break + } + offset, data, bytesWritten = segments[0].off, segments[0].data, 0 + continue + } blockNum := offset / BlockSize blockOffset := offset % BlockSize zeroFirst := false @@ -2588,9 +2634,16 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i duringInPlaceWrite() } - // The rest of a block first written here would otherwise expose old disk contents. + // A block first written here keeps its old disk contents outside the write. Zero what lies below the end of + // the file; growing the file later zeroes the rest. if zeroFirst { - if err := f.fs.zeroBlock(physBlock); err != nil { + blockStart := blockNum * BlockSize + below := min(BlockSize, max(ino.Size, blockStart)-blockStart) + err := f.fs.zeroRange(physBlock, 0, alignSector(blockOffset, true)) + if from := alignSector(blockOffset+uint64(toWrite), false); err == nil && below > from { + err = f.fs.zeroRange(physBlock, from, alignSector(below, true)) + } + if err != nil { logging.Debugf("Write inode %d: zeroing block %d failed: %v", f.inodeNum, physBlock, err) if extentLockHeld { f.fs.release(lock.LockTypeExtent, physBlock, "block") @@ -2652,6 +2705,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i bytesWritten += toWrite offset += uint64(toWrite) + writtenEnd = offset } // Update inode size if extended @@ -2680,6 +2734,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i logging.Debugf("Write inode %d: re-read inode failed: %v", f.inodeNum, err) return 0, syscall.EIO } + onDisk := freshIno.Size // ino was read before the lock, so it can miss another node's extents. if acquiredLockForSize { ino = freshIno @@ -2820,6 +2875,11 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i return errRetryWrite } + // A peer resized the file after growthSegments chose what to zero; the retry zeroes from the new size. + if onDisk < writeStart && onDisk != oldEnd && onDisk%BlockSize != 0 { + return 0, writeFailed(fmt.Errorf("%w: inode %d is %d bytes, not %d as the write planned", extent.ErrTreeConflict, f.inodeNum, onDisk, oldEnd)) + } + if testHookBeforeInodeCommit != nil { if err := testHookBeforeInodeCommit(); err != nil { return 0, writeFailed(err) @@ -2863,7 +2923,37 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i } } - return bytesWritten, nil + return requested, nil +} + +type writeSegment struct { + off uint64 + data []byte +} + +// growthSegments returns what a write must put on disk. Past the end of the file, the rest of its last block may hold +// old bytes, from before a truncate or a disk block's earlier owner; a write that grows the file over them zeroes them +// first, through the same path, so a block shared with a snapshot or clone is copied rather than changed. +func (f *File) growthSegments(ino *inode.Inode, data []byte, offset uint64) ([]writeSegment, error) { + end := ino.Size + if offset <= end || end%BlockSize == 0 || ino.IsSFBBacked() { + return []writeSegment{{offset, data}}, nil + } + ext, _, err := f.fs.extentMgr.Lookup(ino, end/BlockSize) + switch { + case errors.Is(err, extent.ErrStaleTree): + return nil, errRetryWrite + case errors.Is(err, extent.ErrExtentNotFound), err == nil && ext.Flags&extent.FlagUnwritten != 0: + return []writeSegment{{offset, data}}, nil + case err != nil: + return nil, syscall.EIO + } + gapEnd := min(offset, (end/BlockSize+1)*BlockSize) + zeros := make([]byte, gapEnd-end) + if gapEnd == offset { + return []writeSegment{{end, append(zeros, data...)}}, nil + } + return []writeSegment{{end, zeros}, {offset, data}}, nil } // sfbSlotOffset returns the disk offset of an SFB-backed inode's data. SFB @@ -3028,16 +3118,10 @@ func (f *File) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn ctx = detach(ctx) size, resizing := in.GetSize() for attempt := 0; ; attempt++ { - // Holding the inode lock from the zeroing through the size change keeps this node's writes out between them. unlock := f.fs.lockInode(f.inodeNum) - var zeroedAt uint64 - var errno syscall.Errno - var err error - if resizing { - zeroedAt, errno, err = f.zeroPastEnd(ctx, size) - } + errno, err := f.growTo(ctx, size, resizing) if err == nil && errno == 0 { - errno, err = f.setattr(ctx, fh, in, out, zeroedAt) + errno, err = f.setattr(ctx, fh, in, out) } unlock() if err == nil { @@ -3050,49 +3134,31 @@ func (f *File) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn } } -// zeroPastEnd zeroes the bytes a shrink to size leaves in its last block, before the size changes, so growing the -// file later reads zeros there instead of the old data. It writes through Write, which copies a shared block first. -// It returns the MetaGeneration its decision rests on, or 0 when no shrink into a block needs it; setattr starts over -// if the inode changed since. The caller holds the inode lock. -func (f *File) zeroPastEnd(ctx context.Context, size uint64) (uint64, syscall.Errno, error) { - f.fs.invalidateInode(f.inodeNum) +// growTo grows the file to size, if that is larger, as an empty write at size: it zeroes the old bytes the rest of +// the last block may hold before the size covers them. The caller holds the inode lock. +func (f *File) growTo(ctx context.Context, size uint64, resizing bool) (syscall.Errno, error) { + if !resizing { + return 0, nil + } ino, err := f.fs.inodeTbl.Read(f.inodeNum) if err != nil { - return 0, syscall.EIO, nil - } - end := min(ino.Size, (size/BlockSize+1)*BlockSize) - if size%BlockSize == 0 || size >= end { - return 0, 0, nil - } - if ino.IsSFBBacked() { - return ino.MetaGeneration, 0, nil - } - ext, _, err := f.fs.extentMgr.Lookup(ino, size/BlockSize) - switch { - case errors.Is(err, extent.ErrStaleTree): - return 0, 0, errRetryWrite - case errors.Is(err, extent.ErrExtentNotFound), err == nil && ext.Flags&extent.FlagUnwritten != 0: - return ino.MetaGeneration, 0, nil - case err != nil: - return 0, syscall.EIO, nil - } - n, err := f.writeInternal(ctx, make([]byte, end-size), size) - if err == errRetryWrite { - return 0, 0, err + return syscall.EIO, nil } - if err != nil || uint64(n) != end-size { - return 0, cmp.Or(errToErrno(err), syscall.EIO), nil + if size <= ino.Size { + return 0, nil } - f.fs.invalidateInode(f.inodeNum) - if ino, err = f.fs.inodeTbl.Read(f.inodeNum); err != nil { - return 0, syscall.EIO, nil + if _, err := f.writeInternal(ctx, nil, size); err != nil { + if err == errRetryWrite { + return 0, err + } + return errToErrno(err), nil } - return ino.MetaGeneration, 0, nil + return 0, nil } // setattr applies one Setattr attempt. A non-nil error means another node // changed the inode's extent tree first and the attempt should be redone. -func (f *File) setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut, zeroedAt uint64) (syscall.Errno, error) { +func (f *File) setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) (syscall.Errno, error) { // Invalidate cache before modifying - ensures we read fresh data f.fs.invalidateInode(f.inodeNum) @@ -3120,10 +3186,6 @@ func (f *File) setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn } } readGen := ino.MetaGeneration - // Another change since zeroPastEnd looked, such as an SFB migration, may have mapped bytes past the new end again. - if zeroedAt != 0 && readGen != zeroedAt { - return 0, errRetryWrite - } now := time.Now().UnixNano() @@ -3308,7 +3370,10 @@ func (f *File) Allocate(ctx context.Context, fh fs.FileHandle, off uint64, size for attempt := 0; ; attempt++ { unlock := f.fs.lockInode(f.inodeNum) - errno, err := f.allocate(ctx, off, size, mode) + errno, err := f.growTo(ctx, off+size, mode == 0) + if err == nil && errno == 0 { + errno, err = f.allocate(ctx, off, size, mode) + } unlock() if err == nil { return errno diff --git a/internal/goclusterfs/stale_bytes_test.go b/internal/goclusterfs/stale_bytes_test.go index 134e1165..2acfabbc 100644 --- a/internal/goclusterfs/stale_bytes_test.go +++ b/internal/goclusterfs/stale_bytes_test.go @@ -4,7 +4,10 @@ import ( "bytes" "context" "fmt" + "os" "slices" + "strconv" + "strings" "sync" "sync/atomic" "testing" @@ -118,6 +121,25 @@ func TestReadsNeverShowStaleBytes(t *testing.T) { expectContent(t, c, want) return data }}, + {"AppendThenFallocate", func(t *testing.T, fsys *FS, b *File, freed []uint64) []byte { + writeAt(t, b, data, 0) + expectReused(t, fsys, b, 0, freed) + if errno := b.Allocate(context.Background(), nil, 0, 2*BlockSize, 0); errno != 0 { + t.Fatalf("fallocate: %v", errno) + } + return append(slices.Clone(data), make([]byte, 2*BlockSize-len(data))...) + }}, + {"SnapshotOfAReusedBlockThenGrow", func(t *testing.T, fsys *FS, b *File, freed []uint64) []byte { + writeAt(t, b, data, 0) + expectReused(t, fsys, b, 0, freed) + setxattr(t, b, "user.snapshot", "s") + writeAt(t, b, data, 600000) + setxattr(t, b, "user.snapshot.clone", "s:/sc") + sc := lookupFile(t, fsys, "sc") + truncateFile(t, sc, 700000) + expectContent(t, sc, append(slices.Clone(data), make([]byte, 700000-len(data))...)) + return append(append(slices.Clone(data), make([]byte, 600000-len(data))...), data...) + }}, {"CloneOfASnapshotOfATruncatedFile", func(t *testing.T, fsys *FS, b *File, _ []uint64) []byte { writeAt(t, b, pattern(3, 900000), 0) truncateFile(t, b, 1000) @@ -267,3 +289,114 @@ func TestTruncateWhileAppending(t *testing.T) { t.Fatalf("%d of 200 truncates failed while the file was appended to", failed) } } + +// One node appends records while another writes past the end of the file. Zeroing the gap before that write must +// never wipe a record the first node wrote but had not yet covered with its size. +func TestGrowingWriteNeverZeroesAnotherNodesAppend(t *testing.T) { + path := newFormattedImage(t) + nodeA, nodeB := openFormattedFS(t, path), openFormattedFS(t, path) + ctx := context.Background() + const records, size = 64, 4096 + for round := range 8 { + name := fmt.Sprintf("f%d", round) + a := createFile(t, nodeA, name) + b := &File{fs: nodeB, inodeNum: a.inodeNum} + var wg sync.WaitGroup + wg.Go(func() { + for k := range records { + if _, errno := a.Write(ctx, nil, pattern(byte(k+1), size), int64(k*size)); errno != 0 { + t.Errorf("record %d: %v", k, errno) + } + } + }) + time.Sleep(time.Duration(round) * time.Millisecond) + if _, errno := b.Write(ctx, nil, pattern(99, 100), 900000); errno != 0 { + t.Fatalf("write past the end: %v", errno) + } + wg.Wait() + got := readWhole(t, a) + for k := range records { + if !bytes.Equal(got[k*size:(k+1)*size], pattern(byte(k+1), size)) { + t.Fatalf("round %d: record %d was lost to the other node's growth", round, k) + } + } + } +} + +// writtenBytes returns how many bytes this process has passed to write system calls. +func writtenBytes(t *testing.T) uint64 { + t.Helper() + io, err := os.ReadFile("/proc/self/io") + if err != nil { + t.Skipf("no /proc/self/io: %v", err) + } + for _, line := range strings.Split(string(io), "\n") { + if v, ok := strings.CutPrefix(line, "wchar: "); ok { + n, err := strconv.ParseUint(v, 10, 64) + if err != nil { + t.Fatal(err) + } + return n + } + } + t.Fatal("no wchar in /proc/self/io") + return 0 +} + +// A sequential write in FUSE's 128 KiB pieces writes its data once, not a block of zeros before each new block. +func TestSequentialWriteWritesDataOnce(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + f := createFile(t, fsys, "seq") + const total, piece = 16 << 20, 128 << 10 + chunk := pattern(7, piece) + before := writtenBytes(t) + for off := 0; off < total; off += piece { + writeAt(t, f, chunk, int64(off)) + } + ratio := float64(writtenBytes(t)-before) / total + t.Logf("writing %d MiB wrote %.2fx its size", total>>20, ratio) + if ratio > 1.2 { + t.Fatalf("writing %d MiB wrote %.2fx its size, want about 1x", total>>20, ratio) + } +} + +// A write below the end of the file plans no gap, then a peer truncates before the write sets its size. The write must +// not grow the file back over the truncated bytes without zeroing them. +func TestWriteAfterPeerTruncateShowsNoOldBytes(t *testing.T) { + path := newFormattedImage(t) + nodeA, nodeB := openFormattedFS(t, path), openFormattedFS(t, path) + ctx := context.Background() + a := createFile(t, nodeA, "f") + writeAt(t, a, pattern(3, 10000), 0) + b := &File{fs: nodeB, inodeNum: a.inodeNum} + + var once sync.Once + truncated := make(chan struct{}) + beforeSizeUpdate = func(*File) { + once.Do(func() { + go func() { + defer close(truncated) + in := &fuse.SetAttrIn{} + in.Valid, in.Size = fuse.FATTR_SIZE, 100 + if errno := b.Setattr(ctx, nil, in, &fuse.AttrOut{}); errno != 0 { + t.Errorf("peer truncate: %v", errno) + } + }() + select { + case <-truncated: + case <-time.After(3 * time.Second): + t.Error("the peer's truncate did not land while the write waited to set its size") + } + }) + } + t.Cleanup(func() { beforeSizeUpdate = nil }) + + data := pattern(9, 10) + if _, errno := a.Write(ctx, nil, data, 5000); errno != 0 { + t.Fatalf("write: %v", errno) + } + <-truncated + beforeSizeUpdate = nil + want := append(append(pattern(3, 100), make([]byte, 4900)...), data...) + expectContent(t, &File{fs: openFormattedFS(t, path), inodeNum: a.inodeNum}, want) +} diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 82ee4c74..bb2f6e4e 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1970,6 +1970,20 @@ scenarios: environments: go: internal/goclusterfs/stale_bytes_test.go::TestTruncateWhileAppending + - id: growing-write-keeps-other-nodes-append + name: Zeroing before a growing write never wipes another node's append + description: One node appends records while another writes past the end of the file; every acknowledged record survives the zeroing of the gap before the second write + priority: go_only + environments: + go: internal/goclusterfs/stale_bytes_test.go::TestGrowingWriteNeverZeroesAnotherNodesAppend + + - id: sequential-write-writes-data-once + name: A sequential write writes its data once + description: Writing 16 MiB in FUSE's 128 KiB pieces passes about 1x its size to write calls, with no block of zeros before each new block + priority: go_only + environments: + go: internal/goclusterfs/stale_bytes_test.go::TestSequentialWriteWritesDataOnce + - id: fsck-repair-in-use name: fsck -repair refuses an in-use filesystem description: -repair refuses while a node heartbeats or a stopped node's journal is dirty; -force overrides only the journal check; dry runs and checks still run From d59aebc4652818f54aabef871f84d9ac52a7fafe Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 13:27:23 -0600 Subject: [PATCH 2/2] fix: zero only the asked range of a block, not the rest of the pooled buffer The pool rounds sizes up, so zeroing a block's tail also zeroed up to 768 KB of the next block, which another file or directory owns. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/fs.go | 2 +- internal/goclusterfs/stale_bytes_test.go | 38 ++++++++++++++++++++++++ tests/test-manifest.yaml | 7 +++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/internal/goclusterfs/fs.go b/internal/goclusterfs/fs.go index 2a6ae355..e1428455 100644 --- a/internal/goclusterfs/fs.go +++ b/internal/goclusterfs/fs.go @@ -2044,7 +2044,7 @@ func (f *FS) zeroRange(physBlock, from, to uint64) error { buf := storage.GetBuffer(int(to - from)) defer storage.PutBuffer(buf) clear(buf.Bytes()) - _, err := f.dev.WriteAt(buf.Bytes(), int64(f.dataOffset+physBlock*BlockSize+from)) + _, err := f.dev.WriteAt(buf.Bytes()[:to-from], int64(f.dataOffset+physBlock*BlockSize+from)) return err } diff --git a/internal/goclusterfs/stale_bytes_test.go b/internal/goclusterfs/stale_bytes_test.go index 2acfabbc..064aee29 100644 --- a/internal/goclusterfs/stale_bytes_test.go +++ b/internal/goclusterfs/stale_bytes_test.go @@ -400,3 +400,41 @@ func TestWriteAfterPeerTruncateShowsNoOldBytes(t *testing.T) { want := append(append(pattern(3, 100), make([]byte, 4900)...), data...) expectContent(t, &File{fs: openFormattedFS(t, path), inodeNum: a.inodeNum}, want) } + +// The buffer pool rounds sizes up, so zeroing a block's tail once also zeroed the start of the next block, which another +// file or directory owns. +func TestZeroRangeZeroesOnlyItsRange(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + const first = 10 + fill := bytes.Repeat([]byte{0xAB}, BlockSize) + for _, r := range []struct{ from, to uint64 }{ + {0, 4096}, + {0, 176128}, + {163840, BlockSize}, + {176128, BlockSize}, + {1024000, BlockSize}, + {BlockSize - 4096, BlockSize}, + } { + for b := uint64(first); b < first+3; b++ { + if _, err := fsys.dev.WriteAt(fill, int64(fsys.dataOffset+b*BlockSize)); err != nil { + t.Fatal(err) + } + } + if err := fsys.zeroRange(first+1, r.from, r.to); err != nil { + t.Fatal(err) + } + got := make([]byte, 3*BlockSize) + if _, err := fsys.dev.ReadAt(got, int64(fsys.dataOffset+first*BlockSize)); err != nil { + t.Fatal(err) + } + want := bytes.Repeat([]byte{0xAB}, 3*BlockSize) + clear(want[BlockSize+r.from : BlockSize+r.to]) + if !bytes.Equal(got, want) { + at := 0 + for got[at] == want[at] { + at++ + } + t.Errorf("zeroing [%d, %d) of the middle block: first wrong byte at %d of the three blocks", r.from, r.to, at) + } + } +} diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index bb2f6e4e..1b3a063c 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1984,6 +1984,13 @@ scenarios: environments: go: internal/goclusterfs/stale_bytes_test.go::TestSequentialWriteWritesDataOnce + - id: zero-range-stays-in-range + name: Zeroing part of a block leaves the blocks around it alone + description: The buffer pool rounds sizes up, so zeroing a block's tail must not also zero the start of the next block, which another file or directory owns + priority: go_only + environments: + go: internal/goclusterfs/stale_bytes_test.go::TestZeroRangeZeroesOnlyItsRange + - id: fsck-repair-in-use name: fsck -repair refuses an in-use filesystem description: -repair refuses while a node heartbeats or a stopped node's journal is dirty; -force overrides only the journal check; dry runs and checks still run