]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.c
Update bcachefs sources to 386f00b639 bcachefs: Snapshot creation, deletion
[bcachefs-tools-debian] / libbcachefs / buckets.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Code for manipulating bucket marks for garbage collection.
4  *
5  * Copyright 2014 Datera, Inc.
6  */
7
8 #include "bcachefs.h"
9 #include "alloc_background.h"
10 #include "bset.h"
11 #include "btree_gc.h"
12 #include "btree_update.h"
13 #include "buckets.h"
14 #include "ec.h"
15 #include "error.h"
16 #include "movinggc.h"
17 #include "reflink.h"
18 #include "replicas.h"
19 #include "subvolume.h"
20
21 #include <linux/preempt.h>
22 #include <trace/events/bcachefs.h>
23
24 static inline void fs_usage_data_type_to_base(struct bch_fs_usage *fs_usage,
25                                               enum bch_data_type data_type,
26                                               s64 sectors)
27 {
28         switch (data_type) {
29         case BCH_DATA_btree:
30                 fs_usage->btree         += sectors;
31                 break;
32         case BCH_DATA_user:
33         case BCH_DATA_parity:
34                 fs_usage->data          += sectors;
35                 break;
36         case BCH_DATA_cached:
37                 fs_usage->cached        += sectors;
38                 break;
39         default:
40                 break;
41         }
42 }
43
44 /*
45  * Clear journal_seq_valid for buckets for which it's not needed, to prevent
46  * wraparound:
47  */
48 void bch2_bucket_seq_cleanup(struct bch_fs *c)
49 {
50         u64 journal_seq = atomic64_read(&c->journal.seq);
51         u16 last_seq_ondisk = c->journal.last_seq_ondisk;
52         struct bch_dev *ca;
53         struct bucket_array *buckets;
54         struct bucket *g;
55         struct bucket_mark m;
56         unsigned i;
57
58         if (journal_seq - c->last_bucket_seq_cleanup <
59             (1U << (BUCKET_JOURNAL_SEQ_BITS - 2)))
60                 return;
61
62         c->last_bucket_seq_cleanup = journal_seq;
63
64         for_each_member_device(ca, c, i) {
65                 down_read(&ca->bucket_lock);
66                 buckets = bucket_array(ca);
67
68                 for_each_bucket(g, buckets) {
69                         bucket_cmpxchg(g, m, ({
70                                 if (!m.journal_seq_valid ||
71                                     bucket_needs_journal_commit(m, last_seq_ondisk))
72                                         break;
73
74                                 m.journal_seq_valid = 0;
75                         }));
76                 }
77                 up_read(&ca->bucket_lock);
78         }
79 }
80
81 void bch2_fs_usage_initialize(struct bch_fs *c)
82 {
83         struct bch_fs_usage *usage;
84         struct bch_dev *ca;
85         unsigned i;
86
87         percpu_down_write(&c->mark_lock);
88         usage = c->usage_base;
89
90         for (i = 0; i < ARRAY_SIZE(c->usage); i++)
91                 bch2_fs_usage_acc_to_base(c, i);
92
93         for (i = 0; i < BCH_REPLICAS_MAX; i++)
94                 usage->reserved += usage->persistent_reserved[i];
95
96         for (i = 0; i < c->replicas.nr; i++) {
97                 struct bch_replicas_entry *e =
98                         cpu_replicas_entry(&c->replicas, i);
99
100                 fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
101         }
102
103         for_each_member_device(ca, c, i) {
104                 struct bch_dev_usage dev = bch2_dev_usage_read(ca);
105
106                 usage->hidden += (dev.d[BCH_DATA_sb].buckets +
107                                   dev.d[BCH_DATA_journal].buckets) *
108                         ca->mi.bucket_size;
109         }
110
111         percpu_up_write(&c->mark_lock);
112 }
113
114 static inline struct bch_dev_usage *dev_usage_ptr(struct bch_dev *ca,
115                                                   unsigned journal_seq,
116                                                   bool gc)
117 {
118         return this_cpu_ptr(gc
119                             ? ca->usage_gc
120                             : ca->usage[journal_seq & JOURNAL_BUF_MASK]);
121 }
122
123 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
124 {
125         struct bch_fs *c = ca->fs;
126         struct bch_dev_usage ret;
127         unsigned seq, i, u64s = dev_usage_u64s();
128
129         do {
130                 seq = read_seqcount_begin(&c->usage_lock);
131                 memcpy(&ret, ca->usage_base, u64s * sizeof(u64));
132                 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
133                         acc_u64s_percpu((u64 *) &ret, (u64 __percpu *) ca->usage[i], u64s);
134         } while (read_seqcount_retry(&c->usage_lock, seq));
135
136         return ret;
137 }
138
139 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
140                                                 unsigned journal_seq,
141                                                 bool gc)
142 {
143         return this_cpu_ptr(gc
144                             ? c->usage_gc
145                             : c->usage[journal_seq & JOURNAL_BUF_MASK]);
146 }
147
148 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
149 {
150         ssize_t offset = v - (u64 *) c->usage_base;
151         unsigned i, seq;
152         u64 ret;
153
154         BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
155         percpu_rwsem_assert_held(&c->mark_lock);
156
157         do {
158                 seq = read_seqcount_begin(&c->usage_lock);
159                 ret = *v;
160
161                 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
162                         ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
163         } while (read_seqcount_retry(&c->usage_lock, seq));
164
165         return ret;
166 }
167
168 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
169 {
170         struct bch_fs_usage_online *ret;
171         unsigned seq, i, u64s;
172
173         percpu_down_read(&c->mark_lock);
174
175         ret = kmalloc(sizeof(struct bch_fs_usage_online) +
176                       sizeof(u64) * c->replicas.nr, GFP_NOFS);
177         if (unlikely(!ret)) {
178                 percpu_up_read(&c->mark_lock);
179                 return NULL;
180         }
181
182         ret->online_reserved = percpu_u64_get(c->online_reserved);
183
184         u64s = fs_usage_u64s(c);
185         do {
186                 seq = read_seqcount_begin(&c->usage_lock);
187                 memcpy(&ret->u, c->usage_base, u64s * sizeof(u64));
188                 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
189                         acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], u64s);
190         } while (read_seqcount_retry(&c->usage_lock, seq));
191
192         return ret;
193 }
194
195 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
196 {
197         struct bch_dev *ca;
198         unsigned i, u64s = fs_usage_u64s(c);
199
200         BUG_ON(idx >= ARRAY_SIZE(c->usage));
201
202         preempt_disable();
203         write_seqcount_begin(&c->usage_lock);
204
205         acc_u64s_percpu((u64 *) c->usage_base,
206                         (u64 __percpu *) c->usage[idx], u64s);
207         percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
208
209         rcu_read_lock();
210         for_each_member_device_rcu(ca, c, i, NULL) {
211                 u64s = dev_usage_u64s();
212
213                 acc_u64s_percpu((u64 *) ca->usage_base,
214                                 (u64 __percpu *) ca->usage[idx], u64s);
215                 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
216         }
217         rcu_read_unlock();
218
219         write_seqcount_end(&c->usage_lock);
220         preempt_enable();
221 }
222
223 void bch2_fs_usage_to_text(struct printbuf *out,
224                            struct bch_fs *c,
225                            struct bch_fs_usage_online *fs_usage)
226 {
227         unsigned i;
228
229         pr_buf(out, "capacity:\t\t\t%llu\n", c->capacity);
230
231         pr_buf(out, "hidden:\t\t\t\t%llu\n",
232                fs_usage->u.hidden);
233         pr_buf(out, "data:\t\t\t\t%llu\n",
234                fs_usage->u.data);
235         pr_buf(out, "cached:\t\t\t\t%llu\n",
236                fs_usage->u.cached);
237         pr_buf(out, "reserved:\t\t\t%llu\n",
238                fs_usage->u.reserved);
239         pr_buf(out, "nr_inodes:\t\t\t%llu\n",
240                fs_usage->u.nr_inodes);
241         pr_buf(out, "online reserved:\t\t%llu\n",
242                fs_usage->online_reserved);
243
244         for (i = 0;
245              i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
246              i++) {
247                 pr_buf(out, "%u replicas:\n", i + 1);
248                 pr_buf(out, "\treserved:\t\t%llu\n",
249                        fs_usage->u.persistent_reserved[i]);
250         }
251
252         for (i = 0; i < c->replicas.nr; i++) {
253                 struct bch_replicas_entry *e =
254                         cpu_replicas_entry(&c->replicas, i);
255
256                 pr_buf(out, "\t");
257                 bch2_replicas_entry_to_text(out, e);
258                 pr_buf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
259         }
260 }
261
262 static u64 reserve_factor(u64 r)
263 {
264         return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
265 }
266
267 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
268 {
269         return min(fs_usage->u.hidden +
270                    fs_usage->u.btree +
271                    fs_usage->u.data +
272                    reserve_factor(fs_usage->u.reserved +
273                                   fs_usage->online_reserved),
274                    c->capacity);
275 }
276
277 static struct bch_fs_usage_short
278 __bch2_fs_usage_read_short(struct bch_fs *c)
279 {
280         struct bch_fs_usage_short ret;
281         u64 data, reserved;
282
283         ret.capacity = c->capacity -
284                 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
285
286         data            = bch2_fs_usage_read_one(c, &c->usage_base->data) +
287                 bch2_fs_usage_read_one(c, &c->usage_base->btree);
288         reserved        = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
289                 percpu_u64_get(c->online_reserved);
290
291         ret.used        = min(ret.capacity, data + reserve_factor(reserved));
292         ret.free        = ret.capacity - ret.used;
293
294         ret.nr_inodes   = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
295
296         return ret;
297 }
298
299 struct bch_fs_usage_short
300 bch2_fs_usage_read_short(struct bch_fs *c)
301 {
302         struct bch_fs_usage_short ret;
303
304         percpu_down_read(&c->mark_lock);
305         ret = __bch2_fs_usage_read_short(c);
306         percpu_up_read(&c->mark_lock);
307
308         return ret;
309 }
310
311 static inline int is_unavailable_bucket(struct bucket_mark m)
312 {
313         return !is_available_bucket(m);
314 }
315
316 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
317                                             struct bucket_mark m)
318 {
319         return bucket_sectors_used(m)
320                 ? max(0, (int) ca->mi.bucket_size - (int) bucket_sectors_used(m))
321                 : 0;
322 }
323
324 static inline int is_stripe_data_bucket(struct bucket_mark m)
325 {
326         return m.stripe && m.data_type != BCH_DATA_parity;
327 }
328
329 static inline enum bch_data_type bucket_type(struct bucket_mark m)
330 {
331         return m.cached_sectors && !m.dirty_sectors
332                 ? BCH_DATA_cached
333                 : m.data_type;
334 }
335
336 static bool bucket_became_unavailable(struct bucket_mark old,
337                                       struct bucket_mark new)
338 {
339         return is_available_bucket(old) &&
340                !is_available_bucket(new);
341 }
342
343 static inline void account_bucket(struct bch_fs_usage *fs_usage,
344                                   struct bch_dev_usage *dev_usage,
345                                   enum bch_data_type type,
346                                   int nr, s64 size)
347 {
348         if (type == BCH_DATA_sb || type == BCH_DATA_journal)
349                 fs_usage->hidden        += size;
350
351         dev_usage->d[type].buckets      += nr;
352 }
353
354 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
355                                   struct bucket_mark old, struct bucket_mark new,
356                                   u64 journal_seq, bool gc)
357 {
358         struct bch_fs_usage *fs_usage;
359         struct bch_dev_usage *u;
360
361         percpu_rwsem_assert_held(&c->mark_lock);
362
363         preempt_disable();
364         fs_usage = fs_usage_ptr(c, journal_seq, gc);
365         u = dev_usage_ptr(ca, journal_seq, gc);
366
367         if (bucket_type(old))
368                 account_bucket(fs_usage, u, bucket_type(old),
369                                -1, -ca->mi.bucket_size);
370
371         if (bucket_type(new))
372                 account_bucket(fs_usage, u, bucket_type(new),
373                                1, ca->mi.bucket_size);
374
375         u->buckets_ec += (int) new.stripe - (int) old.stripe;
376         u->buckets_unavailable +=
377                 is_unavailable_bucket(new) - is_unavailable_bucket(old);
378
379         u->d[old.data_type].sectors -= old.dirty_sectors;
380         u->d[new.data_type].sectors += new.dirty_sectors;
381         u->d[BCH_DATA_cached].sectors +=
382                 (int) new.cached_sectors - (int) old.cached_sectors;
383
384         u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
385         u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
386
387         preempt_enable();
388
389         if (!is_available_bucket(old) && is_available_bucket(new))
390                 bch2_wake_allocator(ca);
391 }
392
393 static inline int __update_replicas(struct bch_fs *c,
394                                     struct bch_fs_usage *fs_usage,
395                                     struct bch_replicas_entry *r,
396                                     s64 sectors)
397 {
398         int idx = bch2_replicas_entry_idx(c, r);
399
400         if (idx < 0)
401                 return -1;
402
403         fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
404         fs_usage->replicas[idx]         += sectors;
405         return 0;
406 }
407
408 static inline int update_replicas(struct bch_fs *c,
409                         struct bch_replicas_entry *r, s64 sectors,
410                         unsigned journal_seq, bool gc)
411 {
412         struct bch_fs_usage __percpu *fs_usage;
413         int idx = bch2_replicas_entry_idx(c, r);
414
415         if (idx < 0)
416                 return -1;
417
418         preempt_disable();
419         fs_usage = fs_usage_ptr(c, journal_seq, gc);
420         fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
421         fs_usage->replicas[idx]         += sectors;
422         preempt_enable();
423         return 0;
424 }
425
426 static inline int update_cached_sectors(struct bch_fs *c,
427                         unsigned dev, s64 sectors,
428                         unsigned journal_seq, bool gc)
429 {
430         struct bch_replicas_padded r;
431
432         bch2_replicas_entry_cached(&r.e, dev);
433
434         return update_replicas(c, &r.e, sectors, journal_seq, gc);
435 }
436
437 static struct replicas_delta_list *
438 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
439 {
440         struct replicas_delta_list *d = trans->fs_usage_deltas;
441         unsigned new_size = d ? (d->size + more) * 2 : 128;
442         unsigned alloc_size = sizeof(*d) + new_size;
443
444         WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
445
446         if (!d || d->used + more > d->size) {
447                 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
448
449                 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
450
451                 if (!d) {
452                         d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
453                         memset(d, 0, REPLICAS_DELTA_LIST_MAX);
454
455                         if (trans->fs_usage_deltas)
456                                 memcpy(d, trans->fs_usage_deltas,
457                                        trans->fs_usage_deltas->size + sizeof(*d));
458
459                         new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
460                         kfree(trans->fs_usage_deltas);
461                 }
462
463                 d->size = new_size;
464                 trans->fs_usage_deltas = d;
465         }
466         return d;
467 }
468
469 static inline void update_replicas_list(struct btree_trans *trans,
470                                         struct bch_replicas_entry *r,
471                                         s64 sectors)
472 {
473         struct replicas_delta_list *d;
474         struct replicas_delta *n;
475         unsigned b;
476
477         if (!sectors)
478                 return;
479
480         b = replicas_entry_bytes(r) + 8;
481         d = replicas_deltas_realloc(trans, b);
482
483         n = (void *) d->d + d->used;
484         n->delta = sectors;
485         memcpy(&n->r, r, replicas_entry_bytes(r));
486         bch2_replicas_entry_sort(&n->r);
487         d->used += b;
488 }
489
490 static inline void update_cached_sectors_list(struct btree_trans *trans,
491                                               unsigned dev, s64 sectors)
492 {
493         struct bch_replicas_padded r;
494
495         bch2_replicas_entry_cached(&r.e, dev);
496
497         update_replicas_list(trans, &r.e, sectors);
498 }
499
500 #define do_mark_fn(fn, c, pos, flags, ...)                              \
501 ({                                                                      \
502         int gc, ret = 0;                                                \
503                                                                         \
504         percpu_rwsem_assert_held(&c->mark_lock);                        \
505                                                                         \
506         for (gc = 0; gc < 2 && !ret; gc++)                              \
507                 if (!gc == !(flags & BTREE_TRIGGER_GC) ||               \
508                     (gc && gc_visited(c, pos)))                         \
509                         ret = fn(c, __VA_ARGS__, gc);                   \
510         ret;                                                            \
511 })
512
513 void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
514                             size_t b, bool owned_by_allocator)
515 {
516         struct bucket *g = bucket(ca, b);
517         struct bucket_mark old, new;
518
519         old = bucket_cmpxchg(g, new, ({
520                 new.owned_by_allocator  = owned_by_allocator;
521         }));
522
523         BUG_ON(owned_by_allocator == old.owned_by_allocator);
524 }
525
526 static int bch2_mark_alloc(struct bch_fs *c,
527                            struct bkey_s_c old, struct bkey_s_c new,
528                            u64 journal_seq, unsigned flags)
529 {
530         bool gc = flags & BTREE_TRIGGER_GC;
531         struct bkey_alloc_unpacked u;
532         struct bch_dev *ca;
533         struct bucket *g;
534         struct bucket_mark old_m, m;
535
536         /* We don't do anything for deletions - do we?: */
537         if (new.k->type != KEY_TYPE_alloc &&
538             new.k->type != KEY_TYPE_alloc_v2)
539                 return 0;
540
541         /*
542          * alloc btree is read in by bch2_alloc_read, not gc:
543          */
544         if ((flags & BTREE_TRIGGER_GC) &&
545             !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
546                 return 0;
547
548         ca = bch_dev_bkey_exists(c, new.k->p.inode);
549
550         if (new.k->p.offset >= ca->mi.nbuckets)
551                 return 0;
552
553         g = __bucket(ca, new.k->p.offset, gc);
554         u = bch2_alloc_unpack(new);
555
556         old_m = bucket_cmpxchg(g, m, ({
557                 m.gen                   = u.gen;
558                 m.data_type             = u.data_type;
559                 m.dirty_sectors         = u.dirty_sectors;
560                 m.cached_sectors        = u.cached_sectors;
561                 m.stripe                = u.stripe != 0;
562
563                 if (journal_seq) {
564                         m.journal_seq_valid     = 1;
565                         m.journal_seq           = journal_seq;
566                 }
567         }));
568
569         bch2_dev_usage_update(c, ca, old_m, m, journal_seq, gc);
570
571         g->io_time[READ]        = u.read_time;
572         g->io_time[WRITE]       = u.write_time;
573         g->oldest_gen           = u.oldest_gen;
574         g->gen_valid            = 1;
575         g->stripe               = u.stripe;
576         g->stripe_redundancy    = u.stripe_redundancy;
577
578         /*
579          * need to know if we're getting called from the invalidate path or
580          * not:
581          */
582
583         if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
584             old_m.cached_sectors) {
585                 if (update_cached_sectors(c, ca->dev_idx, -old_m.cached_sectors,
586                                           journal_seq, gc)) {
587                         bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
588                         return -1;
589                 }
590
591                 trace_invalidate(ca, bucket_to_sector(ca, new.k->p.offset),
592                                  old_m.cached_sectors);
593         }
594
595         return 0;
596 }
597
598 #define checked_add(a, b)                                       \
599 ({                                                              \
600         unsigned _res = (unsigned) (a) + (b);                   \
601         bool overflow = _res > U16_MAX;                         \
602         if (overflow)                                           \
603                 _res = U16_MAX;                                 \
604         (a) = _res;                                             \
605         overflow;                                               \
606 })
607
608 static int __bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
609                                        size_t b, enum bch_data_type data_type,
610                                        unsigned sectors, bool gc)
611 {
612         struct bucket *g = __bucket(ca, b, gc);
613         struct bucket_mark old, new;
614         bool overflow;
615
616         BUG_ON(data_type != BCH_DATA_sb &&
617                data_type != BCH_DATA_journal);
618
619         old = bucket_cmpxchg(g, new, ({
620                 new.data_type   = data_type;
621                 overflow = checked_add(new.dirty_sectors, sectors);
622         }));
623
624         bch2_fs_inconsistent_on(old.data_type &&
625                                 old.data_type != data_type, c,
626                 "different types of data in same bucket: %s, %s",
627                 bch2_data_types[old.data_type],
628                 bch2_data_types[data_type]);
629
630         bch2_fs_inconsistent_on(overflow, c,
631                 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > U16_MAX",
632                 ca->dev_idx, b, new.gen,
633                 bch2_data_types[old.data_type ?: data_type],
634                 old.dirty_sectors, sectors);
635
636         if (c)
637                 bch2_dev_usage_update(c, ca, old, new, 0, gc);
638
639         return 0;
640 }
641
642 void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
643                                size_t b, enum bch_data_type type,
644                                unsigned sectors, struct gc_pos pos,
645                                unsigned flags)
646 {
647         BUG_ON(type != BCH_DATA_sb &&
648                type != BCH_DATA_journal);
649
650         /*
651          * Backup superblock might be past the end of our normal usable space:
652          */
653         if (b >= ca->mi.nbuckets)
654                 return;
655
656         if (likely(c)) {
657                 do_mark_fn(__bch2_mark_metadata_bucket, c, pos, flags,
658                            ca, b, type, sectors);
659         } else {
660                 __bch2_mark_metadata_bucket(c, ca, b, type, sectors, 0);
661         }
662 }
663
664 static s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
665 {
666         EBUG_ON(sectors < 0);
667
668         return p.crc.compression_type &&
669                 p.crc.compression_type != BCH_COMPRESSION_TYPE_incompressible
670                 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
671                                p.crc.uncompressed_size)
672                 : sectors;
673 }
674
675 static int check_bucket_ref(struct bch_fs *c, struct bkey_s_c k,
676                             const struct bch_extent_ptr *ptr,
677                             s64 sectors, enum bch_data_type ptr_data_type,
678                             u8 bucket_gen, u8 bucket_data_type,
679                             u16 dirty_sectors, u16 cached_sectors)
680 {
681         size_t bucket_nr = PTR_BUCKET_NR(bch_dev_bkey_exists(c, ptr->dev), ptr);
682         u16 bucket_sectors = !ptr->cached
683                 ? dirty_sectors
684                 : cached_sectors;
685         char buf[200];
686
687         if (gen_after(ptr->gen, bucket_gen)) {
688                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
689                         "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
690                         "while marking %s",
691                         ptr->dev, bucket_nr, bucket_gen,
692                         bch2_data_types[bucket_data_type ?: ptr_data_type],
693                         ptr->gen,
694                         (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
695                 return -EIO;
696         }
697
698         if (gen_cmp(bucket_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
699                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
700                         "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
701                         "while marking %s",
702                         ptr->dev, bucket_nr, bucket_gen,
703                         bch2_data_types[bucket_data_type ?: ptr_data_type],
704                         ptr->gen,
705                         (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
706                 return -EIO;
707         }
708
709         if (bucket_gen != ptr->gen && !ptr->cached) {
710                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
711                         "bucket %u:%zu gen %u data type %s: stale dirty ptr (gen %u)\n"
712                         "while marking %s",
713                         ptr->dev, bucket_nr, bucket_gen,
714                         bch2_data_types[bucket_data_type ?: ptr_data_type],
715                         ptr->gen,
716                         (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
717                 return -EIO;
718         }
719
720         if (bucket_gen != ptr->gen)
721                 return 1;
722
723         if (bucket_data_type && ptr_data_type &&
724             bucket_data_type != ptr_data_type) {
725                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
726                         "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
727                         "while marking %s",
728                         ptr->dev, bucket_nr, bucket_gen,
729                         bch2_data_types[bucket_data_type],
730                         bch2_data_types[ptr_data_type],
731                         (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
732                 return -EIO;
733         }
734
735         if ((unsigned) (bucket_sectors + sectors) > U16_MAX) {
736                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
737                         "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
738                         "while marking %s",
739                         ptr->dev, bucket_nr, bucket_gen,
740                         bch2_data_types[bucket_data_type ?: ptr_data_type],
741                         bucket_sectors, sectors,
742                         (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
743                 return -EIO;
744         }
745
746         return 0;
747 }
748
749 static int mark_stripe_bucket(struct bch_fs *c, struct bkey_s_c k,
750                              unsigned ptr_idx,
751                              u64 journal_seq, unsigned flags)
752 {
753         const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
754         unsigned nr_data = s->nr_blocks - s->nr_redundant;
755         bool parity = ptr_idx >= nr_data;
756         const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
757         bool gc = flags & BTREE_TRIGGER_GC;
758         struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
759         struct bucket *g = PTR_BUCKET(ca, ptr, gc);
760         struct bucket_mark new, old;
761         char buf[200];
762         int ret;
763
764         if (g->stripe && g->stripe != k.k->p.offset) {
765                 bch2_fs_inconsistent(c,
766                               "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
767                               ptr->dev, PTR_BUCKET_NR(ca, ptr), g->mark.gen,
768                               (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
769                 return -EINVAL;
770         }
771
772         old = bucket_cmpxchg(g, new, ({
773                 ret = check_bucket_ref(c, k, ptr, 0, 0, new.gen, new.data_type,
774                                        new.dirty_sectors, new.cached_sectors);
775                 if (ret)
776                         return ret;
777
778                 if (parity) {
779                         new.data_type           = BCH_DATA_parity;
780                         new.dirty_sectors       = le16_to_cpu(s->sectors);
781                 }
782
783                 if (journal_seq) {
784                         new.journal_seq_valid   = 1;
785                         new.journal_seq         = journal_seq;
786                 }
787         }));
788
789         g->stripe               = k.k->p.offset;
790         g->stripe_redundancy    = s->nr_redundant;
791
792         bch2_dev_usage_update(c, ca, old, new, journal_seq, gc);
793         return 0;
794 }
795
796 static int __mark_pointer(struct bch_fs *c, struct bkey_s_c k,
797                           const struct bch_extent_ptr *ptr,
798                           s64 sectors, enum bch_data_type ptr_data_type,
799                           u8 bucket_gen, u8 *bucket_data_type,
800                           u16 *dirty_sectors, u16 *cached_sectors)
801 {
802         u16 *dst_sectors = !ptr->cached
803                 ? dirty_sectors
804                 : cached_sectors;
805         int ret = check_bucket_ref(c, k, ptr, sectors, ptr_data_type,
806                                    bucket_gen, *bucket_data_type,
807                                    *dirty_sectors, *cached_sectors);
808
809         if (ret)
810                 return ret;
811
812         *dst_sectors += sectors;
813         *bucket_data_type = *dirty_sectors || *cached_sectors
814                 ? ptr_data_type : 0;
815         return 0;
816 }
817
818 static int bch2_mark_pointer(struct bch_fs *c, struct bkey_s_c k,
819                              struct extent_ptr_decoded p,
820                              s64 sectors, enum bch_data_type data_type,
821                              u64 journal_seq, unsigned flags)
822 {
823         bool gc = flags & BTREE_TRIGGER_GC;
824         struct bucket_mark old, new;
825         struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
826         struct bucket *g = PTR_BUCKET(ca, &p.ptr, gc);
827         u8 bucket_data_type;
828         u64 v;
829         int ret;
830
831         v = atomic64_read(&g->_mark.v);
832         do {
833                 new.v.counter = old.v.counter = v;
834                 bucket_data_type = new.data_type;
835
836                 ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, new.gen,
837                                      &bucket_data_type,
838                                      &new.dirty_sectors,
839                                      &new.cached_sectors);
840                 if (ret)
841                         return ret;
842
843                 new.data_type = bucket_data_type;
844
845                 if (journal_seq) {
846                         new.journal_seq_valid = 1;
847                         new.journal_seq = journal_seq;
848                 }
849
850                 if (flags & BTREE_TRIGGER_NOATOMIC) {
851                         g->_mark = new;
852                         break;
853                 }
854         } while ((v = atomic64_cmpxchg(&g->_mark.v,
855                               old.v.counter,
856                               new.v.counter)) != old.v.counter);
857
858         bch2_dev_usage_update(c, ca, old, new, journal_seq, gc);
859
860         BUG_ON(!gc && bucket_became_unavailable(old, new));
861
862         return 0;
863 }
864
865 static int bch2_mark_stripe_ptr(struct bch_fs *c,
866                                 struct bch_extent_stripe_ptr p,
867                                 enum bch_data_type data_type,
868                                 s64 sectors,
869                                 unsigned journal_seq, unsigned flags)
870 {
871         bool gc = flags & BTREE_TRIGGER_GC;
872         struct bch_replicas_padded r;
873         struct stripe *m;
874         unsigned i, blocks_nonempty = 0;
875
876         m = genradix_ptr(&c->stripes[gc], p.idx);
877
878         spin_lock(&c->ec_stripes_heap_lock);
879
880         if (!m || !m->alive) {
881                 spin_unlock(&c->ec_stripes_heap_lock);
882                 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
883                                     (u64) p.idx);
884                 bch2_inconsistent_error(c);
885                 return -EIO;
886         }
887
888         m->block_sectors[p.block] += sectors;
889
890         r = m->r;
891
892         for (i = 0; i < m->nr_blocks; i++)
893                 blocks_nonempty += m->block_sectors[i] != 0;
894
895         if (m->blocks_nonempty != blocks_nonempty) {
896                 m->blocks_nonempty = blocks_nonempty;
897                 if (!gc)
898                         bch2_stripes_heap_update(c, m, p.idx);
899         }
900
901         spin_unlock(&c->ec_stripes_heap_lock);
902
903         r.e.data_type = data_type;
904         update_replicas(c, &r.e, sectors, journal_seq, gc);
905
906         return 0;
907 }
908
909 static int bch2_mark_extent(struct bch_fs *c,
910                             struct bkey_s_c old, struct bkey_s_c new,
911                             unsigned journal_seq, unsigned flags)
912 {
913         bool gc = flags & BTREE_TRIGGER_GC;
914         struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
915         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
916         const union bch_extent_entry *entry;
917         struct extent_ptr_decoded p;
918         struct bch_replicas_padded r;
919         enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
920                 ? BCH_DATA_btree
921                 : BCH_DATA_user;
922         s64 sectors = bkey_is_btree_ptr(k.k)
923                 ? c->opts.btree_node_size
924                 : k.k->size;
925         s64 dirty_sectors = 0;
926         bool stale;
927         int ret;
928
929         BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
930                (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
931
932         r.e.data_type   = data_type;
933         r.e.nr_devs     = 0;
934         r.e.nr_required = 1;
935
936         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
937                 s64 disk_sectors = ptr_disk_sectors(sectors, p);
938
939                 if (flags & BTREE_TRIGGER_OVERWRITE)
940                         disk_sectors = -disk_sectors;
941
942                 ret = bch2_mark_pointer(c, k, p, disk_sectors, data_type,
943                                         journal_seq, flags);
944                 if (ret < 0)
945                         return ret;
946
947                 stale = ret > 0;
948
949                 if (p.ptr.cached) {
950                         if (!stale)
951                                 if (update_cached_sectors(c, p.ptr.dev, disk_sectors,
952                                                           journal_seq, gc)) {
953                                         bch2_fs_fatal_error(c, "bch2_mark_extent(): no replicas entry while updating cached sectors");
954                                         return -1;
955
956                                 }
957                 } else if (!p.has_ec) {
958                         dirty_sectors          += disk_sectors;
959                         r.e.devs[r.e.nr_devs++] = p.ptr.dev;
960                 } else {
961                         ret = bch2_mark_stripe_ptr(c, p.ec, data_type,
962                                         disk_sectors, journal_seq, flags);
963                         if (ret)
964                                 return ret;
965
966                         /*
967                          * There may be other dirty pointers in this extent, but
968                          * if so they're not required for mounting if we have an
969                          * erasure coded pointer in this extent:
970                          */
971                         r.e.nr_required = 0;
972                 }
973         }
974
975         if (r.e.nr_devs) {
976                 if (update_replicas(c, &r.e, dirty_sectors, journal_seq, gc)) {
977                         char buf[200];
978
979                         bch2_bkey_val_to_text(&PBUF(buf), c, k);
980                         bch2_fs_fatal_error(c, "no replicas entry for %s", buf);
981                         return -1;
982                 }
983         }
984
985         return 0;
986 }
987
988 static int bch2_mark_stripe(struct bch_fs *c,
989                         struct bkey_s_c old, struct bkey_s_c new,
990                         u64 journal_seq, unsigned flags)
991 {
992         bool gc = flags & BTREE_TRIGGER_GC;
993         size_t idx = new.k->p.offset;
994         const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
995                 ? bkey_s_c_to_stripe(old).v : NULL;
996         const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
997                 ? bkey_s_c_to_stripe(new).v : NULL;
998         struct stripe *m = genradix_ptr(&c->stripes[gc], idx);
999         unsigned i;
1000         int ret;
1001
1002         BUG_ON(gc && old_s);
1003
1004         if (!m || (old_s && !m->alive)) {
1005                 bch_err_ratelimited(c, "error marking nonexistent stripe %zu",
1006                                     idx);
1007                 bch2_inconsistent_error(c);
1008                 return -1;
1009         }
1010
1011         if (!new_s) {
1012                 spin_lock(&c->ec_stripes_heap_lock);
1013                 bch2_stripes_heap_del(c, m, idx);
1014                 spin_unlock(&c->ec_stripes_heap_lock);
1015
1016                 memset(m, 0, sizeof(*m));
1017         } else {
1018                 m->alive        = true;
1019                 m->sectors      = le16_to_cpu(new_s->sectors);
1020                 m->algorithm    = new_s->algorithm;
1021                 m->nr_blocks    = new_s->nr_blocks;
1022                 m->nr_redundant = new_s->nr_redundant;
1023                 m->blocks_nonempty = 0;
1024
1025                 for (i = 0; i < new_s->nr_blocks; i++) {
1026                         m->block_sectors[i] =
1027                                 stripe_blockcount_get(new_s, i);
1028                         m->blocks_nonempty += !!m->block_sectors[i];
1029
1030                         m->ptrs[i] = new_s->ptrs[i];
1031                 }
1032
1033                 bch2_bkey_to_replicas(&m->r.e, new);
1034
1035                 if (!gc) {
1036                         spin_lock(&c->ec_stripes_heap_lock);
1037                         bch2_stripes_heap_update(c, m, idx);
1038                         spin_unlock(&c->ec_stripes_heap_lock);
1039                 }
1040         }
1041
1042         if (gc) {
1043                 /*
1044                  * gc recalculates this field from stripe ptr
1045                  * references:
1046                  */
1047                 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1048                 m->blocks_nonempty = 0;
1049
1050                 for (i = 0; i < new_s->nr_blocks; i++) {
1051                         ret = mark_stripe_bucket(c, new, i, journal_seq, flags);
1052                         if (ret)
1053                                 return ret;
1054                 }
1055
1056                 if (update_replicas(c, &m->r.e,
1057                                     ((s64) m->sectors * m->nr_redundant),
1058                                     journal_seq, gc)) {
1059                         char buf[200];
1060
1061                         bch2_bkey_val_to_text(&PBUF(buf), c, new);
1062                         bch2_fs_fatal_error(c, "no replicas entry for %s", buf);
1063                         return -1;
1064                 }
1065         }
1066
1067         return 0;
1068 }
1069
1070 static int bch2_mark_inode(struct bch_fs *c,
1071                         struct bkey_s_c old, struct bkey_s_c new,
1072                         u64 journal_seq, unsigned flags)
1073 {
1074         struct bch_fs_usage __percpu *fs_usage;
1075
1076         preempt_disable();
1077         fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1078         fs_usage->nr_inodes += new.k->type == KEY_TYPE_inode;
1079         fs_usage->nr_inodes -= old.k->type == KEY_TYPE_inode;
1080         preempt_enable();
1081         return 0;
1082 }
1083
1084 static int bch2_mark_reservation(struct bch_fs *c,
1085                         struct bkey_s_c old, struct bkey_s_c new,
1086                         u64 journal_seq, unsigned flags)
1087 {
1088         struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1089         struct bch_fs_usage __percpu *fs_usage;
1090         unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1091         s64 sectors = (s64) k.k->size;
1092
1093         if (flags & BTREE_TRIGGER_OVERWRITE)
1094                 sectors = -sectors;
1095         sectors *= replicas;
1096
1097         preempt_disable();
1098         fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1099         replicas = clamp_t(unsigned, replicas, 1,
1100                            ARRAY_SIZE(fs_usage->persistent_reserved));
1101
1102         fs_usage->reserved                              += sectors;
1103         fs_usage->persistent_reserved[replicas - 1]     += sectors;
1104         preempt_enable();
1105
1106         return 0;
1107 }
1108
1109 static s64 __bch2_mark_reflink_p(struct bch_fs *c, struct bkey_s_c_reflink_p p,
1110                                  u64 idx, unsigned flags, size_t *r_idx)
1111 {
1112         struct reflink_gc *r;
1113         int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1114
1115         while (1) {
1116                 if (*r_idx >= c->reflink_gc_nr)
1117                         goto not_found;
1118                 r = genradix_ptr(&c->reflink_gc_table, *r_idx);
1119                 BUG_ON(!r);
1120
1121                 if (idx < r->offset)
1122                         break;
1123                 (*r_idx)++;
1124         }
1125
1126         BUG_ON((s64) r->refcount + add < 0);
1127
1128         r->refcount += add;
1129         return r->offset - idx;
1130 not_found:
1131         bch2_fs_inconsistent(c,
1132                 "%llu:%llu len %u points to nonexistent indirect extent %llu",
1133                 p.k->p.inode, p.k->p.offset, p.k->size, idx);
1134         bch2_inconsistent_error(c);
1135         return -EIO;
1136 }
1137
1138 static int bch2_mark_reflink_p(struct bch_fs *c,
1139                         struct bkey_s_c old, struct bkey_s_c new,
1140                         u64 journal_seq, unsigned flags)
1141 {
1142         struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1143         struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1144         struct reflink_gc *ref;
1145         size_t l, r, m;
1146         u64 idx = le64_to_cpu(p.v->idx);
1147         unsigned sectors = p.k->size;
1148         s64 ret = 0;
1149
1150         BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
1151                (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
1152
1153         l = 0;
1154         r = c->reflink_gc_nr;
1155         while (l < r) {
1156                 m = l + (r - l) / 2;
1157
1158                 ref = genradix_ptr(&c->reflink_gc_table, m);
1159                 if (ref->offset <= idx)
1160                         l = m + 1;
1161                 else
1162                         r = m;
1163         }
1164
1165         while (sectors) {
1166                 ret = __bch2_mark_reflink_p(c, p, idx, flags, &l);
1167                 if (ret < 0)
1168                         return ret;
1169
1170                 ret = min_t(s64, ret, sectors);
1171                 idx     += ret;
1172                 sectors -= ret;
1173         }
1174
1175         return 0;
1176 }
1177
1178 static int bch2_mark_key_locked(struct bch_fs *c,
1179                    struct bkey_s_c old,
1180                    struct bkey_s_c new,
1181                    u64 journal_seq, unsigned flags)
1182 {
1183         struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1184
1185         BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
1186
1187         switch (k.k->type) {
1188         case KEY_TYPE_alloc:
1189         case KEY_TYPE_alloc_v2:
1190                 return bch2_mark_alloc(c, old, new, journal_seq, flags);
1191         case KEY_TYPE_btree_ptr:
1192         case KEY_TYPE_btree_ptr_v2:
1193         case KEY_TYPE_extent:
1194         case KEY_TYPE_reflink_v:
1195                 return bch2_mark_extent(c, old, new, journal_seq, flags);
1196         case KEY_TYPE_stripe:
1197                 return bch2_mark_stripe(c, old, new, journal_seq, flags);
1198         case KEY_TYPE_inode:
1199                 return bch2_mark_inode(c, old, new, journal_seq, flags);
1200         case KEY_TYPE_reservation:
1201                 return bch2_mark_reservation(c, old, new, journal_seq, flags);
1202         case KEY_TYPE_reflink_p:
1203                 return bch2_mark_reflink_p(c, old, new, journal_seq, flags);
1204         case KEY_TYPE_snapshot:
1205                 return bch2_mark_snapshot(c, old, new, journal_seq, flags);
1206         default:
1207                 return 0;
1208         }
1209 }
1210
1211 int bch2_mark_key(struct bch_fs *c, struct bkey_s_c new, unsigned flags)
1212 {
1213         struct bkey deleted = KEY(0, 0, 0);
1214         struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
1215         int ret;
1216
1217         percpu_down_read(&c->mark_lock);
1218         ret = bch2_mark_key_locked(c, old, new, 0, flags);
1219         percpu_up_read(&c->mark_lock);
1220
1221         return ret;
1222 }
1223
1224 int bch2_mark_update(struct btree_trans *trans, struct btree_path *path,
1225                      struct bkey_i *new, unsigned flags)
1226 {
1227         struct bch_fs           *c = trans->c;
1228         struct bkey             _deleted = KEY(0, 0, 0);
1229         struct bkey_s_c         deleted = (struct bkey_s_c) { &_deleted, NULL };
1230         struct bkey_s_c         old;
1231         struct bkey             unpacked;
1232         int ret;
1233
1234         if (unlikely(flags & BTREE_TRIGGER_NORUN))
1235                 return 0;
1236
1237         if (!btree_node_type_needs_gc(path->btree_id))
1238                 return 0;
1239
1240         old = bch2_btree_path_peek_slot(path, &unpacked);
1241
1242         if (old.k->type == new->k.type &&
1243             ((1U << old.k->type) & BTREE_TRIGGER_WANTS_OLD_AND_NEW)) {
1244                 ret   = bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
1245                                 trans->journal_res.seq,
1246                                 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
1247         } else {
1248                 ret   = bch2_mark_key_locked(c, deleted, bkey_i_to_s_c(new),
1249                                 trans->journal_res.seq,
1250                                 BTREE_TRIGGER_INSERT|flags) ?:
1251                         bch2_mark_key_locked(c, old, deleted,
1252                                 trans->journal_res.seq,
1253                                 BTREE_TRIGGER_OVERWRITE|flags);
1254         }
1255
1256         return ret;
1257 }
1258
1259 static noinline __cold
1260 void fs_usage_apply_warn(struct btree_trans *trans,
1261                          unsigned disk_res_sectors,
1262                          s64 should_not_have_added)
1263 {
1264         struct bch_fs *c = trans->c;
1265         struct btree_insert_entry *i;
1266         char buf[200];
1267
1268         bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1269                 should_not_have_added, disk_res_sectors);
1270
1271         trans_for_each_update(trans, i) {
1272                 pr_err("while inserting");
1273                 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(i->k));
1274                 pr_err("%s", buf);
1275                 pr_err("overlapping with");
1276
1277                 if (!i->cached) {
1278                         struct bkey u;
1279                         struct bkey_s_c k = bch2_btree_path_peek_slot(i->path, &u);
1280
1281                         bch2_bkey_val_to_text(&PBUF(buf), c, k);
1282                         pr_err("%s", buf);
1283                 } else {
1284                         struct bkey_cached *ck = (void *) i->path->l[0].b;
1285
1286                         if (ck->valid) {
1287                                 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(ck->k));
1288                                 pr_err("%s", buf);
1289                         }
1290                 }
1291         }
1292         __WARN();
1293 }
1294
1295 void bch2_trans_fs_usage_apply(struct btree_trans *trans,
1296                                struct replicas_delta_list *deltas)
1297 {
1298         struct bch_fs *c = trans->c;
1299         static int warned_disk_usage = 0;
1300         bool warn = false;
1301         unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1302         struct replicas_delta *d = deltas->d;
1303         struct replicas_delta *top = (void *) deltas->d + deltas->used;
1304         struct bch_fs_usage *dst;
1305         s64 added = 0, should_not_have_added;
1306         unsigned i;
1307
1308         percpu_rwsem_assert_held(&c->mark_lock);
1309
1310         preempt_disable();
1311         dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1312
1313         for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1314                 switch (d->r.data_type) {
1315                 case BCH_DATA_btree:
1316                 case BCH_DATA_user:
1317                 case BCH_DATA_parity:
1318                         added += d->delta;
1319                 }
1320
1321                 BUG_ON(__update_replicas(c, dst, &d->r, d->delta));
1322         }
1323
1324         dst->nr_inodes += deltas->nr_inodes;
1325
1326         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1327                 added                           += deltas->persistent_reserved[i];
1328                 dst->reserved                   += deltas->persistent_reserved[i];
1329                 dst->persistent_reserved[i]     += deltas->persistent_reserved[i];
1330         }
1331
1332         /*
1333          * Not allowed to reduce sectors_available except by getting a
1334          * reservation:
1335          */
1336         should_not_have_added = added - (s64) disk_res_sectors;
1337         if (unlikely(should_not_have_added > 0)) {
1338                 u64 old, new, v = atomic64_read(&c->sectors_available);
1339
1340                 do {
1341                         old = v;
1342                         new = max_t(s64, 0, old - should_not_have_added);
1343                 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1344                                                old, new)) != old);
1345
1346                 added -= should_not_have_added;
1347                 warn = true;
1348         }
1349
1350         if (added > 0) {
1351                 trans->disk_res->sectors -= added;
1352                 this_cpu_sub(*c->online_reserved, added);
1353         }
1354
1355         preempt_enable();
1356
1357         if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1358                 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1359 }
1360
1361 /* trans_mark: */
1362
1363 static struct bkey_alloc_buf *
1364 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
1365                               const struct bch_extent_ptr *ptr,
1366                               struct bkey_alloc_unpacked *u)
1367 {
1368         struct bch_fs *c = trans->c;
1369         struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1370         struct bpos pos = POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
1371         struct bucket *g;
1372         struct bkey_alloc_buf *a;
1373         struct bkey_i *update = btree_trans_peek_updates(trans, BTREE_ID_alloc, pos);
1374         int ret;
1375
1376         a = bch2_trans_kmalloc(trans, sizeof(struct bkey_alloc_buf));
1377         if (IS_ERR(a))
1378                 return a;
1379
1380         bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
1381                              BTREE_ITER_CACHED|
1382                              BTREE_ITER_CACHED_NOFILL|
1383                              BTREE_ITER_INTENT);
1384         ret = bch2_btree_iter_traverse(iter);
1385         if (ret) {
1386                 bch2_trans_iter_exit(trans, iter);
1387                 return ERR_PTR(ret);
1388         }
1389
1390         if (update && !bpos_cmp(update->k.p, pos)) {
1391                 *u = bch2_alloc_unpack(bkey_i_to_s_c(update));
1392         } else {
1393                 percpu_down_read(&c->mark_lock);
1394                 g = bucket(ca, pos.offset);
1395                 *u = alloc_mem_to_key(iter, g, READ_ONCE(g->mark));
1396                 percpu_up_read(&c->mark_lock);
1397         }
1398
1399         return a;
1400 }
1401
1402 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1403                         struct bkey_s_c k, struct extent_ptr_decoded p,
1404                         s64 sectors, enum bch_data_type data_type)
1405 {
1406         struct bch_fs *c = trans->c;
1407         struct btree_iter iter;
1408         struct bkey_alloc_unpacked u;
1409         struct bkey_alloc_buf *a;
1410         int ret;
1411
1412         a = bch2_trans_start_alloc_update(trans, &iter, &p.ptr, &u);
1413         if (IS_ERR(a))
1414                 return PTR_ERR(a);
1415
1416         ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, u.gen, &u.data_type,
1417                              &u.dirty_sectors, &u.cached_sectors);
1418         if (ret)
1419                 goto out;
1420
1421         bch2_alloc_pack(c, a, u);
1422         bch2_trans_update(trans, &iter, &a->k, 0);
1423 out:
1424         bch2_trans_iter_exit(trans, &iter);
1425         return ret;
1426 }
1427
1428 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1429                         struct extent_ptr_decoded p,
1430                         s64 sectors, enum bch_data_type data_type)
1431 {
1432         struct bch_fs *c = trans->c;
1433         struct btree_iter iter;
1434         struct bkey_s_c k;
1435         struct bkey_i_stripe *s;
1436         struct bch_replicas_padded r;
1437         int ret = 0;
1438
1439         bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, p.ec.idx),
1440                              BTREE_ITER_INTENT|
1441                              BTREE_ITER_WITH_UPDATES);
1442         k = bch2_btree_iter_peek_slot(&iter);
1443         ret = bkey_err(k);
1444         if (ret)
1445                 goto err;
1446
1447         if (k.k->type != KEY_TYPE_stripe) {
1448                 bch2_fs_inconsistent(c,
1449                         "pointer to nonexistent stripe %llu",
1450                         (u64) p.ec.idx);
1451                 bch2_inconsistent_error(c);
1452                 ret = -EIO;
1453                 goto err;
1454         }
1455
1456         if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1457                 bch2_fs_inconsistent(c,
1458                         "stripe pointer doesn't match stripe %llu",
1459                         (u64) p.ec.idx);
1460                 ret = -EIO;
1461                 goto err;
1462         }
1463
1464         s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1465         ret = PTR_ERR_OR_ZERO(s);
1466         if (ret)
1467                 goto err;
1468
1469         bkey_reassemble(&s->k_i, k);
1470         stripe_blockcount_set(&s->v, p.ec.block,
1471                 stripe_blockcount_get(&s->v, p.ec.block) +
1472                 sectors);
1473         bch2_trans_update(trans, &iter, &s->k_i, 0);
1474
1475         bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1476         r.e.data_type = data_type;
1477         update_replicas_list(trans, &r.e, sectors);
1478 err:
1479         bch2_trans_iter_exit(trans, &iter);
1480         return ret;
1481 }
1482
1483 static int bch2_trans_mark_extent(struct btree_trans *trans,
1484                         struct bkey_s_c k, unsigned flags)
1485 {
1486         struct bch_fs *c = trans->c;
1487         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1488         const union bch_extent_entry *entry;
1489         struct extent_ptr_decoded p;
1490         struct bch_replicas_padded r;
1491         enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1492                 ? BCH_DATA_btree
1493                 : BCH_DATA_user;
1494         s64 sectors = bkey_is_btree_ptr(k.k)
1495                 ? c->opts.btree_node_size
1496                 : k.k->size;
1497         s64 dirty_sectors = 0;
1498         bool stale;
1499         int ret;
1500
1501         BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
1502                (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
1503
1504         r.e.data_type   = data_type;
1505         r.e.nr_devs     = 0;
1506         r.e.nr_required = 1;
1507
1508         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1509                 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1510
1511                 if (flags & BTREE_TRIGGER_OVERWRITE)
1512                         disk_sectors = -disk_sectors;
1513
1514                 ret = bch2_trans_mark_pointer(trans, k, p,
1515                                         disk_sectors, data_type);
1516                 if (ret < 0)
1517                         return ret;
1518
1519                 stale = ret > 0;
1520
1521                 if (p.ptr.cached) {
1522                         if (!stale)
1523                                 update_cached_sectors_list(trans, p.ptr.dev,
1524                                                            disk_sectors);
1525                 } else if (!p.has_ec) {
1526                         dirty_sectors          += disk_sectors;
1527                         r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1528                 } else {
1529                         ret = bch2_trans_mark_stripe_ptr(trans, p,
1530                                         disk_sectors, data_type);
1531                         if (ret)
1532                                 return ret;
1533
1534                         r.e.nr_required = 0;
1535                 }
1536         }
1537
1538         if (r.e.nr_devs)
1539                 update_replicas_list(trans, &r.e, dirty_sectors);
1540
1541         return 0;
1542 }
1543
1544 static int bch2_trans_mark_stripe_alloc_ref(struct btree_trans *trans,
1545                                             struct bkey_s_c_stripe s,
1546                                             unsigned idx, bool deleting)
1547 {
1548         struct bch_fs *c = trans->c;
1549         const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1550         struct bkey_alloc_buf *a;
1551         struct btree_iter iter;
1552         struct bkey_alloc_unpacked u;
1553         bool parity = idx >= s.v->nr_blocks - s.v->nr_redundant;
1554         int ret = 0;
1555
1556         a = bch2_trans_start_alloc_update(trans, &iter, ptr, &u);
1557         if (IS_ERR(a))
1558                 return PTR_ERR(a);
1559
1560         if (parity) {
1561                 s64 sectors = le16_to_cpu(s.v->sectors);
1562
1563                 if (deleting)
1564                         sectors = -sectors;
1565
1566                 u.dirty_sectors += sectors;
1567                 u.data_type = u.dirty_sectors
1568                         ? BCH_DATA_parity
1569                         : 0;
1570         }
1571
1572         if (!deleting) {
1573                 if (bch2_fs_inconsistent_on(u.stripe && u.stripe != s.k->p.offset, c,
1574                                 "bucket %llu:%llu gen %u: multiple stripes using same bucket (%u, %llu)",
1575                                 iter.pos.inode, iter.pos.offset, u.gen,
1576                                 u.stripe, s.k->p.offset)) {
1577                         ret = -EIO;
1578                         goto err;
1579                 }
1580
1581                 u.stripe                = s.k->p.offset;
1582                 u.stripe_redundancy     = s.v->nr_redundant;
1583         } else {
1584                 u.stripe                = 0;
1585                 u.stripe_redundancy     = 0;
1586         }
1587
1588         bch2_alloc_pack(c, a, u);
1589         bch2_trans_update(trans, &iter, &a->k, 0);
1590 err:
1591         bch2_trans_iter_exit(trans, &iter);
1592         return ret;
1593 }
1594
1595 static int bch2_trans_mark_stripe(struct btree_trans *trans,
1596                                   struct bkey_s_c old, struct bkey_s_c new,
1597                                   unsigned flags)
1598 {
1599         struct bkey_s_c_stripe old_s = { .k = NULL };
1600         struct bkey_s_c_stripe new_s = { .k = NULL };
1601         struct bch_replicas_padded r;
1602         unsigned i;
1603         int ret = 0;
1604
1605         if (old.k->type == KEY_TYPE_stripe)
1606                 old_s = bkey_s_c_to_stripe(old);
1607         if (new.k->type == KEY_TYPE_stripe)
1608                 new_s = bkey_s_c_to_stripe(new);
1609
1610         /*
1611          * If the pointers aren't changing, we don't need to do anything:
1612          */
1613         if (new_s.k && old_s.k &&
1614             new_s.v->nr_blocks          == old_s.v->nr_blocks &&
1615             new_s.v->nr_redundant       == old_s.v->nr_redundant &&
1616             !memcmp(old_s.v->ptrs, new_s.v->ptrs,
1617                     new_s.v->nr_blocks * sizeof(struct bch_extent_ptr)))
1618                 return 0;
1619
1620         if (new_s.k) {
1621                 s64 sectors = le16_to_cpu(new_s.v->sectors);
1622
1623                 bch2_bkey_to_replicas(&r.e, new);
1624                 update_replicas_list(trans, &r.e, sectors * new_s.v->nr_redundant);
1625
1626                 for (i = 0; i < new_s.v->nr_blocks; i++) {
1627                         ret = bch2_trans_mark_stripe_alloc_ref(trans, new_s,
1628                                                                i, false);
1629                         if (ret)
1630                                 return ret;
1631                 }
1632         }
1633
1634         if (old_s.k) {
1635                 s64 sectors = -((s64) le16_to_cpu(old_s.v->sectors));
1636
1637                 bch2_bkey_to_replicas(&r.e, old);
1638                 update_replicas_list(trans, &r.e, sectors * old_s.v->nr_redundant);
1639
1640                 for (i = 0; i < old_s.v->nr_blocks; i++) {
1641                         ret = bch2_trans_mark_stripe_alloc_ref(trans, old_s,
1642                                                                i, true);
1643                         if (ret)
1644                                 return ret;
1645                 }
1646         }
1647
1648         return ret;
1649 }
1650
1651 static int bch2_trans_mark_inode(struct btree_trans *trans,
1652                                  struct bkey_s_c old,
1653                                  struct bkey_s_c new,
1654                                  unsigned flags)
1655 {
1656         int nr = (new.k->type == KEY_TYPE_inode) -
1657                 (old.k->type == KEY_TYPE_inode);
1658
1659         if (nr) {
1660                 struct replicas_delta_list *d =
1661                         replicas_deltas_realloc(trans, 0);
1662                 d->nr_inodes += nr;
1663         }
1664
1665         return 0;
1666 }
1667
1668 static int bch2_trans_mark_reservation(struct btree_trans *trans,
1669                                        struct bkey_s_c k, unsigned flags)
1670 {
1671         unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1672         s64 sectors = (s64) k.k->size;
1673         struct replicas_delta_list *d;
1674
1675         BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
1676                (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
1677
1678         if (flags & BTREE_TRIGGER_OVERWRITE)
1679                 sectors = -sectors;
1680         sectors *= replicas;
1681
1682         d = replicas_deltas_realloc(trans, 0);
1683
1684         replicas = clamp_t(unsigned, replicas, 1,
1685                            ARRAY_SIZE(d->persistent_reserved));
1686
1687         d->persistent_reserved[replicas - 1] += sectors;
1688         return 0;
1689 }
1690
1691 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1692                         struct bkey_s_c_reflink_p p,
1693                         u64 idx, unsigned flags)
1694 {
1695         struct bch_fs *c = trans->c;
1696         struct btree_iter iter;
1697         struct bkey_s_c k;
1698         struct bkey_i *n;
1699         __le64 *refcount;
1700         int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1701         s64 ret;
1702
1703         bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, POS(0, idx),
1704                              BTREE_ITER_INTENT|
1705                              BTREE_ITER_WITH_UPDATES);
1706         k = bch2_btree_iter_peek_slot(&iter);
1707         ret = bkey_err(k);
1708         if (ret)
1709                 goto err;
1710
1711         n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1712         ret = PTR_ERR_OR_ZERO(n);
1713         if (ret)
1714                 goto err;
1715
1716         bkey_reassemble(n, k);
1717
1718         refcount = bkey_refcount(n);
1719         if (!refcount) {
1720                 bch2_fs_inconsistent(c,
1721                         "%llu:%llu len %u points to nonexistent indirect extent %llu",
1722                         p.k->p.inode, p.k->p.offset, p.k->size, idx);
1723                 bch2_inconsistent_error(c);
1724                 ret = -EIO;
1725                 goto err;
1726         }
1727
1728         BUG_ON(!*refcount && (flags & BTREE_TRIGGER_OVERWRITE));
1729         le64_add_cpu(refcount, add);
1730
1731         if (!*refcount) {
1732                 n->k.type = KEY_TYPE_deleted;
1733                 set_bkey_val_u64s(&n->k, 0);
1734         }
1735
1736         bch2_btree_iter_set_pos_to_extent_start(&iter);
1737         ret = bch2_trans_update(trans, &iter, n, 0);
1738         if (ret)
1739                 goto err;
1740
1741         ret = k.k->p.offset - idx;
1742 err:
1743         bch2_trans_iter_exit(trans, &iter);
1744         return ret;
1745 }
1746
1747 static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1748                                      struct bkey_s_c k, unsigned flags)
1749 {
1750         struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1751         u64 idx = le64_to_cpu(p.v->idx);
1752         unsigned sectors = p.k->size;
1753         s64 ret = 0;
1754
1755         while (sectors) {
1756                 ret = __bch2_trans_mark_reflink_p(trans, p, idx, flags);
1757                 if (ret < 0)
1758                         return ret;
1759
1760                 ret = min_t(s64, ret, sectors);
1761                 idx     += ret;
1762                 sectors -= ret;
1763         }
1764
1765         return 0;
1766 }
1767
1768 int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c old,
1769                         struct bkey_s_c new, unsigned flags)
1770 {
1771         struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1772
1773         BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
1774
1775         switch (k.k->type) {
1776         case KEY_TYPE_btree_ptr:
1777         case KEY_TYPE_btree_ptr_v2:
1778         case KEY_TYPE_extent:
1779         case KEY_TYPE_reflink_v:
1780                 return bch2_trans_mark_extent(trans, k, flags);
1781         case KEY_TYPE_stripe:
1782                 return bch2_trans_mark_stripe(trans, old, new, flags);
1783         case KEY_TYPE_inode:
1784                 return bch2_trans_mark_inode(trans, old, new, flags);
1785         case KEY_TYPE_reservation:
1786                 return bch2_trans_mark_reservation(trans, k, flags);
1787         case KEY_TYPE_reflink_p:
1788                 return bch2_trans_mark_reflink_p(trans, k, flags);
1789         default:
1790                 return 0;
1791         }
1792 }
1793
1794 int bch2_trans_mark_update(struct btree_trans *trans,
1795                            struct btree_path *path,
1796                            struct bkey_i *new,
1797                            unsigned flags)
1798 {
1799         struct bkey             _deleted = KEY(0, 0, 0);
1800         struct bkey_s_c         deleted = (struct bkey_s_c) { &_deleted, NULL };
1801         struct bkey_s_c         old;
1802         struct bkey             unpacked;
1803         int ret;
1804
1805         if (unlikely(flags & BTREE_TRIGGER_NORUN))
1806                 return 0;
1807
1808         if (!btree_node_type_needs_gc(path->btree_id))
1809                 return 0;
1810
1811         old = bch2_btree_path_peek_slot(path, &unpacked);
1812
1813         if (old.k->type == new->k.type &&
1814             ((1U << old.k->type) & BTREE_TRIGGER_WANTS_OLD_AND_NEW)) {
1815                 ret   = bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new),
1816                                 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
1817         } else {
1818                 ret   = bch2_trans_mark_key(trans, deleted, bkey_i_to_s_c(new),
1819                                 BTREE_TRIGGER_INSERT|flags) ?:
1820                         bch2_trans_mark_key(trans, old, deleted,
1821                                 BTREE_TRIGGER_OVERWRITE|flags);
1822         }
1823
1824         return ret;
1825 }
1826
1827 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1828                                     struct bch_dev *ca, size_t b,
1829                                     enum bch_data_type type,
1830                                     unsigned sectors)
1831 {
1832         struct bch_fs *c = trans->c;
1833         struct btree_iter iter;
1834         struct bkey_alloc_unpacked u;
1835         struct bkey_alloc_buf *a;
1836         struct bch_extent_ptr ptr = {
1837                 .dev = ca->dev_idx,
1838                 .offset = bucket_to_sector(ca, b),
1839         };
1840         int ret = 0;
1841
1842         /*
1843          * Backup superblock might be past the end of our normal usable space:
1844          */
1845         if (b >= ca->mi.nbuckets)
1846                 return 0;
1847
1848         a = bch2_trans_start_alloc_update(trans, &iter, &ptr, &u);
1849         if (IS_ERR(a))
1850                 return PTR_ERR(a);
1851
1852         if (u.data_type && u.data_type != type) {
1853                 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1854                         "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1855                         "while marking %s",
1856                         iter.pos.inode, iter.pos.offset, u.gen,
1857                         bch2_data_types[u.data_type],
1858                         bch2_data_types[type],
1859                         bch2_data_types[type]);
1860                 ret = -EIO;
1861                 goto out;
1862         }
1863
1864         u.data_type     = type;
1865         u.dirty_sectors = sectors;
1866
1867         bch2_alloc_pack(c, a, u);
1868         bch2_trans_update(trans, &iter, &a->k, 0);
1869 out:
1870         bch2_trans_iter_exit(trans, &iter);
1871         return ret;
1872 }
1873
1874 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1875                                     struct bch_dev *ca, size_t b,
1876                                     enum bch_data_type type,
1877                                     unsigned sectors)
1878 {
1879         return __bch2_trans_do(trans, NULL, NULL, 0,
1880                         __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1881 }
1882
1883 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1884                                             struct bch_dev *ca,
1885                                             u64 start, u64 end,
1886                                             enum bch_data_type type,
1887                                             u64 *bucket, unsigned *bucket_sectors)
1888 {
1889         do {
1890                 u64 b = sector_to_bucket(ca, start);
1891                 unsigned sectors =
1892                         min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1893
1894                 if (b != *bucket && *bucket_sectors) {
1895                         int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1896                                                                   type, *bucket_sectors);
1897                         if (ret)
1898                                 return ret;
1899
1900                         *bucket_sectors = 0;
1901                 }
1902
1903                 *bucket         = b;
1904                 *bucket_sectors += sectors;
1905                 start += sectors;
1906         } while (start < end);
1907
1908         return 0;
1909 }
1910
1911 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1912                                     struct bch_dev *ca)
1913 {
1914         struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1915         u64 bucket = 0;
1916         unsigned i, bucket_sectors = 0;
1917         int ret;
1918
1919         for (i = 0; i < layout->nr_superblocks; i++) {
1920                 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1921
1922                 if (offset == BCH_SB_SECTOR) {
1923                         ret = bch2_trans_mark_metadata_sectors(trans, ca,
1924                                                 0, BCH_SB_SECTOR,
1925                                                 BCH_DATA_sb, &bucket, &bucket_sectors);
1926                         if (ret)
1927                                 return ret;
1928                 }
1929
1930                 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1931                                       offset + (1 << layout->sb_max_size_bits),
1932                                       BCH_DATA_sb, &bucket, &bucket_sectors);
1933                 if (ret)
1934                         return ret;
1935         }
1936
1937         if (bucket_sectors) {
1938                 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1939                                 bucket, BCH_DATA_sb, bucket_sectors);
1940                 if (ret)
1941                         return ret;
1942         }
1943
1944         for (i = 0; i < ca->journal.nr; i++) {
1945                 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1946                                 ca->journal.buckets[i],
1947                                 BCH_DATA_journal, ca->mi.bucket_size);
1948                 if (ret)
1949                         return ret;
1950         }
1951
1952         return 0;
1953 }
1954
1955 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1956 {
1957         return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
1958                         __bch2_trans_mark_dev_sb(&trans, ca));
1959 }
1960
1961 /* Disk reservations: */
1962
1963 #define SECTORS_CACHE   1024
1964
1965 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1966                               u64 sectors, int flags)
1967 {
1968         struct bch_fs_pcpu *pcpu;
1969         u64 old, v, get;
1970         s64 sectors_available;
1971         int ret;
1972
1973         percpu_down_read(&c->mark_lock);
1974         preempt_disable();
1975         pcpu = this_cpu_ptr(c->pcpu);
1976
1977         if (sectors <= pcpu->sectors_available)
1978                 goto out;
1979
1980         v = atomic64_read(&c->sectors_available);
1981         do {
1982                 old = v;
1983                 get = min((u64) sectors + SECTORS_CACHE, old);
1984
1985                 if (get < sectors) {
1986                         preempt_enable();
1987                         goto recalculate;
1988                 }
1989         } while ((v = atomic64_cmpxchg(&c->sectors_available,
1990                                        old, old - get)) != old);
1991
1992         pcpu->sectors_available         += get;
1993
1994 out:
1995         pcpu->sectors_available         -= sectors;
1996         this_cpu_add(*c->online_reserved, sectors);
1997         res->sectors                    += sectors;
1998
1999         preempt_enable();
2000         percpu_up_read(&c->mark_lock);
2001         return 0;
2002
2003 recalculate:
2004         mutex_lock(&c->sectors_available_lock);
2005
2006         percpu_u64_set(&c->pcpu->sectors_available, 0);
2007         sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
2008
2009         if (sectors <= sectors_available ||
2010             (flags & BCH_DISK_RESERVATION_NOFAIL)) {
2011                 atomic64_set(&c->sectors_available,
2012                              max_t(s64, 0, sectors_available - sectors));
2013                 this_cpu_add(*c->online_reserved, sectors);
2014                 res->sectors                    += sectors;
2015                 ret = 0;
2016         } else {
2017                 atomic64_set(&c->sectors_available, sectors_available);
2018                 ret = -ENOSPC;
2019         }
2020
2021         mutex_unlock(&c->sectors_available_lock);
2022         percpu_up_read(&c->mark_lock);
2023
2024         return ret;
2025 }
2026
2027 /* Startup/shutdown: */
2028
2029 static void buckets_free_rcu(struct rcu_head *rcu)
2030 {
2031         struct bucket_array *buckets =
2032                 container_of(rcu, struct bucket_array, rcu);
2033
2034         kvpfree(buckets,
2035                 sizeof(struct bucket_array) +
2036                 buckets->nbuckets * sizeof(struct bucket));
2037 }
2038
2039 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2040 {
2041         struct bucket_array *buckets = NULL, *old_buckets = NULL;
2042         unsigned long *buckets_nouse = NULL;
2043         alloc_fifo      free[RESERVE_NR];
2044         alloc_fifo      free_inc;
2045         alloc_heap      alloc_heap;
2046
2047         size_t btree_reserve    = DIV_ROUND_UP(BTREE_NODE_RESERVE,
2048                              ca->mi.bucket_size / c->opts.btree_node_size);
2049         /* XXX: these should be tunable */
2050         size_t reserve_none     = max_t(size_t, 1, nbuckets >> 9);
2051         size_t copygc_reserve   = max_t(size_t, 2, nbuckets >> 6);
2052         size_t free_inc_nr      = max(max_t(size_t, 1, nbuckets >> 12),
2053                                       btree_reserve * 2);
2054         bool resize = ca->buckets[0] != NULL;
2055         int ret = -ENOMEM;
2056         unsigned i;
2057
2058         memset(&free,           0, sizeof(free));
2059         memset(&free_inc,       0, sizeof(free_inc));
2060         memset(&alloc_heap,     0, sizeof(alloc_heap));
2061
2062         if (!(buckets           = kvpmalloc(sizeof(struct bucket_array) +
2063                                             nbuckets * sizeof(struct bucket),
2064                                             GFP_KERNEL|__GFP_ZERO)) ||
2065             !(buckets_nouse     = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2066                                             sizeof(unsigned long),
2067                                             GFP_KERNEL|__GFP_ZERO)) ||
2068             !init_fifo(&free[RESERVE_MOVINGGC],
2069                        copygc_reserve, GFP_KERNEL) ||
2070             !init_fifo(&free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
2071             !init_fifo(&free_inc,       free_inc_nr, GFP_KERNEL) ||
2072             !init_heap(&alloc_heap,     ALLOC_SCAN_BATCH(ca) << 1, GFP_KERNEL))
2073                 goto err;
2074
2075         buckets->first_bucket   = ca->mi.first_bucket;
2076         buckets->nbuckets       = nbuckets;
2077
2078         bch2_copygc_stop(c);
2079
2080         if (resize) {
2081                 down_write(&c->gc_lock);
2082                 down_write(&ca->bucket_lock);
2083                 percpu_down_write(&c->mark_lock);
2084         }
2085
2086         old_buckets = bucket_array(ca);
2087
2088         if (resize) {
2089                 size_t n = min(buckets->nbuckets, old_buckets->nbuckets);
2090
2091                 memcpy(buckets->b,
2092                        old_buckets->b,
2093                        n * sizeof(struct bucket));
2094                 memcpy(buckets_nouse,
2095                        ca->buckets_nouse,
2096                        BITS_TO_LONGS(n) * sizeof(unsigned long));
2097         }
2098
2099         rcu_assign_pointer(ca->buckets[0], buckets);
2100         buckets = old_buckets;
2101
2102         swap(ca->buckets_nouse, buckets_nouse);
2103
2104         if (resize) {
2105                 percpu_up_write(&c->mark_lock);
2106                 up_write(&c->gc_lock);
2107         }
2108
2109         spin_lock(&c->freelist_lock);
2110         for (i = 0; i < RESERVE_NR; i++) {
2111                 fifo_move(&free[i], &ca->free[i]);
2112                 swap(ca->free[i], free[i]);
2113         }
2114         fifo_move(&free_inc, &ca->free_inc);
2115         swap(ca->free_inc, free_inc);
2116         spin_unlock(&c->freelist_lock);
2117
2118         /* with gc lock held, alloc_heap can't be in use: */
2119         swap(ca->alloc_heap, alloc_heap);
2120
2121         nbuckets = ca->mi.nbuckets;
2122
2123         if (resize)
2124                 up_write(&ca->bucket_lock);
2125
2126         ret = 0;
2127 err:
2128         free_heap(&alloc_heap);
2129         free_fifo(&free_inc);
2130         for (i = 0; i < RESERVE_NR; i++)
2131                 free_fifo(&free[i]);
2132         kvpfree(buckets_nouse,
2133                 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2134         if (buckets)
2135                 call_rcu(&old_buckets->rcu, buckets_free_rcu);
2136
2137         return ret;
2138 }
2139
2140 void bch2_dev_buckets_free(struct bch_dev *ca)
2141 {
2142         unsigned i;
2143
2144         free_heap(&ca->alloc_heap);
2145         free_fifo(&ca->free_inc);
2146         for (i = 0; i < RESERVE_NR; i++)
2147                 free_fifo(&ca->free[i]);
2148         kvpfree(ca->buckets_nouse,
2149                 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2150         kvpfree(rcu_dereference_protected(ca->buckets[0], 1),
2151                 sizeof(struct bucket_array) +
2152                 ca->mi.nbuckets * sizeof(struct bucket));
2153
2154         for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2155                 free_percpu(ca->usage[i]);
2156         kfree(ca->usage_base);
2157 }
2158
2159 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2160 {
2161         unsigned i;
2162
2163         ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2164         if (!ca->usage_base)
2165                 return -ENOMEM;
2166
2167         for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2168                 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2169                 if (!ca->usage[i])
2170                         return -ENOMEM;
2171         }
2172
2173         return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;
2174 }