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