]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/btrfs/ctree.h
btrfs: scan but don't register device on single device filesystem
[thirdparty/linux.git] / fs / btrfs / ctree.h
CommitLineData
9888c340 1/* SPDX-License-Identifier: GPL-2.0 */
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
9888c340
DS
6#ifndef BTRFS_CTREE_H
7#define BTRFS_CTREE_H
eb60ceac 8
3b16a4e3 9#include <linux/pagemap.h>
2992df73 10#include "locking.h"
a56159d4 11#include "fs.h"
e20d96d6 12
e089f05c 13struct btrfs_trans_handle;
79154b1b 14struct btrfs_transaction;
a22285a6 15struct btrfs_pending_snapshot;
31890da0 16struct btrfs_delayed_ref_root;
8719aaae 17struct btrfs_space_info;
32da5386 18struct btrfs_block_group;
e6dcd2dc 19struct btrfs_ordered_sum;
82fa113f 20struct btrfs_ref;
c3a3b19b 21struct btrfs_bio;
1881fba8 22struct btrfs_ioctl_encoded_io_args;
0e75f005
JB
23struct btrfs_device;
24struct btrfs_fs_devices;
25struct btrfs_balance_control;
26struct btrfs_delayed_root;
27struct reloc_control;
e089f05c 28
ace75066
FM
29/* Read ahead values for struct btrfs_path.reada */
30enum {
31 READA_NONE,
32 READA_BACK,
33 READA_FORWARD,
34 /*
35 * Similar to READA_FORWARD but unlike it:
36 *
37 * 1) It will trigger readahead even for leaves that are not close to
38 * each other on disk;
39 * 2) It also triggers readahead for nodes;
40 * 3) During a search, even when a node or leaf is already in memory, it
41 * will still trigger readahead for other nodes and leaves that follow
42 * it.
43 *
44 * This is meant to be used only when we know we are iterating over the
45 * entire tree or a very large part of it.
46 */
47 READA_FORWARD_ALWAYS,
48};
49
fec577fb 50/*
234b63a0
CM
51 * btrfs_paths remember the path taken from the root down to the leaf.
52 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
fec577fb
CM
53 * to any other levels that are present.
54 *
55 * The slots array records the index of the item or block pointer
56 * used while walking the tree.
57 */
234b63a0 58struct btrfs_path {
5f39d397 59 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
234b63a0 60 int slots[BTRFS_MAX_LEVEL];
925baedd 61 /* if there is real range locking, this locks field will change */
4fb72bf2 62 u8 locks[BTRFS_MAX_LEVEL];
dccabfad 63 u8 reada;
925baedd 64 /* keep some upper locks as we walk down */
7853f15b 65 u8 lowest_level;
459931ec
CM
66
67 /*
68 * set by btrfs_split_item, tells search_slot to keep all locks
69 * and to force calls to keep space in the nodes
70 */
b9473439
CM
71 unsigned int search_for_split:1;
72 unsigned int keep_locks:1;
73 unsigned int skip_locking:1;
5d4f98a2 74 unsigned int search_commit_root:1;
3f8a18cc 75 unsigned int need_commit_sem:1;
5f5bc6b1 76 unsigned int skip_release_on_error:1;
9a664971 77 /*
78 * Indicate that new item (btrfs_search_slot) is extending already
79 * existing item and ins_len contains only the data size and not item
80 * header (ie. sizeof(struct btrfs_item) is not included).
81 */
82 unsigned int search_for_extension:1;
857bc13f
JB
83 /* Stop search if any locks need to be taken (for read) */
84 unsigned int nowait:1;
eb60ceac 85};
d9d88fde 86
27cdeb70
MX
87/*
88 * The state of btrfs root
89 */
61fa90c1
DS
90enum {
91 /*
92 * btrfs_record_root_in_trans is a multi-step process, and it can race
93 * with the balancing code. But the race is very small, and only the
94 * first time the root is added to each transaction. So IN_TRANS_SETUP
95 * is used to tell us when more checks are required
96 */
97 BTRFS_ROOT_IN_TRANS_SETUP,
92a7cc42
QW
98
99 /*
100 * Set if tree blocks of this root can be shared by other roots.
101 * Only subvolume trees and their reloc trees have this bit set.
102 * Conflicts with TRACK_DIRTY bit.
103 *
104 * This affects two things:
105 *
106 * - How balance works
107 * For shareable roots, we need to use reloc tree and do path
108 * replacement for balance, and need various pre/post hooks for
109 * snapshot creation to handle them.
110 *
111 * While for non-shareable trees, we just simply do a tree search
112 * with COW.
113 *
114 * - How dirty roots are tracked
115 * For shareable roots, btrfs_record_root_in_trans() is needed to
116 * track them, while non-subvolume roots have TRACK_DIRTY bit, they
117 * don't need to set this manually.
118 */
119 BTRFS_ROOT_SHAREABLE,
61fa90c1 120 BTRFS_ROOT_TRACK_DIRTY,
fc7cbcd4 121 BTRFS_ROOT_IN_RADIX,
61fa90c1
DS
122 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
123 BTRFS_ROOT_DEFRAG_RUNNING,
124 BTRFS_ROOT_FORCE_COW,
125 BTRFS_ROOT_MULTI_LOG_TASKS,
126 BTRFS_ROOT_DIRTY,
83354f07 127 BTRFS_ROOT_DELETING,
d2311e69
QW
128
129 /*
130 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
131 *
132 * Set for the subvolume tree owning the reloc tree.
133 */
134 BTRFS_ROOT_DEAD_RELOC_TREE,
78c52d9e
JB
135 /* Mark dead root stored on device whose cleanup needs to be resumed */
136 BTRFS_ROOT_DEAD_TREE,
47876f7c 137 /* The root has a log tree. Used for subvolume roots and the tree root. */
e7a79811 138 BTRFS_ROOT_HAS_LOG_TREE,
c53e9653
QW
139 /* Qgroup flushing is in progress */
140 BTRFS_ROOT_QGROUP_FLUSHING,
54230013
JB
141 /* We started the orphan cleanup for this root. */
142 BTRFS_ROOT_ORPHAN_CLEANUP,
b4be6aef
JB
143 /* This root has a drop operation that was started previously. */
144 BTRFS_ROOT_UNFINISHED_DROP,
b40130b2
JB
145 /* This reloc root needs to have its buffers lockdep class reset. */
146 BTRFS_ROOT_RESET_LOCKDEP_CLASS,
61fa90c1 147};
27cdeb70 148
370a11b8
QW
149/*
150 * Record swapped tree blocks of a subvolume tree for delayed subtree trace
151 * code. For detail check comment in fs/btrfs/qgroup.c.
152 */
153struct btrfs_qgroup_swapped_blocks {
154 spinlock_t lock;
155 /* RM_EMPTY_ROOT() of above blocks[] */
156 bool swapped;
157 struct rb_root blocks[BTRFS_MAX_LEVEL];
158};
159
9f5fae2f
CM
160/*
161 * in ram representation of the tree. extent_root is used for all allocations
f2458e1d 162 * and for the extent tree extent_root root.
9f5fae2f
CM
163 */
164struct btrfs_root {
abed4aaa
JB
165 struct rb_node rb_node;
166
5f39d397 167 struct extent_buffer *node;
925baedd 168
5f39d397 169 struct extent_buffer *commit_root;
e02119d5 170 struct btrfs_root *log_root;
1a40e23b 171 struct btrfs_root *reloc_root;
31153d81 172
27cdeb70 173 unsigned long state;
62e2749e
CM
174 struct btrfs_root_item root_item;
175 struct btrfs_key root_key;
9f5fae2f 176 struct btrfs_fs_info *fs_info;
d0c803c4
CM
177 struct extent_io_tree dirty_log_pages;
178
a2135011 179 struct mutex objectid_mutex;
7237f183 180
f0486c68
YZ
181 spinlock_t accounting_lock;
182 struct btrfs_block_rsv *block_rsv;
183
e02119d5 184 struct mutex log_mutex;
7237f183
YZ
185 wait_queue_head_t log_writer_wait;
186 wait_queue_head_t log_commit_wait[2];
8b050d35 187 struct list_head log_ctxs[2];
a93e0168 188 /* Used only for log trees of subvolumes, not for the log root tree */
7237f183
YZ
189 atomic_t log_writers;
190 atomic_t log_commit[2];
28a95795 191 /* Used only for log trees of subvolumes, not for the log root tree */
2ecb7923 192 atomic_t log_batch;
bb14a59b 193 int log_transid;
d1433deb
MX
194 /* No matter the commit succeeds or not*/
195 int log_transid_committed;
196 /* Just be updated when the commit succeeds. */
bb14a59b 197 int last_log_commit;
ff782e0a 198 pid_t log_start_pid;
ea8c2819 199
0f7d52f4 200 u64 last_trans;
5f39d397 201
9f5fae2f 202 u32 type;
13a8a7c8 203
6b8fad57 204 u64 free_objectid;
7585717f 205
6702ed49 206 struct btrfs_key defrag_progress;
0ef3e66b 207 struct btrfs_key defrag_max;
0b86a832 208
92a7cc42 209 /* The dirty list is only used by non-shareable roots */
0b86a832 210 struct list_head dirty_list;
7b128766 211
5d4f98a2
YZ
212 struct list_head root_list;
213
2ab28f32
JB
214 spinlock_t log_extents_lock[2];
215 struct list_head logged_list[2];
216
5d4f98a2
YZ
217 spinlock_t inode_lock;
218 /* red-black tree that keeps track of in-memory inodes */
219 struct rb_root inode_tree;
220
16cdcec7 221 /*
088aea3b
DS
222 * radix tree that keeps track of delayed nodes of every inode,
223 * protected by inode_lock
16cdcec7 224 */
088aea3b 225 struct radix_tree_root delayed_nodes_tree;
3394e160
CM
226 /*
227 * right now this just gets used so that a root has its own devid
228 * for stat. It may be used for more later
229 */
0ee5dc67 230 dev_t anon_dev;
f1ebcc74 231
5f3ab90a 232 spinlock_t root_item_lock;
0700cea7 233 refcount_t refs;
eb73c1b7 234
573bfb72 235 struct mutex delalloc_mutex;
eb73c1b7
MX
236 spinlock_t delalloc_lock;
237 /*
238 * all of the inodes that have delalloc bytes. It is possible for
239 * this list to be empty even when there is still dirty data=ordered
240 * extents waiting to finish IO.
241 */
242 struct list_head delalloc_inodes;
243 struct list_head delalloc_root;
244 u64 nr_delalloc_inodes;
31f3d255
MX
245
246 struct mutex ordered_extent_mutex;
199c2a9c
MX
247 /*
248 * this is used by the balancing code to wait for all the pending
249 * ordered extents
250 */
251 spinlock_t ordered_extent_lock;
252
253 /*
254 * all of the data=ordered extents pending writeback
255 * these can span multiple transactions and basically include
256 * every dirty data page that isn't from nodatacow
257 */
258 struct list_head ordered_extents;
259 struct list_head ordered_root;
260 u64 nr_ordered_extents;
2c686537 261
d2311e69
QW
262 /*
263 * Not empty if this subvolume root has gone through tree block swap
264 * (relocation)
265 *
266 * Will be used by reloc_control::dirty_subvol_roots.
267 */
268 struct list_head reloc_dirty_list;
269
2c686537
DS
270 /*
271 * Number of currently running SEND ioctls to prevent
272 * manipulation with the read-only status via SUBVOL_SETFLAGS
273 */
274 int send_in_progress;
62d54f3a
FM
275 /*
276 * Number of currently running deduplication operations that have a
277 * destination inode belonging to this root. Protected by the lock
278 * root_item_lock.
279 */
280 int dedupe_in_progress;
dcc3eb96
NB
281 /* For exclusion of snapshot creation and nocow writes */
282 struct btrfs_drew_lock snapshot_lock;
283
8ecebf4d 284 atomic_t snapshot_force_cow;
8287475a
QW
285
286 /* For qgroup metadata reserved space */
287 spinlock_t qgroup_meta_rsv_lock;
288 u64 qgroup_meta_rsv_pertrans;
289 u64 qgroup_meta_rsv_prealloc;
c53e9653 290 wait_queue_head_t qgroup_flush_wait;
57ec5fb4 291
eede2bf3
OS
292 /* Number of active swapfiles */
293 atomic_t nr_swapfiles;
294
370a11b8
QW
295 /* Record pairs of swapped blocks for qgroup */
296 struct btrfs_qgroup_swapped_blocks swapped_blocks;
297
e289f03e
FM
298 /* Used only by log trees, when logging csum items */
299 struct extent_io_tree log_csum_range;
300
57ec5fb4
DS
301#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
302 u64 alloc_bytenr;
303#endif
bd647ce3
JB
304
305#ifdef CONFIG_BTRFS_DEBUG
306 struct list_head leak_list;
307#endif
62e2749e 308};
118c701e 309
1fe5ebc4
JB
310static inline bool btrfs_root_readonly(const struct btrfs_root *root)
311{
312 /* Byte-swap the constant at compile time, root_item::flags is LE */
313 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
314}
315
316static inline bool btrfs_root_dead(const struct btrfs_root *root)
317{
318 /* Byte-swap the constant at compile time, root_item::flags is LE */
319 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
320}
321
322static inline u64 btrfs_root_id(const struct btrfs_root *root)
323{
324 return root->root_key.objectid;
325}
326
bf385648
FM
327/*
328 * Structure that conveys information about an extent that is going to replace
329 * all the extents in a file range.
330 */
331struct btrfs_replace_extent_info {
690a5dbf
FM
332 u64 disk_offset;
333 u64 disk_len;
334 u64 data_offset;
335 u64 data_len;
336 u64 file_offset;
fb870f6c 337 /* Pointer to a file extent item of type regular or prealloc. */
690a5dbf 338 char *extent_buf;
8fccebfa
FM
339 /*
340 * Set to true when attempting to replace a file range with a new extent
341 * described by this structure, set to false when attempting to clone an
342 * existing extent into a file range.
343 */
344 bool is_new_extent;
983d8209
FM
345 /* Indicate if we should update the inode's mtime and ctime. */
346 bool update_times;
8fccebfa
FM
347 /* Meaningful only if is_new_extent is true. */
348 int qgroup_reserved;
349 /*
350 * Meaningful only if is_new_extent is true.
351 * Used to track how many extent items we have already inserted in a
352 * subvolume tree that refer to the extent described by this structure,
353 * so that we know when to create a new delayed ref or update an existing
354 * one.
355 */
356 int insertions;
690a5dbf
FM
357};
358
5893dfb9
FM
359/* Arguments for btrfs_drop_extents() */
360struct btrfs_drop_extents_args {
361 /* Input parameters */
362
363 /*
364 * If NULL, btrfs_drop_extents() will allocate and free its own path.
365 * If 'replace_extent' is true, this must not be NULL. Also the path
366 * is always released except if 'replace_extent' is true and
367 * btrfs_drop_extents() sets 'extent_inserted' to true, in which case
368 * the path is kept locked.
369 */
370 struct btrfs_path *path;
371 /* Start offset of the range to drop extents from */
372 u64 start;
373 /* End (exclusive, last byte + 1) of the range to drop extents from */
374 u64 end;
375 /* If true drop all the extent maps in the range */
376 bool drop_cache;
377 /*
378 * If true it means we want to insert a new extent after dropping all
379 * the extents in the range. If this is true, the 'extent_item_size'
380 * parameter must be set as well and the 'extent_inserted' field will
381 * be set to true by btrfs_drop_extents() if it could insert the new
382 * extent.
383 * Note: when this is set to true the path must not be NULL.
384 */
385 bool replace_extent;
386 /*
387 * Used if 'replace_extent' is true. Size of the file extent item to
388 * insert after dropping all existing extents in the range
389 */
390 u32 extent_item_size;
391
392 /* Output parameters */
393
394 /*
395 * Set to the minimum between the input parameter 'end' and the end
396 * (exclusive, last byte + 1) of the last dropped extent. This is always
397 * set even if btrfs_drop_extents() returns an error.
398 */
399 u64 drop_end;
2766ff61
FM
400 /*
401 * The number of allocated bytes found in the range. This can be smaller
402 * than the range's length when there are holes in the range.
403 */
404 u64 bytes_found;
5893dfb9
FM
405 /*
406 * Only set if 'replace_extent' is true. Set to true if we were able
407 * to insert a replacement extent after dropping all extents in the
408 * range, otherwise set to false by btrfs_drop_extents().
409 * Also, if btrfs_drop_extents() has set this to true it means it
410 * returned with the path locked, otherwise if it has set this to
411 * false it has returned with the path released.
412 */
413 bool extent_inserted;
414};
415
23b5ec74 416struct btrfs_file_private {
23b5ec74 417 void *filldir_buf;
9b378f6a 418 u64 last_index;
3c32c721 419 struct extent_state *llseek_cached_state;
23b5ec74
JB
420};
421
da17066c 422static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1db1ff92 423{
118c701e 424 return info->nodesize - sizeof(struct btrfs_header);
1db1ff92
JM
425}
426
da17066c 427static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1db1ff92 428{
da17066c 429 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1db1ff92
JM
430}
431
da17066c 432static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1db1ff92 433{
da17066c 434 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1db1ff92
JM
435}
436
da17066c 437static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1db1ff92 438{
da17066c 439 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1db1ff92
JM
440}
441
2e78c927 442#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
265fdfa6 443 ((bytes) >> (fs_info)->sectorsize_bits)
2e78c927 444
3b16a4e3
JB
445static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
446{
c62d2555 447 return mapping_gfp_constraint(mapping, ~__GFP_FS);
3b16a4e3
JB
448}
449
2ff7e61e 450int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
acce952b 451 u64 start, u64 end);
2ff7e61e 452int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
1edb647b 453 u64 num_bytes, u64 *actual_bytes);
2ff7e61e 454int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
acce952b 455
dee26a9f 456/* ctree.c */
226463d7
JB
457int __init btrfs_ctree_init(void);
458void __cold btrfs_ctree_exit(void);
7b00dfff 459
fdf8d595
AJ
460int btrfs_bin_search(struct extent_buffer *eb, int first_slot,
461 const struct btrfs_key *key, int *slot);
7b00dfff 462
e1f60a65 463int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
0b86a832
CM
464int btrfs_previous_item(struct btrfs_root *root,
465 struct btrfs_path *path, u64 min_objectid,
466 int type);
ade2e0b3
WS
467int btrfs_previous_extent_item(struct btrfs_root *root,
468 struct btrfs_path *path, u64 min_objectid);
b7a0365e
DD
469void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
470 struct btrfs_path *path,
310712b2 471 const struct btrfs_key *new_key);
925baedd 472struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
e7a84565 473int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3f157a2f 474 struct btrfs_key *key, int lowest_level,
de78b51a 475 u64 min_trans);
3f157a2f 476int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
de78b51a 477 struct btrfs_path *path,
3f157a2f 478 u64 min_trans);
4b231ae4
DS
479struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
480 int slot);
481
5f39d397
CM
482int btrfs_cow_block(struct btrfs_trans_handle *trans,
483 struct btrfs_root *root, struct extent_buffer *buf,
484 struct extent_buffer *parent, int parent_slot,
9631e4cc
JB
485 struct extent_buffer **cow_ret,
486 enum btrfs_lock_nesting nest);
be20aa9d
CM
487int btrfs_copy_root(struct btrfs_trans_handle *trans,
488 struct btrfs_root *root,
489 struct extent_buffer *buf,
490 struct extent_buffer **cow_ret, u64 new_root_objectid);
5d4f98a2
YZ
491int btrfs_block_can_be_shared(struct btrfs_root *root,
492 struct extent_buffer *buf);
751a2761
FM
493int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
494 struct btrfs_path *path, int level, int slot);
c71dd880 495void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
78ac4f9e 496void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
459931ec
CM
497int btrfs_split_item(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root,
499 struct btrfs_path *path,
310712b2 500 const struct btrfs_key *new_key,
459931ec 501 unsigned long split_offset);
ad48fd75
YZ
502int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
503 struct btrfs_root *root,
504 struct btrfs_path *path,
310712b2 505 const struct btrfs_key *new_key);
e33d5c3d
KN
506int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
507 u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
310712b2
OS
508int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
509 const struct btrfs_key *key, struct btrfs_path *p,
510 int ins_len, int cow);
511int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
5d9e75c4 512 struct btrfs_path *p, u64 time_seq);
2f38b3e1 513int btrfs_search_slot_for_read(struct btrfs_root *root,
310712b2
OS
514 const struct btrfs_key *key,
515 struct btrfs_path *p, int find_higher,
516 int return_any);
6702ed49 517int btrfs_realloc_node(struct btrfs_trans_handle *trans,
5f39d397 518 struct btrfs_root *root, struct extent_buffer *parent,
de78b51a 519 int start_slot, u64 *last_ret,
a6b6e75e 520 struct btrfs_key *progress);
b3b4aa74 521void btrfs_release_path(struct btrfs_path *p);
2c90e5d6
CM
522struct btrfs_path *btrfs_alloc_path(void);
523void btrfs_free_path(struct btrfs_path *p);
b4ce94de 524
85e21bac
CM
525int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
526 struct btrfs_path *path, int slot, int nr);
85e21bac
CM
527static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
528 struct btrfs_root *root,
529 struct btrfs_path *path)
530{
531 return btrfs_del_items(trans, root, path, path->slots[0], 1);
532}
533
b7ef5f3a
FM
534/*
535 * Describes a batch of items to insert in a btree. This is used by
f0641656 536 * btrfs_insert_empty_items().
b7ef5f3a
FM
537 */
538struct btrfs_item_batch {
539 /*
540 * Pointer to an array containing the keys of the items to insert (in
541 * sorted order).
542 */
543 const struct btrfs_key *keys;
544 /* Pointer to an array containing the data size for each item to insert. */
545 const u32 *data_sizes;
546 /*
547 * The sum of data sizes for all items. The caller can compute this while
548 * setting up the data_sizes array, so it ends up being more efficient
549 * than having btrfs_insert_empty_items() or setup_item_for_insert()
550 * doing it, as it would avoid an extra loop over a potentially large
551 * array, and in the case of setup_item_for_insert(), we would be doing
552 * it while holding a write lock on a leaf and often on upper level nodes
553 * too, unnecessarily increasing the size of a critical section.
554 */
555 u32 total_data_size;
556 /* Size of the keys and data_sizes arrays (number of items in the batch). */
557 int nr;
558};
559
f0641656
FM
560void btrfs_setup_item_for_insert(struct btrfs_root *root,
561 struct btrfs_path *path,
562 const struct btrfs_key *key,
563 u32 data_size);
310712b2
OS
564int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
565 const struct btrfs_key *key, void *data, u32 data_size);
9c58309d
CM
566int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
568 struct btrfs_path *path,
b7ef5f3a 569 const struct btrfs_item_batch *batch);
9c58309d
CM
570
571static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root,
573 struct btrfs_path *path,
310712b2 574 const struct btrfs_key *key,
9c58309d
CM
575 u32 data_size)
576{
b7ef5f3a
FM
577 struct btrfs_item_batch batch;
578
579 batch.keys = key;
580 batch.data_sizes = &data_size;
581 batch.total_data_size = data_size;
582 batch.nr = 1;
583
584 return btrfs_insert_empty_items(trans, root, path, &batch);
9c58309d
CM
585}
586
3d7806ec
JS
587int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
588 u64 time_seq);
0ff40a91
MPS
589
590int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
591 struct btrfs_path *path);
592
62142be3
GN
593int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
594 struct btrfs_path *path);
595
596/*
597 * Search in @root for a given @key, and store the slot found in @found_key.
598 *
599 * @root: The root node of the tree.
600 * @key: The key we are looking for.
601 * @found_key: Will hold the found item.
602 * @path: Holds the current slot/leaf.
603 * @iter_ret: Contains the value returned from btrfs_search_slot or
604 * btrfs_get_next_valid_item, whichever was executed last.
605 *
606 * The @iter_ret is an output variable that will contain the return value of
607 * btrfs_search_slot, if it encountered an error, or the value returned from
608 * btrfs_get_next_valid_item otherwise. That return value can be 0, if a valid
609 * slot was found, 1 if there were no more leaves, and <0 if there was an error.
610 *
611 * It's recommended to use a separate variable for iter_ret and then use it to
612 * set the function return value so there's no confusion of the 0/1/errno
613 * values stemming from btrfs_search_slot.
614 */
615#define btrfs_for_each_slot(root, key, found_key, path, iter_ret) \
616 for (iter_ret = btrfs_search_slot(NULL, (root), (key), (path), 0, 0); \
617 (iter_ret) >= 0 && \
618 (iter_ret = btrfs_get_next_valid_item((root), (found_key), (path))) == 0; \
619 (path)->slots[0]++ \
620 )
621
890d2b1a 622int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq);
809d6902
DS
623
624/*
625 * Search the tree again to find a leaf with greater keys.
626 *
627 * Returns 0 if it found something or 1 if there are no greater leaves.
628 * Returns < 0 on error.
629 */
630static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
631{
632 return btrfs_next_old_leaf(root, path, 0);
633}
634
1c8f52a5
AB
635static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
636{
637 return btrfs_next_old_item(root, p, 0);
638}
6c75a589 639int btrfs_leaf_free_space(const struct extent_buffer *leaf);
babbf170 640
95a06077
JS
641static inline int is_fstree(u64 rootid)
642{
643 if (rootid == BTRFS_FS_TREE_OBJECTID ||
e09fe2d2
QW
644 ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
645 !btrfs_qgroup_level(rootid)))
95a06077
JS
646 return 1;
647 return 0;
648}
210549eb 649
37f00a6d
JT
650static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root)
651{
652 return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID;
653}
654
b3cbfb0d 655u16 btrfs_csum_type_size(u16 type);
0e6c40eb
JB
656int btrfs_super_csum_size(const struct btrfs_super_block *s);
657const char *btrfs_super_csum_name(u16 csum_type);
658const char *btrfs_super_csum_driver(u16 csum_type);
659size_t __attribute_const__ btrfs_get_num_csums(void);
660
f57ad937
QW
661/*
662 * We use page status Private2 to indicate there is an ordered extent with
663 * unfinished IO.
664 *
665 * Rename the Private2 accessors to Ordered, to improve readability.
666 */
667#define PageOrdered(page) PagePrivate2(page)
668#define SetPageOrdered(page) SetPagePrivate2(page)
669#define ClearPageOrdered(page) ClearPagePrivate2(page)
895586eb
MWO
670#define folio_test_ordered(folio) folio_test_private_2(folio)
671#define folio_set_ordered(folio) folio_set_private_2(folio)
672#define folio_clear_ordered(folio) folio_clear_private_2(folio)
f57ad937 673
eb60ceac 674#endif