github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/fsimpl/tmpfs/save_restore.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 tmpfs 16 17 import ( 18 goContext "context" 19 "fmt" 20 21 "github.com/metacubex/gvisor/pkg/context" 22 "github.com/metacubex/gvisor/pkg/sentry/pgalloc" 23 "github.com/metacubex/gvisor/pkg/sentry/vfs" 24 ) 25 26 // saveMf is called by stateify. 27 func (fs *filesystem) saveMf() string { 28 if !fs.mf.IsSavable() { 29 panic(fmt.Sprintf("Can't save tmpfs filesystem because its MemoryFile is not savable: %v", fs.mf)) 30 } 31 return fs.mf.RestoreID() 32 } 33 34 // loadMf is called by stateify. 35 func (fs *filesystem) loadMf(ctx goContext.Context, restoreID string) { 36 if restoreID == "" { 37 fs.mf = pgalloc.MemoryFileFromContext(ctx) 38 return 39 } 40 mfmap := pgalloc.MemoryFileMapFromContext(ctx) 41 if mfmap == nil { 42 panic("CtxMemoryFileMap was not provided") 43 } 44 mf, ok := mfmap[restoreID] 45 if !ok { 46 panic(fmt.Sprintf("Memory file for %q not found in CtxMemoryFileMap", restoreID)) 47 } 48 fs.mf = mf 49 } 50 51 // saveParent is called by stateify. 52 func (d *dentry) saveParent() *dentry { 53 return d.parent.Load() 54 } 55 56 // loadParent is called by stateify. 57 func (d *dentry) loadParent(_ goContext.Context, parent *dentry) { 58 d.parent.Store(parent) 59 } 60 61 // PrepareSave implements vfs.FilesystemImplSaveRestoreExtension.PrepareSave. 62 func (fs *filesystem) PrepareSave(ctx context.Context) error { 63 restoreID := fs.mf.RestoreID() 64 if restoreID == "" { 65 return nil 66 } 67 mfmap := pgalloc.MemoryFileMapFromContext(ctx) 68 if mfmap == nil { 69 return fmt.Errorf("CtxMemoryFileMap was not provided") 70 } 71 if _, ok := mfmap[restoreID]; ok { 72 return fmt.Errorf("memory file for %q already exists in CtxMemoryFileMap", restoreID) 73 } 74 mfmap[restoreID] = fs.mf 75 return nil 76 } 77 78 // CompleteRestore implements 79 // vfs.FilesystemImplSaveRestoreExtension.CompleteRestore. 80 func (fs *filesystem) CompleteRestore(ctx context.Context, opts vfs.CompleteRestoreOptions) error { 81 return nil 82 }