mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Sentry virtual filesystem, v2
Major differences from the current ("v1") sentry VFS:
- Path resolution is Filesystem-driven (FilesystemImpl methods call
vfs.ResolvingPath methods) rather than VFS-driven (fs package owns a
Dirent tree and calls fs.InodeOperations methods to populate it). This
drastically improves performance, primarily by reducing overhead from
inefficient synchronization and indirection. It also makes it possible
to implement remote filesystem protocols that translate FS system calls
into single RPCs, rather than having to make (at least) one RPC per path
component, significantly reducing the latency of remote filesystems
(especially during cold starts and for uncacheable shared filesystems).
- Mounts are correctly represented as a separate check based on
contextual state (current mount) rather than direct replacement in a
fs.Dirent tree. This makes it possible to support (non-recursive) bind
mounts and mount namespaces.
Included in this CL is fsimpl/memfs, an incomplete in-memory filesystem
that exists primarily to demonstrate intended filesystem implementation
patterns and for benchmarking:
BenchmarkVFS1TmpfsStat/1-6 3000000 497 ns/op
BenchmarkVFS1TmpfsStat/2-6 2000000 676 ns/op
BenchmarkVFS1TmpfsStat/3-6 2000000 904 ns/op
BenchmarkVFS1TmpfsStat/8-6 1000000 1944 ns/op
BenchmarkVFS1TmpfsStat/64-6 100000 14067 ns/op
BenchmarkVFS1TmpfsStat/100-6 50000 21700 ns/op
BenchmarkVFS2MemfsStat/1-6 10000000 197 ns/op
BenchmarkVFS2MemfsStat/2-6 5000000 233 ns/op
BenchmarkVFS2MemfsStat/3-6 5000000 268 ns/op
BenchmarkVFS2MemfsStat/8-6 3000000 477 ns/op
BenchmarkVFS2MemfsStat/64-6 500000 2592 ns/op
BenchmarkVFS2MemfsStat/100-6 300000 4045 ns/op
BenchmarkVFS1TmpfsMountStat/1-6 2000000 679 ns/op
BenchmarkVFS1TmpfsMountStat/2-6 2000000 912 ns/op
BenchmarkVFS1TmpfsMountStat/3-6 1000000 1113 ns/op
BenchmarkVFS1TmpfsMountStat/8-6 1000000 2118 ns/op
BenchmarkVFS1TmpfsMountStat/64-6 100000 14251 ns/op
BenchmarkVFS1TmpfsMountStat/100-6 100000 22397 ns/op
BenchmarkVFS2MemfsMountStat/1-6 5000000 317 ns/op
BenchmarkVFS2MemfsMountStat/2-6 5000000 361 ns/op
BenchmarkVFS2MemfsMountStat/3-6 5000000 387 ns/op
BenchmarkVFS2MemfsMountStat/8-6 3000000 582 ns/op
BenchmarkVFS2MemfsMountStat/64-6 500000 2699 ns/op
BenchmarkVFS2MemfsMountStat/100-6 300000 4133 ns/op
From this we can infer that, on this machine:
- Constant cost for tmpfs stat() is ~160ns in VFS2 and ~280ns in VFS1.
- Per-path-component cost is ~35ns in VFS2 and ~215ns in VFS1, a
difference of about 6x.
- The cost of crossing a mount boundary is about 80ns in VFS2
(MemfsMountStat/1 does approximately the same amount of work as
MemfsStat/2, except that it also crosses a mount boundary). This is an
inescapable cost of the separate mount lookup needed to support bind
mounts and mount namespaces.
PiperOrigin-RevId: 258853946
This commit is contained in:
+61
-40
@@ -24,25 +24,27 @@ import (
|
||||
|
||||
// Constants for open(2).
|
||||
const (
|
||||
O_ACCMODE = 00000003
|
||||
O_RDONLY = 00000000
|
||||
O_WRONLY = 00000001
|
||||
O_RDWR = 00000002
|
||||
O_CREAT = 00000100
|
||||
O_EXCL = 00000200
|
||||
O_NOCTTY = 00000400
|
||||
O_TRUNC = 00001000
|
||||
O_APPEND = 00002000
|
||||
O_NONBLOCK = 00004000
|
||||
O_DSYNC = 00010000
|
||||
O_ASYNC = 00020000
|
||||
O_DIRECT = 00040000
|
||||
O_LARGEFILE = 00100000
|
||||
O_DIRECTORY = 00200000
|
||||
O_NOFOLLOW = 00400000
|
||||
O_CLOEXEC = 02000000
|
||||
O_SYNC = 04000000
|
||||
O_ACCMODE = 000000003
|
||||
O_RDONLY = 000000000
|
||||
O_WRONLY = 000000001
|
||||
O_RDWR = 000000002
|
||||
O_CREAT = 000000100
|
||||
O_EXCL = 000000200
|
||||
O_NOCTTY = 000000400
|
||||
O_TRUNC = 000001000
|
||||
O_APPEND = 000002000
|
||||
O_NONBLOCK = 000004000
|
||||
O_DSYNC = 000010000
|
||||
O_ASYNC = 000020000
|
||||
O_DIRECT = 000040000
|
||||
O_LARGEFILE = 000100000
|
||||
O_DIRECTORY = 000200000
|
||||
O_NOFOLLOW = 000400000
|
||||
O_NOATIME = 001000000
|
||||
O_CLOEXEC = 002000000
|
||||
O_SYNC = 004000000 // __O_SYNC in Linux
|
||||
O_PATH = 010000000
|
||||
O_TMPFILE = 020000000 // __O_TMPFILE in Linux
|
||||
)
|
||||
|
||||
// Constants for fstatat(2).
|
||||
@@ -124,14 +126,23 @@ const (
|
||||
|
||||
// Values for mode_t.
|
||||
const (
|
||||
FileTypeMask = 0170000
|
||||
ModeSocket = 0140000
|
||||
ModeSymlink = 0120000
|
||||
ModeRegular = 0100000
|
||||
ModeBlockDevice = 060000
|
||||
ModeDirectory = 040000
|
||||
ModeCharacterDevice = 020000
|
||||
ModeNamedPipe = 010000
|
||||
S_IFMT = 0170000
|
||||
S_IFSOCK = 0140000
|
||||
S_IFLNK = 0120000
|
||||
S_IFREG = 0100000
|
||||
S_IFBLK = 060000
|
||||
S_IFDIR = 040000
|
||||
S_IFCHR = 020000
|
||||
S_IFIFO = 010000
|
||||
|
||||
FileTypeMask = S_IFMT
|
||||
ModeSocket = S_IFSOCK
|
||||
ModeSymlink = S_IFLNK
|
||||
ModeRegular = S_IFREG
|
||||
ModeBlockDevice = S_IFBLK
|
||||
ModeDirectory = S_IFDIR
|
||||
ModeCharacterDevice = S_IFCHR
|
||||
ModeNamedPipe = S_IFIFO
|
||||
|
||||
ModeSetUID = 04000
|
||||
ModeSetGID = 02000
|
||||
@@ -152,6 +163,19 @@ const (
|
||||
PermissionsMask = 0777
|
||||
)
|
||||
|
||||
// Values for linux_dirent64.d_type.
|
||||
const (
|
||||
DT_UNKNOWN = 0
|
||||
DT_FIFO = 1
|
||||
DT_CHR = 2
|
||||
DT_DIR = 4
|
||||
DT_BLK = 6
|
||||
DT_REG = 8
|
||||
DT_LNK = 10
|
||||
DT_SOCK = 12
|
||||
DT_WHT = 14
|
||||
)
|
||||
|
||||
// Values for preadv2/pwritev2.
|
||||
const (
|
||||
RWF_HIPRI = 0x00000001
|
||||
@@ -179,19 +203,6 @@ type Stat struct {
|
||||
_ [3]int64
|
||||
}
|
||||
|
||||
// File types.
|
||||
const (
|
||||
DT_BLK = 0x6
|
||||
DT_CHR = 0x2
|
||||
DT_DIR = 0x4
|
||||
DT_FIFO = 0x1
|
||||
DT_LNK = 0xa
|
||||
DT_REG = 0x8
|
||||
DT_SOCK = 0xc
|
||||
DT_UNKNOWN = 0x0
|
||||
DT_WHT = 0xe
|
||||
)
|
||||
|
||||
// SizeOfStat is the size of a Stat struct.
|
||||
var SizeOfStat = binary.Size(Stat{})
|
||||
|
||||
@@ -222,6 +233,17 @@ const (
|
||||
STATX__RESERVED = 0x80000000
|
||||
)
|
||||
|
||||
// Bitmasks for Statx.Attributes and Statx.AttributesMask, from
|
||||
// include/uapi/linux/stat.h.
|
||||
const (
|
||||
STATX_ATTR_COMPRESSED = 0x00000004
|
||||
STATX_ATTR_IMMUTABLE = 0x00000010
|
||||
STATX_ATTR_APPEND = 0x00000020
|
||||
STATX_ATTR_NODUMP = 0x00000040
|
||||
STATX_ATTR_ENCRYPTED = 0x00000800
|
||||
STATX_ATTR_AUTOMOUNT = 0x00001000
|
||||
)
|
||||
|
||||
// Statx represents struct statx.
|
||||
type Statx struct {
|
||||
Mask uint32
|
||||
@@ -231,7 +253,6 @@ type Statx struct {
|
||||
UID uint32
|
||||
GID uint32
|
||||
Mode uint16
|
||||
_ uint16
|
||||
Ino uint64
|
||||
Size uint64
|
||||
Blocks uint64
|
||||
|
||||
@@ -77,6 +77,15 @@ type Statfs struct {
|
||||
Spare [4]uint64
|
||||
}
|
||||
|
||||
// Whence argument to lseek(2), from include/uapi/linux/fs.h.
|
||||
const (
|
||||
SEEK_SET = 0
|
||||
SEEK_CUR = 1
|
||||
SEEK_END = 2
|
||||
SEEK_DATA = 3
|
||||
SEEK_HOLE = 4
|
||||
)
|
||||
|
||||
// Sync_file_range flags, from include/uapi/linux/fs.h
|
||||
const (
|
||||
SYNC_FILE_RANGE_WAIT_BEFORE = 1
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
load("//tools/go_stateify:defs.bzl", "go_library", "go_test")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
licenses = ["notice"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "fspath",
|
||||
srcs = [
|
||||
"builder.go",
|
||||
"builder_unsafe.go",
|
||||
"fspath.go",
|
||||
],
|
||||
importpath = "gvisor.dev/gvisor/pkg/fspath",
|
||||
deps = ["//pkg/syserror"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "fspath_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"builder_test.go",
|
||||
"fspath_test.go",
|
||||
],
|
||||
embed = [":fspath"],
|
||||
deps = ["//pkg/syserror"],
|
||||
)
|
||||
@@ -0,0 +1,104 @@
|
||||
// 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 fspath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Builder is similar to strings.Builder, but is used to produce pathnames
|
||||
// given path components in reverse order (from leaf to root). This is useful
|
||||
// in the common case where a filesystem is represented by a tree of named
|
||||
// nodes, and the path to a given node must be produced by walking upward from
|
||||
// that node to a given root.
|
||||
type Builder struct {
|
||||
buf []byte
|
||||
start int
|
||||
needSep bool
|
||||
}
|
||||
|
||||
// Reset resets the Builder to be empty.
|
||||
func (b *Builder) Reset() {
|
||||
b.start = len(b.buf)
|
||||
b.needSep = false
|
||||
}
|
||||
|
||||
// Len returns the number of accumulated bytes.
|
||||
func (b *Builder) Len() int {
|
||||
return len(b.buf) - b.start
|
||||
}
|
||||
|
||||
func (b *Builder) needToGrow(n int) bool {
|
||||
return b.start < n
|
||||
}
|
||||
|
||||
func (b *Builder) grow(n int) {
|
||||
newLen := b.Len() + n
|
||||
var newCap int
|
||||
if len(b.buf) == 0 {
|
||||
newCap = 64 // arbitrary
|
||||
} else {
|
||||
newCap = 2 * len(b.buf)
|
||||
}
|
||||
for newCap < newLen {
|
||||
newCap *= 2
|
||||
if newCap == 0 {
|
||||
panic(fmt.Sprintf("required length (%d) causes buffer size to overflow", newLen))
|
||||
}
|
||||
}
|
||||
newBuf := make([]byte, newCap)
|
||||
copy(newBuf[newCap-b.Len():], b.buf[b.start:])
|
||||
b.start += newCap - len(b.buf)
|
||||
b.buf = newBuf
|
||||
}
|
||||
|
||||
// PrependComponent prepends the given path component to b's buffer. A path
|
||||
// separator is automatically inserted if appropriate.
|
||||
func (b *Builder) PrependComponent(pc string) {
|
||||
if b.needSep {
|
||||
b.PrependByte('/')
|
||||
}
|
||||
b.PrependString(pc)
|
||||
b.needSep = true
|
||||
}
|
||||
|
||||
// PrependString prepends the given string to b's buffer.
|
||||
func (b *Builder) PrependString(str string) {
|
||||
if b.needToGrow(len(str)) {
|
||||
b.grow(len(str))
|
||||
}
|
||||
b.start -= len(str)
|
||||
copy(b.buf[b.start:], str)
|
||||
}
|
||||
|
||||
// PrependByte prepends the given byte to b's buffer.
|
||||
func (b *Builder) PrependByte(c byte) {
|
||||
if b.needToGrow(1) {
|
||||
b.grow(1)
|
||||
}
|
||||
b.start--
|
||||
b.buf[b.start] = c
|
||||
}
|
||||
|
||||
// AppendString appends the given string to b's buffer.
|
||||
func (b *Builder) AppendString(str string) {
|
||||
if b.needToGrow(len(str)) {
|
||||
b.grow(len(str))
|
||||
}
|
||||
oldStart := b.start
|
||||
b.start -= len(str)
|
||||
copy(b.buf[b.start:], b.buf[oldStart:])
|
||||
copy(b.buf[len(b.buf)-len(str):], str)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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 fspath
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuilder(t *testing.T) {
|
||||
type testCase struct {
|
||||
pcs []string // path components in reverse order
|
||||
after string
|
||||
want string
|
||||
}
|
||||
tests := []testCase{
|
||||
{
|
||||
// Empty case.
|
||||
},
|
||||
{
|
||||
pcs: []string{"foo"},
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
pcs: []string{"foo", "bar", "baz"},
|
||||
want: "baz/bar/foo",
|
||||
},
|
||||
{
|
||||
pcs: []string{"foo", "bar"},
|
||||
after: " (deleted)",
|
||||
want: "bar/foo (deleted)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.want, func(t *testing.T) {
|
||||
var b Builder
|
||||
for _, pc := range test.pcs {
|
||||
b.PrependComponent(pc)
|
||||
}
|
||||
b.AppendString(test.after)
|
||||
if got := b.String(); got != test.want {
|
||||
t.Errorf("got %q, wanted %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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 fspath
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// String returns the accumulated string. No other methods should be called
|
||||
// after String.
|
||||
func (b *Builder) String() string {
|
||||
bs := b.buf[b.start:]
|
||||
// Compare strings.Builder.String().
|
||||
return *(*string)(unsafe.Pointer(&bs))
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
// 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 fspath provides efficient tools for working with file paths in
|
||||
// Linux-compatible filesystem implementations.
|
||||
package fspath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
const pathSep = '/'
|
||||
|
||||
// Parse parses a pathname as described by path_resolution(7).
|
||||
func Parse(pathname string) (Path, error) {
|
||||
if len(pathname) == 0 {
|
||||
// "... POSIX decrees that an empty pathname must not be resolved
|
||||
// successfully. Linux returns ENOENT in this case." -
|
||||
// path_resolution(7)
|
||||
return Path{}, syserror.ENOENT
|
||||
}
|
||||
// Skip leading path separators.
|
||||
i := 0
|
||||
for pathname[i] == pathSep {
|
||||
i++
|
||||
if i == len(pathname) {
|
||||
// pathname consists entirely of path separators.
|
||||
return Path{
|
||||
Absolute: true,
|
||||
Dir: true,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
// Skip trailing path separators. This is required by Iterator.Next. This
|
||||
// loop is guaranteed to terminate with j >= 0 because otherwise the
|
||||
// pathname would consist entirely of path separators, so we would have
|
||||
// returned above.
|
||||
j := len(pathname) - 1
|
||||
for pathname[j] == pathSep {
|
||||
j--
|
||||
}
|
||||
// Find the end of the first path component.
|
||||
firstEnd := i + 1
|
||||
for firstEnd != len(pathname) && pathname[firstEnd] != pathSep {
|
||||
firstEnd++
|
||||
}
|
||||
return Path{
|
||||
Begin: Iterator{
|
||||
partialPathname: pathname[i : j+1],
|
||||
end: firstEnd - i,
|
||||
},
|
||||
Absolute: i != 0,
|
||||
Dir: j != len(pathname)-1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Path contains the information contained in a pathname string.
|
||||
//
|
||||
// Path is copyable by value.
|
||||
type Path struct {
|
||||
// Begin is an iterator to the first path component in the relative part of
|
||||
// the path.
|
||||
//
|
||||
// Path doesn't store information about path components after the first
|
||||
// since this would require allocation.
|
||||
Begin Iterator
|
||||
|
||||
// If true, the path is absolute, such that lookup should begin at the
|
||||
// filesystem root. If false, the path is relative, such that where lookup
|
||||
// begins is unspecified.
|
||||
Absolute bool
|
||||
|
||||
// If true, the pathname contains trailing path separators, so the last
|
||||
// path component must exist and resolve to a directory.
|
||||
Dir bool
|
||||
}
|
||||
|
||||
// String returns a pathname string equivalent to p. Note that the returned
|
||||
// string is not necessarily equal to the string p was parsed from; in
|
||||
// particular, redundant path separators will not be present.
|
||||
func (p Path) String() string {
|
||||
var b strings.Builder
|
||||
if p.Absolute {
|
||||
b.WriteByte(pathSep)
|
||||
}
|
||||
sep := false
|
||||
for pit := p.Begin; pit.Ok(); pit = pit.Next() {
|
||||
if sep {
|
||||
b.WriteByte(pathSep)
|
||||
}
|
||||
b.WriteString(pit.String())
|
||||
sep = true
|
||||
}
|
||||
// Don't return "//" for Parse("/").
|
||||
if p.Dir && p.Begin.Ok() {
|
||||
b.WriteByte(pathSep)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// An Iterator represents either a path component in a Path or a terminal
|
||||
// iterator indicating that the end of the path has been reached.
|
||||
//
|
||||
// Iterator is immutable and copyable by value. The zero value of Iterator is
|
||||
// valid, and represents a terminal iterator.
|
||||
type Iterator struct {
|
||||
// partialPathname is a substring of the original pathname beginning at the
|
||||
// start of the represented path component and ending immediately after the
|
||||
// end of the last path component in the pathname. If partialPathname is
|
||||
// empty, the PathnameIterator is terminal.
|
||||
//
|
||||
// See TestParseIteratorPartialPathnames in fspath_test.go for a worked
|
||||
// example.
|
||||
partialPathname string
|
||||
|
||||
// end is the offset into partialPathname of the first byte after the end
|
||||
// of the represented path component.
|
||||
end int
|
||||
}
|
||||
|
||||
// Ok returns true if it is not terminal.
|
||||
func (it Iterator) Ok() bool {
|
||||
return len(it.partialPathname) != 0
|
||||
}
|
||||
|
||||
// String returns the path component represented by it.
|
||||
//
|
||||
// Preconditions: it.Ok().
|
||||
func (it Iterator) String() string {
|
||||
return it.partialPathname[:it.end]
|
||||
}
|
||||
|
||||
// Next returns an iterator to the path component after it. If it is the last
|
||||
// component in the path, Next returns a terminal iterator.
|
||||
//
|
||||
// Preconditions: it.Ok().
|
||||
func (it Iterator) Next() Iterator {
|
||||
if it.end == len(it.partialPathname) {
|
||||
// End of the path.
|
||||
return Iterator{}
|
||||
}
|
||||
// Skip path separators. Since Parse trims trailing path separators, if we
|
||||
// aren't at the end of the path, there is definitely another path
|
||||
// component.
|
||||
i := it.end + 1
|
||||
for {
|
||||
if it.partialPathname[i] != pathSep {
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
nextPartialPathname := it.partialPathname[i:]
|
||||
// Find the end of this path component.
|
||||
nextEnd := 1
|
||||
for nextEnd < len(nextPartialPathname) && nextPartialPathname[nextEnd] != pathSep {
|
||||
nextEnd++
|
||||
}
|
||||
return Iterator{
|
||||
partialPathname: nextPartialPathname,
|
||||
end: nextEnd,
|
||||
}
|
||||
}
|
||||
|
||||
// NextOk is equivalent to it.Next().Ok(), but is faster.
|
||||
//
|
||||
// Preconditions: it.Ok().
|
||||
func (it Iterator) NextOk() bool {
|
||||
return it.end != len(it.partialPathname)
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
// 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 fspath
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
func TestParseIteratorPartialPathnames(t *testing.T) {
|
||||
path, err := Parse("/foo//bar///baz////")
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
// Parse strips leading slashes, and records their presence as
|
||||
// Path.Absolute.
|
||||
if !path.Absolute {
|
||||
t.Errorf("Path.Absolute: got false, wanted true")
|
||||
}
|
||||
// Parse strips trailing slashes, and records their presence as Path.Dir.
|
||||
if !path.Dir {
|
||||
t.Errorf("Path.Dir: got false, wanted true")
|
||||
}
|
||||
// The first Iterator.partialPathname is the input pathname, with leading
|
||||
// and trailing slashes stripped.
|
||||
it := path.Begin
|
||||
if want := "foo//bar///baz"; it.partialPathname != want {
|
||||
t.Errorf("first Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want)
|
||||
}
|
||||
// Successive Iterator.partialPathnames remove the leading path component
|
||||
// and following slashes, until we run out of path components and get a
|
||||
// terminal Iterator.
|
||||
it = it.Next()
|
||||
if want := "bar///baz"; it.partialPathname != want {
|
||||
t.Errorf("second Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want)
|
||||
}
|
||||
it = it.Next()
|
||||
if want := "baz"; it.partialPathname != want {
|
||||
t.Errorf("third Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want)
|
||||
}
|
||||
it = it.Next()
|
||||
if want := ""; it.partialPathname != want {
|
||||
t.Errorf("fourth Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want)
|
||||
}
|
||||
if it.Ok() {
|
||||
t.Errorf("fourth Iterator.Ok(): got true, wanted false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
type testCase struct {
|
||||
pathname string
|
||||
relpath []string
|
||||
abs bool
|
||||
dir bool
|
||||
}
|
||||
tests := []testCase{
|
||||
{
|
||||
pathname: "/",
|
||||
relpath: []string{},
|
||||
abs: true,
|
||||
dir: true,
|
||||
},
|
||||
{
|
||||
pathname: "//",
|
||||
relpath: []string{},
|
||||
abs: true,
|
||||
dir: true,
|
||||
},
|
||||
}
|
||||
for _, sep := range []string{"/", "//"} {
|
||||
for _, abs := range []bool{false, true} {
|
||||
for _, dir := range []bool{false, true} {
|
||||
for _, pcs := range [][]string{
|
||||
// single path component
|
||||
{"foo"},
|
||||
// multiple path components, including non-UTF-8
|
||||
{".", "foo", "..", "\xe6", "bar"},
|
||||
} {
|
||||
prefix := ""
|
||||
if abs {
|
||||
prefix = sep
|
||||
}
|
||||
suffix := ""
|
||||
if dir {
|
||||
suffix = sep
|
||||
}
|
||||
tests = append(tests, testCase{
|
||||
pathname: prefix + strings.Join(pcs, sep) + suffix,
|
||||
relpath: pcs,
|
||||
abs: abs,
|
||||
dir: dir,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.pathname, func(t *testing.T) {
|
||||
p, err := Parse(test.pathname)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse pathname %q: %v", test.pathname, err)
|
||||
}
|
||||
t.Logf("pathname %q => path %q", test.pathname, p)
|
||||
if p.Absolute != test.abs {
|
||||
t.Errorf("path absoluteness: got %v, wanted %v", p.Absolute, test.abs)
|
||||
}
|
||||
if p.Dir != test.dir {
|
||||
t.Errorf("path must resolve to a directory: got %v, wanted %v", p.Dir, test.dir)
|
||||
}
|
||||
pcs := []string{}
|
||||
for pit := p.Begin; pit.Ok(); pit = pit.Next() {
|
||||
pcs = append(pcs, pit.String())
|
||||
}
|
||||
if !reflect.DeepEqual(pcs, test.relpath) {
|
||||
t.Errorf("relative path: got %v, wanted %v", pcs, test.relpath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEmptyPathname(t *testing.T) {
|
||||
p, err := Parse("")
|
||||
if err != syserror.ENOENT {
|
||||
t.Errorf("parsing empty pathname: got (%v, %v), wanted (<unspecified>, ENOENT)", p, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
load("//tools/go_stateify:defs.bzl", "go_library", "go_test")
|
||||
|
||||
package(licenses = ["notice"])
|
||||
|
||||
load("//tools/go_generics:defs.bzl", "go_template_instance")
|
||||
|
||||
go_template_instance(
|
||||
name = "dentry_list",
|
||||
out = "dentry_list.go",
|
||||
package = "memfs",
|
||||
prefix = "dentry",
|
||||
template = "//pkg/ilist:generic_list",
|
||||
types = {
|
||||
"Element": "*Dentry",
|
||||
"Linker": "*Dentry",
|
||||
},
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "memfs",
|
||||
srcs = [
|
||||
"dentry_list.go",
|
||||
"directory.go",
|
||||
"filesystem.go",
|
||||
"memfs.go",
|
||||
"regular_file.go",
|
||||
"symlink.go",
|
||||
],
|
||||
importpath = "gvisor.dev/gvisor/pkg/sentry/fsimpl/memfs",
|
||||
deps = [
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/sentry/context",
|
||||
"//pkg/sentry/kernel/auth",
|
||||
"//pkg/sentry/usermem",
|
||||
"//pkg/sentry/vfs",
|
||||
"//pkg/syserror",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "benchmark_test",
|
||||
size = "small",
|
||||
srcs = ["benchmark_test.go"],
|
||||
deps = [
|
||||
":memfs",
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/sentry/context",
|
||||
"//pkg/sentry/context/contexttest",
|
||||
"//pkg/sentry/fs",
|
||||
"//pkg/sentry/fs/tmpfs",
|
||||
"//pkg/sentry/kernel/auth",
|
||||
"//pkg/sentry/vfs",
|
||||
"//pkg/syserror",
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,464 @@
|
||||
// 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 benchmark_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/sentry/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/context/contexttest"
|
||||
"gvisor.dev/gvisor/pkg/sentry/fs"
|
||||
_ "gvisor.dev/gvisor/pkg/sentry/fs/tmpfs"
|
||||
"gvisor.dev/gvisor/pkg/sentry/fsimpl/memfs"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
// Differences from stat_benchmark:
|
||||
//
|
||||
// - Syscall interception, CopyInPath, copyOutStat, and overlayfs overheads are
|
||||
// not included.
|
||||
//
|
||||
// - *MountStat benchmarks use a tmpfs root mount and a tmpfs submount at /tmp.
|
||||
// Non-MountStat benchmarks use a tmpfs root mount and no submounts.
|
||||
// stat_benchmark uses a varying root mount, a tmpfs submount at /tmp, and a
|
||||
// subdirectory /tmp/<top_dir> (assuming TEST_TMPDIR == "/tmp"). Thus
|
||||
// stat_benchmark at depth 1 does a comparable amount of work to *MountStat
|
||||
// benchmarks at depth 2, and non-MountStat benchmarks at depth 3.
|
||||
var depths = []int{1, 2, 3, 8, 64, 100}
|
||||
|
||||
const (
|
||||
mountPointName = "tmp"
|
||||
filename = "gvisor_test_temp_0_1557494568"
|
||||
)
|
||||
|
||||
// This is copied from syscalls/linux/sys_file.go, with the dependency on
|
||||
// kernel.Task stripped out.
|
||||
func fileOpOn(ctx context.Context, mntns *fs.MountNamespace, root, wd *fs.Dirent, dirFD int32, path string, resolve bool, fn func(root *fs.Dirent, d *fs.Dirent) error) error {
|
||||
var (
|
||||
d *fs.Dirent // The file.
|
||||
rel *fs.Dirent // The relative directory for search (if required.)
|
||||
err error
|
||||
)
|
||||
|
||||
// Extract the working directory (maybe).
|
||||
if len(path) > 0 && path[0] == '/' {
|
||||
// Absolute path; rel can be nil.
|
||||
} else if dirFD == linux.AT_FDCWD {
|
||||
// Need to reference the working directory.
|
||||
rel = wd
|
||||
} else {
|
||||
// Need to extract the given FD.
|
||||
return syserror.EBADF
|
||||
}
|
||||
|
||||
// Lookup the node.
|
||||
remainingTraversals := uint(linux.MaxSymlinkTraversals)
|
||||
if resolve {
|
||||
d, err = mntns.FindInode(ctx, root, rel, path, &remainingTraversals)
|
||||
} else {
|
||||
d, err = mntns.FindLink(ctx, root, rel, path, &remainingTraversals)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = fn(root, d)
|
||||
d.DecRef()
|
||||
return err
|
||||
}
|
||||
|
||||
func BenchmarkVFS1TmpfsStat(b *testing.B) {
|
||||
for _, depth := range depths {
|
||||
b.Run(fmt.Sprintf("%d", depth), func(b *testing.B) {
|
||||
ctx := contexttest.Context(b)
|
||||
|
||||
// Create VFS.
|
||||
tmpfsFS, ok := fs.FindFilesystem("tmpfs")
|
||||
if !ok {
|
||||
b.Fatalf("failed to find tmpfs filesystem type")
|
||||
}
|
||||
rootInode, err := tmpfsFS.Mount(ctx, "tmpfs", fs.MountSourceFlags{}, "", nil)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create tmpfs root mount: %v", err)
|
||||
}
|
||||
mntns, err := fs.NewMountNamespace(ctx, rootInode)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create mount namespace: %v", err)
|
||||
}
|
||||
defer mntns.DecRef()
|
||||
|
||||
var filePathBuilder strings.Builder
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create nested directories with given depth.
|
||||
root := mntns.Root()
|
||||
defer root.DecRef()
|
||||
d := root
|
||||
d.IncRef()
|
||||
defer d.DecRef()
|
||||
for i := depth; i > 0; i-- {
|
||||
name := fmt.Sprintf("%d", i)
|
||||
if err := d.Inode.CreateDirectory(ctx, d, name, fs.FilePermsFromMode(0755)); err != nil {
|
||||
b.Fatalf("failed to create directory %q: %v", name, err)
|
||||
}
|
||||
next, err := d.Walk(ctx, root, name)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to directory %q: %v", name, err)
|
||||
}
|
||||
d.DecRef()
|
||||
d = next
|
||||
filePathBuilder.WriteString(name)
|
||||
filePathBuilder.WriteByte('/')
|
||||
}
|
||||
|
||||
// Create the file that will be stat'd.
|
||||
file, err := d.Inode.Create(ctx, d, filename, fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0644))
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create file %q: %v", filename, err)
|
||||
}
|
||||
file.DecRef()
|
||||
filePathBuilder.WriteString(filename)
|
||||
filePath := filePathBuilder.String()
|
||||
|
||||
dirPath := false
|
||||
runtime.GC()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := fileOpOn(ctx, mntns, root, root, linux.AT_FDCWD, filePath, true /* resolve */, func(root *fs.Dirent, d *fs.Dirent) error {
|
||||
if dirPath && !fs.IsDir(d.Inode.StableAttr) {
|
||||
return syserror.ENOTDIR
|
||||
}
|
||||
uattr, err := d.Inode.UnstableAttr(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Sanity check.
|
||||
if uattr.Perms.User.Execute {
|
||||
b.Fatalf("got wrong permissions (%0o)", uattr.Perms.LinuxMode())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
b.Fatalf("stat(%q) failed: %v", filePath, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVFS2MemfsStat(b *testing.B) {
|
||||
for _, depth := range depths {
|
||||
b.Run(fmt.Sprintf("%d", depth), func(b *testing.B) {
|
||||
ctx := contexttest.Context(b)
|
||||
creds := auth.CredentialsFromContext(ctx)
|
||||
|
||||
// Create VFS.
|
||||
vfsObj := vfs.New()
|
||||
vfsObj.MustRegisterFilesystemType("memfs", memfs.FilesystemType{})
|
||||
mntns, err := vfsObj.NewMountNamespace(ctx, creds, "", "memfs", &vfs.NewFilesystemOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create tmpfs root mount: %v", err)
|
||||
}
|
||||
|
||||
var filePathBuilder strings.Builder
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create nested directories with given depth.
|
||||
root := mntns.Root()
|
||||
defer root.DecRef()
|
||||
vd := root
|
||||
vd.IncRef()
|
||||
defer vd.DecRef()
|
||||
for i := depth; i > 0; i-- {
|
||||
name := fmt.Sprintf("%d", i)
|
||||
pop := vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: vd,
|
||||
Pathname: name,
|
||||
}
|
||||
if err := vfsObj.MkdirAt(ctx, creds, &pop, &vfs.MkdirOptions{
|
||||
Mode: 0755,
|
||||
}); err != nil {
|
||||
b.Fatalf("failed to create directory %q: %v", name, err)
|
||||
}
|
||||
nextVD, err := vfsObj.GetDentryAt(ctx, creds, &pop, &vfs.GetDentryOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to directory %q: %v", name, err)
|
||||
}
|
||||
vd.DecRef()
|
||||
vd = nextVD
|
||||
filePathBuilder.WriteString(name)
|
||||
filePathBuilder.WriteByte('/')
|
||||
}
|
||||
|
||||
// Create the file that will be stat'd.
|
||||
fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: vd,
|
||||
Pathname: filename,
|
||||
FollowFinalSymlink: true,
|
||||
}, &vfs.OpenOptions{
|
||||
Flags: linux.O_RDWR | linux.O_CREAT | linux.O_EXCL,
|
||||
Mode: 0644,
|
||||
})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create file %q: %v", filename, err)
|
||||
}
|
||||
defer fd.DecRef()
|
||||
filePathBuilder.WriteString(filename)
|
||||
filePath := filePathBuilder.String()
|
||||
|
||||
runtime.GC()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
stat, err := vfsObj.StatAt(ctx, creds, &vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: root,
|
||||
Pathname: filePath,
|
||||
FollowFinalSymlink: true,
|
||||
}, &vfs.StatOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("stat(%q) failed: %v", filePath, err)
|
||||
}
|
||||
// Sanity check.
|
||||
if stat.Mode&^linux.S_IFMT != 0644 {
|
||||
b.Fatalf("got wrong permissions (%0o)", stat.Mode)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVFS1TmpfsMountStat(b *testing.B) {
|
||||
for _, depth := range depths {
|
||||
b.Run(fmt.Sprintf("%d", depth), func(b *testing.B) {
|
||||
ctx := contexttest.Context(b)
|
||||
|
||||
// Create VFS.
|
||||
tmpfsFS, ok := fs.FindFilesystem("tmpfs")
|
||||
if !ok {
|
||||
b.Fatalf("failed to find tmpfs filesystem type")
|
||||
}
|
||||
rootInode, err := tmpfsFS.Mount(ctx, "tmpfs", fs.MountSourceFlags{}, "", nil)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create tmpfs root mount: %v", err)
|
||||
}
|
||||
mntns, err := fs.NewMountNamespace(ctx, rootInode)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create mount namespace: %v", err)
|
||||
}
|
||||
defer mntns.DecRef()
|
||||
|
||||
var filePathBuilder strings.Builder
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create and mount the submount.
|
||||
root := mntns.Root()
|
||||
defer root.DecRef()
|
||||
if err := root.Inode.CreateDirectory(ctx, root, mountPointName, fs.FilePermsFromMode(0755)); err != nil {
|
||||
b.Fatalf("failed to create mount point: %v", err)
|
||||
}
|
||||
mountPoint, err := root.Walk(ctx, root, mountPointName)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to mount point: %v", err)
|
||||
}
|
||||
defer mountPoint.DecRef()
|
||||
submountInode, err := tmpfsFS.Mount(ctx, "tmpfs", fs.MountSourceFlags{}, "", nil)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create tmpfs submount: %v", err)
|
||||
}
|
||||
if err := mntns.Mount(ctx, mountPoint, submountInode); err != nil {
|
||||
b.Fatalf("failed to mount tmpfs submount: %v", err)
|
||||
}
|
||||
filePathBuilder.WriteString(mountPointName)
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create nested directories with given depth.
|
||||
d, err := root.Walk(ctx, root, mountPointName)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to mount root: %v", err)
|
||||
}
|
||||
defer d.DecRef()
|
||||
for i := depth; i > 0; i-- {
|
||||
name := fmt.Sprintf("%d", i)
|
||||
if err := d.Inode.CreateDirectory(ctx, d, name, fs.FilePermsFromMode(0755)); err != nil {
|
||||
b.Fatalf("failed to create directory %q: %v", name, err)
|
||||
}
|
||||
next, err := d.Walk(ctx, root, name)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to directory %q: %v", name, err)
|
||||
}
|
||||
d.DecRef()
|
||||
d = next
|
||||
filePathBuilder.WriteString(name)
|
||||
filePathBuilder.WriteByte('/')
|
||||
}
|
||||
|
||||
// Create the file that will be stat'd.
|
||||
file, err := d.Inode.Create(ctx, d, filename, fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0644))
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create file %q: %v", filename, err)
|
||||
}
|
||||
file.DecRef()
|
||||
filePathBuilder.WriteString(filename)
|
||||
filePath := filePathBuilder.String()
|
||||
|
||||
dirPath := false
|
||||
runtime.GC()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := fileOpOn(ctx, mntns, root, root, linux.AT_FDCWD, filePath, true /* resolve */, func(root *fs.Dirent, d *fs.Dirent) error {
|
||||
if dirPath && !fs.IsDir(d.Inode.StableAttr) {
|
||||
return syserror.ENOTDIR
|
||||
}
|
||||
uattr, err := d.Inode.UnstableAttr(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Sanity check.
|
||||
if uattr.Perms.User.Execute {
|
||||
b.Fatalf("got wrong permissions (%0o)", uattr.Perms.LinuxMode())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
b.Fatalf("stat(%q) failed: %v", filePath, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVFS2MemfsMountStat(b *testing.B) {
|
||||
for _, depth := range depths {
|
||||
b.Run(fmt.Sprintf("%d", depth), func(b *testing.B) {
|
||||
ctx := contexttest.Context(b)
|
||||
creds := auth.CredentialsFromContext(ctx)
|
||||
|
||||
// Create VFS.
|
||||
vfsObj := vfs.New()
|
||||
vfsObj.MustRegisterFilesystemType("memfs", memfs.FilesystemType{})
|
||||
mntns, err := vfsObj.NewMountNamespace(ctx, creds, "", "memfs", &vfs.NewFilesystemOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create tmpfs root mount: %v", err)
|
||||
}
|
||||
|
||||
var filePathBuilder strings.Builder
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create the mount point.
|
||||
root := mntns.Root()
|
||||
defer root.DecRef()
|
||||
pop := vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: root,
|
||||
Pathname: mountPointName,
|
||||
}
|
||||
if err := vfsObj.MkdirAt(ctx, creds, &pop, &vfs.MkdirOptions{
|
||||
Mode: 0755,
|
||||
}); err != nil {
|
||||
b.Fatalf("failed to create mount point: %v", err)
|
||||
}
|
||||
// Save the mount point for later use.
|
||||
mountPoint, err := vfsObj.GetDentryAt(ctx, creds, &pop, &vfs.GetDentryOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to mount point: %v", err)
|
||||
}
|
||||
defer mountPoint.DecRef()
|
||||
// Create and mount the submount.
|
||||
if err := vfsObj.NewMount(ctx, creds, "", &pop, "memfs", &vfs.NewFilesystemOptions{}); err != nil {
|
||||
b.Fatalf("failed to mount tmpfs submount: %v", err)
|
||||
}
|
||||
filePathBuilder.WriteString(mountPointName)
|
||||
filePathBuilder.WriteByte('/')
|
||||
|
||||
// Create nested directories with given depth.
|
||||
vd, err := vfsObj.GetDentryAt(ctx, creds, &pop, &vfs.GetDentryOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to mount root: %v", err)
|
||||
}
|
||||
defer vd.DecRef()
|
||||
for i := depth; i > 0; i-- {
|
||||
name := fmt.Sprintf("%d", i)
|
||||
pop := vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: vd,
|
||||
Pathname: name,
|
||||
}
|
||||
if err := vfsObj.MkdirAt(ctx, creds, &pop, &vfs.MkdirOptions{
|
||||
Mode: 0755,
|
||||
}); err != nil {
|
||||
b.Fatalf("failed to create directory %q: %v", name, err)
|
||||
}
|
||||
nextVD, err := vfsObj.GetDentryAt(ctx, creds, &pop, &vfs.GetDentryOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to walk to directory %q: %v", name, err)
|
||||
}
|
||||
vd.DecRef()
|
||||
vd = nextVD
|
||||
filePathBuilder.WriteString(name)
|
||||
filePathBuilder.WriteByte('/')
|
||||
}
|
||||
|
||||
// Verify that we didn't create any directories under the mount
|
||||
// point (i.e. they were all created on the submount).
|
||||
firstDirName := fmt.Sprintf("%d", depth)
|
||||
if child := mountPoint.Dentry().Child(firstDirName); child != nil {
|
||||
b.Fatalf("created directory %q under root mount, not submount", firstDirName)
|
||||
}
|
||||
|
||||
// Create the file that will be stat'd.
|
||||
fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: vd,
|
||||
Pathname: filename,
|
||||
FollowFinalSymlink: true,
|
||||
}, &vfs.OpenOptions{
|
||||
Flags: linux.O_RDWR | linux.O_CREAT | linux.O_EXCL,
|
||||
Mode: 0644,
|
||||
})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create file %q: %v", filename, err)
|
||||
}
|
||||
fd.DecRef()
|
||||
filePathBuilder.WriteString(filename)
|
||||
filePath := filePathBuilder.String()
|
||||
|
||||
runtime.GC()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
stat, err := vfsObj.StatAt(ctx, creds, &vfs.PathOperation{
|
||||
Root: root,
|
||||
Start: root,
|
||||
Pathname: filePath,
|
||||
FollowFinalSymlink: true,
|
||||
}, &vfs.StatOptions{})
|
||||
if err != nil {
|
||||
b.Fatalf("stat(%q) failed: %v", filePath, err)
|
||||
}
|
||||
// Sanity check.
|
||||
if stat.Mode&^linux.S_IFMT != 0644 {
|
||||
b.Fatalf("got wrong permissions (%0o)", stat.Mode)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
// 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 memfs
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/sentry/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
type directory struct {
|
||||
inode Inode
|
||||
|
||||
// childList is a list containing (1) child Dentries and (2) fake Dentries
|
||||
// (with inode == nil) that represent the iteration position of
|
||||
// directoryFDs. childList is used to support directoryFD.IterDirents()
|
||||
// efficiently. childList is protected by Filesystem.mu.
|
||||
childList dentryList
|
||||
}
|
||||
|
||||
func (fs *Filesystem) newDirectory(creds *auth.Credentials, mode uint16) *Inode {
|
||||
dir := &directory{}
|
||||
dir.inode.init(dir, fs, creds, mode)
|
||||
dir.inode.nlink = 2 // from "." and parent directory or ".." for root
|
||||
return &dir.inode
|
||||
}
|
||||
|
||||
func (i *Inode) isDir() bool {
|
||||
_, ok := i.impl.(*directory)
|
||||
return ok
|
||||
}
|
||||
|
||||
type directoryFD struct {
|
||||
fileDescription
|
||||
vfs.DirectoryFileDescriptionDefaultImpl
|
||||
|
||||
// Protected by Filesystem.mu.
|
||||
iter *Dentry
|
||||
off int64
|
||||
}
|
||||
|
||||
// Release implements vfs.FileDescriptionImpl.Release.
|
||||
func (fd *directoryFD) Release() {
|
||||
if fd.iter != nil {
|
||||
fs := fd.filesystem()
|
||||
dir := fd.inode().impl.(*directory)
|
||||
fs.mu.Lock()
|
||||
dir.childList.Remove(fd.iter)
|
||||
fs.mu.Unlock()
|
||||
fd.iter = nil
|
||||
}
|
||||
}
|
||||
|
||||
// IterDirents implements vfs.FileDescriptionImpl.IterDirents.
|
||||
func (fd *directoryFD) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback) error {
|
||||
fs := fd.filesystem()
|
||||
d := fd.vfsfd.VirtualDentry().Dentry()
|
||||
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
if fd.off == 0 {
|
||||
if !cb.Handle(vfs.Dirent{
|
||||
Name: ".",
|
||||
Type: linux.DT_DIR,
|
||||
Ino: d.Impl().(*Dentry).inode.ino,
|
||||
Off: 0,
|
||||
}) {
|
||||
return nil
|
||||
}
|
||||
fd.off++
|
||||
}
|
||||
if fd.off == 1 {
|
||||
parentInode := d.ParentOrSelf().Impl().(*Dentry).inode
|
||||
if !cb.Handle(vfs.Dirent{
|
||||
Name: "..",
|
||||
Type: parentInode.direntType(),
|
||||
Ino: parentInode.ino,
|
||||
Off: 1,
|
||||
}) {
|
||||
return nil
|
||||
}
|
||||
fd.off++
|
||||
}
|
||||
|
||||
dir := d.Impl().(*Dentry).inode.impl.(*directory)
|
||||
var child *Dentry
|
||||
if fd.iter == nil {
|
||||
// Start iteration at the beginning of dir.
|
||||
child = dir.childList.Front()
|
||||
fd.iter = &Dentry{}
|
||||
} else {
|
||||
// Continue iteration from where we left off.
|
||||
child = fd.iter.Next()
|
||||
dir.childList.Remove(fd.iter)
|
||||
}
|
||||
for child != nil {
|
||||
// Skip other directoryFD iterators.
|
||||
if child.inode != nil {
|
||||
if !cb.Handle(vfs.Dirent{
|
||||
Name: child.vfsd.Name(),
|
||||
Type: child.inode.direntType(),
|
||||
Ino: child.inode.ino,
|
||||
Off: fd.off,
|
||||
}) {
|
||||
dir.childList.InsertBefore(child, fd.iter)
|
||||
return nil
|
||||
}
|
||||
fd.off++
|
||||
}
|
||||
child = child.Next()
|
||||
}
|
||||
dir.childList.PushBack(fd.iter)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Seek implements vfs.FileDescriptionImpl.Seek.
|
||||
func (fd *directoryFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
|
||||
if whence != linux.SEEK_SET {
|
||||
// TODO: Linux also allows SEEK_CUR.
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
if offset < 0 {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
|
||||
fd.off = offset
|
||||
// Compensate for "." and "..".
|
||||
var remChildren int64
|
||||
if offset < 2 {
|
||||
remChildren = 0
|
||||
} else {
|
||||
remChildren = offset - 2
|
||||
}
|
||||
|
||||
fs := fd.filesystem()
|
||||
dir := fd.inode().impl.(*directory)
|
||||
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
// Ensure that fd.iter exists and is not linked into dir.childList.
|
||||
if fd.iter == nil {
|
||||
fd.iter = &Dentry{}
|
||||
} else {
|
||||
dir.childList.Remove(fd.iter)
|
||||
}
|
||||
// Insert fd.iter before the remChildren'th child, or at the end of the
|
||||
// list if remChildren >= number of children.
|
||||
child := dir.childList.Front()
|
||||
for child != nil {
|
||||
// Skip other directoryFD iterators.
|
||||
if child.inode != nil {
|
||||
if remChildren == 0 {
|
||||
dir.childList.InsertBefore(child, fd.iter)
|
||||
return offset, nil
|
||||
}
|
||||
remChildren--
|
||||
}
|
||||
child = child.Next()
|
||||
}
|
||||
dir.childList.PushBack(fd.iter)
|
||||
return offset, nil
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,299 @@
|
||||
// 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 memfs provides a filesystem implementation that behaves like tmpfs:
|
||||
// the Dentry tree is the sole source of truth for the state of the filesystem.
|
||||
//
|
||||
// memfs is intended primarily to demonstrate filesystem implementation
|
||||
// patterns. Real uses cases for an in-memory filesystem should use tmpfs
|
||||
// instead.
|
||||
//
|
||||
// Lock order:
|
||||
//
|
||||
// Filesystem.mu
|
||||
// regularFileFD.offMu
|
||||
// regularFile.mu
|
||||
// Inode.mu
|
||||
package memfs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/sentry/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
// FilesystemType implements vfs.FilesystemType.
|
||||
type FilesystemType struct{}
|
||||
|
||||
// Filesystem implements vfs.FilesystemImpl.
|
||||
type Filesystem struct {
|
||||
vfsfs vfs.Filesystem
|
||||
|
||||
// mu serializes changes to the Dentry tree.
|
||||
mu sync.RWMutex
|
||||
|
||||
nextInoMinusOne uint64 // accessed using atomic memory operations
|
||||
}
|
||||
|
||||
// NewFilesystem implements vfs.FilesystemType.NewFilesystem.
|
||||
func (fstype FilesystemType) NewFilesystem(ctx context.Context, creds *auth.Credentials, source string, opts vfs.NewFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) {
|
||||
var fs Filesystem
|
||||
fs.vfsfs.Init(&fs)
|
||||
root := fs.newDentry(fs.newDirectory(creds, 01777))
|
||||
return &fs.vfsfs, &root.vfsd, nil
|
||||
}
|
||||
|
||||
// Release implements vfs.FilesystemImpl.Release.
|
||||
func (fs *Filesystem) Release() {
|
||||
}
|
||||
|
||||
// Sync implements vfs.FilesystemImpl.Sync.
|
||||
func (fs *Filesystem) Sync(ctx context.Context) error {
|
||||
// All filesystem state is in-memory.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dentry implements vfs.DentryImpl.
|
||||
type Dentry struct {
|
||||
vfsd vfs.Dentry
|
||||
|
||||
// inode is the inode represented by this Dentry. Multiple Dentries may
|
||||
// share a single non-directory Inode (with hard links). inode is
|
||||
// immutable.
|
||||
inode *Inode
|
||||
|
||||
// memfs doesn't count references on Dentries; because the Dentry tree is
|
||||
// the sole source of truth, it is by definition always consistent with the
|
||||
// state of the filesystem. However, it does count references on Inodes,
|
||||
// because Inode resources are released when all references are dropped.
|
||||
// (memfs doesn't really have resources to release, but we implement
|
||||
// reference counting because tmpfs regular files will.)
|
||||
|
||||
// dentryEntry (ugh) links Dentries into their parent directory.childList.
|
||||
dentryEntry
|
||||
}
|
||||
|
||||
func (fs *Filesystem) newDentry(inode *Inode) *Dentry {
|
||||
d := &Dentry{
|
||||
inode: inode,
|
||||
}
|
||||
d.vfsd.Init(d)
|
||||
return d
|
||||
}
|
||||
|
||||
// IncRef implements vfs.DentryImpl.IncRef.
|
||||
func (d *Dentry) IncRef(vfsfs *vfs.Filesystem) {
|
||||
d.inode.incRef()
|
||||
}
|
||||
|
||||
// TryIncRef implements vfs.DentryImpl.TryIncRef.
|
||||
func (d *Dentry) TryIncRef(vfsfs *vfs.Filesystem) bool {
|
||||
return d.inode.tryIncRef()
|
||||
}
|
||||
|
||||
// DecRef implements vfs.DentryImpl.DecRef.
|
||||
func (d *Dentry) DecRef(vfsfs *vfs.Filesystem) {
|
||||
d.inode.decRef()
|
||||
}
|
||||
|
||||
// Inode represents a filesystem object.
|
||||
type Inode struct {
|
||||
// refs is a reference count. refs is accessed using atomic memory
|
||||
// operations.
|
||||
//
|
||||
// A reference is held on all Inodes that are reachable in the filesystem
|
||||
// tree. For non-directories (which may have multiple hard links), this
|
||||
// means that a reference is dropped when nlink reaches 0. For directories,
|
||||
// nlink never reaches 0 due to the "." entry; instead,
|
||||
// Filesystem.RmdirAt() drops the reference.
|
||||
refs int64
|
||||
|
||||
// Inode metadata; protected by mu and accessed using atomic memory
|
||||
// operations unless otherwise specified.
|
||||
mu sync.RWMutex
|
||||
mode uint32 // excluding file type bits, which are based on impl
|
||||
nlink uint32 // protected by Filesystem.mu instead of Inode.mu
|
||||
uid uint32 // auth.KUID, but stored as raw uint32 for sync/atomic
|
||||
gid uint32 // auth.KGID, but ...
|
||||
ino uint64 // immutable
|
||||
|
||||
impl interface{} // immutable
|
||||
}
|
||||
|
||||
func (i *Inode) init(impl interface{}, fs *Filesystem, creds *auth.Credentials, mode uint16) {
|
||||
i.refs = 1
|
||||
i.mode = uint32(mode)
|
||||
i.uid = uint32(creds.EffectiveKUID)
|
||||
i.gid = uint32(creds.EffectiveKGID)
|
||||
i.ino = atomic.AddUint64(&fs.nextInoMinusOne, 1)
|
||||
// i.nlink initialized by caller
|
||||
i.impl = impl
|
||||
}
|
||||
|
||||
// Preconditions: Filesystem.mu must be locked for writing.
|
||||
func (i *Inode) incLinksLocked() {
|
||||
if atomic.AddUint32(&i.nlink, 1) <= 1 {
|
||||
panic("memfs.Inode.incLinksLocked() called with no existing links")
|
||||
}
|
||||
}
|
||||
|
||||
// Preconditions: Filesystem.mu must be locked for writing.
|
||||
func (i *Inode) decLinksLocked() {
|
||||
if nlink := atomic.AddUint32(&i.nlink, ^uint32(0)); nlink == 0 {
|
||||
i.decRef()
|
||||
} else if nlink == ^uint32(0) { // negative overflow
|
||||
panic("memfs.Inode.decLinksLocked() called with no existing links")
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Inode) incRef() {
|
||||
if atomic.AddInt64(&i.refs, 1) <= 1 {
|
||||
panic("memfs.Inode.incRef() called without holding a reference")
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Inode) tryIncRef() bool {
|
||||
for {
|
||||
refs := atomic.LoadInt64(&i.refs)
|
||||
if refs == 0 {
|
||||
return false
|
||||
}
|
||||
if atomic.CompareAndSwapInt64(&i.refs, refs, refs+1) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Inode) decRef() {
|
||||
if refs := atomic.AddInt64(&i.refs, -1); refs == 0 {
|
||||
// This is unnecessary; it's mostly to simulate what tmpfs would do.
|
||||
if regfile, ok := i.impl.(*regularFile); ok {
|
||||
regfile.mu.Lock()
|
||||
regfile.data = nil
|
||||
atomic.StoreInt64(®file.dataLen, 0)
|
||||
regfile.mu.Unlock()
|
||||
}
|
||||
} else if refs < 0 {
|
||||
panic("memfs.Inode.decRef() called without holding a reference")
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Inode) checkPermissions(creds *auth.Credentials, ats vfs.AccessTypes, isDir bool) error {
|
||||
return vfs.GenericCheckPermissions(creds, ats, isDir, uint16(atomic.LoadUint32(&i.mode)), auth.KUID(atomic.LoadUint32(&i.uid)), auth.KGID(atomic.LoadUint32(&i.gid)))
|
||||
}
|
||||
|
||||
// Go won't inline this function, and returning linux.Statx (which is quite
|
||||
// big) means spending a lot of time in runtime.duffcopy(), so instead it's an
|
||||
// output parameter.
|
||||
func (i *Inode) statTo(stat *linux.Statx) {
|
||||
stat.Mask = linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_NLINK | linux.STATX_UID | linux.STATX_GID | linux.STATX_INO
|
||||
stat.Blksize = 1 // usermem.PageSize in tmpfs
|
||||
stat.Nlink = atomic.LoadUint32(&i.nlink)
|
||||
stat.UID = atomic.LoadUint32(&i.uid)
|
||||
stat.GID = atomic.LoadUint32(&i.gid)
|
||||
stat.Mode = uint16(atomic.LoadUint32(&i.mode))
|
||||
stat.Ino = i.ino
|
||||
// TODO: device number
|
||||
switch impl := i.impl.(type) {
|
||||
case *regularFile:
|
||||
stat.Mode |= linux.S_IFREG
|
||||
stat.Mask |= linux.STATX_SIZE | linux.STATX_BLOCKS
|
||||
stat.Size = uint64(atomic.LoadInt64(&impl.dataLen))
|
||||
// In tmpfs, this will be FileRangeSet.Span() / 512 (but also cached in
|
||||
// a uint64 accessed using atomic memory operations to avoid taking
|
||||
// locks).
|
||||
stat.Blocks = allocatedBlocksForSize(stat.Size)
|
||||
case *directory:
|
||||
stat.Mode |= linux.S_IFDIR
|
||||
case *symlink:
|
||||
stat.Mode |= linux.S_IFLNK
|
||||
stat.Mask |= linux.STATX_SIZE | linux.STATX_BLOCKS
|
||||
stat.Size = uint64(len(impl.target))
|
||||
stat.Blocks = allocatedBlocksForSize(stat.Size)
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown inode type: %T", i.impl))
|
||||
}
|
||||
}
|
||||
|
||||
// allocatedBlocksForSize returns the number of 512B blocks needed to
|
||||
// accommodate the given size in bytes, as appropriate for struct
|
||||
// stat::st_blocks and struct statx::stx_blocks. (Note that this 512B block
|
||||
// size is independent of the "preferred block size for I/O", struct
|
||||
// stat::st_blksize and struct statx::stx_blksize.)
|
||||
func allocatedBlocksForSize(size uint64) uint64 {
|
||||
return (size + 511) / 512
|
||||
}
|
||||
|
||||
func (i *Inode) direntType() uint8 {
|
||||
switch i.impl.(type) {
|
||||
case *regularFile:
|
||||
return linux.DT_REG
|
||||
case *directory:
|
||||
return linux.DT_DIR
|
||||
case *symlink:
|
||||
return linux.DT_LNK
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown inode type: %T", i.impl))
|
||||
}
|
||||
}
|
||||
|
||||
// fileDescription is embedded by memfs implementations of
|
||||
// vfs.FileDescriptionImpl.
|
||||
type fileDescription struct {
|
||||
vfsfd vfs.FileDescription
|
||||
|
||||
flags uint32 // status flags; immutable
|
||||
}
|
||||
|
||||
func (fd *fileDescription) filesystem() *Filesystem {
|
||||
return fd.vfsfd.VirtualDentry().Mount().Filesystem().Impl().(*Filesystem)
|
||||
}
|
||||
|
||||
func (fd *fileDescription) inode() *Inode {
|
||||
return fd.vfsfd.VirtualDentry().Dentry().Impl().(*Dentry).inode
|
||||
}
|
||||
|
||||
// StatusFlags implements vfs.FileDescriptionImpl.StatusFlags.
|
||||
func (fd *fileDescription) StatusFlags(ctx context.Context) (uint32, error) {
|
||||
return fd.flags, nil
|
||||
}
|
||||
|
||||
// SetStatusFlags implements vfs.FileDescriptionImpl.SetStatusFlags.
|
||||
func (fd *fileDescription) SetStatusFlags(ctx context.Context, flags uint32) error {
|
||||
// None of the flags settable by fcntl(F_SETFL) are supported, so this is a
|
||||
// no-op.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stat implements vfs.FileDescriptionImpl.Stat.
|
||||
func (fd *fileDescription) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
|
||||
var stat linux.Statx
|
||||
fd.inode().statTo(&stat)
|
||||
return stat, nil
|
||||
}
|
||||
|
||||
// SetStat implements vfs.FileDescriptionImpl.SetStat.
|
||||
func (fd *fileDescription) SetStat(ctx context.Context, opts vfs.SetStatOptions) error {
|
||||
if opts.Stat.Mask == 0 {
|
||||
return nil
|
||||
}
|
||||
// TODO: implement Inode.setStat
|
||||
return syserror.EPERM
|
||||
}
|
||||
@@ -0,0 +1,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 memfs
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/sentry/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
||||
"gvisor.dev/gvisor/pkg/sentry/usermem"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
type regularFile struct {
|
||||
inode Inode
|
||||
|
||||
mu sync.RWMutex
|
||||
data []byte
|
||||
// dataLen is len(data), but accessed using atomic memory operations to
|
||||
// avoid locking in Inode.stat().
|
||||
dataLen int64
|
||||
}
|
||||
|
||||
func (fs *Filesystem) newRegularFile(creds *auth.Credentials, mode uint16) *Inode {
|
||||
file := ®ularFile{}
|
||||
file.inode.init(file, fs, creds, mode)
|
||||
file.inode.nlink = 1 // from parent directory
|
||||
return &file.inode
|
||||
}
|
||||
|
||||
type regularFileFD struct {
|
||||
fileDescription
|
||||
vfs.FileDescriptionDefaultImpl
|
||||
|
||||
// These are immutable.
|
||||
readable bool
|
||||
writable bool
|
||||
|
||||
// off is the file offset. off is accessed using atomic memory operations.
|
||||
// offMu serializes operations that may mutate off.
|
||||
off int64
|
||||
offMu sync.Mutex
|
||||
}
|
||||
|
||||
// Release implements vfs.FileDescriptionImpl.Release.
|
||||
func (fd *regularFileFD) Release() {
|
||||
if fd.writable {
|
||||
fd.vfsfd.VirtualDentry().Mount().EndWrite()
|
||||
}
|
||||
}
|
||||
|
||||
// PRead implements vfs.FileDescriptionImpl.PRead.
|
||||
func (fd *regularFileFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
|
||||
if !fd.readable {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
f := fd.inode().impl.(*regularFile)
|
||||
f.mu.RLock()
|
||||
if offset >= int64(len(f.data)) {
|
||||
f.mu.RUnlock()
|
||||
return 0, io.EOF
|
||||
}
|
||||
n, err := dst.CopyOut(ctx, f.data[offset:])
|
||||
f.mu.RUnlock()
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
// Read implements vfs.FileDescriptionImpl.Read.
|
||||
func (fd *regularFileFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
|
||||
fd.offMu.Lock()
|
||||
n, err := fd.PRead(ctx, dst, fd.off, opts)
|
||||
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) {
|
||||
if !fd.writable {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
if offset < 0 {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
srclen := src.NumBytes()
|
||||
if srclen == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
f := fd.inode().impl.(*regularFile)
|
||||
f.mu.Lock()
|
||||
end := offset + srclen
|
||||
if end < offset {
|
||||
// Overflow.
|
||||
f.mu.Unlock()
|
||||
return 0, syserror.EFBIG
|
||||
}
|
||||
if end > f.dataLen {
|
||||
f.data = append(f.data, make([]byte, end-f.dataLen)...)
|
||||
atomic.StoreInt64(&f.dataLen, end)
|
||||
}
|
||||
n, err := src.CopyIn(ctx, f.data[offset:end])
|
||||
f.mu.Unlock()
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
// Write implements vfs.FileDescriptionImpl.Write.
|
||||
func (fd *regularFileFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
|
||||
fd.offMu.Lock()
|
||||
n, err := fd.PWrite(ctx, src, fd.off, opts)
|
||||
fd.off += n
|
||||
fd.offMu.Unlock()
|
||||
return n, err
|
||||
}
|
||||
|
||||
// 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 += atomic.LoadInt64(&fd.inode().impl.(*regularFile).dataLen)
|
||||
default:
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
if offset < 0 {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
fd.off = offset
|
||||
return offset, nil
|
||||
}
|
||||
|
||||
// Sync implements vfs.FileDescriptionImpl.Sync.
|
||||
func (fd *regularFileFD) Sync(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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 memfs
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
||||
)
|
||||
|
||||
type symlink struct {
|
||||
inode Inode
|
||||
target string // immutable
|
||||
}
|
||||
|
||||
func (fs *Filesystem) newSymlink(creds *auth.Credentials, target string) *Inode {
|
||||
link := &symlink{
|
||||
target: target,
|
||||
}
|
||||
link.inode.init(link, fs, creds, 0777)
|
||||
link.inode.nlink = 1 // from parent directory
|
||||
return &link.inode
|
||||
}
|
||||
|
||||
// O_PATH is unimplemented, so there's no way to get a FileDescription
|
||||
// representing a symlink yet.
|
||||
@@ -0,0 +1,46 @@
|
||||
load("//tools/go_stateify:defs.bzl", "go_library", "go_test")
|
||||
|
||||
package(licenses = ["notice"])
|
||||
|
||||
go_library(
|
||||
name = "vfs",
|
||||
srcs = [
|
||||
"context.go",
|
||||
"debug.go",
|
||||
"dentry.go",
|
||||
"file_description.go",
|
||||
"file_description_impl_util.go",
|
||||
"filesystem.go",
|
||||
"filesystem_type.go",
|
||||
"mount.go",
|
||||
"mount_unsafe.go",
|
||||
"options.go",
|
||||
"permissions.go",
|
||||
"resolving_path.go",
|
||||
"syscalls.go",
|
||||
"vfs.go",
|
||||
],
|
||||
importpath = "gvisor.dev/gvisor/pkg/sentry/vfs",
|
||||
visibility = ["//pkg/sentry:internal"],
|
||||
deps = [
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/fspath",
|
||||
"//pkg/sentry/arch",
|
||||
"//pkg/sentry/context",
|
||||
"//pkg/sentry/kernel/auth",
|
||||
"//pkg/sentry/memmap",
|
||||
"//pkg/sentry/usermem",
|
||||
"//pkg/syserror",
|
||||
"//pkg/waiter",
|
||||
"//third_party/gvsync",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "vfs_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"mount_test.go",
|
||||
],
|
||||
embed = [":vfs"],
|
||||
)
|
||||
@@ -0,0 +1,197 @@
|
||||
# The gVisor Virtual Filesystem
|
||||
|
||||
THIS PACKAGE IS CURRENTLY EXPERIMENTAL AND NOT READY OR ENABLED FOR PRODUCTION
|
||||
USE. For the filesystem implementation currently used by gVisor, see the `fs`
|
||||
package.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
### Reference Counting
|
||||
|
||||
Filesystem, Dentry, Mount, MountNamespace, and FileDescription are all
|
||||
reference-counted. Mount and MountNamespace are exclusively VFS-managed; when
|
||||
their reference count reaches zero, VFS releases their resources. Filesystem and
|
||||
FileDescription management is shared between VFS and filesystem implementations;
|
||||
when their reference count reaches zero, VFS notifies the implementation by
|
||||
calling `FilesystemImpl.Release()` or `FileDescriptionImpl.Release()`
|
||||
respectively and then releases VFS-owned resources. Dentries are exclusively
|
||||
managed by filesystem implementations; reference count changes are abstracted
|
||||
through DentryImpl, which should release resources when reference count reaches
|
||||
zero.
|
||||
|
||||
Filesystem references are held by:
|
||||
|
||||
- Mount: Each referenced Mount holds a reference on the mounted Filesystem.
|
||||
|
||||
Dentry references are held by:
|
||||
|
||||
- FileDescription: Each referenced FileDescription holds a reference on the
|
||||
Dentry through which it was opened, via `FileDescription.vd.dentry`.
|
||||
|
||||
- Mount: Each referenced Mount holds a reference on its mount point and on the
|
||||
mounted filesystem root. The mount point is mutable (`mount(MS_MOVE)`).
|
||||
|
||||
Mount references are held by:
|
||||
|
||||
- FileDescription: Each referenced FileDescription holds a reference on the
|
||||
Mount on which it was opened, via `FileDescription.vd.mount`.
|
||||
|
||||
- Mount: Each referenced Mount holds a reference on its parent, which is the
|
||||
mount containing its mount point.
|
||||
|
||||
- VirtualFilesystem: A reference is held on all Mounts that are attached
|
||||
(reachable by Mount traversal).
|
||||
|
||||
MountNamespace and FileDescription references are held by users of VFS. The
|
||||
expectation is that each `kernel.Task` holds a reference on its corresponding
|
||||
MountNamespace, and each file descriptor holds a reference on its represented
|
||||
FileDescription.
|
||||
|
||||
Notes:
|
||||
|
||||
- Dentries do not hold a reference on their owning Filesystem. Instead, all
|
||||
uses of a Dentry occur in the context of a Mount, which holds a reference on
|
||||
the relevant Filesystem (see e.g. the VirtualDentry type). As a corollary,
|
||||
when releasing references on both a Dentry and its corresponding Mount, the
|
||||
Dentry's reference must be released first (because releasing the Mount's
|
||||
reference may release the last reference on the Filesystem, whose state may
|
||||
be required to release the Dentry reference).
|
||||
|
||||
### The Inheritance Pattern
|
||||
|
||||
Filesystem, Dentry, and FileDescription are all concepts featuring both state
|
||||
that must be shared between VFS and filesystem implementations, and operations
|
||||
that are implementation-defined. To facilitate this, each of these three
|
||||
concepts follows the same pattern, shown below for Dentry:
|
||||
|
||||
```go
|
||||
// Dentry represents a node in a filesystem tree.
|
||||
type Dentry struct {
|
||||
// VFS-required dentry state.
|
||||
parent *Dentry
|
||||
// ...
|
||||
|
||||
// impl is the DentryImpl associated with this Dentry. impl is immutable.
|
||||
// This should be the last field in Dentry.
|
||||
impl DentryImpl
|
||||
}
|
||||
|
||||
// Init must be called before first use of d.
|
||||
func (d *Dentry) Init(impl DentryImpl) {
|
||||
d.impl = impl
|
||||
}
|
||||
|
||||
// Impl returns the DentryImpl associated with d.
|
||||
func (d *Dentry) Impl() DentryImpl {
|
||||
return d.impl
|
||||
}
|
||||
|
||||
// DentryImpl contains implementation-specific details of a Dentry.
|
||||
// Implementations of DentryImpl should contain their associated Dentry by
|
||||
// value as their first field.
|
||||
type DentryImpl interface {
|
||||
// VFS-required implementation-defined dentry operations.
|
||||
IncRef()
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
This construction, which is essentially a type-safe analogue to Linux's
|
||||
`container_of` pattern, has the following properties:
|
||||
|
||||
- VFS works almost exclusively with pointers to Dentry rather than DentryImpl
|
||||
interface objects, such as in the type of `Dentry.parent`. This avoids
|
||||
interface method calls (which are somewhat expensive to perform, and defeat
|
||||
inlining and escape analysis), reduces the size of VFS types (since an
|
||||
interface object is two pointers in size), and allows pointers to be loaded
|
||||
and stored atomically using `sync/atomic`. Implementation-defined behavior
|
||||
is accessed via `Dentry.impl` when required.
|
||||
|
||||
- Filesystem implementations can access the implementation-defined state
|
||||
associated with objects of VFS types by type-asserting or type-switching
|
||||
(e.g. `Dentry.Impl().(*myDentry)`). Type assertions to a concrete type
|
||||
require only an equality comparison of the interface object's type pointer
|
||||
to a static constant, and are consequently very fast.
|
||||
|
||||
- Filesystem implementations can access the VFS state associated with objects
|
||||
of implementation-defined types directly.
|
||||
|
||||
- VFS and implementation-defined state for a given type occupy the same
|
||||
object, minimizing memory allocations and maximizing memory locality. `impl`
|
||||
is the last field in `Dentry`, and `Dentry` is the first field in
|
||||
`DentryImpl` implementations, for similar reasons: this tends to cause
|
||||
fetching of the `Dentry.impl` interface object to also fetch `DentryImpl`
|
||||
fields, either because they are in the same cache line or via next-line
|
||||
prefetching.
|
||||
|
||||
## Future Work
|
||||
|
||||
- Most `mount(2)` features, and unmounting, are incomplete.
|
||||
|
||||
- VFS1 filesystems are not directly compatible with VFS2. It may be possible
|
||||
to implement shims that implement `vfs.FilesystemImpl` for
|
||||
`fs.MountNamespace`, `vfs.DentryImpl` for `fs.Dirent`, and
|
||||
`vfs.FileDescriptionImpl` for `fs.File`, which may be adequate for
|
||||
filesystems that are not performance-critical (e.g. sysfs); however, it is
|
||||
not clear that this will be less effort than simply porting the filesystems
|
||||
in question. Practically speaking, the following filesystems will probably
|
||||
need to be ported or made compatible through a shim to evaluate filesystem
|
||||
performance on realistic workloads:
|
||||
|
||||
- devfs/procfs/sysfs, which will realistically be necessary to execute
|
||||
most applications. (Note that procfs and sysfs do not support hard
|
||||
links, so they do not require the complexity of separate inode objects.
|
||||
Also note that Linux's /dev is actually a variant of tmpfs called
|
||||
devtmpfs.)
|
||||
|
||||
- tmpfs. This should be relatively straightforward: copy/paste memfs,
|
||||
store regular file contents in pgalloc-allocated memory instead of
|
||||
`[]byte`, and add support for file timestamps. (In fact, it probably
|
||||
makes more sense to convert memfs to tmpfs and not keep the former.)
|
||||
|
||||
- A remote filesystem, either lisafs (if it is ready by the time that
|
||||
other benchmarking prerequisites are) or v9fs (aka 9P, aka gofers).
|
||||
|
||||
- epoll files.
|
||||
|
||||
Filesystems that will need to be ported before switching to VFS2, but can
|
||||
probably be skipped for early testing:
|
||||
|
||||
- overlayfs, which is needed for (at least) synthetic mount points.
|
||||
|
||||
- Support for host ttys.
|
||||
|
||||
- timerfd files.
|
||||
|
||||
Filesystems that can be probably dropped:
|
||||
|
||||
- ashmem, which is far too incomplete to use.
|
||||
|
||||
- binder, which is similarly far too incomplete to use.
|
||||
|
||||
- whitelistfs, which we are already actively attempting to remove.
|
||||
|
||||
- Save/restore. For instance, it is unclear if the current implementation of
|
||||
the `state` package supports the inheritance pattern described above.
|
||||
|
||||
- Many features that were previously implemented by VFS must now be
|
||||
implemented by individual filesystems (though, in most cases, this should
|
||||
consist of calls to hooks or libraries provided by `vfs` or other packages).
|
||||
This includes, but is not necessarily limited to:
|
||||
|
||||
- Block and character device special files
|
||||
|
||||
- Inotify
|
||||
|
||||
- File locking
|
||||
|
||||
- `O_ASYNC`
|
||||
|
||||
- Reference counts in the `vfs` package do not use the `refs` package since
|
||||
`refs.AtomicRefCount` adds 64 bytes of overhead to each 8-byte reference
|
||||
count, resulting in considerable cache bloat. 24 bytes of this overhead is
|
||||
for weak reference support, which have poor performance and will not be used
|
||||
by VFS2. The remaining 40 bytes is to store a descriptive string and stack
|
||||
trace for reference leak checking; we can support reference leak checking
|
||||
without incurring this space overhead by including the applicable
|
||||
information directly in finalizers for applicable types.
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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 (
|
||||
"gvisor.dev/gvisor/pkg/sentry/context"
|
||||
)
|
||||
|
||||
// contextID is this package's type for context.Context.Value keys.
|
||||
type contextID int
|
||||
|
||||
const (
|
||||
// CtxMountNamespace is a Context.Value key for a MountNamespace.
|
||||
CtxMountNamespace contextID = iota
|
||||
)
|
||||
|
||||
// MountNamespaceFromContext returns the MountNamespace used by ctx. It does
|
||||
// not take a reference on the returned MountNamespace. If ctx is not
|
||||
// associated with a MountNamespace, MountNamespaceFromContext returns nil.
|
||||
func MountNamespaceFromContext(ctx context.Context) *MountNamespace {
|
||||
if v := ctx.Value(CtxMountNamespace); v != nil {
|
||||
return v.(*MountNamespace)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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
|
||||
|
||||
const (
|
||||
// If checkInvariants is true, perform runtime checks for invariants
|
||||
// expected by the vfs package. This is normally disabled since VFS is
|
||||
// often a hot path.
|
||||
checkInvariants = false
|
||||
)
|
||||
@@ -0,0 +1,347 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
)
|
||||
|
||||
// Dentry represents a node in a Filesystem tree which may represent a file.
|
||||
//
|
||||
// Dentries are reference-counted. Unless otherwise specified, all Dentry
|
||||
// methods require that a reference is held.
|
||||
//
|
||||
// A Dentry transitions through up to 3 different states through its lifetime:
|
||||
//
|
||||
// - Dentries are initially "independent". Independent Dentries have no parent,
|
||||
// and consequently no name.
|
||||
//
|
||||
// - Dentry.InsertChild() causes an independent Dentry to become a "child" of
|
||||
// another Dentry. A child node has a parent node, and a name in that parent,
|
||||
// both of which are mutable by DentryMoveChild(). Each child Dentry's name is
|
||||
// unique within its parent.
|
||||
//
|
||||
// - Dentry.RemoveChild() causes a child Dentry to become "disowned". A
|
||||
// disowned Dentry can still refer to its former parent and its former name in
|
||||
// said parent, but the disowned Dentry is no longer reachable from its parent,
|
||||
// and a new Dentry with the same name may become a child of the parent. (This
|
||||
// is analogous to a struct dentry being "unhashed" in Linux.)
|
||||
//
|
||||
// Dentry is loosely analogous to Linux's struct dentry, but:
|
||||
//
|
||||
// - VFS does not associate Dentries with inodes. gVisor interacts primarily
|
||||
// with filesystems that are accessed through filesystem APIs (as opposed to
|
||||
// raw block devices); many such APIs support only paths and file descriptors,
|
||||
// and not inodes. Furthermore, when parties outside the scope of VFS can
|
||||
// rename inodes on such filesystems, VFS generally cannot "follow" the rename,
|
||||
// both due to synchronization issues and because it may not even be able to
|
||||
// name the destination path; this implies that it would in fact be *incorrect*
|
||||
// for Dentries to be associated with inodes on such filesystems. Consequently,
|
||||
// operations that are inode operations in Linux are FilesystemImpl methods
|
||||
// and/or FileDescriptionImpl methods in gVisor's VFS. Filesystems that do
|
||||
// support inodes may store appropriate state in implementations of DentryImpl.
|
||||
//
|
||||
// - VFS does not provide synchronization for mutable Dentry fields, other than
|
||||
// mount-related ones.
|
||||
//
|
||||
// - VFS does not require that Dentries are instantiated for all paths accessed
|
||||
// through VFS, only those that are tracked beyond the scope of a single
|
||||
// Filesystem operation. This includes file descriptions, mount points, mount
|
||||
// roots, process working directories, and chroots. This avoids instantiation
|
||||
// of Dentries for operations on mutable remote filesystems that can't actually
|
||||
// cache any state in the Dentry.
|
||||
//
|
||||
// - For the reasons above, VFS is not directly responsible for managing Dentry
|
||||
// lifetime. Dentry reference counts only indicate the extent to which VFS
|
||||
// requires Dentries to exist; Filesystems may elect to cache or discard
|
||||
// Dentries with zero references.
|
||||
type Dentry struct {
|
||||
// parent is this Dentry's parent in this Filesystem. If this Dentry is
|
||||
// independent, parent is nil.
|
||||
parent *Dentry
|
||||
|
||||
// name is this Dentry's name in parent.
|
||||
name string
|
||||
|
||||
flags uint32
|
||||
|
||||
// mounts is the number of Mounts for which this Dentry is Mount.point.
|
||||
// mounts is accessed using atomic memory operations.
|
||||
mounts uint32
|
||||
|
||||
// children are child Dentries.
|
||||
children map[string]*Dentry
|
||||
|
||||
// impl is the DentryImpl associated with this Dentry. impl is immutable.
|
||||
// This should be the last field in Dentry.
|
||||
impl DentryImpl
|
||||
}
|
||||
|
||||
const (
|
||||
// dflagsDisownedMask is set in Dentry.flags if the Dentry has been
|
||||
// disowned.
|
||||
dflagsDisownedMask = 1 << iota
|
||||
)
|
||||
|
||||
// Init must be called before first use of d.
|
||||
func (d *Dentry) Init(impl DentryImpl) {
|
||||
d.impl = impl
|
||||
}
|
||||
|
||||
// Impl returns the DentryImpl associated with d.
|
||||
func (d *Dentry) Impl() DentryImpl {
|
||||
return d.impl
|
||||
}
|
||||
|
||||
// DentryImpl contains implementation details for a Dentry. Implementations of
|
||||
// DentryImpl should contain their associated Dentry by value as their first
|
||||
// field.
|
||||
type DentryImpl interface {
|
||||
// IncRef increments the Dentry's reference count. A Dentry with a non-zero
|
||||
// reference count must remain coherent with the state of the filesystem.
|
||||
IncRef(fs *Filesystem)
|
||||
|
||||
// TryIncRef increments the Dentry's reference count and returns true. If
|
||||
// the Dentry's reference count is zero, TryIncRef may do nothing and
|
||||
// return false. (It is also permitted to succeed if it can restore the
|
||||
// guarantee that the Dentry is coherent with the state of the filesystem.)
|
||||
//
|
||||
// TryIncRef does not require that a reference is held on the Dentry.
|
||||
TryIncRef(fs *Filesystem) bool
|
||||
|
||||
// DecRef decrements the Dentry's reference count.
|
||||
DecRef(fs *Filesystem)
|
||||
}
|
||||
|
||||
// IsDisowned returns true if d is disowned.
|
||||
func (d *Dentry) IsDisowned() bool {
|
||||
return atomic.LoadUint32(&d.flags)&dflagsDisownedMask != 0
|
||||
}
|
||||
|
||||
// Preconditions: !d.IsDisowned().
|
||||
func (d *Dentry) setDisowned() {
|
||||
atomic.AddUint32(&d.flags, dflagsDisownedMask)
|
||||
}
|
||||
|
||||
func (d *Dentry) isMounted() bool {
|
||||
return atomic.LoadUint32(&d.mounts) != 0
|
||||
}
|
||||
|
||||
func (d *Dentry) incRef(fs *Filesystem) {
|
||||
d.impl.IncRef(fs)
|
||||
}
|
||||
|
||||
func (d *Dentry) tryIncRef(fs *Filesystem) bool {
|
||||
return d.impl.TryIncRef(fs)
|
||||
}
|
||||
|
||||
func (d *Dentry) decRef(fs *Filesystem) {
|
||||
d.impl.DecRef(fs)
|
||||
}
|
||||
|
||||
// These functions are exported so that filesystem implementations can use
|
||||
// them. The vfs package, and users of VFS, should not call these functions.
|
||||
// Unless otherwise specified, these methods require that there are no
|
||||
// concurrent mutators of d.
|
||||
|
||||
// Name returns d's name in its parent in its owning Filesystem. If d is
|
||||
// independent, Name returns an empty string.
|
||||
func (d *Dentry) Name() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
// Parent returns d's parent in its owning Filesystem. It does not take a
|
||||
// reference on the returned Dentry. If d is independent, Parent returns nil.
|
||||
func (d *Dentry) Parent() *Dentry {
|
||||
return d.parent
|
||||
}
|
||||
|
||||
// ParentOrSelf is equivalent to Parent, but returns d if d is independent.
|
||||
func (d *Dentry) ParentOrSelf() *Dentry {
|
||||
if d.parent == nil {
|
||||
return d
|
||||
}
|
||||
return d.parent
|
||||
}
|
||||
|
||||
// Child returns d's child with the given name in its owning Filesystem. It
|
||||
// does not take a reference on the returned Dentry. If no such child exists,
|
||||
// Child returns nil.
|
||||
func (d *Dentry) Child(name string) *Dentry {
|
||||
return d.children[name]
|
||||
}
|
||||
|
||||
// HasChildren returns true if d has any children.
|
||||
func (d *Dentry) HasChildren() bool {
|
||||
return len(d.children) != 0
|
||||
}
|
||||
|
||||
// InsertChild makes child a child of d with the given name.
|
||||
//
|
||||
// InsertChild is a mutator of d and child.
|
||||
//
|
||||
// Preconditions: child must be an independent Dentry. d and child must be from
|
||||
// the same Filesystem. d must not already have a child with the given name.
|
||||
func (d *Dentry) InsertChild(child *Dentry, name string) {
|
||||
if checkInvariants {
|
||||
if _, ok := d.children[name]; ok {
|
||||
panic(fmt.Sprintf("parent already contains a child named %q", name))
|
||||
}
|
||||
if child.parent != nil || child.name != "" {
|
||||
panic(fmt.Sprintf("child is not independent: parent = %v, name = %q", child.parent, child.name))
|
||||
}
|
||||
}
|
||||
if d.children == nil {
|
||||
d.children = make(map[string]*Dentry)
|
||||
}
|
||||
d.children[name] = child
|
||||
child.parent = d
|
||||
child.name = name
|
||||
}
|
||||
|
||||
// PrepareDeleteDentry must be called before attempting to delete the file
|
||||
// represented by d. If PrepareDeleteDentry succeeds, the caller must call
|
||||
// AbortDeleteDentry or CommitDeleteDentry depending on the deletion's outcome.
|
||||
//
|
||||
// Preconditions: d is a child Dentry.
|
||||
func (vfs *VirtualFilesystem) PrepareDeleteDentry(mntns *MountNamespace, d *Dentry) error {
|
||||
if checkInvariants {
|
||||
if d.parent == nil {
|
||||
panic("d is independent")
|
||||
}
|
||||
if d.IsDisowned() {
|
||||
panic("d is already disowned")
|
||||
}
|
||||
}
|
||||
vfs.mountMu.RLock()
|
||||
if _, ok := mntns.mountpoints[d]; ok {
|
||||
vfs.mountMu.RUnlock()
|
||||
return syserror.EBUSY
|
||||
}
|
||||
// Return with vfs.mountMu locked, which will be unlocked by
|
||||
// AbortDeleteDentry or CommitDeleteDentry.
|
||||
return nil
|
||||
}
|
||||
|
||||
// AbortDeleteDentry must be called after PrepareDeleteDentry if the deletion
|
||||
// fails.
|
||||
func (vfs *VirtualFilesystem) AbortDeleteDentry() {
|
||||
vfs.mountMu.RUnlock()
|
||||
}
|
||||
|
||||
// CommitDeleteDentry must be called after the file represented by d is
|
||||
// deleted, and causes d to become disowned.
|
||||
//
|
||||
// Preconditions: PrepareDeleteDentry was previously called on d.
|
||||
func (vfs *VirtualFilesystem) CommitDeleteDentry(d *Dentry) {
|
||||
delete(d.parent.children, d.name)
|
||||
d.setDisowned()
|
||||
// TODO: lazily unmount mounts at d
|
||||
vfs.mountMu.RUnlock()
|
||||
}
|
||||
|
||||
// DeleteDentry combines PrepareDeleteDentry and CommitDeleteDentry, as
|
||||
// appropriate for in-memory filesystems that don't need to ensure that some
|
||||
// external state change succeeds before committing the deletion.
|
||||
func (vfs *VirtualFilesystem) DeleteDentry(mntns *MountNamespace, d *Dentry) error {
|
||||
if err := vfs.PrepareDeleteDentry(mntns, d); err != nil {
|
||||
return err
|
||||
}
|
||||
vfs.CommitDeleteDentry(d)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrepareRenameDentry must be called before attempting to rename the file
|
||||
// represented by from. If to is not nil, it represents the file that will be
|
||||
// replaced or exchanged by the rename. If PrepareRenameDentry succeeds, the
|
||||
// caller must call AbortRenameDentry, CommitRenameReplaceDentry, or
|
||||
// CommitRenameExchangeDentry depending on the rename's outcome.
|
||||
//
|
||||
// Preconditions: from is a child Dentry. If to is not nil, it must be a child
|
||||
// Dentry from the same Filesystem.
|
||||
func (vfs *VirtualFilesystem) PrepareRenameDentry(mntns *MountNamespace, from, to *Dentry) error {
|
||||
if checkInvariants {
|
||||
if from.parent == nil {
|
||||
panic("from is independent")
|
||||
}
|
||||
if from.IsDisowned() {
|
||||
panic("from is already disowned")
|
||||
}
|
||||
if to != nil {
|
||||
if to.parent == nil {
|
||||
panic("to is independent")
|
||||
}
|
||||
if to.IsDisowned() {
|
||||
panic("to is already disowned")
|
||||
}
|
||||
}
|
||||
}
|
||||
vfs.mountMu.RLock()
|
||||
if _, ok := mntns.mountpoints[from]; ok {
|
||||
vfs.mountMu.RUnlock()
|
||||
return syserror.EBUSY
|
||||
}
|
||||
if to != nil {
|
||||
if _, ok := mntns.mountpoints[to]; ok {
|
||||
vfs.mountMu.RUnlock()
|
||||
return syserror.EBUSY
|
||||
}
|
||||
}
|
||||
// Return with vfs.mountMu locked, which will be unlocked by
|
||||
// AbortRenameDentry, CommitRenameReplaceDentry, or
|
||||
// CommitRenameExchangeDentry.
|
||||
return nil
|
||||
}
|
||||
|
||||
// AbortRenameDentry must be called after PrepareRenameDentry if the rename
|
||||
// fails.
|
||||
func (vfs *VirtualFilesystem) AbortRenameDentry() {
|
||||
vfs.mountMu.RUnlock()
|
||||
}
|
||||
|
||||
// CommitRenameReplaceDentry must be called after the file represented by from
|
||||
// is renamed without RENAME_EXCHANGE. If to is not nil, it represents the file
|
||||
// that was replaced by from.
|
||||
//
|
||||
// Preconditions: PrepareRenameDentry was previously called on from and to.
|
||||
// newParent.Child(newName) == to.
|
||||
func (vfs *VirtualFilesystem) CommitRenameReplaceDentry(from, newParent *Dentry, newName string, to *Dentry) {
|
||||
if to != nil {
|
||||
to.setDisowned()
|
||||
// TODO: lazily unmount mounts at d
|
||||
}
|
||||
if newParent.children == nil {
|
||||
newParent.children = make(map[string]*Dentry)
|
||||
}
|
||||
newParent.children[newName] = from
|
||||
from.parent = newParent
|
||||
from.name = newName
|
||||
vfs.mountMu.RUnlock()
|
||||
}
|
||||
|
||||
// CommitRenameExchangeDentry must be called after the files represented by
|
||||
// from and to are exchanged by rename(RENAME_EXCHANGE).
|
||||
//
|
||||
// Preconditions: PrepareRenameDentry was previously called on from and to.
|
||||
func (vfs *VirtualFilesystem) CommitRenameExchangeDentry(from, to *Dentry) {
|
||||
from.parent, to.parent = to.parent, from.parent
|
||||
from.name, to.name = to.name, from.name
|
||||
from.parent.children[from.name] = from
|
||||
to.parent.children[to.name] = to
|
||||
vfs.mountMu.RUnlock()
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user