Debian Codegraph @

apfs.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
4 */
5
6 #ifndef _APFS_H
7 #define _APFS_H
8
9 #include <linux/buffer_head.h>
10 #include <linux/fs.h>
11 #include <linux/list.h>
12 #include <linux/types.h>
13 #include <linux/version.h>
14
15 /*
16 * Redhat kernels regularly break the api, so they need their own version
17 * checks. Make sure they always fail for non-rhel kernels.
18 *
19 * By the way, if rhel keeps picking up breaking patches for old majors after
20 * a new one is out, this check could break down and things could get more
21 * complicated. I don't think they do that though...
22 */
23 #ifdef RHEL_RELEASE
24 #define RHEL_VERSION_GE(a, b) (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(a, b))
25 #else
26 #define RHEL_VERSION_GE(a, b) 0
27 #endif
28
29 #include "apfs_raw.h"
30
31 #define EFSBADCRC EBADMSG /* Bad CRC detected */
32 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
33
34 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0) /* SB_RDONLY came in 4.14 */
35 #define SB_RDONLY MS_RDONLY
36 #define SB_SILENT MS_SILENT
37 #define SB_NOSEC MS_NOSEC
38 #define SB_ACTIVE MS_ACTIVE
39 static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags & SB_RDONLY; }
40 #endif
41
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 3, 0)
43 #define lockdep_assert_held_write(l) ((void)(l))
44 #endif
45
46 /* Compatibility wrapper around submit_bh() */
47 #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 2, 0)
48 #define apfs_submit_bh(op, op_flags, bh) \
49 do { \
50 if (op == REQ_OP_READ) \
51 bh_submit(bh, op | op_flags, bh_end_read); \
52 else \
53 bh_submit(bh, op | op_flags, bh_end_write); \
54 } while (0)
55 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) || RHEL_VERSION_GE(9, 2)
56 #define apfs_submit_bh(op, op_flags, bh) \
57 do { \
58 if (op == REQ_OP_READ) \
59 bh->b_end_io = end_buffer_read_sync; \
60 else \
61 bh->b_end_io = end_buffer_write_sync; \
62 submit_bh(op | op_flags, bh); \
63 } while (0)
64 #else
65 #define apfs_submit_bh(op, op_flags, bh) \
66 do { \
67 if (op == REQ_OP_READ) \
68 bh->b_end_io = end_buffer_read_sync; \
69 else \
70 bh->b_end_io = end_buffer_write_sync; \
71 submit_bh(op, op_flags, bh); \
72 } while (0)
73 #endif
74
75 /*
76 * Parameter for the snapshot creation ioctl
77 */
78 struct apfs_ioctl_snap_name {
79 char name[APFS_SNAP_MAX_NAMELEN + 1];
80 };
81
82 #define APFS_IOC_SET_DFLT_PFK _IOW('@', 0x80, struct apfs_wrapped_crypto_state)
83 #define APFS_IOC_SET_DIR_CLASS _IOW('@', 0x81, u32)
84 #define APFS_IOC_SET_PFK _IOW('@', 0x82, struct apfs_wrapped_crypto_state)
85 #define APFS_IOC_GET_CLASS _IOR('@', 0x83, u32)
86 #define APFS_IOC_GET_PFK _IOR('@', 0x84, struct apfs_wrapped_crypto_state)
87 #define APFS_IOC_TAKE_SNAPSHOT _IOW('@', 0x85, struct apfs_ioctl_snap_name)
88
89 /*
90 * In-memory representation of an APFS object
91 */
92 struct apfs_object {
93 struct super_block *sb;
94 u64 block_nr;
95 u64 oid;
96
97 /*
98 * Buffer head containing the one block of the object, may be NULL if
99 * the object is only in memory. TODO: support objects with more than
100 * one block.
101 */
102 struct buffer_head *o_bh;
103 char *data; /* The raw object */
104 bool ephemeral; /* Is this an ephemeral object? */
105 };
106
107 /* Constants used in managing the size of a node's table of contents */
108 #define APFS_BTREE_TOC_ENTRY_INCREMENT 8
109 #define APFS_BTREE_TOC_ENTRY_MAX_UNUSED (2 * BTREE_TOC_ENTRY_INCREMENT)
110
111 /*
112 * In-memory representation of an APFS node
113 */
114 struct apfs_node {
115 u32 tree_type; /* Tree type (subtype of the node object) */
116 u16 flags; /* Node flags */
117 u32 records; /* Number of records in the node */
118
119 int key; /* Offset of the key area in the block */
120 int free; /* Offset of the free area in the block */
121 int val; /* Offset of the value area in the block */
122
123 int key_free_list_len; /* Length of the fragmented free key space */
124 int val_free_list_len; /* Length of the fragmented free value space */
125
126 struct apfs_object object; /* Object holding the node */
127 };
128
129 /**
130 * apfs_node_is_leaf - Check if a b-tree node is a leaf
131 * @node: the node to check
132 */
133 static inline bool apfs_node_is_leaf(struct apfs_node *node)
134 {
135 return (node->flags & APFS_BTNODE_LEAF) != 0;
136 }
137
138 /**
139 * apfs_node_is_root - Check if a b-tree node is the root
140 * @node: the node to check
141 */
142 static inline bool apfs_node_is_root(struct apfs_node *node)
143 {
144 return (node->flags & APFS_BTNODE_ROOT) != 0;
145 }
146
147 /**
148 * apfs_node_has_fixed_kv_size - Check if a b-tree node has fixed key/value
149 * sizes
150 * @node: the node to check
151 */
152 static inline bool apfs_node_has_fixed_kv_size(struct apfs_node *node)
153 {
154 return (node->flags & APFS_BTNODE_FIXED_KV_SIZE) != 0;
155 }
156
157 struct apfs_ip_bitmap_block_info {
158 bool dirty; /* Do we need to commit this block to disk? */
159 void *block; /* In-memory address of the bitmap block */
160 };
161
162 /*
163 * Space manager data in memory.
164 */
165 struct apfs_spaceman {
166 struct apfs_spaceman_phys *sm_raw; /* On-disk spaceman structure */
167 struct apfs_nxsb_info *sm_nxi; /* Container superblock */
168 u32 sm_size; /* Size of @sm_raw in bytes */
169
170 u32 sm_blocks_per_chunk; /* Blocks covered by a bitmap block */
171 u32 sm_chunks_per_cib; /* Chunk count in a chunk-info block */
172 u64 sm_block_count; /* Block count for the container */
173 u64 sm_chunk_count; /* Number of bitmap blocks */
174 u32 sm_cib_count; /* Number of chunk-info blocks */
175 u64 sm_free_count; /* Number of free blocks */
176 u32 sm_addr_offset; /* Offset of cib addresses in @sm_raw */
177 u64 sm_main_fq_nodes; /* Number of nodes in the main fq */
178
179 /*
180 * A range of freed blocks not yet put in the free queue. Extend this as
181 * much as possible before creating an actual record.
182 */
183 u64 sm_free_cache_base;
184 u64 sm_free_cache_blkcnt;
185
186 /* Shift to match an ip block with its bitmap in the array */
187 int sm_ip_bmaps_shift;
188 /* Mask to find an ip block's offset inside its ip bitmap */
189 u32 sm_ip_bmaps_mask;
190 /* Number of ip bitmaps */
191 u32 sm_ip_bmaps_count;
192 /* List of ip bitmaps, in order */
193 struct apfs_ip_bitmap_block_info sm_ip_bmaps[];
194 };
195
196 #define APFS_TRANS_MAIN_QUEUE_MAX 10000
197 #define APFS_TRANS_BUFFERS_MAX 65536
198 #define APFS_TRANS_STARTS_MAX 65536
199
200 /* Possible states for the container transaction structure */
201 #define APFS_NX_TRANS_FORCE_COMMIT 1 /* Commit guaranteed */
202 #define APFS_NX_TRANS_DEFER_COMMIT 2 /* Commit banned right now */
203 #define APFS_NX_TRANS_COMMITTING 4 /* Commit ongoing */
204 #define APFS_NX_TRANS_INCOMPLETE_BLOCK 8 /* A data block is not written in full */
205
206 /*
207 * ENOSPC checks before transactions are more or less coarse depending on this.
208 */
209 enum apfs_trans_kind {
210 APFS_TRANS_REG, /* Most transactions */
211 APFS_TRANS_DEL, /* Transactions that usually free space */
212 APFS_TRANS_SYNC, /* Sync (or unmount) transaction */
213 };
214
215 /* The space requirements for each kind of transaction, in blocks */
216 #define APFS_REG_ROOM 128
217 #define APFS_DEL_ROOM 20
218 #define APFS_SYNC_ROOM 6
219
220 /*
221 * Structure that keeps track of a container transaction.
222 */
223 struct apfs_nx_transaction {
224 unsigned int t_state;
225
226 struct delayed_work t_work; /* Work task for transaction commits */
227 struct super_block *t_work_sb; /* sb that last queued a commit */
228
229 struct list_head t_inodes; /* List of inodes in the transaction */
230 struct list_head t_buffers; /* List of buffers in the transaction */
231 size_t t_buffers_count; /* Count of items on the list */
232 int t_starts_count; /* Count of starts for transaction */
233 };
234
235 /* State bits for buffer heads in a transaction */
236 #define BH_TRANS BH_PrivateStart /* Attached to a transaction */
237 #define BH_CSUM (BH_PrivateStart + 1) /* Requires checksum update */
238 BUFFER_FNS(TRANS, trans);
239 BUFFER_FNS(CSUM, csum);
240
241 /*
242 * Additional information for a buffer in a transaction.
243 */
244 struct apfs_bh_info {
245 struct buffer_head *bh; /* The buffer head */
246 struct list_head list; /* List of buffers in the transaction */
247 };
248
249 /*
250 * List entry for an in-memory ephemeral object
251 */
252 struct apfs_ephemeral_object_info {
253 u64 oid; /* Ephemeral object id */
254 u32 size; /* Size of the object in bytes */
255 void *object; /* In-memory address of the object */
256 };
257
258 /*
259 * We allocate a fixed space for the list of ephemeral objects. I don't
260 * actually know how big this should be allowed to get, but all the objects
261 * must be written down with each transaction commit, so probably not too big.
262 */
263 #define APFS_EPHEMERAL_LIST_SIZE 32768
264 #define APFS_EPHEMERAL_LIST_LIMIT (APFS_EPHEMERAL_LIST_SIZE / sizeof(struct apfs_ephemeral_object_info))
265
266 /* Mount option flags for a container */
267 #define APFS_CHECK_NODES 1
268 #define APFS_READWRITE 2
269 /*
270 * Mount options for a container are decided on its first mount. This flag lets
271 * future mounts know that has happened already.
272 */
273 #define APFS_FLAGS_SET 4
274
275 /*
276 * Wrapper around block devices for portability.
277 */
278 struct apfs_blkdev_info {
279 struct block_device *blki_bdev;
280 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0) || RHEL_VERSION_GE(9, 5)
281 struct file *blki_bdev_file;
282 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
283 struct bdev_handle *blki_bdev_handle;
284 #endif
285
286 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0) && !RHEL_VERSION_GE(9, 4)
287 fmode_t blki_mode;
288 #endif
289
290 /*
291 * For tier 2, we need to remember the mount path for show_options().
292 * We don't need this for the main device of course, but better to
293 * keep things consistent.
294 */
295 char *blki_path;
296 };
297
298 /*
299 * Container superblock data in memory
300 */
301 struct apfs_nxsb_info {
302 /* Block device info for the container */
303 struct apfs_blkdev_info *nx_blkdev_info;
304 struct apfs_blkdev_info *nx_tier2_info;
305 u64 nx_tier2_bno; /* Offset for tier 2 block numbers */
306
307 struct apfs_nx_superblock *nx_raw; /* On-disk main sb */
308 u64 nx_bno; /* Current block number for the checkpoint superblock */
309 u64 nx_xid; /* Latest transaction id */
310
311 /* List of ephemeral objects in memory (except the superblock) */
312 struct apfs_ephemeral_object_info *nx_eph_list;
313 int nx_eph_count;
314
315 struct list_head vol_list; /* List of mounted volumes in container */
316
317 unsigned int nx_flags; /* Mount options shared by all volumes */
318 unsigned int nx_refcnt; /* Number of mounted volumes in container */
319
320 /* TODO: handle block sizes above the maximum of PAGE_SIZE? */
321 unsigned long nx_blocksize;
322 unsigned char nx_blocksize_bits;
323
324 struct apfs_spaceman *nx_spaceman;
325 struct apfs_nx_transaction nx_transaction;
326 int nx_trans_buffers_max;
327
328 /* For now, a single semaphore for every operation */
329 struct rw_semaphore nx_big_sem;
330
331 /* List of currently mounted containers */
332 struct list_head nx_list;
333 };
334
335 extern struct mutex nxs_mutex;
336
337 /*
338 * Omap mapping in memory.
339 * TODO: could this and apfs_omap_rec be the same struct?
340 */
341 struct apfs_omap_map {
342 u64 xid;
343 u64 bno;
344 u32 flags;
345 };
346
347 /*
348 * Omap record data in memory
349 */
350 struct apfs_omap_rec {
351 u64 oid;
352 u64 bno;
353 };
354
355 #define APFS_OMAP_CACHE_SLOTS 128
356 #define APFS_OMAP_CACHE_SLOT_MASK (APFS_OMAP_CACHE_SLOTS - 1)
357
358 /**
359 * Cache of omap records
360 */
361 struct apfs_omap_cache {
362 struct apfs_omap_rec recs[APFS_OMAP_CACHE_SLOTS];
363 bool disabled;
364 spinlock_t lock;
365 };
366
367 /*
368 * Omap structure shared by all snapshots for the same volume.
369 */
370 struct apfs_omap {
371 struct apfs_node *omap_root;
372 struct apfs_omap_cache omap_cache;
373
374 /* Transaction id for most recent snapshot */
375 u64 omap_latest_snap;
376
377 /* Number of snapshots sharing this omap */
378 unsigned int omap_refcnt;
379 };
380
381 /*
382 * Volume superblock data in memory
383 */
384 struct apfs_sb_info {
385 struct apfs_nxsb_info *s_nxi; /* In-memory container sb for volume */
386 struct list_head list; /* List of mounted volumes in container */
387 struct apfs_superblock *s_vsb_raw; /* On-disk volume sb */
388
389 dev_t s_anon_dev; /* Anonymous device for this volume-snapshot */
390
391 char *s_tier2_path; /* Path to the tier 2 device */
392
393 char *s_snap_name; /* Label for the mounted snapshot */
394 u64 s_snap_xid; /* Transaction id for mounted snapshot */
395
396 struct apfs_node *s_cat_root; /* Root of the catalog tree */
397 struct apfs_omap *s_omap; /* The object map */
398
399 struct apfs_object s_vobject; /* Volume superblock object */
400
401 /* Mount options */
402 unsigned int s_vol_nr; /* Index of the volume in the sb list */
403 kuid_t s_uid; /* uid to override on-disk uid */
404 kgid_t s_gid; /* gid to override on-disk gid */
405 unsigned int s_mount_opt;
406
407 struct apfs_crypto_state_val *s_dflt_pfk; /* default per-file key */
408
409 struct inode *s_private_dir; /* Inode for the private directory */
410 struct work_struct s_orphan_cleanup_work;
411 atomic_t s_orphan_cleanup_err; /* Error from last orphan cleanup */
412 };
413
414 static inline struct apfs_sb_info *APFS_SB(struct super_block *sb)
415 {
416 return sb->s_fs_info;
417 }
418
419 static inline bool apfs_is_sealed(struct super_block *sb)
420 {
421 u64 flags = le64_to_cpu(APFS_SB(sb)->s_vsb_raw->apfs_incompatible_features);
422
423 return flags & APFS_INCOMPAT_SEALED_VOLUME;
424 }
425
426 /**
427 * apfs_vol_is_encrypted - Check if a volume is encrypting files
428 * @sb: superblock
429 */
430 static inline bool apfs_vol_is_encrypted(struct super_block *sb)
431 {
432 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
433
434 return (vsb_raw->apfs_fs_flags & cpu_to_le64(APFS_FS_UNENCRYPTED)) == 0;
435 }
436
437 /**
438 * APFS_NXI - Get the shared container info for a volume's superblock
439 * @sb: superblock structure
440 */
441 static inline struct apfs_nxsb_info *APFS_NXI(struct super_block *sb)
442 {
443 return APFS_SB(sb)->s_nxi;
444 }
445
446 /**
447 * APFS_SM - Get the shared spaceman struct for a volume's superblock
448 * @sb: superblock structure
449 */
450 static inline struct apfs_spaceman *APFS_SM(struct super_block *sb)
451 {
452 return APFS_NXI(sb)->nx_spaceman;
453 }
454
455 static inline bool apfs_is_case_insensitive(struct super_block *sb)
456 {
457 return (APFS_SB(sb)->s_vsb_raw->apfs_incompatible_features &
458 cpu_to_le64(APFS_INCOMPAT_CASE_INSENSITIVE)) != 0;
459 }
460
461 static inline bool apfs_is_normalization_insensitive(struct super_block *sb)
462 {
463 struct apfs_sb_info *sbi = APFS_SB(sb);
464 u64 flags = le64_to_cpu(sbi->s_vsb_raw->apfs_incompatible_features);
465
466 if (apfs_is_case_insensitive(sb))
467 return true;
468 if (flags & APFS_INCOMPAT_NORMALIZATION_INSENSITIVE)
469 return true;
470 return false;
471 }
472
473 /**
474 * apfs_max_maps_per_block - Find the maximum map count for a mapping block
475 * @sb: superblock structure
476 */
477 static inline int apfs_max_maps_per_block(struct super_block *sb)
478 {
479 unsigned long maps_size;
480
481 maps_size = (sb->s_blocksize - sizeof(struct apfs_checkpoint_map_phys));
482 return maps_size / sizeof(struct apfs_checkpoint_mapping);
483 }
484
485 /*
486 * In-memory representation of a key, as relevant for a b-tree query.
487 */
488 struct apfs_key {
489 u64 id;
490 u64 number; /* Extent offset, name hash or transaction id */
491 const char *name; /* On-disk name string */
492 u8 type; /* Record type (0 for the omap) */
493 };
494
495 /**
496 * apfs_init_free_queue_key - Initialize an in-memory key for a free queue query
497 * @xid: transaction id
498 * @paddr: block number
499 * @key: apfs_key structure to initialize
500 */
501 static inline void apfs_init_free_queue_key(u64 xid, u64 paddr,
502 struct apfs_key *key)
503 {
504 key->id = xid;
505 key->type = 0;
506 key->number = paddr;
507 key->name = NULL;
508 }
509
510 /**
511 * apfs_init_omap_key - Initialize an in-memory key for an omap query
512 * @oid: object id
513 * @xid: latest transaction id
514 * @key: apfs_key structure to initialize
515 */
516 static inline void apfs_init_omap_key(u64 oid, u64 xid, struct apfs_key *key)
517 {
518 key->id = oid;
519 key->type = 0;
520 key->number = xid;
521 key->name = NULL;
522 }
523
524 /**
525 * apfs_init_extent_key - Initialize an in-memory key for an extentref query
526 * @bno: physical block number for the start of the extent
527 * @key: apfs_key structure to initialize
528 */
529 static inline void apfs_init_extent_key(u64 bno, struct apfs_key *key)
530 {
531 key->id = bno;
532 key->type = APFS_TYPE_EXTENT;
533 key->number = 0;
534 key->name = NULL;
535 }
536
537 /**
538 * apfs_init_inode_key - Initialize an in-memory key for an inode query
539 * @ino: inode number
540 * @key: apfs_key structure to initialize
541 */
542 static inline void apfs_init_inode_key(u64 ino, struct apfs_key *key)
543 {
544 key->id = ino;
545 key->type = APFS_TYPE_INODE;
546 key->number = 0;
547 key->name = NULL;
548 }
549
550 /**
551 * apfs_init_file_extent_key - Initialize an in-memory key for an extent query
552 * @id: extent id
553 * @offset: logical address (0 for a multiple query)
554 * @key: apfs_key structure to initialize
555 */
556 static inline void apfs_init_file_extent_key(u64 id, u64 offset,
557 struct apfs_key *key)
558 {
559 key->id = id;
560 key->type = APFS_TYPE_FILE_EXTENT;
561 key->number = offset;
562 key->name = NULL;
563 }
564
565 static inline void apfs_init_fext_key(u64 id, u64 offset, struct apfs_key *key)
566 {
567 key->id = id;
568 key->type = 0;
569 key->number = offset;
570 key->name = NULL;
571 }
572
573 /**
574 * apfs_init_dstream_id_key - Initialize an in-memory key for a dstream query
575 * @id: data stream id
576 * @key: apfs_key structure to initialize
577 */
578 static inline void apfs_init_dstream_id_key(u64 id, struct apfs_key *key)
579 {
580 key->id = id;
581 key->type = APFS_TYPE_DSTREAM_ID;
582 key->number = 0;
583 key->name = NULL;
584 }
585
586 /**
587 * apfs_init_crypto_state_key - Initialize an in-memory key for a crypto query
588 * @id: crypto state id
589 * @key: apfs_key structure to initialize
590 */
591 static inline void apfs_init_crypto_state_key(u64 id, struct apfs_key *key)
592 {
593 key->id = id;
594 key->type = APFS_TYPE_CRYPTO_STATE;
595 key->number = 0;
596 key->name = NULL;
597 }
598
599 /**
600 * apfs_init_sibling_link_key - Initialize an in-memory key for a sibling query
601 * @ino: inode number
602 * @id: sibling id
603 * @key: apfs_key structure to initialize
604 */
605 static inline void apfs_init_sibling_link_key(u64 ino, u64 id,
606 struct apfs_key *key)
607 {
608 key->id = ino;
609 key->type = APFS_TYPE_SIBLING_LINK;
610 key->number = id; /* Only guessing (TODO) */
611 key->name = NULL;
612 }
613
614 /**
615 * apfs_init_sibling_map_key - Initialize in-memory key for a sibling map query
616 * @id: sibling id
617 * @key: apfs_key structure to initialize
618 */
619 static inline void apfs_init_sibling_map_key(u64 id, struct apfs_key *key)
620 {
621 key->id = id;
622 key->type = APFS_TYPE_SIBLING_MAP;
623 key->number = 0;
624 key->name = NULL;
625 }
626
627 extern void apfs_init_drec_key(struct super_block *sb, u64 ino, const char *name,
628 unsigned int name_len, struct apfs_key *key);
629
630 /**
631 * apfs_init_xattr_key - Initialize an in-memory key for a xattr query
632 * @ino: inode number of the parent file
633 * @name: xattr name (NULL for a multiple query)
634 * @key: apfs_key structure to initialize
635 */
636 static inline void apfs_init_xattr_key(u64 ino, const char *name,
637 struct apfs_key *key)
638 {
639 key->id = ino;
640 key->type = APFS_TYPE_XATTR;
641 key->number = 0;
642 key->name = name;
643 }
644
645 static inline void apfs_init_snap_metadata_key(u64 xid, struct apfs_key *key)
646 {
647 key->id = xid;
648 key->type = APFS_TYPE_SNAP_METADATA;
649 key->number = 0;
650 key->name = NULL;
651 }
652
653 static inline void apfs_init_snap_name_key(const char *name, struct apfs_key *key)
654 {
655 key->id = APFS_SNAP_NAME_OBJ_ID;
656 key->type = APFS_TYPE_SNAP_NAME;
657 key->number = 0;
658 key->name = name;
659 }
660
661 static inline void apfs_init_omap_snap_key(u64 xid, struct apfs_key *key)
662 {
663 key->id = xid;
664 key->type = 0;
665 key->number = 0;
666 key->name = NULL;
667 }
668
669 /**
670 * apfs_key_set_hdr - Set the header for a raw catalog key
671 * @type: record type
672 * @id: record id
673 * @key: the key to initialize
674 */
675 static inline void apfs_key_set_hdr(u64 type, u64 id, void *key)
676 {
677 struct apfs_key_header *hdr = key;
678
679 hdr->obj_id_and_type = cpu_to_le64(id | type << APFS_OBJ_TYPE_SHIFT);
680 }
681
682 /**
683 * apfs_cat_type - Read the record type of a catalog key
684 * @key: the raw catalog key
685 */
686 static inline int apfs_cat_type(struct apfs_key_header *key)
687 {
688 return (le64_to_cpu(key->obj_id_and_type) & APFS_OBJ_TYPE_MASK) >> APFS_OBJ_TYPE_SHIFT;
689 }
690
691 /**
692 * apfs_cat_cnid - Read the cnid value on a catalog key
693 * @key: the raw catalog key
694 *
695 * TODO: rename this function, since it's not just for the catalog anymore
696 */
697 static inline u64 apfs_cat_cnid(struct apfs_key_header *key)
698 {
699 return le64_to_cpu(key->obj_id_and_type) & APFS_OBJ_ID_MASK;
700 }
701
702 /* Flags for the query structure */
703 #define APFS_QUERY_TREE_MASK 000177 /* Which b-tree we query */
704 #define APFS_QUERY_OMAP 000001 /* This is a b-tree object map query */
705 #define APFS_QUERY_CAT 000002 /* This is a catalog tree query */
706 #define APFS_QUERY_FREE_QUEUE 000004 /* This is a free queue query */
707 #define APFS_QUERY_EXTENTREF 000010 /* This is an extent reference query */
708 #define APFS_QUERY_FEXT 000020 /* This is a fext tree query */
709 #define APFS_QUERY_SNAP_META 000040 /* This is a snapshot meta query */
710 #define APFS_QUERY_OMAP_SNAP 000100 /* This is an omap snapshots query */
711 #define APFS_QUERY_NEXT 000200 /* Find next of multiple matches */
712 #define APFS_QUERY_EXACT 000400 /* Search for an exact match */
713 #define APFS_QUERY_DONE 001000 /* The search at this level is over */
714 #define APFS_QUERY_ANY_NAME 002000 /* Multiple search for any name */
715 #define APFS_QUERY_ANY_NUMBER 004000 /* Multiple search for any number */
716 #define APFS_QUERY_MULTIPLE (APFS_QUERY_ANY_NAME | APFS_QUERY_ANY_NUMBER)
717 #define APFS_QUERY_PREV 010000 /* Find previous record */
718
719 /*
720 * Structure used to retrieve data from an APFS B-Tree.
721 */
722 struct apfs_query {
723 struct apfs_node *node; /* Node being searched */
724 struct apfs_key key; /* What the query is looking for */
725
726 struct apfs_query *parent; /* Query for parent node */
727 unsigned int flags;
728
729 /* Set by the query on success */
730 int index; /* Index of the entry in the node */
731 int key_off; /* Offset of the key in the node */
732 int key_len; /* Length of the key */
733 int off; /* Offset of the value in the node */
734 int len; /* Length of the value */
735
736 int depth; /* Put a limit on recursion */
737 };
738
739 /**
740 * apfs_query_storage - Get the storage type for a query's btree
741 * @query: the query structure
742 */
743 static inline u32 apfs_query_storage(struct apfs_query *query)
744 {
745 if (query->flags & APFS_QUERY_OMAP)
746 return APFS_OBJ_PHYSICAL;
747 if (query->flags & APFS_QUERY_CAT)
748 return APFS_OBJ_VIRTUAL;
749 if (query->flags & APFS_QUERY_FEXT)
750 return APFS_OBJ_PHYSICAL;
751 if (query->flags & APFS_QUERY_FREE_QUEUE)
752 return APFS_OBJ_EPHEMERAL;
753 if (query->flags & APFS_QUERY_EXTENTREF)
754 return APFS_OBJ_PHYSICAL;
755 if (query->flags & APFS_QUERY_SNAP_META)
756 return APFS_OBJ_PHYSICAL;
757 if (query->flags & APFS_QUERY_OMAP_SNAP)
758 return APFS_OBJ_PHYSICAL;
759
760 /* Absurd, but don't panic: let the callers fail and report it */
761 return -1;
762 }
763
764 /*
765 * Extent record data in memory
766 */
767 struct apfs_file_extent {
768 u64 logical_addr;
769 u64 phys_block_num;
770 u64 len;
771 u64 crypto_id;
772 };
773
774 /*
775 * Physical extent record data in memory
776 */
777 struct apfs_phys_extent {
778 u64 bno;
779 u64 blkcount;
780 u64 len; /* In bytes */
781 u32 refcnt;
782 u8 kind;
783 };
784
785 /*
786 * Data stream info in memory
787 */
788 struct apfs_dstream_info {
789 struct super_block *ds_sb; /* Filesystem superblock */
790 struct inode *ds_inode; /* NULL for xattr dstreams */
791 u64 ds_id; /* ID of the extent records */
792 u64 ds_size; /* Length of the stream */
793 u64 ds_sparse_bytes;/* Hole byte count in stream */
794 struct apfs_file_extent ds_cached_ext; /* Latest extent record */
795 bool ds_ext_dirty; /* Is ds_cached_ext dirty? */
796 spinlock_t ds_ext_lock; /* Protects ds_cached_ext */
797 bool ds_shared; /* Has multiple references? */
798 };
799
800 /**
801 * apfs_alloced_size - Return the alloced size for a data stream
802 * @dstream: data stream info
803 *
804 * TODO: is this always correct? Or could the extents have an unused tail?
805 */
806 static inline u64 apfs_alloced_size(struct apfs_dstream_info *dstream)
807 {
808 struct super_block *sb = dstream->ds_sb;
809 u64 blks = (dstream->ds_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
810
811 return blks << sb->s_blocksize_bits;
812 }
813
814 /*
815 * APFS inode data in memory
816 */
817 struct apfs_inode_info {
818 u64 i_ino64; /* 32-bit-safe inode number */
819 u64 i_parent_id; /* ID of primary parent */
820 struct timespec64 i_crtime; /* Time of creation */
821 u32 i_nchildren; /* Child count for directory */
822 uid_t i_saved_uid; /* User ID on disk */
823 gid_t i_saved_gid; /* Group ID on disk */
824 u32 i_key_class; /* Security class for directory */
825 u64 i_int_flags; /* Internal flags */
826 u32 i_bsd_flags; /* BSD flags */
827 struct list_head i_list; /* List of inodes in transaction */
828
829 bool i_has_dstream; /* Is there a dstream record? */
830 struct apfs_dstream_info i_dstream; /* Dstream data, if any */
831
832 bool i_cleaned; /* Orphan data already deleted */
833
834 struct inode vfs_inode;
835 };
836
837 static inline struct apfs_inode_info *APFS_I(const struct inode *inode)
838 {
839 return container_of(inode, struct apfs_inode_info, vfs_inode);
840 }
841
842 /**
843 * apfs_ino - Get the 64-bit id of an inode
844 * @inode: the vfs inode
845 *
846 * Returns all 64 bits of @inode's id, even on 32-bit architectures.
847 */
848 static inline u64 apfs_ino(const struct inode *inode)
849 {
850 return APFS_I(inode)->i_ino64;
851 }
852
853 /**
854 * apfs_set_ino - Set a 64-bit id on an inode
855 * @inode: the vfs inode
856 * @id: id to set
857 *
858 * Sets both the vfs inode number and the actual 32-bit-safe id.
859 */
860 static inline void apfs_set_ino(struct inode *inode, u64 id)
861 {
862 inode->i_ino = id; /* Higher bits may be lost, but it doesn't matter */
863 APFS_I(inode)->i_ino64 = id;
864 }
865
866 /* Make the compiler complain if we ever access i_ino directly by mistake */
867 #define i_ino DONT_USE_I_INO
868
869 /*
870 * Directory entry record in memory
871 */
872 struct apfs_drec {
873 u8 *name;
874 u64 ino;
875 u64 sibling_id; /* The sibling id; 0 if none */
876 int name_len;
877 unsigned int type;
878 };
879
880 /*
881 * Xattr record data in memory
882 */
883 struct apfs_xattr {
884 u8 *name;
885 u8 *xdata;
886 int name_len;
887 int xdata_len;
888 bool has_dstream;
889 };
890
891 struct apfs_compressed_data {
892 bool has_dstream;
893 u64 size;
894 union {
895 struct apfs_dstream_info *dstream;
896 void *data;
897 };
898 };
899
900 /*
901 * Report function name and line number for the message types that are likely
902 * to signal a bug, to make things easier for reporters. Don't do this for the
903 * common messages, there is no point and it makes the console look too busy.
904 */
905 #define apfs_emerg(sb, fmt, ...) apfs_msg(sb, KERN_EMERG, __func__, __LINE__, fmt, ##__VA_ARGS__)
906 #define apfs_alert(sb, fmt, ...) apfs_msg(sb, KERN_ALERT, __func__, __LINE__, fmt, ##__VA_ARGS__)
907 #define apfs_crit(sb, fmt, ...) apfs_msg(sb, KERN_CRIT, __func__, __LINE__, fmt, ##__VA_ARGS__)
908 #define apfs_err(sb, fmt, ...) apfs_msg(sb, KERN_ERR, __func__, __LINE__, fmt, ##__VA_ARGS__)
909 #define apfs_warn(sb, fmt, ...) apfs_msg(sb, KERN_WARNING, NULL, 0, fmt, ##__VA_ARGS__)
910 #define apfs_notice(sb, fmt, ...) apfs_msg(sb, KERN_NOTICE, NULL, 0, fmt, ##__VA_ARGS__)
911 #define apfs_info(sb, fmt, ...) apfs_msg(sb, KERN_INFO, NULL, 0, fmt, ##__VA_ARGS__)
912
913 #ifdef CONFIG_APFS_DEBUG
914 #define ASSERT(expr) WARN_ON(!(expr))
915 #define apfs_debug(sb, fmt, ...) apfs_msg(sb, KERN_DEBUG, __func__, __LINE__, fmt, ##__VA_ARGS__)
916 #else
917 #define ASSERT(expr) ((void)0)
918 #define apfs_debug(sb, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
919 #endif /* CONFIG_APFS_DEBUG */
920
921 /**
922 * apfs_assert_in_transaction - Assert that the object is in current transaction
923 * @sb: superblock structure
924 * @obj: on-disk object to check
925 */
926 #define apfs_assert_in_transaction(sb, obj) \
927 do { \
928 (void)sb; \
929 (void)obj; \
930 ASSERT(le64_to_cpu((obj)->o_xid) == APFS_NXI(sb)->nx_xid); \
931 } while (0)
932
933 /* btree.c */
934 extern struct apfs_node *apfs_query_root(const struct apfs_query *query);
935 extern struct apfs_query *apfs_alloc_query(struct apfs_node *node,
936 struct apfs_query *parent);
937 extern void apfs_free_query(struct apfs_query *query);
938 extern int apfs_btree_query(struct super_block *sb, struct apfs_query **query);
939 extern int apfs_omap_lookup_block(struct super_block *sb, struct apfs_omap *omap, u64 id, u64 *block, bool write);
940 extern int apfs_omap_lookup_newest_block(struct super_block *sb, struct apfs_omap *omap, u64 id, u64 *block, bool write);
941 extern int apfs_create_omap_rec(struct super_block *sb, u64 oid, u64 bno);
942 extern int apfs_delete_omap_rec(struct super_block *sb, u64 oid);
943 extern int apfs_query_join_transaction(struct apfs_query *query);
944 extern int __apfs_btree_insert(struct apfs_query *query, void *key, int key_len, void *val, int val_len);
945 extern int apfs_btree_insert(struct apfs_query *query, void *key, int key_len,
946 void *val, int val_len);
947 extern int apfs_btree_remove(struct apfs_query *query);
948 extern void apfs_btree_change_node_count(struct apfs_query *query, int change);
949 extern int apfs_btree_replace(struct apfs_query *query, void *key, int key_len,
950 void *val, int val_len);
951 extern void apfs_query_direct_forward(struct apfs_query *query);
952
953 /* compress.c */
954 extern int apfs_compress_get_size(struct inode *inode, loff_t *size);
955
956 /* dir.c */
957 extern int apfs_inode_by_name(struct inode *dir, const struct qstr *child,
958 u64 *ino);
959 extern int apfs_mkany(struct inode *dir, struct dentry *dentry,
960 umode_t mode, dev_t rdev, const char *symname);
961
962 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
963 extern int apfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
964 dev_t rdev);
965 extern int apfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
966 extern int apfs_rename(struct inode *old_dir, struct dentry *old_dentry,
967 struct inode *new_dir, struct dentry *new_dentry,
968 unsigned int flags);
969 extern int apfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
970 bool excl);
971 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
972 extern int apfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
973 struct dentry *dentry, umode_t mode, dev_t rdev);
974 extern int apfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
975 struct dentry *dentry, umode_t mode);
976 extern int apfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
977 struct dentry *old_dentry, struct inode *new_dir,
978 struct dentry *new_dentry, unsigned int flags);
979 extern int apfs_create(struct user_namespace *mnt_userns, struct inode *dir,
980 struct dentry *dentry, umode_t mode, bool excl);
981 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 15, 0)
982 extern int apfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
983 struct dentry *dentry, umode_t mode, dev_t rdev);
984 extern int apfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
985 struct dentry *dentry, umode_t mode);
986 extern int apfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
987 struct dentry *old_dentry, struct inode *new_dir,
988 struct dentry *new_dentry, unsigned int flags);
989 extern int apfs_create(struct mnt_idmap *idmap, struct inode *dir,
990 struct dentry *dentry, umode_t mode, bool excl);
991 #else
992 extern int apfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
993 struct dentry *dentry, umode_t mode, dev_t rdev);
994 extern struct dentry *apfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, umode_t mode);
995 extern int apfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
996 struct dentry *old_dentry, struct inode *new_dir,
997 struct dentry *new_dentry, unsigned int flags);
998 extern int apfs_create(struct mnt_idmap *idmap, struct inode *dir,
999 struct dentry *dentry, umode_t mode, bool excl);
1000 #endif
1001
1002 extern int apfs_link(struct dentry *old_dentry, struct inode *dir,
1003 struct dentry *dentry);
1004 extern int apfs_unlink(struct inode *dir, struct dentry *dentry);
1005 extern int apfs_rmdir(struct inode *dir, struct dentry *dentry);
1006 extern int apfs_delete_orphan_link(struct inode *inode);
1007 extern u64 apfs_any_orphan_ino(struct super_block *sb, u64 *ino_p);
1008
1009 /* extents.c */
1010 extern int apfs_extent_from_query(struct apfs_query *query,
1011 struct apfs_file_extent *extent);
1012 extern int apfs_logic_to_phys_bno(struct apfs_dstream_info *dstream, sector_t dsblock, u64 *bno);
1013 extern int __apfs_get_block(struct apfs_dstream_info *dstream, sector_t iblock,
1014 struct buffer_head *bh_result, int create);
1015 extern int apfs_get_block(struct inode *inode, sector_t iblock,
1016 struct buffer_head *bh_result, int create);
1017 extern int apfs_flush_extent_cache(struct apfs_dstream_info *dstream);
1018 extern int apfs_dstream_get_new_bno(struct apfs_dstream_info *dstream, u64 dsblock, u64 *bno);
1019 extern int apfs_get_new_block(struct inode *inode, sector_t iblock,
1020 struct buffer_head *bh_result, int create);
1021 extern int apfs_truncate(struct apfs_dstream_info *dstream, loff_t new_size);
1022 extern int apfs_inode_delete_front(struct inode *inode);
1023 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
1024 extern loff_t apfs_remap_file_range(struct file *src_file, loff_t off, struct file *dst_file, loff_t destoff, loff_t len, unsigned int remap_flags);
1025 #else
1026 extern int apfs_clone_file_range(struct file *src_file, loff_t off, struct file *dst_file, loff_t destoff, u64 len);
1027 #endif
1028 extern int apfs_clone_extents(struct apfs_dstream_info *dstream, u64 new_id);
1029 extern int apfs_nonsparse_dstream_read(struct apfs_dstream_info *dstream, void *buf, size_t count, u64 offset);
1030 extern void apfs_nonsparse_dstream_preread(struct apfs_dstream_info *dstream);
1031
1032 /* file.c */
1033 extern int apfs_file_mmap(struct file *file, struct vm_area_struct *vma);
1034 extern int apfs_fsync(struct file *file, loff_t start, loff_t end, int datasync);
1035
1036 /* inode.c */
1037 extern struct inode *apfs_iget(struct super_block *sb, u64 cnid);
1038 extern int apfs_update_inode(struct inode *inode, char *new_name);
1039 extern void apfs_schedule_orphan_cleanup(struct super_block *sb);
1040 extern void apfs_orphan_cleanup_work(struct work_struct *work);
1041 extern void apfs_evict_inode(struct inode *inode);
1042 extern struct inode *apfs_new_inode(struct inode *dir, umode_t mode,
1043 dev_t rdev);
1044 extern int apfs_create_inode_rec(struct super_block *sb, struct inode *inode,
1045 struct dentry *dentry);
1046 extern int apfs_inode_create_exclusive_dstream(struct inode *inode);
1047 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
1048 extern int __apfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int flags, struct page **pagep, void **fsdata);
1049 extern int __apfs_write_end(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int copied, struct page *page, void *fsdata);
1050 #else
1051 extern int __apfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int flags, struct page **pagep, void **fsdata);
1052 extern int __apfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int copied, struct page *page, void *fsdata);
1053 #endif
1054 extern int apfs_dstream_adj_refcnt(struct apfs_dstream_info *dstream, u32 delta);
1055
1056 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1057 extern int apfs_setattr(struct dentry *dentry, struct iattr *iattr);
1058 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1059 extern int apfs_setattr(struct user_namespace *mnt_userns,
1060 struct dentry *dentry, struct iattr *iattr);
1061 #else
1062 extern int apfs_setattr(struct mnt_idmap *idmap,
1063 struct dentry *dentry, struct iattr *iattr);
1064 #endif
1065
1066 #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
1067 extern int apfs_update_time(struct inode *inode, enum fs_update_time time, unsigned int flags);
1068 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1069 extern int apfs_update_time(struct inode *inode, struct timespec64 *time, int flags);
1070 #else
1071 extern int apfs_update_time(struct inode *inode, int flags);
1072 #endif
1073 long apfs_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1074 long apfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1075
1076 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) /* No statx yet... */
1077 extern int apfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1078 struct kstat *stat);
1079 #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1080 extern int apfs_getattr(const struct path *path, struct kstat *stat,
1081 u32 request_mask, unsigned int query_flags);
1082 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1083 extern int apfs_getattr(struct user_namespace *mnt_userns,
1084 const struct path *path, struct kstat *stat, u32 request_mask,
1085 unsigned int query_flags);
1086 #else
1087 extern int apfs_getattr(struct mnt_idmap *idmap,
1088 const struct path *path, struct kstat *stat, u32 request_mask,
1089 unsigned int query_flags);
1090 #endif
1091
1092 extern int apfs_crypto_adj_refcnt(struct super_block *sb, u64 crypto_id, int delta);
1093
1094 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
1095 #define fileattr file_kattr
1096 #endif
1097
1098 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) || RHEL_VERSION_GE(9, 6)
1099 extern int apfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1100 extern int apfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry, struct fileattr *fa);
1101 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 13, 0)
1102 extern int apfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1103 extern int apfs_fileattr_set(struct user_namespace *mnt_userns, struct dentry *dentry, struct fileattr *fa);
1104 #endif
1105
1106 /* key.c */
1107 extern int apfs_filename_cmp(struct super_block *sb, const char *name1, unsigned int len1, const char *name2, unsigned int len2);
1108 extern int apfs_keycmp(struct apfs_key *k1, struct apfs_key *k2);
1109 extern int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed);
1110 extern int apfs_read_fext_key(void *raw, int size, struct apfs_key *key);
1111 extern int apfs_read_free_queue_key(void *raw, int size, struct apfs_key *key);
1112 extern int apfs_read_omap_key(void *raw, int size, struct apfs_key *key);
1113 extern int apfs_read_extentref_key(void *raw, int size, struct apfs_key *key);
1114 extern int apfs_read_snap_meta_key(void *raw, int size, struct apfs_key *key);
1115 extern int apfs_read_omap_snap_key(void *raw, int size, struct apfs_key *key);
1116
1117 /* message.c */
1118 extern __printf(5, 6) void apfs_msg(struct super_block *sb, const char *prefix, const char *func, int line, const char *fmt, ...);
1119
1120 /* node.c */
1121 extern struct apfs_node *apfs_read_node(struct super_block *sb, u64 oid,
1122 u32 storage, bool write);
1123 extern void apfs_update_node(struct apfs_node *node);
1124 extern int apfs_delete_node(struct apfs_node *node, int type);
1125 extern int apfs_node_query(struct super_block *sb, struct apfs_query *query);
1126 extern void apfs_node_query_first(struct apfs_query *query);
1127 extern int apfs_omap_map_from_query(struct apfs_query *query, struct apfs_omap_map *map);
1128 extern int apfs_node_split(struct apfs_query *query);
1129 extern int apfs_node_locate_key(struct apfs_node *node, int index, int *off);
1130 extern void apfs_node_free(struct apfs_node *node);
1131 extern void apfs_node_free_range(struct apfs_node *node, u16 off, u16 len);
1132 extern bool apfs_node_has_room(struct apfs_node *node, int length, bool replace);
1133 extern int apfs_node_replace(struct apfs_query *query, void *key, int key_len, void *val, int val_len);
1134 extern int apfs_node_insert(struct apfs_query *query, void *key, int key_len, void *val, int val_len);
1135 extern int apfs_create_single_rec_node(struct apfs_query *query, void *key, int key_len, void *val, int val_len);
1136 extern int apfs_make_empty_btree_root(struct super_block *sb, u32 subtype, u64 *oid);
1137
1138 /* object.c */
1139 extern int apfs_obj_verify_csum(struct super_block *sb, struct buffer_head *bh);
1140 extern void apfs_obj_set_csum(struct super_block *sb, struct apfs_obj_phys *obj);
1141 extern int apfs_multiblock_verify_csum(char *object, u32 size);
1142 extern void apfs_multiblock_set_csum(char *object, u32 size);
1143 extern int apfs_create_cpm_block(struct super_block *sb, u64 bno, struct buffer_head **bh_p);
1144 extern int apfs_create_cpoint_map(struct super_block *sb, struct apfs_checkpoint_map_phys *cpm, struct apfs_obj_phys *obj, u64 bno, u32 size);
1145 extern struct apfs_ephemeral_object_info *apfs_ephemeral_object_lookup(struct super_block *sb, u64 oid);
1146 extern struct buffer_head *apfs_read_object_block(struct super_block *sb, u64 bno, bool write, bool preserve);
1147 extern u32 apfs_index_in_data_area(struct super_block *sb, u64 bno);
1148 extern u64 apfs_data_index_to_bno(struct super_block *sb, u32 index);
1149
1150 /* snapshot.c */
1151 extern int apfs_ioc_take_snapshot(struct file *file, void __user *user_arg);
1152 extern int apfs_switch_to_snapshot(struct super_block *sb);
1153
1154 /* spaceman.c */
1155 extern int apfs_read_spaceman(struct super_block *sb);
1156 extern int apfs_free_queue_insert_nocache(struct super_block *sb, u64 bno, u64 count);
1157 extern int apfs_free_queue_insert(struct super_block *sb, u64 bno, u64 count);
1158 extern int apfs_spaceman_allocate_block(struct super_block *sb, u64 *bno, bool backwards);
1159 extern int apfs_write_ip_bitmaps(struct super_block *sb);
1160 extern int apfs_spaceman_get_free_blkcnt(struct super_block *sb, u64 *blkcnt);
1161
1162 /* super.c */
1163 extern int apfs_map_volume_super_bno(struct super_block *sb, u64 bno, bool check);
1164 extern int apfs_map_volume_super(struct super_block *sb, bool write);
1165 extern void apfs_unmap_volume_super(struct super_block *sb);
1166 extern int apfs_read_omap(struct super_block *sb, bool write);
1167 extern int apfs_read_catalog(struct super_block *sb, bool write);
1168 extern int apfs_sync_fs(struct super_block *sb, int wait);
1169
1170 /* transaction.c */
1171 extern int apfs_cpoint_data_free(struct super_block *sb, u64 bno);
1172 extern void apfs_transaction_init(struct apfs_nx_transaction *trans);
1173 extern int apfs_transaction_start(struct super_block *sb, enum apfs_trans_kind kind);
1174 extern int apfs_transaction_commit(struct super_block *sb);
1175 extern void apfs_inode_join_transaction(struct super_block *sb, struct inode *inode);
1176 extern int apfs_transaction_join(struct super_block *sb,
1177 struct buffer_head *bh);
1178 void apfs_transaction_abort(struct super_block *sb);
1179 extern int apfs_transaction_flush_all_inodes(struct super_block *sb);
1180 extern int apfs_read_ephemeral_objects(struct super_block *sb);
1181
1182 /* xattr.c */
1183 extern int ____apfs_xattr_get(struct inode *inode, const char *name, void *buffer,
1184 size_t size, bool only_whole);
1185 extern int __apfs_xattr_get(struct inode *inode, const char *name, void *buffer,
1186 size_t size);
1187 extern int apfs_delete_all_xattrs(struct inode *inode);
1188 extern int apfs_xattr_set(struct inode *inode, const char *name, const void *value,
1189 size_t size, int flags);
1190 extern ssize_t apfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
1191 extern int apfs_xattr_get_compressed_data(struct inode *inode, const char *name, struct apfs_compressed_data *cdata);
1192 extern void apfs_release_compressed_data(struct apfs_compressed_data *cdata);
1193 extern int apfs_compressed_data_read(struct apfs_compressed_data *cdata, void *buf, size_t count, u64 offset);
1194
1195 /* xfield.c */
1196 extern int apfs_find_xfield(u8 *xfields, int len, u8 xtype, char **xval);
1197 extern int apfs_init_xfields(u8 *buffer, int buflen);
1198 extern int apfs_insert_xfield(u8 *buffer, int buflen,
1199 const struct apfs_x_field *xkey,
1200 const void *xval);
1201
1202 /*
1203 * Inode and file operations
1204 */
1205
1206 /* compress.c */
1207 extern const struct address_space_operations apfs_compress_aops;
1208 extern const struct file_operations apfs_compress_file_operations;
1209
1210 /* dir.c */
1211 extern const struct file_operations apfs_dir_operations;
1212
1213 /* file.c */
1214 extern const struct file_operations apfs_file_operations;
1215 extern const struct inode_operations apfs_file_inode_operations;
1216
1217 /* namei.c */
1218 extern const struct inode_operations apfs_dir_inode_operations;
1219 extern const struct inode_operations apfs_special_inode_operations;
1220 extern const struct dentry_operations apfs_dentry_operations;
1221
1222 /* symlink.c */
1223 extern const struct inode_operations apfs_symlink_inode_operations;
1224
1225 /* xattr.c */
1226 extern const struct xattr_handler *apfs_xattr_handlers[];
1227
1228 /**
1229 * apfs_assert_query_is_valid - Assert that all of a query's ancestors are set
1230 * @query: the query to check
1231 *
1232 * A query may lose some of its ancestors during a node split, but nothing
1233 * should be done to such a query until it gets refreshed.
1234 */
1235 static inline void apfs_assert_query_is_valid(const struct apfs_query *query)
1236 {
1237 ASSERT(apfs_node_is_root(apfs_query_root(query)));
1238 }
1239
1240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
1241 #define page_has_buffers(page) folio_buffers(page_folio(page))
1242 #endif
1243
1244 static inline int apfs_inode_state_read_once(struct inode *inode)
1245 {
1246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
1247 return inode_state_read_once(inode);
1248 #else
1249 return inode->i_state;
1250 #endif
1251 }
1252
1253 static inline void apfs_inode_state_set_raw(struct inode *inode, int flags)
1254 {
1255 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
1256 inode_state_set_raw(inode, flags);
1257 #else
1258 inode->i_state |= flags;
1259 #endif
1260 }
1261
1262 static inline void apfs_inode_state_clear_raw(struct inode *inode, int flags)
1263 {
1264 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
1265 inode_state_clear_raw(inode, flags);
1266 #else
1267 inode->i_state &= ~flags;
1268 #endif
1269 }
1270
1271 /*
1272 * TODO: the following are modified variants of buffer head functions that will
1273 * work with the shared block device for the container. The correct approach
1274 * here would be to avoid buffer heads and use bios, but for now this will do.
1275 */
1276
1277 static inline void
1278 apfs_map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
1279 {
1280 struct apfs_nxsb_info *nxi = NULL;
1281 struct apfs_blkdev_info *info = NULL;
1282
1283 nxi = APFS_NXI(sb);
1284 info = nxi->nx_blkdev_info;
1285
1286 /*
1287 * Block numbers above s_tier2_bno are for the tier 2 device of a
1288 * fusion drive. Don't bother returning an error code if a regular
1289 * drive gets corrupted and reports a tier 2 block number: just treat
1290 * it the same as any other out-of-range block.
1291 */
1292 if (block >= nxi->nx_tier2_bno) {
1293 if (nxi->nx_tier2_info) {
1294 info = nxi->nx_tier2_info;
1295 block -= nxi->nx_tier2_bno;
1296 } else {
1297 apfs_err(sb, "block number in tier 2 range (0x%llx)", (unsigned long long)block);
1298 }
1299 }
1300
1301 set_buffer_mapped(bh);
1302 bh->b_bdev = info->blki_bdev;
1303 bh->b_blocknr = block;
1304 bh->b_size = sb->s_blocksize;
1305 }
1306
1307 static inline struct buffer_head *
1308 apfs_sb_bread(struct super_block *sb, sector_t block)
1309 {
1310 struct apfs_nxsb_info *nxi = NULL;
1311 struct apfs_blkdev_info *info = NULL;
1312
1313 nxi = APFS_NXI(sb);
1314 info = nxi->nx_blkdev_info;
1315
1316 if (block >= nxi->nx_tier2_bno) {
1317 if (!nxi->nx_tier2_info) {
1318 /* Not really a fusion drive, so it's corrupted */
1319 apfs_err(sb, "block number in tier 2 range (0x%llx)", (unsigned long long)block);
1320 return NULL;
1321 }
1322 info = nxi->nx_tier2_info;
1323 block -= nxi->nx_tier2_bno;
1324 }
1325
1326 return __bread_gfp(info->blki_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
1327 }
1328
1329 /* Like apfs_getblk(), but doesn't mark the buffer uptodate */
1330 static inline struct buffer_head *
1331 __apfs_getblk(struct super_block *sb, sector_t block)
1332 {
1333 struct apfs_nxsb_info *nxi = NULL;
1334 struct apfs_blkdev_info *info = NULL;
1335
1336 nxi = APFS_NXI(sb);
1337 info = nxi->nx_blkdev_info;
1338
1339 if (block >= nxi->nx_tier2_bno) {
1340 if (!nxi->nx_tier2_info) {
1341 /* Not really a fusion drive, so it's corrupted */
1342 apfs_err(sb, "block number in tier 2 range (0x%llx)", (unsigned long long)block);
1343 return NULL;
1344 }
1345 info = nxi->nx_tier2_info;
1346 block -= nxi->nx_tier2_bno;
1347 }
1348
1349 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1350 return __getblk_gfp(info->blki_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
1351 #else
1352 return bdev_getblk(info->blki_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
1353 #endif
1354 }
1355
1356 /* Use instead of apfs_sb_bread() for blocks that will just be overwritten */
1357 static inline struct buffer_head *
1358 apfs_getblk(struct super_block *sb, sector_t block)
1359 {
1360 struct buffer_head *bh = NULL;
1361
1362 bh = __apfs_getblk(sb, block);
1363 if (bh)
1364 set_buffer_uptodate(bh);
1365 return bh;
1366 }
1367
1368 #endif /* _APFS_H */