]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.c
Update bcachefs sources to ab3b6e7dd6 bcachefs: Fix pr_buf() calls
[bcachefs-tools-debian] / libbcachefs / alloc_background.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
5 #include "btree_cache.h"
6 #include "btree_io.h"
7 #include "btree_key_cache.h"
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
10 #include "btree_gc.h"
11 #include "buckets.h"
12 #include "buckets_waiting_for_journal.h"
13 #include "clock.h"
14 #include "debug.h"
15 #include "ec.h"
16 #include "error.h"
17 #include "lru.h"
18 #include "recovery.h"
19 #include "varint.h"
20
21 #include <linux/kthread.h>
22 #include <linux/math64.h>
23 #include <linux/random.h>
24 #include <linux/rculist.h>
25 #include <linux/rcupdate.h>
26 #include <linux/sched/task.h>
27 #include <linux/sort.h>
28 #include <trace/events/bcachefs.h>
29
30 /* Persistent alloc info: */
31
32 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
33 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
34         BCH_ALLOC_FIELDS_V1()
35 #undef x
36 };
37
38 const char * const bch2_bucket_states[] = {
39         "free",
40         "need gc gens",
41         "need discard",
42         "cached",
43         "dirty",
44         NULL
45 };
46
47 struct bkey_alloc_unpacked {
48         u64             journal_seq;
49         u64             bucket;
50         u8              dev;
51         u8              gen;
52         u8              oldest_gen;
53         u8              data_type;
54         bool            need_discard:1;
55         bool            need_inc_gen:1;
56 #define x(_name, _bits) u##_bits _name;
57         BCH_ALLOC_FIELDS_V2()
58 #undef  x
59 };
60
61 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
62                                      const void **p, unsigned field)
63 {
64         unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
65         u64 v;
66
67         if (!(a->fields & (1 << field)))
68                 return 0;
69
70         switch (bytes) {
71         case 1:
72                 v = *((const u8 *) *p);
73                 break;
74         case 2:
75                 v = le16_to_cpup(*p);
76                 break;
77         case 4:
78                 v = le32_to_cpup(*p);
79                 break;
80         case 8:
81                 v = le64_to_cpup(*p);
82                 break;
83         default:
84                 BUG();
85         }
86
87         *p += bytes;
88         return v;
89 }
90
91 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
92                                       unsigned field, u64 v)
93 {
94         unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
95
96         if (!v)
97                 return;
98
99         a->v.fields |= 1 << field;
100
101         switch (bytes) {
102         case 1:
103                 *((u8 *) *p) = v;
104                 break;
105         case 2:
106                 *((__le16 *) *p) = cpu_to_le16(v);
107                 break;
108         case 4:
109                 *((__le32 *) *p) = cpu_to_le32(v);
110                 break;
111         case 8:
112                 *((__le64 *) *p) = cpu_to_le64(v);
113                 break;
114         default:
115                 BUG();
116         }
117
118         *p += bytes;
119 }
120
121 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
122                                  struct bkey_s_c k)
123 {
124         const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
125         const void *d = in->data;
126         unsigned idx = 0;
127
128         out->gen = in->gen;
129
130 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
131         BCH_ALLOC_FIELDS_V1()
132 #undef  x
133 }
134
135 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
136                                 struct bkey_s_c k)
137 {
138         struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
139         const u8 *in = a.v->data;
140         const u8 *end = bkey_val_end(a);
141         unsigned fieldnr = 0;
142         int ret;
143         u64 v;
144
145         out->gen        = a.v->gen;
146         out->oldest_gen = a.v->oldest_gen;
147         out->data_type  = a.v->data_type;
148
149 #define x(_name, _bits)                                                 \
150         if (fieldnr < a.v->nr_fields) {                                 \
151                 ret = bch2_varint_decode_fast(in, end, &v);             \
152                 if (ret < 0)                                            \
153                         return ret;                                     \
154                 in += ret;                                              \
155         } else {                                                        \
156                 v = 0;                                                  \
157         }                                                               \
158         out->_name = v;                                                 \
159         if (v != out->_name)                                            \
160                 return -1;                                              \
161         fieldnr++;
162
163         BCH_ALLOC_FIELDS_V2()
164 #undef  x
165         return 0;
166 }
167
168 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
169                                 struct bkey_s_c k)
170 {
171         struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
172         const u8 *in = a.v->data;
173         const u8 *end = bkey_val_end(a);
174         unsigned fieldnr = 0;
175         int ret;
176         u64 v;
177
178         out->gen        = a.v->gen;
179         out->oldest_gen = a.v->oldest_gen;
180         out->data_type  = a.v->data_type;
181         out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
182         out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
183         out->journal_seq = le64_to_cpu(a.v->journal_seq);
184
185 #define x(_name, _bits)                                                 \
186         if (fieldnr < a.v->nr_fields) {                                 \
187                 ret = bch2_varint_decode_fast(in, end, &v);             \
188                 if (ret < 0)                                            \
189                         return ret;                                     \
190                 in += ret;                                              \
191         } else {                                                        \
192                 v = 0;                                                  \
193         }                                                               \
194         out->_name = v;                                                 \
195         if (v != out->_name)                                            \
196                 return -1;                                              \
197         fieldnr++;
198
199         BCH_ALLOC_FIELDS_V2()
200 #undef  x
201         return 0;
202 }
203
204 static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
205 {
206         struct bkey_alloc_unpacked ret = {
207                 .dev    = k.k->p.inode,
208                 .bucket = k.k->p.offset,
209                 .gen    = 0,
210         };
211
212         switch (k.k->type) {
213         case KEY_TYPE_alloc:
214                 bch2_alloc_unpack_v1(&ret, k);
215                 break;
216         case KEY_TYPE_alloc_v2:
217                 bch2_alloc_unpack_v2(&ret, k);
218                 break;
219         case KEY_TYPE_alloc_v3:
220                 bch2_alloc_unpack_v3(&ret, k);
221                 break;
222         }
223
224         return ret;
225 }
226
227 void bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
228 {
229         if (k.k->type == KEY_TYPE_alloc_v4) {
230                 *out = *bkey_s_c_to_alloc_v4(k).v;
231         } else {
232                 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
233
234                 *out = (struct bch_alloc_v4) {
235                         .journal_seq            = u.journal_seq,
236                         .flags                  = u.need_discard,
237                         .gen                    = u.gen,
238                         .oldest_gen             = u.oldest_gen,
239                         .data_type              = u.data_type,
240                         .stripe_redundancy      = u.stripe_redundancy,
241                         .dirty_sectors          = u.dirty_sectors,
242                         .cached_sectors         = u.cached_sectors,
243                         .io_time[READ]          = u.read_time,
244                         .io_time[WRITE]         = u.write_time,
245                         .stripe                 = u.stripe,
246                 };
247         }
248 }
249
250 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
251 {
252         struct bkey_i_alloc_v4 *ret;
253
254         if (k.k->type == KEY_TYPE_alloc_v4) {
255                 ret = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
256                 if (!IS_ERR(ret))
257                         bkey_reassemble(&ret->k_i, k);
258         } else {
259                 ret = bch2_trans_kmalloc(trans, sizeof(*ret));
260                 if (!IS_ERR(ret)) {
261                         bkey_alloc_v4_init(&ret->k_i);
262                         ret->k.p = k.k->p;
263                         bch2_alloc_to_v4(k, &ret->v);
264                 }
265         }
266         return ret;
267 }
268
269 struct bkey_i_alloc_v4 *
270 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
271                               struct bpos pos)
272 {
273         struct bkey_s_c k;
274         struct bkey_i_alloc_v4 *a;
275         int ret;
276
277         bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
278                              BTREE_ITER_WITH_UPDATES|
279                              BTREE_ITER_CACHED|
280                              BTREE_ITER_INTENT);
281         k = bch2_btree_iter_peek_slot(iter);
282         ret = bkey_err(k);
283         if (ret) {
284                 bch2_trans_iter_exit(trans, iter);
285                 return ERR_PTR(ret);
286         }
287
288         a = bch2_alloc_to_v4_mut(trans, k);
289         if (IS_ERR(a))
290                 bch2_trans_iter_exit(trans, iter);
291         return a;
292 }
293
294 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
295 {
296         unsigned i, bytes = offsetof(struct bch_alloc, data);
297
298         for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
299                 if (a->fields & (1 << i))
300                         bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
301
302         return DIV_ROUND_UP(bytes, sizeof(u64));
303 }
304
305 const char *bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k)
306 {
307         struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
308
309         if (k.k->p.inode >= c->sb.nr_devices ||
310             !c->devs[k.k->p.inode])
311                 return "invalid device";
312
313         /* allow for unknown fields */
314         if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v))
315                 return "incorrect value size";
316
317         return NULL;
318 }
319
320 const char *bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
321 {
322         struct bkey_alloc_unpacked u;
323
324         if (k.k->p.inode >= c->sb.nr_devices ||
325             !c->devs[k.k->p.inode])
326                 return "invalid device";
327
328         if (bch2_alloc_unpack_v2(&u, k))
329                 return "unpack error";
330
331         return NULL;
332 }
333
334 const char *bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k)
335 {
336         struct bkey_alloc_unpacked u;
337         struct bch_dev *ca;
338
339         if (k.k->p.inode >= c->sb.nr_devices ||
340             !c->devs[k.k->p.inode])
341                 return "invalid device";
342
343         ca = bch_dev_bkey_exists(c, k.k->p.inode);
344
345         if (k.k->p.offset < ca->mi.first_bucket ||
346             k.k->p.offset >= ca->mi.nbuckets)
347                 return "invalid bucket";
348
349         if (bch2_alloc_unpack_v3(&u, k))
350                 return "unpack error";
351
352         return NULL;
353 }
354
355 const char *bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k)
356 {
357         struct bch_dev *ca;
358
359         if (k.k->p.inode >= c->sb.nr_devices ||
360             !c->devs[k.k->p.inode])
361                 return "invalid device";
362
363         ca = bch_dev_bkey_exists(c, k.k->p.inode);
364
365         if (k.k->p.offset < ca->mi.first_bucket ||
366             k.k->p.offset >= ca->mi.nbuckets)
367                 return "invalid bucket";
368
369         return NULL;
370 }
371
372 void bch2_alloc_v4_swab(struct bkey_s k)
373 {
374         struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
375
376         a->journal_seq          = swab64(a->journal_seq);
377         a->flags                = swab32(a->flags);
378         a->dirty_sectors        = swab32(a->dirty_sectors);
379         a->cached_sectors       = swab32(a->cached_sectors);
380         a->io_time[0]           = swab64(a->io_time[0]);
381         a->io_time[1]           = swab64(a->io_time[1]);
382         a->stripe               = swab32(a->stripe);
383         a->nr_external_backpointers = swab32(a->nr_external_backpointers);
384 }
385
386 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
387 {
388         struct bch_alloc_v4 a;
389
390         bch2_alloc_to_v4(k, &a);
391
392         pr_buf(out, "gen %u oldest_gen %u data_type %s journal_seq %llu need_discard %llu",
393                a.gen, a.oldest_gen, bch2_data_types[a.data_type],
394                a.journal_seq, BCH_ALLOC_V4_NEED_DISCARD(&a));
395         pr_buf(out, " dirty_sectors %u",        a.dirty_sectors);
396         pr_buf(out, " cached_sectors %u",       a.cached_sectors);
397         pr_buf(out, " stripe %u",               a.stripe);
398         pr_buf(out, " stripe_redundancy %u",    a.stripe_redundancy);
399         pr_buf(out, " read_time %llu",          a.io_time[READ]);
400         pr_buf(out, " write_time %llu",         a.io_time[WRITE]);
401 }
402
403 int bch2_alloc_read(struct bch_fs *c)
404 {
405         struct btree_trans trans;
406         struct btree_iter iter;
407         struct bkey_s_c k;
408         struct bch_alloc_v4 a;
409         struct bch_dev *ca;
410         int ret;
411
412         bch2_trans_init(&trans, c, 0, 0);
413
414         for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
415                            BTREE_ITER_PREFETCH, k, ret) {
416                 ca = bch_dev_bkey_exists(c, k.k->p.inode);
417                 bch2_alloc_to_v4(k, &a);
418
419                 *bucket_gen(ca, k.k->p.offset) = a.gen;
420         }
421         bch2_trans_iter_exit(&trans, &iter);
422
423         bch2_trans_exit(&trans);
424
425         if (ret)
426                 bch_err(c, "error reading alloc info: %i", ret);
427
428         return ret;
429 }
430
431 /* Free space/discard btree: */
432
433 static int bch2_bucket_do_index(struct btree_trans *trans,
434                                 struct bkey_s_c alloc_k,
435                                 struct bch_alloc_v4 a,
436                                 bool set)
437 {
438         struct bch_fs *c = trans->c;
439         struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
440         struct btree_iter iter;
441         struct bkey_s_c old;
442         struct bkey_i *k;
443         enum bucket_state state = bucket_state(a);
444         enum btree_id btree;
445         enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
446         enum bch_bkey_type new_type =  set ? KEY_TYPE_set : KEY_TYPE_deleted;
447         struct printbuf buf = PRINTBUF;
448         int ret;
449
450         if (state != BUCKET_free &&
451             state != BUCKET_need_discard)
452                 return 0;
453
454         k = bch2_trans_kmalloc(trans, sizeof(*k));
455         if (IS_ERR(k))
456                 return PTR_ERR(k);
457
458         bkey_init(&k->k);
459         k->k.type = new_type;
460
461         switch (state) {
462         case BUCKET_free:
463                 btree = BTREE_ID_freespace;
464                 k->k.p = alloc_freespace_pos(alloc_k.k->p, a);
465                 bch2_key_resize(&k->k, 1);
466                 break;
467         case BUCKET_need_discard:
468                 btree = BTREE_ID_need_discard;
469                 k->k.p = alloc_k.k->p;
470                 break;
471         default:
472                 return 0;
473         }
474
475         bch2_trans_iter_init(trans, &iter, btree,
476                              bkey_start_pos(&k->k),
477                              BTREE_ITER_INTENT);
478         old = bch2_btree_iter_peek_slot(&iter);
479         ret = bkey_err(old);
480         if (ret)
481                 goto err;
482
483         if (ca->mi.freespace_initialized &&
484             bch2_fs_inconsistent_on(old.k->type != old_type, c,
485                         "incorrect key when %s %s btree (got %s should be %s)\n"
486                         "  for %s",
487                         set ? "setting" : "clearing",
488                         bch2_btree_ids[btree],
489                         bch2_bkey_types[old.k->type],
490                         bch2_bkey_types[old_type],
491                         (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
492                 ret = -EIO;
493                 goto err;
494         }
495
496         ret = bch2_trans_update(trans, &iter, k, 0);
497 err:
498         bch2_trans_iter_exit(trans, &iter);
499         printbuf_exit(&buf);
500         return ret;
501 }
502
503 int bch2_trans_mark_alloc(struct btree_trans *trans,
504                           struct bkey_s_c old, struct bkey_i *new,
505                           unsigned flags)
506 {
507         struct bch_fs *c = trans->c;
508         struct bch_alloc_v4 old_a, *new_a;
509         u64 old_lru, new_lru;
510         int ret = 0;
511
512         /*
513          * Deletion only happens in the device removal path, with
514          * BTREE_TRIGGER_NORUN:
515          */
516         BUG_ON(new->k.type != KEY_TYPE_alloc_v4);
517
518         bch2_alloc_to_v4(old, &old_a);
519         new_a = &bkey_i_to_alloc_v4(new)->v;
520
521         if (new_a->dirty_sectors > old_a.dirty_sectors ||
522             new_a->cached_sectors > old_a.cached_sectors) {
523                 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
524                 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
525                 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
526                 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
527         }
528
529         if (old_a.data_type && !new_a->data_type &&
530             old_a.gen == new_a->gen &&
531             !bch2_bucket_is_open_safe(c, new->k.p.inode, new->k.p.offset)) {
532                 new_a->gen++;
533                 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
534         }
535
536         if (bucket_state(old_a) != bucket_state(*new_a) ||
537             (bucket_state(*new_a) == BUCKET_free &&
538              alloc_freespace_genbits(old_a) != alloc_freespace_genbits(*new_a))) {
539                 ret =   bch2_bucket_do_index(trans, old, old_a, false) ?:
540                         bch2_bucket_do_index(trans, bkey_i_to_s_c(new), *new_a, true);
541                 if (ret)
542                         return ret;
543         }
544
545         old_lru = alloc_lru_idx(old_a);
546         new_lru = alloc_lru_idx(*new_a);
547
548         if (old_lru != new_lru) {
549                 ret = bch2_lru_change(trans, new->k.p.inode, new->k.p.offset,
550                                       old_lru, &new_lru);
551                 if (ret)
552                         return ret;
553
554                 if (new_lru && new_a->io_time[READ] != new_lru)
555                         new_a->io_time[READ] = new_lru;
556         }
557
558         return 0;
559 }
560
561 static int bch2_check_alloc_key(struct btree_trans *trans,
562                                 struct btree_iter *alloc_iter)
563 {
564         struct bch_fs *c = trans->c;
565         struct btree_iter discard_iter, freespace_iter, lru_iter;
566         struct bch_alloc_v4 a;
567         unsigned discard_key_type, freespace_key_type;
568         struct bkey_s_c alloc_k, k;
569         struct printbuf buf = PRINTBUF;
570         struct printbuf buf2 = PRINTBUF;
571         int ret;
572
573         alloc_k = bch2_btree_iter_peek(alloc_iter);
574         if (!alloc_k.k)
575                 return 0;
576
577         ret = bkey_err(alloc_k);
578         if (ret)
579                 return ret;
580
581         bch2_alloc_to_v4(alloc_k, &a);
582         discard_key_type = bucket_state(a) == BUCKET_need_discard
583                 ? KEY_TYPE_set : 0;
584         freespace_key_type = bucket_state(a) == BUCKET_free
585                 ? KEY_TYPE_set : 0;
586
587         bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard,
588                              alloc_k.k->p, 0);
589         bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace,
590                              alloc_freespace_pos(alloc_k.k->p, a), 0);
591         bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
592                              POS(alloc_k.k->p.inode, a.io_time[READ]), 0);
593
594         k = bch2_btree_iter_peek_slot(&discard_iter);
595         ret = bkey_err(k);
596         if (ret)
597                 goto err;
598
599         if (fsck_err_on(k.k->type != discard_key_type, c,
600                         "incorrect key in need_discard btree (got %s should be %s)\n"
601                         "  %s",
602                         bch2_bkey_types[k.k->type],
603                         bch2_bkey_types[discard_key_type],
604                         (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
605                 struct bkey_i *update =
606                         bch2_trans_kmalloc(trans, sizeof(*update));
607
608                 ret = PTR_ERR_OR_ZERO(update);
609                 if (ret)
610                         goto err;
611
612                 bkey_init(&update->k);
613                 update->k.type  = discard_key_type;
614                 update->k.p     = discard_iter.pos;
615
616                 ret =   bch2_trans_update(trans, &discard_iter, update, 0) ?:
617                         bch2_trans_commit(trans, NULL, NULL, 0);
618                 if (ret)
619                         goto err;
620         }
621
622         k = bch2_btree_iter_peek_slot(&freespace_iter);
623         ret = bkey_err(k);
624         if (ret)
625                 goto err;
626
627         if (fsck_err_on(k.k->type != freespace_key_type, c,
628                         "incorrect key in freespace btree (got %s should be %s)\n"
629                         "  %s",
630                         bch2_bkey_types[k.k->type],
631                         bch2_bkey_types[freespace_key_type],
632                         (printbuf_reset(&buf),
633                          bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
634                 struct bkey_i *update =
635                         bch2_trans_kmalloc(trans, sizeof(*update));
636
637                 ret = PTR_ERR_OR_ZERO(update);
638                 if (ret)
639                         goto err;
640
641                 bkey_init(&update->k);
642                 update->k.type  = freespace_key_type;
643                 update->k.p     = freespace_iter.pos;
644                 bch2_key_resize(&update->k, 1);
645
646                 ret   = bch2_trans_update(trans, &freespace_iter, update, 0) ?:
647                         bch2_trans_commit(trans, NULL, NULL, 0);
648                 if (ret)
649                         goto err;
650         }
651
652         if (bucket_state(a) == BUCKET_cached) {
653                 k = bch2_btree_iter_peek_slot(&lru_iter);
654                 ret = bkey_err(k);
655                 if (ret)
656                         goto err;
657
658                 if (fsck_err_on(!a.io_time[READ], c,
659                                 "cached bucket with read_time 0\n"
660                                 "  %s",
661                         (printbuf_reset(&buf),
662                          bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
663                     fsck_err_on(k.k->type != KEY_TYPE_lru ||
664                                 le64_to_cpu(bkey_s_c_to_lru(k).v->idx) != alloc_k.k->p.offset, c,
665                                 "incorrect/missing lru entry\n"
666                                 "  %s\n"
667                                 "  %s",
668                                 (printbuf_reset(&buf),
669                                  bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
670                                 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) {
671                         u64 read_time = a.io_time[READ];
672
673                         if (!a.io_time[READ])
674                                 a.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
675
676                         ret   = bch2_lru_change(trans,
677                                                 alloc_k.k->p.inode,
678                                                 alloc_k.k->p.offset,
679                                                 0, &a.io_time[READ]);
680                         if (ret)
681                                 goto err;
682
683                         if (a.io_time[READ] != read_time) {
684                                 struct bkey_i_alloc_v4 *a_mut =
685                                         bch2_alloc_to_v4_mut(trans, alloc_k);
686                                 ret = PTR_ERR_OR_ZERO(a_mut);
687                                 if (ret)
688                                         goto err;
689
690                                 a_mut->v.io_time[READ] = a.io_time[READ];
691                                 ret = bch2_trans_update(trans, alloc_iter,
692                                                         &a_mut->k_i, BTREE_TRIGGER_NORUN);
693                                 if (ret)
694                                         goto err;
695                         }
696
697                         ret = bch2_trans_commit(trans, NULL, NULL, 0);
698                         if (ret)
699                                 goto err;
700                 }
701         }
702 err:
703 fsck_err:
704         bch2_trans_iter_exit(trans, &lru_iter);
705         bch2_trans_iter_exit(trans, &freespace_iter);
706         bch2_trans_iter_exit(trans, &discard_iter);
707         printbuf_exit(&buf2);
708         printbuf_exit(&buf);
709         return ret;
710 }
711
712 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
713 {
714         struct bch_dev *ca;
715
716         if (pos.inode >= c->sb.nr_devices || !c->devs[pos.inode])
717                 return false;
718
719         ca = bch_dev_bkey_exists(c, pos.inode);
720         return pos.offset >= ca->mi.first_bucket &&
721                 pos.offset < ca->mi.nbuckets;
722 }
723
724 static int bch2_check_freespace_key(struct btree_trans *trans,
725                                     struct btree_iter *freespace_iter,
726                                     bool initial)
727 {
728         struct bch_fs *c = trans->c;
729         struct btree_iter alloc_iter;
730         struct bkey_s_c k, freespace_k;
731         struct bch_alloc_v4 a;
732         u64 genbits;
733         struct bpos pos;
734         struct bkey_i *update;
735         struct printbuf buf = PRINTBUF;
736         int ret;
737
738         freespace_k = bch2_btree_iter_peek(freespace_iter);
739         if (!freespace_k.k)
740                 return 1;
741
742         ret = bkey_err(freespace_k);
743         if (ret)
744                 return ret;
745
746         pos = freespace_iter->pos;
747         pos.offset &= ~(~0ULL << 56);
748         genbits = freespace_iter->pos.offset & (~0ULL << 56);
749
750         bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
751
752         if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
753                         "%llu:%llu set in freespace btree but device or bucket does not exist",
754                         pos.inode, pos.offset))
755                 goto delete;
756
757         k = bch2_btree_iter_peek_slot(&alloc_iter);
758         ret = bkey_err(k);
759         if (ret)
760                 goto err;
761
762         bch2_alloc_to_v4(k, &a);
763
764         if (fsck_err_on(bucket_state(a) != BUCKET_free ||
765                         genbits != alloc_freespace_genbits(a), c,
766                         "%s\n  incorrectly set in freespace index (free %u, genbits %llu should be %llu)",
767                         (bch2_bkey_val_to_text(&buf, c, k), buf.buf),
768                         bucket_state(a) == BUCKET_free,
769                         genbits >> 56, alloc_freespace_genbits(a) >> 56))
770                 goto delete;
771 out:
772 err:
773 fsck_err:
774         bch2_trans_iter_exit(trans, &alloc_iter);
775         printbuf_exit(&buf);
776         return ret;
777 delete:
778         update = bch2_trans_kmalloc(trans, sizeof(*update));
779         ret = PTR_ERR_OR_ZERO(update);
780         if (ret)
781                 goto err;
782
783         bkey_init(&update->k);
784         update->k.p = freespace_iter->pos;
785         bch2_key_resize(&update->k, 1);
786
787         ret   = bch2_trans_update(trans, freespace_iter, update, 0) ?:
788                 bch2_trans_commit(trans, NULL, NULL, 0);
789         goto out;
790 }
791
792 int bch2_check_alloc_info(struct bch_fs *c, bool initial)
793 {
794         struct btree_trans trans;
795         struct btree_iter iter;
796         struct bkey_s_c k;
797         int ret = 0, last_dev = -1;
798
799         bch2_trans_init(&trans, c, 0, 0);
800
801         for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
802                            BTREE_ITER_PREFETCH, k, ret) {
803                 if (k.k->p.inode != last_dev) {
804                         struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
805
806                         if (!ca->mi.freespace_initialized) {
807                                 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
808                                 continue;
809                         }
810
811                         last_dev = k.k->p.inode;
812                 }
813
814                 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
815                         bch2_check_alloc_key(&trans, &iter));
816                 if (ret)
817                         break;
818         }
819         bch2_trans_iter_exit(&trans, &iter);
820
821         if (ret)
822                 goto err;
823
824         bch2_trans_iter_init(&trans, &iter, BTREE_ID_freespace, POS_MIN,
825                              BTREE_ITER_PREFETCH);
826         while (1) {
827                 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
828                         bch2_check_freespace_key(&trans, &iter, initial));
829                 if (ret)
830                         break;
831
832                 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
833         }
834         bch2_trans_iter_exit(&trans, &iter);
835 err:
836         bch2_trans_exit(&trans);
837         return ret < 0 ? ret : 0;
838 }
839
840 static int bch2_clear_need_discard(struct btree_trans *trans, struct bpos pos,
841                                    struct bch_dev *ca, bool *discard_done)
842 {
843         struct bch_fs *c = trans->c;
844         struct btree_iter iter;
845         struct bkey_s_c k;
846         struct bkey_i_alloc_v4 *a;
847         struct printbuf buf = PRINTBUF;
848         int ret;
849
850         bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, pos,
851                              BTREE_ITER_CACHED);
852         k = bch2_btree_iter_peek_slot(&iter);
853         ret = bkey_err(k);
854         if (ret)
855                 goto out;
856
857         a = bch2_alloc_to_v4_mut(trans, k);
858         ret = PTR_ERR_OR_ZERO(a);
859         if (ret)
860                 goto out;
861
862         if (BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
863                 a->v.gen++;
864                 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
865                 goto write;
866         }
867
868         BUG_ON(a->v.journal_seq > c->journal.flushed_seq_ondisk);
869
870         if (bch2_fs_inconsistent_on(!BCH_ALLOC_V4_NEED_DISCARD(&a->v), c,
871                         "%s\n  incorrectly set in need_discard btree",
872                         (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
873                 ret = -EIO;
874                 goto out;
875         }
876
877         if (!*discard_done && ca->mi.discard && !c->opts.nochanges) {
878                 /*
879                  * This works without any other locks because this is the only
880                  * thread that removes items from the need_discard tree
881                  */
882                 bch2_trans_unlock(trans);
883                 blkdev_issue_discard(ca->disk_sb.bdev,
884                                      k.k->p.offset * ca->mi.bucket_size,
885                                      ca->mi.bucket_size,
886                                      GFP_KERNEL, 0);
887                 *discard_done = true;
888
889                 ret = bch2_trans_relock(trans) ? 0 : -EINTR;
890                 if (ret)
891                         goto out;
892         }
893
894         SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
895 write:
896         ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
897 out:
898         bch2_trans_iter_exit(trans, &iter);
899         printbuf_exit(&buf);
900         return ret;
901 }
902
903 static void bch2_do_discards_work(struct work_struct *work)
904 {
905         struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
906         struct bch_dev *ca = NULL;
907         struct btree_trans trans;
908         struct btree_iter iter;
909         struct bkey_s_c k;
910         u64 seen = 0, open = 0, need_journal_commit = 0, discarded = 0;
911         int ret;
912
913         bch2_trans_init(&trans, c, 0, 0);
914
915         for_each_btree_key(&trans, iter, BTREE_ID_need_discard,
916                            POS_MIN, 0, k, ret) {
917                 bool discard_done = false;
918
919                 if (ca && k.k->p.inode != ca->dev_idx) {
920                         percpu_ref_put(&ca->io_ref);
921                         ca = NULL;
922                 }
923
924                 if (!ca) {
925                         ca = bch_dev_bkey_exists(c, k.k->p.inode);
926                         if (!percpu_ref_tryget(&ca->io_ref)) {
927                                 ca = NULL;
928                                 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
929                                 continue;
930                         }
931                 }
932
933                 seen++;
934
935                 if (bch2_bucket_is_open_safe(c, k.k->p.inode, k.k->p.offset)) {
936                         open++;
937                         continue;
938                 }
939
940                 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
941                                 c->journal.flushed_seq_ondisk,
942                                 k.k->p.inode, k.k->p.offset)) {
943                         need_journal_commit++;
944                         continue;
945                 }
946
947                 ret = __bch2_trans_do(&trans, NULL, NULL,
948                                       BTREE_INSERT_USE_RESERVE|
949                                       BTREE_INSERT_NOFAIL,
950                                 bch2_clear_need_discard(&trans, k.k->p, ca, &discard_done));
951                 if (ret)
952                         break;
953
954                 discarded++;
955         }
956         bch2_trans_iter_exit(&trans, &iter);
957
958         if (ca)
959                 percpu_ref_put(&ca->io_ref);
960
961         bch2_trans_exit(&trans);
962
963         if (need_journal_commit * 2 > seen)
964                 bch2_journal_flush_async(&c->journal, NULL);
965
966         percpu_ref_put(&c->writes);
967
968         trace_do_discards(c, seen, open, need_journal_commit, discarded, ret);
969 }
970
971 void bch2_do_discards(struct bch_fs *c)
972 {
973         if (percpu_ref_tryget(&c->writes) &&
974             !queue_work(system_long_wq, &c->discard_work))
975                 percpu_ref_put(&c->writes);
976 }
977
978 static int invalidate_one_bucket(struct btree_trans *trans, struct bch_dev *ca)
979 {
980         struct bch_fs *c = trans->c;
981         struct btree_iter lru_iter, alloc_iter = { NULL };
982         struct bkey_s_c k;
983         struct bkey_i_alloc_v4 *a;
984         u64 bucket, idx;
985         int ret;
986
987         bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
988                              POS(ca->dev_idx, 0), 0);
989         k = bch2_btree_iter_peek(&lru_iter);
990         ret = bkey_err(k);
991         if (ret)
992                 goto out;
993
994         if (!k.k || k.k->p.inode != ca->dev_idx)
995                 goto out;
996
997         if (bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_lru, c,
998                                     "non lru key in lru btree"))
999                 goto out;
1000
1001         idx     = k.k->p.offset;
1002         bucket  = le64_to_cpu(bkey_s_c_to_lru(k).v->idx);
1003
1004         a = bch2_trans_start_alloc_update(trans, &alloc_iter,
1005                                           POS(ca->dev_idx, bucket));
1006         ret = PTR_ERR_OR_ZERO(a);
1007         if (ret)
1008                 goto out;
1009
1010         if (bch2_fs_inconsistent_on(idx != alloc_lru_idx(a->v), c,
1011                         "invalidating bucket with wrong lru idx (got %llu should be %llu",
1012                         idx, alloc_lru_idx(a->v)))
1013                 goto out;
1014
1015         SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1016         a->v.gen++;
1017         a->v.data_type          = 0;
1018         a->v.dirty_sectors      = 0;
1019         a->v.cached_sectors     = 0;
1020         a->v.io_time[READ]      = atomic64_read(&c->io_clock[READ].now);
1021         a->v.io_time[WRITE]     = atomic64_read(&c->io_clock[WRITE].now);
1022
1023         ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1024                                 BTREE_TRIGGER_BUCKET_INVALIDATE);
1025 out:
1026         bch2_trans_iter_exit(trans, &alloc_iter);
1027         bch2_trans_iter_exit(trans, &lru_iter);
1028         return ret;
1029 }
1030
1031 static void bch2_do_invalidates_work(struct work_struct *work)
1032 {
1033         struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1034         struct bch_dev *ca;
1035         struct btree_trans trans;
1036         unsigned i;
1037         int ret = 0;
1038
1039         bch2_trans_init(&trans, c, 0, 0);
1040
1041         for_each_member_device(ca, c, i)
1042                 while (!ret && should_invalidate_buckets(ca))
1043                         ret = __bch2_trans_do(&trans, NULL, NULL,
1044                                               BTREE_INSERT_USE_RESERVE|
1045                                               BTREE_INSERT_NOFAIL,
1046                                         invalidate_one_bucket(&trans, ca));
1047
1048         bch2_trans_exit(&trans);
1049         percpu_ref_put(&c->writes);
1050 }
1051
1052 void bch2_do_invalidates(struct bch_fs *c)
1053 {
1054         if (percpu_ref_tryget(&c->writes))
1055                 queue_work(system_long_wq, &c->invalidate_work);
1056 }
1057
1058 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1059 {
1060         struct btree_trans trans;
1061         struct btree_iter iter;
1062         struct bkey_s_c k;
1063         struct bch_alloc_v4 a;
1064         struct bch_member *m;
1065         int ret;
1066
1067         bch2_trans_init(&trans, c, 0, 0);
1068
1069         for_each_btree_key(&trans, iter, BTREE_ID_alloc,
1070                            POS(ca->dev_idx, ca->mi.first_bucket),
1071                            BTREE_ITER_SLOTS|
1072                            BTREE_ITER_PREFETCH, k, ret) {
1073                 if (iter.pos.offset >= ca->mi.nbuckets)
1074                         break;
1075
1076                 bch2_alloc_to_v4(k, &a);
1077                 ret = __bch2_trans_do(&trans, NULL, NULL,
1078                                       BTREE_INSERT_LAZY_RW,
1079                                  bch2_bucket_do_index(&trans, k, a, true));
1080                 if (ret)
1081                         break;
1082         }
1083         bch2_trans_iter_exit(&trans, &iter);
1084
1085         bch2_trans_exit(&trans);
1086
1087         if (ret) {
1088                 bch_err(ca, "error initializing free space: %i", ret);
1089                 return ret;
1090         }
1091
1092         mutex_lock(&c->sb_lock);
1093         m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1094         SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1095         mutex_unlock(&c->sb_lock);
1096
1097         return ret;
1098 }
1099
1100 int bch2_fs_freespace_init(struct bch_fs *c)
1101 {
1102         struct bch_dev *ca;
1103         unsigned i;
1104         int ret = 0;
1105         bool doing_init = false;
1106
1107         /*
1108          * We can crash during the device add path, so we need to check this on
1109          * every mount:
1110          */
1111
1112         for_each_member_device(ca, c, i) {
1113                 if (ca->mi.freespace_initialized)
1114                         continue;
1115
1116                 if (!doing_init) {
1117                         bch_info(c, "initializing freespace");
1118                         doing_init = true;
1119                 }
1120
1121                 ret = bch2_dev_freespace_init(c, ca);
1122                 if (ret) {
1123                         percpu_ref_put(&ca->ref);
1124                         return ret;
1125                 }
1126         }
1127
1128         if (doing_init) {
1129                 mutex_lock(&c->sb_lock);
1130                 bch2_write_super(c);
1131                 mutex_unlock(&c->sb_lock);
1132
1133                 bch_verbose(c, "done initializing freespace");
1134         }
1135
1136         return ret;
1137 }
1138
1139 /* Bucket IO clocks: */
1140
1141 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1142                               size_t bucket_nr, int rw)
1143 {
1144         struct bch_fs *c = trans->c;
1145         struct btree_iter iter;
1146         struct bkey_i_alloc_v4 *a;
1147         u64 now;
1148         int ret = 0;
1149
1150         a = bch2_trans_start_alloc_update(trans, &iter,  POS(dev, bucket_nr));
1151         ret = PTR_ERR_OR_ZERO(a);
1152         if (ret)
1153                 return ret;
1154
1155         now = atomic64_read(&c->io_clock[rw].now);
1156         if (a->v.io_time[rw] == now)
1157                 goto out;
1158
1159         a->v.io_time[rw] = now;
1160
1161         ret   = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1162                 bch2_trans_commit(trans, NULL, NULL, 0);
1163 out:
1164         bch2_trans_iter_exit(trans, &iter);
1165         return ret;
1166 }
1167
1168 /* Startup/shutdown (ro/rw): */
1169
1170 void bch2_recalc_capacity(struct bch_fs *c)
1171 {
1172         struct bch_dev *ca;
1173         u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1174         unsigned bucket_size_max = 0;
1175         unsigned long ra_pages = 0;
1176         unsigned i;
1177
1178         lockdep_assert_held(&c->state_lock);
1179
1180         for_each_online_member(ca, c, i) {
1181                 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1182
1183                 ra_pages += bdi->ra_pages;
1184         }
1185
1186         bch2_set_ra_pages(c, ra_pages);
1187
1188         for_each_rw_member(ca, c, i) {
1189                 u64 dev_reserve = 0;
1190
1191                 /*
1192                  * We need to reserve buckets (from the number
1193                  * of currently available buckets) against
1194                  * foreground writes so that mainly copygc can
1195                  * make forward progress.
1196                  *
1197                  * We need enough to refill the various reserves
1198                  * from scratch - copygc will use its entire
1199                  * reserve all at once, then run against when
1200                  * its reserve is refilled (from the formerly
1201                  * available buckets).
1202                  *
1203                  * This reserve is just used when considering if
1204                  * allocations for foreground writes must wait -
1205                  * not -ENOSPC calculations.
1206                  */
1207
1208                 dev_reserve += ca->nr_btree_reserve * 2;
1209                 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1210
1211                 dev_reserve += 1;       /* btree write point */
1212                 dev_reserve += 1;       /* copygc write point */
1213                 dev_reserve += 1;       /* rebalance write point */
1214
1215                 dev_reserve *= ca->mi.bucket_size;
1216
1217                 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1218                                              ca->mi.first_bucket);
1219
1220                 reserved_sectors += dev_reserve * 2;
1221
1222                 bucket_size_max = max_t(unsigned, bucket_size_max,
1223                                         ca->mi.bucket_size);
1224         }
1225
1226         gc_reserve = c->opts.gc_reserve_bytes
1227                 ? c->opts.gc_reserve_bytes >> 9
1228                 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1229
1230         reserved_sectors = max(gc_reserve, reserved_sectors);
1231
1232         reserved_sectors = min(reserved_sectors, capacity);
1233
1234         c->capacity = capacity - reserved_sectors;
1235
1236         c->bucket_size_max = bucket_size_max;
1237
1238         /* Wake up case someone was waiting for buckets */
1239         closure_wake_up(&c->freelist_wait);
1240 }
1241
1242 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1243 {
1244         struct open_bucket *ob;
1245         bool ret = false;
1246
1247         for (ob = c->open_buckets;
1248              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1249              ob++) {
1250                 spin_lock(&ob->lock);
1251                 if (ob->valid && !ob->on_partial_list &&
1252                     ob->dev == ca->dev_idx)
1253                         ret = true;
1254                 spin_unlock(&ob->lock);
1255         }
1256
1257         return ret;
1258 }
1259
1260 /* device goes ro: */
1261 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1262 {
1263         unsigned i;
1264
1265         /* First, remove device from allocation groups: */
1266
1267         for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1268                 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1269
1270         /*
1271          * Capacity is calculated based off of devices in allocation groups:
1272          */
1273         bch2_recalc_capacity(c);
1274
1275         /* Next, close write points that point to this device... */
1276         for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1277                 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1278
1279         bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1280         bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1281         bch2_writepoint_stop(c, ca, &c->btree_write_point);
1282
1283         mutex_lock(&c->btree_reserve_cache_lock);
1284         while (c->btree_reserve_cache_nr) {
1285                 struct btree_alloc *a =
1286                         &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1287
1288                 bch2_open_buckets_put(c, &a->ob);
1289         }
1290         mutex_unlock(&c->btree_reserve_cache_lock);
1291
1292         while (1) {
1293                 struct open_bucket *ob;
1294
1295                 spin_lock(&c->freelist_lock);
1296                 if (!ca->open_buckets_partial_nr) {
1297                         spin_unlock(&c->freelist_lock);
1298                         break;
1299                 }
1300                 ob = c->open_buckets +
1301                         ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1302                 ob->on_partial_list = false;
1303                 spin_unlock(&c->freelist_lock);
1304
1305                 bch2_open_bucket_put(c, ob);
1306         }
1307
1308         bch2_ec_stop_dev(c, ca);
1309
1310         /*
1311          * Wake up threads that were blocked on allocation, so they can notice
1312          * the device can no longer be removed and the capacity has changed:
1313          */
1314         closure_wake_up(&c->freelist_wait);
1315
1316         /*
1317          * journal_res_get() can block waiting for free space in the journal -
1318          * it needs to notice there may not be devices to allocate from anymore:
1319          */
1320         wake_up(&c->journal.wait);
1321
1322         /* Now wait for any in flight writes: */
1323
1324         closure_wait_event(&c->open_buckets_wait,
1325                            !bch2_dev_has_open_write_point(c, ca));
1326 }
1327
1328 /* device goes rw: */
1329 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1330 {
1331         unsigned i;
1332
1333         for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1334                 if (ca->mi.data_allowed & (1 << i))
1335                         set_bit(ca->dev_idx, c->rw_devs[i].d);
1336 }
1337
1338 void bch2_fs_allocator_background_init(struct bch_fs *c)
1339 {
1340         spin_lock_init(&c->freelist_lock);
1341         INIT_WORK(&c->discard_work, bch2_do_discards_work);
1342         INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);
1343 }