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