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