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
61 changes: 53 additions & 8 deletions notifier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fuse

import (
"sync"
"syscall"
"unsafe"

"github.com/jacobsa/fuse/fuseops"
Expand All @@ -13,12 +15,21 @@ import (
type Notifier struct {
inodeInvalidations chan invalidateInodeCommand
dentryInvalidations chan invalidateEntryCommand

mu sync.Mutex
// stopped is closed while no server wrapped with this Notifier serves.
//
// GUARDED_BY(mu)
stopped chan struct{}
}

func NewNotifier() *Notifier {
stopped := make(chan struct{})
close(stopped)
return &Notifier{
inodeInvalidations: make(chan invalidateInodeCommand),
dentryInvalidations: make(chan invalidateEntryCommand),
stopped: stopped,
}
}

Expand All @@ -45,10 +56,18 @@ type invalidateEntryCommand struct {
// InvalidateInode blocks until the kernel write completes, and returns the
// error from the kernel, if any. ENOSYS indicates that the kernel does not
// support inode invalidations.
//
// While no server wrapped with this Notifier serves, before its ServeOps
// starts or once it has returned, InvalidateInode sends nothing and returns
// ENOTCONN.
func (n *Notifier) InvalidateInode(inode fuseops.InodeID, offset, length int64) error {
done := make(chan error)
n.inodeInvalidations <- invalidateInodeCommand{inode, offset, length, done}
return <-done
select {
case n.inodeInvalidations <- invalidateInodeCommand{inode, offset, length, done}:
return <-done
case <-n.servingStopped():
return syscall.ENOTCONN
}
}

// InvalidateEntry notifies to the kernel to invalidate a dentry cache entry.
Expand All @@ -59,10 +78,25 @@ func (n *Notifier) InvalidateInode(inode fuseops.InodeID, offset, length int64)
// InvalidateEntry blocks until the kernel write completes, and returns the
// error from the kernel, if any. ENOSYS indicates that the kernel does not
// support dentry invalidations.
//
// While no server wrapped with this Notifier serves, before its ServeOps
// starts or once it has returned, InvalidateEntry sends nothing and returns
// ENOTCONN.
func (n *Notifier) InvalidateEntry(parent fuseops.InodeID, name string) error {
done := make(chan error)
n.dentryInvalidations <- invalidateEntryCommand{parent, name, done}
return <-done
select {
case n.dentryInvalidations <- invalidateEntryCommand{parent, name, done}:
return <-done
case <-n.servingStopped():
return syscall.ENOTCONN
}
}

// LOCKS_EXCLUDED(n.mu)
func (n *Notifier) servingStopped() <-chan struct{} {
n.mu.Lock()
defer n.mu.Unlock()
return n.stopped
}

func serviceInodeInvalidation(c *Connection, inode fuseops.InodeID, offset, length int64) error {
Expand Down Expand Up @@ -119,12 +153,23 @@ type notifierServer struct {
s Server
}

// ServeOps sends the Notifier's notifications while s serves. Once s
// returns, it finishes the notification in progress, if any, before it
// returns, so that the connection is still open for it.
func (s *notifierServer) ServeOps(c *Connection) {
terminate := make(chan struct{})

go s.n.notify(c, terminate)
stopped := make(chan struct{})
s.n.mu.Lock()
s.n.stopped = stopped
s.n.mu.Unlock()

notified := make(chan struct{})
go func() {
defer close(notified)
s.n.notify(c, stopped)
}()
s.s.ServeOps(c)
close(terminate)
close(stopped)
<-notified
}

func NewServerWithNotifier(n *Notifier, s Server) Server {
Expand Down
102 changes: 102 additions & 0 deletions notifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2026 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fuse

import (
"errors"
"io"
"os"
"syscall"
"testing"
"unsafe"

"github.com/jacobsa/fuse/internal/fusekernel"
)

// blockingServer is a Server whose ServeOps closes started, then returns once
// release is closed. It never reads the connection.
type blockingServer struct {
started chan struct{}
release chan struct{}
}

func newBlockingServer() blockingServer {
return blockingServer{started: make(chan struct{}), release: make(chan struct{})}
}

func (s blockingServer) ServeOps(*Connection) {
close(s.started)
<-s.release
}

func checkNotifierENOTCONN(t *testing.T, n *Notifier, when string) {
t.Helper()
if err := n.InvalidateInode(1, 0, 0); !errors.Is(err, syscall.ENOTCONN) {
t.Errorf("InvalidateInode %s = %v, want ENOTCONN", when, err)
}
if err := n.InvalidateEntry(1, "x"); !errors.Is(err, syscall.ENOTCONN) {
t.Errorf("InvalidateEntry %s = %v, want ENOTCONN", when, err)
}
}

func TestNotifierReturnsENOTCONNWhileNothingServes(t *testing.T) {
n := NewNotifier()
checkNotifierENOTCONN(t, n, "before ServeOps")

s := newBlockingServer()
close(s.release)
NewServerWithNotifier(n, s).ServeOps(nil)
checkNotifierENOTCONN(t, n, "after ServeOps returned")
}

// While the wrapped server serves, the Notifier writes each notification to
// the connection and returns the write's result.
func TestNotifierSendsWhileServing(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Pipe: %v", err)
}
defer r.Close()
defer w.Close()

n := NewNotifier()
s := newBlockingServer()
served := make(chan struct{})
go func() {
NewServerWithNotifier(n, s).ServeOps(&Connection{dev: w})
close(served)
}()
<-s.started

if err := n.InvalidateInode(7, 4096, 8192); err != nil {
t.Fatalf("InvalidateInode: %v", err)
}
var msg struct {
Header fusekernel.OutHeader
Body fusekernel.NotifyInvalInodeOut
}
if _, err := io.ReadFull(r, unsafe.Slice((*byte)(unsafe.Pointer(&msg)), unsafe.Sizeof(msg))); err != nil {
t.Fatalf("reading the notification: %v", err)
}
if msg.Header.Error != fusekernel.NotifyCodeInvalInode || msg.Header.Unique != 0 {
t.Errorf("notification header = %+v, want error %d and unique 0", msg.Header, fusekernel.NotifyCodeInvalInode)
}
if want := (fusekernel.NotifyInvalInodeOut{Ino: 7, Off: 4096, Len: 8192}); msg.Body != want {
t.Errorf("notification body = %+v, want %+v", msg.Body, want)
}

close(s.release)
<-served
}