summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fsimpl/tmpfs/tmpfs.go
diff options
context:
space:
mode:
authorDean Deng <deandeng@google.com>2020-08-04 18:18:01 -0700
committergVisor bot <gvisor-bot@google.com>2020-08-04 18:20:20 -0700
commitb44408b40e3e8762a77ccf5eeb7f2ef567235c43 (patch)
tree3df8313d5d930a48c6bc1f350ce0bbf342f4a3aa /pkg/sentry/fsimpl/tmpfs/tmpfs.go
parent338f96b36c778748ff27f5ae73cc73b222c5a90e (diff)
Automated rollback of changelist 324906582
PiperOrigin-RevId: 324931854
Diffstat (limited to 'pkg/sentry/fsimpl/tmpfs/tmpfs.go')
-rw-r--r--pkg/sentry/fsimpl/tmpfs/tmpfs.go27
1 files changed, 21 insertions, 6 deletions
diff --git a/pkg/sentry/fsimpl/tmpfs/tmpfs.go b/pkg/sentry/fsimpl/tmpfs/tmpfs.go
index 5640380dc..68e615e8b 100644
--- a/pkg/sentry/fsimpl/tmpfs/tmpfs.go
+++ b/pkg/sentry/fsimpl/tmpfs/tmpfs.go
@@ -285,10 +285,13 @@ type inode struct {
// fs is the owning filesystem. fs is immutable.
fs *filesystem
+ // refs is a reference count. refs is accessed using atomic memory
+ // operations.
+ //
// A reference is held on all inodes as long as they are reachable in the
// filesystem tree, i.e. nlink is nonzero. This reference is dropped when
// nlink reaches 0.
- refs inodeRefs
+ refs int64
// xattrs implements extended attributes.
//
@@ -324,6 +327,7 @@ func (i *inode) init(impl interface{}, fs *filesystem, kuid auth.KUID, kgid auth
panic("file type is required in FileMode")
}
i.fs = fs
+ i.refs = 1
i.mode = uint32(mode)
i.uid = uint32(kuid)
i.gid = uint32(kgid)
@@ -335,7 +339,6 @@ func (i *inode) init(impl interface{}, fs *filesystem, kuid auth.KUID, kgid auth
i.mtime = now
// i.nlink initialized by caller
i.impl = impl
- i.refs.EnableLeakCheck()
}
// incLinksLocked increments i's link count.
@@ -366,15 +369,25 @@ func (i *inode) decLinksLocked(ctx context.Context) {
}
func (i *inode) incRef() {
- i.refs.IncRef()
+ if atomic.AddInt64(&i.refs, 1) <= 1 {
+ panic("tmpfs.inode.incRef() called without holding a reference")
+ }
}
func (i *inode) tryIncRef() bool {
- return i.refs.TryIncRef()
+ 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(ctx context.Context) {
- i.refs.DecRef(func() {
+ if refs := atomic.AddInt64(&i.refs, -1); refs == 0 {
i.watches.HandleDeletion(ctx)
if regFile, ok := i.impl.(*regularFile); ok {
// Release memory used by regFile to store data. Since regFile is
@@ -382,7 +395,9 @@ func (i *inode) decRef(ctx context.Context) {
// metadata.
regFile.data.DropAll(regFile.memFile)
}
- })
+ } else if refs < 0 {
+ panic("tmpfs.inode.decRef() called without holding a reference")
+ }
}
func (i *inode) checkPermissions(creds *auth.Credentials, ats vfs.AccessTypes) error {