-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgoofys.go
More file actions
1230 lines (1119 loc) · 30.4 KB
/
Copy pathgoofys.go
File metadata and controls
1230 lines (1119 loc) · 30.4 KB
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 - 2017 Ka-Hing Cheung
// Copyright 2021 Yandex LLC
// Copyright 2024 Tigris Data, Inc.
//
// 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 core
import (
"context"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/jacobsa/fuse/fuseops"
"github.com/rs/zerolog"
"github.com/tigrisdata/tigrisfs/core/cfg"
"github.com/tigrisdata/tigrisfs/log"
)
// goofys is a Filey System written in Go. All the backend data is
// stored on S3 as is. It's a Filey System instead of a File System
// because it makes minimal effort at being POSIX
// compliant. Particularly things that are difficult to support on S3
// or would translate into more than one round-trip would either fail
// (rename non-empty dir) or faked (no per-file permission). goofys
// does not have a on disk data cache, and consistency model is
// close-to-open.
type Goofys struct {
bucket string
flags *cfg.FlagStorage
umask uint32
rootAttrs InodeAttributes
bufferPool *BufferPool
wantFree int32
shutdown int32
shutdownCh chan struct{}
// A lock protecting the state of the file system struct itself (distinct
// from per-inode locks). Should be always taken after any inode locks.
mu sync.RWMutex
flusherMu sync.Mutex
flusherCond *sync.Cond
flushPending int32
// 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
inodesByTime map[int64]map[fuseops.InodeID]bool
// Inflight changes are tracked to skip them in parallel listings
// Required because we don't have guarantees about listing & change ordering
inflightListingId int
inflightListings map[int]map[string]bool
inflightChanges map[string]int
nextHandleID fuseops.HandleID
dirHandles map[fuseops.HandleID]*DirHandle
fileHandles map[fuseops.HandleID]*FileHandle
activeFlushers int64
flushRetrySet int32
hasNewWrites uint64
flushPriorities []int64
forgotCnt uint32
cleanQueue BufferQueue
inodeQueue InodeQueue
zeroBuf []byte
diskFdQueue *FDQueue
stats OpStats
NotifyCallback func(notifications []interface{})
cloud atomic.Pointer[StorageBackend]
}
func (g *Goofys) setCloud(cloud StorageBackend) {
g.cloud.Store(&cloud)
}
func (g *Goofys) getCloud() StorageBackend {
if g == nil {
return nil
}
return *g.cloud.Load()
}
type OpStats struct {
reads int64
readHits int64
writes int64
flushes int64
metadataReads int64
metadataWrites int64
noops int64
evicts int64
ts time.Time
}
var (
s3Log = log.GetLogger("s3")
mainLog = log.GetLogger("main")
fuseLog = log.GetLogger("fuse")
)
func NewBackend(bucket string, flags *cfg.FlagStorage) (cloud StorageBackend, err error) {
if flags.Backend == nil {
flags.Backend = (&cfg.S3Config{}).Init()
}
if config, ok := flags.Backend.(*cfg.AZBlobConfig); ok {
cloud, err = NewAZBlob(bucket, config)
} else if config, ok := flags.Backend.(*cfg.ADLv1Config); ok {
cloud, err = NewADLv1(bucket, flags, config)
} else if config, ok := flags.Backend.(*cfg.ADLv2Config); ok {
cloud, err = NewADLv2(bucket, flags, config)
} else if config, ok := flags.Backend.(*cfg.S3Config); ok {
if strings.HasSuffix(flags.Endpoint, "/storage.googleapis.com") {
cloud, err = NewGCS3(bucket, flags, config)
} else {
cloud, err = NewS3(bucket, flags, config)
}
} else {
err = fmt.Errorf("Unknown backend config: %T", flags.Backend)
}
return
}
type BucketSpec struct {
Scheme string
Bucket string
Prefix string
}
func ParseBucketSpec(bucket string) (spec BucketSpec, err error) {
if strings.Contains(bucket, "://") {
var u *url.URL
u, err = url.Parse(bucket)
if err != nil {
return
}
spec.Scheme = u.Scheme
spec.Bucket = u.Host
if u.User != nil {
// wasb url can be wasb://container@storage-end-point
// we want to return the entire thing as bucket
spec.Bucket = u.User.String() + "@" + u.Host
}
spec.Prefix = u.Path
} else {
spec.Scheme = "s3"
colon := strings.Index(bucket, ":")
if colon != -1 {
spec.Prefix = bucket[colon+1:]
spec.Bucket = bucket[0:colon]
} else {
spec.Bucket = bucket
}
}
spec.Prefix = strings.Trim(spec.Prefix, "/")
if spec.Prefix != "" {
spec.Prefix += "/"
}
return
}
func NewGoofys(ctx context.Context, bucketName string, flags *cfg.FlagStorage) (*Goofys, error) {
if flags.DebugFuse || flags.DebugMain {
mainLog.SetLevel(zerolog.DebugLevel)
}
if flags.DebugFuse {
fuseLog.SetLevel(zerolog.DebugLevel)
}
if flags.DebugS3 {
log.SetCloudLogLevel(zerolog.DebugLevel)
}
if flags.Backend == nil {
if spec, err := ParseBucketSpec(bucketName); err == nil {
switch spec.Scheme {
case "adl":
auth, err := cfg.AzureAuthorizerConfig{
Log: log.GetLogger("adlv1"),
}.Authorizer()
if err != nil {
err = fmt.Errorf("couldn't load azure credentials: %v",
err)
return nil, err
}
flags.Backend = &cfg.ADLv1Config{
Endpoint: spec.Bucket,
Authorizer: auth,
}
// adlv1 doesn't really have bucket
// names, but we will rebuild the
// prefix
bucketName = ""
if spec.Prefix != "" {
bucketName = ":" + spec.Prefix
}
case "wasb":
config, err := cfg.AzureBlobConfig(flags.Endpoint, spec.Bucket, "blob")
if err != nil {
return nil, err
}
flags.Backend = &config
if config.Container != "" {
bucketName = config.Container
} else {
bucketName = spec.Bucket
}
if config.Prefix != "" {
spec.Prefix = config.Prefix
}
if spec.Prefix != "" {
bucketName += ":" + spec.Prefix
}
case "abfs":
config, err := cfg.AzureBlobConfig(flags.Endpoint, spec.Bucket, "dfs")
if err != nil {
return nil, err
}
flags.Backend = &config
/*
if config.Container != "" {
bucketName = config.Container
} else {
bucketName = spec.Bucket
}
if config.Prefix != "" {
spec.Prefix = config.Prefix
}
if spec.Prefix != "" {
bucketName += ":" + spec.Prefix
}
*/
flags.Backend = &cfg.ADLv2Config{
Endpoint: config.Endpoint,
Authorizer: &config,
}
bucketName = spec.Bucket
if spec.Prefix != "" {
bucketName += ":" + spec.Prefix
}
}
}
}
return newGoofys(ctx, bucketName, flags, NewBackend)
}
func newGoofys(ctx context.Context, bucket string, flags *cfg.FlagStorage,
newBackend func(string, *cfg.FlagStorage) (StorageBackend, error),
) (*Goofys, error) {
// Set up the basic struct.
fs := &Goofys{
bucket: bucket,
flags: flags,
umask: 0o122,
shutdownCh: make(chan struct{}),
zeroBuf: make([]byte, 1048576),
inflightChanges: make(map[string]int),
inflightListings: make(map[int]map[string]bool),
stats: OpStats{
ts: time.Now(),
},
flushPriorities: make([]int64, MAX_FLUSH_PRIORITY+1),
}
var prefix string
colon := strings.Index(bucket, ":")
if colon != -1 {
prefix = bucket[colon+1:]
prefix = strings.Trim(prefix, "/")
if prefix != "" {
prefix += "/"
}
fs.bucket = bucket[0:colon]
bucket = fs.bucket
}
if flags.DebugS3 {
s3Log.SetLevel(zerolog.DebugLevel)
}
cloud, err := newBackend(bucket, flags)
if err != nil {
return nil, fmt.Errorf("Unable to setup backend: %v", err)
}
randomObjectName := prefix + (RandStringBytesMaskImprSrc(32))
err = cloud.Init(randomObjectName)
if err != nil {
return nil, fmt.Errorf("Unable to access '%v': %v", bucket, err)
}
_, _ = cloud.MultipartExpire(&MultipartExpireInput{})
now := time.Now()
fs.rootAttrs = InodeAttributes{
Size: 4096,
Ctime: now,
Mtime: now,
}
if os.Getenv("GOGC") == "" {
// Set garbage collection ratio to 20 instead of 100 by default.
debug.SetGCPercent(20)
}
fs.bufferPool = NewBufferPool(int64(flags.MemoryLimit), uint64(flags.GCInterval)<<20)
fs.bufferPool.FreeSomeCleanBuffers = func(size int64) (int64, bool) {
return fs.FreeSomeCleanBuffers(size)
}
fs.nextInodeID = fuseops.RootInodeID + 1
fs.inodes = make(map[fuseops.InodeID]*Inode)
fs.inodesByTime = make(map[int64]map[fuseops.InodeID]bool)
fs.setCloud(cloud)
root := NewInode(fs, nil, "")
root.refcnt = 1
root.Id = fuseops.RootInodeID
root.ToDir()
root.dir.mountPrefix = prefix
root.userMetadata = make(map[string][]byte)
root.Attributes.Mtime = fs.rootAttrs.Mtime
root.Attributes.Ctime = fs.rootAttrs.Ctime
fs.inodes[fuseops.RootInodeID] = root
fs.nextHandleID = 1
fs.dirHandles = make(map[fuseops.HandleID]*DirHandle)
fs.fileHandles = make(map[fuseops.HandleID]*FileHandle)
fs.flusherCond = sync.NewCond(&fs.flusherMu)
go fs.Flusher()
if fs.flags.StatsInterval > 0 {
go fs.StatPrinter()
}
if fs.flags.CachePath != "" {
fs.diskFdQueue = NewFDQueue(int(fs.flags.MaxDiskCacheFD))
if fs.flags.MaxDiskCacheFD > 0 {
go fs.FDCloser()
}
}
go fs.MetaEvictor()
return fs, nil
}
func (fs *Goofys) Shutdown() {
atomic.StoreInt32(&fs.shutdown, 1)
close(fs.shutdownCh)
fs.WakeupFlusher()
if fs.diskFdQueue != nil {
fs.diskFdQueue.cond.Broadcast()
}
}
// from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func RandStringBytesMaskImprSrc(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func (fs *Goofys) SigUsr1() {
fs.mu.RLock()
mainLog.Infof("forgot %v inodes", fs.forgotCnt)
mainLog.Infof("%v inodes", len(fs.inodes))
fs.mu.RUnlock()
debug.FreeOSMemory()
}
// Find the given inode. Panic if it doesn't exist.
//
// LOCKS_EXCLUDED(fs.mu)
func (fs *Goofys) getInodeOrDie(id fuseops.InodeID) (inode *Inode) {
fs.mu.RLock()
inode = fs.inodes[id]
fs.mu.RUnlock()
if inode == nil {
panic(fmt.Sprintf("Unknown inode: %v", id))
}
return
}
func (fs *Goofys) AddDirHandle(dh *DirHandle) fuseops.HandleID {
fs.mu.Lock()
handleID := fs.nextHandleID
fs.nextHandleID++
fs.dirHandles[handleID] = dh
fs.mu.Unlock()
return handleID
}
func (fs *Goofys) AddFileHandle(fh *FileHandle) fuseops.HandleID {
fs.mu.Lock()
handleID := fs.nextHandleID
fs.nextHandleID++
fs.fileHandles[handleID] = fh
fs.mu.Unlock()
return handleID
}
func (fs *Goofys) StatPrinter() {
for atomic.LoadInt32(&fs.shutdown) == 0 {
select {
case <-time.After(fs.flags.StatsInterval):
case <-fs.shutdownCh:
return
}
now := time.Now()
d := now.Sub(fs.stats.ts).Seconds()
reads := atomic.SwapInt64(&fs.stats.reads, 0)
readHits := atomic.SwapInt64(&fs.stats.readHits, 0)
writes := atomic.SwapInt64(&fs.stats.writes, 0)
flushes := atomic.SwapInt64(&fs.stats.flushes, 0)
metadataReads := atomic.SwapInt64(&fs.stats.metadataReads, 0)
metadataWrites := atomic.SwapInt64(&fs.stats.metadataWrites, 0)
noops := atomic.SwapInt64(&fs.stats.noops, 0)
evicts := atomic.SwapInt64(&fs.stats.evicts, 0)
fs.mu.RLock()
inodeCount := len(fs.inodes)
fs.mu.RUnlock()
fs.stats.ts = now
readsOr1 := float64(reads)
if reads == 0 {
readsOr1 = 1
}
mainLog.Infof(
"I/O: %.2f read/s, %.2f %% hits, %.2f write/s; metadata: %.2f read/s, %.2f write/s, %.2f noop/s, %v alive, %.2f evict/s; %.2f flush/s",
float64(reads)/d,
float64(readHits)/readsOr1*100,
float64(writes)/d,
float64(metadataReads)/d,
float64(metadataWrites)/d,
float64(noops)/d,
inodeCount,
float64(evicts)/d,
float64(flushes)/d,
)
}
}
// Close unneeded cache FDs
func (fs *Goofys) FDCloser() {
for atomic.LoadInt32(&fs.shutdown) == 0 {
fs.diskFdQueue.CloseExtra()
}
}
// Try to reclaim some clean buffers
func (fs *Goofys) FreeSomeCleanBuffers(origSize int64) (int64, bool) {
freed := int64(0)
// Free at least 5 MB
size := origSize
if size < 5*1024*1024 {
size = 5 * 1024 * 1024
}
var inode *Inode
var cleanEnd, cleanQueueID uint64
for freed < size {
inode, cleanEnd, cleanQueueID = fs.cleanQueue.NextClean(cleanQueueID)
if cleanQueueID == 0 {
break
}
inode.mu.Lock()
toFs := -1
buf := inode.buffers.Get(cleanEnd)
// Never evict buffers flushed in an incomplete (last) part
if buf != nil && (buf.state == BUF_CLEAN || buf.state == BUF_FLUSHED_FULL) &&
buf.ptr != nil && !inode.IsRangeLocked(buf.offset, buf.length, false) {
fs.tryEvictToDisk(inode, buf, &toFs)
allocated, _ := inode.buffers.EvictFromMemory(buf)
if allocated != 0 {
_ = fs.bufferPool.UseUnlocked(allocated, false)
freed -= allocated
}
}
inode.mu.Unlock()
if freed >= size {
break
}
}
haveDirty := fs.inodeQueue.Size() > 0
if freed < origSize && haveDirty {
fs.bufferPool.mu.Unlock()
atomic.AddInt32(&fs.wantFree, 1)
fs.WakeupFlusherAndWait(true)
atomic.AddInt32(&fs.wantFree, -1)
fs.bufferPool.mu.Lock()
}
return freed, haveDirty
}
// FIXME: Implement disk cache size limit, add another btree.Map-based
// "LRU" queue to delete old files from the disk.
func (fs *Goofys) tryEvictToDisk(inode *Inode, buf *FileBuffer, toFs *int) {
if fs.flags.CachePath != "" && !buf.onDisk {
if *toFs == -1 {
*toFs = 1
}
if *toFs > 0 {
// Evict to disk
err := inode.OpenCacheFD()
if err != nil {
*toFs = 0
} else {
_, err := inode.DiskCacheFD.WriteAt(buf.data, int64(buf.offset))
if err != nil {
*toFs = 0
mainLog.Errorf("Couldn't write %v bytes at offset %v to %v: %v",
len(buf.data), buf.offset, fs.flags.CachePath+"/"+inode.FullName(), err)
} else {
buf.onDisk = true
}
}
}
}
}
func (fs *Goofys) WakeupFlusherAndWait(wait bool) {
fs.flusherMu.Lock()
if fs.flushPending == 0 {
fs.flushPending = 1
fs.flusherCond.Broadcast()
}
if wait {
// Wait for any result
fs.flusherCond.Wait()
}
fs.flusherMu.Unlock()
}
func (fs *Goofys) WakeupFlusher() {
fs.WakeupFlusherAndWait(false)
}
func (fs *Goofys) ScheduleRetryFlush() {
if atomic.CompareAndSwapInt32(&fs.flushRetrySet, 0, 1) {
time.AfterFunc(fs.flags.RetryInterval, func() {
atomic.StoreInt32(&fs.flushRetrySet, 0)
// Wakeup flusher after retry interval
fs.WakeupFlusher()
})
}
}
// Flusher goroutine.
// Overall algorithm:
// 1. File opened => reads and writes just populate cache
// 2. File closed => flush it
// Created or fully overwritten =>
// => Less than 5 MB => upload in a single part
// => More than 5 MB => upload using multipart
// Updated => CURRENTLY:
// => Less than 5 MB => upload in a single part
// => More than 5 MB => update using multipart copy
// Also we can't update less than 5 MB because it's the minimal part size
// 3. Fsync triggered => intermediate full flush (same algorithm)
// 4. Dirty memory limit reached => without on-disk cache we have to flush the whole object.
// With on-disk cache we can unload some dirty buffers to disk.
func (fs *Goofys) Flusher() {
var inodeID, nextQueueID uint64
priority := 1
for atomic.LoadInt32(&fs.shutdown) == 0 {
fs.flusherMu.Lock()
if fs.flushPending == 0 {
fs.flusherCond.Wait()
}
fs.flushPending = 0
fs.flusherMu.Unlock()
attempts := 1
if priority > 1 || priority == 1 && nextQueueID != 0 {
attempts = 2
}
curPriorityOk := false
for i := 1; i <= priority; i++ {
curPriorityOk = curPriorityOk || atomic.LoadInt64(&fs.flushPriorities[priority]) > 0
}
for attempts > 0 && atomic.LoadInt64(&fs.activeFlushers) < atomic.LoadInt64(&fs.flags.MaxFlushers) {
inodeID, nextQueueID = fs.inodeQueue.Next(nextQueueID)
if inodeID == 0 {
if curPriorityOk {
break
}
priority++
if priority > MAX_FLUSH_PRIORITY {
attempts--
priority = 1
}
} else {
if atomic.CompareAndSwapUint64(&fs.hasNewWrites, 1, 0) {
// restart from the beginning
//inodeID, nextQueueID = 0, 0
priority = 1
attempts = 1
curPriorityOk = atomic.LoadInt64(&fs.flushPriorities[1]) > 0
continue
}
fs.mu.RLock()
inode := fs.inodes[fuseops.InodeID(inodeID)]
fs.mu.RUnlock()
started := false
if inode != nil {
started = inode.TryFlush(priority)
}
curPriorityOk = curPriorityOk || started
}
}
}
}
func (fs *Goofys) EvictEntry(id fuseops.InodeID) bool {
fs.mu.RLock()
childTmp := fs.inodes[id]
fs.mu.RUnlock()
if childTmp == nil ||
childTmp.Id == fuseops.RootInodeID ||
atomic.LoadInt32(&childTmp.fileHandles) > 0 ||
atomic.LoadInt32(&childTmp.CacheState) > ST_DEAD ||
childTmp.isDir() && atomic.LoadInt64(&childTmp.dir.ModifiedChildren) > 0 {
return false
}
if !childTmp.mu.TryLock() {
return false
}
// We CAN evict inodes which are still referenced by the kernel,
// but only if they're expired!
if childTmp.ExpireTime.After(time.Now()) {
childTmp.mu.Unlock()
return false
}
tmpParent := childTmp.Parent
// Respect locking order: parent before child, inode before fs
childTmp.mu.Unlock()
if !tmpParent.mu.TryLock() {
return false
}
if !childTmp.mu.TryLock() {
tmpParent.mu.Unlock()
return false
}
if childTmp.Parent != tmpParent ||
atomic.LoadInt32(&tmpParent.fileHandles) > 0 {
childTmp.mu.Unlock()
tmpParent.mu.Unlock()
return false
}
found := tmpParent.findChildUnlocked(childTmp.Name)
if found == childTmp {
tmpParent.removeChildUnlocked(childTmp)
// Mark directory listing as unfinished
tmpParent.dir.DirTime = time.Time{}
tmpParent.dir.forgetDuringList = true
}
childTmp.resetCache()
childTmp.SetCacheState(ST_DEAD)
// Drop inode
fs.mu.Lock()
childTmp.resetExpireTime()
delete(fs.inodes, childTmp.Id)
fs.forgotCnt += 1
fs.mu.Unlock()
childTmp.mu.Unlock()
tmpParent.mu.Unlock()
return true
}
func (fs *Goofys) MetaEvictor() {
retry := false
var seen map[fuseops.InodeID]bool
for atomic.LoadInt32(&fs.shutdown) == 0 {
if !retry {
select {
case <-time.After(1 * time.Second):
case <-fs.shutdownCh:
return
}
seen = make(map[fuseops.InodeID]bool)
}
// Try to keep the number of cached inodes under control %)
fs.mu.RLock()
totalInodes := len(fs.inodes)
toEvict := (totalInodes - fs.flags.EntryLimit) * 2
if toEvict < 0 {
fs.mu.RUnlock()
retry = false
continue
}
if toEvict < fs.flags.EntryLimit/100 {
toEvict = fs.flags.EntryLimit / 100
}
if toEvict < 10 {
toEvict = 10
}
expireUnix := time.Now().Add(-fs.flags.StatCacheTTL).Unix()
var scan []fuseops.InodeID
for tm, inodes := range fs.inodesByTime {
if tm < expireUnix {
for inode := range inodes {
if !seen[inode] {
scan = append(scan, inode)
}
if len(scan) >= toEvict {
break
}
}
}
if len(scan) >= toEvict {
break
}
}
fs.mu.RUnlock()
evicted := 0
for _, id := range scan {
if fs.EvictEntry(id) {
evicted++
} else {
seen[id] = true
}
}
retry = len(scan) >= toEvict && totalInodes > fs.flags.EntryLimit
atomic.AddInt64(&fs.stats.evicts, int64(evicted))
if len(scan) > 0 {
mainLog.Debugf("metadata cache: alive %v, scanned %v, evicted %v", totalInodes, len(scan), evicted)
}
}
}
type Mount struct {
// Mount Point relative to goofys's root mount.
name string
cloud StorageBackend
prefix string
mounted bool
}
func (fs *Goofys) mount(mp *Inode, b *Mount) {
if b.mounted {
return
}
name := strings.Trim(b.name, "/")
// create path for the mount. AttrTime is set to TIME_MAX so
// they will never expire and be removed. But DirTime is not
// so we will still consult the underlining cloud for listing
// (which will then be merged with the cached result)
for {
idx := strings.Index(name, "/")
if idx == -1 {
break
}
dirName := name[0:idx]
name = name[idx+1:]
mp.mu.Lock()
dirInode := mp.findChildUnlocked(dirName)
if dirInode == nil {
dirInode = NewInode(fs, mp, dirName)
dirInode.ToDir()
dirInode.SetAttrTime(TIME_MAX)
dirInode.userMetadata = make(map[string][]byte)
fs.insertInode(mp, dirInode)
}
mp.mu.Unlock()
mp = dirInode
}
mp.mu.Lock()
defer mp.mu.Unlock()
prev := mp.findChildUnlocked(name)
if prev == nil {
mountInode := NewInode(fs, mp, name)
mountInode.ToDir()
mountInode.dir.mountPrefix = b.prefix
mountInode.SetAttrTime(TIME_MAX)
mountInode.userMetadata = make(map[string][]byte)
fs.insertInode(mp, mountInode)
prev = mountInode
} else {
if !prev.isDir() {
panic(fmt.Sprintf("inode %v is not a directory", prev.FullName()))
}
// This inode might have some cached data from a parent mount.
// Clear this cache by resetting the DirTime.
// Note: resetDirTimeRec should be called without holding the lock.
prev.resetDirTimeRec()
prev.mu.Lock()
defer prev.mu.Unlock()
prev.dir.mountPrefix = b.prefix
prev.SetAttrTime(TIME_MAX)
}
prev.addModified(1)
fuseLog.Infof("mounted /%v", prev.FullName())
b.mounted = true
}
func (fs *Goofys) MountAll(mounts []*Mount) {
root := fs.getInodeOrDie(fuseops.RootInodeID)
for _, m := range mounts {
fs.mount(root, m)
}
}
func (fs *Goofys) Mount(mount *Mount) {
root := fs.getInodeOrDie(fuseops.RootInodeID)
fs.mount(root, mount)
}
func (fs *Goofys) Unmount(mountPoint string) {
mp := fs.getInodeOrDie(fuseops.RootInodeID)
fuseLog.Infof("Attempting to unmount %v", mountPoint)
path := strings.Split(strings.Trim(mountPoint, "/"), "/")
for _, localName := range path {
dirInode := mp.findChild(localName)
if dirInode == nil || !dirInode.isDir() {
fuseLog.Errorf("Failed to find directory:%v while unmounting %v. "+
"Ignoring the unmount operation.", localName, mountPoint)
return
}
mp = dirInode
}
mp.addModified(-1)
mp.ResetForUnmount()
}
func (fs *Goofys) RefreshInodeCache(inode *Inode) error {
inode.mu.Lock()
parent := inode.Parent
parentId := fuseops.InodeID(0)
if parent != nil {
parentId = parent.Id
}
name := inode.Name
inodeId := inode.Id
inode.mu.Unlock()
inode.resetDirTimeRec()
var mappedErr error
var notifications []interface{}
if parent == nil {
// For regular directories it's enough to send one invalidation
// message, the kernel will send forgets for their children and
// everything will be refreshed just fine.
// But root directory is a special case: we should invalidate all
// inodes in it ourselves. Basically this means that we have to do
// a listing and notify the kernel about every file in the root
// directory.
dh := inode.OpenDir()
dh.mu.Lock()
for {
en, err := dh.ReadDir()
if err != nil {
mappedErr = mapAwsError(err)
break
}
if en == nil {
break
}
if dh.lastInternalOffset >= 2 {
// Delete notifications are sent by ReadDir() itself
notifications = append(notifications, &fuseops.NotifyInvalEntry{
Parent: inode.Id,
Name: en.Name,
})
}
dh.Next(en.Name)
}
_ = dh.CloseDir()
dh.mu.Unlock()
if fs.NotifyCallback != nil {
fs.NotifyCallback(notifications)
}
return mappedErr
}
// Use recheckInodeByName to ensure we work with the current child instance
// This handles cases where the inode passed to RefreshInodeCache might be
// a stale reference from fs.inodes while parent.dir.Children has a newer instance
_, err := parent.recheckInodeByName(name)
mappedErr = mapAwsError(err)
if mappedErr == syscall.ENOENT {
notifications = append(notifications, &fuseops.NotifyDelete{
Parent: parentId,
Child: inodeId,
Name: name,
})
} else {
notifications = append(notifications, &fuseops.NotifyInvalEntry{
Parent: parentId,
Name: name,
})
}
if fs.NotifyCallback != nil {
fs.NotifyCallback(notifications)
}
if mappedErr == syscall.ENOENT {
// We don't mind if the file disappeared
return nil
}
return mappedErr
}
// FIXME: Add similar write backoff (now it's handled by file/dir code)
func ReadBackoff(flags *cfg.FlagStorage, try func(attempt int) error) (err error) {
interval := flags.ReadRetryInterval
attempt := 1
for {
err = try(attempt)
if err != nil {
if shouldRetry(err) && (flags.ReadRetryAttempts < 1 || attempt < flags.ReadRetryAttempts) {
attempt++
time.Sleep(interval)
interval = time.Duration(flags.ReadRetryMultiplier * float64(interval))
if interval > flags.ReadRetryMax {
interval = flags.ReadRetryMax