Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

APFS: 2.3 times the speed for one reader, 7.5 for eight -- what each step gave

vcvkk73d0da1a0885
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.c
index 9e4349e..a225967 100644
--- a/tools/apfs/apfs-tool.c
+++ b/tools/apfs/apfs-tool.c
@@ -336,6 +336,7 @@ struct census {
uint8_t *buf;
long max;
long seen;
+ size_t chunk; /* how much each read asks for */
bool meta_only;
int error;
};
@@ -352,7 +353,7 @@ file_hash(struct census *cs, const struct apfs_inode *ino, uint64_t size)
cf.held = APFS_NO_PIECE;
for (;;) {
size_t got = 0;
- int error = apfs_read_file(cs->vol, ino, &cf, off, cs->buf, 65536, &got);
+ int error = apfs_read_file(cs->vol, ino, &cf, off, cs->buf, cs->chunk, &got);
if (error) {
fprintf(stderr, "apfs-tool: %s: read at %" PRIu64 ": %s\n", cs->path,
@@ -457,7 +458,22 @@ cmd_census(const char *path, const char *where, bool meta_only)
cs.vol = &vol;
cs.path_cap = 4096;
cs.path = malloc(cs.path_cap);
- cs.buf = malloc(65536);
+ /*
+ * APFS_TOOL_CHUNK: reads of another size than 64 KB (say 4096, or 12345 and 200000,
+ * which never line up with a compressed file's 64 KB pieces), so that the roads through
+ * apfs_read_file the census does not otherwise take are checked over a whole volume too.
+ */
+ cs.chunk = 65536;
+ if (getenv("APFS_TOOL_CHUNK") != NULL) {
+ long c = strtol(getenv("APFS_TOOL_CHUNK"), NULL, 0);
+
+ if (c < 1 || c > 16 * 1024 * 1024) {
+ fprintf(stderr, "apfs-tool: APFS_TOOL_CHUNK %ld is not 1..16M\n", c);
+ return 2;
+ }
+ cs.chunk = (size_t)c;
+ }
+ cs.buf = malloc(cs.chunk);
cs.max = 0;
cs.meta_only = meta_only;
if (cs.path == NULL || cs.buf == NULL) {
@@ -481,6 +497,9 @@ cmd_census(const char *path, const char *where, bool meta_only)
}
fprintf(stderr, "apfs-tool: %ld entries, %" PRIu64 " blocks read, %" PRIu64
" cache hits\n", cs.seen, nx.reads, nx.hits);
+ fprintf(stderr, "apfs-tool: inflate %" PRIu64 " calls, %" PRIu64 " bytes in, %" PRIu64
+ " bytes out, %" PRIu64 " by zlib\n", nx.inflate_calls, nx.inflate_in, nx.inflate_out,
+ nx.inflate_fallbacks);
fprintf(stderr, "apfs-tool: highest block touched %" PRIu64 " (%.2f GB in), of which "
"file contents reach %" PRIu64 " (%.2f GB in)\n", nx.max_addr,
nx.max_addr * (double)nx.block_size / 1e9, nx.max_data_addr,
diff --git a/tools/apfs/build-tool.sh b/tools/apfs/build-tool.sh
index 25478e5..e969378 100755
--- a/tools/apfs/build-tool.sh
+++ b/tools/apfs/build-tool.sh
@@ -1,8 +1,9 @@
#!/bin/bash
# Build the kernel's APFS reading code as a program for this machine, so it can be run
-# against a real image before it goes anywhere near the kernel. The .c file is taken from
+# against a real image before it goes anywhere near the kernel. The .c files are taken from
# the kernel tree unchanged; only apfs_lib.h's few allocation and logging macros differ
-# when KERNEL is not defined.
+# when KERNEL is not defined. apfs_deflate.c is libdeflate's decompressor; host-shim/ stands
+# in for its x86 code, which is not in the kernel tree, so the host runs the kernel's decoder.
#
# usage: tools/apfs/build-tool.sh [OUT] (default: apfs-work/apfs-tool)
set -eu
@@ -17,6 +18,11 @@ done
[ -n "$SRC" ] || { echo "build-tool.sh: apfs_lib.c is in neither the kernel tree nor xnu/files" >&2; exit 1; }
mkdir -p "$(dirname "$OUT")"
+OBJ=$(mktemp -d)
+trap 'rm -rf "$OBJ"' EXIT
+# (libdeflate as released compares signed with unsigned in a few places; -Wextra says so)
+cc -std=c11 -O2 -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -I"$HERE/host-shim" -c -o "$OBJ/apfs_deflate.o" \
+ "$SRC/apfs_deflate.c"
cc -std=c11 -D_DEFAULT_SOURCE -O2 -Wall -Wextra -Wno-unused-parameter -I"$SRC" -I"$HERE" \
- -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" -lz
+ -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" -lz
echo "build-tool.sh: $OUT (from $SRC)"
diff --git a/tools/apfs/host-shim/x86/adler32_impl.h b/tools/apfs/host-shim/x86/adler32_impl.h
new file mode 100644
index 0000000..7a95047
--- /dev/null
+++ b/tools/apfs/host-shim/x86/adler32_impl.h
@@ -0,0 +1,5 @@
+/*
+ * Empty on purpose. libdeflate's x86 code is not taken into the kernel tree (the kernel is
+ * arm64); tools/apfs/build-tool.sh finds this file instead, and the host tool runs the same
+ * portable decoder as the kernel.
+ */
diff --git a/tools/apfs/host-shim/x86/decompress_impl.h b/tools/apfs/host-shim/x86/decompress_impl.h
new file mode 100644
index 0000000..7a95047
--- /dev/null
+++ b/tools/apfs/host-shim/x86/decompress_impl.h
@@ -0,0 +1,5 @@
+/*
+ * Empty on purpose. libdeflate's x86 code is not taken into the kernel tree (the kernel is
+ * arm64); tools/apfs/build-tool.sh finds this file instead, and the host tool runs the same
+ * portable decoder as the kernel.
+ */
diff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.h
index 5586aba..f6035ff 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs.h
+++ b/xnu/files/bsd/miscfs/apfs/apfs.h
@@ -28,6 +28,9 @@
struct apfs_node;
+#define APFS_IOBUF 65536 /* what one round of vnop_read reads */
+#define APFS_IOBUF_KEEP 8
+
struct apfs_mount {
mount_t am_mp;
vnode_t am_devvp;
@@ -53,6 +56,12 @@ struct apfs_mount {
uint64_t am_lock_wait_ticks;
uint64_t am_lock_held_ticks;
uint64_t am_lock_t0; /* when the holder took it; only the holder writes */
+ /*
+ * vnop_read's 64 KB buffers, kept for the next read rather than allocated and freed
+ * for every one (at that size the kernel goes to the VM). Under am_lock.
+ */
+ void *am_iobuf[APFS_IOBUF_KEEP];
+ uint32_t am_iobuf_kept;
};
/* The private part of a vnode: which object it is. */
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_deflate.c b/xnu/files/bsd/miscfs/apfs/apfs_deflate.c
new file mode 100644
index 0000000..013a543
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/apfs_deflate.c
@@ -0,0 +1,29 @@
+/*
+ * libdeflate's decompressor, for the zlib pieces of compressed files. The same file is
+ * built into the kernel and into tools/apfs/apfs-tool, so the two run one decoder.
+ *
+ * The sources are bsd/miscfs/apfs/libdeflate as tools/mk-libdeflate.sh takes them from the
+ * release, unchanged. libdeflate is Copyright 2016 Eric Biggers and 2024 Google LLC, under
+ * the MIT licence (libdeflate/COPYING).
+ *
+ * Built FREESTANDING: no C library, memcpy and friends become compiler built-ins, and the
+ * CPU's features are only what the compiler is told -- here, NEON and nothing past it, so
+ * the Adler-32 over each piece's output is libdeflate's NEON one (the decompressor itself
+ * has no arm code of its own). The kernel saves the vector registers on every exception
+ * and its own lz4 decoder uses them, so that is safe here. <arm_neon.h> comes from the
+ * compiler (bsd/conf/Makefile.arm64). What lib/utils.c would add is two pointers, the
+ * allocator, given here instead; its fallback memset/memcpy are not wanted next to the
+ * kernel's own.
+ */
+#define FREESTANDING 1
+
+#include "libdeflate/lib/deflate_decompress.c"
+#include "libdeflate/lib/zlib_decompress.c"
+#include "libdeflate/lib/adler32.c"
+
+/* in apfs_lib.c, which knows how this side allocates */
+extern void *apfs_deflate_malloc(size_t size);
+extern void apfs_deflate_free(void *ptr);
+
+malloc_func_t libdeflate_default_malloc_func = apfs_deflate_malloc;
+free_func_t libdeflate_default_free_func = apfs_deflate_free;
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
index 9dde810..1c88457 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
@@ -18,6 +18,7 @@
#else
#include <zlib.h>
#endif
+#include "libdeflate/libdeflate.h"
/* The pieces a resource-fork-compressed file is cut into. */
#define APFS_RSRC_PIECE APFS_DECMPFS_BLOCK_SIZE
@@ -689,11 +690,15 @@ out:
return error;
}
+static void scratch_drain(struct apfs_nx *nx);
+
void
apfs_nx_close(struct apfs_nx *nx)
{
uint32_t i;
+ scratch_drain(nx);
+
for (i = 0; i < APFS_CACHE_BLOCKS; i++) {
if (nx->cache[i].data != NULL) {
apfs_free(nx->cache[i].data, nx->block_size);
@@ -1521,6 +1526,171 @@ apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,
return 0;
}
+/*
+ * File contents without the lock. apfs_read_stream above fetches a file's data a block
+ * at a time through the block cache, with the caller's lock held all the while: one
+ * request to the device per 4096 bytes, each block copied in and copied out again, and
+ * the cache filled with data that pushes the B-tree nodes out of it. Reading a stream is
+ * split in two here instead. stream_plan(), with the lock, turns the extent records into
+ * runs of blocks on the device; stream_fetch(), without it, reads those runs with
+ * dev.read_data -- one request a run, whole blocks straight into the destination.
+ */
+struct apfs_run {
+ uint64_t at; /* bytes into the request */
+ uint64_t addr; /* the run's first block on the device */
+ uint32_t skew; /* bytes into that block where the run starts */
+ uint64_t len; /* bytes */
+};
+
+#define APFS_MAX_RUNS 8
+
+/*
+ * Where [off, off + len) of a stream lies, as at most `max` runs in order (len already cut
+ * to the stream's size). What the runs leave out of [0, *covered) is holes, read as
+ * zeros. *covered is less than len when there were more than `max` runs: the caller plans
+ * again from there. With the lock held.
+ */
+static int
+stream_plan(struct apfs_vol *vol, uint64_t stream_id, uint64_t off, uint64_t len,
+ struct apfs_run *runs, uint32_t max, uint32_t *nruns, uint64_t *covered)
+{
+ struct apfs_cursor cur;
+ struct apfs_rec rec;
+ uint32_t bs = vol->nx->block_size;
+ uint64_t end = off + len;
+ int error;
+
+ *nruns = 0;
+ *covered = len;
+ error = apfs_seek(&vol->tree, stream_id, APFS_TYPE_FILE_EXTENT, &cur);
+ if (error) {
+ return error;
+ }
+ for (;;) {
+ uint64_t logical, length, phys, from, to, inner;
+
+ error = apfs_cursor_next(&cur, &rec);
+ if (error == ENOENT) {
+ break;
+ }
+ if (error) {
+ return error;
+ }
+ if (rec.obj_id != stream_id || rec.type != APFS_TYPE_FILE_EXTENT) {
+ break;
+ }
+ if (rec.key_len < 16 || rec.val == NULL || rec.val_len < 24) {
+ return EIO;
+ }
+ logical = le64(rec.key + 8);
+ length = le64(rec.val) & APFS_J_FILE_EXTENT_LEN_MASK;
+ phys = le64(rec.val + 8);
+ if (length == 0 || (length % bs) != 0) {
+ return EIO;
+ }
+ if (logical >= end) {
+ break; /* extents are in order: past what we need */
+ }
+ if (logical + length <= off || phys == 0) {
+ continue; /* before what we need, or a hole */
+ }
+ if (phys >= vol->nx->block_count ||
+ length / bs > vol->nx->block_count - phys) {
+ return EIO;
+ }
+ if (phys + length / bs - 1 > vol->nx->max_data_addr) {
+ vol->nx->max_data_addr = phys + length / bs - 1;
+ }
+ /* the overlap of [logical, logical+length) with [off, end) */
+ from = logical > off ? logical : off;
+ to = (logical + length) < end ? (logical + length) : end;
+ inner = from - logical;
+ if (*nruns == max) {
+ *covered = from - off;
+ break;
+ }
+ runs[*nruns].at = from - off;
+ runs[*nruns].addr = phys + inner / bs;
+ runs[*nruns].skew = (uint32_t)(inner % bs);
+ runs[*nruns].len = to - from;
+ (*nruns)++;
+ }
+ return 0;
+}
+
+/* One read of file contents; without the lock. */
+static int
+dev_read_data(struct apfs_nx *nx, uint64_t addr, void *buf, size_t len, uint64_t *calls,
+ uint64_t *bytes)
+{
+ int error = (nx->dev.read_data != NULL)
+ ? nx->dev.read_data(nx->dev.ctx, addr * (uint64_t)nx->block_size, buf, len)
+ : nx->dev.read(nx->dev.ctx, addr * (uint64_t)nx->block_size, buf, len);
+
+ (*calls)++;
+ *bytes += len;
+ return error;
+}
+
+/*
+ * Read what stream_plan found into dst, without the lock: whole blocks straight in, the
+ * ends of a run that do not fill a block through `bounce` (one block), zeros for holes.
+ */
+static int
+stream_fetch(struct apfs_nx *nx, const struct apfs_run *runs, uint32_t nruns,
+ uint64_t covered, uint8_t *dst, uint8_t *bounce, uint64_t *calls, uint64_t *bytes)
+{
+ uint32_t bs = nx->block_size;
+ uint64_t done = 0;
+ uint32_t r;
+ int error;
+
+ for (r = 0; r < nruns; r++) {
+ uint64_t at = runs[r].at, addr = runs[r].addr, left = runs[r].len;
+ uint32_t skew = runs[r].skew;
+
+ if (at > done) {
+ memset(dst + done, 0, (size_t)(at - done));
+ }
+ if (skew != 0 || left < bs) { /* starts inside a block, or is less than one */
+ uint64_t n = bs - skew < left ? bs - skew : left;
+
+ error = dev_read_data(nx, addr, bounce, bs, calls, bytes);
+ if (error) {
+ return error;
+ }
+ memcpy(dst + at, bounce + skew, (size_t)n);
+ at += n;
+ left -= n;
+ addr++;
+ }
+ if (left >= bs) {
+ uint64_t n = left - left % bs;
+
+ error = dev_read_data(nx, addr, dst + at, (size_t)n, calls, bytes);
+ if (error) {
+ return error;
+ }
+ at += n;
+ left -= n;
+ addr += n / bs;
+ }
+ if (left > 0) { /* the start of one more block */
+ error = dev_read_data(nx, addr, bounce, bs, calls, bytes);
+ if (error) {
+ return error;
+ }
+ memcpy(dst + at, bounce, (size_t)left);
+ at += left;
+ }
+ done = at;
+ }
+ if (covered > done) {
+ memset(dst + done, 0, (size_t)(covered - done));
+ }
+ return 0;
+}
+
int
apfs_readlink(struct apfs_vol *vol, uint64_t obj_id, char *buf, size_t len, size_t *out)
{
@@ -1745,6 +1915,41 @@ apfs_cfile_init(struct apfs_vol *vol, const struct apfs_inode *ino,
return 0;
}
+/*
+ * libdeflate allocates its decompressor through these (apfs_deflate.c). It frees by pointer
+ * alone, so each block carries its own length in front of it, as for zlib below.
+ */
+void *
+apfs_deflate_malloc(size_t size)
+{
+ size_t n = size + sizeof(size_t);
+ uint8_t *p;
+
+ if (size > (~(size_t)0) - sizeof(size_t)) {
+ return NULL;
+ }
+ p = apfs_alloc(n);
+ if (p == NULL) {
+ return NULL;
+ }
+ memcpy(p, &n, sizeof(n));
+ return p + sizeof(size_t);
+}
+
+void
+apfs_deflate_free(void *ptr)
+{
+ uint8_t *p;
+ size_t n;
+
+ if (ptr == NULL) {
+ return;
+ }
+ p = (uint8_t *)ptr - sizeof(size_t);
+ memcpy(&n, p, sizeof(n));
+ apfs_free(p, n);
+}
+
/*
* zlib asks for its own working memory through these. The kernel's copy of zlib has no
* allocator of its own -- a zeroed z_stream leaves zalloc NULL and inflateInit_ then calls
@@ -1789,7 +1994,56 @@ apfs_zfree(voidpf opaque, voidpf address)
apfs_free(p, n);
}
-/* One zlib stream. A leading 0xff means the piece was stored rather than compressed. */
+/*
+ * One zlib stream through libdeflate; a leading 0xff means the piece was stored, not
+ * compressed. libdeflate takes the whole stream at once and decodes with large tables:
+ * over all 19149 pieces of the restore ramdisk, 740 MB/s on the build machine against
+ * zlib's 344, and on the phone 1248 ms against 3834 for one reader (856 once its Adler-32
+ * is the NEON one). It checks the stream's Adler-32 just as zlib does. A stream it will
+ * not take is handed to zlib instead and counted, so that such a thing cannot go by
+ * unnoticed -- none of the pieces in either iOS volume is one. `d` is the caller's
+ * decompressor, NULL for one made here for this call alone.
+ */
+static int apfs_inflate_counted(struct apfs_nx *nx, const uint8_t *src, size_t src_len,
+ uint8_t *dst, size_t dst_len, size_t *out);
+
+static int
+apfs_inflate_fast(struct libdeflate_decompressor *d, const uint8_t *src, size_t src_len,
+ uint8_t *dst, size_t dst_len, size_t *out, bool *fell_back)
+{
+ struct libdeflate_decompressor *own = NULL;
+ enum libdeflate_result r;
+ size_t got = 0;
+
+ if (src_len == 0) {
+ return EIO;
+ }
+ if (src[0] == 0xFF) { /* stored, not compressed */
+ size_t n = src_len - 1 < dst_len ? src_len - 1 : dst_len;
+
+ memcpy(dst, src + 1, n);
+ *out = n;
+ return 0;
+ }
+ if (d == NULL) {
+ own = libdeflate_alloc_decompressor();
+ if (own == NULL) {
+ return ENOMEM;
+ }
+ d = own;
+ }
+ r = libdeflate_zlib_decompress(d, src, src_len, dst, dst_len, &got);
+ if (own != NULL) {
+ libdeflate_free_decompressor(own);
+ }
+ if (r == LIBDEFLATE_SUCCESS) {
+ *out = got;
+ return 0;
+ }
+ *fell_back = true; /* counted by the caller, which may not hold the lock now */
+ return apfs_inflate_counted(NULL, src, src_len, dst, dst_len, out);
+}
+
static int
apfs_inflate_counted(struct apfs_nx *nx, const uint8_t *src, size_t src_len, uint8_t *dst,
size_t dst_len, size_t *out)
@@ -1870,8 +2124,15 @@ decmpfs_attr(struct apfs_nx *nx, uint32_t type, const uint8_t *src, size_t src_l
*out = n;
return 0;
}
- case APFS_DECMPFS_TYPE_ZLIB_ATTR:
- return apfs_inflate_counted(nx, src, src_len, dst, want, out);
+ case APFS_DECMPFS_TYPE_ZLIB_ATTR: {
+ bool fb = false;
+ int error = apfs_inflate_fast(NULL, src, src_len, dst, want, out, &fb);
+
+ if (fb && nx != NULL) {
+ nx->inflate_fallbacks++;
+ }
+ return error;
+ }
default:
return ENOTSUP;
}
@@ -1975,13 +2236,105 @@ out:
* through the file -- or a page fault asking for 16 KB of it -- does not decompress the
* same 64 KB again.
*/
+/*
+ * What reading a compressed piece needs besides the file's own state: a decompressor, room
+ * for the compressed bytes, and room for a piece when it goes to the file's cache. Made
+ * and thrown away on every read before, which was three allocations a read -- with the
+ * lock held, and the 64 KB one big enough that the kernel goes to the VM for it. They are
+ * kept on the container now, taken and given back with the lock held, a few at most.
+ */
+#define APFS_PIECE_CAP (APFS_DECMPFS_BLOCK_SIZE + 1024) /* stored: 1 + 64 KB; zlib: a few bytes more */
+#define APFS_SCRATCH_KEEP 16
+
+struct apfs_scratch {
+ struct apfs_scratch *next;
+ struct libdeflate_decompressor *dec;
+ uint8_t *piece; /* APFS_PIECE_CAP */
+ uint8_t *mine; /* APFS_DECMPFS_BLOCK_SIZE, made when first wanted */
+ /*
+ * The blocks a piece lies in, read in one request (APFS_PIECE_CAP and a block either
+ * side); also the bounce block for the ends of runs.
+ */
+ uint8_t *raw;
+ size_t raw_cap;
+};
+
+static void
+scratch_free(struct apfs_scratch *sc)
+{
+ if (sc->dec != NULL) {
+ libdeflate_free_decompressor(sc->dec);
+ }
+ if (sc->piece != NULL) {
+ apfs_free(sc->piece, APFS_PIECE_CAP);
+ }
+ if (sc->mine != NULL) {
+ apfs_free(sc->mine, APFS_DECMPFS_BLOCK_SIZE);
+ }
+ if (sc->raw != NULL) {
+ apfs_free(sc->raw, sc->raw_cap);
+ }
+ apfs_free(sc, sizeof(*sc));
+}
+
+static struct apfs_scratch *
+scratch_get(struct apfs_nx *nx)
+{
+ struct apfs_scratch *sc = nx->scratch;
+
+ if (sc != NULL) {
+ nx->scratch = sc->next;
+ nx->scratch_kept--;
+ sc->next = NULL;
+ return sc;
+ }
+ sc = apfs_alloc(sizeof(*sc));
+ if (sc == NULL) {
+ return NULL;
+ }
+ sc->dec = libdeflate_alloc_decompressor();
+ sc->piece = apfs_alloc(APFS_PIECE_CAP);
+ sc->raw_cap = APFS_PIECE_CAP + 2 * (size_t)nx->block_size;
+ sc->raw = apfs_alloc(sc->raw_cap);
+ if (sc->dec == NULL || sc->piece == NULL || sc->raw == NULL) {
+ scratch_free(sc);
+ return NULL;
+ }
+ return sc;
+}
+
+static void
+scratch_put(struct apfs_nx *nx, struct apfs_scratch *sc)
+{
+ if (nx->scratch_kept >= APFS_SCRATCH_KEEP) {
+ scratch_free(sc);
+ return;
+ }
+ sc->next = nx->scratch;
+ nx->scratch = sc;
+ nx->scratch_kept++;
+}
+
+static void
+scratch_drain(struct apfs_nx *nx)
+{
+ while (nx->scratch != NULL) {
+ struct apfs_scratch *sc = nx->scratch;
+
+ nx->scratch = sc->next;
+ scratch_free(sc);
+ }
+ nx->scratch_kept = 0;
+}
+
static int
read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
struct apfs_cfile *cf, uint64_t off, uint8_t *buf, size_t len, size_t *out)
{
- uint8_t *piece = NULL;
- uint8_t *mine = NULL; /* a piece's worth, decompressed into without the lock */
- size_t piece_cap = 0;
+ struct apfs_scratch *sc = NULL;
+ uint8_t *big = NULL; /* a piece bigger than APFS_PIECE_CAP, should there be one */
+ size_t big_cap = 0;
+ uint32_t bs = vol->nx->block_size;
uint64_t first, last, i;
int error = 0;
@@ -2009,78 +2362,148 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
if ((i + 1) * APFS_DECMPFS_BLOCK_SIZE > cf->size) {
want = (size_t)(cf->size - i * APFS_DECMPFS_BLOCK_SIZE);
}
+ from_in_piece = (off > i * APFS_DECMPFS_BLOCK_SIZE)
+ ? (size_t)(off - i * APFS_DECMPFS_BLOCK_SIZE) : 0;
if (cf->held != i) {
uint32_t plen = cf->piece_len[i];
-
- if (plen > piece_cap) {
- if (piece != NULL) {
- apfs_free(piece, piece_cap);
+ uint64_t poff = cf->table_base + cf->piece_off[i];
+ bool raw_type = (cf->type == APFS_DECMPFS_TYPE_RAW_RSRC);
+ struct apfs_run runs[APFS_MAX_RUNS];
+ uint32_t nruns = 0;
+ uint64_t covered = 0, calls = 0, bytes = 0, t0, t1, t2;
+ bool planned = false, fell_back = false;
+ /*
+ * The whole piece wanted, from its start (a 64 KB read at a 64 KB
+ * offset, which is what read(2) in big chunks and pagein ask for):
+ * straight into the caller's buffer, and the file's cache is left as
+ * it was. Otherwise into a buffer of our own, which goes into the
+ * file's cache once the lock is back, by swapping buffers.
+ */
+ bool direct = !raw_type && from_in_piece == 0 && len - *out >= want;
+ const uint8_t *src;
+ uint8_t *piece, *dst = NULL;
+ size_t got_len = 0;
+
+ if (sc == NULL) {
+ sc = scratch_get(vol->nx);
+ if (sc == NULL) {
+ error = ENOMEM;
+ goto out;
+ }
+ }
+ piece = sc->piece;
+ if (plen <= APFS_PIECE_CAP && poff < cf->rsrc_size &&
+ plen <= cf->rsrc_size - poff) {
+ error = stream_plan(vol, cf->rsrc_id, poff, plen, runs, APFS_MAX_RUNS,
+ &nruns, &covered);
+ if (error) {
+ goto out;
+ }
+ planned = (covered == plen);
+ }
+ if (!planned) {
+ /*
+ * Bigger than a piece should be, running past the fork's end, or
+ * in more runs than a plan holds: the old way, with the lock.
+ */
+ if (plen > APFS_PIECE_CAP) {
+ if (plen > big_cap) {
+ if (big != NULL) {
+ apfs_free(big, big_cap);
+ }
+ big_cap = plen;
+ big = apfs_alloc(big_cap);
+ if (big == NULL) {
+ error = ENOMEM;
+ goto out;
+ }
+ }
+ piece = big;
+ }
+ error = apfs_read_stream(vol, cf->rsrc_id, cf->rsrc_size, poff, piece,
+ plen, &got);
+ if (error) {
+ goto out;
}
- piece_cap = plen;
- piece = apfs_alloc(piece_cap);
- if (piece == NULL) {
+ if (got < plen) {
+ error = EIO;
+ goto out;
+ }
+ }
+ if (!raw_type && !direct && sc->mine == NULL) {
+ sc->mine = apfs_alloc(APFS_DECMPFS_BLOCK_SIZE);
+ if (sc->mine == NULL) {
error = ENOMEM;
goto out;
}
}
- error = apfs_read_stream(vol, cf->rsrc_id, cf->rsrc_size,
- cf->table_base + cf->piece_off[i], piece, plen, &got);
- if (error) {
- goto out;
+ dst = direct ? buf + *out : sc->mine;
+
+ /*
+ * Without the lock from here: reading the piece off the device and
+ * decompressing it touch nothing anyone else can see. Decompressing
+ * is most of the time spent here -- with the lock held through it,
+ * four readers on eight processors kept the lock busy 94% of the
+ * time. Two readers of the same file may decompress the same piece
+ * once each; that is the price, and the cache stays right either way.
+ */
+ t0 = apfs_ticks();
+ nx_unlock(vol->nx);
+ src = piece;
+ if (planned && nruns == 1 && runs[0].at == 0 && runs[0].len == plen) {
+ /* the usual case: the blocks the piece lies in, in one request */
+ size_t span = ((size_t)runs[0].skew + plen + bs - 1) / bs * bs;
+
+ error = dev_read_data(vol->nx, runs[0].addr, sc->raw, span, &calls,
+ &bytes);
+ src = sc->raw + runs[0].skew;
+ } else if (planned) {
+ error = stream_fetch(vol->nx, runs, nruns, covered, piece, sc->raw,
+ &calls, &bytes);
}
- if (got < plen) {
- error = EIO;
+ t1 = apfs_ticks();
+ if (error == 0 && !raw_type) {
+ error = apfs_inflate_fast(sc->dec, src, plen, dst, want, &got_len,
+ &fell_back);
+ }
+ nx_relock(vol->nx);
+ t2 = apfs_ticks();
+ vol->nx->data_calls += calls;
+ vol->nx->data_bytes += bytes;
+ vol->nx->data_ticks += t1 - t0;
+ if (!raw_type) {
+ if (fell_back) {
+ vol->nx->inflate_fallbacks++;
+ }
+ vol->nx->inflate_ticks += t2 - t1;
+ vol->nx->inflate_calls++;
+ vol->nx->inflate_in += plen;
+ vol->nx->inflate_out += got_len;
+ }
+ if (error) {
goto out;
}
- if (cf->type == APFS_DECMPFS_TYPE_RAW_RSRC) {
+ if (raw_type) {
size_t m = plen < want ? (size_t)plen : want;
cf->held = APFS_NO_PIECE;
- memcpy(cf->held_data, piece, m);
+ memcpy(cf->held_data, src, m);
cf->held_len = m;
} else {
- /*
- * Decompress without the caller's lock, into a buffer of our
- * own: zlib touches nothing anyone else can see, and it is most
- * of the time spent here -- with the lock held through it, four
- * readers on eight processors kept the lock busy 94% of the
- * time. The piece only goes into the file's cache once the
- * lock is back, by swapping buffers. Two readers of the same
- * file may decompress the same piece once each; that is the
- * price, and the cache stays right either way.
- */
- size_t got_len = 0;
- uint64_t t0;
-
- if (mine == NULL) {
- mine = apfs_alloc(APFS_DECMPFS_BLOCK_SIZE);
- if (mine == NULL) {
- error = ENOMEM;
- goto out;
- }
- }
- t0 = apfs_ticks();
- nx_unlock(vol->nx);
- error = apfs_inflate_counted(NULL, piece, plen, mine, want,
- &got_len);
- nx_relock(vol->nx);
- vol->nx->inflate_ticks += apfs_ticks() - t0;
- vol->nx->inflate_calls++;
- vol->nx->inflate_in += plen;
- vol->nx->inflate_out += got_len;
- if (error) {
- goto out;
- }
if (got_len != want) {
error = EIO;
goto out;
}
+ if (direct) {
+ *out += want;
+ continue;
+ }
/* in it goes: the old piece's buffer becomes ours */
{
uint8_t *t = cf->held_data;
- cf->held_data = mine;
- mine = t;
+ cf->held_data = sc->mine;
+ sc->mine = t;
}
cf->held_len = got_len;
}
@@ -2090,8 +2513,6 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
}
cf->held = i;
}
- from_in_piece = (off > i * APFS_DECMPFS_BLOCK_SIZE)
- ? (size_t)(off - i * APFS_DECMPFS_BLOCK_SIZE) : 0;
n = cf->held_len - from_in_piece;
if (n > len - *out) {
n = len - *out;
@@ -2100,11 +2521,68 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
*out += n;
}
out:
- if (piece != NULL) {
- apfs_free(piece, piece_cap);
+ if (big != NULL) {
+ apfs_free(big, big_cap);
+ }
+ if (sc != NULL) {
+ scratch_put(vol->nx, sc);
+ }
+ return error;
+}
+
+/*
+ * An uncompressed file's contents: planned with the lock, read without it, whole blocks
+ * straight into the caller's buffer.
+ */
+static int
+read_plain(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size, uint64_t off,
+ uint8_t *buf, size_t len, size_t *out)
+{
+ struct apfs_run runs[APFS_MAX_RUNS];
+ struct apfs_scratch *sc;
+ size_t done = 0;
+ int error = 0;
+
+ *out = 0;
+ if (off >= stream_size) {
+ return 0;
+ }
+ if (len > stream_size - off) {
+ len = (size_t)(stream_size - off);
+ }
+ sc = scratch_get(vol->nx);
+ if (sc == NULL) {
+ return ENOMEM;
+ }
+ while (done < len) {
+ uint32_t nruns = 0;
+ uint64_t covered = 0, calls = 0, bytes = 0, t0;
+
+ error = stream_plan(vol, stream_id, off + done, len - done, runs, APFS_MAX_RUNS,
+ &nruns, &covered);
+ if (error) {
+ break;
+ }
+ if (covered == 0) {
+ error = EIO; /* cannot happen: a plan covers at least its first run */
+ break;
+ }
+ t0 = apfs_ticks();
+ nx_unlock(vol->nx);
+ error = stream_fetch(vol->nx, runs, nruns, covered, buf + done, sc->raw, &calls,
+ &bytes);
+ nx_relock(vol->nx);
+ vol->nx->data_calls += calls;
+ vol->nx->data_bytes += bytes;
+ vol->nx->data_ticks += apfs_ticks() - t0;
+ if (error) {
+ break;
+ }
+ done += (size_t)covered;
}
- if (mine != NULL) {
- apfs_free(mine, APFS_DECMPFS_BLOCK_SIZE);
+ scratch_put(vol->nx, sc);
+ if (error == 0) {
+ *out = done;
}
return error;
}
@@ -2119,17 +2597,24 @@ apfs_read_file(struct apfs_vol *vol, const struct apfs_inode *ino,
*out = 0;
if ((ino->mode & APFS_S_IFMT) == APFS_S_IFLNK) {
+ /*
+ * The whole target, then the part asked for. Reading only `len` bytes of it and
+ * then counting from `off` gave the wrong bytes to anyone reading a link in
+ * pieces smaller than its target (the host tool, reading a byte at a time).
+ */
size_t n = 0;
+ char *target = apfs_alloc(APFS_LINK_MAX);
- error = apfs_readlink(vol, ino->obj_id, buf, len, &n);
- if (error) {
- return error;
+ if (target == NULL) {
+ return ENOMEM;
}
- *out = (off < n) ? n - (size_t)off : 0;
- if (off > 0 && *out > 0) {
- memmove(buf, (uint8_t *)buf + off, *out);
+ error = apfs_readlink(vol, ino->obj_id, target, APFS_LINK_MAX, &n);
+ if (error == 0 && off < n) {
+ *out = n - (size_t)off < len ? n - (size_t)off : len;
+ memcpy(buf, target + off, *out);
}
- return 0;
+ apfs_free(target, APFS_LINK_MAX);
+ return error;
}
/*
* Without a context from the caller, one is made for this call alone -- correct, and
@@ -2145,8 +2630,7 @@ apfs_read_file(struct apfs_vol *vol, const struct apfs_inode *ino,
if (error == 0) {
if (!cf->compressed) {
error = ino->has_dstream
- ? apfs_read_stream(vol, ino->private_id, ino->size, off, buf, len,
- out)
+ ? read_plain(vol, ino->private_id, ino->size, off, buf, len, out)
: 0;
} else if (cf->in_rsrc) {
error = read_cfile_rsrc(vol, ino, cf, off, buf, len, out);
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
index 0acb63a..6605a3a 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
@@ -72,14 +72,24 @@ apfs_ticks(void)
/* How many blocks of metadata to keep; a B-tree descent touches one per level. */
#define APFS_CACHE_BLOCKS 64
+/* Room for a symbolic link's target (Darwin's own limit is 1024 bytes; it is kept in an xattr) */
+#define APFS_LINK_MAX 4096
+
/*
* The device. read() must fill the whole buffer or return an error; offsets and lengths
* are always whole blocks of the container.
+ *
+ * read_data() is the same for the contents of files, which are read once and not kept:
+ * straight into the caller's memory, past any cache, and called without the caller's lock
+ * -- so it must be safe on several threads at once and touch nothing apfs_lib keeps.
+ * NULL: read() is used for those too (still without the lock, so read() must then be safe
+ * that way as well).
*/
struct apfs_dev {
void *ctx;
uint64_t size; /* bytes, 0 if not known */
int (*read)(void *ctx, uint64_t off, void *buf, size_t len);
+ int (*read_data)(void *ctx, uint64_t off, void *buf, size_t len);
};
/* One cached block of metadata. */
@@ -109,6 +119,8 @@ struct apfs_omap {
bool ready;
};
+struct apfs_scratch; /* apfs_lib.c's own */
+
/* A mounted container. */
struct apfs_nx {
struct apfs_dev dev;
@@ -122,9 +134,10 @@ struct apfs_nx {
struct apfs_omap omap; /* resolves volume superblocks */
/*
* The caller's lock round this container, which apfs_lib may let go of while it
- * does work that touches nothing shared -- decompressing a piece into a buffer of
- * its own -- and take back afterwards. Everything else here (the block cache, the
- * cursors, a file's held piece) is only touched with it held. NULL for a caller
+ * does work that touches nothing shared -- reading a file's contents off the device
+ * (dev.read_data), decompressing a piece into a buffer no one else sees -- and take
+ * back afterwards. Everything else here (the block cache, the cursors, a file's held
+ * piece, the scratch list) is only touched with it held. NULL for a caller
* with no lock, such as the single-threaded program on the build machine.
*/
void (*unlock)(void *ctx);
@@ -139,6 +152,8 @@ struct apfs_nx {
* Ticks, not nanoseconds -- see apfs_ticks().
*/
uint64_t dev_calls, dev_ticks, dev_bytes;
+ /* the same for file contents, read without the lock (read_data) */
+ uint64_t data_calls, data_ticks, data_bytes;
/*
* The highest block either kind of read has reached. What it is for: the phone has
* no driver for its own storage, so a volume there has to live in a RAM disk, and
@@ -147,6 +162,13 @@ struct apfs_nx {
uint64_t max_addr; /* any block */
uint64_t max_data_addr; /* blocks of file contents only */
uint64_t inflate_calls, inflate_ticks, inflate_in, inflate_out;
+ uint64_t inflate_fallbacks; /* streams libdeflate refused and zlib took */
+ /*
+ * Decompressors and buffers for reading compressed pieces, kept from one read to the
+ * next instead of allocated for each; touched only with the lock held.
+ */
+ struct apfs_scratch *scratch;
+ uint32_t scratch_kept;
};
/* A volume inside the container. */
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
index e5cfb96..fbb717f 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
@@ -71,6 +71,57 @@ apfs_dev_read(void *ctx, uint64_t off, void *buf, size_t len)
return 0;
}
+/*
+ * A file's contents, straight into the caller's memory: one buffer header of our own and
+ * one VNOP_STRATEGY a request, past the buffer cache. apfs_lib calls this without the
+ * volume's lock, from several threads at once; nothing here is shared but the device.
+ * (Tried once before, in QEMU, in place of the block cache for the middle of a request and
+ * with the lock held, it was slower there; here it is the device reads coming out from
+ * under the lock, on a device where one costs a memory copy -- see apfs_lib.c.)
+ */
+#define APFS_DATA_MAX (128 * 1024)
+
+static int
+apfs_dev_read_data(void *ctx, uint64_t off, void *buf, size_t len)
+{
+ struct apfs_devctx *dc = ctx;
+ char *out = buf;
+
+ if ((off % dc->dc_devblock) != 0 || (len % dc->dc_devblock) != 0) {
+ return EINVAL;
+ }
+ while (len > 0) {
+ size_t n = len < APFS_DATA_MAX ? len : APFS_DATA_MAX;
+ buf_t bp = buf_alloc(dc->dc_devvp);
+ int error;
+
+ if (bp == NULL) {
+ return ENOMEM;
+ }
+ buf_reset(bp, B_READ);
+ buf_setblkno(bp, (daddr64_t)(off / dc->dc_devblock));
+ buf_setlblkno(bp, (daddr64_t)(off / dc->dc_devblock));
+ buf_setcount(bp, (uint32_t)n);
+ buf_setdataptr(bp, (uintptr_t)out);
+ error = VNOP_STRATEGY(bp);
+ if (error == 0) {
+ error = (int)buf_biowait(bp);
+ }
+ /* read() must fill the whole buffer, and so must this (md cuts the count short) */
+ if (error == 0 && (buf_resid(bp) != 0 || buf_count(bp) != n)) {
+ error = EIO;
+ }
+ buf_free(bp);
+ if (error) {
+ return error;
+ }
+ out += n;
+ off += n;
+ len -= n;
+ }
+ return 0;
+}
+
/* ------------------------------------------------------------- where the time goes */
/*
@@ -112,7 +163,8 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS
"pagein %llu calls %llu ms %llu bytes\n"
"read %llu calls %llu ms %llu bytes\n"
"device %llu calls %llu ms %llu bytes\n"
- "inflate %llu calls %llu ms %llu bytes in %llu bytes out\n"
+ "data %llu calls %llu ms %llu bytes\n"
+ "inflate %llu calls %llu ms %llu bytes in %llu bytes out %llu by zlib\n"
"blocks %llu read %llu cache hits\n",
(unsigned long long)am->am_pagein_calls,
(unsigned long long)apfs_ticks_ms(am->am_pagein_ticks),
@@ -123,10 +175,14 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS
(unsigned long long)nx->dev_calls,
(unsigned long long)apfs_ticks_ms(nx->dev_ticks),
(unsigned long long)nx->dev_bytes,
+ (unsigned long long)nx->data_calls,
+ (unsigned long long)apfs_ticks_ms(nx->data_ticks),
+ (unsigned long long)nx->data_bytes,
(unsigned long long)nx->inflate_calls,
(unsigned long long)apfs_ticks_ms(nx->inflate_ticks),
(unsigned long long)nx->inflate_in,
(unsigned long long)nx->inflate_out,
+ (unsigned long long)nx->inflate_fallbacks,
(unsigned long long)nx->reads,
(unsigned long long)nx->hits);
}
@@ -139,8 +195,9 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS
am->am_pagein_calls = am->am_pagein_ticks = am->am_pagein_bytes = 0;
am->am_read_calls = am->am_read_ticks = am->am_read_bytes = 0;
am->am_nx.dev_calls = am->am_nx.dev_ticks = am->am_nx.dev_bytes = 0;
+ am->am_nx.data_calls = am->am_nx.data_ticks = am->am_nx.data_bytes = 0;
am->am_nx.inflate_calls = am->am_nx.inflate_ticks = 0;
- am->am_nx.inflate_in = am->am_nx.inflate_out = 0;
+ am->am_nx.inflate_in = am->am_nx.inflate_out = am->am_nx.inflate_fallbacks = 0;
am->am_nx.reads = am->am_nx.hits = 0;
am->am_lock_waits = am->am_lock_wait_ticks = am->am_lock_held_ticks = 0;
apfs_unlock(am);
@@ -175,10 +232,17 @@ apfs_stats_print(void)
(unsigned long long)nx->dev_calls,
(unsigned long long)apfs_ticks_ms(nx->dev_ticks),
(unsigned long long)nx->dev_bytes);
- printf("apfs: inflate %llu calls %llu ms %llu bytes in %llu bytes out\n",
+ /* file contents, read without the lock (the line above is metadata, with it) */
+ printf("apfs: data %llu calls %llu ms %llu bytes\n",
+ (unsigned long long)nx->data_calls,
+ (unsigned long long)apfs_ticks_ms(nx->data_ticks),
+ (unsigned long long)nx->data_bytes);
+ /* "by zlib": streams libdeflate refused (bad, or more out than the piece holds), zlib's then */
+ printf("apfs: inflate %llu calls %llu ms %llu bytes in %llu bytes out %llu by zlib\n",
(unsigned long long)nx->inflate_calls,
(unsigned long long)apfs_ticks_ms(nx->inflate_ticks),
- (unsigned long long)nx->inflate_in, (unsigned long long)nx->inflate_out);
+ (unsigned long long)nx->inflate_in, (unsigned long long)nx->inflate_out,
+ (unsigned long long)nx->inflate_fallbacks);
printf("apfs: blocks %llu read %llu cache hits highest block %llu\n",
(unsigned long long)nx->reads, (unsigned long long)nx->hits,
(unsigned long long)nx->max_addr);
@@ -336,6 +400,9 @@ apfs_lib_relock(void *ctx)
static void
apfs_free_mount(struct apfs_mount *am)
{
+ while (am->am_iobuf_kept > 0) {
+ kfree_data(am->am_iobuf[--am->am_iobuf_kept], APFS_IOBUF);
+ }
apfs_nx_close(&am->am_nx);
lck_mtx_destroy(&am->am_lock, &apfs_lck_grp);
kfree_type(struct apfs_mount, am);
@@ -379,6 +446,7 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)
dev.ctx = dc;
dev.size = devblocks * (uint64_t)devblock;
dev.read = apfs_dev_read;
+ dev.read_data = apfs_dev_read_data;
error = apfs_nx_open(&am->am_nx, &dev);
if (error) {
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c b/xnu/files/bsd/miscfs/apfs/apfs_vnops.c
index 686f4eb..a0c44b7 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_vnops.c
@@ -24,7 +24,6 @@
#include <vfs/vfs_support.h>
/* How much is read out of a file at a time when copying into a uio. */
-#define APFS_IOBUF 65536
void
apfs_lock(struct apfs_mount *am)
@@ -180,37 +179,47 @@ apfs_vnop_read(struct vnop_read_args *ap)
}
ino = APFS_VP(vp)->an_ino;
size = APFS_VP(vp)->an_size;
- buf = kalloc_data(APFS_IOBUF, Z_WAITOK);
- if (buf == NULL) {
- return ENOMEM;
- }
t0 = apfs_ticks();
+ apfs_lock(am);
+ if (am->am_iobuf_kept > 0) {
+ buf = am->am_iobuf[--am->am_iobuf_kept];
+ } else {
+ apfs_unlock(am);
+ buf = kalloc_data(APFS_IOBUF, Z_WAITOK);
+ if (buf == NULL) {
+ return ENOMEM;
+ }
+ apfs_lock(am);
+ }
+ /* the lock is held at the top of every round and let go of only for uiomove */
while (uio_resid(uio) > 0 && (uint64_t)uio_offset(uio) < size) {
size_t want = (size_t)MIN((user_ssize_t)APFS_IOBUF, uio_resid(uio));
size_t got = 0;
- apfs_lock(am);
error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf,
(uint64_t)uio_offset(uio), buf, want, &got);
- apfs_unlock(am);
- if (error) {
- break;
- }
- if (got == 0) {
+ if (error || got == 0) {
break;
}
+ apfs_unlock(am);
error = uiomove((caddr_t)buf, (int)got, uio);
+ apfs_lock(am);
if (error) {
break;
}
read_bytes += got;
}
- kfree_data(buf, APFS_IOBUF);
- apfs_lock(am);
+ if (am->am_iobuf_kept < APFS_IOBUF_KEEP) {
+ am->am_iobuf[am->am_iobuf_kept++] = buf;
+ buf = NULL;
+ }
am->am_read_ticks += apfs_ticks() - t0;
am->am_read_calls++;
am->am_read_bytes += read_bytes;
apfs_unlock(am);
+ if (buf != NULL) {
+ kfree_data(buf, APFS_IOBUF);
+ }
return error;
}
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index 8c19945..3111f18 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -45,8 +45,24 @@ index 222158a93918..68bb0f3e56bf 100755
set path = ($path .)
######################################################################
# HISTORY
+diff --git a/bsd/conf/Makefile.arm64 b/bsd/conf/Makefile.arm64
+index a4d25040f38f..aad72993d139 100644
+--- a/bsd/conf/Makefile.arm64
++++ b/bsd/conf/Makefile.arm64
+@@ -5,6 +5,11 @@
+ # Inline assembly doesn't interact well with LTO
+ fbt_arm.o_CFLAGS_ADD += $(CFLAGS_NOLTO_FLAG)
+
++# libdeflate's arm headers (bsd/miscfs/apfs/libdeflate) include the compiler's own <arm_acle.h>
++# and <arm_neon.h>, which the kernel's include paths do not have: they come after those paths,
++# so the kernel's <stdint.h> and the rest are still the ones found.
++apfs_deflate.o_CFLAGS_ADD += -idirafter $(shell $(CC) -print-resource-dir)/include
++
+ kern_csr.o_INCFLAGS += -I$(SRCROOT)/osfmk
+ kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
+
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..489ab8da091f 100644
+index 44274016a4cf..8cf6540f1a50 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
@@ -109,6 +109,34 @@ bsd/dev/munge.c optional config_requires_u32_munging
@@ -84,10 +100,11 @@ index 44274016a4cf..489ab8da091f 100644
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +183,13 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +183,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
++bsd/miscfs/apfs/apfs_deflate.c optional apfs
+bsd/miscfs/apfs/apfs_lib.c optional apfs
+bsd/miscfs/apfs/apfs_vfsops.c optional apfs
+bsd/miscfs/apfs/apfs_vnops.c optional apfs
@@ -98,7 +115,7 @@ index 44274016a4cf..489ab8da091f 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +396,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +397,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -106,7 +123,7 @@ index 44274016a4cf..489ab8da091f 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +419,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +420,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard