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