-
Notifications
You must be signed in to change notification settings - Fork 413
/
file.go
249 lines (206 loc) · 5.75 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
// 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"
"github.com/googlecloudplatform/gcsfuse/gcsproxy"
"github.com/googlecloudplatform/gcsfuse/timeutil"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/gcloud/syncutil"
"golang.org/x/net/context"
)
type FileInode struct {
/////////////////////////
// Dependencies
/////////////////////////
bucket gcs.Bucket
/////////////////////////
// Constant data
/////////////////////////
id fuseops.InodeID
/////////////////////////
// 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
// A proxy for the backing object in GCS.
//
// INVARIANT: proxy.CheckInvariants() does not panic
//
// GUARDED_BY(mu)
proxy *gcsproxy.ObjectProxy
}
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: len(o.Name) > 0
// REQUIRES: o.Name[len(o.Name)-1] != '/'
func NewFileInode(
clock timeutil.Clock,
bucket gcs.Bucket,
id fuseops.InodeID,
o *gcs.Object) (f *FileInode) {
// Set up the basic struct.
f = &FileInode{
bucket: bucket,
id: id,
proxy: gcsproxy.NewObjectProxy(clock, bucket, 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() {
// Make sure the name is legal.
name := f.proxy.Name()
if len(name) == 0 || name[len(name)-1] == '/' {
panic("Illegal file name: " + name)
}
// INVARIANT: proxy.CheckInvariants() does not panic
f.proxy.CheckInvariants()
}
////////////////////////////////////////////////////////////////////////
// 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.proxy.Name()
}
// Return the object generation number from which this inode was branched.
//
// Does not require the lock to be held, but may spontaneously increase if the
// lock is not held.
func (f *FileInode) SourceGeneration() int64 {
return f.proxy.SourceGeneration()
}
// 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
func (f *FileInode) Destroy() (err error) {
err = f.proxy.Destroy()
return
}
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Attributes(
ctx context.Context) (attrs fuseops.InodeAttributes, err error) {
// Stat the object.
sr, err := f.proxy.Stat(ctx)
if err != nil {
err = fmt.Errorf("Stat: %v", err)
return
}
// Fill out the struct.
attrs = fuseops.InodeAttributes{
Nlink: 1,
Size: uint64(sr.Size),
Mode: 0700,
Mtime: sr.Mtime,
}
// If the object has been clobbered, we reflect that as the inode being
// unlinked.
if sr.Clobbered {
attrs.Nlink = 0
}
return
}
// Serve a read op for this file, without responding.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Read(
op *fuseops.ReadFileOp) (err error) {
// Read from the proxy.
buf := make([]byte, op.Size)
n, err := f.proxy.ReadAt(op.Context(), buf, op.Offset)
// We don't return errors for EOF. Otherwise, propagate errors.
if err == io.EOF {
err = nil
} else if err != nil {
err = fmt.Errorf("ReadAt: %v", err)
return
}
// Fill in the response.
op.Data = buf[:n]
return
}
// Serve a write op for this file, without responding.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Write(
op *fuseops.WriteFileOp) (err error) {
// Write to the proxy. Note that the proxy guarantees that it returns an
// error for short writes.
_, err = f.proxy.WriteAt(op.Context(), op.Data, op.Offset)
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) {
// Write out the proxy's contents if it is dirty.
err = f.proxy.Sync(ctx)
// 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("ObjectProxy.Sync: %v", err)
return
}
return
}
// Truncate the file to the specified size.
//
// LOCKS_REQUIRED(f.mu)
func (f *FileInode) Truncate(
ctx context.Context,
size int64) (err error) {
err = f.proxy.Truncate(ctx, size)
return
}