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