1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 
19 /// D translation of ctree.h from btrfs-progs (v5.9)
20 module btrfs.c.kernel_shared.ctree;
21 
22 import std.bitmanip;
23 
24 import btrfs.c.dcompat;
25 import btrfs.c.kerncompat;
26 import btrfs.c.kernel_lib.list;
27 import btrfs.c.kernel_lib.rbtree;
28 import btrfs.c.kernel_lib.sizes;
29 
30 import btrfs.c.common.extent_cache;
31 import btrfs.c.kernel_shared.extent_io;
32 import btrfs.c.ioctl;
33 
34 struct btrfs_trans_handle;
35 struct btrfs_free_space_ctl;
36 enum BTRFS_MAGIC = 0x4D5F53665248425FUL /* ascii _BHRfS_M, no null */;
37 /*
38  * Fake signature for an unfinalized filesystem, which only has barebone tree
39  * structures (normally 6 near empty trees, on SINGLE meta/sys temporary chunks)
40  *
41  * ascii !BHRfS_M, no null
42  */
43 enum BTRFS_MAGIC_TEMPORARY = 0x4D5F536652484221UL;
44 enum BTRFS_MAX_MIRRORS = 3;
45 enum BTRFS_MAX_LEVEL = 8;
46 /* holds pointers to all of the tree roots */
47 enum BTRFS_ROOT_TREE_OBJECTID = 1UL;
48 /* stores information about which extents are in use, and reference counts */
49 enum BTRFS_EXTENT_TREE_OBJECTID = 2UL;
50 /*
51  * chunk tree stores translations from logical -> physical block numbering
52  * the super block points to the chunk tree
53  */
54 enum BTRFS_CHUNK_TREE_OBJECTID = 3UL;
55 /*
56  * stores information about which areas of a given device are in use.
57  * one per device.  The tree of tree roots points to the device tree
58  */
59 enum BTRFS_DEV_TREE_OBJECTID = 4UL;
60 /* one per subvolume, storing files and directories */
61 enum BTRFS_FS_TREE_OBJECTID = 5UL;
62 /* directory objectid inside the root tree */
63 enum BTRFS_ROOT_TREE_DIR_OBJECTID = 6UL;
64 /* holds checksums of all the data extents */
65 enum BTRFS_CSUM_TREE_OBJECTID = 7UL;
66 enum BTRFS_QUOTA_TREE_OBJECTID = 8UL;
67 /* for storing items that use the BTRFS_UUID_KEY* */
68 enum BTRFS_UUID_TREE_OBJECTID = 9UL;
69 /* tracks free space in block groups. */
70 enum BTRFS_FREE_SPACE_TREE_OBJECTID = 10UL;
71 /* device stats in the device tree */
72 enum BTRFS_DEV_STATS_OBJECTID = 0UL;
73 /* for storing balance parameters in the root tree */
74 enum BTRFS_BALANCE_OBJECTID = -4UL;
75 /* orphan objectid for tracking unlinked/truncated files */
76 enum BTRFS_ORPHAN_OBJECTID = -5UL;
77 /* does write ahead logging to speed up fsyncs */
78 enum BTRFS_TREE_LOG_OBJECTID = -6UL;
79 enum BTRFS_TREE_LOG_FIXUP_OBJECTID = -7UL;
80 /* space balancing */
81 enum BTRFS_TREE_RELOC_OBJECTID = -8UL;
82 enum BTRFS_DATA_RELOC_TREE_OBJECTID = -9UL;
83 /*
84  * extent checksums all have this objectid
85  * this allows them to share the logging tree
86  * for fsyncs
87  */
88 enum BTRFS_EXTENT_CSUM_OBJECTID = -10UL;
89 /* For storing free space cache */
90 enum BTRFS_FREE_SPACE_OBJECTID = -11UL;
91 /*
92  * The inode number assigned to the special inode for storing
93  * free ino cache
94  */
95 enum BTRFS_FREE_INO_OBJECTID = -12UL;
96 /* dummy objectid represents multiple objectids */
97 enum BTRFS_MULTIPLE_OBJECTIDS = -255UL;
98 /*
99  * All files have objectids in this range.
100  */
101 enum BTRFS_FIRST_FREE_OBJECTID = 256UL;
102 enum BTRFS_LAST_FREE_OBJECTID = -256UL;
103 enum BTRFS_FIRST_CHUNK_TREE_OBJECTID = 256UL;
104 
105 
106 /*
107  * the device items go into the chunk tree.  The key is in the form
108  * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
109  */
110 enum BTRFS_DEV_ITEMS_OBJECTID = 1UL;
111 enum BTRFS_EMPTY_SUBVOL_DIR_OBJECTID = 2UL;
112 /*
113  * the max metadata block size.  This limit is somewhat artificial,
114  * but the memmove costs go through the roof for larger blocks.
115  */
116 enum BTRFS_MAX_METADATA_BLOCKSIZE = 65536;
117 /*
118  * we can actually store much bigger names, but lets not confuse the rest
119  * of linux
120  */
121 enum BTRFS_NAME_LEN = 255;
122 /*
123  * Theoretical limit is larger, but we keep this down to a sane
124  * value. That should limit greatly the possibility of collisions on
125  * inode ref items.
126  */
127 enum	BTRFS_LINK_MAX	= 65535U;
128 
129 /* 32 bytes in various csum fields */
130 enum BTRFS_CSUM_SIZE = 32;
131 /* csum types */
132 enum btrfs_csum_type {
133 	BTRFS_CSUM_TYPE_CRC32		= 0,
134 	BTRFS_CSUM_TYPE_XXHASH		= 1,
135 	BTRFS_CSUM_TYPE_SHA256		= 2,
136 	BTRFS_CSUM_TYPE_BLAKE2		= 3,
137 }
138 
139 enum BTRFS_EMPTY_DIR_SIZE = 0;
140 enum BTRFS_FT_UNKNOWN	= 0;
141 enum BTRFS_FT_REG_FILE	= 1;
142 enum BTRFS_FT_DIR		= 2;
143 enum BTRFS_FT_CHRDEV		= 3;
144 enum BTRFS_FT_BLKDEV		= 4;
145 enum BTRFS_FT_FIFO		= 5;
146 enum BTRFS_FT_SOCK		= 6;
147 enum BTRFS_FT_SYMLINK	= 7;
148 enum BTRFS_FT_XATTR		= 8;
149 enum BTRFS_FT_MAX		= 9;
150 enum BTRFS_ROOT_SUBVOL_RDONLY	= (1UL << 0);
151 /*
152  * the key defines the order in the tree, and so it also defines (optimal)
153  * block layout.  objectid corresponds to the inode number.  The flags
154  * tells us things about the object, and is a kind of stream selector.
155  * so for a given inode, keys with flags of 1 might refer to the inode
156  * data, flags of 2 may point to file data in the btree and flags == 3
157  * may point to extents.
158  *
159  * offset is the starting byte offset for this key in the stream.
160  *
161  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
162  * in cpu native order.  Otherwise they are identical and their sizes
163  * should be the same (ie both packed)
164  */
165 struct btrfs_disk_key {
166 align(1):
167 	__le64 objectid;
168 	u8 type;
169 	__le64 offset;
170 }
171 
172 struct btrfs_key {
173 align(1):
174 	u64 objectid;
175 	u8 type;
176 	u64 offset;
177 }
178 
179 struct btrfs_mapping_tree {
180 	.cache_tree cache_tree;
181 }
182 
183 enum BTRFS_UUID_SIZE = 16;
184 struct btrfs_dev_item {
185 align(1):
186 	/* the internal btrfs device id */
187 	__le64 devid;
188 
189 	/* size of the device */
190 	__le64 total_bytes;
191 
192 	/* bytes used */
193 	__le64 bytes_used;
194 
195 	/* optimal io alignment for this device */
196 	__le32 io_align;
197 
198 	/* optimal io width for this device */
199 	__le32 io_width;
200 
201 	/* minimal io size for this device */
202 	__le32 sector_size;
203 
204 	/* type and info about this device */
205 	__le64 type;
206 
207 	/* expected generation for this device */
208 	__le64 generation;
209 
210 	/*
211 	 * starting byte of this partition on the device,
212 	 * to allow for stripe alignment in the future
213 	 */
214 	__le64 start_offset;
215 
216 	/* grouping information for allocation decisions */
217 	__le32 dev_group;
218 
219 	/* seek speed 0-100 where 100 is fastest */
220 	u8 seek_speed;
221 
222 	/* bandwidth 0-100 where 100 is fastest */
223 	u8 bandwidth;
224 
225 	/* btrfs generated uuid for this device */
226 	u8[BTRFS_UUID_SIZE] uuid;
227 
228 	/* uuid of FS who owns this device */
229 	u8[BTRFS_UUID_SIZE] fsid;
230 }
231 
232 struct btrfs_stripe {
233 align(1):
234 	__le64 devid;
235 	__le64 offset;
236 	u8[BTRFS_UUID_SIZE] dev_uuid;
237 }
238 
239 struct btrfs_chunk {
240 align(1):
241 	/* size of this chunk in bytes */
242 	__le64 length;
243 
244 	/* objectid of the root referencing this chunk */
245 	__le64 owner;
246 
247 	__le64 stripe_len;
248 	__le64 type;
249 
250 	/* optimal io alignment for this chunk */
251 	__le32 io_align;
252 
253 	/* optimal io width for this chunk */
254 	__le32 io_width;
255 
256 	/* minimal io size for this chunk */
257 	__le32 sector_size;
258 
259 	/* 2^16 stripes is quite a lot, a second limit is the size of a single
260 	 * item in the btree
261 	 */
262 	__le16 num_stripes;
263 
264 	/* sub stripes only matter for raid10 */
265 	__le16 sub_stripes;
266 	btrfs_stripe[0] stripe;
267 	/* additional stripes go here */
268 }
269 
270 enum BTRFS_FREE_SPACE_EXTENT	= 1;
271 enum BTRFS_FREE_SPACE_BITMAP	= 2;
272 struct btrfs_free_space_entry {
273 	__le64 offset;
274 	__le64 bytes;
275 	u8 type;
276 }
277 
278 struct btrfs_free_space_header {
279 	btrfs_disk_key location;
280 	__le64 generation;
281 	__le64 num_entries;
282 	__le64 num_bitmaps;
283 }
284 
285 ulong btrfs_chunk_item_size(int num_stripes)
286 {
287 	assert(num_stripes != 0);
288 	return btrfs_chunk.sizeof +
289 		btrfs_stripe.sizeof * (num_stripes - 1);
290 }
291 
292 enum BTRFS_HEADER_FLAG_WRITTEN		= (1UL << 0);
293 enum BTRFS_HEADER_FLAG_RELOC			= (1UL << 1);
294 enum BTRFS_SUPER_FLAG_SEEDING		= (1UL << 32);
295 enum BTRFS_SUPER_FLAG_METADUMP		= (1UL << 33);
296 enum BTRFS_SUPER_FLAG_METADUMP_V2		= (1UL << 34);
297 enum BTRFS_SUPER_FLAG_CHANGING_FSID		= (1UL << 35);
298 enum BTRFS_SUPER_FLAG_CHANGING_FSID_V2	= (1UL << 36);
299 enum BTRFS_BACKREF_REV_MAX		= 256;
300 enum BTRFS_BACKREF_REV_SHIFT		= 56;
301 enum BTRFS_BACKREF_REV_MASK		= ((cast(u64)BTRFS_BACKREF_REV_MAX - 1) <<
302 					 BTRFS_BACKREF_REV_SHIFT);
303 enum BTRFS_OLD_BACKREF_REV		= 0;
304 enum BTRFS_MIXED_BACKREF_REV		= 1;
305 /*
306  * every tree block (leaf or node) starts with this header.
307  */
308 struct btrfs_header {
309 align(1):
310 	/* these first four must match the super block */
311 	u8[BTRFS_CSUM_SIZE] csum;
312 	u8[BTRFS_FSID_SIZE] fsid; /* FS specific uuid */
313 	__le64 bytenr; /* which block this node is supposed to live in */
314 	__le64 flags;
315 
316 	/* allowed to be different from the super from here on down */
317 	u8[BTRFS_UUID_SIZE] chunk_tree_uuid;
318 	__le64 generation;
319 	__le64 owner;
320 	__le32 nritems;
321 	u8 level;
322 }
323 
324 auto __BTRFS_LEAF_DATA_SIZE(T)(T bs) { return bs - btrfs_header.sizeof; }
325 auto BTRFS_LEAF_DATA_SIZE(T)(T fs_info) { return
326 				(__BTRFS_LEAF_DATA_SIZE(fs_info.nodesize)); }
327 
328 /*
329  * this is a very generous portion of the super block, giving us
330  * room to translate 14 chunks with 3 stripes each.
331  */
332 enum BTRFS_SYSTEM_CHUNK_ARRAY_SIZE = 2048;
333 enum BTRFS_LABEL_SIZE = 256;
334 /*
335  * just in case we somehow lose the roots and are not able to mount,
336  * we store an array of the roots from previous transactions
337  * in the super.
338  */
339 enum BTRFS_NUM_BACKUP_ROOTS = 4;
340 struct btrfs_root_backup {
341 align(1):
342 	__le64 tree_root;
343 	__le64 tree_root_gen;
344 
345 	__le64 chunk_root;
346 	__le64 chunk_root_gen;
347 
348 	__le64 extent_root;
349 	__le64 extent_root_gen;
350 
351 	__le64 fs_root;
352 	__le64 fs_root_gen;
353 
354 	__le64 dev_root;
355 	__le64 dev_root_gen;
356 
357 	__le64 csum_root;
358 	__le64 csum_root_gen;
359 
360 	__le64 total_bytes;
361 	__le64 bytes_used;
362 	__le64 num_devices;
363 	/* future */
364 	__le64[4] unsed_64;
365 
366 	u8 tree_root_level;
367 	u8 chunk_root_level;
368 	u8 extent_root_level;
369 	u8 fs_root_level;
370 	u8 dev_root_level;
371 	u8 csum_root_level;
372 	/* future and to align */
373 	u8[10] unused_8;
374 }
375 
376 /*
377  * the super block basically lists the main trees of the FS
378  * it currently lacks any block count etc etc
379  */
380 struct btrfs_super_block {
381 align(1):
382 	u8[BTRFS_CSUM_SIZE] csum;
383 	/* the first 3 fields must match struct btrfs_header */
384 	u8[BTRFS_FSID_SIZE] fsid;    /* FS specific uuid */
385 	__le64 bytenr; /* this block number */
386 	__le64 flags;
387 
388 	/* allowed to be different from the btrfs_header from here own down */
389 	__le64 magic;
390 	__le64 generation;
391 	__le64 root;
392 	__le64 chunk_root;
393 	__le64 log_root;
394 
395 	/* this will help find the new super based on the log root */
396 	__le64 log_root_transid;
397 	__le64 total_bytes;
398 	__le64 bytes_used;
399 	__le64 root_dir_objectid;
400 	__le64 num_devices;
401 	__le32 sectorsize;
402 	__le32 nodesize;
403 	/* Unused and must be equal to nodesize */
404 	__le32 __unused_leafsize;
405 	__le32 stripesize;
406 	__le32 sys_chunk_array_size;
407 	__le64 chunk_root_generation;
408 	__le64 compat_flags;
409 	__le64 compat_ro_flags;
410 	__le64 incompat_flags;
411 	__le16 csum_type;
412 	u8 root_level;
413 	u8 chunk_root_level;
414 	u8 log_root_level;
415 	btrfs_dev_item dev_item;
416 
417 	char[BTRFS_LABEL_SIZE] label;
418 
419 	__le64 cache_generation;
420 	__le64 uuid_tree_generation;
421 
422 	u8[BTRFS_FSID_SIZE] metadata_uuid;
423 	/* future expansion */
424 	__le64[28] reserved;
425 	u8[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE] sys_chunk_array;
426 	btrfs_root_backup[BTRFS_NUM_BACKUP_ROOTS] super_roots;
427 }
428 
429 /*
430  * Compat flags that we support.  If any incompat flags are set other than the
431  * ones specified below then we will fail to mount
432  */
433 enum BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE	= (1UL << 0);
434 /*
435  * Older kernels on big-endian systems produced broken free space tree bitmaps,
436  * and btrfs-progs also used to corrupt the free space tree. If this bit is
437  * clear, then the free space tree cannot be trusted. btrfs-progs can also
438  * intentionally clear this bit to ask the kernel to rebuild the free space
439  * tree.
440  */
441 enum BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID	= (1UL << 1);
442 enum BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF	= (1UL << 0);
443 enum BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL	= (1UL << 1);
444 enum BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS	= (1UL << 2);
445 enum BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO	= (1UL << 3);
446 enum BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD	= (1UL << 4);
447 /*
448  * older kernels tried to do bigger metadata blocks, but the
449  * code was pretty buggy.  Lets not let them try anymore.
450  */
451 enum BTRFS_FEATURE_INCOMPAT_BIG_METADATA     = (1UL << 5);
452 enum BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF	= (1UL << 6);
453 enum BTRFS_FEATURE_INCOMPAT_RAID56		= (1UL << 7);
454 enum BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA	= (1UL << 8);
455 enum BTRFS_FEATURE_INCOMPAT_NO_HOLES		= (1UL << 9);
456 enum BTRFS_FEATURE_INCOMPAT_METADATA_UUID    = (1UL << 10);
457 enum BTRFS_FEATURE_INCOMPAT_RAID1C34		= (1UL << 11);
458 enum BTRFS_FEATURE_COMPAT_SUPP		= 0UL;
459 /*
460  * The FREE_SPACE_TREE and FREE_SPACE_TREE_VALID compat_ro bits must not be
461  * added here until read-write support for the free space tree is implemented in
462  * btrfs-progs.
463  */
464 enum BTRFS_FEATURE_COMPAT_RO_SUPP			=
465 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |
466 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID);
467 enum BTRFS_FEATURE_INCOMPAT_SUPP			=
468 	(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |
469 	 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |
470 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |
471 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD |
472 	 BTRFS_FEATURE_INCOMPAT_BIG_METADATA |
473 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |
474 	 BTRFS_FEATURE_INCOMPAT_RAID56 |
475 	 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |
476 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |
477 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES |
478 	 BTRFS_FEATURE_INCOMPAT_RAID1C34 |
479 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
480 /*
481  * A leaf is full of items. offset and size tell us where to find
482  * the item in the leaf (relative to the start of the data area)
483  */
484 struct btrfs_item {
485 	align(1):
486 	btrfs_disk_key key;
487 	__le32 offset;
488 	__le32 size;
489 }
490 
491 /*
492  * leaves have an item area and a data area:
493  * [item0, item1....itemN] [free space] [dataN...data1, data0]
494  *
495  * The data is separate from the items to get the keys closer together
496  * during searches.
497  */
498 struct btrfs_leaf {
499 align(1):
500 	btrfs_header header;
501 	btrfs_item[0] items;
502 }
503 
504 /*
505  * all non-leaf blocks are nodes, they hold only keys and pointers to
506  * other blocks
507  */
508 struct btrfs_key_ptr {
509 align(1):
510 	btrfs_disk_key key;
511 	__le64 blockptr;
512 	__le64 generation;
513 }
514 
515 struct btrfs_node {
516 align(1):
517 	btrfs_header header;
518 	btrfs_key_ptr[0] ptrs;
519 }
520 
521 /*
522  * btrfs_paths remember the path taken from the root down to the leaf.
523  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
524  * to any other levels that are present.
525  *
526  * The slots array records the index of the item or block pointer
527  * used while walking the tree.
528  */
529 enum { READA_NONE = 0, READA_BACK, READA_FORWARD }
530 struct btrfs_path {
531 	extent_buffer*[BTRFS_MAX_LEVEL] nodes;
532 	int[BTRFS_MAX_LEVEL] slots;
533 version (none) {
534 	/* The kernel locking scheme is not done in userspace. */
535 	int[BTRFS_MAX_LEVEL] locks;
536 }
537 	byte reada;
538 	/* keep some upper locks as we walk down */
539 	u8 lowest_level;
540 
541 	/*
542 	 * set by btrfs_split_item, tells search_slot to keep all locks
543 	 * and to force calls to keep space in the nodes
544 	 */
545 	u8 search_for_split;
546 	u8 skip_check_block;
547 }
548 
549 /*
550  * items in the extent btree are used to record the objectid of the
551  * owner of the block and the number of references
552  */
553 
554 struct btrfs_extent_item {
555 align(1):
556 	__le64 refs;
557 	__le64 generation;
558 	__le64 flags;
559 }
560 
561 struct btrfs_extent_item_v0 {
562 align(1):
563 	__le32 refs;
564 }
565 
566 auto BTRFS_MAX_EXTENT_ITEM_SIZE(T)(T r) { return
567 			((BTRFS_LEAF_DATA_SIZE(r.fs_info) >> 4) -
568 					btrfs_item.sizeof); }
569 enum BTRFS_MAX_EXTENT_SIZE		= SZ_128M;
570 enum BTRFS_EXTENT_FLAG_DATA		= (1UL << 0);
571 enum BTRFS_EXTENT_FLAG_TREE_BLOCK	= (1UL << 1);
572 /* following flags only apply to tree blocks */
573 
574 /* use full backrefs for extent pointers in the block*/
575 enum BTRFS_BLOCK_FLAG_FULL_BACKREF	= (1UL << 8);
576 struct btrfs_tree_block_info {
577 align(1):
578 	btrfs_disk_key key;
579 	u8 level;
580 }
581 
582 struct btrfs_extent_data_ref {
583 align(1):
584 	__le64 root;
585 	__le64 objectid;
586 	__le64 offset;
587 	__le32 count;
588 }
589 
590 struct btrfs_shared_data_ref {
591 align(1):
592 	__le32 count;
593 }
594 
595 struct btrfs_extent_inline_ref {
596 align(1):
597 	u8 type;
598 	__le64 offset;
599 }
600 
601 struct btrfs_extent_ref_v0 {
602 align(1):
603 	__le64 root;
604 	__le64 generation;
605 	__le64 objectid;
606 	__le32 count;
607 }
608 
609 /* dev extents record free space on individual devices.  The owner
610  * field points back to the chunk allocation mapping tree that allocated
611  * the extent.  The chunk tree uuid field is a way to double check the owner
612  */
613 struct btrfs_dev_extent {
614 align(1):
615 	__le64 chunk_tree;
616 	__le64 chunk_objectid;
617 	__le64 chunk_offset;
618 	__le64 length;
619 	u8[BTRFS_UUID_SIZE] chunk_tree_uuid;
620 }
621 
622 struct btrfs_inode_ref {
623 align(1):
624 	__le64 index;
625 	__le16 name_len;
626 	/* name goes here */
627 }
628 
629 struct btrfs_inode_extref {
630 align(1):
631 	__le64 parent_objectid;
632 	__le64 index;
633 	__le16 name_len;
634 	__u8[0]   name; /* name goes here */
635 }
636 
637 struct btrfs_timespec {
638 align(1):
639 	__le64 sec;
640 	__le32 nsec;
641 }
642 
643 enum btrfs_compression_type {
644 	BTRFS_COMPRESS_NONE  = 0,
645 	BTRFS_COMPRESS_ZLIB  = 1,
646 	BTRFS_COMPRESS_LZO   = 2,
647 	BTRFS_COMPRESS_ZSTD  = 3,
648 	BTRFS_COMPRESS_TYPES = 3,
649 	BTRFS_COMPRESS_LAST  = 4,
650 }
651 
652 /* we don't understand any encryption methods right now */
653 enum btrfs_encryption_type {
654 	BTRFS_ENCRYPTION_NONE = 0,
655 	BTRFS_ENCRYPTION_LAST = 1,
656 }
657 
658 enum btrfs_tree_block_status {
659 	BTRFS_TREE_BLOCK_CLEAN,
660 	BTRFS_TREE_BLOCK_INVALID_NRITEMS,
661 	BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
662 	BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
663 	BTRFS_TREE_BLOCK_INVALID_LEVEL,
664 	BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
665 	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
666 }
667 
668 struct btrfs_inode_item {
669 align(1):
670 	/* nfs style generation number */
671 	__le64 generation;
672 	/* transid that last touched this inode */
673 	__le64 transid;
674 	__le64 size;
675 	__le64 nbytes;
676 	__le64 block_group;
677 	__le32 nlink;
678 	__le32 uid;
679 	__le32 gid;
680 	__le32 mode;
681 	__le64 rdev;
682 	__le64 flags;
683 
684 	/* modification sequence number for NFS */
685 	__le64 sequence;
686 
687 	/*
688 	 * a little future expansion, for more than this we can
689 	 * just grow the inode item and version it
690 	 */
691 	__le64[4] reserved;
692 	btrfs_timespec atime;
693 	btrfs_timespec ctime;
694 	btrfs_timespec mtime;
695 	btrfs_timespec otime;
696 }
697 
698 struct btrfs_dir_log_item {
699 align(1):
700 	__le64 end;
701 }
702 
703 struct btrfs_dir_item {
704 align(1):
705 	btrfs_disk_key location;
706 	__le64 transid;
707 	__le16 data_len;
708 	__le16 name_len;
709 	u8 type;
710 }
711 
712 struct btrfs_root_item_v0 {
713 align(1):
714 	btrfs_inode_item inode;
715 	__le64 generation;
716 	__le64 root_dirid;
717 	__le64 bytenr;
718 	__le64 byte_limit;
719 	__le64 bytes_used;
720 	__le64 last_snapshot;
721 	__le64 flags;
722 	__le32 refs;
723 	btrfs_disk_key drop_progress;
724 	u8 drop_level;
725 	u8 level;
726 }
727 
728 struct btrfs_root_item {
729 align(1):
730 	btrfs_inode_item inode;
731 	__le64 generation;
732 	__le64 root_dirid;
733 	__le64 bytenr;
734 	__le64 byte_limit;
735 	__le64 bytes_used;
736 	__le64 last_snapshot;
737 	__le64 flags;
738 	__le32 refs;
739 	btrfs_disk_key drop_progress;
740 	u8 drop_level;
741 	u8 level;
742 
743 	/*
744 	 * The following fields appear after subvol_uuids+subvol_times
745 	 * were introduced.
746 	 */
747 
748 	/*
749 	 * This generation number is used to test if the new fields are valid
750 	 * and up to date while reading the root item. Every time the root item
751 	 * is written out, the "generation" field is copied into this field. If
752 	 * anyone ever mounted the fs with an older kernel, we will have
753 	 * mismatching generation values here and thus must invalidate the
754 	 * new fields. See btrfs_update_root and btrfs_find_last_root for
755 	 * details.
756 	 * the offset of generation_v2 is also used as the start for the memset
757 	 * when invalidating the fields.
758 	 */
759 	__le64 generation_v2;
760 	u8[BTRFS_UUID_SIZE] uuid;
761 	u8[BTRFS_UUID_SIZE] parent_uuid;
762 	u8[BTRFS_UUID_SIZE] received_uuid;
763 	__le64 ctransid; /* updated when an inode changes */
764 	__le64 otransid; /* trans when created */
765 	__le64 stransid; /* trans when sent. non-zero for received subvol */
766 	__le64 rtransid; /* trans when received. non-zero for received subvol */
767 	btrfs_timespec ctime;
768 	btrfs_timespec otime;
769 	btrfs_timespec stime;
770 	btrfs_timespec rtime;
771         __le64[8] reserved; /* for future */
772 }
773 
774 /*
775  * this is used for both forward and backward root refs
776  */
777 struct btrfs_root_ref {
778 align(1):
779 	__le64 dirid;
780 	__le64 sequence;
781 	__le16 name_len;
782 }
783 
784 struct btrfs_disk_balance_args {
785 align(1):
786 	/*
787 	 * profiles to operate on, single is denoted by
788 	 * BTRFS_AVAIL_ALLOC_BIT_SINGLE
789 	 */
790 	__le64 profiles;
791 
792 	/*
793 	 * usage filter
794 	 * BTRFS_BALANCE_ARGS_USAGE with a single value means '0..N'
795 	 * BTRFS_BALANCE_ARGS_USAGE_RANGE - range syntax, min..max
796 	 */
797 	union {
798 		__le64 usage;
799 		struct {
800 			__le32 usage_min;
801 			__le32 usage_max;
802 		}
803 	}
804 
805 	/* devid filter */
806 	__le64 devid;
807 
808 	/* devid subset filter [pstart..pend) */
809 	__le64 pstart;
810 	__le64 pend;
811 
812 	/* btrfs virtual address space subset filter [vstart..vend) */
813 	__le64 vstart;
814 	__le64 vend;
815 
816 	/*
817 	 * profile to convert to, single is denoted by
818 	 * BTRFS_AVAIL_ALLOC_BIT_SINGLE
819 	 */
820 	__le64 target;
821 
822 	/* BTRFS_BALANCE_ARGS_* */
823 	__le64 flags;
824 
825 	/*
826 	 * BTRFS_BALANCE_ARGS_LIMIT with value 'limit'
827 	 * BTRFS_BALANCE_ARGS_LIMIT_RANGE - the extend version can use minimum
828 	 * and maximum
829 	 */
830 	union {
831 		__le64 limit;
832 		struct {
833 			__le32 limit_min;
834 			__le32 limit_max;
835 		}
836 	}
837 
838 	/*
839 	 * Process chunks that cross stripes_min..stripes_max devices,
840 	 * BTRFS_BALANCE_ARGS_STRIPES_RANGE
841 	 */
842 	__le32 stripes_min;
843 	__le32 stripes_max;
844 
845 	__le64[6] unused;
846 }
847 
848 /*
849  * store balance parameters to disk so that balance can be properly
850  * resumed after crash or unmount
851  */
852 struct btrfs_balance_item {
853 align(1):
854 	/* BTRFS_BALANCE_* */
855 	__le64 flags;
856 
857 	btrfs_disk_balance_args data;
858 	btrfs_disk_balance_args meta;
859 	btrfs_disk_balance_args sys;
860 
861 	__le64[4] unused;
862 }
863 
864 enum BTRFS_FILE_EXTENT_INLINE = 0;
865 enum BTRFS_FILE_EXTENT_REG = 1;
866 enum BTRFS_FILE_EXTENT_PREALLOC = 2;
867 struct btrfs_file_extent_item {
868 align(1):
869 	/*
870 	 * transaction id that created this extent
871 	 */
872 	__le64 generation;
873 	/*
874 	 * max number of bytes to hold this extent in ram
875 	 * when we split a compressed extent we can't know how big
876 	 * each of the resulting pieces will be.  So, this is
877 	 * an upper limit on the size of the extent in ram instead of
878 	 * an exact limit.
879 	 */
880 	__le64 ram_bytes;
881 
882 	/*
883 	 * 32 bits for the various ways we might encode the data,
884 	 * including compression and encryption.  If any of these
885 	 * are set to something a given disk format doesn't understand
886 	 * it is treated like an incompat flag for reading and writing,
887 	 * but not for stat.
888 	 */
889 	u8 compression;
890 	u8 encryption;
891 	__le16 other_encoding; /* spare for later use */
892 
893 	/* are we inline data or a real extent? */
894 	u8 type;
895 
896 	/*
897 	 * Disk space consumed by the data extent
898 	 * Data checksum is stored in csum tree, thus no bytenr/length takes
899 	 * csum into consideration.
900 	 *
901 	 * The inline extent data starts at this offset in the structure.
902 	 */
903 	__le64 disk_bytenr;
904 	__le64 disk_num_bytes;
905 	/*
906 	 * The logical offset in file blocks.
907 	 * this extent record is for.  This allows a file extent to point
908 	 * into the middle of an existing extent on disk, sharing it
909 	 * between two snapshots (useful if some bytes in the middle of the
910 	 * extent have changed
911 	 */
912 	__le64 offset;
913 	/*
914 	 * The logical number of file blocks. This always reflects the size
915 	 * uncompressed and without encoding.
916 	 */
917 	__le64 num_bytes;
918 
919 }
920 
921 struct btrfs_dev_stats_item {
922 align(1):
923         /*
924          * grow this item struct at the end for future enhancements and keep
925          * the existing values unchanged
926          */
927         __le64[btrfs_dev_stat_values.BTRFS_DEV_STAT_VALUES_MAX] values;
928 }
929 
930 struct btrfs_csum_item {
931 align(1):
932 	u8 csum;
933 }
934 
935 /*
936  * We don't want to overwrite 1M at the beginning of device, even though
937  * there is our 1st superblock at 64k. Some possible reasons:
938  *  - the first 64k blank is useful for some boot loader/manager
939  *  - the first 1M could be scratched by buggy partitioner or somesuch
940  */
941 enum BTRFS_BLOCK_RESERVED_1M_FOR_SUPER	= (cast(u64)SZ_1M);
942 /* tag for the radix tree of block groups in ram */
943 enum BTRFS_BLOCK_GROUP_DATA		= (1UL << 0);
944 enum BTRFS_BLOCK_GROUP_SYSTEM	= (1UL << 1);
945 enum BTRFS_BLOCK_GROUP_METADATA	= (1UL << 2);
946 enum BTRFS_BLOCK_GROUP_RAID0		= (1UL << 3);
947 enum BTRFS_BLOCK_GROUP_RAID1		= (1UL << 4);
948 enum BTRFS_BLOCK_GROUP_DUP		= (1UL << 5);
949 enum BTRFS_BLOCK_GROUP_RAID10	= (1UL << 6);
950 enum BTRFS_BLOCK_GROUP_RAID5    	= (1UL << 7);
951 enum BTRFS_BLOCK_GROUP_RAID6    	= (1UL << 8);
952 enum BTRFS_BLOCK_GROUP_RAID1C3    	= (1UL << 9);
953 enum BTRFS_BLOCK_GROUP_RAID1C4    	= (1UL << 10);
954 enum BTRFS_BLOCK_GROUP_RESERVED	= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
955 enum btrfs_raid_types {
956 	BTRFS_RAID_RAID10,
957 	BTRFS_RAID_RAID1,
958 	BTRFS_RAID_DUP,
959 	BTRFS_RAID_RAID0,
960 	BTRFS_RAID_SINGLE,
961 	BTRFS_RAID_RAID5,
962 	BTRFS_RAID_RAID6,
963 	BTRFS_RAID_RAID1C3,
964 	BTRFS_RAID_RAID1C4,
965 	BTRFS_NR_RAID_TYPES
966 }
967 
968 enum BTRFS_BLOCK_GROUP_TYPE_MASK	= (BTRFS_BLOCK_GROUP_DATA |
969 					 BTRFS_BLOCK_GROUP_SYSTEM |
970 					 BTRFS_BLOCK_GROUP_METADATA);
971 enum BTRFS_BLOCK_GROUP_PROFILE_MASK	= (BTRFS_BLOCK_GROUP_RAID0 |
972 					 BTRFS_BLOCK_GROUP_RAID1 |
973 					 BTRFS_BLOCK_GROUP_RAID5 |
974 					 BTRFS_BLOCK_GROUP_RAID6 |
975 					 BTRFS_BLOCK_GROUP_RAID1C3 |
976 					 BTRFS_BLOCK_GROUP_RAID1C4 |
977 					 BTRFS_BLOCK_GROUP_DUP |
978 					 BTRFS_BLOCK_GROUP_RAID10);
979 /* used in struct btrfs_balance_args fields */
980 enum BTRFS_AVAIL_ALLOC_BIT_SINGLE	= (1UL << 48);
981 enum BTRFS_EXTENDED_PROFILE_MASK	= (BTRFS_BLOCK_GROUP_PROFILE_MASK |
982 					 BTRFS_AVAIL_ALLOC_BIT_SINGLE);
983 /*
984  * GLOBAL_RSV does not exist as a on-disk block group type and is used
985  * internally for exporting info about global block reserve from space infos
986  */
987 enum BTRFS_SPACE_INFO_GLOBAL_RSV    = (1UL << 49);
988 enum BTRFS_QGROUP_LEVEL_SHIFT		= 48;
989 u64 btrfs_qgroup_level()(u64 qgroupid)
990 {
991 	return qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT;
992 }
993 
994 u64 btrfs_qgroup_subvid()(u64 qgroupid)
995 {
996 	return qgroupid & ((1UL << BTRFS_QGROUP_LEVEL_SHIFT) - 1);
997 }
998 
999 enum BTRFS_QGROUP_STATUS_FLAG_ON		= (1UL << 0);
1000 enum BTRFS_QGROUP_STATUS_FLAG_RESCAN		= (1UL << 1);
1001 enum BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT	= (1UL << 2);
1002 struct btrfs_qgroup_status_item {
1003 align(1):
1004 	__le64 version_;
1005 	__le64 generation;
1006 	__le64 flags;
1007 	__le64 rescan;		/* progress during scanning */
1008 }
1009 
1010 enum BTRFS_QGROUP_STATUS_VERSION		= 1;
1011 struct btrfs_block_group_item {
1012 align(1):
1013 	__le64 used;
1014 	__le64 chunk_objectid;
1015 	__le64 flags;
1016 }
1017 
1018 struct btrfs_free_space_info {
1019 align(1):
1020 	__le32 extent_count;
1021 	__le32 flags;
1022 }
1023 
1024 enum BTRFS_FREE_SPACE_USING_BITMAPS = (1UL << 0);
1025 struct btrfs_qgroup_info_item {
1026 align(1):
1027 	__le64 generation;
1028 	__le64 referenced;
1029 	__le64 referenced_compressed;
1030 	__le64 exclusive;
1031 	__le64 exclusive_compressed;
1032 }
1033 
1034 /* flags definition for qgroup limits */
1035 enum BTRFS_QGROUP_LIMIT_MAX_RFER	= (1UL << 0);
1036 enum BTRFS_QGROUP_LIMIT_MAX_EXCL	= (1UL << 1);
1037 enum BTRFS_QGROUP_LIMIT_RSV_RFER	= (1UL << 2);
1038 enum BTRFS_QGROUP_LIMIT_RSV_EXCL	= (1UL << 3);
1039 enum BTRFS_QGROUP_LIMIT_RFER_CMPR	= (1UL << 4);
1040 enum BTRFS_QGROUP_LIMIT_EXCL_CMPR	= (1UL << 5);
1041 struct btrfs_qgroup_limit_item {
1042 align(1):
1043 	__le64 flags;
1044 	__le64 max_referenced;
1045 	__le64 max_exclusive;
1046 	__le64 rsv_referenced;
1047 	__le64 rsv_exclusive;
1048 }
1049 
1050 struct btrfs_space_info {
1051 	u64 flags;
1052 	u64 total_bytes;
1053 	/*
1054 	 * Space already used.
1055 	 * Only accounting space in current extent tree, thus delayed ref
1056 	 * won't be accounted here.
1057 	 */
1058 	u64 bytes_used;
1059 
1060 	/*
1061 	 * Space being pinned down.
1062 	 * So extent allocator will not try to allocate space from them.
1063 	 *
1064 	 * For cases like extents being freed in current transaction, or
1065 	 * manually pinned bytes for re-initializing certain trees.
1066 	 */
1067 	u64 bytes_pinned;
1068 
1069 	/*
1070 	 * Space being reserved.
1071 	 * Space has already being reserved but not yet reach extent tree.
1072 	 *
1073 	 * New tree blocks allocated in current transaction goes here.
1074 	 */
1075 	u64 bytes_reserved;
1076 	int full;
1077 	list_head list;
1078 }
1079 
1080 struct btrfs_block_group {
1081 	btrfs_space_info *space_info;
1082 	btrfs_free_space_ctl *free_space_ctl;
1083 	u64 start;
1084 	u64 length;
1085 	u64 used;
1086 	u64 bytes_super;
1087 	u64 pinned;
1088 	u64 flags;
1089 	int cached;
1090 	int ro;
1091 	/*
1092 	 * If the free space extent count exceeds this number, convert the block
1093 	 * group to bitmaps.
1094 	 */
1095 	u32 bitmap_high_thresh;
1096 	/*
1097 	 * If the free space extent count drops below this number, convert the
1098 	 * block group back to extents.
1099 	 */
1100 	u32 bitmap_low_thresh;
1101 
1102 	/* Block group cache stuff */
1103 	rb_node cache_node;
1104 
1105 	/* For dirty block groups */
1106 	list_head dirty_list;
1107 }
1108 
1109 struct btrfs_device;
1110 struct btrfs_fs_devices;
1111 struct btrfs_fs_info {
1112 	u8[BTRFS_UUID_SIZE] chunk_tree_uuid;
1113 	u8 *new_chunk_tree_uuid;
1114 	btrfs_root *fs_root;
1115 	btrfs_root *extent_root;
1116 	btrfs_root *tree_root;
1117 	btrfs_root *chunk_root;
1118 	btrfs_root *dev_root;
1119 	btrfs_root *csum_root;
1120 	btrfs_root *quota_root;
1121 	btrfs_root *free_space_root;
1122 	btrfs_root *uuid_root;
1123 
1124 	rb_root fs_root_tree;
1125 
1126 	/* the log root tree is a directory of all the other log roots */
1127 	btrfs_root *log_root_tree;
1128 
1129 	extent_io_tree extent_cache;
1130 	extent_io_tree free_space_cache;
1131 	extent_io_tree pinned_extents;
1132 	extent_io_tree extent_ins;
1133 	extent_io_tree *excluded_extents;
1134 
1135 	rb_root block_group_cache_tree;
1136 	/* logical->physical extent mapping */
1137 	btrfs_mapping_tree mapping_tree;
1138 
1139 	u64 generation;
1140 	u64 last_trans_committed;
1141 
1142 	u64 avail_data_alloc_bits;
1143 	u64 avail_metadata_alloc_bits;
1144 	u64 avail_system_alloc_bits;
1145 	u64 data_alloc_profile;
1146 	u64 metadata_alloc_profile;
1147 	u64 system_alloc_profile;
1148 
1149 	btrfs_trans_handle *running_transaction;
1150 	btrfs_super_block *super_copy;
1151 
1152 	u64 super_bytenr;
1153 	u64 total_pinned;
1154 
1155 	list_head dirty_cowonly_roots;
1156 	list_head recow_ebs;
1157 
1158 	btrfs_fs_devices *fs_devices;
1159 	list_head space_info;
1160 
1161 	mixin(bitfields!(
1162 		uint, q{system_allocs}, 1,
1163 		uint, q{readonly}, 1,
1164 		uint, q{on_restoring}, 1,
1165 		uint, q{is_chunk_recover}, 1,
1166 		uint, q{quota_enabled}, 1,
1167 		uint, q{suppress_check_block_errors}, 1,
1168 		uint, q{ignore_fsid_mismatch}, 1,
1169 		uint, q{ignore_chunk_tree_error}, 1,
1170 		uint, q{avoid_meta_chunk_alloc}, 1,
1171 		uint, q{avoid_sys_chunk_alloc}, 1,
1172 		uint, q{finalize_on_close}, 1,
1173 		uint, q{hide_names}, 1,
1174 		uint, q{}, 4,
1175 	));
1176 
1177 	int transaction_aborted;
1178 
1179 	int function(u64 bytenr, u64 num_bytes, u64 parent,
1180 				u64 root_objectid, u64 owner, u64 offset,
1181 				int refs_to_drop) free_extent_hook;
1182 	cache_tree *fsck_extent_cache;
1183 	cache_tree *corrupt_blocks;
1184 
1185 	/* Cached block sizes */
1186 	u32 nodesize;
1187 	u32 sectorsize;
1188 	u32 stripesize;
1189 }
1190 
1191 /*
1192  * in ram representation of the tree.  extent_root is used for all allocations
1193  * and for the extent tree extent_root root.
1194  */
1195 struct btrfs_root {
1196 	extent_buffer *node;
1197 	extent_buffer *commit_root;
1198 	btrfs_root_item root_item;
1199 	btrfs_key root_key;
1200 	btrfs_fs_info *fs_info;
1201 	u64 objectid;
1202 	u64 last_trans;
1203 
1204 	int ref_cows;
1205 	int track_dirty;
1206 
1207 
1208 	u32 type;
1209 	u64 last_inode_alloc;
1210 
1211 	list_head unaligned_extent_recs;
1212 
1213 	/* the dirty list is only used by non-reference counted roots */
1214 	list_head dirty_list;
1215 	.rb_node rb_node;
1216 }
1217 
1218 u32 BTRFS_MAX_ITEM_SIZE()(const btrfs_fs_info *info)
1219 {
1220 	return BTRFS_LEAF_DATA_SIZE(info) - btrfs_item.sizeof;
1221 }
1222 
1223 u32 BTRFS_NODEPTRS_PER_BLOCK()(const btrfs_fs_info *info)
1224 {
1225 	return BTRFS_LEAF_DATA_SIZE(info) / btrfs_key_ptr.sizeof;
1226 }
1227 
1228 u32 BTRFS_NODEPTRS_PER_EXTENT_BUFFER(const extent_buffer *eb)
1229 {
1230 	assert(!(eb.fs_info && eb.fs_info.nodesize != eb.len));
1231 	return cast(u32)(__BTRFS_LEAF_DATA_SIZE(eb.len) / btrfs_key_ptr.sizeof);
1232 }
1233 
1234 enum BTRFS_FILE_EXTENT_INLINE_DATA_START		=
1235 	btrfs_file_extent_item.disk_bytenr.offsetof;
1236 u32 BTRFS_MAX_INLINE_DATA_SIZE()(const btrfs_fs_info *info)
1237 {
1238 	return BTRFS_MAX_ITEM_SIZE(info) -
1239 		BTRFS_FILE_EXTENT_INLINE_DATA_START;
1240 }
1241 
1242 u32 BTRFS_MAX_XATTR_SIZE()(const btrfs_fs_info *info)
1243 {
1244 	return BTRFS_MAX_ITEM_SIZE(info) - btrfs_dir_item.sizeof;
1245 }
1246 
1247 /*
1248  * inode items have the data typically returned from stat and store other
1249  * info about object characteristics.  There is one for every file and dir in
1250  * the FS
1251  */
1252 enum BTRFS_INODE_ITEM_KEY		= 1;
1253 enum BTRFS_INODE_REF_KEY		= 12;
1254 enum BTRFS_INODE_EXTREF_KEY		= 13;
1255 enum BTRFS_XATTR_ITEM_KEY		= 24;
1256 enum BTRFS_ORPHAN_ITEM_KEY		= 48;
1257 enum BTRFS_DIR_LOG_ITEM_KEY  = 60;
1258 enum BTRFS_DIR_LOG_INDEX_KEY = 72;
1259 /*
1260  * dir items are the name . inode pointers in a directory.  There is one
1261  * for every name in a directory.
1262  */
1263 enum BTRFS_DIR_ITEM_KEY	= 84;
1264 enum BTRFS_DIR_INDEX_KEY	= 96;
1265 /*
1266  * extent data is for file data
1267  */
1268 enum BTRFS_EXTENT_DATA_KEY	= 108;
1269 /*
1270  * csum items have the checksums for data in the extents
1271  */
1272 enum BTRFS_CSUM_ITEM_KEY	= 120;
1273 /*
1274  * extent csums are stored in a separate tree and hold csums for
1275  * an entire extent on disk.
1276  */
1277 enum BTRFS_EXTENT_CSUM_KEY	= 128;
1278 /*
1279  * root items point to tree roots.  There are typically in the root
1280  * tree used by the super block to find all the other trees
1281  */
1282 enum BTRFS_ROOT_ITEM_KEY	= 132;
1283 /*
1284  * root backrefs tie subvols and snapshots to the directory entries that
1285  * reference them
1286  */
1287 enum BTRFS_ROOT_BACKREF_KEY	= 144;
1288 /*
1289  * root refs make a fast index for listing all of the snapshots and
1290  * subvolumes referenced by a given root.  They point directly to the
1291  * directory item in the root that references the subvol
1292  */
1293 enum BTRFS_ROOT_REF_KEY	= 156;
1294 /*
1295  * extent items are in the extent map tree.  These record which blocks
1296  * are used, and how many references there are to each block
1297  */
1298 enum BTRFS_EXTENT_ITEM_KEY	= 168;
1299 /*
1300  * The same as the BTRFS_EXTENT_ITEM_KEY, except it's metadata we already know
1301  * the length, so we save the level in key.offset instead of the length.
1302  */
1303 enum BTRFS_METADATA_ITEM_KEY	= 169;
1304 enum BTRFS_TREE_BLOCK_REF_KEY	= 176;
1305 enum BTRFS_EXTENT_DATA_REF_KEY	= 178;
1306 /* old style extent backrefs */
1307 enum BTRFS_EXTENT_REF_V0_KEY		= 180;
1308 enum BTRFS_SHARED_BLOCK_REF_KEY	= 182;
1309 enum BTRFS_SHARED_DATA_REF_KEY	= 184;
1310 
1311 /*
1312  * block groups give us hints into the extent allocation trees.  Which
1313  * blocks are free etc etc
1314  */
1315 enum BTRFS_BLOCK_GROUP_ITEM_KEY = 192;
1316 /*
1317  * Every block group is represented in the free space tree by a free space info
1318  * item, which stores some accounting information. It is keyed on
1319  * (block_group_start, FREE_SPACE_INFO, block_group_length).
1320  */
1321 enum BTRFS_FREE_SPACE_INFO_KEY = 198;
1322 /*
1323  * A free space extent tracks an extent of space that is free in a block group.
1324  * It is keyed on (start, FREE_SPACE_EXTENT, length).
1325  */
1326 enum BTRFS_FREE_SPACE_EXTENT_KEY = 199;
1327 /*
1328  * When a block group becomes very fragmented, we convert it to use bitmaps
1329  * instead of extents. A free space bitmap is keyed on
1330  * (start, FREE_SPACE_BITMAP, length); the corresponding item is a bitmap with
1331  * (length / sectorsize) bits.
1332  */
1333 enum BTRFS_FREE_SPACE_BITMAP_KEY = 200;
1334 enum BTRFS_DEV_EXTENT_KEY	= 204;
1335 enum BTRFS_DEV_ITEM_KEY	= 216;
1336 enum BTRFS_CHUNK_ITEM_KEY	= 228;
1337 enum BTRFS_BALANCE_ITEM_KEY	= 248;
1338 /*
1339  * quota groups
1340  */
1341 enum BTRFS_QGROUP_STATUS_KEY		= 240;
1342 enum BTRFS_QGROUP_INFO_KEY		= 242;
1343 enum BTRFS_QGROUP_LIMIT_KEY		= 244;
1344 enum BTRFS_QGROUP_RELATION_KEY	= 246;
1345 /*
1346  * Obsolete name, see BTRFS_TEMPORARY_ITEM_KEY.
1347  */
1348 //enum BTRFS_BALANCE_ITEM_KEY	= 248;
1349 /*
1350  * The key type for tree items that are stored persistently, but do not need to
1351  * exist for extended period of time. The items can exist in any tree.
1352  *
1353  * [subtype, BTRFS_TEMPORARY_ITEM_KEY, data]
1354  *
1355  * Existing items:
1356  *
1357  * - balance status item
1358  *   (BTRFS_BALANCE_OBJECTID, BTRFS_TEMPORARY_ITEM_KEY, 0)
1359  */
1360 enum BTRFS_TEMPORARY_ITEM_KEY	= 248;
1361 /*
1362  * Obsolete name, see BTRFS_PERSISTENT_ITEM_KEY
1363  */
1364 enum BTRFS_DEV_STATS_KEY		= 249;
1365 /*
1366  * The key type for tree items that are stored persistently and usually exist
1367  * for a long period, eg. filesystem lifetime. The item kinds can be status
1368  * information, stats or preference values. The item can exist in any tree.
1369  *
1370  * [subtype, BTRFS_PERSISTENT_ITEM_KEY, data]
1371  *
1372  * Existing items:
1373  *
1374  * - device statistics, store IO stats in the device tree, one key for all
1375  *   stats
1376  *   (BTRFS_DEV_STATS_OBJECTID, BTRFS_DEV_STATS_KEY, 0)
1377  */
1378 enum BTRFS_PERSISTENT_ITEM_KEY	= 249;
1379 /*
1380  * Persistently stores the device replace state in the device tree.
1381  * The key is built like this: (0, BTRFS_DEV_REPLACE_KEY, 0).
1382  */
1383 enum BTRFS_DEV_REPLACE_KEY	= 250;
1384 /*
1385  * Stores items that allow to quickly map UUIDs to something else.
1386  * These items are part of the filesystem UUID tree.
1387  * The key is built like this:
1388  * (UUID_upper_64_bits, BTRFS_UUID_KEY*, UUID_lower_64_bits).
1389  */
1390 static if (BTRFS_UUID_SIZE != 16) {
1391 static assert(false, "UUID items require BTRFS_UUID_SIZE == 16!");
1392 }
1393 enum BTRFS_UUID_KEY_SUBVOL	= 251	/* for UUIDs assigned to subvols */;
1394 enum BTRFS_UUID_KEY_RECEIVED_SUBVOL	= 252;	/* for UUIDs assigned to
1395 						 * received subvols */
1396 
1397 /*
1398  * string items are for debugging.  They just store a short string of
1399  * data in the FS
1400  */
1401 enum BTRFS_STRING_ITEM_KEY	= 253;
1402 /*
1403  * Inode flags
1404  */
1405 enum BTRFS_INODE_NODATASUM		= (1 << 0);
1406 enum BTRFS_INODE_NODATACOW		= (1 << 1);
1407 enum BTRFS_INODE_READONLY		= (1 << 2);
1408 enum BTRFS_INODE_NOCOMPRESS		= (1 << 3);
1409 enum BTRFS_INODE_PREALLOC		= (1 << 4);
1410 enum BTRFS_INODE_SYNC		= (1 << 5);
1411 enum BTRFS_INODE_IMMUTABLE		= (1 << 6);
1412 enum BTRFS_INODE_APPEND		= (1 << 7);
1413 enum BTRFS_INODE_NODUMP		= (1 << 8);
1414 enum BTRFS_INODE_NOATIME		= (1 << 9);
1415 enum BTRFS_INODE_DIRSYNC		= (1 << 10);
1416 enum BTRFS_INODE_COMPRESS		= (1 << 11);
1417 void read_eb_member(
1418 	type,
1419 	string member,
1420 )(
1421 	const extent_buffer* eb,
1422 	const void* ptr,
1423 	void* result,
1424 )
1425 {
1426 	read_extent_buffer(eb, cast(ubyte*)result,
1427 			   (cast(ulong)(ptr)) +
1428 			    mixin(q{type..} ~ member ~ q{.offsetof}),
1429                mixin(q{type..} ~ member ~ q{.sizeof}));
1430 }
1431 
1432 void write_eb_member(
1433 	type,
1434 	string member,
1435 )(
1436 	extent_buffer* eb,
1437 	const void* ptr,
1438 	void* result,
1439 )
1440 {
1441 	write_extent_buffer(eb, cast(ubyte*)result,
1442 			   (cast(ulong)(ptr)) +
1443 			    mixin(q{type..} ~ member ~ q{.offsetof}),
1444                mixin(q{type..} ~ member ~ q{.sizeof}));
1445 }
1446 
1447 mixin template BTRFS_SETGET_HEADER_FUNCS(string name, type, string member, string bits)
1448 {
1449 	mixin(`
1450 		u` ~ bits ~ ` btrfs_` ~ name ~ `()(const extent_buffer* eb)
1451 		{
1452 			const btrfs_header* h = cast(btrfs_header*)eb.data;
1453 			return le` ~ bits ~ `_to_cpu(h.` ~ member ~ `);
1454 		}
1455 		void btrfs_set_` ~ name ~ `()(extent_buffer* eb,
1456 							u` ~ bits ~ ` val)
1457 		{
1458 			btrfs_header *h = cast(btrfs_header*)eb.data;
1459 			h.` ~ member ~ ` = cpu_to_le` ~ bits ~ `(val);
1460 		}
1461 	`);
1462 }
1463 
1464 mixin template BTRFS_SETGET_FUNCS(string name, type, string member, string bits)
1465 {
1466 	mixin(`
1467 		u` ~ bits ~ ` btrfs_` ~ name ~ `()(const extent_buffer* eb,
1468 						const type *s)
1469 		{
1470 			ulong offset = cast(ulong)s;
1471 			const type* p = cast(type*) (eb.data.ptr + offset);
1472 			return get_unaligned_le` ~ bits ~ `(&p.` ~ member ~ `);
1473 		}
1474 		void btrfs_set_` ~ name ~ `()(extent_buffer* eb,
1475 						type *s, u` ~ bits ~ ` val)
1476 		{
1477 			ulong offset = cast(ulong)s;
1478 			type* p = cast(type*) (eb.data.ptr + offset);
1479 			put_unaligned_le` ~ bits ~ `(val, &p.` ~ member ~ `);
1480 		}
1481 	`);
1482 }
1483 
1484 mixin template BTRFS_SETGET_STACK_FUNCS(string name, type, string member, string bits)
1485 {
1486 	mixin(`
1487 		u` ~ bits ~ ` btrfs_` ~ name ~ `()(const type* s)
1488 		{
1489 			return le` ~ bits ~ `_to_cpu(s.` ~ member ~ `);
1490 		}
1491 		void btrfs_set_` ~ name ~ `()(type* s, u` ~ bits ~ ` val)
1492 		{
1493 			s.member = cpu_to_le` ~ bits ~ `(val);
1494 		}
1495 	`);
1496 }
1497 
1498 mixin BTRFS_SETGET_FUNCS!(q{device_type}, btrfs_dev_item, q{type}, q{64});
1499 mixin BTRFS_SETGET_FUNCS!(q{device_total_bytes}, btrfs_dev_item, q{total_bytes}, q{64});
1500 mixin BTRFS_SETGET_FUNCS!(q{device_bytes_used}, btrfs_dev_item, q{bytes_used}, q{64});
1501 mixin BTRFS_SETGET_FUNCS!(q{device_io_align}, btrfs_dev_item, q{io_align}, q{32});
1502 mixin BTRFS_SETGET_FUNCS!(q{device_io_width}, btrfs_dev_item, q{io_width}, q{32});
1503 mixin BTRFS_SETGET_FUNCS!(q{device_start_offset}, btrfs_dev_item,
1504 		   q{start_offset}, q{64});
1505 mixin BTRFS_SETGET_FUNCS!(q{device_sector_size}, btrfs_dev_item, q{sector_size}, q{32});
1506 mixin BTRFS_SETGET_FUNCS!(q{device_id}, btrfs_dev_item, q{devid}, q{64});
1507 mixin BTRFS_SETGET_FUNCS!(q{device_group}, btrfs_dev_item, q{dev_group}, q{32});
1508 mixin BTRFS_SETGET_FUNCS!(q{device_seek_speed}, btrfs_dev_item, q{seek_speed}, q{8});
1509 mixin BTRFS_SETGET_FUNCS!(q{device_bandwidth}, btrfs_dev_item, q{bandwidth}, q{8});
1510 mixin BTRFS_SETGET_FUNCS!(q{device_generation}, btrfs_dev_item, q{generation}, q{64});
1511 
1512 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_type}, btrfs_dev_item, q{type}, q{64});
1513 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_total_bytes}, btrfs_dev_item,
1514 			 q{total_bytes}, q{64});
1515 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_bytes_used}, btrfs_dev_item,
1516 			 q{bytes_used}, q{64});
1517 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_io_align}, btrfs_dev_item,
1518 			 q{io_align}, q{32});
1519 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_io_width}, btrfs_dev_item,
1520 			 q{io_width}, q{32});
1521 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_sector_size}, btrfs_dev_item,
1522 			 q{sector_size}, q{32});
1523 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_id}, btrfs_dev_item, q{devid}, q{64});
1524 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_group}, btrfs_dev_item,
1525 			 q{dev_group}, q{32});
1526 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_seek_speed}, btrfs_dev_item,
1527 			 q{seek_speed}, q{8});
1528 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_bandwidth}, btrfs_dev_item,
1529 			 q{bandwidth}, q{8});
1530 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_device_generation}, btrfs_dev_item,
1531 			 q{generation}, q{64});
1532 
1533 char *btrfs_device_uuid()(btrfs_dev_item *d)
1534 {
1535 	return cast(char *)d + btrfs_dev_item.uuid.offsetof;
1536 }
1537 
1538 char *btrfs_device_fsid()(btrfs_dev_item *d)
1539 {
1540 	return cast(char *)d + btrfs_dev_item.fsid.offsetof;
1541 }
1542 
1543 mixin BTRFS_SETGET_FUNCS!(q{chunk_length}, btrfs_chunk, q{length}, q{64});
1544 mixin BTRFS_SETGET_FUNCS!(q{chunk_owner}, btrfs_chunk, q{owner}, q{64});
1545 mixin BTRFS_SETGET_FUNCS!(q{chunk_stripe_len}, btrfs_chunk, q{stripe_len}, q{64});
1546 mixin BTRFS_SETGET_FUNCS!(q{chunk_io_align}, btrfs_chunk, q{io_align}, q{32});
1547 mixin BTRFS_SETGET_FUNCS!(q{chunk_io_width}, btrfs_chunk, q{io_width}, q{32});
1548 mixin BTRFS_SETGET_FUNCS!(q{chunk_sector_size}, btrfs_chunk, q{sector_size}, q{32});
1549 mixin BTRFS_SETGET_FUNCS!(q{chunk_type}, btrfs_chunk, q{type}, q{64});
1550 mixin BTRFS_SETGET_FUNCS!(q{chunk_num_stripes}, btrfs_chunk, q{num_stripes}, q{16});
1551 mixin BTRFS_SETGET_FUNCS!(q{chunk_sub_stripes}, btrfs_chunk, q{sub_stripes}, q{16});
1552 mixin BTRFS_SETGET_FUNCS!(q{stripe_devid}, btrfs_stripe, q{devid}, q{64});
1553 mixin BTRFS_SETGET_FUNCS!(q{stripe_offset}, btrfs_stripe, q{offset}, q{64});
1554 
1555 char *btrfs_stripe_dev_uuid()(btrfs_stripe *s)
1556 {
1557 	return cast(char *)s + btrfs_stripe.dev_uuid.offsetof;
1558 }
1559 
1560 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_length}, btrfs_chunk, q{length}, q{64});
1561 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_owner}, btrfs_chunk, q{owner}, q{64});
1562 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_stripe_len}, btrfs_chunk,
1563 			 q{stripe_len}, q{64});
1564 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_io_align}, btrfs_chunk,
1565 			 q{io_align}, q{32});
1566 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_io_width}, btrfs_chunk,
1567 			 q{io_width}, q{32});
1568 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_sector_size}, btrfs_chunk,
1569 			 q{sector_size}, q{32});
1570 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_type}, btrfs_chunk, q{type}, q{64});
1571 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_num_stripes}, btrfs_chunk,
1572 			 q{num_stripes}, q{16});
1573 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_chunk_sub_stripes}, btrfs_chunk,
1574 			 q{sub_stripes}, q{16});
1575 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_stripe_devid}, btrfs_stripe, q{devid}, q{64});
1576 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_stripe_offset}, btrfs_stripe, q{offset}, q{64});
1577 
1578 btrfs_stripe *btrfs_stripe_nr()(btrfs_chunk *c,
1579 						   int nr)
1580 {
1581 	ulong offset = cast(ulong)c;
1582 	offset += btrfs_chunk.stripe.offsetof;
1583 	offset += nr * btrfs_stripe.sizeof;
1584 	return cast(btrfs_stripe*)offset;
1585 }
1586 
1587 char *btrfs_stripe_dev_uuid_nr()(btrfs_chunk *c, int nr)
1588 {
1589 	return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1590 }
1591 
1592 u64 btrfs_stripe_offset_nr()(extent_buffer *eb,
1593 					 btrfs_chunk *c, int nr)
1594 {
1595 	return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1596 }
1597 
1598 void btrfs_set_stripe_offset_nr()(extent_buffer *eb,
1599 					     btrfs_chunk *c, int nr,
1600 					     u64 val)
1601 {
1602 	btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
1603 }
1604 
1605 u64 btrfs_stripe_devid_nr()(extent_buffer *eb,
1606 					 btrfs_chunk *c, int nr)
1607 {
1608 	return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1609 }
1610 
1611 void btrfs_set_stripe_devid_nr()(extent_buffer *eb,
1612 					     btrfs_chunk *c, int nr,
1613 					     u64 val)
1614 {
1615 	btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
1616 }
1617 
1618 /* struct btrfs_block_group_item */
1619 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_block_group_used}, btrfs_block_group_item,
1620 			 q{used}, q{64});
1621 mixin BTRFS_SETGET_FUNCS!(q{block_group_used}, btrfs_block_group_item,
1622 			 q{used}, q{64});
1623 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_block_group_chunk_objectid},
1624 			 btrfs_block_group_item, q{chunk_objectid}, q{64});
1625 
1626 mixin BTRFS_SETGET_FUNCS!(q{block_group_chunk_objectid},
1627 		   btrfs_block_group_item, q{chunk_objectid}, q{64});
1628 mixin BTRFS_SETGET_FUNCS!(q{block_group_flags},
1629 		   btrfs_block_group_item, q{flags}, q{64});
1630 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_block_group_flags},
1631 			btrfs_block_group_item, q{flags}, q{64});
1632 
1633 /* struct btrfs_free_space_info */
1634 mixin BTRFS_SETGET_FUNCS!(q{free_space_extent_count}, btrfs_free_space_info,
1635 		   q{extent_count}, q{32});
1636 mixin BTRFS_SETGET_FUNCS!(q{free_space_flags}, btrfs_free_space_info, q{flags}, q{32});
1637 
1638 /* struct btrfs_inode_ref */
1639 mixin BTRFS_SETGET_FUNCS!(q{inode_ref_name_len}, btrfs_inode_ref, q{name_len}, q{16});
1640 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_ref_name_len}, btrfs_inode_ref, q{name_len}, q{16});
1641 mixin BTRFS_SETGET_FUNCS!(q{inode_ref_index}, btrfs_inode_ref, q{index}, q{64});
1642 
1643 /* struct btrfs_inode_extref */
1644 mixin BTRFS_SETGET_FUNCS!(q{inode_extref_parent}, btrfs_inode_extref,
1645 		   q{parent_objectid}, q{64});
1646 mixin BTRFS_SETGET_FUNCS!(q{inode_extref_name_len}, btrfs_inode_extref,
1647 		   q{name_len}, q{16});
1648 mixin BTRFS_SETGET_FUNCS!(q{inode_extref_index}, btrfs_inode_extref, q{index}, q{64});
1649 
1650 /* struct btrfs_inode_item */
1651 mixin BTRFS_SETGET_FUNCS!(q{inode_generation}, btrfs_inode_item, q{generation}, q{64});
1652 mixin BTRFS_SETGET_FUNCS!(q{inode_sequence}, btrfs_inode_item, q{sequence}, q{64});
1653 mixin BTRFS_SETGET_FUNCS!(q{inode_transid}, btrfs_inode_item, q{transid}, q{64});
1654 mixin BTRFS_SETGET_FUNCS!(q{inode_size}, btrfs_inode_item, q{size}, q{64});
1655 mixin BTRFS_SETGET_FUNCS!(q{inode_nbytes}, btrfs_inode_item, q{nbytes}, q{64});
1656 mixin BTRFS_SETGET_FUNCS!(q{inode_block_group}, btrfs_inode_item, q{block_group}, q{64});
1657 mixin BTRFS_SETGET_FUNCS!(q{inode_nlink}, btrfs_inode_item, q{nlink}, q{32});
1658 mixin BTRFS_SETGET_FUNCS!(q{inode_uid}, btrfs_inode_item, q{uid}, q{32});
1659 mixin BTRFS_SETGET_FUNCS!(q{inode_gid}, btrfs_inode_item, q{gid}, q{32});
1660 mixin BTRFS_SETGET_FUNCS!(q{inode_mode}, btrfs_inode_item, q{mode}, q{32});
1661 mixin BTRFS_SETGET_FUNCS!(q{inode_rdev}, btrfs_inode_item, q{rdev}, q{64});
1662 mixin BTRFS_SETGET_FUNCS!(q{inode_flags}, btrfs_inode_item, q{flags}, q{64});
1663 
1664 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_generation},
1665 			 btrfs_inode_item, q{generation}, q{64});
1666 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_sequence},
1667 			 btrfs_inode_item, q{sequence}, q{64});
1668 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_transid},
1669 			 btrfs_inode_item, q{transid}, q{64});
1670 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_size},
1671 			 btrfs_inode_item, q{size}, q{64});
1672 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_nbytes},
1673 			 btrfs_inode_item, q{nbytes}, q{64});
1674 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_block_group},
1675 			 btrfs_inode_item, q{block_group}, q{64});
1676 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_nlink},
1677 			 btrfs_inode_item, q{nlink}, q{32});
1678 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_uid},
1679 			 btrfs_inode_item, q{uid}, q{32});
1680 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_gid},
1681 			 btrfs_inode_item, q{gid}, q{32});
1682 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_mode},
1683 			 btrfs_inode_item, q{mode}, q{32});
1684 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_rdev},
1685 			 btrfs_inode_item, q{rdev}, q{64});
1686 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_inode_flags},
1687 			 btrfs_inode_item, q{flags}, q{64});
1688 
1689 btrfs_timespec *
1690 btrfs_inode_atime()(btrfs_inode_item *inode_item)
1691 {
1692 	ulong ptr = cast(ulong)inode_item;
1693 	ptr += btrfs_inode_item.atime.offsetof;
1694 	return cast(btrfs_timespec*)ptr;
1695 }
1696 
1697 btrfs_timespec *
1698 btrfs_inode_mtime()(btrfs_inode_item *inode_item)
1699 {
1700 	ulong ptr = cast(ulong)inode_item;
1701 	ptr += btrfs_inode_item.mtime.offsetof;
1702 	return cast(btrfs_timespec*)ptr;
1703 }
1704 
1705 btrfs_timespec *
1706 btrfs_inode_ctime()(btrfs_inode_item *inode_item)
1707 {
1708 	ulong ptr = cast(ulong)inode_item;
1709 	ptr += btrfs_inode_item.ctime.offsetof;
1710 	return cast(btrfs_timespec*)ptr;
1711 }
1712 
1713 btrfs_timespec *
1714 btrfs_inode_otime()(btrfs_inode_item *inode_item)
1715 {
1716 	ulong ptr = cast(ulong)inode_item;
1717 	ptr += btrfs_inode_item.otime.offsetof;
1718 	return cast(btrfs_timespec*)ptr;
1719 }
1720 
1721 mixin BTRFS_SETGET_FUNCS!(q{timespec_sec}, btrfs_timespec, q{sec}, q{64});
1722 mixin BTRFS_SETGET_FUNCS!(q{timespec_nsec}, btrfs_timespec, q{nsec}, q{32});
1723 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_timespec_sec}, btrfs_timespec,
1724 			 q{sec}, q{64});
1725 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_timespec_nsec}, btrfs_timespec,
1726 			 q{nsec}, q{32});
1727 
1728 /* struct btrfs_dev_extent */
1729 mixin BTRFS_SETGET_FUNCS!(q{dev_extent_chunk_tree}, btrfs_dev_extent,
1730 		   q{chunk_tree}, q{64});
1731 mixin BTRFS_SETGET_FUNCS!(q{dev_extent_chunk_objectid}, btrfs_dev_extent,
1732 		   q{chunk_objectid}, q{64});
1733 mixin BTRFS_SETGET_FUNCS!(q{dev_extent_chunk_offset}, btrfs_dev_extent,
1734 		   q{chunk_offset}, q{64});
1735 mixin BTRFS_SETGET_FUNCS!(q{dev_extent_length}, btrfs_dev_extent, q{length}, q{64});
1736 
1737 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_dev_extent_length}, btrfs_dev_extent,
1738 			 q{length}, q{64});
1739 
1740 u8 *btrfs_dev_extent_chunk_tree_uuid()(btrfs_dev_extent *dev)
1741 {
1742 	ulong ptr = btrfs_dev_extent.chunk_tree_uuid.offsetof;
1743 	return cast(u8*)(cast(ulong)dev + ptr);
1744 }
1745 
1746 
1747 /* struct btrfs_extent_item */
1748 mixin BTRFS_SETGET_FUNCS!(q{extent_refs}, btrfs_extent_item, q{refs}, q{64});
1749 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_extent_refs}, btrfs_extent_item, q{refs}, q{64});
1750 mixin BTRFS_SETGET_FUNCS!(q{extent_generation}, btrfs_extent_item,
1751 		   q{generation}, q{64});
1752 mixin BTRFS_SETGET_FUNCS!(q{extent_flags}, btrfs_extent_item, q{flags}, q{64});
1753 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_extent_flags}, btrfs_extent_item, q{flags}, q{64});
1754 
1755 mixin BTRFS_SETGET_FUNCS!(q{extent_refs_v0}, btrfs_extent_item_v0, q{refs}, q{32});
1756 
1757 mixin BTRFS_SETGET_FUNCS!(q{tree_block_level}, btrfs_tree_block_info, q{level}, q{8});
1758 
1759 void btrfs_tree_block_key()(extent_buffer *eb,
1760 					btrfs_tree_block_info *item,
1761 					btrfs_disk_key *key)
1762 {
1763 	read_eb_member!(btrfs_tree_block_info, q{key})(eb, item, key);
1764 }
1765 
1766 void btrfs_set_tree_block_key()(extent_buffer *eb,
1767 					    btrfs_tree_block_info *item,
1768 					    btrfs_disk_key *key)
1769 {
1770 	write_eb_member!(btrfs_tree_block_info, q{key})(eb, item, key);
1771 }
1772 
1773 mixin BTRFS_SETGET_FUNCS!(q{extent_data_ref_root}, btrfs_extent_data_ref,
1774 		   q{root}, q{64});
1775 mixin BTRFS_SETGET_FUNCS!(q{extent_data_ref_objectid}, btrfs_extent_data_ref,
1776 		   q{objectid}, q{64});
1777 mixin BTRFS_SETGET_FUNCS!(q{extent_data_ref_offset}, btrfs_extent_data_ref,
1778 		   q{offset}, q{64});
1779 mixin BTRFS_SETGET_FUNCS!(q{extent_data_ref_count}, btrfs_extent_data_ref,
1780 		   q{count}, q{32});
1781 
1782 mixin BTRFS_SETGET_FUNCS!(q{shared_data_ref_count}, btrfs_shared_data_ref,
1783 		   q{count}, q{32});
1784 
1785 mixin BTRFS_SETGET_FUNCS!(q{extent_inline_ref_type}, btrfs_extent_inline_ref,
1786 		   q{type}, q{8});
1787 mixin BTRFS_SETGET_FUNCS!(q{extent_inline_ref_offset}, btrfs_extent_inline_ref,
1788 		   q{offset}, q{64});
1789 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_extent_inline_ref_type},
1790 			 btrfs_extent_inline_ref, q{type}, q{8});
1791 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_extent_inline_ref_offset},
1792 			 btrfs_extent_inline_ref, q{offset}, q{64});
1793 
1794 u32 btrfs_extent_inline_ref_size()(int type)
1795 {
1796 	if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1797 	    type == BTRFS_SHARED_BLOCK_REF_KEY)
1798 		return btrfs_extent_inline_ref.sizeof;
1799 	if (type == BTRFS_SHARED_DATA_REF_KEY)
1800 		return btrfs_shared_data_ref.sizeof +
1801 		       btrfs_extent_inline_ref.sizeof;
1802 	if (type == BTRFS_EXTENT_DATA_REF_KEY)
1803 		return btrfs_extent_data_ref.sizeof +
1804 		       btrfs_extent_inline_ref.offset.offsetof;
1805 	BUG();
1806 	return 0;
1807 }
1808 
1809 mixin BTRFS_SETGET_FUNCS!(q{ref_root_v0}, btrfs_extent_ref_v0, q{root}, q{64});
1810 mixin BTRFS_SETGET_FUNCS!(q{ref_generation_v0}, btrfs_extent_ref_v0,
1811 		   q{generation}, q{64});
1812 mixin BTRFS_SETGET_FUNCS!(q{ref_objectid_v0}, btrfs_extent_ref_v0, q{objectid}, q{64});
1813 mixin BTRFS_SETGET_FUNCS!(q{ref_count_v0}, btrfs_extent_ref_v0, q{count}, q{32});
1814 
1815 /* struct btrfs_node */
1816 mixin BTRFS_SETGET_FUNCS!(q{key_blockptr}, btrfs_key_ptr, q{blockptr}, q{64});
1817 mixin BTRFS_SETGET_FUNCS!(q{key_generation}, btrfs_key_ptr, q{generation}, q{64});
1818 
1819 u64 btrfs_node_blockptr()(extent_buffer *eb, int nr)
1820 {
1821 	ulong ptr;
1822 	ptr = btrfs_node.ptrs.offsetof +
1823 		btrfs_key_ptr.sizeof * nr;
1824 	return btrfs_key_blockptr(eb, cast(btrfs_key_ptr *)ptr);
1825 }
1826 
1827 void btrfs_set_node_blockptr()(extent_buffer *eb,
1828 					   int nr, u64 val)
1829 {
1830 	ulong ptr;
1831 	ptr = btrfs_node.ptrs.offsetof +
1832 		btrfs_key_ptr.sizeof * nr;
1833 	btrfs_set_key_blockptr(eb, cast(btrfs_key_ptr *)ptr, val);
1834 }
1835 
1836 u64 btrfs_node_ptr_generation()(extent_buffer *eb, int nr)
1837 {
1838 	ulong ptr;
1839 	ptr = btrfs_node.ptrs.offsetof +
1840 		btrfs_key_ptr.sizeof * nr;
1841 	return btrfs_key_generation(eb, cast(btrfs_key_ptr *)ptr);
1842 }
1843 
1844 void btrfs_set_node_ptr_generation()(extent_buffer *eb,
1845 						 int nr, u64 val)
1846 {
1847 	ulong ptr;
1848 	ptr = btrfs_node.ptrs.offsetof +
1849 		btrfs_key_ptr.sizeof * nr;
1850 	btrfs_set_key_generation(eb, cast(btrfs_key_ptr *)ptr, val);
1851 }
1852 
1853 ulong btrfs_node_key_ptr_offset()(int nr)
1854 {
1855 	return btrfs_node.ptrs.offsetof +
1856 		btrfs_key_ptr.sizeof * nr;
1857 }
1858 
1859 void btrfs_node_key()(extent_buffer *eb,
1860 				  btrfs_disk_key *disk_key, int nr)
1861 {
1862 	ulong ptr;
1863 	ptr = btrfs_node_key_ptr_offset(nr);
1864 	read_eb_member!(
1865 		       btrfs_key_ptr, q{key})(eb, cast(btrfs_key_ptr *)ptr, disk_key);
1866 }
1867 
1868 void btrfs_set_node_key()(extent_buffer *eb,
1869 				      btrfs_disk_key *disk_key, int nr)
1870 {
1871 	ulong ptr;
1872 	ptr = btrfs_node_key_ptr_offset(nr);
1873 	write_eb_member!(
1874 		       btrfs_key_ptr, q{key})(eb, cast(btrfs_key_ptr *)ptr, disk_key);
1875 }
1876 
1877 /* struct btrfs_item */
1878 mixin BTRFS_SETGET_FUNCS!(q{item_offset}, btrfs_item, q{offset}, q{32});
1879 mixin BTRFS_SETGET_FUNCS!(q{item_size}, btrfs_item, q{size}, q{32});
1880 
1881 ulong btrfs_item_nr_offset()(int nr)
1882 {
1883 	return btrfs_leaf.items.offsetof +
1884 		btrfs_item.sizeof * nr;
1885 }
1886 
1887 btrfs_item *btrfs_item_nr()(int nr)
1888 {
1889 	return cast(btrfs_item*)btrfs_item_nr_offset(nr);
1890 }
1891 
1892 u32 btrfs_item_end()(extent_buffer *eb,
1893 				 btrfs_item *item)
1894 {
1895 	return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
1896 }
1897 
1898 u32 btrfs_item_end_nr()(extent_buffer *eb, int nr)
1899 {
1900 	return btrfs_item_end(eb, btrfs_item_nr(nr));
1901 }
1902 
1903 u32 btrfs_item_offset_nr()(const extent_buffer *eb, int nr)
1904 {
1905 	return btrfs_item_offset(eb, btrfs_item_nr(nr));
1906 }
1907 
1908 u32 btrfs_item_size_nr()(extent_buffer *eb, int nr)
1909 {
1910 	return btrfs_item_size(eb, btrfs_item_nr(nr));
1911 }
1912 
1913 void btrfs_item_key()(extent_buffer *eb,
1914 			   btrfs_disk_key *disk_key, int nr)
1915 {
1916 	btrfs_item *item = btrfs_item_nr(nr);
1917 	read_eb_member!(btrfs_item, q{key})(eb, item, disk_key);
1918 }
1919 
1920 void btrfs_set_item_key()(extent_buffer *eb,
1921 			       btrfs_disk_key *disk_key, int nr)
1922 {
1923 	btrfs_item *item = btrfs_item_nr(nr);
1924 	write_eb_member!(btrfs_item, q{key})(eb, item, disk_key);
1925 }
1926 
1927 mixin BTRFS_SETGET_FUNCS!(q{dir_log_end}, btrfs_dir_log_item, q{end}, q{64});
1928 
1929 /*
1930  * struct btrfs_root_ref
1931  */
1932 mixin BTRFS_SETGET_FUNCS!(q{root_ref_dirid}, btrfs_root_ref, q{dirid}, q{64});
1933 mixin BTRFS_SETGET_FUNCS!(q{root_ref_sequence}, btrfs_root_ref, q{sequence}, q{64});
1934 mixin BTRFS_SETGET_FUNCS!(q{root_ref_name_len}, btrfs_root_ref, q{name_len}, q{16});
1935 
1936 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_root_ref_dirid}, btrfs_root_ref, q{dirid}, q{64});
1937 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_root_ref_sequence}, btrfs_root_ref, q{sequence}, q{64});
1938 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_root_ref_name_len}, btrfs_root_ref, q{name_len}, q{16});
1939 
1940 /* struct btrfs_dir_item */
1941 mixin BTRFS_SETGET_FUNCS!(q{dir_data_len}, btrfs_dir_item, q{data_len}, q{16});
1942 mixin BTRFS_SETGET_FUNCS!(q{dir_type}, btrfs_dir_item, q{type}, q{8});
1943 mixin BTRFS_SETGET_FUNCS!(q{dir_name_len}, btrfs_dir_item, q{name_len}, q{16});
1944 mixin BTRFS_SETGET_FUNCS!(q{dir_transid}, btrfs_dir_item, q{transid}, q{64});
1945 
1946 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_dir_data_len}, btrfs_dir_item, q{data_len}, q{16});
1947 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_dir_type}, btrfs_dir_item, q{type}, q{8});
1948 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_dir_name_len}, btrfs_dir_item, q{name_len}, q{16});
1949 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_dir_transid}, btrfs_dir_item, q{transid}, q{64});
1950 
1951 void btrfs_dir_item_key()(extent_buffer *eb,
1952 				      btrfs_dir_item *item,
1953 				      btrfs_disk_key *key)
1954 {
1955 	read_eb_member!(btrfs_dir_item, q{location})(eb, item, key);
1956 }
1957 
1958 void btrfs_set_dir_item_key()(extent_buffer *eb,
1959 					  btrfs_dir_item *item,
1960 					  btrfs_disk_key *key)
1961 {
1962 	write_eb_member!(btrfs_dir_item, q{location})(eb, item, key);
1963 }
1964 
1965 /* struct btrfs_free_space_header */
1966 mixin BTRFS_SETGET_FUNCS!(q{free_space_entries}, btrfs_free_space_header,
1967 		   q{num_entries}, q{64});
1968 mixin BTRFS_SETGET_FUNCS!(q{free_space_bitmaps}, btrfs_free_space_header,
1969 		   q{num_bitmaps}, q{64});
1970 mixin BTRFS_SETGET_FUNCS!(q{free_space_generation}, btrfs_free_space_header,
1971 		   q{generation}, q{64});
1972 
1973 void btrfs_free_space_key()(extent_buffer *eb,
1974 					btrfs_free_space_header *h,
1975 					btrfs_disk_key *key)
1976 {
1977 	read_eb_member!(btrfs_free_space_header, q{location})(eb, h, key);
1978 }
1979 
1980 void btrfs_set_free_space_key()(extent_buffer *eb,
1981 					    btrfs_free_space_header *h,
1982 					    btrfs_disk_key *key)
1983 {
1984 	write_eb_member!(btrfs_free_space_header, q{location})(eb, h, key);
1985 }
1986 
1987 /* struct btrfs_disk_key */
1988 mixin BTRFS_SETGET_STACK_FUNCS!(q{disk_key_objectid}, btrfs_disk_key,
1989 			 q{objectid}, q{64});
1990 mixin BTRFS_SETGET_STACK_FUNCS!(q{disk_key_offset}, btrfs_disk_key, q{offset}, q{64});
1991 mixin BTRFS_SETGET_STACK_FUNCS!(q{disk_key_type}, btrfs_disk_key, q{type}, q{8});
1992 
1993 void btrfs_disk_key_to_cpu(btrfs_key *cpu,
1994 					 btrfs_disk_key *disk)
1995 {
1996 	cpu.offset = le64_to_cpu(disk.offset);
1997 	cpu.type = disk.type;
1998 	cpu.objectid = le64_to_cpu(disk.objectid);
1999 }
2000 
2001 void btrfs_cpu_key_to_disk(btrfs_disk_key *disk,
2002 					 const btrfs_key *cpu)
2003 {
2004 	disk.offset = cpu_to_le64(cpu.offset);
2005 	disk.type = cpu.type;
2006 	disk.objectid = cpu_to_le64(cpu.objectid);
2007 }
2008 
2009 void btrfs_node_key_to_cpu()(extent_buffer *eb,
2010 				  btrfs_key *key, int nr)
2011 {
2012 	btrfs_disk_key disk_key;
2013 	btrfs_node_key(eb, &disk_key, nr);
2014 	btrfs_disk_key_to_cpu(key, &disk_key);
2015 }
2016 
2017 void btrfs_item_key_to_cpu()(extent_buffer *eb,
2018 				  btrfs_key *key, int nr)
2019 {
2020 	btrfs_disk_key disk_key;
2021 	btrfs_item_key(eb, &disk_key, nr);
2022 	btrfs_disk_key_to_cpu(key, &disk_key);
2023 }
2024 
2025 void btrfs_dir_item_key_to_cpu()(extent_buffer *eb,
2026 				      btrfs_dir_item *item,
2027 				      btrfs_key *key)
2028 {
2029 	btrfs_disk_key disk_key;
2030 	btrfs_dir_item_key(eb, item, &disk_key);
2031 	btrfs_disk_key_to_cpu(key, &disk_key);
2032 }
2033 
2034 /* struct btrfs_header */
2035 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_bytenr}, btrfs_header, q{bytenr}, q{64});
2036 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_generation}, btrfs_header,
2037 			  q{generation}, q{64});
2038 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_owner}, btrfs_header, q{owner}, q{64});
2039 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_nritems}, btrfs_header, q{nritems}, q{32});
2040 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_flags}, btrfs_header, q{flags}, q{64});
2041 mixin BTRFS_SETGET_HEADER_FUNCS!(q{header_level}, btrfs_header, q{level}, q{8});
2042 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_header_bytenr}, btrfs_header, q{bytenr}, q{64});
2043 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_header_nritems}, btrfs_header, q{nritems},
2044 			 q{32});
2045 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_header_owner}, btrfs_header, q{owner}, q{64});
2046 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_header_generation}, btrfs_header,
2047 			 q{generation}, q{64});
2048 
2049 int btrfs_header_flag(extent_buffer *eb, u64 flag)
2050 {
2051 	return (btrfs_header_flags(eb) & flag) == flag;
2052 }
2053 
2054 int btrfs_set_header_flag(extent_buffer *eb, u64 flag)
2055 {
2056 	u64 flags = btrfs_header_flags(eb);
2057 	btrfs_set_header_flags(eb, flags | flag);
2058 	return (flags & flag) == flag;
2059 }
2060 
2061 int btrfs_clear_header_flag(extent_buffer *eb, u64 flag)
2062 {
2063 	u64 flags = btrfs_header_flags(eb);
2064 	btrfs_set_header_flags(eb, flags & ~flag);
2065 	return (flags & flag) == flag;
2066 }
2067 
2068 int btrfs_header_backref_rev(extent_buffer *eb)
2069 {
2070 	u64 flags = btrfs_header_flags(eb);
2071 	return flags >> BTRFS_BACKREF_REV_SHIFT;
2072 }
2073 
2074 void btrfs_set_header_backref_rev()(extent_buffer *eb,
2075 						int rev)
2076 {
2077 	u64 flags = btrfs_header_flags(eb);
2078 	flags &= ~BTRFS_BACKREF_REV_MASK;
2079 	flags |= cast(u64)rev << BTRFS_BACKREF_REV_SHIFT;
2080 	btrfs_set_header_flags(eb, flags);
2081 }
2082 
2083 ulong btrfs_header_fsid()
2084 {
2085 	return btrfs_header.fsid.offsetof;
2086 }
2087 
2088 ulong btrfs_header_chunk_tree_uuid(extent_buffer *eb)
2089 {
2090 	return btrfs_header.chunk_tree_uuid.offsetof;
2091 }
2092 
2093 u8 *btrfs_header_csum(extent_buffer *eb)
2094 {
2095 	ulong ptr = btrfs_header.csum.offsetof;
2096 	return cast(u8*)ptr;
2097 }
2098 
2099 int btrfs_is_leaf(extent_buffer *eb)
2100 {
2101 	return (btrfs_header_level(eb) == 0);
2102 }
2103 
2104 /* struct btrfs_root_item */
2105 mixin BTRFS_SETGET_FUNCS!(q{disk_root_generation}, btrfs_root_item,
2106 		   q{generation}, q{64});
2107 mixin BTRFS_SETGET_FUNCS!(q{disk_root_refs}, btrfs_root_item, q{refs}, q{32});
2108 mixin BTRFS_SETGET_FUNCS!(q{disk_root_bytenr}, btrfs_root_item, q{bytenr}, q{64});
2109 mixin BTRFS_SETGET_FUNCS!(q{disk_root_level}, btrfs_root_item, q{level}, q{8});
2110 
2111 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_generation}, btrfs_root_item,
2112 			 q{generation}, q{64});
2113 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_bytenr}, btrfs_root_item, q{bytenr}, q{64});
2114 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_level}, btrfs_root_item, q{level}, q{8});
2115 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_dirid}, btrfs_root_item, q{root_dirid}, q{64});
2116 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_refs}, btrfs_root_item, q{refs}, q{32});
2117 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_flags}, btrfs_root_item, q{flags}, q{64});
2118 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_used}, btrfs_root_item, q{bytes_used}, q{64});
2119 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_limit}, btrfs_root_item, q{byte_limit}, q{64});
2120 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_last_snapshot}, btrfs_root_item,
2121 			 q{last_snapshot}, q{64});
2122 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_generation_v2}, btrfs_root_item,
2123 			 q{generation_v2}, q{64});
2124 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_ctransid}, btrfs_root_item,
2125 			 q{ctransid}, q{64});
2126 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_otransid}, btrfs_root_item,
2127 			 q{otransid}, q{64});
2128 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_stransid}, btrfs_root_item,
2129 			 q{stransid}, q{64});
2130 mixin BTRFS_SETGET_STACK_FUNCS!(q{root_rtransid}, btrfs_root_item,
2131 			 q{rtransid}, q{64});
2132 
2133 btrfs_timespec* btrfs_root_ctime(
2134 		btrfs_root_item *root_item)
2135 {
2136 	ulong ptr = cast(ulong)root_item;
2137 	ptr += btrfs_root_item.ctime.offsetof;
2138 	return cast(btrfs_timespec*)ptr;
2139 }
2140 
2141 btrfs_timespec* btrfs_root_otime(
2142 		btrfs_root_item *root_item)
2143 {
2144 	ulong ptr = cast(ulong)root_item;
2145 	ptr += btrfs_root_item.otime.offsetof;
2146 	return cast(btrfs_timespec*)ptr;
2147 }
2148 
2149 btrfs_timespec* btrfs_root_stime(
2150 		btrfs_root_item *root_item)
2151 {
2152 	ulong ptr = cast(ulong)root_item;
2153 	ptr += btrfs_root_item.stime.offsetof;
2154 	return cast(btrfs_timespec*)ptr;
2155 }
2156 
2157 btrfs_timespec* btrfs_root_rtime(
2158 		btrfs_root_item *root_item)
2159 {
2160 	ulong ptr = cast(ulong)root_item;
2161 	ptr += btrfs_root_item.rtime.offsetof;
2162 	return cast(btrfs_timespec*)ptr;
2163 }
2164 
2165 /* struct btrfs_root_backup */
2166 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_tree_root}, btrfs_root_backup,
2167 		   q{tree_root}, q{64});
2168 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_tree_root_gen}, btrfs_root_backup,
2169 		   q{tree_root_gen}, q{64});
2170 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_tree_root_level}, btrfs_root_backup,
2171 		   q{tree_root_level}, q{8});
2172 
2173 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_chunk_root}, btrfs_root_backup,
2174 		   q{chunk_root}, q{64});
2175 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_chunk_root_gen}, btrfs_root_backup,
2176 		   q{chunk_root_gen}, q{64});
2177 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_chunk_root_level}, btrfs_root_backup,
2178 		   q{chunk_root_level}, q{8});
2179 
2180 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_extent_root}, btrfs_root_backup,
2181 		   q{extent_root}, q{64});
2182 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_extent_root_gen}, btrfs_root_backup,
2183 		   q{extent_root_gen}, q{64});
2184 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_extent_root_level}, btrfs_root_backup,
2185 		   q{extent_root_level}, q{8});
2186 
2187 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_fs_root}, btrfs_root_backup,
2188 		   q{fs_root}, q{64});
2189 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_fs_root_gen}, btrfs_root_backup,
2190 		   q{fs_root_gen}, q{64});
2191 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_fs_root_level}, btrfs_root_backup,
2192 		   q{fs_root_level}, q{8});
2193 
2194 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_dev_root}, btrfs_root_backup,
2195 		   q{dev_root}, q{64});
2196 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_dev_root_gen}, btrfs_root_backup,
2197 		   q{dev_root_gen}, q{64});
2198 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_dev_root_level}, btrfs_root_backup,
2199 		   q{dev_root_level}, q{8});
2200 
2201 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_csum_root}, btrfs_root_backup,
2202 		   q{csum_root}, q{64});
2203 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_csum_root_gen}, btrfs_root_backup,
2204 		   q{csum_root_gen}, q{64});
2205 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_csum_root_level}, btrfs_root_backup,
2206 		   q{csum_root_level}, q{8});
2207 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_total_bytes}, btrfs_root_backup,
2208 		   q{total_bytes}, q{64});
2209 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_bytes_used}, btrfs_root_backup,
2210 		   q{bytes_used}, q{64});
2211 mixin BTRFS_SETGET_STACK_FUNCS!(q{backup_num_devices}, btrfs_root_backup,
2212 		   q{num_devices}, q{64});
2213 
2214 /* struct btrfs_super_block */
2215 
2216 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_bytenr}, btrfs_super_block, q{bytenr}, q{64});
2217 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_flags}, btrfs_super_block, q{flags}, q{64});
2218 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_generation}, btrfs_super_block,
2219 			 q{generation}, q{64});
2220 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_root}, btrfs_super_block, q{root}, q{64});
2221 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_sys_array_size},
2222 			 btrfs_super_block, q{sys_chunk_array_size}, q{32});
2223 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_chunk_root_generation},
2224 			 btrfs_super_block, q{chunk_root_generation}, q{64});
2225 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_root_level}, btrfs_super_block,
2226 			 q{root_level}, q{8});
2227 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_chunk_root}, btrfs_super_block,
2228 			 q{chunk_root}, q{64});
2229 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_chunk_root_level}, btrfs_super_block,
2230 			 q{chunk_root_level}, q{8});
2231 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_log_root}, btrfs_super_block,
2232 			 q{log_root}, q{64});
2233 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_log_root_transid}, btrfs_super_block,
2234 			 q{log_root_transid}, q{64});
2235 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_log_root_level}, btrfs_super_block,
2236 			 q{log_root_level}, q{8});
2237 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_total_bytes}, btrfs_super_block,
2238 			 q{total_bytes}, q{64});
2239 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_bytes_used}, btrfs_super_block,
2240 			 q{bytes_used}, q{64});
2241 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_sectorsize}, btrfs_super_block,
2242 			 q{sectorsize}, q{32});
2243 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_nodesize}, btrfs_super_block,
2244 			 q{nodesize}, q{32});
2245 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_stripesize}, btrfs_super_block,
2246 			 q{stripesize}, q{32});
2247 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_root_dir}, btrfs_super_block,
2248 			 q{root_dir_objectid}, q{64});
2249 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_num_devices}, btrfs_super_block,
2250 			 q{num_devices}, q{64});
2251 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_compat_flags}, btrfs_super_block,
2252 			 q{compat_flags}, q{64});
2253 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_compat_ro_flags}, btrfs_super_block,
2254 			 q{compat_ro_flags}, q{64});
2255 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_incompat_flags}, btrfs_super_block,
2256 			 q{incompat_flags}, q{64});
2257 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_csum_type}, btrfs_super_block,
2258 			 q{csum_type}, q{16});
2259 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_cache_generation}, btrfs_super_block,
2260 			 q{cache_generation}, q{64});
2261 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_uuid_tree_generation}, btrfs_super_block,
2262 			 q{uuid_tree_generation}, q{64});
2263 mixin BTRFS_SETGET_STACK_FUNCS!(q{super_magic}, btrfs_super_block, q{magic}, q{64});
2264 
2265 ulong btrfs_leaf_data(extent_buffer *l)
2266 {
2267 	return btrfs_leaf.items.offsetof;
2268 }
2269 
2270 /* struct btrfs_file_extent_item */
2271 mixin BTRFS_SETGET_FUNCS!(q{file_extent_type}, btrfs_file_extent_item, q{type}, q{8});
2272 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_type}, btrfs_file_extent_item, q{type}, q{8});
2273 
2274 ulong btrfs_file_extent_inline_start(btrfs_file_extent_item *e)
2275 {
2276 	ulong offset = cast(ulong)e;
2277 	offset += btrfs_file_extent_item.disk_bytenr.offsetof;
2278 	return offset;
2279 }
2280 
2281 u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2282 {
2283 	return cast(u32)btrfs_file_extent_item.disk_bytenr.offsetof + datasize;
2284 }
2285 
2286 mixin BTRFS_SETGET_FUNCS!(q{file_extent_disk_bytenr}, btrfs_file_extent_item,
2287 		   q{disk_bytenr}, q{64});
2288 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_disk_bytenr}, btrfs_file_extent_item,
2289 		   q{disk_bytenr}, q{64});
2290 mixin BTRFS_SETGET_FUNCS!(q{file_extent_generation}, btrfs_file_extent_item,
2291 		   q{generation}, q{64});
2292 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_generation}, btrfs_file_extent_item,
2293 		   q{generation}, q{64});
2294 mixin BTRFS_SETGET_FUNCS!(q{file_extent_disk_num_bytes}, btrfs_file_extent_item,
2295 		   q{disk_num_bytes}, q{64});
2296 mixin BTRFS_SETGET_FUNCS!(q{file_extent_offset}, btrfs_file_extent_item,
2297 		  q{offset}, q{64});
2298 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_offset}, btrfs_file_extent_item,
2299 		  q{offset}, q{64});
2300 mixin BTRFS_SETGET_FUNCS!(q{file_extent_num_bytes}, btrfs_file_extent_item,
2301 		   q{num_bytes}, q{64});
2302 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_num_bytes}, btrfs_file_extent_item,
2303 		   q{num_bytes}, q{64});
2304 mixin BTRFS_SETGET_FUNCS!(q{file_extent_ram_bytes}, btrfs_file_extent_item,
2305 		   q{ram_bytes}, q{64});
2306 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_ram_bytes}, btrfs_file_extent_item,
2307 		   q{ram_bytes}, q{64});
2308 mixin BTRFS_SETGET_FUNCS!(q{file_extent_compression}, btrfs_file_extent_item,
2309 		   q{compression}, q{8});
2310 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_file_extent_compression}, btrfs_file_extent_item,
2311 		   q{compression}, q{8});
2312 mixin BTRFS_SETGET_FUNCS!(q{file_extent_encryption}, btrfs_file_extent_item,
2313 		   q{encryption}, q{8});
2314 mixin BTRFS_SETGET_FUNCS!(q{file_extent_other_encoding}, btrfs_file_extent_item,
2315 		   q{other_encoding}, q{16});
2316 
2317 /* btrfs_qgroup_status_item */
2318 mixin BTRFS_SETGET_FUNCS!(q{qgroup_status_version}, btrfs_qgroup_status_item,
2319 		   q{version_}, q{64});
2320 mixin BTRFS_SETGET_FUNCS!(q{qgroup_status_generation}, btrfs_qgroup_status_item,
2321 		   q{generation}, q{64});
2322 mixin BTRFS_SETGET_FUNCS!(q{qgroup_status_flags}, btrfs_qgroup_status_item,
2323 		   q{flags}, q{64});
2324 mixin BTRFS_SETGET_FUNCS!(q{qgroup_status_rescan}, btrfs_qgroup_status_item,
2325 		   q{rescan}, q{64});
2326 
2327 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_status_version},
2328 			 btrfs_qgroup_status_item, q{version_}, q{64});
2329 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_status_generation},
2330 			 btrfs_qgroup_status_item, q{generation}, q{64});
2331 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_status_flags},
2332 			 btrfs_qgroup_status_item, q{flags}, q{64});
2333 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_status_rescan},
2334 			 btrfs_qgroup_status_item, q{rescan}, q{64});
2335 
2336 /* btrfs_qgroup_info_item */
2337 mixin BTRFS_SETGET_FUNCS!(q{qgroup_info_generation}, btrfs_qgroup_info_item,
2338 		   q{generation}, q{64});
2339 mixin BTRFS_SETGET_FUNCS!(q{qgroup_info_referenced}, btrfs_qgroup_info_item,
2340 		   q{referenced}, q{64});
2341 mixin BTRFS_SETGET_FUNCS!(q{qgroup_info_referenced_compressed},
2342 		   btrfs_qgroup_info_item, q{referenced_compressed}, q{64});
2343 mixin BTRFS_SETGET_FUNCS!(q{qgroup_info_exclusive}, btrfs_qgroup_info_item,
2344 		   q{exclusive}, q{64});
2345 mixin BTRFS_SETGET_FUNCS!(q{qgroup_info_exclusive_compressed},
2346 		   btrfs_qgroup_info_item, q{exclusive_compressed}, q{64});
2347 
2348 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_info_generation},
2349 			 btrfs_qgroup_info_item, q{generation}, q{64});
2350 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_info_referenced},
2351 			 btrfs_qgroup_info_item, q{referenced}, q{64});
2352 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_info_referenced_compressed},
2353 		   btrfs_qgroup_info_item, q{referenced_compressed}, q{64});
2354 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_info_exclusive},
2355 			 btrfs_qgroup_info_item, q{exclusive}, q{64});
2356 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_info_exclusive_compressed},
2357 		   btrfs_qgroup_info_item, q{exclusive_compressed}, q{64});
2358 
2359 /* btrfs_qgroup_limit_item */
2360 mixin BTRFS_SETGET_FUNCS!(q{qgroup_limit_flags}, btrfs_qgroup_limit_item,
2361 		   q{flags}, q{64});
2362 mixin BTRFS_SETGET_FUNCS!(q{qgroup_limit_max_referenced}, btrfs_qgroup_limit_item,
2363 		   q{max_referenced}, q{64});
2364 mixin BTRFS_SETGET_FUNCS!(q{qgroup_limit_max_exclusive}, btrfs_qgroup_limit_item,
2365 		   q{max_exclusive}, q{64});
2366 mixin BTRFS_SETGET_FUNCS!(q{qgroup_limit_rsv_referenced}, btrfs_qgroup_limit_item,
2367 		   q{rsv_referenced}, q{64});
2368 mixin BTRFS_SETGET_FUNCS!(q{qgroup_limit_rsv_exclusive}, btrfs_qgroup_limit_item,
2369 		   q{rsv_exclusive}, q{64});
2370 
2371 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_limit_flags},
2372 			 btrfs_qgroup_limit_item, q{flags}, q{64});
2373 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_limit_max_referenced},
2374 			 btrfs_qgroup_limit_item, q{max_referenced}, q{64});
2375 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_limit_max_exclusive},
2376 			 btrfs_qgroup_limit_item, q{max_exclusive}, q{64});
2377 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_limit_rsv_referenced},
2378 			 btrfs_qgroup_limit_item, q{rsv_referenced}, q{64});
2379 mixin BTRFS_SETGET_STACK_FUNCS!(q{stack_qgroup_limit_rsv_exclusive},
2380 			 btrfs_qgroup_limit_item, q{rsv_exclusive}, q{64});
2381 
2382 /* btrfs_balance_item */
2383 mixin BTRFS_SETGET_FUNCS!(q{balance_item_flags}, btrfs_balance_item, q{flags}, q{64});
2384 
2385 btrfs_disk_balance_args* btrfs_balance_item_data(
2386 		extent_buffer *eb, btrfs_balance_item *bi)
2387 {
2388 	ulong offset = cast(ulong)bi;
2389 	btrfs_balance_item *p;
2390 	p = cast(btrfs_balance_item *)(eb.data.ptr + offset);
2391 	return &p.data;
2392 }
2393 
2394 btrfs_disk_balance_args* btrfs_balance_item_meta(
2395 		extent_buffer *eb, btrfs_balance_item *bi)
2396 {
2397 	ulong offset = cast(ulong)bi;
2398 	btrfs_balance_item *p;
2399 	p = cast(btrfs_balance_item *)(eb.data.ptr + offset);
2400 	return &p.meta;
2401 }
2402 
2403 btrfs_disk_balance_args* btrfs_balance_item_sys(
2404 		extent_buffer *eb, btrfs_balance_item *bi)
2405 {
2406 	ulong offset = cast(ulong)bi;
2407 	btrfs_balance_item *p;
2408 	p = cast(btrfs_balance_item *)(eb.data.ptr + offset);
2409 	return &p.sys;
2410 }
2411 
2412 u64 btrfs_dev_stats_value()(const extent_buffer *eb,
2413 					const btrfs_dev_stats_item *ptr,
2414 					int index)
2415 {
2416 	u64 val;
2417 
2418 	read_extent_buffer(eb, &val,
2419 			   btrfs_dev_stats_item.values.offsetof +
2420 			    (cast(ulong)ptr) + (index * u64.sizeof),
2421 			   val.sizeof);
2422 	return val;
2423 }
2424 
2425 /*
2426  * this returns the number of bytes used by the item on disk, minus the
2427  * size of any extent headers.  If a file is compressed on disk, this is
2428  * the compressed size
2429  */
2430 u32 btrfs_file_extent_inline_item_len()(extent_buffer *eb,
2431 						    btrfs_item *e)
2432 {
2433        ulong offset;
2434        offset = btrfs_file_extent_item.disk_bytenr.offsetof;
2435        return cast(u32)(btrfs_item_size(eb, e) - offset);
2436 }
2437 
2438 /* struct btrfs_ioctl_search_header */
2439 u64 btrfs_search_header_transid(btrfs_ioctl_search_header *sh)
2440 {
2441 	return get_unaligned_64(&sh.transid);
2442 }
2443 
2444 u64 btrfs_search_header_objectid(btrfs_ioctl_search_header *sh)
2445 {
2446 	return get_unaligned_64(&sh.objectid);
2447 }
2448 
2449 u64 btrfs_search_header_offset(btrfs_ioctl_search_header *sh)
2450 {
2451 	return get_unaligned_64(&sh.offset);
2452 }
2453 
2454 u32 btrfs_search_header_type(btrfs_ioctl_search_header *sh)
2455 {
2456 	return get_unaligned_32(&sh.type);
2457 }
2458 
2459 u32 btrfs_search_header_len(btrfs_ioctl_search_header *sh)
2460 {
2461 	return get_unaligned_32(&sh.len);
2462 }
2463 
2464 bool btrfs_fs_incompat(string opt)(btrfs_fs_info *fs_info)
2465 {
2466 	return __btrfs_fs_incompat(fs_info, mixin(q{BTRFS_FEATURE_INCOMPAT_} ~ opt));
2467 }
2468 
2469 bool __btrfs_fs_incompat(btrfs_fs_info *fs_info, u64 flag)
2470 {
2471 	btrfs_super_block *disk_super;
2472 	disk_super = fs_info.super_copy;
2473 	return !!(btrfs_super_incompat_flags(disk_super) & flag);
2474 }
2475 
2476 bool btrfs_fs_incompat_ro(string opt)(btrfs_fs_info *fs_info)
2477 {
2478 	return __btrfs_fs_incompat(fs_info, mixin(q{BTRFS_FEATURE_INCOMPAT_RO_} ~ opt));
2479 }
2480 
2481 int __btrfs_fs_compat_ro(btrfs_fs_info *fs_info, u64 flag)
2482 {
2483 	btrfs_super_block *disk_super;
2484 	disk_super = fs_info.super_copy;
2485 	return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
2486 }
2487 
2488 /* helper function to cast into the data area of the leaf. */
2489 type* btrfs_item_ptr(type)(extent_buffer *leaf, int slot)
2490 {
2491 	return cast(type *)(btrfs_leaf_data(leaf) +
2492 		btrfs_item_offset_nr(leaf, slot));
2493 }
2494 
2495 ulong btrfs_item_ptr_offset()(extent_buffer *leaf, int slot)
2496 {
2497 	return cast(ulong)(btrfs_leaf_data(leaf) +
2498 		btrfs_item_offset_nr(leaf, slot));
2499 }
2500 
2501 u64 btrfs_name_hash()(const char *name, int len)
2502 {
2503 	return crc32c((u32)~1, name, len);
2504 }
2505 
2506 /*
2507  * Figure the key offset of an extended inode ref
2508  */
2509 u64 btrfs_extref_hash()(u64 parent_objectid, const char *name,
2510 				    int len)
2511 {
2512 	return cast(u64)btrfs_crc32c(parent_objectid, name, len);
2513 }
2514 
2515 /* extent-tree.c */
2516 int btrfs_reserve_extent(btrfs_trans_handle *trans,
2517 			 btrfs_root *root,
2518 			 u64 num_bytes, u64 empty_size,
2519 			 u64 hint_byte, u64 search_end,
2520 			 btrfs_key *ins, bool is_data);
2521 int btrfs_fix_block_accounting(btrfs_trans_handle *trans);
2522 void btrfs_pin_extent(btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes);
2523 void btrfs_unpin_extent(btrfs_fs_info *fs_info,
2524 			u64 bytenr, u64 num_bytes);
2525 btrfs_block_group *btrfs_lookup_block_group(btrfs_fs_info *info,
2526 						   u64 bytenr);
2527 btrfs_block_group *btrfs_lookup_first_block_group(
2528 						       btrfs_fs_info *info,
2529 						       u64 bytenr);
2530 extent_buffer *btrfs_alloc_free_block(btrfs_trans_handle *trans,
2531 					btrfs_root *root,
2532 					u32 blocksize, u64 root_objectid,
2533 					btrfs_disk_key *key, int level,
2534 					u64 hint, u64 empty_size);
2535 int btrfs_lookup_extent_info(btrfs_trans_handle *trans,
2536 			     btrfs_fs_info *fs_info, u64 bytenr,
2537 			     u64 offset, int metadata, u64 *refs, u64 *flags);
2538 int btrfs_set_block_flags(btrfs_trans_handle *trans, u64 bytenr,
2539 			  int level, u64 flags);
2540 int btrfs_inc_ref(btrfs_trans_handle *trans, btrfs_root *root,
2541 		  extent_buffer *buf, int record_parent);
2542 int btrfs_dec_ref(btrfs_trans_handle *trans, btrfs_root *root,
2543 		  extent_buffer *buf, int record_parent);
2544 int btrfs_free_tree_block(btrfs_trans_handle *trans,
2545 			  btrfs_root *root,
2546 			  extent_buffer *buf,
2547 			  u64 parent, int last_ref);
2548 int btrfs_free_extent(btrfs_trans_handle *trans,
2549 		      btrfs_root *root,
2550 		      u64 bytenr, u64 num_bytes, u64 parent,
2551 		      u64 root_objectid, u64 owner, u64 offset);
2552 void btrfs_finish_extent_commit(btrfs_trans_handle *trans);
2553 int btrfs_inc_extent_ref(btrfs_trans_handle *trans,
2554 			 btrfs_root *root,
2555 			 u64 bytenr, u64 num_bytes, u64 parent,
2556 			 u64 root_objectid, u64 owner, u64 offset);
2557 int btrfs_update_extent_ref(btrfs_trans_handle *trans,
2558 			    btrfs_root *root, u64 bytenr,
2559 			    u64 orig_parent, u64 parent,
2560 			    u64 root_objectid, u64 ref_generation,
2561 			    u64 owner_objectid);
2562 int btrfs_write_dirty_block_groups(btrfs_trans_handle *trans);
2563 int update_space_info(btrfs_fs_info *info, u64 flags,
2564 		      u64 total_bytes, u64 bytes_used,
2565 		      btrfs_space_info **space_info);
2566 int btrfs_free_block_groups(btrfs_fs_info *info);
2567 int btrfs_read_block_groups(btrfs_fs_info *info);
2568 btrfs_block_group *
2569 btrfs_add_block_group(btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
2570 		      u64 chunk_offset, u64 size);
2571 int btrfs_make_block_group(btrfs_trans_handle *trans,
2572 			   btrfs_fs_info *fs_info, u64 bytes_used,
2573 			   u64 type, u64 chunk_offset, u64 size);
2574 int btrfs_make_block_groups(btrfs_trans_handle *trans,
2575 			    btrfs_fs_info *fs_info);
2576 int btrfs_update_block_group(btrfs_trans_handle *trans, u64 bytenr,
2577 			     u64 num, int alloc, int mark_free);
2578 int btrfs_record_file_extent(btrfs_trans_handle *trans,
2579 			      btrfs_root *root, u64 objectid,
2580 			      btrfs_inode_item *inode,
2581 			      u64 file_pos, u64 disk_bytenr,
2582 			      u64 num_bytes);
2583 int btrfs_remove_block_group(btrfs_trans_handle *trans,
2584 			     u64 bytenr, u64 len);
2585 void free_excluded_extents(btrfs_fs_info *fs_info,
2586 			   btrfs_block_group *cache);
2587 int exclude_super_stripes(btrfs_fs_info *fs_info,
2588 			  btrfs_block_group *cache);
2589 u64 add_new_free_space(btrfs_block_group *block_group,
2590 		       btrfs_fs_info *info, u64 start, u64 end);
2591 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
2592 
2593 /* ctree.c */
2594 int btrfs_comp_cpu_keys(const btrfs_key *k1, const btrfs_key *k2);
2595 int btrfs_del_ptr(btrfs_root *root, btrfs_path *path,
2596 		int level, int slot);
2597 btrfs_tree_block_status
2598 btrfs_check_node(btrfs_fs_info *fs_info,
2599 		 btrfs_disk_key *parent_key, extent_buffer *buf);
2600 btrfs_tree_block_status
2601 btrfs_check_leaf(btrfs_fs_info *fs_info,
2602 		 btrfs_disk_key *parent_key, extent_buffer *buf);
2603 void reada_for_search(btrfs_fs_info *fs_info, btrfs_path *path,
2604 		      int level, int slot, u64 objectid);
2605 extent_buffer *read_node_slot(btrfs_fs_info *fs_info,
2606 				   extent_buffer *parent, int slot);
2607 int btrfs_previous_item(btrfs_root *root,
2608 			btrfs_path *path, u64 min_objectid,
2609 			int type);
2610 int btrfs_previous_extent_item(btrfs_root *root,
2611 			btrfs_path *path, u64 min_objectid);
2612 int btrfs_next_extent_item(btrfs_root *root,
2613 			btrfs_path *path, u64 max_objectid);
2614 int btrfs_cow_block(btrfs_trans_handle *trans,
2615 		    btrfs_root *root, extent_buffer *buf,
2616 		    extent_buffer *parent, int parent_slot,
2617 		    extent_buffer **cow_ret);
2618 int __btrfs_cow_block(btrfs_trans_handle *trans,
2619 			     btrfs_root *root,
2620 			     extent_buffer *buf,
2621 			     extent_buffer *parent, int parent_slot,
2622 			     extent_buffer **cow_ret,
2623 			     u64 search_start, u64 empty_size);
2624 int btrfs_copy_root(btrfs_trans_handle *trans,
2625 		      btrfs_root *root,
2626 		      extent_buffer *buf,
2627 		      extent_buffer **cow_ret, u64 new_root_objectid);
2628 int btrfs_create_root(btrfs_trans_handle *trans,
2629 		      btrfs_fs_info *fs_info, u64 objectid);
2630 int btrfs_extend_item(btrfs_root *root, btrfs_path *path,
2631 		u32 data_size);
2632 int btrfs_truncate_item(btrfs_root *root, btrfs_path *path,
2633 			u32 new_size, int from_end);
2634 int btrfs_split_item(btrfs_trans_handle *trans,
2635 		     btrfs_root *root,
2636 		     btrfs_path *path,
2637 		     btrfs_key *new_key,
2638 		     ulong split_offset);
2639 int btrfs_search_slot(btrfs_trans_handle *trans,
2640 		btrfs_root *root, const btrfs_key *key,
2641 		btrfs_path *p, int ins_len, int cow);
2642 int btrfs_search_slot_for_read(btrfs_root *root,
2643                                const btrfs_key *key,
2644                                btrfs_path *p, int find_higher,
2645                                int return_any);
2646 int btrfs_bin_search(extent_buffer *eb, const btrfs_key *key,
2647 		     int *slot);
2648 int btrfs_find_item(btrfs_root *fs_root, btrfs_path *found_path,
2649 		u64 iobjectid, u64 ioff, u8 key_type,
2650 		btrfs_key *found_key);
2651 void btrfs_release_path(btrfs_path *p);
2652 void add_root_to_dirty_list(btrfs_root *root);
2653 btrfs_path *btrfs_alloc_path();
2654 void btrfs_free_path(btrfs_path *p);
2655 void btrfs_init_path(btrfs_path *p);
2656 int btrfs_del_items(btrfs_trans_handle *trans, btrfs_root *root,
2657 		   btrfs_path *path, int slot, int nr);
2658 
2659 int btrfs_del_item()(btrfs_trans_handle *trans,
2660 				 btrfs_root *root,
2661 				 btrfs_path *path)
2662 {
2663 	return btrfs_del_items(trans, root, path, path.slots[0], 1);
2664 }
2665 
2666 int btrfs_insert_item(btrfs_trans_handle *trans, btrfs_root
2667 		      *root, btrfs_key *key, void *data, u32 data_size);
2668 int btrfs_insert_empty_items(btrfs_trans_handle *trans,
2669 			     btrfs_root *root,
2670 			     btrfs_path *path,
2671 			     btrfs_key *cpu_key, u32 *data_size, int nr);
2672 
2673 int btrfs_insert_empty_item()(btrfs_trans_handle *trans,
2674 					  btrfs_root *root,
2675 					  btrfs_path *path,
2676 					  btrfs_key *key,
2677 					  u32 data_size)
2678 {
2679 	return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
2680 }
2681 
2682 int btrfs_next_sibling_tree_block(btrfs_fs_info *fs_info,
2683 				  btrfs_path *path);
2684 
2685 /*
2686  * Walk up the tree as far as necessary to find the next leaf.
2687  *
2688  * returns 0 if it found something or 1 if there are no greater leaves.
2689  * returns < 0 on io errors.
2690  */
2691 int btrfs_next_leaf()(btrfs_root *root,
2692 				  btrfs_path *path)
2693 {
2694 	path.lowest_level = 0;
2695 	return btrfs_next_sibling_tree_block(root.fs_info, path);
2696 }
2697 
2698 int btrfs_next_item()(btrfs_root *root,
2699 				  btrfs_path *p)
2700 {
2701 	++p.slots[0];
2702 	if (p.slots[0] >= btrfs_header_nritems(p.nodes[0]))
2703 		return btrfs_next_leaf(root, p);
2704 	return 0;
2705 }
2706 
2707 int btrfs_prev_leaf(btrfs_root *root, btrfs_path *path);
2708 int btrfs_leaf_free_space(extent_buffer *leaf);
2709 void btrfs_fixup_low_keys(btrfs_root *root, btrfs_path *path,
2710 			  btrfs_disk_key *key, int level);
2711 int btrfs_set_item_key_safe(btrfs_root *root, btrfs_path *path,
2712 			    btrfs_key *new_key);
2713 void btrfs_set_item_key_unsafe(btrfs_root *root,
2714 			       btrfs_path *path,
2715 			       btrfs_key *new_key);
2716 
2717 u16 btrfs_super_csum_size(const btrfs_super_block *s);
2718 const(char)*btrfs_super_csum_name(u16 csum_type);
2719 u16 btrfs_csum_type_size(u16 csum_type);
2720 size_t btrfs_super_num_csums();
2721 
2722 /* root-item.c */
2723 int btrfs_add_root_ref(btrfs_trans_handle *trans,
2724 		       btrfs_root *tree_root,
2725 		       u64 root_id, u8 type, u64 ref_id,
2726 		       u64 dirid, u64 sequence,
2727 		       const char *name, int name_len);
2728 int btrfs_insert_root(btrfs_trans_handle *trans, btrfs_root
2729 		      *root, btrfs_key *key, btrfs_root_item
2730 		      *item);
2731 int btrfs_del_root(btrfs_trans_handle *trans, btrfs_root *root,
2732 		   btrfs_key *key);
2733 int btrfs_update_root(btrfs_trans_handle *trans, btrfs_root
2734 		      *root, btrfs_key *key, btrfs_root_item
2735 		      *item);
2736 int btrfs_find_last_root(btrfs_root *root, u64 objectid,
2737 			 btrfs_root_item *item, btrfs_key *key);
2738 /* dir-item.c */
2739 int btrfs_insert_dir_item(btrfs_trans_handle *trans, btrfs_root
2740 			  *root, const char *name, int name_len, u64 dir,
2741 			  btrfs_key *location, u8 type, u64 index);
2742 btrfs_dir_item *btrfs_lookup_dir_item(btrfs_trans_handle *trans,
2743 					     btrfs_root *root,
2744 					     btrfs_path *path, u64 dir,
2745 					     const char *name, int name_len,
2746 					     int mod);
2747 btrfs_dir_item *btrfs_lookup_dir_index_item(btrfs_trans_handle *trans,
2748 					btrfs_root *root,
2749 					btrfs_path *path, u64 dir,
2750 					u64 objectid, const char *name, int name_len,
2751 					int mod);
2752 int btrfs_delete_one_dir_name(btrfs_trans_handle *trans,
2753 			      btrfs_root *root,
2754 			      btrfs_path *path,
2755 			      btrfs_dir_item *di);
2756 int btrfs_insert_xattr_item(btrfs_trans_handle *trans,
2757 			    btrfs_root *root, const char *name,
2758 			    u16 name_len, const void *data, u16 data_len,
2759 			    u64 dir);
2760 btrfs_dir_item *btrfs_match_dir_item_name(btrfs_root *root,
2761 			      btrfs_path *path,
2762 			      const char *name, int name_len);
2763 
2764 /* inode-item.c */
2765 int btrfs_insert_inode_ref(btrfs_trans_handle *trans,
2766 			   btrfs_root *root,
2767 			   const char *name, int name_len,
2768 			   u64 inode_objectid, u64 ref_objectid, u64 index);
2769 int btrfs_insert_inode(btrfs_trans_handle *trans, btrfs_root
2770 		       *root, u64 objectid, btrfs_inode_item
2771 		       *inode_item);
2772 int btrfs_lookup_inode(btrfs_trans_handle *trans, btrfs_root
2773 		       *root, btrfs_path *path,
2774 		       btrfs_key *location, int mod);
2775 btrfs_inode_extref *btrfs_lookup_inode_extref(btrfs_trans_handle
2776 		*trans, btrfs_path *path, btrfs_root *root,
2777 		u64 ino, u64 parent_ino, u64 index, const char *name,
2778 		int namelen, int ins_len);
2779 int btrfs_del_inode_extref(btrfs_trans_handle *trans,
2780 			   btrfs_root *root,
2781 			   const char *name, int name_len,
2782 			   u64 inode_objectid, u64 ref_objectid,
2783 			   u64 *index);
2784 int btrfs_insert_inode_extref(btrfs_trans_handle *trans,
2785 			      btrfs_root *root,
2786 			      const char *name, int name_len,
2787 			      u64 inode_objectid, u64 ref_objectid, u64 index);
2788 btrfs_inode_ref *btrfs_lookup_inode_ref(btrfs_trans_handle *trans,
2789 		btrfs_root *root, btrfs_path *path,
2790 		const char *name, int namelen, u64 ino, u64 parent_ino,
2791 		int ins_len);
2792 int btrfs_del_inode_ref(btrfs_trans_handle *trans,
2793 			btrfs_root *root, const char *name, int name_len,
2794 			u64 ino, u64 parent_ino, u64 *index);
2795 
2796 /* file-item.c */
2797 int btrfs_del_csums(btrfs_trans_handle *trans, u64 bytenr, u64 len);
2798 int btrfs_insert_file_extent(btrfs_trans_handle *trans,
2799 			     btrfs_root *root,
2800 			     u64 objectid, u64 pos, u64 offset,
2801 			     u64 disk_num_bytes,
2802 			     u64 num_bytes);
2803 int btrfs_insert_inline_extent(btrfs_trans_handle *trans,
2804 				btrfs_root *root, u64 objectid,
2805 				u64 offset, const char *buffer, size_t size);
2806 int btrfs_csum_file_block(btrfs_trans_handle *trans,
2807 			  btrfs_root *root, u64 alloc_end,
2808 			  u64 bytenr, char *data, size_t len);
2809 int btrfs_csum_truncate(btrfs_trans_handle *trans,
2810 			btrfs_root *root, btrfs_path *path,
2811 			u64 isize);
2812 
2813 /* uuid-tree.c, interface for mounted mounted filesystem */
2814 int btrfs_lookup_uuid_subvol_item(int fd, const u8 *uuid, u64 *subvol_id);
2815 int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
2816 					   u64 *subvol_id);
2817 
2818 /* uuid-tree.c, interface for unmounte filesystem */
2819 int btrfs_uuid_tree_add(btrfs_trans_handle *trans, u8 *uuid, u8 type,
2820 			u64 subvol_id_cpu);
2821 
2822 int is_fstree(u64 rootid)
2823 {
2824 	if (rootid == BTRFS_FS_TREE_OBJECTID ||
2825 	    cast(long)rootid >= cast(long)BTRFS_FIRST_FREE_OBJECTID)
2826 		return 1;
2827 	return 0;
2828 }
2829 
2830 void btrfs_uuid_to_key(const u8 *uuid, btrfs_key *key);
2831 
2832 /* inode.c */
2833 int check_dir_conflict(btrfs_root *root, char *name, int namelen,
2834 		u64 dir, u64 index);
2835 int btrfs_new_inode(btrfs_trans_handle *trans, btrfs_root *root,
2836 		u64 ino, u32 mode);
2837 int btrfs_change_inode_flags(btrfs_trans_handle *trans,
2838 			     btrfs_root *root, u64 ino, u64 flags);
2839 int btrfs_add_link(btrfs_trans_handle *trans, btrfs_root *root,
2840 		   u64 ino, u64 parent_ino, char *name, int namelen,
2841 		   u8 type, u64 *index, int add_backref, int ignore_existed);
2842 int btrfs_unlink(btrfs_trans_handle *trans, btrfs_root *root,
2843 		 u64 ino, u64 parent_ino, u64 index, const char *name,
2844 		 int namelen, int add_orphan);
2845 int btrfs_add_orphan_item(btrfs_trans_handle *trans,
2846 			  btrfs_root *root, btrfs_path *path,
2847 			  u64 ino);
2848 int btrfs_mkdir(btrfs_trans_handle *trans, btrfs_root *root,
2849 		char *name, int namelen, u64 parent_ino, u64 *ino, int mode);
2850 btrfs_root *btrfs_mksubvol(btrfs_root *root, const char *base,
2851 				  u64 root_objectid, bool convert);
2852 int btrfs_find_free_objectid(btrfs_trans_handle *trans,
2853 			     btrfs_root *fs_root,
2854 			     u64 dirid, u64 *objectid);
2855 
2856 /* file.c */
2857 int btrfs_get_extent(btrfs_trans_handle *trans,
2858 		     btrfs_root *root,
2859 		     btrfs_path *path,
2860 		     u64 ino, u64 offset, u64 len, int ins_len);
2861 int btrfs_punch_hole(btrfs_trans_handle *trans,
2862 		     btrfs_root *root,
2863 		     u64 ino, u64 offset, u64 len);
2864 int btrfs_read_file(btrfs_root *root, u64 ino, u64 start, int len,
2865 		    char *dest);
2866 
2867 /* extent-tree.c */
2868 int btrfs_run_delayed_refs(btrfs_trans_handle *trans, ulong nr);