github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/fsimpl/erofs/save_restore.go (about)

     1  // Copyright 2023 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 erofs
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/metacubex/gvisor/pkg/erofs"
    23  	"github.com/metacubex/gvisor/pkg/sentry/vfs"
    24  )
    25  
    26  // afterLoad is called by stateify.
    27  func (fs *filesystem) afterLoad(ctx context.Context) {
    28  	fdmap := vfs.RestoreFilesystemFDMapFromContext(ctx)
    29  	fd, ok := fdmap[fs.iopts.UniqueID]
    30  	if !ok {
    31  		panic(fmt.Sprintf("no image FD available for filesystem with unique ID %q", fs.iopts.UniqueID))
    32  	}
    33  	newImage, err := erofs.OpenImage(os.NewFile(uintptr(fd), "EROFS image file"))
    34  	if err != nil {
    35  		panic(fmt.Sprintf("erofs.OpenImage failed: %v", err))
    36  	}
    37  	if got, want := newImage.SuperBlock(), fs.image.SuperBlock(); got != want {
    38  		panic(fmt.Sprintf("superblock mismatch detected on restore, got %+v, expected %+v", got, want))
    39  	}
    40  	// We need to update the image in place, as there are other pointers
    41  	// pointing to this image as well.
    42  	*fs.image = *newImage
    43  }
    44  
    45  // saveParent is called by stateify.
    46  func (d *dentry) saveParent() *dentry {
    47  	return d.parent.Load()
    48  }
    49  
    50  // loadParent is called by stateify.
    51  func (d *dentry) loadParent(_ context.Context, parent *dentry) {
    52  	d.parent.Store(parent)
    53  }