1
2#ifndef _BTRFS_CTREE_H_
3#define _BTRFS_CTREE_H_
4
5#include <linux/btrfs.h>
6#include <linux/types.h>
7#ifdef __KERNEL__
8#include <linux/stddef.h>
9#else
10#include <stddef.h>
11#endif
12
13
14
15
16
17
18
19
20
21#define BTRFS_ROOT_TREE_OBJECTID 1ULL
22
23
24#define BTRFS_EXTENT_TREE_OBJECTID 2ULL
25
26
27
28
29
30#define BTRFS_CHUNK_TREE_OBJECTID 3ULL
31
32
33
34
35
36#define BTRFS_DEV_TREE_OBJECTID 4ULL
37
38
39#define BTRFS_FS_TREE_OBJECTID 5ULL
40
41
42#define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
43
44
45#define BTRFS_CSUM_TREE_OBJECTID 7ULL
46
47
48#define BTRFS_QUOTA_TREE_OBJECTID 8ULL
49
50
51#define BTRFS_UUID_TREE_OBJECTID 9ULL
52
53
54#define BTRFS_FREE_SPACE_TREE_OBJECTID 10ULL
55
56
57#define BTRFS_DEV_STATS_OBJECTID 0ULL
58
59
60#define BTRFS_BALANCE_OBJECTID -4ULL
61
62
63#define BTRFS_ORPHAN_OBJECTID -5ULL
64
65
66#define BTRFS_TREE_LOG_OBJECTID -6ULL
67#define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
68
69
70#define BTRFS_TREE_RELOC_OBJECTID -8ULL
71#define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL
72
73
74
75
76
77
78#define BTRFS_EXTENT_CSUM_OBJECTID -10ULL
79
80
81#define BTRFS_FREE_SPACE_OBJECTID -11ULL
82
83
84
85
86
87#define BTRFS_FREE_INO_OBJECTID -12ULL
88
89
90#define BTRFS_MULTIPLE_OBJECTIDS -255ULL
91
92
93
94
95#define BTRFS_FIRST_FREE_OBJECTID 256ULL
96#define BTRFS_LAST_FREE_OBJECTID -256ULL
97#define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
98
99
100
101
102
103
104#define BTRFS_DEV_ITEMS_OBJECTID 1ULL
105
106#define BTRFS_BTREE_INODE_OBJECTID 1
107
108#define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2
109
110#define BTRFS_DEV_REPLACE_DEVID 0ULL
111
112
113
114
115
116
117#define BTRFS_INODE_ITEM_KEY 1
118#define BTRFS_INODE_REF_KEY 12
119#define BTRFS_INODE_EXTREF_KEY 13
120#define BTRFS_XATTR_ITEM_KEY 24
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141#define BTRFS_VERITY_DESC_ITEM_KEY 36
142#define BTRFS_VERITY_MERKLE_ITEM_KEY 37
143
144#define BTRFS_ORPHAN_ITEM_KEY 48
145
146
147
148
149
150
151
152
153#define BTRFS_DIR_LOG_ITEM_KEY 60
154#define BTRFS_DIR_LOG_INDEX_KEY 72
155#define BTRFS_DIR_ITEM_KEY 84
156#define BTRFS_DIR_INDEX_KEY 96
157
158
159
160#define BTRFS_EXTENT_DATA_KEY 108
161
162
163
164
165
166#define BTRFS_EXTENT_CSUM_KEY 128
167
168
169
170
171
172#define BTRFS_ROOT_ITEM_KEY 132
173
174
175
176
177
178#define BTRFS_ROOT_BACKREF_KEY 144
179
180
181
182
183
184
185#define BTRFS_ROOT_REF_KEY 156
186
187
188
189
190
191#define BTRFS_EXTENT_ITEM_KEY 168
192
193
194
195
196
197#define BTRFS_METADATA_ITEM_KEY 169
198
199#define BTRFS_TREE_BLOCK_REF_KEY 176
200
201#define BTRFS_EXTENT_DATA_REF_KEY 178
202
203#define BTRFS_EXTENT_REF_V0_KEY 180
204
205#define BTRFS_SHARED_BLOCK_REF_KEY 182
206
207#define BTRFS_SHARED_DATA_REF_KEY 184
208
209
210
211
212
213#define BTRFS_BLOCK_GROUP_ITEM_KEY 192
214
215
216
217
218
219
220#define BTRFS_FREE_SPACE_INFO_KEY 198
221
222
223
224
225
226#define BTRFS_FREE_SPACE_EXTENT_KEY 199
227
228
229
230
231
232
233
234#define BTRFS_FREE_SPACE_BITMAP_KEY 200
235
236#define BTRFS_DEV_EXTENT_KEY 204
237#define BTRFS_DEV_ITEM_KEY 216
238#define BTRFS_CHUNK_ITEM_KEY 228
239
240
241
242
243
244
245#define BTRFS_QGROUP_STATUS_KEY 240
246
247
248
249
250#define BTRFS_QGROUP_INFO_KEY 242
251
252
253
254
255#define BTRFS_QGROUP_LIMIT_KEY 244
256
257
258
259
260
261
262#define BTRFS_QGROUP_RELATION_KEY 246
263
264
265
266
267#define BTRFS_BALANCE_ITEM_KEY 248
268
269
270
271
272
273
274
275
276
277
278
279
280#define BTRFS_TEMPORARY_ITEM_KEY 248
281
282
283
284
285#define BTRFS_DEV_STATS_KEY 249
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300#define BTRFS_PERSISTENT_ITEM_KEY 249
301
302
303
304
305
306#define BTRFS_DEV_REPLACE_KEY 250
307
308
309
310
311
312
313
314#if BTRFS_UUID_SIZE != 16
315#error "UUID items require BTRFS_UUID_SIZE == 16!"
316#endif
317#define BTRFS_UUID_KEY_SUBVOL 251
318#define BTRFS_UUID_KEY_RECEIVED_SUBVOL 252
319
320
321
322
323
324
325#define BTRFS_STRING_ITEM_KEY 253
326
327
328#define BTRFS_MAX_METADATA_BLOCKSIZE 65536
329
330
331#define BTRFS_CSUM_SIZE 32
332
333
334enum btrfs_csum_type {
335 BTRFS_CSUM_TYPE_CRC32 = 0,
336 BTRFS_CSUM_TYPE_XXHASH = 1,
337 BTRFS_CSUM_TYPE_SHA256 = 2,
338 BTRFS_CSUM_TYPE_BLAKE2 = 3,
339};
340
341
342
343
344
345
346
347
348
349#define BTRFS_FT_UNKNOWN 0
350#define BTRFS_FT_REG_FILE 1
351#define BTRFS_FT_DIR 2
352#define BTRFS_FT_CHRDEV 3
353#define BTRFS_FT_BLKDEV 4
354#define BTRFS_FT_FIFO 5
355#define BTRFS_FT_SOCK 6
356#define BTRFS_FT_SYMLINK 7
357#define BTRFS_FT_XATTR 8
358#define BTRFS_FT_MAX 9
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377struct btrfs_disk_key {
378 __le64 objectid;
379 __u8 type;
380 __le64 offset;
381} __attribute__ ((__packed__));
382
383struct btrfs_key {
384 __u64 objectid;
385 __u8 type;
386 __u64 offset;
387} __attribute__ ((__packed__));
388
389struct btrfs_dev_item {
390
391 __le64 devid;
392
393
394 __le64 total_bytes;
395
396
397 __le64 bytes_used;
398
399
400 __le32 io_align;
401
402
403 __le32 io_width;
404
405
406 __le32 sector_size;
407
408
409 __le64 type;
410
411
412 __le64 generation;
413
414
415
416
417
418 __le64 start_offset;
419
420
421 __le32 dev_group;
422
423
424 __u8 seek_speed;
425
426
427 __u8 bandwidth;
428
429
430 __u8 uuid[BTRFS_UUID_SIZE];
431
432
433 __u8 fsid[BTRFS_UUID_SIZE];
434} __attribute__ ((__packed__));
435
436struct btrfs_stripe {
437 __le64 devid;
438 __le64 offset;
439 __u8 dev_uuid[BTRFS_UUID_SIZE];
440} __attribute__ ((__packed__));
441
442struct btrfs_chunk {
443
444 __le64 length;
445
446
447 __le64 owner;
448
449 __le64 stripe_len;
450 __le64 type;
451
452
453 __le32 io_align;
454
455
456 __le32 io_width;
457
458
459 __le32 sector_size;
460
461
462
463
464 __le16 num_stripes;
465
466
467 __le16 sub_stripes;
468 struct btrfs_stripe stripe;
469
470} __attribute__ ((__packed__));
471
472#define BTRFS_FREE_SPACE_EXTENT 1
473#define BTRFS_FREE_SPACE_BITMAP 2
474
475struct btrfs_free_space_entry {
476 __le64 offset;
477 __le64 bytes;
478 __u8 type;
479} __attribute__ ((__packed__));
480
481struct btrfs_free_space_header {
482 struct btrfs_disk_key location;
483 __le64 generation;
484 __le64 num_entries;
485 __le64 num_bitmaps;
486} __attribute__ ((__packed__));
487
488#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)
489#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)
490
491
492
493#define BTRFS_SUPER_FLAG_ERROR (1ULL << 2)
494
495#define BTRFS_SUPER_FLAG_SEEDING (1ULL << 32)
496#define BTRFS_SUPER_FLAG_METADUMP (1ULL << 33)
497#define BTRFS_SUPER_FLAG_METADUMP_V2 (1ULL << 34)
498#define BTRFS_SUPER_FLAG_CHANGING_FSID (1ULL << 35)
499#define BTRFS_SUPER_FLAG_CHANGING_FSID_V2 (1ULL << 36)
500
501
502
503
504
505
506
507struct btrfs_extent_item {
508 __le64 refs;
509 __le64 generation;
510 __le64 flags;
511} __attribute__ ((__packed__));
512
513struct btrfs_extent_item_v0 {
514 __le32 refs;
515} __attribute__ ((__packed__));
516
517
518#define BTRFS_EXTENT_FLAG_DATA (1ULL << 0)
519#define BTRFS_EXTENT_FLAG_TREE_BLOCK (1ULL << 1)
520
521
522
523
524#define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8)
525
526
527
528
529
530#define BTRFS_EXTENT_FLAG_SUPER (1ULL << 48)
531
532struct btrfs_tree_block_info {
533 struct btrfs_disk_key key;
534 __u8 level;
535} __attribute__ ((__packed__));
536
537struct btrfs_extent_data_ref {
538 __le64 root;
539 __le64 objectid;
540 __le64 offset;
541 __le32 count;
542} __attribute__ ((__packed__));
543
544struct btrfs_shared_data_ref {
545 __le32 count;
546} __attribute__ ((__packed__));
547
548struct btrfs_extent_inline_ref {
549 __u8 type;
550 __le64 offset;
551} __attribute__ ((__packed__));
552
553
554
555
556
557struct btrfs_dev_extent {
558 __le64 chunk_tree;
559 __le64 chunk_objectid;
560 __le64 chunk_offset;
561 __le64 length;
562 __u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
563} __attribute__ ((__packed__));
564
565struct btrfs_inode_ref {
566 __le64 index;
567 __le16 name_len;
568
569} __attribute__ ((__packed__));
570
571struct btrfs_inode_extref {
572 __le64 parent_objectid;
573 __le64 index;
574 __le16 name_len;
575 __u8 name[0];
576
577} __attribute__ ((__packed__));
578
579struct btrfs_timespec {
580 __le64 sec;
581 __le32 nsec;
582} __attribute__ ((__packed__));
583
584struct btrfs_inode_item {
585
586 __le64 generation;
587
588 __le64 transid;
589 __le64 size;
590 __le64 nbytes;
591 __le64 block_group;
592 __le32 nlink;
593 __le32 uid;
594 __le32 gid;
595 __le32 mode;
596 __le64 rdev;
597 __le64 flags;
598
599
600 __le64 sequence;
601
602
603
604
605
606 __le64 reserved[4];
607 struct btrfs_timespec atime;
608 struct btrfs_timespec ctime;
609 struct btrfs_timespec mtime;
610 struct btrfs_timespec otime;
611} __attribute__ ((__packed__));
612
613struct btrfs_dir_log_item {
614 __le64 end;
615} __attribute__ ((__packed__));
616
617struct btrfs_dir_item {
618 struct btrfs_disk_key location;
619 __le64 transid;
620 __le16 data_len;
621 __le16 name_len;
622 __u8 type;
623} __attribute__ ((__packed__));
624
625#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0)
626
627
628
629
630
631#define BTRFS_ROOT_SUBVOL_DEAD (1ULL << 48)
632
633struct btrfs_root_item {
634 struct btrfs_inode_item inode;
635 __le64 generation;
636 __le64 root_dirid;
637 __le64 bytenr;
638 __le64 byte_limit;
639 __le64 bytes_used;
640 __le64 last_snapshot;
641 __le64 flags;
642 __le32 refs;
643 struct btrfs_disk_key drop_progress;
644 __u8 drop_level;
645 __u8 level;
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663 __le64 generation_v2;
664 __u8 uuid[BTRFS_UUID_SIZE];
665 __u8 parent_uuid[BTRFS_UUID_SIZE];
666 __u8 received_uuid[BTRFS_UUID_SIZE];
667 __le64 ctransid;
668 __le64 otransid;
669 __le64 stransid;
670 __le64 rtransid;
671 struct btrfs_timespec ctime;
672 struct btrfs_timespec otime;
673 struct btrfs_timespec stime;
674 struct btrfs_timespec rtime;
675 __le64 reserved[8];
676} __attribute__ ((__packed__));
677
678
679
680
681
682static inline __u32 btrfs_legacy_root_item_size(void)
683{
684 return offsetof(struct btrfs_root_item, generation_v2);
685}
686
687
688
689
690struct btrfs_root_ref {
691 __le64 dirid;
692 __le64 sequence;
693 __le16 name_len;
694} __attribute__ ((__packed__));
695
696struct btrfs_disk_balance_args {
697
698
699
700
701 __le64 profiles;
702
703
704
705
706
707
708 union {
709 __le64 usage;
710 struct {
711 __le32 usage_min;
712 __le32 usage_max;
713 };
714 };
715
716
717 __le64 devid;
718
719
720 __le64 pstart;
721 __le64 pend;
722
723
724 __le64 vstart;
725 __le64 vend;
726
727
728
729
730
731 __le64 target;
732
733
734 __le64 flags;
735
736
737
738
739
740
741 union {
742 __le64 limit;
743 struct {
744 __le32 limit_min;
745 __le32 limit_max;
746 };
747 };
748
749
750
751
752
753 __le32 stripes_min;
754 __le32 stripes_max;
755
756 __le64 unused[6];
757} __attribute__ ((__packed__));
758
759
760
761
762
763struct btrfs_balance_item {
764
765 __le64 flags;
766
767 struct btrfs_disk_balance_args data;
768 struct btrfs_disk_balance_args meta;
769 struct btrfs_disk_balance_args sys;
770
771 __le64 unused[4];
772} __attribute__ ((__packed__));
773
774enum {
775 BTRFS_FILE_EXTENT_INLINE = 0,
776 BTRFS_FILE_EXTENT_REG = 1,
777 BTRFS_FILE_EXTENT_PREALLOC = 2,
778 BTRFS_NR_FILE_EXTENT_TYPES = 3,
779};
780
781struct btrfs_file_extent_item {
782
783
784
785 __le64 generation;
786
787
788
789
790
791
792
793 __le64 ram_bytes;
794
795
796
797
798
799
800
801
802 __u8 compression;
803 __u8 encryption;
804 __le16 other_encoding;
805
806
807 __u8 type;
808
809
810
811
812
813
814
815 __le64 disk_bytenr;
816 __le64 disk_num_bytes;
817
818
819
820
821
822
823
824 __le64 offset;
825
826
827
828
829 __le64 num_bytes;
830
831} __attribute__ ((__packed__));
832
833struct btrfs_csum_item {
834 __u8 csum;
835} __attribute__ ((__packed__));
836
837struct btrfs_dev_stats_item {
838
839
840
841
842 __le64 values[BTRFS_DEV_STAT_VALUES_MAX];
843} __attribute__ ((__packed__));
844
845#define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS 0
846#define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID 1
847
848struct btrfs_dev_replace_item {
849
850
851
852
853 __le64 src_devid;
854 __le64 cursor_left;
855 __le64 cursor_right;
856 __le64 cont_reading_from_srcdev_mode;
857
858 __le64 replace_state;
859 __le64 time_started;
860 __le64 time_stopped;
861 __le64 num_write_errors;
862 __le64 num_uncorrectable_read_errors;
863} __attribute__ ((__packed__));
864
865
866#define BTRFS_BLOCK_GROUP_DATA (1ULL << 0)
867#define BTRFS_BLOCK_GROUP_SYSTEM (1ULL << 1)
868#define BTRFS_BLOCK_GROUP_METADATA (1ULL << 2)
869#define BTRFS_BLOCK_GROUP_RAID0 (1ULL << 3)
870#define BTRFS_BLOCK_GROUP_RAID1 (1ULL << 4)
871#define BTRFS_BLOCK_GROUP_DUP (1ULL << 5)
872#define BTRFS_BLOCK_GROUP_RAID10 (1ULL << 6)
873#define BTRFS_BLOCK_GROUP_RAID5 (1ULL << 7)
874#define BTRFS_BLOCK_GROUP_RAID6 (1ULL << 8)
875#define BTRFS_BLOCK_GROUP_RAID1C3 (1ULL << 9)
876#define BTRFS_BLOCK_GROUP_RAID1C4 (1ULL << 10)
877#define BTRFS_BLOCK_GROUP_RESERVED (BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
878 BTRFS_SPACE_INFO_GLOBAL_RSV)
879
880enum btrfs_raid_types {
881 BTRFS_RAID_RAID10,
882 BTRFS_RAID_RAID1,
883 BTRFS_RAID_DUP,
884 BTRFS_RAID_RAID0,
885 BTRFS_RAID_SINGLE,
886 BTRFS_RAID_RAID5,
887 BTRFS_RAID_RAID6,
888 BTRFS_RAID_RAID1C3,
889 BTRFS_RAID_RAID1C4,
890 BTRFS_NR_RAID_TYPES
891};
892
893#define BTRFS_BLOCK_GROUP_TYPE_MASK (BTRFS_BLOCK_GROUP_DATA | \
894 BTRFS_BLOCK_GROUP_SYSTEM | \
895 BTRFS_BLOCK_GROUP_METADATA)
896
897#define BTRFS_BLOCK_GROUP_PROFILE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \
898 BTRFS_BLOCK_GROUP_RAID1 | \
899 BTRFS_BLOCK_GROUP_RAID1C3 | \
900 BTRFS_BLOCK_GROUP_RAID1C4 | \
901 BTRFS_BLOCK_GROUP_RAID5 | \
902 BTRFS_BLOCK_GROUP_RAID6 | \
903 BTRFS_BLOCK_GROUP_DUP | \
904 BTRFS_BLOCK_GROUP_RAID10)
905#define BTRFS_BLOCK_GROUP_RAID56_MASK (BTRFS_BLOCK_GROUP_RAID5 | \
906 BTRFS_BLOCK_GROUP_RAID6)
907
908#define BTRFS_BLOCK_GROUP_RAID1_MASK (BTRFS_BLOCK_GROUP_RAID1 | \
909 BTRFS_BLOCK_GROUP_RAID1C3 | \
910 BTRFS_BLOCK_GROUP_RAID1C4)
911
912
913
914
915
916
917
918
919#define BTRFS_AVAIL_ALLOC_BIT_SINGLE (1ULL << 48)
920
921
922
923
924
925#define BTRFS_SPACE_INFO_GLOBAL_RSV (1ULL << 49)
926
927#define BTRFS_EXTENDED_PROFILE_MASK (BTRFS_BLOCK_GROUP_PROFILE_MASK | \
928 BTRFS_AVAIL_ALLOC_BIT_SINGLE)
929
930static inline __u64 chunk_to_extended(__u64 flags)
931{
932 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)
933 flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
934
935 return flags;
936}
937static inline __u64 extended_to_chunk(__u64 flags)
938{
939 return flags & ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
940}
941
942struct btrfs_block_group_item {
943 __le64 used;
944 __le64 chunk_objectid;
945 __le64 flags;
946} __attribute__ ((__packed__));
947
948struct btrfs_free_space_info {
949 __le32 extent_count;
950 __le32 flags;
951} __attribute__ ((__packed__));
952
953#define BTRFS_FREE_SPACE_USING_BITMAPS (1ULL << 0)
954
955#define BTRFS_QGROUP_LEVEL_SHIFT 48
956static inline __u16 btrfs_qgroup_level(__u64 qgroupid)
957{
958 return (__u16)(qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT);
959}
960
961
962
963
964#define BTRFS_QGROUP_STATUS_FLAG_ON (1ULL << 0)
965
966
967
968#define BTRFS_QGROUP_STATUS_FLAG_RESCAN (1ULL << 1)
969
970
971
972
973
974
975
976#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)
977
978#define BTRFS_QGROUP_STATUS_VERSION 1
979
980struct btrfs_qgroup_status_item {
981 __le64 version;
982
983
984
985
986
987
988 __le64 generation;
989
990
991 __le64 flags;
992
993
994
995
996
997 __le64 rescan;
998} __attribute__ ((__packed__));
999
1000struct btrfs_qgroup_info_item {
1001 __le64 generation;
1002 __le64 rfer;
1003 __le64 rfer_cmpr;
1004 __le64 excl;
1005 __le64 excl_cmpr;
1006} __attribute__ ((__packed__));
1007
1008struct btrfs_qgroup_limit_item {
1009
1010
1011
1012 __le64 flags;
1013 __le64 max_rfer;
1014 __le64 max_excl;
1015 __le64 rsv_rfer;
1016 __le64 rsv_excl;
1017} __attribute__ ((__packed__));
1018
1019struct btrfs_verity_descriptor_item {
1020
1021 __le64 size;
1022
1023
1024
1025
1026
1027 __le64 reserved[2];
1028 __u8 encryption;
1029} __attribute__ ((__packed__));
1030
1031#endif
1032