forked from GoogleCloudPlatform/gcsfuse
/
fs.go
1651 lines (1396 loc) · 41.1 KB
/
fs.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 fs
import (
"errors"
"fmt"
"io"
"log"
"os"
"reflect"
"time"
"github.com/googlecloudplatform/gcsfuse/internal/fs/handle"
"github.com/googlecloudplatform/gcsfuse/internal/fs/inode"
"github.com/googlecloudplatform/gcsfuse/internal/gcsx"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/syncutil"
"github.com/jacobsa/timeutil"
"golang.org/x/net/context"
)
type ServerConfig struct {
// A clock used for cache expiration. It is *not* used for inode times, for
// which we use the wall clock.
CacheClock timeutil.Clock
// The bucket that the file system is to export.
Bucket gcs.Bucket
// The temporary directory to use for local caching, or the empty string to
// use the system default.
TempDir string
// By default, if a bucket contains the object "foo/bar" but no object named
// "foo/", it's as if the directory doesn't exist. This allows us to have
// non-flaky name resolution code.
//
// Setting this bool to true enables a mode where object listings are
// consulted to allow for the directory in the situation above to exist. Note
// that this has drawbacks in the form of name resolution flakiness and
// surprising behavior.
//
// See docs/semantics.md for more info.
ImplicitDirectories bool
// How long to allow the kernel to cache inode attributes.
//
// Any given object generation in GCS is immutable, and a new generation
// results in a new inode number. So every update from a remote system results
// in a new inode number, and it's therefore safe to allow the kernel to cache
// inode attributes.
//
// The one exception to the above logic is that objects can be _deleted_, in
// which case stat::st_nlink changes. So choosing this value comes down to
// whether you care about that field being up to date.
InodeAttributeCacheTTL time.Duration
// If non-zero, each directory will maintain a cache from child name to
// information about whether that name exists as a file and/or directory.
// This may speed up calls to look up and stat inodes, especially when
// combined with a stat-caching GCS bucket, but comes at the cost of
// consistency: if the child is removed and recreated with a different type
// before the expiration, we may fail to find it.
DirTypeCacheTTL time.Duration
// The UID and GID that owns all inodes in the file system.
Uid uint32
Gid uint32
// Permissions bits to use for files and directories. No bits outside of
// os.ModePerm may be set.
FilePerms os.FileMode
DirPerms os.FileMode
// Files backed by on object of length at least AppendThreshold that have
// only been appended to (i.e. none of the object's contents have been
// dirtied) will be written out by "appending" to the object in GCS with this
// process:
//
// 1. Write out a temporary object containing the appended contents whose
// name begins with TmpObjectPrefix.
//
// 2. Compose the original object and the temporary object on top of the
// original object.
//
// 3. Delete the temporary object.
//
// Note that if the process fails or is interrupted the temporary object will
// not be cleaned up, so the user must ensure that TmpObjectPrefix is
// periodically garbage collected.
AppendThreshold int64
TmpObjectPrefix string
}
// Create a fuse file system server according to the supplied configuration.
func NewServer(cfg *ServerConfig) (server fuse.Server, err error) {
// Check permissions bits.
if cfg.FilePerms&^os.ModePerm != 0 {
err = fmt.Errorf("Illegal file perms: %v", cfg.FilePerms)
return
}
if cfg.DirPerms&^os.ModePerm != 0 {
err = fmt.Errorf("Illegal dir perms: %v", cfg.FilePerms)
return
}
// Set up a bucket that infers content types when creating files.
bucket := gcsx.NewContentTypeBucket(cfg.Bucket)
// Create the object syncer.
if cfg.TmpObjectPrefix == "" {
err = errors.New("You must set TmpObjectPrefix.")
return
}
syncer := gcsx.NewSyncer(
cfg.AppendThreshold,
cfg.TmpObjectPrefix,
bucket)
// Set up the basic struct.
fs := &fileSystem{
mtimeClock: timeutil.RealClock(),
cacheClock: cfg.CacheClock,
bucket: bucket,
syncer: syncer,
tempDir: cfg.TempDir,
implicitDirs: cfg.ImplicitDirectories,
inodeAttributeCacheTTL: cfg.InodeAttributeCacheTTL,
dirTypeCacheTTL: cfg.DirTypeCacheTTL,
uid: cfg.Uid,
gid: cfg.Gid,
fileMode: cfg.FilePerms,
dirMode: cfg.DirPerms | os.ModeDir,
inodes: make(map[fuseops.InodeID]inode.Inode),
nextInodeID: fuseops.RootInodeID + 1,
generationBackedInodes: make(map[string]inode.GenerationBackedInode),
implicitDirInodes: make(map[string]inode.DirInode),
handles: make(map[fuseops.HandleID]interface{}),
}
// Set up the root inode.
root := inode.NewDirInode(
fuseops.RootInodeID,
"", // name
fuseops.InodeAttributes{
Uid: fs.uid,
Gid: fs.gid,
Mode: fs.dirMode,
// We guarantee only that directory times be "reasonable".
Atime: fs.mtimeClock.Now(),
Ctime: fs.mtimeClock.Now(),
Mtime: fs.mtimeClock.Now(),
},
fs.implicitDirs,
fs.dirTypeCacheTTL,
fs.bucket,
fs.mtimeClock,
fs.cacheClock)
root.Lock()
root.IncrementLookupCount()
fs.inodes[fuseops.RootInodeID] = root
fs.implicitDirInodes[root.Name()] = root
root.Unlock()
// Set up invariant checking.
fs.mu = syncutil.NewInvariantMutex(fs.checkInvariants)
// Periodically garbage collect temporary objects.
var gcCtx context.Context
gcCtx, fs.stopGarbageCollecting = context.WithCancel(context.Background())
go garbageCollect(gcCtx, cfg.TmpObjectPrefix, fs.bucket)
server = fuseutil.NewFileSystemServer(fs)
return
}
////////////////////////////////////////////////////////////////////////
// fileSystem type
////////////////////////////////////////////////////////////////////////
// LOCK ORDERING
//
// Let FS be the file system lock. Define a strict partial order < as follows:
//
// 1. For any inode lock I, I < FS.
// 2. For any handle lock H and inode lock I, H < I.
//
// We follow the rule "acquire A then B only if A < B".
//
// In other words:
//
// * Don't hold multiple handle locks at the same time.
// * Don't hold multiple inode locks at the same time.
// * Don't acquire inode locks before handle locks.
// * Don't acquire file system locks before either.
//
// The intuition is that we hold inode and handle locks for long-running
// operations, and we don't want to block the entire file system on those.
//
// See http://goo.gl/rDxxlG for more discussion, including an informal proof
// that a strict partial order is sufficient.
type fileSystem struct {
fuseutil.NotImplementedFileSystem
/////////////////////////
// Dependencies
/////////////////////////
mtimeClock timeutil.Clock
cacheClock timeutil.Clock
bucket gcs.Bucket
syncer gcsx.Syncer
/////////////////////////
// Constant data
/////////////////////////
tempDir string
implicitDirs bool
inodeAttributeCacheTTL time.Duration
dirTypeCacheTTL time.Duration
// The user and group owning everything in the file system.
uid uint32
gid uint32
// Mode bits for all inodes.
fileMode os.FileMode
dirMode os.FileMode
// A function that shuts down the garbage collector.
stopGarbageCollecting func()
/////////////////////////
// Mutable state
/////////////////////////
// A lock protecting the state of the file system struct itself (distinct
// from per-inode locks). Make sure to see the notes on lock ordering above.
mu syncutil.InvariantMutex
// The next inode ID to hand out. We assume that this will never overflow,
// since even if we were handing out inode IDs at 4 GHz, it would still take
// over a century to do so.
//
// GUARDED_BY(mu)
nextInodeID fuseops.InodeID
// The collection of live inodes, keyed by inode ID. No ID less than
// fuseops.RootInodeID is ever used.
//
// INVARIANT: For all keys k, fuseops.RootInodeID <= k < nextInodeID
// INVARIANT: For all keys k, inodes[k].ID() == k
// INVARIANT: inodes[fuseops.RootInodeID] is missing or of type inode.DirInode
// INVARIANT: For all v, if IsDirName(v.Name()) then v is inode.DirInode
//
// GUARDED_BY(mu)
inodes map[fuseops.InodeID]inode.Inode
// A map from object name to an inode for that name backed by a GCS object.
// Populated during the name -> inode lookup process, cleared during the
// forget inode process.
//
// Entries may be stale for two reasons:
//
// 1. There is a newer generation in GCS, not caused by the inode. The next
// name lookup will detect this by statting the object, acquiring the
// inode's lock (to get an up to date look at what the latest generation
// the inode caused was), and replacing the entry if the inode's
// generation is less than the stat generation.
//
// 2. The object no longer exists. This is harmless; the name lookup process
// will return ENOENT before it ever consults this map. Eventually the
// kernel will send ForgetInodeOp and we will clear the entry.
//
// Crucially, we never replace an up to date entry with a stale one. If the
// name lookup process sees that the stat result is older than the inode, it
// starts over, statting again.
//
// Note that there is no invariant that says *all* of the object-backed
// inodes are represented here because we may have multiple distinct inodes
// for a given name existing concurrently if we observe an object generation
// that was not caused by our existing inode (e.g. if the file is clobbered
// remotely). We must retain the old inode until the kernel tells us to
// forget it.
//
// INVARIANT: For each k/v, v.Name() == k
// INVARIANT: For each value v, inodes[v.ID()] == v
//
// GUARDED_BY(mu)
generationBackedInodes map[string]inode.GenerationBackedInode
// A map from object name to the implicit directory inode that represents
// that name, if any. There can be at most one implicit directory inode for a
// given name accessible to us at any given time.
//
// INVARIANT: For each k/v, v.Name() == k
// INVARIANT: For each value v, inodes[v.ID()] == v
// INVARIANT: For each value v, v is not ExplicitDirInode
// INVARIANT: For each in in inodes such that in is DirInode but not
// ExplicitDirInode, implicitDirInodes[d.Name()] == d
//
// GUARDED_BY(mu)
implicitDirInodes map[string]inode.DirInode
// The collection of live handles, keyed by handle ID.
//
// INVARIANT: All values are of type *dirHandle or *handle.FileHandle
//
// GUARDED_BY(mu)
handles map[fuseops.HandleID]interface{}
// The next handle ID to hand out. We assume that this will never overflow.
//
// INVARIANT: For all keys k in handles, k < nextHandleID
//
// GUARDED_BY(mu)
nextHandleID fuseops.HandleID
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
func (fs *fileSystem) checkInvariants() {
//////////////////////////////////
// inodes
//////////////////////////////////
// INVARIANT: For all keys k, fuseops.RootInodeID <= k < nextInodeID
for id, _ := range fs.inodes {
if id < fuseops.RootInodeID || id >= fs.nextInodeID {
panic(fmt.Sprintf("Illegal inode ID: %v", id))
}
}
// INVARIANT: For all keys k, inodes[k].ID() == k
for id, in := range fs.inodes {
if in.ID() != id {
panic(fmt.Sprintf("ID mismatch: %v vs. %v", in.ID(), id))
}
}
// INVARIANT: inodes[fuseops.RootInodeID] is missing or of type inode.DirInode
//
// The missing case is when we've received a forget request for the root
// inode, while unmounting.
switch in := fs.inodes[fuseops.RootInodeID].(type) {
case nil:
case inode.DirInode:
default:
panic(fmt.Sprintf("Unexpected type for root: %v", reflect.TypeOf(in)))
}
// INVARIANT: For all v, if IsDirName(v.Name()) then v is inode.DirInode
for _, in := range fs.inodes {
if inode.IsDirName(in.Name()) {
_, ok := in.(inode.DirInode)
if !ok {
panic(fmt.Sprintf(
"Unexpected inode type for name \"%s\": %v",
in.Name(),
reflect.TypeOf(in)))
}
}
}
//////////////////////////////////
// generationBackedInodes
//////////////////////////////////
// INVARIANT: For each k/v, v.Name() == k
for k, v := range fs.generationBackedInodes {
if !(v.Name() == k) {
panic(fmt.Sprintf(
"Unexpected name: \"%s\" vs. \"%s\"",
v.Name(),
k))
}
}
// INVARIANT: For each value v, inodes[v.ID()] == v
for _, v := range fs.generationBackedInodes {
if fs.inodes[v.ID()] != v {
panic(fmt.Sprintf(
"Mismatch for ID %v: %v %v",
v.ID(),
fs.inodes[v.ID()],
v))
}
}
//////////////////////////////////
// implicitDirInodes
//////////////////////////////////
// INVARIANT: For each k/v, v.Name() == k
for k, v := range fs.implicitDirInodes {
if !(v.Name() == k) {
panic(fmt.Sprintf(
"Unexpected name: \"%s\" vs. \"%s\"",
v.Name(),
k))
}
}
// INVARIANT: For each value v, inodes[v.ID()] == v
for _, v := range fs.implicitDirInodes {
if fs.inodes[v.ID()] != v {
panic(fmt.Sprintf(
"Mismatch for ID %v: %v %v",
v.ID(),
fs.inodes[v.ID()],
v))
}
}
// INVARIANT: For each value v, v is not ExplicitDirInode
for _, v := range fs.implicitDirInodes {
if _, ok := v.(inode.ExplicitDirInode); ok {
panic(fmt.Sprintf(
"Unexpected implicit dir inode %d, type %T",
v.ID(),
v))
}
}
// INVARIANT: For each in in inodes such that in is DirInode but not
// ExplicitDirInode, implicitDirInodes[d.Name()] == d
for _, in := range fs.inodes {
_, dir := in.(inode.DirInode)
_, edir := in.(inode.ExplicitDirInode)
if dir && !edir {
if !(fs.implicitDirInodes[in.Name()] == in) {
panic(fmt.Sprintf(
"implicitDirInodes mismatch: %q %v %v",
in.Name(),
fs.implicitDirInodes[in.Name()],
in))
}
}
}
//////////////////////////////////
// handles
//////////////////////////////////
// INVARIANT: All values are of type *dirHandle or *handle.FileHandle
for _, h := range fs.handles {
switch h.(type) {
case *dirHandle:
case *handle.FileHandle:
default:
panic(fmt.Sprintf("Unexpected handle type: %T", h))
}
}
//////////////////////////////////
// nextHandleID
//////////////////////////////////
// INVARIANT: For all keys k in handles, k < nextHandleID
for k, _ := range fs.handles {
if k >= fs.nextHandleID {
panic(fmt.Sprintf("Illegal handle ID: %v", k))
}
}
}
// Implementation detail of lookUpOrCreateInodeIfNotStale; do not use outside
// of that function.
//
// LOCKS_REQUIRED(fs.mu)
func (fs *fileSystem) mintInode(name string, o *gcs.Object) (in inode.Inode) {
// Choose an ID.
id := fs.nextInodeID
fs.nextInodeID++
// Create the inode.
switch {
// Explicit directories
case o != nil && inode.IsDirName(o.Name):
in = inode.NewExplicitDirInode(
id,
o,
fuseops.InodeAttributes{
Uid: fs.uid,
Gid: fs.gid,
Mode: fs.dirMode,
// We guarantee only that directory times be "reasonable".
Atime: fs.mtimeClock.Now(),
Ctime: fs.mtimeClock.Now(),
Mtime: fs.mtimeClock.Now(),
},
fs.implicitDirs,
fs.dirTypeCacheTTL,
fs.bucket,
fs.mtimeClock,
fs.cacheClock)
// Implicit directories
case inode.IsDirName(name):
in = inode.NewDirInode(
id,
name,
fuseops.InodeAttributes{
Uid: fs.uid,
Gid: fs.gid,
Mode: fs.dirMode,
// We guarantee only that directory times be "reasonable".
Atime: fs.mtimeClock.Now(),
Ctime: fs.mtimeClock.Now(),
Mtime: fs.mtimeClock.Now(),
},
fs.implicitDirs,
fs.dirTypeCacheTTL,
fs.bucket,
fs.mtimeClock,
fs.cacheClock)
case inode.IsSymlink(o):
in = inode.NewSymlinkInode(
id,
o,
fuseops.InodeAttributes{
Uid: fs.uid,
Gid: fs.gid,
Mode: fs.fileMode | os.ModeSymlink,
})
default:
in = inode.NewFileInode(
id,
o,
fuseops.InodeAttributes{
Uid: fs.uid,
Gid: fs.gid,
Mode: fs.fileMode,
},
fs.bucket,
fs.syncer,
fs.tempDir,
fs.mtimeClock)
}
// Place it in our map of IDs to inodes.
fs.inodes[in.ID()] = in
return
}
// Attempt to find an inode for the given name, backed by the supplied object
// record (or nil for implicit directories). Create one if one has never yet
// existed and, if the record is non-nil, the record is newer than any inode
// we've yet recorded.
//
// If the record is stale (i.e. some newer inode exists), return nil. In this
// case, the caller may obtain a fresh record and try again. Otherwise,
// increment the inode's lookup count and return it locked.
//
// UNLOCK_FUNCTION(fs.mu)
// LOCK_FUNCTION(in)
func (fs *fileSystem) lookUpOrCreateInodeIfNotStale(
name string,
o *gcs.Object) (in inode.Inode) {
// Sanity check.
if o != nil && name != o.Name {
panic(fmt.Sprintf("Name mismatch: %q vs. %q", name, o.Name))
}
// Ensure that no matter which inode we return, we increase its lookup count
// on the way out and then release the file system lock.
//
// INVARIANT: we return with fs.mu held, and with in.mu held with in != nil.
defer func() {
if in != nil {
in.IncrementLookupCount()
}
fs.mu.Unlock()
}()
// Handle implicit directories.
if o == nil {
if !inode.IsDirName(name) {
panic(fmt.Sprintf("Unexpected name for an implicit directory: %q", name))
}
// If we don't have an entry, create one.
var ok bool
in, ok = fs.implicitDirInodes[name]
if !ok {
in = fs.mintInode(name, nil)
fs.implicitDirInodes[in.Name()] = in.(inode.DirInode)
}
in.Lock()
return
}
oGen := inode.Generation{
Object: o.Generation,
Metadata: o.MetaGeneration,
}
// Retry loop for the stale index entry case below. On entry, we hold fs.mu
// but no inode lock.
for {
// Look at the current index entry.
existingInode, ok := fs.generationBackedInodes[o.Name]
// If we have no existing record for this name, mint an inode and return it.
if !ok {
in = fs.mintInode(o.Name, o)
fs.generationBackedInodes[in.Name()] = in.(inode.GenerationBackedInode)
in.Lock()
return
}
// Otherwise we need to read the inode's source generation below, which
// requires the inode's lock. We must not hold the inode lock while
// acquiring the file system lock, so drop it while acquiring the inode's
// lock, then reacquire.
fs.mu.Unlock()
existingInode.Lock()
fs.mu.Lock()
// Check that the index still points at this inode. If not, it's possible
// that the inode is in the process of being destroyed and is unsafe to
// use. Go around and try again.
if fs.generationBackedInodes[o.Name] != existingInode {
existingInode.Unlock()
continue
}
// Have we found the correct inode?
cmp := oGen.Compare(existingInode.SourceGeneration())
if cmp == 0 {
in = existingInode
return
}
// Is the object record stale? If so, return nil.
if cmp == -1 {
existingInode.Unlock()
return
}
// We've observed that the record is newer than the existing inode, while
// holding the inode lock, excluding concurrent actions by the inode (in
// particular concurrent calls to Sync, which changes generation numbers).
// This means we've proven that the record cannot have been caused by the
// inode's actions, and therefore this is not the inode we want.
//
// Replace it with a newly-mintend inode and then go around, acquiring its
// lock in accordance with our lock ordering rules.
existingInode.Unlock()
in = fs.mintInode(o.Name, o)
fs.generationBackedInodes[in.Name()] = in.(inode.GenerationBackedInode)
continue
}
}
// Look up the child with the given name within the parent, then return an
// existing inode for that child or create a new one if necessary. Return
// ENOENT if the child doesn't exist.
//
// Return the child locked, incrementing its lookup count.
//
// LOCKS_EXCLUDED(fs.mu)
// LOCKS_EXCLUDED(parent)
// LOCK_FUNCTION(child)
func (fs *fileSystem) lookUpOrCreateChildInode(
ctx context.Context,
parent inode.DirInode,
childName string) (child inode.Inode, err error) {
// Set up a function that will find a lookup result for the child with the
// given name. Expects no locks to be held.
getLookupResult := func() (r inode.LookUpResult, err error) {
parent.Lock()
defer parent.Unlock()
r, err = parent.LookUpChild(ctx, childName)
if err != nil {
err = fmt.Errorf("LookUpChild: %v", err)
return
}
return
}
// Run a retry loop around lookUpOrCreateInodeIfNotStale.
const maxTries = 3
for n := 0; n < maxTries; n++ {
// Create a record.
var result inode.LookUpResult
result, err = getLookupResult()
if err != nil {
return
}
if !result.Exists() {
err = fuse.ENOENT
return
}
// Attempt to create the inode. Return if successful.
fs.mu.Lock()
child = fs.lookUpOrCreateInodeIfNotStale(result.FullName, result.Object)
if child != nil {
return
}
}
err = fmt.Errorf("Did not converge after %v tries", maxTries)
return
}
// Synchronize the supplied file inode to GCS, updating the index as
// appropriate.
//
// LOCKS_EXCLUDED(fs.mu)
// LOCKS_REQUIRED(f)
func (fs *fileSystem) syncFile(
ctx context.Context,
f *inode.FileInode) (err error) {
// Sync the inode.
err = f.Sync(ctx)
if err != nil {
err = fmt.Errorf("FileInode.Sync: %v", err)
return
}
// We need not update fileIndex:
//
// We've held the inode lock the whole time, so there's no way that this
// inode could have been booted from the index. Therefore if it's not in the
// index at the moment, it must not have been in there when we started. That
// is, it must have been clobbered remotely, which we treat as unlinking.
//
// In other words, either this inode is still in the index or it has been
// unlinked and *should* be anonymous.
return
}
// Decrement the supplied inode's lookup count, destroying it if the inode says
// that it has hit zero.
//
// We require the file system lock to exclude concurrent lookups, which might
// otherwise find an inode whose lookup count has gone to zero.
//
// UNLOCK_FUNCTION(fs.mu)
// UNLOCK_FUNCTION(in)
func (fs *fileSystem) unlockAndDecrementLookupCount(
in inode.Inode,
N uint64) {
name := in.Name()
// Decrement the lookup count.
shouldDestroy := in.DecrementLookupCount(N)
// Update file system state, orphaning the inode if we're going to destroy it
// below.
if shouldDestroy {
delete(fs.inodes, in.ID())
// Update indexes if necessary.
if fs.generationBackedInodes[name] == in {
delete(fs.generationBackedInodes, name)
}
if fs.implicitDirInodes[name] == in {
delete(fs.implicitDirInodes, name)
}
}
// We are done with the file system.
fs.mu.Unlock()
// Now we can destroy the inode if necessary.
if shouldDestroy {
destroyErr := in.Destroy()
if destroyErr != nil {
log.Printf("Error destroying inode %q: %v", name, destroyErr)
}
}
in.Unlock()
}
// A helper function for use after incrementing an inode's lookup count.
// Ensures that the lookup count is decremented again if the caller is going to
// return in error (in which case the kernel and gcsfuse would otherwise
// disagree about the lookup count for the inode's ID), so that the inode isn't
// leaked.
//
// Typical usage:
//
// func (fs *fileSystem) doFoo() (err error) {
// in, err := fs.lookUpOrCreateInodeIfNotStale(...)
// if err != nil {
// return
// }
//
// defer fs.unlockAndMaybeDisposeOfInode(in, &err)
//
// ...
// }
//
// LOCKS_EXCLUDED(fs.mu)
// UNLOCK_FUNCTION(in)
func (fs *fileSystem) unlockAndMaybeDisposeOfInode(
in inode.Inode,
err *error) {
// If there is no error, just unlock.
if *err == nil {
in.Unlock()
return
}
// Otherwise, go through the decrement helper, which requires the file system
// lock.
fs.mu.Lock()
fs.unlockAndDecrementLookupCount(in, 1)
}
// Fetch attributes for the supplied inode and fill in an appropriate
// expiration time for them.
//
// LOCKS_REQUIRED(in)
func (fs *fileSystem) getAttributes(
ctx context.Context,
in inode.Inode) (
attr fuseops.InodeAttributes,
expiration time.Time,
err error) {
// Call through.
attr, err = in.Attributes(ctx)
if err != nil {
return
}
// Set up the expiration time.
if fs.inodeAttributeCacheTTL > 0 {
expiration = time.Now().Add(fs.inodeAttributeCacheTTL)
}
return
}
// inodeOrDie returns the inode with the given ID, panicking with a helpful
// error message if it doesn't exist.
//
// LOCKS_REQUIRED(fs.mu)
func (fs *fileSystem) inodeOrDie(id fuseops.InodeID) (in inode.Inode) {
in = fs.inodes[id]
if in == nil {
panic(fmt.Sprintf("inode %d doesn't exist", id))
}
return
}
// dirInodeOrDie returns the directory inode with the given ID, panicking with
// a helpful error message if it doesn't exist or is the wrong type.
//
// LOCKS_REQUIRED(fs.mu)
func (fs *fileSystem) dirInodeOrDie(id fuseops.InodeID) (in inode.DirInode) {
tmp := fs.inodes[id]
in, ok := tmp.(inode.DirInode)
if !ok {
panic(fmt.Sprintf("inode %d is %T, wanted inode.DirInode", id, tmp))
}
return
}
// fileInodeOrDie returns the file inode with the given ID, panicking with a
// helpful error message if it doesn't exist or is the wrong type.
//
// LOCKS_REQUIRED(fs.mu)
func (fs *fileSystem) fileInodeOrDie(id fuseops.InodeID) (in *inode.FileInode) {
tmp := fs.inodes[id]
in, ok := tmp.(*inode.FileInode)
if !ok {
panic(fmt.Sprintf("inode %d is %T, wanted *inode.FileInode", id, tmp))
}
return
}
// symlinkInodeOrDie returns the symlink inode with the given ID, panicking
// with a helpful error message if it doesn't exist or is the wrong type.
//
// LOCKS_REQUIRED(fs.mu)
func (fs *fileSystem) symlinkInodeOrDie(
id fuseops.InodeID) (in *inode.SymlinkInode) {
tmp := fs.inodes[id]
in, ok := tmp.(*inode.SymlinkInode)
if !ok {
panic(fmt.Sprintf("inode %d is %T, wanted *inode.SymlinkInode", id, tmp))
}
return
}
////////////////////////////////////////////////////////////////////////
// fuse.FileSystem methods
////////////////////////////////////////////////////////////////////////
func (fs *fileSystem) Destroy() {
fs.stopGarbageCollecting()
}
func (fs *fileSystem) StatFS(
ctx context.Context,
op *fuseops.StatFSOp) (err error) {
// Simulate a large amount of free space so that the Finder doesn't refuse to
// copy in files. (See issue #125.) Use 2^17 as the block size because that
// is the largest that OS X will pass on.
op.BlockSize = 1 << 17
op.Blocks = 1 << 33
op.BlocksFree = op.Blocks
op.BlocksAvailable = op.Blocks
// Similarly with inodes.
op.Inodes = 1 << 50
op.InodesFree = op.Inodes
// Prefer large transfers. This is the largest value that OS X will
// faithfully pass on, according to fuseops/ops.go.
op.IoSize = 1 << 20
return
}
// LOCKS_EXCLUDED(fs.mu)
func (fs *fileSystem) LookUpInode(
ctx context.Context,
op *fuseops.LookUpInodeOp) (err error) {
// Find the parent directory in question.
fs.mu.Lock()
parent := fs.dirInodeOrDie(op.Parent)
fs.mu.Unlock()
// Find or create the child inode.
child, err := fs.lookUpOrCreateChildInode(ctx, parent, op.Name)
if err != nil {
return
}
defer fs.unlockAndMaybeDisposeOfInode(child, &err)
// Fill out the response.
e := &op.Entry
e.Child = child.ID()
e.Attributes, e.AttributesExpiration, err = fs.getAttributes(ctx, child)
if err != nil {
return
}
return
}
// LOCKS_EXCLUDED(fs.mu)
func (fs *fileSystem) GetInodeAttributes(
ctx context.Context,
op *fuseops.GetInodeAttributesOp) (err error) {
// Find the inode.
fs.mu.Lock()