xref: /openbmc/linux/fs/btrfs/ctree.h (revision ae213c44)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #ifndef BTRFS_CTREE_H
7 #define BTRFS_CTREE_H
8 
9 #include <linux/mm.h>
10 #include <linux/sched/signal.h>
11 #include <linux/highmem.h>
12 #include <linux/fs.h>
13 #include <linux/rwsem.h>
14 #include <linux/semaphore.h>
15 #include <linux/completion.h>
16 #include <linux/backing-dev.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/kobject.h>
20 #include <trace/events/btrfs.h>
21 #include <asm/kmap_types.h>
22 #include <linux/pagemap.h>
23 #include <linux/btrfs.h>
24 #include <linux/btrfs_tree.h>
25 #include <linux/workqueue.h>
26 #include <linux/security.h>
27 #include <linux/sizes.h>
28 #include <linux/dynamic_debug.h>
29 #include <linux/refcount.h>
30 #include <linux/crc32c.h>
31 #include "extent_io.h"
32 #include "extent_map.h"
33 #include "async-thread.h"
34 
35 struct btrfs_trans_handle;
36 struct btrfs_transaction;
37 struct btrfs_pending_snapshot;
38 struct btrfs_delayed_ref_root;
39 extern struct kmem_cache *btrfs_trans_handle_cachep;
40 extern struct kmem_cache *btrfs_bit_radix_cachep;
41 extern struct kmem_cache *btrfs_path_cachep;
42 extern struct kmem_cache *btrfs_free_space_cachep;
43 struct btrfs_ordered_sum;
44 struct btrfs_ref;
45 
46 #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
47 
48 #define BTRFS_MAX_MIRRORS 3
49 
50 #define BTRFS_MAX_LEVEL 8
51 
52 #define BTRFS_OLDEST_GENERATION	0ULL
53 
54 /*
55  * the max metadata block size.  This limit is somewhat artificial,
56  * but the memmove costs go through the roof for larger blocks.
57  */
58 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
59 
60 /*
61  * we can actually store much bigger names, but lets not confuse the rest
62  * of linux
63  */
64 #define BTRFS_NAME_LEN 255
65 
66 /*
67  * Theoretical limit is larger, but we keep this down to a sane
68  * value. That should limit greatly the possibility of collisions on
69  * inode ref items.
70  */
71 #define BTRFS_LINK_MAX 65535U
72 
73 /* four bytes for CRC32 */
74 static const int btrfs_csum_sizes[] = { 4 };
75 
76 #define BTRFS_EMPTY_DIR_SIZE 0
77 
78 /* ioprio of readahead is set to idle */
79 #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
80 
81 #define BTRFS_DIRTY_METADATA_THRESH	SZ_32M
82 
83 /*
84  * Use large batch size to reduce overhead of metadata updates.  On the reader
85  * side, we only read it when we are close to ENOSPC and the read overhead is
86  * mostly related to the number of CPUs, so it is OK to use arbitrary large
87  * value here.
88  */
89 #define BTRFS_TOTAL_BYTES_PINNED_BATCH	SZ_128M
90 
91 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
92 
93 
94 /*
95  * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
96  */
97 static inline u32 count_max_extents(u64 size)
98 {
99 	return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
100 }
101 
102 struct btrfs_mapping_tree {
103 	struct extent_map_tree map_tree;
104 };
105 
106 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
107 {
108 	BUG_ON(num_stripes == 0);
109 	return sizeof(struct btrfs_chunk) +
110 		sizeof(struct btrfs_stripe) * (num_stripes - 1);
111 }
112 
113 /*
114  * Runtime (in-memory) states of filesystem
115  */
116 enum {
117 	/* Global indicator of serious filesystem errors */
118 	BTRFS_FS_STATE_ERROR,
119 	/*
120 	 * Filesystem is being remounted, allow to skip some operations, like
121 	 * defrag
122 	 */
123 	BTRFS_FS_STATE_REMOUNTING,
124 	/* Track if a transaction abort has been reported on this filesystem */
125 	BTRFS_FS_STATE_TRANS_ABORTED,
126 	/*
127 	 * Bio operations should be blocked on this filesystem because a source
128 	 * or target device is being destroyed as part of a device replace
129 	 */
130 	BTRFS_FS_STATE_DEV_REPLACING,
131 	/* The btrfs_fs_info created for self-tests */
132 	BTRFS_FS_STATE_DUMMY_FS_INFO,
133 };
134 
135 #define BTRFS_BACKREF_REV_MAX		256
136 #define BTRFS_BACKREF_REV_SHIFT		56
137 #define BTRFS_BACKREF_REV_MASK		(((u64)BTRFS_BACKREF_REV_MAX - 1) << \
138 					 BTRFS_BACKREF_REV_SHIFT)
139 
140 #define BTRFS_OLD_BACKREF_REV		0
141 #define BTRFS_MIXED_BACKREF_REV		1
142 
143 /*
144  * every tree block (leaf or node) starts with this header.
145  */
146 struct btrfs_header {
147 	/* these first four must match the super block */
148 	u8 csum[BTRFS_CSUM_SIZE];
149 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
150 	__le64 bytenr; /* which block this node is supposed to live in */
151 	__le64 flags;
152 
153 	/* allowed to be different from the super from here on down */
154 	u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
155 	__le64 generation;
156 	__le64 owner;
157 	__le32 nritems;
158 	u8 level;
159 } __attribute__ ((__packed__));
160 
161 /*
162  * this is a very generous portion of the super block, giving us
163  * room to translate 14 chunks with 3 stripes each.
164  */
165 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
166 
167 /*
168  * just in case we somehow lose the roots and are not able to mount,
169  * we store an array of the roots from previous transactions
170  * in the super.
171  */
172 #define BTRFS_NUM_BACKUP_ROOTS 4
173 struct btrfs_root_backup {
174 	__le64 tree_root;
175 	__le64 tree_root_gen;
176 
177 	__le64 chunk_root;
178 	__le64 chunk_root_gen;
179 
180 	__le64 extent_root;
181 	__le64 extent_root_gen;
182 
183 	__le64 fs_root;
184 	__le64 fs_root_gen;
185 
186 	__le64 dev_root;
187 	__le64 dev_root_gen;
188 
189 	__le64 csum_root;
190 	__le64 csum_root_gen;
191 
192 	__le64 total_bytes;
193 	__le64 bytes_used;
194 	__le64 num_devices;
195 	/* future */
196 	__le64 unused_64[4];
197 
198 	u8 tree_root_level;
199 	u8 chunk_root_level;
200 	u8 extent_root_level;
201 	u8 fs_root_level;
202 	u8 dev_root_level;
203 	u8 csum_root_level;
204 	/* future and to align */
205 	u8 unused_8[10];
206 } __attribute__ ((__packed__));
207 
208 /*
209  * the super block basically lists the main trees of the FS
210  * it currently lacks any block count etc etc
211  */
212 struct btrfs_super_block {
213 	/* the first 4 fields must match struct btrfs_header */
214 	u8 csum[BTRFS_CSUM_SIZE];
215 	/* FS specific UUID, visible to user */
216 	u8 fsid[BTRFS_FSID_SIZE];
217 	__le64 bytenr; /* this block number */
218 	__le64 flags;
219 
220 	/* allowed to be different from the btrfs_header from here own down */
221 	__le64 magic;
222 	__le64 generation;
223 	__le64 root;
224 	__le64 chunk_root;
225 	__le64 log_root;
226 
227 	/* this will help find the new super based on the log root */
228 	__le64 log_root_transid;
229 	__le64 total_bytes;
230 	__le64 bytes_used;
231 	__le64 root_dir_objectid;
232 	__le64 num_devices;
233 	__le32 sectorsize;
234 	__le32 nodesize;
235 	__le32 __unused_leafsize;
236 	__le32 stripesize;
237 	__le32 sys_chunk_array_size;
238 	__le64 chunk_root_generation;
239 	__le64 compat_flags;
240 	__le64 compat_ro_flags;
241 	__le64 incompat_flags;
242 	__le16 csum_type;
243 	u8 root_level;
244 	u8 chunk_root_level;
245 	u8 log_root_level;
246 	struct btrfs_dev_item dev_item;
247 
248 	char label[BTRFS_LABEL_SIZE];
249 
250 	__le64 cache_generation;
251 	__le64 uuid_tree_generation;
252 
253 	/* the UUID written into btree blocks */
254 	u8 metadata_uuid[BTRFS_FSID_SIZE];
255 
256 	/* future expansion */
257 	__le64 reserved[28];
258 	u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
259 	struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
260 } __attribute__ ((__packed__));
261 
262 /*
263  * Compat flags that we support.  If any incompat flags are set other than the
264  * ones specified below then we will fail to mount
265  */
266 #define BTRFS_FEATURE_COMPAT_SUPP		0ULL
267 #define BTRFS_FEATURE_COMPAT_SAFE_SET		0ULL
268 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR		0ULL
269 
270 #define BTRFS_FEATURE_COMPAT_RO_SUPP			\
271 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |	\
272 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
273 
274 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET	0ULL
275 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR	0ULL
276 
277 #define BTRFS_FEATURE_INCOMPAT_SUPP			\
278 	(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |		\
279 	 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |	\
280 	 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |		\
281 	 BTRFS_FEATURE_INCOMPAT_BIG_METADATA |		\
282 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |		\
283 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD |		\
284 	 BTRFS_FEATURE_INCOMPAT_RAID56 |		\
285 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |		\
286 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
287 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES	|	\
288 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
289 
290 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET			\
291 	(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
292 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR		0ULL
293 
294 /*
295  * A leaf is full of items. offset and size tell us where to find
296  * the item in the leaf (relative to the start of the data area)
297  */
298 struct btrfs_item {
299 	struct btrfs_disk_key key;
300 	__le32 offset;
301 	__le32 size;
302 } __attribute__ ((__packed__));
303 
304 /*
305  * leaves have an item area and a data area:
306  * [item0, item1....itemN] [free space] [dataN...data1, data0]
307  *
308  * The data is separate from the items to get the keys closer together
309  * during searches.
310  */
311 struct btrfs_leaf {
312 	struct btrfs_header header;
313 	struct btrfs_item items[];
314 } __attribute__ ((__packed__));
315 
316 /*
317  * all non-leaf blocks are nodes, they hold only keys and pointers to
318  * other blocks
319  */
320 struct btrfs_key_ptr {
321 	struct btrfs_disk_key key;
322 	__le64 blockptr;
323 	__le64 generation;
324 } __attribute__ ((__packed__));
325 
326 struct btrfs_node {
327 	struct btrfs_header header;
328 	struct btrfs_key_ptr ptrs[];
329 } __attribute__ ((__packed__));
330 
331 /*
332  * btrfs_paths remember the path taken from the root down to the leaf.
333  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
334  * to any other levels that are present.
335  *
336  * The slots array records the index of the item or block pointer
337  * used while walking the tree.
338  */
339 enum { READA_NONE, READA_BACK, READA_FORWARD };
340 struct btrfs_path {
341 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
342 	int slots[BTRFS_MAX_LEVEL];
343 	/* if there is real range locking, this locks field will change */
344 	u8 locks[BTRFS_MAX_LEVEL];
345 	u8 reada;
346 	/* keep some upper locks as we walk down */
347 	u8 lowest_level;
348 
349 	/*
350 	 * set by btrfs_split_item, tells search_slot to keep all locks
351 	 * and to force calls to keep space in the nodes
352 	 */
353 	unsigned int search_for_split:1;
354 	unsigned int keep_locks:1;
355 	unsigned int skip_locking:1;
356 	unsigned int leave_spinning:1;
357 	unsigned int search_commit_root:1;
358 	unsigned int need_commit_sem:1;
359 	unsigned int skip_release_on_error:1;
360 };
361 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
362 					sizeof(struct btrfs_item))
363 struct btrfs_dev_replace {
364 	u64 replace_state;	/* see #define above */
365 	time64_t time_started;	/* seconds since 1-Jan-1970 */
366 	time64_t time_stopped;	/* seconds since 1-Jan-1970 */
367 	atomic64_t num_write_errors;
368 	atomic64_t num_uncorrectable_read_errors;
369 
370 	u64 cursor_left;
371 	u64 committed_cursor_left;
372 	u64 cursor_left_last_write_of_item;
373 	u64 cursor_right;
374 
375 	u64 cont_reading_from_srcdev_mode;	/* see #define above */
376 
377 	int is_valid;
378 	int item_needs_writeback;
379 	struct btrfs_device *srcdev;
380 	struct btrfs_device *tgtdev;
381 
382 	struct mutex lock_finishing_cancel_unmount;
383 	struct rw_semaphore rwsem;
384 
385 	struct btrfs_scrub_progress scrub_progress;
386 
387 	struct percpu_counter bio_counter;
388 	wait_queue_head_t replace_wait;
389 };
390 
391 /* For raid type sysfs entries */
392 struct raid_kobject {
393 	u64 flags;
394 	struct kobject kobj;
395 	struct list_head list;
396 };
397 
398 struct btrfs_space_info {
399 	spinlock_t lock;
400 
401 	u64 total_bytes;	/* total bytes in the space,
402 				   this doesn't take mirrors into account */
403 	u64 bytes_used;		/* total bytes used,
404 				   this doesn't take mirrors into account */
405 	u64 bytes_pinned;	/* total bytes pinned, will be freed when the
406 				   transaction finishes */
407 	u64 bytes_reserved;	/* total bytes the allocator has reserved for
408 				   current allocations */
409 	u64 bytes_may_use;	/* number of bytes that may be used for
410 				   delalloc/allocations */
411 	u64 bytes_readonly;	/* total bytes that are read only */
412 
413 	u64 max_extent_size;	/* This will hold the maximum extent size of
414 				   the space info if we had an ENOSPC in the
415 				   allocator. */
416 
417 	unsigned int full:1;	/* indicates that we cannot allocate any more
418 				   chunks for this space */
419 	unsigned int chunk_alloc:1;	/* set if we are allocating a chunk */
420 
421 	unsigned int flush:1;		/* set if we are trying to make space */
422 
423 	unsigned int force_alloc;	/* set if we need to force a chunk
424 					   alloc for this space */
425 
426 	u64 disk_used;		/* total bytes used on disk */
427 	u64 disk_total;		/* total bytes on disk, takes mirrors into
428 				   account */
429 
430 	u64 flags;
431 
432 	/*
433 	 * bytes_pinned is kept in line with what is actually pinned, as in
434 	 * we've called update_block_group and dropped the bytes_used counter
435 	 * and increased the bytes_pinned counter.  However this means that
436 	 * bytes_pinned does not reflect the bytes that will be pinned once the
437 	 * delayed refs are flushed, so this counter is inc'ed every time we
438 	 * call btrfs_free_extent so it is a realtime count of what will be
439 	 * freed once the transaction is committed.  It will be zeroed every
440 	 * time the transaction commits.
441 	 */
442 	struct percpu_counter total_bytes_pinned;
443 
444 	struct list_head list;
445 	/* Protected by the spinlock 'lock'. */
446 	struct list_head ro_bgs;
447 	struct list_head priority_tickets;
448 	struct list_head tickets;
449 	/*
450 	 * tickets_id just indicates the next ticket will be handled, so note
451 	 * it's not stored per ticket.
452 	 */
453 	u64 tickets_id;
454 
455 	struct rw_semaphore groups_sem;
456 	/* for block groups in our same type */
457 	struct list_head block_groups[BTRFS_NR_RAID_TYPES];
458 	wait_queue_head_t wait;
459 
460 	struct kobject kobj;
461 	struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
462 };
463 
464 /*
465  * Types of block reserves
466  */
467 enum {
468 	BTRFS_BLOCK_RSV_GLOBAL,
469 	BTRFS_BLOCK_RSV_DELALLOC,
470 	BTRFS_BLOCK_RSV_TRANS,
471 	BTRFS_BLOCK_RSV_CHUNK,
472 	BTRFS_BLOCK_RSV_DELOPS,
473 	BTRFS_BLOCK_RSV_DELREFS,
474 	BTRFS_BLOCK_RSV_EMPTY,
475 	BTRFS_BLOCK_RSV_TEMP,
476 };
477 
478 struct btrfs_block_rsv {
479 	u64 size;
480 	u64 reserved;
481 	struct btrfs_space_info *space_info;
482 	spinlock_t lock;
483 	unsigned short full;
484 	unsigned short type;
485 	unsigned short failfast;
486 
487 	/*
488 	 * Qgroup equivalent for @size @reserved
489 	 *
490 	 * Unlike normal @size/@reserved for inode rsv, qgroup doesn't care
491 	 * about things like csum size nor how many tree blocks it will need to
492 	 * reserve.
493 	 *
494 	 * Qgroup cares more about net change of the extent usage.
495 	 *
496 	 * So for one newly inserted file extent, in worst case it will cause
497 	 * leaf split and level increase, nodesize for each file extent is
498 	 * already too much.
499 	 *
500 	 * In short, qgroup_size/reserved is the upper limit of possible needed
501 	 * qgroup metadata reservation.
502 	 */
503 	u64 qgroup_rsv_size;
504 	u64 qgroup_rsv_reserved;
505 };
506 
507 /*
508  * free clusters are used to claim free space in relatively large chunks,
509  * allowing us to do less seeky writes. They are used for all metadata
510  * allocations. In ssd_spread mode they are also used for data allocations.
511  */
512 struct btrfs_free_cluster {
513 	spinlock_t lock;
514 	spinlock_t refill_lock;
515 	struct rb_root root;
516 
517 	/* largest extent in this cluster */
518 	u64 max_size;
519 
520 	/* first extent starting offset */
521 	u64 window_start;
522 
523 	/* We did a full search and couldn't create a cluster */
524 	bool fragmented;
525 
526 	struct btrfs_block_group_cache *block_group;
527 	/*
528 	 * when a cluster is allocated from a block group, we put the
529 	 * cluster onto a list in the block group so that it can
530 	 * be freed before the block group is freed.
531 	 */
532 	struct list_head block_group_list;
533 };
534 
535 enum btrfs_caching_type {
536 	BTRFS_CACHE_NO,
537 	BTRFS_CACHE_STARTED,
538 	BTRFS_CACHE_FAST,
539 	BTRFS_CACHE_FINISHED,
540 	BTRFS_CACHE_ERROR,
541 };
542 
543 enum btrfs_disk_cache_state {
544 	BTRFS_DC_WRITTEN,
545 	BTRFS_DC_ERROR,
546 	BTRFS_DC_CLEAR,
547 	BTRFS_DC_SETUP,
548 };
549 
550 struct btrfs_caching_control {
551 	struct list_head list;
552 	struct mutex mutex;
553 	wait_queue_head_t wait;
554 	struct btrfs_work work;
555 	struct btrfs_block_group_cache *block_group;
556 	u64 progress;
557 	refcount_t count;
558 };
559 
560 /* Once caching_thread() finds this much free space, it will wake up waiters. */
561 #define CACHING_CTL_WAKE_UP SZ_2M
562 
563 struct btrfs_io_ctl {
564 	void *cur, *orig;
565 	struct page *page;
566 	struct page **pages;
567 	struct btrfs_fs_info *fs_info;
568 	struct inode *inode;
569 	unsigned long size;
570 	int index;
571 	int num_pages;
572 	int entries;
573 	int bitmaps;
574 	unsigned check_crcs:1;
575 };
576 
577 /*
578  * Tree to record all locked full stripes of a RAID5/6 block group
579  */
580 struct btrfs_full_stripe_locks_tree {
581 	struct rb_root root;
582 	struct mutex lock;
583 };
584 
585 struct btrfs_block_group_cache {
586 	struct btrfs_key key;
587 	struct btrfs_block_group_item item;
588 	struct btrfs_fs_info *fs_info;
589 	struct inode *inode;
590 	spinlock_t lock;
591 	u64 pinned;
592 	u64 reserved;
593 	u64 delalloc_bytes;
594 	u64 bytes_super;
595 	u64 flags;
596 	u64 cache_generation;
597 
598 	/*
599 	 * If the free space extent count exceeds this number, convert the block
600 	 * group to bitmaps.
601 	 */
602 	u32 bitmap_high_thresh;
603 
604 	/*
605 	 * If the free space extent count drops below this number, convert the
606 	 * block group back to extents.
607 	 */
608 	u32 bitmap_low_thresh;
609 
610 	/*
611 	 * It is just used for the delayed data space allocation because
612 	 * only the data space allocation and the relative metadata update
613 	 * can be done cross the transaction.
614 	 */
615 	struct rw_semaphore data_rwsem;
616 
617 	/* for raid56, this is a full stripe, without parity */
618 	unsigned long full_stripe_len;
619 
620 	unsigned int ro;
621 	unsigned int iref:1;
622 	unsigned int has_caching_ctl:1;
623 	unsigned int removed:1;
624 
625 	int disk_cache_state;
626 
627 	/* cache tracking stuff */
628 	int cached;
629 	struct btrfs_caching_control *caching_ctl;
630 	u64 last_byte_to_unpin;
631 
632 	struct btrfs_space_info *space_info;
633 
634 	/* free space cache stuff */
635 	struct btrfs_free_space_ctl *free_space_ctl;
636 
637 	/* block group cache stuff */
638 	struct rb_node cache_node;
639 
640 	/* for block groups in the same raid type */
641 	struct list_head list;
642 
643 	/* usage count */
644 	atomic_t count;
645 
646 	/* List of struct btrfs_free_clusters for this block group.
647 	 * Today it will only have one thing on it, but that may change
648 	 */
649 	struct list_head cluster_list;
650 
651 	/* For delayed block group creation or deletion of empty block groups */
652 	struct list_head bg_list;
653 
654 	/* For read-only block groups */
655 	struct list_head ro_list;
656 
657 	atomic_t trimming;
658 
659 	/* For dirty block groups */
660 	struct list_head dirty_list;
661 	struct list_head io_list;
662 
663 	struct btrfs_io_ctl io_ctl;
664 
665 	/*
666 	 * Incremented when doing extent allocations and holding a read lock
667 	 * on the space_info's groups_sem semaphore.
668 	 * Decremented when an ordered extent that represents an IO against this
669 	 * block group's range is created (after it's added to its inode's
670 	 * root's list of ordered extents) or immediately after the allocation
671 	 * if it's a metadata extent or fallocate extent (for these cases we
672 	 * don't create ordered extents).
673 	 */
674 	atomic_t reservations;
675 
676 	/*
677 	 * Incremented while holding the spinlock *lock* by a task checking if
678 	 * it can perform a nocow write (incremented if the value for the *ro*
679 	 * field is 0). Decremented by such tasks once they create an ordered
680 	 * extent or before that if some error happens before reaching that step.
681 	 * This is to prevent races between block group relocation and nocow
682 	 * writes through direct IO.
683 	 */
684 	atomic_t nocow_writers;
685 
686 	/* Lock for free space tree operations. */
687 	struct mutex free_space_lock;
688 
689 	/*
690 	 * Does the block group need to be added to the free space tree?
691 	 * Protected by free_space_lock.
692 	 */
693 	int needs_free_space;
694 
695 	/* Record locked full stripes for RAID5/6 block group */
696 	struct btrfs_full_stripe_locks_tree full_stripe_locks_root;
697 };
698 
699 /* delayed seq elem */
700 struct seq_list {
701 	struct list_head list;
702 	u64 seq;
703 };
704 
705 #define SEQ_LIST_INIT(name)	{ .list = LIST_HEAD_INIT((name).list), .seq = 0 }
706 
707 #define SEQ_LAST	((u64)-1)
708 
709 enum btrfs_orphan_cleanup_state {
710 	ORPHAN_CLEANUP_STARTED	= 1,
711 	ORPHAN_CLEANUP_DONE	= 2,
712 };
713 
714 /* used by the raid56 code to lock stripes for read/modify/write */
715 struct btrfs_stripe_hash {
716 	struct list_head hash_list;
717 	spinlock_t lock;
718 };
719 
720 /* used by the raid56 code to lock stripes for read/modify/write */
721 struct btrfs_stripe_hash_table {
722 	struct list_head stripe_cache;
723 	spinlock_t cache_lock;
724 	int cache_size;
725 	struct btrfs_stripe_hash table[];
726 };
727 
728 #define BTRFS_STRIPE_HASH_TABLE_BITS 11
729 
730 void btrfs_init_async_reclaim_work(struct work_struct *work);
731 
732 /* fs_info */
733 struct reloc_control;
734 struct btrfs_device;
735 struct btrfs_fs_devices;
736 struct btrfs_balance_control;
737 struct btrfs_delayed_root;
738 
739 /*
740  * Block group or device which contains an active swapfile. Used for preventing
741  * unsafe operations while a swapfile is active.
742  *
743  * These are sorted on (ptr, inode) (note that a block group or device can
744  * contain more than one swapfile). We compare the pointer values because we
745  * don't actually care what the object is, we just need a quick check whether
746  * the object exists in the rbtree.
747  */
748 struct btrfs_swapfile_pin {
749 	struct rb_node node;
750 	void *ptr;
751 	struct inode *inode;
752 	/*
753 	 * If true, ptr points to a struct btrfs_block_group_cache. Otherwise,
754 	 * ptr points to a struct btrfs_device.
755 	 */
756 	bool is_block_group;
757 };
758 
759 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
760 
761 enum {
762 	BTRFS_FS_BARRIER,
763 	BTRFS_FS_CLOSING_START,
764 	BTRFS_FS_CLOSING_DONE,
765 	BTRFS_FS_LOG_RECOVERING,
766 	BTRFS_FS_OPEN,
767 	BTRFS_FS_QUOTA_ENABLED,
768 	BTRFS_FS_UPDATE_UUID_TREE_GEN,
769 	BTRFS_FS_CREATING_FREE_SPACE_TREE,
770 	BTRFS_FS_BTREE_ERR,
771 	BTRFS_FS_LOG1_ERR,
772 	BTRFS_FS_LOG2_ERR,
773 	BTRFS_FS_QUOTA_OVERRIDE,
774 	/* Used to record internally whether fs has been frozen */
775 	BTRFS_FS_FROZEN,
776 	/*
777 	 * Indicate that a whole-filesystem exclusive operation is running
778 	 * (device replace, resize, device add/delete, balance)
779 	 */
780 	BTRFS_FS_EXCL_OP,
781 	/*
782 	 * To info transaction_kthread we need an immediate commit so it
783 	 * doesn't need to wait for commit_interval
784 	 */
785 	BTRFS_FS_NEED_ASYNC_COMMIT,
786 	/*
787 	 * Indicate that balance has been set up from the ioctl and is in the
788 	 * main phase. The fs_info::balance_ctl is initialized.
789 	 */
790 	BTRFS_FS_BALANCE_RUNNING,
791 
792 	/* Indicate that the cleaner thread is awake and doing something. */
793 	BTRFS_FS_CLEANER_RUNNING,
794 };
795 
796 struct btrfs_fs_info {
797 	u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
798 	unsigned long flags;
799 	struct btrfs_root *extent_root;
800 	struct btrfs_root *tree_root;
801 	struct btrfs_root *chunk_root;
802 	struct btrfs_root *dev_root;
803 	struct btrfs_root *fs_root;
804 	struct btrfs_root *csum_root;
805 	struct btrfs_root *quota_root;
806 	struct btrfs_root *uuid_root;
807 	struct btrfs_root *free_space_root;
808 
809 	/* the log root tree is a directory of all the other log roots */
810 	struct btrfs_root *log_root_tree;
811 
812 	spinlock_t fs_roots_radix_lock;
813 	struct radix_tree_root fs_roots_radix;
814 
815 	/* block group cache stuff */
816 	spinlock_t block_group_cache_lock;
817 	u64 first_logical_byte;
818 	struct rb_root block_group_cache_tree;
819 
820 	/* keep track of unallocated space */
821 	atomic64_t free_chunk_space;
822 
823 	struct extent_io_tree freed_extents[2];
824 	struct extent_io_tree *pinned_extents;
825 
826 	/* logical->physical extent mapping */
827 	struct btrfs_mapping_tree mapping_tree;
828 
829 	/*
830 	 * block reservation for extent, checksum, root tree and
831 	 * delayed dir index item
832 	 */
833 	struct btrfs_block_rsv global_block_rsv;
834 	/* block reservation for metadata operations */
835 	struct btrfs_block_rsv trans_block_rsv;
836 	/* block reservation for chunk tree */
837 	struct btrfs_block_rsv chunk_block_rsv;
838 	/* block reservation for delayed operations */
839 	struct btrfs_block_rsv delayed_block_rsv;
840 	/* block reservation for delayed refs */
841 	struct btrfs_block_rsv delayed_refs_rsv;
842 
843 	struct btrfs_block_rsv empty_block_rsv;
844 
845 	u64 generation;
846 	u64 last_trans_committed;
847 	u64 avg_delayed_ref_runtime;
848 
849 	/*
850 	 * this is updated to the current trans every time a full commit
851 	 * is required instead of the faster short fsync log commits
852 	 */
853 	u64 last_trans_log_full_commit;
854 	unsigned long mount_opt;
855 	/*
856 	 * Track requests for actions that need to be done during transaction
857 	 * commit (like for some mount options).
858 	 */
859 	unsigned long pending_changes;
860 	unsigned long compress_type:4;
861 	unsigned int compress_level;
862 	u32 commit_interval;
863 	/*
864 	 * It is a suggestive number, the read side is safe even it gets a
865 	 * wrong number because we will write out the data into a regular
866 	 * extent. The write side(mount/remount) is under ->s_umount lock,
867 	 * so it is also safe.
868 	 */
869 	u64 max_inline;
870 
871 	struct btrfs_transaction *running_transaction;
872 	wait_queue_head_t transaction_throttle;
873 	wait_queue_head_t transaction_wait;
874 	wait_queue_head_t transaction_blocked_wait;
875 	wait_queue_head_t async_submit_wait;
876 
877 	/*
878 	 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
879 	 * when they are updated.
880 	 *
881 	 * Because we do not clear the flags for ever, so we needn't use
882 	 * the lock on the read side.
883 	 *
884 	 * We also needn't use the lock when we mount the fs, because
885 	 * there is no other task which will update the flag.
886 	 */
887 	spinlock_t super_lock;
888 	struct btrfs_super_block *super_copy;
889 	struct btrfs_super_block *super_for_commit;
890 	struct super_block *sb;
891 	struct inode *btree_inode;
892 	struct mutex tree_log_mutex;
893 	struct mutex transaction_kthread_mutex;
894 	struct mutex cleaner_mutex;
895 	struct mutex chunk_mutex;
896 
897 	/*
898 	 * this is taken to make sure we don't set block groups ro after
899 	 * the free space cache has been allocated on them
900 	 */
901 	struct mutex ro_block_group_mutex;
902 
903 	/* this is used during read/modify/write to make sure
904 	 * no two ios are trying to mod the same stripe at the same
905 	 * time
906 	 */
907 	struct btrfs_stripe_hash_table *stripe_hash_table;
908 
909 	/*
910 	 * this protects the ordered operations list only while we are
911 	 * processing all of the entries on it.  This way we make
912 	 * sure the commit code doesn't find the list temporarily empty
913 	 * because another function happens to be doing non-waiting preflush
914 	 * before jumping into the main commit.
915 	 */
916 	struct mutex ordered_operations_mutex;
917 
918 	struct rw_semaphore commit_root_sem;
919 
920 	struct rw_semaphore cleanup_work_sem;
921 
922 	struct rw_semaphore subvol_sem;
923 	struct srcu_struct subvol_srcu;
924 
925 	spinlock_t trans_lock;
926 	/*
927 	 * the reloc mutex goes with the trans lock, it is taken
928 	 * during commit to protect us from the relocation code
929 	 */
930 	struct mutex reloc_mutex;
931 
932 	struct list_head trans_list;
933 	struct list_head dead_roots;
934 	struct list_head caching_block_groups;
935 
936 	spinlock_t delayed_iput_lock;
937 	struct list_head delayed_iputs;
938 	atomic_t nr_delayed_iputs;
939 	wait_queue_head_t delayed_iputs_wait;
940 
941 	/* this protects tree_mod_seq_list */
942 	spinlock_t tree_mod_seq_lock;
943 	atomic64_t tree_mod_seq;
944 	struct list_head tree_mod_seq_list;
945 
946 	/* this protects tree_mod_log */
947 	rwlock_t tree_mod_log_lock;
948 	struct rb_root tree_mod_log;
949 
950 	atomic_t async_delalloc_pages;
951 
952 	/*
953 	 * this is used to protect the following list -- ordered_roots.
954 	 */
955 	spinlock_t ordered_root_lock;
956 
957 	/*
958 	 * all fs/file tree roots in which there are data=ordered extents
959 	 * pending writeback are added into this list.
960 	 *
961 	 * these can span multiple transactions and basically include
962 	 * every dirty data page that isn't from nodatacow
963 	 */
964 	struct list_head ordered_roots;
965 
966 	struct mutex delalloc_root_mutex;
967 	spinlock_t delalloc_root_lock;
968 	/* all fs/file tree roots that have delalloc inodes. */
969 	struct list_head delalloc_roots;
970 
971 	/*
972 	 * there is a pool of worker threads for checksumming during writes
973 	 * and a pool for checksumming after reads.  This is because readers
974 	 * can run with FS locks held, and the writers may be waiting for
975 	 * those locks.  We don't want ordering in the pending list to cause
976 	 * deadlocks, and so the two are serviced separately.
977 	 *
978 	 * A third pool does submit_bio to avoid deadlocking with the other
979 	 * two
980 	 */
981 	struct btrfs_workqueue *workers;
982 	struct btrfs_workqueue *delalloc_workers;
983 	struct btrfs_workqueue *flush_workers;
984 	struct btrfs_workqueue *endio_workers;
985 	struct btrfs_workqueue *endio_meta_workers;
986 	struct btrfs_workqueue *endio_raid56_workers;
987 	struct btrfs_workqueue *endio_repair_workers;
988 	struct btrfs_workqueue *rmw_workers;
989 	struct btrfs_workqueue *endio_meta_write_workers;
990 	struct btrfs_workqueue *endio_write_workers;
991 	struct btrfs_workqueue *endio_freespace_worker;
992 	struct btrfs_workqueue *submit_workers;
993 	struct btrfs_workqueue *caching_workers;
994 	struct btrfs_workqueue *readahead_workers;
995 
996 	/*
997 	 * fixup workers take dirty pages that didn't properly go through
998 	 * the cow mechanism and make them safe to write.  It happens
999 	 * for the sys_munmap function call path
1000 	 */
1001 	struct btrfs_workqueue *fixup_workers;
1002 	struct btrfs_workqueue *delayed_workers;
1003 
1004 	/* the extent workers do delayed refs on the extent allocation tree */
1005 	struct btrfs_workqueue *extent_workers;
1006 	struct task_struct *transaction_kthread;
1007 	struct task_struct *cleaner_kthread;
1008 	u32 thread_pool_size;
1009 
1010 	struct kobject *space_info_kobj;
1011 	struct list_head pending_raid_kobjs;
1012 	spinlock_t pending_raid_kobjs_lock; /* uncontended */
1013 
1014 	u64 total_pinned;
1015 
1016 	/* used to keep from writing metadata until there is a nice batch */
1017 	struct percpu_counter dirty_metadata_bytes;
1018 	struct percpu_counter delalloc_bytes;
1019 	struct percpu_counter dio_bytes;
1020 	s32 dirty_metadata_batch;
1021 	s32 delalloc_batch;
1022 
1023 	struct list_head dirty_cowonly_roots;
1024 
1025 	struct btrfs_fs_devices *fs_devices;
1026 
1027 	/*
1028 	 * The space_info list is effectively read only after initial
1029 	 * setup.  It is populated at mount time and cleaned up after
1030 	 * all block groups are removed.  RCU is used to protect it.
1031 	 */
1032 	struct list_head space_info;
1033 
1034 	struct btrfs_space_info *data_sinfo;
1035 
1036 	struct reloc_control *reloc_ctl;
1037 
1038 	/* data_alloc_cluster is only used in ssd_spread mode */
1039 	struct btrfs_free_cluster data_alloc_cluster;
1040 
1041 	/* all metadata allocations go through this cluster */
1042 	struct btrfs_free_cluster meta_alloc_cluster;
1043 
1044 	/* auto defrag inodes go here */
1045 	spinlock_t defrag_inodes_lock;
1046 	struct rb_root defrag_inodes;
1047 	atomic_t defrag_running;
1048 
1049 	/* Used to protect avail_{data, metadata, system}_alloc_bits */
1050 	seqlock_t profiles_lock;
1051 	/*
1052 	 * these three are in extended format (availability of single
1053 	 * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
1054 	 * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
1055 	 */
1056 	u64 avail_data_alloc_bits;
1057 	u64 avail_metadata_alloc_bits;
1058 	u64 avail_system_alloc_bits;
1059 
1060 	/* restriper state */
1061 	spinlock_t balance_lock;
1062 	struct mutex balance_mutex;
1063 	atomic_t balance_pause_req;
1064 	atomic_t balance_cancel_req;
1065 	struct btrfs_balance_control *balance_ctl;
1066 	wait_queue_head_t balance_wait_q;
1067 
1068 	u32 data_chunk_allocations;
1069 	u32 metadata_ratio;
1070 
1071 	void *bdev_holder;
1072 
1073 	/* private scrub information */
1074 	struct mutex scrub_lock;
1075 	atomic_t scrubs_running;
1076 	atomic_t scrub_pause_req;
1077 	atomic_t scrubs_paused;
1078 	atomic_t scrub_cancel_req;
1079 	wait_queue_head_t scrub_pause_wait;
1080 	/*
1081 	 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
1082 	 * running.
1083 	 */
1084 	refcount_t scrub_workers_refcnt;
1085 	struct btrfs_workqueue *scrub_workers;
1086 	struct btrfs_workqueue *scrub_wr_completion_workers;
1087 	struct btrfs_workqueue *scrub_parity_workers;
1088 
1089 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1090 	u32 check_integrity_print_mask;
1091 #endif
1092 	/* is qgroup tracking in a consistent state? */
1093 	u64 qgroup_flags;
1094 
1095 	/* holds configuration and tracking. Protected by qgroup_lock */
1096 	struct rb_root qgroup_tree;
1097 	spinlock_t qgroup_lock;
1098 
1099 	/*
1100 	 * used to avoid frequently calling ulist_alloc()/ulist_free()
1101 	 * when doing qgroup accounting, it must be protected by qgroup_lock.
1102 	 */
1103 	struct ulist *qgroup_ulist;
1104 
1105 	/* protect user change for quota operations */
1106 	struct mutex qgroup_ioctl_lock;
1107 
1108 	/* list of dirty qgroups to be written at next commit */
1109 	struct list_head dirty_qgroups;
1110 
1111 	/* used by qgroup for an efficient tree traversal */
1112 	u64 qgroup_seq;
1113 
1114 	/* qgroup rescan items */
1115 	struct mutex qgroup_rescan_lock; /* protects the progress item */
1116 	struct btrfs_key qgroup_rescan_progress;
1117 	struct btrfs_workqueue *qgroup_rescan_workers;
1118 	struct completion qgroup_rescan_completion;
1119 	struct btrfs_work qgroup_rescan_work;
1120 	bool qgroup_rescan_running;	/* protected by qgroup_rescan_lock */
1121 
1122 	/* filesystem state */
1123 	unsigned long fs_state;
1124 
1125 	struct btrfs_delayed_root *delayed_root;
1126 
1127 	/* readahead tree */
1128 	spinlock_t reada_lock;
1129 	struct radix_tree_root reada_tree;
1130 
1131 	/* readahead works cnt */
1132 	atomic_t reada_works_cnt;
1133 
1134 	/* Extent buffer radix tree */
1135 	spinlock_t buffer_lock;
1136 	struct radix_tree_root buffer_radix;
1137 
1138 	/* next backup root to be overwritten */
1139 	int backup_root_index;
1140 
1141 	/* device replace state */
1142 	struct btrfs_dev_replace dev_replace;
1143 
1144 	struct semaphore uuid_tree_rescan_sem;
1145 
1146 	/* Used to reclaim the metadata space in the background. */
1147 	struct work_struct async_reclaim_work;
1148 
1149 	spinlock_t unused_bgs_lock;
1150 	struct list_head unused_bgs;
1151 	struct mutex unused_bg_unpin_mutex;
1152 	struct mutex delete_unused_bgs_mutex;
1153 
1154 	/* Cached block sizes */
1155 	u32 nodesize;
1156 	u32 sectorsize;
1157 	u32 stripesize;
1158 
1159 	/* Block groups and devices containing active swapfiles. */
1160 	spinlock_t swapfile_pins_lock;
1161 	struct rb_root swapfile_pins;
1162 
1163 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1164 	spinlock_t ref_verify_lock;
1165 	struct rb_root block_tree;
1166 #endif
1167 };
1168 
1169 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
1170 {
1171 	return sb->s_fs_info;
1172 }
1173 
1174 struct btrfs_subvolume_writers {
1175 	struct percpu_counter	counter;
1176 	wait_queue_head_t	wait;
1177 };
1178 
1179 /*
1180  * The state of btrfs root
1181  */
1182 enum {
1183 	/*
1184 	 * btrfs_record_root_in_trans is a multi-step process, and it can race
1185 	 * with the balancing code.   But the race is very small, and only the
1186 	 * first time the root is added to each transaction.  So IN_TRANS_SETUP
1187 	 * is used to tell us when more checks are required
1188 	 */
1189 	BTRFS_ROOT_IN_TRANS_SETUP,
1190 	BTRFS_ROOT_REF_COWS,
1191 	BTRFS_ROOT_TRACK_DIRTY,
1192 	BTRFS_ROOT_IN_RADIX,
1193 	BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
1194 	BTRFS_ROOT_DEFRAG_RUNNING,
1195 	BTRFS_ROOT_FORCE_COW,
1196 	BTRFS_ROOT_MULTI_LOG_TASKS,
1197 	BTRFS_ROOT_DIRTY,
1198 	BTRFS_ROOT_DELETING,
1199 
1200 	/*
1201 	 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
1202 	 *
1203 	 * Set for the subvolume tree owning the reloc tree.
1204 	 */
1205 	BTRFS_ROOT_DEAD_RELOC_TREE,
1206 	/* Mark dead root stored on device whose cleanup needs to be resumed */
1207 	BTRFS_ROOT_DEAD_TREE,
1208 };
1209 
1210 /*
1211  * Record swapped tree blocks of a subvolume tree for delayed subtree trace
1212  * code. For detail check comment in fs/btrfs/qgroup.c.
1213  */
1214 struct btrfs_qgroup_swapped_blocks {
1215 	spinlock_t lock;
1216 	/* RM_EMPTY_ROOT() of above blocks[] */
1217 	bool swapped;
1218 	struct rb_root blocks[BTRFS_MAX_LEVEL];
1219 };
1220 
1221 /*
1222  * in ram representation of the tree.  extent_root is used for all allocations
1223  * and for the extent tree extent_root root.
1224  */
1225 struct btrfs_root {
1226 	struct extent_buffer *node;
1227 
1228 	struct extent_buffer *commit_root;
1229 	struct btrfs_root *log_root;
1230 	struct btrfs_root *reloc_root;
1231 
1232 	unsigned long state;
1233 	struct btrfs_root_item root_item;
1234 	struct btrfs_key root_key;
1235 	struct btrfs_fs_info *fs_info;
1236 	struct extent_io_tree dirty_log_pages;
1237 
1238 	struct mutex objectid_mutex;
1239 
1240 	spinlock_t accounting_lock;
1241 	struct btrfs_block_rsv *block_rsv;
1242 
1243 	/* free ino cache stuff */
1244 	struct btrfs_free_space_ctl *free_ino_ctl;
1245 	enum btrfs_caching_type ino_cache_state;
1246 	spinlock_t ino_cache_lock;
1247 	wait_queue_head_t ino_cache_wait;
1248 	struct btrfs_free_space_ctl *free_ino_pinned;
1249 	u64 ino_cache_progress;
1250 	struct inode *ino_cache_inode;
1251 
1252 	struct mutex log_mutex;
1253 	wait_queue_head_t log_writer_wait;
1254 	wait_queue_head_t log_commit_wait[2];
1255 	struct list_head log_ctxs[2];
1256 	atomic_t log_writers;
1257 	atomic_t log_commit[2];
1258 	atomic_t log_batch;
1259 	int log_transid;
1260 	/* No matter the commit succeeds or not*/
1261 	int log_transid_committed;
1262 	/* Just be updated when the commit succeeds. */
1263 	int last_log_commit;
1264 	pid_t log_start_pid;
1265 
1266 	u64 last_trans;
1267 
1268 	u32 type;
1269 
1270 	u64 highest_objectid;
1271 
1272 	u64 defrag_trans_start;
1273 	struct btrfs_key defrag_progress;
1274 	struct btrfs_key defrag_max;
1275 
1276 	/* the dirty list is only used by non-reference counted roots */
1277 	struct list_head dirty_list;
1278 
1279 	struct list_head root_list;
1280 
1281 	spinlock_t log_extents_lock[2];
1282 	struct list_head logged_list[2];
1283 
1284 	int orphan_cleanup_state;
1285 
1286 	spinlock_t inode_lock;
1287 	/* red-black tree that keeps track of in-memory inodes */
1288 	struct rb_root inode_tree;
1289 
1290 	/*
1291 	 * radix tree that keeps track of delayed nodes of every inode,
1292 	 * protected by inode_lock
1293 	 */
1294 	struct radix_tree_root delayed_nodes_tree;
1295 	/*
1296 	 * right now this just gets used so that a root has its own devid
1297 	 * for stat.  It may be used for more later
1298 	 */
1299 	dev_t anon_dev;
1300 
1301 	spinlock_t root_item_lock;
1302 	refcount_t refs;
1303 
1304 	struct mutex delalloc_mutex;
1305 	spinlock_t delalloc_lock;
1306 	/*
1307 	 * all of the inodes that have delalloc bytes.  It is possible for
1308 	 * this list to be empty even when there is still dirty data=ordered
1309 	 * extents waiting to finish IO.
1310 	 */
1311 	struct list_head delalloc_inodes;
1312 	struct list_head delalloc_root;
1313 	u64 nr_delalloc_inodes;
1314 
1315 	struct mutex ordered_extent_mutex;
1316 	/*
1317 	 * this is used by the balancing code to wait for all the pending
1318 	 * ordered extents
1319 	 */
1320 	spinlock_t ordered_extent_lock;
1321 
1322 	/*
1323 	 * all of the data=ordered extents pending writeback
1324 	 * these can span multiple transactions and basically include
1325 	 * every dirty data page that isn't from nodatacow
1326 	 */
1327 	struct list_head ordered_extents;
1328 	struct list_head ordered_root;
1329 	u64 nr_ordered_extents;
1330 
1331 	/*
1332 	 * Not empty if this subvolume root has gone through tree block swap
1333 	 * (relocation)
1334 	 *
1335 	 * Will be used by reloc_control::dirty_subvol_roots.
1336 	 */
1337 	struct list_head reloc_dirty_list;
1338 
1339 	/*
1340 	 * Number of currently running SEND ioctls to prevent
1341 	 * manipulation with the read-only status via SUBVOL_SETFLAGS
1342 	 */
1343 	int send_in_progress;
1344 	/*
1345 	 * Number of currently running deduplication operations that have a
1346 	 * destination inode belonging to this root. Protected by the lock
1347 	 * root_item_lock.
1348 	 */
1349 	int dedupe_in_progress;
1350 	struct btrfs_subvolume_writers *subv_writers;
1351 	atomic_t will_be_snapshotted;
1352 	atomic_t snapshot_force_cow;
1353 
1354 	/* For qgroup metadata reserved space */
1355 	spinlock_t qgroup_meta_rsv_lock;
1356 	u64 qgroup_meta_rsv_pertrans;
1357 	u64 qgroup_meta_rsv_prealloc;
1358 
1359 	/* Number of active swapfiles */
1360 	atomic_t nr_swapfiles;
1361 
1362 	/* Record pairs of swapped blocks for qgroup */
1363 	struct btrfs_qgroup_swapped_blocks swapped_blocks;
1364 
1365 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1366 	u64 alloc_bytenr;
1367 #endif
1368 };
1369 
1370 struct btrfs_file_private {
1371 	void *filldir_buf;
1372 };
1373 
1374 static inline u32 btrfs_inode_sectorsize(const struct inode *inode)
1375 {
1376 	return btrfs_sb(inode->i_sb)->sectorsize;
1377 }
1378 
1379 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1380 {
1381 
1382 	return info->nodesize - sizeof(struct btrfs_header);
1383 }
1384 
1385 #define BTRFS_LEAF_DATA_OFFSET		offsetof(struct btrfs_leaf, items)
1386 
1387 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1388 {
1389 	return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1390 }
1391 
1392 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1393 {
1394 	return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1395 }
1396 
1397 #define BTRFS_FILE_EXTENT_INLINE_DATA_START		\
1398 		(offsetof(struct btrfs_file_extent_item, disk_bytenr))
1399 static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
1400 {
1401 	return BTRFS_MAX_ITEM_SIZE(info) -
1402 	       BTRFS_FILE_EXTENT_INLINE_DATA_START;
1403 }
1404 
1405 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1406 {
1407 	return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1408 }
1409 
1410 /*
1411  * Flags for mount options.
1412  *
1413  * Note: don't forget to add new options to btrfs_show_options()
1414  */
1415 #define BTRFS_MOUNT_NODATASUM		(1 << 0)
1416 #define BTRFS_MOUNT_NODATACOW		(1 << 1)
1417 #define BTRFS_MOUNT_NOBARRIER		(1 << 2)
1418 #define BTRFS_MOUNT_SSD			(1 << 3)
1419 #define BTRFS_MOUNT_DEGRADED		(1 << 4)
1420 #define BTRFS_MOUNT_COMPRESS		(1 << 5)
1421 #define BTRFS_MOUNT_NOTREELOG           (1 << 6)
1422 #define BTRFS_MOUNT_FLUSHONCOMMIT       (1 << 7)
1423 #define BTRFS_MOUNT_SSD_SPREAD		(1 << 8)
1424 #define BTRFS_MOUNT_NOSSD		(1 << 9)
1425 #define BTRFS_MOUNT_DISCARD		(1 << 10)
1426 #define BTRFS_MOUNT_FORCE_COMPRESS      (1 << 11)
1427 #define BTRFS_MOUNT_SPACE_CACHE		(1 << 12)
1428 #define BTRFS_MOUNT_CLEAR_CACHE		(1 << 13)
1429 #define BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED (1 << 14)
1430 #define BTRFS_MOUNT_ENOSPC_DEBUG	 (1 << 15)
1431 #define BTRFS_MOUNT_AUTO_DEFRAG		(1 << 16)
1432 #define BTRFS_MOUNT_INODE_MAP_CACHE	(1 << 17)
1433 #define BTRFS_MOUNT_USEBACKUPROOT	(1 << 18)
1434 #define BTRFS_MOUNT_SKIP_BALANCE	(1 << 19)
1435 #define BTRFS_MOUNT_CHECK_INTEGRITY	(1 << 20)
1436 #define BTRFS_MOUNT_CHECK_INTEGRITY_INCLUDING_EXTENT_DATA (1 << 21)
1437 #define BTRFS_MOUNT_PANIC_ON_FATAL_ERROR	(1 << 22)
1438 #define BTRFS_MOUNT_RESCAN_UUID_TREE	(1 << 23)
1439 #define BTRFS_MOUNT_FRAGMENT_DATA	(1 << 24)
1440 #define BTRFS_MOUNT_FRAGMENT_METADATA	(1 << 25)
1441 #define BTRFS_MOUNT_FREE_SPACE_TREE	(1 << 26)
1442 #define BTRFS_MOUNT_NOLOGREPLAY		(1 << 27)
1443 #define BTRFS_MOUNT_REF_VERIFY		(1 << 28)
1444 
1445 #define BTRFS_DEFAULT_COMMIT_INTERVAL	(30)
1446 #define BTRFS_DEFAULT_MAX_INLINE	(2048)
1447 
1448 #define btrfs_clear_opt(o, opt)		((o) &= ~BTRFS_MOUNT_##opt)
1449 #define btrfs_set_opt(o, opt)		((o) |= BTRFS_MOUNT_##opt)
1450 #define btrfs_raw_test_opt(o, opt)	((o) & BTRFS_MOUNT_##opt)
1451 #define btrfs_test_opt(fs_info, opt)	((fs_info)->mount_opt & \
1452 					 BTRFS_MOUNT_##opt)
1453 
1454 #define btrfs_set_and_info(fs_info, opt, fmt, args...)			\
1455 {									\
1456 	if (!btrfs_test_opt(fs_info, opt))				\
1457 		btrfs_info(fs_info, fmt, ##args);			\
1458 	btrfs_set_opt(fs_info->mount_opt, opt);				\
1459 }
1460 
1461 #define btrfs_clear_and_info(fs_info, opt, fmt, args...)		\
1462 {									\
1463 	if (btrfs_test_opt(fs_info, opt))				\
1464 		btrfs_info(fs_info, fmt, ##args);			\
1465 	btrfs_clear_opt(fs_info->mount_opt, opt);			\
1466 }
1467 
1468 #ifdef CONFIG_BTRFS_DEBUG
1469 static inline int
1470 btrfs_should_fragment_free_space(struct btrfs_block_group_cache *block_group)
1471 {
1472 	struct btrfs_fs_info *fs_info = block_group->fs_info;
1473 
1474 	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
1475 		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
1476 	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
1477 		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
1478 }
1479 #endif
1480 
1481 /*
1482  * Requests for changes that need to be done during transaction commit.
1483  *
1484  * Internal mount options that are used for special handling of the real
1485  * mount options (eg. cannot be set during remount and have to be set during
1486  * transaction commit)
1487  */
1488 
1489 #define BTRFS_PENDING_SET_INODE_MAP_CACHE	(0)
1490 #define BTRFS_PENDING_CLEAR_INODE_MAP_CACHE	(1)
1491 #define BTRFS_PENDING_COMMIT			(2)
1492 
1493 #define btrfs_test_pending(info, opt)	\
1494 	test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1495 #define btrfs_set_pending(info, opt)	\
1496 	set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1497 #define btrfs_clear_pending(info, opt)	\
1498 	clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1499 
1500 /*
1501  * Helpers for setting pending mount option changes.
1502  *
1503  * Expects corresponding macros
1504  * BTRFS_PENDING_SET_ and CLEAR_ + short mount option name
1505  */
1506 #define btrfs_set_pending_and_info(info, opt, fmt, args...)            \
1507 do {                                                                   \
1508        if (!btrfs_raw_test_opt((info)->mount_opt, opt)) {              \
1509                btrfs_info((info), fmt, ##args);                        \
1510                btrfs_set_pending((info), SET_##opt);                   \
1511                btrfs_clear_pending((info), CLEAR_##opt);               \
1512        }                                                               \
1513 } while(0)
1514 
1515 #define btrfs_clear_pending_and_info(info, opt, fmt, args...)          \
1516 do {                                                                   \
1517        if (btrfs_raw_test_opt((info)->mount_opt, opt)) {               \
1518                btrfs_info((info), fmt, ##args);                        \
1519                btrfs_set_pending((info), CLEAR_##opt);                 \
1520                btrfs_clear_pending((info), SET_##opt);                 \
1521        }                                                               \
1522 } while(0)
1523 
1524 /*
1525  * Inode flags
1526  */
1527 #define BTRFS_INODE_NODATASUM		(1 << 0)
1528 #define BTRFS_INODE_NODATACOW		(1 << 1)
1529 #define BTRFS_INODE_READONLY		(1 << 2)
1530 #define BTRFS_INODE_NOCOMPRESS		(1 << 3)
1531 #define BTRFS_INODE_PREALLOC		(1 << 4)
1532 #define BTRFS_INODE_SYNC		(1 << 5)
1533 #define BTRFS_INODE_IMMUTABLE		(1 << 6)
1534 #define BTRFS_INODE_APPEND		(1 << 7)
1535 #define BTRFS_INODE_NODUMP		(1 << 8)
1536 #define BTRFS_INODE_NOATIME		(1 << 9)
1537 #define BTRFS_INODE_DIRSYNC		(1 << 10)
1538 #define BTRFS_INODE_COMPRESS		(1 << 11)
1539 
1540 #define BTRFS_INODE_ROOT_ITEM_INIT	(1 << 31)
1541 
1542 #define BTRFS_INODE_FLAG_MASK						\
1543 	(BTRFS_INODE_NODATASUM |					\
1544 	 BTRFS_INODE_NODATACOW |					\
1545 	 BTRFS_INODE_READONLY |						\
1546 	 BTRFS_INODE_NOCOMPRESS |					\
1547 	 BTRFS_INODE_PREALLOC |						\
1548 	 BTRFS_INODE_SYNC |						\
1549 	 BTRFS_INODE_IMMUTABLE |					\
1550 	 BTRFS_INODE_APPEND |						\
1551 	 BTRFS_INODE_NODUMP |						\
1552 	 BTRFS_INODE_NOATIME |						\
1553 	 BTRFS_INODE_DIRSYNC |						\
1554 	 BTRFS_INODE_COMPRESS |						\
1555 	 BTRFS_INODE_ROOT_ITEM_INIT)
1556 
1557 struct btrfs_map_token {
1558 	const struct extent_buffer *eb;
1559 	char *kaddr;
1560 	unsigned long offset;
1561 };
1562 
1563 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
1564 				((bytes) >> (fs_info)->sb->s_blocksize_bits)
1565 
1566 static inline void btrfs_init_map_token (struct btrfs_map_token *token)
1567 {
1568 	token->kaddr = NULL;
1569 }
1570 
1571 /* some macros to generate set/get functions for the struct fields.  This
1572  * assumes there is a lefoo_to_cpu for every type, so lets make a simple
1573  * one for u8:
1574  */
1575 #define le8_to_cpu(v) (v)
1576 #define cpu_to_le8(v) (v)
1577 #define __le8 u8
1578 
1579 #define read_eb_member(eb, ptr, type, member, result) (\
1580 	read_extent_buffer(eb, (char *)(result),			\
1581 			   ((unsigned long)(ptr)) +			\
1582 			    offsetof(type, member),			\
1583 			   sizeof(((type *)0)->member)))
1584 
1585 #define write_eb_member(eb, ptr, type, member, result) (\
1586 	write_extent_buffer(eb, (char *)(result),			\
1587 			   ((unsigned long)(ptr)) +			\
1588 			    offsetof(type, member),			\
1589 			   sizeof(((type *)0)->member)))
1590 
1591 #define DECLARE_BTRFS_SETGET_BITS(bits)					\
1592 u##bits btrfs_get_token_##bits(const struct extent_buffer *eb,		\
1593 			       const void *ptr, unsigned long off,	\
1594 			       struct btrfs_map_token *token);		\
1595 void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr,	\
1596 			    unsigned long off, u##bits val,		\
1597 			    struct btrfs_map_token *token);		\
1598 static inline u##bits btrfs_get_##bits(const struct extent_buffer *eb,	\
1599 				       const void *ptr,			\
1600 				       unsigned long off)		\
1601 {									\
1602 	return btrfs_get_token_##bits(eb, ptr, off, NULL);		\
1603 }									\
1604 static inline void btrfs_set_##bits(struct extent_buffer *eb, void *ptr,\
1605 				    unsigned long off, u##bits val)	\
1606 {									\
1607        btrfs_set_token_##bits(eb, ptr, off, val, NULL);			\
1608 }
1609 
1610 DECLARE_BTRFS_SETGET_BITS(8)
1611 DECLARE_BTRFS_SETGET_BITS(16)
1612 DECLARE_BTRFS_SETGET_BITS(32)
1613 DECLARE_BTRFS_SETGET_BITS(64)
1614 
1615 #define BTRFS_SETGET_FUNCS(name, type, member, bits)			\
1616 static inline u##bits btrfs_##name(const struct extent_buffer *eb,	\
1617 				   const type *s)			\
1618 {									\
1619 	BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);	\
1620 	return btrfs_get_##bits(eb, s, offsetof(type, member));		\
1621 }									\
1622 static inline void btrfs_set_##name(struct extent_buffer *eb, type *s,	\
1623 				    u##bits val)			\
1624 {									\
1625 	BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);	\
1626 	btrfs_set_##bits(eb, s, offsetof(type, member), val);		\
1627 }									\
1628 static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\
1629 					 const type *s,			\
1630 					 struct btrfs_map_token *token)	\
1631 {									\
1632 	BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);	\
1633 	return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \
1634 }									\
1635 static inline void btrfs_set_token_##name(struct extent_buffer *eb,	\
1636 					  type *s, u##bits val,		\
1637                                          struct btrfs_map_token *token)	\
1638 {									\
1639 	BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);	\
1640 	btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \
1641 }
1642 
1643 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits)		\
1644 static inline u##bits btrfs_##name(const struct extent_buffer *eb)	\
1645 {									\
1646 	const type *p = page_address(eb->pages[0]);			\
1647 	u##bits res = le##bits##_to_cpu(p->member);			\
1648 	return res;							\
1649 }									\
1650 static inline void btrfs_set_##name(struct extent_buffer *eb,		\
1651 				    u##bits val)			\
1652 {									\
1653 	type *p = page_address(eb->pages[0]);				\
1654 	p->member = cpu_to_le##bits(val);				\
1655 }
1656 
1657 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits)		\
1658 static inline u##bits btrfs_##name(const type *s)			\
1659 {									\
1660 	return le##bits##_to_cpu(s->member);				\
1661 }									\
1662 static inline void btrfs_set_##name(type *s, u##bits val)		\
1663 {									\
1664 	s->member = cpu_to_le##bits(val);				\
1665 }
1666 
1667 
1668 static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,
1669 					   struct btrfs_dev_item *s)
1670 {
1671 	BUILD_BUG_ON(sizeof(u64) !=
1672 		     sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1673 	return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1674 					    total_bytes));
1675 }
1676 static inline void btrfs_set_device_total_bytes(struct extent_buffer *eb,
1677 						struct btrfs_dev_item *s,
1678 						u64 val)
1679 {
1680 	BUILD_BUG_ON(sizeof(u64) !=
1681 		     sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1682 	WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
1683 	btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
1684 }
1685 
1686 
1687 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
1688 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1689 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1690 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
1691 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1692 		   start_offset, 64);
1693 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1694 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
1695 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1696 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1697 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
1698 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
1699 
1700 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1701 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1702 			 total_bytes, 64);
1703 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1704 			 bytes_used, 64);
1705 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1706 			 io_align, 32);
1707 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1708 			 io_width, 32);
1709 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1710 			 sector_size, 32);
1711 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
1712 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1713 			 dev_group, 32);
1714 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1715 			 seek_speed, 8);
1716 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1717 			 bandwidth, 8);
1718 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1719 			 generation, 64);
1720 
1721 static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
1722 {
1723 	return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
1724 }
1725 
1726 static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
1727 {
1728 	return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
1729 }
1730 
1731 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
1732 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1733 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1734 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1735 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1736 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1737 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1738 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
1739 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
1740 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1741 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1742 
1743 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1744 {
1745 	return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1746 }
1747 
1748 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
1749 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1750 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1751 			 stripe_len, 64);
1752 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1753 			 io_align, 32);
1754 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1755 			 io_width, 32);
1756 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1757 			 sector_size, 32);
1758 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1759 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1760 			 num_stripes, 16);
1761 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1762 			 sub_stripes, 16);
1763 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1764 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1765 
1766 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1767 						   int nr)
1768 {
1769 	unsigned long offset = (unsigned long)c;
1770 	offset += offsetof(struct btrfs_chunk, stripe);
1771 	offset += nr * sizeof(struct btrfs_stripe);
1772 	return (struct btrfs_stripe *)offset;
1773 }
1774 
1775 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1776 {
1777 	return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1778 }
1779 
1780 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
1781 					 struct btrfs_chunk *c, int nr)
1782 {
1783 	return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1784 }
1785 
1786 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
1787 					 struct btrfs_chunk *c, int nr)
1788 {
1789 	return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1790 }
1791 
1792 /* struct btrfs_block_group_item */
1793 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
1794 			 used, 64);
1795 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
1796 			 used, 64);
1797 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
1798 			struct btrfs_block_group_item, chunk_objectid, 64);
1799 
1800 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
1801 		   struct btrfs_block_group_item, chunk_objectid, 64);
1802 BTRFS_SETGET_FUNCS(disk_block_group_flags,
1803 		   struct btrfs_block_group_item, flags, 64);
1804 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
1805 			struct btrfs_block_group_item, flags, 64);
1806 
1807 /* struct btrfs_free_space_info */
1808 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
1809 		   extent_count, 32);
1810 BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
1811 
1812 /* struct btrfs_inode_ref */
1813 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1814 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1815 
1816 /* struct btrfs_inode_extref */
1817 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1818 		   parent_objectid, 64);
1819 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1820 		   name_len, 16);
1821 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1822 
1823 /* struct btrfs_inode_item */
1824 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1825 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
1826 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1827 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1828 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
1829 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1830 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1831 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1832 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1833 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1834 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1835 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
1836 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1837 			 generation, 64);
1838 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1839 			 sequence, 64);
1840 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1841 			 transid, 64);
1842 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1843 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1844 			 nbytes, 64);
1845 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1846 			 block_group, 64);
1847 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1848 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1849 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1850 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1851 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1852 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1853 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1854 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1855 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1856 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1857 
1858 /* struct btrfs_dev_extent */
1859 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1860 		   chunk_tree, 64);
1861 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1862 		   chunk_objectid, 64);
1863 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1864 		   chunk_offset, 64);
1865 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1866 
1867 static inline unsigned long btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1868 {
1869 	unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1870 	return (unsigned long)dev + ptr;
1871 }
1872 
1873 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1874 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1875 		   generation, 64);
1876 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1877 
1878 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
1879 
1880 
1881 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1882 
1883 static inline void btrfs_tree_block_key(struct extent_buffer *eb,
1884 					struct btrfs_tree_block_info *item,
1885 					struct btrfs_disk_key *key)
1886 {
1887 	read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1888 }
1889 
1890 static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
1891 					    struct btrfs_tree_block_info *item,
1892 					    struct btrfs_disk_key *key)
1893 {
1894 	write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1895 }
1896 
1897 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1898 		   root, 64);
1899 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1900 		   objectid, 64);
1901 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1902 		   offset, 64);
1903 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1904 		   count, 32);
1905 
1906 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1907 		   count, 32);
1908 
1909 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1910 		   type, 8);
1911 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1912 		   offset, 64);
1913 
1914 static inline u32 btrfs_extent_inline_ref_size(int type)
1915 {
1916 	if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1917 	    type == BTRFS_SHARED_BLOCK_REF_KEY)
1918 		return sizeof(struct btrfs_extent_inline_ref);
1919 	if (type == BTRFS_SHARED_DATA_REF_KEY)
1920 		return sizeof(struct btrfs_shared_data_ref) +
1921 		       sizeof(struct btrfs_extent_inline_ref);
1922 	if (type == BTRFS_EXTENT_DATA_REF_KEY)
1923 		return sizeof(struct btrfs_extent_data_ref) +
1924 		       offsetof(struct btrfs_extent_inline_ref, offset);
1925 	return 0;
1926 }
1927 
1928 BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
1929 BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
1930 		   generation, 64);
1931 BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
1932 BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
1933 
1934 /* struct btrfs_node */
1935 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1936 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1937 BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
1938 			 blockptr, 64);
1939 BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1940 			 generation, 64);
1941 
1942 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1943 {
1944 	unsigned long ptr;
1945 	ptr = offsetof(struct btrfs_node, ptrs) +
1946 		sizeof(struct btrfs_key_ptr) * nr;
1947 	return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1948 }
1949 
1950 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1951 					   int nr, u64 val)
1952 {
1953 	unsigned long ptr;
1954 	ptr = offsetof(struct btrfs_node, ptrs) +
1955 		sizeof(struct btrfs_key_ptr) * nr;
1956 	btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1957 }
1958 
1959 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1960 {
1961 	unsigned long ptr;
1962 	ptr = offsetof(struct btrfs_node, ptrs) +
1963 		sizeof(struct btrfs_key_ptr) * nr;
1964 	return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1965 }
1966 
1967 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1968 						 int nr, u64 val)
1969 {
1970 	unsigned long ptr;
1971 	ptr = offsetof(struct btrfs_node, ptrs) +
1972 		sizeof(struct btrfs_key_ptr) * nr;
1973 	btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1974 }
1975 
1976 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1977 {
1978 	return offsetof(struct btrfs_node, ptrs) +
1979 		sizeof(struct btrfs_key_ptr) * nr;
1980 }
1981 
1982 void btrfs_node_key(const struct extent_buffer *eb,
1983 		    struct btrfs_disk_key *disk_key, int nr);
1984 
1985 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1986 				      struct btrfs_disk_key *disk_key, int nr)
1987 {
1988 	unsigned long ptr;
1989 	ptr = btrfs_node_key_ptr_offset(nr);
1990 	write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1991 		       struct btrfs_key_ptr, key, disk_key);
1992 }
1993 
1994 /* struct btrfs_item */
1995 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
1996 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
1997 BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
1998 BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
1999 
2000 static inline unsigned long btrfs_item_nr_offset(int nr)
2001 {
2002 	return offsetof(struct btrfs_leaf, items) +
2003 		sizeof(struct btrfs_item) * nr;
2004 }
2005 
2006 static inline struct btrfs_item *btrfs_item_nr(int nr)
2007 {
2008 	return (struct btrfs_item *)btrfs_item_nr_offset(nr);
2009 }
2010 
2011 static inline u32 btrfs_item_end(const struct extent_buffer *eb,
2012 				 struct btrfs_item *item)
2013 {
2014 	return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
2015 }
2016 
2017 static inline u32 btrfs_item_end_nr(const struct extent_buffer *eb, int nr)
2018 {
2019 	return btrfs_item_end(eb, btrfs_item_nr(nr));
2020 }
2021 
2022 static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
2023 {
2024 	return btrfs_item_offset(eb, btrfs_item_nr(nr));
2025 }
2026 
2027 static inline u32 btrfs_item_size_nr(const struct extent_buffer *eb, int nr)
2028 {
2029 	return btrfs_item_size(eb, btrfs_item_nr(nr));
2030 }
2031 
2032 static inline void btrfs_item_key(const struct extent_buffer *eb,
2033 			   struct btrfs_disk_key *disk_key, int nr)
2034 {
2035 	struct btrfs_item *item = btrfs_item_nr(nr);
2036 	read_eb_member(eb, item, struct btrfs_item, key, disk_key);
2037 }
2038 
2039 static inline void btrfs_set_item_key(struct extent_buffer *eb,
2040 			       struct btrfs_disk_key *disk_key, int nr)
2041 {
2042 	struct btrfs_item *item = btrfs_item_nr(nr);
2043 	write_eb_member(eb, item, struct btrfs_item, key, disk_key);
2044 }
2045 
2046 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
2047 
2048 /*
2049  * struct btrfs_root_ref
2050  */
2051 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
2052 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
2053 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
2054 
2055 /* struct btrfs_dir_item */
2056 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
2057 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
2058 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
2059 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
2060 BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
2061 BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
2062 			 data_len, 16);
2063 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
2064 			 name_len, 16);
2065 BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
2066 			 transid, 64);
2067 
2068 static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
2069 				      const struct btrfs_dir_item *item,
2070 				      struct btrfs_disk_key *key)
2071 {
2072 	read_eb_member(eb, item, struct btrfs_dir_item, location, key);
2073 }
2074 
2075 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
2076 					  struct btrfs_dir_item *item,
2077 					  const struct btrfs_disk_key *key)
2078 {
2079 	write_eb_member(eb, item, struct btrfs_dir_item, location, key);
2080 }
2081 
2082 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
2083 		   num_entries, 64);
2084 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
2085 		   num_bitmaps, 64);
2086 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
2087 		   generation, 64);
2088 
2089 static inline void btrfs_free_space_key(const struct extent_buffer *eb,
2090 					const struct btrfs_free_space_header *h,
2091 					struct btrfs_disk_key *key)
2092 {
2093 	read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2094 }
2095 
2096 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
2097 					    struct btrfs_free_space_header *h,
2098 					    const struct btrfs_disk_key *key)
2099 {
2100 	write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2101 }
2102 
2103 /* struct btrfs_disk_key */
2104 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
2105 			 objectid, 64);
2106 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
2107 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
2108 
2109 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
2110 					 const struct btrfs_disk_key *disk)
2111 {
2112 	cpu->offset = le64_to_cpu(disk->offset);
2113 	cpu->type = disk->type;
2114 	cpu->objectid = le64_to_cpu(disk->objectid);
2115 }
2116 
2117 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
2118 					 const struct btrfs_key *cpu)
2119 {
2120 	disk->offset = cpu_to_le64(cpu->offset);
2121 	disk->type = cpu->type;
2122 	disk->objectid = cpu_to_le64(cpu->objectid);
2123 }
2124 
2125 static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2126 					 struct btrfs_key *key, int nr)
2127 {
2128 	struct btrfs_disk_key disk_key;
2129 	btrfs_node_key(eb, &disk_key, nr);
2130 	btrfs_disk_key_to_cpu(key, &disk_key);
2131 }
2132 
2133 static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2134 					 struct btrfs_key *key, int nr)
2135 {
2136 	struct btrfs_disk_key disk_key;
2137 	btrfs_item_key(eb, &disk_key, nr);
2138 	btrfs_disk_key_to_cpu(key, &disk_key);
2139 }
2140 
2141 static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2142 					     const struct btrfs_dir_item *item,
2143 					     struct btrfs_key *key)
2144 {
2145 	struct btrfs_disk_key disk_key;
2146 	btrfs_dir_item_key(eb, item, &disk_key);
2147 	btrfs_disk_key_to_cpu(key, &disk_key);
2148 }
2149 
2150 static inline u8 btrfs_key_type(const struct btrfs_key *key)
2151 {
2152 	return key->type;
2153 }
2154 
2155 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
2156 {
2157 	key->type = val;
2158 }
2159 
2160 /* struct btrfs_header */
2161 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
2162 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
2163 			  generation, 64);
2164 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
2165 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
2166 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
2167 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
2168 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
2169 			 generation, 64);
2170 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
2171 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
2172 			 nritems, 32);
2173 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
2174 
2175 static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
2176 {
2177 	return (btrfs_header_flags(eb) & flag) == flag;
2178 }
2179 
2180 static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
2181 {
2182 	u64 flags = btrfs_header_flags(eb);
2183 	btrfs_set_header_flags(eb, flags | flag);
2184 }
2185 
2186 static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
2187 {
2188 	u64 flags = btrfs_header_flags(eb);
2189 	btrfs_set_header_flags(eb, flags & ~flag);
2190 }
2191 
2192 static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
2193 {
2194 	u64 flags = btrfs_header_flags(eb);
2195 	return flags >> BTRFS_BACKREF_REV_SHIFT;
2196 }
2197 
2198 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
2199 						int rev)
2200 {
2201 	u64 flags = btrfs_header_flags(eb);
2202 	flags &= ~BTRFS_BACKREF_REV_MASK;
2203 	flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
2204 	btrfs_set_header_flags(eb, flags);
2205 }
2206 
2207 static inline unsigned long btrfs_header_fsid(void)
2208 {
2209 	return offsetof(struct btrfs_header, fsid);
2210 }
2211 
2212 static inline unsigned long btrfs_header_chunk_tree_uuid(const struct extent_buffer *eb)
2213 {
2214 	return offsetof(struct btrfs_header, chunk_tree_uuid);
2215 }
2216 
2217 static inline int btrfs_is_leaf(const struct extent_buffer *eb)
2218 {
2219 	return btrfs_header_level(eb) == 0;
2220 }
2221 
2222 /* struct btrfs_root_item */
2223 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
2224 		   generation, 64);
2225 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
2226 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
2227 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
2228 
2229 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
2230 			 generation, 64);
2231 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
2232 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
2233 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
2234 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
2235 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
2236 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
2237 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
2238 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
2239 			 last_snapshot, 64);
2240 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
2241 			 generation_v2, 64);
2242 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
2243 			 ctransid, 64);
2244 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
2245 			 otransid, 64);
2246 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
2247 			 stransid, 64);
2248 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
2249 			 rtransid, 64);
2250 
2251 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
2252 {
2253 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
2254 }
2255 
2256 static inline bool btrfs_root_dead(const struct btrfs_root *root)
2257 {
2258 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
2259 }
2260 
2261 /* struct btrfs_root_backup */
2262 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
2263 		   tree_root, 64);
2264 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
2265 		   tree_root_gen, 64);
2266 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
2267 		   tree_root_level, 8);
2268 
2269 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
2270 		   chunk_root, 64);
2271 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
2272 		   chunk_root_gen, 64);
2273 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
2274 		   chunk_root_level, 8);
2275 
2276 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
2277 		   extent_root, 64);
2278 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
2279 		   extent_root_gen, 64);
2280 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
2281 		   extent_root_level, 8);
2282 
2283 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
2284 		   fs_root, 64);
2285 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
2286 		   fs_root_gen, 64);
2287 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
2288 		   fs_root_level, 8);
2289 
2290 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
2291 		   dev_root, 64);
2292 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
2293 		   dev_root_gen, 64);
2294 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
2295 		   dev_root_level, 8);
2296 
2297 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
2298 		   csum_root, 64);
2299 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
2300 		   csum_root_gen, 64);
2301 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
2302 		   csum_root_level, 8);
2303 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
2304 		   total_bytes, 64);
2305 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
2306 		   bytes_used, 64);
2307 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
2308 		   num_devices, 64);
2309 
2310 /* struct btrfs_balance_item */
2311 BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
2312 
2313 static inline void btrfs_balance_data(const struct extent_buffer *eb,
2314 				      const struct btrfs_balance_item *bi,
2315 				      struct btrfs_disk_balance_args *ba)
2316 {
2317 	read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2318 }
2319 
2320 static inline void btrfs_set_balance_data(struct extent_buffer *eb,
2321 				  struct btrfs_balance_item *bi,
2322 				  const struct btrfs_disk_balance_args *ba)
2323 {
2324 	write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2325 }
2326 
2327 static inline void btrfs_balance_meta(const struct extent_buffer *eb,
2328 				      const struct btrfs_balance_item *bi,
2329 				      struct btrfs_disk_balance_args *ba)
2330 {
2331 	read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2332 }
2333 
2334 static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
2335 				  struct btrfs_balance_item *bi,
2336 				  const struct btrfs_disk_balance_args *ba)
2337 {
2338 	write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2339 }
2340 
2341 static inline void btrfs_balance_sys(const struct extent_buffer *eb,
2342 				     const struct btrfs_balance_item *bi,
2343 				     struct btrfs_disk_balance_args *ba)
2344 {
2345 	read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2346 }
2347 
2348 static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
2349 				 struct btrfs_balance_item *bi,
2350 				 const struct btrfs_disk_balance_args *ba)
2351 {
2352 	write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2353 }
2354 
2355 static inline void
2356 btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
2357 			       const struct btrfs_disk_balance_args *disk)
2358 {
2359 	memset(cpu, 0, sizeof(*cpu));
2360 
2361 	cpu->profiles = le64_to_cpu(disk->profiles);
2362 	cpu->usage = le64_to_cpu(disk->usage);
2363 	cpu->devid = le64_to_cpu(disk->devid);
2364 	cpu->pstart = le64_to_cpu(disk->pstart);
2365 	cpu->pend = le64_to_cpu(disk->pend);
2366 	cpu->vstart = le64_to_cpu(disk->vstart);
2367 	cpu->vend = le64_to_cpu(disk->vend);
2368 	cpu->target = le64_to_cpu(disk->target);
2369 	cpu->flags = le64_to_cpu(disk->flags);
2370 	cpu->limit = le64_to_cpu(disk->limit);
2371 	cpu->stripes_min = le32_to_cpu(disk->stripes_min);
2372 	cpu->stripes_max = le32_to_cpu(disk->stripes_max);
2373 }
2374 
2375 static inline void
2376 btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
2377 			       const struct btrfs_balance_args *cpu)
2378 {
2379 	memset(disk, 0, sizeof(*disk));
2380 
2381 	disk->profiles = cpu_to_le64(cpu->profiles);
2382 	disk->usage = cpu_to_le64(cpu->usage);
2383 	disk->devid = cpu_to_le64(cpu->devid);
2384 	disk->pstart = cpu_to_le64(cpu->pstart);
2385 	disk->pend = cpu_to_le64(cpu->pend);
2386 	disk->vstart = cpu_to_le64(cpu->vstart);
2387 	disk->vend = cpu_to_le64(cpu->vend);
2388 	disk->target = cpu_to_le64(cpu->target);
2389 	disk->flags = cpu_to_le64(cpu->flags);
2390 	disk->limit = cpu_to_le64(cpu->limit);
2391 	disk->stripes_min = cpu_to_le32(cpu->stripes_min);
2392 	disk->stripes_max = cpu_to_le32(cpu->stripes_max);
2393 }
2394 
2395 /* struct btrfs_super_block */
2396 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
2397 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
2398 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
2399 			 generation, 64);
2400 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
2401 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
2402 			 struct btrfs_super_block, sys_chunk_array_size, 32);
2403 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
2404 			 struct btrfs_super_block, chunk_root_generation, 64);
2405 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
2406 			 root_level, 8);
2407 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
2408 			 chunk_root, 64);
2409 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
2410 			 chunk_root_level, 8);
2411 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
2412 			 log_root, 64);
2413 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
2414 			 log_root_transid, 64);
2415 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
2416 			 log_root_level, 8);
2417 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
2418 			 total_bytes, 64);
2419 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
2420 			 bytes_used, 64);
2421 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
2422 			 sectorsize, 32);
2423 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
2424 			 nodesize, 32);
2425 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
2426 			 stripesize, 32);
2427 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
2428 			 root_dir_objectid, 64);
2429 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
2430 			 num_devices, 64);
2431 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
2432 			 compat_flags, 64);
2433 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
2434 			 compat_ro_flags, 64);
2435 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
2436 			 incompat_flags, 64);
2437 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
2438 			 csum_type, 16);
2439 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
2440 			 cache_generation, 64);
2441 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
2442 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
2443 			 uuid_tree_generation, 64);
2444 
2445 static inline int btrfs_super_csum_size(const struct btrfs_super_block *s)
2446 {
2447 	u16 t = btrfs_super_csum_type(s);
2448 	/*
2449 	 * csum type is validated at mount time
2450 	 */
2451 	return btrfs_csum_sizes[t];
2452 }
2453 
2454 
2455 /*
2456  * The leaf data grows from end-to-front in the node.
2457  * this returns the address of the start of the last item,
2458  * which is the stop of the leaf data stack
2459  */
2460 static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
2461 {
2462 	u32 nr = btrfs_header_nritems(leaf);
2463 
2464 	if (nr == 0)
2465 		return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
2466 	return btrfs_item_offset_nr(leaf, nr - 1);
2467 }
2468 
2469 /* struct btrfs_file_extent_item */
2470 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
2471 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
2472 			 struct btrfs_file_extent_item, disk_bytenr, 64);
2473 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
2474 			 struct btrfs_file_extent_item, offset, 64);
2475 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
2476 			 struct btrfs_file_extent_item, generation, 64);
2477 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
2478 			 struct btrfs_file_extent_item, num_bytes, 64);
2479 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
2480 			 struct btrfs_file_extent_item, disk_num_bytes, 64);
2481 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
2482 			 struct btrfs_file_extent_item, compression, 8);
2483 
2484 static inline unsigned long
2485 btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
2486 {
2487 	return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
2488 }
2489 
2490 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2491 {
2492 	return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
2493 }
2494 
2495 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2496 		   disk_bytenr, 64);
2497 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2498 		   generation, 64);
2499 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2500 		   disk_num_bytes, 64);
2501 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2502 		  offset, 64);
2503 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2504 		   num_bytes, 64);
2505 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2506 		   ram_bytes, 64);
2507 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2508 		   compression, 8);
2509 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2510 		   encryption, 8);
2511 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2512 		   other_encoding, 16);
2513 
2514 /*
2515  * this returns the number of bytes used by the item on disk, minus the
2516  * size of any extent headers.  If a file is compressed on disk, this is
2517  * the compressed size
2518  */
2519 static inline u32 btrfs_file_extent_inline_item_len(
2520 						const struct extent_buffer *eb,
2521 						struct btrfs_item *e)
2522 {
2523 	return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
2524 }
2525 
2526 /* btrfs_dev_stats_item */
2527 static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
2528 					const struct btrfs_dev_stats_item *ptr,
2529 					int index)
2530 {
2531 	u64 val;
2532 
2533 	read_extent_buffer(eb, &val,
2534 			   offsetof(struct btrfs_dev_stats_item, values) +
2535 			    ((unsigned long)ptr) + (index * sizeof(u64)),
2536 			   sizeof(val));
2537 	return val;
2538 }
2539 
2540 static inline void btrfs_set_dev_stats_value(struct extent_buffer *eb,
2541 					     struct btrfs_dev_stats_item *ptr,
2542 					     int index, u64 val)
2543 {
2544 	write_extent_buffer(eb, &val,
2545 			    offsetof(struct btrfs_dev_stats_item, values) +
2546 			     ((unsigned long)ptr) + (index * sizeof(u64)),
2547 			    sizeof(val));
2548 }
2549 
2550 /* btrfs_qgroup_status_item */
2551 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2552 		   generation, 64);
2553 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2554 		   version, 64);
2555 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2556 		   flags, 64);
2557 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
2558 		   rescan, 64);
2559 
2560 /* btrfs_qgroup_info_item */
2561 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2562 		   generation, 64);
2563 BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
2564 BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
2565 		   rfer_cmpr, 64);
2566 BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
2567 BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
2568 		   excl_cmpr, 64);
2569 
2570 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2571 			 struct btrfs_qgroup_info_item, generation, 64);
2572 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
2573 			 rfer, 64);
2574 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
2575 			 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
2576 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
2577 			 excl, 64);
2578 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
2579 			 struct btrfs_qgroup_info_item, excl_cmpr, 64);
2580 
2581 /* btrfs_qgroup_limit_item */
2582 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2583 		   flags, 64);
2584 BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
2585 		   max_rfer, 64);
2586 BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
2587 		   max_excl, 64);
2588 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
2589 		   rsv_rfer, 64);
2590 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
2591 		   rsv_excl, 64);
2592 
2593 /* btrfs_dev_replace_item */
2594 BTRFS_SETGET_FUNCS(dev_replace_src_devid,
2595 		   struct btrfs_dev_replace_item, src_devid, 64);
2596 BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
2597 		   struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
2598 		   64);
2599 BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
2600 		   replace_state, 64);
2601 BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
2602 		   time_started, 64);
2603 BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
2604 		   time_stopped, 64);
2605 BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
2606 		   num_write_errors, 64);
2607 BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
2608 		   struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
2609 		   64);
2610 BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
2611 		   cursor_left, 64);
2612 BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
2613 		   cursor_right, 64);
2614 
2615 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
2616 			 struct btrfs_dev_replace_item, src_devid, 64);
2617 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
2618 			 struct btrfs_dev_replace_item,
2619 			 cont_reading_from_srcdev_mode, 64);
2620 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
2621 			 struct btrfs_dev_replace_item, replace_state, 64);
2622 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
2623 			 struct btrfs_dev_replace_item, time_started, 64);
2624 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
2625 			 struct btrfs_dev_replace_item, time_stopped, 64);
2626 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
2627 			 struct btrfs_dev_replace_item, num_write_errors, 64);
2628 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
2629 			 struct btrfs_dev_replace_item,
2630 			 num_uncorrectable_read_errors, 64);
2631 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
2632 			 struct btrfs_dev_replace_item, cursor_left, 64);
2633 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
2634 			 struct btrfs_dev_replace_item, cursor_right, 64);
2635 
2636 /* helper function to cast into the data area of the leaf. */
2637 #define btrfs_item_ptr(leaf, slot, type) \
2638 	((type *)(BTRFS_LEAF_DATA_OFFSET + \
2639 	btrfs_item_offset_nr(leaf, slot)))
2640 
2641 #define btrfs_item_ptr_offset(leaf, slot) \
2642 	((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
2643 	btrfs_item_offset_nr(leaf, slot)))
2644 
2645 static inline u64 btrfs_name_hash(const char *name, int len)
2646 {
2647        return crc32c((u32)~1, name, len);
2648 }
2649 
2650 /*
2651  * Figure the key offset of an extended inode ref
2652  */
2653 static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
2654                                    int len)
2655 {
2656        return (u64) crc32c(parent_objectid, name, len);
2657 }
2658 
2659 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
2660 {
2661 	return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2662 		(space_info->flags & BTRFS_BLOCK_GROUP_DATA));
2663 }
2664 
2665 static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2666 {
2667 	return mapping_gfp_constraint(mapping, ~__GFP_FS);
2668 }
2669 
2670 /* extent-tree.c */
2671 
2672 enum btrfs_inline_ref_type {
2673 	BTRFS_REF_TYPE_INVALID,
2674 	BTRFS_REF_TYPE_BLOCK,
2675 	BTRFS_REF_TYPE_DATA,
2676 	BTRFS_REF_TYPE_ANY,
2677 };
2678 
2679 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
2680 				     struct btrfs_extent_inline_ref *iref,
2681 				     enum btrfs_inline_ref_type is_data);
2682 
2683 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
2684 
2685 static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_fs_info *fs_info,
2686 						 unsigned num_items)
2687 {
2688 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
2689 }
2690 
2691 /*
2692  * Doing a truncate won't result in new nodes or leaves, just what we need for
2693  * COW.
2694  */
2695 static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info,
2696 						 unsigned num_items)
2697 {
2698 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
2699 }
2700 
2701 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
2702 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
2703 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
2704 					 const u64 start);
2705 void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg);
2706 bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2707 void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2708 void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg);
2709 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2710 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2711 			   unsigned long count);
2712 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
2713 				  struct btrfs_delayed_ref_root *delayed_refs,
2714 				  struct btrfs_delayed_ref_head *head);
2715 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
2716 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2717 			     struct btrfs_fs_info *fs_info, u64 bytenr,
2718 			     u64 offset, int metadata, u64 *refs, u64 *flags);
2719 int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2720 		     u64 bytenr, u64 num, int reserved);
2721 int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
2722 				    u64 bytenr, u64 num_bytes);
2723 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
2724 int btrfs_cross_ref_exist(struct btrfs_root *root,
2725 			  u64 objectid, u64 offset, u64 bytenr);
2726 struct btrfs_block_group_cache *btrfs_lookup_block_group(
2727 						 struct btrfs_fs_info *info,
2728 						 u64 bytenr);
2729 void btrfs_get_block_group(struct btrfs_block_group_cache *cache);
2730 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2731 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
2732 					     struct btrfs_root *root,
2733 					     u64 parent, u64 root_objectid,
2734 					     const struct btrfs_disk_key *key,
2735 					     int level, u64 hint,
2736 					     u64 empty_size);
2737 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2738 			   struct btrfs_root *root,
2739 			   struct extent_buffer *buf,
2740 			   u64 parent, int last_ref);
2741 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
2742 				     struct btrfs_root *root, u64 owner,
2743 				     u64 offset, u64 ram_bytes,
2744 				     struct btrfs_key *ins);
2745 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2746 				   u64 root_objectid, u64 owner, u64 offset,
2747 				   struct btrfs_key *ins);
2748 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
2749 			 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
2750 			 struct btrfs_key *ins, int is_data, int delalloc);
2751 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2752 		  struct extent_buffer *buf, int full_backref);
2753 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2754 		  struct extent_buffer *buf, int full_backref);
2755 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2756 				u64 bytenr, u64 num_bytes, u64 flags,
2757 				int level, int is_data);
2758 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
2759 
2760 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2761 			       u64 start, u64 len, int delalloc);
2762 int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
2763 				       u64 start, u64 len);
2764 void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info);
2765 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
2766 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2767 			 struct btrfs_ref *generic_ref);
2768 
2769 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
2770 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
2771 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
2772 int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
2773 int btrfs_free_block_groups(struct btrfs_fs_info *info);
2774 int btrfs_read_block_groups(struct btrfs_fs_info *info);
2775 int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr);
2776 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2777 			   u64 bytes_used, u64 type, u64 chunk_offset,
2778 			   u64 size);
2779 void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
2780 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
2781 				struct btrfs_fs_info *fs_info,
2782 				const u64 chunk_offset);
2783 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
2784 			     u64 group_start, struct extent_map *em);
2785 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
2786 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache);
2787 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache);
2788 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
2789 u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info);
2790 u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info);
2791 u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info);
2792 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2793 
2794 enum btrfs_reserve_flush_enum {
2795 	/* If we are in the transaction, we can't flush anything.*/
2796 	BTRFS_RESERVE_NO_FLUSH,
2797 	/*
2798 	 * Flushing delalloc may cause deadlock somewhere, in this
2799 	 * case, use FLUSH LIMIT
2800 	 */
2801 	BTRFS_RESERVE_FLUSH_LIMIT,
2802 	BTRFS_RESERVE_FLUSH_ALL,
2803 };
2804 
2805 enum btrfs_flush_state {
2806 	FLUSH_DELAYED_ITEMS_NR	=	1,
2807 	FLUSH_DELAYED_ITEMS	=	2,
2808 	FLUSH_DELAYED_REFS_NR	=	3,
2809 	FLUSH_DELAYED_REFS	=	4,
2810 	FLUSH_DELALLOC		=	5,
2811 	FLUSH_DELALLOC_WAIT	=	6,
2812 	ALLOC_CHUNK		=	7,
2813 	ALLOC_CHUNK_FORCE	=	8,
2814 	COMMIT_TRANS		=	9,
2815 };
2816 
2817 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes);
2818 int btrfs_check_data_free_space(struct inode *inode,
2819 			struct extent_changeset **reserved, u64 start, u64 len);
2820 void btrfs_free_reserved_data_space(struct inode *inode,
2821 			struct extent_changeset *reserved, u64 start, u64 len);
2822 void btrfs_delalloc_release_space(struct inode *inode,
2823 				  struct extent_changeset *reserved,
2824 				  u64 start, u64 len, bool qgroup_free);
2825 void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
2826 					    u64 len);
2827 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
2828 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2829 				     struct btrfs_block_rsv *rsv,
2830 				     int nitems, bool use_global_rsv);
2831 void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
2832 				      struct btrfs_block_rsv *rsv);
2833 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
2834 				    bool qgroup_free);
2835 
2836 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes);
2837 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
2838 				     bool qgroup_free);
2839 int btrfs_delalloc_reserve_space(struct inode *inode,
2840 			struct extent_changeset **reserved, u64 start, u64 len);
2841 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type);
2842 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
2843 					      unsigned short type);
2844 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
2845 				   struct btrfs_block_rsv *rsv,
2846 				   unsigned short type);
2847 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
2848 			  struct btrfs_block_rsv *rsv);
2849 int btrfs_block_rsv_add(struct btrfs_root *root,
2850 			struct btrfs_block_rsv *block_rsv, u64 num_bytes,
2851 			enum btrfs_reserve_flush_enum flush);
2852 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
2853 int btrfs_block_rsv_refill(struct btrfs_root *root,
2854 			   struct btrfs_block_rsv *block_rsv, u64 min_reserved,
2855 			   enum btrfs_reserve_flush_enum flush);
2856 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
2857 			    struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
2858 			    bool update_size);
2859 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
2860 			     struct btrfs_block_rsv *dest, u64 num_bytes,
2861 			     int min_factor);
2862 void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
2863 			     struct btrfs_block_rsv *block_rsv,
2864 			     u64 num_bytes);
2865 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
2866 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
2867 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
2868 				  enum btrfs_reserve_flush_enum flush);
2869 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
2870 				       struct btrfs_block_rsv *src,
2871 				       u64 num_bytes);
2872 int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache);
2873 void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache);
2874 void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
2875 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2876 int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
2877 				   u64 start, u64 end);
2878 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2879 			 u64 num_bytes, u64 *actual_bytes);
2880 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type);
2881 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
2882 
2883 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2884 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2885 					 struct btrfs_fs_info *fs_info);
2886 int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
2887 void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
2888 void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
2889 void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type);
2890 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
2891 		       u64 start, u64 end);
2892 void btrfs_mark_bg_unused(struct btrfs_block_group_cache *bg);
2893 
2894 /* ctree.c */
2895 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
2896 		     int level, int *slot);
2897 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
2898 int btrfs_previous_item(struct btrfs_root *root,
2899 			struct btrfs_path *path, u64 min_objectid,
2900 			int type);
2901 int btrfs_previous_extent_item(struct btrfs_root *root,
2902 			struct btrfs_path *path, u64 min_objectid);
2903 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2904 			     struct btrfs_path *path,
2905 			     const struct btrfs_key *new_key);
2906 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
2907 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
2908 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
2909 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
2910 			struct btrfs_key *key, int lowest_level,
2911 			u64 min_trans);
2912 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
2913 			 struct btrfs_path *path,
2914 			 u64 min_trans);
2915 enum btrfs_compare_tree_result {
2916 	BTRFS_COMPARE_TREE_NEW,
2917 	BTRFS_COMPARE_TREE_DELETED,
2918 	BTRFS_COMPARE_TREE_CHANGED,
2919 	BTRFS_COMPARE_TREE_SAME,
2920 };
2921 typedef int (*btrfs_changed_cb_t)(struct btrfs_path *left_path,
2922 				  struct btrfs_path *right_path,
2923 				  struct btrfs_key *key,
2924 				  enum btrfs_compare_tree_result result,
2925 				  void *ctx);
2926 int btrfs_compare_trees(struct btrfs_root *left_root,
2927 			struct btrfs_root *right_root,
2928 			btrfs_changed_cb_t cb, void *ctx);
2929 int btrfs_cow_block(struct btrfs_trans_handle *trans,
2930 		    struct btrfs_root *root, struct extent_buffer *buf,
2931 		    struct extent_buffer *parent, int parent_slot,
2932 		    struct extent_buffer **cow_ret);
2933 int btrfs_copy_root(struct btrfs_trans_handle *trans,
2934 		      struct btrfs_root *root,
2935 		      struct extent_buffer *buf,
2936 		      struct extent_buffer **cow_ret, u64 new_root_objectid);
2937 int btrfs_block_can_be_shared(struct btrfs_root *root,
2938 			      struct extent_buffer *buf);
2939 void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
2940 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
2941 int btrfs_split_item(struct btrfs_trans_handle *trans,
2942 		     struct btrfs_root *root,
2943 		     struct btrfs_path *path,
2944 		     const struct btrfs_key *new_key,
2945 		     unsigned long split_offset);
2946 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
2947 			 struct btrfs_root *root,
2948 			 struct btrfs_path *path,
2949 			 const struct btrfs_key *new_key);
2950 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2951 		u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
2952 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2953 		      const struct btrfs_key *key, struct btrfs_path *p,
2954 		      int ins_len, int cow);
2955 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2956 			  struct btrfs_path *p, u64 time_seq);
2957 int btrfs_search_slot_for_read(struct btrfs_root *root,
2958 			       const struct btrfs_key *key,
2959 			       struct btrfs_path *p, int find_higher,
2960 			       int return_any);
2961 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
2962 		       struct btrfs_root *root, struct extent_buffer *parent,
2963 		       int start_slot, u64 *last_ret,
2964 		       struct btrfs_key *progress);
2965 void btrfs_release_path(struct btrfs_path *p);
2966 struct btrfs_path *btrfs_alloc_path(void);
2967 void btrfs_free_path(struct btrfs_path *p);
2968 void btrfs_set_path_blocking(struct btrfs_path *p);
2969 void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
2970 
2971 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2972 		   struct btrfs_path *path, int slot, int nr);
2973 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
2974 				 struct btrfs_root *root,
2975 				 struct btrfs_path *path)
2976 {
2977 	return btrfs_del_items(trans, root, path, path->slots[0], 1);
2978 }
2979 
2980 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
2981 			    const struct btrfs_key *cpu_key, u32 *data_size,
2982 			    u32 total_data, u32 total_size, int nr);
2983 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2984 		      const struct btrfs_key *key, void *data, u32 data_size);
2985 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2986 			     struct btrfs_root *root,
2987 			     struct btrfs_path *path,
2988 			     const struct btrfs_key *cpu_key, u32 *data_size,
2989 			     int nr);
2990 
2991 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2992 					  struct btrfs_root *root,
2993 					  struct btrfs_path *path,
2994 					  const struct btrfs_key *key,
2995 					  u32 data_size)
2996 {
2997 	return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
2998 }
2999 
3000 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
3001 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
3002 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
3003 			u64 time_seq);
3004 static inline int btrfs_next_old_item(struct btrfs_root *root,
3005 				      struct btrfs_path *p, u64 time_seq)
3006 {
3007 	++p->slots[0];
3008 	if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
3009 		return btrfs_next_old_leaf(root, p, time_seq);
3010 	return 0;
3011 }
3012 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
3013 {
3014 	return btrfs_next_old_item(root, p, 0);
3015 }
3016 int btrfs_leaf_free_space(struct extent_buffer *leaf);
3017 int __must_check btrfs_drop_snapshot(struct btrfs_root *root,
3018 				     struct btrfs_block_rsv *block_rsv,
3019 				     int update_ref, int for_reloc);
3020 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3021 			struct btrfs_root *root,
3022 			struct extent_buffer *node,
3023 			struct extent_buffer *parent);
3024 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
3025 {
3026 	/*
3027 	 * Do it this way so we only ever do one test_bit in the normal case.
3028 	 */
3029 	if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
3030 		if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
3031 			return 2;
3032 		return 1;
3033 	}
3034 	return 0;
3035 }
3036 
3037 /*
3038  * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
3039  * anything except sleeping. This function is used to check the status of
3040  * the fs.
3041  */
3042 static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
3043 {
3044 	return fs_info->sb->s_flags & SB_RDONLY || btrfs_fs_closing(fs_info);
3045 }
3046 
3047 static inline void free_fs_info(struct btrfs_fs_info *fs_info)
3048 {
3049 	kfree(fs_info->balance_ctl);
3050 	kfree(fs_info->delayed_root);
3051 	kfree(fs_info->extent_root);
3052 	kfree(fs_info->tree_root);
3053 	kfree(fs_info->chunk_root);
3054 	kfree(fs_info->dev_root);
3055 	kfree(fs_info->csum_root);
3056 	kfree(fs_info->quota_root);
3057 	kfree(fs_info->uuid_root);
3058 	kfree(fs_info->free_space_root);
3059 	kfree(fs_info->super_copy);
3060 	kfree(fs_info->super_for_commit);
3061 	kvfree(fs_info);
3062 }
3063 
3064 /* tree mod log functions from ctree.c */
3065 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
3066 			   struct seq_list *elem);
3067 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
3068 			    struct seq_list *elem);
3069 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq);
3070 
3071 /* root-item.c */
3072 int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3073 		       u64 ref_id, u64 dirid, u64 sequence, const char *name,
3074 		       int name_len);
3075 int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3076 		       u64 ref_id, u64 dirid, u64 *sequence, const char *name,
3077 		       int name_len);
3078 int btrfs_del_root(struct btrfs_trans_handle *trans,
3079 		   const struct btrfs_key *key);
3080 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3081 		      const struct btrfs_key *key,
3082 		      struct btrfs_root_item *item);
3083 int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
3084 				   struct btrfs_root *root,
3085 				   struct btrfs_key *key,
3086 				   struct btrfs_root_item *item);
3087 int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
3088 		    struct btrfs_path *path, struct btrfs_root_item *root_item,
3089 		    struct btrfs_key *root_key);
3090 int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
3091 void btrfs_set_root_node(struct btrfs_root_item *item,
3092 			 struct extent_buffer *node);
3093 void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
3094 void btrfs_update_root_times(struct btrfs_trans_handle *trans,
3095 			     struct btrfs_root *root);
3096 
3097 /* uuid-tree.c */
3098 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3099 			u64 subid);
3100 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3101 			u64 subid);
3102 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
3103 			    int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
3104 					      u64));
3105 
3106 /* dir-item.c */
3107 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
3108 			  const char *name, int name_len);
3109 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
3110 			  int name_len, struct btrfs_inode *dir,
3111 			  struct btrfs_key *location, u8 type, u64 index);
3112 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
3113 					     struct btrfs_root *root,
3114 					     struct btrfs_path *path, u64 dir,
3115 					     const char *name, int name_len,
3116 					     int mod);
3117 struct btrfs_dir_item *
3118 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
3119 			    struct btrfs_root *root,
3120 			    struct btrfs_path *path, u64 dir,
3121 			    u64 objectid, const char *name, int name_len,
3122 			    int mod);
3123 struct btrfs_dir_item *
3124 btrfs_search_dir_index_item(struct btrfs_root *root,
3125 			    struct btrfs_path *path, u64 dirid,
3126 			    const char *name, int name_len);
3127 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
3128 			      struct btrfs_root *root,
3129 			      struct btrfs_path *path,
3130 			      struct btrfs_dir_item *di);
3131 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
3132 			    struct btrfs_root *root,
3133 			    struct btrfs_path *path, u64 objectid,
3134 			    const char *name, u16 name_len,
3135 			    const void *data, u16 data_len);
3136 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3137 					  struct btrfs_root *root,
3138 					  struct btrfs_path *path, u64 dir,
3139 					  const char *name, u16 name_len,
3140 					  int mod);
3141 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
3142 						 struct btrfs_path *path,
3143 						 const char *name,
3144 						 int name_len);
3145 
3146 /* orphan.c */
3147 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
3148 			     struct btrfs_root *root, u64 offset);
3149 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
3150 			  struct btrfs_root *root, u64 offset);
3151 int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
3152 
3153 /* inode-item.c */
3154 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
3155 			   struct btrfs_root *root,
3156 			   const char *name, int name_len,
3157 			   u64 inode_objectid, u64 ref_objectid, u64 index);
3158 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
3159 			   struct btrfs_root *root,
3160 			   const char *name, int name_len,
3161 			   u64 inode_objectid, u64 ref_objectid, u64 *index);
3162 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
3163 			     struct btrfs_root *root,
3164 			     struct btrfs_path *path, u64 objectid);
3165 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
3166 		       *root, struct btrfs_path *path,
3167 		       struct btrfs_key *location, int mod);
3168 
3169 struct btrfs_inode_extref *
3170 btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
3171 			  struct btrfs_root *root,
3172 			  struct btrfs_path *path,
3173 			  const char *name, int name_len,
3174 			  u64 inode_objectid, u64 ref_objectid, int ins_len,
3175 			  int cow);
3176 
3177 int btrfs_find_name_in_backref(struct extent_buffer *leaf, int slot,
3178 			       const char *name,
3179 			       int name_len, struct btrfs_inode_ref **ref_ret);
3180 int btrfs_find_name_in_ext_backref(struct extent_buffer *leaf, int slot,
3181 				   u64 ref_objectid, const char *name,
3182 				   int name_len,
3183 				   struct btrfs_inode_extref **extref_ret);
3184 
3185 /* file-item.c */
3186 struct btrfs_dio_private;
3187 int btrfs_del_csums(struct btrfs_trans_handle *trans,
3188 		    struct btrfs_fs_info *fs_info, u64 bytenr, u64 len);
3189 blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst);
3190 blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio,
3191 			      u64 logical_offset);
3192 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
3193 			     struct btrfs_root *root,
3194 			     u64 objectid, u64 pos,
3195 			     u64 disk_offset, u64 disk_num_bytes,
3196 			     u64 num_bytes, u64 offset, u64 ram_bytes,
3197 			     u8 compression, u8 encryption, u16 other_encoding);
3198 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3199 			     struct btrfs_root *root,
3200 			     struct btrfs_path *path, u64 objectid,
3201 			     u64 bytenr, int mod);
3202 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
3203 			   struct btrfs_root *root,
3204 			   struct btrfs_ordered_sum *sums);
3205 blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
3206 		       u64 file_start, int contig);
3207 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3208 			     struct list_head *list, int search_commit);
3209 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
3210 				     const struct btrfs_path *path,
3211 				     struct btrfs_file_extent_item *fi,
3212 				     const bool new_inline,
3213 				     struct extent_map *em);
3214 
3215 /* inode.c */
3216 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
3217 					   u64 start, u64 len);
3218 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
3219 			      u64 *orig_start, u64 *orig_block_len,
3220 			      u64 *ram_bytes);
3221 
3222 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
3223 				struct btrfs_inode *inode);
3224 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
3225 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
3226 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3227 		       struct btrfs_root *root,
3228 		       struct btrfs_inode *dir, struct btrfs_inode *inode,
3229 		       const char *name, int name_len);
3230 int btrfs_add_link(struct btrfs_trans_handle *trans,
3231 		   struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
3232 		   const char *name, int name_len, int add_backref, u64 index);
3233 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
3234 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
3235 			int front);
3236 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3237 			       struct btrfs_root *root,
3238 			       struct inode *inode, u64 new_size,
3239 			       u32 min_type);
3240 
3241 int btrfs_start_delalloc_snapshot(struct btrfs_root *root);
3242 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr);
3243 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
3244 			      unsigned int extra_bits,
3245 			      struct extent_state **cached_state, int dedupe);
3246 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
3247 			     struct btrfs_root *new_root,
3248 			     struct btrfs_root *parent_root,
3249 			     u64 new_dirid);
3250  void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
3251 			       unsigned *bits);
3252 void btrfs_clear_delalloc_extent(struct inode *inode,
3253 				 struct extent_state *state, unsigned *bits);
3254 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
3255 				 struct extent_state *other);
3256 void btrfs_split_delalloc_extent(struct inode *inode,
3257 				 struct extent_state *orig, u64 split);
3258 int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
3259 			     unsigned long bio_flags);
3260 void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end);
3261 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
3262 int btrfs_readpage(struct file *file, struct page *page);
3263 void btrfs_evict_inode(struct inode *inode);
3264 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc);
3265 struct inode *btrfs_alloc_inode(struct super_block *sb);
3266 void btrfs_destroy_inode(struct inode *inode);
3267 void btrfs_free_inode(struct inode *inode);
3268 int btrfs_drop_inode(struct inode *inode);
3269 int __init btrfs_init_cachep(void);
3270 void __cold btrfs_destroy_cachep(void);
3271 struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
3272 			      struct btrfs_root *root, int *new,
3273 			      struct btrfs_path *path);
3274 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3275 			 struct btrfs_root *root, int *was_new);
3276 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
3277 				    struct page *page, size_t pg_offset,
3278 				    u64 start, u64 end, int create);
3279 int btrfs_update_inode(struct btrfs_trans_handle *trans,
3280 			      struct btrfs_root *root,
3281 			      struct inode *inode);
3282 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3283 				struct btrfs_root *root, struct inode *inode);
3284 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3285 		struct btrfs_inode *inode);
3286 int btrfs_orphan_cleanup(struct btrfs_root *root);
3287 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size);
3288 void btrfs_add_delayed_iput(struct inode *inode);
3289 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
3290 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
3291 int btrfs_prealloc_file_range(struct inode *inode, int mode,
3292 			      u64 start, u64 num_bytes, u64 min_size,
3293 			      loff_t actual_len, u64 *alloc_hint);
3294 int btrfs_prealloc_file_range_trans(struct inode *inode,
3295 				    struct btrfs_trans_handle *trans, int mode,
3296 				    u64 start, u64 num_bytes, u64 min_size,
3297 				    loff_t actual_len, u64 *alloc_hint);
3298 int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
3299 		u64 start, u64 end, int *page_started, unsigned long *nr_written,
3300 		struct writeback_control *wbc);
3301 int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);
3302 void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3303 					  u64 end, int uptodate);
3304 extern const struct dentry_operations btrfs_dentry_operations;
3305 
3306 /* ioctl.c */
3307 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3308 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3309 int btrfs_ioctl_get_supported_features(void __user *arg);
3310 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
3311 int btrfs_is_empty_uuid(u8 *uuid);
3312 int btrfs_defrag_file(struct inode *inode, struct file *file,
3313 		      struct btrfs_ioctl_defrag_range_args *range,
3314 		      u64 newer_than, unsigned long max_pages);
3315 void btrfs_get_block_group_info(struct list_head *groups_list,
3316 				struct btrfs_ioctl_space_info *space);
3317 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3318 			       struct btrfs_ioctl_balance_args *bargs);
3319 
3320 /* file.c */
3321 int __init btrfs_auto_defrag_init(void);
3322 void __cold btrfs_auto_defrag_exit(void);
3323 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
3324 			   struct btrfs_inode *inode);
3325 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
3326 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
3327 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3328 void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
3329 			     int skip_pinned);
3330 extern const struct file_operations btrfs_file_operations;
3331 int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
3332 			 struct btrfs_root *root, struct inode *inode,
3333 			 struct btrfs_path *path, u64 start, u64 end,
3334 			 u64 *drop_end, int drop_cache,
3335 			 int replace_extent,
3336 			 u32 extent_item_size,
3337 			 int *key_inserted);
3338 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
3339 		       struct btrfs_root *root, struct inode *inode, u64 start,
3340 		       u64 end, int drop_cache);
3341 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
3342 			      struct btrfs_inode *inode, u64 start, u64 end);
3343 int btrfs_release_file(struct inode *inode, struct file *file);
3344 int btrfs_dirty_pages(struct inode *inode, struct page **pages,
3345 		      size_t num_pages, loff_t pos, size_t write_bytes,
3346 		      struct extent_state **cached);
3347 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
3348 loff_t btrfs_remap_file_range(struct file *file_in, loff_t pos_in,
3349 			      struct file *file_out, loff_t pos_out,
3350 			      loff_t len, unsigned int remap_flags);
3351 
3352 /* tree-defrag.c */
3353 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
3354 			struct btrfs_root *root);
3355 
3356 /* sysfs.c */
3357 int __init btrfs_init_sysfs(void);
3358 void __cold btrfs_exit_sysfs(void);
3359 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
3360 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
3361 
3362 /* super.c */
3363 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
3364 			unsigned long new_flags);
3365 int btrfs_sync_fs(struct super_block *sb, int wait);
3366 
3367 static inline __printf(2, 3) __cold
3368 void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
3369 {
3370 }
3371 
3372 #ifdef CONFIG_PRINTK
3373 __printf(2, 3)
3374 __cold
3375 void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
3376 #else
3377 #define btrfs_printk(fs_info, fmt, args...) \
3378 	btrfs_no_printk(fs_info, fmt, ##args)
3379 #endif
3380 
3381 #define btrfs_emerg(fs_info, fmt, args...) \
3382 	btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
3383 #define btrfs_alert(fs_info, fmt, args...) \
3384 	btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
3385 #define btrfs_crit(fs_info, fmt, args...) \
3386 	btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
3387 #define btrfs_err(fs_info, fmt, args...) \
3388 	btrfs_printk(fs_info, KERN_ERR fmt, ##args)
3389 #define btrfs_warn(fs_info, fmt, args...) \
3390 	btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
3391 #define btrfs_notice(fs_info, fmt, args...) \
3392 	btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
3393 #define btrfs_info(fs_info, fmt, args...) \
3394 	btrfs_printk(fs_info, KERN_INFO fmt, ##args)
3395 
3396 /*
3397  * Wrappers that use printk_in_rcu
3398  */
3399 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
3400 	btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3401 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
3402 	btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3403 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
3404 	btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3405 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
3406 	btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
3407 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
3408 	btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3409 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
3410 	btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3411 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
3412 	btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
3413 
3414 /*
3415  * Wrappers that use a ratelimited printk_in_rcu
3416  */
3417 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
3418 	btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3419 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
3420 	btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3421 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
3422 	btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3423 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
3424 	btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
3425 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
3426 	btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3427 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
3428 	btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3429 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
3430 	btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
3431 
3432 /*
3433  * Wrappers that use a ratelimited printk
3434  */
3435 #define btrfs_emerg_rl(fs_info, fmt, args...) \
3436 	btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
3437 #define btrfs_alert_rl(fs_info, fmt, args...) \
3438 	btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
3439 #define btrfs_crit_rl(fs_info, fmt, args...) \
3440 	btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
3441 #define btrfs_err_rl(fs_info, fmt, args...) \
3442 	btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
3443 #define btrfs_warn_rl(fs_info, fmt, args...) \
3444 	btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
3445 #define btrfs_notice_rl(fs_info, fmt, args...) \
3446 	btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
3447 #define btrfs_info_rl(fs_info, fmt, args...) \
3448 	btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
3449 
3450 #if defined(CONFIG_DYNAMIC_DEBUG)
3451 #define btrfs_debug(fs_info, fmt, args...)				\
3452 	_dynamic_func_call_no_desc(fmt, btrfs_printk,			\
3453 				   fs_info, KERN_DEBUG fmt, ##args)
3454 #define btrfs_debug_in_rcu(fs_info, fmt, args...)			\
3455 	_dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu,		\
3456 				   fs_info, KERN_DEBUG fmt, ##args)
3457 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...)			\
3458 	_dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu,		\
3459 				   fs_info, KERN_DEBUG fmt, ##args)
3460 #define btrfs_debug_rl(fs_info, fmt, args...)				\
3461 	_dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited,	\
3462 				   fs_info, KERN_DEBUG fmt, ##args)
3463 #elif defined(DEBUG)
3464 #define btrfs_debug(fs_info, fmt, args...) \
3465 	btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
3466 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3467 	btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3468 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3469 	btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3470 #define btrfs_debug_rl(fs_info, fmt, args...) \
3471 	btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
3472 #else
3473 #define btrfs_debug(fs_info, fmt, args...) \
3474 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3475 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3476 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3477 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3478 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3479 #define btrfs_debug_rl(fs_info, fmt, args...) \
3480 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3481 #endif
3482 
3483 #define btrfs_printk_in_rcu(fs_info, fmt, args...)	\
3484 do {							\
3485 	rcu_read_lock();				\
3486 	btrfs_printk(fs_info, fmt, ##args);		\
3487 	rcu_read_unlock();				\
3488 } while (0)
3489 
3490 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...)	\
3491 do {							\
3492 	rcu_read_lock();				\
3493 	btrfs_no_printk(fs_info, fmt, ##args);		\
3494 	rcu_read_unlock();				\
3495 } while (0)
3496 
3497 #define btrfs_printk_ratelimited(fs_info, fmt, args...)		\
3498 do {								\
3499 	static DEFINE_RATELIMIT_STATE(_rs,			\
3500 		DEFAULT_RATELIMIT_INTERVAL,			\
3501 		DEFAULT_RATELIMIT_BURST);       		\
3502 	if (__ratelimit(&_rs))					\
3503 		btrfs_printk(fs_info, fmt, ##args);		\
3504 } while (0)
3505 
3506 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...)		\
3507 do {								\
3508 	rcu_read_lock();					\
3509 	btrfs_printk_ratelimited(fs_info, fmt, ##args);		\
3510 	rcu_read_unlock();					\
3511 } while (0)
3512 
3513 __cold
3514 static inline void assfail(const char *expr, const char *file, int line)
3515 {
3516 	if (IS_ENABLED(CONFIG_BTRFS_ASSERT)) {
3517 		pr_err("assertion failed: %s, file: %s, line: %d\n",
3518 		       expr, file, line);
3519 		BUG();
3520 	}
3521 }
3522 
3523 #define ASSERT(expr)	\
3524 	(likely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
3525 
3526 /*
3527  * Use that for functions that are conditionally exported for sanity tests but
3528  * otherwise static
3529  */
3530 #ifndef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3531 #define EXPORT_FOR_TESTS static
3532 #else
3533 #define EXPORT_FOR_TESTS
3534 #endif
3535 
3536 __cold
3537 static inline void btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
3538 {
3539 	btrfs_err(fs_info,
3540 "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
3541 }
3542 
3543 __printf(5, 6)
3544 __cold
3545 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
3546 		     unsigned int line, int errno, const char *fmt, ...);
3547 
3548 const char *btrfs_decode_error(int errno);
3549 
3550 __cold
3551 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
3552 			       const char *function,
3553 			       unsigned int line, int errno);
3554 
3555 /*
3556  * Call btrfs_abort_transaction as early as possible when an error condition is
3557  * detected, that way the exact line number is reported.
3558  */
3559 #define btrfs_abort_transaction(trans, errno)		\
3560 do {								\
3561 	/* Report first abort since mount */			\
3562 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
3563 			&((trans)->fs_info->fs_state))) {	\
3564 		if ((errno) != -EIO) {				\
3565 			WARN(1, KERN_DEBUG				\
3566 			"BTRFS: Transaction aborted (error %d)\n",	\
3567 			(errno));					\
3568 		} else {						\
3569 			btrfs_debug((trans)->fs_info,			\
3570 				    "Transaction aborted (error %d)", \
3571 				  (errno));			\
3572 		}						\
3573 	}							\
3574 	__btrfs_abort_transaction((trans), __func__,		\
3575 				  __LINE__, (errno));		\
3576 } while (0)
3577 
3578 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...)		\
3579 do {								\
3580 	__btrfs_handle_fs_error((fs_info), __func__, __LINE__,	\
3581 			  (errno), fmt, ##args);		\
3582 } while (0)
3583 
3584 __printf(5, 6)
3585 __cold
3586 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3587 		   unsigned int line, int errno, const char *fmt, ...);
3588 /*
3589  * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
3590  * will panic().  Otherwise we BUG() here.
3591  */
3592 #define btrfs_panic(fs_info, errno, fmt, args...)			\
3593 do {									\
3594 	__btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args);	\
3595 	BUG();								\
3596 } while (0)
3597 
3598 
3599 /* compatibility and incompatibility defines */
3600 
3601 #define btrfs_set_fs_incompat(__fs_info, opt) \
3602 	__btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3603 
3604 static inline void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info,
3605 					   u64 flag)
3606 {
3607 	struct btrfs_super_block *disk_super;
3608 	u64 features;
3609 
3610 	disk_super = fs_info->super_copy;
3611 	features = btrfs_super_incompat_flags(disk_super);
3612 	if (!(features & flag)) {
3613 		spin_lock(&fs_info->super_lock);
3614 		features = btrfs_super_incompat_flags(disk_super);
3615 		if (!(features & flag)) {
3616 			features |= flag;
3617 			btrfs_set_super_incompat_flags(disk_super, features);
3618 			btrfs_info(fs_info, "setting %llu feature flag",
3619 					 flag);
3620 		}
3621 		spin_unlock(&fs_info->super_lock);
3622 	}
3623 }
3624 
3625 #define btrfs_clear_fs_incompat(__fs_info, opt) \
3626 	__btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3627 
3628 static inline void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info,
3629 					     u64 flag)
3630 {
3631 	struct btrfs_super_block *disk_super;
3632 	u64 features;
3633 
3634 	disk_super = fs_info->super_copy;
3635 	features = btrfs_super_incompat_flags(disk_super);
3636 	if (features & flag) {
3637 		spin_lock(&fs_info->super_lock);
3638 		features = btrfs_super_incompat_flags(disk_super);
3639 		if (features & flag) {
3640 			features &= ~flag;
3641 			btrfs_set_super_incompat_flags(disk_super, features);
3642 			btrfs_info(fs_info, "clearing %llu feature flag",
3643 					 flag);
3644 		}
3645 		spin_unlock(&fs_info->super_lock);
3646 	}
3647 }
3648 
3649 #define btrfs_fs_incompat(fs_info, opt) \
3650 	__btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3651 
3652 static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
3653 {
3654 	struct btrfs_super_block *disk_super;
3655 	disk_super = fs_info->super_copy;
3656 	return !!(btrfs_super_incompat_flags(disk_super) & flag);
3657 }
3658 
3659 #define btrfs_set_fs_compat_ro(__fs_info, opt) \
3660 	__btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3661 
3662 static inline void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info,
3663 					    u64 flag)
3664 {
3665 	struct btrfs_super_block *disk_super;
3666 	u64 features;
3667 
3668 	disk_super = fs_info->super_copy;
3669 	features = btrfs_super_compat_ro_flags(disk_super);
3670 	if (!(features & flag)) {
3671 		spin_lock(&fs_info->super_lock);
3672 		features = btrfs_super_compat_ro_flags(disk_super);
3673 		if (!(features & flag)) {
3674 			features |= flag;
3675 			btrfs_set_super_compat_ro_flags(disk_super, features);
3676 			btrfs_info(fs_info, "setting %llu ro feature flag",
3677 				   flag);
3678 		}
3679 		spin_unlock(&fs_info->super_lock);
3680 	}
3681 }
3682 
3683 #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
3684 	__btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3685 
3686 static inline void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info,
3687 					      u64 flag)
3688 {
3689 	struct btrfs_super_block *disk_super;
3690 	u64 features;
3691 
3692 	disk_super = fs_info->super_copy;
3693 	features = btrfs_super_compat_ro_flags(disk_super);
3694 	if (features & flag) {
3695 		spin_lock(&fs_info->super_lock);
3696 		features = btrfs_super_compat_ro_flags(disk_super);
3697 		if (features & flag) {
3698 			features &= ~flag;
3699 			btrfs_set_super_compat_ro_flags(disk_super, features);
3700 			btrfs_info(fs_info, "clearing %llu ro feature flag",
3701 				   flag);
3702 		}
3703 		spin_unlock(&fs_info->super_lock);
3704 	}
3705 }
3706 
3707 #define btrfs_fs_compat_ro(fs_info, opt) \
3708 	__btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3709 
3710 static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
3711 {
3712 	struct btrfs_super_block *disk_super;
3713 	disk_super = fs_info->super_copy;
3714 	return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
3715 }
3716 
3717 /* acl.c */
3718 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
3719 struct posix_acl *btrfs_get_acl(struct inode *inode, int type);
3720 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
3721 int btrfs_init_acl(struct btrfs_trans_handle *trans,
3722 		   struct inode *inode, struct inode *dir);
3723 #else
3724 #define btrfs_get_acl NULL
3725 #define btrfs_set_acl NULL
3726 static inline int btrfs_init_acl(struct btrfs_trans_handle *trans,
3727 				 struct inode *inode, struct inode *dir)
3728 {
3729 	return 0;
3730 }
3731 #endif
3732 
3733 /* relocation.c */
3734 int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
3735 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
3736 			  struct btrfs_root *root);
3737 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
3738 			    struct btrfs_root *root);
3739 int btrfs_recover_relocation(struct btrfs_root *root);
3740 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len);
3741 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
3742 			  struct btrfs_root *root, struct extent_buffer *buf,
3743 			  struct extent_buffer *cow);
3744 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
3745 			      u64 *bytes_to_reserve);
3746 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3747 			      struct btrfs_pending_snapshot *pending);
3748 
3749 /* scrub.c */
3750 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3751 		    u64 end, struct btrfs_scrub_progress *progress,
3752 		    int readonly, int is_dev_replace);
3753 void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
3754 void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
3755 int btrfs_scrub_cancel(struct btrfs_fs_info *info);
3756 int btrfs_scrub_cancel_dev(struct btrfs_device *dev);
3757 int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3758 			 struct btrfs_scrub_progress *progress);
3759 static inline void btrfs_init_full_stripe_locks_tree(
3760 			struct btrfs_full_stripe_locks_tree *locks_root)
3761 {
3762 	locks_root->root = RB_ROOT;
3763 	mutex_init(&locks_root->lock);
3764 }
3765 
3766 /* dev-replace.c */
3767 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info);
3768 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info);
3769 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount);
3770 
3771 static inline void btrfs_bio_counter_dec(struct btrfs_fs_info *fs_info)
3772 {
3773 	btrfs_bio_counter_sub(fs_info, 1);
3774 }
3775 
3776 /* reada.c */
3777 struct reada_control {
3778 	struct btrfs_fs_info	*fs_info;		/* tree to prefetch */
3779 	struct btrfs_key	key_start;
3780 	struct btrfs_key	key_end;	/* exclusive */
3781 	atomic_t		elems;
3782 	struct kref		refcnt;
3783 	wait_queue_head_t	wait;
3784 };
3785 struct reada_control *btrfs_reada_add(struct btrfs_root *root,
3786 			      struct btrfs_key *start, struct btrfs_key *end);
3787 int btrfs_reada_wait(void *handle);
3788 void btrfs_reada_detach(void *handle);
3789 int btree_readahead_hook(struct extent_buffer *eb, int err);
3790 
3791 static inline int is_fstree(u64 rootid)
3792 {
3793 	if (rootid == BTRFS_FS_TREE_OBJECTID ||
3794 	    ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
3795 	      !btrfs_qgroup_level(rootid)))
3796 		return 1;
3797 	return 0;
3798 }
3799 
3800 static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
3801 {
3802 	return signal_pending(current);
3803 }
3804 
3805 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
3806 
3807 /* Sanity test specific functions */
3808 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3809 void btrfs_test_inode_set_ops(struct inode *inode);
3810 void btrfs_test_destroy_inode(struct inode *inode);
3811 
3812 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3813 {
3814 	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
3815 }
3816 #else
3817 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3818 {
3819 	return 0;
3820 }
3821 #endif
3822 
3823 static inline void cond_wake_up(struct wait_queue_head *wq)
3824 {
3825 	/*
3826 	 * This implies a full smp_mb barrier, see comments for
3827 	 * waitqueue_active why.
3828 	 */
3829 	if (wq_has_sleeper(wq))
3830 		wake_up(wq);
3831 }
3832 
3833 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
3834 {
3835 	/*
3836 	 * Special case for conditional wakeup where the barrier required for
3837 	 * waitqueue_active is implied by some of the preceding code. Eg. one
3838 	 * of such atomic operations (atomic_dec_and_return, ...), or a
3839 	 * unlock/lock sequence, etc.
3840 	 */
3841 	if (waitqueue_active(wq))
3842 		wake_up(wq);
3843 }
3844 
3845 #endif
3846