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