Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
175 changes: 120 additions & 55 deletions internal/goclusterfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package goclusterfs

import (
"bytes"
"cmp"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -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()[:to-from], 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading