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 on the phone: the volume mounts on real hardware and reads the same bytes
vcvkk0f3e193a16dc
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.cindex bd12985..9e4349e 100644--- a/tools/apfs/apfs-tool.c+++ b/tools/apfs/apfs-tool.c@@ -13,7 +13,7 @@* apfs-tool ls IMAGE [PATH]* apfs-tool stat IMAGE PATH* apfs-tool cat IMAGE PATH- * apfs-tool census IMAGE [PATH]+ * apfs-tool census IMAGE [PATH] [--meta] (--meta: do not read contents)*//* pread(2) and clock_gettime(2) come with -D_DEFAULT_SOURCE, set by build-tool.sh */@@ -336,6 +336,7 @@ struct census {uint8_t *buf;long max;long seen;+ bool meta_only;int error;};@@ -408,8 +409,8 @@ census_cb(void *ctx, const char *name, size_t name_len, uint64_t file_id, uint8_if (apfs_inode_size(cs->vol, &ino, &size) != 0) {size = ino.size;}- if ((ino.mode & APFS_S_IFMT) == APFS_S_IFREG ||- (ino.mode & APFS_S_IFMT) == APFS_S_IFLNK) {+ if (!cs->meta_only && ((ino.mode & APFS_S_IFMT) == APFS_S_IFREG ||+ (ino.mode & APFS_S_IFMT) == APFS_S_IFLNK)) {h = file_hash(cs, &ino, size);}printf("%s\t%u\t%06o\t%u\t%u\t%" PRIu64 "\t%016" PRIx64 "\n", cs->path, d_type,@@ -436,7 +437,7 @@ census_dir(struct census *cs, uint64_t dir_id)}static int-cmd_census(const char *path, const char *where)+cmd_census(const char *path, const char *where, bool meta_only){struct apfs_nx nx;struct apfs_vol vol;@@ -458,6 +459,7 @@ cmd_census(const char *path, const char *where)cs.path = malloc(cs.path_cap);cs.buf = malloc(65536);cs.max = 0;+ cs.meta_only = meta_only;if (cs.path == NULL || cs.buf == NULL) {return 1;}@@ -479,6 +481,10 @@ cmd_census(const char *path, const char *where)}fprintf(stderr, "apfs-tool: %ld entries, %" PRIu64 " blocks read, %" PRIu64" cache hits\n", cs.seen, nx.reads, nx.hits);+ 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,+ nx.max_data_addr * (double)nx.block_size / 1e9);free(cs.path);free(cs.buf);apfs_nx_close(&nx);@@ -494,7 +500,7 @@ main(int argc, char **argv)" apfs-tool ls IMAGE [PATH]\n"" apfs-tool stat IMAGE PATH\n"" apfs-tool cat IMAGE PATH\n"- " apfs-tool census IMAGE [PATH]\n");+ " apfs-tool census IMAGE [PATH] [--meta]\n");return 2;}if (strcmp(argv[1], "info") == 0) {@@ -510,7 +516,8 @@ main(int argc, char **argv)return cmd_cat(argv[2], argv[3]);}if (strcmp(argv[1], "census") == 0) {- return cmd_census(argv[2], argc > 3 ? argv[3] : NULL);+ return cmd_census(argv[2], (argc > 3 && argv[3][0] == '/') ? argv[3] : NULL,+ (argc > 3 && strcmp(argv[argc - 1], "--meta") == 0));}fprintf(stderr, "apfs-tool: unknown command %s\n", argv[1]);return 2;diff --git a/xnu/files/bsd/dev/redwood_apfs_check.c b/xnu/files/bsd/dev/redwood_apfs_check.cindex 64a74d9..b4ebddf 100644--- a/xnu/files/bsd/dev/redwood_apfs_check.c+++ b/xnu/files/bsd/dev/redwood_apfs_check.c@@ -8,6 +8,13 @@* apfs_check=3 mount it and stop, leaving it for user space to read* (userland/src/apfs_mmap_test.c, which is the only way the* driver's vnop_pagein ever runs)+ * apfs_check=4 mount nothing: walk the root filesystem, which is what the+ * root already is when the volume was handed to the kernel as+ * its root device. This is how the phone is reached: it has+ * no driver for its own storage and no virtio, so the only+ * block device there is a RAM disk, and a RAM disk given to+ * the kernel as the root is mounted by vfs_mountroot.+ * apfs_dev=md1 the device to mount for modes 1 to 3 (default rvblk0)* apfs_subtree=/usr walk only that part of the volume** A line is the same one tools/apfs/apfs-tool's census and `verify.py clines` print:@@ -50,6 +57,9 @@#include <pexpert/pexpert.h>#include <sys/redwood_apfs_check.h>+extern void loader_return(void); /* osfmk/arm64/loader_return.c */+extern void apfs_stats_print(void); /* bsd/miscfs/apfs/apfs_vfsops.c */+#define CHECK_MNT "/private/var/apfs-check"#define CHECK_PARENT "/private/var"#define CHECK_LEAF "apfs-check"@@ -86,6 +96,20 @@ struct census {uint32_t errors;bool print_lines;int depth;+ /*+ * The volume being walked. Walking the root means walking over the mount points of+ * other filesystems -- devfs on /dev above all -- and their contents are not this+ * volume's, so anything that turns out to live on another mount is passed over.+ */+ mount_t mp;+ /*+ * NOCROSSMOUNT when the volume being walked is the root: a lookup then gives the+ * directory this volume has under a mount point rather than the root of whatever is+ * mounted on it, so /dev is counted as the (empty) directory it is here instead of+ * being either skipped or walked as devfs. Not set when the volume is reached+ * through a path that itself crosses into it.+ */+ int lookup_flags;};/* The format bits of a mode, which vnode_getattr does not report in va_mode. */@@ -155,7 +179,7 @@ hash_link(struct census *cs, vnode_t vp, uint64_t *out)return 0;}-static void+static boolcensus_entry(struct census *cs, uint8_t d_type){struct vnode_attr va;@@ -173,11 +197,15 @@ census_entry(struct census *cs, uint8_t d_type)* would look like a lookup failure. Crossing mount points has to stay allowed: the* path leads from the ramfs that holds the mount point into the volume itself.*/- error = vnode_lookup(cs->path, VNODE_LOOKUP_NOFOLLOW, &vp, cs->ctx);+ error = vnode_lookup(cs->path, cs->lookup_flags, &vp, cs->ctx);if (error) {printf("apfs-check: %s: lookup failed: %d\n", cs->path + cs->rel, error);cs->errors++;- return;+ return false;+ }+ if (cs->mp != NULL && vnode_mount(vp) != cs->mp) {+ vnode_put(vp); /* another filesystem is mounted here */+ return false;}VATTR_INIT(&va);VATTR_WANTED(&va, va_mode);@@ -226,6 +254,7 @@ census_entry(struct census *cs, uint8_t d_type)}out:vnode_put(vp);+ return error == 0;}static void census_dir(struct census *cs);@@ -246,8 +275,7 @@ census_child(struct census *cs, const char *name, size_t name_len, uint8_t d_typcs->len += name_len;cs->path[cs->len] = '\0';- census_entry(cs, d_type);- if (d_type == DT_DIR && cs->depth < MAX_DEPTH) {+ if (census_entry(cs, d_type) && d_type == DT_DIR && cs->depth < MAX_DEPTH) {cs->depth++;census_dir(cs);cs->depth--;@@ -265,9 +293,14 @@ census_dir(struct census *cs)off_t offset = 0;int error;- error = vnode_lookup(cs->path, VNODE_LOOKUP_NOFOLLOW, &dvp, cs->ctx);+ error = vnode_lookup(cs->len != 0 ? cs->path : "/", cs->lookup_flags, &dvp,+ cs->ctx);+ if (error == 0 && cs->mp == NULL) {+ cs->mp = vnode_mount(dvp);+ }if (error) {- printf("apfs-check: %s: lookup failed: %d\n", cs->path + cs->rel, error);+ printf("apfs-check: %s: lookup failed: %d\n",+ cs->len != 0 ? cs->path + cs->rel : "/", error);cs->errors++;return;}@@ -373,14 +406,39 @@ apfs_check_thread(void *arg, __unused wait_result_t wr)uint32_t mode = (uint32_t)(uintptr_t)arg;vfs_context_t ctx = vfs_context_kernel();struct census cs;- char devname[] = CHECK_DEV;+ char devname[64] = CHECK_DEV;char subtree[256] = "";uint64_t start, end;int error, tries;bzero(&cs, sizeof(cs));cs.ctx = ctx;- cs.print_lines = (mode >= 2);+ cs.print_lines = (mode == 2);+ cs.lookup_flags = VNODE_LOOKUP_NOFOLLOW |+ ((mode >= 4) ? VNODE_LOOKUP_NOCROSSMOUNT : 0);+ if (PE_parse_boot_arg_str("apfs_dev", subtree, sizeof(subtree)) && subtree[0] != '\0') {+ snprintf(devname, sizeof(devname), "/dev/%s", subtree);+ }+ subtree[0] = '\0';++ cs.path = kalloc_data(PATH_CAP, Z_WAITOK | Z_ZERO);+ cs.buf = kalloc_data(READ_CHUNK, Z_WAITOK);+ if (cs.path == NULL || cs.buf == NULL) {+ printf("apfs-check: out of memory\n");+ return;+ }++ if (mode >= 4) {+ /*+ * The volume is already the root: nothing to mount, and no mount point to+ * make, so none of what follows about /private/var applies.+ */+ cs.rel = 0;+ cs.len = 0;+ cs.path[0] = '\0';+ printf("apfs-check: walking the root filesystem\n");+ goto walk;+ }/** /private/var is a ramfs that userland's init mounts, so the mount point cannot be@@ -407,23 +465,17 @@ apfs_check_thread(void *arg, __unused wait_result_t wr)error = kernel_mount("apfs", NULLVP, NULLVP, CHECK_MNT, devname, 0,MNT_RDONLY | MNT_DONTBROWSE, KERNEL_MOUNT_NOAUTH, ctx);if (error) {- printf("apfs-check: mounting %s on %s failed: %d\n", CHECK_DEV, CHECK_MNT,+ printf("apfs-check: mounting %s on %s failed: %d\n", devname, CHECK_MNT,error);return;}- printf("apfs-check: %s mounted on %s\n", CHECK_DEV, CHECK_MNT);+ printf("apfs-check: %s mounted on %s\n", devname, CHECK_MNT);if (mode >= 3) {/* someone in user space is going to read it; leave it alone */printf("apfs-check: left mounted, not walking it here\n");return;}- cs.path = kalloc_data(PATH_CAP, Z_WAITOK | Z_ZERO);- cs.buf = kalloc_data(READ_CHUNK, Z_WAITOK);- if (cs.path == NULL || cs.buf == NULL) {- printf("apfs-check: out of memory\n");- return;- }cs.rel = sizeof(CHECK_MNT) - 1;strlcpy(cs.path, CHECK_MNT, PATH_CAP);cs.len = cs.rel;@@ -439,6 +491,7 @@ apfs_check_thread(void *arg, __unused wait_result_t wr)printf("apfs-check: walking the whole volume\n");}+walk:start = mach_absolute_time();census_dir(&cs);end = mach_absolute_time();@@ -454,6 +507,7 @@ apfs_check_thread(void *arg, __unused wait_result_t wr)printf("apfs-check: content bytes %llu\n", cs.bytes);printf("apfs-check: line-hash sum %016llx\n", (unsigned long long)cs.sum);printf("apfs-check: errors %u\n", cs.errors);+ apfs_stats_print();printf("apfs-check: compare with tools/apfs/verify.py sum\n");kfree_data(cs.path, PATH_CAP);@@ -469,6 +523,19 @@ redwood_apfs_check_init(void)if (!PE_parse_boot_argn("apfs_check", &mode, sizeof(mode)) || mode == 0) {return;}+ if (mode >= 4) {+ /*+ * The volume is the root, so everything the walk needs is already up -- and+ * bsd_init is about to exec /sbin/launchd off it, which on anything but our+ * own packfs image ends in a panic before a thread of our own would finish.+ * So walk it here, on this thread, and go back to the loader with the answer+ * rather than letting that happen.+ */+ apfs_check_thread((void *)(uintptr_t)mode, 0);+ printf("apfs-check: going back to the loader\n");+ loader_return();+ return;+ }if (kernel_thread_start(apfs_check_thread, (void *)(uintptr_t)mode, &th) !=KERN_SUCCESS) {printf("apfs-check: cannot start the thread\n");diff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.hindex 4583ec1..0b74d38 100644--- a/xnu/files/bsd/miscfs/apfs/apfs.h+++ b/xnu/files/bsd/miscfs/apfs/apfs.h@@ -83,6 +83,9 @@ int apfs_node_vnode(struct apfs_mount *am, uint64_t obj_id, vnode_t dvp,struct componentname *cnp, vnode_t *vpp);void apfs_node_detach(struct apfs_node *an);+/* the breakdown of where the time went, straight to the log (apfs_vfsops.c) */+void apfs_stats_print(void);+void apfs_lock(struct apfs_mount *am);void apfs_unlock(struct apfs_mount *am);diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.cindex fa51816..98c15b3 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c@@ -118,6 +118,9 @@ nx_block(struct apfs_nx *nx, uint64_t addr, const uint8_t **out, bool verify)if (addr == 0 || addr >= nx->block_count) {return EIO;}+ if (addr > nx->max_addr) {+ nx->max_addr = addr;+ }for (i = 0; i < APFS_CACHE_BLOCKS; i++) {if (nx->cache[i].addr == addr && nx->cache[i].data != NULL) {nx->cache[i].age = ++nx->clock;@@ -1466,6 +1469,9 @@ apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,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) */{uint64_t from = logical > off ? logical : off;diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.hindex ff909f1..db95c7b 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h@@ -129,6 +129,13 @@ struct apfs_nx {* Ticks, not nanoseconds -- see apfs_ticks().*/uint64_t dev_calls, dev_ticks, dev_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+ * that says how much of the front of the image would be enough.+ */+ 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;};diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex 632cff6..b773109 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -147,6 +147,42 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGSreturn 0;}+/*+ * The same breakdown, straight to the log. Needed where there is no user space to read+ * the sysctl: when the volume is the root, nothing else is running.+ */+void+apfs_stats_print(void)+{+ struct apfs_mount *am = apfs_the_mount;+ struct apfs_nx *nx;++ if (am == NULL) {+ printf("apfs: nothing mounted\n");+ return;+ }+ nx = &am->am_nx;+ printf("apfs: pagein %llu calls %llu ms %llu bytes\n",+ (unsigned long long)am->am_pagein_calls,+ (unsigned long long)apfs_ticks_ms(am->am_pagein_ticks),+ (unsigned long long)am->am_pagein_bytes);+ printf("apfs: read %llu calls %llu ms %llu bytes\n",+ (unsigned long long)am->am_read_calls,+ (unsigned long long)apfs_ticks_ms(am->am_read_ticks),+ (unsigned long long)am->am_read_bytes);+ printf("apfs: device %llu calls %llu ms %llu bytes\n",+ (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",+ (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);+ 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);+}+SYSCTL_PROC(_kern, OID_AUTO, apfs_stats, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,0, 0, sysctl_apfs_stats, "A", "APFS: where the time goes; write to zero it");