mergerfs/mkdocs/docs/config/func_readdir.md

68 lines
4.5 KiB
Markdown
Raw Normal View History

# func.readdir
2025-09-21 15:25:24 -04:00
* type: `STR`
* default: `seq`
* examples: `func.readdir=seq`, `func.readdir=cor:4`
2025-09-21 15:25:24 -04:00
`readdir` has policies to control how to read directory content.
| Policy | Description |
| ------ | ----------- |
| seq | "sequential" : Iterate sequentially over branches in the order defined in `branches`. This is the default and traditional behavior found prior to the readdir policy introduction. This will be increasingly slower as more branches are added to the pool. Especially if needing to wait for drives to spin up or network filesystems to respond. |
fix: validate and cap thread and queue counts - std::stoi threw std::out_of_range for a func.readdir concurrency or queue depth above INT_MAX, e.g. cor:99999999999. Given at mount, mergerfs aborted. Set at runtime through .mergerfs, the daemon shut down and the mount went away, or with process-thread-count>0 the request never got a reply. Parse with str::from, which reports the overflow, and reject the value like any other malformed one. - Reject a readdir queue depth whose product with the concurrency overflows an int, e.g. cor:2147483647:2147483647, instead of wrapping it. - Limit read-thread-count, process-thread-count and the cor/cosr pool size to 64 threads per pool, after resolving automatic and CPU-divided counts, and log a warning when clamping. Resolve negative FUSE counts without overflowing on INT_MIN, and drop the unreachable negative readdir concurrency branch. - process-thread-queue-depth is multiplied by the process thread count. Saturate the product at INT_MAX instead of overflowing. - Correct the docs: cor/cosr counts accept digits only and 0 means the core count, at most 8; a queue depth of 0 means 2 per thread; read-thread-count=0 with process-thread-count=0 is 2 read threads and max(2, cores - 2) process threads, at most 8; the queue limit is depth times process threads, each read thread holding one in-flight buffer of its own. Point at threads.md for how the settings combine. - Document the per-read-thread /dev/fuse clones and their fallback. - tests.cpp covers the overflow cases. Performance: default thread counts are unchanged (8 on hosts with 8 or more cores). Only explicit or CPU-divided counts above 64, which 2.42.0 honored, are reduced.
2026-11-21 14:33:27 -05:00
| cosr:N:M | "concurrent open, sequential read" : Concurrently open branch directories using a thread pool and process them in the order defined in `branches`. This keeps memory and CPU usage low while also reducing the time spent waiting on branches to respond. `N` is the number of threads, at most 64. If `0` it is the core count, at most 8. `M` is the queue depth per thread. If `0` it is 2. |
| cosr:N | cosr:N:M with M = 0 |
| cosr | cosr:N:M with N = 0 and M = 0 |
fix: keep readdir listings stable under fill errors - func.readdir=cor appended entries in whichever order branch workers finished. Directory offsets are positions in that list, so a reader reopening an unchanged directory and seeking to a previously issued cookie (NFS does this) could see a duplicate and miss a later entry. Read branches concurrently into their own buffers, then merge and deduplicate them in configured branch order. The merge mutex is gone. - Growing a dirents buffer overwrote the only pointer with realloc's result while recording the new capacity, so an allocation failure leaked the buffer and the next fill wrote through NULL. Grow both vectors without changing pointer, length or capacity until the allocation succeeds, and return ENOMEM. - seq and cosr ignored that failure and served a listing with entries missing. Every policy now returns the error, and libfuse resets the handle's buffer whenever a fill fails, so a later request at a nonzero offset re-enumerates instead of serving a partial or stale listing as success. cosr closes the directories its workers opened when it gives up early. - Document the fill-error contract, which doesn't promise that every branch was read: seq and cosr can treat a branch read error as the end of the directory. Document cor's memory use, and refer to func.getattr, not fuse.getattr. - TEST_readdir_cor_cookie_replay compares cor listings, offsets and inodes with seq and replays cookies on fresh handles. TEST_dirents_allocation injects allocation failures into the dirents code. TEST_readdir_cor_enomem fails one allocation in a mounted daemon for cor and seq and checks the same handle then serves the exact suffix. Performance: cor copies each entry twice and merges serially after the parallel reads. Listing 100,000 entries over 4 tmpfs branches took the same time before and after (81-83 ms per listing for cor:4 and seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
2026-05-13 10:58:01 -05:00
| cor:N:M | "concurrent open and read" : Concurrently open branch directories and immediately start reading their contents using a thread pool. This will result in higher memory and CPU usage but reduced latency: peak memory is roughly all outstanding per-branch listings plus the merged output. Particularly when using higher latency / slower speed network filesystem branches. Each branch is read into its own buffer and the buffers are merged in the order defined in `branches` so, as with `seq` and `cosr`, the order of files is stable across reads. A stable order is required for correct readdir offset handling given the kernel may drop and re-open a directory mid-stream, such as when exported via NFS. `N` is the number of threads, at most 64. If `0` it is the core count, at most 8. `M` is the queue depth per thread. If `0` it is 2. |
fix: validate and cap thread and queue counts - std::stoi threw std::out_of_range for a func.readdir concurrency or queue depth above INT_MAX, e.g. cor:99999999999. Given at mount, mergerfs aborted. Set at runtime through .mergerfs, the daemon shut down and the mount went away, or with process-thread-count>0 the request never got a reply. Parse with str::from, which reports the overflow, and reject the value like any other malformed one. - Reject a readdir queue depth whose product with the concurrency overflows an int, e.g. cor:2147483647:2147483647, instead of wrapping it. - Limit read-thread-count, process-thread-count and the cor/cosr pool size to 64 threads per pool, after resolving automatic and CPU-divided counts, and log a warning when clamping. Resolve negative FUSE counts without overflowing on INT_MIN, and drop the unreachable negative readdir concurrency branch. - process-thread-queue-depth is multiplied by the process thread count. Saturate the product at INT_MAX instead of overflowing. - Correct the docs: cor/cosr counts accept digits only and 0 means the core count, at most 8; a queue depth of 0 means 2 per thread; read-thread-count=0 with process-thread-count=0 is 2 read threads and max(2, cores - 2) process threads, at most 8; the queue limit is depth times process threads, each read thread holding one in-flight buffer of its own. Point at threads.md for how the settings combine. - Document the per-read-thread /dev/fuse clones and their fallback. - tests.cpp covers the overflow cases. Performance: default thread counts are unchanged (8 on hosts with 8 or more cores). Only explicit or CPU-divided counts above 64, which 2.42.0 honored, are reduced.
2026-11-21 14:33:27 -05:00
| cor:N | cor:N:M with M = 0 |
| cor | cor:N:M with N = 0 and M = 0 |
For `cor` and `cosr`, write `N` and `M` as decimal digits only (no sign),
from 0 through `INT_MAX` (2147483647). Out-of-range values are rejected,
not clamped. A valid thread count above 64 is clamped to 64 with a syslog
warning (`LOG_WARNING`); the cap does not apply to the per-thread queue
depth. After defaults and the thread cap are applied, `N * M` must fit in
`INT_MAX` or the entire setting is rejected; the queue depth is not saturated.
For example, `cor:65:2` uses 64 threads and 128 queue slots. `cor:4294967296`
is rejected because the count is out of range; `cor:64:34000000` is rejected
because its total queue depth exceeds `INT_MAX`.
Keep in mind that `readdir` mostly just provides a list of file names
in a directory and possibly some basic metadata about said files. To
know details about the files, as one would see from commands like
`find` or `ls`, it is required to call `stat` on the file which is
fix: keep readdir listings stable under fill errors - func.readdir=cor appended entries in whichever order branch workers finished. Directory offsets are positions in that list, so a reader reopening an unchanged directory and seeking to a previously issued cookie (NFS does this) could see a duplicate and miss a later entry. Read branches concurrently into their own buffers, then merge and deduplicate them in configured branch order. The merge mutex is gone. - Growing a dirents buffer overwrote the only pointer with realloc's result while recording the new capacity, so an allocation failure leaked the buffer and the next fill wrote through NULL. Grow both vectors without changing pointer, length or capacity until the allocation succeeds, and return ENOMEM. - seq and cosr ignored that failure and served a listing with entries missing. Every policy now returns the error, and libfuse resets the handle's buffer whenever a fill fails, so a later request at a nonzero offset re-enumerates instead of serving a partial or stale listing as success. cosr closes the directories its workers opened when it gives up early. - Document the fill-error contract, which doesn't promise that every branch was read: seq and cosr can treat a branch read error as the end of the directory. Document cor's memory use, and refer to func.getattr, not fuse.getattr. - TEST_readdir_cor_cookie_replay compares cor listings, offsets and inodes with seq and replays cookies on fresh handles. TEST_dirents_allocation injects allocation failures into the dirents code. TEST_readdir_cor_enomem fails one allocation in a mounted daemon for cor and seq and checks the same handle then serves the exact suffix. Performance: cor copies each entry twice and merges serially after the parallel reads. Listing 100,000 entries over 4 tmpfs branches took the same time before and after (81-83 ms per listing for cor:4 and seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
2026-05-13 10:58:01 -05:00
controlled by `func.getattr`.
2025-09-21 15:25:24 -04:00
Until Linux v6.16, despite being able to change the size of FUSE
messages, `readdir` requests were limited to 1 page (4KiB) in size per
message. This limitation limited throughput and therefore performance
of the `readdir` call. In v6.16 that was changed to be able to grow to
2025-10-24 22:31:04 -05:00
the [fuse-msg-size](fuse-msg-size.md) which allows a lot more data to
2025-09-21 15:25:24 -04:00
be sent per request and therefore much better performance in certain
situations.
## Technical Details
* On Linux
[getdents](https://man7.org/linux/man-pages/man2/getdents.2.html) is
used instead of
[readdir](https://man7.org/linux/man-pages/man2/readdir.2.html) in
order to leverage larger buffers for better performance on larger
directory reads as well as have more control in concurrent policies.
Individual branch `getdents64` requests are capped at 128 KiB to work
around problems with larger requests on virtiofs. This applies to
`seq`, `cosr`, and `cor`; it does not limit the total directory listing
or change [fuse-msg-size](fuse-msg-size.md).
* [readdir](https://man7.org/linux/man-pages/man2/readdir.2.html) is
used on FreeBSD.
fix: keep readdir listings stable under fill errors - func.readdir=cor appended entries in whichever order branch workers finished. Directory offsets are positions in that list, so a reader reopening an unchanged directory and seeking to a previously issued cookie (NFS does this) could see a duplicate and miss a later entry. Read branches concurrently into their own buffers, then merge and deduplicate them in configured branch order. The merge mutex is gone. - Growing a dirents buffer overwrote the only pointer with realloc's result while recording the new capacity, so an allocation failure leaked the buffer and the next fill wrote through NULL. Grow both vectors without changing pointer, length or capacity until the allocation succeeds, and return ENOMEM. - seq and cosr ignored that failure and served a listing with entries missing. Every policy now returns the error, and libfuse resets the handle's buffer whenever a fill fails, so a later request at a nonzero offset re-enumerates instead of serving a partial or stale listing as success. cosr closes the directories its workers opened when it gives up early. - Document the fill-error contract, which doesn't promise that every branch was read: seq and cosr can treat a branch read error as the end of the directory. Document cor's memory use, and refer to func.getattr, not fuse.getattr. - TEST_readdir_cor_cookie_replay compares cor listings, offsets and inodes with seq and replays cookies on fresh handles. TEST_dirents_allocation injects allocation failures into the dirents code. TEST_readdir_cor_enomem fails one allocation in a mounted daemon for cor and seq and checks the same handle then serves the exact suffix. Performance: cor copies each entry twice and merges serially after the parallel reads. Listing 100,000 entries over 4 tmpfs branches took the same time before and after (81-83 ms per listing for cor:4 and seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
2026-05-13 10:58:01 -05:00
* Each open directory handle caches its listing. If a policy reports a
fill error (for example, `ENOMEM` while growing the entry buffer),
mergerfs returns that error and discards the incomplete buffer. The
next request rebuilds the listing, even when resuming at a nonzero
offset. With unchanged branches and directory contents, merging entries
in branch order preserves their positions across those retries.
* This is not a guarantee that every branch was read successfully. For
example, `seq` and `cosr` can treat a branch read error as end of
directory and return the entries collected so far.