fix(proot): close cached folder handles whose folder was renamed or deleted

This commit is contained in:
maxjivi05 committed 2026-11-09 18:28:13 -05:00
1 parent ad24943155
commit b676448e0a
2 files changed
+28 -4

No files matched your search

+17 -4
View File
@@ -418,7 +418,7 @@ static int host_bind(const char *h) {
static unsigned hash_of(const char *s);
#define DIRS 64
#define DIR_FD_FLOOR 1000
struct dirslot { unsigned hash; int fd, refs; char path[PATH_MAX]; char seen[PATH_MAX]; };
struct dirslot { unsigned hash; int fd, refs; dev_t dev; ino_t ino; char path[PATH_MAX]; char seen[PATH_MAX]; };
static struct dirslot dirs[DIRS];
struct near { long fd; int slot, own; const char *name; };
@@ -443,6 +443,14 @@ static long fd_path(long fd, char *out) {
return 0;
}
static int fd_id(long fd, dev_t *dev, ino_t *ino) {
struct stat st;
if (sc(SYS_newfstatat, fd, (long)"", (long)&st, AT_EMPTY_PATH, 0) != 0) return -1;
*dev = st.st_dev;
*ino = st.st_ino;
return 0;
}
static void near_put(struct near *n) {
if (n->own) sc(SYS_close, n->fd, 0, 0, 0, 0);
else if (n->slot >= 0) __atomic_sub_fetch(&dirs[n->slot].refs, 1, __ATOMIC_RELEASE);
@@ -482,15 +490,20 @@ static int dir_keep(const char *dir, long fd, struct near *n) {
n->slot = -1;
n->own = 1;
char seen[PATH_MAX];
if (fd_path(high, seen) < 0 || strlen(dir) >= PATH_MAX) return 0;
dev_t dev;
ino_t ino;
if (fd_path(high, seen) < 0 || fd_id(high, &dev, &ino) < 0 || strlen(dir) >= PATH_MAX) return 0;
unsigned h = hash_of(dir);
struct dirslot *e = &dirs[h % DIRS];
if (pthread_mutex_trylock(&dir_lock) != 0) return 0;
if (__atomic_load_n(&e->refs, __ATOMIC_ACQUIRE) == 0) {
char old[PATH_MAX];
if (e->fd > 0 && fd_path(e->fd, old) == 0 && strcmp(old, e->seen) == 0) sc(SYS_close, e->fd, 0, 0, 0, 0);
dev_t odev;
ino_t oino;
if (e->fd > 0 && fd_id(e->fd, &odev, &oino) == 0 && odev == e->dev && oino == e->ino) sc(SYS_close, e->fd, 0, 0, 0, 0);
e->hash = h;
e->fd = (int)high;
e->dev = dev;
e->ino = ino;
e->refs = 1;
strcpy(e->path, dir);
strcpy(e->seen, seen);
+11
View File
@@ -146,6 +146,17 @@ class FastPathFuseDirsTest(unittest.TestCase):
self.assertEqual(["ok"] * 5, out)
self.assertEqual(["new"], [p.name for p in data.iterdir()])
def test_folders_deleted_and_made_again_many_times_keep_descriptors_bounded(self):
data = self.card / "Games/steamapps/common/Game/Data"
ops = ["fuse"]
for _ in range(200):
ops += ["touch", "/mnt/card/Games/steamapps/common/Game/Data/x",
"rm", "/mnt/card/Games/steamapps/common/Game/Data/x",
"host-rmdir", str(data), "host-mkdir", str(data)]
out = self.ask(*ops, "fds")
self.assertEqual(["ok"] * 800, out[:-1])
self.assertLessEqual(int(out[-1]), 64)
def test_descriptors_the_program_takes_over_are_left_alone(self):
data = "/mnt/card/Games/steamapps/common/Game/Data"
out = self.ask("fuse", "touch", f"{data}/a", "clobber", "touch", f"{data}/b",