Skip to content

squashfuse_ll: let the kernel cache directory listings - #159

Merged
vasi merged 1 commit into
vasi:masterfrom
simonpintarelli:readdir-cache
Nov 21, 2026
Merged

vasi merged 1 commit into
vasi:masterfrom
simonpintarelli:readdir-cache

Conversation

@simonpintarelli

Copy link
Copy Markdown
Contributor

Summary

Right now, every directory listing on a squashfuse_ll mount sends four requests to the daemon: OPENDIR, READDIR, a second READDIR that comes back empty, and RELEASEDIR. The kernel caches none of them. Lookups and attributes are already cached forever (SQFS_TIMEOUT = DBL_MAX), so on a repeated tree walk (find, fd, build tools that scan include paths) nearly all remaining requests are these directory ones.

The image is read-only and can't change while mounted, so the kernel can safely keep these results. This PR makes it do so:

  1. cache_readdir / keep_cache in opendir. sqfs_ll_op_opendir sets fi->cache_readdir = 1 and fi->keep_cache = 1 (FOPEN_CACHE_DIR, Linux ≥ 4.20). Readdir results then stay in the page cache across opens.
  2. No OPENDIR/RELEASEDIR when the kernel supports skipping them. sqfs_ll_op_init records whether conn->capable has FUSE_CAP_NO_OPENDIR_SUPPORT (Linux ≥ 5.1). If it does, opendir replies ENOSYS. As documented in fuse_lowlevel.h, the kernel then handles opendir/releasedir itself and caches readdir "as if opendir returned FOPEN_KEEP_CACHE | FOPEN_CACHE_DIR". sqfs_ll_op_readdir then receives fi->fh == 0, and in that case it looks up the inode into a stack sqfs_ll_i. That's safe because readdir replies before it returns.

On a warm cache, a traversal now sends no requests to the daemon at all.

Everything is behind #ifdef FUSE_CAP_NO_OPENDIR_SUPPORT. That flag and cache_readdir both arrived in libfuse 3.5, so FUSE 2.x and older libfuse 3 builds don't change. On a kernel without no-opendir support you still get (1).

Notes

  • open_refcount stays balanced: when opendir is skipped, releasedir is never called either. Idle-timeout unmount still works from the last access time.
  • Programs that use libsquashfuse_ll and build their own fuse_lowlevel_ops need to register sqfs_ll_op_init (as squashfuse_ll already does) to get (2). Without it they only get (1).

Testing

  • make check passes (7/7) with the multithreaded build, Linux 6.18, libfuse 3.
  • I built an image of /usr/share (370,571 entries, including large directories that need several READDIR calls). I mounted it with three builds: unpatched, patched, and patched with no-opendir forced off (to simulate an older kernel). The full find listing and ls -laR output give the same md5 for all three.
  • Benchmark: hyperfine -N --warmup 2 'fd -HI -jN does-not-exist <mnt>', warm cache, same image:
build fd -j1 fd -j8
master 662.6 ms ± 19.9 244.4 ms ± 5.6
(1) only (older kernels) 248.2 ms ± 5.1 79.3 ms ± 1.4
(1) + (2) 136.6 ms ± 2.4 36.9 ms ± 1.3

On an HPC system (aarch64, kernel 6.4, libfuse 3.10, image with 16k directories), fd -j1 went from 1.50 s to 201 ms. The daemon used no CPU during a cached run.

  • Not tested: macOS/macFUSE and FreeBSD (the no-opendir capability is Linux-only), and the first traversal right after mounting.

🤖 Generated with Claude Code

The image is read-only, so directory listings never change while mounted.

- In sqfs_ll_op_opendir, set cache_readdir and keep_cache so the kernel
  keeps readdir results in the page cache (FOPEN_CACHE_DIR).
- If the kernel advertises FUSE_CAP_NO_OPENDIR_SUPPORT, reply ENOSYS to
  opendir. The kernel then stops sending OPENDIR/RELEASEDIR and caches
  readdir results as if opendir returned FOPEN_KEEP_CACHE | FOPEN_CACHE_DIR.
  sqfs_ll_op_readdir looks up the inode itself when there is no handle.

A cached traversal then sends no requests to the daemon. fd on a
370k-entry image: 663 ms -> 137 ms (-j1), 244 ms -> 37 ms (-j8).

Guarded by FUSE_CAP_NO_OPENDIR_SUPPORT (libfuse >= 3.5, same as
cache_readdir), so older libfuse and FUSE 2 builds are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vasi

vasi commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks!

@vasi
vasi merged commit b008af9 into vasi:master Nov 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants