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: read a file's data a whole extent at a time (this turns out to be slower)
vcvkk70fd0169984c
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.cindex f95ad03..6f18667 100644--- a/tools/apfs/apfs-tool.c+++ b/tools/apfs/apfs-tool.c@@ -80,6 +80,8 @@ open_image(const char *path, struct apfs_nx *nx, struct apfs_vol *vol)dev.ctx = (void *)(intptr_t)fd;dev.size = (uint64_t)lseek(fd, 0, SEEK_END);dev.read = file_read;+ /* pread already fetches a whole run in one call */+ dev.read_bulk = file_read;error = apfs_nx_open(nx, &dev);if (error) {@@ -135,6 +137,8 @@ cmd_info(const char *path)v.tree.key_count, v.tree.node_count, v.tree.flags);}printf(" blocks read %" PRIu64 ", cache hits %" PRIu64 "\n", nx.reads, nx.hits);+ printf(" file-data requests %" PRIu64 ", bytes %" PRIu64 "\n", nx.bulk_calls,+ nx.bulk_bytes);apfs_nx_close(&nx);return 0;}@@ -480,7 +484,8 @@ cmd_census(const char *path, const char *where)return 1;}fprintf(stderr, "apfs-tool: %ld entries, %" PRIu64 " blocks read, %" PRIu64- " cache hits\n", cs.seen, nx.reads, nx.hits);+ " cache hits, %" PRIu64 " file-data requests for %" PRIu64 " bytes\n",+ cs.seen, nx.reads, nx.hits, nx.bulk_calls, nx.bulk_bytes);free(cs.path);free(cs.buf);apfs_nx_close(&nx);diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.cindex 63daed6..c38dc1b 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c@@ -161,6 +161,35 @@ nx_block(struct apfs_nx *nx, uint64_t addr, const uint8_t **out, bool verify)return 0;}+/*+ * A run of whole blocks of file data, straight into the caller's buffer. Nothing is+ * cached: see the note on read_bulk in struct apfs_dev.+ */+static int+nx_bulk(struct apfs_nx *nx, uint64_t off, void *buf, size_t len)+{+ int error;++ if (len == 0) {+ return 0;+ }+ if ((off % nx->block_size) != 0 || (len % nx->block_size) != 0) {+ return EIO;+ }+ if (off / nx->block_size >= nx->block_count ||+ len / nx->block_size > nx->block_count - off / nx->block_size) {+ return EIO;+ }+ error = (nx->dev.read_bulk != NULL)+ ? nx->dev.read_bulk(nx->dev.ctx, off, buf, len)+ : nx->dev.read(nx->dev.ctx, off, buf, len);+ if (error == 0) {+ nx->bulk_calls++;+ nx->bulk_bytes += len;+ }+ return error;+}+static uint32_tobj_type(const uint8_t *blk){@@ -1463,28 +1492,50 @@ apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,{uint64_t from = logical > off ? logical : off;uint64_t to = (logical + length) < end ? (logical + length) : end;- uint64_t inner = from - logical;+ uint64_t dev_off = phys * (uint64_t)bs + (from - logical);uint64_t nbytes = to - from;- uint64_t addr = phys + inner / bs;- uint64_t skew = inner % bs;-- while (nbytes > 0) {- const uint8_t *blk;+ uint8_t *dp = dst + (from - off);+ const uint8_t *blk;++ /*+ * The whole blocks in the middle go to the device in one request and land+ * in the caller's buffer directly. Only the ends, where the request does+ * not line up with a block, go through the block cache -- one block each.+ * File data has no object header, so none of it is checksummed.+ */+ if ((dev_off % bs) != 0) {+ uint64_t skew = dev_off % bs;uint64_t n = bs - skew;if (n > nbytes) {n = nbytes;}- /* file data has no object header and so no checksum */- error = nx_block(vol->nx, addr, &blk, false);+ error = nx_block(vol->nx, dev_off / bs, &blk, false);+ if (error) {+ return error;+ }+ memcpy(dp, blk + skew, (size_t)n);+ dp += n;+ dev_off += n;+ nbytes -= n;+ }+ if (nbytes >= bs) {+ uint64_t n = (nbytes / bs) * bs;++ error = nx_bulk(vol->nx, dev_off, dp, (size_t)n);if (error) {return error;}- memcpy(dst + (from - off), blk + skew, (size_t)n);- from += n;+ dp += n;+ dev_off += n;nbytes -= n;- addr++;- skew = 0;+ }+ if (nbytes > 0) {+ error = nx_block(vol->nx, dev_off / bs, &blk, false);+ if (error) {+ return error;+ }+ memcpy(dp, blk, (size_t)nbytes);}}}diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.hindex 5d29e00..da10dea 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h@@ -60,6 +60,15 @@ 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);+ /*+ * The same, for a file's contents rather than metadata: a whole run of blocks+ * straight into the caller's buffer, in as few requests to the device as it can+ * manage, and not through any cache. Metadata is read over and over and is worth+ * caching; a file's bytes are usually wanted once, and putting them through the+ * cache a block at a time cost one request to the device per 4096 bytes and pushed+ * the metadata out of it. May be NULL, and then read() is used instead.+ */+ int (*read_bulk)(void *ctx, uint64_t off, void *buf, size_t len);};/* One cached block of metadata. */@@ -104,6 +113,8 @@ struct apfs_nx {uint32_t clock;uint64_t reads;uint64_t hits;+ uint64_t bulk_calls; /* requests for file data, outside the cache */+ uint64_t bulk_bytes;};/* A volume inside the container. */diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex 1975c12..fa09b3c 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -70,6 +70,51 @@ apfs_dev_read(void *ctx, uint64_t off, void *buf, size_t len)return 0;}+/*+ * A file's contents, straight into the caller's buffer. One buffer of our own per+ * request, outside the buffer cache: these bytes are usually wanted once, and putting a+ * 64 KB read through the cache a block at a time meant sixteen requests to the device and+ * sixteen cached blocks nobody would ask for again.+ */+#define APFS_BULK_MAX (64 * 1024)++static int+apfs_dev_read_bulk(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_BULK_MAX ? len : APFS_BULK_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);+ }+ buf_free(bp);+ if (error) {+ return error;+ }+ out += n;+ off += n;+ len -= n;+ }+ return 0;+}+/* ------------------------------------------------------------------ vnodes */static uint32_t@@ -245,6 +290,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_bulk = apfs_dev_read_bulk;error = apfs_nx_open(&am->am_nx, &dev);if (error) {@@ -394,6 +440,11 @@ apfs_unmount(mount_t mp, int mntflags, vfs_context_t ctx)if (error) {return error;}+ printf("apfs: %llu blocks read for metadata (%llu cache hits), %llu requests for "+ "file data carrying %llu bytes\n",+ (unsigned long long)am->am_nx.reads, (unsigned long long)am->am_nx.hits,+ (unsigned long long)am->am_nx.bulk_calls,+ (unsigned long long)am->am_nx.bulk_bytes);vfs_setfsprivate(mp, NULL);if (am->am_owns_devvp && am->am_devvp != NULLVP) {(void)VNOP_CLOSE(am->am_devvp, FREAD, ctx);