2019-07-18 22:09:14 +00:00
|
|
|
// Copyright 2019 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2019-12-11 02:16:47 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
2020-01-27 23:17:58 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/context"
|
2019-07-18 22:09:14 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
|
|
|
"gvisor.dev/gvisor/pkg/syserror"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Mount is a replacement of a Dentry (Mount.key.point) from one Filesystem
|
|
|
|
// (Mount.key.parent.fs) with a Dentry (Mount.root) from another Filesystem
|
|
|
|
// (Mount.fs), which applies to path resolution in the context of a particular
|
|
|
|
// Mount (Mount.key.parent).
|
|
|
|
//
|
|
|
|
// Mounts are reference-counted. Unless otherwise specified, all Mount methods
|
|
|
|
// require that a reference is held.
|
|
|
|
//
|
|
|
|
// Mount and Filesystem are distinct types because it's possible for a single
|
|
|
|
// Filesystem to be mounted at multiple locations and/or in multiple mount
|
|
|
|
// namespaces.
|
|
|
|
//
|
|
|
|
// Mount is analogous to Linux's struct mount. (gVisor does not distinguish
|
|
|
|
// between struct mount and struct vfsmount.)
|
2020-02-14 21:39:51 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-07-18 22:09:14 +00:00
|
|
|
type Mount struct {
|
2019-11-25 23:20:25 +00:00
|
|
|
// vfs, fs, and root are immutable. References are held on fs and root.
|
|
|
|
//
|
|
|
|
// Invariant: root belongs to fs.
|
|
|
|
vfs *VirtualFilesystem
|
|
|
|
fs *Filesystem
|
|
|
|
root *Dentry
|
2019-07-18 22:09:14 +00:00
|
|
|
|
|
|
|
// key is protected by VirtualFilesystem.mountMu and
|
|
|
|
// VirtualFilesystem.mounts.seq, and may be nil. References are held on
|
|
|
|
// key.parent and key.point if they are not nil.
|
|
|
|
//
|
|
|
|
// Invariant: key.parent != nil iff key.point != nil. key.point belongs to
|
|
|
|
// key.parent.fs.
|
|
|
|
key mountKey
|
|
|
|
|
2019-11-25 23:20:25 +00:00
|
|
|
// ns is the namespace in which this Mount was mounted. ns is protected by
|
|
|
|
// VirtualFilesystem.mountMu.
|
|
|
|
ns *MountNamespace
|
|
|
|
|
|
|
|
// The lower 63 bits of refs are a reference count. The MSB of refs is set
|
|
|
|
// if the Mount has been eagerly umounted, as by umount(2) without the
|
|
|
|
// MNT_DETACH flag. refs is accessed using atomic memory operations.
|
|
|
|
refs int64
|
|
|
|
|
|
|
|
// children is the set of all Mounts for which Mount.key.parent is this
|
|
|
|
// Mount. children is protected by VirtualFilesystem.mountMu.
|
|
|
|
children map[*Mount]struct{}
|
|
|
|
|
|
|
|
// umounted is true if VFS.umountRecursiveLocked() has been called on this
|
|
|
|
// Mount. VirtualFilesystem does not hold a reference on Mounts for which
|
|
|
|
// umounted is true. umounted is protected by VirtualFilesystem.mountMu.
|
|
|
|
umounted bool
|
|
|
|
|
|
|
|
// The lower 63 bits of writers is the number of calls to
|
|
|
|
// Mount.CheckBeginWrite() that have not yet been paired with a call to
|
|
|
|
// Mount.EndWrite(). The MSB of writers is set if MS_RDONLY is in effect.
|
|
|
|
// writers is accessed using atomic memory operations.
|
|
|
|
writers int64
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A MountNamespace is a collection of Mounts.
|
|
|
|
//
|
|
|
|
// MountNamespaces are reference-counted. Unless otherwise specified, all
|
|
|
|
// MountNamespace methods require that a reference is held.
|
|
|
|
//
|
|
|
|
// MountNamespace is analogous to Linux's struct mnt_namespace.
|
2020-02-14 21:39:51 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2019-07-18 22:09:14 +00:00
|
|
|
type MountNamespace struct {
|
|
|
|
// root is the MountNamespace's root mount. root is immutable.
|
|
|
|
root *Mount
|
|
|
|
|
2019-11-25 23:20:25 +00:00
|
|
|
// refs is the reference count. refs is accessed using atomic memory
|
|
|
|
// operations.
|
|
|
|
refs int64
|
|
|
|
|
|
|
|
// mountpoints maps all Dentries which are mount points in this namespace
|
|
|
|
// to the number of Mounts for which they are mount points. mountpoints is
|
|
|
|
// protected by VirtualFilesystem.mountMu.
|
2019-07-18 22:09:14 +00:00
|
|
|
//
|
|
|
|
// mountpoints is used to determine if a Dentry can be moved or removed
|
|
|
|
// (which requires that the Dentry is not a mount point in the calling
|
|
|
|
// namespace).
|
|
|
|
//
|
|
|
|
// mountpoints is maintained even if there are no references held on the
|
|
|
|
// MountNamespace; this is required to ensure that
|
|
|
|
// VFS.PrepareDeleteDentry() and VFS.PrepareRemoveDentry() operate
|
|
|
|
// correctly on unreferenced MountNamespaces.
|
2019-11-25 23:20:25 +00:00
|
|
|
mountpoints map[*Dentry]uint32
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMountNamespace returns a new mount namespace with a root filesystem
|
|
|
|
// configured by the given arguments. A reference is taken on the returned
|
|
|
|
// MountNamespace.
|
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
|
|
|
func (vfs *VirtualFilesystem) NewMountNamespace(ctx context.Context, creds *auth.Credentials, source, fsTypeName string, opts *GetFilesystemOptions) (*MountNamespace, error) {
|
2019-12-27 08:12:14 +00:00
|
|
|
rft := vfs.getFilesystemType(fsTypeName)
|
|
|
|
if rft == nil {
|
2020-02-14 19:11:55 +00:00
|
|
|
ctx.Warningf("Unknown filesystem: %s", fsTypeName)
|
2019-07-18 22:09:14 +00:00
|
|
|
return nil, syserror.ENODEV
|
|
|
|
}
|
2019-12-27 08:12:14 +00:00
|
|
|
fs, root, err := rft.fsType.GetFilesystem(ctx, vfs, creds, source, *opts)
|
2019-07-18 22:09:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mntns := &MountNamespace{
|
|
|
|
refs: 1,
|
2019-11-25 23:20:25 +00:00
|
|
|
mountpoints: make(map[*Dentry]uint32),
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
mntns.root = &Mount{
|
2019-11-25 23:20:25 +00:00
|
|
|
vfs: vfs,
|
2019-07-18 22:09:14 +00:00
|
|
|
fs: fs,
|
|
|
|
root: root,
|
|
|
|
ns: mntns,
|
|
|
|
refs: 1,
|
|
|
|
}
|
|
|
|
return mntns, nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 03:12:22 +00:00
|
|
|
// NewDisconnectedMount returns a Mount representing fs with the given root
|
|
|
|
// (which may be nil). The new Mount is not associated with any MountNamespace
|
|
|
|
// and is not connected to any other Mounts. References are taken on fs and
|
|
|
|
// root.
|
|
|
|
func (vfs *VirtualFilesystem) NewDisconnectedMount(fs *Filesystem, root *Dentry, opts *MountOptions) (*Mount, error) {
|
|
|
|
fs.IncRef()
|
|
|
|
if root != nil {
|
|
|
|
root.IncRef()
|
|
|
|
}
|
|
|
|
return &Mount{
|
|
|
|
vfs: vfs,
|
|
|
|
fs: fs,
|
|
|
|
root: root,
|
|
|
|
refs: 1,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 02:16:47 +00:00
|
|
|
// MountAt creates and mounts a Filesystem configured by the given arguments.
|
|
|
|
func (vfs *VirtualFilesystem) MountAt(ctx context.Context, creds *auth.Credentials, source string, target *PathOperation, fsTypeName string, opts *MountOptions) error {
|
2019-12-27 08:12:14 +00:00
|
|
|
rft := vfs.getFilesystemType(fsTypeName)
|
|
|
|
if rft == nil {
|
2019-07-18 22:09:14 +00:00
|
|
|
return syserror.ENODEV
|
|
|
|
}
|
2019-12-27 08:12:14 +00:00
|
|
|
if !opts.InternalMount && !rft.opts.AllowUserMount {
|
|
|
|
return syserror.ENODEV
|
|
|
|
}
|
|
|
|
fs, root, err := rft.fsType.GetFilesystem(ctx, vfs, creds, source, opts.GetFilesystemOptions)
|
2019-07-18 22:09:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We can't hold vfs.mountMu while calling FilesystemImpl methods due to
|
|
|
|
// lock ordering.
|
|
|
|
vd, err := vfs.GetDentryAt(ctx, creds, target, &GetDentryOptions{})
|
|
|
|
if err != nil {
|
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
|
|
|
root.DecRef()
|
|
|
|
fs.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
vfs.mountMu.Lock()
|
2019-11-25 23:20:25 +00:00
|
|
|
vd.dentry.mu.Lock()
|
2019-07-18 22:09:14 +00:00
|
|
|
for {
|
|
|
|
if vd.dentry.IsDisowned() {
|
2019-11-25 23:20:25 +00:00
|
|
|
vd.dentry.mu.Unlock()
|
2019-07-18 22:09:14 +00:00
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
vd.DecRef()
|
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
|
|
|
root.DecRef()
|
|
|
|
fs.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
return syserror.ENOENT
|
|
|
|
}
|
|
|
|
// vd might have been mounted over between vfs.GetDentryAt() and
|
|
|
|
// vfs.mountMu.Lock().
|
|
|
|
if !vd.dentry.isMounted() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
nextmnt := vfs.mounts.Lookup(vd.mount, vd.dentry)
|
|
|
|
if nextmnt == nil {
|
|
|
|
break
|
|
|
|
}
|
2019-11-25 23:20:25 +00:00
|
|
|
// It's possible that nextmnt has been umounted but not disconnected,
|
|
|
|
// in which case vfs no longer holds a reference on it, and the last
|
|
|
|
// reference may be concurrently dropped even though we're holding
|
|
|
|
// vfs.mountMu.
|
|
|
|
if !nextmnt.tryIncMountedRef() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// This can't fail since we're holding vfs.mountMu.
|
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
|
|
|
nextmnt.root.IncRef()
|
2019-11-25 23:20:25 +00:00
|
|
|
vd.dentry.mu.Unlock()
|
2019-07-18 22:09:14 +00:00
|
|
|
vd.DecRef()
|
|
|
|
vd = VirtualDentry{
|
|
|
|
mount: nextmnt,
|
|
|
|
dentry: nextmnt.root,
|
|
|
|
}
|
2019-11-25 23:20:25 +00:00
|
|
|
vd.dentry.mu.Lock()
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
// TODO: Linux requires that either both the mount point and the mount root
|
|
|
|
// are directories, or neither are, and returns ENOTDIR if this is not the
|
|
|
|
// case.
|
|
|
|
mntns := vd.mount.ns
|
|
|
|
mnt := &Mount{
|
2019-11-25 23:20:25 +00:00
|
|
|
vfs: vfs,
|
2019-07-18 22:09:14 +00:00
|
|
|
fs: fs,
|
|
|
|
root: root,
|
|
|
|
ns: mntns,
|
|
|
|
refs: 1,
|
|
|
|
}
|
2019-11-25 23:20:25 +00:00
|
|
|
vfs.mounts.seq.BeginWrite()
|
|
|
|
vfs.connectLocked(mnt, vd, mntns)
|
|
|
|
vfs.mounts.seq.EndWrite()
|
|
|
|
vd.dentry.mu.Unlock()
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 02:16:47 +00:00
|
|
|
// UmountAt removes the Mount at the given path.
|
|
|
|
func (vfs *VirtualFilesystem) UmountAt(ctx context.Context, creds *auth.Credentials, pop *PathOperation, opts *UmountOptions) error {
|
|
|
|
if opts.Flags&^(linux.MNT_FORCE|linux.MNT_DETACH) != 0 {
|
|
|
|
return syserror.EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// MNT_FORCE is currently unimplemented except for the permission check.
|
|
|
|
if opts.Flags&linux.MNT_FORCE != 0 && creds.HasCapabilityIn(linux.CAP_SYS_ADMIN, creds.UserNamespace.Root()) {
|
|
|
|
return syserror.EPERM
|
|
|
|
}
|
|
|
|
|
|
|
|
vd, err := vfs.GetDentryAt(ctx, creds, pop, &GetDentryOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer vd.DecRef()
|
|
|
|
if vd.dentry != vd.mount.root {
|
|
|
|
return syserror.EINVAL
|
|
|
|
}
|
|
|
|
vfs.mountMu.Lock()
|
2020-02-14 19:11:55 +00:00
|
|
|
if mntns := MountNamespaceFromContext(ctx); mntns != nil {
|
|
|
|
defer mntns.DecRef()
|
|
|
|
if mntns != vd.mount.ns {
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
return syserror.EINVAL
|
|
|
|
}
|
2019-12-11 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(jamieliu): Linux special-cases umount of the caller's root, which
|
|
|
|
// we don't implement yet (we'll just fail it since the caller holds a
|
|
|
|
// reference on it).
|
|
|
|
|
|
|
|
vfs.mounts.seq.BeginWrite()
|
|
|
|
if opts.Flags&linux.MNT_DETACH == 0 {
|
|
|
|
if len(vd.mount.children) != 0 {
|
|
|
|
vfs.mounts.seq.EndWrite()
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
return syserror.EBUSY
|
|
|
|
}
|
|
|
|
// We are holding a reference on vd.mount.
|
|
|
|
expectedRefs := int64(1)
|
|
|
|
if !vd.mount.umounted {
|
|
|
|
expectedRefs = 2
|
|
|
|
}
|
|
|
|
if atomic.LoadInt64(&vd.mount.refs)&^math.MinInt64 != expectedRefs { // mask out MSB
|
|
|
|
vfs.mounts.seq.EndWrite()
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
return syserror.EBUSY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vdsToDecRef, mountsToDecRef := vfs.umountRecursiveLocked(vd.mount, &umountRecursiveOptions{
|
|
|
|
eager: opts.Flags&linux.MNT_DETACH == 0,
|
|
|
|
disconnectHierarchy: true,
|
|
|
|
}, nil, nil)
|
|
|
|
vfs.mounts.seq.EndWrite()
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
for _, vd := range vdsToDecRef {
|
|
|
|
vd.DecRef()
|
|
|
|
}
|
|
|
|
for _, mnt := range mountsToDecRef {
|
|
|
|
mnt.DecRef()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 23:20:25 +00:00
|
|
|
type umountRecursiveOptions struct {
|
|
|
|
// If eager is true, ensure that future calls to Mount.tryIncMountedRef()
|
|
|
|
// on umounted mounts fail.
|
|
|
|
//
|
|
|
|
// eager is analogous to Linux's UMOUNT_SYNC.
|
|
|
|
eager bool
|
|
|
|
|
|
|
|
// If disconnectHierarchy is true, Mounts that are umounted hierarchically
|
|
|
|
// should be disconnected from their parents. (Mounts whose parents are not
|
|
|
|
// umounted, which in most cases means the Mount passed to the initial call
|
|
|
|
// to umountRecursiveLocked, are unconditionally disconnected for
|
|
|
|
// consistency with Linux.)
|
|
|
|
//
|
|
|
|
// disconnectHierarchy is analogous to Linux's !UMOUNT_CONNECTED.
|
|
|
|
disconnectHierarchy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// umountRecursiveLocked marks mnt and its descendants as umounted. It does not
|
|
|
|
// release mount or dentry references; instead, it appends VirtualDentries and
|
|
|
|
// Mounts on which references must be dropped to vdsToDecRef and mountsToDecRef
|
|
|
|
// respectively, and returns updated slices. (This is necessary because
|
|
|
|
// filesystem locks possibly taken by DentryImpl.DecRef() may precede
|
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
|
|
|
// vfs.mountMu in the lock order, and Mount.DecRef() may lock vfs.mountMu.)
|
2019-11-25 23:20:25 +00:00
|
|
|
//
|
|
|
|
// umountRecursiveLocked is analogous to Linux's fs/namespace.c:umount_tree().
|
|
|
|
//
|
|
|
|
// Preconditions: vfs.mountMu must be locked. vfs.mounts.seq must be in a
|
|
|
|
// writer critical section.
|
|
|
|
func (vfs *VirtualFilesystem) umountRecursiveLocked(mnt *Mount, opts *umountRecursiveOptions, vdsToDecRef []VirtualDentry, mountsToDecRef []*Mount) ([]VirtualDentry, []*Mount) {
|
|
|
|
if !mnt.umounted {
|
|
|
|
mnt.umounted = true
|
|
|
|
mountsToDecRef = append(mountsToDecRef, mnt)
|
|
|
|
if parent := mnt.parent(); parent != nil && (opts.disconnectHierarchy || !parent.umounted) {
|
|
|
|
vdsToDecRef = append(vdsToDecRef, vfs.disconnectLocked(mnt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.eager {
|
|
|
|
for {
|
|
|
|
refs := atomic.LoadInt64(&mnt.refs)
|
|
|
|
if refs < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapInt64(&mnt.refs, refs, refs|math.MinInt64) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for child := range mnt.children {
|
|
|
|
vdsToDecRef, mountsToDecRef = vfs.umountRecursiveLocked(child, opts, vdsToDecRef, mountsToDecRef)
|
|
|
|
}
|
|
|
|
return vdsToDecRef, mountsToDecRef
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectLocked makes vd the mount parent/point for mnt. It consumes
|
|
|
|
// references held by vd.
|
|
|
|
//
|
|
|
|
// Preconditions: vfs.mountMu must be locked. vfs.mounts.seq must be in a
|
|
|
|
// writer critical section. d.mu must be locked. mnt.parent() == nil.
|
|
|
|
func (vfs *VirtualFilesystem) connectLocked(mnt *Mount, vd VirtualDentry, mntns *MountNamespace) {
|
|
|
|
mnt.storeKey(vd)
|
|
|
|
if vd.mount.children == nil {
|
|
|
|
vd.mount.children = make(map[*Mount]struct{})
|
|
|
|
}
|
|
|
|
vd.mount.children[mnt] = struct{}{}
|
2019-07-18 22:09:14 +00:00
|
|
|
atomic.AddUint32(&vd.dentry.mounts, 1)
|
2019-11-25 23:20:25 +00:00
|
|
|
mntns.mountpoints[vd.dentry]++
|
|
|
|
vfs.mounts.insertSeqed(mnt)
|
2019-07-18 22:09:14 +00:00
|
|
|
vfsmpmounts, ok := vfs.mountpoints[vd.dentry]
|
|
|
|
if !ok {
|
|
|
|
vfsmpmounts = make(map[*Mount]struct{})
|
|
|
|
vfs.mountpoints[vd.dentry] = vfsmpmounts
|
|
|
|
}
|
|
|
|
vfsmpmounts[mnt] = struct{}{}
|
2019-11-25 23:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// disconnectLocked makes vd have no mount parent/point and returns its old
|
|
|
|
// mount parent/point with a reference held.
|
|
|
|
//
|
|
|
|
// Preconditions: vfs.mountMu must be locked. vfs.mounts.seq must be in a
|
|
|
|
// writer critical section. mnt.parent() != nil.
|
|
|
|
func (vfs *VirtualFilesystem) disconnectLocked(mnt *Mount) VirtualDentry {
|
|
|
|
vd := mnt.loadKey()
|
|
|
|
mnt.storeKey(VirtualDentry{})
|
|
|
|
delete(vd.mount.children, mnt)
|
|
|
|
atomic.AddUint32(&vd.dentry.mounts, math.MaxUint32) // -1
|
|
|
|
mnt.ns.mountpoints[vd.dentry]--
|
|
|
|
if mnt.ns.mountpoints[vd.dentry] == 0 {
|
|
|
|
delete(mnt.ns.mountpoints, vd.dentry)
|
|
|
|
}
|
|
|
|
vfs.mounts.removeSeqed(mnt)
|
|
|
|
vfsmpmounts := vfs.mountpoints[vd.dentry]
|
|
|
|
delete(vfsmpmounts, mnt)
|
|
|
|
if len(vfsmpmounts) == 0 {
|
|
|
|
delete(vfs.mountpoints, vd.dentry)
|
|
|
|
}
|
|
|
|
return vd
|
|
|
|
}
|
|
|
|
|
|
|
|
// tryIncMountedRef increments mnt's reference count and returns true. If mnt's
|
|
|
|
// reference count is already zero, or has been eagerly umounted,
|
|
|
|
// tryIncMountedRef does nothing and returns false.
|
|
|
|
//
|
|
|
|
// tryIncMountedRef does not require that a reference is held on mnt.
|
|
|
|
func (mnt *Mount) tryIncMountedRef() bool {
|
|
|
|
for {
|
|
|
|
refs := atomic.LoadInt64(&mnt.refs)
|
|
|
|
if refs <= 0 { // refs < 0 => MSB set => eagerly unmounted
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapInt64(&mnt.refs, refs, refs+1) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// IncRef increments mnt's reference count.
|
|
|
|
func (mnt *Mount) IncRef() {
|
2019-11-25 23:20:25 +00:00
|
|
|
// In general, negative values for mnt.refs are valid because the MSB is
|
|
|
|
// the eager-unmount bit.
|
|
|
|
atomic.AddInt64(&mnt.refs, 1)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// DecRef decrements mnt's reference count.
|
|
|
|
func (mnt *Mount) DecRef() {
|
2019-11-25 23:20:25 +00:00
|
|
|
refs := atomic.AddInt64(&mnt.refs, -1)
|
|
|
|
if refs&^math.MinInt64 == 0 { // mask out MSB
|
|
|
|
var vd VirtualDentry
|
|
|
|
if mnt.parent() != nil {
|
|
|
|
mnt.vfs.mountMu.Lock()
|
|
|
|
mnt.vfs.mounts.seq.BeginWrite()
|
|
|
|
vd = mnt.vfs.disconnectLocked(mnt)
|
|
|
|
mnt.vfs.mounts.seq.EndWrite()
|
|
|
|
mnt.vfs.mountMu.Unlock()
|
|
|
|
}
|
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.root.DecRef()
|
|
|
|
mnt.fs.DecRef()
|
2019-11-25 23:20:25 +00:00
|
|
|
if vd.Ok() {
|
|
|
|
vd.DecRef()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncRef increments mntns' reference count.
|
|
|
|
func (mntns *MountNamespace) IncRef() {
|
|
|
|
if atomic.AddInt64(&mntns.refs, 1) <= 1 {
|
|
|
|
panic("MountNamespace.IncRef() called without holding a reference")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecRef decrements mntns' reference count.
|
2020-02-04 19:47:41 +00:00
|
|
|
func (mntns *MountNamespace) DecRef() {
|
|
|
|
vfs := mntns.root.fs.VirtualFilesystem()
|
2019-11-25 23:20:25 +00:00
|
|
|
if refs := atomic.AddInt64(&mntns.refs, -1); refs == 0 {
|
|
|
|
vfs.mountMu.Lock()
|
|
|
|
vfs.mounts.seq.BeginWrite()
|
|
|
|
vdsToDecRef, mountsToDecRef := vfs.umountRecursiveLocked(mntns.root, &umountRecursiveOptions{
|
|
|
|
disconnectHierarchy: true,
|
|
|
|
}, nil, nil)
|
|
|
|
vfs.mounts.seq.EndWrite()
|
|
|
|
vfs.mountMu.Unlock()
|
|
|
|
for _, vd := range vdsToDecRef {
|
|
|
|
vd.DecRef()
|
|
|
|
}
|
|
|
|
for _, mnt := range mountsToDecRef {
|
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.DecRef()
|
2019-11-25 23:20:25 +00:00
|
|
|
}
|
|
|
|
} else if refs < 0 {
|
|
|
|
panic("MountNamespace.DecRef() called without holding a reference")
|
|
|
|
}
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getMountAt returns the last Mount in the stack mounted at (mnt, d). It takes
|
|
|
|
// a reference on the returned Mount. If (mnt, d) is not a mount point,
|
|
|
|
// getMountAt returns nil.
|
|
|
|
//
|
|
|
|
// getMountAt is analogous to Linux's fs/namei.c:follow_mount().
|
|
|
|
//
|
|
|
|
// Preconditions: References are held on mnt and d.
|
|
|
|
func (vfs *VirtualFilesystem) getMountAt(mnt *Mount, d *Dentry) *Mount {
|
|
|
|
// The first mount is special-cased:
|
|
|
|
//
|
|
|
|
// - The caller is assumed to have checked d.isMounted() already. (This
|
|
|
|
// isn't a precondition because it doesn't matter for correctness.)
|
|
|
|
//
|
|
|
|
// - We return nil, instead of mnt, if there is no mount at (mnt, d).
|
|
|
|
//
|
|
|
|
// - We don't drop the caller's references on mnt and d.
|
|
|
|
retryFirst:
|
|
|
|
next := vfs.mounts.Lookup(mnt, d)
|
|
|
|
if next == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !next.tryIncMountedRef() {
|
|
|
|
// Raced with umount.
|
|
|
|
goto retryFirst
|
|
|
|
}
|
|
|
|
mnt = next
|
|
|
|
d = next.root
|
|
|
|
// We don't need to take Dentry refs anywhere in this function because
|
|
|
|
// Mounts hold references on Mount.root, which is immutable.
|
|
|
|
for d.isMounted() {
|
|
|
|
next := vfs.mounts.Lookup(mnt, d)
|
|
|
|
if next == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !next.tryIncMountedRef() {
|
|
|
|
// Raced with umount.
|
|
|
|
continue
|
|
|
|
}
|
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.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
mnt = next
|
|
|
|
d = next.root
|
|
|
|
}
|
|
|
|
return mnt
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMountpointAt returns the mount point for the stack of Mounts including
|
2019-11-25 23:20:25 +00:00
|
|
|
// mnt. It takes a reference on the returned VirtualDentry. If no such mount
|
2019-07-18 22:09:14 +00:00
|
|
|
// point exists (i.e. mnt is a root mount), getMountpointAt returns (nil, nil).
|
|
|
|
//
|
|
|
|
// Preconditions: References are held on mnt and root. vfsroot is not (mnt,
|
|
|
|
// mnt.root).
|
2019-11-25 23:20:25 +00:00
|
|
|
func (vfs *VirtualFilesystem) getMountpointAt(mnt *Mount, vfsroot VirtualDentry) VirtualDentry {
|
2019-07-18 22:09:14 +00:00
|
|
|
// The first mount is special-cased:
|
|
|
|
//
|
|
|
|
// - The caller must have already checked mnt against vfsroot.
|
|
|
|
//
|
|
|
|
// - We return nil, instead of mnt, if there is no mount point for mnt.
|
|
|
|
//
|
|
|
|
// - We don't drop the caller's reference on mnt.
|
|
|
|
retryFirst:
|
|
|
|
epoch := vfs.mounts.seq.BeginRead()
|
2019-11-25 23:20:25 +00:00
|
|
|
parent, point := mnt.parent(), mnt.point()
|
2019-07-18 22:09:14 +00:00
|
|
|
if !vfs.mounts.seq.ReadOk(epoch) {
|
|
|
|
goto retryFirst
|
|
|
|
}
|
|
|
|
if parent == nil {
|
2019-11-25 23:20:25 +00:00
|
|
|
return VirtualDentry{}
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
if !parent.tryIncMountedRef() {
|
|
|
|
// Raced with umount.
|
|
|
|
goto retryFirst
|
|
|
|
}
|
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
|
|
|
if !point.TryIncRef() {
|
2019-07-18 22:09:14 +00:00
|
|
|
// Since Mount holds a reference on Mount.key.point, this can only
|
|
|
|
// happen due to a racing change to Mount.key.
|
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
|
|
|
parent.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
goto retryFirst
|
|
|
|
}
|
2019-11-25 23:20:25 +00:00
|
|
|
if !vfs.mounts.seq.ReadOk(epoch) {
|
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
|
|
|
point.DecRef()
|
|
|
|
parent.DecRef()
|
2019-11-25 23:20:25 +00:00
|
|
|
goto retryFirst
|
|
|
|
}
|
2019-07-18 22:09:14 +00:00
|
|
|
mnt = parent
|
|
|
|
d := point
|
|
|
|
for {
|
|
|
|
if mnt == vfsroot.mount && d == vfsroot.dentry {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if d != mnt.root {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
retryNotFirst:
|
|
|
|
epoch := vfs.mounts.seq.BeginRead()
|
2019-11-25 23:20:25 +00:00
|
|
|
parent, point := mnt.parent(), mnt.point()
|
2019-07-18 22:09:14 +00:00
|
|
|
if !vfs.mounts.seq.ReadOk(epoch) {
|
|
|
|
goto retryNotFirst
|
|
|
|
}
|
|
|
|
if parent == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !parent.tryIncMountedRef() {
|
|
|
|
// Raced with umount.
|
|
|
|
goto retryNotFirst
|
|
|
|
}
|
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
|
|
|
if !point.TryIncRef() {
|
2019-07-18 22:09:14 +00:00
|
|
|
// Since Mount holds a reference on Mount.key.point, this can
|
|
|
|
// only happen due to a racing change to Mount.key.
|
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
|
|
|
parent.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
goto retryNotFirst
|
|
|
|
}
|
|
|
|
if !vfs.mounts.seq.ReadOk(epoch) {
|
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
|
|
|
point.DecRef()
|
|
|
|
parent.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
goto retryNotFirst
|
|
|
|
}
|
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
|
|
|
d.DecRef()
|
|
|
|
mnt.DecRef()
|
2019-07-18 22:09:14 +00:00
|
|
|
mnt = parent
|
|
|
|
d = point
|
|
|
|
}
|
2019-11-25 23:20:25 +00:00
|
|
|
return VirtualDentry{mnt, d}
|
2019-07-18 22:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckBeginWrite increments the counter of in-progress write operations on
|
|
|
|
// mnt. If mnt is mounted MS_RDONLY, CheckBeginWrite does nothing and returns
|
|
|
|
// EROFS.
|
|
|
|
//
|
|
|
|
// If CheckBeginWrite succeeds, EndWrite must be called when the write
|
|
|
|
// operation is finished.
|
|
|
|
func (mnt *Mount) CheckBeginWrite() error {
|
|
|
|
if atomic.AddInt64(&mnt.writers, 1) < 0 {
|
|
|
|
atomic.AddInt64(&mnt.writers, -1)
|
|
|
|
return syserror.EROFS
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndWrite indicates that a write operation signaled by a previous successful
|
|
|
|
// call to CheckBeginWrite has finished.
|
|
|
|
func (mnt *Mount) EndWrite() {
|
|
|
|
atomic.AddInt64(&mnt.writers, -1)
|
|
|
|
}
|
|
|
|
|
2019-11-25 23:20:25 +00:00
|
|
|
// Preconditions: VirtualFilesystem.mountMu must be locked.
|
2019-07-18 22:09:14 +00:00
|
|
|
func (mnt *Mount) setReadOnlyLocked(ro bool) error {
|
|
|
|
if oldRO := atomic.LoadInt64(&mnt.writers) < 0; oldRO == ro {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ro {
|
|
|
|
if !atomic.CompareAndSwapInt64(&mnt.writers, 0, math.MinInt64) {
|
|
|
|
return syserror.EBUSY
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Unset MSB without dropping any temporary increments from failed calls to
|
|
|
|
// mnt.CheckBeginWrite().
|
|
|
|
atomic.AddInt64(&mnt.writers, math.MinInt64)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filesystem returns the mounted Filesystem. It does not take a reference on
|
|
|
|
// the returned Filesystem.
|
|
|
|
func (mnt *Mount) Filesystem() *Filesystem {
|
|
|
|
return mnt.fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns mntns' root. A reference is taken on the returned
|
|
|
|
// VirtualDentry.
|
|
|
|
func (mntns *MountNamespace) Root() VirtualDentry {
|
|
|
|
vd := VirtualDentry{
|
|
|
|
mount: mntns.root,
|
|
|
|
dentry: mntns.root.root,
|
|
|
|
}
|
|
|
|
vd.IncRef()
|
|
|
|
return vd
|
|
|
|
}
|