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