Skip to content

Commit 3a29c55

Browse files
committed
Add notification messages framework
Now we only impl the NotifyInvalInode,NotifyInvalEntry, NotifyDelete. Notify funcs allow the filesystem to invalidate VFS caches. The statement that 'In a networked fuse file-system, the local fuse instance may receive changes from a remote system that do not pass through the local kernel vfs. When those changes are made the local fuse instance must notify the local kernel of the change so as to keep the local kernel's cache in sync. This is accomplished via notify delete or notify invalidate. In other words, these notifications only apply if fuse file-system makes changes to the file-system that are not initiated by the local kernel: changes initiated by the local kernel must not involve notify delete or notify invalidate (will deadlock), and are not required.' is quoted from Jhon Muir. About fuse notify func history ,please ref link http://lkml.iu.edu/hypermail/linux/kernel/0906.0/01120.html .
1 parent 88e3bc5 commit 3a29c55

8 files changed

Lines changed: 321 additions & 2 deletions

File tree

connection.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,19 @@ func (c *Connection) beginOp(
241241
// should not record any state keyed on their ID.
242242
//
243243
// Cf. https://github.com/osxfuse/osxfuse/issues/208
244-
if opCode != fusekernel.OpForget {
244+
245+
// Special case: For notify ops ,it is issued by the userspace filesystem to
246+
// fuse kernel. The ops's kernel opCode is 0 in kernel' viewpoint or kernel not
247+
// care whether what the iopcode is .But the ops have the notifycode from 1
248+
// to 6 now. For adapt the notfiy ops to the BeginOP ,we fake an opcode as
249+
// notfiycode + 100. Now opcode range of ops from kernel including origin
250+
// from userspace and just from kernel is < 100. The notify ops's opcode
251+
// range is [101-106].
252+
// Then we can process all ops consistently.
253+
// Cf. func (c *Connection) SetNotifyContext(op interface{})
254+
// (context.Context, error) {
255+
256+
if opCode != fusekernel.OpForget && opCode < 100 {
245257
var cancel func()
246258
ctx, cancel = context.WithCancel(ctx)
247259
c.recordCancelFunc(fuseID, cancel)
@@ -411,6 +423,38 @@ func (c *Connection) ReadOp() (ctx context.Context, op interface{}, err error) {
411423
}
412424
}
413425

426+
// The SetNotifyContext set context according with value of the notify op's
427+
// fuseops.Notify*Op.
428+
func (c *Connection) SetNotifyContext(op interface{}) (context.Context, error) {
429+
430+
outMsg := c.getOutMessage()
431+
432+
err := c.buildNotify(outMsg, op)
433+
if err != nil {
434+
return nil, err
435+
}
436+
437+
ctx := context.Background()
438+
439+
switch op.(type) {
440+
case *fuseops.NotifyInvalInodeOp:
441+
ctx = c.beginOp(100+uint32(fusekernel.NotifyCodeInvalInode), 0)
442+
443+
case *fuseops.NotifyInvalEntryOp:
444+
ctx = c.beginOp(100+uint32(fusekernel.NotifyCodeInvalEntry), 0)
445+
446+
case *fuseops.NotifyDeleteOp:
447+
ctx = c.beginOp(100+uint32(fusekernel.NotifyCodeDelete), 0)
448+
449+
default:
450+
panic(fmt.Sprintf("Unexpected op: %#v", op))
451+
}
452+
453+
ctx = context.WithValue(ctx, contextKey, opState{nil, outMsg, op})
454+
return ctx, nil
455+
456+
}
457+
414458
// Skip errors that happen as a matter of course, since they spook users.
415459
func (c *Connection) shouldLogError(
416460
op interface{},
@@ -498,6 +542,29 @@ func (c *Connection) Reply(ctx context.Context, opErr error) {
498542
}
499543
}
500544

545+
// The NotifyKernel is same as Reply func of Connection.But the diff is
546+
// that the func only send to kernel.
547+
func (c *Connection) NotifyKernel(ctx context.Context) {
548+
549+
// we should get outmsg from context
550+
var key interface{} = contextKey
551+
foo := ctx.Value(key)
552+
state, ok := foo.(opState)
553+
if !ok {
554+
panic(fmt.Sprintf("Reply called with invalid context: %#v", ctx))
555+
}
556+
557+
outMsg := state.outMsg
558+
defer c.putOutMessage(outMsg)
559+
560+
c.debugLogger.Println("dev fd is:unique:notifycode ", c.dev.Fd(), outMsg.OutHeader().Unique, outMsg.OutHeader().Error)
561+
err := c.writeMessage(outMsg.Bytes())
562+
if err != nil && c.errorLogger != nil {
563+
c.errorLogger.Printf("writeMessage: %v %v", err, outMsg.Bytes())
564+
}
565+
566+
}
567+
501568
// Close the connection. Must not be called until operations that were read
502569
// from the connection have been responded to.
503570
func (c *Connection) close() (err error) {

conversions.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,59 @@ func (c *Connection) kernelResponse(
597597
return
598598
}
599599

600+
func (c *Connection) buildNotify(
601+
m *buffer.OutMessage,
602+
op interface{}) error {
603+
604+
h := m.OutHeader()
605+
h.Unique = 0
606+
// Create the appropriate output message
607+
switch o := op.(type) {
608+
case *fuseops.NotifyInvalInodeOp:
609+
h.Error = fusekernel.NotifyCodeInvalInode
610+
size := fusekernel.NotifyInvalInodeOutSize
611+
out := (*fusekernel.NotifyInvalInodeOut)(m.Grow(size))
612+
out.Ino = uint64(o.Ino)
613+
out.Off = int64(o.Off)
614+
out.Len = int64(o.Len)
615+
616+
case *fuseops.NotifyInvalEntryOp:
617+
err := checkName(o.Name)
618+
if err != nil {
619+
return err
620+
}
621+
h.Error = fusekernel.NotifyCodeInvalEntry
622+
size := fusekernel.NotifyInvalEntryOutSize
623+
out := (*fusekernel.NotifyInvalEntryOut)(m.Grow(size))
624+
out.Parent = uint64(o.Parent)
625+
out.Namelen = uint32(len(o.Name))
626+
m.Append([]byte(o.Name))
627+
b := []byte{'\x00'}
628+
m.Append(b)
629+
630+
case *fuseops.NotifyDeleteOp:
631+
err := checkName(o.Name)
632+
if err != nil {
633+
return err
634+
}
635+
h.Error = fusekernel.NotifyCodeDelete
636+
size := fusekernel.NotifyDeleteOutSize
637+
out := (*fusekernel.NotifyDeleteOut)(m.Grow(size))
638+
out.Parent = uint64(o.Parent)
639+
out.Child = uint64(o.Child)
640+
out.Namelen = uint32(len(o.Name))
641+
m.Append([]byte(o.Name))
642+
b := []byte{'\x00'}
643+
m.Append(b)
644+
645+
default:
646+
return errors.New("unexpectedop")
647+
}
648+
h.Len = uint32(m.Len())
649+
650+
return nil
651+
}
652+
600653
// Like kernelResponse, but assumes the user replied with a nil error to the
601654
// op.
602655
func (c *Connection) kernelResponseForOp(
@@ -888,3 +941,11 @@ func writeXattrSize(m *buffer.OutMessage, size uint32) {
888941
out := (*fusekernel.GetxattrOut)(m.Grow(int(unsafe.Sizeof(fusekernel.GetxattrOut{}))))
889942
out.Size = size
890943
}
944+
func checkName(name string) error {
945+
const maxUint32 = ^uint32(0)
946+
if uint64(len(name)) > uint64(maxUint32) {
947+
// very unlikely, but we don't want to silently truncate
948+
return syscall.ENAMETOOLONG
949+
}
950+
return nil
951+
}

fuseops/ops.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,62 @@ type SetXattrOp struct {
845845
// simply replace the value if the attribute exists.
846846
Flags uint32
847847
}
848+
849+
// Notify IO readiness event
850+
type NotifyPollOp struct {
851+
}
852+
853+
// Notify to invalidate cache for an inode.
854+
// Added in FUSE protocol version 7.12. If the kernel does not support this
855+
// (or a newer) version, the op will return -ENOSYS and do nothing
856+
type NotifyInvalInodeOp struct {
857+
// inode to invalidatej
858+
Ino InodeID
859+
860+
// the offset in the inode where to start invalidating or negative to invalidate attributes only
861+
Off int64
862+
863+
// the amount of cache to invalidate or 0 for all
864+
Len int64
865+
}
866+
867+
// Notify to invalidate parent attributes and the dentry matching parent/name
868+
// Added in FUSE protocol version 7.12. If the kernel does not support this
869+
// (or a newer) version, the op will return -ENOSYS and do nothing
870+
type NotifyInvalEntryOp struct {
871+
// the inode number
872+
Parent InodeID
873+
874+
// the child entry file name
875+
Name string
876+
}
877+
878+
// Store data to the kernel buffers
879+
// Cf:http://libfuse.github.io/doxygen/fuse__lowlevel_8h.html#a9cb974af9745294ff446d11cba2422f1
880+
type NotifyStoreOp struct {
881+
}
882+
883+
// Retrieve data from the kernel buffers
884+
type NotifyRetrieveOp struct {
885+
}
886+
887+
// This op behaves like NotifyInvalEntryOp with the following additional
888+
// effect (at least as of Linux kernel 4.8):
889+
890+
// If the provided child inode matches the inode that is currently
891+
// associated with the cached dentry, and if there are any inotify
892+
// watches registered for the dentry, then the watchers are informed
893+
// that the dentry has been deleted.
894+
// Added in FUSE protocol version 7.18. If the kernel does not
895+
// support this (or a newer) version, op will return -ENOSYS and do nothing.
896+
897+
type NotifyDeleteOp struct {
898+
// the inode number
899+
Parent InodeID
900+
901+
// the child entry's inode
902+
Child InodeID
903+
904+
// the child entry file name
905+
Name string
906+
}

fuseutil/file_system.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func NewFileSystemServer(fs FileSystem) fuse.Server {
9090
type fileSystemServer struct {
9191
fs FileSystem
9292
opsInFlight sync.WaitGroup
93+
//use set/get to use mfs not Mfs
94+
Mfs *fuse.MountedFileSystem
9395
}
9496

9597
func (s *fileSystemServer) ServeOps(c *fuse.Connection) {
@@ -123,6 +125,59 @@ func (s *fileSystemServer) ServeOps(c *fuse.Connection) {
123125
}
124126
}
125127

128+
func (s *fileSystemServer) InvalidateEntry(parent fuseops.InodeID, name string) error {
129+
c := s.GetMfs().Conn.(*fuse.Connection)
130+
131+
op := &fuseops.NotifyInvalEntryOp{
132+
Parent: fuseops.InodeID(parent),
133+
Name: string(name),
134+
}
135+
ctx, _ := c.SetNotifyContext(op)
136+
s.opsInFlight.Add(1)
137+
go func(ctx context.Context) {
138+
defer s.opsInFlight.Done()
139+
c.NotifyKernel(ctx)
140+
}(ctx)
141+
return nil
142+
}
143+
func (s *fileSystemServer) NotifyDelete(
144+
parent fuseops.InodeID,
145+
child fuseops.InodeID,
146+
name string) error {
147+
c := s.GetMfs().Conn.(*fuse.Connection)
148+
op := &fuseops.NotifyDeleteOp{
149+
Parent: fuseops.InodeID(parent),
150+
Child: fuseops.InodeID(child),
151+
Name: string(name),
152+
}
153+
ctx, _ := c.SetNotifyContext(op)
154+
s.opsInFlight.Add(1)
155+
go func(ctx context.Context) {
156+
defer s.opsInFlight.Done()
157+
c.NotifyKernel(ctx)
158+
}(ctx)
159+
return nil
160+
161+
}
162+
func (s *fileSystemServer) InvalidateInode(
163+
ino fuseops.InodeID,
164+
off int64,
165+
len int64) error {
166+
c := s.GetMfs().Conn.(*fuse.Connection)
167+
op := &fuseops.NotifyInvalInodeOp{
168+
Ino: fuseops.InodeID(ino),
169+
Off: off,
170+
Len: len,
171+
}
172+
ctx, _ := c.SetNotifyContext(op)
173+
s.opsInFlight.Add(1)
174+
go func(ctx context.Context) {
175+
defer s.opsInFlight.Done()
176+
c.NotifyKernel(ctx)
177+
}(ctx)
178+
return nil
179+
180+
}
126181
func (s *fileSystemServer) handleOp(
127182
c *fuse.Connection,
128183
ctx context.Context,
@@ -216,3 +271,12 @@ func (s *fileSystemServer) handleOp(
216271

217272
c.Reply(ctx, err)
218273
}
274+
275+
func (s *fileSystemServer) GetMfs() *fuse.MountedFileSystem {
276+
return s.Mfs
277+
278+
}
279+
func (s *fileSystemServer) SetMfs(mfs *fuse.MountedFileSystem) {
280+
s.Mfs = mfs
281+
282+
}

internal/fusekernel/fuse_kernel.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,9 @@ const (
756756
NotifyCodePoll int32 = 1
757757
NotifyCodeInvalInode int32 = 2
758758
NotifyCodeInvalEntry int32 = 3
759+
NotifyCodeStore int32 = 4
760+
NotifyCodeRetrieve int32 = 5
761+
NotifyCodeDelete int32 = 6
759762
)
760763

761764
type NotifyInvalInodeOut struct {
@@ -764,8 +767,46 @@ type NotifyInvalInodeOut struct {
764767
Len int64
765768
}
766769

770+
const NotifyInvalInodeOutSize = int(unsafe.Sizeof(NotifyInvalInodeOut{}))
771+
767772
type NotifyInvalEntryOut struct {
768773
Parent uint64
769774
Namelen uint32
770775
padding uint32
771776
}
777+
778+
const NotifyInvalEntryOutSize = int(unsafe.Sizeof(NotifyInvalEntryOut{}))
779+
780+
type NotifyDeleteOut struct {
781+
Parent uint64
782+
Child uint64
783+
Namelen uint32
784+
padding uint32
785+
}
786+
787+
const NotifyDeleteOutSize = int(unsafe.Sizeof(NotifyDeleteOut{}))
788+
789+
type NotifyStoreOut struct {
790+
Nodeid uint64
791+
Offset uint64
792+
Size uint32
793+
padding uint32
794+
}
795+
796+
type NotifyRetrieveOut struct {
797+
NotifyUnique uint64
798+
Nodeid uint64
799+
Offset uint64
800+
Size uint32
801+
padding uint32
802+
}
803+
804+
/* Matches the size of fuse_write_in */
805+
type NotifyRetrieveIn struct {
806+
Dummy1 uint64
807+
Offset uint64
808+
Size uint32
809+
Dummy2 uint32
810+
Dummy3 uint64
811+
Dummy4 uint64
812+
}

mount.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"os"
2020

21+
"github.com/jacobsa/fuse/fuseops"
2122
"golang.org/x/net/context"
2223
)
2324

@@ -28,6 +29,11 @@ type Server interface {
2829
// until all operations have been responded to. Must not be called more than
2930
// once.
3031
ServeOps(*Connection)
32+
InvalidateEntry(fuseops.InodeID, string) error
33+
InvalidateInode(fuseops.InodeID, int64, int64) error
34+
NotifyDelete(fuseops.InodeID, fuseops.InodeID, string) error
35+
SetMfs(*MountedFileSystem)
36+
GetMfs() *MountedFileSystem
3137
}
3238

3339
// Mount attempts to mount a file system on the given directory, using the
@@ -85,7 +91,10 @@ func Mount(
8591
return
8692
}
8793

88-
// Serve the connection in the background. When done, set the join status.
94+
mfs.Conn = connection
95+
server.SetMfs(mfs)
96+
97+
// Serve the connection in the background. When done, set the join status
8998
go func() {
9099
server.ServeOps(connection)
91100
mfs.joinStatus = connection.close()

0 commit comments

Comments
 (0)