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