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
|
package main
import (
"context"
"fmt"
"sync/atomic"
"syscall"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"lukeshu.com/btrfs-tools/pkg/btrfs"
"lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem"
"lukeshu.com/btrfs-tools/pkg/util"
)
type dirState struct {
Dir *dir
}
type subvolumeFUSE struct {
fuseutil.NotImplementedFileSystem
lastHandle uint64
dirHandles util.SyncMap[uint64, *dirState]
}
func (sv *subvolumeFUSE) init() {}
func inodeItemToFUSE(itemBody btrfsitem.Inode) fuseops.InodeAttributes {
return fuseops.InodeAttributes{
Size: uint64(itemBody.Size),
Nlink: uint32(itemBody.NLink),
Mode: uint32(itemBody.Mode),
//RDev: itemBody.Rdev, // jacobsa/fuse doesn't expose rdev
Atime: itemBody.ATime.ToStd(),
Mtime: itemBody.MTime.ToStd(),
Ctime: itemBody.CTime.ToStd(),
//Crtime: itemBody.OTime,
Uid: uint32(itemBody.UID),
Gid: uint32(itemBody.GID),
}
}
func (sv *Subvolume) StatFS(_ context.Context, op *fuseops.StatFSOp) error {
// See linux.git/fs/btrfs/super.c:btrfs_statfs()
sb, err := sv.FS.Superblock()
if err != nil {
return err
}
op.IoSize = sb.Data.SectorSize
op.BlockSize = sb.Data.SectorSize
op.Blocks = sb.Data.TotalBytes / uint64(sb.Data.SectorSize) // TODO: adjust for RAID type
//op.BlocksFree = TODO
// btrfs doesn't have a fixed number of inodes
op.Inodes = 0
op.InodesFree = 0
// jacobsa/fuse doesn't expose namelen, instead hard-coding it
// to 255. Which is fine by us, because that's what it is for
// btrfs.
return nil
}
func (sv *Subvolume) LookUpInode(_ context.Context, op *fuseops.LookUpInodeOp) error {
if op.Parent == fuseops.RootInodeID {
parent, err := sv.getRootInode()
if err != nil {
return err
}
op.Parent = fuseops.InodeID(parent)
}
dir, err := sv.loadDir(btrfs.ObjID(op.Parent))
if err != nil {
return err
}
entry, ok := dir.ChildrenByName[op.Name]
if !ok {
return syscall.ENOENT
}
if entry.Location.ItemType != btrfsitem.INODE_ITEM_KEY {
return fmt.Errorf("child %q is not an inode: %w", op.Name, syscall.ENOSYS)
}
inodeItem, err := sv.loadInode(entry.Location.ObjectID)
if err != nil {
return err
}
op.Entry = fuseops.ChildInodeEntry{
Child: fuseops.InodeID(entry.Location.ObjectID),
Generation: fuseops.GenerationNumber(inodeItem.Sequence),
Attributes: inodeItemToFUSE(inodeItem),
}
return nil
}
func (sv *Subvolume) GetInodeAttributes(_ context.Context, op *fuseops.GetInodeAttributesOp) error {
if op.Inode == fuseops.RootInodeID {
inode, err := sv.getRootInode()
if err != nil {
return err
}
op.Inode = fuseops.InodeID(inode)
}
inodeItem, err := sv.loadInode(btrfs.ObjID(op.Inode))
if err != nil {
return err
}
op.Attributes = inodeItemToFUSE(inodeItem)
return nil
}
func (sv *Subvolume) OpenDir(_ context.Context, op *fuseops.OpenDirOp) error {
if op.Inode == fuseops.RootInodeID {
inode, err := sv.getRootInode()
if err != nil {
return err
}
op.Inode = fuseops.InodeID(inode)
}
dir, err := sv.loadDir(btrfs.ObjID(op.Inode))
if err != nil {
return err
}
handle := atomic.AddUint64(&sv.lastHandle, 1)
sv.dirHandles.Store(handle, &dirState{
Dir: dir,
})
op.Handle = fuseops.HandleID(handle)
return nil
}
func (sv *Subvolume) ReadDir(_ context.Context, op *fuseops.ReadDirOp) error {
state, ok := sv.dirHandles.Load(uint64(op.Handle))
if !ok {
return syscall.EBADF
}
indexes := util.SortedMapKeys(state.Dir.ChildrenByIndex)
origOffset := op.Offset
for _, index := range indexes {
if index < uint64(origOffset) {
continue
}
entry := state.Dir.ChildrenByIndex[index]
n := fuseutil.WriteDirent(op.Dst[op.BytesRead:], fuseutil.Dirent{
Offset: fuseops.DirOffset(index + 1),
Inode: fuseops.InodeID(entry.Location.ObjectID),
Name: string(entry.Name),
Type: map[btrfsitem.FileType]fuseutil.DirentType{
btrfsitem.FT_UNKNOWN: fuseutil.DT_Unknown,
btrfsitem.FT_REG_FILE: fuseutil.DT_File,
btrfsitem.FT_DIR: fuseutil.DT_Directory,
btrfsitem.FT_CHRDEV: fuseutil.DT_Char,
btrfsitem.FT_BLKDEV: fuseutil.DT_Block,
btrfsitem.FT_FIFO: fuseutil.DT_FIFO,
btrfsitem.FT_SOCK: fuseutil.DT_Socket,
btrfsitem.FT_SYMLINK: fuseutil.DT_Link,
}[entry.Type],
})
if n == 0 {
break
}
op.BytesRead += n
}
return nil
}
func (sv *Subvolume) ReleaseDirHandle(_ context.Context, op *fuseops.ReleaseDirHandleOp) error {
_, ok := sv.dirHandles.LoadAndDelete(uint64(op.Handle))
if !ok {
return syscall.EBADF
}
return nil
}
func (sv *Subvolume) OpenFile(_ context.Context, op *fuseops.OpenFileOp) error { return syscall.ENOSYS }
func (sv *Subvolume) ReadFile(_ context.Context, op *fuseops.ReadFileOp) error { return syscall.ENOSYS }
func (sv *Subvolume) ReleaseFileHandle(_ context.Context, op *fuseops.ReleaseFileHandleOp) error {
return syscall.ENOSYS
}
func (sv *Subvolume) ReadSymlink(_ context.Context, op *fuseops.ReadSymlinkOp) error {
return syscall.ENOSYS
}
func (sv *Subvolume) GetXattr(_ context.Context, op *fuseops.GetXattrOp) error { return syscall.ENOSYS }
func (sv *Subvolume) ListXattr(_ context.Context, op *fuseops.ListXattrOp) error {
return syscall.ENOSYS
}
func (sv *Subvolume) Destroy() {}
|