2019-07-23 22:50:35 +00:00
|
|
|
// Copyright 2019 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package ext
|
|
|
|
|
|
|
|
import (
|
2019-08-07 21:22:19 +00:00
|
|
|
"fmt"
|
2019-07-23 22:50:35 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
2019-07-30 01:32:45 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
2019-08-09 20:07:06 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sentry/fsimpl/ext/disklayout"
|
2019-08-07 21:22:19 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
2019-07-23 22:50:35 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/syserror"
|
|
|
|
)
|
|
|
|
|
|
|
|
// inode represents an ext inode.
|
2019-07-30 01:32:45 +00:00
|
|
|
//
|
|
|
|
// inode uses the same inheritance pattern that pkg/sentry/vfs structures use.
|
|
|
|
// This has been done to increase memory locality.
|
|
|
|
//
|
|
|
|
// Implementations:
|
|
|
|
// inode --
|
|
|
|
// |-- dir
|
|
|
|
// |-- symlink
|
|
|
|
// |-- regular--
|
|
|
|
// |-- extent file
|
|
|
|
// |-- block map file
|
2019-07-23 22:50:35 +00:00
|
|
|
type inode struct {
|
|
|
|
// refs is a reference count. refs is accessed using atomic memory operations.
|
|
|
|
refs int64
|
|
|
|
|
Minor VFS2 interface changes.
- Remove the Filesystem argument from DentryImpl.*Ref(); in general DentryImpls
that need the Filesystem for reference counting will probably also need it
for other interface methods that don't plumb Filesystem, so it's easier to
just store a pointer to the filesystem in the DentryImpl.
- Add a pointer to the VirtualFilesystem to Filesystem, which is needed by the
gofer client to disown dentries for cache eviction triggered by dentry
reference count changes.
- Rename FilesystemType.NewFilesystem to GetFilesystem; in some cases (e.g.
sysfs, cgroupfs) it's much cleaner for there to be only one Filesystem that
is used by all mounts, and in at least one case (devtmpfs) it's visibly
incorrect not to do so, so NewFilesystem doesn't always actually create and
return a *new* Filesystem.
- Require callers of FileDescription.Init() to increment Mount/Dentry
references. This is because the gofer client may, in the OpenAt() path, take
a reference on a dentry with 0 references, which is safe due to
synchronization that is outside the scope of this CL, and it would be safer
to still have its implementation of DentryImpl.IncRef() check for an
increment for 0 references in other cases.
- Add FileDescription.TryIncRef. This is used by the gofer client to take
references on "special file descriptions" (FDs for files such as pipes,
sockets, and devices), which use per-FD handles (fids) instead of
dentry-shared handles, for sync() and syncfs().
PiperOrigin-RevId: 282473364
2019-11-26 02:09:15 +00:00
|
|
|
// fs is the containing filesystem.
|
|
|
|
fs *filesystem
|
|
|
|
|
2019-07-24 03:34:49 +00:00
|
|
|
// inodeNum is the inode number of this inode on disk. This is used to
|
|
|
|
// identify inodes within the ext filesystem.
|
|
|
|
inodeNum uint32
|
|
|
|
|
2019-07-31 02:42:50 +00:00
|
|
|
// blkSize is the fs data block size. Same as filesystem.sb.BlockSize().
|
|
|
|
blkSize uint64
|
|
|
|
|
2019-07-23 22:50:35 +00:00
|
|
|
// diskInode gives us access to the inode struct on disk. Immutable.
|
|
|
|
diskInode disklayout.Inode
|
|
|
|
|
2019-07-30 01:32:45 +00:00
|
|
|
// This is immutable. The first field of the implementations must have inode
|
|
|
|
// as the first field to ensure temporality.
|
|
|
|
impl interface{}
|
2019-07-23 22:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// incRef increments the inode ref count.
|
|
|
|
func (in *inode) incRef() {
|
|
|
|
atomic.AddInt64(&in.refs, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tryIncRef tries to increment the ref count. Returns true if successful.
|
|
|
|
func (in *inode) tryIncRef() bool {
|
|
|
|
for {
|
|
|
|
refs := atomic.LoadInt64(&in.refs)
|
|
|
|
if refs == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapInt64(&in.refs, refs, refs+1) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 03:34:49 +00:00
|
|
|
// decRef decrements the inode ref count and releases the inode resources if
|
|
|
|
// the ref count hits 0.
|
|
|
|
//
|
Minor VFS2 interface changes.
- Remove the Filesystem argument from DentryImpl.*Ref(); in general DentryImpls
that need the Filesystem for reference counting will probably also need it
for other interface methods that don't plumb Filesystem, so it's easier to
just store a pointer to the filesystem in the DentryImpl.
- Add a pointer to the VirtualFilesystem to Filesystem, which is needed by the
gofer client to disown dentries for cache eviction triggered by dentry
reference count changes.
- Rename FilesystemType.NewFilesystem to GetFilesystem; in some cases (e.g.
sysfs, cgroupfs) it's much cleaner for there to be only one Filesystem that
is used by all mounts, and in at least one case (devtmpfs) it's visibly
incorrect not to do so, so NewFilesystem doesn't always actually create and
return a *new* Filesystem.
- Require callers of FileDescription.Init() to increment Mount/Dentry
references. This is because the gofer client may, in the OpenAt() path, take
a reference on a dentry with 0 references, which is safe due to
synchronization that is outside the scope of this CL, and it would be safer
to still have its implementation of DentryImpl.IncRef() check for an
increment for 0 references in other cases.
- Add FileDescription.TryIncRef. This is used by the gofer client to take
references on "special file descriptions" (FDs for files such as pipes,
sockets, and devices), which use per-FD handles (fids) instead of
dentry-shared handles, for sync() and syncfs().
PiperOrigin-RevId: 282473364
2019-11-26 02:09:15 +00:00
|
|
|
// Precondition: Must have locked filesystem.mu.
|
|
|
|
func (in *inode) decRef() {
|
2019-07-24 03:34:49 +00:00
|
|
|
if refs := atomic.AddInt64(&in.refs, -1); refs == 0 {
|
Minor VFS2 interface changes.
- Remove the Filesystem argument from DentryImpl.*Ref(); in general DentryImpls
that need the Filesystem for reference counting will probably also need it
for other interface methods that don't plumb Filesystem, so it's easier to
just store a pointer to the filesystem in the DentryImpl.
- Add a pointer to the VirtualFilesystem to Filesystem, which is needed by the
gofer client to disown dentries for cache eviction triggered by dentry
reference count changes.
- Rename FilesystemType.NewFilesystem to GetFilesystem; in some cases (e.g.
sysfs, cgroupfs) it's much cleaner for there to be only one Filesystem that
is used by all mounts, and in at least one case (devtmpfs) it's visibly
incorrect not to do so, so NewFilesystem doesn't always actually create and
return a *new* Filesystem.
- Require callers of FileDescription.Init() to increment Mount/Dentry
references. This is because the gofer client may, in the OpenAt() path, take
a reference on a dentry with 0 references, which is safe due to
synchronization that is outside the scope of this CL, and it would be safer
to still have its implementation of DentryImpl.IncRef() check for an
increment for 0 references in other cases.
- Add FileDescription.TryIncRef. This is used by the gofer client to take
references on "special file descriptions" (FDs for files such as pipes,
sockets, and devices), which use per-FD handles (fids) instead of
dentry-shared handles, for sync() and syncfs().
PiperOrigin-RevId: 282473364
2019-11-26 02:09:15 +00:00
|
|
|
delete(in.fs.inodeCache, in.inodeNum)
|
2019-07-24 03:34:49 +00:00
|
|
|
} else if refs < 0 {
|
2019-07-23 22:50:35 +00:00
|
|
|
panic("ext.inode.decRef() called without holding a reference")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 03:34:49 +00:00
|
|
|
// newInode is the inode constructor. Reads the inode off disk. Identifies
|
|
|
|
// inodes based on the absolute inode number on disk.
|
2019-08-07 21:22:19 +00:00
|
|
|
func newInode(fs *filesystem, inodeNum uint32) (*inode, error) {
|
2019-07-24 03:34:49 +00:00
|
|
|
if inodeNum == 0 {
|
|
|
|
panic("inode number 0 on ext filesystems is not possible")
|
|
|
|
}
|
|
|
|
|
2019-07-31 02:42:50 +00:00
|
|
|
inodeRecordSize := fs.sb.InodeSize()
|
2019-07-30 01:32:45 +00:00
|
|
|
var diskInode disklayout.Inode
|
2019-07-24 03:34:49 +00:00
|
|
|
if inodeRecordSize == disklayout.OldInodeSize {
|
2019-07-30 01:32:45 +00:00
|
|
|
diskInode = &disklayout.InodeOld{}
|
2019-07-24 03:34:49 +00:00
|
|
|
} else {
|
2019-07-30 01:32:45 +00:00
|
|
|
diskInode = &disklayout.InodeNew{}
|
2019-07-24 03:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate where the inode is actually placed.
|
2019-07-31 02:42:50 +00:00
|
|
|
inodesPerGrp := fs.sb.InodesPerGroup()
|
|
|
|
blkSize := fs.sb.BlockSize()
|
|
|
|
inodeTableOff := fs.bgs[getBGNum(inodeNum, inodesPerGrp)].InodeTable() * blkSize
|
2019-07-24 03:34:49 +00:00
|
|
|
inodeOff := inodeTableOff + uint64(uint32(inodeRecordSize)*getBGOff(inodeNum, inodesPerGrp))
|
|
|
|
|
2019-07-31 02:42:50 +00:00
|
|
|
if err := readFromDisk(fs.dev, int64(inodeOff), diskInode); err != nil {
|
2019-07-24 03:34:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-30 01:32:45 +00:00
|
|
|
// Build the inode based on its type.
|
|
|
|
inode := inode{
|
Minor VFS2 interface changes.
- Remove the Filesystem argument from DentryImpl.*Ref(); in general DentryImpls
that need the Filesystem for reference counting will probably also need it
for other interface methods that don't plumb Filesystem, so it's easier to
just store a pointer to the filesystem in the DentryImpl.
- Add a pointer to the VirtualFilesystem to Filesystem, which is needed by the
gofer client to disown dentries for cache eviction triggered by dentry
reference count changes.
- Rename FilesystemType.NewFilesystem to GetFilesystem; in some cases (e.g.
sysfs, cgroupfs) it's much cleaner for there to be only one Filesystem that
is used by all mounts, and in at least one case (devtmpfs) it's visibly
incorrect not to do so, so NewFilesystem doesn't always actually create and
return a *new* Filesystem.
- Require callers of FileDescription.Init() to increment Mount/Dentry
references. This is because the gofer client may, in the OpenAt() path, take
a reference on a dentry with 0 references, which is safe due to
synchronization that is outside the scope of this CL, and it would be safer
to still have its implementation of DentryImpl.IncRef() check for an
increment for 0 references in other cases.
- Add FileDescription.TryIncRef. This is used by the gofer client to take
references on "special file descriptions" (FDs for files such as pipes,
sockets, and devices), which use per-FD handles (fids) instead of
dentry-shared handles, for sync() and syncfs().
PiperOrigin-RevId: 282473364
2019-11-26 02:09:15 +00:00
|
|
|
fs: fs,
|
2019-07-30 01:32:45 +00:00
|
|
|
inodeNum: inodeNum,
|
2019-07-31 02:42:50 +00:00
|
|
|
blkSize: blkSize,
|
2019-07-30 01:32:45 +00:00
|
|
|
diskInode: diskInode,
|
2019-07-24 03:34:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 01:32:45 +00:00
|
|
|
switch diskInode.Mode().FileType() {
|
|
|
|
case linux.ModeSymlink:
|
2019-07-31 02:42:50 +00:00
|
|
|
f, err := newSymlink(inode)
|
2019-07-30 01:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &f.inode, nil
|
|
|
|
case linux.ModeRegular:
|
2019-07-31 02:42:50 +00:00
|
|
|
f, err := newRegularFile(inode)
|
2019-07-30 01:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &f.inode, nil
|
|
|
|
case linux.ModeDirectory:
|
2019-08-07 21:22:19 +00:00
|
|
|
f, err := newDirectroy(inode, fs.sb.IncompatibleFeatures().DirentFileType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &f.inode, nil
|
2019-07-30 01:32:45 +00:00
|
|
|
default:
|
2019-08-07 21:22:19 +00:00
|
|
|
// TODO(b/134676337): Return appropriate errors for sockets, pipes and devices.
|
2019-07-30 01:32:45 +00:00
|
|
|
return nil, syserror.EINVAL
|
|
|
|
}
|
2019-07-24 03:34:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 21:22:19 +00:00
|
|
|
// open creates and returns a file description for the dentry passed in.
|
|
|
|
func (in *inode) open(rp *vfs.ResolvingPath, vfsd *vfs.Dentry, flags uint32) (*vfs.FileDescription, error) {
|
|
|
|
ats := vfs.AccessTypesForOpenFlags(flags)
|
|
|
|
if err := in.checkPermissions(rp.Credentials(), ats); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Minor VFS2 interface changes.
- Remove the Filesystem argument from DentryImpl.*Ref(); in general DentryImpls
that need the Filesystem for reference counting will probably also need it
for other interface methods that don't plumb Filesystem, so it's easier to
just store a pointer to the filesystem in the DentryImpl.
- Add a pointer to the VirtualFilesystem to Filesystem, which is needed by the
gofer client to disown dentries for cache eviction triggered by dentry
reference count changes.
- Rename FilesystemType.NewFilesystem to GetFilesystem; in some cases (e.g.
sysfs, cgroupfs) it's much cleaner for there to be only one Filesystem that
is used by all mounts, and in at least one case (devtmpfs) it's visibly
incorrect not to do so, so NewFilesystem doesn't always actually create and
return a *new* Filesystem.
- Require callers of FileDescription.Init() to increment Mount/Dentry
references. This is because the gofer client may, in the OpenAt() path, take
a reference on a dentry with 0 references, which is safe due to
synchronization that is outside the scope of this CL, and it would be safer
to still have its implementation of DentryImpl.IncRef() check for an
increment for 0 references in other cases.
- Add FileDescription.TryIncRef. This is used by the gofer client to take
references on "special file descriptions" (FDs for files such as pipes,
sockets, and devices), which use per-FD handles (fids) instead of
dentry-shared handles, for sync() and syncfs().
PiperOrigin-RevId: 282473364
2019-11-26 02:09:15 +00:00
|
|
|
mnt := rp.Mount()
|
2019-08-07 21:22:19 +00:00
|
|
|
switch in.impl.(type) {
|
|
|
|
case *regularFile:
|
|
|
|
var fd regularFileFD
|
2020-01-22 20:27:16 +00:00
|
|
|
if err := fd.vfsfd.Init(&fd, flags, mnt, vfsd, &vfs.FileDescriptionOptions{}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-07 21:22:19 +00:00
|
|
|
return &fd.vfsfd, nil
|
|
|
|
case *directory:
|
|
|
|
// Can't open directories writably. This check is not necessary for a read
|
|
|
|
// only filesystem but will be required when write is implemented.
|
|
|
|
if ats&vfs.MayWrite != 0 {
|
|
|
|
return nil, syserror.EISDIR
|
|
|
|
}
|
|
|
|
var fd directoryFD
|
2020-01-22 20:27:16 +00:00
|
|
|
if err := fd.vfsfd.Init(&fd, flags, mnt, vfsd, &vfs.FileDescriptionOptions{}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-07 21:22:19 +00:00
|
|
|
return &fd.vfsfd, nil
|
|
|
|
case *symlink:
|
|
|
|
if flags&linux.O_PATH == 0 {
|
|
|
|
// Can't open symlinks without O_PATH.
|
|
|
|
return nil, syserror.ELOOP
|
|
|
|
}
|
|
|
|
var fd symlinkFD
|
2019-12-20 19:52:24 +00:00
|
|
|
fd.vfsfd.Init(&fd, flags, mnt, vfsd, &vfs.FileDescriptionOptions{})
|
2019-08-07 21:22:19 +00:00
|
|
|
return &fd.vfsfd, nil
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown inode type: %T", in.impl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (in *inode) checkPermissions(creds *auth.Credentials, ats vfs.AccessTypes) error {
|
|
|
|
return vfs.GenericCheckPermissions(creds, ats, in.isDir(), uint16(in.diskInode.Mode()), in.diskInode.UID(), in.diskInode.GID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// statTo writes the statx fields to the output parameter.
|
|
|
|
func (in *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 | linux.STATX_SIZE |
|
|
|
|
linux.STATX_ATIME | linux.STATX_CTIME | linux.STATX_MTIME
|
|
|
|
stat.Blksize = uint32(in.blkSize)
|
|
|
|
stat.Mode = uint16(in.diskInode.Mode())
|
|
|
|
stat.Nlink = uint32(in.diskInode.LinksCount())
|
|
|
|
stat.UID = uint32(in.diskInode.UID())
|
|
|
|
stat.GID = uint32(in.diskInode.GID())
|
|
|
|
stat.Ino = uint64(in.inodeNum)
|
|
|
|
stat.Size = in.diskInode.Size()
|
|
|
|
stat.Atime = in.diskInode.AccessTime().StatxTimestamp()
|
|
|
|
stat.Ctime = in.diskInode.ChangeTime().StatxTimestamp()
|
|
|
|
stat.Mtime = in.diskInode.ModificationTime().StatxTimestamp()
|
|
|
|
// TODO(b/134676337): Set stat.Blocks which is the number of 512 byte blocks
|
|
|
|
// (including metadata blocks) required to represent this file.
|
|
|
|
}
|
|
|
|
|
2019-07-24 03:34:49 +00:00
|
|
|
// getBGNum returns the block group number that a given inode belongs to.
|
|
|
|
func getBGNum(inodeNum uint32, inodesPerGrp uint32) uint32 {
|
|
|
|
return (inodeNum - 1) / inodesPerGrp
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBGOff returns the offset at which the given inode lives in the block
|
|
|
|
// group's inode table, i.e. the index of the inode in the inode table.
|
|
|
|
func getBGOff(inodeNum uint32, inodesPerGrp uint32) uint32 {
|
|
|
|
return (inodeNum - 1) % inodesPerGrp
|
|
|
|
}
|