summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fsimpl/ext/regular_file.go
blob: 5ad9befcda6833dcf937b9f3a11b0b3f4c10ddf3 (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
// Copyright 2019 The gVisor Authors.
//
// 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 ext

import (
	"io"

	"gvisor.dev/gvisor/pkg/abi/linux"
	"gvisor.dev/gvisor/pkg/context"
	"gvisor.dev/gvisor/pkg/safemem"
	"gvisor.dev/gvisor/pkg/sentry/memmap"
	"gvisor.dev/gvisor/pkg/sentry/vfs"
	"gvisor.dev/gvisor/pkg/sync"
	"gvisor.dev/gvisor/pkg/syserror"
	"gvisor.dev/gvisor/pkg/usermem"
)

// regularFile represents a regular file's inode. This too follows the
// inheritance pattern prevelant in the vfs layer described in
// pkg/sentry/vfs/README.md.
//
// +stateify savable
type regularFile struct {
	inode inode

	// This is immutable. The first field of fileReader implementations must be
	// regularFile to ensure temporality.
	// io.ReaderAt is more strict than io.Reader in the sense that a partial read
	// is always accompanied by an error. If a read spans past the end of file, a
	// partial read (within file range) is done and io.EOF is returned.
	impl io.ReaderAt
}

// newRegularFile is the regularFile constructor. It figures out what kind of
// file this is and initializes the fileReader.
func newRegularFile(args inodeArgs) (*regularFile, error) {
	if args.diskInode.Flags().Extents {
		file, err := newExtentFile(args)
		if err != nil {
			return nil, err
		}
		return &file.regFile, nil
	}

	file, err := newBlockMapFile(args)
	if err != nil {
		return nil, err
	}
	return &file.regFile, nil
}

func (in *inode) isRegular() bool {
	_, ok := in.impl.(*regularFile)
	return ok
}

// directoryFD represents a directory file description. It implements
// vfs.FileDescriptionImpl.
//
// +stateify savable
type regularFileFD struct {
	fileDescription
	vfs.LockFD

	// off is the file offset. off is accessed using atomic memory operations.
	off int64

	// offMu serializes operations that may mutate off.
	offMu sync.Mutex `state:"nosave"`
}

// Release implements vfs.FileDescriptionImpl.Release.
func (fd *regularFileFD) Release(context.Context) {}

// PRead implements vfs.FileDescriptionImpl.PRead.
func (fd *regularFileFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
	safeReader := safemem.FromIOReaderAt{
		ReaderAt: fd.inode().impl.(*regularFile).impl,
		Offset:   offset,
	}

	// Copies data from disk directly into usermem without any intermediate
	// allocations (if dst is converted into BlockSeq such that it does not need
	// safe copying).
	return dst.CopyOutFrom(ctx, safeReader)
}

// Read implements vfs.FileDescriptionImpl.Read.
func (fd *regularFileFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
	n, err := fd.PRead(ctx, dst, fd.off, opts)
	fd.offMu.Lock()
	fd.off += n
	fd.offMu.Unlock()
	return n, err
}

// PWrite implements vfs.FileDescriptionImpl.PWrite.
func (fd *regularFileFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) {
	// write(2) specifies that EBADF must be returned if the fd is not open for
	// writing.
	return 0, syserror.EBADF
}

// Write implements vfs.FileDescriptionImpl.Write.
func (fd *regularFileFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
	n, err := fd.PWrite(ctx, src, fd.off, opts)
	fd.offMu.Lock()
	fd.off += n
	fd.offMu.Unlock()
	return n, err
}

// IterDirents implements vfs.FileDescriptionImpl.IterDirents.
func (fd *regularFileFD) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback) error {
	return syserror.ENOTDIR
}

// Seek implements vfs.FileDescriptionImpl.Seek.
func (fd *regularFileFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
	fd.offMu.Lock()
	defer fd.offMu.Unlock()
	switch whence {
	case linux.SEEK_SET:
		// Use offset as specified.
	case linux.SEEK_CUR:
		offset += fd.off
	case linux.SEEK_END:
		offset += int64(fd.inode().diskInode.Size())
	default:
		return 0, syserror.EINVAL
	}
	if offset < 0 {
		return 0, syserror.EINVAL
	}
	fd.off = offset
	return offset, nil
}

// ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap.
func (fd *regularFileFD) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
	// TODO(b/134676337): Implement mmap(2).
	return syserror.ENODEV
}