forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bserver_disk.go
550 lines (473 loc) · 14 KB
/
bserver_disk.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"errors"
"fmt"
"math"
"os"
"path/filepath"
"sync"
"github.com/keybase/client/go/kbfs/ioutil"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/kbfscodec"
"github.com/keybase/client/go/kbfs/kbfscrypto"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
type blockServerDiskTlfStorage struct {
lock sync.RWMutex
// store is nil after it is shut down in Shutdown().
store *blockDiskStore
}
// BlockServerDisk implements the BlockServer interface by just
// storing blocks in a local disk store.
type BlockServerDisk struct {
codec kbfscodec.Codec
log logger.Logger
dirPath string
shutdownFunc func(logger.Logger)
tlfStorageLock sync.RWMutex
// tlfStorage is nil after Shutdown() is called.
tlfStorage map[tlf.ID]*blockServerDiskTlfStorage
}
var _ blockServerLocal = (*BlockServerDisk)(nil)
// newBlockServerDisk constructs a new BlockServerDisk that stores
// its data in the given directory.
func newBlockServerDisk(
codec kbfscodec.Codec, log logger.Logger,
dirPath string, shutdownFunc func(logger.Logger)) *BlockServerDisk {
bserv := &BlockServerDisk{
codec, log, dirPath, shutdownFunc, sync.RWMutex{},
make(map[tlf.ID]*blockServerDiskTlfStorage),
}
return bserv
}
// NewBlockServerDir constructs a new BlockServerDisk that stores
// its data in the given directory.
func NewBlockServerDir(codec kbfscodec.Codec,
log logger.Logger, dirPath string) *BlockServerDisk {
return newBlockServerDisk(codec, log, dirPath, nil)
}
// NewBlockServerTempDir constructs a new BlockServerDisk that stores its
// data in a temp directory which is cleaned up on shutdown.
func NewBlockServerTempDir(codec kbfscodec.Codec,
log logger.Logger) (*BlockServerDisk, error) {
tempdir, err := ioutil.TempDir(os.TempDir(), "kbfs_bserver_tmp")
if err != nil {
return nil, err
}
return newBlockServerDisk(codec, log, tempdir, func(log logger.Logger) {
err := ioutil.RemoveAll(tempdir)
if err != nil {
log.Warning("error removing %s: %s", tempdir, err)
}
}), nil
}
var errBlockServerDiskShutdown = errors.New("BlockServerDisk is shutdown")
func (b *BlockServerDisk) getStorage(tlfID tlf.ID) (
*blockServerDiskTlfStorage, error) {
storage, err := func() (*blockServerDiskTlfStorage, error) {
b.tlfStorageLock.RLock()
defer b.tlfStorageLock.RUnlock()
if b.tlfStorage == nil {
return nil, errBlockServerDiskShutdown
}
return b.tlfStorage[tlfID], nil
}()
if err != nil {
return nil, err
}
if storage != nil {
return storage, nil
}
b.tlfStorageLock.Lock()
defer b.tlfStorageLock.Unlock()
if b.tlfStorage == nil {
return nil, errBlockServerDiskShutdown
}
storage, ok := b.tlfStorage[tlfID]
if ok {
return storage, nil
}
path := filepath.Join(b.dirPath, tlfID.String())
store := makeBlockDiskStore(b.codec, path)
storage = &blockServerDiskTlfStorage{
store: store,
}
b.tlfStorage[tlfID] = storage
return storage, nil
}
// Get implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) Get(
ctx context.Context, tlfID tlf.ID, id kbfsblock.ID,
context kbfsblock.Context, _ DiskBlockCacheType) (
data []byte, serverHalf kbfscrypto.BlockCryptKeyServerHalf, err error) {
if err := checkContext(ctx); err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.Get id=%s tlfID=%s context=%s",
id, tlfID, context)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
tlfStorage.lock.RLock()
defer tlfStorage.lock.RUnlock()
if tlfStorage.store == nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{},
errBlockServerDiskShutdown
}
data, keyServerHalf, err := tlfStorage.store.getDataWithContext(
ctx, id, context)
if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
return data, keyServerHalf, nil
}
// GetEncodedSizes implements the BlockServer interface for
// BlockServerDisk.
func (b *BlockServerDisk) GetEncodedSizes(
ctx context.Context, tlfID tlf.ID, ids []kbfsblock.ID,
contexts []kbfsblock.Context) (
sizes []uint32, statuses []keybase1.BlockStatus, err error) {
if err := checkContext(ctx); err != nil {
return nil, nil, err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx,
"BlockServerDisk.GetEncodedSizes id=%s tlfID=%s context=%s",
ids, tlfID, contexts)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return nil, nil, err
}
tlfStorage.lock.RLock()
defer tlfStorage.lock.RUnlock()
if tlfStorage.store == nil {
return nil, nil, errBlockServerDiskShutdown
}
sizes = make([]uint32, len(ids))
statuses = make([]keybase1.BlockStatus, len(ids))
for i, id := range ids {
context := contexts[i]
hasContext, refStatus, err := tlfStorage.store.hasContext(
ctx, id, context)
if err != nil {
return nil, nil, err
}
if !hasContext {
sizes[i] = 0
statuses[i] = keybase1.BlockStatus_UNKNOWN
continue
}
size64, err := tlfStorage.store.getDataSize(ctx, id)
if err != nil {
return nil, nil, err
}
sizes[i] = uint32(size64)
statuses[i] = refStatus.toBlockStatus()
}
return sizes, statuses, nil
}
// Put implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) Put(
ctx context.Context, tlfID tlf.ID, id kbfsblock.ID,
context kbfsblock.Context, buf []byte,
serverHalf kbfscrypto.BlockCryptKeyServerHalf,
_ DiskBlockCacheType) (err error) {
if err := checkContext(ctx); err != nil {
return err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.Put id=%s tlfID=%s context=%s size=%d",
id, tlfID, context, len(buf))
if context.GetRefNonce() != kbfsblock.ZeroRefNonce {
return errors.New("can't Put() a block with a non-zero refnonce")
}
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return errBlockServerDiskShutdown
}
_, err = tlfStorage.store.put(ctx, true, id, context, buf, serverHalf)
if err != nil {
return err
}
err = tlfStorage.store.addReference(ctx, id, context, "tag")
if err != nil {
return err
}
return nil
}
// PutAgain implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) PutAgain(
ctx context.Context, tlfID tlf.ID, id kbfsblock.ID,
context kbfsblock.Context, buf []byte,
serverHalf kbfscrypto.BlockCryptKeyServerHalf,
_ DiskBlockCacheType) (err error) {
if err := checkContext(ctx); err != nil {
return err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.PutAgain id=%s tlfID=%s context=%s size=%d",
id, tlfID, context, len(buf))
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return errBlockServerDiskShutdown
}
_, err = tlfStorage.store.put(ctx, false, id, context, buf, serverHalf)
if err != nil {
return err
}
err = tlfStorage.store.addReference(ctx, id, context, "tag")
if err != nil {
return err
}
return nil
}
// AddBlockReference implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) AddBlockReference(ctx context.Context, tlfID tlf.ID,
id kbfsblock.ID, context kbfsblock.Context) error {
if err := checkContext(ctx); err != nil {
return err
}
b.log.CDebugf(ctx, "BlockServerDisk.AddBlockReference id=%s "+
"tlfID=%s context=%s", id, tlfID, context)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return errBlockServerDiskShutdown
}
hasRef, err := tlfStorage.store.hasAnyRef(ctx, id)
if err != nil {
return err
}
if !hasRef {
return kbfsblock.ServerErrorBlockNonExistent{Msg: fmt.Sprintf("Block ID %s "+
"doesn't exist and cannot be referenced.", id)}
}
hasNonArchivedRef, err := tlfStorage.store.hasNonArchivedRef(ctx, id)
if err != nil {
return err
}
if !hasNonArchivedRef {
return kbfsblock.ServerErrorBlockArchived{Msg: fmt.Sprintf("Block ID %s has "+
"been archived and cannot be referenced.", id)}
}
return tlfStorage.store.addReference(ctx, id, context, "")
}
// RemoveBlockReferences implements the BlockServer interface for
// BlockServerDisk.
func (b *BlockServerDisk) RemoveBlockReferences(ctx context.Context,
tlfID tlf.ID, contexts kbfsblock.ContextMap) (
liveCounts map[kbfsblock.ID]int, err error) {
if err := checkContext(ctx); err != nil {
return nil, err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.RemoveBlockReference "+
"tlfID=%s contexts=%v", tlfID, contexts)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return nil, err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return nil, errBlockServerDiskShutdown
}
liveCounts = make(map[kbfsblock.ID]int)
for id, idContexts := range contexts {
liveCount, err := tlfStorage.store.removeReferences(
ctx, id, idContexts, "")
if err != nil {
return nil, err
}
liveCounts[id] = liveCount
if liveCount == 0 {
err := tlfStorage.store.remove(ctx, id)
if err != nil {
return nil, err
}
}
}
return liveCounts, nil
}
// ArchiveBlockReferences implements the BlockServer interface for
// BlockServerDisk.
func (b *BlockServerDisk) ArchiveBlockReferences(ctx context.Context,
tlfID tlf.ID, contexts kbfsblock.ContextMap) (err error) {
if err := checkContext(ctx); err != nil {
return err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.ArchiveBlockReferences "+
"tlfID=%s contexts=%v", tlfID, contexts)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return errBlockServerDiskShutdown
}
for id, idContexts := range contexts {
for _, context := range idContexts {
hasContext, _, err := tlfStorage.store.hasContext(ctx, id, context)
if err != nil {
return err
}
if !hasContext {
return kbfsblock.ServerErrorBlockNonExistent{
Msg: fmt.Sprintf(
"Block ID %s (context %s) doesn't "+
"exist and cannot be archived.",
id, context),
}
}
}
}
return tlfStorage.store.archiveReferences(ctx, contexts, "")
}
// GetLiveBlockReferences implements the BlockServer interface for
// BlockServerDisk.
func (b *BlockServerDisk) GetLiveBlockReferences(
ctx context.Context, tlfID tlf.ID, contexts kbfsblock.ContextMap) (
liveCounts map[kbfsblock.ID]int, err error) {
if err := checkContext(ctx); err != nil {
return nil, err
}
defer func() {
err = translateToBlockServerError(err)
}()
b.log.CDebugf(ctx, "BlockServerDisk.GetLiveBlockReferences "+
"tlfID=%s contexts=%v", tlfID, contexts)
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return nil, err
}
tlfStorage.lock.Lock()
defer tlfStorage.lock.Unlock()
if tlfStorage.store == nil {
return nil, errBlockServerDiskShutdown
}
liveCounts = make(map[kbfsblock.ID]int)
for id := range contexts {
liveCount, err := tlfStorage.store.getLiveCount(ctx, id)
if err != nil {
return nil, err
}
liveCounts[id] = liveCount
}
return liveCounts, nil
}
// getAllRefsForTest implements the blockServerLocal interface for
// BlockServerDisk.
func (b *BlockServerDisk) getAllRefsForTest(ctx context.Context, tlfID tlf.ID) (
map[kbfsblock.ID]blockRefMap, error) {
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return nil, err
}
tlfStorage.lock.RLock()
defer tlfStorage.lock.RUnlock()
if tlfStorage.store == nil {
return nil, errBlockServerDiskShutdown
}
return tlfStorage.store.getAllRefsForTest()
}
// IsUnflushed implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) IsUnflushed(ctx context.Context, tlfID tlf.ID,
_ kbfsblock.ID) (bool, error) {
if err := checkContext(ctx); err != nil {
return false, err
}
tlfStorage, err := b.getStorage(tlfID)
if err != nil {
return false, err
}
tlfStorage.lock.RLock()
defer tlfStorage.lock.RUnlock()
if tlfStorage.store == nil {
return false, errBlockServerDiskShutdown
}
return false, nil
}
// Shutdown implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) Shutdown(ctx context.Context) {
tlfStorage := func() map[tlf.ID]*blockServerDiskTlfStorage {
b.tlfStorageLock.Lock()
defer b.tlfStorageLock.Unlock()
// Make further accesses error out.
tlfStorage := b.tlfStorage
b.tlfStorage = nil
return tlfStorage
}()
for _, s := range tlfStorage {
func() {
s.lock.Lock()
defer s.lock.Unlock()
if s.store == nil {
// Already shutdown.
return
}
// Make further accesses error out.
s.store = nil
}()
}
if b.shutdownFunc != nil {
b.shutdownFunc(b.log)
}
}
// RefreshAuthToken implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) RefreshAuthToken(_ context.Context) {}
// GetUserQuotaInfo implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) GetUserQuotaInfo(ctx context.Context) (info *kbfsblock.QuotaInfo, err error) {
if err := checkContext(ctx); err != nil {
return nil, err
}
// Return a dummy value here.
return &kbfsblock.QuotaInfo{Limit: math.MaxInt64}, nil
}
// GetTeamQuotaInfo implements the BlockServer interface for BlockServerDisk.
func (b *BlockServerDisk) GetTeamQuotaInfo(
ctx context.Context, _ keybase1.TeamID) (
info *kbfsblock.QuotaInfo, err error) {
if err := checkContext(ctx); err != nil {
return nil, err
}
// TODO: check team membership and return error if not a reader?
// Return a dummy value here.
return &kbfsblock.QuotaInfo{Limit: math.MaxInt64}, nil
}