-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctree.h
6862 lines (5995 loc) · 203 KB
/
ctree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2007 Oracle. All rights reserved.
*/
#ifndef APFS_CTREE_H
#define APFS_CTREE_H
#include <linux/mm.h>
#include <linux/sched/signal.h>
#include <linux/highmem.h>
#include <linux/fs.h>
#include <linux/rwsem.h>
#include <linux/semaphore.h>
#include <linux/completion.h>
#include <linux/backing-dev.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#include <linux/pagemap.h>
#include "apfs.h"
#include "apfs_tree.h"
#include <linux/workqueue.h>
#include <linux/security.h>
#include <linux/sizes.h>
#include <linux/dynamic_debug.h>
#include <linux/refcount.h>
#include <linux/crc32c.h>
#include <linux/iomap.h>
#include "extent-io-tree.h"
#include "extent_io.h"
#include "extent_map.h"
#include "async-thread.h"
#include "block-rsv.h"
#include "locking.h"
#include "linux/ftrace.h"
struct apfs_trans_handle;
struct apfs_transaction;
struct apfs_pending_snapshot;
struct apfs_delayed_ref_root;
struct apfs_delayed_ref_head;
struct apfs_space_info;
struct apfs_block_group;
extern struct kmem_cache *apfs_trans_handle_cachep;
extern struct kmem_cache *apfs_bit_radix_cachep;
extern struct kmem_cache *apfs_path_cachep;
extern struct kmem_cache *apfs_free_space_cachep;
extern struct kmem_cache *apfs_free_space_bitmap_cachep;
struct apfs_ordered_sum;
struct apfs_ref;
struct apfs_obj_header;
#define APFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
#define APFS_SUPER_MAGIC 0x42535041
#define APFS_TEST_MAGIC 0x73727279
/*
* Block and Container Sizes
*/
#define APFS_MINIMUM_BLOCK_SIZE 4096
#define APFS_DEFAULT_BLOCK_SIZE 4096
#define APFS_MAXIMUM_BLOCK_SIZE 65536
#define APFS_MAX_METADATA_BLOCKSIZE 65536
#define APFS_MINIMUM_CONTAINER_SIZE 1048576
#define APFS_SUPER_INFO_SIZE 4096
#define APFS_FIXED_KEY_SIZE (sizeof(u64) << 1) // id + offset
#define APFS_FIXED_VAL_SIZE sizeof(u64) // node ptr
struct apfs_map_token {
struct extent_buffer *eb;
char *kaddr;
unsigned long offset;
};
#define APFS_BYTES_TO_BLKS(fs_info, bytes) \
((bytes) >> (fs_info)->sectorsize_bits)
static inline void apfs_init_map_token(struct apfs_map_token *token,
struct extent_buffer *eb)
{
token->eb = eb;
token->kaddr = page_address(eb->pages[0]);
token->offset = 0;
}
/* some macros to generate set/get functions for the struct fields. This
* assumes there is a lefoo_to_cpu for every type, so lets make a simple
* one for u8:
*/
#define le8_to_cpu(v) (v)
#define cpu_to_le8(v) (v)
#define __le8 u8
static inline u8 get_unaligned_le8(const void *p)
{
return *(u8 *)p;
}
static inline void put_unaligned_le8(u8 val, void *p)
{
*(u8 *)p = val;
}
#define read_eb_member(eb, ptr, type, member, result) (\
read_extent_buffer(eb, (char *)(result), \
((unsigned long)(ptr)) + \
offsetof(type, member), \
sizeof(((type *)0)->member)))
#define write_eb_member(eb, ptr, type, member, result) (\
write_extent_buffer(eb, (char *)(result), \
((unsigned long)(ptr)) + \
offsetof(type, member), \
sizeof(((type *)0)->member)))
#define DECLARE_APFS_SETGET_BITS(bits) \
u##bits apfs_get_token_##bits(struct apfs_map_token *token, \
const void *ptr, unsigned long off); \
void apfs_set_token_##bits(struct apfs_map_token *token, \
const void *ptr, unsigned long off, \
u##bits val); \
u##bits apfs_get_##bits(const struct extent_buffer *eb, \
const void *ptr, unsigned long off); \
void apfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
unsigned long off, u##bits val);
DECLARE_APFS_SETGET_BITS(8)
DECLARE_APFS_SETGET_BITS(16)
DECLARE_APFS_SETGET_BITS(32)
DECLARE_APFS_SETGET_BITS(64)
#define APFS_SETGET_TOKEN_FUNCS(name, type, member, bits) \
static inline u##bits apfs_token_##name(struct apfs_map_token *token, \
const type *s) \
{ \
BUG(); \
} \
\
static inline void apfs_set_token_##name(struct apfs_map_token *token,\
type *s, u##bits val) \
{ \
BUG(); \
}
#define APFS_SETGET_FUNCS(name, type, member, bits) \
static inline u##bits apfs_##name(const struct extent_buffer *eb, \
const type *s) \
{ \
BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
return apfs_get_##bits(eb, s, offsetof(type, member)); \
} \
static inline void apfs_set_##name(const struct extent_buffer *eb, type *s, \
u##bits val) \
{ \
BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
apfs_set_##bits(eb, s, offsetof(type, member), val); \
} \
static inline u##bits apfs_token_##name(struct apfs_map_token *token, \
const type *s) \
{ \
BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
return apfs_get_token_##bits(token, s, offsetof(type, member));\
} \
static inline void apfs_set_token_##name(struct apfs_map_token *token,\
type *s, u##bits val) \
{ \
BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
apfs_set_token_##bits(token, s, offsetof(type, member), val); \
}
#define APFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
static inline u##bits apfs_##name(const struct extent_buffer *eb) \
{ \
const type *p = page_address(eb->pages[0]) + \
offset_in_page(eb->start); \
return get_unaligned_le##bits(&p->member); \
} \
static inline void apfs_set_##name(const struct extent_buffer *eb, \
u##bits val) \
{ \
type *p = page_address(eb->pages[0]) + offset_in_page(eb->start); \
put_unaligned_le##bits(val, &p->member); \
}
#define APFS_SETGET_STACK_FUNCS(name, type, member, bits) \
static inline u##bits apfs_##name(const type *s) \
{ \
return get_unaligned_le##bits(&s->member); \
} \
static inline void apfs_set_##name(type *s, u##bits val) \
{ \
put_unaligned_le##bits(val, &s->member); \
}
#define APFS_SETGET_OBJ_FUNCS(name, stype) \
static inline u64 apfs_##name##_oid(const stype *s) \
{ \
return get_unaligned_le64(&((const struct apfs_obj_header *)s)->oid); \
} \
static inline void apfs_set_##name##_oid(stype *s, u64 val) \
{ \
put_unaligned_le64(val, &((struct apfs_obj_header *)s)->oid); \
} \
static inline u64 apfs_##name##_xid(const stype *s) \
{ \
return get_unaligned_le64(&s->o.xid); \
} \
static inline void apfs_set_##name##_xid(stype *s, u64 val) \
{ \
put_unaligned_le64(val, &((struct apfs_obj_header *)s)->xid); \
} \
static inline u32 apfs_##name##_type(const stype *s) \
{ \
return get_unaligned_le64(&((const struct apfs_obj_header *)s)->type); \
} \
static inline void apfs_set_##name##_type(stype *s, u32 val) \
{ \
put_unaligned_le64(val, &((struct apfs_obj_header *)s)->type); \
} \
static inline u32 apfs_##name##_subtype(const stype *s) \
{ \
return get_unaligned_le64(&((const struct apfs_obj_header *)s)->subtype); \
} \
static inline void apfs_set_##name##_subtype(stype *s, u32 val) \
{ \
put_unaligned_le64(val, &((struct apfs_obj_header *)s)->subtype); \
}
static inline __printf(2, 3) __cold
void apfs_no_printk(const struct apfs_fs_info *fs_info, const char *fmt, ...)
{
}
#ifdef CONFIG_PRINTK
__printf(2, 3)
__cold
void apfs_printk(const struct apfs_fs_info *fs_info, const char *fmt, ...);
#else
#define apfs_printk(fs_info, fmt, args...) \
apfs_no_printk(fs_info, fmt, ##args)
#endif
#define apfs_emerg(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_EMERG fmt, ##args)
#define apfs_alert(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_ALERT fmt, ##args)
#define apfs_crit(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_CRIT fmt, ##args)
#define apfs_err(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_ERR fmt, ##args)
#define apfs_warn(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_WARNING fmt, ##args)
#define apfs_notice(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_NOTICE fmt, ##args)
#define apfs_info(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_INFO fmt, ##args)
/*
* Wrappers that use printk_in_rcu
*/
#define apfs_emerg_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
#define apfs_alert_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
#define apfs_crit_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
#define apfs_err_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
#define apfs_warn_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
#define apfs_notice_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
#define apfs_info_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
/*
* Wrappers that use a ratelimited printk_in_rcu
*/
#define apfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
#define apfs_alert_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
#define apfs_crit_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
#define apfs_err_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
#define apfs_warn_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
#define apfs_notice_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
#define apfs_info_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
/*
* Wrappers that use a ratelimited printk
*/
#define apfs_emerg_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
#define apfs_alert_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
#define apfs_crit_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
#define apfs_err_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
#define apfs_warn_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
#define apfs_notice_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
#define apfs_info_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
#if defined(CONFIG_DYNAMIC_DEBUG)
#define apfs_debug(fs_info, fmt, args...) \
_dynamic_func_call_no_desc(fmt, apfs_printk, \
fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_in_rcu(fs_info, fmt, args...) \
_dynamic_func_call_no_desc(fmt, apfs_printk_in_rcu, \
fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl_in_rcu(fs_info, fmt, args...) \
_dynamic_func_call_no_desc(fmt, apfs_printk_rl_in_rcu, \
fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl(fs_info, fmt, args...) \
_dynamic_func_call_no_desc(fmt, apfs_printk_ratelimited, \
fs_info, KERN_DEBUG fmt, ##args)
#elif defined(DEBUG)
#define apfs_debug(fs_info, fmt, args...) \
apfs_printk(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_in_rcu(fs_info, fmt, args...) \
apfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl_in_rcu(fs_info, fmt, args...) \
apfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl(fs_info, fmt, args...) \
apfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
#else
#define apfs_debug(fs_info, fmt, args...) \
apfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_in_rcu(fs_info, fmt, args...) \
apfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl_in_rcu(fs_info, fmt, args...) \
apfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
#define apfs_debug_rl(fs_info, fmt, args...) \
apfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
#endif
#define apfs_printk_in_rcu(fs_info, fmt, args...) \
do { \
rcu_read_lock(); \
apfs_printk(fs_info, fmt, ##args); \
rcu_read_unlock(); \
} while (0)
#define apfs_no_printk_in_rcu(fs_info, fmt, args...) \
do { \
rcu_read_lock(); \
apfs_no_printk(fs_info, fmt, ##args); \
rcu_read_unlock(); \
} while (0)
#define apfs_printk_ratelimited(fs_info, fmt, args...) \
do { \
static DEFINE_RATELIMIT_STATE(_rs, \
DEFAULT_RATELIMIT_INTERVAL, \
DEFAULT_RATELIMIT_BURST); \
if (__ratelimit(&_rs)) \
apfs_printk(fs_info, fmt, ##args); \
} while (0)
#define apfs_printk_rl_in_rcu(fs_info, fmt, args...) \
do { \
rcu_read_lock(); \
apfs_printk_ratelimited(fs_info, fmt, ##args); \
rcu_read_unlock(); \
} while (0)
#ifdef CONFIG_APFS_ASSERT
__cold __noreturn
static inline void assertfail(const char *expr, const char *file, int line)
{
pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
BUG();
}
#define ASSERT(expr) \
(likely(expr) ? (void)0 : assertfail(#expr, __FILE__, __LINE__))
#else
static inline void assertfail(const char *expr, const char* file, int line) { }
#define ASSERT(expr) (void)(expr)
#endif
#if BITS_PER_LONG == 32
#define APFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
/*
* The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
* addresses of extents.
*
* For 4K page size it's about 10T, for 64K it's 160T.
*/
#define APFS_32BIT_EARLY_WARN_THRESHOLD (APFS_32BIT_MAX_FILE_SIZE * 5 / 8)
void apfs_warn_32bit_limit(struct apfs_fs_info *fs_info);
void apfs_err_32bit_limit(struct apfs_fs_info *fs_info);
#endif
/*
* Get the correct offset inside the page of extent buffer.
*
* @eb: target extent buffer
* @start: offset inside the extent buffer
*
* Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
*/
static inline size_t get_eb_offset_in_page(const struct extent_buffer *eb,
unsigned long offset)
{
/*
* For sectorsize == PAGE_SIZE case, eb->start will always be aligned
* to PAGE_SIZE, thus adding it won't cause any difference.
*
* For sectorsize < PAGE_SIZE, we must only read the data that belongs
* to the eb, thus we have to take the eb->start into consideration.
*/
return offset_in_page(offset + eb->start);
}
static inline unsigned long get_eb_page_index(unsigned long offset)
{
/*
* For sectorsize == PAGE_SIZE case, plain >> PAGE_SHIFT is enough.
*
* For sectorsize < PAGE_SIZE case, we only support 64K PAGE_SIZE,
* and have ensured that all tree blocks are contained in one page,
* thus we always get index == 0.
*/
return offset >> PAGE_SHIFT;
}
/*
* Use that for functions that are conditionally exported for sanity tests but
* otherwise static
*/
#ifndef CONFIG_APFS_FS_RUN_SANITY_TESTS
#define EXPORT_FOR_TESTS static
#else
#define EXPORT_FOR_TESTS
#endif
__cold
static inline void apfs_print_v0_err(struct apfs_fs_info *fs_info)
{
apfs_err(fs_info,
"Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
}
__printf(5, 6)
__cold
void __apfs_handle_fs_error(struct apfs_fs_info *fs_info, const char *function,
unsigned int line, int errno, const char *fmt, ...);
const char * __attribute_const__ apfs_decode_error(int errno);
__cold
void __apfs_abort_transaction(struct apfs_trans_handle *trans,
const char *function,
unsigned int line, int errno);
/*
* Call apfs_abort_transaction as early as possible when an error condition is
* detected, that way the exact line number is reported.
*/
#define apfs_abort_transaction(trans, errno) \
do { \
/* Report first abort since mount */ \
if (!test_and_set_bit(APFS_FS_STATE_TRANS_ABORTED, \
&((trans)->fs_info->fs_state))) { \
if ((errno) != -EIO && (errno) != -EROFS) { \
WARN(1, KERN_DEBUG \
"APFS: Transaction aborted (error %d)\n", \
(errno)); \
} else { \
apfs_debug((trans)->fs_info, \
"Transaction aborted (error %d)", \
(errno)); \
} \
} \
__apfs_abort_transaction((trans), __func__, \
__LINE__, (errno)); \
} while (0)
#define apfs_handle_fs_error(fs_info, errno, fmt, args...) \
do { \
__apfs_handle_fs_error((fs_info), __func__, __LINE__, \
(errno), fmt, ##args); \
} while (0)
__printf(5, 6)
__cold
void __apfs_panic(struct apfs_fs_info *fs_info, const char *function,
unsigned int line, int errno, const char *fmt, ...);
/*
* If APFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __apfs_panic
* will panic(). Otherwise we BUG() here.
*/
#define apfs_panic(fs_info, errno, fmt, args...) \
do { \
__apfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
BUG(); \
} while (0)
/* fs_info */
struct reloc_control;
struct apfs_device;
struct apfs_fs_devices;
struct apfs_balance_control;
struct apfs_delayed_root;
int apfs_read_generic(struct block_device *bdev, u64 bytenr, unsigned long len,
void *res);
/*
* A range of physical addresses.
*/
struct apfs_prange {
__le64 start;
__le64 block_count;
};
/* Header of all objects */
struct apfs_obj_header {
u8 csum[APFS_CSUM_SIZE];
__le64 oid;
__le64 xid;
/*
* The low 16 bits value means object type.
* The high 16 bits are mixed flags.
*/
__le32 type;
__le32 subtype;
};
APFS_SETGET_FUNCS(obj_oid, struct apfs_obj_header, oid, 64);
APFS_SETGET_FUNCS(obj_xid, struct apfs_obj_header, xid, 64);
APFS_SETGET_FUNCS(obj_type, struct apfs_obj_header, type, 32);
APFS_SETGET_FUNCS(obj_subtype, struct apfs_obj_header, subtype, 32);
APFS_SETGET_STACK_FUNCS(stack_obj_oid, struct apfs_obj_header, oid, 64);
APFS_SETGET_STACK_FUNCS(stack_obj_xid, struct apfs_obj_header, xid, 64);
APFS_SETGET_STACK_FUNCS(stack_obj_type, struct apfs_obj_header, type, 32);
APFS_SETGET_STACK_FUNCS(stack_obj_subtype, struct apfs_obj_header, subtype, 32);
/* Object identifier constants */
enum {
APFS_OID_INVALID = 0,
APFS_OID_SUPERBLOCK = 1, //container superblock
APFS_OID_RESERVED = 1024, //oids < 1024 are reserved.
};
#define APFS_OBJ_TYPE_MASK 0x0000ffff
#define APFS_OBJ_TYPE_FLAGS_MASK 0xffff0000
#define APFS_OBJ_STG_TYPE_MASK 0xc0000000
#define APFS_OBJ_TYPE_FLAGS_DEFINED_MASK 0xf8000000
/* Object types */
enum {
APFS_OBJ_TYPE_INVALID = 0,
APFS_OBJ_TYPE_SUPERBLOCK = 1,
APFS_OBJ_TYPE_BTREE = 2,
APFS_OBJ_TYPE_BTREE_NODE = 3,
APFS_OBJ_TYPE_SPACEMAN = 5,
APFS_OBJ_TYPE_SPACEMAN_CAB,
APFS_OBJ_TYPE_SPACEMAN_CIB,
APFS_OBJ_TYPE_SPACEMAN_BITMAP,
APFS_OBJ_TYPE_SPACEMAN_FREE_QUEUE,
APFS_OBJ_TYPE_EXTENT_LIST_TREE,
APFS_OBJ_TYPE_OMAP,
APFS_OBJ_TYPE_CHECKPOINT_MAP,
APFS_OBJ_TYPE_FS,
APFS_OBJ_TYPE_FSTREE,
APFS_OBJ_TYPE_REFTREE,
APFS_OBJ_TYPE_SNAPTREE,
APFS_OBJ_TYPE_NX_REAPER,
APFS_OBJ_TYPE_NX_REAP_LIST,
APFS_OBJ_TYPE_OMAP_SNAPSHOT,
APFS_OBJ_TYPE_EFI_JUMPSTART,
APFS_OBJ_TYPE_FUSION_MIDDLE_TREE,
APFS_OBJ_TYPE_NX_FUSION_WBC,
APFS_OBJ_TYPE_NX_FUSION_WBC_LIST,
APFS_OBJ_TYPE_ER_STATE,
APFS_OBJ_TYPE_GBITMAP,
APFS_OBJ_TYPE_GBITMAP_TREE,
APFS_OBJ_TYPE_GBITMAP_BLOCK,
APFS_OBJ_TYPE_ER_RECOVERY_BLOCK,
APFS_OBJ_TYPE_SNAP_META_EXT,
APFS_OBJ_TYPE_INTEGRITY_META,
APFS_OBJ_TYPE_FEXT_TREE,
APFS_OBJ_TYPE_RESERVED_20,
APFS_OBJ_TYPE_TEST = 0xff,
};
enum apfs_storage {
// virtual object
APFS_STG_VIRTUAL = 0x00000000,
// ephemeral object
APFS_STG_EPHEMERAL = 0x80000000,
// physical object
APFS_STG_PHYSICAL = 0x40000000,
// object without apfs_obj_header
APFS_STG_NOHEADER = 0x20000000,
// encrypted object.
APFS_STG_ENCRYPTED = 0x10000000,
// ephemeral object isn't persisted across unmounting
// SHOULD NEVER HAPPEN
APFS_STG_NONPERSISTENT = 0x08000000,
};
#define APFS_OBJ_TYPE_CONTAINER_KEYBAG "keys"
#define APFS_OBJ_TYPE_VOLUME_KEYBAG "recs"
#define APFS_OBJ_TYPE_MEDIA_KEYBAG "mkey"
#define APFS_NX_MAGIC 1112758350 // (u32)'BSXN'
#define APFS_MAX_FILE_SYSTEMS 100
/* length of apfs_nx_superblock::ephemeral_info */
#define APFS_EPH_INFO_COUNT 4
/*
* Minimum size in blocks used While picking a new checkpoint data area
*/
#define APFS_EPH_MIN_BLOCK_COUNT 8
#define APFS_MAX_FILE_SYSTEM_EPH_STRUCTS 4
#define APFS_TX_MIN_CHECKPOINT_COUNT 4
#define APFS_EPH_INFO_VERSION_1 1
/* Container Flags */
enum apfs_container_flags {
APFS_RESERVED_1 = 0x00000001LL,
APFS_RESERVED_2 = 0x00000002LL,
/* The container uses software cryptography. */
APFS_CRYPTO_SW = 0x00000004LL,
};
enum apfs_container_features {
/*
* The volumes in this container support defragmentation.
*/
APFS_FEATURE_DEFRAG = 0x0000000000000001ULL,
/* This container is using low-capacity Fusion Drive mode. */
/*
* The volumes in this container support defragmentation.
* Low-capacity Fusion Drive mode is enabled when the solid-state
* drive has a smaller capacity and so the cache must be smaller.
*/
APFS_FEATURE_LCFD = 0x0000000000000002ULL,
APFS_SUPPORTED_FEATURES_MASK = (APFS_FEATURE_DEFRAG | APFS_FEATURE_LCFD)
};
#define APFS_SUPPORTED_ROCOMPAT_MASK (0x0ULL)
enum apfs_container_incompat_ro_features {
/*
* macOS 10.12 uses version 1
*/
APFS_INCOMPAT_VERSION1 = 0x1ULL,
/*
* used by macOS 10.13 and iOS 10.3.
*/
APFS_INCOMPAT_VERSION2 = 0x2ULL,
/*
* The container supports Fusion Drives. Since Apple has not released
* the detailed disk layout, not supported yet.
*/
APFS_INCOMPAT_FUSION = 0x100ULL,
/*
* A bit mask of all the backward-incompatible features.
*/
APFS_SUPPORTED_INCOMPAT_MASK =
(APFS_INCOMPAT_VERSION2 | APFS_INCOMPAT_FUSION),
};
enum apfs_counter_id {
/*
* Times of csuming objects.
*/
APFS_COUNT_OBJ_CSUM_SET = 0,
/*
* Times of csuming objects on error.
*/
APFS_COUNT_OBJ_CSUM_FAIL = 1,
/*
* Maximum counters.
*/
APFS_NUM_COUNTERS = 32
};
struct apfs_nx_superblock {
struct apfs_obj_header o;
__le32 magic; // must be APFS_MAGIC
/*
* mutipile/single dev blocksize
*/
__le32 block_size;
/*
* block counts avaiable
*/
__le64 block_count;
/*
* For write, not used as for now
*/
__le64 features;
__le64 readonly_compatible_features;
__le64 incompatible_features;
uuid_t uuid;
__le64 next_oid;
__le64 next_xid;
/*
* Number of blocks occupied by checkpoint descriptor area.
* The highest bit is used as a flag.
*/
__le32 xp_desc_blocks;
/*
* Number of blocks occupied by checkpoint data area.
* The highest bit is used as a flag.
*/
__le32 xp_data_blocks;
/*
* The start address of the checkpoint descriptor area,
* or the physical object identifier of a tree that contains the
* address information.
*/
__le64 xp_desc_base;
/*
* The start address of the checkpoint data area,
* or the physical object identifier of a tree that contains the
* address information.
*/
__le64 xp_data_base;
/*
* next checkpoint desc index
*/
__le32 xp_desc_next;
/*
* next checkpoint data index
*/
__le32 xp_data_next;
/*
* Start index in desc area. Ignore it if superblock is not in
* desc area e.g. start at block 0.
*/
__le32 xp_desc_index;
/*
* blocks number of desc area.
*/
__le32 xp_desc_len;
/*
* Start index in data area. Ignore it if superblock is not in
* desc area e.g. start at block 0.
*/
__le32 xp_data_index;
/*
* blocks number of data area.
*/
__le32 xp_data_len;
/*
* space manager ephemeral object identifier.
*/
__le64 spaceman_oid;
/*
* physical address of object map.
*/
__le64 omap_oid;
/*
* ephemeral address of reaper.
*/
__le64 reaper_oid;
/*
* for testing
*/
__le32 test_type;
/*
* Maximum numbers of the container can contain.
* DIV_ROUND_UP(container_size, 512MiB)
*
* Maximum: APFS_MAX_FILE_SYSTEMS (100).
*/
__le32 max_file_systems;
/*
* Virtual address for apfs volumes
*/
__le64 fs_oid[APFS_MAX_FILE_SYSTEMS];
/*
* counters for debug intention
*/
__le64 counters[APFS_NUM_COUNTERS];
/*
* The area should never be allocated. For shrink purpose.
*/
__le64 pinned_block_start;
__le64 pinned_block_count;
/*
* Physical address points a tree containing blocks should be unpinned.
*/
__le64 evict_mapping_tree_oid;
__le64 flags;
uuid_t fusion_uuid;
/*
* range of keybag.
*/
__le64 keylocker_start;
__le64 keylocker_count;
__le64 ephemeral_info[APFS_EPH_INFO_COUNT];
/*
* For test only.
*/
__le64 test_oid;
__le64 fusion_mt_oid;
__le64 fusion_wbc_oid;
struct apfs_prange fusion_wbc;
/*
* Reserved.
* Do NOT touch this field.
*/
__le64 newest_mounted_version;
/*
* Wrapped media key.
*/
struct apfs_prange mkb_locker;
};
APFS_SETGET_OBJ_FUNCS(nx_super, struct apfs_nx_superblock);
/* struct apfs_nx_superblock */
APFS_SETGET_STACK_FUNCS(nx_super_magic, struct apfs_nx_superblock, magic, 32);
APFS_SETGET_STACK_FUNCS(nx_super_block_size, struct apfs_nx_superblock,
block_size, 32);
APFS_SETGET_STACK_FUNCS(nx_super_block_count, struct apfs_nx_superblock,
block_count, 64);
APFS_SETGET_STACK_FUNCS(nx_super_features, struct apfs_nx_superblock, features,
64);
APFS_SETGET_STACK_FUNCS(nx_super_ro_compat_features, struct apfs_nx_superblock,
readonly_compatible_features, 64);
APFS_SETGET_STACK_FUNCS(nx_super_incompat_features, struct apfs_nx_superblock,
incompatible_features, 64);
APFS_SETGET_STACK_FUNCS(nx_super_next_oid, struct apfs_nx_superblock, next_oid,
64);
APFS_SETGET_STACK_FUNCS(nx_super_next_xid, struct apfs_nx_superblock, next_xid,
64);
APFS_SETGET_STACK_FUNCS(nx_super_xp_desc_blocks, struct apfs_nx_superblock,
xp_desc_blocks, 32);
APFS_SETGET_STACK_FUNCS(nx_super_xp_data_blocks, struct apfs_nx_superblock,
xp_data_blocks, 32);
APFS_SETGET_STACK_FUNCS(nx_super_xp_desc_base, struct apfs_nx_superblock,
xp_desc_base, 64);
APFS_SETGET_STACK_FUNCS(nx_super_xp_desc_index, struct apfs_nx_superblock,
xp_desc_index, 32);
APFS_SETGET_STACK_FUNCS(nx_super_xp_desc_len, struct apfs_nx_superblock,
xp_desc_len, 32);
APFS_SETGET_STACK_FUNCS(nx_super_xp_data_base, struct apfs_nx_superblock,
xp_data_base, 64);
APFS_SETGET_STACK_FUNCS(nx_super_omap_oid, struct apfs_nx_superblock,
omap_oid, 64);
APFS_SETGET_STACK_FUNCS(nx_super_max_index, struct apfs_nx_superblock,
max_file_systems, 64);
static inline u64
apfs_fs_oid(struct apfs_nx_superblock *sb, int nr)
{
ASSERT(0 <= nr && nr < APFS_MAX_FILE_SYSTEMS);
return __le64_to_cpu(sb->fs_oid[nr]);
}
/*
* It depends on the highest bit of the xp_desc/data base.
* If 0, it means areas are contiuguous.
* If 1, the values points to a tree consists of address ranges.
*/
static inline u64
__apfs_xp_base(const struct apfs_nx_superblock *super, bool desc)
{
return 0;
}
static inline u64
apfs_xp_desc_base(const struct apfs_nx_superblock *super, bool desc)
{
return __apfs_xp_base(super, super->xp_desc_base);
}
static inline u64
apfs_xp_data_base(const struct apfs_nx_superblock *super, bool desc)
{
return __apfs_xp_base(super, super->xp_data_base);
}
struct apfs_checkpoint_mapping {
/*
* The low 16 bits means object type,
* the high 16 bits are flags.
*/
__le32 type;
__le32 subtype;
__le32 size;
__le32 pad;
/*
* virtual address of one volume
*/
__le64 fs_oid;
/*
* ephemeral address
*/
__le64 oid;
/*
* address in the checkpoint data
*/
__le64 paddr;
};
#define APFS_CHECKPOINT_MAP_LAST 0x00000001
/*
* flags in last block is with the APFS_CHECKPOINT_MAP_LAST.
*/
struct apfs_checkpoint_map_phys {
struct apfs_obj_header o;
__le32 flags;
__le32 count;
struct apfs_checkpoint_mapping map[];
};
/* struct apfs_nx_superblock */
APFS_SETGET_OBJ_FUNCS(checkpoint_map, struct apfs_checkpoint_map_phys);
/* struct apfs_nx_superblock */
APFS_SETGET_STACK_FUNCS(checkpoint_map_count, struct apfs_checkpoint_map_phys,
count, 32);
/* struct apfs_nx_superblock */
APFS_SETGET_STACK_FUNCS(checkpoint_map_flags, struct apfs_checkpoint_map_phys,
flags, 32);
/*
* see apfs_nx_superblock::evict_mapping_tree_oid
*/
struct apfs_evict_mapping_val {
u64 dst_paddr;
__le64 len;
} __attribute__((__packed__));
#define APFS_MAX_OMAP_SNAP_COUNT UINT32_MAX
/* Object map reaper constants */
/* map tree is in progress of deleting. */
#define APFS_OMAP_REAP_PHASE_MAP_TREE 1
/* snap tree is in progress of deleting. */
#define APFS_OMAP_REAP_PHASE_SNAPSHOT_TREE 2
/* object map Flags */
/* The object map doesn't support snapshots. */
#define APFS_OMAP_MANUALLY_MANAGED 0x00000001
#define APFS_OMAP_ENCRYPTING 0x00000002
#define APFS_OMAP_DECRYPTING 0x00000004
/*
* Even newkey is assigned, the older key is used for encrypted.
*/
#define APFS_OMAP_KEYROLLING 0x00000008
#define APFS_OMAP_CRYPTO_GENERATION 0x00000010
#define APFS_OMAP_VALID_FLAGS 0x0000001f
struct apfs_omap_phys {
struct apfs_obj_header o;
__le32 flags;
__le32 snap_count;
__le32 tree_type;
__le32 snap_tree_type;
__le64 tree_oid;
__le64 snap_tree_oid;
__le64 latest_snap;
/* minimum xid for revert. */
__le64 revert_min;
/* maximum xid for revert. */