Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions guest/kernel/patches/0006-fsnotify-events-from-the-host.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
# which hears of those changes from the Mac, raise the fsnotify events Linux
# raises for a local change ("M\t/mnt/mac/…" → IN_MODIFY, C/A/D likewise).
# Paths are looked up beneath /mnt/mac only (no symlinks, no other mounts),
# nothing is written and no dentry dropped, and only the VM's agent may
# write (root, in the VM's first mount namespace: not a container).
# nothing is written, and deleted or modified file names are dropped from
# the guest's dcache so lookups see replacements before Apple's timeout.
# Only the VM's agent may write (root, in the VM's first mount namespace:
# not a container).
--- a/fs/notify/Makefile
+++ b/fs/notify/Makefile
@@ -5,3 +5,5 @@
Expand All @@ -17,7 +19,7 @@
+obj-$(CONFIG_FSNOTIFY) += kiri_inject.o
--- /dev/null
+++ b/fs/notify/kiri_inject.c
@@ -0,0 +1,191 @@
@@ -0,0 +1,221 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * /proc/kiri-fsnotify: changes made outside the VM, raised as fsnotify
Expand All @@ -35,9 +37,10 @@
+ * A PATH's attributes changed (IN_ATTRIB)
+ * D PATH was deleted (IN_DELETE on its directory, IN_DELETE_SELF)
+ *
+ * Only events are raised: nothing is written, and no dentry is dropped
+ * (the share caches nothing, so lookups already ask the Mac). PATH is
+ * looked up beneath /mnt/mac only, crossing no symlink and no mount, and
+ * Nothing is written. Deleted or modified cached file names are dropped
+ * so a lookup asks the Mac again rather than opening a deleted or
+ * replaced node until Apple's timeout. Directories and mount points stay.
+ * PATH is looked up beneath /mnt/mac only, crossing no symlink and no mount, and
+ * /mnt/mac must be the FUSE (virtiofs) share: a name can't lead anywhere
+ * else in the VM. Only the VM's own agent may write: root, in the VM's
+ * first mount namespace, so a container can't, even as root.
Expand All @@ -60,12 +63,28 @@
+#define KIRI_SHARE "/mnt/mac"
+#define KIRI_SCOPED (LOOKUP_BENEATH | LOOKUP_NO_SYMLINKS | LOOKUP_NO_XDEV | LOOKUP_NO_MAGICLINKS)
+
+/* The file was deleted outside: tell its directory, and the file's own
+ * watchers if the VM still has its inode. */
+/* Expire a cached file name without unlinking or asking the Mac. Keep the
+ * returned reference until after a fresh lookup: an in-place change must
+ * not evict a watched inode. Directories and mount points stay reachable. */
+static struct dentry *kiri_expire(struct path *parent, struct qstr *name)
+{
+ struct dentry *child = try_lookup_noperm(name, parent->dentry);
+
+ if (IS_ERR_OR_NULL(child))
+ return child;
+ spin_lock(&child->d_lock);
+ if (d_really_is_positive(child) && !d_is_dir(child) && !d_mountpoint(child))
+ __d_drop(child);
+ spin_unlock(&child->d_lock);
+ return child;
+}
+
+/* The file was deleted outside: forget its cached name, tell its
+ * directory, and the file's own watchers if the VM still has its inode. */
+static int kiri_deleted(struct path *parent, struct qstr *name)
+{
+ struct inode *dir = d_inode(parent->dentry);
+ struct dentry *child = try_lookup_noperm(name, parent->dentry);
+ struct dentry *child = kiri_expire(parent, name);
+
+ if (IS_ERR(child))
+ return PTR_ERR(child);
Expand All @@ -91,6 +110,7 @@
+{
+ char *slash = strrchr(rel, '/');
+ struct path parent, target;
+ struct dentry *cached = NULL;
+ struct qstr name;
+ char *last;
+ int err;
Expand All @@ -113,6 +133,20 @@
+ err = kiri_deleted(&parent, &name);
+ goto out;
+ }
+ if (op == 'M') {
+ /* An editor's rename onto a file arrives as M, not D. Expire
+ * before looking it up and before a watcher hears of the save. */
+ cached = kiri_expire(&parent, &name);
+ if (IS_ERR(cached)) {
+ err = PTR_ERR(cached);
+ cached = NULL;
+ goto out;
+ }
+ if (!fsnotify_sb_has_watchers(share->dentry->d_sb)) {
+ err = 0;
+ goto out;
+ }
+ }
+ err = vfs_path_lookup(share->dentry, share->mnt, rel, KIRI_SCOPED, &target);
+ if (err)
+ goto out;
Expand All @@ -134,6 +168,7 @@
+ }
+ path_put(&target);
+out:
+ dput(cached);
+ path_put(&parent);
+ return err;
+}
Expand Down Expand Up @@ -169,13 +204,6 @@
+ path_put(&share);
+ return -EXDEV;
+ }
+ /* Without a watcher there is no one to tell, and a batch costs no
+ * lookups (a container writing many files to a bind mount sends its
+ * own changes back). */
+ if (!fsnotify_sb_has_watchers(share.dentry->d_sb)) {
+ path_put(&share);
+ return count;
+ }
+ buf = memdup_user_nul(ubuf, count);
+ if (IS_ERR(buf)) {
+ path_put(&share);
Expand All @@ -191,6 +219,10 @@
+ err = -EINVAL;
+ continue;
+ }
+ /* Deletes and modifications expire names without watchers.
+ * Other events still cost no lookups without a watcher. */
+ if (line[0] != 'D' && line[0] != 'M' && !fsnotify_sb_has_watchers(share.dentry->d_sb))
+ continue;
+ err = kiri_event(&share, line[0], rel);
+ }
+ kfree(buf);
Expand Down
79 changes: 79 additions & 0 deletions tests/bind-recreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
# A name cached by virtiofs must be reusable after the Mac deletes or
# replaces the file (#399), with and without an inotify watcher:
# DOCKER_CONTEXT=kiri tests/bind-recreate.sh
set -eu

