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
|
package main
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"sync"
"sync/atomic"
"syscall"
"github.com/datawire/dlib/dgroup"
"github.com/jacobsa/fuse"
"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/linux"
"lukeshu.com/btrfs-tools/pkg/util"
)
type dirState struct {
Dir *btrfs.Dir
}
type fileState struct {
File *btrfs.File
}
type Subvolume struct {
btrfs.Subvolume
DeviceName string
Mountpoint string
fuseutil.NotImplementedFileSystem
lastHandle uint64
dirHandles util.SyncMap[fuseops.HandleID, *dirState]
fileHandles util.SyncMap[fuseops.HandleID, *fileState]
subvolMu sync.Mutex
subvols map[string]struct{}
grp *dgroup.Group
}
func (sv *Subvolume) Run(ctx context.Context) error {
sv.grp = dgroup.NewGroup(ctx, dgroup.GroupConfig{})
sv.grp.Go("self", func(ctx context.Context) error {
cfg := &fuse.MountConfig{
FSName: sv.DeviceName,
Subtype: "btrfs",
ReadOnly: true,
Options: map[string]string{
"allow_other": "",
},
}
return Mount(ctx, sv.Mountpoint, fuseutil.NewFileSystemServer(sv), cfg)
})
return sv.grp.Wait()
}
func (sv *Subvolume) newHandle() fuseops.HandleID {
return fuseops.HandleID(atomic.AddUint64(&sv.lastHandle, 1))
}
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) LoadDir(inode btrfs.ObjID) (val *btrfs.Dir, err error) {
val, err = sv.Subvolume.LoadDir(inode)
if val != nil {
haveSubvolumes := false
for _, index := range util.SortedMapKeys(val.ChildrenByIndex) {
entry := val.ChildrenByIndex[index]
if entry.Location.ItemType == btrfsitem.ROOT_ITEM_KEY {
haveSubvolumes = true
break
}
}
if haveSubvolumes {
abspath, _err := val.AbsPath()
if _err != nil {
return
}
sv.subvolMu.Lock()
for _, index := range util.SortedMapKeys(val.ChildrenByIndex) {
entry := val.ChildrenByIndex[index]
if entry.Location.ItemType != btrfsitem.ROOT_ITEM_KEY {
continue
}
if sv.subvols == nil {
sv.subvols = make(map[string]struct{})
}
subMountpoint := filepath.Join(abspath, string(entry.Name))
if _, alreadyMounted := sv.subvols[subMountpoint]; !alreadyMounted {
sv.subvols[subMountpoint] = struct{}{}
workerName := fmt.Sprintf("%d-%s", val.Inode, filepath.Base(subMountpoint))
sv.grp.Go(workerName, func(ctx context.Context) error {
subSv := &Subvolume{
Subvolume: btrfs.Subvolume{
FS: sv.FS,
TreeID: entry.Location.ObjectID,
},
DeviceName: sv.DeviceName,
Mountpoint: filepath.Join(sv.Mountpoint, subMountpoint[1:]),
}
return subSv.Run(ctx)
})
}
}
sv.subvolMu.Unlock()
}
}
return
}
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 {
// Subvolume
//
// Because each subvolume has its own pool of inodes
// (as in 2 different subvolumes can have files with
// te same inode number), so to represent that to FUSE
// we need to have this be a full separate mountpoint.
//
// I'd want to return EIO or EINTR or something here,
// but both the FUSE userspace tools and the kernel
// itself stat the mountpoint before mounting it, so
// we've got to return something bogus here to let
// that mount happen.
op.Entry = fuseops.ChildInodeEntry{
Child: 2, // an inode number that a real file will never have
Attributes: fuseops.InodeAttributes{
Nlink: 1,
Mode: uint32(linux.ModeFmtDir | 0700),
},
}
return nil
}
bareInode, err := sv.LoadBareInode(entry.Location.ObjectID)
if err != nil {
return err
}
op.Entry = fuseops.ChildInodeEntry{
Child: fuseops.InodeID(entry.Location.ObjectID),
Generation: fuseops.GenerationNumber(bareInode.InodeItem.Sequence),
Attributes: inodeItemToFUSE(*bareInode.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)
}
bareInode, err := sv.LoadBareInode(btrfs.ObjID(op.Inode))
if err != nil {
return err
}
op.Attributes = inodeItemToFUSE(*bareInode.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 := sv.newHandle()
sv.dirHandles.Store(handle, &dirState{
Dir: dir,
})
op.Handle = handle
return nil
}
func (sv *Subvolume) ReadDir(_ context.Context, op *fuseops.ReadDirOp) error {
state, ok := sv.dirHandles.Load(op.Handle)
if !ok {
return syscall.EBADF
}
origOffset := op.Offset
for _, index := range util.SortedMapKeys(state.Dir.ChildrenByIndex) {
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(op.Handle)
if !ok {
return syscall.EBADF
}
return nil
}
func (sv *Subvolume) OpenFile(_ context.Context, op *fuseops.OpenFileOp) error {
file, err := sv.LoadFile(btrfs.ObjID(op.Inode))
if err != nil {
return err
}
handle := sv.newHandle()
sv.fileHandles.Store(handle, &fileState{
File: file,
})
op.Handle = handle
op.KeepPageCache = true
return nil
}
func (sv *Subvolume) ReadFile(_ context.Context, op *fuseops.ReadFileOp) error {
state, ok := sv.fileHandles.Load(op.Handle)
if !ok {
return syscall.EBADF
}
size := op.Size
var dat []byte
if op.Dst != nil {
size = util.Min(int64(len(op.Dst)), size)
dat = op.Dst
} else {
dat = make([]byte, op.Size)
op.Data = [][]byte{dat}
}
var err error
op.BytesRead, err = state.File.ReadAt(dat, op.Offset)
if errors.Is(err, io.EOF) {
err = nil
}
return err
}
func (sv *Subvolume) ReleaseFileHandle(_ context.Context, op *fuseops.ReleaseFileHandleOp) error {
_, ok := sv.fileHandles.LoadAndDelete(op.Handle)
if !ok {
return syscall.EBADF
}
return nil
}
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() {}
|