-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path5dfs.go
554 lines (436 loc) · 9.66 KB
/
5dfs.go
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"time"
"encoding/binary"
"golang.org/x/sys/unix"
"bazil.org/fuse"
"bazil.org/fuse/fs"
. "github.com/mattn/go-getopt"
"golang.org/x/net/context"
"sync"
)
//=============================================================================
type DNode struct {
nid uint64
name string
attr fuse.Attr
valid bool
kids map[string]*DNode
data []uint8
}
type Dfs struct{}
var root *DNode
var _ fs.Node = (*DNode)(nil)
var _ fs.FS = (*Dfs)(nil)
var debug = false
var mountPoint = "5dfs"
var nodeMap = make(map[uint64]*DNode)
var nextNid uint64 = 1
var conn *fuse.Conn
var lock sync.Mutex
func p_out(s string, args ...interface{}) {
if !debug {
return
}
fmt.Printf(s, args...)
}
func p_err(s string, args ...interface{}) {
fmt.Printf(s, args...)
}
func p_call(s string, args ...interface{}) {
if !debug {
return
}
fmt.Printf(s, args...)
}
// Implement:
func (Dfs) Root() (n fs.Node, err error) {
lock.Lock()
defer lock.Unlock()
p_call("Root\n")
// return root
return root, nil
}
func (n *DNode) Attr(ctx context.Context, attr *fuse.Attr) error {
lock.Lock()
defer lock.Unlock()
p_call("Attr\n")
if !n.valid {
return fuse.ENOENT
}
// same as getattr, not really sure why we have both...
*attr = n.attr
return nil
}
func (n *DNode) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
lock.Lock()
defer lock.Unlock()
p_call("Open\n")
if !n.valid {
return nil, fuse.ENOENT
}
resp.Flags |= fuse.OpenDirectIO
return n, nil
}
func (n *DNode) Lookup(ctx context.Context, name string) (fs.Node, error) {
lock.Lock()
defer lock.Unlock()
p_call("Lookup\n")
if !n.valid {
return nil, fuse.ENOENT
}
// return kid if exists
if n.kids[name] != nil {
return n.kids[name], nil
}
return nil, fuse.ENOENT
}
func (n *DNode) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
lock.Lock()
defer lock.Unlock()
p_call("ReadDirAll\n")
if !n.valid {
return nil, fuse.ENOENT
}
if n.attr.Mode|os.ModeDir == 0 {
return nil, fuse.EPERM
}
ret := make([]fuse.Dirent, 0)
// iterate over kids and return all of them as Dirents
for k, v := range n.kids {
dirent := new(fuse.Dirent)
dirent.Inode = v.attr.Inode
if v.attr.Mode.IsDir() {
dirent.Type = fuse.DT_Dir
} else {
dirent.Type = fuse.DT_File
}
dirent.Name = k
ret = append(ret, *dirent)
}
return ret, nil
}
func (n *DNode) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
lock.Lock()
defer lock.Unlock()
p_call("Getattr\n")
if !n.valid {
return fuse.ENOENT
}
// just return attr
resp.Attr = n.attr
return nil
}
func (n *DNode) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
lock.Lock()
defer lock.Unlock()
p_call("Getxattr\n")
if n.nid != root.nid || req.Name != "timelines" {
p_out("%v %v %v\n", n.nid, root.nid, req.Name)
return fuse.ENOSYS
}
// return # of child universes
// check buf is big enough
if req.Size < 8 {
return unix.E2BIG
}
resp.Xattr = make([]byte, 8)
binary.LittleEndian.PutUint64(resp.Xattr, uint64(len(current_uni.paths)))
return nil
}
// must be defined or editing w/ vi or emacs fails. Doesn't have to do anything
func (n *DNode) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
lock.Lock()
defer lock.Unlock()
p_call("Fsync\n")
if !n.valid {
return fuse.ENOENT
}
// do nothing...
return nil
}
func (n *DNode) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
lock.Lock()
defer lock.Unlock()
p_call("Setattr\n")
if !n.valid {
return fuse.ENOENT
}
if req.Valid.Mode() && req.Mode != 000 {
return fuse.EPERM // no setting flag to readable
}
setattrTX := SetAttrTX {
n.nid,
req.Valid,
req.Size,
req.Atime,
req.Mtime,
req.Mode,
req.Uid,
req.Gid,
}
setattrTX.run() // no error to check
resp.Attr = n.attr
// add TX to timeline
add_tx(setattrTX)
return nil
}
func (n *DNode) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
lock.Lock()
defer lock.Unlock()
p_call("Setxattr\n")
if len(req.Xattr) != 8 {
return unix.EINVAL
}
arg := binary.LittleEndian.Uint64(req.Xattr)
if req.Name == "timeleap" {
// jump to past (parent universe)
// Xattr is packed int denoting how much to jump up
if arg <= 0 {
return unix.EINVAL
}
new_uni := current_uni
for i := uint64(0); i < arg; i++ {
if new_uni.parent == nil {
return unix.EINVAL
}
new_uni = new_uni.parent
}
// must reset FS state and rerun TXs
// invalidate all nodes
for _, n := range nodeMap {
if n.nid != root.nid {
n.valid = false
}
}
nodeMap = make(map[uint64]*DNode)
nextNid = 1
root.init_node("", os.ModeDir|0755)
// run txs, rn it just resets fs entirely
txs := make([]TX, 0)
for tmp_uni := new_uni; tmp_uni.parent != nil; tmp_uni = tmp_uni.parent {
txs = append(txs, tmp_uni.tx)
}
for i := len(txs)-1; i >= 0; i-- {
txs[i].run()
}
current_uni = new_uni
} else if req.Name == "backtothefuture" {
// jump to specific child universe
// Xattr is packed int denoting which future universe to go to
if arg >= uint64(len(current_uni.paths)) {
return unix.EINVAL
}
// when travelling forward, we can simply run the TX
new_uni := current_uni.paths[arg]
if !new_uni.allow {
return fuse.EPERM
}
new_uni.tx.run()
current_uni = new_uni
}
// clears kernel cache
exec.Command("sync").Run()
os.WriteFile("/proc/sys/vm/drop_caches", []byte("3\n"), 0755)
return nil
}
func (n *DNode) init_node(name string, mode os.FileMode) {
p_call("init_node\n")
n.nid = uint64(nextNid)
nextNid++
n.name = name
n.valid = true
n.data = make([]uint8, 0)
n.kids = make(map[string]*DNode)
n.attr.Inode = n.nid
n.attr.Mode = mode
time := time.Now()
n.attr.Atime = time
n.attr.Ctime = time
n.attr.Mtime = time
n.attr.Nlink = 1
n.attr.Size = 0
n.attr.Blocks = 0
n.attr.Uid = uint32(os.Geteuid())
n.attr.Gid = uint32(os.Geteuid())
nodeMap[n.nid] = n
}
func (p *DNode) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
lock.Lock()
defer lock.Unlock()
p_call("Mkdir\n")
if !p.valid {
return nil, fuse.ENOENT
}
mkdirTX := MkdirTX {
p.nid,
req.Name,
req.Mode,
}
newnid, err := mkdirTX.run()
if err != nil {
return nil, err
}
// add TX to timeline
add_tx(mkdirTX)
return nodeMap[newnid], nil
}
func (p *DNode) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
lock.Lock()
defer lock.Unlock()
p_call("Create\n")
if !p.valid {
return nil, nil, fuse.ENOENT
}
createTX := CreateTX {
p.nid,
req.Name,
req.Mode,
}
newnid, err := createTX.run()
if err != nil {
return nil, nil, err
}
// add TX to timeline
add_tx(createTX)
return nodeMap[newnid], nodeMap[newnid], nil
}
func (n *DNode) ReadAll(ctx context.Context) ([]byte, error) {
lock.Lock()
defer lock.Unlock()
p_call("ReadAll\n")
if !n.valid {
return nil, fuse.ENOENT
}
if n.attr.Mode.Perm() == 0 {
return nil, fuse.EPERM
}
n.attr.Atime = time.Now() // update atime
// just return our entire data
return n.data, nil
}
func (n *DNode) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
lock.Lock()
defer lock.Unlock()
p_call("Write %v %v %v\n", n.attr.Size, req.Offset, len(req.Data))
if !n.valid {
return fuse.ENOENT
}
writeTX := WriteTX {
n.nid,
req.Offset,
append(make([]byte, 0, len(req.Data)), req.Data...),
}
// no error to check...
sz, _ := writeTX.run()
resp.Size = int(sz)
// add TX to timeline
add_tx(writeTX)
return nil
}
func (n *DNode) Flush(ctx context.Context, req *fuse.FlushRequest) error {
lock.Lock()
defer lock.Unlock()
p_call("Flush\n")
if !n.valid {
return fuse.ENOENT
}
// no need to do anything here, as our writes are not buffered...
return nil
}
func (n *DNode) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
lock.Lock()
defer lock.Unlock()
p_call("Remove\n")
if !n.valid {
return fuse.ENOENT
}
removeTX := RemoveTX {
n.nid,
req.Name,
req.Dir,
}
_, err := removeTX.run()
if err != nil {
return err
}
// add TX to timeline
add_tx(removeTX)
return nil
}
func (n *DNode) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fs.Node) error {
lock.Lock()
defer lock.Unlock()
p_call("Rename\n")
if !n.valid {
return fuse.ENOENT
}
renameTX := RenameTX {
n.nid,
req.OldName,
req.NewName,
newDir.(*DNode).nid,
}
_, err := renameTX.run()
if err != nil {
return err
}
// add TX to timeline
add_tx(renameTX)
return nil
}
func main() {
var flag int
for {
if flag = Getopt("dm:"); flag == EOF {
break
}
switch flag {
case 'd':
debug = !debug
case 'm':
mountPoint = OptArg
default:
println("usage: main.go [-d | -m <mountpt>]", flag)
os.Exit(1)
}
}
p_out("mounting on %q, debug %v\n", mountPoint, debug)
// create root node
root = new(DNode)
root.init_node("", os.ModeDir|0755)
init_multiverse()
p_out("root inode %d", int(root.attr.Inode))
if _, err := os.Stat(mountPoint); err != nil {
os.Mkdir(mountPoint, 0755)
}
fuse.Unmount(mountPoint)
c, err := fuse.Mount(mountPoint, fuse.FSName("5dfs"), fuse.Subtype("5dfs"),
fuse.LocalVolume(), fuse.VolumeName("5dfs"), fuse.AllowOther())
if err != nil {
log.Fatal(err)
}
conn = c
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill)
go func() {
<-ch
defer conn.Close()
fuse.Unmount(mountPoint)
os.Exit(1)
}()
err = fs.Serve(conn, Dfs{})
if err != nil {
log.Fatal(err)
}
// check if the mount process has an error to report
<-conn.Ready
if err := conn.MountError; err != nil {
log.Fatal(err)
}
}