forked from GoogleCloudPlatform/gcsfuse
/
file.go
504 lines (422 loc) · 11.6 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
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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 inode
import (
"fmt"
"io"
"time"
"github.com/googlecloudplatform/gcsfuse/internal/gcsx"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/syncutil"
"github.com/jacobsa/timeutil"
"golang.org/x/net/context"
)
// A GCS object metadata key for file mtimes. mtimes are UTC, and are stored in
// the format defined by time.RFC3339Nano.
const FileMtimeMetadataKey = gcsx.MtimeMetadataKey
type FileInode struct {
/////////////////////////
// Dependencies
/////////////////////////
bucket gcs.Bucket
syncer gcsx.Syncer
mtimeClock timeutil.Clock
/////////////////////////
// Constant data
/////////////////////////
id fuseops.InodeID
name string
attrs fuseops.InodeAttributes
tempDir string
/////////////////////////
// Mutable state
/////////////////////////
// A mutex that must be held when calling certain methods. See documentation
// for each method.
mu syncutil.InvariantMutex
// GUARDED_BY(mu)
lc lookupCount
// The source object from which this inode derives.
//
// INVARIANT: src.Name == name
//
// GUARDED_BY(mu)
src gcs.Object
// The current content of this inode, or nil if the source object is still
// authoritative.
content gcsx.TempFile
// Has Destroy been called?
//
// GUARDED_BY(mu)
destroyed bool
}
var _ Inode = &FileInode{}
// Create a file inode for the given object in GCS. The initial lookup count is
// zero.
//
// REQUIRES: o != nil
// REQUIRES: o.Generation > 0
// REQUIRES: o.MetaGeneration > 0
// REQUIRES: len(o.Name) > 0
// REQUIRES: o.Name[len(o.Name)-1] != '/'
func NewFileInode(
id fuseops.InodeID,
o *gcs.Object,
attrs fuseops.InodeAttributes,
bucket gcs.Bucket,
syncer gcsx.Syncer,
tempDir string,
mtimeClock timeutil.Clock) (f *FileInode) {
// Set up the basic struct.
f = &FileInode{
bucket: bucket,
syncer: syncer,
mtimeClock: mtimeClock,
id: id,
name: o.Name,
attrs: attrs,
tempDir: tempDir,
src: *o,
}
f.lc.Init(id)
// Set up invariant checking.
f.mu = syncutil.NewInvariantMutex(f.checkInvariants)
return
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) checkInvariants() {
if f.destroyed {
return
}
// Make sure the name is legal.
name := f.Name()
if len(name) == 0 || name[len(name)-1] == '/' {
panic("Illegal file name: " + name)
}
// INVARIANT: src.Name == name
if f.src.Name != name {
panic(fmt.Sprintf("Name mismatch: %q vs. %q", f.src.Name, name))
}
// INVARIANT: content.CheckInvariants() does not panic
if f.content != nil {
f.content.CheckInvariants()
}
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) clobbered(ctx context.Context) (b bool, err error) {
// Stat the object in GCS.
req := &gcs.StatObjectRequest{Name: f.name}
o, err := f.bucket.StatObject(ctx, req)
// Special case: "not found" means we have been clobbered.
if _, ok := err.(*gcs.NotFoundError); ok {
err = nil
b = true
return
}
// Propagate other errors.
if err != nil {
err = fmt.Errorf("StatObject: %v", err)
return
}
// We are clobbered iff the generation doesn't match our source generation.
oGen := Generation{o.Generation, o.MetaGeneration}
b = f.SourceGeneration().Compare(oGen) != 0
return
}
// Ensure that f.content != nil
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) ensureContent(ctx context.Context) (err error) {
// Is there anything to do?
if f.content != nil {
return
}
// Open a reader for the generation we care about.
rc, err := f.bucket.NewReader(
ctx,
&gcs.ReadObjectRequest{
Name: f.src.Name,
Generation: f.src.Generation,
})
if err != nil {
err = fmt.Errorf("NewReader: %v", err)
return
}
defer rc.Close()
// Create a temporary file with its contents.
tf, err := gcsx.NewTempFile(rc, f.tempDir, f.mtimeClock)
if err != nil {
err = fmt.Errorf("NewTempFile: %v", err)
return
}
// Update state.
f.content = tf
return
}
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
func (f *FileInode) Lock() {
f.mu.Lock()
}
func (f *FileInode) Unlock() {
f.mu.Unlock()
}
func (f *FileInode) ID() fuseops.InodeID {
return f.id
}
func (f *FileInode) Name() string {
return f.name
}
// Return a record for the GCS object from which this inode is branched. The
// record is guaranteed not to be modified, and users must not modify it.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Source() *gcs.Object {
// Make a copy, since we modify f.src.
o := f.src
return &o
}
// If true, it is safe to serve reads directly from the object given by
// f.Source(), rather than calling f.ReadAt. Doing so may be more efficient,
// because f.ReadAt may cause the entire object to be faulted in and requires
// the inode to be locked during the read.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) SourceGenerationIsAuthoritative() bool {
return f.content == nil
}
// Equivalent to the generation returned by f.Source().
//
// LOCKS_REQUIRED(f)
func (f *FileInode) SourceGeneration() (g Generation) {
g.Object = f.src.Generation
g.Metadata = f.src.MetaGeneration
return
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) IncrementLookupCount() {
f.lc.Inc()
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) DecrementLookupCount(n uint64) (destroy bool) {
destroy = f.lc.Dec(n)
return
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Destroy() (err error) {
f.destroyed = true
if f.content != nil {
f.content.Destroy()
}
return
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Attributes(
ctx context.Context) (attrs fuseops.InodeAttributes, err error) {
attrs = f.attrs
// Obtain default information from the source object.
attrs.Mtime = f.src.Updated
attrs.Size = uint64(f.src.Size)
// We require only that atime and ctime be "reasonable".
attrs.Atime = attrs.Mtime
attrs.Ctime = attrs.Mtime
// If the source object has an mtime metadata key, use that instead of its
// update time.
if formatted, ok := f.src.Metadata["gcsfuse_mtime"]; ok {
attrs.Mtime, err = time.Parse(time.RFC3339Nano, formatted)
if err != nil {
err = fmt.Errorf("time.Parse(%q): %v", formatted, err)
return
}
}
// If we've got local content, its size and (maybe) mtime take precedence.
if f.content != nil {
var sr gcsx.StatResult
sr, err = f.content.Stat()
if err != nil {
err = fmt.Errorf("Stat: %v", err)
return
}
attrs.Size = uint64(sr.Size)
if sr.Mtime != nil {
attrs.Mtime = *sr.Mtime
}
}
// If the object has been clobbered, we reflect that as the inode being
// unlinked.
clobbered, err := f.clobbered(ctx)
if err != nil {
err = fmt.Errorf("clobbered: %v", err)
return
}
if !clobbered {
attrs.Nlink = 1
}
return
}
// Serve a read for this file with semantics matching io.ReaderAt.
//
// The caller may be better off reading directly from GCS when
// f.SourceGenerationIsAuthoritative() is true.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Read(
ctx context.Context,
dst []byte,
offset int64) (n int, err error) {
// Make sure f.content != nil.
err = f.ensureContent(ctx)
if err != nil {
err = fmt.Errorf("ensureContent: %v", err)
return
}
// Read from the local content, propagating io.EOF.
n, err = f.content.ReadAt(dst, offset)
switch {
case err == io.EOF:
return
case err != nil:
err = fmt.Errorf("content.ReadAt: %v", err)
return
}
return
}
// Serve a write for this file with semantics matching fuseops.WriteFileOp.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Write(
ctx context.Context,
data []byte,
offset int64) (err error) {
// Make sure f.content != nil.
err = f.ensureContent(ctx)
if err != nil {
err = fmt.Errorf("ensureContent: %v", err)
return
}
// Write to the mutable content. Note that io.WriterAt guarantees it returns
// an error for short writes.
_, err = f.content.WriteAt(data, offset)
return
}
// Set the mtime for this file. May involve a round trip to GCS.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) SetMtime(
ctx context.Context,
mtime time.Time) (err error) {
// If we have a local temp file, stat it.
var sr gcsx.StatResult
if f.content != nil {
sr, err = f.content.Stat()
if err != nil {
err = fmt.Errorf("Stat: %v", err)
return
}
}
// If the local content is dirty, simply update its mtime and return. This
// will cause the object in the bucket to be updated once we sync. If we lose
// power or something the mtime update will be lost, but so will the file
// data modifications so this doesn't seem so bad. It's worth saving the
// round trip to GCS for the common case of Linux writeback caching, where we
// always receive a setattr request just before a flush of a dirty file.
if sr.Mtime != nil {
f.content.SetMtime(mtime)
return
}
// Otherwise, update the backing object's metadata.
formatted := mtime.UTC().Format(time.RFC3339Nano)
srcGen := f.SourceGeneration()
req := &gcs.UpdateObjectRequest{
Name: f.src.Name,
Generation: srcGen.Object,
MetaGenerationPrecondition: &srcGen.Metadata,
Metadata: map[string]*string{
FileMtimeMetadataKey: &formatted,
},
}
o, err := f.bucket.UpdateObject(ctx, req)
switch err.(type) {
case nil:
f.src = *o
return
case *gcs.NotFoundError:
// Special case: silently ignore not found errors, which mean the file has
// been unlinked.
err = nil
return
case *gcs.PreconditionError:
// Special case: silently ignore precondition errors, which we also take to
// mean the file has been unlinked.
err = nil
return
default:
err = fmt.Errorf("UpdateObject: %v", err)
return
}
}
// Write out contents to GCS. If this fails due to the generation having been
// clobbered, treat it as a non-error (simulating the inode having been
// unlinked).
//
// After this method succeeds, SourceGeneration will return the new generation
// by which this inode should be known (which may be the same as before). If it
// fails, the generation will not change.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Sync(ctx context.Context) (err error) {
// If we have not been dirtied, there is nothing to do.
if f.content == nil {
return
}
// Write out the contents if they are dirty.
newObj, err := f.syncer.SyncObject(ctx, &f.src, f.content)
// Special case: a precondition error means we were clobbered, which we treat
// as being unlinked. There's no reason to return an error in that case.
if _, ok := err.(*gcs.PreconditionError); ok {
err = nil
}
// Propagate other errors.
if err != nil {
err = fmt.Errorf("SyncObject: %v", err)
return
}
// If we wrote out a new object, we need to update our state.
if newObj != nil {
f.src = *newObj
f.content = nil
}
return
}
// Truncate the file to the specified size.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Truncate(
ctx context.Context,
size int64) (err error) {
// Make sure f.content != nil.
err = f.ensureContent(ctx)
if err != nil {
err = fmt.Errorf("ensureContent: %v", err)
return
}
// Call through.
err = f.content.Truncate(size)
return
}