forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_ops.go
214 lines (183 loc) · 5.71 KB
/
block_ops.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
// 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 (
"github.com/keybase/kbfs/kbfsblock"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/keybase/kbfs/tlf"
"golang.org/x/net/context"
)
type blockOpsConfig interface {
dataVersioner
logMaker
blockCacher
blockServerGetter
codecGetter
cryptoPureGetter
keyGetterGetter
diskBlockCacheGetter
syncedTlfGetterSetter
initModeGetter
}
// BlockOpsStandard implements the BlockOps interface by relaying
// requests to the block server.
type BlockOpsStandard struct {
config blockOpsConfig
log traceLogger
queue *blockRetrievalQueue
}
var _ BlockOps = (*BlockOpsStandard)(nil)
// NewBlockOpsStandard creates a new BlockOpsStandard
func NewBlockOpsStandard(config blockOpsConfig,
queueSize, prefetchQueueSize int) *BlockOpsStandard {
bg := &realBlockGetter{config: config}
qConfig := &realBlockRetrievalConfig{
blockRetrievalPartialConfig: config,
bg: bg,
}
q := newBlockRetrievalQueue(queueSize, prefetchQueueSize, qConfig)
bops := &BlockOpsStandard{
config: config,
log: traceLogger{config.MakeLogger("")},
queue: q,
}
return bops
}
// Get implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Get(ctx context.Context, kmd KeyMetadata,
blockPtr BlockPointer, block Block, lifetime BlockCacheLifetime) error {
// Check the journal explicitly first, so we don't get stuck in
// the block-fetching queue.
if journalBServer, ok := b.config.BlockServer().(journalBlockServer); ok {
data, serverHalf, found, err := journalBServer.getBlockFromJournal(
kmd.TlfID(), blockPtr.ID)
if err != nil {
return err
}
if found {
return assembleBlock(
ctx, b.config.keyGetter(), b.config.Codec(),
b.config.cryptoPure(), kmd, blockPtr, block, data, serverHalf)
}
}
b.log.LazyTrace(ctx, "BOps: Requesting %s", blockPtr.ID)
errCh := b.queue.Request(ctx, defaultOnDemandRequestPriority, kmd,
blockPtr, block, lifetime)
err := <-errCh
b.log.LazyTrace(ctx, "BOps: Request fulfilled for %s (err=%v)", blockPtr.ID, err)
return err
}
// GetEncodedSize implements the BlockOps interface for
// BlockOpsStandard.
func (b *BlockOpsStandard) GetEncodedSize(ctx context.Context, kmd KeyMetadata,
blockPtr BlockPointer) (uint32, error) {
// Check the journal explicitly first, so we don't get stuck in
// the block-fetching queue.
if journalBServer, ok := b.config.BlockServer().(journalBlockServer); ok {
size, found, err := journalBServer.getBlockSizeFromJournal(
kmd.TlfID(), blockPtr.ID)
if err != nil {
return 0, err
}
if found {
return size, nil
}
}
// Otherwise fetch the entire block from the server, since we
// can't trust the server to report the size without being able
// to verify the BlockID.
block := NewCommonBlock()
errCh := b.queue.Request(ctx, defaultOnDemandRequestPriority, kmd,
blockPtr, block, NoCacheEntry)
err := <-errCh
if err != nil {
return 0, err
}
return block.GetEncodedSize(), nil
}
// Ready implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Ready(ctx context.Context, kmd KeyMetadata,
block Block) (id kbfsblock.ID, plainSize int, readyBlockData ReadyBlockData,
err error) {
defer func() {
if err != nil {
id = kbfsblock.ID{}
plainSize = 0
readyBlockData = ReadyBlockData{}
}
}()
crypto := b.config.cryptoPure()
tlfCryptKey, err := b.config.keyGetter().
GetTLFCryptKeyForEncryption(ctx, kmd)
if err != nil {
return
}
// New server key half for the block.
serverHalf, err := crypto.MakeRandomBlockCryptKeyServerHalf()
if err != nil {
return
}
blockKey := kbfscrypto.UnmaskBlockCryptKey(serverHalf, tlfCryptKey)
plainSize, encryptedBlock, err := crypto.EncryptBlock(block, blockKey)
if err != nil {
return
}
buf, err := b.config.Codec().Encode(encryptedBlock)
if err != nil {
return
}
readyBlockData = ReadyBlockData{
buf: buf,
serverHalf: serverHalf,
}
encodedSize := readyBlockData.GetEncodedSize()
if encodedSize < plainSize {
err = TooLowByteCountError{
ExpectedMinByteCount: plainSize,
ByteCount: encodedSize,
}
return
}
id, err = kbfsblock.MakePermanentID(buf)
if err != nil {
return
}
// Cache the encoded size.
block.SetEncodedSize(uint32(encodedSize))
return
}
// Delete implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Delete(ctx context.Context, tlfID tlf.ID,
ptrs []BlockPointer) (liveCounts map[kbfsblock.ID]int, err error) {
contexts := make(kbfsblock.ContextMap)
for _, ptr := range ptrs {
contexts[ptr.ID] = append(contexts[ptr.ID], ptr.Context)
}
return b.config.BlockServer().RemoveBlockReferences(ctx, tlfID, contexts)
}
// Archive implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Archive(ctx context.Context, tlfID tlf.ID,
ptrs []BlockPointer) error {
contexts := make(kbfsblock.ContextMap)
for _, ptr := range ptrs {
contexts[ptr.ID] = append(contexts[ptr.ID], ptr.Context)
}
return b.config.BlockServer().ArchiveBlockReferences(ctx, tlfID, contexts)
}
// TogglePrefetcher implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) TogglePrefetcher(enable bool) <-chan struct{} {
return b.queue.TogglePrefetcher(enable, nil)
}
// Prefetcher implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Prefetcher() Prefetcher {
return b.queue.Prefetcher()
}
// BlockRetriever implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) BlockRetriever() BlockRetriever {
return b.queue
}
// Shutdown implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Shutdown() {
b.queue.Shutdown()
}