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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
|
// 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 fuse
import (
"fmt"
gotime "time"
"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/hostarch"
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/marshal/primitive"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
"gvisor.dev/gvisor/pkg/sentry/kernel"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/kernel/time"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sync"
)
// +stateify savable
type fileHandle struct {
new bool
handle uint64
flags uint32
}
// inode implements kernfs.Inode.
//
// +stateify savable
type inode struct {
inodeRefs
kernfs.InodeNotAnonymous
kernfs.InodeNotSymlink
kernfs.InodeWatches
kernfs.OrderedChildren
kernfs.CachedMappable
// the owning filesystem. fs is immutable.
fs *filesystem
// nodeID is a unique id which identifies the inode between userspace and
// the sentry. generation is used to distinguish inodes in case of nodeID
// reuse. Both are immutable.
nodeID uint64
generation uint64
// entryTime is the time at which the entry must be revalidated. Reading
// entryTime requires either using entryTimeSeq and SeqAtomicLoadTime, or
// that attrMu is locked. Writing entryTime requires that attrMu is locked
// and that entryTimeSeq is in a writer critical section.
entryTimeSeq sync.SeqCount `state:"nosave"`
entryTime time.Time
// attrVersion is the version of the last attribute change.
attrVersion atomicbitops.Uint64
// attrTime is the time at which the attributes become invalid.
attrTime time.Time
// link is result of following a symbolic link.
link string
// fh caches the file handle returned by the server from a FUSE_CREATE request
// so we don't have to send a separate FUSE_OPEN request.
fh fileHandle
locks vfs.FileLocks
watches vfs.Watches
// attrMu protects the attributes of this inode.
attrMu sync.Mutex `state:"nosave"`
// +checklocks:attrMu
ino atomicbitops.Uint64 // Stat data, not accessed for path walking.
// +checklocks:attrMu
uid atomicbitops.Uint32 // auth.KUID, but stored as raw uint32 for sync/atomic.
// +checklocks:attrMu
gid atomicbitops.Uint32 // auth.KGID, but...
// +checklocks:attrMu
mode atomicbitops.Uint32 // File type and mode.
// Timestamps in nanoseconds from the unix epoch.
// +checklocks:attrMu
atime atomicbitops.Int64
// +checklocks:attrMu
mtime atomicbitops.Int64
// +checklocks:attrMu
ctime atomicbitops.Int64
// +checklocks:attrMu
size atomicbitops.Uint64
// nlink counts the number of hard links to this inode. It's updated and
// accessed used atomic operations but not protected by attrMu.
nlink atomicbitops.Uint32
// +checklocks:attrMu
blockSize atomicbitops.Uint32 // 0 if unknown.
}
func blockerFromContext(ctx context.Context) context.Blocker {
kernelTask := kernel.TaskFromContext(ctx)
if kernelTask == nil {
return ctx
}
return kernelTask
}
func pidFromContext(ctx context.Context) uint32 {
kernelTask := kernel.TaskFromContext(ctx)
if kernelTask == nil {
return 0
}
return uint32(kernelTask.ThreadID())
}
func umaskFromContext(ctx context.Context) uint32 {
kernelTask := kernel.TaskFromContext(ctx)
umask := uint32(0)
if kernelTask != nil {
umask = uint32(kernelTask.FSContext().Umask())
}
return umask
}
func (i *inode) Mode() linux.FileMode {
i.attrMu.Lock()
defer i.attrMu.Unlock()
return i.filemode()
}
func (i *inode) UID() auth.KUID {
i.attrMu.Lock()
defer i.attrMu.Unlock()
return auth.KUID(i.uid.Load())
}
func (i *inode) GID() auth.KGID {
i.attrMu.Lock()
defer i.attrMu.Unlock()
return auth.KGID(i.gid.Load())
}
// +checklocks:i.attrMu
func (i *inode) filemode() linux.FileMode {
return linux.FileMode(i.mode.Load())
}
// touchCMTime updates the ctime and mtime attributes to be the current time.
//
// +checklocks:i.attrMu
func (i *inode) touchCMtime() {
now := i.fs.clock.Now().Nanoseconds()
i.mtime.Store(now)
i.ctime.Store(now)
}
// touchAtime updates the atime attribute to be the current time.
//
// +checklocks:i.attrMu
func (i *inode) touchAtime() {
i.atime.Store(i.fs.clock.Now().Nanoseconds())
}
// +checklocks:i.attrMu
func (i *inode) init(creds *auth.Credentials, devMajor, devMinor uint32, nodeid uint64, mode linux.FileMode, nlink uint32) {
if mode.FileType() == 0 {
panic(fmt.Sprintf("No file type specified in 'mode' for InodeAttrs.Init(): mode=0%o", mode))
}
i.nodeID = nodeid
i.ino.Store(nodeid)
i.mode.Store(uint32(mode))
i.uid.Store(uint32(creds.EffectiveKUID))
i.gid.Store(uint32(creds.EffectiveKGID))
i.nlink.Store(nlink)
i.blockSize.Store(hostarch.PageSize)
now := i.fs.clock.Now().Nanoseconds()
i.atime.Store(now)
i.mtime.Store(now)
i.ctime.Store(now)
}
// +checklocks:i.attrMu
func (i *inode) updateEntryTime(entrySec, entryNSec int64) {
entryTime := time.FromTimespec(linux.Timespec{Sec: entrySec, Nsec: entryNSec})
SeqAtomicStoreTime(&i.entryTimeSeq, &i.entryTime, i.fs.clock.Now().AddTime(entryTime))
}
// CheckPermissions implements kernfs.Inode.CheckPermissions.
func (i *inode) CheckPermissions(ctx context.Context, creds *auth.Credentials, ats vfs.AccessTypes) error {
// Since FUSE operations are ultimately backed by a userspace process (the
// fuse daemon), allowing a process to call into fusefs grants the daemon
// ptrace-like capabilities over the calling process. Because of this, by
// default FUSE only allows the mount owner to interact with the
// filesystem. This explicitly excludes setuid/setgid processes.
//
// This behaviour can be overridden with the 'allow_other' mount option.
//
// See fs/fuse/dir.c:fuse_allow_current_process() in Linux.
if !i.fs.opts.allowOther {
if creds.RealKUID != i.fs.opts.uid ||
creds.EffectiveKUID != i.fs.opts.uid ||
creds.SavedKUID != i.fs.opts.uid ||
creds.RealKGID != i.fs.opts.gid ||
creds.EffectiveKGID != i.fs.opts.gid ||
creds.SavedKGID != i.fs.opts.gid {
return linuxerr.EACCES
}
}
// By default, fusefs delegates all permission checks to the server.
// However, standard unix permission checks can be enabled with the
// default_permissions mount option.
i.attrMu.Lock()
defer i.attrMu.Unlock()
refreshed := false
opts := vfs.StatOptions{Mask: linux.STATX_MODE | linux.STATX_UID | linux.STATX_GID}
if i.fs.opts.defaultPermissions || (ats.MayExec() && i.filemode().FileType() == linux.S_IFREG) {
if i.fs.clock.Now().After(i.attrTime) {
refreshed = true
if _, err := i.getAttr(ctx, i.fs.VFSFilesystem(), opts, 0, 0); err != nil {
return err
}
}
}
if i.fs.opts.defaultPermissions || (ats.MayExec() && i.filemode().FileType() == linux.S_IFREG) {
err := vfs.GenericCheckPermissions(creds, ats, linux.FileMode(i.mode.Load()), auth.KUID(i.uid.Load()), auth.KGID(i.gid.Load()))
if linuxerr.Equals(linuxerr.EACCES, err) && !refreshed {
if _, err := i.getAttr(ctx, i.fs.VFSFilesystem(), opts, 0, 0); err != nil {
return err
}
return vfs.GenericCheckPermissions(creds, ats, linux.FileMode(i.mode.Load()), auth.KUID(i.uid.Load()), auth.KGID(i.gid.Load()))
}
return err
} else if ats.MayRead() || ats.MayWrite() || ats.MayExec() {
in := linux.FUSEAccessIn{Mask: uint32(ats)}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, linux.FUSE_ACCESS, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return err
}
return res.Error()
}
return nil
}
// Open implements kernfs.Inode.Open.
func (i *inode) Open(ctx context.Context, rp *vfs.ResolvingPath, d *kernfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
opts.Flags &= linux.O_ACCMODE | linux.O_CREAT | linux.O_EXCL | linux.O_TRUNC |
linux.O_DIRECTORY | linux.O_NOFOLLOW | linux.O_NONBLOCK | linux.O_NOCTTY |
linux.O_APPEND | linux.O_DIRECT
i.attrMu.Lock()
defer i.attrMu.Unlock()
if opts.Flags&linux.O_LARGEFILE == 0 && i.size.Load() > linux.MAX_NON_LFS {
return nil, linuxerr.EOVERFLOW
}
var (
fd *fileDescription
fdImpl vfs.FileDescriptionImpl
opcode linux.FUSEOpcode
)
switch i.filemode().FileType() {
case linux.S_IFREG:
regularFD := ®ularFileFD{}
fd = &(regularFD.fileDescription)
fdImpl = regularFD
opcode = linux.FUSE_OPEN
case linux.S_IFDIR:
if opts.Flags&linux.O_CREAT != 0 {
return nil, linuxerr.EISDIR
}
if ats := vfs.AccessTypesForOpenFlags(&opts); ats.MayWrite() {
return nil, linuxerr.EISDIR
}
if opts.Flags&linux.O_DIRECT != 0 {
return nil, linuxerr.EINVAL
}
directoryFD := &directoryFD{}
fd = &(directoryFD.fileDescription)
fdImpl = directoryFD
opcode = linux.FUSE_OPENDIR
case linux.S_IFLNK:
return nil, linuxerr.ELOOP
}
fd.LockFD.Init(&i.locks)
// FOPEN_KEEP_CACHE is the default flag for noOpen.
fd.OpenFlag = linux.FOPEN_KEEP_CACHE
truncateRegFile := opts.Flags&linux.O_TRUNC != 0 && i.filemode().FileType() == linux.S_IFREG
if truncateRegFile && (i.fh.new || !i.fs.conn.atomicOTrunc) {
// If the regular file needs to be truncated, but the connection doesn't
// support O_TRUNC or if we are optimizing away the Open RPC, then manually
// truncate the file *before* Open. As per libfuse, "If [atomic O_TRUNC is]
// disabled, and an application specifies O_TRUNC, fuse first calls
// truncate() and then open() with O_TRUNC filtered out.".
opts := vfs.SetStatOptions{Stat: linux.Statx{Size: 0, Mask: linux.STATX_SIZE}}
if err := i.setAttr(ctx, i.fs.VFSFilesystem(), auth.CredentialsFromContext(ctx), opts, fhOptions{useFh: false}); err != nil {
return nil, err
}
}
if i.fh.new {
fd.OpenFlag = i.fh.flags
fd.Fh = i.fh.handle
i.fh.new = false
// Only send an open request when the FUSE server supports open or is
// opening a directory.
} else if !i.fs.conn.noOpen || i.filemode().IsDir() {
in := linux.FUSEOpenIn{Flags: opts.Flags & ^uint32(linux.O_CREAT|linux.O_EXCL|linux.O_NOCTTY)}
// Clear O_TRUNC if the server doesn't support it.
if !i.fs.conn.atomicOTrunc {
in.Flags &= ^uint32(linux.O_TRUNC)
}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, opcode, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return nil, err
}
if err := res.Error(); err != nil {
if linuxerr.Equals(linuxerr.ENOSYS, err) && !i.filemode().IsDir() {
i.fs.conn.noOpen = true
} else {
return nil, err
}
} else {
out := linux.FUSEOpenOut{}
if err := res.UnmarshalPayload(&out); err != nil {
return nil, err
}
fd.OpenFlag = out.OpenFlag
fd.Fh = out.Fh
// Open was successful. Update inode's size if atomicOTrunc && O_TRUNC.
if truncateRegFile && i.fs.conn.atomicOTrunc {
i.fs.conn.mu.Lock()
i.attrVersion.Store(i.fs.conn.attributeVersion.Add(1))
i.fs.conn.mu.Unlock()
i.size.Store(0)
i.touchCMtime()
}
}
}
if i.filemode().IsDir() {
fd.OpenFlag &= ^uint32(linux.FOPEN_DIRECT_IO)
}
// TODO(gvisor.dev/issue/3234): invalidate mmap after implemented it for FUSE Inode
fd.DirectIO = fd.OpenFlag&linux.FOPEN_DIRECT_IO != 0
fdOptions := &vfs.FileDescriptionOptions{}
if fd.OpenFlag&linux.FOPEN_NONSEEKABLE != 0 {
fdOptions.DenyPRead = true
fdOptions.DenyPWrite = true
fd.Nonseekable = true
}
if err := fd.vfsfd.Init(fdImpl, opts.Flags, rp.Mount(), d.VFSDentry(), fdOptions); err != nil {
return nil, err
}
return &fd.vfsfd, nil
}
func (i *inode) Valid(ctx context.Context, parent *kernfs.Dentry, name string) bool {
now := i.fs.clock.Now()
if entryTime := SeqAtomicLoadTime(&i.entryTimeSeq, &i.entryTime); entryTime.After(now) {
return true
}
i.attrMu.Lock()
defer i.attrMu.Unlock()
if i.entryTime.After(now) {
return true
}
in := linux.FUSELookupIn{Name: linux.CString(name)}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), parent.Inode().(*inode).nodeID, linux.FUSE_LOOKUP, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return false
}
if res.Error() != nil {
return false
}
var out linux.FUSEEntryOut
if res.UnmarshalPayload(&out) != nil {
return false
}
if i.nodeID != out.NodeID {
return false
}
// Don't enforce fuse_invalid_attr() => fuse_valid_type(),
// fuse_valid_size() since inode.updateAttrs() and its callers
// don't. But do enforce fuse_stale_inode():
if i.generation != out.Generation {
return false
}
if (i.mode.RacyLoad()^out.Attr.Mode)&linux.S_IFMT != 0 {
return false
}
i.updateEntryTime(int64(out.EntryValid), int64(out.EntryValidNSec))
return true
}
// Lookup implements kernfs.Inode.Lookup.
func (i *inode) Lookup(ctx context.Context, name string) (kernfs.Inode, error) {
in := linux.FUSELookupIn{Name: linux.CString(name)}
return i.newEntry(ctx, name, 0, linux.FUSE_LOOKUP, &in)
}
// Keep implements kernfs.Inode.Keep.
func (i *inode) Keep() bool {
// Return true so that kernfs keeps the new dentry pointing to this
// inode in the dentry tree. This is needed because inodes created via
// Lookup are not temporary. They might refer to existing files on server
// that can be Unlink'd/Rmdir'd.
return true
}
// IterDirents implements kernfs.Inode.IterDirents.
func (*inode) IterDirents(ctx context.Context, mnt *vfs.Mount, callback vfs.IterDirentsCallback, offset, relOffset int64) (int64, error) {
return offset, nil
}
// NewFile implements kernfs.Inode.NewFile.
func (i *inode) NewFile(ctx context.Context, name string, opts vfs.OpenOptions) (kernfs.Inode, error) {
opts.Flags &= linux.O_ACCMODE | linux.O_CREAT | linux.O_EXCL | linux.O_TRUNC |
linux.O_DIRECTORY | linux.O_NOFOLLOW | linux.O_NONBLOCK | linux.O_NOCTTY
in := linux.FUSECreateIn{
CreateMeta: linux.FUSECreateMeta{
Flags: opts.Flags,
Mode: uint32(opts.Mode) | linux.S_IFREG,
Umask: umaskFromContext(ctx),
},
Name: linux.CString(name),
}
return i.newEntry(ctx, name, linux.S_IFREG, linux.FUSE_CREATE, &in)
}
// NewNode implements kernfs.Inode.NewNode.
func (i *inode) NewNode(ctx context.Context, name string, opts vfs.MknodOptions) (kernfs.Inode, error) {
in := linux.FUSEMknodIn{
MknodMeta: linux.FUSEMknodMeta{
Mode: uint32(opts.Mode),
Rdev: linux.MakeDeviceID(uint16(opts.DevMajor), opts.DevMinor),
Umask: umaskFromContext(ctx),
},
Name: linux.CString(name),
}
return i.newEntry(ctx, name, opts.Mode.FileType(), linux.FUSE_MKNOD, &in)
}
// NewSymlink implements kernfs.Inode.NewSymlink.
func (i *inode) NewSymlink(ctx context.Context, name, target string) (kernfs.Inode, error) {
in := linux.FUSESymlinkIn{
Name: linux.CString(name),
Target: linux.CString(target),
}
return i.newEntry(ctx, name, linux.S_IFLNK, linux.FUSE_SYMLINK, &in)
}
// NewLink implements kernfs.Inode.NewLink.
func (i *inode) NewLink(ctx context.Context, name string, target kernfs.Inode) (kernfs.Inode, error) {
targetInode := target.(*inode)
in := linux.FUSELinkIn{
OldNodeID: primitive.Uint64(targetInode.nodeID),
Name: linux.CString(name),
}
return i.newEntry(ctx, name, targetInode.Mode().FileType(), linux.FUSE_LINK, &in)
}
// Unlink implements kernfs.Inode.Unlink.
func (i *inode) Unlink(ctx context.Context, name string, child kernfs.Inode) error {
in := linux.FUSEUnlinkIn{Name: linux.CString(name)}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, linux.FUSE_UNLINK, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return err
}
// only return error, discard res.
return res.Error()
}
// NewDir implements kernfs.Inode.NewDir.
func (i *inode) NewDir(ctx context.Context, name string, opts vfs.MkdirOptions) (kernfs.Inode, error) {
in := linux.FUSEMkdirIn{
MkdirMeta: linux.FUSEMkdirMeta{
Mode: uint32(opts.Mode),
Umask: umaskFromContext(ctx),
},
Name: linux.CString(name),
}
return i.newEntry(ctx, name, linux.S_IFDIR, linux.FUSE_MKDIR, &in)
}
// RmDir implements kernfs.Inode.RmDir.
func (i *inode) RmDir(ctx context.Context, name string, child kernfs.Inode) error {
in := linux.FUSERmDirIn{Name: linux.CString(name)}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, linux.FUSE_RMDIR, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return err
}
return res.Error()
}
// Rename implements kernfs.Inode.Rename.
func (i *inode) Rename(ctx context.Context, oldname, newname string, child, dstDir kernfs.Inode) error {
dstDirInode := dstDir.(*inode)
in := linux.FUSERenameIn{
Newdir: primitive.Uint64(dstDirInode.nodeID),
Oldname: linux.CString(oldname),
Newname: linux.CString(newname),
}
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, linux.FUSE_RENAME, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return err
}
return res.Error()
}
// newEntry calls FUSE server for entry creation and allocates corresponding
// entry according to response. Shared by FUSE_MKNOD, FUSE_MKDIR, FUSE_SYMLINK,
// FUSE_LINK and FUSE_LOOKUP.
func (i *inode) newEntry(ctx context.Context, name string, fileType linux.FileMode, opcode linux.FUSEOpcode, payload marshal.Marshallable) (kernfs.Inode, error) {
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, opcode, payload)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return nil, err
}
if err := res.Error(); err != nil {
return nil, err
}
out := linux.FUSECreateOut{}
if opcode == linux.FUSE_CREATE {
if err := res.UnmarshalPayload(&out); err != nil {
return nil, err
}
} else {
if err := res.UnmarshalPayload(&out.FUSEEntryOut); err != nil {
return nil, err
}
}
if opcode != linux.FUSE_LOOKUP && ((out.Attr.Mode&linux.S_IFMT)^uint32(fileType) != 0 || out.NodeID == 0 || out.NodeID == linux.FUSE_ROOT_ID) {
return nil, linuxerr.EIO
}
child := i.fs.newInode(ctx, out.FUSEEntryOut)
if opcode == linux.FUSE_CREATE {
// File handler is returned by fuse server at a time of file create.
// Save it temporary in a created child, so Open could return it when invoked
// to be sure after fh is consumed reset 'isNewFh' flag of inode
childI, ok := child.(*inode)
if ok {
childI.fh.new = true
childI.fh.handle = out.FUSEOpenOut.Fh
childI.fh.flags = out.FUSEOpenOut.OpenFlag
}
}
return child, nil
}
// Getlink implements kernfs.Inode.Getlink.
func (i *inode) Getlink(ctx context.Context, mnt *vfs.Mount) (vfs.VirtualDentry, string, error) {
path, err := i.Readlink(ctx, mnt)
return vfs.VirtualDentry{}, path, err
}
// Readlink implements kernfs.Inode.Readlink.
func (i *inode) Readlink(ctx context.Context, mnt *vfs.Mount) (string, error) {
i.attrMu.Lock()
defer i.attrMu.Unlock()
if i.filemode().FileType()&linux.S_IFLNK == 0 {
return "", linuxerr.EINVAL
}
if len(i.link) == 0 {
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID, linux.FUSE_READLINK, &linux.FUSEEmptyIn{})
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return "", err
}
i.link = string(res.data[res.hdr.SizeBytes():])
if !mnt.Options().ReadOnly {
i.attrTime = time.ZeroTime
}
}
return i.link, nil
}
// getFUSEAttr returns a linux.FUSEAttr of this inode stored in local cache.
//
// +checklocks:i.attrMu
func (i *inode) getFUSEAttr() linux.FUSEAttr {
ns := gotime.Second.Nanoseconds()
return linux.FUSEAttr{
Ino: i.nodeID,
UID: i.uid.Load(),
GID: i.gid.Load(),
Size: i.size.Load(),
Mode: uint32(i.filemode()),
BlkSize: i.blockSize.Load(),
Atime: uint64(i.atime.Load() / ns),
Mtime: uint64(i.mtime.Load() / ns),
Ctime: uint64(i.ctime.Load() / ns),
AtimeNsec: uint32(i.atime.Load() % ns),
MtimeNsec: uint32(i.mtime.Load() % ns),
CtimeNsec: uint32(i.ctime.Load() % ns),
Nlink: i.nlink.Load(),
}
}
// statFromFUSEAttr makes attributes from linux.FUSEAttr to linux.Statx. The
// opts.Sync attribute is ignored since the synchronization is handled by the
// FUSE server.
func statFromFUSEAttr(attr linux.FUSEAttr, mask, devMinor uint32) linux.Statx {
var stat linux.Statx
stat.Blksize = attr.BlkSize
stat.DevMajor, stat.DevMinor = linux.UNNAMED_MAJOR, devMinor
rdevMajor, rdevMinor := linux.DecodeDeviceID(attr.Rdev)
stat.RdevMajor, stat.RdevMinor = uint32(rdevMajor), rdevMinor
if mask&linux.STATX_MODE != 0 {
stat.Mode = uint16(attr.Mode)
}
if mask&linux.STATX_NLINK != 0 {
stat.Nlink = attr.Nlink
}
if mask&linux.STATX_UID != 0 {
stat.UID = attr.UID
}
if mask&linux.STATX_GID != 0 {
stat.GID = attr.GID
}
if mask&linux.STATX_ATIME != 0 {
stat.Atime = linux.StatxTimestamp{
Sec: int64(attr.Atime),
Nsec: attr.AtimeNsec,
}
}
if mask&linux.STATX_MTIME != 0 {
stat.Mtime = linux.StatxTimestamp{
Sec: int64(attr.Mtime),
Nsec: attr.MtimeNsec,
}
}
if mask&linux.STATX_CTIME != 0 {
stat.Ctime = linux.StatxTimestamp{
Sec: int64(attr.Ctime),
Nsec: attr.CtimeNsec,
}
}
if mask&linux.STATX_INO != 0 {
stat.Ino = attr.Ino
}
if mask&linux.STATX_SIZE != 0 {
stat.Size = attr.Size
}
if mask&linux.STATX_BLOCKS != 0 {
stat.Blocks = attr.Blocks
}
return stat
}
// getAttr gets the attribute of this inode by issuing a FUSE_GETATTR request
// or read from local cache. It updates the corresponding attributes if
// necessary.
//
// +checklocks:i.attrMu
func (i *inode) getAttr(ctx context.Context, fs *vfs.Filesystem, opts vfs.StatOptions, flags uint32, fh uint64) (linux.FUSEAttr, error) {
// TODO(gvisor.dev/issue/3679): send the request only if
// - invalid local cache for fields specified in the opts.Mask
// - forced update
// - i.attributeTime expired
// If local cache is still valid, return local cache.
// Currently we always send a request,
// and we always set the metadata with the new result,
// unless attributeVersion has changed.
creds := auth.CredentialsFromContext(ctx)
in := linux.FUSEGetAttrIn{
GetAttrFlags: flags,
Fh: fh,
}
req := i.fs.conn.NewRequest(creds, pidFromContext(ctx), i.nodeID, linux.FUSE_GETATTR, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return linux.FUSEAttr{}, err
}
if err := res.Error(); err != nil {
return linux.FUSEAttr{}, err
}
var out linux.FUSEAttrOut
if err := res.UnmarshalPayload(&out); err != nil {
return linux.FUSEAttr{}, err
}
// Local version is newer, return the local one.
i.fs.conn.mu.Lock()
attributeVersion := i.fs.conn.attributeVersion.Load()
if attributeVersion != 0 && i.attrVersion.Load() > attributeVersion {
i.fs.conn.mu.Unlock()
return i.getFUSEAttr(), nil
}
i.fs.conn.mu.Unlock()
i.updateAttrs(out.Attr, int64(out.AttrValid), int64(out.AttrValidNsec))
return out.Attr, nil
}
// reviseAttr attempts to update the attributes for internal purposes
// by calling getAttr with a pre-specified mask.
// Used by read, write, lseek.
//
// +checklocks:i.attrMu
func (i *inode) reviseAttr(ctx context.Context, flags uint32, fh uint64) error {
// Never need atime for internal purposes.
_, err := i.getAttr(ctx, i.fs.VFSFilesystem(), vfs.StatOptions{
Mask: linux.STATX_BASIC_STATS &^ linux.STATX_ATIME,
}, flags, fh)
return err
}
// Stat implements kernfs.Inode.Stat.
func (i *inode) Stat(ctx context.Context, fs *vfs.Filesystem, opts vfs.StatOptions) (linux.Statx, error) {
i.attrMu.Lock()
defer i.attrMu.Unlock()
attr, err := i.getAttr(ctx, fs, opts, 0, 0)
if err != nil {
return linux.Statx{}, err
}
return statFromFUSEAttr(attr, opts.Mask, i.fs.devMinor), nil
}
// DecRef implements kernfs.Inode.DecRef.
func (i *inode) DecRef(ctx context.Context) {
i.inodeRefs.DecRef(func() { i.Destroy(ctx) })
}
// StatFS implements kernfs.Inode.StatFS.
func (i *inode) StatFS(ctx context.Context, fs *vfs.Filesystem) (linux.Statfs, error) {
req := i.fs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), i.nodeID,
linux.FUSE_STATFS, &linux.FUSEEmptyIn{},
)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return linux.Statfs{}, err
}
if err := res.Error(); err != nil {
return linux.Statfs{}, err
}
var out linux.FUSEStatfsOut
if err := res.UnmarshalPayload(&out); err != nil {
return linux.Statfs{}, err
}
return linux.Statfs{
Type: linux.FUSE_SUPER_MAGIC,
Blocks: uint64(out.Blocks),
BlocksFree: out.BlocksFree,
BlocksAvailable: out.BlocksAvailable,
Files: out.Files,
FilesFree: out.FilesFree,
BlockSize: int64(out.BlockSize),
NameLength: uint64(out.NameLength),
FragmentSize: int64(out.FragmentSize),
}, nil
}
// fattrMaskFromStats converts vfs.SetStatOptions.Stat.Mask to linux stats mask
// aligned with the attribute mask defined in include/linux/fs.h.
func fattrMaskFromStats(mask uint32) uint32 {
var fuseAttrMask uint32
maskMap := map[uint32]uint32{
linux.STATX_MODE: linux.FATTR_MODE,
linux.STATX_UID: linux.FATTR_UID,
linux.STATX_GID: linux.FATTR_GID,
linux.STATX_SIZE: linux.FATTR_SIZE,
linux.STATX_ATIME: linux.FATTR_ATIME,
linux.STATX_MTIME: linux.FATTR_MTIME,
linux.STATX_CTIME: linux.FATTR_CTIME,
}
for statxMask, fattrMask := range maskMap {
if mask&statxMask != 0 {
fuseAttrMask |= fattrMask
}
}
return fuseAttrMask
}
// SetStat implements kernfs.Inode.SetStat.
func (i *inode) SetStat(ctx context.Context, fs *vfs.Filesystem, creds *auth.Credentials, opts vfs.SetStatOptions) error {
i.attrMu.Lock()
defer i.attrMu.Unlock()
if err := vfs.CheckSetStat(ctx, creds, &opts, i.filemode(), auth.KUID(i.uid.Load()), auth.KGID(i.gid.Load())); err != nil {
return err
}
if opts.Stat.Mask == 0 {
return nil
}
return i.setAttr(ctx, fs, creds, opts, fhOptions{useFh: false})
}
type fhOptions struct {
useFh bool
fh uint64
}
// +checklocks:i.attrMu
func (i *inode) setAttr(ctx context.Context, fs *vfs.Filesystem, creds *auth.Credentials, opts vfs.SetStatOptions, fhOpts fhOptions) error {
// We should retain the original file type when assigning a new mode.
fattrMask := fattrMaskFromStats(opts.Stat.Mask)
if fhOpts.useFh {
fattrMask |= linux.FATTR_FH
}
if opts.Stat.Mask&linux.STATX_ATIME != 0 && opts.Stat.Atime.Nsec == linux.UTIME_NOW {
fattrMask |= linux.FATTR_ATIME_NOW
}
if opts.Stat.Mask&linux.STATX_MTIME != 0 && opts.Stat.Mtime.Nsec == linux.UTIME_NOW {
fattrMask |= linux.FATTR_ATIME_NOW
}
in := linux.FUSESetAttrIn{
Valid: fattrMask,
Fh: fhOpts.fh,
Size: opts.Stat.Size,
Atime: uint64(opts.Stat.Atime.Sec),
Mtime: uint64(opts.Stat.Mtime.Sec),
Ctime: uint64(opts.Stat.Ctime.Sec),
AtimeNsec: opts.Stat.Atime.Nsec,
MtimeNsec: opts.Stat.Mtime.Nsec,
CtimeNsec: opts.Stat.Ctime.Nsec,
Mode: uint32(uint16(i.filemode().FileType()) | opts.Stat.Mode),
UID: opts.Stat.UID,
GID: opts.Stat.GID,
}
req := i.fs.conn.NewRequest(creds, pidFromContext(ctx), i.nodeID, linux.FUSE_SETATTR, &in)
res, err := i.fs.conn.Call(ctx, req)
if err != nil {
return err
}
if err := res.Error(); err != nil {
return err
}
out := linux.FUSEAttrOut{}
if err := res.UnmarshalPayload(&out); err != nil {
return err
}
i.updateAttrs(out.Attr, int64(out.AttrValid), int64(out.AttrValidNsec))
return nil
}
// +checklocks:i.attrMu
func (i *inode) updateAttrs(attr linux.FUSEAttr, validSec, validNSec int64) {
i.fs.conn.mu.Lock()
i.attrVersion.Store(i.fs.conn.attributeVersion.Add(1))
i.fs.conn.mu.Unlock()
i.attrTime = i.fs.clock.Now().AddTime(time.FromTimespec(linux.Timespec{Sec: validSec, Nsec: validNSec}))
i.ino.Store(attr.Ino)
i.mode.Store((attr.Mode & 07777) | (i.mode.Load() & linux.S_IFMT))
i.uid.Store(attr.UID)
i.gid.Store(attr.GID)
i.atime.Store(attr.ATimeNsec())
i.mtime.Store(attr.MTimeNsec())
i.ctime.Store(attr.CTimeNsec())
i.size.Store(attr.Size)
i.nlink.Store(attr.Nlink)
if !i.fs.opts.defaultPermissions {
i.mode.Store(i.mode.Load() & ^uint32(linux.S_ISVTX))
}
}
|