forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
504 lines (443 loc) · 10.7 KB
/
dir.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
package fs
import (
"bytes"
"errors"
"fmt"
"os"
"sync"
"syscall"
"bazil.org/bazil/cas/blobs"
wirecas "bazil.org/bazil/cas/wire"
"bazil.org/bazil/fs/inodes"
"bazil.org/bazil/fs/snap"
wiresnap "bazil.org/bazil/fs/snap/wire"
"bazil.org/bazil/fs/wire"
"bazil.org/bazil/util/env"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
"golang.org/x/net/context"
)
type dir struct {
fs.NodeRef
inode uint64
parent *dir
fs *Volume
// mu protects the fields below.
mu sync.Mutex
name string
// each in-memory child, so we can return the same node on
// multiple Lookups and know what to do on .save()
//
// each child also stores its own name; if the value in the child,
// looked up in this map, does not equal the child, that means the
// child has been unlinked
active map[string]node
}
var _ = node(&dir{})
var _ = fs.Node(&dir{})
var _ = fs.NodeCreater(&dir{})
var _ = fs.NodeForgetter(&dir{})
var _ = fs.NodeMkdirer(&dir{})
var _ = fs.NodeRemover(&dir{})
var _ = fs.NodeRenamer(&dir{})
var _ = fs.NodeStringLookuper(&dir{})
var _ = fs.HandleReadDirAller(&dir{})
func (d *dir) setName(name string) {
d.mu.Lock()
defer d.mu.Unlock()
d.name = name
}
func (d *dir) Attr() fuse.Attr {
return fuse.Attr{
Inode: d.inode,
Mode: os.ModeDir | 0755,
Nlink: 1,
Uid: env.MyUID,
Gid: env.MyGID,
}
}
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
if d.inode == 1 && name == ".snap" {
return &listSnaps{
fs: d.fs,
rootDir: d,
}, nil
}
d.mu.Lock()
defer d.mu.Unlock()
if child, ok := d.active[name]; ok {
return child, nil
}
key := pathToKey(d.inode, name)
var buf []byte
err := d.fs.db.View(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
buf = bucket.Get(key)
return nil
})
if err != nil {
return nil, err
}
if buf == nil {
return nil, fuse.ENOENT
}
de, err := d.unmarshalDirent(buf)
if err != nil {
return nil, fmt.Errorf("dirent unmarshal problem: %v", err)
}
child, err := d.reviveNode(de, name)
if err != nil {
return nil, fmt.Errorf("dirent node unmarshal problem: %v", err)
}
d.active[name] = child
return child, nil
}
func (d *dir) unmarshalDirent(buf []byte) (*wire.Dirent, error) {
var de wire.Dirent
err := proto.Unmarshal(buf, &de)
if err != nil {
return nil, err
}
return &de, nil
}
func (d *dir) reviveDir(de *wire.Dirent, name string) (*dir, error) {
if de.Type.Dir == nil {
return nil, fmt.Errorf("tried to revive non-directory as directory: %v", de.GetValue())
}
child := &dir{
inode: de.Inode,
name: name,
parent: d,
fs: d.fs,
active: make(map[string]node),
}
return child, nil
}
func (d *dir) reviveNode(de *wire.Dirent, name string) (node, error) {
switch {
case de.Type.Dir != nil:
return d.reviveDir(de, name)
case de.Type.File != nil:
manifest := de.Type.File.Manifest.ToBlob("file")
blob, err := blobs.Open(d.fs.chunkStore, manifest)
if err != nil {
return nil, err
}
child := &file{
inode: de.Inode,
name: name,
parent: d,
blob: blob,
}
return child, nil
}
return nil, fmt.Errorf("dirent unknown type: %v", de.GetValue())
}
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
d.mu.Lock()
defer d.mu.Unlock()
var entries []fuse.Dirent
err := d.fs.db.View(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
c := bucket.Cursor()
prefix := pathToKey(d.inode, "")
for k, v := c.Seek(prefix); k != nil; k, v = c.Next() {
if !bytes.HasPrefix(k, prefix) {
// past the end of the directory
break
}
name := string(k[len(prefix):])
de, err := d.unmarshalDirent(v)
if err != nil {
return fmt.Errorf("readdir error: %v", err)
}
fde := de.GetFUSEDirent(name)
entries = append(entries, fde)
}
return nil
})
return entries, err
}
// caller does locking
func (d *dir) saveInternal(tx *bolt.Tx, name string, n node) error {
if have, ok := d.active[name]; !ok || have != n {
// unlinked
return nil
}
de, err := n.marshal()
if err != nil {
return fmt.Errorf("node save error: %v", err)
}
buf, err := proto.Marshal(de)
if err != nil {
return fmt.Errorf("Dirent marshal error: %v", err)
}
key := pathToKey(d.inode, name)
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
err = bucket.Put(key, buf)
if err != nil {
return fmt.Errorf("db write error: %v", err)
}
return nil
}
func (d *dir) marshal() (*wire.Dirent, error) {
de := &wire.Dirent{
Inode: d.inode,
}
de.Type.Dir = &wire.Dir{}
return de, nil
}
func (d *dir) save(tx *bolt.Tx, name string, n node) error {
d.mu.Lock()
defer d.mu.Unlock()
return d.saveInternal(tx, name, n)
}
func (d *dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
d.mu.Lock()
defer d.mu.Unlock()
// TODO check for duplicate name
switch req.Mode & os.ModeType {
case 0:
var child node
err := d.fs.db.Update(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketInode)
if bucket == nil {
return errors.New("inode bucket is missing")
}
inode, err := inodes.Allocate(bucket)
if err != nil {
return err
}
manifest := blobs.EmptyManifest("file")
blob, err := blobs.Open(d.fs.chunkStore, manifest)
if err != nil {
return fmt.Errorf("blob open problem: %v", err)
}
child = &file{
inode: inode,
name: req.Name,
parent: d,
blob: blob,
}
d.active[req.Name] = child
return d.saveInternal(tx, req.Name, child)
// TODO clean up active on error
})
if err != nil {
return nil, nil, err
}
return child, child, nil
default:
return nil, nil, fuse.EPERM
}
}
// caller does locking
func (d *dir) forgetChild(name string, child node) {
d.mu.Lock()
defer d.mu.Unlock()
if have, ok := d.active[name]; ok {
// have something by that name
if have == child {
// has not been overwritten
delete(d.active, name)
}
}
}
func (d *dir) Forget() {
if d.parent == nil {
// root dir, don't keep track
return
}
d.parent.forgetChild(d.name, d)
}
func (d *dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
d.mu.Lock()
defer d.mu.Unlock()
// TODO handle req.Mode
var child node
err := d.fs.db.Update(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketInode)
if bucket == nil {
return errors.New("inode bucket is missing")
}
inode, err := inodes.Allocate(bucket)
if err != nil {
return err
}
child = &dir{
inode: inode,
name: req.Name,
parent: d,
fs: d.fs,
active: make(map[string]node),
}
d.active[req.Name] = child
return d.saveInternal(tx, req.Name, child)
// TODO clean up active on error
})
if err != nil {
if err == inodes.OutOfInodes {
return nil, fuse.Errno(syscall.ENOSPC)
}
return nil, err
}
return child, nil
}
func (d *dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
d.mu.Lock()
defer d.mu.Unlock()
key := pathToKey(d.inode, req.Name)
err := d.fs.db.Update(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
// does it exist? can short-circuit existence check if active
if _, ok := d.active[req.Name]; !ok {
if bucket.Get(key) == nil {
return fuse.ENOENT
}
}
err := bucket.Delete(key)
if err != nil {
return err
}
delete(d.active, req.Name)
// TODO free inode
return nil
})
return err
}
func (d *dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fs.Node) error {
d.mu.Lock()
defer d.mu.Unlock()
// if you ever change this, also guard against renaming into
// special directories like .snap; check type of newDir is *dir
//
// also worry about deadlocks
if newDir != d {
return fuse.Errno(syscall.EXDEV)
}
kOld := pathToKey(d.inode, req.OldName)
kNew := pathToKey(d.inode, req.NewName)
// the file getting overwritten
var loserInode uint64
err := d.fs.db.Update(func(tx *bolt.Tx) error {
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
// TODO don't need to load from db if req.OldName is in active.
// instead, save active state if we have it; call .save() not this
// kludge
bufOld := bucket.Get(kOld)
if bufOld == nil {
return fuse.ENOENT
}
{
// TODO don't need to load from db if req.NewName is in active
bufLoser := bucket.Get(kNew)
if bufLoser != nil {
// overwriting
deLoser, err := d.unmarshalDirent(bufLoser)
if err != nil {
return fmt.Errorf("dirent unmarshal problem: %v", err)
}
loserInode = deLoser.Inode
}
}
if err := bucket.Put(kNew, bufOld); err != nil {
return err
}
if err := bucket.Delete(kOld); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
// if the source inode is active, record its new name
if nodeOld, ok := d.active[req.OldName]; ok {
nodeOld.setName(req.NewName)
delete(d.active, req.OldName)
d.active[req.NewName] = nodeOld
}
if loserInode > 0 {
// TODO free loser inode
}
return nil
}
// snapshot records a snapshot of the directory and stores it in wde
func (d *dir) snapshot(ctx context.Context, tx *bolt.Tx, out *wiresnap.Dir) error {
// NOT HOLDING THE LOCK, accessing database snapshot ONLY
// TODO move bucket lookup to caller?
bucket := d.fs.bucket(tx).Bucket(bucketDir)
if bucket == nil {
return errors.New("dir bucket missing")
}
manifest := blobs.EmptyManifest("dir")
blob, err := blobs.Open(d.fs.chunkStore, manifest)
if err != nil {
return err
}
w := snap.NewWriter(blob)
c := bucket.Cursor()
prefix := pathToKey(d.inode, "")
for k, v := c.Seek(prefix); k != nil; k, v = c.Next() {
if !bytes.HasPrefix(k, prefix) {
// past the end of the directory
break
}
name := string(k[len(prefix):])
de, err := d.unmarshalDirent(v)
if err != nil {
return err
}
sde := wiresnap.Dirent{
Name: name,
}
switch {
case de.Type.File != nil:
// TODO d.reviveNode would do blobs.Open and that's a bit
// too much work; rework the apis
sde.Type.File = &wiresnap.File{
Manifest: de.Type.File.Manifest,
}
case de.Type.Dir != nil:
child, err := d.reviveDir(de, name)
if err != nil {
return err
}
sde.Type.Dir = &wiresnap.Dir{}
err = child.snapshot(ctx, tx, sde.Type.Dir)
if err != nil {
return err
}
default:
return errors.New("TODO")
}
err = w.Add(&sde)
if err != nil {
return err
}
}
manifest, err = blob.Save()
if err != nil {
return err
}
out.Manifest = wirecas.FromBlob(manifest)
out.Align = w.Align()
return nil
}