instancefs: purge past the depth bound; a moved tree is not "gone"
At 65,536 levels the walk refused with ErrTooDeep and the demo purge logged DEMO-PURGE-OPERATOR for a hand removal no verb or runbook supported. The walk now renames the directory it would descend into up into the purge root (renameat2 NOREPLACE, top reopened and checked by dev/ino as before) and carries on, so any depth purges with the same 65,536-name stack and three descriptors. ErrTooDeep and the operator log line are gone; purgeDemoWithJobs is as it was before this branch. A child that vanished before descent, or a stack directory missing on the re-walk, returned an ENOENT PathError, which every caller maps to "already gone". A vanished child now re-lists the directory (up to 64 times); a missing stack directory is an error that is not ErrNotExist. Astra R1 P2 (recovery path), Fable R1 P2 (ENOENT; runbook gap).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+181
−37
internal/instancectl/demo_cleanup.go+0 −6internal/instancefs/dirs_linux.go+103 −14internal/instancefs/instancefs.go+0 −5internal/instancefs/removeall_linux_test.go+78 −12
diff --git a/internal/instancectl/demo_cleanup.go b/internal/instancectl/demo_cleanup.go| index c5e84977b..ec5b89d5d 100644 |
| --- a/internal/instancectl/demo_cleanup.go |
| +++ b/internal/instancectl/demo_cleanup.go |
| @@ -133,12 +133,6 @@ func (s *Server) purgeDemoWithJobs(ctx context.Context, d demo.Descriptor, jobs |
| if errors.Is(err, os.ErrNotExist) { |
| return nil |
| } |
| - if errors.Is(err, instancefs.ErrTooDeep) { |
| - // No retry can finish this tree (F6). The purge fails as any other |
| - // does: the demo stays releasing under its hold and the agent logs |
| - // DEMO-EXPIRED with each retry. This line says why. |
| - s.logf("instance-control: DEMO-PURGE-OPERATOR demo=%s host=%s: %v: remove it by hand, then the next retry completes the purge", d.ID, d.Host, err) |
| - } |
| return err |
| } |
| // Delete the database before its local initialization proof. |
diff --git a/internal/instancefs/dirs_linux.go b/internal/instancefs/dirs_linux.go| index cc7425baf..f541352f4 100644 |
| --- a/internal/instancefs/dirs_linux.go |
| +++ b/internal/instancefs/dirs_linux.go |
| @@ -168,10 +168,10 @@ func RenameBetweenNoReplace(srcDir, srcName, dstDir, dstName string) error { |
| // |
| // Neither depth nor width costs descriptors or much memory (F6). The walk |
| // holds at most three directory descriptors, dir's included, keeps a stack |
| -// of names rather than descriptors, and reads entries 256 at a time. A |
| -// tree deeper than 65,536 directories is refused with ErrTooDeep and what |
| -// remains is left for an operator. An interrupted removal leaves a smaller |
| -// tree that the next call finishes. |
| +// of names rather than descriptors, and reads entries 256 at a time. At |
| +// 65,536 levels it moves the directory it would descend into up into name |
| +// itself and carries on, so any depth is removed with that stack. An |
| +// interrupted removal leaves a smaller tree that the next call finishes. |
| func RemoveAllBeneath(dir, name string) error { |
| if err := bareName(name); err != nil { |
| return err |
| @@ -195,6 +195,9 @@ func removeAllAt(parent int, name, path string) error { |
| const ( |
| removeBatch = 256 |
| removeMaxDepth = 65536 |
| + // removeVanishedRetries is how many times in a row the walk looks again |
| + // for a directory that went between its listing and the descent. |
| + removeVanishedRetries = 64 |
| // removeAnchorEvery is how far apart the walk's one kept directory |
| // descriptor (its anchor) sits in a deep tree, so that backing up a |
| // level re-walks at most this many components rather than the whole |
| @@ -226,6 +229,9 @@ type treeRemover struct { |
| // afterBatch runs after each batch of entries is handled, with the |
| // stack of names below top. Tests only; nil in production. |
| afterBatch func(stack []string) error |
| + // beforeOpen runs just before the walk descends into child. Tests |
| + // only; nil in production. |
| + beforeOpen func(stack []string, child string) |
| stack []string |
| anchor int // a descriptor, or -1 for none |
| @@ -233,6 +239,7 @@ type treeRemover struct { |
| topDev uint64 |
| topIno uint64 |
| live, peak int // directory descriptors open now, and at most |
| + hoisted int // directories moved up into top so far |
| buf []byte |
| off, end int |
| names []string |
| @@ -265,6 +272,7 @@ func (r *treeRemover) remove() (err error) { |
| if err != nil { |
| return r.walkError(err) |
| } |
| + vanished := 0 |
| for { |
| child, err := r.clear(cur) |
| if err != nil { |
| @@ -273,14 +281,27 @@ func (r *treeRemover) remove() (err error) { |
| } |
| if child != "" { |
| if len(r.stack) >= r.maxDepth { |
| - r.release(cur) |
| - return fmt.Errorf("%w: %s", ErrTooDeep, r.path) |
| + if err := r.hoist(cur, child); err != nil { |
| + r.release(cur) |
| + return err |
| + } |
| + continue |
| + } |
| + if r.beforeOpen != nil { |
| + r.beforeOpen(r.stack, child) |
| } |
| next, err := r.open(cur, child) |
| + if errors.Is(err, unix.ENOENT) && vanished < removeVanishedRetries { |
| + // It went after the listing: list cur again. It is not |
| + // the end of the tree. |
| + vanished++ |
| + continue |
| + } |
| r.release(cur) |
| if err != nil { |
| - return &os.PathError{Op: "openat2", Path: r.where(child), Err: err} |
| + return r.changed(&os.PathError{Op: "openat2", Path: r.where(child), Err: err}) |
| } |
| + vanished = 0 |
| r.stack = append(r.stack, child) |
| cur = next |
| if d := len(r.stack); d%removeAnchorEvery == 0 { |
| @@ -301,7 +322,7 @@ func (r *treeRemover) remove() (err error) { |
| } |
| parent, err := r.at(depth - 1) |
| if err != nil { |
| - return r.walkError(err) |
| + return r.changed(r.walkError(err)) |
| } |
| if err := unix.Unlinkat(parent, name, unix.AT_REMOVEDIR); err != nil && !errors.Is(err, unix.ENOENT) { |
| r.release(parent) |
| @@ -332,6 +353,60 @@ func (r *treeRemover) walkError(err error) error { |
| return &os.PathError{Op: "openat2", Path: r.where("."), Err: err} |
| } |
| +// changed is a mid-walk error. A directory the walk already stood in that |
| +// is now missing was moved, perhaps out of the tree with what it holds, so |
| +// "no such file" here is never evidence that the tree is gone: callers map |
| +// that to success. Other errors pass unchanged. |
| +func (r *treeRemover) changed(err error) error { |
| + if errors.Is(err, unix.ENOENT) { |
| + return fmt.Errorf("instancefs: %s changed during removal: %v", r.path, err) |
| + } |
| + return err |
| +} |
| + |
| +// hoist moves child, a directory in cur at the depth bound, up into top |
| +// under a fresh name, so the walk meets it again near the top instead of |
| +// keeping more names. Both directories are inside the tree being removed; |
| +// top is reopened from dir as at does, checked to be the directory the walk |
| +// began on, and renameat2 follows no link and replaces nothing. cur stays |
| +// open. The anchor is dropped unless it is cur, so at most three |
| +// descriptors are open: cur, dir and top. |
| +func (r *treeRemover) hoist(cur int, child string) error { |
| + if r.anchor != cur { |
| + r.dropAnchor() |
| + } |
| + d, err := r.openDirFD() |
| + if err != nil { |
| + return err |
| + } |
| + top, err := r.open(d, r.top) |
| + r.closeDirFD(d) |
| + if err == nil { |
| + if err = r.sameTop(top); err != nil { |
| + r.release(top) |
| + } |
| + } |
| + if err != nil { |
| + return r.changed(r.walkError(err)) |
| + } |
| + defer r.release(top) |
| + for range removeVanishedRetries { |
| + name := fmt.Sprintf(".carlos-removing-%d", r.hoisted) |
| + r.hoisted++ |
| + err = unix.Renameat2(cur, child, top, name, unix.RENAME_NOREPLACE) |
| + if errors.Is(err, unix.ENOENT) { |
| + return nil // child went meanwhile: cur is listed again |
| + } |
| + if !errors.Is(err, unix.EEXIST) { |
| + break |
| + } |
| + } |
| + if err != nil { |
| + return &os.PathError{Op: "renameat2", Path: r.where(child), Err: err} |
| + } |
| + return nil |
| +} |
| + |
| // open opens the directory name under parent: exactly one component, never |
| // through a symlink and never out of parent. |
| func (r *treeRemover) open(parent int, name string) (int, error) { |
| @@ -375,21 +450,35 @@ func (r *treeRemover) setAnchor(fd, depth int) { |
| r.anchor, r.anchorDepth = fd, depth |
| } |
| -// anchorAtDir opens dir exactly as RemoveAllBeneath's caller named it, or |
| -// duplicates the caller's held descriptor for it. |
| -func (r *treeRemover) anchorAtDir() error { |
| - r.dropAnchor() |
| +// openDirFD opens dir exactly as RemoveAllBeneath's caller named it, or |
| +// duplicates the caller's held descriptor for it. closeDirFD gives it back. |
| +func (r *treeRemover) openDirFD() (int, error) { |
| var fd int |
| var err error |
| if r.holdsDir { |
| if fd, err = unix.FcntlInt(uintptr(r.held), unix.F_DUPFD_CLOEXEC, 0); err != nil { |
| - return &os.PathError{Op: "dup", Path: r.dir, Err: err} |
| + return -1, &os.PathError{Op: "dup", Path: r.dir, Err: err} |
| } |
| } else if fd, err = openDir(r.dir); err != nil { |
| - return err |
| + return -1, err |
| } |
| r.live++ |
| r.peak = max(r.peak, r.live) |
| + return fd, nil |
| +} |
| + |
| +func (r *treeRemover) closeDirFD(fd int) { |
| + unix.Close(fd) |
| + r.live-- |
| +} |
| + |
| +// anchorAtDir makes dir the walk's anchor. |
| +func (r *treeRemover) anchorAtDir() error { |
| + r.dropAnchor() |
| + fd, err := r.openDirFD() |
| + if err != nil { |
| + return err |
| + } |
| r.anchor, r.anchorDepth = fd, -1 |
| return nil |
| } |
diff --git a/internal/instancefs/instancefs.go b/internal/instancefs/instancefs.go| index ce391d1d4..5ea8c09e7 100644 |
| --- a/internal/instancefs/instancefs.go |
| +++ b/internal/instancefs/instancefs.go |
| @@ -44,11 +44,6 @@ var ErrNotRegular = errors.New("instancefs: not a regular file") |
| // directory: never evidence that the name is absent. |
| var ErrDirUnopened = errors.New("instancefs: the directory could not be opened") |
| -// ErrTooDeep is returned when RemoveAllBeneath meets a tree nested deeper |
| -// than it will walk (65,536 directories). It stops and leaves what remains |
| -// in place for an operator. |
| -var ErrTooDeep = errors.New("instancefs: tree too deep to remove") |
| - |
| // ErrNotProven is returned by PurgeProven when its removals ran and it |
| // still cannot say the storage it opened is empty of what it was asked to |
| // remove and is what the path names: something is left, or a directory was |
diff --git a/internal/instancefs/removeall_linux_test.go b/internal/instancefs/removeall_linux_test.go| index 7d8f797e9..2a18ca20c 100644 |
| --- a/internal/instancefs/removeall_linux_test.go |
| +++ b/internal/instancefs/removeall_linux_test.go |
| @@ -272,23 +272,89 @@ func TestRemoveAllBeneathInterruptedResumes(t *testing.T) { |
| gone(t, top) |
| } |
| -// Past the depth bound the walk refuses with ErrTooDeep and leaves the |
| -// tree; within it, it removes everything. |
| -func TestRemoveAllBeneathTooDeep(t *testing.T) { |
| +// Past the depth bound the walk does not refuse: it moves the directory it |
| +// would have descended into up into top and carries on, so any depth |
| +// purges with the same bounded stack and descriptors. The bound sits at an |
| +// anchor (512) and between anchors (300), and below the first (8). |
| +func TestRemoveAllBeneathPastDepthBoundHoists(t *testing.T) { |
| + for _, c := range []struct{ bound, depth int }{{8, 40}, {300, 1500}, {512, 1500}} { |
| + t.Run(fmt.Sprintf("bound=%d", c.bound), func(t *testing.T) { |
| + dir := t.TempDir() |
| + chain(t, dir, c.depth, "d") |
| + files(t, filepath.Join(dir, "top", "d"), 300) |
| + lowNofile(t, 16) |
| + r := treeRemover{dir: dir, top: "top", path: filepath.Join(dir, "top"), maxDepth: c.bound} |
| + if err := r.remove(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r.live != 0 || r.peak > 3 { |
| + t.Fatalf("live=%d peak=%d directory descriptors", r.live, r.peak) |
| + } |
| + gone(t, filepath.Join(dir, "top")) |
| + }) |
| + } |
| +} |
| + |
| +// A directory on the walk's stack renamed out of the tree mid-walk leaves |
| +// its contents behind under the new name. The walk cannot reach it again, |
| +// and its error must not read as "no such file": callers take that for a |
| +// tree already gone and report the purge done. |
| +func TestRemoveAllBeneathAncestorMovedIsNotGone(t *testing.T) { |
| dir := t.TempDir() |
| - chain(t, dir, 12, "d") |
| - r := treeRemover{dir: dir, top: "top", path: filepath.Join(dir, "top"), maxDepth: 8} |
| - if err := r.remove(); !errors.Is(err, ErrTooDeep) { |
| - t.Fatalf("err=%v", err) |
| + top := filepath.Join(dir, "top") |
| + files(t, filepath.Join(top, "a", "b", "c"), 300) |
| + files(t, filepath.Join(top, "a", "keep"), 1) |
| + moved := false |
| + r := treeRemover{dir: dir, top: "top", path: top, maxDepth: removeMaxDepth, afterBatch: func(stack []string) error { |
| + if len(stack) == 3 && !moved { |
| + moved = true |
| + if err := os.Rename(filepath.Join(top, "a"), filepath.Join(dir, "aside")); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return nil |
| + }} |
| + err := r.remove() |
| + if !moved { |
| + t.Fatal("the walk never reached c") |
| + } |
| + if err == nil || errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("err=%v: a tree moved mid-walk reads as removed", err) |
| } |
| if r.live != 0 { |
| t.Fatalf("%d directory descriptors left open", r.live) |
| } |
| - if _, err := os.Lstat(filepath.Join(dir, "top", "d", "d", "d", "d", "d", "d", "d", "d", "d")); err != nil { |
| - t.Fatal("refused removal did not leave the tree", err) |
| - } |
| - if err := RemoveAllBeneath(dir, "top"); err != nil { |
| + if _, err := os.Lstat(filepath.Join(dir, "aside", "keep")); err != nil { |
| t.Fatal(err) |
| } |
| - gone(t, filepath.Join(dir, "top")) |
| +} |
| + |
| +// A directory that goes between the listing and the walk's descent into |
| +// it is looked for again, not taken for the end of the tree: the rest of |
| +// top is still removed. |
| +func TestRemoveAllBeneathChildVanishedBeforeDescent(t *testing.T) { |
| + dir := t.TempDir() |
| + top := filepath.Join(dir, "top") |
| + files(t, top, 300) |
| + files(t, filepath.Join(top, "x"), 3) |
| + files(t, filepath.Join(top, "y"), 3) |
| + var vanished string |
| + r := treeRemover{dir: dir, top: "top", path: top, maxDepth: removeMaxDepth, beforeOpen: func(stack []string, child string) { |
| + if vanished == "" && len(stack) == 0 { |
| + vanished = child |
| + if err := os.Rename(filepath.Join(top, child), filepath.Join(dir, "aside")); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + }} |
| + if err := r.remove(); err != nil { |
| + t.Fatalf("err=%v", err) |
| + } |
| + if vanished == "" { |
| + t.Fatal("the walk never descended") |
| + } |
| + if r.live != 0 { |
| + t.Fatalf("%d directory descriptors left open", r.live) |
| + } |
| + gone(t, top) |
| } |