101 Commits
Author SHA1 Message Date
Ayush RanjanandgVisor bot ab9d8455d4 Re-use the same device file inode in tmpfs for overlay whiteout files.
When using overlayfs with tmpfs as the upper layer (common case), depending on
the application, a lot of whiteouts can be created. This leads to a lot of
memory allocation because new dentry and inode structs need to be allocated for
each whiteout. With this change, we at least avoid the inode allocations.

This is analogous with what Linux does. See fs/overlayfs/ovl_entry.h:ovl_fs's
field `whiteout` with comment "Shared whiteout cache".

PiperOrigin-RevId: 704897805
2024-12-10 17:11:22 -08:00
Jamie LiuandgVisor bot f66f0e235a Fix memmap.MappingIdentity.Device/InodeID() lock ordering.
For vfs.FileDescriptions for which FileDescriptionOptions.UseDentryMetadata is
true, memmap.MappingIdentity.Device/InodeID() => FileDescription.Stat() =>
FilesystemImpl.StatAt() takes fsimpl locks for path traversal, which violates
the lock ordering and is unnecessary since no path is being traversed. Fix this
by carving out a special case where FilesystemImpl.Stat() (and
FileDescriptionImpl.Stat()) are required to meet the lock ordering requirements
of memmap.MappingIdentity.Device/InodeID(), and implement that special case by
skipping path traversal (and gofer revalidation) locks when not required.

PiperOrigin-RevId: 698608924
2024-11-20 19:30:02 -08:00
Andrei VaginandgVisor bot a92fc7b8c9 overlay: remove dirInoCache entries from RmdirAt()
Right now, entries are never removed from dirInoCache and if someone creates
and deletes directories in a loop, they observe memory leaks.

PiperOrigin-RevId: 698195540
2024-11-19 17:14:01 -08:00
Jamie LiuandgVisor bot 336dc85043 vfs: use ancestryMu in implementations of DentryImpl.InotifyWithParent
This lock was introduced in cl/696713993 and only protects parent/name for all
dentries, which is all that's required by InotifyWithParent().

PiperOrigin-RevId: 697016824
2024-11-15 15:27:27 -08:00
Jamie LiuandgVisor bot 0659b6035a vfs: establish lock ordering for FilesystemImpl.PrependPath
- Add type parameter Filesystem to vfs/genericfstree, which is required to
  provide `ancestryMu sync.RWMutex`, and add such a RWMutex to all FSImpls that
  use genericfstree.

- Modify genericfstree.PrependPath() and genericfstree.IsDescendant() to use
  ancestryMu to ensure atomicity. For callers of genericfstree.PrependPath(),
  this means that (broader) FSImpl locks no longer need to be held during the
  call. For callers of genericfstree.IsDescendant(), this means that we can
  remove documentation warnings about its non-atomicity.

- Minor cleanup: Remove useless variable `start`, which is always 0, from
  MM.ReadMaps/SmapsDataInto().

PiperOrigin-RevId: 696713993
2024-11-14 18:14:27 -08:00
Ayush RanjanandgVisor bot 7e395bbbd4 Plumb restore context to load*() methods.
This allows for external information to be passed to restore code.
Similar to c087777e37 ("Plumb restore context to afterLoad()").

Updates #1956.

PiperOrigin-RevId: 614125262
2024-03-08 20:28:02 -08:00
Fabricio VoznikaandgVisor bot c087777e37 Plumb restore context to afterLoad()
This allows for external information to be passed to restore code, like
host FDs to be remapped.

Updates #1956

PiperOrigin-RevId: 612540749
2024-03-04 12:21:50 -08:00
Jing ChenandgVisor bot be48200c0e Re-order loads in BUILD files to make transformations reversible in Copybara.
PiperOrigin-RevId: 598898756
2024-01-16 11:21:40 -08:00
Andrei VaginandgVisor bot a8a46b4c7f Only tmpfs is allowed on an upper level of overlayfs
All other file systems don't support whiteouts and trusted.overlay attributes.
This restriction is applied only from mounts created from inside the sandbox.

We need this change to support applications such as Docker that is trying to
construct overlay mounts and falls back to other options if it fails.

PiperOrigin-RevId: 579343274
2023-11-03 17:09:03 -07:00
Andrei Vagin 5f4abad306 Fix a few typos
It is an idea of running codespell as part of our presubmit checks.
Before enabling it for new changes, let's fix what it has found.

Signed-off-by: Andrei Vagin <avagin@gmail.com>
2023-10-25 12:13:42 -07:00
Lucas ManningandgVisor bot 9d5198a863 Add IsDescendant to FilesystemImpl.
IsDescendant gives the VFS layer an easy way to check if a dentry is a
descendant of another, which is important for some mounting procedures.

This method does not take any locks when accessing the parent, so parent
fields need to switch to be atomic to avoid data races.

PiperOrigin-RevId: 570454446
2023-10-03 11:57:01 -07:00
Lucas ManningandgVisor bot c74f5866cb Fix circular lock that can happen during unlink.
Reported-by: syzbot+d93d90350a33fbd2c8b9@syzkaller.appspotmail.com
PiperOrigin-RevId: 570420492
2023-10-03 10:07:40 -07:00
Andrei VaginandgVisor bot 1c8e91d04c fs/overlay: create the "work" directory in workdir
Linux creates the "work" directory in `workdir`.  Docker calls chown on it and
fails if it doesn't exist.

