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