bind=$(mktemp -d)
bind=$(cd "$bind" && pwd -P)
container="kiri-bind-recreate-$$"
cleanup() {
docker rm -f "$container" >/dev/null 2>&1 || true
rm -rf "$bind"
}
trap cleanup EXIT
failed=0
cat > "$bind/onchange.sh" <<'SH'
#!/bin/sh
cat "$2/$3" > "/tmp/seen-$3" 2>&1
SH
chmod +x "$bind/onchange.sh"

for watched in no yes; do
if [ "$watched" = yes ]; then
docker run -d --name "$container" -v "$bind:/w" alpine inotifyd /w/onchange.sh /w:c >/dev/null
else
docker run -d --name "$container" -v "$bind:/w" alpine sleep 60 >/dev/null
fi
# The daemon scans for new bind sources once a second. Let FSEvents
# subscribe before testing changes, so this tests the guest's cache.
sleep 2
for change in guest-create host-replace host-recreate; do
for i in 1 2 3 4 5; do
file="$watched-$change-$i"
echo old > "$bind/$file"
docker exec "$container" cat "/w/$file" >/dev/null
if [ "$watched" = yes ]; then
sleep 0.1
docker exec "$container" rm -f "/tmp/seen-$file"
fi
case "$change" in
guest-create) rm "$bind/$file" ;;
host-replace) echo new > "$bind/tmp"; mv "$bind/tmp" "$bind/$file" ;;
host-recreate) rm "$bind/$file"; echo new > "$bind/$file" ;;
esac
# FSEvents batches changes for 50 ms. Allow delivery, but stay well
# inside Apple's roughly one-second positive-entry cache timeout.
sleep 0.2
if [ "$change" = guest-create ] &&
! docker exec "$container" sh -c 'echo new > "/w/$1"' sh "$file"; then
echo "FAIL $change (watcher: $watched, file: $file)" >&2
failed=1
continue
fi
# The watcher's first read after the event must already see the
# replacement, before the explicit read below can refresh its name.
if [ "$watched" = yes ]; then
if ! content=$(docker exec "$container" sh -c '
for attempt in 1 2 3 4 5 6 7 8 9 10; do
if [ -f "/tmp/seen-$1" ]; then cat "/tmp/seen-$1"; exit; fi
sleep 0.02
done
exit 1' sh "$file") || [ "$content" != new ]; then
echo "FAIL watcher read after $change (file: $file): ${content:-no event}" >&2
failed=1
fi
fi
if ! content=$(docker exec "$container" cat "/w/$file") ||
[ "$content" != new ] || [ "$(cat "$bind/$file" 2>/dev/null)" != new ]; then
echo "FAIL $change (watcher: $watched, file: $file)" >&2
failed=1
fi
done
done
docker rm -f "$container" >/dev/null
done
if [ "$failed" = 0 ]; then
echo "PASS bind recreate and replace, with and without a watcher (30 attempts)"
fi
exit "$failed"
1 change: 1 addition & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ t bind-mount-flag "from the mac" $D run --rm --mount "type=bind,source=$BIND,tar
code bind-mount-missing 125 $D run --rm --mount type=bind,source=/kiri/does/not/exist,target=/m alpine true
t bind-v-creates "made" bash -c "$D run --rm -v $BIND/new/dir:/n alpine true && [ -d $BIND/new/dir ] && echo made"
t bind-file "from the mac" $D run --rm -v "$BIND/hello.txt:/etc/greeting" alpine cat /etc/greeting
code bind-recreate 0 bash "$(dirname "$0")/bind-recreate.sh"
# A file made, changed and deleted on the Mac raises inotify events in the
# container, as one made there does: file watchers (Vite, webpack, nodemon)
# pick up saves made in an editor on the Mac. inotifyd prints n (created),
Expand Down