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
|
|
|
|
|
2019-07-24 23:02:07 +00:00
|
|
|
import (
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
|
|
|
)
|
|
|
|
|
2019-07-23 22:50:35 +00:00
|
|
|
// dentry implements vfs.DentryImpl.
|
|
|
|
type dentry struct {
|
2019-07-24 23:02:07 +00:00
|
|
|
vfsd vfs.Dentry
|
|
|
|
|
2019-07-23 22:50:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-07-24 23:02:07 +00:00
|
|
|
|
|
|
|
// Compiles only if dentry implements vfs.DentryImpl.
|
|
|
|
var _ vfs.DentryImpl = (*dentry)(nil)
|
|
|
|
|
2019-07-25 00:58:28 +00:00
|
|
|
// newDentry is the dentry constructor.
|
|
|
|
func newDentry(in *inode) *dentry {
|
|
|
|
d := &dentry{
|
|
|
|
inode: in,
|
|
|
|
}
|
|
|
|
d.vfsd.Init(d)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-07-24 23:02:07 +00:00
|
|
|
// IncRef implements vfs.DentryImpl.IncRef.
|
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 (d *dentry) IncRef() {
|
2019-07-24 23:02:07 +00:00
|
|
|
d.inode.incRef()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryIncRef implements vfs.DentryImpl.TryIncRef.
|
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 (d *dentry) TryIncRef() bool {
|
2019-07-24 23:02:07 +00:00
|
|
|
return d.inode.tryIncRef()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecRef implements vfs.DentryImpl.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
|
|
|
func (d *dentry) DecRef() {
|
|
|
|
// FIXME(b/134676337): filesystem.mu may not be locked as required by
|
|
|
|
// inode.decRef().
|
|
|
|
d.inode.decRef()
|
2019-07-24 23:02:07 +00:00
|
|
|
}
|