github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/dev/full.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 dev
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/context"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil"
    22  	"github.com/SagerNet/gvisor/pkg/syserror"
    23  	"github.com/SagerNet/gvisor/pkg/usermem"
    24  	"github.com/SagerNet/gvisor/pkg/waiter"
    25  )
    26  
    27  // fullDevice is used to implement /dev/full.
    28  //
    29  // +stateify savable
    30  type fullDevice struct {
    31  	fsutil.InodeGenericChecker       `state:"nosave"`
    32  	fsutil.InodeNoExtendedAttributes `state:"nosave"`
    33  	fsutil.InodeNoopAllocate         `state:"nosave"`
    34  	fsutil.InodeNoopRelease          `state:"nosave"`
    35  	fsutil.InodeNoopTruncate         `state:"nosave"`
    36  	fsutil.InodeNoopWriteOut         `state:"nosave"`
    37  	fsutil.InodeNotDirectory         `state:"nosave"`
    38  	fsutil.InodeNotMappable          `state:"nosave"`
    39  	fsutil.InodeNotSocket            `state:"nosave"`
    40  	fsutil.InodeNotSymlink           `state:"nosave"`
    41  	fsutil.InodeVirtual              `state:"nosave"`
    42  
    43  	fsutil.InodeSimpleAttributes
    44  }
    45  
    46  var _ fs.InodeOperations = (*fullDevice)(nil)
    47  
    48  func newFullDevice(ctx context.Context, owner fs.FileOwner, mode linux.FileMode) *fullDevice {
    49  	f := &fullDevice{
    50  		InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, owner, fs.FilePermsFromMode(mode), linux.TMPFS_MAGIC),
    51  	}
    52  	return f
    53  }
    54  
    55  // GetFile implements fs.InodeOperations.GetFile.
    56  func (f *fullDevice) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
    57  	flags.Pread = true
    58  	return fs.NewFile(ctx, dirent, flags, &fullFileOperations{}), nil
    59  }
    60  
    61  // +stateify savable
    62  type fullFileOperations struct {
    63  	waiter.AlwaysReady              `state:"nosave"`
    64  	fsutil.FileGenericSeek          `state:"nosave"`
    65  	fsutil.FileNoIoctl              `state:"nosave"`
    66  	fsutil.FileNoMMap               `state:"nosave"`
    67  	fsutil.FileNoopFlush            `state:"nosave"`
    68  	fsutil.FileNoopFsync            `state:"nosave"`
    69  	fsutil.FileNoopRelease          `state:"nosave"`
    70  	fsutil.FileNotDirReaddir        `state:"nosave"`
    71  	fsutil.FileUseInodeUnstableAttr `state:"nosave"`
    72  	fsutil.FileNoSplice             `state:"nosave"`
    73  	readZeros                       `state:"nosave"`
    74  }
    75  
    76  var _ fs.FileOperations = (*fullFileOperations)(nil)
    77  
    78  // Write implements FileOperations.Write.
    79  func (*fullFileOperations) Write(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
    80  	return 0, syserror.ENOSPC
    81  }