ctree.h 23 KB

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