Skip to content
Merged
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: 2 additions & 0 deletions internal/fsck/fsck.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ type Checker struct {
unnamed []uint32 // inodes other than root that no directory names
snapshotInodes map[uint32]bool // inodes referenced by snapshots
snapshotData map[uint32]bool // inodes holding a snapshot's captured extents
orphanData map[uint32]int // phase 7's problem for each data inode no snapshot refers to
snapsIncomplete []string // why snapshotInodes may miss references
sfbContainers map[uint64]bool // blocks holding small files of older versions
extentTrees map[uint32]*extentTree
Expand Down Expand Up @@ -478,6 +479,7 @@ func New(dev *storage.Device, opts Options) *Checker {
dirNames: make(map[uint32]int),
snapshotInodes: make(map[uint32]bool),
snapshotData: make(map[uint32]bool),
orphanData: make(map[uint32]int),
sfbContainers: make(map[uint64]bool),
extentTrees: make(map[uint32]*extentTree),
uncounted: make(map[uint32]int),
Expand Down
5 changes: 3 additions & 2 deletions internal/fsck/phase7_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func (c *Checker) checkSnapshots() error {
for num := range c.snapshotData {
if !dataInodes[num] {
c.addProblem(PhaseSnapshots, Warning, fmt.Sprintf("inode %d", num),
"snapshot data inode belongs to no snapshot (left by a crash during snapshot creation)",
false)
"snapshot data inode belongs to no snapshot, as a crash while a snapshot is created or deleted leaves it",
true)
c.orphanData[num] = c.lastProblem()
}
}

Expand Down
11 changes: 11 additions & 0 deletions internal/fsck/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func (c *Checker) lastProblem() int {
return len(c.result.Problems) - 1
}

// setUnrepairable marks the problem recorded for num, if any, as one repair cannot fix.
func (c *Checker) setUnrepairable(problems map[uint32]int, num uint32) {
i, ok := problems[num]
if !ok {
return
}
c.result.mu.Lock()
defer c.result.mu.Unlock()
c.result.Problems[i].Repairable = false
}

// write writes a repaired sector and marks problems repaired. A dry run only
// marks them as ones repair would fix.
func (c *Checker) write(buf []byte, offset uint64, problems ...int) error {
Expand Down
6 changes: 6 additions & 0 deletions internal/fsck/unnamed.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ func (c *Checker) checkUnnamed() error {
} else {
why = c.keepUnnamed(num, ino)
}
if why != "" || !reclaim {
c.setUnrepairable(c.orphanData, num)
}
if why != "" {
if kept++; kept <= reportedLeaks {
c.addProblem(PhaseCrossValidation, Warning, loc, "unnamed: no directory names it; kept, because "+why, false)
}
continue
}
if i, ok := c.orphanData[num]; ok {
problems = append(problems, i)
}
if free = append(free, ino); len(free) <= reportedLeaks {
c.addProblem(PhaseCrossValidation, Warning, loc, "unnamed: no directory names it", reclaim)
problems = append(problems, c.lastProblem())
Expand Down
32 changes: 12 additions & 20 deletions internal/goclusterfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3295,13 +3295,13 @@ func sharingErrno(err error) syscall.Errno {

// claimInode allocates an inode slot, retrying when another node takes the
// free slot first.
func (f *FS) claimInode(ctx context.Context) (*inode.Inode, error) {
func (f *FS) claimInode(ctx context.Context, init func(*inode.Inode)) (*inode.Inode, error) {
for attempt := 0; ; attempt++ {
num, err := f.inodeTbl.FindFreeSlot()
if err != nil {
return nil, err
}
ino, err := f.inodeTbl.ClaimSlot(ctx, num)
ino, err := f.inodeTbl.ClaimSlotAs(ctx, num, init)
if !errors.Is(err, inode.ErrInodeInUse) || attempt == maxConflictRetries-1 {
return ino, err
}
Expand Down Expand Up @@ -3364,19 +3364,15 @@ func (f *File) createSnapshot(ctx context.Context, name string) syscall.Errno {
return syscall.EIO
}

data, err := f.fs.claimInode(ctx)
data, err := f.fs.claimInode(ctx, func(ino *inode.Inode) {
ino.Mode, ino.UID, ino.GID = src.Mode, src.UID, src.GID
ino.Mtime, ino.Ctime = src.Mtime, src.Ctime
ino.Flags |= inode.FlagSnapshotData
})
if err != nil {
log.Printf("[snapshot] No inode for the data of a snapshot of inode %d: %v", f.inodeNum, err)
return claimErrno(err)
}
data.Mode, data.UID, data.GID = src.Mode, src.UID, src.GID
data.Mtime, data.Ctime = src.Mtime, src.Ctime
data.Flags |= inode.FlagSnapshotData
if err := f.fs.inodeTbl.Write(ctx, data); err != nil {
f.fs.freeNewInode(ctx, data.InodeNum)
log.Printf("[snapshot] Failed to write data inode %d: %v", data.InodeNum, err)
return syscall.EIO
}

desc, err := f.fs.snapshotReg.Create(ctx, name, f.inodeNum, 0,
data.InodeNum, src.AllocatedBlocks, src.Size, snapshot.FlagReadOnly)
Expand Down Expand Up @@ -3486,22 +3482,18 @@ func (f *File) cloneInode(ctx context.Context, srcNum uint32, destPath string, s
return syscall.EIO
}

cloneIno, err := f.fs.claimInode(ctx)
now := time.Now().UnixNano()
cloneIno, err := f.fs.claimInode(ctx, func(ino *inode.Inode) {
ino.Mode, ino.UID, ino.GID = srcIno.Mode, srcIno.UID, srcIno.GID
ino.Atime, ino.Mtime, ino.Ctime = now, now, now
})
if err != nil {
log.Printf("[clone] Failed to claim an inode slot: %v", err)
return claimErrno(err)
}
cloneInodeNum := cloneIno.InodeNum

now := time.Now().UnixNano()
cloneIno.Mode = srcIno.Mode
cloneIno.UID = srcIno.UID
cloneIno.GID = srcIno.GID
cloneIno.LinkCount = 1
cloneIno.Flags |= inode.FlagHasSnapshot
cloneIno.Atime = now
cloneIno.Mtime = now
cloneIno.Ctime = now

if err := f.fs.shareInto(ctx, srcNum, cloneIno, stillValid); err != nil {
f.fs.discardInode(ctx, cloneInodeNum)
Expand Down
27 changes: 20 additions & 7 deletions internal/goclusterfs/fsck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ func newFormattedImageOfSize(t *testing.T, size int64) string {
// goclusterfs-mount does.
func openFormattedFS(t *testing.T, path string) *FS {
t.Helper()
ctx := context.Background()
fsys := openFormattedComponents(t, path)
startSnapshots(t, fsys)
return fsys
}

func startSnapshots(t *testing.T, fsys *FS) {
t.Helper()
sb, err := superblock.Read(fsys.dev)
if err != nil {
t.Fatal(err)
}
if err := fsys.InitSnapshots(context.Background(), sb.SnapshotRegOffset, sb.SnapshotRegSize, fsys.lockMgr, int(fsys.nodeUUID[15])); err != nil {
t.Fatalf("InitSnapshots: %v", err)
}
t.Cleanup(fsys.snapshotWkr.Stop)
}

func openFormattedComponents(t *testing.T, path string) *FS {
t.Helper()
dev, err := storage.Open(path)
if err != nil {
t.Fatal(err)
Expand All @@ -79,12 +97,7 @@ func openFormattedFS(t *testing.T, path string) *FS {
inodeTbl.SetTreeFlusher(extentMgr)
refs := refcount.NewRegion(dev, sb.RefCountTableOffset, sb.RefCountTableSize, sb.TotalBlocks, locks)
dirMgr := dir.NewManager(dev, inodeTbl, extentMgr, locks, sb.DataRegionOffset)
fsys := NewWithComponents(dev, bm, inodeTbl, extentMgr, dirMgr, nil, nil, nil, nil, refs, locks, nodeUUID, sb.DataRegionOffset)
if err := fsys.InitSnapshots(ctx, sb.SnapshotRegOffset, sb.SnapshotRegSize, locks, slot); err != nil {
t.Fatalf("InitSnapshots: %v", err)
}
t.Cleanup(fsys.snapshotWkr.Stop)
return fsys
return NewWithComponents(dev, bm, inodeTbl, extentMgr, dirMgr, nil, nil, nil, nil, refs, locks, nodeUUID, sb.DataRegionOffset)
}

// createFile adds a regular file to the root directory as Create does.
Expand Down
7 changes: 6 additions & 1 deletion internal/goclusterfs/journal_crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func crashAfterCommit(t *testing.T, fsys *FS, op string) <-chan struct{} {
func remountAndReplay(t *testing.T, path string, offset, size uint64) *FS {
t.Helper()
fsys := openFormattedFS(t, path)
replayCrashedJournal(t, fsys, offset, size)
return fsys
}

func replayCrashedJournal(t *testing.T, fsys *FS, offset, size uint64) {
t.Helper()
region, err := journal.OpenRegionForRecovery(fsys.dev, offset, size)
if err != nil {
t.Fatalf("open the crashed node's journal: %v", err)
Expand All @@ -77,7 +83,6 @@ func remountAndReplay(t *testing.T, path string, offset, size uint64) *FS {
if err := fsys.lockMgr.ReleaseAdopted(adopted); err != nil {
t.Fatalf("release the crashed node's locks: %v", err)
}
return fsys
}

func expectCleanFsck(t *testing.T, fsys *FS) {
Expand Down
Loading
Loading