]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/alloc.h
ac83e4f22d0e8bc27d33f97fcc53b8a2f86aee53
[bcachefs-tools-debian] / libbcache / alloc.h
1 #ifndef _BCACHE_ALLOC_H
2 #define _BCACHE_ALLOC_H
3
4 #include "alloc_types.h"
5
6 struct bkey;
7 struct bucket;
8 struct cache;
9 struct cache_set;
10 struct cache_group;
11
12 static inline size_t prios_per_bucket(const struct cache *ca)
13 {
14         return (bucket_bytes(ca) - sizeof(struct prio_set)) /
15                 sizeof(struct bucket_disk);
16 }
17
18 static inline size_t prio_buckets(const struct cache *ca)
19 {
20         return DIV_ROUND_UP((size_t) (ca)->mi.nbuckets, prios_per_bucket(ca));
21 }
22
23 void bch_cache_group_remove_cache(struct cache_group *, struct cache *);
24 void bch_cache_group_add_cache(struct cache_group *, struct cache *);
25
26 int bch_prio_read(struct cache *);
27
28 void bch_recalc_min_prio(struct cache *, int);
29
30 void bch_open_bucket_put(struct cache_set *, struct open_bucket *);
31
32 struct open_bucket *bch_alloc_sectors_start(struct cache_set *,
33                                             struct write_point *,
34                                             unsigned, enum alloc_reserve,
35                                             struct closure *);
36
37 void bch_alloc_sectors_append_ptrs(struct cache_set *, struct bkey_i_extent *,
38                                    unsigned, struct open_bucket *, unsigned);
39 void bch_alloc_sectors_done(struct cache_set *, struct write_point *,
40                             struct open_bucket *);
41
42 struct open_bucket *bch_alloc_sectors(struct cache_set *, struct write_point *,
43                                       struct bkey_i_extent *, unsigned,
44                                       enum alloc_reserve, struct closure *);
45
46 static inline void bch_wake_allocator(struct cache *ca)
47 {
48         struct task_struct *p;
49
50         rcu_read_lock();
51         if ((p = ACCESS_ONCE(ca->alloc_thread)))
52                 wake_up_process(p);
53         rcu_read_unlock();
54 }
55
56 static inline struct cache *cache_group_next_rcu(struct cache_group *devs,
57                                                  unsigned *iter)
58 {
59         struct cache *ret = NULL;
60
61         while (*iter < devs->nr_devices &&
62                !(ret = rcu_dereference(devs->d[*iter].dev)))
63                 (*iter)++;
64
65         return ret;
66 }
67
68 #define group_for_each_cache_rcu(ca, devs, iter)                        \
69         for ((iter) = 0;                                                \
70              ((ca) = cache_group_next_rcu((devs), &(iter)));            \
71              (iter)++)
72
73 static inline struct cache *cache_group_next(struct cache_group *devs,
74                                              unsigned *iter)
75 {
76         struct cache *ret;
77
78         rcu_read_lock();
79         if ((ret = cache_group_next_rcu(devs, iter)))
80                 percpu_ref_get(&ret->ref);
81         rcu_read_unlock();
82
83         return ret;
84 }
85
86 #define group_for_each_cache(ca, devs, iter)                            \
87         for ((iter) = 0;                                                \
88              (ca = cache_group_next(devs, &(iter)));                    \
89              percpu_ref_put(&ca->ref), (iter)++)
90
91 #define __open_bucket_next_online_device(_c, _ob, _ptr, _ca)            \
92 ({                                                                      \
93         (_ca) = NULL;                                                   \
94                                                                         \
95         while ((_ptr) < (_ob)->ptrs + (_ob)->nr_ptrs &&                 \
96                !((_ca) = PTR_CACHE(_c, _ptr)))                          \
97                 (_ptr)++;                                               \
98         (_ca);                                                          \
99 })
100
101 #define open_bucket_for_each_online_device(_c, _ob, _ptr, _ca)          \
102         for ((_ptr) = (_ob)->ptrs;                                      \
103              ((_ca) = __open_bucket_next_online_device(_c, _ob, _ptr, _ca));\
104              (_ptr)++)
105
106 void bch_cache_allocator_stop(struct cache *);
107 int bch_cache_allocator_start(struct cache *);
108 void bch_open_buckets_init(struct cache_set *);
109
110 #endif /* _BCACHE_ALLOC_H */