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
|
// Copyright 2018 Google LLC
//
// 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 (
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/log"
"gvisor.googlesource.com/gvisor/pkg/refs"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/lock"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel/auth"
"gvisor.googlesource.com/gvisor/pkg/sentry/memmap"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket/unix/transport"
"gvisor.googlesource.com/gvisor/pkg/syserror"
)
// Inode is a file system object that can be simultaneously referenced by different
// components of the VFS (Dirent, fs.File, etc).
//
// +stateify savable
type Inode struct {
// AtomicRefCount is our reference count.
refs.AtomicRefCount
// InodeOperations is the file system specific behavior of the Inode.
InodeOperations InodeOperations
// StableAttr are stable cached attributes of the Inode.
StableAttr StableAttr
// LockCtx is the file lock context. It manages its own sychronization and tracks
// regions of the Inode that have locks held.
LockCtx LockCtx
// Watches is the set of inotify watches for this inode.
Watches *Watches
// MountSource is the mount source this Inode is a part of.
MountSource *MountSource
// overlay is the overlay entry for this Inode.
overlay *overlayEntry
}
// LockCtx is an Inode's lock context and contains different personalities of locks; both
// Posix and BSD style locks are supported.
//
// Note that in Linux fcntl(2) and flock(2) locks are _not_ cooperative, because race and
// deadlock conditions make merging them prohibitive. We do the same and keep them oblivious
// to each other but provide a "context" as a convenient container.
//
// +stateify savable
type LockCtx struct {
// Posix is a set of POSIX-style regional advisory locks, see fcntl(2).
Posix lock.Locks
// BSD is a set of BSD-style advisory file wide locks, see flock(2).
BSD lock.Locks
}
// NewInode constructs an Inode from InodeOperations, a MountSource, and stable attributes.
//
// NewInode takes a reference on msrc.
func NewInode(iops InodeOperations, msrc *MountSource, sattr StableAttr) *Inode {
msrc.IncRef()
return &Inode{
InodeOperations: iops,
StableAttr: sattr,
Watches: newWatches(),
MountSource: msrc,
}
}
// DecRef drops a reference on the Inode.
func (i *Inode) DecRef() {
i.DecRefWithDestructor(i.destroy)
}
// destroy releases the Inode and releases the msrc reference taken.
func (i *Inode) destroy() {
// FIXME: Context is not plumbed here.
ctx := context.Background()
if err := i.WriteOut(ctx); err != nil {
// FIXME: Mark as warning again once noatime is
// properly supported.
log.Debugf("Inode %+v, failed to sync all metadata: %v", i.StableAttr, err)
}
// If this inode is being destroyed because it was unlinked, queue a
// deletion event. This may not be the case for inodes being revalidated.
if i.Watches.unlinked {
i.Watches.Notify("", linux.IN_DELETE_SELF, 0)
}
// Remove references from the watch owners to the watches on this inode,
// since the watches are about to be GCed. Note that we don't need to worry
// about the watch pins since if there were any active pins, this inode
// wouldn't be in the destructor.
i.Watches.targetDestroyed()
if i.overlay != nil {
i.overlay.release()
} else {
i.InodeOperations.Release(ctx)
}
i.MountSource.DecRef()
}
// Mappable calls i.InodeOperations.Mappable.
func (i *Inode) Mappable() memmap.Mappable {
if i.overlay != nil {
// In an overlay, Mappable is always implemented by
// the overlayEntry metadata to synchronize memory
// access of files with copy up. But first check if
// the Inodes involved would be mappable in the first
// place.
i.overlay.copyMu.RLock()
ok := i.overlay.isMappableLocked()
i.overlay.copyMu.RUnlock()
if !ok {
return nil
}
return i.overlay
}
return i.InodeOperations.Mappable(i)
}
// WriteOut calls i.InodeOperations.WriteOut with i as the Inode.
func (i *Inode) WriteOut(ctx context.Context) error {
if i.overlay != nil {
return overlayWriteOut(ctx, i.overlay)
}
return i.InodeOperations.WriteOut(ctx, i)
}
// Lookup calls i.InodeOperations.Lookup with i as the directory.
func (i *Inode) Lookup(ctx context.Context, name string) (*Dirent, error) {
if i.overlay != nil {
return overlayLookup(ctx, i.overlay, i, name)
}
return i.InodeOperations.Lookup(ctx, i, name)
}
// Create calls i.InodeOperations.Create with i as the directory.
func (i *Inode) Create(ctx context.Context, d *Dirent, name string, flags FileFlags, perm FilePermissions) (*File, error) {
if i.overlay != nil {
return overlayCreate(ctx, i.overlay, d, name, flags, perm)
}
return i.InodeOperations.Create(ctx, i, name, flags, perm)
}
// CreateDirectory calls i.InodeOperations.CreateDirectory with i as the directory.
func (i *Inode) CreateDirectory(ctx context.Context, d *Dirent, name string, perm FilePermissions) error {
if i.overlay != nil {
return overlayCreateDirectory(ctx, i.overlay, d, name, perm)
}
return i.InodeOperations.CreateDirectory(ctx, i, name, perm)
}
// CreateLink calls i.InodeOperations.CreateLink with i as the directory.
func (i *Inode) CreateLink(ctx context.Context, d *Dirent, oldname string, newname string) error {
if i.overlay != nil {
return overlayCreateLink(ctx, i.overlay, d, oldname, newname)
}
return i.InodeOperations.CreateLink(ctx, i, oldname, newname)
}
// CreateHardLink calls i.InodeOperations.CreateHardLink with i as the directory.
func (i *Inode) CreateHardLink(ctx context.Context, d *Dirent, target *Dirent, name string) error {
if i.overlay != nil {
return overlayCreateHardLink(ctx, i.overlay, d, target, name)
}
return i.InodeOperations.CreateHardLink(ctx, i, target.Inode, name)
}
// CreateFifo calls i.InodeOperations.CreateFifo with i as the directory.
func (i *Inode) CreateFifo(ctx context.Context, d *Dirent, name string, perm FilePermissions) error {
if i.overlay != nil {
return overlayCreateFifo(ctx, i.overlay, d, name, perm)
}
return i.InodeOperations.CreateFifo(ctx, i, name, perm)
}
// Remove calls i.InodeOperations.Remove/RemoveDirectory with i as the directory.
func (i *Inode) Remove(ctx context.Context, d *Dirent, remove *Dirent) error {
if i.overlay != nil {
return overlayRemove(ctx, i.overlay, d, remove)
}
switch remove.Inode.StableAttr.Type {
case Directory, SpecialDirectory:
return i.InodeOperations.RemoveDirectory(ctx, i, remove.name)
default:
return i.InodeOperations.Remove(ctx, i, remove.name)
}
}
// Rename calls i.InodeOperations.Rename with the given arguments.
func (i *Inode) Rename(ctx context.Context, oldParent *Dirent, renamed *Dirent, newParent *Dirent, newName string) error {
if i.overlay != nil {
return overlayRename(ctx, i.overlay, oldParent, renamed, newParent, newName)
}
return i.InodeOperations.Rename(ctx, oldParent.Inode, renamed.name, newParent.Inode, newName)
}
// Bind calls i.InodeOperations.Bind with i as the directory.
func (i *Inode) Bind(ctx context.Context, name string, data transport.BoundEndpoint, perm FilePermissions) (*Dirent, error) {
if i.overlay != nil {
return overlayBind(ctx, i.overlay, name, data, perm)
}
return i.InodeOperations.Bind(ctx, i, name, data, perm)
}
// BoundEndpoint calls i.InodeOperations.BoundEndpoint with i as the Inode.
func (i *Inode) BoundEndpoint(path string) transport.BoundEndpoint {
if i.overlay != nil {
return overlayBoundEndpoint(i.overlay, path)
}
return i.InodeOperations.BoundEndpoint(i, path)
}
// GetFile calls i.InodeOperations.GetFile with the given arguments.
func (i *Inode) GetFile(ctx context.Context, d *Dirent, flags FileFlags) (*File, error) {
if i.overlay != nil {
return overlayGetFile(ctx, i.overlay, d, flags)
}
return i.InodeOperations.GetFile(ctx, d, flags)
}
// UnstableAttr calls i.InodeOperations.UnstableAttr with i as the Inode.
func (i *Inode) UnstableAttr(ctx context.Context) (UnstableAttr, error) {
if i.overlay != nil {
return overlayUnstableAttr(ctx, i.overlay)
}
return i.InodeOperations.UnstableAttr(ctx, i)
}
// Getxattr calls i.InodeOperations.Getxattr with i as the Inode.
func (i *Inode) Getxattr(name string) ([]byte, error) {
if i.overlay != nil {
return overlayGetxattr(i.overlay, name)
}
return i.InodeOperations.Getxattr(i, name)
}
// Listxattr calls i.InodeOperations.Listxattr with i as the Inode.
func (i *Inode) Listxattr() (map[string]struct{}, error) {
if i.overlay != nil {
return overlayListxattr(i.overlay)
}
return i.InodeOperations.Listxattr(i)
}
// CheckPermission will check if the caller may access this file in the
// requested way for reading, writing, or executing.
//
// CheckPermission is like Linux's fs/namei.c:inode_permission. It
// - checks file system mount flags,
// - and utilizes InodeOperations.Check to check capabilities and modes.
func (i *Inode) CheckPermission(ctx context.Context, p PermMask) error {
// First check the outer-most mounted filesystem.
if p.Write && i.MountSource.Flags.ReadOnly {
return syserror.EROFS
}
if i.overlay != nil {
// CheckPermission requires some special handling for
// an overlay.
//
// Writes will always be redirected to an upper filesystem,
// so ignore all lower layers being read-only.
//
// But still honor the upper-most filesystem's mount flags;
// we should not attempt to modify the writable layer if it
// is mounted read-only.
if p.Write && overlayUpperMountSource(i.MountSource).Flags.ReadOnly {
return syserror.EROFS
}
}
return i.check(ctx, p)
}
func (i *Inode) check(ctx context.Context, p PermMask) error {
if i.overlay != nil {
return overlayCheck(ctx, i.overlay, p)
}
if !i.InodeOperations.Check(ctx, i, p) {
return syserror.EACCES
}
return nil
}
// SetPermissions calls i.InodeOperations.SetPermissions with i as the Inode.
func (i *Inode) SetPermissions(ctx context.Context, d *Dirent, f FilePermissions) bool {
if i.overlay != nil {
return overlaySetPermissions(ctx, i.overlay, d, f)
}
return i.InodeOperations.SetPermissions(ctx, i, f)
}
// SetOwner calls i.InodeOperations.SetOwner with i as the Inode.
func (i *Inode) SetOwner(ctx context.Context, d *Dirent, o FileOwner) error {
if i.overlay != nil {
return overlaySetOwner(ctx, i.overlay, d, o)
}
return i.InodeOperations.SetOwner(ctx, i, o)
}
// SetTimestamps calls i.InodeOperations.SetTimestamps with i as the Inode.
func (i *Inode) SetTimestamps(ctx context.Context, d *Dirent, ts TimeSpec) error {
if i.overlay != nil {
return overlaySetTimestamps(ctx, i.overlay, d, ts)
}
return i.InodeOperations.SetTimestamps(ctx, i, ts)
}
// Truncate calls i.InodeOperations.Truncate with i as the Inode.
func (i *Inode) Truncate(ctx context.Context, d *Dirent, size int64) error {
if i.overlay != nil {
return overlayTruncate(ctx, i.overlay, d, size)
}
return i.InodeOperations.Truncate(ctx, i, size)
}
// Readlink calls i.InodeOperations.Readlnk with i as the Inode.
func (i *Inode) Readlink(ctx context.Context) (string, error) {
if i.overlay != nil {
return overlayReadlink(ctx, i.overlay)
}
return i.InodeOperations.Readlink(ctx, i)
}
// Getlink calls i.InodeOperations.Getlink.
func (i *Inode) Getlink(ctx context.Context) (*Dirent, error) {
if i.overlay != nil {
return overlayGetlink(ctx, i.overlay)
}
return i.InodeOperations.Getlink(ctx, i)
}
// AddLink calls i.InodeOperations.AddLink.
func (i *Inode) AddLink() {
if i.overlay != nil {
// FIXME: Remove this from InodeOperations altogether.
//
// This interface (including DropLink and NotifyStatusChange)
// is only used by ramfs to update metadata of children. These
// filesystems should _never_ have overlay Inodes cached as
// children. So explicitly disallow this scenario and avoid plumbing
// Dirents through to do copy up.
panic("overlay Inodes cached in ramfs directories are not supported")
}
i.InodeOperations.AddLink()
}
// DropLink calls i.InodeOperations.DropLink.
func (i *Inode) DropLink() {
if i.overlay != nil {
// Same as AddLink.
panic("overlay Inodes cached in ramfs directories are not supported")
}
i.InodeOperations.DropLink()
}
// NotifyStatusChange calls i.InodeOperations.NotifyStatusChange.
func (i *Inode) NotifyStatusChange(ctx context.Context) {
if i.overlay != nil {
// Same as AddLink.
panic("overlay Inodes cached in ramfs directories are not supported")
}
i.InodeOperations.NotifyStatusChange(ctx)
}
// IsVirtual calls i.InodeOperations.IsVirtual.
func (i *Inode) IsVirtual() bool {
if i.overlay != nil {
// An overlay configuration does not support virtual files.
return false
}
return i.InodeOperations.IsVirtual()
}
// StatFS calls i.InodeOperations.StatFS.
func (i *Inode) StatFS(ctx context.Context) (Info, error) {
if i.overlay != nil {
return overlayStatFS(ctx, i.overlay)
}
return i.InodeOperations.StatFS(ctx)
}
// HandleOps extracts HandleOperations from i.
func (i *Inode) HandleOps() HandleOperations {
if i.overlay != nil {
return overlayHandleOps(i.overlay)
}
if h, ok := i.InodeOperations.(HandleOperations); ok {
return h
}
return nil
}
// CheckOwnership checks whether `ctx` owns this Inode or may act as its owner.
// Compare Linux's fs/inode.c:inode_owner_or_capable().
func (i *Inode) CheckOwnership(ctx context.Context) bool {
uattr, err := i.UnstableAttr(ctx)
if err != nil {
return false
}
creds := auth.CredentialsFromContext(ctx)
if uattr.Owner.UID == creds.EffectiveKUID {
return true
}
if creds.HasCapability(linux.CAP_FOWNER) && creds.UserNamespace.MapFromKUID(uattr.Owner.UID).Ok() {
return true
}
return false
}
// CheckCapability checks whether `ctx` has capability `cp` with respect to
// operations on this Inode.
//
// Compare Linux's kernel/capability.c:capable_wrt_inode_uidgid().
func (i *Inode) CheckCapability(ctx context.Context, cp linux.Capability) bool {
uattr, err := i.UnstableAttr(ctx)
if err != nil {
return false
}
creds := auth.CredentialsFromContext(ctx)
if !creds.UserNamespace.MapFromKUID(uattr.Owner.UID).Ok() {
return false
}
if !creds.UserNamespace.MapFromKGID(uattr.Owner.GID).Ok() {
return false
}
return creds.HasCapability(cp)
}
|