forked from nyaxt/otaru
/
filesystem.go
511 lines (412 loc) · 11.6 KB
/
filesystem.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
package otaru
import (
"fmt"
"log"
"sync"
"syscall"
"github.com/nyaxt/otaru/blobstore"
"github.com/nyaxt/otaru/btncrypt"
"github.com/nyaxt/otaru/chunkstore"
fl "github.com/nyaxt/otaru/flags"
"github.com/nyaxt/otaru/inodedb"
"github.com/nyaxt/otaru/util"
)
const (
EACCES = syscall.Errno(syscall.EACCES)
EBADF = syscall.Errno(syscall.EBADF)
EEXIST = syscall.Errno(syscall.EEXIST)
EISDIR = syscall.Errno(syscall.EISDIR)
ENOENT = syscall.Errno(syscall.ENOENT)
ENOTDIR = syscall.Errno(syscall.ENOTDIR)
ENOTEMPTY = syscall.Errno(syscall.ENOTEMPTY)
EPERM = syscall.Errno(syscall.EPERM)
)
const (
FileWriteCacheMaxPatches = 32
FileWriteCacheMaxPatchContentLen = 256 * 1024
)
type FileSystem struct {
idb inodedb.DBHandler
bs blobstore.RandomAccessBlobStore
c btncrypt.Cipher
newChunkedFileIO func(bs blobstore.RandomAccessBlobStore, c btncrypt.Cipher, caio chunkstore.ChunksArrayIO) blobstore.BlobHandle
muOpenFiles sync.Mutex
openFiles map[inodedb.ID]*OpenFile
muOrigPath sync.Mutex
origpath map[inodedb.ID]string
}
func NewFileSystem(idb inodedb.DBHandler, bs blobstore.RandomAccessBlobStore, c btncrypt.Cipher) *FileSystem {
fs := &FileSystem{
idb: idb,
bs: bs,
c: c,
newChunkedFileIO: func(bs blobstore.RandomAccessBlobStore, c btncrypt.Cipher, caio chunkstore.ChunksArrayIO) blobstore.BlobHandle {
return chunkstore.NewChunkedFileIO(bs, c, caio)
},
openFiles: make(map[inodedb.ID]*OpenFile),
origpath: make(map[inodedb.ID]string),
}
fs.setOrigPathForId(inodedb.RootDirID, "/")
return fs
}
func (fs *FileSystem) tryGetOrigPath(id inodedb.ID) string {
fs.muOrigPath.Lock()
defer fs.muOrigPath.Unlock()
origpath, ok := fs.origpath[id]
if !ok {
log.Printf("Failed to lookup orig path for ID %d", id)
return "<unknown>"
}
log.Printf("Orig path for ID %d is \"%s\"", id, origpath)
return origpath
}
func (fs *FileSystem) setOrigPathForId(id inodedb.ID, origpath string) {
fs.muOrigPath.Lock()
defer fs.muOrigPath.Unlock()
if len(origpath) == 0 {
delete(fs.origpath, id)
}
fs.origpath[id] = origpath
}
func (fs *FileSystem) Sync() error {
es := []error{}
if s, ok := fs.idb.(util.Syncer); ok {
if err := s.Sync(); err != nil {
es = append(es, fmt.Errorf("Failed to sync INodeDB: %v", err))
}
}
// FIXME: sync active handles
return util.ToErrors(es)
}
func (fs *FileSystem) OverrideNewChunkedFileIOForTesting(newChunkedFileIO func(blobstore.RandomAccessBlobStore, btncrypt.Cipher, chunkstore.ChunksArrayIO) blobstore.BlobHandle) {
fs.newChunkedFileIO = newChunkedFileIO
}
func (fs *FileSystem) DirEntries(id inodedb.ID) (map[string]inodedb.ID, error) {
v, _, err := fs.idb.QueryNode(id, false)
if err != nil {
return nil, err
}
if v.GetType() != inodedb.DirNodeT {
return nil, ENOTDIR
}
dv := v.(*inodedb.DirNodeView)
dirorigpath := fs.tryGetOrigPath(id)
for name, id := range dv.Entries {
fs.setOrigPathForId(id, fmt.Sprintf("%s/%s", dirorigpath, name))
}
return dv.Entries, err
}
func (fs *FileSystem) Rename(srcDirID inodedb.ID, srcName string, dstDirID inodedb.ID, dstName string) error {
tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
&inodedb.RenameOp{
SrcDirID: srcDirID, SrcName: srcName,
DstDirID: dstDirID, DstName: dstName,
},
}}
if _, err := fs.idb.ApplyTransaction(tx); err != nil {
return err
}
// FIXME: fs.setOrigPathForId
return nil
}
func (fs *FileSystem) Remove(dirID inodedb.ID, name string) error {
tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
&inodedb.RemoveOp{
NodeLock: inodedb.NodeLock{dirID, inodedb.NoTicket}, Name: name,
},
}}
if _, err := fs.idb.ApplyTransaction(tx); err != nil {
return err
}
// FIXME: fs.setOrigPathForId
return nil
}
func (fs *FileSystem) createNode(dirID inodedb.ID, name string, typ inodedb.Type) (inodedb.ID, error) {
nlock, err := fs.idb.LockNode(inodedb.AllocateNewNodeID)
if err != nil {
return 0, err
}
defer func() {
if err := fs.idb.UnlockNode(nlock); err != nil {
log.Printf("Failed to unlock node when creating file: %v", err)
}
}()
dirorigpath := fs.tryGetOrigPath(dirID)
origpath := fmt.Sprintf("%s/%s", dirorigpath, name)
tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
&inodedb.CreateNodeOp{NodeLock: nlock, OrigPath: origpath, Type: typ},
&inodedb.HardLinkOp{NodeLock: inodedb.NodeLock{dirID, inodedb.NoTicket}, Name: name, TargetID: nlock.ID},
}}
if _, err := fs.idb.ApplyTransaction(tx); err != nil {
return 0, err
}
fs.setOrigPathForId(nlock.ID, origpath)
return nlock.ID, nil
}
func (fs *FileSystem) CreateFile(dirID inodedb.ID, name string) (inodedb.ID, error) {
return fs.createNode(dirID, name, inodedb.FileNodeT)
}
func (fs *FileSystem) CreateDir(dirID inodedb.ID, name string) (inodedb.ID, error) {
return fs.createNode(dirID, name, inodedb.DirNodeT)
}
type Attr struct {
ID inodedb.ID
Type inodedb.Type
Size int64
}
func (fs *FileSystem) Attr(id inodedb.ID) (Attr, error) {
v, _, err := fs.idb.QueryNode(id, false)
if err != nil {
return Attr{}, err
}
size := int64(0)
if fn, ok := v.(*inodedb.FileNodeView); ok {
size = fn.Size
}
a := Attr{
ID: v.GetID(),
Type: v.GetType(),
Size: size,
}
return a, nil
}
func (fs *FileSystem) IsDir(id inodedb.ID) (bool, error) {
v, _, err := fs.idb.QueryNode(id, false)
if err != nil {
return false, err
}
return v.GetType() == inodedb.DirNodeT, nil
}
type FileHandle struct {
of *OpenFile
flags int
}
type OpenFile struct {
fs *FileSystem
nlock inodedb.NodeLock
wc *FileWriteCache
cfio blobstore.BlobHandle
origFilename string
handles []*FileHandle
mu sync.Mutex
}
func (fs *FileSystem) getOrCreateOpenFile(id inodedb.ID) *OpenFile {
fs.muOpenFiles.Lock()
defer fs.muOpenFiles.Unlock()
of, ok := fs.openFiles[id]
if ok {
return of
}
of = &OpenFile{
fs: fs,
wc: NewFileWriteCache(),
handles: make([]*FileHandle, 0, 1),
}
fs.openFiles[id] = of
return of
}
type origFilenameSetter interface {
SetOrigFilename(name string)
}
func (fs *FileSystem) OpenFile(id inodedb.ID, flags int) (*FileHandle, error) {
log.Printf("OpenFile(id: %v, flags rok: %t wok: %t)", id, fl.IsReadAllowed(flags), fl.IsWriteAllowed(flags))
tryLock := fl.IsWriteAllowed(flags)
if tryLock && !fl.IsWriteAllowed(fs.bs.Flags()) {
return nil, EACCES
}
of := fs.getOrCreateOpenFile(id)
of.mu.Lock()
defer of.mu.Unlock()
ofIsInitialized := of.nlock.ID != 0
if ofIsInitialized && (of.nlock.HasTicket() || !tryLock) {
// No need to upgrade lock. Just use cached filehandle.
log.Printf("Using cached of for inode id: %v", id)
return of.OpenHandleWithoutLock(flags), nil
}
// upgrade lock or acquire new lock...
v, nlock, err := fs.idb.QueryNode(id, tryLock)
if err != nil {
return nil, err
}
if v.GetType() != inodedb.FileNodeT {
if err := fs.idb.UnlockNode(nlock); err != nil {
log.Printf("Unlock node failed for non-file node: %v", err)
}
if v.GetType() == inodedb.DirNodeT {
return nil, EISDIR
}
return nil, fmt.Errorf("Specified node not file but has type %v", v.GetType())
}
of.nlock = nlock
caio := NewINodeDBChunksArrayIO(fs.idb, nlock)
of.cfio = fs.newChunkedFileIO(fs.bs, fs.c, caio)
if setter, ok := of.cfio.(origFilenameSetter); ok {
setter.SetOrigFilename(fs.tryGetOrigPath(nlock.ID))
}
return of.OpenHandleWithoutLock(flags), nil
}
func (of *OpenFile) OpenHandleWithoutLock(flags int) *FileHandle {
fh := &FileHandle{of: of, flags: flags}
of.handles = append(of.handles, fh)
return fh
}
func (of *OpenFile) CloseHandle(tgt *FileHandle) {
if tgt.of == nil {
log.Printf("Detected FileHandle double close!")
return
}
if tgt.of != of {
log.Fatalf("Attempt to close handle for other OpenFile. tgt fh: %+v, of: %+v", tgt, of)
return
}
wasWriteHandle := fl.IsWriteAllowed(tgt.flags)
ofHasOtherWriteHandle := false
tgt.of = nil
of.mu.Lock()
defer of.mu.Unlock()
// remove tgt from of.handles slice
newHandles := make([]*FileHandle, 0, len(of.handles)-1)
for _, h := range of.handles {
if h != tgt {
if fl.IsWriteAllowed(h.flags) {
ofHasOtherWriteHandle = true
}
newHandles = append(newHandles, h)
}
}
of.handles = newHandles
if wasWriteHandle && !ofHasOtherWriteHandle {
of.downgradeToReadLock()
}
}
func (of *OpenFile) downgradeToReadLock() {
log.Printf("Downgrade %v to read lock.", of)
// Note: assumes of.mu is Lock()-ed
if !of.nlock.HasTicket() {
log.Printf("Attempt to downgrade node lock, but no excl lock found. of: %v", of)
return
}
if err := of.fs.idb.UnlockNode(of.nlock); err != nil {
log.Printf("Unlocking node to downgrade to read lock failed: %v", err)
}
of.nlock.Ticket = inodedb.NoTicket
caio := NewINodeDBChunksArrayIO(of.fs.idb, of.nlock)
of.cfio = of.fs.newChunkedFileIO(of.fs.bs, of.fs.c, caio)
}
func (of *OpenFile) updateSizeWithoutLock(newsize int64) error {
tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
&inodedb.UpdateSizeOp{NodeLock: of.nlock, Size: newsize},
}}
if _, err := of.fs.idb.ApplyTransaction(tx); err != nil {
return fmt.Errorf("Failed to update FileNode size: %v", err)
}
return nil
}
// sizeMayFailWithoutLock returns file size if succeed. The size query may fail with an error.
func (of *OpenFile) sizeMayFailWithoutLock() (int64, error) {
v, _, err := of.fs.idb.QueryNode(of.nlock.ID, false)
if err != nil {
return 0, fmt.Errorf("Failed to QueryNode inodedb: %v", err)
}
fv, ok := v.(*inodedb.FileNodeView)
if !ok {
return 0, fmt.Errorf("Non-FileNodeView returned from QueryNode. Type: %v", v.GetType())
}
return fv.Size, nil
}
func (of *OpenFile) PWrite(offset int64, p []byte) error {
of.mu.Lock()
defer of.mu.Unlock()
currentSize, err := of.sizeMayFailWithoutLock()
if err != nil {
return err
}
// Pass wc.PWrite a copy of "p", as wc.PWrite expects its slice to be never modified afterwards.
pcopy := make([]byte, len(p))
copy(pcopy, p)
if err := of.wc.PWrite(offset, pcopy); err != nil {
return err
}
if of.wc.NeedsSync() {
if err := of.wc.Sync(of.cfio); err != nil {
return err
}
}
right := offset + int64(len(p))
if right > currentSize {
return of.updateSizeWithoutLock(right)
}
return nil
}
func (of *OpenFile) PRead(offset int64, p []byte) error {
of.mu.Lock()
defer of.mu.Unlock()
return of.wc.PReadThrough(offset, p, of.cfio)
}
func (of *OpenFile) Sync() error {
of.mu.Lock()
defer of.mu.Unlock()
if err := of.wc.Sync(of.cfio); err != nil {
return fmt.Errorf("FileWriteCache sync failed: %v", err)
}
return nil
}
func (of *OpenFile) Size() int64 {
of.mu.Lock()
defer of.mu.Unlock()
size, err := of.sizeMayFailWithoutLock()
if err != nil {
return 0
}
return size
}
func (of *OpenFile) Truncate(newsize int64) error {
of.mu.Lock()
defer of.mu.Unlock()
oldsize, err := of.sizeMayFailWithoutLock()
if err != nil {
return err
}
if newsize > oldsize {
return of.updateSizeWithoutLock(newsize)
} else if newsize < oldsize {
of.wc.Truncate(newsize)
of.cfio.Truncate(newsize)
return of.updateSizeWithoutLock(newsize)
} else {
return nil
}
}
func (fh *FileHandle) ID() inodedb.ID {
return fh.of.nlock.ID
}
func (fh *FileHandle) PWrite(offset int64, p []byte) error {
if !fl.IsWriteAllowed(fh.flags) {
return EBADF
}
return fh.of.PWrite(offset, p)
}
func (fh *FileHandle) PRead(offset int64, p []byte) error {
if !fl.IsReadAllowed(fh.flags) {
return EBADF
}
return fh.of.PRead(offset, p)
}
func (fh *FileHandle) Sync() error {
if !fl.IsWriteAllowed(fh.flags) {
return nil
}
return fh.of.Sync()
}
func (fh *FileHandle) Size() int64 {
return fh.of.Size()
}
func (fh *FileHandle) Truncate(newsize int64) error {
if !fl.IsWriteAllowed(fh.flags) {
return EBADF
}
return fh.of.Truncate(newsize)
}
func (fh *FileHandle) Close() {
fh.of.CloseHandle(fh)
}