PiperOrigin-RevId: 567053404
2023-09-20 12:58:03 -07:00
Ayush RanjanandgVisor bot ee37961d4a Mark newly created directories in merged parents in overlay as opaque.
This is an optimization that will prevent needless lookups on lower layers for
directories. This is analogous to the Linux commit 97c684cc9110 ("ovl: create
directories inside merged parent opaque").

If mkdir(2) does succeed, then none of the lower layers have a file at that
position, otherwise mkdir(2) would have failed with EEXIST.

Changes on the lower layer underneath a new upper layer directory are not
visible. See the following for example:
```
$ sudo mount -t overlay -o lowerdir=lower,upperdir=upper,workdir=workdir none overlay
$ mkdir -p overlay/dir/dir1
$ mkdir -p lower/dir/dir2
$ ls overlay/dir/
dir1
```

Benchmarking shows that this change cuts ABSL build time by 9.5%!
```
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
                                                  │ /tmp/benchout.runsc │     /tmp/benchout.runsc-opt      │
                                                  │       sec/op        │   sec/op    vs base              │
BuildABSL/page_cache.clean/filesystem.bindfs-8              71.16 ± 12%   64.40 ± 3%  -9.50% (p=0.003 n=8)
BuildGRPC/page_cache.clean/filesystem.bindfs-8              418.2 ±  1%   419.9 ± 1%       ~ (p=0.721 n=8)
RubySpecTest/page_cache.clean/filesystem.bindfs-8           76.67 ±  5%   75.17 ± 1%       ~ (p=0.382 n=8)
geomean                                                     131.6         126.7       -3.78%

                                                  │ /tmp/benchout.runsc │   /tmp/benchout.runsc-opt   │
                                                  │      load.sec       │  load.sec   vs base         │
RubySpecTest/page_cache.clean/filesystem.bindfs-8            12.95 ± 6%   12.80 ± 5%  ~ (p=0.625 n=8)
```

PiperOrigin-RevId: 538823897
2023-06-08 10:33:25 -07:00
Etienne PerotandgVisor bot f8b9824813 Update unimpl.EmitUnimplementedEvent interface to add the syscall number.
This catches up the interface to the `EmitUnimplementedEvent` method signature
on `kernel.Kernel`.

Also add build-time test to verify that `kernel.Kernel` implements this
interface, in order to catch such breakages at build time in the future.

PiperOrigin-RevId: 519000411
2023-03-23 17:01:37 -07:00
Adin ScannellandgVisor bot 1ceb814544 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Ayush RanjanandgVisor bot 09459b203a Hide root overlay filestore from container using whiteout.
PiperOrigin-RevId: 507355437
2023-02-05 19:36:11 -08:00
Ayush RanjanandgVisor bot aff351f684 Handle terminal symlinks correctly in OpenAt() implementations.
All filesystems except tmpfs had the same bug regarding handling symlinks
in the terminal path component. When they tried to open(2) such a symlink
file, these implementations called into stepLocked() with
Then these implementations also called vfs.HandleSymlink() which would
also advance rp (when rp.Done() was already true).

This was tripping the invariants checks in sentry/vfs/debug.go.

PiperOrigin-RevId: 504098263
2023-01-23 15:35:15 -08:00
Andrei VaginandgVisor bot 14b5ff5a2f overlayfs: don't call SetStat and StatAt under dentry.mapsMu
This allows to avoid lock order inversions with inodeMutex and
filesystemRWMutex.

PiperOrigin-RevId: 495629138
2022-12-15 10:16:06 -08:00
Ayush RanjanandgVisor bot d17af25336 Add ENAMETOOLONG checks in overlayfs.
Linux overlayfs uses the max of all layers' filename length limit. Do the same
in gVisor.

This is needed to get PHP runtime test ext/standard/tests/strings/007.phpt to
pass with overlayfs.

PiperOrigin-RevId: 493942515
2022-12-08 11:04:37 -08:00
Ayush RanjanandgVisor bot 175db901ca Replace pkg/refs with pkg/refsvfs2.
All VFS1 only bits have been deleted.

Updates #1624

PiperOrigin-RevId: 492273183
2022-12-01 12:46:11 -08:00
Etienne PerotandgVisor bot 445fa6f40c Lockdep: Print more info in the "unbalanced unlock" case.
This CL does the following:

- Add the ability for nested locks to have names.
- Give names to all current uses of nested locks in the codebase.
- Truncate `lockdep` debug stack traces to avoid the clutter from the
  `lockdep` code itself
- Simplify `lockdep` to not longer require `classMap`.

PiperOrigin-RevId: 491486620
2022-11-28 17:53:09 -08:00
Ayush RanjanandgVisor bot 7eeeb796f8 Delete VFS1 filesystem implementations.
Updates #1624

PiperOrigin-RevId: 488986080
2022-11-16 11:05:10 -08:00
Kevin KrakauerandgVisor bot d8aa09e04c convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Ayush RanjanandgVisor bot 208f87f387 Copy up consistently for all file types if open mode is writable.
Earlier, we were only copying up regular files. We were missing
out device files. ensureOpenableLocked() should ensure that
files are copied up if mode is writable. Without this, we
violate the preconditions for openCopiedUp().

Also, without this, open(file, O_WRONLY) on a lower layer pipe
returns EROFS, which is confusing because the upper layer is
still writable. Now it will return EPERM which indicates that
the pipe can not be copied up.

PiperOrigin-RevId: 465838606
2022-08-07 00:08:24 -07:00