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
|
// Copyright 2018 Google 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 fsutil
import (
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/memmap"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserror"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
// Handle implements FileOperations.
//
// FIXME: Remove Handle entirely in favor of individual fs.File
// implementations using simple generic utilities.
//
// +stateify savable
type Handle struct {
NoopRelease `state:"nosave"`
NoIoctl `state:"nosave"`
HandleOperations fs.HandleOperations
// dirCursor is the directory cursor.
dirCursor string
}
// NewHandle returns a File backed by the Dirent and FileFlags.
func NewHandle(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags, hops fs.HandleOperations) *fs.File {
if !fs.IsPipe(dirent.Inode.StableAttr) && !fs.IsSocket(dirent.Inode.StableAttr) {
// Allow reading/writing at an arbitrary offset for non-pipes
// and non-sockets.
flags.Pread = true
flags.Pwrite = true
}
return fs.NewFile(ctx, dirent, flags, &Handle{HandleOperations: hops})
}
// Readiness implements waiter.Waitable.Readiness.
func (h *Handle) Readiness(mask waiter.EventMask) waiter.EventMask {
return h.HandleOperations.Readiness(mask)
}
// EventRegister implements waiter.Waitable.EventRegister.
func (h *Handle) EventRegister(e *waiter.Entry, mask waiter.EventMask) {
h.HandleOperations.EventRegister(e, mask)
}
// EventUnregister implements waiter.Waitable.EventUnregister.
func (h *Handle) EventUnregister(e *waiter.Entry) {
h.HandleOperations.EventUnregister(e)
}
// Readdir implements FileOperations.Readdir.
func (h *Handle) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &h.dirCursor,
}
n, err := fs.DirentReaddir(ctx, file.Dirent, h, root, dirCtx, file.Offset())
return n, err
}
// Seek implements FileOperations.Seek.
func (h *Handle) Seek(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64) (int64, error) {
return SeekWithDirCursor(ctx, file, whence, offset, &h.dirCursor)
}
// IterateDir implements DirIterator.IterateDir.
func (h *Handle) IterateDir(ctx context.Context, dirCtx *fs.DirCtx, offset int) (int, error) {
return h.HandleOperations.DeprecatedReaddir(ctx, dirCtx, offset)
}
// Read implements FileOperations.Read.
func (h *Handle) Read(ctx context.Context, file *fs.File, dst usermem.IOSequence, offset int64) (int64, error) {
return h.HandleOperations.DeprecatedPreadv(ctx, dst, offset)
}
// Write implements FileOperations.Write.
func (h *Handle) Write(ctx context.Context, file *fs.File, src usermem.IOSequence, offset int64) (int64, error) {
return h.HandleOperations.DeprecatedPwritev(ctx, src, offset)
}
// Fsync implements FileOperations.Fsync.
func (h *Handle) Fsync(ctx context.Context, file *fs.File, start int64, end int64, syncType fs.SyncType) error {
switch syncType {
case fs.SyncAll, fs.SyncData:
// Write out metadata.
if err := file.Dirent.Inode.WriteOut(ctx); err != nil {
return err
}
fallthrough
case fs.SyncBackingStorage:
// Use DeprecatedFsync to sync disks.
return h.HandleOperations.DeprecatedFsync()
}
panic("invalid sync type")
}
// Flush implements FileOperations.Flush.
func (h *Handle) Flush(context.Context, *fs.File) error {
return h.HandleOperations.DeprecatedFlush()
}
// ConfigureMMap implements FileOperations.ConfigureMMap.
func (h *Handle) ConfigureMMap(ctx context.Context, file *fs.File, opts *memmap.MMapOpts) error {
mappable := file.Dirent.Inode.Mappable()
if mappable == nil {
return syserror.ENODEV
}
return GenericConfigureMMap(file, mappable, opts)
}
|