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