2019-07-18 22:09:14 +00:00
|
|
|
// 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 vfs
|
|
|
|
|
|
|
|
import (
|
2019-08-14 00:52:53 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
2020-01-27 23:17:58 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/context"
|
2021-06-29 22:05:27 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/errors/linuxerr"
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sentry/arch"
|
2020-06-10 01:44:57 +00:00
|
|
|
fslock "gvisor.dev/gvisor/pkg/sentry/fs/lock"
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sentry/memmap"
|
2020-01-10 06:00:42 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sync"
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/syserror"
|
2020-01-27 23:17:58 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/usermem"
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/waiter"
|
|
|
|
)
|
|
|
|
|
2019-08-16 17:18:58 +00:00
|
|
|
// The following design pattern is strongly recommended for filesystem
|
|
|
|
// implementations to adapt:
|
|
|
|
// - Have a local fileDescription struct (containing FileDescription) which
|
|
|
|
// embeds FileDescriptionDefaultImpl and overrides the default methods
|
2020-03-06 20:58:45 +00:00
|
|
|
// which are common to all fd implementations for that filesystem like
|
|
|
|
// StatusFlags, SetStatusFlags, Stat, SetStat, StatFS, etc.
|
2019-08-16 17:18:58 +00:00
|
|
|
// - This should be embedded in all file description implementations as the
|
|
|
|
// first field by value.
|
|
|
|
// - Directory FDs would also embed DirectoryFileDescriptionDefaultImpl.
|
|
|
|
|
2019-07-18 22:09:14 +00:00
|
|
|
// FileDescriptionDefaultImpl may be embedded by implementations of
|
|
|
|
// FileDescriptionImpl to obtain implementations of many FileDescriptionImpl
|
|
|
|
// methods with default behavior analogous to Linux's.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-07-18 22:09:14 +00:00
|
|
|
type FileDescriptionDefaultImpl struct{}
|
|
|
|
|
|
|
|
// OnClose implements FileDescriptionImpl.OnClose analogously to
|
|
|
|
// file_operations::flush == NULL in Linux.
|
2019-10-16 01:39:16 +00:00
|
|
|
func (FileDescriptionDefaultImpl) OnClose(ctx context.Context) error {
|
2019-07-18 22:09:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatFS implements FileDescriptionImpl.StatFS analogously to
|
|
|
|
// super_operations::statfs == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) StatFS(ctx context.Context) (linux.Statfs, error) {
|
|
|
|
return linux.Statfs{}, syserror.ENOSYS
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:13:04 +00:00
|
|
|
// Allocate implements FileDescriptionImpl.Allocate analogously to
|
2020-09-17 22:36:40 +00:00
|
|
|
// fallocate called on an invalid type of file in Linux.
|
|
|
|
//
|
|
|
|
// Note that directories can rely on this implementation even though they
|
|
|
|
// should technically return EISDIR. Allocate should never be called for a
|
|
|
|
// directory, because it requires a writable fd.
|
2020-07-01 20:13:04 +00:00
|
|
|
func (FileDescriptionDefaultImpl) Allocate(ctx context.Context, mode, offset, length uint64) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENODEV
|
2020-07-01 20:13:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 22:09:14 +00:00
|
|
|
// Readiness implements waiter.Waitable.Readiness analogously to
|
|
|
|
// file_operations::poll == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Readiness(mask waiter.EventMask) waiter.EventMask {
|
|
|
|
// include/linux/poll.h:vfs_poll() => DEFAULT_POLLMASK
|
2021-03-24 19:08:24 +00:00
|
|
|
return waiter.ReadableEvents | waiter.WritableEvents
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EventRegister implements waiter.Waitable.EventRegister analogously to
|
|
|
|
// file_operations::poll == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) EventRegister(e *waiter.Entry, mask waiter.EventMask) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventUnregister implements waiter.Waitable.EventUnregister analogously to
|
|
|
|
// file_operations::poll == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) EventUnregister(e *waiter.Entry) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// PRead implements FileDescriptionImpl.PRead analogously to
|
|
|
|
// file_operations::read == file_operations::read_iter == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts ReadOptions) (int64, error) {
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements FileDescriptionImpl.Read analogously to
|
|
|
|
// file_operations::read == file_operations::read_iter == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Read(ctx context.Context, dst usermem.IOSequence, opts ReadOptions) (int64, error) {
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PWrite implements FileDescriptionImpl.PWrite analogously to
|
|
|
|
// file_operations::write == file_operations::write_iter == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts WriteOptions) (int64, error) {
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements FileDescriptionImpl.Write analogously to
|
|
|
|
// file_operations::write == file_operations::write_iter == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Write(ctx context.Context, src usermem.IOSequence, opts WriteOptions) (int64, error) {
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterDirents implements FileDescriptionImpl.IterDirents analogously to
|
|
|
|
// file_operations::iterate == file_operations::iterate_shared == NULL in
|
|
|
|
// Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) IterDirents(ctx context.Context, cb IterDirentsCallback) error {
|
2021-07-12 22:24:04 +00:00
|
|
|
return linuxerr.ENOTDIR
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek implements FileDescriptionImpl.Seek analogously to
|
|
|
|
// file_operations::llseek == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
|
2021-07-01 19:02:59 +00:00
|
|
|
return 0, linuxerr.ESPIPE
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements FileDescriptionImpl.Sync analogously to
|
|
|
|
// file_operations::fsync == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Sync(ctx context.Context) error {
|
2021-06-29 22:05:27 +00:00
|
|
|
return linuxerr.EINVAL
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigureMMap implements FileDescriptionImpl.ConfigureMMap analogously to
|
|
|
|
// file_operations::mmap == NULL in Linux.
|
2019-10-16 01:39:16 +00:00
|
|
|
func (FileDescriptionDefaultImpl) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENODEV
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ioctl implements FileDescriptionImpl.Ioctl analogously to
|
|
|
|
// file_operations::unlocked_ioctl == NULL in Linux.
|
|
|
|
func (FileDescriptionDefaultImpl) Ioctl(ctx context.Context, uio usermem.IO, args arch.SyscallArguments) (uintptr, error) {
|
2021-07-12 22:24:04 +00:00
|
|
|
return 0, linuxerr.ENOTTY
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:49:51 +00:00
|
|
|
// ListXattr implements FileDescriptionImpl.ListXattr analogously to
|
2019-12-18 23:47:24 +00:00
|
|
|
// inode_operations::listxattr == NULL in Linux.
|
2020-09-08 18:49:51 +00:00
|
|
|
func (FileDescriptionDefaultImpl) ListXattr(ctx context.Context, size uint64) ([]string, error) {
|
|
|
|
// This isn't exactly accurate; see FileDescription.ListXattr.
|
2021-07-01 19:02:59 +00:00
|
|
|
return nil, linuxerr.ENOTSUP
|
2019-12-18 23:47:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:49:51 +00:00
|
|
|
// GetXattr implements FileDescriptionImpl.GetXattr analogously to
|
2019-12-18 23:47:24 +00:00
|
|
|
// inode::i_opflags & IOP_XATTR == 0 in Linux.
|
2020-09-08 18:49:51 +00:00
|
|
|
func (FileDescriptionDefaultImpl) GetXattr(ctx context.Context, opts GetXattrOptions) (string, error) {
|
2021-07-01 19:02:59 +00:00
|
|
|
return "", linuxerr.ENOTSUP
|
2019-12-18 23:47:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:49:51 +00:00
|
|
|
// SetXattr implements FileDescriptionImpl.SetXattr analogously to
|
2019-12-18 23:47:24 +00:00
|
|
|
// inode::i_opflags & IOP_XATTR == 0 in Linux.
|
2020-09-08 18:49:51 +00:00
|
|
|
func (FileDescriptionDefaultImpl) SetXattr(ctx context.Context, opts SetXattrOptions) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOTSUP
|
2019-12-18 23:47:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:49:51 +00:00
|
|
|
// RemoveXattr implements FileDescriptionImpl.RemoveXattr analogously to
|
2019-12-18 23:47:24 +00:00
|
|
|
// inode::i_opflags & IOP_XATTR == 0 in Linux.
|
2020-09-08 18:49:51 +00:00
|
|
|
func (FileDescriptionDefaultImpl) RemoveXattr(ctx context.Context, name string) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOTSUP
|
2019-12-18 23:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 22:09:14 +00:00
|
|
|
// DirectoryFileDescriptionDefaultImpl may be embedded by implementations of
|
|
|
|
// FileDescriptionImpl that always represent directories to obtain
|
2019-08-16 17:18:58 +00:00
|
|
|
// implementations of non-directory I/O methods that return EISDIR.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-08-16 17:18:58 +00:00
|
|
|
type DirectoryFileDescriptionDefaultImpl struct{}
|
2019-07-18 22:09:14 +00:00
|
|
|
|
2020-07-01 20:13:04 +00:00
|
|
|
// Allocate implements DirectoryFileDescriptionDefaultImpl.Allocate.
|
|
|
|
func (DirectoryFileDescriptionDefaultImpl) Allocate(ctx context.Context, mode, offset, length uint64) error {
|
|
|
|
return syserror.EISDIR
|
|
|
|
}
|
|
|
|
|
2019-07-18 22:09:14 +00:00
|
|
|
// PRead implements FileDescriptionImpl.PRead.
|
|
|
|
func (DirectoryFileDescriptionDefaultImpl) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts ReadOptions) (int64, error) {
|
|
|
|
return 0, syserror.EISDIR
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements FileDescriptionImpl.Read.
|
|
|
|
func (DirectoryFileDescriptionDefaultImpl) Read(ctx context.Context, dst usermem.IOSequence, opts ReadOptions) (int64, error) {
|
|
|
|
return 0, syserror.EISDIR
|
|
|
|
}
|
|
|
|
|
|
|
|
// PWrite implements FileDescriptionImpl.PWrite.
|
|
|
|
func (DirectoryFileDescriptionDefaultImpl) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts WriteOptions) (int64, error) {
|
|
|
|
return 0, syserror.EISDIR
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements FileDescriptionImpl.Write.
|
|
|
|
func (DirectoryFileDescriptionDefaultImpl) Write(ctx context.Context, src usermem.IOSequence, opts WriteOptions) (int64, error) {
|
|
|
|
return 0, syserror.EISDIR
|
|
|
|
}
|
2019-08-14 00:52:53 +00:00
|
|
|
|
2019-12-30 19:35:06 +00:00
|
|
|
// DentryMetadataFileDescriptionImpl may be embedded by implementations of
|
|
|
|
// FileDescriptionImpl for which FileDescriptionOptions.UseDentryMetadata is
|
|
|
|
// true to obtain implementations of Stat and SetStat that panic.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-12-30 19:35:06 +00:00
|
|
|
type DentryMetadataFileDescriptionImpl struct{}
|
|
|
|
|
|
|
|
// Stat implements FileDescriptionImpl.Stat.
|
|
|
|
func (DentryMetadataFileDescriptionImpl) Stat(ctx context.Context, opts StatOptions) (linux.Statx, error) {
|
|
|
|
panic("illegal call to DentryMetadataFileDescriptionImpl.Stat")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStat implements FileDescriptionImpl.SetStat.
|
|
|
|
func (DentryMetadataFileDescriptionImpl) SetStat(ctx context.Context, opts SetStatOptions) error {
|
|
|
|
panic("illegal call to DentryMetadataFileDescriptionImpl.SetStat")
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:52:53 +00:00
|
|
|
// DynamicBytesSource represents a data source for a
|
|
|
|
// DynamicBytesFileDescriptionImpl.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-08-14 00:52:53 +00:00
|
|
|
type DynamicBytesSource interface {
|
|
|
|
// Generate writes the file's contents to buf.
|
|
|
|
Generate(ctx context.Context, buf *bytes.Buffer) error
|
|
|
|
}
|
|
|
|
|
2019-12-26 22:42:19 +00:00
|
|
|
// StaticData implements DynamicBytesSource over a static string.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-12-26 22:42:19 +00:00
|
|
|
type StaticData struct {
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate implements DynamicBytesSource.
|
|
|
|
func (s *StaticData) Generate(ctx context.Context, buf *bytes.Buffer) error {
|
|
|
|
buf.WriteString(s.Data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-29 02:30:36 +00:00
|
|
|
// WritableDynamicBytesSource extends DynamicBytesSource to allow writes to the
|
|
|
|
// underlying source.
|
2021-02-10 03:15:53 +00:00
|
|
|
//
|
|
|
|
// TODO(b/179825241): Make utility for integer-based writable files.
|
2020-01-29 02:30:36 +00:00
|
|
|
type WritableDynamicBytesSource interface {
|
|
|
|
DynamicBytesSource
|
|
|
|
|
|
|
|
// Write sends writes to the source.
|
|
|
|
Write(ctx context.Context, src usermem.IOSequence, offset int64) (int64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DynamicBytesFileDescriptionImpl may be embedded by implementations of
|
|
|
|
// FileDescriptionImpl that represent read-only regular files whose contents
|
|
|
|
// are backed by a bytes.Buffer that is regenerated when necessary, consistent
|
|
|
|
// with Linux's fs/seq_file.c:single_open().
|
|
|
|
//
|
2021-04-03 04:08:53 +00:00
|
|
|
// If data additionally implements WritableDynamicBytesSource, writes are
|
|
|
|
// dispatched to the implementer. The source data is not automatically modified.
|
|
|
|
//
|
2020-01-29 02:30:36 +00:00
|
|
|
// DynamicBytesFileDescriptionImpl.SetDataSource() must be called before first
|
|
|
|
// use.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2020-01-29 02:30:36 +00:00
|
|
|
type DynamicBytesFileDescriptionImpl struct {
|
|
|
|
data DynamicBytesSource // immutable
|
2020-09-24 17:11:10 +00:00
|
|
|
mu sync.Mutex `state:"nosave"` // protects the following fields
|
|
|
|
buf bytes.Buffer `state:".([]byte)"`
|
2020-01-29 02:30:36 +00:00
|
|
|
off int64
|
|
|
|
lastRead int64 // offset at which the last Read, PRead, or Seek ended
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:11:10 +00:00
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) saveBuf() []byte {
|
|
|
|
return fd.buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) loadBuf(p []byte) {
|
|
|
|
fd.buf.Write(p)
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:52:53 +00:00
|
|
|
// SetDataSource must be called exactly once on fd before first use.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) SetDataSource(data DynamicBytesSource) {
|
|
|
|
fd.data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preconditions: fd.mu must be locked.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) preadLocked(ctx context.Context, dst usermem.IOSequence, offset int64, opts *ReadOptions) (int64, error) {
|
|
|
|
// Regenerate the buffer if it's empty, or before pread() at a new offset.
|
|
|
|
// Compare fs/seq_file.c:seq_read() => traverse().
|
|
|
|
switch {
|
|
|
|
case offset != fd.lastRead:
|
|
|
|
fd.buf.Reset()
|
|
|
|
fallthrough
|
|
|
|
case fd.buf.Len() == 0:
|
|
|
|
if err := fd.data.Generate(ctx, &fd.buf); err != nil {
|
|
|
|
fd.buf.Reset()
|
|
|
|
// fd.off is not updated in this case.
|
|
|
|
fd.lastRead = 0
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bs := fd.buf.Bytes()
|
|
|
|
if offset >= int64(len(bs)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n, err := dst.CopyOut(ctx, bs[offset:])
|
|
|
|
fd.lastRead = offset + int64(n)
|
|
|
|
return int64(n), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PRead implements FileDescriptionImpl.PRead.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts ReadOptions) (int64, error) {
|
|
|
|
fd.mu.Lock()
|
|
|
|
n, err := fd.preadLocked(ctx, dst, offset, &opts)
|
|
|
|
fd.mu.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements FileDescriptionImpl.Read.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) Read(ctx context.Context, dst usermem.IOSequence, opts ReadOptions) (int64, error) {
|
|
|
|
fd.mu.Lock()
|
|
|
|
n, err := fd.preadLocked(ctx, dst, fd.off, &opts)
|
|
|
|
fd.off += n
|
|
|
|
fd.mu.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek implements FileDescriptionImpl.Seek.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
|
|
|
|
fd.mu.Lock()
|
|
|
|
defer fd.mu.Unlock()
|
|
|
|
switch whence {
|
|
|
|
case linux.SEEK_SET:
|
|
|
|
// Use offset as given.
|
|
|
|
case linux.SEEK_CUR:
|
|
|
|
offset += fd.off
|
|
|
|
default:
|
|
|
|
// fs/seq_file:seq_lseek() rejects SEEK_END etc.
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-08-14 00:52:53 +00:00
|
|
|
}
|
|
|
|
if offset < 0 {
|
2021-06-29 22:05:27 +00:00
|
|
|
return 0, linuxerr.EINVAL
|
2019-08-14 00:52:53 +00:00
|
|
|
}
|
|
|
|
if offset != fd.lastRead {
|
|
|
|
// Regenerate the file's contents immediately. Compare
|
|
|
|
// fs/seq_file.c:seq_lseek() => traverse().
|
|
|
|
fd.buf.Reset()
|
|
|
|
if err := fd.data.Generate(ctx, &fd.buf); err != nil {
|
|
|
|
fd.buf.Reset()
|
|
|
|
fd.off = 0
|
|
|
|
fd.lastRead = 0
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
fd.lastRead = offset
|
|
|
|
}
|
|
|
|
fd.off = offset
|
|
|
|
return offset, nil
|
|
|
|
}
|
2019-12-12 21:17:47 +00:00
|
|
|
|
2020-01-29 02:30:36 +00:00
|
|
|
// Preconditions: fd.mu must be locked.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) pwriteLocked(ctx context.Context, src usermem.IOSequence, offset int64, opts WriteOptions) (int64, error) {
|
|
|
|
if opts.Flags&^(linux.RWF_HIPRI|linux.RWF_DSYNC|linux.RWF_SYNC) != 0 {
|
2021-07-12 22:24:04 +00:00
|
|
|
return 0, linuxerr.EOPNOTSUPP
|
2020-01-29 02:30:36 +00:00
|
|
|
}
|
2020-03-16 22:59:29 +00:00
|
|
|
limit, err := CheckLimit(ctx, offset, src.NumBytes())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
src = src.TakeFirst64(limit)
|
2020-01-29 02:30:36 +00:00
|
|
|
|
|
|
|
writable, ok := fd.data.(WritableDynamicBytesSource)
|
|
|
|
if !ok {
|
2020-06-25 02:20:58 +00:00
|
|
|
return 0, syserror.EIO
|
2020-01-29 02:30:36 +00:00
|
|
|
}
|
|
|
|
n, err := writable.Write(ctx, src, offset)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate cached data that might exist prior to this call.
|
|
|
|
fd.buf.Reset()
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PWrite implements FileDescriptionImpl.PWrite.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts WriteOptions) (int64, error) {
|
|
|
|
fd.mu.Lock()
|
|
|
|
n, err := fd.pwriteLocked(ctx, src, offset, opts)
|
|
|
|
fd.mu.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements FileDescriptionImpl.Write.
|
|
|
|
func (fd *DynamicBytesFileDescriptionImpl) Write(ctx context.Context, src usermem.IOSequence, opts WriteOptions) (int64, error) {
|
|
|
|
fd.mu.Lock()
|
|
|
|
n, err := fd.pwriteLocked(ctx, src, fd.off, opts)
|
|
|
|
fd.off += n
|
|
|
|
fd.mu.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:17:47 +00:00
|
|
|
// GenericConfigureMMap may be used by most implementations of
|
|
|
|
// FileDescriptionImpl.ConfigureMMap.
|
|
|
|
func GenericConfigureMMap(fd *FileDescription, m memmap.Mappable, opts *memmap.MMapOpts) error {
|
|
|
|
opts.Mappable = m
|
|
|
|
opts.MappingIdentity = fd
|
|
|
|
fd.IncRef()
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-10 01:44:57 +00:00
|
|
|
|
|
|
|
// LockFD may be used by most implementations of FileDescriptionImpl.Lock*
|
|
|
|
// functions. Caller must call Init().
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2020-06-10 01:44:57 +00:00
|
|
|
type LockFD struct {
|
2020-06-17 17:02:41 +00:00
|
|
|
locks *FileLocks
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// SupportsLocks implements FileDescriptionImpl.SupportsLocks.
|
|
|
|
func (LockFD) SupportsLocks() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:44:57 +00:00
|
|
|
// Init initializes fd with FileLocks to use.
|
2020-06-17 17:02:41 +00:00
|
|
|
func (fd *LockFD) Init(locks *FileLocks) {
|
2020-06-10 01:44:57 +00:00
|
|
|
fd.locks = locks
|
|
|
|
}
|
|
|
|
|
2020-06-17 17:02:41 +00:00
|
|
|
// Locks returns the locks associated with this file.
|
|
|
|
func (fd *LockFD) Locks() *FileLocks {
|
|
|
|
return fd.locks
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// LockBSD implements FileDescriptionImpl.LockBSD.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (fd *LockFD) LockBSD(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, block fslock.Blocker) error {
|
|
|
|
return fd.locks.LockBSD(ctx, uid, ownerPID, t, block)
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// UnlockBSD implements FileDescriptionImpl.UnlockBSD.
|
2020-06-10 01:44:57 +00:00
|
|
|
func (fd *LockFD) UnlockBSD(ctx context.Context, uid fslock.UniqueID) error {
|
|
|
|
fd.locks.UnlockBSD(uid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// LockPOSIX implements FileDescriptionImpl.LockPOSIX.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (fd *LockFD) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block fslock.Blocker) error {
|
|
|
|
return fd.locks.LockPOSIX(ctx, uid, ownerPID, t, r, block)
|
2021-01-21 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// UnlockPOSIX implements FileDescriptionImpl.UnlockPOSIX.
|
2021-01-21 00:51:59 +00:00
|
|
|
func (fd *LockFD) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error {
|
|
|
|
return fd.locks.UnlockPOSIX(ctx, uid, r)
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// TestPOSIX implements FileDescriptionImpl.TestPOSIX.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (fd *LockFD) TestPOSIX(ctx context.Context, uid fslock.UniqueID, t fslock.LockType, r fslock.LockRange) (linux.Flock, error) {
|
|
|
|
return fd.locks.TestPOSIX(ctx, uid, t, r)
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:44:57 +00:00
|
|
|
// NoLockFD implements Lock*/Unlock* portion of FileDescriptionImpl interface
|
|
|
|
// returning ENOLCK.
|
2020-09-24 17:11:10 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2020-06-10 01:44:57 +00:00
|
|
|
type NoLockFD struct{}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// SupportsLocks implements FileDescriptionImpl.SupportsLocks.
|
|
|
|
func (NoLockFD) SupportsLocks() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockBSD implements FileDescriptionImpl.LockBSD.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (NoLockFD) LockBSD(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, block fslock.Blocker) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOLCK
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// UnlockBSD implements FileDescriptionImpl.UnlockBSD.
|
2020-06-10 01:44:57 +00:00
|
|
|
func (NoLockFD) UnlockBSD(ctx context.Context, uid fslock.UniqueID) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOLCK
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// LockPOSIX implements FileDescriptionImpl.LockPOSIX.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (NoLockFD) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block fslock.Blocker) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOLCK
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// UnlockPOSIX implements FileDescriptionImpl.UnlockPOSIX.
|
2021-01-21 00:51:59 +00:00
|
|
|
func (NoLockFD) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linuxerr.ENOLCK
|
2020-06-10 01:44:57 +00:00
|
|
|
}
|
2021-01-22 21:55:42 +00:00
|
|
|
|
2021-05-14 21:02:04 +00:00
|
|
|
// TestPOSIX implements FileDescriptionImpl.TestPOSIX.
|
2021-01-22 21:55:42 +00:00
|
|
|
func (NoLockFD) TestPOSIX(ctx context.Context, uid fslock.UniqueID, t fslock.LockType, r fslock.LockRange) (linux.Flock, error) {
|
2021-07-01 19:02:59 +00:00
|
|
|
return linux.Flock{}, linuxerr.ENOLCK
|
2021-01-22 21:55:42 +00:00
|
|
|
}
|
2021-05-14 21:02:04 +00:00
|
|
|
|
|
|
|
// BadLockFD implements Lock*/Unlock* portion of FileDescriptionImpl interface
|
|
|
|
// returning EBADF.
|
|
|
|
//
|
|
|
|
// +stateify savable
|
|
|
|
type BadLockFD struct{}
|
|
|
|
|
|
|
|
// SupportsLocks implements FileDescriptionImpl.SupportsLocks.
|
|
|
|
func (BadLockFD) SupportsLocks() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockBSD implements FileDescriptionImpl.LockBSD.
|
|
|
|
func (BadLockFD) LockBSD(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, block fslock.Blocker) error {
|
2021-06-30 15:15:44 +00:00
|
|
|
return linuxerr.EBADF
|
2021-05-14 21:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnlockBSD implements FileDescriptionImpl.UnlockBSD.
|
|
|
|
func (BadLockFD) UnlockBSD(ctx context.Context, uid fslock.UniqueID) error {
|
2021-06-30 15:15:44 +00:00
|
|
|
return linuxerr.EBADF
|
2021-05-14 21:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockPOSIX implements FileDescriptionImpl.LockPOSIX.
|
|
|
|
func (BadLockFD) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block fslock.Blocker) error {
|
2021-06-30 15:15:44 +00:00
|
|
|
return linuxerr.EBADF
|
2021-05-14 21:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnlockPOSIX implements FileDescriptionImpl.UnlockPOSIX.
|
|
|
|
func (BadLockFD) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error {
|
2021-06-30 15:15:44 +00:00
|
|
|
return linuxerr.EBADF
|
2021-05-14 21:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPOSIX implements FileDescriptionImpl.TestPOSIX.
|
|
|
|
func (BadLockFD) TestPOSIX(ctx context.Context, uid fslock.UniqueID, t fslock.LockType, r fslock.LockRange) (linux.Flock, error) {
|
2021-06-30 15:15:44 +00:00
|
|
|
return linux.Flock{}, linuxerr.EBADF
|
2021-05-14 21:02:04 +00:00
|
|
|
}
|