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