summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/vfs/device.go
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2020-01-24 21:06:56 +0000
committergVisor bot <gvisor-bot@google.com>2020-01-24 21:06:56 +0000
commit1e9a87f806f148df8ebe96d995fe21d6ba4b3384 (patch)
tree13b7a892b8c4bee2ad1f0987e265e52fe4629c05 /pkg/sentry/vfs/device.go
parent0749ef3adde8e644fca52482b1c1d1df26e6b759 (diff)
parentd135b5abf6eafa92d2745dc98d48ef39d2f90e75 (diff)
Merge release-20200115.0-98-gd135b5a (automated)
Diffstat (limited to 'pkg/sentry/vfs/device.go')
-rwxr-xr-xpkg/sentry/vfs/device.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/pkg/sentry/vfs/device.go b/pkg/sentry/vfs/device.go
index cb672e36f..9f9d6e783 100755
--- a/pkg/sentry/vfs/device.go
+++ b/pkg/sentry/vfs/device.go
@@ -98,3 +98,32 @@ func (vfs *VirtualFilesystem) OpenDeviceSpecialFile(ctx context.Context, mnt *Mo
}
return rd.dev.Open(ctx, mnt, d, *opts)
}
+
+// GetAnonBlockDevMinor allocates and returns an unused minor device number for
+// an "anonymous" block device with major number 0.
+func (vfs *VirtualFilesystem) GetAnonBlockDevMinor() (uint32, error) {
+ vfs.anonBlockDevMinorMu.Lock()
+ defer vfs.anonBlockDevMinorMu.Unlock()
+ minor := vfs.anonBlockDevMinorNext
+ const maxDevMinor = (1 << 20) - 1
+ for minor < maxDevMinor {
+ if _, ok := vfs.anonBlockDevMinor[minor]; !ok {
+ vfs.anonBlockDevMinor[minor] = struct{}{}
+ vfs.anonBlockDevMinorNext = minor + 1
+ return minor, nil
+ }
+ minor++
+ }
+ return 0, syserror.EMFILE
+}
+
+// PutAnonBlockDevMinor deallocates a minor device number returned by a
+// previous call to GetAnonBlockDevMinor.
+func (vfs *VirtualFilesystem) PutAnonBlockDevMinor(minor uint32) {
+ vfs.anonBlockDevMinorMu.Lock()
+ defer vfs.anonBlockDevMinorMu.Unlock()
+ delete(vfs.anonBlockDevMinor, minor)
+ if minor < vfs.anonBlockDevMinorNext {
+ vfs.anonBlockDevMinorNext = minor
+ }
+}