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