forked from mitre/fusera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
332 lines (277 loc) · 7.2 KB
/
file.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
// Copyright 2015 - 2017 Ka-Hing Cheung
// Modifications Copyright 2018 The MITRE Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fusera
import (
"fmt"
"io"
"sync"
"syscall"
"time"
"github.com/mattrbianchi/twig"
"github.com/mitre/fusera/awsutil"
"github.com/mitre/fusera/nr"
"github.com/pkg/errors"
"github.com/jacobsa/fuse"
)
type FileHandle struct {
inode *Inode
mpuKey *string
dirty bool
writeInit sync.Once
mpuWG sync.WaitGroup
etags []*string
mu sync.Mutex
mpuId *string
nextWriteOffset int64
lastPartId int
poolHandle *BufferPool
buf *MBuf
lastWriteError error
// read
reader io.ReadCloser
readBufOffset int64
// parallel read
buffers []*S3ReadBuffer
existingReadahead int
seqReadAmount uint64
numOOORead uint64 // number of out of order read
}
const MAX_READAHEAD = uint32(100 * 1024 * 1024)
const READAHEAD_CHUNK = uint32(20 * 1024 * 1024)
func NewFileHandle(in *Inode) *FileHandle {
fh := &FileHandle{inode: in}
return fh
}
type S3ReadBuffer struct {
offset uint64
size uint32
buf *Buffer
}
func (b *S3ReadBuffer) Read(offset uint64, p []byte) (n int, err error) {
if b.offset == offset {
n, err = io.ReadFull(b.buf, p)
if n != 0 && err == io.ErrUnexpectedEOF {
err = nil
}
if n > 0 {
if uint32(n) > b.size {
panic(fmt.Sprintf("read more than available %v %v", n, b.size))
}
b.offset += uint64(n)
b.size -= uint32(n)
}
return
} else {
panic(fmt.Sprintf("not the right buffer, expecting %v got %v, %v left", b.offset, offset, b.size))
// err = errors.New(fmt.Sprintf("not the right buffer, expecting %v got %v", b.offset, offset))
// return
}
}
func (fh *FileHandle) readFromReadAhead(offset uint64, buf []byte) (bytesRead int, err error) {
var nread int
for len(fh.buffers) != 0 {
nread, err = fh.buffers[0].Read(offset+uint64(bytesRead), buf)
bytesRead += nread
if err != nil {
return
}
if fh.buffers[0].size == 0 {
// we've exhausted the first buffer
fh.buffers[0].buf.Close()
fh.buffers = fh.buffers[1:]
}
buf = buf[nread:]
if len(buf) == 0 {
// we've filled the user buffer
return
}
}
return
}
func (fh *FileHandle) ReadFile(offset int64, buf []byte) (bytesRead int, err error) {
// fh.inode.logFuse("ReadFile", offset, len(buf))
defer func() {
// fh.inode.logFuse("< ReadFile", bytesRead, err)
if err != nil {
if err == io.EOF {
err = nil
}
}
}()
fh.mu.Lock()
defer fh.mu.Unlock()
nwant := len(buf)
var nread int
for bytesRead < nwant && err == nil {
nread, err = fh.readFile(offset+int64(bytesRead), buf[bytesRead:])
if nread > 0 {
bytesRead += nread
}
}
return
}
func (fh *FileHandle) readFile(offset int64, buf []byte) (bytesRead int, err error) {
defer func() {
if bytesRead > 0 {
fh.readBufOffset += int64(bytesRead)
fh.seqReadAmount += uint64(bytesRead)
}
// fh.inode.logFuse("< readFile", bytesRead, err)
}()
if uint64(offset) >= fh.inode.Attributes.Size {
twig.Debug("nothing to read")
// nothing to read
if fh.inode.Invalid {
err = fuse.ENOENT
} else if fh.inode.KnownSize == nil {
err = io.EOF
} else {
err = io.EOF
}
return
}
fs := fh.inode.fs
if fh.poolHandle == nil {
fh.poolHandle = fs.bufferPool
}
if fh.readBufOffset != offset {
// XXX out of order read, maybe disable prefetching
// fh.inode.logFuse("out of order read", offset, fh.readBufOffset)
fh.readBufOffset = offset
fh.seqReadAmount = 0
if fh.reader != nil {
fh.reader.Close()
fh.reader = nil
}
if fh.buffers != nil {
// we misdetected
fh.numOOORead++
}
for _, b := range fh.buffers {
b.buf.Close()
}
fh.buffers = nil
}
bytesRead, err = fh.readFromStream(offset, buf)
return
}
func (fh *FileHandle) Release() {
// read buffers
for _, b := range fh.buffers {
b.buf.Close()
}
fh.buffers = nil
if fh.reader != nil {
fh.reader.Close()
}
// write buffers
if fh.poolHandle != nil {
if fh.buf != nil && fh.buf.buffers != nil {
if fh.lastWriteError == nil {
panic("buf not freed but error is nil")
}
fh.buf.Free()
// the other in-flight multipart PUT buffers will be
// freed when they finish/error out
}
}
fh.inode.mu.Lock()
defer fh.inode.mu.Unlock()
if fh.inode.fileHandles == 0 {
panic(fh.inode.fileHandles)
}
fh.inode.fileHandles -= 1
}
// Returns the number of bytes read and a file error if one occured.
func (fh *FileHandle) readFromStream(offset int64, buf []byte) (bytesRead int, err error) {
defer func() {
if fh.inode.fs.opt.DebugFuse {
// fh.inode.logFuse("< readFromStream", bytesRead)
}
}()
if uint64(offset) >= fh.inode.Attributes.Size {
// nothing to read
return
}
if fh.reader == nil {
sd, _ := time.ParseDuration("30s")
exp := fh.inode.Attributes.ExpirationDate
if !exp.IsZero() {
twig.Debugf("seems like we have a url that expires: %s", exp)
if time.Until(exp) < sd {
twig.Debug("url is expired")
// Time to hot swap urls!
link, err := newURL(fh.inode)
if err != nil {
// fh.inode.logFuse("< readFromStream error", 0, err)
return 0, syscall.EACCES
}
fh.inode.Link = link
}
}
bytes := ""
if offset != 0 {
bytes = fmt.Sprintf("bytes=%v-", offset)
}
resp, err := awsutil.GetObjectRange(fh.inode.Link, bytes)
if err != nil {
return 0, err
}
fh.reader = resp.Body
}
bytesRead, err = fh.reader.Read(buf)
if err != nil {
twig.Debug("error reading file")
twig.Debug(err.Error())
if err != io.EOF {
twig.Debugf("readFromStream error: %s", err.Error())
// fh.inode.logFuse("< readFromStream error", bytesRead, err)
}
// always retry error on read
fh.reader.Close()
fh.reader = nil
err = nil
}
return
}
func newURL(inode *Inode) (string, error) {
errfmtstr := "\naccession: %s\nfile: %s\n"
payload, err := nr.ResolveNames(inode.fs.opt.ApiEndpoint, inode.fs.opt.Loc, inode.fs.opt.Ngc, map[string]bool{inode.Acc: true})
if err != nil {
return "", errors.Wrapf(err, "issue contacting API while trying to renew signed url for:%s", errfmtstr, inode.Acc, inode.Name)
}
twig.Debug("resolved a url")
for _, p := range payload {
for _, f := range p.Files {
if f.Name == *inode.Name {
twig.Debug("got a new link")
if f.Link == "" {
return "", errors.Errorf("API did not give new signed url for:%s", errfmtstr, inode.Acc, inode.Name)
}
return f.Link, nil
}
}
}
twig.Debug("did not get a new link")
return "", errors.Errorf("couldn't get new signed url for:%s", errfmtstr, inode.Acc, inode.Name)
}
func (fh *FileHandle) resetToKnownSize() {
if fh.inode.KnownSize != nil {
fh.inode.Attributes.Size = *fh.inode.KnownSize
} else {
fh.inode.Attributes.Size = 0
fh.inode.Invalid = true
}
}