26. What a pad costs: copy on first write without root, and eager clones
26. What a pad costs: copy on first write without root, and eager clones¶
Learning goal: whether a job's pad can be copy-on-first-write (Gavin: "a userspace file system that only makes the copy of the file via a reflink if something from gvisor actually tries to write to the file"), without root; and if not, what the eager design (a reflink clone of the whole pad per job) costs on real caches. A job that never writes its pad pays for the clone anyway (Gavin: "if the job does NOTHING with that directory that an insane amount of waste").
What was ruled out before running, from gVisor's source: an overlay
mounted inside the sandbox copies up with a 32 KiB read/write loop
(vfs.CopyRegularFileData), and gVisor answers FICLONE with
EOPNOTSUPP, so in-sandbox copy-up is a byte copy. Kernel overlayfs copies
up by reflink (since Linux 4.10) but needs root to mount where containerd
can see it.
A. A rootless kernel overlay, reached through /proc/PID/root¶
Method: on smaug (Linux 6.12, ZFS 2.4.4), as the developer, mount
kernel overlayfs in a new user and mount namespace (unshare -Urm,
-o userxattr) with lower, upper and work directories on
minipool/allmyram-dev, and keep a process alive inside it. Then start a
runc pod and a runsc pod with a bind mount whose host path is
/proc/<that process>/root/<merged dir>, the way root could reach a
mount in another namespace.
No predictions were written down before this part ran; the risks named
beforehand were whether containerd and runsc accept a /proc/<pid>/root
mount source, and whether a rootless overlay works on ZFS.
Results (2026-11-25):
- The rootless overlay mounts on ZFS and behaves as overlayfs should: reads come from the lower layer, a new file lands in the upper layer, and a deleted lower file becomes a whiteout (a 0,0 character device) in the upper layer, all owned by the developer outside the namespace.
- runc refuses the mount:
error mounting "/proc/4059208/root/var/lib/allmyram-dev/ovl26/merged" to rootfs at "/pad" ... flags: 0x5000: invalid argument. The kernel won't bind-mount a mount that belongs to another mount namespace. - runsc fails the same step in its gofer: the container start reads an
empty mounts file (
reading mounts file: error unmarshaling mounts: unexpected end of JSON input). Its task then wouldn't delete untilctr task delete -f.
What we learned: a mount made in a user namespace can't be handed to
containerd: the bind mount fails with EINVAL across mount namespaces,
and moving a mount into the host's namespace (open_tree, move_mount)
needs CAP_SYS_ADMIN there, which is root. Copy on first write without
root is out by this route.
B. Eager reflink clones of real caches¶
Method: spike/pad_clone_cost.py on minipool/allmyram-dev: a real
cargo target/ (a debug build of a project depending on tokio, serde,
reqwest, clap, regex and sqlx), Go's build cache and pip's cache from the
developer's home directory. Each is copied once into the dataset as a
"published version"; then, three times, it is cloned with cp -a
--reflink=always, walked (stat of every entry, as measuring a pad's size
would), flushed with sync -f, and deleted.
Predictions:
| Question | Prediction |
|---|---|
cargo target/ size |
1–3 GB, 5,000–20,000 entries |
| Clone time | grows with files and blocks: ~1–3 s for the cargo target/, under 1 s for pip's cache |
| Clone shares blocks | yes: bcloneused rises by about the tree's size, and free space barely moves |
| Walk | 0.1–0.3 s |
sync -f after a clone |
under 1 s |
| Delete | about the clone's time |
Results (2026-11-25, minipool/allmyram-dev, medians of three runs):
| Pad | Entries | Apparent size | Clone | Walk | sync -f |
Delete |
|---|---|---|---|---|---|---|
Transpire's webhooks target/ (cargo test --no-run, 26 dependencies; cold build 37 s) |
3,867 | 1.88 GB | 0.375 s | 0.020 s | 0.011 s | 0.157 s |
The made-up cargo project's target/ (cold build 20 s) |
1,942 | 777 MB | 0.179 s | 0.011 s | 0.009 s | 0.079 s |
| Go's build cache | 7,344 | 942 MB | 0.658 s | 0.032 s | 0.023 s | 0.289 s |
| pip's cache | 886 | 297 MB | 0.093 s | 0.012 s | 0.005 s | 0.043 s |
Clones share blocks: after a ZFS commit, cloning the 777 MB target/
took 1.1 MB of pool space and raised the pool's bclonesaved by 334 MB
(its compressed size); a --reflink=never copy took 277 MB. The
script's bcloneused counter showed 0 because its "version" was itself
made by cp -a on the same filesystem, which reflinks by default
(coreutils 9.7), so the blocks were already shared. A dataset's own used
charges cloned blocks in full: the reflink and plain copies each raised
it by 336 MB.
What we learned¶
- An eager clone costs a fraction of a second. Cloning cost grows with
entries and blocks, about 0.1 ms per entry plus the blocks; a real Rust
service's 1.9 GB
target/clones in 0.38 s, against a 37 s cold build. - A job that never touches its pad wastes about half a second for that
target/(clone 0.38 s, deleting the replaced version 0.16 s, flush 0.01 s), about 1% of what the pad saves. - Copy on first write without root isn't possible through a host-side overlay (part A), and in-sandbox overlays copy bytes.
- ZFS quotas count clones in full, since a dataset's
useddoes.
What it changes¶
- The eager reflink clone stands. Its waste on an untouched pad is too small to justify an overlay even if one were possible.
- A ZFS quota on the runner's data dataset would fill at the apparent size of all its clones, not their real disk use; worth saying in the deployment guide (kata m6tj).
C. A parallel clone, with a library¶
Gavin, after part B: try a parallel clone, and first look for a small,
well-used library. github.com/otiai10/copy (MIT, ~1,300 importing
modules) copies a tree with a pool of workers (NumOfWorkers) and keeps
modes and times (PreserveTimes). It has no reflink option, but with
default options it copies each file through Go's io.Copy into an
*os.File, which on Linux is copy_file_range, which ZFS (2.2+ with block
cloning), XFS and btrfs turn into a clone. (opencoff/go-fio's clone.Tree
calls FICLONE directly, but it has one importer and a GPL-2.0 licence.)
Method: spike/padclone (otiai10/copy) and
spike/pad_clone_parallel.sh on minipool/allmyram-dev, three runs each,
against cp -a --reflink=always. For comparison only, a hand-written
version (a walk plus FICLONE per file across 16 workers, keeping hard
links) ran alongside; it was then removed from the spike. Block sharing
is read from the pool's bclonesaved after a ZFS commit.
Results (2026-11-25):
webhooks target/ (3,868 entries, 1.9 GB; cold build 34 s) |
Go build cache (7,345 entries, 942 MB) | |
|---|---|---|
cp -a --reflink=always |
0.39–0.40 s (2.7 s on the first, cold run) | 0.65–0.66 s |
| otiai10/copy, 1 worker | 0.44–0.46 s | 0.72–0.73 s |
| otiai10/copy, 4 workers | 0.15 s | 0.24–0.27 s |
| otiai10/copy, 16 workers | 0.10–0.14 s | 0.16–0.18 s |
hand-written FICLONE, 16 workers |
0.13 s | 0.22–0.26 s |
- The library's copies are full clones. 2,000 files of 20 KB each:
the library and
FICLONEeach shared 40.96 MB of blocks (bclonesaved). One 300 MiB file with a partial last block: each shared 314.6 MB. Identical to the byte. - Cargo treats a library-cloned
target/as up to date: put in the original's place,cargo test --no-runrecompiled 0 crates. - A misreading, corrected: the first check of the
target/read the pool's free space, which fell 103 MB against 951 MB shared, and looked like 10% copied. minipool holds other busy datasets, so its free space moves on its own: in the small-file and big-file test it came out negative. Onlybclonesavedis a reliable measure on a shared pool.
What part C changes¶
- Pads clone with otiai10/copy at 16 workers: a 1.9 GB
target/in about 0.1 s, 3–4 times faster thancp. - Its differences from a direct
FICLONE: hard links become separate files (sharing blocks, so free on disk, but counted twice in apparent sizes); extended attributes aren't copied; and where cloning is impossible,copy_file_rangequietly copies bytes instead of failing, so the runner's startup check, not the copy, is what proves cloning works. In return,copy_file_rangeworks across two mounts of one filesystem, whereFICLONEreturnsEXDEV, which matters under systemd's per-path bind mounts.