btree_cache.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_BTREE_CACHE_H
  3. #define _BCACHEFS_BTREE_CACHE_H
  4. #include "bcachefs.h"
  5. #include "btree_types.h"
  6. #include "bkey_methods.h"
  7. extern const char * const bch2_btree_node_flags[];
  8. struct btree_iter;
  9. void bch2_recalc_btree_reserve(struct bch_fs *);
  10. void bch2_btree_node_to_freelist(struct bch_fs *, struct btree *);
  11. void __bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
  12. void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
  13. int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *);
  14. int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *,
  15. unsigned, enum btree_id);
  16. void bch2_node_pin(struct bch_fs *, struct btree *);
  17. void bch2_btree_cache_unpin(struct bch_fs *);
  18. void bch2_btree_node_update_key_early(struct btree_trans *, enum btree_id, unsigned,
  19. struct bkey_s_c, struct bkey_i *);
  20. void bch2_btree_cache_cannibalize_unlock(struct btree_trans *);
  21. int bch2_btree_cache_cannibalize_lock(struct btree_trans *, struct closure *);
  22. struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *);
  23. struct btree *bch2_btree_node_mem_alloc(struct btree_trans *, bool);
  24. struct btree *bch2_btree_node_get(struct btree_trans *, struct btree_path *,
  25. const struct bkey_i *, unsigned,
  26. enum six_lock_type, unsigned long);
  27. struct btree *bch2_btree_node_get_noiter(struct btree_trans *, const struct bkey_i *,
  28. enum btree_id, unsigned, bool);
  29. int bch2_btree_node_prefetch(struct btree_trans *, struct btree_path *,
  30. const struct bkey_i *, enum btree_id, unsigned);
  31. void bch2_btree_node_evict(struct btree_trans *, const struct bkey_i *);
  32. void bch2_fs_btree_cache_exit(struct bch_fs *);
  33. int bch2_fs_btree_cache_init(struct bch_fs *);
  34. void bch2_fs_btree_cache_init_early(struct btree_cache *);
  35. static inline u64 btree_ptr_hash_val(const struct bkey_i *k)
  36. {
  37. switch (k->k.type) {
  38. case KEY_TYPE_btree_ptr:
  39. return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start);
  40. case KEY_TYPE_btree_ptr_v2:
  41. /*
  42. * The cast/deref is only necessary to avoid sparse endianness
  43. * warnings:
  44. */
  45. return *((u64 *) &bkey_i_to_btree_ptr_v2_c(k)->v.seq);
  46. default:
  47. return 0;
  48. }
  49. }
  50. static inline struct btree *btree_node_mem_ptr(const struct bkey_i *k)
  51. {
  52. return k->k.type == KEY_TYPE_btree_ptr_v2
  53. ? (void *)(unsigned long)bkey_i_to_btree_ptr_v2_c(k)->v.mem_ptr
  54. : NULL;
  55. }
  56. /* is btree node in hash table? */
  57. static inline bool btree_node_hashed(struct btree *b)
  58. {
  59. return b->hash_val != 0;
  60. }
  61. #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \
  62. for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \
  63. &(_c)->btree_cache.table), \
  64. _iter = 0; _iter < (_tbl)->size; _iter++) \
  65. rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
  66. static inline size_t btree_buf_bytes(const struct btree *b)
  67. {
  68. return 1UL << b->byte_order;
  69. }
  70. static inline size_t btree_buf_max_u64s(const struct btree *b)
  71. {
  72. return (btree_buf_bytes(b) - sizeof(struct btree_node)) / sizeof(u64);
  73. }
  74. static inline size_t btree_max_u64s(const struct bch_fs *c)
  75. {
  76. return (c->opts.btree_node_size - sizeof(struct btree_node)) / sizeof(u64);
  77. }
  78. static inline size_t btree_sectors(const struct bch_fs *c)
  79. {
  80. return c->opts.btree_node_size >> SECTOR_SHIFT;
  81. }
  82. static inline unsigned btree_blocks(const struct bch_fs *c)
  83. {
  84. return btree_sectors(c) >> c->block_bits;
  85. }
  86. #define BTREE_SPLIT_THRESHOLD(c) (btree_max_u64s(c) * 2 / 3)
  87. #define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3)
  88. #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \
  89. (BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \
  90. (BTREE_FOREGROUND_MERGE_THRESHOLD(c) >> 2))
  91. static inline unsigned btree_id_nr_alive(struct bch_fs *c)
  92. {
  93. return BTREE_ID_NR + c->btree_roots_extra.nr;
  94. }
  95. static inline struct btree_root *bch2_btree_id_root(struct bch_fs *c, unsigned id)
  96. {
  97. if (likely(id < BTREE_ID_NR)) {
  98. return &c->btree_roots_known[id];
  99. } else {
  100. unsigned idx = id - BTREE_ID_NR;
  101. EBUG_ON(idx >= c->btree_roots_extra.nr);
  102. return &c->btree_roots_extra.data[idx];
  103. }
  104. }
  105. static inline struct btree *btree_node_root(struct bch_fs *c, struct btree *b)
  106. {
  107. return bch2_btree_id_root(c, b->c.btree_id)->b;
  108. }
  109. const char *bch2_btree_id_str(enum btree_id);
  110. void bch2_btree_id_to_text(struct printbuf *, enum btree_id);
  111. void bch2_btree_pos_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
  112. void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
  113. void bch2_btree_cache_to_text(struct printbuf *, const struct btree_cache *);
  114. #endif /* _BCACHEFS_BTREE_CACHE_H */