1 #ifndef _BCACHEFS_ALLOC_H
2 #define _BCACHEFS_ALLOC_H
5 #include "alloc_types.h"
12 struct dev_alloc_list {
14 u8 devs[BCH_SB_MEMBERS_MAX];
17 struct dev_alloc_list bch2_wp_alloc_list(struct bch_fs *,
19 struct bch_devs_mask *);
20 void bch2_wp_rescale(struct bch_fs *, struct bch_dev *,
21 struct write_point *);
23 int bch2_alloc_read(struct bch_fs *, struct list_head *);
24 int bch2_alloc_replay_key(struct bch_fs *, struct bpos);
26 enum bucket_alloc_ret {
28 OPEN_BUCKETS_EMPTY = -1,
29 FREELIST_EMPTY = -2, /* Allocator thread not keeping up */
30 NO_DEVICES = -3, /* -EROFS */
33 int bch2_bucket_alloc(struct bch_fs *, struct bch_dev *, enum alloc_reserve, bool,
36 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
38 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
40 if (atomic_dec_and_test(&ob->pin))
41 __bch2_open_bucket_put(c, ob);
44 static inline void bch2_open_bucket_put_refs(struct bch_fs *c, u8 *nr, u8 *refs)
48 for (i = 0; i < *nr; i++)
49 bch2_open_bucket_put(c, c->open_buckets + refs[i]);
54 static inline void bch2_open_bucket_get(struct bch_fs *c,
55 struct write_point *wp,
60 for (i = 0; i < wp->nr_ptrs_can_use; i++) {
61 struct open_bucket *ob = wp->ptrs[i];
64 refs[(*nr)++] = ob - c->open_buckets;
68 struct write_point *bch2_alloc_sectors_start(struct bch_fs *,
70 struct write_point_specifier,
71 struct bch_devs_list *,
77 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
78 struct bkey_i_extent *, unsigned);
79 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
81 static inline void bch2_wake_allocator(struct bch_dev *ca)
83 struct task_struct *p;
86 if ((p = READ_ONCE(ca->alloc_thread)))
91 #define writepoint_for_each_ptr(_wp, _ob, _i) \
93 (_i) < (_wp)->nr_ptrs && ((_ob) = (_wp)->ptrs[_i], true); \
96 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
98 return (struct write_point_specifier) { .v = v | 1 };
101 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
103 return (struct write_point_specifier) { .v = (unsigned long) wp };
106 void bch2_recalc_capacity(struct bch_fs *);
108 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
109 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
111 void bch2_dev_allocator_stop(struct bch_dev *);
112 int bch2_dev_allocator_start(struct bch_dev *);
114 static inline void writepoint_init(struct write_point *wp,
115 enum bch_data_type type)
117 mutex_init(&wp->lock);
121 int bch2_alloc_write(struct bch_fs *);
122 int bch2_fs_allocator_start(struct bch_fs *);
123 void bch2_fs_allocator_init(struct bch_fs *);
125 extern const struct bkey_ops bch2_bkey_alloc_ops;
127 #endif /* _BCACHEFS_ALLOC_H */