github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/anon/anon.go (about)

     1  // Copyright 2018 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 anon implements an anonymous inode, useful for implementing
    16  // inodes for pseudo filesystems.
    17  package anon
    18  
    19  import (
    20  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    21  	"github.com/SagerNet/gvisor/pkg/context"
    22  	"github.com/SagerNet/gvisor/pkg/hostarch"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil"
    25  )
    26  
    27  // NewInode constructs an anonymous Inode that is not associated
    28  // with any real filesystem. Some types depend on completely pseudo
    29  // "anon" inodes (eventfds, epollfds, etc).
    30  func NewInode(ctx context.Context) *fs.Inode {
    31  	iops := &fsutil.SimpleFileInode{
    32  		InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, fs.RootOwner, fs.FilePermissions{
    33  			User: fs.PermMask{Read: true, Write: true},
    34  		}, linux.ANON_INODE_FS_MAGIC),
    35  	}
    36  	return fs.NewInode(ctx, iops, fs.NewPseudoMountSource(ctx), fs.StableAttr{
    37  		Type:      fs.Anonymous,
    38  		DeviceID:  PseudoDevice.DeviceID(),
    39  		InodeID:   PseudoDevice.NextIno(),
    40  		BlockSize: hostarch.PageSize,
    41  	})
    42  }