Skip to content

Commit

Permalink
Add inode.Initialized() for checking if an inode has been initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
iain-macdonald committed Jun 25, 2024
1 parent fc0fdbf commit 8de7154
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
33 changes: 33 additions & 0 deletions fs/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,36 @@ func (fn *testIno1) Lookup(ctx context.Context, name string, out *fuse.EntryOut)
child := fn.NewInode(ctx, &testIno1{}, stable)
return child, 0
}

func TestForgottenIno(t *testing.T) {
rootNode := testForgottenIno{}
testMount(t, &rootNode, nil)
if rootNode.Initialized() && rootNode.Forgotten() {
t.Error("newly-created inode should not be forgotten")
}
ino, errno := rootNode.Lookup(context.TODO(), "test", nil)
if errno != syscall.F_OK {
t.Error("error creating new inode")
}
if ino.Initialized() && ino.Forgotten() {
t.Error("newly-created inode should not be forgotten")
}
ok := rootNode.AddChild("test", ino, false)
if !ok {
t.Error("error adding child inode to fs")
}
if ino.Initialized() && ino.Forgotten() {
t.Error("newly-mounted inode should not be forgotten")
}
}

type testForgottenIno struct {
Inode
}

func (n *testForgottenIno) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) {
childNode := &testForgottenIno{}
stable := StableAttr{Mode: syscall.S_IFREG, Ino: 999}
child := n.NewInode(ctx, childNode, stable)
return child, syscall.F_OK
}
8 changes: 8 additions & 0 deletions fs/inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ func unlockNodes(ns ...*Inode) {
}
}

// Initialized returns true if this inode has been initialized and added to the
// filesystem tree.
func (n *Inode) Initialized() bool {
n.mu.Lock()
defer n.mu.Unlock()
return n.changeCounter > 0
}

// Forgotten returns true if the kernel holds no references to this
// inode. This can be used for background cleanup tasks, since the
// kernel has no way of reviving forgotten nodes by its own
Expand Down

0 comments on commit 8de7154

Please sign in to comment.