alloc_foreground.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
  3. #define _BCACHEFS_ALLOC_FOREGROUND_H
  4. #include "bcachefs.h"
  5. #include "alloc_types.h"
  6. #include "extents.h"
  7. #include "sb-members.h"
  8. #include <linux/hash.h>
  9. struct bkey;
  10. struct bch_dev;
  11. struct bch_fs;
  12. struct bch_devs_List;
  13. extern const char * const bch2_watermarks[];
  14. void bch2_reset_alloc_cursors(struct bch_fs *);
  15. struct dev_alloc_list {
  16. unsigned nr;
  17. u8 devs[BCH_SB_MEMBERS_MAX];
  18. };
  19. struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
  20. struct dev_stripe_state *,
  21. struct bch_devs_mask *);
  22. void bch2_dev_stripe_increment(struct bch_dev *, struct dev_stripe_state *);
  23. long bch2_bucket_alloc_new_fs(struct bch_dev *);
  24. static inline struct bch_dev *ob_dev(struct bch_fs *c, struct open_bucket *ob)
  25. {
  26. return bch2_dev_have_ref(c, ob->dev);
  27. }
  28. struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
  29. enum bch_watermark, enum bch_data_type,
  30. struct closure *);
  31. static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
  32. struct open_bucket *ob)
  33. {
  34. BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
  35. obs->v[obs->nr++] = ob - c->open_buckets;
  36. }
  37. #define open_bucket_for_each(_c, _obs, _ob, _i) \
  38. for ((_i) = 0; \
  39. (_i) < (_obs)->nr && \
  40. ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
  41. (_i)++)
  42. static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
  43. struct open_buckets *obs)
  44. {
  45. struct open_bucket *ob;
  46. unsigned i;
  47. open_bucket_for_each(c, obs, ob, i)
  48. if (ob->ec)
  49. return ob;
  50. return NULL;
  51. }
  52. void bch2_open_bucket_write_error(struct bch_fs *,
  53. struct open_buckets *, unsigned);
  54. void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
  55. static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
  56. {
  57. if (atomic_dec_and_test(&ob->pin))
  58. __bch2_open_bucket_put(c, ob);
  59. }
  60. static inline void bch2_open_buckets_put(struct bch_fs *c,
  61. struct open_buckets *ptrs)
  62. {
  63. struct open_bucket *ob;
  64. unsigned i;
  65. open_bucket_for_each(c, ptrs, ob, i)
  66. bch2_open_bucket_put(c, ob);
  67. ptrs->nr = 0;
  68. }
  69. static inline void bch2_alloc_sectors_done_inlined(struct bch_fs *c, struct write_point *wp)
  70. {
  71. struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
  72. struct open_bucket *ob;
  73. unsigned i;
  74. open_bucket_for_each(c, &wp->ptrs, ob, i)
  75. ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
  76. wp->ptrs = keep;
  77. mutex_unlock(&wp->lock);
  78. bch2_open_buckets_put(c, &ptrs);
  79. }
  80. static inline void bch2_open_bucket_get(struct bch_fs *c,
  81. struct write_point *wp,
  82. struct open_buckets *ptrs)
  83. {
  84. struct open_bucket *ob;
  85. unsigned i;
  86. open_bucket_for_each(c, &wp->ptrs, ob, i) {
  87. ob->data_type = wp->data_type;
  88. atomic_inc(&ob->pin);
  89. ob_push(c, ptrs, ob);
  90. }
  91. }
  92. static inline open_bucket_idx_t *open_bucket_hashslot(struct bch_fs *c,
  93. unsigned dev, u64 bucket)
  94. {
  95. return c->open_buckets_hash +
  96. (jhash_3words(dev, bucket, bucket >> 32, 0) &
  97. (OPEN_BUCKETS_COUNT - 1));
  98. }
  99. static inline bool bch2_bucket_is_open(struct bch_fs *c, unsigned dev, u64 bucket)
  100. {
  101. open_bucket_idx_t slot = *open_bucket_hashslot(c, dev, bucket);
  102. while (slot) {
  103. struct open_bucket *ob = &c->open_buckets[slot];
  104. if (ob->dev == dev && ob->bucket == bucket)
  105. return true;
  106. slot = ob->hash;
  107. }
  108. return false;
  109. }
  110. static inline bool bch2_bucket_is_open_safe(struct bch_fs *c, unsigned dev, u64 bucket)
  111. {
  112. bool ret;
  113. if (bch2_bucket_is_open(c, dev, bucket))
  114. return true;
  115. spin_lock(&c->freelist_lock);
  116. ret = bch2_bucket_is_open(c, dev, bucket);
  117. spin_unlock(&c->freelist_lock);
  118. return ret;
  119. }
  120. enum bch_write_flags;
  121. int bch2_bucket_alloc_set_trans(struct btree_trans *, struct open_buckets *,
  122. struct dev_stripe_state *, struct bch_devs_mask *,
  123. unsigned, unsigned *, bool *, enum bch_write_flags,
  124. enum bch_data_type, enum bch_watermark,
  125. struct closure *);
  126. int bch2_alloc_sectors_start_trans(struct btree_trans *,
  127. unsigned, unsigned,
  128. struct write_point_specifier,
  129. struct bch_devs_list *,
  130. unsigned, unsigned,
  131. enum bch_watermark,
  132. enum bch_write_flags,
  133. struct closure *,
  134. struct write_point **);
  135. struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *, struct open_bucket *);
  136. /*
  137. * Append pointers to the space we just allocated to @k, and mark @sectors space
  138. * as allocated out of @ob
  139. */
  140. static inline void
  141. bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs *c, struct write_point *wp,
  142. struct bkey_i *k, unsigned sectors,
  143. bool cached)
  144. {
  145. struct open_bucket *ob;
  146. unsigned i;
  147. BUG_ON(sectors > wp->sectors_free);
  148. wp->sectors_free -= sectors;
  149. wp->sectors_allocated += sectors;
  150. open_bucket_for_each(c, &wp->ptrs, ob, i) {
  151. struct bch_dev *ca = ob_dev(c, ob);
  152. struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
  153. ptr.cached = cached ||
  154. (!ca->mi.durability &&
  155. wp->data_type == BCH_DATA_user);
  156. bch2_bkey_append_ptr(k, ptr);
  157. BUG_ON(sectors > ob->sectors_free);
  158. ob->sectors_free -= sectors;
  159. }
  160. }
  161. void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
  162. struct bkey_i *, unsigned, bool);
  163. void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
  164. void bch2_open_buckets_stop(struct bch_fs *c, struct bch_dev *, bool);
  165. static inline struct write_point_specifier writepoint_hashed(unsigned long v)
  166. {
  167. return (struct write_point_specifier) { .v = v | 1 };
  168. }
  169. static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
  170. {
  171. return (struct write_point_specifier) { .v = (unsigned long) wp };
  172. }
  173. void bch2_fs_allocator_foreground_init(struct bch_fs *);
  174. void bch2_open_bucket_to_text(struct printbuf *, struct bch_fs *, struct open_bucket *);
  175. void bch2_open_buckets_to_text(struct printbuf *, struct bch_fs *, struct bch_dev *);
  176. void bch2_open_buckets_partial_to_text(struct printbuf *, struct bch_fs *);
  177. void bch2_write_points_to_text(struct printbuf *, struct bch_fs *);
  178. void bch2_fs_alloc_debug_to_text(struct printbuf *, struct bch_fs *);
  179. void bch2_dev_alloc_debug_to_text(struct printbuf *, struct bch_dev *);
  180. void __bch2_wait_on_allocator(struct bch_fs *, struct closure *);
  181. static inline void bch2_wait_on_allocator(struct bch_fs *c, struct closure *cl)
  182. {
  183. if (cl->closure_get_happened)
  184. __bch2_wait_on_allocator(c, cl);
  185. }
  186. #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */