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