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