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