Linux Audio

Check our new training course

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