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
|
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"log"
"sync"
"unsafe"
"github.com/hanwen/go-fuse/fuse"
)
// openedFile stores either an open dir or an open file.
type openedFile struct {
handled
WithFlags
dir *connectorDir
}
type fileSystemMount struct {
// Node that we were mounted on.
mountInode *Inode
// Parent to the mountInode.
parentInode *Inode
// Options for the mount.
options *Options
// Protects the "children" and "parents" hashmaps of the inodes
// within the mount.
// treeLock should be acquired before openFilesLock.
//
// If multiple treeLocks must be acquired, the treeLocks
// closer to the root must be acquired first.
treeLock sync.RWMutex
// Manage filehandles of open files.
openFiles handleMap
Debug bool
connector *FileSystemConnector
}
// Must called with lock for parent held.
func (m *fileSystemMount) mountName() string {
for k, v := range m.parentInode.children {
if m.mountInode == v {
return k
}
}
panic("not found")
}
func (m *fileSystemMount) setOwner(attr *fuse.Attr) {
if m.options.Owner != nil {
attr.Owner = *(*fuse.Owner)(m.options.Owner)
}
}
func (m *fileSystemMount) fillEntry(out *fuse.EntryOut) {
splitDuration(m.options.EntryTimeout, &out.EntryValid, &out.EntryValidNsec)
splitDuration(m.options.AttrTimeout, &out.AttrValid, &out.AttrValidNsec)
m.setOwner(&out.Attr)
if out.Mode&fuse.S_IFDIR == 0 && out.Nlink == 0 {
out.Nlink = 1
}
}
func (m *fileSystemMount) fillAttr(out *fuse.AttrOut, nodeId uint64) {
splitDuration(m.options.AttrTimeout, &out.AttrValid, &out.AttrValidNsec)
m.setOwner(&out.Attr)
if out.Ino == 0 {
out.Ino = nodeId
}
}
func (m *fileSystemMount) getOpenedFile(h uint64) *openedFile {
var b *openedFile
if h != 0 {
b = (*openedFile)(unsafe.Pointer(m.openFiles.Decode(h)))
}
if b != nil && m.connector.debug && b.WithFlags.Description != "" {
log.Printf("File %d = %q", h, b.WithFlags.Description)
}
return b
}
func (m *fileSystemMount) unregisterFileHandle(handle uint64, node *Inode) *openedFile {
_, obj := m.openFiles.Forget(handle, 1)
opened := (*openedFile)(unsafe.Pointer(obj))
node.openFilesMutex.Lock()
idx := -1
for i, v := range node.openFiles {
if v == opened {
idx = i
break
}
}
l := len(node.openFiles)
if idx == l-1 {
node.openFiles[idx] = nil
} else {
node.openFiles[idx] = node.openFiles[l-1]
}
node.openFiles = node.openFiles[:l-1]
node.openFilesMutex.Unlock()
return opened
}
func (m *fileSystemMount) registerFileHandle(node *Inode, dir *connectorDir, f File, flags uint32) (uint64, *openedFile) {
node.openFilesMutex.Lock()
b := &openedFile{
dir: dir,
WithFlags: WithFlags{
File: f,
OpenFlags: flags,
},
}
for {
withFlags, ok := f.(*WithFlags)
if !ok {
break
}
b.WithFlags.File = withFlags.File
b.WithFlags.FuseFlags |= withFlags.FuseFlags
b.WithFlags.Description += withFlags.Description
f = withFlags.File
}
if b.WithFlags.File != nil {
b.WithFlags.File.SetInode(node)
}
node.openFiles = append(node.openFiles, b)
handle, _ := m.openFiles.Register(&b.handled)
node.openFilesMutex.Unlock()
return handle, b
}
// Creates a return entry for a non-existent path.
func (m *fileSystemMount) negativeEntry(out *fuse.EntryOut) bool {
if m.options.NegativeTimeout > 0.0 {
out.NodeId = 0
splitDuration(m.options.NegativeTimeout, &out.EntryValid, &out.EntryValidNsec)
return true
}
return false
}
|