fuse: use readdir result to shortcut negative lookups
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest7 patches 6 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/7 | fuse: forget READDIRPLUS entries that were not linkedfs/ | +68 | 0 replies | |
| 2/7 | fuse: add a negative lookup cache for directoriesfs/ fs/ fs/ +2 more | +332 | 0 replies | |
| 3/7 | fuse: fill the negative lookup cache from complete READDIRPLUS passesfs/ fs/ fs/ +2 more | +162 | 3 replies | |
| 4/7 | fuse: split readdir requests from struct filefs/ fs/ fs/ | +130 | 0 replies | |
| 5/7 | fuse: allow requests on behalf of no processfs/ fs/ | +9 | 0 replies | |
| 6/7 | fuse: open and release files for the kernel itselffs/ fs/ | +32 | 0 replies | |
| 7/7 | fuse: let the server ask for a directory to be read in the backgroundfs/ fs/ fs/ +4 more | +347 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/7] fuse: use readdir result to shortcut negative lookups
FUSE servers can respond to LOOKUP requests with {OK, nodeID=0} to indicate an entry is not present, and the negative entry can be cached for a specified interval. This helps reduce the number of LOOKUP calls, but it is still problematic: each negative dentry still costs a roundtrip. Since the universe of non-existing files is unlimited, in practice we must still do many roundtrips.
We can do better: once the kernel has read directory contents (through READDIRPLUS), the server has specified which positive dentries exist. In most file systems, this means that all other lookups in the directory should yield negative results, and we can spare ourselves the roundtrips.
In this patch series, we collect filename hashes from directory reads, and use them to populate a Bloom filter once the read is finished. Then, on lookup, if we don't find the name in the Bloom filter, we skip the roundtrip and return a negative result immediately.
The Bloom filter is space efficient, while still allowing efficient updates when entries are added through MKDIR/CREATE/LINK operations.
The TTL of the Bloom filter is derived from the entry TTL of the "." entry, if specified, or the minimum TTL of the positive entries. We use READDIRPLUS rather than READDIR, because the latter has no channel to signal a TTL.
Once the filter is in place, the server should rarely get lookups in the directory. If the server does get lookups, this can mean two things:
1) the filter has expired
2) it was a false positive from the Bloom filter
To address case 1), the server should be able to trigger repopulating the filter, but because of 2), issuing a normal readdir unconditionally is expensive, and the server may not have access to the mountpoint anyway.
To solve this, we add a new notify method, which can return EALREADY (the filter is still valid; no further side effect) or OK (there is no filter; trigger a new READDIRPLUS to populate the filter again). The notify does not wait for the server, so it can be sent from the LOOKUP handler itself.
The kernel reads the directory from a work item, with synchronous OPENDIR, READDIRPLUS and RELEASEDIR requests made on behalf of no process. The work items run on a workqueue per connection, so a server that does not reply only holds up its own reads, and at most 8 run per connection; further notifications fail with EBUSY.
The notification also lets a server preemptively load a directory:
READDIRPLUS is attractive because a single roundtrip can load many entries, reducing overhead, but many programs (eg. GCC) only cause single file lookups, and never issue readdir.
Unfortunately, virtiofs currently does not support notifications in general, so the latter benefits are not available for virtiofs.
The series is organized as follows:
* patch 1 fixes lookup references leaked on READDIRPLUS errors. It is independent of the rest.
* patch 2 adds the Bloom filter machinery
* patch 3 populates the filter from READDIRPLUS results (FOPEN_COMPLETE_DIR)
* patches 4-6 prepare for reading a directory from a work item, without functional change: readdir without a struct file, requests on behalf of no process, and opening and releasing a file for the kernel itself
* patch 7 adds the notify method.
To support this feature in userspace, the server only needs to reply with FOPEN_COMPLETE_DIR flag for the OPENDIR opcode. I did this in Go-FUSE, and added tests and benchmarks for the feature. Tests ran against a kernel with debug/paranoia settings. Benchmark against a kernel with production settings.[1]
An i5-8350U pinned at 2 GHz yields the following performance numbers, in median ns/op over 11 runs:
ReadDirEntCompleteDir (per entry) 2,281
ReaddirplusHint (per entry) 1,801
NegStatUncached (LOOKUP round trip) 22,192
NegStatCached (dcache hit) 797
NegStatUncachedDir (Bloom filter) 1,587
This was a synthetic, in-memory file system serving a directory with 500 entries, using the Go-FUSE high-level API running against /dev/fuse, measured with single threaded access to the mountpoint, and GOMAXPROCS=2. ReaddirplusHint drops the filter with FUSE_NOTIFY_INVAL_INODE and has the kernel read the directory again with the new notification; it is cheaper per entry than getdents(), as no entries are copied to userspace.
The bloom filter is sized to 10 bits per entry, but then rounded to a power of 2. For 500 entries, this yields 8192 (16.4 bits per entry), with an expected false positive rate of 0.0006/op; the benchmark sees 0.0004/op.
So, the Bloom filter yields a 14x speed up on uncached negative lookups, at the cost of a full directory read. This pays off if
size-of-dir < (cost_LOOKUP - cost_BLOOM)/cost_DIRENT * unique-negative-lookups
= 9 * unique-negative-lookups
As a practical one-point sample: one of my open source C++ projects (lilypond) shows that a single G++ invocation gets ENOENT for 145 unique names under /usr/include/libpng16, a directory with 3 entries, so scenarios where this is a win are easy to find.
This code was written and reviewed with Claude Opus 5.5. The plan is included in [1].
It may be interesting to lift the Bloom filter into the VFS, for two reasons:
- other filesystems could benefit from this performance optimization, if the negative lookup is expensive. In particular: NFS, CephFS and overlayfs.
- the Bloom filter uses little memory, and makes negative dentries cheap to recreate. This could justify a more aggressive reclaim policy for them.
However, unlike the FUSE case, there is no way for a server to request a readdir, so it would need kernel-side heuristics to guess when this pays off.
Open questions:
- Where should the filter TTL come from? The entry timeout of "." is convenient because of backward compatibility, but it feels like a hack. Alternatively, we could extend fuse_open_out (or repurpose the space for backing_id in case of directories). This has the added advantage that we could populate the negative filter from READDIR as well. Then again, READDIRPLUS is better overall because it also loads positive entries with little overhead.
- Should filters be reclaimed under memory pressure, and if so, how? The filter costs ~2 bytes per file name, which seems little, but can grow to 512kb for a large directory.
- Should filter use be tracked? A referenced bit (set on the first negative answer) would let a shrinker drop unused filters first, and would tell whether re-listing a directory paid off. Hit counters per filter would contend between parallel lookups, so observability may be better served by tracepoints, or per-cpu counters per connection in fusectl.
- For the background read, can we use a kernel thread and leave it and its buffer unaccounted for? An earlier version used a background request, scheduling follow-on requests from the end callbacks, but it looked more complicated.
- what level of self-tests is appropriate for this change? The simplest test I can think of is a file system that runs the notify, waits for the readdir, and then checks that stat() calls don't trigger LOOKUP.
[1] https://github.com/hanwen/go-fuse/tree/complete-dir
Han-Wen Nienhuys (7):
fuse: forget READDIRPLUS entries that were not linked
fuse: add a negative lookup cache for directories
fuse: fill the negative lookup cache from complete READDIRPLUS passes
fuse: split readdir requests from struct file
fuse: allow requests on behalf of no process
fuse: open and release files for the kernel itself
fuse: let the server ask for a directory to be read in the background
fs/fuse/Makefile | 2 +- fs/fuse/args.h | 2 + fs/fuse/dir.c | 35 ++- fs/fuse/dir_names.c | 274 +++++++++++++++++ fs/fuse/file.c | 48 ++- fs/fuse/fuse_i.h | 66 ++++ fs/fuse/inode.c | 18 +- fs/fuse/notify.c | 23 ++ fs/fuse/readdir.c | 613 ++++++++++++++++++++++++++++++++++---- fs/fuse/req.c | 7 + fs/fuse/virtio_fs.c | 1 + include/uapi/linux/fuse.h | 45 ++- 12 files changed, 1054 insertions(+), 80 deletions(-) create mode 100644 fs/fuse/dir_names.c
Signature · 1 line
2.56.0