1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package fuse
// This file contains the internal logic of the
// FileSystemConnector. The functions for satisfying the raw interface
// are in fsops.go
import (
"log"
"path/filepath"
"strings"
"sync/atomic"
"time"
"unsafe"
"github.com/hanwen/go-fuse/raw"
)
// Tests should set to true.
var paranoia = false
// FilesystemConnector is a raw FUSE filesystem that manages
// in-process mounts and inodes. Its job is twofold:
//
// * It translates between the raw kernel interface (padded structs of
// int32 and int64) and the more abstract Go-ish NodeFileSystem
// interface.
//
// * It manages mounting and unmounting of NodeFileSystems into the
// directory hierarchy
//
// To achieve this, the connector only needs a pointer to the root
// node.
type FileSystemConnector struct {
DefaultRawFileSystem
Debug bool
// Callbacks for talking back to the kernel.
fsInit RawFsInit
nodeFs NodeFileSystem
// Translate between uint64 handles and *Inode.
inodeMap HandleMap
// The root of the FUSE file system.
rootNode *Inode
// Used as the generation inodes.
generation uint64
}
func NewFileSystemOptions() *FileSystemOptions {
return &FileSystemOptions{
NegativeTimeout: 0,
AttrTimeout: time.Second,
EntryTimeout: time.Second,
Owner: CurrentOwner(),
}
}
func NewFileSystemConnector(nodeFs NodeFileSystem, opts *FileSystemOptions) (c *FileSystemConnector) {
c = new(FileSystemConnector)
if opts == nil {
opts = NewFileSystemOptions()
}
c.nodeFs = nodeFs
c.inodeMap = NewHandleMap(opts.PortableInodes)
c.rootNode = newInode(true, nodeFs.Root())
// Make sure we don't reuse generation numbers.
c.generation = uint64(time.Now().UnixNano())
c.verify()
c.MountRoot(nodeFs, opts)
// FUSE does not issue a LOOKUP for 1 (obviously), but it does
// issue a forget. This lookupUpdate is to make the counts match.
c.lookupUpdate(c.rootNode)
return c
}
func (c *FileSystemConnector) nextGeneration() uint64 {
return atomic.AddUint64(&c.generation, 1)
}
// This verifies invariants of the data structure. This routine
// acquires tree locks as it walks the inode tree.
func (c *FileSystemConnector) verify() {
if !paranoia {
return
}
root := c.rootNode
root.verify(c.rootNode.mountPoint)
}
// Generate EntryOut and increase the lookup count for an inode.
func (c *FileSystemConnector) childLookup(out *raw.EntryOut, fsi FsNode) {
n := fsi.Inode()
fsi.GetAttr((*Attr)(&out.Attr), nil, nil)
n.mount.fillEntry(out)
out.Ino = c.lookupUpdate(n)
out.NodeId = out.Ino
if out.Nlink == 0 {
// With Nlink == 0, newer kernels will refuse link
// operations.
out.Nlink = 1
}
}
func (c *FileSystemConnector) toInode(nodeid uint64) *Inode {
if nodeid == raw.FUSE_ROOT_ID {
return c.rootNode
}
i := (*Inode)(unsafe.Pointer(c.inodeMap.Decode(nodeid)))
return i
}
// Must run outside treeLock. Returns the nodeId.
func (c *FileSystemConnector) lookupUpdate(node *Inode) (id uint64) {
id = c.inodeMap.Register(&node.handled)
c.verify()
return id
}
// Must run outside treeLock.
func (c *FileSystemConnector) forgetUpdate(nodeID uint64, forgetCount int) {
if nodeID == raw.FUSE_ROOT_ID {
c.nodeFs.OnUnmount()
// We never got a lookup for root, so don't try to
// forget root.
return
}
if forgotten, handled := c.inodeMap.Forget(nodeID, forgetCount); forgotten {
node := (*Inode)(unsafe.Pointer(handled))
node.mount.treeLock.Lock()
c.recursiveConsiderDropInode(node)
node.mount.treeLock.Unlock()
}
// TODO - try to drop children even forget was not successful.
c.verify()
}
// InodeCount returns the number of inodes registered with the kernel.
func (c *FileSystemConnector) InodeHandleCount() int {
return c.inodeMap.Count()
}
// Must hold treeLock.
func (c *FileSystemConnector) recursiveConsiderDropInode(n *Inode) (drop bool) {
delChildren := []string{}
for k, v := range n.children {
// Only consider children from the same mount, or
// already unmounted mountpoints.
if v.mountPoint == nil && c.recursiveConsiderDropInode(v) {
delChildren = append(delChildren, k)
}
}
for _, k := range delChildren {
ch := n.rmChild(k)
if ch == nil {
log.Panicf("trying to del child %q, but not present", k)
}
ch.fsInode.OnForget()
}
if len(n.children) > 0 || !n.FsNode().Deletable() {
return false
}
if n == c.rootNode || n.mountPoint != nil {
return false
}
n.openFilesMutex.Lock()
ok := len(n.openFiles) == 0
n.openFilesMutex.Unlock()
return ok
}
// Finds a node within the currently known inodes, returns the last
// known node and the remaining unknown path components. If parent is
// nil, start from FUSE mountpoint.
func (c *FileSystemConnector) Node(parent *Inode, fullPath string) (*Inode, []string) {
if parent == nil {
parent = c.rootNode
}
if fullPath == "" {
return parent, nil
}
sep := string(filepath.Separator)
fullPath = strings.TrimLeft(filepath.Clean(fullPath), sep)
comps := strings.Split(fullPath, sep)
node := parent
if node.mountPoint == nil {
node.mount.treeLock.RLock()
defer node.mount.treeLock.RUnlock()
}
for i, component := range comps {
if len(component) == 0 {
continue
}
if node.mountPoint != nil {
node.mount.treeLock.RLock()
defer node.mount.treeLock.RUnlock()
}
next := node.children[component]
if next == nil {
return node, comps[i:]
}
node = next
}
return node, nil
}
func (c *FileSystemConnector) LookupNode(parent *Inode, path string) *Inode {
if path == "" {
return parent
}
components := strings.Split(path, "/")
for _, r := range components {
var a Attr
child, _ := c.internalLookup(&a, parent, r, nil)
if child == nil {
return nil
}
parent = child
}
return parent
}
////////////////////////////////////////////////////////////////
func (c *FileSystemConnector) MountRoot(nodeFs NodeFileSystem, opts *FileSystemOptions) {
c.rootNode.mountFs(nodeFs, opts)
c.rootNode.mount.connector = c
nodeFs.OnMount(c)
c.verify()
}
// Mount() generates a synthetic directory node, and mounts the file
// system there. If opts is nil, the mount options of the root file
// system are inherited. The encompassing filesystem should pretend
// the mount point does not exist. If it does, it will generate an
// Inode with the same, which will cause Mount() to return EBUSY.
//
// Return values:
//
// ENOENT: the directory containing the mount point does not exist.
//
// EBUSY: the intended mount point already exists.
func (c *FileSystemConnector) Mount(parent *Inode, name string, nodeFs NodeFileSystem, opts *FileSystemOptions) Status {
defer c.verify()
parent.mount.treeLock.Lock()
defer parent.mount.treeLock.Unlock()
node := parent.children[name]
if node != nil {
return EBUSY
}
node = newInode(true, nodeFs.Root())
if opts == nil {
opts = c.rootNode.mountPoint.options
}
node.mountFs(nodeFs, opts)
node.mount.connector = c
parent.addChild(name, node)
node.mountPoint.parentInode = parent
if c.Debug {
log.Println("Mount: ", nodeFs, "on subdir", name,
"parent", c.inodeMap.Handle(&parent.handled))
}
nodeFs.OnMount(c)
return OK
}
// Unmount() tries to unmount the given inode.
//
// Returns the following error codes:
//
// EINVAL: path does not exist, or is not a mount point.
//
// EBUSY: there are open files, or submounts below this node.
func (c *FileSystemConnector) Unmount(node *Inode) Status {
// TODO - racy.
if node.mountPoint == nil {
log.Println("not a mountpoint:", c.inodeMap.Handle(&node.handled))
return EINVAL
}
nodeId := c.inodeMap.Handle(&node.handled)
// Must lock parent to update tree structure.
parentNode := node.mountPoint.parentInode
parentNode.mount.treeLock.Lock()
defer parentNode.mount.treeLock.Unlock()
mount := node.mountPoint
name := node.mountPoint.mountName()
if mount.openFiles.Count() > 0 {
return EBUSY
}
node.mount.treeLock.Lock()
defer node.mount.treeLock.Unlock()
if mount.mountInode != node {
log.Panicf("got two different mount inodes %v vs %v",
c.inodeMap.Handle(&mount.mountInode.handled),
c.inodeMap.Handle(&node.handled))
}
if !node.canUnmount() {
return EBUSY
}
delete(parentNode.children, name)
mount.fs.OnUnmount()
parentId := c.inodeMap.Handle(&parentNode.handled)
if parentNode == c.rootNode {
// TODO - test coverage. Currently covered by zipfs/multizip_test.go
parentId = raw.FUSE_ROOT_ID
}
// We have to wait until the kernel has forgotten the
// mountpoint, so the write to node.mountPoint is no longer
// racy.
code := c.fsInit.DeleteNotify(parentId, c.inodeMap.Handle(&node.handled), name)
if code.Ok() {
mount.treeLock.Unlock()
parentNode.mount.treeLock.Unlock()
delay := 100 * time.Microsecond
for {
// This operation is rare, so we kludge it to avoid
// contention.
time.Sleep(delay)
delay = delay*2 + 1
if !c.inodeMap.Has(nodeId) {
break
}
}
parentNode.mount.treeLock.Lock()
mount.treeLock.Lock()
}
mount.mountInode = nil
node.mountPoint = nil
return OK
}
func (c *FileSystemConnector) FileNotify(node *Inode, off int64, length int64) Status {
var nId uint64
if node == c.rootNode {
nId = raw.FUSE_ROOT_ID
} else {
nId = c.inodeMap.Handle(&node.handled)
}
if nId == 0 {
return OK
}
out := raw.NotifyInvalInodeOut{
Length: length,
Off: off,
Ino: nId,
}
return c.fsInit.InodeNotify(&out)
}
func (c *FileSystemConnector) EntryNotify(node *Inode, name string) Status {
var nId uint64
if node == c.rootNode {
nId = raw.FUSE_ROOT_ID
} else {
nId = c.inodeMap.Handle(&node.handled)
}
if nId == 0 {
return OK
}
return c.fsInit.EntryNotify(nId, name)
}
func (c *FileSystemConnector) DeleteNotify(dir *Inode, child *Inode, name string) Status {
var nId uint64
if dir == c.rootNode {
nId = raw.FUSE_ROOT_ID
} else {
nId = c.inodeMap.Handle(&dir.handled)
}
if nId == 0 {
return OK
}
chId := c.inodeMap.Handle(&child.handled)
return c.fsInit.DeleteNotify(nId, chId, name)
}