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 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
// Copyright 2022 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 gofer
import (
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/fsutil"
"gvisor.dev/gvisor/pkg/lisafs"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/vfs"
)
// We do *not* define an interface for dentry.impl because making interface
// method calls is almost 2.5x slower than calling the same method on a
// concrete type. Instead, we use type assertions in switch statements. The
// asserted type is a concrete dentry implementation and methods are called
// directly on the concrete type. This helps in the following ways:
//
// 1. This is faster because concrete type assertion just needs to compare the
// itab pointer in the interface value to a constant which is relatively
// cheap. Benchmarking showed that such type switches don't add almost any
// overhead.
// 2. Passing any pointer to an interface method immediately causes the pointed
// object to escape to heap. Making concrete method calls allows escape
// analysis to proceed as usual and avoids heap allocations.
//
// Also note that the default case in these type switch statements panics. We
// do not do panic(fmt.Sprintf("... %T", d.impl)) because somehow it adds a lot
// of overhead to the type switch. So instead we panic with a constant string.
// Precondition: d.handleMu must be locked.
func (d *dentry) isReadHandleOk() bool {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.readFDLisa.Ok()
case *directfsDentry:
return d.readFD.RacyLoad() >= 0
case nil: // synthetic dentry
return false
default:
panic("unknown dentry implementation")
}
}
// Precondition: d.handleMu must be locked.
func (d *dentry) isWriteHandleOk() bool {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.writeFDLisa.Ok()
case *directfsDentry:
return d.writeFD.RacyLoad() >= 0
case nil: // synthetic dentry
return false
default:
panic("unknown dentry implementation")
}
}
// Precondition: d.handleMu must be locked.
func (d *dentry) readHandle() handle {
switch dt := d.impl.(type) {
case *lisafsDentry:
return handle{
fdLisa: dt.readFDLisa,
fd: d.readFD.RacyLoad(),
}
case *directfsDentry:
return handle{fd: d.readFD.RacyLoad()}
case nil: // synthetic dentry
return noHandle
default:
panic("unknown dentry implementation")
}
}
// Precondition: d.handleMu must be locked.
func (d *dentry) writeHandle() handle {
switch dt := d.impl.(type) {
case *lisafsDentry:
return handle{
fdLisa: dt.writeFDLisa,
fd: d.writeFD.RacyLoad(),
}
case *directfsDentry:
return handle{fd: d.writeFD.RacyLoad()}
case nil: // synthetic dentry
return noHandle
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - fs.renameMu is locked.
func (d *dentry) openHandle(ctx context.Context, read, write, trunc bool) (handle, error) {
flags := uint32(unix.O_RDONLY)
switch {
case read && write:
flags = unix.O_RDWR
case read:
flags = unix.O_RDONLY
case write:
flags = unix.O_WRONLY
default:
log.Debugf("openHandle called with read = write = false. Falling back to read only FD.")
}
if trunc {
flags |= unix.O_TRUNC
}
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.openHandle(ctx, flags)
case *directfsDentry:
return dt.openHandle(ctx, flags)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - d.handleMu must be locked.
// - !d.isSynthetic().
func (d *dentry) updateHandles(ctx context.Context, h handle, readable, writable bool) {
switch dt := d.impl.(type) {
case *lisafsDentry:
dt.updateHandles(ctx, h, readable, writable)
case *directfsDentry:
// No update needed.
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - d.handleMu must be locked.
// - !d.isSynthetic().
func (d *dentry) closeHostFDs() {
// We can use RacyLoad() because d.handleMu is locked.
if d.readFD.RacyLoad() >= 0 {
_ = unix.Close(int(d.readFD.RacyLoad()))
}
if d.writeFD.RacyLoad() >= 0 && d.readFD.RacyLoad() != d.writeFD.RacyLoad() {
_ = unix.Close(int(d.writeFD.RacyLoad()))
}
d.readFD = atomicbitops.FromInt32(-1)
d.writeFD = atomicbitops.FromInt32(-1)
d.mmapFD = atomicbitops.FromInt32(-1)
switch dt := d.impl.(type) {
case *directfsDentry:
if dt.controlFD >= 0 {
_ = unix.Close(dt.controlFD)
dt.controlFD = -1
}
}
}
// updateMetadataLocked updates the dentry's metadata fields. The h parameter
// is optional. If it is not provided, an appropriate FD should be chosen to
// stat the remote file.
//
// Preconditions:
// - !d.isSynthetic().
// - d.metadataMu is locked.
//
// +checklocks:d.metadataMu
func (d *dentry) updateMetadataLocked(ctx context.Context, h handle) error {
// Need checklocksforce below because checklocks has no way of knowing that
// d.impl.(*dentryImpl).dentry == d. It can't know that the right metadataMu
// is already locked.
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.updateMetadataLocked(ctx, h) // +checklocksforce: acquired by precondition.
case *directfsDentry:
return dt.updateMetadataLocked(h) // +checklocksforce: acquired by precondition.
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - fs.renameMu is locked.
func (d *dentry) prepareSetStat(ctx context.Context, stat *linux.Statx) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
// Nothing to be done.
return nil
case *directfsDentry:
return dt.prepareSetStat(ctx, stat)
default:
panic("unknown dentry implementation")
}
}
// Precondition: fs.renameMu is locked if d is a socket.
func (d *dentry) chmod(ctx context.Context, mode uint16) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return chmod(ctx, dt.controlFD, mode)
case *directfsDentry:
return dt.chmod(ctx, mode)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - d.handleMu is locked.
// - fs.renameMu is locked.
func (d *dentry) setStatLocked(ctx context.Context, stat *linux.Statx) (uint32, error, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.SetStat(ctx, stat)
case *directfsDentry:
failureMask, failureErr := dt.setStatLocked(ctx, stat)
return failureMask, failureErr, nil
default:
panic("unknown dentry implementation")
}
}
// Precondition: d.handleMu must be locked.
func (d *dentry) destroyImpl(ctx context.Context) {
switch dt := d.impl.(type) {
case *lisafsDentry:
dt.destroy(ctx)
case *directfsDentry:
dt.destroy(ctx)
case nil: // synthetic dentry
default:
panic("unknown dentry implementation")
}
}
// Postcondition: Caller must do dentry caching appropriately.
//
// +checklocksread:d.opMu
func (d *dentry) getRemoteChild(ctx context.Context, name string) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.getRemoteChild(ctx, name)
case *directfsDentry:
return dt.getHostChild(name)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - fs.renameMu must be locked.
// - parent.opMu must be locked for reading.
// - parent.isDir().
// - !rp.Done() && rp.Component() is not "." or "..".
//
// Postcondition: The returned dentry is already cached appropriately.
//
// +checklocksread:d.opMu
func (d *dentry) getRemoteChildAndWalkPathLocked(ctx context.Context, rp resolvingPath, ds **[]*dentry) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.getRemoteChildAndWalkPathLocked(ctx, rp, ds)
case *directfsDentry:
// We need to check for races because opMu is read locked which allows
// concurrent walks to occur.
return d.fs.getRemoteChildLocked(ctx, d, rp.Component(), true /* checkForRace */, ds)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) listXattrImpl(ctx context.Context, size uint64) ([]string, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.ListXattr(ctx, size)
case *directfsDentry:
// Consistent with runsc/fsgofer.
return nil, linuxerr.EOPNOTSUPP
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) getXattrImpl(ctx context.Context, opts *vfs.GetXattrOptions) (string, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.GetXattr(ctx, opts.Name, opts.Size)
case *directfsDentry:
return dt.getXattr(opts.Name, opts.Size)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) setXattrImpl(ctx context.Context, opts *vfs.SetXattrOptions) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.SetXattr(ctx, opts.Name, opts.Value, opts.Flags)
case *directfsDentry:
// Consistent with runsc/fsgofer.
return linuxerr.EOPNOTSUPP
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) removeXattrImpl(ctx context.Context, name string) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.RemoveXattr(ctx, name)
case *directfsDentry:
// Consistent with runsc/fsgofer.
return linuxerr.EOPNOTSUPP
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) mknod(ctx context.Context, name string, creds *auth.Credentials, opts *vfs.MknodOptions) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.mknod(ctx, name, creds, opts)
case *directfsDentry:
return dt.mknod(ctx, name, creds, opts)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - !target.isSynthetic().
// - d.fs.renameMu must be locked.
func (d *dentry) link(ctx context.Context, target *dentry, name string) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.link(ctx, target.impl.(*lisafsDentry), name)
case *directfsDentry:
return dt.link(target.impl.(*directfsDentry), name)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) mkdir(ctx context.Context, name string, mode linux.FileMode, uid auth.KUID, gid auth.KGID) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.mkdir(ctx, name, mode, uid, gid)
case *directfsDentry:
return dt.mkdir(name, mode, uid, gid)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) symlink(ctx context.Context, name, target string, creds *auth.Credentials) (*dentry, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.symlink(ctx, name, target, creds)
case *directfsDentry:
return dt.symlink(name, target, creds)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) openCreate(ctx context.Context, name string, accessFlags uint32, mode linux.FileMode, uid auth.KUID, gid auth.KGID) (*dentry, handle, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.openCreate(ctx, name, accessFlags, mode, uid, gid)
case *directfsDentry:
return dt.openCreate(name, accessFlags, mode, uid, gid)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - d.isDir().
// - d.handleMu must be locked.
// - !d.isSynthetic().
func (d *dentry) getDirentsLocked(ctx context.Context, recordDirent func(name string, key inoKey, dType uint8)) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.getDirentsLocked(ctx, recordDirent)
case *directfsDentry:
return dt.getDirentsLocked(recordDirent)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) flush(ctx context.Context) error {
d.handleMu.RLock()
defer d.handleMu.RUnlock()
switch dt := d.impl.(type) {
case *lisafsDentry:
return flush(ctx, dt.writeFDLisa)
case *directfsDentry:
// Nothing to do here.
return nil
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) allocate(ctx context.Context, mode, offset, length uint64) error {
d.handleMu.RLock()
defer d.handleMu.RUnlock()
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.writeFDLisa.Allocate(ctx, mode, offset, length)
case *directfsDentry:
return unix.Fallocate(int(d.writeFD.RacyLoad()), uint32(mode), int64(offset), int64(length))
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - fs.renameMu is locked.
func (d *dentry) connect(ctx context.Context, sockType linux.SockType) (int, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.Connect(ctx, sockType)
case *directfsDentry:
return dt.connect(ctx, sockType)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) readlinkImpl(ctx context.Context) (string, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.ReadLinkAt(ctx)
case *directfsDentry:
return dt.readlink()
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) unlink(ctx context.Context, name string, flags uint32) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.UnlinkAt(ctx, name, flags)
case *directfsDentry:
return unix.Unlinkat(dt.controlFD, name, int(flags))
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) rename(ctx context.Context, oldName string, newParent *dentry, newName string) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.controlFD.RenameAt(ctx, oldName, newParent.impl.(*lisafsDentry).controlFD.ID(), newName)
case *directfsDentry:
return fsutil.RenameAt(dt.controlFD, oldName, newParent.impl.(*directfsDentry).controlFD, newName)
default:
panic("unknown dentry implementation")
}
}
// Precondition: !d.isSynthetic().
func (d *dentry) statfs(ctx context.Context) (linux.Statfs, error) {
switch dt := d.impl.(type) {
case *lisafsDentry:
return dt.statfs(ctx)
case *directfsDentry:
return dt.statfs()
default:
panic("unknown dentry implementation")
}
}
func (fs *filesystem) restoreRoot(ctx context.Context, opts *vfs.CompleteRestoreOptions) error {
rootInode, rootHostFD, err := fs.initClientAndGetRoot(ctx)
if err != nil {
return err
}
// The root is always non-synthetic.
switch dt := fs.root.impl.(type) {
case *lisafsDentry:
return dt.restoreFile(ctx, &rootInode, opts)
case *directfsDentry:
dt.controlFDLisa = fs.client.NewFD(rootInode.ControlFD)
return dt.restoreFile(ctx, rootHostFD, opts)
default:
panic("unknown dentry implementation")
}
}
// Preconditions:
// - !d.isSynthetic().
// - d.parent != nil and has been restored.
func (d *dentry) restoreFile(ctx context.Context, opts *vfs.CompleteRestoreOptions) error {
switch dt := d.impl.(type) {
case *lisafsDentry:
controlFD := d.parent.Load().impl.(*lisafsDentry).controlFD
inode, err := controlFD.Walk(ctx, d.name)
if err != nil {
if !dt.isDir() || !dt.forMountpoint {
return err
}
// Recreate directories that were created during volume mounting, since
// during restore we don't attempt to remount them.
inode, err = controlFD.MkdirAt(ctx, d.name, linux.FileMode(d.mode.Load()), lisafs.UID(d.uid.Load()), lisafs.GID(d.gid.Load()))
if err != nil {
return err
}
}
return dt.restoreFile(ctx, &inode, opts)
case *directfsDentry:
controlFD := d.parent.Load().impl.(*directfsDentry).controlFD
childFD, err := tryOpen(func(flags int) (int, error) {
n, err := unix.Openat(controlFD, d.name, flags, 0)
return n, err
})
if err != nil {
if !dt.isDir() || !dt.forMountpoint {
return err
}
// Recreate directories that were created during volume mounting, since
// during restore we don't attempt to remount them.
if err := unix.Mkdirat(controlFD, d.name, d.mode.Load()); err != nil {
return err
}
// Try again...
childFD, err = tryOpen(func(flags int) (int, error) {
return unix.Openat(controlFD, d.name, flags, 0)
})
if err != nil {
return err
}
}
return dt.restoreFile(ctx, childFD, opts)
default:
panic("unknown dentry implementation")
}
}
// doRevalidation calls into r.start's dentry implementation to perform
// revalidation on all the dentries contained in r.
//
// Preconditions:
// - fs.renameMu must be locked.
// - InteropModeShared is in effect.
func (r *revalidateState) doRevalidation(ctx context.Context, vfsObj *vfs.VirtualFilesystem, ds **[]*dentry) error {
// Skip synthetic dentries because there is no actual implementation that can
// be used to walk the remote filesystem. A start dentry cannot be replaced.
if r.start.isSynthetic() {
return nil
}
switch r.start.impl.(type) {
case *lisafsDentry:
return doRevalidationLisafs(ctx, vfsObj, r, ds)
case *directfsDentry:
return doRevalidationDirectfs(ctx, vfsObj, r, ds)
default:
panic("unknown dentry implementation")
}
}
|