gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/devtmpfs/devtmpfs.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package devtmpfs provides a singleton fsimpl/dev filesystem instance, 16 // analogous to Linux's devtmpfs. 17 package devtmpfs 18 19 import ( 20 "gvisor.dev/gvisor/pkg/context" 21 "gvisor.dev/gvisor/pkg/sentry/fsimpl/dev" 22 "gvisor.dev/gvisor/pkg/sentry/kernel/auth" 23 "gvisor.dev/gvisor/pkg/sentry/vfs" 24 "gvisor.dev/gvisor/pkg/sync" 25 ) 26 27 // Name is the default filesystem name. 28 const Name = "devtmpfs" 29 30 // FilesystemType implements vfs.FilesystemType. 31 // 32 // +stateify savable 33 type FilesystemType struct { 34 initOnce sync.Once `state:"nosave"` 35 initErr error 36 37 // fs is the tmpfs filesystem that backs all mounts of this FilesystemType. 38 // root is fs' root. fs and root are immutable. 39 fs *vfs.Filesystem 40 root *vfs.Dentry 41 } 42 43 // Name implements vfs.FilesystemType.Name. 44 func (*FilesystemType) Name() string { 45 return Name 46 } 47 48 // GetFilesystem implements vfs.FilesystemType.GetFilesystem. 49 func (fst *FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, source string, opts vfs.GetFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) { 50 fst.initOnce.Do(func() { 51 fs, root, err := dev.FilesystemType{}.GetFilesystem(ctx, vfsObj, creds, source, opts) 52 if err != nil { 53 fst.initErr = err 54 return 55 } 56 fst.fs = fs 57 fst.root = root 58 }) 59 if fst.initErr != nil { 60 return nil, nil, fst.initErr 61 } 62 fst.fs.IncRef() 63 fst.root.IncRef() 64 return fst.fs, fst.root, nil 65 } 66 67 // Release implements vfs.FilesystemType.Release. 68 func (fst *FilesystemType) Release(ctx context.Context) { 69 if fst.fs != nil { 70 // Release the original reference obtained when creating the filesystem. 71 fst.root.DecRef(ctx) 72 fst.fs.DecRef(ctx) 73 } 74 }