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