1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 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 /* ASCII for _BHRfS_M, no terminating nul */ 14 #define BTRFS_MAGIC 0x4D5F53665248425FULL 15 16 #define BTRFS_MAX_LEVEL 8 17 18 /* 19 * We can actually store much bigger names, but lets not confuse the rest of 20 * linux. 21 */ 22 #define BTRFS_NAME_LEN 255 23 24 /* 25 * Theoretical limit is larger, but we keep this down to a sane value. That 26 * should limit greatly the possibility of collisions on inode ref items. 27 */ 28 #define BTRFS_LINK_MAX 65535U 29 30 /* 31 * This header contains the structure definitions and constants used 32 * by file system objects that can be retrieved using 33 * the BTRFS_IOC_SEARCH_TREE ioctl. That means basically anything that 34 * is needed to describe a leaf node's key or item contents. 35 */ 36 37 /* holds pointers to all of the tree roots */ 38 #define BTRFS_ROOT_TREE_OBJECTID 1ULL 39 40 /* stores information about which extents are in use, and reference counts */ 41 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL 42 43 /* 44 * chunk tree stores translations from logical -> physical block numbering 45 * the super block points to the chunk tree 46 */ 47 #define BTRFS_CHUNK_TREE_OBJECTID 3ULL 48 49 /* 50 * stores information about which areas of a given device are in use. 51 * one per device. The tree of tree roots points to the device tree 52 */ 53 #define BTRFS_DEV_TREE_OBJECTID 4ULL 54 55 /* one per subvolume, storing files and directories */ 56 #define BTRFS_FS_TREE_OBJECTID 5ULL 57 58 /* directory objectid inside the root tree */ 59 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL 60 61 /* holds checksums of all the data extents */ 62 #define BTRFS_CSUM_TREE_OBJECTID 7ULL 63 64 /* holds quota configuration and tracking */ 65 #define BTRFS_QUOTA_TREE_OBJECTID 8ULL 66 67 /* for storing items that use the BTRFS_UUID_KEY* types */ 68 #define BTRFS_UUID_TREE_OBJECTID 9ULL 69 70 /* tracks free space in block groups. */ 71 #define BTRFS_FREE_SPACE_TREE_OBJECTID 10ULL 72 73 /* Holds the block group items for extent tree v2. */ 74 #define BTRFS_BLOCK_GROUP_TREE_OBJECTID 11ULL 75 76 /* Tracks RAID stripes in block groups. */ 77 #define BTRFS_RAID_STRIPE_TREE_OBJECTID 12ULL 78 79 /* device stats in the device tree */ 80 #define BTRFS_DEV_STATS_OBJECTID 0ULL 81 82 /* for storing balance parameters in the root tree */ 83 #define BTRFS_BALANCE_OBJECTID -4ULL 84 85 /* orphan objectid for tracking unlinked/truncated files */ 86 #define BTRFS_ORPHAN_OBJECTID -5ULL 87 88 /* does write ahead logging to speed up fsyncs */ 89 #define BTRFS_TREE_LOG_OBJECTID -6ULL 90 #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL 91 92 /* for space balancing */ 93 #define BTRFS_TREE_RELOC_OBJECTID -8ULL 94 #define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL 95 96 /* 97 * extent checksums all have this objectid 98 * this allows them to share the logging tree 99 * for fsyncs 100 */ 101 #define BTRFS_EXTENT_CSUM_OBJECTID -10ULL 102 103 /* For storing free space cache */ 104 #define BTRFS_FREE_SPACE_OBJECTID -11ULL 105 106 /* 107 * The inode number assigned to the special inode for storing 108 * free ino cache 109 */ 110 #define BTRFS_FREE_INO_OBJECTID -12ULL 111 112 /* dummy objectid represents multiple objectids */ 113 #define BTRFS_MULTIPLE_OBJECTIDS -255ULL 114 115 /* 116 * All files have objectids in this range. 117 */ 118 #define BTRFS_FIRST_FREE_OBJECTID 256ULL 119 #define BTRFS_LAST_FREE_OBJECTID -256ULL 120 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL 121 122 123 /* 124 * the device items go into the chunk tree. The key is in the form 125 * [ 1 BTRFS_DEV_ITEM_KEY device_id ] 126 */ 127 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL 128 129 #define BTRFS_BTREE_INODE_OBJECTID 1 130 131 #define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2 132 133 #define BTRFS_DEV_REPLACE_DEVID 0ULL 134 135 /* 136 * inode items have the data typically returned from stat and store other 137 * info about object characteristics. There is one for every file and dir in 138 * the FS 139 */ 140 #define BTRFS_INODE_ITEM_KEY 1 141 #define BTRFS_INODE_REF_KEY 12 142 #define BTRFS_INODE_EXTREF_KEY 13 143 #define BTRFS_XATTR_ITEM_KEY 24 144 145 /* 146 * fs verity items are stored under two different key types on disk. 147 * The descriptor items: 148 * [ inode objectid, BTRFS_VERITY_DESC_ITEM_KEY, offset ] 149 * 150 * At offset 0, we store a btrfs_verity_descriptor_item which tracks the size 151 * of the descriptor item and some extra data for encryption. 152 * Starting at offset 1, these hold the generic fs verity descriptor. The 153 * latter are opaque to btrfs, we just read and write them as a blob for the 154 * higher level verity code. The most common descriptor size is 256 bytes. 155 * 156 * The merkle tree items: 157 * [ inode objectid, BTRFS_VERITY_MERKLE_ITEM_KEY, offset ] 158 * 159 * These also start at offset 0, and correspond to the merkle tree bytes. When 160 * fsverity asks for page 0 of the merkle tree, we pull up one page starting at 161 * offset 0 for this key type. These are also opaque to btrfs, we're blindly 162 * storing whatever fsverity sends down. 163 */ 164 #define BTRFS_VERITY_DESC_ITEM_KEY 36 165 #define BTRFS_VERITY_MERKLE_ITEM_KEY 37 166 167 #define BTRFS_ORPHAN_ITEM_KEY 48 168 /* reserve 2-15 close to the inode for later flexibility */ 169 170 /* 171 * dir items are the name -> inode pointers in a directory. There is one 172 * for every name in a directory. BTRFS_DIR_LOG_ITEM_KEY is no longer used 173 * but it's still defined here for documentation purposes and to help avoid 174 * having its numerical value reused in the future. 175 */ 176 #define BTRFS_DIR_LOG_ITEM_KEY 60 177 #define BTRFS_DIR_LOG_INDEX_KEY 72 178 #define BTRFS_DIR_ITEM_KEY 84 179 #define BTRFS_DIR_INDEX_KEY 96 180 /* 181 * extent data is for file data 182 */ 183 #define BTRFS_EXTENT_DATA_KEY 108 184 185 /* 186 * extent csums are stored in a separate tree and hold csums for 187 * an entire extent on disk. 188 */ 189 #define BTRFS_EXTENT_CSUM_KEY 128 190 191 /* 192 * root items point to tree roots. They are typically in the root 193 * tree used by the super block to find all the other trees 194 */ 195 #define BTRFS_ROOT_ITEM_KEY 132 196 197 /* 198 * root backrefs tie subvols and snapshots to the directory entries that 199 * reference them 200 */ 201 #define BTRFS_ROOT_BACKREF_KEY 144 202 203 /* 204 * root refs make a fast index for listing all of the snapshots and 205 * subvolumes referenced by a given root. They point directly to the 206 * directory item in the root that references the subvol 207 */ 208 #define BTRFS_ROOT_REF_KEY 156 209 210 /* 211 * extent items are in the extent map tree. These record which blocks 212 * are used, and how many references there are to each block 213 */ 214 #define BTRFS_EXTENT_ITEM_KEY 168 215 216 /* 217 * The same as the BTRFS_EXTENT_ITEM_KEY, except it's metadata we already know 218 * the length, so we save the level in key->offset instead of the length. 219 */ 220 #define BTRFS_METADATA_ITEM_KEY 169 221 222 #define BTRFS_TREE_BLOCK_REF_KEY 176 223 224 #define BTRFS_EXTENT_DATA_REF_KEY 178 225 226 /* 227 * Obsolete key. Defintion removed in 6.6, value may be reused in the future. 228 * 229 * #define BTRFS_EXTENT_REF_V0_KEY 180 230 */ 231 232 #define BTRFS_SHARED_BLOCK_REF_KEY 182 233 234 #define BTRFS_SHARED_DATA_REF_KEY 184 235 236 /* 237 * block groups give us hints into the extent allocation trees. Which 238 * blocks are free etc etc 239 */ 240 #define BTRFS_BLOCK_GROUP_ITEM_KEY 192 241 242 /* 243 * Every block group is represented in the free space tree by a free space info 244 * item, which stores some accounting information. It is keyed on 245 * (block_group_start, FREE_SPACE_INFO, block_group_length). 246 */ 247 #define BTRFS_FREE_SPACE_INFO_KEY 198 248 249 /* 250 * A free space extent tracks an extent of space that is free in a block group. 251 * It is keyed on (start, FREE_SPACE_EXTENT, length). 252 */ 253 #define BTRFS_FREE_SPACE_EXTENT_KEY 199 254 255 /* 256 * When a block group becomes very fragmented, we convert it to use bitmaps 257 * instead of extents. A free space bitmap is keyed on 258 * (start, FREE_SPACE_BITMAP, length); the corresponding item is a bitmap with 259 * (length / sectorsize) bits. 260 */ 261 #define BTRFS_FREE_SPACE_BITMAP_KEY 200 262 263 #define BTRFS_DEV_EXTENT_KEY 204 264 #define BTRFS_DEV_ITEM_KEY 216 265 #define BTRFS_CHUNK_ITEM_KEY 228 266 267 #define BTRFS_RAID_STRIPE_KEY 230 268 269 /* 270 * Records the overall state of the qgroups. 271 * There's only one instance of this key present, 272 * (0, BTRFS_QGROUP_STATUS_KEY, 0) 273 */ 274 #define BTRFS_QGROUP_STATUS_KEY 240 275 /* 276 * Records the currently used space of the qgroup. 277 * One key per qgroup, (0, BTRFS_QGROUP_INFO_KEY, qgroupid). 278 */ 279 #define BTRFS_QGROUP_INFO_KEY 242 280 /* 281 * Contains the user configured limits for the qgroup. 282 * One key per qgroup, (0, BTRFS_QGROUP_LIMIT_KEY, qgroupid). 283 */ 284 #define BTRFS_QGROUP_LIMIT_KEY 244 285 /* 286 * Records the child-parent relationship of qgroups. For 287 * each relation, 2 keys are present: 288 * (childid, BTRFS_QGROUP_RELATION_KEY, parentid) 289 * (parentid, BTRFS_QGROUP_RELATION_KEY, childid) 290 */ 291 #define BTRFS_QGROUP_RELATION_KEY 246 292 293 /* 294 * Obsolete name, see BTRFS_TEMPORARY_ITEM_KEY. 295 */ 296 #define BTRFS_BALANCE_ITEM_KEY 248 297 298 /* 299 * The key type for tree items that are stored persistently, but do not need to 300 * exist for extended period of time. The items can exist in any tree. 301 * 302 * [subtype, BTRFS_TEMPORARY_ITEM_KEY, data] 303 * 304 * Existing items: 305 * 306 * - balance status item 307 * (BTRFS_BALANCE_OBJECTID, BTRFS_TEMPORARY_ITEM_KEY, 0) 308 */ 309 #define BTRFS_TEMPORARY_ITEM_KEY 248 310 311 /* 312 * Obsolete name, see BTRFS_PERSISTENT_ITEM_KEY 313 */ 314 #define BTRFS_DEV_STATS_KEY 249 315 316 /* 317 * The key type for tree items that are stored persistently and usually exist 318 * for a long period, eg. filesystem lifetime. The item kinds can be status 319 * information, stats or preference values. The item can exist in any tree. 320 * 321 * [subtype, BTRFS_PERSISTENT_ITEM_KEY, data] 322 * 323 * Existing items: 324 * 325 * - device statistics, store IO stats in the device tree, one key for all 326 * stats 327 * (BTRFS_DEV_STATS_OBJECTID, BTRFS_DEV_STATS_KEY, 0) 328 */ 329 #define BTRFS_PERSISTENT_ITEM_KEY 249 330 331 /* 332 * Persistently stores the device replace state in the device tree. 333 * The key is built like this: (0, BTRFS_DEV_REPLACE_KEY, 0). 334 */ 335 #define BTRFS_DEV_REPLACE_KEY 250 336 337 /* 338 * Stores items that allow to quickly map UUIDs to something else. 339 * These items are part of the filesystem UUID tree. 340 * The key is built like this: 341 * (UUID_upper_64_bits, BTRFS_UUID_KEY*, UUID_lower_64_bits). 342 */ 343 #if BTRFS_UUID_SIZE != 16 344 #error "UUID items require BTRFS_UUID_SIZE == 16!" 345 #endif 346 #define BTRFS_UUID_KEY_SUBVOL 251 /* for UUIDs assigned to subvols */ 347 #define BTRFS_UUID_KEY_RECEIVED_SUBVOL 252 /* for UUIDs assigned to 348 * received subvols */ 349 350 /* 351 * string items are for debugging. They just store a short string of 352 * data in the FS 353 */ 354 #define BTRFS_STRING_ITEM_KEY 253 355 356 /* Maximum metadata block size (nodesize) */ 357 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536 358 359 /* 32 bytes in various csum fields */ 360 #define BTRFS_CSUM_SIZE 32 361 362 /* csum types */ 363 enum btrfs_csum_type { 364 BTRFS_CSUM_TYPE_CRC32 = 0, 365 BTRFS_CSUM_TYPE_XXHASH = 1, 366 BTRFS_CSUM_TYPE_SHA256 = 2, 367 BTRFS_CSUM_TYPE_BLAKE2 = 3, 368 }; 369 370 /* 371 * flags definitions for directory entry item type 372 * 373 * Used by: 374 * struct btrfs_dir_item.type 375 * 376 * Values 0..7 must match common file type values in fs_types.h. 377 */ 378 #define BTRFS_FT_UNKNOWN 0 379 #define BTRFS_FT_REG_FILE 1 380 #define BTRFS_FT_DIR 2 381 #define BTRFS_FT_CHRDEV 3 382 #define BTRFS_FT_BLKDEV 4 383 #define BTRFS_FT_FIFO 5 384 #define BTRFS_FT_SOCK 6 385 #define BTRFS_FT_SYMLINK 7 386 #define BTRFS_FT_XATTR 8 387 #define BTRFS_FT_MAX 9 388 /* Directory contains encrypted data */ 389 #define BTRFS_FT_ENCRYPTED 0x80 390 391 static inline __u8 btrfs_dir_flags_to_ftype(__u8 flags) 392 { 393 return flags & ~BTRFS_FT_ENCRYPTED; 394 } 395 396 /* 397 * Inode flags 398 */ 399 #define BTRFS_INODE_NODATASUM (1U << 0) 400 #define BTRFS_INODE_NODATACOW (1U << 1) 401 #define BTRFS_INODE_READONLY (1U << 2) 402 #define BTRFS_INODE_NOCOMPRESS (1U << 3) 403 #define BTRFS_INODE_PREALLOC (1U << 4) 404 #define BTRFS_INODE_SYNC (1U << 5) 405 #define BTRFS_INODE_IMMUTABLE (1U << 6) 406 #define BTRFS_INODE_APPEND (1U << 7) 407 #define BTRFS_INODE_NODUMP (1U << 8) 408 #define BTRFS_INODE_NOATIME (1U << 9) 409 #define BTRFS_INODE_DIRSYNC (1U << 10) 410 #define BTRFS_INODE_COMPRESS (1U << 11) 411 412 #define BTRFS_INODE_ROOT_ITEM_INIT (1U << 31) 413 414 #define BTRFS_INODE_FLAG_MASK \ 415 (BTRFS_INODE_NODATASUM | \ 416 BTRFS_INODE_NODATACOW | \ 417 BTRFS_INODE_READONLY | \ 418 BTRFS_INODE_NOCOMPRESS | \ 419 BTRFS_INODE_PREALLOC | \ 420 BTRFS_INODE_SYNC | \ 421 BTRFS_INODE_IMMUTABLE | \ 422 BTRFS_INODE_APPEND | \ 423 BTRFS_INODE_NODUMP | \ 424 BTRFS_INODE_NOATIME | \ 425 BTRFS_INODE_DIRSYNC | \ 426 BTRFS_INODE_COMPRESS | \ 427 BTRFS_INODE_ROOT_ITEM_INIT) 428 429 #define BTRFS_INODE_RO_VERITY (1U << 0) 430 431 #define BTRFS_INODE_RO_FLAG_MASK (BTRFS_INODE_RO_VERITY) 432 433 /* 434 * The key defines the order in the tree, and so it also defines (optimal) 435 * block layout. 436 * 437 * objectid corresponds to the inode number. 438 * 439 * type tells us things about the object, and is a kind of stream selector. 440 * so for a given inode, keys with type of 1 might refer to the inode data, 441 * type of 2 may point to file data in the btree and type == 3 may point to 442 * extents. 443 * 444 * offset is the starting byte offset for this key in the stream. 445 * 446 * btrfs_disk_key is in disk byte order. struct btrfs_key is always 447 * in cpu native order. Otherwise they are identical and their sizes 448 * should be the same (ie both packed) 449 */ 450 struct btrfs_disk_key { 451 __le64 objectid; 452 __u8 type; 453 __le64 offset; 454 } __attribute__ ((__packed__)); 455 456 struct btrfs_key { 457 __u64 objectid; 458 __u8 type; 459 __u64 offset; 460 } __attribute__ ((__packed__)); 461 462 /* 463 * Every tree block (leaf or node) starts with this header. 464 */ 465 struct btrfs_header { 466 /* These first four must match the super block */ 467 __u8 csum[BTRFS_CSUM_SIZE]; 468 /* FS specific uuid */ 469 __u8 fsid[BTRFS_FSID_SIZE]; 470 /* Which block this node is supposed to live in */ 471 __le64 bytenr; 472 __le64 flags; 473 474 /* Allowed to be different from the super from here on down */ 475 __u8 chunk_tree_uuid[BTRFS_UUID_SIZE]; 476 __le64 generation; 477 __le64 owner; 478 __le32 nritems; 479 __u8 level; 480 } __attribute__ ((__packed__)); 481 482 /* 483 * This is a very generous portion of the super block, giving us room to 484 * translate 14 chunks with 3 stripes each. 485 */ 486 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048 487 488 /* 489 * Just in case we somehow lose the roots and are not able to mount, we store 490 * an array of the roots from previous transactions in the super. 491 */ 492 #define BTRFS_NUM_BACKUP_ROOTS 4 493 struct btrfs_root_backup { 494 __le64 tree_root; 495 __le64 tree_root_gen; 496 497 __le64 chunk_root; 498 __le64 chunk_root_gen; 499 500 __le64 extent_root; 501 __le64 extent_root_gen; 502 503 __le64 fs_root; 504 __le64 fs_root_gen; 505 506 __le64 dev_root; 507 __le64 dev_root_gen; 508 509 __le64 csum_root; 510 __le64 csum_root_gen; 511 512 __le64 total_bytes; 513 __le64 bytes_used; 514 __le64 num_devices; 515 /* future */ 516 __le64 unused_64[4]; 517 518 __u8 tree_root_level; 519 __u8 chunk_root_level; 520 __u8 extent_root_level; 521 __u8 fs_root_level; 522 __u8 dev_root_level; 523 __u8 csum_root_level; 524 /* future and to align */ 525 __u8 unused_8[10]; 526 } __attribute__ ((__packed__)); 527 528 /* 529 * A leaf is full of items. offset and size tell us where to find the item in 530 * the leaf (relative to the start of the data area) 531 */ 532 struct btrfs_item { 533 struct btrfs_disk_key key; 534 __le32 offset; 535 __le32 size; 536 } __attribute__ ((__packed__)); 537 538 /* 539 * Leaves have an item area and a data area: 540 * [item0, item1....itemN] [free space] [dataN...data1, data0] 541 * 542 * The data is separate from the items to get the keys closer together during 543 * searches. 544 */ 545 struct btrfs_leaf { 546 struct btrfs_header header; 547 struct btrfs_item items[]; 548 } __attribute__ ((__packed__)); 549 550 /* 551 * All non-leaf blocks are nodes, they hold only keys and pointers to other 552 * blocks. 553 */ 554 struct btrfs_key_ptr { 555 struct btrfs_disk_key key; 556 __le64 blockptr; 557 __le64 generation; 558 } __attribute__ ((__packed__)); 559 560 struct btrfs_node { 561 struct btrfs_header header; 562 struct btrfs_key_ptr ptrs[]; 563 } __attribute__ ((__packed__)); 564 565 struct btrfs_dev_item { 566 /* the internal btrfs device id */ 567 __le64 devid; 568 569 /* size of the device */ 570 __le64 total_bytes; 571 572 /* bytes used */ 573 __le64 bytes_used; 574 575 /* optimal io alignment for this device */ 576 __le32 io_align; 577 578 /* optimal io width for this device */ 579 __le32 io_width; 580 581 /* minimal io size for this device */ 582 __le32 sector_size; 583 584 /* type and info about this device */ 585 __le64 type; 586 587 /* expected generation for this device */ 588 __le64 generation; 589 590 /* 591 * starting byte of this partition on the device, 592 * to allow for stripe alignment in the future 593 */ 594 __le64 start_offset; 595 596 /* grouping information for allocation decisions */ 597 __le32 dev_group; 598 599 /* seek speed 0-100 where 100 is fastest */ 600 __u8 seek_speed; 601 602 /* bandwidth 0-100 where 100 is fastest */ 603 __u8 bandwidth; 604 605 /* btrfs generated uuid for this device */ 606 __u8 uuid[BTRFS_UUID_SIZE]; 607 608 /* uuid of FS who owns this device */ 609 __u8 fsid[BTRFS_UUID_SIZE]; 610 } __attribute__ ((__packed__)); 611 612 struct btrfs_stripe { 613 __le64 devid; 614 __le64 offset; 615 __u8 dev_uuid[BTRFS_UUID_SIZE]; 616 } __attribute__ ((__packed__)); 617 618 struct btrfs_chunk { 619 /* size of this chunk in bytes */ 620 __le64 length; 621 622 /* objectid of the root referencing this chunk */ 623 __le64 owner; 624 625 __le64 stripe_len; 626 __le64 type; 627 628 /* optimal io alignment for this chunk */ 629 __le32 io_align; 630 631 /* optimal io width for this chunk */ 632 __le32 io_width; 633 634 /* minimal io size for this chunk */ 635 __le32 sector_size; 636 637 /* 2^16 stripes is quite a lot, a second limit is the size of a single 638 * item in the btree 639 */ 640 __le16 num_stripes; 641 642 /* sub stripes only matter for raid10 */ 643 __le16 sub_stripes; 644 struct btrfs_stripe stripe; 645 /* additional stripes go here */ 646 } __attribute__ ((__packed__)); 647 648 /* 649 * The super block basically lists the main trees of the FS. 650 */ 651 struct btrfs_super_block { 652 /* The first 4 fields must match struct btrfs_header */ 653 __u8 csum[BTRFS_CSUM_SIZE]; 654 /* FS specific UUID, visible to user */ 655 __u8 fsid[BTRFS_FSID_SIZE]; 656 /* This block number */ 657 __le64 bytenr; 658 __le64 flags; 659 660 /* Allowed to be different from the btrfs_header from here own down */ 661 __le64 magic; 662 __le64 generation; 663 __le64 root; 664 __le64 chunk_root; 665 __le64 log_root; 666 667 /* 668 * This member has never been utilized since the very beginning, thus 669 * it's always 0 regardless of kernel version. We always use 670 * generation + 1 to read log tree root. So here we mark it deprecated. 671 */ 672 __le64 __unused_log_root_transid; 673 __le64 total_bytes; 674 __le64 bytes_used; 675 __le64 root_dir_objectid; 676 __le64 num_devices; 677 __le32 sectorsize; 678 __le32 nodesize; 679 __le32 __unused_leafsize; 680 __le32 stripesize; 681 __le32 sys_chunk_array_size; 682 __le64 chunk_root_generation; 683 __le64 compat_flags; 684 __le64 compat_ro_flags; 685 __le64 incompat_flags; 686 __le16 csum_type; 687 __u8 root_level; 688 __u8 chunk_root_level; 689 __u8 log_root_level; 690 struct btrfs_dev_item dev_item; 691 692 char label[BTRFS_LABEL_SIZE]; 693 694 __le64 cache_generation; 695 __le64 uuid_tree_generation; 696 697 /* The UUID written into btree blocks */ 698 __u8 metadata_uuid[BTRFS_FSID_SIZE]; 699 700 __u64 nr_global_roots; 701 702 /* Future expansion */ 703 __le64 reserved[27]; 704 __u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE]; 705 struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS]; 706 707 /* Padded to 4096 bytes */ 708 __u8 padding[565]; 709 } __attribute__ ((__packed__)); 710 711 #define BTRFS_FREE_SPACE_EXTENT 1 712 #define BTRFS_FREE_SPACE_BITMAP 2 713 714 struct btrfs_free_space_entry { 715 __le64 offset; 716 __le64 bytes; 717 __u8 type; 718 } __attribute__ ((__packed__)); 719 720 struct btrfs_free_space_header { 721 struct btrfs_disk_key location; 722 __le64 generation; 723 __le64 num_entries; 724 __le64 num_bitmaps; 725 } __attribute__ ((__packed__)); 726 727 struct btrfs_raid_stride { 728 /* The id of device this raid extent lives on. */ 729 __le64 devid; 730 /* The physical location on disk. */ 731 __le64 physical; 732 } __attribute__ ((__packed__)); 733 734 /* The stripe_extent::encoding, 1:1 mapping of enum btrfs_raid_types. */ 735 #define BTRFS_STRIPE_RAID0 1 736 #define BTRFS_STRIPE_RAID1 2 737 #define BTRFS_STRIPE_DUP 3 738 #define BTRFS_STRIPE_RAID10 4 739 #define BTRFS_STRIPE_RAID5 5 740 #define BTRFS_STRIPE_RAID6 6 741 #define BTRFS_STRIPE_RAID1C3 7 742 #define BTRFS_STRIPE_RAID1C4 8 743 744 struct btrfs_stripe_extent { 745 __u8 encoding; 746 __u8 reserved[7]; 747 /* An array of raid strides this stripe is composed of. */ 748 struct btrfs_raid_stride strides[]; 749 } __attribute__ ((__packed__)); 750 751 #define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0) 752 #define BTRFS_HEADER_FLAG_RELOC (1ULL << 1) 753 754 /* Super block flags */ 755 /* Errors detected */ 756 #define BTRFS_SUPER_FLAG_ERROR (1ULL << 2) 757 758 #define BTRFS_SUPER_FLAG_SEEDING (1ULL << 32) 759 #define BTRFS_SUPER_FLAG_METADUMP (1ULL << 33) 760 #define BTRFS_SUPER_FLAG_METADUMP_V2 (1ULL << 34) 761 #define BTRFS_SUPER_FLAG_CHANGING_FSID (1ULL << 35) 762 #define BTRFS_SUPER_FLAG_CHANGING_FSID_V2 (1ULL << 36) 763 764 765 /* 766 * items in the extent btree are used to record the objectid of the 767 * owner of the block and the number of references 768 */ 769 770 struct btrfs_extent_item { 771 __le64 refs; 772 __le64 generation; 773 __le64 flags; 774 } __attribute__ ((__packed__)); 775 776 struct btrfs_extent_item_v0 { 777 __le32 refs; 778 } __attribute__ ((__packed__)); 779 780 781 #define BTRFS_EXTENT_FLAG_DATA (1ULL << 0) 782 #define BTRFS_EXTENT_FLAG_TREE_BLOCK (1ULL << 1) 783 784 /* following flags only apply to tree blocks */ 785 786 /* use full backrefs for extent pointers in the block */ 787 #define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8) 788 789 #define BTRFS_BACKREF_REV_MAX 256 790 #define BTRFS_BACKREF_REV_SHIFT 56 791 #define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \ 792 BTRFS_BACKREF_REV_SHIFT) 793 794 #define BTRFS_OLD_BACKREF_REV 0 795 #define BTRFS_MIXED_BACKREF_REV 1 796 797 /* 798 * this flag is only used internally by scrub and may be changed at any time 799 * it is only declared here to avoid collisions 800 */ 801 #define BTRFS_EXTENT_FLAG_SUPER (1ULL << 48) 802 803 struct btrfs_tree_block_info { 804 struct btrfs_disk_key key; 805 __u8 level; 806 } __attribute__ ((__packed__)); 807 808 struct btrfs_extent_data_ref { 809 __le64 root; 810 __le64 objectid; 811 __le64 offset; 812 __le32 count; 813 } __attribute__ ((__packed__)); 814 815 struct btrfs_shared_data_ref { 816 __le32 count; 817 } __attribute__ ((__packed__)); 818 819 struct btrfs_extent_inline_ref { 820 __u8 type; 821 __le64 offset; 822 } __attribute__ ((__packed__)); 823 824 /* dev extents record free space on individual devices. The owner 825 * field points back to the chunk allocation mapping tree that allocated 826 * the extent. The chunk tree uuid field is a way to double check the owner 827 */ 828 struct btrfs_dev_extent { 829 __le64 chunk_tree; 830 __le64 chunk_objectid; 831 __le64 chunk_offset; 832 __le64 length; 833 __u8 chunk_tree_uuid[BTRFS_UUID_SIZE]; 834 } __attribute__ ((__packed__)); 835 836 struct btrfs_inode_ref { 837 __le64 index; 838 __le16 name_len; 839 /* name goes here */ 840 } __attribute__ ((__packed__)); 841 842 struct btrfs_inode_extref { 843 __le64 parent_objectid; 844 __le64 index; 845 __le16 name_len; 846 __u8 name[]; 847 /* name goes here */ 848 } __attribute__ ((__packed__)); 849 850 struct btrfs_timespec { 851 __le64 sec; 852 __le32 nsec; 853 } __attribute__ ((__packed__)); 854 855 struct btrfs_inode_item { 856 /* nfs style generation number */ 857 __le64 generation; 858 /* transid that last touched this inode */ 859 __le64 transid; 860 __le64 size; 861 __le64 nbytes; 862 __le64 block_group; 863 __le32 nlink; 864 __le32 uid; 865 __le32 gid; 866 __le32 mode; 867 __le64 rdev; 868 __le64 flags; 869 870 /* modification sequence number for NFS */ 871 __le64 sequence; 872 873 /* 874 * a little future expansion, for more than this we can 875 * just grow the inode item and version it 876 */ 877 __le64 reserved[4]; 878 struct btrfs_timespec atime; 879 struct btrfs_timespec ctime; 880 struct btrfs_timespec mtime; 881 struct btrfs_timespec otime; 882 } __attribute__ ((__packed__)); 883 884 struct btrfs_dir_log_item { 885 __le64 end; 886 } __attribute__ ((__packed__)); 887 888 struct btrfs_dir_item { 889 struct btrfs_disk_key location; 890 __le64 transid; 891 __le16 data_len; 892 __le16 name_len; 893 __u8 type; 894 } __attribute__ ((__packed__)); 895 896 #define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0) 897 898 /* 899 * Internal in-memory flag that a subvolume has been marked for deletion but 900 * still visible as a directory 901 */ 902 #define BTRFS_ROOT_SUBVOL_DEAD (1ULL << 48) 903 904 struct btrfs_root_item { 905 struct btrfs_inode_item inode; 906 __le64 generation; 907 __le64 root_dirid; 908 __le64 bytenr; 909 __le64 byte_limit; 910 __le64 bytes_used; 911 __le64 last_snapshot; 912 __le64 flags; 913 __le32 refs; 914 struct btrfs_disk_key drop_progress; 915 __u8 drop_level; 916 __u8 level; 917 918 /* 919 * The following fields appear after subvol_uuids+subvol_times 920 * were introduced. 921 */ 922 923 /* 924 * This generation number is used to test if the new fields are valid 925 * and up to date while reading the root item. Every time the root item 926 * is written out, the "generation" field is copied into this field. If 927 * anyone ever mounted the fs with an older kernel, we will have 928 * mismatching generation values here and thus must invalidate the 929 * new fields. See btrfs_update_root and btrfs_find_last_root for 930 * details. 931 * the offset of generation_v2 is also used as the start for the memset 932 * when invalidating the fields. 933 */ 934 __le64 generation_v2; 935 __u8 uuid[BTRFS_UUID_SIZE]; 936 __u8 parent_uuid[BTRFS_UUID_SIZE]; 937 __u8 received_uuid[BTRFS_UUID_SIZE]; 938 __le64 ctransid; /* updated when an inode changes */ 939 __le64 otransid; /* trans when created */ 940 __le64 stransid; /* trans when sent. non-zero for received subvol */ 941 __le64 rtransid; /* trans when received. non-zero for received subvol */ 942 struct btrfs_timespec ctime; 943 struct btrfs_timespec otime; 944 struct btrfs_timespec stime; 945 struct btrfs_timespec rtime; 946 __le64 reserved[8]; /* for future */ 947 } __attribute__ ((__packed__)); 948 949 /* 950 * Btrfs root item used to be smaller than current size. The old format ends 951 * at where member generation_v2 is. 952 */ 953 static inline __u32 btrfs_legacy_root_item_size(void) 954 { 955 return offsetof(struct btrfs_root_item, generation_v2); 956 } 957 958 /* 959 * this is used for both forward and backward root refs 960 */ 961 struct btrfs_root_ref { 962 __le64 dirid; 963 __le64 sequence; 964 __le16 name_len; 965 } __attribute__ ((__packed__)); 966 967 struct btrfs_disk_balance_args { 968 /* 969 * profiles to operate on, single is denoted by 970 * BTRFS_AVAIL_ALLOC_BIT_SINGLE 971 */ 972 __le64 profiles; 973 974 /* 975 * usage filter 976 * BTRFS_BALANCE_ARGS_USAGE with a single value means '0..N' 977 * BTRFS_BALANCE_ARGS_USAGE_RANGE - range syntax, min..max 978 */ 979 union { 980 __le64 usage; 981 struct { 982 __le32 usage_min; 983 __le32 usage_max; 984 }; 985 }; 986 987 /* devid filter */ 988 __le64 devid; 989 990 /* devid subset filter [pstart..pend) */ 991 __le64 pstart; 992 __le64 pend; 993 994 /* btrfs virtual address space subset filter [vstart..vend) */ 995 __le64 vstart; 996 __le64 vend; 997 998 /* 999 * profile to convert to, single is denoted by 1000 * BTRFS_AVAIL_ALLOC_BIT_SINGLE 1001 */ 1002 __le64 target; 1003 1004 /* BTRFS_BALANCE_ARGS_* */ 1005 __le64 flags; 1006 1007 /* 1008 * BTRFS_BALANCE_ARGS_LIMIT with value 'limit' 1009 * BTRFS_BALANCE_ARGS_LIMIT_RANGE - the extend version can use minimum 1010 * and maximum 1011 */ 1012 union { 1013 __le64 limit; 1014 struct { 1015 __le32 limit_min; 1016 __le32 limit_max; 1017 }; 1018 }; 1019 1020 /* 1021 * Process chunks that cross stripes_min..stripes_max devices, 1022 * BTRFS_BALANCE_ARGS_STRIPES_RANGE 1023 */ 1024 __le32 stripes_min; 1025 __le32 stripes_max; 1026 1027 __le64 unused[6]; 1028 } __attribute__ ((__packed__)); 1029 1030 /* 1031 * store balance parameters to disk so that balance can be properly 1032 * resumed after crash or unmount 1033 */ 1034 struct btrfs_balance_item { 1035 /* BTRFS_BALANCE_* */ 1036 __le64 flags; 1037 1038 struct btrfs_disk_balance_args data; 1039 struct btrfs_disk_balance_args meta; 1040 struct btrfs_disk_balance_args sys; 1041 1042 __le64 unused[4]; 1043 } __attribute__ ((__packed__)); 1044 1045 enum { 1046 BTRFS_FILE_EXTENT_INLINE = 0, 1047 BTRFS_FILE_EXTENT_REG = 1, 1048 BTRFS_FILE_EXTENT_PREALLOC = 2, 1049 BTRFS_NR_FILE_EXTENT_TYPES = 3, 1050 }; 1051 1052 struct btrfs_file_extent_item { 1053 /* 1054 * transaction id that created this extent 1055 */ 1056 __le64 generation; 1057 /* 1058 * max number of bytes to hold this extent in ram 1059 * when we split a compressed extent we can't know how big 1060 * each of the resulting pieces will be. So, this is 1061 * an upper limit on the size of the extent in ram instead of 1062 * an exact limit. 1063 */ 1064 __le64 ram_bytes; 1065 1066 /* 1067 * 32 bits for the various ways we might encode the data, 1068 * including compression and encryption. If any of these 1069 * are set to something a given disk format doesn't understand 1070 * it is treated like an incompat flag for reading and writing, 1071 * but not for stat. 1072 */ 1073 __u8 compression; 1074 __u8 encryption; 1075 __le16 other_encoding; /* spare for later use */ 1076 1077 /* are we inline data or a real extent? */ 1078 __u8 type; 1079 1080 /* 1081 * disk space consumed by the extent, checksum blocks are included 1082 * in these numbers 1083 * 1084 * At this offset in the structure, the inline extent data start. 1085 */ 1086 __le64 disk_bytenr; 1087 __le64 disk_num_bytes; 1088 /* 1089 * the logical offset in file blocks (no csums) 1090 * this extent record is for. This allows a file extent to point 1091 * into the middle of an existing extent on disk, sharing it 1092 * between two snapshots (useful if some bytes in the middle of the 1093 * extent have changed 1094 */ 1095 __le64 offset; 1096 /* 1097 * the logical number of file blocks (no csums included). This 1098 * always reflects the size uncompressed and without encoding. 1099 */ 1100 __le64 num_bytes; 1101 1102 } __attribute__ ((__packed__)); 1103 1104 struct btrfs_csum_item { 1105 __u8 csum; 1106 } __attribute__ ((__packed__)); 1107 1108 struct btrfs_dev_stats_item { 1109 /* 1110 * grow this item struct at the end for future enhancements and keep 1111 * the existing values unchanged 1112 */ 1113 __le64 values[BTRFS_DEV_STAT_VALUES_MAX]; 1114 } __attribute__ ((__packed__)); 1115 1116 #define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS 0 1117 #define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID 1 1118 1119 struct btrfs_dev_replace_item { 1120 /* 1121 * grow this item struct at the end for future enhancements and keep 1122 * the existing values unchanged 1123 */ 1124 __le64 src_devid; 1125 __le64 cursor_left; 1126 __le64 cursor_right; 1127 __le64 cont_reading_from_srcdev_mode; 1128 1129 __le64 replace_state; 1130 __le64 time_started; 1131 __le64 time_stopped; 1132 __le64 num_write_errors; 1133 __le64 num_uncorrectable_read_errors; 1134 } __attribute__ ((__packed__)); 1135 1136 /* different types of block groups (and chunks) */ 1137 #define BTRFS_BLOCK_GROUP_DATA (1ULL << 0) 1138 #define BTRFS_BLOCK_GROUP_SYSTEM (1ULL << 1) 1139 #define BTRFS_BLOCK_GROUP_METADATA (1ULL << 2) 1140 #define BTRFS_BLOCK_GROUP_RAID0 (1ULL << 3) 1141 #define BTRFS_BLOCK_GROUP_RAID1 (1ULL << 4) 1142 #define BTRFS_BLOCK_GROUP_DUP (1ULL << 5) 1143 #define BTRFS_BLOCK_GROUP_RAID10 (1ULL << 6) 1144 #define BTRFS_BLOCK_GROUP_RAID5 (1ULL << 7) 1145 #define BTRFS_BLOCK_GROUP_RAID6 (1ULL << 8) 1146 #define BTRFS_BLOCK_GROUP_RAID1C3 (1ULL << 9) 1147 #define BTRFS_BLOCK_GROUP_RAID1C4 (1ULL << 10) 1148 #define BTRFS_BLOCK_GROUP_RESERVED (BTRFS_AVAIL_ALLOC_BIT_SINGLE | \ 1149 BTRFS_SPACE_INFO_GLOBAL_RSV) 1150 1151 #define BTRFS_BLOCK_GROUP_TYPE_MASK (BTRFS_BLOCK_GROUP_DATA | \ 1152 BTRFS_BLOCK_GROUP_SYSTEM | \ 1153 BTRFS_BLOCK_GROUP_METADATA) 1154 1155 #define BTRFS_BLOCK_GROUP_PROFILE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \ 1156 BTRFS_BLOCK_GROUP_RAID1 | \ 1157 BTRFS_BLOCK_GROUP_RAID1C3 | \ 1158 BTRFS_BLOCK_GROUP_RAID1C4 | \ 1159 BTRFS_BLOCK_GROUP_RAID5 | \ 1160 BTRFS_BLOCK_GROUP_RAID6 | \ 1161 BTRFS_BLOCK_GROUP_DUP | \ 1162 BTRFS_BLOCK_GROUP_RAID10) 1163 #define BTRFS_BLOCK_GROUP_RAID56_MASK (BTRFS_BLOCK_GROUP_RAID5 | \ 1164 BTRFS_BLOCK_GROUP_RAID6) 1165 1166 #define BTRFS_BLOCK_GROUP_RAID1_MASK (BTRFS_BLOCK_GROUP_RAID1 | \ 1167 BTRFS_BLOCK_GROUP_RAID1C3 | \ 1168 BTRFS_BLOCK_GROUP_RAID1C4) 1169 1170 /* 1171 * We need a bit for restriper to be able to tell when chunks of type 1172 * SINGLE are available. This "extended" profile format is used in 1173 * fs_info->avail_*_alloc_bits (in-memory) and balance item fields 1174 * (on-disk). The corresponding on-disk bit in chunk.type is reserved 1175 * to avoid remappings between two formats in future. 1176 */ 1177 #define BTRFS_AVAIL_ALLOC_BIT_SINGLE (1ULL << 48) 1178 1179 /* 1180 * A fake block group type that is used to communicate global block reserve 1181 * size to userspace via the SPACE_INFO ioctl. 1182 */ 1183 #define BTRFS_SPACE_INFO_GLOBAL_RSV (1ULL << 49) 1184 1185 #define BTRFS_EXTENDED_PROFILE_MASK (BTRFS_BLOCK_GROUP_PROFILE_MASK | \ 1186 BTRFS_AVAIL_ALLOC_BIT_SINGLE) 1187 1188 static inline __u64 chunk_to_extended(__u64 flags) 1189 { 1190 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0) 1191 flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE; 1192 1193 return flags; 1194 } 1195 static inline __u64 extended_to_chunk(__u64 flags) 1196 { 1197 return flags & ~BTRFS_AVAIL_ALLOC_BIT_SINGLE; 1198 } 1199 1200 struct btrfs_block_group_item { 1201 __le64 used; 1202 __le64 chunk_objectid; 1203 __le64 flags; 1204 } __attribute__ ((__packed__)); 1205 1206 struct btrfs_free_space_info { 1207 __le32 extent_count; 1208 __le32 flags; 1209 } __attribute__ ((__packed__)); 1210 1211 #define BTRFS_FREE_SPACE_USING_BITMAPS (1ULL << 0) 1212 1213 #define BTRFS_QGROUP_LEVEL_SHIFT 48 1214 static inline __u16 btrfs_qgroup_level(__u64 qgroupid) 1215 { 1216 return (__u16)(qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT); 1217 } 1218 1219 /* 1220 * is subvolume quota turned on? 1221 */ 1222 #define BTRFS_QGROUP_STATUS_FLAG_ON (1ULL << 0) 1223 /* 1224 * RESCAN is set during the initialization phase 1225 */ 1226 #define BTRFS_QGROUP_STATUS_FLAG_RESCAN (1ULL << 1) 1227 /* 1228 * Some qgroup entries are known to be out of date, 1229 * either because the configuration has changed in a way that 1230 * makes a rescan necessary, or because the fs has been mounted 1231 * with a non-qgroup-aware version. 1232 * Turning qouta off and on again makes it inconsistent, too. 1233 */ 1234 #define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2) 1235 1236 #define BTRFS_QGROUP_STATUS_FLAGS_MASK (BTRFS_QGROUP_STATUS_FLAG_ON | \ 1237 BTRFS_QGROUP_STATUS_FLAG_RESCAN | \ 1238 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) 1239 1240 #define BTRFS_QGROUP_STATUS_VERSION 1 1241 1242 struct btrfs_qgroup_status_item { 1243 __le64 version; 1244 /* 1245 * the generation is updated during every commit. As older 1246 * versions of btrfs are not aware of qgroups, it will be 1247 * possible to detect inconsistencies by checking the 1248 * generation on mount time 1249 */ 1250 __le64 generation; 1251 1252 /* flag definitions see above */ 1253 __le64 flags; 1254 1255 /* 1256 * only used during scanning to record the progress 1257 * of the scan. It contains a logical address 1258 */ 1259 __le64 rescan; 1260 } __attribute__ ((__packed__)); 1261 1262 struct btrfs_qgroup_info_item { 1263 __le64 generation; 1264 __le64 rfer; 1265 __le64 rfer_cmpr; 1266 __le64 excl; 1267 __le64 excl_cmpr; 1268 } __attribute__ ((__packed__)); 1269 1270 struct btrfs_qgroup_limit_item { 1271 /* 1272 * only updated when any of the other values change 1273 */ 1274 __le64 flags; 1275 __le64 max_rfer; 1276 __le64 max_excl; 1277 __le64 rsv_rfer; 1278 __le64 rsv_excl; 1279 } __attribute__ ((__packed__)); 1280 1281 struct btrfs_verity_descriptor_item { 1282 /* Size of the verity descriptor in bytes */ 1283 __le64 size; 1284 /* 1285 * When we implement support for fscrypt, we will need to encrypt the 1286 * Merkle tree for encrypted verity files. These 128 bits are for the 1287 * eventual storage of an fscrypt initialization vector. 1288 */ 1289 __le64 reserved[2]; 1290 __u8 encryption; 1291 } __attribute__ ((__packed__)); 1292 1293 #endif /* _BTRFS_CTREE_H_ */ 1294