xref: /linux/fs/btrfs/ctree.h (revision 50c44fea13ec339d0d457079b254e8c8420d6511)
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/cleanup.h>
10 #include <linux/spinlock.h>
11 #include <linux/rbtree.h>
12 #include <linux/mutex.h>
13 #include <linux/wait.h>
14 #include <linux/list.h>
15 #include <linux/atomic.h>
16 #include <linux/xarray.h>
17 #include <linux/refcount.h>
18 #include <uapi/linux/btrfs_tree.h>
19 #include "locking.h"
20 #include "accessors.h"
21 
22 struct extent_buffer;
23 struct btrfs_block_rsv;
24 struct btrfs_trans_handle;
25 struct btrfs_block_group;
26 
27 /* Read ahead values for struct btrfs_path.reada */
28 enum {
29 	READA_NONE,
30 	READA_BACK,
31 	READA_FORWARD,
32 	/*
33 	 * Similar to READA_FORWARD but unlike it:
34 	 *
35 	 * 1) It will trigger readahead even for leaves that are not close to
36 	 *    each other on disk;
37 	 * 2) It also triggers readahead for nodes;
38 	 * 3) During a search, even when a node or leaf is already in memory, it
39 	 *    will still trigger readahead for other nodes and leaves that follow
40 	 *    it.
41 	 *
42 	 * This is meant to be used only when we know we are iterating over the
43 	 * entire tree or a very large part of it.
44 	 */
45 	READA_FORWARD_ALWAYS,
46 };
47 
48 /*
49  * btrfs_paths remember the path taken from the root down to the leaf.
50  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
51  * to any other levels that are present.
52  *
53  * The slots array records the index of the item or block pointer
54  * used while walking the tree.
55  */
56 struct btrfs_path {
57 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
58 	int slots[BTRFS_MAX_LEVEL];
59 	/* if there is real range locking, this locks field will change */
60 	u8 locks[BTRFS_MAX_LEVEL];
61 	u8 reada;
62 	u8 lowest_level;
63 
64 	/*
65 	 * set by btrfs_split_item, tells search_slot to keep all locks
66 	 * and to force calls to keep space in the nodes
67 	 */
68 	bool search_for_split:1;
69 	/* Keep some upper locks as we walk down. */
70 	bool keep_locks:1;
71 	bool skip_locking:1;
72 	bool search_commit_root:1;
73 	bool need_commit_sem:1;
74 	bool skip_release_on_error:1;
75 	/*
76 	 * Indicate that new item (btrfs_search_slot) is extending already
77 	 * existing item and ins_len contains only the data size and not item
78 	 * header (ie. sizeof(struct btrfs_item) is not included).
79 	 */
80 	bool search_for_extension:1;
81 	/* Stop search if any locks need to be taken (for read) */
82 	bool nowait:1;
83 };
84 
85 #define BTRFS_PATH_AUTO_FREE(path_name)					\
86 	struct btrfs_path *path_name __free(btrfs_free_path) = NULL
87 
88 /*
89  * This defines an on-stack path that will be auto released when exiting the scope.
90  *
91  * It is compatible with any existing manual btrfs_release_path() calls.
92  */
93 #define BTRFS_PATH_AUTO_RELEASE(path_name)					\
94 	struct btrfs_path path_name __free(btrfs_release_path) = { 0 }
95 
96 /*
97  * The state of btrfs root
98  */
99 enum {
100 	/*
101 	 * btrfs_record_root_in_trans is a multi-step process, and it can race
102 	 * with the balancing code.   But the race is very small, and only the
103 	 * first time the root is added to each transaction.  So IN_TRANS_SETUP
104 	 * is used to tell us when more checks are required
105 	 */
106 	BTRFS_ROOT_IN_TRANS_SETUP,
107 
108 	/*
109 	 * Set if tree blocks of this root can be shared by other roots.
110 	 * Only subvolume trees and their reloc trees have this bit set.
111 	 * Conflicts with TRACK_DIRTY bit.
112 	 *
113 	 * This affects two things:
114 	 *
115 	 * - How balance works
116 	 *   For shareable roots, we need to use reloc tree and do path
117 	 *   replacement for balance, and need various pre/post hooks for
118 	 *   snapshot creation to handle them.
119 	 *
120 	 *   While for non-shareable trees, we just simply do a tree search
121 	 *   with COW.
122 	 *
123 	 * - How dirty roots are tracked
124 	 *   For shareable roots, btrfs_record_root_in_trans() is needed to
125 	 *   track them, while non-subvolume roots have TRACK_DIRTY bit, they
126 	 *   don't need to set this manually.
127 	 */
128 	BTRFS_ROOT_SHAREABLE,
129 	BTRFS_ROOT_TRACK_DIRTY,
130 	BTRFS_ROOT_IN_RADIX,
131 	BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
132 	BTRFS_ROOT_DEFRAG_RUNNING,
133 	BTRFS_ROOT_FORCE_COW,
134 	BTRFS_ROOT_DIRTY,
135 	BTRFS_ROOT_DELETING,
136 
137 	/*
138 	 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
139 	 *
140 	 * Set for the subvolume tree owning the reloc tree.
141 	 */
142 	BTRFS_ROOT_DEAD_RELOC_TREE,
143 	/* Mark dead root stored on device whose cleanup needs to be resumed */
144 	BTRFS_ROOT_DEAD_TREE,
145 	/* The root has a log tree. Used for subvolume roots and the tree root. */
146 	BTRFS_ROOT_HAS_LOG_TREE,
147 	/* Qgroup flushing is in progress */
148 	BTRFS_ROOT_QGROUP_FLUSHING,
149 	/* We started the orphan cleanup for this root. */
150 	BTRFS_ROOT_ORPHAN_CLEANUP,
151 	/* This root has a drop operation that was started previously. */
152 	BTRFS_ROOT_UNFINISHED_DROP,
153 	/* This reloc root needs to have its buffers lockdep class reset. */
154 	BTRFS_ROOT_RESET_LOCKDEP_CLASS,
155 };
156 
157 /*
158  * Record swapped tree blocks of a subvolume tree for delayed subtree trace
159  * code. For detail check comment in fs/btrfs/qgroup.c.
160  */
161 struct btrfs_qgroup_swapped_blocks {
162 	spinlock_t lock;
163 	/* RM_EMPTY_ROOT() of above blocks[] */
164 	bool swapped;
165 	struct rb_root blocks[BTRFS_MAX_LEVEL];
166 };
167 
168 /*
169  * in ram representation of the tree.  extent_root is used for all allocations
170  * and for the extent tree extent_root root.
171  */
172 struct btrfs_root {
173 	struct rb_node rb_node;
174 
175 	struct extent_buffer *node;
176 
177 	struct extent_buffer *commit_root;
178 	struct btrfs_root *log_root;
179 	struct btrfs_root *reloc_root;
180 
181 	unsigned long state;
182 	struct btrfs_root_item root_item;
183 	struct btrfs_key root_key;
184 	struct btrfs_fs_info *fs_info;
185 	struct extent_io_tree dirty_log_pages;
186 
187 	struct mutex objectid_mutex;
188 
189 	spinlock_t accounting_lock;
190 	struct btrfs_block_rsv *block_rsv;
191 
192 	struct mutex log_mutex;
193 	wait_queue_head_t log_writer_wait;
194 	wait_queue_head_t log_commit_wait[2];
195 	struct list_head log_ctxs[2];
196 	/* Used only for log trees of subvolumes, not for the log root tree */
197 	atomic_t log_writers;
198 	bool log_commit[2];
199 	/*
200 	 * Protected by the 'log_mutex' lock but can be read without holding
201 	 * that lock to avoid unnecessary lock contention, in which case it
202 	 * should be read using btrfs_get_root_log_transid() except if it's a
203 	 * log tree in which case it can be directly accessed. Updates to this
204 	 * field should always use btrfs_set_root_log_transid(), except for log
205 	 * trees where the field can be updated directly.
206 	 */
207 	int log_transid;
208 	/* No matter the commit succeeds or not*/
209 	int log_transid_committed;
210 	/*
211 	 * Just be updated when the commit succeeds. Use
212 	 * btrfs_get_root_last_log_commit() and btrfs_set_root_last_log_commit()
213 	 * to access this field.
214 	 */
215 	int last_log_commit;
216 
217 	u64 last_trans;
218 
219 	u64 free_objectid;
220 
221 	struct btrfs_key defrag_progress;
222 	struct btrfs_key defrag_max;
223 
224 	/* The dirty list is only used by non-shareable roots */
225 	struct list_head dirty_list;
226 
227 	struct list_head root_list;
228 
229 	/* Xarray that keeps track of in-memory inodes. */
230 	struct xarray inodes;
231 
232 	/* Xarray that keeps track of delayed nodes of every inode. */
233 	struct xarray delayed_nodes;
234 	/*
235 	 * right now this just gets used so that a root has its own devid
236 	 * for stat.  It may be used for more later
237 	 */
238 	dev_t anon_dev;
239 
240 	spinlock_t root_item_lock;
241 	refcount_t refs;
242 
243 	struct mutex delalloc_mutex;
244 	spinlock_t delalloc_lock;
245 	/*
246 	 * all of the inodes that have delalloc bytes.  It is possible for
247 	 * this list to be empty even when there is still dirty data=ordered
248 	 * extents waiting to finish IO.
249 	 */
250 	struct list_head delalloc_inodes;
251 	struct list_head delalloc_root;
252 	u64 nr_delalloc_inodes;
253 
254 	struct mutex ordered_extent_mutex;
255 	/*
256 	 * this is used by the balancing code to wait for all the pending
257 	 * ordered extents
258 	 */
259 	spinlock_t ordered_extent_lock;
260 
261 	/*
262 	 * all of the data=ordered extents pending writeback
263 	 * these can span multiple transactions and basically include
264 	 * every dirty data page that isn't from nodatacow
265 	 */
266 	struct list_head ordered_extents;
267 	struct list_head ordered_root;
268 	u64 nr_ordered_extents;
269 
270 	/*
271 	 * Not empty if this subvolume root has gone through tree block swap
272 	 * (relocation)
273 	 *
274 	 * Will be used by reloc_control::dirty_subvol_roots.
275 	 */
276 	struct list_head reloc_dirty_list;
277 
278 	/*
279 	 * Number of currently running SEND ioctls to prevent
280 	 * manipulation with the read-only status via SUBVOL_SETFLAGS
281 	 */
282 	int send_in_progress;
283 	/*
284 	 * Number of currently running deduplication operations that have a
285 	 * destination inode belonging to this root. Protected by the lock
286 	 * root_item_lock.
287 	 */
288 	int dedupe_in_progress;
289 	/* For exclusion of snapshot creation and nocow writes */
290 	struct btrfs_drew_lock snapshot_lock;
291 
292 	atomic_t snapshot_force_cow;
293 
294 	/* For qgroup metadata reserved space */
295 	spinlock_t qgroup_meta_rsv_lock;
296 	u64 qgroup_meta_rsv_pertrans;
297 	u64 qgroup_meta_rsv_prealloc;
298 	wait_queue_head_t qgroup_flush_wait;
299 
300 	/* Number of active swapfiles */
301 	atomic_t nr_swapfiles;
302 
303 	/* Record pairs of swapped blocks for qgroup */
304 	struct btrfs_qgroup_swapped_blocks swapped_blocks;
305 
306 	/* Used only by log trees, when logging csum items */
307 	struct extent_io_tree log_csum_range;
308 
309 	/* Used in simple quotas, track root during relocation. */
310 	u64 relocation_src_root;
311 
312 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
313 	u64 alloc_bytenr;
314 #endif
315 
316 #ifdef CONFIG_BTRFS_DEBUG
317 	struct list_head leak_list;
318 #endif
319 };
320 
btrfs_root_readonly(const struct btrfs_root * root)321 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
322 {
323 	/* Byte-swap the constant at compile time, root_item::flags is LE */
324 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
325 }
326 
btrfs_root_dead(const struct btrfs_root * root)327 static inline bool btrfs_root_dead(const struct btrfs_root *root)
328 {
329 	/* Byte-swap the constant at compile time, root_item::flags is LE */
330 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
331 }
332 
btrfs_root_id(const struct btrfs_root * root)333 static inline u64 btrfs_root_id(const struct btrfs_root *root)
334 {
335 	return root->root_key.objectid;
336 }
337 
btrfs_get_root_log_transid(const struct btrfs_root * root)338 static inline int btrfs_get_root_log_transid(const struct btrfs_root *root)
339 {
340 	return READ_ONCE(root->log_transid);
341 }
342 
btrfs_set_root_log_transid(struct btrfs_root * root,int log_transid)343 static inline void btrfs_set_root_log_transid(struct btrfs_root *root, int log_transid)
344 {
345 	WRITE_ONCE(root->log_transid, log_transid);
346 }
347 
btrfs_get_root_last_log_commit(const struct btrfs_root * root)348 static inline int btrfs_get_root_last_log_commit(const struct btrfs_root *root)
349 {
350 	return READ_ONCE(root->last_log_commit);
351 }
352 
btrfs_set_root_last_log_commit(struct btrfs_root * root,int commit_id)353 static inline void btrfs_set_root_last_log_commit(struct btrfs_root *root, int commit_id)
354 {
355 	WRITE_ONCE(root->last_log_commit, commit_id);
356 }
357 
btrfs_get_root_last_trans(const struct btrfs_root * root)358 static inline u64 btrfs_get_root_last_trans(const struct btrfs_root *root)
359 {
360 	return READ_ONCE(root->last_trans);
361 }
362 
btrfs_set_root_last_trans(struct btrfs_root * root,u64 transid)363 static inline void btrfs_set_root_last_trans(struct btrfs_root *root, u64 transid)
364 {
365 	WRITE_ONCE(root->last_trans, transid);
366 }
367 
368 /*
369  * Return the generation this root started with.
370  *
371  * Every normal root that is created with root->root_key.offset set to it's
372  * originating generation.  If it is a snapshot it is the generation when the
373  * snapshot was created.
374  *
375  * However for TREE_RELOC roots root_key.offset is the objectid of the owning
376  * tree root.  Thankfully we copy the root item of the owning tree root, which
377  * has it's last_snapshot set to what we would have root_key.offset set to, so
378  * return that if this is a TREE_RELOC root.
379  */
btrfs_root_origin_generation(const struct btrfs_root * root)380 static inline u64 btrfs_root_origin_generation(const struct btrfs_root *root)
381 {
382 	if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID)
383 		return btrfs_root_last_snapshot(&root->root_item);
384 	return root->root_key.offset;
385 }
386 
387 /*
388  * Structure that conveys information about an extent that is going to replace
389  * all the extents in a file range.
390  */
391 struct btrfs_replace_extent_info {
392 	u64 disk_offset;
393 	u64 disk_len;
394 	u64 data_offset;
395 	u64 data_len;
396 	u64 file_offset;
397 	/* Pointer to a file extent item of type regular or prealloc. */
398 	char *extent_buf;
399 	/*
400 	 * Set to true when attempting to replace a file range with a new extent
401 	 * described by this structure, set to false when attempting to clone an
402 	 * existing extent into a file range.
403 	 */
404 	bool is_new_extent;
405 	/* Indicate if we should update the inode's mtime and ctime. */
406 	bool update_times;
407 	/* Meaningful only if is_new_extent is true. */
408 	int qgroup_reserved;
409 	/*
410 	 * Meaningful only if is_new_extent is true.
411 	 * Used to track how many extent items we have already inserted in a
412 	 * subvolume tree that refer to the extent described by this structure,
413 	 * so that we know when to create a new delayed ref or update an existing
414 	 * one.
415 	 */
416 	int insertions;
417 };
418 
419 /* Arguments for btrfs_drop_extents() */
420 struct btrfs_drop_extents_args {
421 	/* Input parameters */
422 
423 	/*
424 	 * If NULL, btrfs_drop_extents() will allocate and free its own path.
425 	 * If 'replace_extent' is true, this must not be NULL. Also the path
426 	 * is always released except if 'replace_extent' is true and
427 	 * btrfs_drop_extents() sets 'extent_inserted' to true, in which case
428 	 * the path is kept locked.
429 	 */
430 	struct btrfs_path *path;
431 	/* Start offset of the range to drop extents from */
432 	u64 start;
433 	/* End (exclusive, last byte + 1) of the range to drop extents from */
434 	u64 end;
435 	/* If true drop all the extent maps in the range */
436 	bool drop_cache;
437 	/*
438 	 * If true it means we want to insert a new extent after dropping all
439 	 * the extents in the range. If this is true, the 'extent_item_size'
440 	 * parameter must be set as well and the 'extent_inserted' field will
441 	 * be set to true by btrfs_drop_extents() if it could insert the new
442 	 * extent.
443 	 * Note: when this is set to true the path must not be NULL.
444 	 */
445 	bool replace_extent;
446 	/*
447 	 * Used if 'replace_extent' is true. Size of the file extent item to
448 	 * insert after dropping all existing extents in the range
449 	 */
450 	u32 extent_item_size;
451 
452 	/* Output parameters */
453 
454 	/*
455 	 * Set to the minimum between the input parameter 'end' and the end
456 	 * (exclusive, last byte + 1) of the last dropped extent. This is always
457 	 * set even if btrfs_drop_extents() returns an error.
458 	 */
459 	u64 drop_end;
460 	/*
461 	 * The number of allocated bytes found in the range. This can be smaller
462 	 * than the range's length when there are holes in the range.
463 	 */
464 	u64 bytes_found;
465 	/*
466 	 * Only set if 'replace_extent' is true. Set to true if we were able
467 	 * to insert a replacement extent after dropping all extents in the
468 	 * range, otherwise set to false by btrfs_drop_extents().
469 	 * Also, if btrfs_drop_extents() has set this to true it means it
470 	 * returned with the path locked, otherwise if it has set this to
471 	 * false it has returned with the path released.
472 	 */
473 	bool extent_inserted;
474 };
475 
476 struct btrfs_file_private {
477 	void *filldir_buf;
478 	u64 last_index;
479 	struct extent_state *llseek_cached_state;
480 	/* Task that allocated this structure. */
481 	struct task_struct *owner_task;
482 };
483 
BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info * info)484 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
485 {
486 	return info->nodesize - sizeof(struct btrfs_header);
487 }
488 
BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info * info)489 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
490 {
491 	return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
492 }
493 
BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info * info)494 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
495 {
496 	return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
497 }
498 
BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info * info)499 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
500 {
501 	return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
502 }
503 
504 int __init btrfs_ctree_init(void);
505 void __cold btrfs_ctree_exit(void);
506 
507 int btrfs_bin_search(const struct extent_buffer *eb, int first_slot,
508 		     const struct btrfs_key *key, int *slot);
509 
510 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
511 
512 #ifdef __LITTLE_ENDIAN
513 
514 /*
515  * Compare two keys, on little-endian the disk order is same as CPU order and
516  * we can avoid the conversion.
517  */
btrfs_comp_keys(const struct btrfs_disk_key * disk_key,const struct btrfs_key * k2)518 static inline int btrfs_comp_keys(const struct btrfs_disk_key *disk_key,
519 				  const struct btrfs_key *k2)
520 {
521 	const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
522 
523 	return btrfs_comp_cpu_keys(k1, k2);
524 }
525 
526 #else
527 
528 /* Compare two keys in a memcmp fashion. */
btrfs_comp_keys(const struct btrfs_disk_key * disk,const struct btrfs_key * k2)529 static inline int btrfs_comp_keys(const struct btrfs_disk_key *disk,
530 				  const struct btrfs_key *k2)
531 {
532 	struct btrfs_key k1;
533 
534 	btrfs_disk_key_to_cpu(&k1, disk);
535 
536 	return btrfs_comp_cpu_keys(&k1, k2);
537 }
538 
539 #endif
540 
541 int btrfs_previous_item(struct btrfs_root *root,
542 			struct btrfs_path *path, u64 min_objectid,
543 			int type);
544 int btrfs_previous_extent_item(struct btrfs_root *root,
545 			struct btrfs_path *path, u64 min_objectid);
546 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
547 			     const struct btrfs_path *path,
548 			     const struct btrfs_key *new_key);
549 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
550 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
551 			struct btrfs_key *key, int lowest_level,
552 			u64 min_trans);
553 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
554 			 struct btrfs_path *path,
555 			 u64 min_trans);
556 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
557 					   int slot);
558 
559 int btrfs_cow_block(struct btrfs_trans_handle *trans,
560 		    struct btrfs_root *root, struct extent_buffer *buf,
561 		    struct extent_buffer *parent, int parent_slot,
562 		    struct extent_buffer **cow_ret,
563 		    enum btrfs_lock_nesting nest);
564 int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
565 			  struct btrfs_root *root,
566 			  struct extent_buffer *buf,
567 			  struct extent_buffer *parent, int parent_slot,
568 			  struct extent_buffer **cow_ret,
569 			  u64 search_start, u64 empty_size,
570 			  enum btrfs_lock_nesting nest);
571 int btrfs_copy_root(struct btrfs_trans_handle *trans,
572 		      struct btrfs_root *root,
573 		      struct extent_buffer *buf,
574 		      struct extent_buffer **cow_ret, u64 new_root_objectid);
575 bool btrfs_block_can_be_shared(const struct btrfs_trans_handle *trans,
576 			       const struct btrfs_root *root,
577 			       const struct extent_buffer *buf);
578 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
579 		  struct btrfs_path *path, int level, int slot);
580 void btrfs_extend_item(struct btrfs_trans_handle *trans,
581 		       const struct btrfs_path *path, u32 data_size);
582 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
583 			 const struct btrfs_path *path, u32 new_size, int from_end);
584 int btrfs_split_item(struct btrfs_trans_handle *trans,
585 		     struct btrfs_root *root,
586 		     struct btrfs_path *path,
587 		     const struct btrfs_key *new_key,
588 		     unsigned long split_offset);
589 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
590 			 struct btrfs_root *root,
591 			 struct btrfs_path *path,
592 			 const struct btrfs_key *new_key);
593 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
594 		u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
595 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
596 		      const struct btrfs_key *key, struct btrfs_path *p,
597 		      int ins_len, int cow);
598 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
599 			  struct btrfs_path *p, u64 time_seq);
600 int btrfs_search_slot_for_read(struct btrfs_root *root,
601 			       const struct btrfs_key *key,
602 			       struct btrfs_path *p, int find_higher,
603 			       int return_any);
604 void btrfs_release_path(struct btrfs_path *p);
605 struct btrfs_path *btrfs_alloc_path(void);
606 void btrfs_free_path(struct btrfs_path *p);
607 DEFINE_FREE(btrfs_free_path, struct btrfs_path *, btrfs_free_path(_T))
608 DEFINE_FREE(btrfs_release_path, struct btrfs_path, btrfs_release_path(&_T))
609 
610 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
611 		   struct btrfs_path *path, int slot, int nr);
btrfs_del_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path)612 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
613 				 struct btrfs_root *root,
614 				 struct btrfs_path *path)
615 {
616 	return btrfs_del_items(trans, root, path, path->slots[0], 1);
617 }
618 
619 /*
620  * Describes a batch of items to insert in a btree. This is used by
621  * btrfs_insert_empty_items().
622  */
623 struct btrfs_item_batch {
624 	/*
625 	 * Pointer to an array containing the keys of the items to insert (in
626 	 * sorted order).
627 	 */
628 	const struct btrfs_key *keys;
629 	/* Pointer to an array containing the data size for each item to insert. */
630 	const u32 *data_sizes;
631 	/*
632 	 * The sum of data sizes for all items. The caller can compute this while
633 	 * setting up the data_sizes array, so it ends up being more efficient
634 	 * than having btrfs_insert_empty_items() or setup_item_for_insert()
635 	 * doing it, as it would avoid an extra loop over a potentially large
636 	 * array, and in the case of setup_item_for_insert(), we would be doing
637 	 * it while holding a write lock on a leaf and often on upper level nodes
638 	 * too, unnecessarily increasing the size of a critical section.
639 	 */
640 	u32 total_data_size;
641 	/* Size of the keys and data_sizes arrays (number of items in the batch). */
642 	int nr;
643 };
644 
645 void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
646 				 struct btrfs_root *root,
647 				 struct btrfs_path *path,
648 				 const struct btrfs_key *key,
649 				 u32 data_size);
650 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
651 		      const struct btrfs_key *key, void *data, u32 data_size);
652 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
653 			     struct btrfs_root *root,
654 			     struct btrfs_path *path,
655 			     const struct btrfs_item_batch *batch);
656 
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)657 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
658 					  struct btrfs_root *root,
659 					  struct btrfs_path *path,
660 					  const struct btrfs_key *key,
661 					  u32 data_size)
662 {
663 	struct btrfs_item_batch batch;
664 
665 	batch.keys = key;
666 	batch.data_sizes = &data_size;
667 	batch.total_data_size = data_size;
668 	batch.nr = 1;
669 
670 	return btrfs_insert_empty_items(trans, root, path, &batch);
671 }
672 
673 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
674 			u64 time_seq);
675 
676 int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
677 			   struct btrfs_path *path);
678 
679 int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
680 			      struct btrfs_path *path);
681 
682 /*
683  * Search in @root for a given @key, and store the slot found in @found_key.
684  *
685  * @root:	The root node of the tree.
686  * @key:	The key we are looking for.
687  * @found_key:	Will hold the found item.
688  * @path:	Holds the current slot/leaf.
689  * @iter_ret:	Contains the value returned from btrfs_search_slot or
690  * 		btrfs_get_next_valid_item, whichever was executed last.
691  *
692  * The @iter_ret is an output variable that will contain the return value of
693  * btrfs_search_slot, if it encountered an error, or the value returned from
694  * btrfs_get_next_valid_item otherwise. That return value can be 0, if a valid
695  * slot was found, 1 if there were no more leaves, and <0 if there was an error.
696  *
697  * It's recommended to use a separate variable for iter_ret and then use it to
698  * set the function return value so there's no confusion of the 0/1/errno
699  * values stemming from btrfs_search_slot.
700  */
701 #define btrfs_for_each_slot(root, key, found_key, path, iter_ret)		\
702 	for (iter_ret = btrfs_search_slot(NULL, (root), (key), (path), 0, 0);	\
703 		(iter_ret) >= 0 &&						\
704 		(iter_ret = btrfs_get_next_valid_item((root), (found_key), (path))) == 0; \
705 		(path)->slots[0]++						\
706 	)
707 
708 int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq);
709 
710 /*
711  * Search the tree again to find a leaf with greater keys.
712  *
713  * Returns 0 if it found something or 1 if there are no greater leaves.
714  * Returns < 0 on error.
715  */
btrfs_next_leaf(struct btrfs_root * root,struct btrfs_path * path)716 static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
717 {
718 	return btrfs_next_old_leaf(root, path, 0);
719 }
720 
btrfs_next_item(struct btrfs_root * root,struct btrfs_path * p)721 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
722 {
723 	return btrfs_next_old_item(root, p, 0);
724 }
725 int btrfs_leaf_free_space(const struct extent_buffer *leaf);
726 
btrfs_is_fstree(u64 rootid)727 static inline bool btrfs_is_fstree(u64 rootid)
728 {
729 	if (rootid == BTRFS_FS_TREE_OBJECTID)
730 		return true;
731 
732 	if ((s64)rootid < (s64)BTRFS_FIRST_FREE_OBJECTID)
733 		return false;
734 
735 	if (btrfs_qgroup_level(rootid) != 0)
736 		return false;
737 
738 	return true;
739 }
740 
btrfs_is_data_reloc_root(const struct btrfs_root * root)741 static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root)
742 {
743 	return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID;
744 }
745 
746 #endif
747