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: every name found by its hash, Unicode and case-insensitive volumes included
vcvkke333c69848f8
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.cindex bc66d3f..05937ee 100644--- a/tools/apfs/apfs-tool.c+++ b/tools/apfs/apfs-tool.c@@ -1081,6 +1081,132 @@ cmd_cfrace(const char *path, int n, int rounds)return atomic_load(&cf_bad) != 0;}+/*+ * namehash IMAGE OUT: every directory record's name hashed the way the lookup hashes it,+ * against the hash APFS stored with it; and into OUT, for tools/apfs/test_namehash.py to work+ * out independently, a line per record: directory, the name in hex, the stored hash, the hash+ * as on a case-sensitive volume and as on a case-insensitive one, the file's id.+ */+struct nh_ctx {+ FILE *out;+ bool fold;+ uint64_t total, ascii, other, same, differ, unhashable;+};++static int+namehash_cb(void *ctx, uint64_t dir_id, const char *name, size_t name_len, uint32_t stored, uint64_t file_id)+{+ struct nh_ctx *c = ctx;+ uint32_t cs = 0, ci = 0, mine;+ bool ok_cs = apfs_name_hash(name, name_len, false, &cs), ok_ci = apfs_name_hash(name, name_len, true, &ci);+ bool ascii = true;++ for (size_t i = 0; i < name_len; i++) {+ ascii &= (uint8_t)name[i] < 0x80;+ }+ c->total++;+ if (ascii) {+ c->ascii++;+ } else {+ c->other++;+ }+ mine = c->fold ? ci : cs;+ if (!(c->fold ? ok_ci : ok_cs)) {+ c->unhashable++;+ } else if (mine == stored) {+ c->same++;+ } else {+ c->differ++;+ fprintf(stderr, "apfs-tool: namehash: dir %" PRIu64 ": %.*s: stored %06x, worked out %06x\n", dir_id,+ (int)name_len, name, stored, mine);+ }+ if (c->out != NULL) {+ fprintf(c->out, "%" PRIu64 "\t", dir_id);+ for (size_t i = 0; i < name_len; i++) {+ fprintf(c->out, "%02x", (uint8_t)name[i]);+ }+ fprintf(c->out, "\t%06x\t%06x\t%06x\t%" PRIu64 "\n", stored, ok_cs ? cs : 0xffffffu, ok_ci ? ci : 0xffffffu,+ file_id);+ }+ return 0;+}++static int+cmd_namehash(const char *path, const char *out)+{+ struct apfs_nx nx;+ struct apfs_vol vol;+ struct nh_ctx c;+ int error;++ if (open_image(path, &nx, &vol) != 0) {+ return 1;+ }+ memset(&c, 0, sizeof(c));+ c.fold = (vol.incompat & APFS_INCOMPAT_CASE_INSENSITIVE) != 0;+ c.out = out != NULL ? fopen(out, "w") : NULL;+ error = apfs_dirrec_walk(&vol, namehash_cb, &c);+ if (c.out != NULL) {+ fclose(c.out);+ }+ fprintf(stderr, "apfs-tool: namehash: %s volume: %" PRIu64 " directory records (%" PRIu64 " plain ASCII, %" PRIu64+ " not): hash as stored %" PRIu64 ", not %" PRIu64 ", cannot be hashed %" PRIu64 "%s\n",+ c.fold ? "case-insensitive" : "case-sensitive", c.total, c.ascii, c.other, c.same, c.differ, c.unhashable,+ error ? " (WALK FAILED)" : "");+ return (error || c.differ || c.unhashable) ? 1 : 0;+}++/*+ * lookupnames IMAGE LIST: a line of LIST is a directory, a name in hex and what looking it up+ * must give (an id, or "none"); each looked up the way the kernel's vnop_lookup does it.+ */+static int+cmd_lookupnames(const char *path, const char *list)+{+ struct apfs_nx nx;+ struct apfs_vol vol;+ FILE *f = fopen(list, "r");+ char line[4096];+ uint64_t total = 0, right = 0, hashed0, scanned0;++ if (f == NULL || open_image(path, &nx, &vol) != 0) {+ return 1;+ }+ hashed0 = nx.lookups_hashed;+ scanned0 = nx.lookups_scanned;+ while (fgets(line, sizeof(line), f) != NULL) {+ char hex[2048], want[32], name[1024];+ uint64_t dir, id = 0;+ size_t n = 0;+ uint8_t dt;+ int error;++ if (sscanf(line, "%" SCNu64 " %2047s %31s", &dir, hex, want) != 3) {+ continue;+ }+ for (size_t i = 0; hex[2 * i] != 0 && hex[2 * i + 1] != 0 && n < sizeof(name); i++) {+ unsigned v;++ sscanf(hex + 2 * i, "%2x", &v);+ name[n++] = (char)v;+ }+ total++;+ error = apfs_lookup(&vol, dir, name, n, &id, &dt);+ if ((strcmp(want, "none") == 0 && error == ENOENT) ||+ (strcmp(want, "none") != 0 && error == 0 && id == strtoull(want, NULL, 0))) {+ right++;+ } else if (total - right <= 20) {+ fprintf(stderr, "apfs-tool: lookupnames: dir %" PRIu64 " %s: want %s, got %s %" PRIu64 "\n", dir, hex, want,+ error ? strerror(error) : "id", id);+ }+ }+ fclose(f);+ fprintf(stderr, "apfs-tool: lookupnames: %" PRIu64 " names, %" PRIu64 " as expected, %" PRIu64 " not; %" PRIu64+ " found by the hash, %" PRIu64 " by reading the directory\n", total, right, total - right,+ nx.lookups_hashed - hashed0, nx.lookups_scanned - scanned0);+ return right == total ? 0 : 1;+}+/** Every name in the volume looked up by name, the way the kernel's vnop_lookup does it,* against what reading its directory gave; and each name with "#" after it, which must@@ -1248,7 +1374,9 @@ main(int argc, char **argv)" apfs-tool cat IMAGE PATH\n"" apfs-tool census IMAGE [PATH] [--meta]\n"" apfs-tool lookups IMAGE\n"- " apfs-tool cfrace IMAGE [THREADS [ROUNDS]]\n");+ " apfs-tool cfrace IMAGE [THREADS [ROUNDS]]\n"+ " apfs-tool namehash IMAGE [OUT]\n"+ " apfs-tool lookupnames IMAGE LIST\n");return 2;}if (strcmp(argv[1], "info") == 0) {@@ -1270,6 +1398,12 @@ main(int argc, char **argv)if (strcmp(argv[1], "lookups") == 0) {return cmd_lookups(argv[2]);}+ if (strcmp(argv[1], "namehash") == 0) {+ return cmd_namehash(argv[2], argc > 3 ? argv[3] : NULL);+ }+ if (strcmp(argv[1], "lookupnames") == 0 && argc > 3) {+ return cmd_lookupnames(argv[2], argv[3]);+ }if (strcmp(argv[1], "cfrace") == 0) {return cmd_cfrace(argv[2], argc > 3 ? atoi(argv[3]) : 4, argc > 4 ? atoi(argv[4]) : 3);}diff --git a/tools/apfs/build-tool.sh b/tools/apfs/build-tool.shindex 076468e..ad5b8dd 100755--- a/tools/apfs/build-tool.sh+++ b/tools/apfs/build-tool.sh@@ -25,6 +25,20 @@ 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 $TOOL_CFLAGS -I"$HERE/host-shim" -c -o "$OBJ/apfs_deflate.o" \"$SRC/apfs_deflate.c"+# Names that are not plain ASCII are hashed and compared after Unicode decomposition (and folding):+# with the kernel's own code for that, bsd/vfs/vfs_unicode.c, when the kernel tree is here.+UNI=""+XNUBSD=$(cd "$SRC/../.." && pwd)+if [ -f "$XNUBSD/vfs/vfs_unicode.c" ] && [ -f "$XNUBSD/sys/unicode.h" ]; then+ mkdir -p "$OBJ/inc/sys" "$OBJ/inc/libkern"+ cp "$XNUBSD/sys/unicode.h" "$OBJ/inc/sys/unicode.h"+ printf '#include <stdbool.h>\n#include <stdint.h>\n#include <stddef.h>\n#include <string.h>\n' > "$OBJ/inc/libkern/libkern.h"+ cc -std=c11 -D_DEFAULT_SOURCE -DKERNEL_PRIVATE -O2 -w $TOOL_CFLAGS -I"$OBJ/inc" -I"$XNUBSD/vfs" -c -o "$OBJ/vfs_unicode.o" \+ "$XNUBSD/vfs/vfs_unicode.c"+ UNI="-DAPFS_HOST_UNICODE -DKERNEL_PRIVATE -I$OBJ/inc $OBJ/vfs_unicode.o"+else+ echo "build-tool.sh: no bsd/vfs/vfs_unicode.c: names other than plain ASCII are found by reading the directory" >&2+ficc -std=c11 -D_DEFAULT_SOURCE -O2 -Wall -Wextra -Wno-unused-parameter -I"$SRC" -I"$HERE" \- $TOOL_CFLAGS -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" -lz -pthread+ $TOOL_CFLAGS -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" $UNI -lz -pthreadecho "build-tool.sh: $OUT (from $SRC)"diff --git a/tools/apfs/test_namenorm.py b/tools/apfs/test_namenorm.pynew file mode 100755index 0000000..50482cb--- /dev/null+++ b/tools/apfs/test_namenorm.py@@ -0,0 +1,108 @@+#!/usr/bin/env python3+# The name hashes and lookups of apfs_lib, checked independently over every name of a volume.+#+# 1. apfs-tool namehash IMAGE TSV (a line per directory record: dir, name in hex, the hash+# stored, apfs_lib's hash case-sensitive and case-insensitive,+# the file's id)+# 2. test_namenorm.py TSV LIST this: works the hashes out its own way -- Python's+# unicodedata, NFD (and casefold of the NFD, NFD again, the+# "canonical caseless form"), CRC-32C over UTF-32LE from all+# ones, low 22 bits -- and compares them with apfs_lib's and+# with the stored ones; and writes LIST: names to look up, in+# other forms than stored, with what the lookup must give+# 3. apfs-tool lookupnames IMAGE LIST looks them up through apfs_lookup+#+# What a lookup must give is worked out from the directory's own names: on a case-sensitive,+# normalization-insensitive volume (iOS) a name matches the entry whose NFD is the same; another+# composition of a stored name (NFC of an NFD name, say) finds that entry; a case variant finds+# only an entry with exactly that name; the name with "#" after it, nothing.++import struct+import sys+import unicodedata++POLY = 0x82F63B78+TABLE = []+for _i in range(256):+ _c = _i+ for _ in range(8):+ _c = (_c >> 1) ^ POLY if _c & 1 else _c >> 1+ TABLE.append(_c)+++def crc_hash(text):+ crc = 0xFFFFFFFF+ for ch in text:+ for b in struct.pack("<I", ord(ch)):+ crc = TABLE[(crc ^ b) & 0xFF] ^ (crc >> 8)+ return crc & 0x3FFFFF+++def nfd(s):+ return unicodedata.normalize("NFD", s)+++def caseless(s):+ return nfd(nfd(s).casefold())+++def main():+ if len(sys.argv) != 3:+ print("usage: test_namenorm.py TSV LIST", file=sys.stderr)+ return 2+ recs = []+ for line in open(sys.argv[1]):+ d, hx, stored, cs, ci, fid = line.split("\t")+ recs.append((int(d), bytes.fromhex(hx), int(stored, 16), int(cs, 16), int(ci, 16), int(fid)))+ dirs = {}+ for d, raw, *_rest, fid in recs:+ dirs.setdefault(d, {})[raw] = fid+ n = len(recs)+ other = [r for r in recs if any(b >= 0x80 for b in r[1])]+ cs_ref = ci_ref = stored_ref = undecodable = 0+ bad = []+ for d, raw, stored, cs, ci, fid in recs:+ try:+ name = raw.decode("utf-8")+ except UnicodeDecodeError:+ undecodable += 1+ continue+ rc, ri = crc_hash(nfd(name)), crc_hash(caseless(name))+ cs_ref += rc == cs+ ci_ref += ri == ci+ stored_ref += rc == stored+ if (rc != cs or ri != ci or rc != stored) and len(bad) < 10:+ bad.append((name, stored, cs, rc, ci, ri))+ print("names %d (%d not plain ASCII), not UTF-8 %d; Python's NFD hash = apfs_lib's case-sensitive %d, "+ "= stored %d; Python's caseless hash = apfs_lib's case-insensitive %d; Unicode %s here" %+ (n, len(other), undecodable, cs_ref, stored_ref, ci_ref, unicodedata.unidata_version))+ for b in bad:+ print(" %r: stored %06x, apfs_lib cs %06x, python %06x; apfs_lib ci %06x, python %06x" % b)++ # the lookups, for every name not plain ASCII and a sample of the rest+ out = open(sys.argv[2], "w")+ count = 0+ for i, (d, raw, stored, cs, ci, fid) in enumerate(recs):+ if all(b < 0x80 for b in raw) and i % 97 != 0:+ continue+ name = raw.decode("utf-8")+ by_nfd = {}+ for r, f in dirs[d].items():+ try:+ by_nfd.setdefault(nfd(r.decode("utf-8")), f)+ except UnicodeDecodeError:+ pass+ variants = {name, unicodedata.normalize("NFC", name), nfd(name), name.upper(), name.lower(),+ name.swapcase(), name + "#"}+ for v in variants:+ want = by_nfd.get(nfd(v))+ out.write("%d\t%s\t%s\n" % (d, v.encode("utf-8").hex(), "none" if want is None else str(want)))+ count += 1+ out.close()+ print("lookups written: %d" % count)+ ok = cs_ref == n - undecodable and ci_ref == n - undecodable and stored_ref == n - undecodable+ return 0 if ok else 1+++if __name__ == "__main__":+ sys.exit(main())diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.cindex 4c652d0..d7aab02 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c@@ -13,6 +13,13 @@#include "apfs_lib.h"+#if defined(KERNEL) || defined(APFS_HOST_UNICODE)+#define APFS_HAVE_UNICODE 1+#include <sys/unicode.h> /* utf8_normalizeOptCaseFoldAndHash, ...AndCompare (bsd/vfs/vfs_unicode.c) */+#else+#define APFS_HAVE_UNICODE 0+#endif+#ifdef KERNEL#include <libkern/zlib.h>#else@@ -1518,26 +1525,16 @@ ascii_lower(int c)return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;}+static bool names_equal(const char *a, size_t a_len, const char *b, size_t b_len, bool fold);+static intlookup_cb(void *ctx, const char *name, size_t name_len, uint64_t file_id, uint8_t d_type){struct lookup_ctx *lc = ctx;- size_t i;- if (name_len != lc->name_len) {+ if (!names_equal(name, name_len, lc->name, lc->name_len, lc->fold)) {return 0;}- for (i = 0; i < name_len; i++) {- int a = (unsigned char)name[i], b = (unsigned char)lc->name[i];-- if (lc->fold) {- a = ascii_lower(a);- b = ascii_lower(b);- }- if (a != b) {- return 0;- }- }lc->file_id = file_id;lc->d_type = d_type;lc->found = true;@@ -1545,12 +1542,14 @@ lookup_cb(void *ctx, const char *name, size_t name_len, uint64_t file_id, uint8_}/*- * The hash in a directory record's key, for a name of ASCII characters: CRC-32C over the- * name as little-endian 32-bit code points, started at all ones and not inverted at the- * end, low 22 bits. For other names APFS hashes the name after Unicode canonical- * decomposition (and case folding, on a case-insensitive volume), which is not done- * here. Checked against the hash stored with every ASCII name of the iOS 16.5 root- * volume: 317749 of 317749 (tools/apfs/test_namehash.py).+ * The hash in a directory record's key: CRC-32C over the name as little-endian 32-bit code+ * points, started at all ones and not inverted at the end, low 22 bits -- the name taken after+ * Unicode canonical decomposition (NFD), and on a case-insensitive volume in its canonical+ * caseless form (case-folded NFD), Unicode 9.0 as APFS has it. For the decomposition and folding+ * the kernel's own (bsd/vfs/vfs_unicode.c, utf8_normalizeOptCaseFoldAndHash, which is APFS's);+ * a name of plain ASCII needs neither and is hashed directly. Checked against the hash stored+ * with every name of the iOS 16.5 root volume, ASCII and not (tools/apfs/test_namehash.py,+ * apfs-tool namehash).*/static const uint32_t crc32c_nibble[16] = {0x00000000, 0x105ec76f, 0x20bd8ede, 0x30e349b1,@@ -1559,36 +1558,119 @@ static const uint32_t crc32c_nibble[16] = {0xc38d26c4, 0xd3d3e1ab, 0xe330a81a, 0xf36e6f75,};+static inline uint32_t+crc32c_code_point(uint32_t crc, uint32_t cp)+{+ uint8_t b[4] = { (uint8_t)cp, (uint8_t)(cp >> 8), (uint8_t)(cp >> 16), (uint8_t)(cp >> 24) };+ int j;++ for (j = 0; j < 4; j++) {+ crc ^= b[j];+ crc = (crc >> 4) ^ crc32c_nibble[crc & 15];+ crc = (crc >> 4) ^ crc32c_nibble[crc & 15];+ }+ return crc;+}+static bool-ascii_name_hash(const char *name, size_t name_len, uint32_t *hash)+name_is_ascii(const char *name, size_t name_len){- uint32_t crc = 0xffffffffu;size_t i;for (i = 0; i < name_len; i++) {- uint8_t c = (uint8_t)name[i];- uint8_t b[4] = { c, 0, 0, 0 }; /* the code point, little-endian */- int j;-- if (c == 0 || c >= 0x80) {+ if ((uint8_t)name[i] == 0 || (uint8_t)name[i] >= 0x80) {return false;}- for (j = 0; j < 4; j++) {- crc ^= b[j];- crc = (crc >> 4) ^ crc32c_nibble[crc & 15];- crc = (crc >> 4) ^ crc32c_nibble[crc & 15];+ }+ return true;+}++#if APFS_HAVE_UNICODE+static void+name_hash_feed(void *buf, size_t buf_len, void *ctx)+{+ const int32_t *u = buf;+ uint32_t *crc = ctx;+ size_t i;++ for (i = 0; i < buf_len / sizeof(int32_t); i++) {+ *crc = crc32c_code_point(*crc, (uint32_t)u[i]);+ }+}+#endif++bool+apfs_name_hash(const char *name, size_t name_len, bool fold, uint32_t *hash)+{+ uint32_t crc = 0xffffffffu;+ size_t i;++ if (name_len == 0) {+ return false;+ }+ if (name_is_ascii(name, name_len)) {+ for (i = 0; i < name_len; i++) {+ uint32_t c = (uint8_t)name[i];++ if (fold && c >= 'A' && c <= 'Z') {+ c += 'a' - 'A'; /* the caseless form of an ASCII letter */+ }+ crc = crc32c_code_point(crc, c);}+ *hash = crc & 0x3fffff;+ return true;+ }+#if APFS_HAVE_UNICODE+ if (utf8_normalizeOptCaseFoldAndHash(name, name_len, !fold, name_hash_feed, &crc) != 0) {+ return false; /* not well-formed UTF-8 (or not stream safe) */}*hash = crc & 0x3fffff;return true;+#else+ return false;+#endif+}++/*+ * Two names the same, as the volume sees names: byte for byte when both are plain ASCII (folding+ * the letters on a case-insensitive volume), else the same after decomposition (and folding).+ */+static bool+names_equal(const char *a, size_t a_len, const char *b, size_t b_len, bool fold)+{+ if (name_is_ascii(a, a_len) && name_is_ascii(b, b_len)) {+ size_t i;++ if (a_len != b_len) {+ return false;+ }+ if (!fold) {+ return memcmp(a, b, a_len) == 0;+ }+ for (i = 0; i < a_len; i++) {+ if (ascii_lower((unsigned char)a[i]) != ascii_lower((unsigned char)b[i])) {+ return false;+ }+ }+ return true;+ }+#if APFS_HAVE_UNICODE+ {+ bool equal = false;++ if (utf8_normalizeOptCaseFoldAndCompare(a, a_len, b, b_len, !fold, &equal) == 0) {+ return equal;+ }+ }+#endif+ return a_len == b_len && memcmp(a, b, a_len) == 0;}/** A name in a directory by its hash: straight to the records with that hash and compare- * only those, instead of reading the directory from its start. Gives ENOENT for a name- * that is not there, EAGAIN when this way does not apply (not hashed keys, a case-- * insensitive volume, a name that is not plain ASCII) and the caller goes through the- * directory instead. Either way the match is the same: the name byte for byte.+ * only those, instead of reading the directory from its start. Gives ENOENT for a name that+ * is not there, EAGAIN when this way does not apply (not hashed keys, a name that cannot be+ * hashed) and the caller goes through the directory instead.*/static intlookup_hashed(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t name_len,@@ -1596,14 +1678,14 @@ lookup_hashed(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t na{struct seek_target t = { .obj_id = dir_id, .type = APFS_TYPE_DIR_REC,.with_nlh = true };+ bool fold = (vol->incompat & APFS_INCOMPAT_CASE_INSENSITIVE) != 0;struct apfs_cursor cur;struct apfs_rec rec;uint32_t hash;int error;- if (!vol->hashed_names || (vol->incompat & APFS_INCOMPAT_CASE_INSENSITIVE) != 0 ||- name_len == 0 || name_len >= APFS_J_DREC_LEN_MASK ||- !ascii_name_hash(name, name_len, &hash)) {+ if (!vol->hashed_names || name_len == 0 || name_len >= APFS_J_DREC_LEN_MASK ||+ !apfs_name_hash(name, name_len, fold, &hash)) {return EAGAIN;}t.nlh = hash << APFS_J_DREC_HASH_SHIFT;@@ -1626,8 +1708,8 @@ lookup_hashed(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t nareturn ENOENT; /* past the records with this hash */}len = nlh & APFS_J_DREC_LEN_MASK;- if (len == name_len + 1 && 12 + len <= rec.key_len &&- memcmp(rec.key + 12, name, name_len) == 0) {+ if (len >= 1 && 12 + len <= rec.key_len &&+ names_equal((const char *)rec.key + 12, len - 1, name, name_len, fold)) {if (rec.val == NULL || rec.val_len < 18) {return EIO;}@@ -1642,6 +1724,46 @@ lookup_hashed(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t na}}+#ifndef KERNEL+int+apfs_dirrec_walk(struct apfs_vol *vol, int (*cb)(void *ctx, uint64_t dir_id, const char *name,+ size_t name_len, uint32_t stored_hash, uint64_t file_id), void *ctx)+{+ struct apfs_cursor cur;+ struct apfs_rec rec;+ int error;++ if (!vol->hashed_names) {+ return ENOTSUP;+ }+ error = apfs_seek(&vol->tree, 0, 0, &cur);+ if (error) {+ return error;+ }+ while ((error = apfs_cursor_next(&cur, &rec)) == 0) {+ uint32_t nlh, len;++ if (rec.type != APFS_TYPE_DIR_REC || rec.key_len < 12) {+ continue;+ }+ nlh = le32(rec.key + 8);+ len = nlh & APFS_J_DREC_LEN_MASK;+ if (len < 1 || 12 + len > rec.key_len) {+ return EIO;+ }+ if (rec.val == NULL || rec.val_len < 18) {+ return EIO;+ }+ error = cb(ctx, rec.obj_id, (const char *)rec.key + 12, len - 1, nlh >> APFS_J_DREC_HASH_SHIFT,+ le64(rec.val));+ if (error) {+ return error;+ }+ }+ return error == ENOENT ? 0 : error;+}+#endif+intapfs_lookup(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t name_len,uint64_t *file_id, uint8_t *d_type)@@ -1651,8 +1773,10 @@ apfs_lookup(struct apfs_vol *vol, uint64_t dir_id, const char *name, size_t nameerror = lookup_hashed(vol, dir_id, name, name_len, file_id, d_type);if (error != EAGAIN) {+ vol->nx->lookups_hashed++;return error;}+ vol->nx->lookups_scanned++;memset(&lc, 0, sizeof(lc));lc.name = name;lc.name_len = name_len;diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.hindex 6b149fd..0240ea6 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h@@ -206,6 +206,7 @@ struct apfs_nx {* gets new memory; the caller frees what no operation still running can be looking at* (apfs_nx_reclaim, with the oldest epoch any of its operations began at).*/+ uint64_t lookups_hashed, lookups_scanned; /* names found by their hash, by reading the directory */uint64_t epoch;struct apfs_retired *retired; /* newest first */uint64_t retired_now, retired_most, unlocked_fills, fill_races;@@ -420,11 +421,16 @@ struct apfs_cfile {int apfs_nx_open(struct apfs_nx *nx, const struct apfs_dev *dev);void apfs_nx_close(struct apfs_nx *nx);+/* The hash APFS keeps with a name in its directory record (22 bits); false: the name cannot be hashed */+bool apfs_name_hash(const char *name, size_t name_len, bool fold, uint32_t *hash);/* Free the retired blocks' memory no operation that began at `oldest` or later can see (lock held) */void apfs_nx_reclaim(struct apfs_nx *nx, uint64_t oldest);#ifndef KERNEL/* For tests on the build machine: every cached block out (retired, with a caller's lock), so all is read again */void apfs_nx_cache_flush(struct apfs_nx *nx);+/* Every directory record of the volume, with the hash stored in its key (hashed keys only) */+int apfs_dirrec_walk(struct apfs_vol *vol, int (*cb)(void *ctx, uint64_t dir_id, const char *name,+ size_t name_len, uint32_t stored_hash, uint64_t file_id), void *ctx);#endifint apfs_vol_open(struct apfs_nx *nx, uint32_t index, struct apfs_vol *vol);diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex 32cf54d..8ce465c 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -158,7 +158,7 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS{#pragma unused(oidp, arg1, arg2)struct apfs_mount *am = apfs_the_mount;- char text[640];+ char text[1024];int error;if (am == NULL) {@@ -172,7 +172,9 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS"device %llu calls %llu ms %llu bytes\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",+ "blocks %llu read %llu cache hits\n"+ "lookups %llu by the name's hash %llu by reading the directory\n"+ "lock %llu takes %llu ms waiting %llu ms held; metadata read without it %llu times\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,@@ -191,7 +193,13 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS(unsigned long long)nx->inflate_out,(unsigned long long)nx->inflate_fallbacks,(unsigned long long)nx->reads,- (unsigned long long)nx->hits);+ (unsigned long long)nx->hits,+ (unsigned long long)nx->lookups_hashed,+ (unsigned long long)nx->lookups_scanned,+ (unsigned long long)am->am_lock_waits,+ (unsigned long long)apfs_ticks_ms(am->am_lock_wait_ticks),+ (unsigned long long)apfs_ticks_ms(am->am_lock_held_ticks),+ (unsigned long long)nx->unlocked_fills);}error = SYSCTL_OUT(req, text, strlen(text) + 1);if (error || req->newptr == USER_ADDR_NULL) {@@ -280,6 +288,8 @@ apfs_stats_print(void)printf("apfs: lock holds: %llu under 10 us, %llu to 100 us, %llu to 1 ms, %llu to 10 ms, %llu longer; ""longest %llu us\n", am->am_lock_hold_hist[0], am->am_lock_hold_hist[1], am->am_lock_hold_hist[2],am->am_lock_hold_hist[3], am->am_lock_hold_hist[4], max_ns / 1000);+ printf("apfs: lookups %llu by the name's hash, %llu by reading the directory\n", nx->lookups_hashed,+ nx->lookups_scanned);printf("apfs: metadata read without the lock: %llu times, %llu of them already in by another's read; ""retired blocks %llu now, %llu at most; operations going on now %u\n", nx->unlocked_fills, nx->fill_races,nx->retired_now, nx->retired_most, am->am_nops);