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