Skip to content

Commit 8310735

Browse files
E4242E4242
authored andcommitted
Able to compile imgfs
1 parent fb6592b commit 8310735

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

pkg/sentry/fs/imgfs/file.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package imgfs
1616

1717
import (
18+
"io"
1819
"gvisor.dev/gvisor/pkg/sentry/context"
1920
"gvisor.dev/gvisor/pkg/sentry/fs"
2021
"gvisor.dev/gvisor/pkg/sentry/fs/fsutil"
@@ -28,13 +29,16 @@ import (
2829
//
2930
// +stateify savable
3031
type regularFileOperations struct {
31-
waiter.AlwaysReady `state:"nosave"`
32-
fsutil.FileNoopRelease `state:"nosave"`
33-
fsutil.FileGenericSeek `state:"nosave"`
34-
fsutil.FileNotDirReaddir `state:"nosave"`
35-
fsutil.FileNoopFsync `state:"nosave"`
36-
fsutil.FileNoopFlush `state:"nosave"`
37-
fsutil.FileNoIoctl `state:"nosave"`
32+
33+
fsutil.FileNoopRelease `state:"nosave"`
34+
fsutil.FileGenericSeek `state:"nosave"`
35+
fsutil.FileNotDirReaddir `state:"nosave"`
36+
fsutil.FileNoopFsync `state:"nosave"`
37+
fsutil.FileNoopFlush `state:"nosave"`
38+
fsutil.FileNoIoctl `state:"nosave"`
39+
fsutil.FileNoSplice `state:"nosave"`
40+
fsutil.FileUseInodeUnstableAttr `state:"nosave"`
41+
waiter.AlwaysReady `state:"nosave"`
3842

3943
// iops is the InodeOperations of a regular tmpfs file. It is
4044
// guaranteed to be the same as file.Dirent.Inode.InodeOperations,
@@ -56,3 +60,8 @@ func (r *regularFileOperations) Write(ctx context.Context, file *fs.File, src us
5660
func (r *regularFileOperations) ConfigureMMap(ctx context.Context, file *fs.File, opts *memmap.MMapOpts) error {
5761
return fsutil.GenericConfigureMMap(file, r.iops, opts)
5862
}
63+
64+
// ReadFrom implements fs.FileOperations.ReadFrom.
65+
func (r *regularFileOperations) ReadFrom(context.Context, *fs.File, io.Reader, int64) (int64, error) {
66+
return 0, syserror.ENOSYS
67+
}

pkg/sentry/fs/imgfs/fs.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"gvisor.dev/gvisor/pkg/sentry/fs/ramfs"
3232
"gvisor.dev/gvisor/pkg/log"
3333
"gvisor.dev/gvisor/pkg/sentry/usermem"
34+
3435
)
3536

3637
// FilesystemName is the name under which Filesystem is registered.
@@ -121,7 +122,7 @@ func (f *Filesystem) Mount(ctx context.Context, _ string, flags fs.MountSourceFl
121122
}
122123

123124
// Construct img file system mount and inode.
124-
msrc := fs.NewCachingMountSource(f, flags)
125+
msrc := fs.NewCachingMountSource(ctx, f, flags)
125126

126127
var s syscall.Stat_t
127128
err := syscall.Fstat(int(f.packageFD), &s)
@@ -199,15 +200,15 @@ func MountImgRecursive(ctx context.Context, msrc *fs.MountSource, metadata []fil
199200
}
200201
}
201202
d := ramfs.NewDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(linux.FileMode(dirMode)))
202-
newinode := fs.NewInode(d, msrc, fs.StableAttr{
203+
newinode := fs.NewInode(ctx, d, msrc, fs.StableAttr{
203204
DeviceID: imgfsFileDevice.DeviceID(),
204205
InodeID: imgfsFileDevice.NextIno(),
205206
BlockSize: usermem.PageSize,
206207
Type: fs.Directory,
207208
})
208209

209210
for _, fn := range whitoutFiles {
210-
newinode.InodeOperations.Setxattr(newinode, fs.XattrOverlayWhiteout(fn), []byte("y"))
211+
newinode.InodeOperations.Setxattr(newinode, fs.XattrOverlayWhiteout(fn), string([]byte("y")))
211212
}
212213
return newinode, nil
213214
}

pkg/sentry/fs/imgfs/inode.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"fmt"
1919
"io"
2020
"os"
21+
"sync"
2122

2223
"gvisor.dev/gvisor/pkg/abi/linux"
24+
"gvisor.dev/gvisor/pkg/log"
2325
"gvisor.dev/gvisor/pkg/sentry/context"
2426
"gvisor.dev/gvisor/pkg/sentry/fs"
2527
"gvisor.dev/gvisor/pkg/sentry/fs/fsutil"
@@ -29,6 +31,8 @@ import (
2931
"gvisor.dev/gvisor/pkg/sentry/safemem"
3032
"gvisor.dev/gvisor/pkg/sentry/usermem"
3133
"gvisor.dev/gvisor/pkg/syserror"
34+
"gvisor.dev/gvisor/pkg/sentry/kernel"
35+
"gvisor.dev/gvisor/pkg/sentry/usage"
3236
)
3337
// inodeOperations implements fs.InodeOperations for an fs.Inodes backed
3438
// by a host file descriptor.
@@ -41,6 +45,7 @@ type fileInodeOperations struct {
4145
fsutil.InodeNotSocket `state:"nosave"`
4246
fsutil.InodeNotSymlink `state:"nosave"`
4347

48+
4449
fsutil.InodeSimpleExtendedAttributes
4550

4651
attr fs.UnstableAttr
@@ -52,6 +57,40 @@ type fileInodeOperations struct {
5257
offsetEnd int64
5358

5459
packageFD int
60+
61+
// kernel is used to allocate memory that stores the file's contents.
62+
kernel *kernel.Kernel
63+
64+
// memUsage is the default memory usage that will be reported by this file.
65+
memUsage usage.MemoryKind
66+
67+
attrMu sync.Mutex `state:"nosave"`
68+
69+
mapsMu sync.Mutex `state:"nosave"`
70+
71+
// writableMappingPages tracks how many pages of virtual memory are mapped
72+
// as potentially writable from this file. If a page has multiple mappings,
73+
// each mapping is counted separately.
74+
//
75+
// This counter is susceptible to overflow as we can potentially count
76+
// mappings from many VMAs. We count pages rather than bytes to slightly
77+
// mitigate this.
78+
//
79+
// Protected by mapsMu.
80+
writableMappingPages uint64
81+
82+
dataMu sync.RWMutex `state:"nosave"`
83+
84+
// data maps offsets into the file to offsets into platform.Memory() that
85+
// store the file's data.
86+
//
87+
// data is protected by dataMu.
88+
data fsutil.FileRangeSet
89+
90+
// seals represents file seals on this inode.
91+
//
92+
// Protected by dataMu.
93+
seals uint32
5594
}
5695

5796
type Symlink struct {
@@ -96,8 +135,14 @@ func (f *fileInodeOperations) Mappable(*fs.Inode) memmap.Mappable {
96135
}
97136

98137
// Rename implements fs.InodeOperations.Rename.
99-
func (*fileInodeOperations) Rename(ctx context.Context, oldParent *fs.Inode, oldName string, newParent *fs.Inode, newName string, replacement bool) error {
100-
return syserror.EPERM
138+
func (*fileInodeOperations) Rename(ctx context.Context, inode *fs.Inode, oldParent *fs.Inode, oldName string, newParent *fs.Inode, newName string, replacement bool) error {
139+
return rename(ctx, oldParent, oldName, newParent, newName, replacement)
140+
}
141+
142+
// rename implements fs.InodeOperations.Rename for tmpfs nodes.
143+
func rename(ctx context.Context, oldParent *fs.Inode, oldName string, newParent *fs.Inode, newName string, replacement bool) error {
144+
log.Infof("Called rename, not supported in imgfs")
145+
return syserror.EXDEV
101146
}
102147

103148
// GetFile implements fs.InodeOperations.GetFile.
@@ -239,6 +284,12 @@ func (f *fileInodeOperations) MapInternal(fr platform.FileRange, at usermem.Acce
239284
return seq, nil
240285
}
241286

287+
// Allocate implements fs.InodeOperations.Allocate.
288+
func (f *fileInodeOperations) Allocate(ctx context.Context, _ *fs.Inode, offset, length int64) error {
289+
log.Infof("Calling unimplemented method in imgfs")
290+
return nil
291+
}
292+
242293
// Translate implements memmap.Mappable.Translate.
243294
func (f *fileInodeOperations) Translate(ctx context.Context, required, optional memmap.MappableRange, at usermem.AccessType) ([]memmap.Translation, error) {
244295
return []memmap.Translation{
@@ -267,13 +318,13 @@ func newInode(ctx context.Context, msrc *fs.MountSource, begin int64, end int64,
267318
offsetEnd: end,
268319
packageFD: packageFD,
269320
}
270-
return fs.NewInode(iops, msrc, sattr), nil
321+
return fs.NewInode(ctx, iops, msrc, sattr), nil
271322
}
272323

273324
// newSymlink returns a new fs.Inode
274325
func newSymlink(ctx context.Context, msrc *fs.MountSource, target string) *fs.Inode {
275326
s := &Symlink{Symlink: *ramfs.NewSymlink(ctx, fs.RootOwner, target)}
276-
return fs.NewInode(s, msrc, fs.StableAttr{
327+
return fs.NewInode(ctx, s, msrc, fs.StableAttr{
277328
DeviceID: imgfsFileDevice.DeviceID(),
278329
InodeID: imgfsFileDevice.NextIno(),
279330
BlockSize: usermem.PageSize,

0 commit comments

Comments
 (0)