]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
Update bcachefs sources to 62de7539dc bcachefs: Make bkey types globally unique
[bcachefs-tools-debian] / libbcachefs / buckets.h
1 /*
2  * Code for manipulating bucket marks for garbage collection.
3  *
4  * Copyright 2014 Datera, Inc.
5  */
6
7 #ifndef _BUCKETS_H
8 #define _BUCKETS_H
9
10 #include "buckets_types.h"
11 #include "super.h"
12
13 #define for_each_bucket(_b, _buckets)                           \
14         for (_b = (_buckets)->b + (_buckets)->first_bucket;     \
15              _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
16
17 #define bucket_cmpxchg(g, new, expr)                            \
18 ({                                                              \
19         u64 _v = atomic64_read(&(g)->_mark.v);                  \
20         struct bucket_mark _old;                                \
21                                                                 \
22         do {                                                    \
23                 (new).v.counter = _old.v.counter = _v;          \
24                 expr;                                           \
25         } while ((_v = atomic64_cmpxchg(&(g)->_mark.v,          \
26                                _old.v.counter,                  \
27                                (new).v.counter)) != _old.v.counter);\
28         _old;                                                   \
29 })
30
31 static inline struct bucket_array *__bucket_array(struct bch_dev *ca,
32                                                   bool gc)
33 {
34         return rcu_dereference_check(ca->buckets[gc],
35                                      !ca->fs ||
36                                      percpu_rwsem_is_held(&ca->fs->usage_lock) ||
37                                      lockdep_is_held(&ca->fs->gc_lock) ||
38                                      lockdep_is_held(&ca->bucket_lock));
39 }
40
41 static inline struct bucket_array *bucket_array(struct bch_dev *ca)
42 {
43         return __bucket_array(ca, false);
44 }
45
46 static inline struct bucket *__bucket(struct bch_dev *ca, size_t b, bool gc)
47 {
48         struct bucket_array *buckets = __bucket_array(ca, gc);
49
50         BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
51         return buckets->b + b;
52 }
53
54 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
55 {
56         return __bucket(ca, b, false);
57 }
58
59 static inline void bucket_io_clock_reset(struct bch_fs *c, struct bch_dev *ca,
60                                          size_t b, int rw)
61 {
62         bucket(ca, b)->io_time[rw] = c->bucket_clock[rw].hand;
63 }
64
65 static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
66 {
67         return c->bucket_clock[rw].hand - g->io_time[rw];
68 }
69
70 /*
71  * bucket_gc_gen() returns the difference between the bucket's current gen and
72  * the oldest gen of any pointer into that bucket in the btree.
73  */
74
75 static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
76 {
77         return bucket(ca, b)->mark.gen - ca->oldest_gens[b];
78 }
79
80 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
81                                    const struct bch_extent_ptr *ptr)
82 {
83         return sector_to_bucket(ca, ptr->offset);
84 }
85
86 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
87                                         const struct bch_extent_ptr *ptr)
88 {
89         return bucket(ca, PTR_BUCKET_NR(ca, ptr));
90 }
91
92 static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
93                                                  const struct bch_extent_ptr *ptr)
94 {
95         struct bucket_mark m;
96
97         rcu_read_lock();
98         m = READ_ONCE(bucket(ca, PTR_BUCKET_NR(ca, ptr))->mark);
99         rcu_read_unlock();
100
101         return m;
102 }
103
104 static inline int gen_cmp(u8 a, u8 b)
105 {
106         return (s8) (a - b);
107 }
108
109 static inline int gen_after(u8 a, u8 b)
110 {
111         int r = gen_cmp(a, b);
112
113         return r > 0 ? r : 0;
114 }
115
116 /**
117  * ptr_stale() - check if a pointer points into a bucket that has been
118  * invalidated.
119  */
120 static inline u8 ptr_stale(struct bch_dev *ca,
121                            const struct bch_extent_ptr *ptr)
122 {
123         return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
124 }
125
126 /* bucket gc marks */
127
128 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
129 {
130         return mark.dirty_sectors + mark.cached_sectors;
131 }
132
133 static inline bool bucket_unused(struct bucket_mark mark)
134 {
135         return !mark.owned_by_allocator &&
136                 !mark.data_type &&
137                 !bucket_sectors_used(mark);
138 }
139
140 /* Device usage: */
141
142 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *, bool);
143 struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *, struct bch_dev *);
144
145 static inline u64 __dev_buckets_available(struct bch_dev *ca,
146                                           struct bch_dev_usage stats)
147 {
148         u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
149
150         if (WARN_ONCE(stats.buckets_unavailable > total,
151                       "buckets_unavailable overflow (%llu > %llu)\n",
152                       stats.buckets_unavailable, total))
153                 return 0;
154
155         return total - stats.buckets_unavailable;
156 }
157
158 /*
159  * Number of reclaimable buckets - only for use by the allocator thread:
160  */
161 static inline u64 dev_buckets_available(struct bch_fs *c, struct bch_dev *ca)
162 {
163         return __dev_buckets_available(ca, bch2_dev_usage_read(c, ca));
164 }
165
166 static inline u64 __dev_buckets_free(struct bch_dev *ca,
167                                      struct bch_dev_usage stats)
168 {
169         return __dev_buckets_available(ca, stats) +
170                 fifo_used(&ca->free[RESERVE_NONE]) +
171                 fifo_used(&ca->free_inc);
172 }
173
174 static inline u64 dev_buckets_free(struct bch_fs *c, struct bch_dev *ca)
175 {
176         return __dev_buckets_free(ca, bch2_dev_usage_read(c, ca));
177 }
178
179 /* Filesystem usage: */
180
181 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *, bool);
182 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
183 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
184                          struct disk_reservation *, struct gc_pos);
185
186 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage);
187
188 static inline u64 bch2_fs_sectors_free(struct bch_fs *c,
189                                        struct bch_fs_usage stats)
190 {
191         return c->capacity - bch2_fs_sectors_used(c, stats);
192 }
193
194 static inline bool is_available_bucket(struct bucket_mark mark)
195 {
196         return (!mark.owned_by_allocator &&
197                 !mark.dirty_sectors &&
198                 !mark.stripe &&
199                 !mark.nouse);
200 }
201
202 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
203                                                u16 last_seq_ondisk)
204 {
205         return m.journal_seq_valid &&
206                 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
207 }
208
209 void bch2_bucket_seq_cleanup(struct bch_fs *);
210
211 void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
212                             size_t, struct bucket_mark *);
213 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
214                             size_t, bool, struct gc_pos, unsigned);
215 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
216                                size_t, enum bch_data_type, unsigned,
217                                struct gc_pos, unsigned);
218
219 #define BCH_BUCKET_MARK_NOATOMIC                (1 << 0)
220 #define BCH_BUCKET_MARK_GC                      (1 << 1)
221
222 int bch2_mark_key_locked(struct bch_fs *, struct bkey_s_c,
223                   bool, s64, struct gc_pos,
224                   struct bch_fs_usage *, u64, unsigned);
225 int bch2_mark_key(struct bch_fs *, struct bkey_s_c,
226                   bool, s64, struct gc_pos,
227                   struct bch_fs_usage *, u64, unsigned);
228 void bch2_mark_update(struct btree_insert *, struct btree_insert_entry *);
229
230 void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);
231
232 static inline void bch2_disk_reservation_put(struct bch_fs *c,
233                                              struct disk_reservation *res)
234 {
235         if (res->sectors)
236                 __bch2_disk_reservation_put(c, res);
237 }
238
239 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
240 #define BCH_DISK_RESERVATION_GC_LOCK_HELD       (1 << 1)
241 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD   (1 << 2)
242
243 int bch2_disk_reservation_add(struct bch_fs *,
244                              struct disk_reservation *,
245                              unsigned, int);
246
247 static inline struct disk_reservation
248 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
249 {
250         return (struct disk_reservation) {
251                 .sectors        = 0,
252 #if 0
253                 /* not used yet: */
254                 .gen            = c->capacity_gen,
255 #endif
256                 .nr_replicas    = nr_replicas,
257         };
258 }
259
260 static inline int bch2_disk_reservation_get(struct bch_fs *c,
261                                             struct disk_reservation *res,
262                                             unsigned sectors,
263                                             unsigned nr_replicas,
264                                             int flags)
265 {
266         *res = bch2_disk_reservation_init(c, nr_replicas);
267
268         return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
269 }
270
271 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
272 void bch2_dev_buckets_free(struct bch_dev *);
273 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
274
275 #endif /* _BUCKETS_H */