summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/fsutil/file.go
blob: 46db2e51c7c5f1e62ac2514e419bc9b8a5241c71 (plain)
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
// 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 fsutil

import (
	"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
	"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"
)

// NoopRelease implements FileOperations.Release for files that have no
// resources to release.
type NoopRelease struct{}

// Release is a no-op.
func (NoopRelease) Release() {}

// SeekWithDirCursor is used to implement fs.FileOperations.Seek.  If dirCursor
// is not nil and the seek was on a directory, the cursor will be updated.
//
// Currently only seeking to 0 on a directory is supported.
//
// FIXME: Lift directory seeking limitations.
func SeekWithDirCursor(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64, dirCursor *string) (int64, error) {
	inode := file.Dirent.Inode
	current := file.Offset()

	// Does the Inode represents a non-seekable type?
	if fs.IsPipe(inode.StableAttr) || fs.IsSocket(inode.StableAttr) {
		return current, syserror.ESPIPE
	}

	// Does the Inode represent a character device?
	if fs.IsCharDevice(inode.StableAttr) {
		// Ignore seek requests.
		//
		// FIXME: This preserves existing
		// behavior but is not universally correct.
		return 0, nil
	}

	// Otherwise compute the new offset.
	switch whence {
	case fs.SeekSet:
		switch inode.StableAttr.Type {
		case fs.RegularFile, fs.SpecialFile, fs.BlockDevice:
			if offset < 0 {
				return current, syserror.EINVAL
			}
			return offset, nil
		case fs.Directory, fs.SpecialDirectory:
			if offset != 0 {
				return current, syserror.EINVAL
			}
			// SEEK_SET to 0 moves the directory "cursor" to the beginning.
			if dirCursor != nil {
				*dirCursor = ""
			}
			return 0, nil
		default:
			return current, syserror.EINVAL
		}
	case fs.SeekCurrent:
		switch inode.StableAttr.Type {
		case fs.RegularFile, fs.SpecialFile, fs.BlockDevice:
			if current+offset < 0 {
				return current, syserror.EINVAL
			}
			return current + offset, nil
		case fs.Directory, fs.SpecialDirectory:
			if offset != 0 {
				return current, syserror.EINVAL
			}
			return current, nil
		default:
			return current, syserror.EINVAL
		}
	case fs.SeekEnd:
		switch inode.StableAttr.Type {
		case fs.RegularFile, fs.BlockDevice:
			// Allow the file to determine the end.
			uattr, err := inode.UnstableAttr(ctx)
			if err != nil {
				return current, err
			}
			sz := uattr.Size
			if sz+offset < 0 {
				return current, syserror.EINVAL
			}
			return sz + offset, nil
		// FIXME: This is not universally correct.
		// Remove SpecialDirectory.
		case fs.SpecialDirectory:
			if offset != 0 {
				return current, syserror.EINVAL
			}
			// SEEK_END to 0 moves the directory "cursor" to the end.
			//
			// FIXME: The ensures that after the seek,
			// reading on the directory will get EOF. But it is not
			// correct in general because the directory can grow in
			// size; attempting to read those new entries will be
			// futile (EOF will always be the result).
			return fs.FileMaxOffset, nil
		default:
			return current, syserror.EINVAL
		}
	}

	// Not a valid seek request.
	return current, syserror.EINVAL
}

// GenericSeek implements FileOperations.Seek for files that use a generic
// seek implementation.
type GenericSeek struct{}

// Seek implements fs.FileOperations.Seek.
func (GenericSeek) Seek(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64) (int64, error) {
	return SeekWithDirCursor(ctx, file, whence, offset, nil)
}

// ZeroSeek implements FileOperations.Seek for files that maintain a constant
// zero-value offset and require a no-op Seek.
type ZeroSeek struct{}

// Seek implements FileOperations.Seek.
func (ZeroSeek) Seek(context.Context, *fs.File, fs.SeekWhence, int64) (int64, error) {
	return 0, nil
}

// PipeSeek implements FileOperations.Seek and can be used for files that behave
// like pipes (seeking is not supported).
type PipeSeek struct{}

