xref: /reactos/drivers/filesystems/btrfs/btrfs.h (revision 6e0cf03d)
1 /* btrfs.h
2  * Generic btrfs header file. Thanks to whoever it was who wrote
3  * https://btrfs.wiki.kernel.org/index.php/On-disk_Format - you saved me a lot of time!
4  *
5  * I release this file, and this file only, into the public domain - do whatever
6  * you want with it. You don't have to, but I'd appreciate if you let me know if you
7  * use it anything cool - mark@harmstone.com. */
8 
9 #pragma once
10 
11 #include <stdint.h>
12 #ifndef __REACTOS__
13 #include <assert.h>
14 #endif // __REACTOS__
15 
16 static const uint64_t superblock_addrs[] = { 0x10000, 0x4000000, 0x4000000000, 0x4000000000000, 0 };
17 
18 #define BTRFS_MAGIC         0x4d5f53665248425f
19 #define MAX_LABEL_SIZE      0x100
20 #define SUBVOL_ROOT_INODE   0x100
21 #define BTRFS_LAST_FREE_OBJECTID    0xffffffffffffff00
22 
23 #define TYPE_INODE_ITEM        0x01
24 #define TYPE_INODE_REF         0x0C
25 #define TYPE_INODE_EXTREF      0x0D
26 #define TYPE_XATTR_ITEM        0x18
27 #define TYPE_ORPHAN_INODE      0x30
28 #define TYPE_DIR_ITEM          0x54
29 #define TYPE_DIR_INDEX         0x60
30 #define TYPE_EXTENT_DATA       0x6C
31 #define TYPE_EXTENT_CSUM       0x80
32 #define TYPE_ROOT_ITEM         0x84
33 #define TYPE_ROOT_BACKREF      0x90
34 #define TYPE_ROOT_REF          0x9C
35 #define TYPE_EXTENT_ITEM       0xA8
36 #define TYPE_METADATA_ITEM     0xA9
37 #define TYPE_TREE_BLOCK_REF    0xB0
38 #define TYPE_EXTENT_DATA_REF   0xB2
39 #define TYPE_EXTENT_REF_V0     0xB4
40 #define TYPE_SHARED_BLOCK_REF  0xB6
41 #define TYPE_SHARED_DATA_REF   0xB8
42 #define TYPE_BLOCK_GROUP_ITEM  0xC0
43 #define TYPE_FREE_SPACE_INFO   0xC6
44 #define TYPE_FREE_SPACE_EXTENT 0xC7
45 #define TYPE_FREE_SPACE_BITMAP 0xC8
46 #define TYPE_DEV_EXTENT        0xCC
47 #define TYPE_DEV_ITEM          0xD8
48 #define TYPE_CHUNK_ITEM        0xE4
49 #define TYPE_TEMP_ITEM         0xF8
50 #define TYPE_DEV_STATS         0xF9
51 #define TYPE_SUBVOL_UUID       0xFB
52 #define TYPE_SUBVOL_REC_UUID   0xFC
53 
54 #define BTRFS_ROOT_ROOT         1
55 #define BTRFS_ROOT_EXTENT       2
56 #define BTRFS_ROOT_CHUNK        3
57 #define BTRFS_ROOT_DEVTREE      4
58 #define BTRFS_ROOT_FSTREE       5
59 #define BTRFS_ROOT_TREEDIR      6
60 #define BTRFS_ROOT_CHECKSUM     7
61 #define BTRFS_ROOT_UUID         9
62 #define BTRFS_ROOT_FREE_SPACE   0xa
63 #define BTRFS_ROOT_DATA_RELOC   0xFFFFFFFFFFFFFFF7
64 
65 #define BTRFS_COMPRESSION_NONE  0
66 #define BTRFS_COMPRESSION_ZLIB  1
67 #define BTRFS_COMPRESSION_LZO   2
68 #define BTRFS_COMPRESSION_ZSTD  3
69 
70 #define BTRFS_ENCRYPTION_NONE   0
71 
72 #define BTRFS_ENCODING_NONE     0
73 
74 #define EXTENT_TYPE_INLINE      0
75 #define EXTENT_TYPE_REGULAR     1
76 #define EXTENT_TYPE_PREALLOC    2
77 
78 #define BLOCK_FLAG_DATA         0x001
79 #define BLOCK_FLAG_SYSTEM       0x002
80 #define BLOCK_FLAG_METADATA     0x004
81 #define BLOCK_FLAG_RAID0        0x008
82 #define BLOCK_FLAG_RAID1        0x010
83 #define BLOCK_FLAG_DUPLICATE    0x020
84 #define BLOCK_FLAG_RAID10       0x040
85 #define BLOCK_FLAG_RAID5        0x080
86 #define BLOCK_FLAG_RAID6        0x100
87 #define BLOCK_FLAG_RAID1C3      0x200
88 #define BLOCK_FLAG_RAID1C4      0x400
89 
90 #define FREE_SPACE_CACHE_ID     0xFFFFFFFFFFFFFFF5
91 #define EXTENT_CSUM_ID          0xFFFFFFFFFFFFFFF6
92 #define BALANCE_ITEM_ID         0xFFFFFFFFFFFFFFFC
93 
94 #define BTRFS_INODE_NODATASUM   0x001
95 #define BTRFS_INODE_NODATACOW   0x002
96 #define BTRFS_INODE_READONLY    0x004
97 #define BTRFS_INODE_NOCOMPRESS  0x008
98 #define BTRFS_INODE_PREALLOC    0x010
99 #define BTRFS_INODE_SYNC        0x020
100 #define BTRFS_INODE_IMMUTABLE   0x040
101 #define BTRFS_INODE_APPEND      0x080
102 #define BTRFS_INODE_NODUMP      0x100
103 #define BTRFS_INODE_NOATIME     0x200
104 #define BTRFS_INODE_DIRSYNC     0x400
105 #define BTRFS_INODE_COMPRESS    0x800
106 
107 #define BTRFS_INODE_RO_VERITY   0x1
108 
109 #define BTRFS_SUBVOL_READONLY   0x1
110 
111 #define BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE          0x1
112 #define BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE_VALID    0x2
113 #define BTRFS_COMPAT_RO_FLAGS_VERITY                    0x4
114 
115 #define BTRFS_INCOMPAT_FLAGS_MIXED_BACKREF      0x0001
116 #define BTRFS_INCOMPAT_FLAGS_DEFAULT_SUBVOL     0x0002
117 #define BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS       0x0004
118 #define BTRFS_INCOMPAT_FLAGS_COMPRESS_LZO       0x0008
119 #define BTRFS_INCOMPAT_FLAGS_COMPRESS_ZSTD      0x0010
120 #define BTRFS_INCOMPAT_FLAGS_BIG_METADATA       0x0020
121 #define BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF      0x0040
122 #define BTRFS_INCOMPAT_FLAGS_RAID56             0x0080
123 #define BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA    0x0100
124 #define BTRFS_INCOMPAT_FLAGS_NO_HOLES           0x0200
125 #define BTRFS_INCOMPAT_FLAGS_METADATA_UUID      0x0400
126 #define BTRFS_INCOMPAT_FLAGS_RAID1C34           0x0800
127 
128 #define BTRFS_SUPERBLOCK_FLAGS_SEEDING   0x100000000
129 
130 #define BTRFS_ORPHAN_INODE_OBJID         0xFFFFFFFFFFFFFFFB
131 
132 #define CSUM_TYPE_CRC32C        0
133 #define CSUM_TYPE_XXHASH        1
134 #define CSUM_TYPE_SHA256        2
135 #define CSUM_TYPE_BLAKE2        3
136 
137 #pragma pack(push, 1)
138 
139 typedef struct {
140     uint8_t uuid[16];
141 } BTRFS_UUID;
142 
143 typedef struct {
144     uint64_t obj_id;
145     uint8_t obj_type;
146     uint64_t offset;
147 } KEY;
148 
149 #define HEADER_FLAG_WRITTEN         0x000000000000001
150 #define HEADER_FLAG_SHARED_BACKREF  0x000000000000002
151 #define HEADER_FLAG_MIXED_BACKREF   0x100000000000000
152 
153 typedef struct {
154     uint8_t csum[32];
155     BTRFS_UUID fs_uuid;
156     uint64_t address;
157     uint64_t flags;
158     BTRFS_UUID chunk_tree_uuid;
159     uint64_t generation;
160     uint64_t tree_id;
161     uint32_t num_items;
162     uint8_t level;
163 } tree_header;
164 
165 typedef struct {
166     KEY key;
167     uint32_t offset;
168     uint32_t size;
169 } leaf_node;
170 
171 typedef struct {
172     KEY key;
173     uint64_t address;
174     uint64_t generation;
175 } internal_node;
176 
177 typedef struct {
178     uint64_t dev_id;
179     uint64_t num_bytes;
180     uint64_t bytes_used;
181     uint32_t optimal_io_align;
182     uint32_t optimal_io_width;
183     uint32_t minimal_io_size;
184     uint64_t type;
185     uint64_t generation;
186     uint64_t start_offset;
187     uint32_t dev_group;
188     uint8_t seek_speed;
189     uint8_t bandwidth;
190     BTRFS_UUID device_uuid;
191     BTRFS_UUID fs_uuid;
192 } DEV_ITEM;
193 
194 #define SYS_CHUNK_ARRAY_SIZE 0x800
195 #define BTRFS_NUM_BACKUP_ROOTS 4
196 
197 typedef struct {
198     uint64_t root_tree_addr;
199     uint64_t root_tree_generation;
200     uint64_t chunk_tree_addr;
201     uint64_t chunk_tree_generation;
202     uint64_t extent_tree_addr;
203     uint64_t extent_tree_generation;
204     uint64_t fs_tree_addr;
205     uint64_t fs_tree_generation;
206     uint64_t dev_root_addr;
207     uint64_t dev_root_generation;
208     uint64_t csum_root_addr;
209     uint64_t csum_root_generation;
210     uint64_t total_bytes;
211     uint64_t bytes_used;
212     uint64_t num_devices;
213     uint64_t reserved[4];
214     uint8_t root_level;
215     uint8_t chunk_root_level;
216     uint8_t extent_root_level;
217     uint8_t fs_root_level;
218     uint8_t dev_root_level;
219     uint8_t csum_root_level;
220     uint8_t reserved2[10];
221 } superblock_backup;
222 
223 typedef struct {
224     uint8_t checksum[32];
225     BTRFS_UUID uuid;
226     uint64_t sb_phys_addr;
227     uint64_t flags;
228     uint64_t magic;
229     uint64_t generation;
230     uint64_t root_tree_addr;
231     uint64_t chunk_tree_addr;
232     uint64_t log_tree_addr;
233     uint64_t log_root_transid;
234     uint64_t total_bytes;
235     uint64_t bytes_used;
236     uint64_t root_dir_objectid;
237     uint64_t num_devices;
238     uint32_t sector_size;
239     uint32_t node_size;
240     uint32_t leaf_size;
241     uint32_t stripe_size;
242     uint32_t n;
243     uint64_t chunk_root_generation;
244     uint64_t compat_flags;
245     uint64_t compat_ro_flags;
246     uint64_t incompat_flags;
247     uint16_t csum_type;
248     uint8_t root_level;
249     uint8_t chunk_root_level;
250     uint8_t log_root_level;
251     DEV_ITEM dev_item;
252     char label[MAX_LABEL_SIZE];
253     uint64_t cache_generation;
254     uint64_t uuid_tree_generation;
255     BTRFS_UUID metadata_uuid;
256     uint64_t reserved[28];
257     uint8_t sys_chunk_array[SYS_CHUNK_ARRAY_SIZE];
258     superblock_backup backup[BTRFS_NUM_BACKUP_ROOTS];
259     uint8_t reserved2[565];
260 } superblock;
261 
262 #define BTRFS_TYPE_UNKNOWN   0
263 #define BTRFS_TYPE_FILE      1
264 #define BTRFS_TYPE_DIRECTORY 2
265 #define BTRFS_TYPE_CHARDEV   3
266 #define BTRFS_TYPE_BLOCKDEV  4
267 #define BTRFS_TYPE_FIFO      5
268 #define BTRFS_TYPE_SOCKET    6
269 #define BTRFS_TYPE_SYMLINK   7
270 #define BTRFS_TYPE_EA        8
271 
272 typedef struct {
273     KEY key;
274     uint64_t transid;
275     uint16_t m;
276     uint16_t n;
277     uint8_t type;
278     char name[1];
279 } DIR_ITEM;
280 
281 typedef struct {
282     uint64_t seconds;
283     uint32_t nanoseconds;
284 } BTRFS_TIME;
285 
286 typedef struct {
287     uint64_t generation;
288     uint64_t transid;
289     uint64_t st_size;
290     uint64_t st_blocks;
291     uint64_t block_group;
292     uint32_t st_nlink;
293     uint32_t st_uid;
294     uint32_t st_gid;
295     uint32_t st_mode;
296     uint64_t st_rdev;
297     uint32_t flags;
298     uint32_t flags_ro;
299     uint64_t sequence;
300     uint8_t reserved[32];
301     BTRFS_TIME st_atime;
302     BTRFS_TIME st_ctime;
303     BTRFS_TIME st_mtime;
304     BTRFS_TIME otime;
305 } INODE_ITEM;
306 
307 #ifndef __REACTOS__
308 static_assert(sizeof(INODE_ITEM) == 0xa0, "INODE_ITEM has wrong size");
309 #endif // __REACTOS__
310 
311 typedef struct {
312     INODE_ITEM inode;
313     uint64_t generation;
314     uint64_t objid;
315     uint64_t block_number;
316     uint64_t byte_limit;
317     uint64_t bytes_used;
318     uint64_t last_snapshot_generation;
319     uint64_t flags;
320     uint32_t num_references;
321     KEY drop_progress;
322     uint8_t drop_level;
323     uint8_t root_level;
324     uint64_t generation2;
325     BTRFS_UUID uuid;
326     BTRFS_UUID parent_uuid;
327     BTRFS_UUID received_uuid;
328     uint64_t ctransid;
329     uint64_t otransid;
330     uint64_t stransid;
331     uint64_t rtransid;
332     BTRFS_TIME ctime;
333     BTRFS_TIME otime;
334     BTRFS_TIME stime;
335     BTRFS_TIME rtime;
336     uint64_t reserved[8];
337 } ROOT_ITEM;
338 
339 typedef struct {
340     uint64_t size;
341     uint64_t root_id;
342     uint64_t stripe_length;
343     uint64_t type;
344     uint32_t opt_io_alignment;
345     uint32_t opt_io_width;
346     uint32_t sector_size;
347     uint16_t num_stripes;
348     uint16_t sub_stripes;
349 } CHUNK_ITEM;
350 
351 typedef struct {
352     uint64_t dev_id;
353     uint64_t offset;
354     BTRFS_UUID dev_uuid;
355 } CHUNK_ITEM_STRIPE;
356 
357 typedef struct {
358     uint64_t generation;
359     uint64_t decoded_size;
360     uint8_t compression;
361     uint8_t encryption;
362     uint16_t encoding;
363     uint8_t type;
364     uint8_t data[1];
365 } EXTENT_DATA;
366 
367 typedef struct {
368     uint64_t address;
369     uint64_t size;
370     uint64_t offset;
371     uint64_t num_bytes;
372 } EXTENT_DATA2;
373 
374 typedef struct {
375     uint64_t index;
376     uint16_t n;
377     char name[1];
378 } INODE_REF;
379 
380 typedef struct {
381     uint64_t dir;
382     uint64_t index;
383     uint16_t n;
384     char name[1];
385 } INODE_EXTREF;
386 
387 #define EXTENT_ITEM_DATA            0x001
388 #define EXTENT_ITEM_TREE_BLOCK      0x002
389 #define EXTENT_ITEM_SHARED_BACKREFS 0x100
390 
391 typedef struct {
392     uint64_t refcount;
393     uint64_t generation;
394     uint64_t flags;
395 } EXTENT_ITEM;
396 
397 typedef struct {
398     KEY firstitem;
399     uint8_t level;
400 } EXTENT_ITEM2;
401 
402 typedef struct {
403     uint32_t refcount;
404 } EXTENT_ITEM_V0;
405 
406 typedef struct {
407     EXTENT_ITEM extent_item;
408     KEY firstitem;
409     uint8_t level;
410 } EXTENT_ITEM_TREE;
411 
412 typedef struct {
413     uint64_t offset;
414 } TREE_BLOCK_REF;
415 
416 typedef struct {
417     uint64_t root;
418     uint64_t objid;
419     uint64_t offset;
420     uint32_t count;
421 } EXTENT_DATA_REF;
422 
423 typedef struct {
424     uint64_t used;
425     uint64_t chunk_tree;
426     uint64_t flags;
427 } BLOCK_GROUP_ITEM;
428 
429 typedef struct {
430     uint64_t root;
431     uint64_t gen;
432     uint64_t objid;
433     uint32_t count;
434 } EXTENT_REF_V0;
435 
436 typedef struct {
437     uint64_t offset;
438 } SHARED_BLOCK_REF;
439 
440 typedef struct {
441     uint64_t offset;
442     uint32_t count;
443 } SHARED_DATA_REF;
444 
445 #define FREE_SPACE_EXTENT 1
446 #define FREE_SPACE_BITMAP 2
447 
448 typedef struct {
449     uint64_t offset;
450     uint64_t size;
451     uint8_t type;
452 } FREE_SPACE_ENTRY;
453 
454 typedef struct {
455     KEY key;
456     uint64_t generation;
457     uint64_t num_entries;
458     uint64_t num_bitmaps;
459 } FREE_SPACE_ITEM;
460 
461 typedef struct {
462     uint64_t dir;
463     uint64_t index;
464     uint16_t n;
465     char name[1];
466 } ROOT_REF;
467 
468 typedef struct {
469     uint64_t chunktree;
470     uint64_t objid;
471     uint64_t address;
472     uint64_t length;
473     BTRFS_UUID chunktree_uuid;
474 } DEV_EXTENT;
475 
476 #define BALANCE_FLAGS_DATA          0x1
477 #define BALANCE_FLAGS_SYSTEM        0x2
478 #define BALANCE_FLAGS_METADATA      0x4
479 
480 #define BALANCE_ARGS_FLAGS_PROFILES         0x001
481 #define BALANCE_ARGS_FLAGS_USAGE            0x002
482 #define BALANCE_ARGS_FLAGS_DEVID            0x004
483 #define BALANCE_ARGS_FLAGS_DRANGE           0x008
484 #define BALANCE_ARGS_FLAGS_VRANGE           0x010
485 #define BALANCE_ARGS_FLAGS_LIMIT            0x020
486 #define BALANCE_ARGS_FLAGS_LIMIT_RANGE      0x040
487 #define BALANCE_ARGS_FLAGS_STRIPES_RANGE    0x080
488 #define BALANCE_ARGS_FLAGS_CONVERT          0x100
489 #define BALANCE_ARGS_FLAGS_SOFT             0x200
490 #define BALANCE_ARGS_FLAGS_USAGE_RANGE      0x400
491 
492 typedef struct {
493     uint64_t profiles;
494 
495     union {
496             uint64_t usage;
497             struct {
498                     uint32_t usage_start;
499                     uint32_t usage_end;
500             };
501     };
502 
503     uint64_t devid;
504     uint64_t drange_start;
505     uint64_t drange_end;
506     uint64_t vrange_start;
507     uint64_t vrange_end;
508     uint64_t convert;
509     uint64_t flags;
510 
511     union {
512             uint64_t limit;
513             struct {
514                     uint32_t limit_start;
515                     uint32_t limit_end;
516             };
517     };
518 
519     uint32_t stripes_start;
520     uint32_t stripes_end;
521     uint8_t reserved[48];
522 } BALANCE_ARGS;
523 
524 typedef struct {
525     uint64_t flags;
526     BALANCE_ARGS data;
527     BALANCE_ARGS metadata;
528     BALANCE_ARGS system;
529     uint8_t reserved[32];
530 } BALANCE_ITEM;
531 
532 #define BTRFS_FREE_SPACE_USING_BITMAPS      1
533 
534 typedef struct {
535     uint32_t count;
536     uint32_t flags;
537 } FREE_SPACE_INFO;
538 
539 #define BTRFS_DEV_STAT_WRITE_ERRORS          0
540 #define BTRFS_DEV_STAT_READ_ERRORS           1
541 #define BTRFS_DEV_STAT_FLUSH_ERRORS          2
542 #define BTRFS_DEV_STAT_CORRUPTION_ERRORS     3
543 #define BTRFS_DEV_STAT_GENERATION_ERRORS     4
544 
545 #define BTRFS_SEND_CMD_SUBVOL          1
546 #define BTRFS_SEND_CMD_SNAPSHOT        2
547 #define BTRFS_SEND_CMD_MKFILE          3
548 #define BTRFS_SEND_CMD_MKDIR           4
549 #define BTRFS_SEND_CMD_MKNOD           5
550 #define BTRFS_SEND_CMD_MKFIFO          6
551 #define BTRFS_SEND_CMD_MKSOCK          7
552 #define BTRFS_SEND_CMD_SYMLINK         8
553 #define BTRFS_SEND_CMD_RENAME          9
554 #define BTRFS_SEND_CMD_LINK           10
555 #define BTRFS_SEND_CMD_UNLINK         11
556 #define BTRFS_SEND_CMD_RMDIR          12
557 #define BTRFS_SEND_CMD_SET_XATTR      13
558 #define BTRFS_SEND_CMD_REMOVE_XATTR   14
559 #define BTRFS_SEND_CMD_WRITE          15
560 #define BTRFS_SEND_CMD_CLONE          16
561 #define BTRFS_SEND_CMD_TRUNCATE       17
562 #define BTRFS_SEND_CMD_CHMOD          18
563 #define BTRFS_SEND_CMD_CHOWN          19
564 #define BTRFS_SEND_CMD_UTIMES         20
565 #define BTRFS_SEND_CMD_END            21
566 #define BTRFS_SEND_CMD_UPDATE_EXTENT  22
567 
568 #define BTRFS_SEND_TLV_UUID             1
569 #define BTRFS_SEND_TLV_TRANSID          2
570 #define BTRFS_SEND_TLV_INODE            3
571 #define BTRFS_SEND_TLV_SIZE             4
572 #define BTRFS_SEND_TLV_MODE             5
573 #define BTRFS_SEND_TLV_UID              6
574 #define BTRFS_SEND_TLV_GID              7
575 #define BTRFS_SEND_TLV_RDEV             8
576 #define BTRFS_SEND_TLV_CTIME            9
577 #define BTRFS_SEND_TLV_MTIME           10
578 #define BTRFS_SEND_TLV_ATIME           11
579 #define BTRFS_SEND_TLV_OTIME           12
580 #define BTRFS_SEND_TLV_XATTR_NAME      13
581 #define BTRFS_SEND_TLV_XATTR_DATA      14
582 #define BTRFS_SEND_TLV_PATH            15
583 #define BTRFS_SEND_TLV_PATH_TO         16
584 #define BTRFS_SEND_TLV_PATH_LINK       17
585 #define BTRFS_SEND_TLV_OFFSET          18
586 #define BTRFS_SEND_TLV_DATA            19
587 #define BTRFS_SEND_TLV_CLONE_UUID      20
588 #define BTRFS_SEND_TLV_CLONE_CTRANSID  21
589 #define BTRFS_SEND_TLV_CLONE_PATH      22
590 #define BTRFS_SEND_TLV_CLONE_OFFSET    23
591 #define BTRFS_SEND_TLV_CLONE_LENGTH    24
592 
593 #define BTRFS_SEND_MAGIC "btrfs-stream"
594 
595 typedef struct {
596     uint8_t magic[13];
597     uint32_t version;
598 } btrfs_send_header;
599 
600 typedef struct {
601     uint32_t length;
602     uint16_t cmd;
603     uint32_t csum;
604 } btrfs_send_command;
605 
606 typedef struct {
607     uint16_t type;
608     uint16_t length;
609 } btrfs_send_tlv;
610 
611 #pragma pack(pop)
612