1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package erofs implements erofs.
package erofs
import (
"os"
"runtime"
"strconv"
"sync"
"sync/atomic"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/cleanup"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/erofs"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/memmap"
"gvisor.dev/gvisor/pkg/sentry/vfs"
)
// Name is the filesystem name. It is part of the interface used by users,
// e.g. via annotations, and shouldn't change.
const Name = "erofs"
// Mount option names for EROFS.
const (
moptImageFD = "ifd"
)
// FilesystemType implements vfs.FilesystemType.
//
// +stateify savable
type FilesystemType struct{}
// filesystem implements vfs.FilesystemImpl.
//
// +stateify savable
type filesystem struct {
vfsfs vfs.Filesystem
// Immutable options.
mopts string
iopts InternalFilesystemOptions
// devMinor is the filesystem's minor device number. devMinor is immutable.
devMinor uint32
// root is the root dentry. root is immutable.
root *dentry
// image is the EROFS image. image is immutable.
image *erofs.Image
// mf implements memmap.File for this image.
mf imageMemmapFile
// inodeBuckets contains the inodes in use. Multiple buckets are used to
// reduce the lock contention. Bucket is chosen based on the hash calculation
// on nid in filesystem.inodeBucket.
inodeBuckets []inodeBucket
}
// InternalFilesystemOptions may be passed as
// vfs.GetFilesystemOptions.InternalData to FilesystemType.GetFilesystem.
//
// +stateify savable
type InternalFilesystemOptions struct {
// If UniqueID is non-empty, it is an opaque string used to reassociate the
// filesystem with a new image FD during restoration from checkpoint.
UniqueID vfs.RestoreID
}
// Name implements vfs.FilesystemType.Name.
func (FilesystemType) Name() string {
return Name
}
// Release implements vfs.FilesystemType.Release.
func (FilesystemType) Release(ctx context.Context) {}
// GetFilesystem implements vfs.FilesystemType.GetFilesystem.
func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, source string, opts vfs.GetFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) {
mopts := vfs.GenericParseMountOptions(opts.Data)
var cu cleanup.Cleanup
defer cu.Clean()
fd, err := getFDFromMountOptionsMap(ctx, mopts)
if err != nil {
return nil, nil, err
}
f := os.NewFile(uintptr(fd), "EROFS image file")
image, err := erofs.OpenImage(f)
if err != nil {
f.Close()
return nil, nil, err
}
cu.Add(func() { image.Close() })
iopts, ok := opts.InternalData.(InternalFilesystemOptions)
if opts.InternalData != nil && !ok {
ctx.Warningf("erofs.FilesystemType.GetFilesystem: GetFilesystemOptions.InternalData has type %T, wanted erofs.InternalFilesystemOptions", opts.InternalData)
return nil, nil, linuxerr.EINVAL
}
devMinor, err := vfsObj.GetAnonBlockDevMinor()
if err != nil {
return nil, nil, err
}
fs := &filesystem{
mopts: opts.Data,
iopts: iopts,
image: image,
devMinor: devMinor,
mf: imageMemmapFile{image: image},
}
fs.vfsfs.Init(vfsObj, &fstype, fs)
cu.Add(func() { fs.vfsfs.DecRef(ctx) })
fs.inodeBuckets = make([]inodeBucket, runtime.GOMAXPROCS(0))
for i := range fs.inodeBuckets {
fs.inodeBuckets[i].init()
}
root, err := fs.newDentry(image.RootNid())
if err != nil {
return nil, nil, err
}
// Increase the root's reference count to 2. One reference is returned to
// the caller, and the other is held by fs.
root.IncRef()
fs.root = root
cu.Release()
return &fs.vfsfs, &root.vfsd, nil
}
func getFDFromMountOptionsMap(ctx context.Context, mopts map[string]string) (int, error) {
ifdstr, ok := mopts[moptImageFD]
if !ok {
ctx.Warningf("erofs.getFDFromMountOptionsMap: image FD must be specified as '%s=<file descriptor>'", moptImageFD)
return -1, linuxerr.EINVAL
}
delete(mopts, moptImageFD)
ifd, err := strconv.Atoi(ifdstr)
if err != nil {
ctx.Warningf("erofs.getFDFromMountOptionsMap: invalid image FD: %s=%s", moptImageFD, ifdstr)
return -1, linuxerr.EINVAL
}
return ifd, nil
}
// Release implements vfs.FilesystemImpl.Release.
func (fs *filesystem) Release(ctx context.Context) {
// An extra reference was held by the filesystem on the root.
if fs.root != nil {
fs.root.DecRef(ctx)
}
fs.image.Close()
fs.vfsfs.VirtualFilesystem().PutAnonBlockDevMinor(fs.devMinor)
}
func (fs *filesystem) statFS() linux.Statfs {
blockSize := int64(fs.image.BlockSize())
return linux.Statfs{
Type: erofs.SuperBlockMagicV1,
NameLength: erofs.MaxNameLen,
BlockSize: blockSize,
FragmentSize: blockSize,
Blocks: uint64(fs.image.Blocks()),
}
}
// +stateify savable
type inodeBucket struct {
// mu protects inodeMap.
mu sync.RWMutex `state:"nosave"`
// inodeMap contains the inodes indexed by nid.
// +checklocks:mu
inodeMap map[uint64]*inode
}
func (ib *inodeBucket) init() {
ib.inodeMap = make(map[uint64]*inode) // +checklocksignore
}
// getInode returns the inode identified by nid. A reference on inode is also
// returned to caller.
func (ib *inodeBucket) getInode(nid uint64) *inode {
ib.mu.RLock()
defer ib.mu.RUnlock()
i := ib.inodeMap[nid]
if i != nil {
i.IncRef()
}
return i
}
// addInode adds the inode identified by nid into the bucket. It will first check
// whether the old inode exists. If not, it will call newInode() to get the new inode.
// The inode eventually saved in the bucket will be returned with a reference for caller.
func (ib *inodeBucket) addInode(nid uint64, newInode func() *inode) *inode {
ib.mu.Lock()
defer ib.mu.Unlock()
if i, ok := ib.inodeMap[nid]; ok {
i.IncRef()
return i
}
i := newInode()
ib.inodeMap[nid] = i
return i
}
// removeInode removes the inode identified by nid.
func (ib *inodeBucket) removeInode(nid uint64) {
ib.mu.Lock()
delete(ib.inodeMap, nid)
ib.mu.Unlock()
}
func (fs *filesystem) inodeBucket(nid uint64) *inodeBucket {
bucket := nid % uint64(len(fs.inodeBuckets))
return &fs.inodeBuckets[bucket]
}
// inode represents a filesystem object.
//
// Each dentry holds a reference on the inode it represents. An inode will
// be dropped once its reference count reaches zero. We do not cache inodes
// directly. The caching policy is implemented on top of dentries.
//
// +stateify savable
type inode struct {
erofs.Inode
// inodeRefs is the reference count.
inodeRefs
// fs is the owning filesystem.
fs *filesystem
// dirMu protects dirents. dirents is immutable after creation.
dirMu sync.RWMutex `state:"nosave"`
// +checklocks:dirMu
dirents []vfs.Dirent `state:"nosave"`
// mapsMu protects mappings.
mapsMu sync.Mutex `state:"nosave"`
// mappings tracks the mappings of the file into memmap.MappingSpaces
// if this inode represents a regular file.
// +checklocks:mapsMu
mappings memmap.MappingSet
// locks supports POSIX and BSD style locks.
locks vfs.FileLocks
// Inotify watches for this inode.
watches vfs.Watches
}
// getInode returns the inode identified by nid. A reference on inode is also
// returned to caller.
func (fs *filesystem) getInode(nid uint64) (*inode, error) {
bucket := fs.inodeBucket(nid)
// Fast path, inode already exists.
if i := bucket.getInode(nid); i != nil {
return i, nil
}
// Slow path, create a new inode.
//
// Construct the underlying inode object from the image without taking
// the bucket lock first to reduce the contention.
ino, err := fs.image.Inode(nid)
if err != nil {
return nil, err
}
return bucket.addInode(nid, func() *inode {
i := &inode{
Inode: ino,
fs: fs,
}
i.InitRefs()
return i
}), nil
}
// DecRef should be called when you're finished with an inode.
func (i *inode) DecRef(ctx context.Context) {
i.inodeRefs.DecRef(func() {
nid := i.Nid()
i.fs.inodeBucket(nid).removeInode(nid)
})
}
func (i *inode) checkPermissions(creds *auth.Credentials, ats vfs.AccessTypes) error {
return vfs.GenericCheckPermissions(creds, ats, linux.FileMode(i.Mode()), auth.KUID(i.UID()), auth.KGID(i.GID()))
}
func (i *inode) statTo(stat *linux.Statx) {
stat.Mask = linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_NLINK |
linux.STATX_UID | linux.STATX_GID | linux.STATX_INO | linux.STATX_SIZE |
linux.STATX_BLOCKS | linux.STATX_ATIME | linux.STATX_CTIME |
linux.STATX_MTIME
stat.Blksize = i.fs.image.BlockSize()
stat.Nlink = i.Nlink()
stat.UID = i.UID()
stat.GID = i.GID()
stat.Mode = i.Mode()
stat.Ino = i.Nid()
stat.Size = i.Size()
stat.Blocks = (stat.Size + 511) / 512
stat.Mtime = linux.StatxTimestamp{
Sec: int64(i.Mtime()),
Nsec: i.MtimeNsec(),
}
stat.Atime = stat.Mtime
stat.Ctime = stat.Mtime
stat.DevMajor = linux.UNNAMED_MAJOR
stat.DevMinor = i.fs.devMinor
}
func (i *inode) fileType() uint16 {
return i.Mode() & linux.S_IFMT
}
// dentry implements vfs.DentryImpl.
//
// The filesystem is read-only and currently we never drop the cached dentries
// until the filesystem is unmounted. The reference model works like this:
//
// - The initial reference count of each dentry is one, which is the reference
// held by the parent (so when the reference count is one, it also means that
// this is a cached dentry, i.e. not in use).
//
// - When a dentry is used (e.g. opened by someone), its reference count will
// be increased and the new reference is held by caller.
//
// - The reference count of root dentry is two. One reference is returned to
// the caller of `GetFilesystem()`, and the other is held by `fs`.
//
// TODO: This can lead to unbounded memory growth in sentry due to the ever-growing
// dentry tree. We should have a dentry LRU cache, similar to what fsimpl/gofer does.
//
// +stateify savable
type dentry struct {
vfsd vfs.Dentry
// dentryRefs is the reference count.
dentryRefs
// parent is this dentry's parent directory. If this dentry is
// a file system root, parent is nil.
parent atomic.Pointer[dentry] `state:".(*dentry)"`
// name is this dentry's name in its parent. If this dentry is
// a file system root, name is the empty string.
name string
// inode is the inode represented by this dentry.
inode *inode
// dirMu serializes changes to the dentry tree.
dirMu sync.RWMutex `state:"nosave"`
// childMap contains the mappings of child names to dentries if this
// dentry represents a directory.
// +checklocks:dirMu
childMap map[string]*dentry
}
// The caller is expected to handle dentry insertion into dentry tree.
func (fs *filesystem) newDentry(nid uint64) (*dentry, error) {
i, err := fs.getInode(nid)
if err != nil {
return nil, err
}
d := &dentry{
inode: i,
}
d.InitRefs()
d.vfsd.Init(d)
return d, nil
}
// DecRef implements vfs.DentryImpl.DecRef.
func (d *dentry) DecRef(ctx context.Context) {
d.dentryRefs.DecRef(func() {
d.dirMu.Lock()
for _, c := range d.childMap {
c.DecRef(ctx)
}
d.childMap = nil
d.dirMu.Unlock()
d.inode.DecRef(ctx)
})
}
// InotifyWithParent implements vfs.DentryImpl.InotifyWithParent.
func (d *dentry) InotifyWithParent(ctx context.Context, events, cookie uint32, et vfs.EventType) {
if d.inode.IsDir() {
events |= linux.IN_ISDIR
}
// The ordering below is important, Linux always notifies the parent first.
if parent := d.parent.Load(); parent != nil {
parent.inode.watches.Notify(ctx, d.name, events, cookie, et, false)
}
d.inode.watches.Notify(ctx, "", events, cookie, et, false)
}
// Watches implements vfs.DentryImpl.Watches.
func (d *dentry) Watches() *vfs.Watches {
return &d.inode.watches
}
// OnZeroWatches implements vfs.DentryImpl.OnZeroWatches.
func (d *dentry) OnZeroWatches(ctx context.Context) {}
func (d *dentry) open(ctx context.Context, rp *vfs.ResolvingPath, opts *vfs.OpenOptions) (*vfs.FileDescription, error) {
ats := vfs.AccessTypesForOpenFlags(opts)
if err := d.inode.checkPermissions(rp.Credentials(), ats); err != nil {
return nil, err
}
switch d.inode.fileType() {
case linux.S_IFREG:
if ats&vfs.MayWrite != 0 {
return nil, linuxerr.EROFS
}
var fd regularFileFD
fd.LockFD.Init(&d.inode.locks)
if err := fd.vfsfd.Init(&fd, opts.Flags, rp.Mount(), &d.vfsd, &vfs.FileDescriptionOptions{AllowDirectIO: true}); err != nil {
return nil, err
}
return &fd.vfsfd, nil
case linux.S_IFDIR:
// Can't open directories with O_CREAT.
if opts.Flags&linux.O_CREAT != 0 {
return nil, linuxerr.EISDIR
}
// Can't open directories writably.
if ats&vfs.MayWrite != 0 {
return nil, linuxerr.EISDIR
}
if opts.Flags&linux.O_DIRECT != 0 {
return nil, linuxerr.EINVAL
}
var fd directoryFD
fd.LockFD.Init(&d.inode.locks)
if err := fd.vfsfd.Init(&fd, opts.Flags, rp.Mount(), &d.vfsd, &vfs.FileDescriptionOptions{AllowDirectIO: true}); err != nil {
return nil, err
}
return &fd.vfsfd, nil
case linux.S_IFLNK:
// Can't open symlinks without O_PATH, which is handled at the VFS layer.
return nil, linuxerr.ELOOP
default:
return nil, linuxerr.ENXIO
}
}
// +stateify savable
type fileDescription struct {
vfsfd vfs.FileDescription
vfs.FileDescriptionDefaultImpl
vfs.LockFD
lockLogging sync.Once `state:"nosave"`
}
func (fd *fileDescription) filesystem() *filesystem {
return fd.vfsfd.Mount().Filesystem().Impl().(*filesystem)
}
func (fd *fileDescription) dentry() *dentry {
return fd.vfsfd.Dentry().Impl().(*dentry)
}
func (fd *fileDescription) inode() *inode {
return fd.dentry().inode
}
// Stat implements vfs.FileDescriptionImpl.Stat.
func (fd *fileDescription) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
var stat linux.Statx
fd.inode().statTo(&stat)
return stat, nil
}
// SetStat implements vfs.FileDescriptionImpl.SetStat.
func (fd *fileDescription) SetStat(ctx context.Context, opts vfs.SetStatOptions) error {
return linuxerr.EROFS
}
// StatFS implements vfs.FileDescriptionImpl.StatFS.
func (fd *fileDescription) StatFS(ctx context.Context) (linux.Statfs, error) {
return fd.filesystem().statFS(), nil
}
// ListXattr implements vfs.FileDescriptionImpl.ListXattr.
func (fd *fileDescription) ListXattr(ctx context.Context, size uint64) ([]string, error) {
return nil, linuxerr.ENOTSUP
}
// GetXattr implements vfs.FileDescriptionImpl.GetXattr.
func (fd *fileDescription) GetXattr(ctx context.Context, opts vfs.GetXattrOptions) (string, error) {
return "", linuxerr.ENOTSUP
}
// SetXattr implements vfs.FileDescriptionImpl.SetXattr.
func (fd *fileDescription) SetXattr(ctx context.Context, opts vfs.SetXattrOptions) error {
return linuxerr.EROFS
}
// RemoveXattr implements vfs.FileDescriptionImpl.RemoveXattr.
func (fd *fileDescription) RemoveXattr(ctx context.Context, name string) error {
return linuxerr.EROFS
}
// Sync implements vfs.FileDescriptionImpl.Sync.
func (*fileDescription) Sync(context.Context) error {
return nil
}
// Release implements vfs.FileDescriptionImpl.Release.
func (*fileDescription) Release(ctx context.Context) {}
|