xref: /openbmc/linux/fs/btrfs/ctree.h (revision 9144f784f852f9a125cabe9927b986d909bfa439)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
69888c340SDavid Sterba #ifndef BTRFS_CTREE_H
79888c340SDavid Sterba #define BTRFS_CTREE_H
8eb60ceacSChris Mason 
9810191ffSChris Mason #include <linux/mm.h>
10174cd4b1SIngo Molnar #include <linux/sched/signal.h>
11810191ffSChris Mason #include <linux/highmem.h>
12e20d96d6SChris Mason #include <linux/fs.h>
13a2de733cSArne Jansen #include <linux/rwsem.h>
14803b2f54SStefan Behrens #include <linux/semaphore.h>
1558176a96SJosef Bacik #include <linux/completion.h>
1604160088SChris Mason #include <linux/backing-dev.h>
17e6dcd2dcSChris Mason #include <linux/wait.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
191abe9b8aSliubo #include <trace/events/btrfs.h>
2065019df8SJohannes Thumshirn #include <asm/unaligned.h>
213b16a4e3SJosef Bacik #include <linux/pagemap.h>
2255e301fdSFilipe Brandenburger #include <linux/btrfs.h>
23db671160SJeff Mahoney #include <linux/btrfs_tree.h>
2421c7e756SMiao Xie #include <linux/workqueue.h>
25f667aef6SQu Wenruo #include <linux/security.h>
26ee22184bSByongho Lee #include <linux/sizes.h>
27897a41b1SJeff Mahoney #include <linux/dynamic_debug.h>
281e4f4714SElena Reshetova #include <linux/refcount.h>
299678c543SNikolay Borisov #include <linux/crc32c.h>
304e4cabecSGoldwyn Rodrigues #include <linux/iomap.h>
31ab3c5c18SSweet Tea Dorminy #include <linux/fscrypt.h>
329c7d3a54SJosef Bacik #include "extent-io-tree.h"
33d1310b2eSChris Mason #include "extent_io.h"
345f39d397SChris Mason #include "extent_map.h"
358b712842SChris Mason #include "async-thread.h"
36d12ffdd1SJosef Bacik #include "block-rsv.h"
372992df73SNikolay Borisov #include "locking.h"
38c7321b76SDavid Sterba #include "misc.h"
39a56159d4SJosef Bacik #include "fs.h"
40e20d96d6SChris Mason 
41e089f05cSChris Mason struct btrfs_trans_handle;
4279154b1bSChris Mason struct btrfs_transaction;
43a22285a6SYan, Zheng struct btrfs_pending_snapshot;
4431890da0SJosef Bacik struct btrfs_delayed_ref_root;
458719aaaeSJosef Bacik struct btrfs_space_info;
4632da5386SDavid Sterba struct btrfs_block_group;
47e6dcd2dcSChris Mason struct btrfs_ordered_sum;
4882fa113fSQu Wenruo struct btrfs_ref;
49c3a3b19bSQu Wenruo struct btrfs_bio;
501881fba8SOmar Sandoval struct btrfs_ioctl_encoded_io_args;
510e75f005SJosef Bacik struct btrfs_device;
520e75f005SJosef Bacik struct btrfs_fs_devices;
530e75f005SJosef Bacik struct btrfs_balance_control;
540e75f005SJosef Bacik struct btrfs_delayed_root;
550e75f005SJosef Bacik struct reloc_control;
56e089f05cSChris Mason 
57ace75066SFilipe Manana /* Read ahead values for struct btrfs_path.reada */
58ace75066SFilipe Manana enum {
59ace75066SFilipe Manana 	READA_NONE,
60ace75066SFilipe Manana 	READA_BACK,
61ace75066SFilipe Manana 	READA_FORWARD,
62ace75066SFilipe Manana 	/*
63ace75066SFilipe Manana 	 * Similar to READA_FORWARD but unlike it:
64ace75066SFilipe Manana 	 *
65ace75066SFilipe Manana 	 * 1) It will trigger readahead even for leaves that are not close to
66ace75066SFilipe Manana 	 *    each other on disk;
67ace75066SFilipe Manana 	 * 2) It also triggers readahead for nodes;
68ace75066SFilipe Manana 	 * 3) During a search, even when a node or leaf is already in memory, it
69ace75066SFilipe Manana 	 *    will still trigger readahead for other nodes and leaves that follow
70ace75066SFilipe Manana 	 *    it.
71ace75066SFilipe Manana 	 *
72ace75066SFilipe Manana 	 * This is meant to be used only when we know we are iterating over the
73ace75066SFilipe Manana 	 * entire tree or a very large part of it.
74ace75066SFilipe Manana 	 */
75ace75066SFilipe Manana 	READA_FORWARD_ALWAYS,
76ace75066SFilipe Manana };
77ace75066SFilipe Manana 
78fec577fbSChris Mason /*
79234b63a0SChris Mason  * btrfs_paths remember the path taken from the root down to the leaf.
80234b63a0SChris Mason  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
81fec577fbSChris Mason  * to any other levels that are present.
82fec577fbSChris Mason  *
83fec577fbSChris Mason  * The slots array records the index of the item or block pointer
84fec577fbSChris Mason  * used while walking the tree.
85fec577fbSChris Mason  */
86234b63a0SChris Mason struct btrfs_path {
875f39d397SChris Mason 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
88234b63a0SChris Mason 	int slots[BTRFS_MAX_LEVEL];
89925baeddSChris Mason 	/* if there is real range locking, this locks field will change */
904fb72bf2SDavid Sterba 	u8 locks[BTRFS_MAX_LEVEL];
91dccabfadSDavid Sterba 	u8 reada;
92925baeddSChris Mason 	/* keep some upper locks as we walk down */
937853f15bSDavid Sterba 	u8 lowest_level;
94459931ecSChris Mason 
95459931ecSChris Mason 	/*
96459931ecSChris Mason 	 * set by btrfs_split_item, tells search_slot to keep all locks
97459931ecSChris Mason 	 * and to force calls to keep space in the nodes
98459931ecSChris Mason 	 */
99b9473439SChris Mason 	unsigned int search_for_split:1;
100b9473439SChris Mason 	unsigned int keep_locks:1;
101b9473439SChris Mason 	unsigned int skip_locking:1;
1025d4f98a2SYan Zheng 	unsigned int search_commit_root:1;
1033f8a18ccSJosef Bacik 	unsigned int need_commit_sem:1;
1045f5bc6b1SFilipe Manana 	unsigned int skip_release_on_error:1;
1059a664971Sethanwu 	/*
1069a664971Sethanwu 	 * Indicate that new item (btrfs_search_slot) is extending already
1079a664971Sethanwu 	 * existing item and ins_len contains only the data size and not item
1089a664971Sethanwu 	 * header (ie. sizeof(struct btrfs_item) is not included).
1099a664971Sethanwu 	 */
1109a664971Sethanwu 	unsigned int search_for_extension:1;
111857bc13fSJosef Bacik 	/* Stop search if any locks need to be taken (for read) */
112857bc13fSJosef Bacik 	unsigned int nowait:1;
113eb60ceacSChris Mason };
114d9d88fdeSJosef Bacik 
115d9d88fdeSJosef Bacik /*
11627cdeb70SMiao Xie  * The state of btrfs root
11727cdeb70SMiao Xie  */
11861fa90c1SDavid Sterba enum {
11927cdeb70SMiao Xie 	/*
12061fa90c1SDavid Sterba 	 * btrfs_record_root_in_trans is a multi-step process, and it can race
12161fa90c1SDavid Sterba 	 * with the balancing code.   But the race is very small, and only the
12261fa90c1SDavid Sterba 	 * first time the root is added to each transaction.  So IN_TRANS_SETUP
12327cdeb70SMiao Xie 	 * is used to tell us when more checks are required
12427cdeb70SMiao Xie 	 */
12561fa90c1SDavid Sterba 	BTRFS_ROOT_IN_TRANS_SETUP,
12692a7cc42SQu Wenruo 
12792a7cc42SQu Wenruo 	/*
12892a7cc42SQu Wenruo 	 * Set if tree blocks of this root can be shared by other roots.
12992a7cc42SQu Wenruo 	 * Only subvolume trees and their reloc trees have this bit set.
13092a7cc42SQu Wenruo 	 * Conflicts with TRACK_DIRTY bit.
13192a7cc42SQu Wenruo 	 *
13292a7cc42SQu Wenruo 	 * This affects two things:
13392a7cc42SQu Wenruo 	 *
13492a7cc42SQu Wenruo 	 * - How balance works
13592a7cc42SQu Wenruo 	 *   For shareable roots, we need to use reloc tree and do path
13692a7cc42SQu Wenruo 	 *   replacement for balance, and need various pre/post hooks for
13792a7cc42SQu Wenruo 	 *   snapshot creation to handle them.
13892a7cc42SQu Wenruo 	 *
13992a7cc42SQu Wenruo 	 *   While for non-shareable trees, we just simply do a tree search
14092a7cc42SQu Wenruo 	 *   with COW.
14192a7cc42SQu Wenruo 	 *
14292a7cc42SQu Wenruo 	 * - How dirty roots are tracked
14392a7cc42SQu Wenruo 	 *   For shareable roots, btrfs_record_root_in_trans() is needed to
14492a7cc42SQu Wenruo 	 *   track them, while non-subvolume roots have TRACK_DIRTY bit, they
14592a7cc42SQu Wenruo 	 *   don't need to set this manually.
14692a7cc42SQu Wenruo 	 */
14792a7cc42SQu Wenruo 	BTRFS_ROOT_SHAREABLE,
14861fa90c1SDavid Sterba 	BTRFS_ROOT_TRACK_DIRTY,
149fc7cbcd4SDavid Sterba 	BTRFS_ROOT_IN_RADIX,
15061fa90c1SDavid Sterba 	BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
15161fa90c1SDavid Sterba 	BTRFS_ROOT_DEFRAG_RUNNING,
15261fa90c1SDavid Sterba 	BTRFS_ROOT_FORCE_COW,
15361fa90c1SDavid Sterba 	BTRFS_ROOT_MULTI_LOG_TASKS,
15461fa90c1SDavid Sterba 	BTRFS_ROOT_DIRTY,
15583354f07SJosef Bacik 	BTRFS_ROOT_DELETING,
156d2311e69SQu Wenruo 
157d2311e69SQu Wenruo 	/*
158d2311e69SQu Wenruo 	 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
159d2311e69SQu Wenruo 	 *
160d2311e69SQu Wenruo 	 * Set for the subvolume tree owning the reloc tree.
161d2311e69SQu Wenruo 	 */
162d2311e69SQu Wenruo 	BTRFS_ROOT_DEAD_RELOC_TREE,
16378c52d9eSJosef Bacik 	/* Mark dead root stored on device whose cleanup needs to be resumed */
16478c52d9eSJosef Bacik 	BTRFS_ROOT_DEAD_TREE,
16547876f7cSFilipe Manana 	/* The root has a log tree. Used for subvolume roots and the tree root. */
166e7a79811SFilipe Manana 	BTRFS_ROOT_HAS_LOG_TREE,
167c53e9653SQu Wenruo 	/* Qgroup flushing is in progress */
168c53e9653SQu Wenruo 	BTRFS_ROOT_QGROUP_FLUSHING,
16954230013SJosef Bacik 	/* We started the orphan cleanup for this root. */
17054230013SJosef Bacik 	BTRFS_ROOT_ORPHAN_CLEANUP,
171b4be6aefSJosef Bacik 	/* This root has a drop operation that was started previously. */
172b4be6aefSJosef Bacik 	BTRFS_ROOT_UNFINISHED_DROP,
173b40130b2SJosef Bacik 	/* This reloc root needs to have its buffers lockdep class reset. */
174b40130b2SJosef Bacik 	BTRFS_ROOT_RESET_LOCKDEP_CLASS,
17561fa90c1SDavid Sterba };
17627cdeb70SMiao Xie 
17727cdeb70SMiao Xie /*
178370a11b8SQu Wenruo  * Record swapped tree blocks of a subvolume tree for delayed subtree trace
179370a11b8SQu Wenruo  * code. For detail check comment in fs/btrfs/qgroup.c.
180370a11b8SQu Wenruo  */
181370a11b8SQu Wenruo struct btrfs_qgroup_swapped_blocks {
182370a11b8SQu Wenruo 	spinlock_t lock;
183370a11b8SQu Wenruo 	/* RM_EMPTY_ROOT() of above blocks[] */
184370a11b8SQu Wenruo 	bool swapped;
185370a11b8SQu Wenruo 	struct rb_root blocks[BTRFS_MAX_LEVEL];
186370a11b8SQu Wenruo };
187370a11b8SQu Wenruo 
188370a11b8SQu Wenruo /*
18962e2749eSChris Mason  * in ram representation of the tree.  extent_root is used for all allocations
190f2458e1dSChris Mason  * and for the extent tree extent_root root.
19162e2749eSChris Mason  */
19262e2749eSChris Mason struct btrfs_root {
193abed4aaaSJosef Bacik 	struct rb_node rb_node;
194abed4aaaSJosef Bacik 
1955f39d397SChris Mason 	struct extent_buffer *node;
196925baeddSChris Mason 
1975f39d397SChris Mason 	struct extent_buffer *commit_root;
198e02119d5SChris Mason 	struct btrfs_root *log_root;
1991a40e23bSZheng Yan 	struct btrfs_root *reloc_root;
20031153d81SYan Zheng 
20127cdeb70SMiao Xie 	unsigned long state;
20262e2749eSChris Mason 	struct btrfs_root_item root_item;
20362e2749eSChris Mason 	struct btrfs_key root_key;
2049f5fae2fSChris Mason 	struct btrfs_fs_info *fs_info;
205d0c803c4SChris Mason 	struct extent_io_tree dirty_log_pages;
206d0c803c4SChris Mason 
207a2135011SChris Mason 	struct mutex objectid_mutex;
2087237f183SYan Zheng 
209f0486c68SYan, Zheng 	spinlock_t accounting_lock;
210f0486c68SYan, Zheng 	struct btrfs_block_rsv *block_rsv;
211f0486c68SYan, Zheng 
212e02119d5SChris Mason 	struct mutex log_mutex;
2137237f183SYan Zheng 	wait_queue_head_t log_writer_wait;
2147237f183SYan Zheng 	wait_queue_head_t log_commit_wait[2];
2158b050d35SMiao Xie 	struct list_head log_ctxs[2];
216a93e0168SFilipe Manana 	/* Used only for log trees of subvolumes, not for the log root tree */
2177237f183SYan Zheng 	atomic_t log_writers;
2187237f183SYan Zheng 	atomic_t log_commit[2];
21928a95795SFilipe Manana 	/* Used only for log trees of subvolumes, not for the log root tree */
2202ecb7923SMiao Xie 	atomic_t log_batch;
221bb14a59bSMiao Xie 	int log_transid;
222d1433debSMiao Xie 	/* No matter the commit succeeds or not*/
223d1433debSMiao Xie 	int log_transid_committed;
224d1433debSMiao Xie 	/* Just be updated when the commit succeeds. */
225bb14a59bSMiao Xie 	int last_log_commit;
226ff782e0aSJosef Bacik 	pid_t log_start_pid;
227ea8c2819SChris Mason 
2280f7d52f4SChris Mason 	u64 last_trans;
2295f39d397SChris Mason 
2309f5fae2fSChris Mason 	u32 type;
23113a8a7c8SYan, Zheng 
2326b8fad57SNikolay Borisov 	u64 free_objectid;
2337585717fSChris Mason 
2346702ed49SChris Mason 	struct btrfs_key defrag_progress;
2350ef3e66bSChris Mason 	struct btrfs_key defrag_max;
2360b86a832SChris Mason 
23792a7cc42SQu Wenruo 	/* The dirty list is only used by non-shareable roots */
2380b86a832SChris Mason 	struct list_head dirty_list;
2397b128766SJosef Bacik 
2405d4f98a2SYan Zheng 	struct list_head root_list;
2415d4f98a2SYan Zheng 
2422ab28f32SJosef Bacik 	spinlock_t log_extents_lock[2];
2432ab28f32SJosef Bacik 	struct list_head logged_list[2];
2442ab28f32SJosef Bacik 
2455d4f98a2SYan Zheng 	spinlock_t inode_lock;
2465d4f98a2SYan Zheng 	/* red-black tree that keeps track of in-memory inodes */
2475d4f98a2SYan Zheng 	struct rb_root inode_tree;
2485d4f98a2SYan Zheng 
2493394e160SChris Mason 	/*
250088aea3bSDavid Sterba 	 * radix tree that keeps track of delayed nodes of every inode,
251088aea3bSDavid Sterba 	 * protected by inode_lock
25216cdcec7SMiao Xie 	 */
253088aea3bSDavid Sterba 	struct radix_tree_root delayed_nodes_tree;
25416cdcec7SMiao Xie 	/*
2553394e160SChris Mason 	 * right now this just gets used so that a root has its own devid
2563394e160SChris Mason 	 * for stat.  It may be used for more later
2573394e160SChris Mason 	 */
2580ee5dc67SAl Viro 	dev_t anon_dev;
259f1ebcc74SLiu Bo 
2605f3ab90aSAnand Jain 	spinlock_t root_item_lock;
2610700cea7SElena Reshetova 	refcount_t refs;
262eb73c1b7SMiao Xie 
263573bfb72SMiao Xie 	struct mutex delalloc_mutex;
264eb73c1b7SMiao Xie 	spinlock_t delalloc_lock;
265eb73c1b7SMiao Xie 	/*
266eb73c1b7SMiao Xie 	 * all of the inodes that have delalloc bytes.  It is possible for
267eb73c1b7SMiao Xie 	 * this list to be empty even when there is still dirty data=ordered
268eb73c1b7SMiao Xie 	 * extents waiting to finish IO.
269eb73c1b7SMiao Xie 	 */
270eb73c1b7SMiao Xie 	struct list_head delalloc_inodes;
271eb73c1b7SMiao Xie 	struct list_head delalloc_root;
272eb73c1b7SMiao Xie 	u64 nr_delalloc_inodes;
27331f3d255SMiao Xie 
27431f3d255SMiao Xie 	struct mutex ordered_extent_mutex;
275199c2a9cSMiao Xie 	/*
276199c2a9cSMiao Xie 	 * this is used by the balancing code to wait for all the pending
277199c2a9cSMiao Xie 	 * ordered extents
278199c2a9cSMiao Xie 	 */
279199c2a9cSMiao Xie 	spinlock_t ordered_extent_lock;
280199c2a9cSMiao Xie 
281199c2a9cSMiao Xie 	/*
282199c2a9cSMiao Xie 	 * all of the data=ordered extents pending writeback
283199c2a9cSMiao Xie 	 * these can span multiple transactions and basically include
284199c2a9cSMiao Xie 	 * every dirty data page that isn't from nodatacow
285199c2a9cSMiao Xie 	 */
286199c2a9cSMiao Xie 	struct list_head ordered_extents;
287199c2a9cSMiao Xie 	struct list_head ordered_root;
288199c2a9cSMiao Xie 	u64 nr_ordered_extents;
2892c686537SDavid Sterba 
2902c686537SDavid Sterba 	/*
291d2311e69SQu Wenruo 	 * Not empty if this subvolume root has gone through tree block swap
292d2311e69SQu Wenruo 	 * (relocation)
293d2311e69SQu Wenruo 	 *
294d2311e69SQu Wenruo 	 * Will be used by reloc_control::dirty_subvol_roots.
295d2311e69SQu Wenruo 	 */
296d2311e69SQu Wenruo 	struct list_head reloc_dirty_list;
297d2311e69SQu Wenruo 
298d2311e69SQu Wenruo 	/*
2992c686537SDavid Sterba 	 * Number of currently running SEND ioctls to prevent
3002c686537SDavid Sterba 	 * manipulation with the read-only status via SUBVOL_SETFLAGS
3012c686537SDavid Sterba 	 */
3022c686537SDavid Sterba 	int send_in_progress;
30362d54f3aSFilipe Manana 	/*
30462d54f3aSFilipe Manana 	 * Number of currently running deduplication operations that have a
30562d54f3aSFilipe Manana 	 * destination inode belonging to this root. Protected by the lock
30662d54f3aSFilipe Manana 	 * root_item_lock.
30762d54f3aSFilipe Manana 	 */
30862d54f3aSFilipe Manana 	int dedupe_in_progress;
309dcc3eb96SNikolay Borisov 	/* For exclusion of snapshot creation and nocow writes */
310dcc3eb96SNikolay Borisov 	struct btrfs_drew_lock snapshot_lock;
311dcc3eb96SNikolay Borisov 
3128ecebf4dSRobbie Ko 	atomic_t snapshot_force_cow;
3138287475aSQu Wenruo 
3148287475aSQu Wenruo 	/* For qgroup metadata reserved space */
3158287475aSQu Wenruo 	spinlock_t qgroup_meta_rsv_lock;
3168287475aSQu Wenruo 	u64 qgroup_meta_rsv_pertrans;
3178287475aSQu Wenruo 	u64 qgroup_meta_rsv_prealloc;
318c53e9653SQu Wenruo 	wait_queue_head_t qgroup_flush_wait;
31957ec5fb4SDavid Sterba 
320eede2bf3SOmar Sandoval 	/* Number of active swapfiles */
321eede2bf3SOmar Sandoval 	atomic_t nr_swapfiles;
322eede2bf3SOmar Sandoval 
323370a11b8SQu Wenruo 	/* Record pairs of swapped blocks for qgroup */
324370a11b8SQu Wenruo 	struct btrfs_qgroup_swapped_blocks swapped_blocks;
325370a11b8SQu Wenruo 
326e289f03eSFilipe Manana 	/* Used only by log trees, when logging csum items */
327e289f03eSFilipe Manana 	struct extent_io_tree log_csum_range;
328e289f03eSFilipe Manana 
32957ec5fb4SDavid Sterba #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
33057ec5fb4SDavid Sterba 	u64 alloc_bytenr;
33157ec5fb4SDavid Sterba #endif
332bd647ce3SJosef Bacik 
333bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
334bd647ce3SJosef Bacik 	struct list_head leak_list;
335bd647ce3SJosef Bacik #endif
33662e2749eSChris Mason };
337118c701eSNikolay Borisov 
btrfs_root_readonly(const struct btrfs_root * root)3381fe5ebc4SJosef Bacik static inline bool btrfs_root_readonly(const struct btrfs_root *root)
3391fe5ebc4SJosef Bacik {
3401fe5ebc4SJosef Bacik 	/* Byte-swap the constant at compile time, root_item::flags is LE */
3411fe5ebc4SJosef Bacik 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
3421fe5ebc4SJosef Bacik }
3431fe5ebc4SJosef Bacik 
btrfs_root_dead(const struct btrfs_root * root)3441fe5ebc4SJosef Bacik static inline bool btrfs_root_dead(const struct btrfs_root *root)
3451fe5ebc4SJosef Bacik {
3461fe5ebc4SJosef Bacik 	/* Byte-swap the constant at compile time, root_item::flags is LE */
3471fe5ebc4SJosef Bacik 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
3481fe5ebc4SJosef Bacik }
3491fe5ebc4SJosef Bacik 
btrfs_root_id(const struct btrfs_root * root)3501fe5ebc4SJosef Bacik static inline u64 btrfs_root_id(const struct btrfs_root *root)
3511fe5ebc4SJosef Bacik {
3521fe5ebc4SJosef Bacik 	return root->root_key.objectid;
3531fe5ebc4SJosef Bacik }
3541fe5ebc4SJosef Bacik 
355bf385648SFilipe Manana /*
356bf385648SFilipe Manana  * Structure that conveys information about an extent that is going to replace
357bf385648SFilipe Manana  * all the extents in a file range.
358bf385648SFilipe Manana  */
359bf385648SFilipe Manana struct btrfs_replace_extent_info {
360690a5dbfSFilipe Manana 	u64 disk_offset;
361690a5dbfSFilipe Manana 	u64 disk_len;
362690a5dbfSFilipe Manana 	u64 data_offset;
363690a5dbfSFilipe Manana 	u64 data_len;
364690a5dbfSFilipe Manana 	u64 file_offset;
365fb870f6cSFilipe Manana 	/* Pointer to a file extent item of type regular or prealloc. */
366690a5dbfSFilipe Manana 	char *extent_buf;
3678fccebfaSFilipe Manana 	/*
3688fccebfaSFilipe Manana 	 * Set to true when attempting to replace a file range with a new extent
3698fccebfaSFilipe Manana 	 * described by this structure, set to false when attempting to clone an
3708fccebfaSFilipe Manana 	 * existing extent into a file range.
3718fccebfaSFilipe Manana 	 */
3728fccebfaSFilipe Manana 	bool is_new_extent;
373983d8209SFilipe Manana 	/* Indicate if we should update the inode's mtime and ctime. */
374983d8209SFilipe Manana 	bool update_times;
3758fccebfaSFilipe Manana 	/* Meaningful only if is_new_extent is true. */
3768fccebfaSFilipe Manana 	int qgroup_reserved;
3778fccebfaSFilipe Manana 	/*
3788fccebfaSFilipe Manana 	 * Meaningful only if is_new_extent is true.
3798fccebfaSFilipe Manana 	 * Used to track how many extent items we have already inserted in a
3808fccebfaSFilipe Manana 	 * subvolume tree that refer to the extent described by this structure,
3818fccebfaSFilipe Manana 	 * so that we know when to create a new delayed ref or update an existing
3828fccebfaSFilipe Manana 	 * one.
3838fccebfaSFilipe Manana 	 */
3848fccebfaSFilipe Manana 	int insertions;
385690a5dbfSFilipe Manana };
386690a5dbfSFilipe Manana 
3875893dfb9SFilipe Manana /* Arguments for btrfs_drop_extents() */
3885893dfb9SFilipe Manana struct btrfs_drop_extents_args {
3895893dfb9SFilipe Manana 	/* Input parameters */
3905893dfb9SFilipe Manana 
3915893dfb9SFilipe Manana 	/*
3925893dfb9SFilipe Manana 	 * If NULL, btrfs_drop_extents() will allocate and free its own path.
3935893dfb9SFilipe Manana 	 * If 'replace_extent' is true, this must not be NULL. Also the path
3945893dfb9SFilipe Manana 	 * is always released except if 'replace_extent' is true and
3955893dfb9SFilipe Manana 	 * btrfs_drop_extents() sets 'extent_inserted' to true, in which case
3965893dfb9SFilipe Manana 	 * the path is kept locked.
3975893dfb9SFilipe Manana 	 */
3985893dfb9SFilipe Manana 	struct btrfs_path *path;
3995893dfb9SFilipe Manana 	/* Start offset of the range to drop extents from */
4005893dfb9SFilipe Manana 	u64 start;
4015893dfb9SFilipe Manana 	/* End (exclusive, last byte + 1) of the range to drop extents from */
4025893dfb9SFilipe Manana 	u64 end;
4035893dfb9SFilipe Manana 	/* If true drop all the extent maps in the range */
4045893dfb9SFilipe Manana 	bool drop_cache;
4055893dfb9SFilipe Manana 	/*
4065893dfb9SFilipe Manana 	 * If true it means we want to insert a new extent after dropping all
4075893dfb9SFilipe Manana 	 * the extents in the range. If this is true, the 'extent_item_size'
4085893dfb9SFilipe Manana 	 * parameter must be set as well and the 'extent_inserted' field will
4095893dfb9SFilipe Manana 	 * be set to true by btrfs_drop_extents() if it could insert the new
4105893dfb9SFilipe Manana 	 * extent.
4115893dfb9SFilipe Manana 	 * Note: when this is set to true the path must not be NULL.
4125893dfb9SFilipe Manana 	 */
4135893dfb9SFilipe Manana 	bool replace_extent;
4145893dfb9SFilipe Manana 	/*
4155893dfb9SFilipe Manana 	 * Used if 'replace_extent' is true. Size of the file extent item to
4165893dfb9SFilipe Manana 	 * insert after dropping all existing extents in the range
4175893dfb9SFilipe Manana 	 */
4185893dfb9SFilipe Manana 	u32 extent_item_size;
4195893dfb9SFilipe Manana 
4205893dfb9SFilipe Manana 	/* Output parameters */
4215893dfb9SFilipe Manana 
4225893dfb9SFilipe Manana 	/*
4235893dfb9SFilipe Manana 	 * Set to the minimum between the input parameter 'end' and the end
4245893dfb9SFilipe Manana 	 * (exclusive, last byte + 1) of the last dropped extent. This is always
4255893dfb9SFilipe Manana 	 * set even if btrfs_drop_extents() returns an error.
4265893dfb9SFilipe Manana 	 */
4275893dfb9SFilipe Manana 	u64 drop_end;
4285893dfb9SFilipe Manana 	/*
4292766ff61SFilipe Manana 	 * The number of allocated bytes found in the range. This can be smaller
4302766ff61SFilipe Manana 	 * than the range's length when there are holes in the range.
4312766ff61SFilipe Manana 	 */
4322766ff61SFilipe Manana 	u64 bytes_found;
4332766ff61SFilipe Manana 	/*
4345893dfb9SFilipe Manana 	 * Only set if 'replace_extent' is true. Set to true if we were able
4355893dfb9SFilipe Manana 	 * to insert a replacement extent after dropping all extents in the
4365893dfb9SFilipe Manana 	 * range, otherwise set to false by btrfs_drop_extents().
4375893dfb9SFilipe Manana 	 * Also, if btrfs_drop_extents() has set this to true it means it
4385893dfb9SFilipe Manana 	 * returned with the path locked, otherwise if it has set this to
4395893dfb9SFilipe Manana 	 * false it has returned with the path released.
4405893dfb9SFilipe Manana 	 */
4415893dfb9SFilipe Manana 	bool extent_inserted;
4425893dfb9SFilipe Manana };
4435893dfb9SFilipe Manana 
44423b5ec74SJosef Bacik struct btrfs_file_private {
44523b5ec74SJosef Bacik 	void *filldir_buf;
4469b378f6aSFilipe Manana 	u64 last_index;
4473c32c721SFilipe Manana 	struct extent_state *llseek_cached_state;
448f56a6d9cSFilipe Manana 	/* Task that allocated this structure. */
449f56a6d9cSFilipe Manana 	struct task_struct *owner_task;
45023b5ec74SJosef Bacik };
45123b5ec74SJosef Bacik 
BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info * info)452da17066cSJeff Mahoney static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
4531db1ff92SJeff Mahoney {
454118c701eSNikolay Borisov 	return info->nodesize - sizeof(struct btrfs_header);
4551db1ff92SJeff Mahoney }
4561db1ff92SJeff Mahoney 
BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info * info)457da17066cSJeff Mahoney static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
4581db1ff92SJeff Mahoney {
459da17066cSJeff Mahoney 	return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
4601db1ff92SJeff Mahoney }
4611db1ff92SJeff Mahoney 
BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info * info)462da17066cSJeff Mahoney static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
4631db1ff92SJeff Mahoney {
464da17066cSJeff Mahoney 	return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
4651db1ff92SJeff Mahoney }
4661db1ff92SJeff Mahoney 
BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info * info)467da17066cSJeff Mahoney static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
4681db1ff92SJeff Mahoney {
469da17066cSJeff Mahoney 	return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
4701db1ff92SJeff Mahoney }
4711db1ff92SJeff Mahoney 
4722e78c927SChandan Rajendra #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
473265fdfa6SDavid Sterba 				((bytes) >> (fs_info)->sectorsize_bits)
4742e78c927SChandan Rajendra 
btrfs_crc32c(u32 crc,const void * address,unsigned length)47565019df8SJohannes Thumshirn static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
47665019df8SJohannes Thumshirn {
47765019df8SJohannes Thumshirn 	return crc32c(crc, address, length);
47865019df8SJohannes Thumshirn }
47965019df8SJohannes Thumshirn 
btrfs_crc32c_final(u32 crc,u8 * result)48065019df8SJohannes Thumshirn static inline void btrfs_crc32c_final(u32 crc, u8 *result)
48165019df8SJohannes Thumshirn {
48265019df8SJohannes Thumshirn 	put_unaligned_le32(~crc, result);
48365019df8SJohannes Thumshirn }
48465019df8SJohannes Thumshirn 
btrfs_name_hash(const char * name,int len)4859678c543SNikolay Borisov static inline u64 btrfs_name_hash(const char *name, int len)
4869678c543SNikolay Borisov {
4879678c543SNikolay Borisov        return crc32c((u32)~1, name, len);
4889678c543SNikolay Borisov }
4899678c543SNikolay Borisov 
4909678c543SNikolay Borisov /*
4919678c543SNikolay Borisov  * Figure the key offset of an extended inode ref
4929678c543SNikolay Borisov  */
btrfs_extref_hash(u64 parent_objectid,const char * name,int len)4939678c543SNikolay Borisov static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
4949678c543SNikolay Borisov                                    int len)
4959678c543SNikolay Borisov {
4969678c543SNikolay Borisov        return (u64) crc32c(parent_objectid, name, len);
4979678c543SNikolay Borisov }
4989678c543SNikolay Borisov 
btrfs_alloc_write_mask(struct address_space * mapping)4993b16a4e3SJosef Bacik static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
5003b16a4e3SJosef Bacik {
501c62d2555SMichal Hocko 	return mapping_gfp_constraint(mapping, ~__GFP_FS);
5023b16a4e3SJosef Bacik }
5033b16a4e3SJosef Bacik 
5042ff7e61eSJeff Mahoney int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
505acce952bSliubo 				   u64 start, u64 end);
5062ff7e61eSJeff Mahoney int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
5075378e607SLi Dongyang 			 u64 num_bytes, u64 *actual_bytes);
5082ff7e61eSJeff Mahoney int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
509acce952bSliubo 
510dee26a9fSChris Mason /* ctree.c */
511226463d7SJosef Bacik int __init btrfs_ctree_init(void);
512226463d7SJosef Bacik void __cold btrfs_ctree_exit(void);
5137b00dfffSFilipe Manana 
514fdf8d595SAnand Jain int btrfs_bin_search(struct extent_buffer *eb, int first_slot,
5157b00dfffSFilipe Manana 		     const struct btrfs_key *key, int *slot);
5167b00dfffSFilipe Manana 
517e1f60a65SDavid Sterba int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
5180b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
5190b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
5200b86a832SChris Mason 			int type);
521ade2e0b3SWang Shilong int btrfs_previous_extent_item(struct btrfs_root *root,
522ade2e0b3SWang Shilong 			struct btrfs_path *path, u64 min_objectid);
523d5e09e38SFilipe Manana void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
524b7a0365eSDaniel Dressler 			     struct btrfs_path *path,
525310712b2SOmar Sandoval 			     const struct btrfs_key *new_key);
526925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
527e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5283f157a2fSChris Mason 			struct btrfs_key *key, int lowest_level,
529de78b51aSEric Sandeen 			u64 min_trans);
5303f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
531de78b51aSEric Sandeen 			 struct btrfs_path *path,
5323f157a2fSChris Mason 			 u64 min_trans);
5334b231ae4SDavid Sterba struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
5344b231ae4SDavid Sterba 					   int slot);
5354b231ae4SDavid Sterba 
5365f39d397SChris Mason int btrfs_cow_block(struct btrfs_trans_handle *trans,
5375f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5385f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5399631e4ccSJosef Bacik 		    struct extent_buffer **cow_ret,
5409631e4ccSJosef Bacik 		    enum btrfs_lock_nesting nest);
541*0d2cc60bSFilipe Manana int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
542*0d2cc60bSFilipe Manana 			  struct btrfs_root *root,
543*0d2cc60bSFilipe Manana 			  struct extent_buffer *buf,
544*0d2cc60bSFilipe Manana 			  struct extent_buffer *parent, int parent_slot,
545*0d2cc60bSFilipe Manana 			  struct extent_buffer **cow_ret,
546*0d2cc60bSFilipe Manana 			  u64 search_start, u64 empty_size,
547*0d2cc60bSFilipe Manana 			  enum btrfs_lock_nesting nest);
548be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
549be20aa9dSChris Mason 		      struct btrfs_root *root,
550be20aa9dSChris Mason 		      struct extent_buffer *buf,
551be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid);
552eb96e221SFilipe Manana int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
553eb96e221SFilipe Manana 			      struct btrfs_root *root,
5545d4f98a2SYan Zheng 			      struct extent_buffer *buf);
555751a2761SFilipe Manana int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
556751a2761SFilipe Manana 		  struct btrfs_path *path, int level, int slot);
557d5e09e38SFilipe Manana void btrfs_extend_item(struct btrfs_trans_handle *trans,
558d5e09e38SFilipe Manana 		       struct btrfs_path *path, u32 data_size);
559d5e09e38SFilipe Manana void btrfs_truncate_item(struct btrfs_trans_handle *trans,
560d5e09e38SFilipe Manana 			 struct btrfs_path *path, u32 new_size, int from_end);
561459931ecSChris Mason int btrfs_split_item(struct btrfs_trans_handle *trans,
562459931ecSChris Mason 		     struct btrfs_root *root,
563459931ecSChris Mason 		     struct btrfs_path *path,
564310712b2SOmar Sandoval 		     const struct btrfs_key *new_key,
565459931ecSChris Mason 		     unsigned long split_offset);
566ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
567ad48fd75SYan, Zheng 			 struct btrfs_root *root,
568ad48fd75SYan, Zheng 			 struct btrfs_path *path,
569310712b2SOmar Sandoval 			 const struct btrfs_key *new_key);
570e33d5c3dSKelley Nielsen int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
571e33d5c3dSKelley Nielsen 		u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
572310712b2SOmar Sandoval int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
573310712b2SOmar Sandoval 		      const struct btrfs_key *key, struct btrfs_path *p,
574310712b2SOmar Sandoval 		      int ins_len, int cow);
575310712b2SOmar Sandoval int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
5765d9e75c4SJan Schmidt 			  struct btrfs_path *p, u64 time_seq);
5772f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root,
578310712b2SOmar Sandoval 			       const struct btrfs_key *key,
579310712b2SOmar Sandoval 			       struct btrfs_path *p, int find_higher,
580310712b2SOmar Sandoval 			       int return_any);
5816702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
5825f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
583de78b51aSEric Sandeen 		       int start_slot, u64 *last_ret,
584a6b6e75eSChris Mason 		       struct btrfs_key *progress);
585b3b4aa74SDavid Sterba void btrfs_release_path(struct btrfs_path *p);
5862c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void);
5872c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p);
588b4ce94deSChris Mason 
58985e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
59085e21bacSChris Mason 		   struct btrfs_path *path, int slot, int nr);
btrfs_del_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path)59185e21bacSChris Mason static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
59285e21bacSChris Mason 				 struct btrfs_root *root,
59385e21bacSChris Mason 				 struct btrfs_path *path)
59485e21bacSChris Mason {
59585e21bacSChris Mason 	return btrfs_del_items(trans, root, path, path->slots[0], 1);
59685e21bacSChris Mason }
59785e21bacSChris Mason 
598b7ef5f3aSFilipe Manana /*
599b7ef5f3aSFilipe Manana  * Describes a batch of items to insert in a btree. This is used by
600f0641656SFilipe Manana  * btrfs_insert_empty_items().
601b7ef5f3aSFilipe Manana  */
602b7ef5f3aSFilipe Manana struct btrfs_item_batch {
603b7ef5f3aSFilipe Manana 	/*
604b7ef5f3aSFilipe Manana 	 * Pointer to an array containing the keys of the items to insert (in
605b7ef5f3aSFilipe Manana 	 * sorted order).
606b7ef5f3aSFilipe Manana 	 */
607b7ef5f3aSFilipe Manana 	const struct btrfs_key *keys;
608b7ef5f3aSFilipe Manana 	/* Pointer to an array containing the data size for each item to insert. */
609b7ef5f3aSFilipe Manana 	const u32 *data_sizes;
610b7ef5f3aSFilipe Manana 	/*
611b7ef5f3aSFilipe Manana 	 * The sum of data sizes for all items. The caller can compute this while
612b7ef5f3aSFilipe Manana 	 * setting up the data_sizes array, so it ends up being more efficient
613b7ef5f3aSFilipe Manana 	 * than having btrfs_insert_empty_items() or setup_item_for_insert()
614b7ef5f3aSFilipe Manana 	 * doing it, as it would avoid an extra loop over a potentially large
615b7ef5f3aSFilipe Manana 	 * array, and in the case of setup_item_for_insert(), we would be doing
616b7ef5f3aSFilipe Manana 	 * it while holding a write lock on a leaf and often on upper level nodes
617b7ef5f3aSFilipe Manana 	 * too, unnecessarily increasing the size of a critical section.
618b7ef5f3aSFilipe Manana 	 */
619b7ef5f3aSFilipe Manana 	u32 total_data_size;
620b7ef5f3aSFilipe Manana 	/* Size of the keys and data_sizes arrays (number of items in the batch). */
621b7ef5f3aSFilipe Manana 	int nr;
622b7ef5f3aSFilipe Manana };
623b7ef5f3aSFilipe Manana 
624d5e09e38SFilipe Manana void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
625d5e09e38SFilipe Manana 				 struct btrfs_root *root,
626b7ef5f3aSFilipe Manana 				 struct btrfs_path *path,
627b7ef5f3aSFilipe Manana 				 const struct btrfs_key *key,
628f0641656SFilipe Manana 				 u32 data_size);
629310712b2SOmar Sandoval int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
630310712b2SOmar Sandoval 		      const struct btrfs_key *key, void *data, u32 data_size);
6319c58309dSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
6329c58309dSChris Mason 			     struct btrfs_root *root,
6339c58309dSChris Mason 			     struct btrfs_path *path,
634b7ef5f3aSFilipe Manana 			     const struct btrfs_item_batch *batch);
6359c58309dSChris Mason 
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)6369c58309dSChris Mason static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
6379c58309dSChris Mason 					  struct btrfs_root *root,
6389c58309dSChris Mason 					  struct btrfs_path *path,
639310712b2SOmar Sandoval 					  const struct btrfs_key *key,
6409c58309dSChris Mason 					  u32 data_size)
6419c58309dSChris Mason {
642b7ef5f3aSFilipe Manana 	struct btrfs_item_batch batch;
643b7ef5f3aSFilipe Manana 
644b7ef5f3aSFilipe Manana 	batch.keys = key;
645b7ef5f3aSFilipe Manana 	batch.data_sizes = &data_size;
646b7ef5f3aSFilipe Manana 	batch.total_data_size = data_size;
647b7ef5f3aSFilipe Manana 	batch.nr = 1;
648b7ef5f3aSFilipe Manana 
649b7ef5f3aSFilipe Manana 	return btrfs_insert_empty_items(trans, root, path, &batch);
6509c58309dSChris Mason }
6519c58309dSChris Mason 
6523d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
6533d7806ecSJan Schmidt 			u64 time_seq);
6540ff40a91SMarcos Paulo de Souza 
6550ff40a91SMarcos Paulo de Souza int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
6560ff40a91SMarcos Paulo de Souza 			   struct btrfs_path *path);
6570ff40a91SMarcos Paulo de Souza 
65862142be3SGabriel Niebler int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
65962142be3SGabriel Niebler 			      struct btrfs_path *path);
66062142be3SGabriel Niebler 
66162142be3SGabriel Niebler /*
66262142be3SGabriel Niebler  * Search in @root for a given @key, and store the slot found in @found_key.
66362142be3SGabriel Niebler  *
66462142be3SGabriel Niebler  * @root:	The root node of the tree.
66562142be3SGabriel Niebler  * @key:	The key we are looking for.
66662142be3SGabriel Niebler  * @found_key:	Will hold the found item.
66762142be3SGabriel Niebler  * @path:	Holds the current slot/leaf.
66862142be3SGabriel Niebler  * @iter_ret:	Contains the value returned from btrfs_search_slot or
66962142be3SGabriel Niebler  * 		btrfs_get_next_valid_item, whichever was executed last.
67062142be3SGabriel Niebler  *
67162142be3SGabriel Niebler  * The @iter_ret is an output variable that will contain the return value of
67262142be3SGabriel Niebler  * btrfs_search_slot, if it encountered an error, or the value returned from
67362142be3SGabriel Niebler  * btrfs_get_next_valid_item otherwise. That return value can be 0, if a valid
67462142be3SGabriel Niebler  * slot was found, 1 if there were no more leaves, and <0 if there was an error.
67562142be3SGabriel Niebler  *
67662142be3SGabriel Niebler  * It's recommended to use a separate variable for iter_ret and then use it to
67762142be3SGabriel Niebler  * set the function return value so there's no confusion of the 0/1/errno
67862142be3SGabriel Niebler  * values stemming from btrfs_search_slot.
67962142be3SGabriel Niebler  */
68062142be3SGabriel Niebler #define btrfs_for_each_slot(root, key, found_key, path, iter_ret)		\
68162142be3SGabriel Niebler 	for (iter_ret = btrfs_search_slot(NULL, (root), (key), (path), 0, 0);	\
68262142be3SGabriel Niebler 		(iter_ret) >= 0 &&						\
68362142be3SGabriel Niebler 		(iter_ret = btrfs_get_next_valid_item((root), (found_key), (path))) == 0; \
68462142be3SGabriel Niebler 		(path)->slots[0]++						\
68562142be3SGabriel Niebler 	)
68662142be3SGabriel Niebler 
687890d2b1aSJosef Bacik int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq);
688809d6902SDavid Sterba 
689809d6902SDavid Sterba /*
690809d6902SDavid Sterba  * Search the tree again to find a leaf with greater keys.
691809d6902SDavid Sterba  *
692809d6902SDavid Sterba  * Returns 0 if it found something or 1 if there are no greater leaves.
693809d6902SDavid Sterba  * Returns < 0 on error.
694809d6902SDavid Sterba  */
btrfs_next_leaf(struct btrfs_root * root,struct btrfs_path * path)695809d6902SDavid Sterba static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
696809d6902SDavid Sterba {
697809d6902SDavid Sterba 	return btrfs_next_old_leaf(root, path, 0);
698809d6902SDavid Sterba }
699809d6902SDavid Sterba 
btrfs_next_item(struct btrfs_root * root,struct btrfs_path * p)7001c8f52a5SAlexander Block static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
7011c8f52a5SAlexander Block {
7021c8f52a5SAlexander Block 	return btrfs_next_old_item(root, p, 0);
7031c8f52a5SAlexander Block }
7046c75a589SQu Wenruo int btrfs_leaf_free_space(const struct extent_buffer *leaf);
705babbf170SMiao Xie 
is_fstree(u64 rootid)70695a06077SJan Schmidt static inline int is_fstree(u64 rootid)
70795a06077SJan Schmidt {
70895a06077SJan Schmidt 	if (rootid == BTRFS_FS_TREE_OBJECTID ||
709e09fe2d2SQu Wenruo 	    ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
710e09fe2d2SQu Wenruo 	      !btrfs_qgroup_level(rootid)))
71195a06077SJan Schmidt 		return 1;
71295a06077SJan Schmidt 	return 0;
71395a06077SJan Schmidt }
714210549ebSDavid Sterba 
btrfs_is_data_reloc_root(const struct btrfs_root * root)71537f00a6dSJohannes Thumshirn static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root)
71637f00a6dSJohannes Thumshirn {
71737f00a6dSJohannes Thumshirn 	return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID;
71837f00a6dSJohannes Thumshirn }
71937f00a6dSJohannes Thumshirn 
720b3cbfb0dSJosef Bacik u16 btrfs_csum_type_size(u16 type);
7210e6c40ebSJosef Bacik int btrfs_super_csum_size(const struct btrfs_super_block *s);
7220e6c40ebSJosef Bacik const char *btrfs_super_csum_name(u16 csum_type);
7230e6c40ebSJosef Bacik const char *btrfs_super_csum_driver(u16 csum_type);
7240e6c40ebSJosef Bacik size_t __attribute_const__ btrfs_get_num_csums(void);
7250e6c40ebSJosef Bacik 
726f57ad937SQu Wenruo /*
727f57ad937SQu Wenruo  * We use page status Private2 to indicate there is an ordered extent with
728f57ad937SQu Wenruo  * unfinished IO.
729f57ad937SQu Wenruo  *
730f57ad937SQu Wenruo  * Rename the Private2 accessors to Ordered, to improve readability.
731f57ad937SQu Wenruo  */
732f57ad937SQu Wenruo #define PageOrdered(page)		PagePrivate2(page)
733f57ad937SQu Wenruo #define SetPageOrdered(page)		SetPagePrivate2(page)
734f57ad937SQu Wenruo #define ClearPageOrdered(page)		ClearPagePrivate2(page)
735895586ebSMatthew Wilcox (Oracle) #define folio_test_ordered(folio)	folio_test_private_2(folio)
736895586ebSMatthew Wilcox (Oracle) #define folio_set_ordered(folio)	folio_set_private_2(folio)
737895586ebSMatthew Wilcox (Oracle) #define folio_clear_ordered(folio)	folio_clear_private_2(folio)
738f57ad937SQu Wenruo 
739eb60ceacSChris Mason #endif
740