// Seek implements FileOperations.Seek.
func (PipeSeek) Seek(context.Context, *fs.File, fs.SeekWhence, int64) (int64, error) {
	return 0, syserror.ESPIPE
}

// NotDirReaddir implements FileOperations.Readdir for non-directories.
type NotDirReaddir struct{}

// Readdir implements FileOperations.NotDirReaddir.
func (NotDirReaddir) Readdir(context.Context, *fs.File, fs.DentrySerializer) (int64, error) {
	return 0, syserror.ENOTDIR
}

// NoFsync implements FileOperations.Fsync for files that don't support syncing.
type NoFsync struct{}

// Fsync implements FileOperations.Fsync.
func (NoFsync) Fsync(context.Context, *fs.File, int64, int64, fs.SyncType) error {
	return syserror.EINVAL
}

// NoopFsync implements FileOperations.Fsync for files that don't need to synced.
type NoopFsync struct{}

// Fsync implements FileOperations.Fsync.
func (NoopFsync) Fsync(context.Context, *fs.File, int64, int64, fs.SyncType) error {
	return nil
}

// NoopFlush implements FileOperations.Flush as a no-op.
type NoopFlush struct{}

// Flush implements FileOperations.Flush.
func (NoopFlush) Flush(context.Context, *fs.File) error {
	return nil
}

// NoMMap implements fs.FileOperations.Mappable for files that cannot
// be memory mapped.
type NoMMap struct{}

// ConfigureMMap implements fs.FileOperations.ConfigureMMap.
func (NoMMap) ConfigureMMap(context.Context, *fs.File, *memmap.MMapOpts) error {
	return syserror.ENODEV
}

// GenericConfigureMMap implements fs.FileOperations.ConfigureMMap for most
// filesystems that support memory mapping.
func GenericConfigureMMap(file *fs.File, m memmap.Mappable, opts *memmap.MMapOpts) error {
	opts.Mappable = m
	opts.MappingIdentity = file
	file.IncRef()
	return nil
}

// NoIoctl implements fs.FileOperations.Ioctl for files that don't implement
// the ioctl syscall.
type NoIoctl struct{}

// Ioctl implements fs.FileOperations.Ioctl.
func (NoIoctl) Ioctl(ctx context.Context, io usermem.IO, args arch.SyscallArguments) (uintptr, error) {
	return 0, syserror.ENOTTY
}

// DirFileOperations implements FileOperations for directories.
//
// +stateify savable
type DirFileOperations struct {
	waiter.AlwaysReady `state:"nosave"`
	NoopRelease        `state:"nosave"`
	GenericSeek        `state:"nosave"`
	NoFsync            `state:"nosave"`
	NoopFlush          `state:"nosave"`
	NoMMap             `state:"nosave"`
	NoIoctl            `state:"nosave"`

	// dentryMap is a SortedDentryMap used to implement Readdir.
	dentryMap *fs.SortedDentryMap

	// dirCursor contains the name of the last directory entry that was
	// serialized.
	dirCursor string
}

// NewDirFileOperations returns a new DirFileOperations that will iterate the
// given denty map.
func NewDirFileOperations(dentries *fs.SortedDentryMap) *DirFileOperations {
	return &DirFileOperations{
		dentryMap: dentries,
	}
}

// IterateDir implements DirIterator.IterateDir.
func (dfo *DirFileOperations) IterateDir(ctx context.Context, dirCtx *fs.DirCtx, offset int) (int, error) {
	n, err := fs.GenericReaddir(dirCtx, dfo.dentryMap)
	return offset + n, err
}

// Readdir implements FileOperations.Readdir.
func (dfo *DirFileOperations) 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:  &dfo.dirCursor,
	}
	return fs.DirentReaddir(ctx, file.Dirent, dfo, root, dirCtx, file.Offset())
}

// Read implements FileOperations.Read
func (*DirFileOperations) Read(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
	return 0, syserror.EISDIR
}

// Write implements FileOperations.Write.
func (*DirFileOperations) Write(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
	return 0, syserror.EISDIR
}