Debian Codegraph @

apfs_raw.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2019 Ernesto A. Fernández <[email protected]>
4 *
5 * Definitions for all on-disk data structures.
6 */
7
8 #ifndef _APFS_RAW_H
9 #define _APFS_RAW_H
10
11 #include <linux/types.h>
12 #include <linux/uuid.h>
13
14 /* Object identifiers constants */
15 #define APFS_OID_NX_SUPERBLOCK 1
16 #define APFS_OID_INVALID 0ULL
17 #define APFS_OID_RESERVED_COUNT 1024
18
19 /* Object type masks */
20 #define APFS_OBJECT_TYPE_MASK 0x0000ffff
21 #define APFS_OBJECT_TYPE_FLAGS_MASK 0xffff0000
22 #define APFS_OBJ_STORAGETYPE_MASK 0xc0000000
23 #define APFS_OBJECT_TYPE_FLAGS_DEFINED_MASK 0xf8000000
24
25 /* Object types */
26 #define APFS_OBJECT_TYPE_NX_SUPERBLOCK 0x00000001
27 #define APFS_OBJECT_TYPE_BTREE 0x00000002
28 #define APFS_OBJECT_TYPE_BTREE_NODE 0x00000003
29 #define APFS_OBJECT_TYPE_SPACEMAN 0x00000005
30 #define APFS_OBJECT_TYPE_SPACEMAN_CAB 0x00000006
31 #define APFS_OBJECT_TYPE_SPACEMAN_CIB 0x00000007
32 #define APFS_OBJECT_TYPE_SPACEMAN_BITMAP 0x00000008
33 #define APFS_OBJECT_TYPE_SPACEMAN_FREE_QUEUE 0x00000009
34 #define APFS_OBJECT_TYPE_EXTENT_LIST_TREE 0x0000000a
35 #define APFS_OBJECT_TYPE_OMAP 0x0000000b
36 #define APFS_OBJECT_TYPE_CHECKPOINT_MAP 0x0000000c
37 #define APFS_OBJECT_TYPE_FS 0x0000000d
38 #define APFS_OBJECT_TYPE_FSTREE 0x0000000e
39 #define APFS_OBJECT_TYPE_BLOCKREFTREE 0x0000000f
40 #define APFS_OBJECT_TYPE_SNAPMETATREE 0x00000010
41 #define APFS_OBJECT_TYPE_NX_REAPER 0x00000011
42 #define APFS_OBJECT_TYPE_NX_REAP_LIST 0x00000012
43 #define APFS_OBJECT_TYPE_OMAP_SNAPSHOT 0x00000013
44 #define APFS_OBJECT_TYPE_EFI_JUMPSTART 0x00000014
45 #define APFS_OBJECT_TYPE_FUSION_MIDDLE_TREE 0x00000015
46 #define APFS_OBJECT_TYPE_NX_FUSION_WBC 0x00000016
47 #define APFS_OBJECT_TYPE_NX_FUSION_WBC_LIST 0x00000017
48 #define APFS_OBJECT_TYPE_ER_STATE 0x00000018
49 #define APFS_OBJECT_TYPE_GBITMAP 0x00000019
50 #define APFS_OBJECT_TYPE_GBITMAP_TREE 0x0000001a
51 #define APFS_OBJECT_TYPE_GBITMAP_BLOCK 0x0000001b
52 #define APFS_OBJECT_TYPE_ER_RECOVERY_BLOCK 0x0000001c
53 #define APFS_OBJECT_TYPE_SNAP_META_EXT 0x0000001d
54 #define APFS_OBJECT_TYPE_INTEGRITY_META 0x0000001e
55 #define APFS_OBJECT_TYPE_FEXT_TREE 0x0000001f
56 #define APFS_OBJECT_TYPE_RESERVED_20 0x00000020
57 #define APFS_OBJECT_TYPE_INVALID 0x00000000
58 #define APFS_OBJECT_TYPE_TEST 0x000000ff
59
60 /* Object type flags */
61 #define APFS_OBJ_VIRTUAL 0x00000000
62 #define APFS_OBJ_EPHEMERAL 0x80000000
63 #define APFS_OBJ_PHYSICAL 0x40000000
64 #define APFS_OBJ_NOHEADER 0x20000000
65 #define APFS_OBJ_ENCRYPTED 0x10000000
66 #define APFS_OBJ_NONPERSISTENT 0x08000000
67
68 #define APFS_MAX_CKSUM_SIZE 8
69
70 /*
71 * On-disk representation of an APFS object
72 */
73 struct apfs_obj_phys {
74 /*00*/ __le64 o_cksum; /* Fletcher checksum */
75 __le64 o_oid; /* Object-id */
76 /*10*/ __le64 o_xid; /* Transaction ID */
77 __le32 o_type; /* Object type */
78 __le32 o_subtype; /* Object subtype */
79 } __packed;
80
81 /* Flags for the object map structure */
82 #define APFS_OMAP_MANUALLY_MANAGED 0x00000001
83 #define APFS_OMAP_ENCRYPTING 0x00000002
84 #define APFS_OMAP_DECRYPTING 0x00000004
85 #define APFS_OMAP_KEYROLLING 0x00000008
86 #define APFS_OMAP_CRYPTO_GENERATION 0x00000010
87 #define APFS_OMAP_FLAGS_VALID_MASK (APFS_OMAP_MANUALLY_MANAGED \
88 | APFS_OMAP_ENCRYPTING \
89 | APFS_OMAP_DECRYPTING \
90 | APFS_OMAP_KEYROLLING \
91 | APFS_OMAP_CRYPTO_GENERATION)
92
93 /*
94 * On-disk representation of an object map
95 */
96 struct apfs_omap_phys {
97 /*00*/ struct apfs_obj_phys om_o;
98 /*20*/ __le32 om_flags;
99 __le32 om_snap_count;
100 __le32 om_tree_type;
101 __le32 om_snapshot_tree_type;
102 /*30*/ __le64 om_tree_oid;
103 __le64 om_snapshot_tree_oid;
104 /*40*/ __le64 om_most_recent_snap;
105 __le64 om_pending_revert_min;
106 __le64 om_pending_revert_max;
107 } __packed;
108
109 /* Object map value flags */
110 #define APFS_OMAP_VAL_DELETED 0x00000001
111 #define APFS_OMAP_VAL_SAVED 0x00000002
112 #define APFS_OMAP_VAL_ENCRYPTED 0x00000004
113 #define APFS_OMAP_VAL_NOHEADER 0x00000008
114 #define APFS_OMAP_VAL_CRYPTO_GENERATION 0x00000010
115 #define APFS_OMAP_VAL_FLAGS_VALID_MASK (APFS_OMAP_VAL_DELETED \
116 | APFS_OMAP_VAL_SAVED \
117 | APFS_OMAP_VAL_ENCRYPTED \
118 | APFS_OMAP_VAL_NOHEADER \
119 | APFS_OMAP_VAL_CRYPTO_GENERATION)
120
121 /*
122 * Structure of a value in an object map B-tree
123 */
124 struct apfs_omap_val {
125 __le32 ov_flags;
126 __le32 ov_size;
127 __le64 ov_paddr;
128 } __packed;
129
130 /*
131 * Structure of a value in an omap's snapshot tree
132 */
133 struct apfs_omap_snapshot {
134 __le32 oms_flags;
135 __le32 oms_pad;
136 __le64 oms_oid;
137 } __packed;
138
139 /* B-tree node flags */
140 #define APFS_BTNODE_ROOT 0x0001
141 #define APFS_BTNODE_LEAF 0x0002
142 #define APFS_BTNODE_FIXED_KV_SIZE 0x0004
143 #define APFS_BTNODE_CHECK_KOFF_INVAL 0x8000
144 #define APFS_BTNODE_MASK 0x0007 /* Valid on-disk flags */
145
146 /* B-tree location constants */
147 #define APFS_BTOFF_INVALID 0xffff
148
149 /*
150 * Structure storing a location inside a B-tree node
151 */
152 struct apfs_nloc {
153 __le16 off;
154 __le16 len;
155 } __packed;
156
157 /*
158 * Structure storing the location of a key/value pair within a B-tree node
159 */
160 struct apfs_kvloc {
161 struct apfs_nloc k;
162 struct apfs_nloc v;
163 } __packed;
164
165 /*
166 * Structure storing the location of a key/value pair within a B-tree node
167 * having fixed-size key and value (flag APFS_BTNODE_FIXED_KV_SIZE is present)
168 */
169 struct apfs_kvoff {
170 __le16 k;
171 __le16 v;
172 } __packed;
173
174 /*
175 * On-disk representation of a B-tree node
176 */
177 struct apfs_btree_node_phys {
178 /*00*/ struct apfs_obj_phys btn_o;
179 /*20*/ __le16 btn_flags;
180 __le16 btn_level;
181 __le32 btn_nkeys;
182 /*28*/ struct apfs_nloc btn_table_space;
183 struct apfs_nloc btn_free_space;
184 struct apfs_nloc btn_key_free_list;
185 struct apfs_nloc btn_val_free_list;
186 /*38*/ __le64 btn_data[];
187 } __packed;
188
189 /* B-tree info flags */
190 #define APFS_BTREE_UINT64_KEYS 0x00000001
191 #define APFS_BTREE_SEQUENTIAL_INSERT 0x00000002
192 #define APFS_BTREE_ALLOW_GHOSTS 0x00000004
193 #define APFS_BTREE_EPHEMERAL 0x00000008
194 #define APFS_BTREE_PHYSICAL 0x00000010
195 #define APFS_BTREE_NONPERSISTENT 0x00000020
196 #define APFS_BTREE_KV_NONALIGNED 0x00000040
197 #define APFS_BTREE_FLAGS_VALID_MASK (APFS_BTREE_UINT64_KEYS \
198 | APFS_BTREE_SEQUENTIAL_INSERT \
199 | APFS_BTREE_ALLOW_GHOSTS \
200 | APFS_BTREE_EPHEMERAL \
201 | APFS_BTREE_PHYSICAL \
202 | APFS_BTREE_NONPERSISTENT \
203 | APFS_BTREE_KV_NONALIGNED)
204
205 /*
206 * Structure used to store information about a B-tree that won't change
207 * over time
208 */
209 struct apfs_btree_info_fixed {
210 __le32 bt_flags;
211 __le32 bt_node_size;
212 __le32 bt_key_size;
213 __le32 bt_val_size;
214 } __packed;
215
216 /*
217 * Structure used to store information about a B-tree (located at the end of
218 * a B-tree root node block)
219 */
220 struct apfs_btree_info {
221 struct apfs_btree_info_fixed bt_fixed;
222 __le32 bt_longest_key; /* Longest key ever stored */
223 __le32 bt_longest_val; /* Longest value ever stored */
224 __le64 bt_key_count;
225 __le64 bt_node_count;
226 } __packed;
227
228 /*
229 * Structure of the value of a directory entry. This is the data in
230 * the catalog nodes for record type APFS_TYPE_DIR_REC.
231 */
232 struct apfs_drec_val {
233 __le64 file_id;
234 __le64 date_added;
235 __le16 flags;
236 u8 xfields[];
237 } __packed;
238
239 /* Physical extent records */
240 #define APFS_PEXT_LEN_MASK 0x0fffffffffffffffULL
241 #define APFS_PEXT_KIND_MASK 0xf000000000000000ULL
242 #define APFS_PEXT_KIND_SHIFT 60
243
244 /* The kind of a physical extent record */
245 enum {
246 APFS_KIND_ANY = 0,
247 APFS_KIND_NEW = 1,
248 APFS_KIND_UPDATE = 2,
249 APFS_KIND_DEAD = 3,
250 APFS_KIND_UPDATE_REFCNT = 4,
251
252 APFS_KIND_INVALID = 255 /* This is weird, won't fit in 4 bits */
253 };
254
255 #define APFS_OWNING_OBJ_ID_INVALID (~0ULL)
256 #define APFS_OWNING_OBJ_ID_UNKNOWN (~1ULL)
257
258 /*
259 * Structure of a physical extent record
260 */
261 struct apfs_phys_ext_val {
262 __le64 len_and_kind;
263 __le64 owning_obj_id;
264 __le32 refcnt;
265 } __packed;
266
267 /* File extent records */
268 #define APFS_FILE_EXTENT_LEN_MASK 0x00ffffffffffffffULL
269 #define APFS_FILE_EXTENT_FLAG_MASK 0xff00000000000000ULL
270 #define APFS_FILE_EXTENT_FLAG_SHIFT 56
271 #define APFS_FILE_EXTENT_CRYPTO_FLAG 0x01 /* Made-up name */
272 #define APFS_FILE_EXTENT_PREALLOCATED 0x02 /* Made-up name */
273 #define APFS_VALID_FILE_EXTENT_FLAGS (APFS_FILE_EXTENT_CRYPTO_FLAG \
274 | APFS_FILE_EXTENT_PREALLOCATED)
275
276 /*
277 * Put a bound on maximum file size so that a growing truncation will always
278 * produce a single hole extent, even if 64k block sizes were in use. Larger
279 * file sizes could be supported with multiple extents of course, but it takes
280 * some work and I don't see the point.
281 */
282 #define APFS_MAX_FILE_SIZE 0x00ffffffffff0000ULL
283
284 /*
285 * Structure of a file extent record
286 */
287 struct apfs_file_extent_val {
288 __le64 len_and_flags;
289 __le64 phys_block_num;
290 __le64 crypto_id;
291 } __packed;
292
293 /*
294 * Structure of a data stream record
295 */
296 struct apfs_dstream_id_val {
297 __le32 refcnt;
298 } __packed;
299
300 #define APFS_CP_MAX_WRAPPEDKEYSIZE 128
301
302 /*
303 * Structure used to store the encryption state for PFKs
304 */
305 struct apfs_wrapped_crypto_state {
306 __le16 major_version;
307 __le16 minor_version;
308 __le32 cpflags;
309 __le32 persistent_class;
310 __le32 key_os_version;
311 __le16 key_revision;
312 __le16 key_len;
313 u8 persistent_key[];
314 } __packed;
315
316 /*
317 * Structure of a crypto state record
318 */
319 struct apfs_crypto_state_val {
320 __le32 refcnt;
321 struct apfs_wrapped_crypto_state state;
322 } __packed;
323
324 /* Inode numbers for special inodes */
325 #define APFS_INVALID_INO_NUM 0
326
327 #define APFS_ROOT_DIR_PARENT 1 /* Root directory parent */
328 #define APFS_ROOT_DIR_INO_NUM 2 /* Root directory */
329 #define APFS_PRIV_DIR_INO_NUM 3 /* Private directory */
330 #define APFS_SNAP_DIR_INO_NUM 6 /* Snapshots metadata */
331 #define APFS_PURGEABLE_DIR_INO_NUM 7 /* Parent of purgeable files */
332
333 /* Smallest inode number available for user content */
334 #define APFS_MIN_USER_INO_NUM 16
335
336 #define APFS_UNIFIED_ID_SPACE_MARK 0x0800000000000000
337
338 /* Inode internal flags */
339 #define APFS_INODE_IS_APFS_PRIVATE 0x00000001
340 #define APFS_INODE_MAINTAIN_DIR_STATS 0x00000002
341 #define APFS_INODE_DIR_STATS_ORIGIN 0x00000004
342 #define APFS_INODE_PROT_CLASS_EXPLICIT 0x00000008
343 #define APFS_INODE_WAS_CLONED 0x00000010
344 #define APFS_INODE_FLAG_UNUSED 0x00000020
345 #define APFS_INODE_HAS_SECURITY_EA 0x00000040
346 #define APFS_INODE_BEING_TRUNCATED 0x00000080
347 #define APFS_INODE_HAS_FINDER_INFO 0x00000100
348 #define APFS_INODE_IS_SPARSE 0x00000200
349 #define APFS_INODE_WAS_EVER_CLONED 0x00000400
350 #define APFS_INODE_ACTIVE_FILE_TRIMMED 0x00000800
351 #define APFS_INODE_PINNED_TO_MAIN 0x00001000
352 #define APFS_INODE_PINNED_TO_TIER2 0x00002000
353 #define APFS_INODE_HAS_RSRC_FORK 0x00004000
354 #define APFS_INODE_NO_RSRC_FORK 0x00008000
355 #define APFS_INODE_ALLOCATION_SPILLEDOVER 0x00010000
356 #define APFS_INODE_FAST_PROMOTE 0x00020000
357 #define APFS_INODE_HAS_UNCOMPRESSED_SIZE 0x00040000
358 #define APFS_INODE_IS_PURGEABLE 0x00080000
359 #define APFS_INODE_WANTS_TO_BE_PURGEABLE 0x00100000
360 #define APFS_INODE_IS_SYNC_ROOT 0x00200000
361 #define APFS_INODE_SNAPSHOT_COW_EXEMPTION 0x00400000
362 /* This flag is not documented */
363 #define APFS_INODE_HAS_PURGEABLE_FLAGS 0x02000000
364
365 /* Masks for internal flags */
366 #define APFS_VALID_INTERNAL_INODE_FLAGS (APFS_INODE_IS_APFS_PRIVATE \
367 | APFS_INODE_MAINTAIN_DIR_STATS \
368 | APFS_INODE_DIR_STATS_ORIGIN \
369 | APFS_INODE_PROT_CLASS_EXPLICIT \
370 | APFS_INODE_WAS_CLONED \
371 | APFS_INODE_HAS_SECURITY_EA \
372 | APFS_INODE_BEING_TRUNCATED \
373 | APFS_INODE_HAS_FINDER_INFO \
374 | APFS_INODE_IS_SPARSE \
375 | APFS_INODE_WAS_EVER_CLONED \
376 | APFS_INODE_ACTIVE_FILE_TRIMMED \
377 | APFS_INODE_PINNED_TO_MAIN \
378 | APFS_INODE_PINNED_TO_TIER2 \
379 | APFS_INODE_HAS_RSRC_FORK \
380 | APFS_INODE_NO_RSRC_FORK \
381 | APFS_INODE_ALLOCATION_SPILLEDOVER \
382 | APFS_INODE_FAST_PROMOTE \
383 | APFS_INODE_HAS_UNCOMPRESSED_SIZE \
384 | APFS_INODE_IS_PURGEABLE \
385 | APFS_INODE_WANTS_TO_BE_PURGEABLE \
386 | APFS_INODE_IS_SYNC_ROOT \
387 | APFS_INODE_SNAPSHOT_COW_EXEMPTION \
388 | APFS_INODE_HAS_PURGEABLE_FLAGS)
389 #define APFS_INODE_INHERITED_INTERNAL_FLAGS (APFS_INODE_MAINTAIN_DIR_STATS)
390 #define APFS_INDOE_CLONED_INTERNAL_FLAGS (APFS_INODE_HAS_RSRC_FORK \
391 | APFS_INODE_NO_RSRC_FORK \
392 | APFS_INODE_HAS_FINDER_INFO)
393 #define APFS_INODE_PINNED_MASK (APFS_INODE_PINNED_TO_MAIN \
394 | APFS_INODE_PINNED_TO_TIER2)
395
396 /* BSD flags */
397 #define APFS_INOBSD_NODUMP 0x00000001
398 #define APFS_INOBSD_IMMUTABLE 0x00000002
399 #define APFS_INOBSD_APPEND 0x00000004
400 #define APFS_INOBSD_COMPRESSED 0x00000020
401
402 /*
403 * Structure of an inode as stored as a B-tree value
404 */
405 struct apfs_inode_val {
406 /*00*/ __le64 parent_id;
407 __le64 private_id;
408 /*10*/ __le64 create_time;
409 __le64 mod_time;
410 __le64 change_time;
411 __le64 access_time;
412 /*30*/ __le64 internal_flags;
413 union {
414 __le32 nchildren;
415 __le32 nlink;
416 };
417 __le32 default_protection_class;
418 /*40*/ __le32 write_generation_counter;
419 __le32 bsd_flags;
420 __le32 owner;
421 __le32 group;
422 /*50*/ __le16 mode;
423 __le16 pad1;
424 __le64 uncompressed_size;
425 /*5C*/ u8 xfields[];
426 } __packed;
427
428 /* Extended field types for dentries */
429 #define APFS_DREC_EXT_TYPE_SIBLING_ID 1
430
431 /* Extended field types for inodes */
432 #define APFS_INO_EXT_TYPE_SNAP_XID 1
433 #define APFS_INO_EXT_TYPE_DELTA_TREE_OID 2
434 #define APFS_INO_EXT_TYPE_DOCUMENT_ID 3
435 #define APFS_INO_EXT_TYPE_NAME 4
436 #define APFS_INO_EXT_TYPE_PREV_FSIZE 5
437 #define APFS_INO_EXT_TYPE_RESERVED_6 6
438 #define APFS_INO_EXT_TYPE_FINDER_INFO 7
439 #define APFS_INO_EXT_TYPE_DSTREAM 8
440 #define APFS_INO_EXT_TYPE_RESERVED_9 9
441 #define APFS_INO_EXT_TYPE_DIR_STATS_KEY 10
442 #define APFS_INO_EXT_TYPE_FS_UUID 11
443 #define APFS_INO_EXT_TYPE_RESERVED_12 12
444 #define APFS_INO_EXT_TYPE_SPARSE_BYTES 13
445 #define APFS_INO_EXT_TYPE_RDEV 14
446 #define APFS_INO_EXT_TYPE_PURGEABLE_FLAGS 15
447 #define APFS_INO_EXT_TYPE_ORIG_SYNC_ROOT_ID 16
448
449 /* Extended field flags */
450 #define APFS_XF_DATA_DEPENDENT 0x01
451 #define APFS_XF_DO_NOT_COPY 0x02
452 #define APFS_XF_RESERVED_4 0x04
453 #define APFS_XF_CHILDREN_INHERIT 0x08
454 #define APFS_XF_USER_FIELD 0x10
455 #define APFS_XF_SYSTEM_FIELD 0x20
456 #define APFS_XF_RESERVED_40 0x40
457 #define APFS_XF_RESERVED_80 0x80
458
459 /* Constants for extended fields */
460 #define APFS_MIN_DOC_ID 3 /* Smallest not reserved document id */
461
462 /*
463 * Structure used to store the number and size of an xfield collection. The
464 * official reference seems to be wrong about @xf_used_data: it's the size of
465 * the xfield values alone, without the metadata.
466 */
467 struct apfs_xf_blob {
468 __le16 xf_num_exts;
469 __le16 xf_used_data;
470 u8 xf_data[];
471 } __packed;
472
473 /*
474 * Structure used to describe an extended field
475 */
476 struct apfs_x_field {
477 u8 x_type;
478 u8 x_flags;
479 __le16 x_size;
480 } __packed;
481
482 /*
483 * Structure used to store information about a data stream
484 */
485 struct apfs_dstream {
486 __le64 size;
487 __le64 alloced_size;
488 __le64 default_crypto_id;
489 __le64 total_bytes_written;
490 __le64 total_bytes_read;
491 } __packed;
492
493 /*
494 * Structure used to store directory information
495 */
496 struct apfs_dir_stats_val {
497 __le64 num_children;
498 __le64 total_size;
499 __le64 chained_key;
500 __le64 gen_count;
501 } __packed;
502
503 /*
504 * Structure of the value for a sibling link record. These are used to
505 * list the hard links for a given inode.
506 */
507 struct apfs_sibling_val {
508 __le64 parent_id;
509 __le16 name_len;
510 u8 name[];
511 } __packed;
512
513 /*
514 * Structure of the value for a sibling map record. No idea what these are for.
515 */
516 struct apfs_sibling_map_val {
517 __le64 file_id;
518 } __packed;
519
520 /*
521 * Structure of a key in an object map B-tree
522 */
523 struct apfs_omap_key {
524 __le64 ok_oid;
525 __le64 ok_xid;
526 } __packed;
527
528 /*
529 * Structure of a key in a free-space queue b-tree
530 */
531 struct apfs_spaceman_free_queue_key {
532 __le64 sfqk_xid;
533 __le64 sfqk_paddr;
534 } __packed;
535
536 /* Catalog records types */
537 enum {
538 APFS_TYPE_ANY = 0,
539 APFS_TYPE_SNAP_METADATA = 1,
540 APFS_TYPE_EXTENT = 2,
541 APFS_TYPE_INODE = 3,
542 APFS_TYPE_XATTR = 4,
543 APFS_TYPE_SIBLING_LINK = 5,
544 APFS_TYPE_DSTREAM_ID = 6,
545 APFS_TYPE_CRYPTO_STATE = 7,
546 APFS_TYPE_FILE_EXTENT = 8,
547 APFS_TYPE_DIR_REC = 9,
548 APFS_TYPE_DIR_STATS = 10,
549 APFS_TYPE_SNAP_NAME = 11,
550 APFS_TYPE_SIBLING_MAP = 12,
551 APFS_TYPE_MAX_VALID = 12,
552 APFS_TYPE_MAX = 15,
553 APFS_TYPE_INVALID = 15,
554 };
555
556 /* Bit masks for the 'obj_id_and_type' field of a key header */
557 #define APFS_OBJ_ID_MASK 0x0fffffffffffffffULL
558 #define APFS_OBJ_TYPE_MASK 0xf000000000000000ULL
559 #define APFS_OBJ_TYPE_SHIFT 60
560
561 /* Key header for filesystem-object keys */
562 struct apfs_key_header {
563 __le64 obj_id_and_type;
564 } __packed;
565
566 /*
567 * Structure of the key for a physical extent record
568 */
569 struct apfs_phys_ext_key {
570 struct apfs_key_header hdr;
571 } __packed;
572
573 /*
574 * Structure of the key for an inode record
575 */
576 struct apfs_inode_key {
577 struct apfs_key_header hdr;
578 } __packed;
579
580 /*
581 * Structure of the key for a file extent record
582 */
583 struct apfs_file_extent_key {
584 struct apfs_key_header hdr;
585 __le64 logical_addr;
586 } __packed;
587
588 /*
589 * Structure of the key for a data stream record
590 */
591 struct apfs_dstream_id_key {
592 struct apfs_key_header hdr;
593 } __packed;
594
595 /*
596 * Structure of the key for a crypto state record
597 */
598 struct apfs_crypto_state_key {
599 struct apfs_key_header hdr;
600 } __packed;
601
602 /* Bit masks for the 'name_len_and_hash' field of a directory entry */
603 #define APFS_DREC_LEN_MASK 0x000003ff
604 #define APFS_DREC_HASH_MASK 0xfffffc00
605 #define APFS_DREC_HASH_SHIFT 10
606
607 /* We could support bigger filenames, but I don't know if Linux allows it */
608 #define APFS_NAME_LEN NAME_MAX
609
610 /* Bit masks for the 'type' field of a directory entry */
611 enum {
612 APFS_DREC_TYPE_MASK = 0x000f,
613 APFS_DREC_RESERVED_10 = 0x0010,
614
615 /* These flags are not documented */
616 APFS_DREC_PURGEABLE_2 = 0x0200,
617 APFS_DREC_PURGEABLE_8 = 0x0800,
618 };
619
620 #define APFS_DREC_PURGEABLE (APFS_DREC_PURGEABLE_2 | APFS_DREC_PURGEABLE_8)
621
622 /*
623 * Structure of the key for a directory entry - no hash, used on normalization
624 * sensitive volumes
625 */
626 struct apfs_drec_key {
627 struct apfs_key_header hdr;
628 __le16 name_len;
629 u8 name[];
630 } __packed;
631
632 /*
633 * Structure of the key for a directory entry, including a precomputed
634 * hash of its name
635 */
636 struct apfs_drec_hashed_key {
637 struct apfs_key_header hdr;
638 __le32 name_len_and_hash;
639 u8 name[];
640 } __packed;
641
642 /*
643 * Structure of the key for an extended attributes record
644 */
645 struct apfs_xattr_key {
646 struct apfs_key_header hdr;
647 __le16 name_len;
648 u8 name[];
649 } __packed;
650
651 /*
652 * Structure of the key for a snapshot metadata record
653 */
654 struct apfs_snap_metadata_key {
655 struct apfs_key_header hdr;
656 } __packed;
657
658 /* The snapshot name headers always have this placeholder object id */
659 #define APFS_SNAP_NAME_OBJ_ID (~0ULL & APFS_OBJ_ID_MASK)
660
661 /*
662 * Structure of the key for a snapshot name record
663 */
664 struct apfs_snap_name_key {
665 struct apfs_key_header hdr;
666 __le16 name_len;
667 u8 name[];
668 } __packed;
669
670 /*
671 * Structure of the key for a sibling link record
672 */
673 struct apfs_sibling_link_key {
674 struct apfs_key_header hdr;
675 __le64 sibling_id;
676 } __packed;
677
678 /*
679 * Structure of the key for a siblink map record
680 */
681 struct apfs_sibling_map_key {
682 struct apfs_key_header hdr;
683 } __packed;
684
685 /*
686 * On-disk allocation info for a chunk of blocks
687 */
688 struct apfs_chunk_info {
689 __le64 ci_xid;
690 __le64 ci_addr;
691 __le32 ci_block_count;
692 __le32 ci_free_count;
693 __le64 ci_bitmap_addr;
694 } __packed;
695
696 /* Constants for the chunk info block */
697 #define APFS_CI_COUNT_MASK 0x000FFFFF
698 #define APFS_CI_COUNT_RESERVED_MASK 0xFFF00000
699
700 /*
701 * Structure of a block with an array of chunk allocation info structures
702 */
703 struct apfs_chunk_info_block {
704 struct apfs_obj_phys cib_o;
705 __le32 cib_index;
706 __le32 cib_chunk_info_count;
707 struct apfs_chunk_info cib_chunk_info[];
708 } __packed;
709
710 /*
711 * Structure of a block with an array of addresses to chunk information blocks
712 */
713 struct apfs_cib_addr_block {
714 struct apfs_obj_phys cab_o;
715 __le32 cab_index;
716 __le32 cab_cib_count;
717 __le64 cab_cib_addr[];
718 } __packed;
719
720 /*
721 * On-disk structure for a free queue
722 */
723 struct apfs_spaceman_free_queue {
724 __le64 sfq_count;
725 __le64 sfq_tree_oid;
726 __le64 sfq_oldest_xid;
727 __le16 sfq_tree_node_limit;
728 __le16 sfq_pad16;
729 __le32 sfq_pad32;
730 __le64 sfq_reserved;
731 } __packed;
732
733 /* Indexes for a free queue array */
734 enum {
735 APFS_SFQ_IP = 0,
736 APFS_SFQ_MAIN = 1,
737 APFS_SFQ_TIER2 = 2,
738 APFS_SFQ_COUNT = 3
739 };
740
741 /*
742 * On-disk structure for device allocation information
743 */
744 struct apfs_spaceman_device {
745 __le64 sm_block_count;
746 __le64 sm_chunk_count;
747 __le32 sm_cib_count;
748 __le32 sm_cab_count;
749 __le64 sm_free_count;
750 __le32 sm_addr_offset;
751 __le32 sm_reserved;
752 __le64 sm_reserved2;
753 } __packed;
754
755 /* Indexes for a device array */
756 enum {
757 APFS_SD_MAIN = 0,
758 APFS_SD_TIER2 = 1,
759 APFS_SD_COUNT = 2
760 };
761
762 /*
763 * On-disk structure to describe allocation zone boundaries
764 */
765 struct apfs_spaceman_allocation_zone_boundaries {
766 __le64 saz_zone_start;
767 __le64 saz_zone_end;
768 } __packed;
769
770 /* Allocation zone constants */
771 #define APFS_SM_ALLOCZONE_INVALID_END_BOUNDARY 0
772 #define APFS_SM_ALLOCZONE_NUM_PREVIOUS_BOUNDARIES 7
773
774 struct apfs_spaceman_allocation_zone_info_phys {
775 struct apfs_spaceman_allocation_zone_boundaries saz_current_boundaries;
776 struct apfs_spaceman_allocation_zone_boundaries
777 saz_previous_boundaries[APFS_SM_ALLOCZONE_NUM_PREVIOUS_BOUNDARIES];
778
779 __le16 saz_zone_id;
780 __le16 saz_previous_boundary_index;
781 __le32 saz_reserved;
782 } __packed;
783
784 /* Datazone constants */
785 #define APFS_SM_DATAZONE_ALLOCZONE_COUNT 8
786
787 struct apfs_spaceman_datazone_info_phys {
788 struct apfs_spaceman_allocation_zone_info_phys
789 sdz_allocation_zones[APFS_SD_COUNT][APFS_SM_DATAZONE_ALLOCZONE_COUNT];
790 } __packed;
791
792 /* Internal-pool bitmap constants */
793 #define APFS_SPACEMAN_IP_BM_TX_MULTIPLIER 16
794 #define APFS_SPACEMAN_IP_BM_INDEX_INVALID 0xFFFF
795 #define APFS_SPACEMAN_IP_BM_BLOCK_COUNT_MAX 0xFFFE
796
797 /* Space manager flags */
798 #define APFS_SM_FLAG_VERSIONED 0x00000001
799 #define APFS_SM_FLAGS_VALID_MASK APFS_SM_FLAG_VERSIONED
800
801 /*
802 * On-disk structure for the space manager
803 */
804 struct apfs_spaceman_phys {
805 struct apfs_obj_phys sm_o;
806 __le32 sm_block_size;
807 __le32 sm_blocks_per_chunk;
808 __le32 sm_chunks_per_cib;
809 __le32 sm_cibs_per_cab;
810 struct apfs_spaceman_device sm_dev[APFS_SD_COUNT];
811 __le32 sm_flags;
812 __le32 sm_ip_bm_tx_multiplier;
813 __le64 sm_ip_block_count;
814 __le32 sm_ip_bm_size_in_blocks;
815 __le32 sm_ip_bm_block_count;
816 __le64 sm_ip_bm_base;
817 __le64 sm_ip_base;
818 __le64 sm_fs_reserve_block_count;
819 __le64 sm_fs_reserve_alloc_count;
820 struct apfs_spaceman_free_queue sm_fq[APFS_SFQ_COUNT];
821 __le16 sm_ip_bm_free_head;
822 __le16 sm_ip_bm_free_tail;
823 __le32 sm_ip_bm_xid_offset;
824 __le32 sm_ip_bitmap_offset;
825 __le32 sm_ip_bm_free_next_offset;
826 __le32 sm_version;
827 __le32 sm_struct_size;
828 struct apfs_spaceman_datazone_info_phys sm_datazone;
829 } __packed;
830
831 /*
832 * Structure used to store a range of physical blocks
833 */
834 struct apfs_prange {
835 __le64 pr_start_paddr;
836 __le64 pr_block_count;
837 } __packed;
838
839 /* Reaper flags */
840 #define APFS_NR_BHM_FLAG 0x00000001
841 #define APFS_NR_CONTINUE 0x00000002
842 #define APFS_NR_FLAGS_VALID_MASK (APFS_NR_BHM_FLAG | APFS_NR_CONTINUE)
843
844 /*
845 * On-disk reaper structure
846 */
847 struct apfs_nx_reaper_phys {
848 struct apfs_obj_phys nr_o;
849 __le64 nr_next_reap_id;
850 __le64 nr_completed_id;
851 __le64 nr_head;
852 __le64 nr_tail;
853 __le32 nr_flags;
854 __le32 nr_rlcount;
855 __le32 nr_type;
856 __le32 nr_size;
857 __le64 nr_fs_oid;
858 __le64 nr_oid;
859 __le64 nr_xid;
860 __le32 nr_nrle_flags;
861 __le32 nr_state_buffer_size;
862 u8 nr_state_buffer[];
863 } __packed;
864
865 struct apfs_nx_reap_list_entry {
866 __le32 nrle_next;
867 __le32 nrle_flags;
868 __le32 nrle_type;
869 __le32 nrle_size;
870 __le64 nrle_fs_oid;
871 __le64 nrle_oid;
872 __le64 nrle_xid;
873 } __packed;
874
875 struct apfs_nx_reap_list_phys {
876 struct apfs_obj_phys nrl_o;
877 __le64 nrl_next;
878 __le32 nrl_flags;
879 __le32 nrl_max;
880 __le32 nrl_count;
881 __le32 nrl_first;
882 __le32 nrl_last;
883 __le32 nrl_free;
884 struct apfs_nx_reap_list_entry nrl_entries[];
885 } __packed;
886
887 /* EFI constants */
888 #define APFS_NX_EFI_JUMPSTART_MAGIC 0x5244534A
889 #define APFS_NX_EFI_JUMPSTART_VERSION 1
890
891 /*
892 * Information about the embedded EFI driver
893 */
894 struct apfs_nx_efi_jumpstart {
895 struct apfs_obj_phys nej_o;
896 __le32 nej_magic;
897 __le32 nej_version;
898 __le32 nej_efi_file_len;
899 __le32 nej_num_extents;
900 __le64 nej_reserved[16];
901 struct apfs_prange nej_rec_extents[];
902 } __packed;
903
904 /* Main container */
905
906 /* Container constants */
907 #define APFS_NX_MAGIC 0x4253584E
908 #define APFS_NX_BLOCK_NUM 0
909 #define APFS_NX_MAX_FILE_SYSTEMS 100
910
911 #define APFS_NX_EPH_INFO_COUNT 4
912 #define APFS_NX_EPH_MIN_BLOCK_COUNT 8
913 #define APFS_NX_MAX_FILE_SYSTEM_EPH_STRUCTS 4
914 #define APFS_NX_TX_MIN_CHECKPOINT_COUNT 4
915 #define APFS_NX_EPH_INFO_VERSION_1 1
916
917 /* Container flags */
918 #define APFS_NX_RESERVED_1 0x00000001LL
919 #define APFS_NX_RESERVED_2 0x00000002LL
920 #define APFS_NX_CRYPTO_SW 0x00000004LL
921 #define APFS_NX_FLAGS_VALID_MASK (APFS_NX_RESERVED_1 \
922 | APFS_NX_RESERVED_2 \
923 | APFS_NX_CRYPTO_SW)
924
925 /* Optional container feature flags */
926 #define APFS_NX_FEATURE_DEFRAG 0x0000000000000001ULL
927 #define APFS_NX_FEATURE_LCFD 0x0000000000000002ULL
928 #define APFS_NX_SUPPORTED_FEATURES_MASK (APFS_NX_FEATURE_DEFRAG | \
929 APFS_NX_FEATURE_LCFD)
930
931 /* Read-only compatible container feature flags */
932 #define APFS_NX_SUPPORTED_ROCOMPAT_MASK (0x0ULL)
933
934 /* Incompatible container feature flags */
935 #define APFS_NX_INCOMPAT_VERSION1 0x0000000000000001ULL
936 #define APFS_NX_INCOMPAT_VERSION2 0x0000000000000002ULL
937 #define APFS_NX_INCOMPAT_FUSION 0x0000000000000100ULL
938 #define APFS_NX_SUPPORTED_INCOMPAT_MASK (APFS_NX_INCOMPAT_VERSION2 \
939 | APFS_NX_INCOMPAT_FUSION)
940
941 /* Block and container sizes */
942 #define APFS_NX_MINIMUM_BLOCK_SIZE 4096
943 #define APFS_NX_DEFAULT_BLOCK_SIZE 4096
944 #define APFS_NX_MAXIMUM_BLOCK_SIZE 65536
945 #define APFS_NX_MINIMUM_CONTAINER_SIZE 1048576
946
947 /* Indexes into a container superblock's array of counters */
948 enum {
949 APFS_NX_CNTR_OBJ_CKSUM_SET = 0,
950 APFS_NX_CNTR_OBJ_CKSUM_FAIL = 1,
951
952 APFS_NX_NUM_COUNTERS = 32
953 };
954
955 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
956 #define UUID_SIZE 16
957 #endif
958
959 /*
960 * On-disk representation of the container superblock
961 */
962 struct apfs_nx_superblock {
963 /*00*/ struct apfs_obj_phys nx_o;
964 /*20*/ __le32 nx_magic;
965 __le32 nx_block_size;
966 __le64 nx_block_count;
967
968 /*30*/ __le64 nx_features;
969 __le64 nx_readonly_compatible_features;
970 __le64 nx_incompatible_features;
971
972 /*48*/ char nx_uuid[UUID_SIZE];
973
974 /*58*/ __le64 nx_next_oid;
975 __le64 nx_next_xid;
976
977 /*68*/ __le32 nx_xp_desc_blocks;
978 __le32 nx_xp_data_blocks;
979 /*70*/ __le64 nx_xp_desc_base;
980 __le64 nx_xp_data_base;
981 __le32 nx_xp_desc_next;
982 __le32 nx_xp_data_next;
983 /*88*/ __le32 nx_xp_desc_index;
984 __le32 nx_xp_desc_len;
985 __le32 nx_xp_data_index;
986 __le32 nx_xp_data_len;
987
988 /*98*/ __le64 nx_spaceman_oid;
989 __le64 nx_omap_oid;
990 __le64 nx_reaper_oid;
991
992 /*B0*/ __le32 nx_test_type;
993
994 __le32 nx_max_file_systems;
995 /*B8*/ __le64 nx_fs_oid[APFS_NX_MAX_FILE_SYSTEMS];
996 /*3D8*/ __le64 nx_counters[APFS_NX_NUM_COUNTERS];
997 /*4D8*/ struct apfs_prange nx_blocked_out_prange;
998 __le64 nx_evict_mapping_tree_oid;
999 /*4F0*/ __le64 nx_flags;
1000 __le64 nx_efi_jumpstart;
1001 /*500*/ char nx_fusion_uuid[UUID_SIZE];
1002 struct apfs_prange nx_keylocker;
1003 /*520*/ __le64 nx_ephemeral_info[APFS_NX_EPH_INFO_COUNT];
1004
1005 /*540*/ __le64 nx_test_oid;
1006
1007 __le64 nx_fusion_mt_oid;
1008 /*550*/ __le64 nx_fusion_wbc_oid;
1009 struct apfs_prange nx_fusion_wbc;
1010
1011 __le64 nx_newest_mounted_version;
1012
1013 /*570*/ struct apfs_prange nx_mkb_locker;
1014 } __packed;
1015
1016 /*
1017 * A mapping from an ephemeral object id to its physical address
1018 */
1019 struct apfs_checkpoint_mapping {
1020 __le32 cpm_type;
1021 __le32 cpm_subtype;
1022 __le32 cpm_size;
1023 __le32 cpm_pad;
1024 __le64 cpm_fs_oid;
1025 __le64 cpm_oid;
1026 __le64 cpm_paddr;
1027 } __packed;
1028
1029 /* Checkpoint flags */
1030 #define APFS_CHECKPOINT_MAP_LAST 0x00000001
1031
1032 /*
1033 * A checkpoint-mapping block
1034 */
1035 struct apfs_checkpoint_map_phys {
1036 struct apfs_obj_phys cpm_o;
1037 __le32 cpm_flags;
1038 __le32 cpm_count;
1039 struct apfs_checkpoint_mapping cpm_map[];
1040 } __packed;
1041
1042 /* Volume */
1043
1044 /* Volume constants */
1045 #define APFS_MAGIC 0x42535041
1046
1047 #define APFS_MAX_HIST 8
1048 #define APFS_VOLNAME_LEN 256
1049
1050 /* Volume flags */
1051 #define APFS_FS_UNENCRYPTED 0x00000001LL
1052 #define APFS_FS_EFFACEABLE 0x00000002LL
1053 #define APFS_FS_RESERVED_4 0x00000004LL
1054 #define APFS_FS_ONEKEY 0x00000008LL
1055 #define APFS_FS_SPILLEDOVER 0x00000010LL
1056 #define APFS_FS_RUN_SPILLOVER_CLEANER 0x00000020LL
1057 #define APFS_FS_ALWAYS_CHECK_EXTENTREF 0x00000040LL
1058 #define APFS_FS_PREVIOUSLY_SEALED 0x00000080LL /* Made-up name */
1059 #define APFS_FS_PFK 0x00000100LL /* Made-up name */
1060 #define APFS_FS_UNKNOWN_200 0x00000200LL
1061 #define APFS_FS_FLAGS_VALID_MASK (APFS_FS_UNENCRYPTED \
1062 | APFS_FS_EFFACEABLE \
1063 | APFS_FS_RESERVED_4 \
1064 | APFS_FS_ONEKEY \
1065 | APFS_FS_SPILLEDOVER \
1066 | APFS_FS_RUN_SPILLOVER_CLEANER \
1067 | APFS_FS_ALWAYS_CHECK_EXTENTREF \
1068 | APFS_FS_PREVIOUSLY_SEALED \
1069 | APFS_FS_PFK \
1070 | APFS_FS_UNKNOWN_200)
1071
1072 #define APFS_FS_CRYPTOFLAGS (APFS_FS_UNENCRYPTED \
1073 | APFS_FS_EFFACEABLE \
1074 | APFS_FS_ONEKEY)
1075
1076 /* Volume roles */
1077 #define APFS_VOLUME_ENUM_SHIFT 6
1078 #define APFS_VOL_ROLE_NONE 0x0000
1079 #define APFS_VOL_ROLE_SYSTEM 0x0001
1080 #define APFS_VOL_ROLE_USER 0x0002
1081 #define APFS_VOL_ROLE_RECOVERY 0x0004
1082 #define APFS_VOL_ROLE_VM 0x0008
1083 #define APFS_VOL_ROLE_PREBOOT 0x0010
1084 #define APFS_VOL_ROLE_INSTALLER 0x0020
1085 #define APFS_VOL_ROLE_DATA (1 << APFS_VOLUME_ENUM_SHIFT)
1086 #define APFS_VOL_ROLE_BASEBAND (2 << APFS_VOLUME_ENUM_SHIFT)
1087 #define APFS_VOL_ROLE_UPDATE (3 << APFS_VOLUME_ENUM_SHIFT)
1088 #define APFS_VOL_ROLE_XART (4 << APFS_VOLUME_ENUM_SHIFT)
1089 #define APFS_VOL_ROLE_HARDWARE (5 << APFS_VOLUME_ENUM_SHIFT)
1090 #define APFS_VOL_ROLE_BACKUP (6 << APFS_VOLUME_ENUM_SHIFT)
1091 #define APFS_VOL_ROLE_RESERVED_7 (7 << APFS_VOLUME_ENUM_SHIFT)
1092 #define APFS_VOL_ROLE_RESERVED_8 (8 << APFS_VOLUME_ENUM_SHIFT)
1093 #define APFS_VOL_ROLE_ENTERPRISE (9 << APFS_VOLUME_ENUM_SHIFT)
1094 #define APFS_VOL_ROLE_RESERVED_10 (10 << APFS_VOLUME_ENUM_SHIFT)
1095 #define APFS_VOL_ROLE_PRELOGIN (11 << APFS_VOLUME_ENUM_SHIFT)
1096 #define APFS_VOL_ROLES_VALID_MASK (APFS_VOL_ROLE_SYSTEM \
1097 | APFS_VOL_ROLE_USER \
1098 | APFS_VOL_ROLE_RECOVERY \
1099 | APFS_VOL_ROLE_VM \
1100 | APFS_VOL_ROLE_PREBOOT \
1101 | APFS_VOL_ROLE_INSTALLER \
1102 | APFS_VOL_ROLE_DATA \
1103 | APFS_VOL_ROLE_BASEBAND \
1104 | APFS_VOL_ROLE_UPDATE \
1105 | APFS_VOL_ROLE_XART \
1106 | APFS_VOL_ROLE_HARDWARE \
1107 | APFS_VOL_ROLE_BACKUP \
1108 | APFS_VOL_ROLE_RESERVED_7 \
1109 | APFS_VOL_ROLE_RESERVED_8 \
1110 | APFS_VOL_ROLE_ENTERPRISE \
1111 | APFS_VOL_ROLE_RESERVED_10 \
1112 | APFS_VOL_ROLE_PRELOGIN)
1113
1114 /* Optional volume feature flags */
1115 #define APFS_FEATURE_DEFRAG_PRERELEASE 0x00000001LL
1116 #define APFS_FEATURE_HARDLINK_MAP_RECORDS 0x00000002LL
1117 #define APFS_FEATURE_DEFRAG 0x00000004LL
1118 #define APFS_FEATURE_STRICTATIME 0x00000008LL
1119 #define APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE 0x00000010LL
1120
1121 #define APFS_SUPPORTED_FEATURES_MASK (APFS_FEATURE_DEFRAG \
1122 | APFS_FEATURE_DEFRAG_PRERELEASE \
1123 | APFS_FEATURE_HARDLINK_MAP_RECORDS \
1124 | APFS_FEATURE_STRICTATIME \
1125 | APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE)
1126
1127 /* Read-only compatible volume feature flags */
1128 #define APFS_SUPPORTED_ROCOMPAT_MASK (0x0ULL)
1129
1130 /* Incompatible volume feature flags */
1131 #define APFS_INCOMPAT_CASE_INSENSITIVE 0x00000001LL
1132 #define APFS_INCOMPAT_DATALESS_SNAPS 0x00000002LL
1133 #define APFS_INCOMPAT_ENC_ROLLED 0x00000004LL
1134 #define APFS_INCOMPAT_NORMALIZATION_INSENSITIVE 0x00000008LL
1135 #define APFS_INCOMPAT_INCOMPLETE_RESTORE 0x00000010LL
1136 #define APFS_INCOMPAT_SEALED_VOLUME 0x00000020LL
1137 #define APFS_INCOMPAT_PFK 0x00000040LL /* Made-up name */
1138 #define APFS_INCOMPAT_EXTENT_PREALLOC_FLAG 0x00000080LL /* Made-up name */
1139 #define APFS_INCOMPAT_SECONDARY_FSROOT 0x00000100LL /* Made-up name */
1140
1141 #define APFS_SUPPORTED_INCOMPAT_MASK (APFS_INCOMPAT_CASE_INSENSITIVE \
1142 | APFS_INCOMPAT_DATALESS_SNAPS \
1143 | APFS_INCOMPAT_ENC_ROLLED \
1144 | APFS_INCOMPAT_NORMALIZATION_INSENSITIVE \
1145 | APFS_INCOMPAT_INCOMPLETE_RESTORE \
1146 | APFS_INCOMPAT_SEALED_VOLUME \
1147 | APFS_INCOMPAT_PFK \
1148 | APFS_INCOMPAT_EXTENT_PREALLOC_FLAG \
1149 | APFS_INCOMPAT_SECONDARY_FSROOT)
1150
1151 #define APFS_MODIFIED_NAMELEN 32
1152
1153 /*
1154 * Structure containing information about a program that modified the volume
1155 */
1156 struct apfs_modified_by {
1157 u8 id[APFS_MODIFIED_NAMELEN];
1158 __le64 timestamp;
1159 __le64 last_xid;
1160 } __packed;
1161
1162 /* Version constants for wrapped meta crypto state */
1163 #define APFS_WMCS_MAJOR_VERSION 5
1164 #define APFS_WMCS_MINOR_VERSION 0
1165
1166 /* Protection classes */
1167 #define APFS_PROTECTION_CLASS_DIR_NONE 0 /* Inherits the directory's default */
1168 #define APFS_PROTECTION_CLASS_A 1
1169 #define APFS_PROTECTION_CLASS_B 2
1170 #define APFS_PROTECTION_CLASS_C 3
1171 #define APFS_PROTECTION_CLASS_D 4 /* No protection */
1172 #define APFS_PROTECTION_CLASS_F 6 /* No protection, nonpersistent key */
1173
1174 /* Encryption identifiers */
1175 #define APFS_CRYPTO_SW_ID 4
1176 #define APFS_CRYPTO_RESERVED_5 5
1177 #define APFS_UNASSIGNED_CRYPTO_ID (~0ULL)
1178
1179 /* Doc id index flags. I'm making up the names for now. */
1180 #define APFS_DOC_ID_HAS_PREV_TREE 0x00000001
1181 #define APFS_DOC_ID_UNKNOWN_02 0x00000002
1182 #define APFS_DOC_ID_UNKNOWN_04 0x00000004
1183 #define APFS_DOC_ID_UNKNOWN_08 0x00000008
1184 #define APFS_DOC_ID_UNKNOWN_10 0x00000010
1185 #define APFS_DOC_ID_VALID_FLAGS (APFS_DOC_ID_HAS_PREV_TREE \
1186 | APFS_DOC_ID_UNKNOWN_02 \
1187 | APFS_DOC_ID_UNKNOWN_04 \
1188 | APFS_DOC_ID_UNKNOWN_08 \
1189 | APFS_DOC_ID_UNKNOWN_10)
1190
1191 /*
1192 * Structure used to store the encryption state
1193 */
1194 struct apfs_wrapped_meta_crypto_state {
1195 __le16 major_version;
1196 __le16 minor_version;
1197 __le32 cpflags;
1198 __le32 persistent_class;
1199 __le32 key_os_version;
1200 __le16 key_revision;
1201 __le16 unused;
1202 } __packed;
1203
1204 /*
1205 * On-disk representation of a volume superblock
1206 */
1207 struct apfs_superblock {
1208 /*00*/ struct apfs_obj_phys apfs_o;
1209
1210 /*20*/ __le32 apfs_magic;
1211 __le32 apfs_fs_index;
1212
1213 /*28*/ __le64 apfs_features;
1214 __le64 apfs_readonly_compatible_features;
1215 __le64 apfs_incompatible_features;
1216
1217 /*40*/ __le64 apfs_unmount_time;
1218
1219 __le64 apfs_fs_reserve_block_count;
1220 __le64 apfs_fs_quota_block_count;
1221 __le64 apfs_fs_alloc_count;
1222
1223 /*60*/ struct apfs_wrapped_meta_crypto_state apfs_meta_crypto;
1224
1225 /*74*/ __le32 apfs_root_tree_type;
1226 __le32 apfs_extentref_tree_type;
1227 __le32 apfs_snap_meta_tree_type;
1228
1229 /*80*/ __le64 apfs_omap_oid;
1230 __le64 apfs_root_tree_oid;
1231 __le64 apfs_extentref_tree_oid;
1232 __le64 apfs_snap_meta_tree_oid;
1233
1234 /*A0*/ __le64 apfs_revert_to_xid;
1235 __le64 apfs_revert_to_sblock_oid;
1236
1237 /*B0*/ __le64 apfs_next_obj_id;
1238
1239 /*B8*/ __le64 apfs_num_files;
1240 __le64 apfs_num_directories;
1241 __le64 apfs_num_symlinks;
1242 __le64 apfs_num_other_fsobjects;
1243 __le64 apfs_num_snapshots;
1244
1245 /*E0*/ __le64 apfs_total_blocks_alloced;
1246 __le64 apfs_total_blocks_freed;
1247
1248 /*F0*/ char apfs_vol_uuid[UUID_SIZE];
1249 /*100*/ __le64 apfs_last_mod_time;
1250
1251 __le64 apfs_fs_flags;
1252
1253 /*110*/ struct apfs_modified_by apfs_formatted_by;
1254 /*140*/ struct apfs_modified_by apfs_modified_by[APFS_MAX_HIST];
1255
1256 /*2C0*/ u8 apfs_volname[APFS_VOLNAME_LEN];
1257 /*3C0*/ __le32 apfs_next_doc_id;
1258
1259 __le16 apfs_role;
1260 __le16 reserved;
1261
1262 /*3C8*/ __le64 apfs_root_to_xid;
1263 __le64 apfs_er_state_oid;
1264
1265 __le64 apfs_cloneinfo_id_epoch;
1266 __le64 apfs_cloneinfo_xid;
1267
1268 __le64 apfs_snap_meta_ext_oid;
1269
1270 /*3F0*/ char apfs_volume_group_id[UUID_SIZE];
1271
1272 /*400*/ __le64 apfs_integrity_meta_oid;
1273
1274 __le64 apfs_fext_tree_oid;
1275 /*410*/ __le32 apfs_fext_tree_type;
1276
1277 __le32 reserved_type;
1278 __le64 reserved_oid;
1279
1280 /*420*/ __le64 apfs_doc_id_index_xid;
1281 __le32 apfs_doc_id_index_flags;
1282 __le32 apfs_doc_id_tree_type;
1283 /*430*/ __le64 apfs_doc_id_tree_oid; /* Made-up name */
1284 __le64 apfs_prev_doc_id_tree_oid;
1285 __le64 apfs_doc_id_fixup_cursor;
1286 __le64 apfs_sec_root_tree_oid;
1287 /*450*/ __le32 apfs_sec_root_tree_type;
1288 } __packed;
1289
1290 /* Extended attributes constants */
1291 #define APFS_XATTR_MAX_EMBEDDED_SIZE 3804
1292
1293 /* Extended attributes names */
1294 #define APFS_XATTR_NAME_SYMLINK "com.apple.fs.symlink"
1295 #define APFS_XATTR_NAME_COMPRESSED "com.apple.decmpfs"
1296 #define APFS_XATTR_NAME_RSRC_FORK "com.apple.ResourceFork"
1297 #define APFS_XATTR_NAME_SECURITY "com.apple.system.Security"
1298 #define APFS_XATTR_NAME_FINDER_INFO "com.apple.FinderInfo"
1299
1300 /* Extended attributes flags */
1301 enum {
1302 APFS_XATTR_DATA_STREAM = 0x00000001,
1303 APFS_XATTR_DATA_EMBEDDED = 0x00000002,
1304 APFS_XATTR_FILE_SYSTEM_OWNED = 0x00000004,
1305 APFS_XATTR_RESERVED_8 = 0x00000008,
1306 APFS_XATTR_UNKNOWN_10 = 0x00000010,
1307 };
1308
1309 #define APFS_XATTR_VALID_FLAGS 0x0000001f
1310
1311 /*
1312 * Structure of the value of an extended attributes record
1313 */
1314 struct apfs_xattr_val {
1315 __le16 flags;
1316 __le16 xdata_len;
1317 u8 xdata[];
1318 } __packed;
1319
1320 /*
1321 * Structure used to store the data of an extended attributes record
1322 */
1323 struct apfs_xattr_dstream {
1324 __le64 xattr_obj_id;
1325 struct apfs_dstream dstream;
1326 } __packed;
1327
1328 /*
1329 * Integrity metadata for a sealed volume
1330 */
1331 struct apfs_integrity_meta_phys {
1332 struct apfs_obj_phys im_o;
1333
1334 __le32 im_version;
1335 __le32 im_flags;
1336 __le32 im_hash_type;
1337 __le32 im_root_hash_offset;
1338 __le64 im_broken_xid;
1339 __le64 im_reserved[9];
1340 } __packed;
1341
1342 /*
1343 * Version numbers for the integrity metadata structure
1344 */
1345 enum {
1346 APFS_INTEGRITY_META_VERSION_INVALID = 0,
1347 APFS_INTEGRITY_META_VERSION_1 = 1,
1348 APFS_INTEGRITY_META_VERSION_2 = 2,
1349 APFS_INTEGRITY_META_VERSION_HIGHEST = APFS_INTEGRITY_META_VERSION_2
1350 };
1351
1352 /* Flags used by integrity metadata */
1353 #define APFS_SEAL_BROKEN (1U << 0)
1354
1355 /*
1356 * Constants used to identify hash algorithms
1357 */
1358 enum {
1359 APFS_HASH_INVALID = 0,
1360 APFS_HASH_SHA256 = 0x1,
1361 APFS_HASH_SHA512_256 = 0x2,
1362 APFS_HASH_SHA384 = 0x3,
1363 APFS_HASH_SHA512 = 0x4,
1364
1365 APFS_HASH_MIN = APFS_HASH_SHA256,
1366 APFS_HASH_MAX = APFS_HASH_SHA512,
1367 APFS_HASH_DEFAULT = APFS_HASH_SHA256,
1368 };
1369
1370 #define APFS_HASH_CCSHA256_SIZE 32
1371 #define APFS_HASH_CCSHA512_256_SIZE 32
1372 #define APFS_HASH_CCSHA384_SIZE 48
1373 #define APFS_HASH_CCSHA512_SIZE 64
1374
1375 #define APFS_HASH_MAX_SIZE 64
1376
1377 /*
1378 * Structure of a key in a fext tree
1379 */
1380 struct apfs_fext_tree_key {
1381 __le64 private_id;
1382 __le64 logical_addr;
1383 } __packed;
1384
1385 /*
1386 * Structure of a value in a fext tree
1387 */
1388 struct apfs_fext_tree_val {
1389 __le64 len_and_flags;
1390 __le64 phys_block_num;
1391 } __packed;
1392
1393 /*
1394 * Structure of the key for a file info record
1395 */
1396 struct apfs_file_info_key {
1397 struct apfs_key_header hdr;
1398 __le64 info_and_lba;
1399 } __packed;
1400
1401 #define APFS_FILE_INFO_LBA_MASK 0x00ffffffffffffffULL
1402 #define APFS_FILE_INFO_TYPE_MASK 0xff00000000000000ULL
1403 #define APFS_FILE_INFO_TYPE_SHIFT 56
1404
1405 /*
1406 * A hash of file data
1407 */
1408 struct apfs_file_data_hash_val {
1409 __le16 hashed_len;
1410 u8 hash_size;
1411 u8 hash[];
1412 } __packed;
1413
1414 #define APFS_FILE_INFO_DATA_HASH 1
1415
1416 /*
1417 * Structure of the value for a file info record
1418 */
1419 struct apfs_file_info_val {
1420 union {
1421 struct apfs_file_data_hash_val dhash;
1422 };
1423 } __packed;
1424
1425 #define APFS_BTREE_NODE_HASH_SIZE_MAX 64
1426
1427 /*
1428 * Structure of the value of an index record for a hashed catalog tree
1429 */
1430 struct apfs_btn_index_node_val {
1431 __le64 binv_child_oid;
1432 /*
1433 * The reference seems to be wrong about the hash size, at least for
1434 * SHA-256. TODO: check what happens with other hash functions.
1435 */
1436 u8 binv_child_hash[APFS_HASH_CCSHA256_SIZE];
1437 } __packed;
1438
1439 /*
1440 * Compressed file header
1441 */
1442 struct apfs_compress_hdr {
1443 __le32 signature;
1444 __le32 algo;
1445 __le64 size;
1446 } __packed;
1447
1448 #define APFS_COMPRESS_ZLIB_ATTR 3
1449 #define APFS_COMPRESS_ZLIB_RSRC 4
1450 #define APFS_COMPRESS_LZVN_ATTR 7
1451 #define APFS_COMPRESS_LZVN_RSRC 8
1452 #define APFS_COMPRESS_PLAIN_ATTR 9
1453 #define APFS_COMPRESS_PLAIN_RSRC 10
1454 #define APFS_COMPRESS_LZFSE_ATTR 11
1455 #define APFS_COMPRESS_LZFSE_RSRC 12
1456 #define APFS_COMPRESS_LZBITMAP_ATTR 13
1457 #define APFS_COMPRESS_LZBITMAP_RSRC 14
1458
1459 struct apfs_compress_rsrc_hdr {
1460 __be32 data_offs;
1461 __be32 mgmt_offs;
1462 __be32 data_size;
1463 __be32 mgmt_size;
1464 } __packed;
1465
1466 #define APFS_COMPRESS_BLOCK 0x10000
1467
1468 struct apfs_compress_rsrc_data {
1469 __le32 unknown;
1470 __le32 num;
1471 struct apfs_compress_rsrc_block {
1472 __le32 offs;
1473 __le32 size;
1474 } __packed block[];
1475 } __packed;
1476
1477 #define APFS_FUSION_TIER2_DEVICE_BYTE_ADDR 0x4000000000000000ULL
1478
1479 /*
1480 * TODO: this is a placeholder, create some snapshots with the official
1481 * implementation to find the actual limit.
1482 */
1483 #define APFS_SNAP_MAX_NAMELEN 255
1484
1485 /*
1486 * Structure of the value of a snapshot metadata record
1487 */
1488 struct apfs_snap_metadata_val {
1489 __le64 extentref_tree_oid;
1490 __le64 sblock_oid;
1491 __le64 create_time;
1492 __le64 change_time;
1493 __le64 inum;
1494 __le32 extentref_tree_type;
1495 __le32 flags;
1496 __le16 name_len;
1497 u8 name[];
1498 } __packed;
1499
1500 /* Snapshot metadata record flags */
1501 enum {
1502 APFS_SNAP_META_PENDING_DATALESS = 0x00000001,
1503 APFS_SNAP_MERGE_IN_PROGRESS = 0x00000002,
1504 };
1505
1506 /*
1507 * Structure of the value of a snapshot name record
1508 */
1509 struct apfs_snap_name_val {
1510 __le64 snap_xid;
1511 } __packed;
1512
1513 /*
1514 * Structure of the extended snapshot metadata
1515 */
1516 struct apfs_snap_meta_ext {
1517 struct apfs_obj_phys sme_o;
1518
1519 __le32 sme_version;
1520 __le32 sme_flags;
1521 __le64 sme_snap_xid;
1522 char sme_uuid[UUID_SIZE];
1523 __le64 sme_token;
1524 } __packed;
1525
1526 #define APFS_OBJECT_TYPE_KEYBAG 0x6b657973 /* Spells 'syek' */
1527
1528 #define APFS_VOL_KEYBAG_ENTRY_MAX_SIZE 512
1529 #define APFS_FV_PERSONAL_RECOVERY_KEY_UUID "EBC6C064-0000-11AA-AA11-00306543ECAC"
1530
1531 /* Keybag entry types */
1532 enum {
1533 KB_TAG_UNKNOWN = 0,
1534 KB_TAG_RESERVED_1 = 1,
1535
1536 KB_TAG_VOLUME_KEY = 2,
1537 KB_TAG_VOLUME_UNLOCK_RECORDS = 3,
1538 KB_TAG_VOLUME_PASSPHRASE_HINT = 4,
1539
1540 KB_TAG_WRAPPING_M_KEY = 5,
1541 KB_TAG_VOLUME_M_KEY = 6,
1542
1543 KB_TAG_RESERVED_F8 = 0xF8
1544 };
1545
1546 /*
1547 * Structure of a single entry in the keybag
1548 */
1549 struct apfs_keybag_entry {
1550 char ke_uuid[UUID_SIZE];
1551 __le16 ke_tag;
1552 __le16 ke_keylen;
1553 __le32 padding;
1554 u8 ke_keydata[];
1555 } __packed;
1556
1557 #define APFS_KEYBAG_VERSION 2
1558
1559 /*
1560 * Structure of the locker in the keybag
1561 */
1562 struct apfs_kb_locker {
1563 __le16 kl_version;
1564 __le16 kl_nkeys;
1565 __le32 kl_nbytes;
1566 __le64 padding;
1567 struct apfs_keybag_entry kl_entries[];
1568 } __packed;
1569
1570 #endif /* _APFS_RAW_H */