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

A store of fixed-length files, and the volume made once on a computer

vcvkk5e6304613f89
diff --git a/tools/apfs/mkstore.py b/tools/apfs/mkstore.py
new file mode 100644
index 0000000..5cefe91
--- /dev/null
+++ b/tools/apfs/mkstore.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+"""Build the store volume: an APFS container whose root holds the fixed-length slot files.
+
+The kernel's APFS driver reads and does not write, and making a file is a transaction (see
+bsd/miscfs/apfs/apfs_store.c and tools/apfs/txn-probe.py for what one costs). So the volume is
+made here, once, by the same program that makes every other image this port uses -- mkapfs.py --
+and from then on nothing ever changes its structure again: only the bytes inside the slot files
+are overwritten, where they lie, by apfs_write_in_place(). That is the whole reason this idea
+does not fold back into needing a writable APFS, and the price of it is this one step on a
+computer.
+
+The slots are pre-allocated to their full length, so each is one extent with no hole in it --
+a hole would mean allocating a block, which is the thing we are avoiding. They are stored
+uncompressed for the same reason: a compressed file keeps its bytes somewhere else entirely and
+apfs_write_in_place refuses one.
+
+usage: mkstore.py -o IMAGE [--slot-bytes N] [--slots N] [--dense]
+ --slot-bytes N how long each slot is; N may end in K, M or G (default 256M)
+ --slots N how many (default 2: the two-slot discipline needs exactly two, and more are
+ only for testing what the code does with them)
+ --dense do not dig the zero blocks back out of the image file afterwards
+
+The image is a whole container and goes onto the flash as one: into the free space of the
+partition darwin, past the blocks the loader and the kernel both refuse to write before
+(BLOCK_WRITE_FIRST / UFS_WRITE_FIRST, block 2042368). Writing it there is a once-only step from
+Android with tools/darwin-write.py and it destroys whatever the store held before, so the image
+is made once and kept.
+
+Check it as any other image is checked: tools/apfs/check-image.sh IMAGE RedwoodStore=DIR, and
+`apfs-tool store info IMAGE` says whether the driver will have it.
+"""
+import argparse
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+VOLUME = 'RedwoodStore' # apfs_store.h: the name is a gate, not decoration
+HEAD = 4096 # APFS_STORE_PAYLOAD: the header block at the front
+
+
+def bytes_arg(text):
+ mult = {'k': 1 << 10, 'm': 1 << 20, 'g': 1 << 30}.get(text[-1:].lower())
+ n = int(text[:-1]) * mult if mult else int(text)
+ if n <= HEAD or n % 4096:
+ raise argparse.ArgumentTypeError(
+ 'a slot must be a whole number of 4096-byte blocks and longer than %d' % HEAD)
+ return n
+
+
+def main(argv):
+ ap = argparse.ArgumentParser(usage=__doc__.strip().splitlines()[2].strip()[7:])
+ ap.add_argument('-o', '--output', required=True)
+ ap.add_argument('--slot-bytes', type=bytes_arg, default=256 << 20)
+ ap.add_argument('--slots', type=int, default=2)
+ ap.add_argument('--dense', action='store_true')
+ args = ap.parse_args(argv)
+ if args.slots < 1 or args.slots > 8:
+ raise SystemExit('mkstore.py: between one and eight slots')
+
+ tmp = tempfile.mkdtemp(prefix='mkstore.')
+ try:
+ for i in range(args.slots):
+ # sparse here; mkapfs.py gives each one a real extent of its whole length
+ with open(os.path.join(tmp, 'slot%d' % i), 'wb') as f:
+ f.truncate(args.slot_bytes)
+ argv = [sys.executable, os.path.join(HERE, 'mkapfs.py'), '-o', args.output,
+ '%s=%s' % (VOLUME, tmp)]
+ if subprocess.call(argv) != 0:
+ raise SystemExit('mkstore.py: mkapfs.py failed')
+ finally:
+ shutil.rmtree(tmp, ignore_errors=True)
+
+ # mkapfs.py writes the zeros out; on this machine they need not take room (on the flash they
+ # will, and the image is written there whole either way)
+ if not args.dense:
+ subprocess.call(['fallocate', '-d', args.output])
+ size = os.path.getsize(args.output)
+ print('mkstore.py: %s: volume %s, %d slots of %d bytes (%d of payload each), container %d '
+ 'bytes' % (args.output, VOLUME, args.slots, args.slot_bytes,
+ args.slot_bytes - HEAD, size))
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_store.c b/xnu/files/bsd/miscfs/apfs/apfs_store.c
new file mode 100644
index 0000000..f1a9088
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/apfs_store.c
@@ -0,0 +1,481 @@
+/*
+ * A store of a few fixed-length files of an APFS volume, overwritten where they lie.
+ *
+ * What this is instead of
+ * -----------------------
+ * The data of iOS survives a boot today by being packed into an archive and written into the
+ * phone's flash at a number in a comment: two slots of two gigabytes each at byte 8589934592 of
+ * the partition darwin, straight onto a character device (userland/src/var_store.c,
+ * bsd/dev/redwood_ufs.c). It works, and its weakness is not the writing but the addressing --
+ * nothing on the medium says those bytes are a store, how long a slot is, or that something else
+ * may not have them. Only the constant does, in two places that have to agree.
+ *
+ * Here the same bytes live in named files of an APFS volume, and where they are comes from the
+ * volume's own records: the slots are two files of the volume's root, a slot's length is its
+ * inode's length, and every write goes through apfs_write_in_place(), which will not touch a
+ * block unless the file's extents and the extent-reference tree both say it is that file's.
+ *
+ * Nothing here needs the transaction machinery. A slot file is made once, of a fixed length,
+ * and from then on only the bytes inside it ever change: no record of the volume is ever
+ * rewritten, so there is nothing to order and no window in which the volume is inconsistent.
+ *
+ * Who makes the volume
+ * --------------------
+ * Not this code, and not the kernel: a writable APFS would be needed for that and it is a piece
+ * of work of its own (tools/apfs/txn-probe.py measures why). The volume is built on the build
+ * machine by tools/apfs/mkstore.py -- which is tools/apfs/mkapfs.py, the same program that makes
+ * every other image this port uses -- and put on the flash once, in the free space of darwin past
+ * the blocks the loader and the kernel refuse to write before. After that its structure never
+ * changes again. That is the whole escape from needing a writable APFS, and it costs one
+ * preparation step on a computer.
+ *
+ * Finding the newest good slot, without a transaction
+ * ---------------------------------------------------
+ * Unchanged from var_store.c, because it never depended on the medium:
+ *
+ * - a save goes into the slot that does not hold the newest good header;
+ * - that slot's header block is zeroed first, so from then until the commit the slot holds
+ * nothing good and a reader takes the other one;
+ * - the payload next, from the front;
+ * - the header last, in one block of its own, carrying the generation, the payload's length
+ * and a sum of it, and a sum of itself.
+ *
+ * So the worst a save that does not finish can do is lose itself. A reader takes the slot with
+ * the highest generation whose header sums right and whose payload sums to what the header says;
+ * a torn one fails one of those and the slot before it, which a save never touches, is still
+ * whole. The header is one block and the payload starts at the next, so no write ever touches
+ * both.
+ *
+ * Sums are FNV-1a over 64 bits, which is what this port uses everywhere else it wants to know
+ * whether a lot of bytes are the bytes it meant (tools/apfs/fnvsum.c, the census lines, the
+ * walks); it is here to catch a torn or half-written slot, not an adversary.
+ */
+
+#include "apfs_store.h"
+
+#define FNV_INIT 0xcbf29ce484222325ULL
+#define FNV_PRIME 0x100000001b3ULL
+
+static uint64_t
+fnv1a(const void *data, size_t len, uint64_t h)
+{
+ const uint8_t *p = data;
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ h ^= p[i];
+ h *= FNV_PRIME;
+ }
+ return h;
+}
+
+/* ---------------------------------------------------------------- the header on disk */
+
+static void
+put_le32(uint8_t *p, uint32_t v)
+{
+ p[0] = (uint8_t)v;
+ p[1] = (uint8_t)(v >> 8);
+ p[2] = (uint8_t)(v >> 16);
+ p[3] = (uint8_t)(v >> 24);
+}
+
+static void
+put_le64(uint8_t *p, uint64_t v)
+{
+ put_le32(p, (uint32_t)v);
+ put_le32(p + 4, (uint32_t)(v >> 32));
+}
+
+static uint32_t
+get_le32(const uint8_t *p)
+{
+ return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
+}
+
+static uint64_t
+get_le64(const uint8_t *p)
+{
+ return (uint64_t)get_le32(p) | (uint64_t)get_le32(p + 4) << 32;
+}
+
+/* The header block as it goes out: head_sum covers the bytes before it and is written over zero. */
+static void
+head_pack(uint8_t *blk, const struct apfs_store_head *h)
+{
+ memset(blk, 0, APFS_STORE_PAYLOAD);
+ memcpy(blk, APFS_STORE_MAGIC, 8);
+ put_le32(blk + 8, h->version);
+ put_le32(blk + 12, h->slot);
+ put_le64(blk + 16, h->generation);
+ put_le64(blk + 24, h->bytes);
+ put_le64(blk + 32, h->seconds);
+ put_le64(blk + 40, h->note);
+ put_le64(blk + 48, h->sum);
+ put_le64(blk + 56, fnv1a(blk, 56, FNV_INIT));
+}
+
+static bool
+head_unpack(const uint8_t *blk, int slot, uint64_t payload_max, struct apfs_store_head *h)
+{
+ if (memcmp(blk, APFS_STORE_MAGIC, 8) != 0) {
+ return false;
+ }
+ if (get_le64(blk + 56) != fnv1a(blk, 56, FNV_INIT)) {
+ return false; /* a header half written, or never written at all */
+ }
+ h->version = get_le32(blk + 8);
+ h->slot = get_le32(blk + 12);
+ h->generation = get_le64(blk + 16);
+ h->bytes = get_le64(blk + 24);
+ h->seconds = get_le64(blk + 32);
+ h->note = get_le64(blk + 40);
+ h->sum = get_le64(blk + 48);
+ if (h->version != APFS_STORE_VERSION || h->slot != (uint32_t)slot || h->generation == 0 ||
+ h->bytes > payload_max) {
+ return false;
+ }
+ return true;
+}
+
+/* ---------------------------------------------------------------- the slots */
+
+static int
+slot_read(struct apfs_store *st, int slot, uint64_t off, void *buf, size_t len)
+{
+ struct apfs_inode ino;
+ struct apfs_cfile cf;
+ size_t got = 0;
+ int error;
+
+ if (slot < 0 || slot >= APFS_STORE_SLOTS || len == 0) {
+ return EINVAL;
+ }
+ if (off > st->slot_bytes || len > st->slot_bytes - off) {
+ return EINVAL;
+ }
+ error = apfs_inode_get(st->vol, st->slot_id[slot], &ino);
+ if (error) {
+ return error;
+ }
+ memset(&cf, 0, sizeof(cf));
+ error = apfs_read_file(st->vol, &ino, &cf, off, buf, len, &got);
+ apfs_cfile_fini(&cf);
+ if (error == 0 && got != len) {
+ error = EIO;
+ }
+ return error;
+}
+
+static int
+slot_write(struct apfs_store *st, int slot, uint64_t off, const void *buf, size_t len,
+ uint64_t *blocks)
+{
+ struct apfs_write_report rep;
+ int error;
+
+ if (slot < 0 || slot >= APFS_STORE_SLOTS) {
+ return EINVAL;
+ }
+ error = apfs_write_in_place(st->vol, st->slot_id[slot], off, buf, len, &rep);
+ if (blocks != NULL) {
+ *blocks += rep.blocks;
+ }
+ return error;
+}
+
+int
+apfs_store_open(struct apfs_vol *vol, struct apfs_store *st, const char **why)
+{
+ static const char *const names[APFS_STORE_SLOTS] = { APFS_STORE_SLOT0, APFS_STORE_SLOT1 };
+ struct apfs_xattr xa;
+ const char *reason = NULL;
+ uint64_t size = 0;
+ int i, error = 0;
+
+ memset(st, 0, sizeof(*st));
+ st->vol = vol;
+ /*
+ * The name is a gate, not decoration: it is the only thing that says this volume is the
+ * store, and without it a store pointed at the wrong volume would write over whatever
+ * files of it happened to be called slot0 and slot1.
+ */
+ if (strncmp(vol->name, APFS_STORE_VOLNAME, sizeof(vol->name)) != 0) {
+ reason = "the volume is not named " APFS_STORE_VOLNAME;
+ error = ENOENT;
+ goto out;
+ }
+ /*
+ * Whether the store can be written is not asked here. A store is read at every boot off a
+ * volume mounted read-only -- which is every mount this driver makes -- and only the one
+ * program that saves ever has a device that takes writes. apfs_write_in_place() asks the
+ * question itself, at the first block, and nothing between here and there can change the
+ * answer; apfs_store_writable() is for a caller that wants to say so up front.
+ */
+ for (i = 0; i < APFS_STORE_SLOTS; i++) {
+ struct apfs_inode ino;
+ uint64_t id = 0;
+ uint8_t type = 0;
+
+ error = apfs_lookup(vol, APFS_ROOT_DIR_INO_NUM, names[i], strlen(names[i]), &id,
+ &type);
+ if (error != 0 || type != APFS_DT_REG) {
+ reason = "the volume's root has no such slot file";
+ error = ENOENT;
+ goto out;
+ }
+ error = apfs_inode_get(vol, id, &ino);
+ if (error) {
+ reason = "a slot file's inode cannot be read";
+ goto out;
+ }
+ if ((ino.mode & APFS_S_IFMT) != APFS_S_IFREG) {
+ reason = "a slot is not a plain file";
+ error = ENOENT;
+ goto out;
+ }
+ /*
+ * The likeliest way to get a store wrong is to build its volume with
+ * compression on: a compressed file keeps its bytes in an attribute or a
+ * resource fork and has no data stream at all, so it would be refused anyway --
+ * but by a message that does not say what to change (mkstore.py does not pass
+ * -z to mkapfs.py; something else did).
+ */
+ if (apfs_xattr_lookup(vol, id, APFS_DECMPFS_XATTR_NAME, &xa) == 0) {
+ reason = "a slot was stored compressed: its volume must be built without -z";
+ error = ENOENT;
+ goto out;
+ }
+ if (!ino.has_dstream) {
+ reason = "a slot has no data stream";
+ error = ENOENT;
+ goto out;
+ }
+ if (ino.size <= APFS_STORE_PAYLOAD) {
+ reason = "a slot is too short to hold a header and anything else";
+ error = ENOENT;
+ goto out;
+ }
+ if (i == 0) {
+ size = ino.size;
+ } else if (ino.size != size) {
+ reason = "the slots are not the same length";
+ error = ENOENT;
+ goto out;
+ }
+ st->slot_id[i] = id;
+ }
+ if (st->slot_id[0] == st->slot_id[1]) {
+ reason = "both slots are the same file";
+ error = ENOENT;
+ goto out;
+ }
+ st->slot_bytes = size;
+ st->payload_max = size - APFS_STORE_PAYLOAD;
+out:
+ if (why != NULL) {
+ *why = reason;
+ }
+ if (error != 0) {
+ apfs_log("store: %s\n", reason != NULL ? reason : "cannot be opened");
+ }
+ return error;
+}
+
+int
+apfs_store_writable(struct apfs_store *st, const char **why)
+{
+ return apfs_write_volume_ok(st->vol, why);
+}
+
+int
+apfs_store_head(struct apfs_store *st, int slot, struct apfs_store_head *head)
+{
+ uint8_t *blk = apfs_alloc(APFS_STORE_PAYLOAD);
+ int error;
+
+ if (blk == NULL) {
+ return ENOMEM;
+ }
+ error = slot_read(st, slot, 0, blk, APFS_STORE_PAYLOAD);
+ if (error == 0 && !head_unpack(blk, slot, st->payload_max, head)) {
+ error = ENOENT;
+ }
+ apfs_free(blk, APFS_STORE_PAYLOAD);
+ return error;
+}
+
+/* Does the payload still sum to what the header says? This is the second reading of it. */
+static int
+slot_payload_ok(struct apfs_store *st, int slot, const struct apfs_store_head *h, void *scratch,
+ size_t scratch_len, bool *ok)
+{
+ uint64_t off = 0, sum = FNV_INIT;
+
+ *ok = false;
+ if (scratch == NULL || scratch_len == 0) {
+ return EINVAL;
+ }
+ while (off < h->bytes) {
+ size_t want = (size_t)(h->bytes - off);
+ int error;
+
+ if (want > scratch_len) {
+ want = scratch_len;
+ }
+ error = slot_read(st, slot, APFS_STORE_PAYLOAD + off, scratch, want);
+ if (error) {
+ return error;
+ }
+ sum = fnv1a(scratch, want, sum);
+ off += want;
+ }
+ *ok = (sum == h->sum);
+ return 0;
+}
+
+int
+apfs_store_newest(struct apfs_store *st, void *scratch, size_t scratch_len,
+ struct apfs_store_head *head)
+{
+ int best = -1, slot;
+
+ for (slot = 0; slot < APFS_STORE_SLOTS; slot++) {
+ struct apfs_store_head h;
+ bool ok = false;
+
+ if (apfs_store_head(st, slot, &h) != 0) {
+ continue;
+ }
+ if (best >= 0 && h.generation <= head->generation) {
+ continue; /* older than one we already have; do not pay to sum it */
+ }
+ if (slot_payload_ok(st, slot, &h, scratch, scratch_len, &ok) != 0 || !ok) {
+ apfs_log("store: slot %d says generation %llu but its payload does not "
+ "sum right: passing over it\n", slot,
+ (unsigned long long)h.generation);
+ continue;
+ }
+ *head = h;
+ best = slot;
+ }
+ return best;
+}
+
+int
+apfs_store_read(struct apfs_store *st, int slot, uint64_t off, void *buf, size_t len)
+{
+ if (off > st->payload_max || len > st->payload_max - off) {
+ return EINVAL;
+ }
+ return slot_read(st, slot, APFS_STORE_PAYLOAD + off, buf, len);
+}
+
+/* ---------------------------------------------------------------- saving */
+
+int
+apfs_store_begin(struct apfs_store *st, void *scratch, size_t scratch_len,
+ struct apfs_store_write *w)
+{
+ struct apfs_store_head newest;
+ uint8_t *blk;
+ int best, error;
+
+ memset(w, 0, sizeof(*w));
+ memset(&newest, 0, sizeof(newest));
+ w->st = st;
+ w->sum = FNV_INIT;
+ best = apfs_store_newest(st, scratch, scratch_len, &newest);
+ /* the slot that is not the newest good one; either, when neither is good */
+ w->slot = (best == 0) ? 1 : 0;
+ w->generation = newest.generation + 1;
+
+ blk = apfs_alloc(APFS_STORE_PAYLOAD);
+ if (blk == NULL) {
+ return ENOMEM;
+ }
+ /*
+ * The old header out first, over a block of zeros. Until the commit this slot says
+ * nothing, which is the point: a save that stops anywhere in the middle is not mistaken
+ * for the save before it.
+ */
+ memset(blk, 0, APFS_STORE_PAYLOAD);
+ error = slot_write(st, w->slot, 0, blk, APFS_STORE_PAYLOAD, &w->blocks);
+ apfs_free(blk, APFS_STORE_PAYLOAD);
+ if (error == 0) {
+ w->open = true;
+ }
+ return error;
+}
+
+int
+apfs_store_put(struct apfs_store_write *w, const void *data, size_t len)
+{
+ int error;
+
+ if (!w->open) {
+ return EINVAL;
+ }
+ if (len == 0) {
+ return 0;
+ }
+ if (w->at > w->st->payload_max || len > w->st->payload_max - w->at) {
+ apfs_log("store: %llu bytes more than the slot's %llu will hold\n",
+ (unsigned long long)len, (unsigned long long)w->st->payload_max);
+ return ENOSPC;
+ }
+ error = slot_write(w->st, w->slot, APFS_STORE_PAYLOAD + w->at, data, len, &w->blocks);
+ if (error) {
+ return error;
+ }
+ w->sum = fnv1a(data, len, w->sum);
+ w->at += len;
+ return 0;
+}
+
+int
+apfs_store_commit(struct apfs_store_write *w, uint64_t seconds, uint64_t note)
+{
+ struct apfs_store_head h;
+ uint8_t *blk;
+ int error;
+
+ if (!w->open) {
+ return EINVAL;
+ }
+ memset(&h, 0, sizeof(h));
+ h.version = APFS_STORE_VERSION;
+ h.slot = (uint32_t)w->slot;
+ h.generation = w->generation;
+ h.bytes = w->at;
+ h.seconds = seconds;
+ h.note = note;
+ h.sum = w->sum;
+
+ blk = apfs_alloc(APFS_STORE_PAYLOAD);
+ if (blk == NULL) {
+ return ENOMEM;
+ }
+ head_pack(blk, &h);
+ error = slot_write(w->st, w->slot, 0, blk, APFS_STORE_PAYLOAD, &w->blocks);
+ apfs_free(blk, APFS_STORE_PAYLOAD);
+ if (error == 0) {
+ w->open = false;
+ }
+ return error;
+}
+
+int
+apfs_store_save(struct apfs_store *st, const void *data, uint64_t len, uint64_t seconds,
+ uint64_t note, void *scratch, size_t scratch_len, struct apfs_store_write *w)
+{
+ int error = apfs_store_begin(st, scratch, scratch_len, w);
+
+ if (error == 0 && len != 0) {
+ error = apfs_store_put(w, data, (size_t)len);
+ }
+ if (error == 0) {
+ error = apfs_store_commit(w, seconds, note);
+ }
+ return error;
+}
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_store.h b/xnu/files/bsd/miscfs/apfs/apfs_store.h
new file mode 100644
index 0000000..d59ebb7
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/apfs_store.h
@@ -0,0 +1,104 @@
+/*
+ * A store of a few fixed-length files of an APFS volume, overwritten where they lie.
+ *
+ * See apfs_store.c for the shape of it and for what it is instead of.
+ */
+
+#ifndef _MISCFS_APFS_APFS_STORE_H_
+#define _MISCFS_APFS_APFS_STORE_H_
+
+#include "apfs_write.h"
+
+/* The volume this will touch and no other: its name, and the names of the slots in its root. */
+#define APFS_STORE_VOLNAME "RedwoodStore"
+#define APFS_STORE_SLOTS 2
+#define APFS_STORE_SLOT0 "slot0"
+#define APFS_STORE_SLOT1 "slot1"
+
+/* The first block of a slot is its header alone; the payload begins after it. */
+#define APFS_STORE_PAYLOAD 4096
+#define APFS_STORE_MAGIC "RDWSTOR"
+#define APFS_STORE_VERSION 1
+#define APFS_STORE_HEAD_LEN 64
+
+/*
+ * What a slot says about itself. On disk this is APFS_STORE_HEAD_LEN bytes at the front of the
+ * slot's first block, little-endian, the rest of the block zero; in memory it is this.
+ */
+struct apfs_store_head {
+ uint32_t version;
+ uint32_t slot;
+ uint64_t generation; /* the newest good slot is the one with the highest */
+ uint64_t bytes; /* the payload, as the writer handed it over */
+ uint64_t seconds; /* when it was written, by the writer's clock */
+ uint64_t note; /* the writer's own number, kept and handed back */
+ uint64_t sum; /* of the payload */
+};
+
+/* The store, as apfs_store_open() finds it. The caller keeps this while it uses the store. */
+struct apfs_store {
+ struct apfs_vol *vol;
+ uint64_t slot_id[APFS_STORE_SLOTS]; /* the inodes of the slot files */
+ uint64_t slot_bytes; /* the length of a slot, from its own inode */
+ uint64_t payload_max; /* slot_bytes - APFS_STORE_PAYLOAD */
+};
+
+/* A save in progress: begun, written to, then committed. */
+struct apfs_store_write {
+ struct apfs_store *st;
+ int slot;
+ uint64_t generation; /* one more than the newest good one */
+ uint64_t at; /* payload bytes put so far */
+ uint64_t sum;
+ uint64_t blocks; /* blocks written, for the report */
+ bool open;
+};
+
+/*
+ * Find the store on a volume: its name must be APFS_STORE_VOLNAME, and its root must hold the
+ * slots, each a plain file of the same length and long enough for a header. ENOENT with the
+ * reason in *why; *why is NULL when all is well. This says nothing about whether the store can
+ * be written -- see apfs_store_writable().
+ */
+int apfs_store_open(struct apfs_vol *vol, struct apfs_store *st, const char **why);
+
+/* Would a save go through on this volume at all? 0, or EROFS with the reason in *why. */
+int apfs_store_writable(struct apfs_store *st, const char **why);
+
+/*
+ * The newest slot whose header and payload both check out, or -1 with *head left alone. `scratch`
+ * is where the payload is read in pieces to sum it; a bigger one costs fewer reads and nothing
+ * else. This is the whole of "finding the newest good one", and it needs no transaction: a slot
+ * says what generation it is, and whether it is torn.
+ */
+int apfs_store_newest(struct apfs_store *st, void *scratch, size_t scratch_len,
+ struct apfs_store_head *head);
+
+/* The header of one slot as it reads now; ENOENT when that slot holds nothing good. */
+int apfs_store_head(struct apfs_store *st, int slot, struct apfs_store_head *head);
+
+/* Payload bytes of one slot, which the caller has already satisfied itself about. */
+int apfs_store_read(struct apfs_store *st, int slot, uint64_t off, void *buf, size_t len);
+
+/*
+ * Begin a save. It goes into the slot that does not hold the newest good header, and the first
+ * thing written is that slot's header block zeroed -- so from here until the commit the slot
+ * holds nothing good, and the other slot is what a reader will take.
+ */
+int apfs_store_begin(struct apfs_store *st, void *scratch, size_t scratch_len,
+ struct apfs_store_write *w);
+
+/* More payload, in order from the front. ENOSPC when it would not fit the slot. */
+int apfs_store_put(struct apfs_store_write *w, const void *data, size_t len);
+
+/*
+ * Finish it: the header block, written last, which is what makes everything before it good at
+ * once. After this the slot is the newest.
+ */
+int apfs_store_commit(struct apfs_store_write *w, uint64_t seconds, uint64_t note);
+
+/* A whole payload in one call: begin, put, commit. */
+int apfs_store_save(struct apfs_store *st, const void *data, uint64_t len, uint64_t seconds,
+ uint64_t note, void *scratch, size_t scratch_len, struct apfs_store_write *w);
+
+#endif /* _MISCFS_APFS_APFS_STORE_H_ */
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index 1299f00..74ccac3 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -62,7 +62,7 @@ index a4d25040f38f..aad72993d139 100644
kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..8631bb972944 100644
+index 44274016a4cf..9cd24b42caca 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
@@ -109,6 +109,37 @@ bsd/dev/munge.c optional config_requires_u32_munging
@@ -103,7 +103,7 @@ index 44274016a4cf..8631bb972944 100644
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +186,13 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +186,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
@@ -111,13 +111,14 @@ index 44274016a4cf..8631bb972944 100644
+bsd/miscfs/apfs/apfs_zbitmap.c optional apfs
+bsd/miscfs/apfs/apfs_lib.c optional apfs
+bsd/miscfs/apfs/apfs_write.c optional apfs
++bsd/miscfs/apfs/apfs_store.c optional apfs
+bsd/miscfs/apfs/apfs_vfsops.c optional apfs
+bsd/miscfs/apfs/apfs_vnops.c optional apfs
+bsd/miscfs/ramfs/ramfs.c optional ramfs
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +399,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +400,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -125,7 +126,7 @@ index 44274016a4cf..8631bb972944 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +422,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +423,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