github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/host/inode_state.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 host
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"golang.org/x/sys/unix"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/device"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    23  )
    24  
    25  // afterLoad is invoked by stateify.
    26  func (i *inodeFileState) afterLoad() {
    27  	// Initialize the descriptor value.
    28  	if err := i.descriptor.initAfterLoad(i.sattr.InodeID, &i.queue); err != nil {
    29  		panic(fmt.Sprintf("failed to load value of descriptor: %v", err))
    30  	}
    31  
    32  	// Remap the inode number.
    33  	var s unix.Stat_t
    34  	if err := unix.Fstat(i.FD(), &s); err != nil {
    35  		panic(fs.ErrCorruption{fmt.Errorf("failed to get metadata for fd %d: %v", i.FD(), err)})
    36  	}
    37  	key := device.MultiDeviceKey{
    38  		Device: s.Dev,
    39  		Inode:  s.Ino,
    40  	}
    41  	if !hostFileDevice.Load(key, i.sattr.InodeID) {
    42  		// This means there was a conflict at s.Dev and s.Ino with
    43  		// another inode mapping: two files that were unique on the
    44  		// saved filesystem are no longer unique on this filesystem.
    45  		// Since this violates the contract that filesystems cannot
    46  		// change across save and restore, error out.
    47  		panic(fs.ErrCorruption{fmt.Errorf("host %s conflict in host device mappings: %s", key, hostFileDevice)})
    48  	}
    49  }