blob: 4ff56fa4d539201e648f28a9225e9d71a59907f0 [file] [log] [blame]
Kent Overstreet7b3f84e2018-10-06 00:46:55 -04001// SPDX-License-Identifier: GPL-2.0
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002#include "bcachefs.h"
Kent Overstreet7b3f84e2018-10-06 00:46:55 -04003#include "alloc_background.h"
4#include "alloc_foreground.h"
Kent Overstreeta8c752b2022-03-17 20:51:27 -04005#include "backpointers.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08006#include "btree_cache.h"
7#include "btree_io.h"
Kent Overstreet5d20ba42019-10-05 12:54:53 -04008#include "btree_key_cache.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08009#include "btree_update.h"
10#include "btree_update_interior.h"
11#include "btree_gc.h"
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -050012#include "btree_write_buffer.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080013#include "buckets.h"
Kent Overstreet21aec962022-01-04 22:32:09 -050014#include "buckets_waiting_for_journal.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080015#include "clock.h"
16#include "debug.h"
Kent Overstreetcd575dd2018-11-01 15:13:19 -040017#include "ec.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080018#include "error.h"
Kent Overstreetc6b28262021-12-11 17:13:09 -050019#include "lru.h"
Kent Overstreetd0734352019-04-11 22:39:39 -040020#include "recovery.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080021#include "trace.h"
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050022#include "varint.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080023
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080024#include <linux/kthread.h>
25#include <linux/math64.h>
26#include <linux/random.h>
27#include <linux/rculist.h>
28#include <linux/rcupdate.h>
29#include <linux/sched/task.h>
30#include <linux/sort.h>
31
Kent Overstreeta393f332024-02-16 01:08:25 -050032static void bch2_discard_one_bucket_fast(struct bch_fs *c, struct bpos bucket);
33
Kent Overstreet3d48a7f2021-12-31 20:03:29 -050034/* Persistent alloc info: */
35
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050036static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
37#define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
38 BCH_ALLOC_FIELDS_V1()
Kent Overstreet90541a72018-07-21 23:36:11 -040039#undef x
Kent Overstreet90541a72018-07-21 23:36:11 -040040};
41
Kent Overstreet3d48a7f2021-12-31 20:03:29 -050042struct bkey_alloc_unpacked {
43 u64 journal_seq;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -050044 u8 gen;
45 u8 oldest_gen;
46 u8 data_type;
47 bool need_discard:1;
48 bool need_inc_gen:1;
49#define x(_name, _bits) u##_bits _name;
50 BCH_ALLOC_FIELDS_V2()
51#undef x
52};
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080053
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050054static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
55 const void **p, unsigned field)
Kent Overstreet90541a72018-07-21 23:36:11 -040056{
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050057 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
Kent Overstreet90541a72018-07-21 23:36:11 -040058 u64 v;
59
60 if (!(a->fields & (1 << field)))
61 return 0;
62
63 switch (bytes) {
64 case 1:
65 v = *((const u8 *) *p);
66 break;
67 case 2:
68 v = le16_to_cpup(*p);
69 break;
70 case 4:
71 v = le32_to_cpup(*p);
72 break;
73 case 8:
74 v = le64_to_cpup(*p);
75 break;
76 default:
77 BUG();
78 }
79
80 *p += bytes;
81 return v;
82}
83
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050084static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
85 struct bkey_s_c k)
86{
87 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
88 const void *d = in->data;
89 unsigned idx = 0;
90
91 out->gen = in->gen;
92
93#define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
94 BCH_ALLOC_FIELDS_V1()
95#undef x
96}
97
Kent Overstreet7f4e1d52021-01-22 18:01:07 -050098static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
99 struct bkey_s_c k)
100{
101 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
102 const u8 *in = a.v->data;
103 const u8 *end = bkey_val_end(a);
104 unsigned fieldnr = 0;
105 int ret;
106 u64 v;
107
108 out->gen = a.v->gen;
109 out->oldest_gen = a.v->oldest_gen;
110 out->data_type = a.v->data_type;
111
112#define x(_name, _bits) \
113 if (fieldnr < a.v->nr_fields) { \
Kent Overstreet8d344582021-07-13 16:03:51 -0400114 ret = bch2_varint_decode_fast(in, end, &v); \
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500115 if (ret < 0) \
116 return ret; \
117 in += ret; \
118 } else { \
119 v = 0; \
120 } \
121 out->_name = v; \
122 if (v != out->_name) \
123 return -1; \
124 fieldnr++;
125
126 BCH_ALLOC_FIELDS_V2()
127#undef x
128 return 0;
129}
130
Kent Overstreet3e52c222021-10-29 21:14:23 -0400131static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
132 struct bkey_s_c k)
133{
134 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
135 const u8 *in = a.v->data;
136 const u8 *end = bkey_val_end(a);
137 unsigned fieldnr = 0;
138 int ret;
139 u64 v;
140
141 out->gen = a.v->gen;
142 out->oldest_gen = a.v->oldest_gen;
143 out->data_type = a.v->data_type;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500144 out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
145 out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
Kent Overstreet3e52c222021-10-29 21:14:23 -0400146 out->journal_seq = le64_to_cpu(a.v->journal_seq);
147
148#define x(_name, _bits) \
149 if (fieldnr < a.v->nr_fields) { \
150 ret = bch2_varint_decode_fast(in, end, &v); \
151 if (ret < 0) \
152 return ret; \
153 in += ret; \
154 } else { \
155 v = 0; \
156 } \
157 out->_name = v; \
158 if (v != out->_name) \
159 return -1; \
160 fieldnr++;
161
162 BCH_ALLOC_FIELDS_V2()
163#undef x
164 return 0;
165}
166
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500167static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
Kent Overstreet8fe826f2019-02-13 14:46:32 -0500168{
Kent Overstreet19a614d2023-01-30 20:58:43 -0500169 struct bkey_alloc_unpacked ret = { .gen = 0 };
Kent Overstreet460651e2019-04-17 18:14:46 -0400170
Kent Overstreet3e52c222021-10-29 21:14:23 -0400171 switch (k.k->type) {
172 case KEY_TYPE_alloc:
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500173 bch2_alloc_unpack_v1(&ret, k);
Kent Overstreet3e52c222021-10-29 21:14:23 -0400174 break;
175 case KEY_TYPE_alloc_v2:
176 bch2_alloc_unpack_v2(&ret, k);
177 break;
178 case KEY_TYPE_alloc_v3:
179 bch2_alloc_unpack_v3(&ret, k);
180 break;
181 }
Kent Overstreet460651e2019-04-17 18:14:46 -0400182
Kent Overstreet8fe826f2019-02-13 14:46:32 -0500183 return ret;
184}
185
Kent Overstreet6ad060b2021-04-16 21:53:23 -0400186static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800187{
Kent Overstreet90541a72018-07-21 23:36:11 -0400188 unsigned i, bytes = offsetof(struct bch_alloc, data);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800189
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500190 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
Kent Overstreet90541a72018-07-21 23:36:11 -0400191 if (a->fields & (1 << i))
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500192 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800193
194 return DIV_ROUND_UP(bytes, sizeof(u64));
195}
196
Kent Overstreetb65db752023-10-24 20:44:36 -0400197int bch2_alloc_v1_invalid(struct bch_fs *c, struct bkey_s_c k,
Kent Overstreet8726dc92023-07-06 21:16:10 -0400198 enum bkey_invalid_flags flags,
199 struct printbuf *err)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800200{
Kent Overstreet26609b62018-11-01 15:10:01 -0400201 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
Kent Overstreetb65db752023-10-24 20:44:36 -0400202 int ret = 0;
Kent Overstreet26609b62018-11-01 15:10:01 -0400203
Kent Overstreet26609b62018-11-01 15:10:01 -0400204 /* allow for unknown fields */
Kent Overstreetb65db752023-10-24 20:44:36 -0400205 bkey_fsck_err_on(bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v), c, err,
206 alloc_v1_val_size_bad,
207 "incorrect value size (%zu < %u)",
208 bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
209fsck_err:
210 return ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800211}
212
Kent Overstreetb65db752023-10-24 20:44:36 -0400213int bch2_alloc_v2_invalid(struct bch_fs *c, struct bkey_s_c k,
Kent Overstreet8726dc92023-07-06 21:16:10 -0400214 enum bkey_invalid_flags flags,
215 struct printbuf *err)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800216{
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500217 struct bkey_alloc_unpacked u;
Kent Overstreetb65db752023-10-24 20:44:36 -0400218 int ret = 0;
Kent Overstreet319f9ac2018-11-09 01:24:07 -0500219
Kent Overstreetb65db752023-10-24 20:44:36 -0400220 bkey_fsck_err_on(bch2_alloc_unpack_v2(&u, k), c, err,
221 alloc_v2_unpack_error,
222 "unpack error");
223fsck_err:
224 return ret;
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500225}
226
Kent Overstreetb65db752023-10-24 20:44:36 -0400227int bch2_alloc_v3_invalid(struct bch_fs *c, struct bkey_s_c k,
Kent Overstreet8726dc92023-07-06 21:16:10 -0400228 enum bkey_invalid_flags flags,
229 struct printbuf *err)
Kent Overstreet3e52c222021-10-29 21:14:23 -0400230{
231 struct bkey_alloc_unpacked u;
Kent Overstreetb65db752023-10-24 20:44:36 -0400232 int ret = 0;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500233
Kent Overstreetb65db752023-10-24 20:44:36 -0400234 bkey_fsck_err_on(bch2_alloc_unpack_v3(&u, k), c, err,
235 alloc_v2_unpack_error,
236 "unpack error");
237fsck_err:
238 return ret;
Kent Overstreet3e52c222021-10-29 21:14:23 -0400239}
240
Kent Overstreetb65db752023-10-24 20:44:36 -0400241int bch2_alloc_v4_invalid(struct bch_fs *c, struct bkey_s_c k,
Kent Overstreet8726dc92023-07-06 21:16:10 -0400242 enum bkey_invalid_flags flags, struct printbuf *err)
Kent Overstreet7f4e1d52021-01-22 18:01:07 -0500243{
Kent Overstreet11c7d3e2022-04-06 17:22:47 -0400244 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
Kent Overstreetb65db752023-10-24 20:44:36 -0400245 int ret = 0;
Kent Overstreet11c7d3e2022-04-06 17:22:47 -0400246
Kent Overstreetb65db752023-10-24 20:44:36 -0400247 bkey_fsck_err_on(alloc_v4_u64s(a.v) > bkey_val_u64s(k.k), c, err,
248 alloc_v4_val_size_bad,
249 "bad val size (%u > %zu)",
250 alloc_v4_u64s(a.v), bkey_val_u64s(k.k));
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500251
Kent Overstreetb65db752023-10-24 20:44:36 -0400252 bkey_fsck_err_on(!BCH_ALLOC_V4_BACKPOINTERS_START(a.v) &&
253 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v), c, err,
254 alloc_v4_backpointers_start_bad,
255 "invalid backpointers_start");
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400256
Kent Overstreetb65db752023-10-24 20:44:36 -0400257 bkey_fsck_err_on(alloc_data_type(*a.v, a.v->data_type) != a.v->data_type, c, err,
258 alloc_key_data_type_bad,
259 "invalid data type (got %u should be %u)",
260 a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400261
Kent Overstreet71aba592023-08-22 18:48:09 -0400262 switch (a.v->data_type) {
263 case BCH_DATA_free:
264 case BCH_DATA_need_gc_gens:
265 case BCH_DATA_need_discard:
Kent Overstreetdafff7e2023-11-23 18:05:18 -0500266 bkey_fsck_err_on(bch2_bucket_sectors(*a.v) || a.v->stripe,
267 c, err, alloc_key_empty_but_have_data,
Kent Overstreetb65db752023-10-24 20:44:36 -0400268 "empty data type free but have data");
Kent Overstreet71aba592023-08-22 18:48:09 -0400269 break;
270 case BCH_DATA_sb:
271 case BCH_DATA_journal:
272 case BCH_DATA_btree:
273 case BCH_DATA_user:
274 case BCH_DATA_parity:
Kent Overstreetdafff7e2023-11-23 18:05:18 -0500275 bkey_fsck_err_on(!bch2_bucket_sectors_dirty(*a.v),
276 c, err, alloc_key_dirty_sectors_0,
Kent Overstreetb65db752023-10-24 20:44:36 -0400277 "data_type %s but dirty_sectors==0",
Kent Overstreete58f9632024-01-06 20:57:43 -0500278 bch2_data_type_str(a.v->data_type));
Kent Overstreet71aba592023-08-22 18:48:09 -0400279 break;
280 case BCH_DATA_cached:
Kent Overstreetb65db752023-10-24 20:44:36 -0400281 bkey_fsck_err_on(!a.v->cached_sectors ||
Kent Overstreetdafff7e2023-11-23 18:05:18 -0500282 bch2_bucket_sectors_dirty(*a.v) ||
283 a.v->stripe,
284 c, err, alloc_key_cached_inconsistency,
Kent Overstreetb65db752023-10-24 20:44:36 -0400285 "data type inconsistency");
Kent Overstreet62491952022-04-07 17:32:57 -0400286
Kent Overstreetb65db752023-10-24 20:44:36 -0400287 bkey_fsck_err_on(!a.v->io_time[READ] &&
288 c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_to_lru_refs,
Kent Overstreetdafff7e2023-11-23 18:05:18 -0500289 c, err, alloc_key_cached_but_read_time_zero,
Kent Overstreetb65db752023-10-24 20:44:36 -0400290 "cached bucket with read_time == 0");
Kent Overstreet71aba592023-08-22 18:48:09 -0400291 break;
292 case BCH_DATA_stripe:
Kent Overstreet71aba592023-08-22 18:48:09 -0400293 break;
Kent Overstreet11c7d3e2022-04-06 17:22:47 -0400294 }
Kent Overstreetb65db752023-10-24 20:44:36 -0400295fsck_err:
296 return ret;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500297}
298
299void bch2_alloc_v4_swab(struct bkey_s k)
300{
301 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400302 struct bch_backpointer *bp, *bps;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500303
304 a->journal_seq = swab64(a->journal_seq);
305 a->flags = swab32(a->flags);
306 a->dirty_sectors = swab32(a->dirty_sectors);
307 a->cached_sectors = swab32(a->cached_sectors);
308 a->io_time[0] = swab64(a->io_time[0]);
309 a->io_time[1] = swab64(a->io_time[1]);
310 a->stripe = swab32(a->stripe);
311 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
Brian Foster0996c722023-11-03 09:09:37 -0400312 a->fragmentation_lru = swab64(a->fragmentation_lru);
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400313
314 bps = alloc_v4_backpointers(a);
315 for (bp = bps; bp < bps + BCH_ALLOC_V4_NR_BACKPOINTERS(a); bp++) {
316 bp->bucket_offset = swab40(bp->bucket_offset);
317 bp->bucket_len = swab32(bp->bucket_len);
318 bch2_bpos_swab(&bp->pos);
319 }
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500320}
321
322void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
323{
Kent Overstreet19a614d2023-01-30 20:58:43 -0500324 struct bch_alloc_v4 _a;
325 const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &_a);
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500326
Kent Overstreet19a614d2023-01-30 20:58:43 -0500327 prt_newline(out);
328 printbuf_indent_add(out, 2);
Kent Overstreet3d48a7f2021-12-31 20:03:29 -0500329
Kent Overstreete58f9632024-01-06 20:57:43 -0500330 prt_printf(out, "gen %u oldest_gen %u data_type ", a->gen, a->oldest_gen);
331 bch2_prt_data_type(out, a->data_type);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500332 prt_newline(out);
333 prt_printf(out, "journal_seq %llu", a->journal_seq);
334 prt_newline(out);
335 prt_printf(out, "need_discard %llu", BCH_ALLOC_V4_NEED_DISCARD(a));
336 prt_newline(out);
337 prt_printf(out, "need_inc_gen %llu", BCH_ALLOC_V4_NEED_INC_GEN(a));
338 prt_newline(out);
339 prt_printf(out, "dirty_sectors %u", a->dirty_sectors);
340 prt_newline(out);
341 prt_printf(out, "cached_sectors %u", a->cached_sectors);
342 prt_newline(out);
343 prt_printf(out, "stripe %u", a->stripe);
344 prt_newline(out);
345 prt_printf(out, "stripe_redundancy %u", a->stripe_redundancy);
346 prt_newline(out);
347 prt_printf(out, "io_time[READ] %llu", a->io_time[READ]);
348 prt_newline(out);
349 prt_printf(out, "io_time[WRITE] %llu", a->io_time[WRITE]);
350 prt_newline(out);
Kent Overstreet80c33082022-12-05 10:24:19 -0500351 prt_printf(out, "fragmentation %llu", a->fragmentation_lru);
352 prt_newline(out);
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400353 prt_printf(out, "bp_start %llu", BCH_ALLOC_V4_BACKPOINTERS_START(a));
Kent Overstreet19a614d2023-01-30 20:58:43 -0500354 printbuf_indent_sub(out, 2);
355}
356
Kent Overstreet19a614d2023-01-30 20:58:43 -0500357void __bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
358{
359 if (k.k->type == KEY_TYPE_alloc_v4) {
360 void *src, *dst;
361
362 *out = *bkey_s_c_to_alloc_v4(k).v;
363
364 src = alloc_v4_backpointers(out);
365 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
366 dst = alloc_v4_backpointers(out);
367
368 if (src < dst)
369 memset(src, 0, dst - src);
Kent Overstreet62a03552023-03-31 16:24:45 -0400370
371 SET_BCH_ALLOC_V4_NR_BACKPOINTERS(out, 0);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500372 } else {
373 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
374
375 *out = (struct bch_alloc_v4) {
376 .journal_seq = u.journal_seq,
377 .flags = u.need_discard,
378 .gen = u.gen,
379 .oldest_gen = u.oldest_gen,
380 .data_type = u.data_type,
381 .stripe_redundancy = u.stripe_redundancy,
382 .dirty_sectors = u.dirty_sectors,
383 .cached_sectors = u.cached_sectors,
384 .io_time[READ] = u.read_time,
385 .io_time[WRITE] = u.write_time,
386 .stripe = u.stripe,
387 };
388
389 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
390 }
391}
392
393static noinline struct bkey_i_alloc_v4 *
394__bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
395{
396 struct bkey_i_alloc_v4 *ret;
Kent Overstreet19a614d2023-01-30 20:58:43 -0500397
Kent Overstreet615fcca2023-04-16 07:10:46 -0400398 ret = bch2_trans_kmalloc(trans, max(bkey_bytes(k.k), sizeof(struct bkey_i_alloc_v4)));
Kent Overstreet62a03552023-03-31 16:24:45 -0400399 if (IS_ERR(ret))
400 return ret;
401
402 if (k.k->type == KEY_TYPE_alloc_v4) {
403 void *src, *dst;
Kent Overstreet19a614d2023-01-30 20:58:43 -0500404
405 bkey_reassemble(&ret->k_i, k);
406
407 src = alloc_v4_backpointers(&ret->v);
408 SET_BCH_ALLOC_V4_BACKPOINTERS_START(&ret->v, BCH_ALLOC_V4_U64s);
409 dst = alloc_v4_backpointers(&ret->v);
410
411 if (src < dst)
412 memset(src, 0, dst - src);
Kent Overstreet62a03552023-03-31 16:24:45 -0400413
414 SET_BCH_ALLOC_V4_NR_BACKPOINTERS(&ret->v, 0);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500415 set_alloc_v4_u64s(ret);
416 } else {
Kent Overstreeta8c752b2022-03-17 20:51:27 -0400417 bkey_alloc_v4_init(&ret->k_i);
418 ret->k.p = k.k->p;
419 bch2_alloc_to_v4(k, &ret->v);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500420 }
421 return ret;
422}
423
424static inline struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut_inlined(struct btree_trans *trans, struct bkey_s_c k)
425{
Kent Overstreet62a03552023-03-31 16:24:45 -0400426 struct bkey_s_c_alloc_v4 a;
427
Kent Overstreet19a614d2023-01-30 20:58:43 -0500428 if (likely(k.k->type == KEY_TYPE_alloc_v4) &&
Kent Overstreet62a03552023-03-31 16:24:45 -0400429 ((a = bkey_s_c_to_alloc_v4(k), true) &&
Kent Overstreet34dfa5d2023-04-27 23:48:33 -0400430 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v) == 0))
Kent Overstreetdbda63b2023-04-30 19:21:06 -0400431 return bch2_bkey_make_mut_noupdate_typed(trans, k, alloc_v4);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500432
433 return __bch2_alloc_to_v4_mut(trans, k);
434}
435
436struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
437{
438 return bch2_alloc_to_v4_mut_inlined(trans, k);
439}
440
441struct bkey_i_alloc_v4 *
442bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
443 struct bpos pos)
444{
445 struct bkey_s_c k;
446 struct bkey_i_alloc_v4 *a;
447 int ret;
448
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400449 k = bch2_bkey_get_iter(trans, iter, BTREE_ID_alloc, pos,
Kent Overstreet19a614d2023-01-30 20:58:43 -0500450 BTREE_ITER_WITH_UPDATES|
451 BTREE_ITER_CACHED|
452 BTREE_ITER_INTENT);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500453 ret = bkey_err(k);
454 if (unlikely(ret))
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400455 return ERR_PTR(ret);
Kent Overstreet19a614d2023-01-30 20:58:43 -0500456
457 a = bch2_alloc_to_v4_mut_inlined(trans, k);
458 ret = PTR_ERR_OR_ZERO(a);
459 if (unlikely(ret))
460 goto err;
461 return a;
462err:
463 bch2_trans_iter_exit(trans, iter);
464 return ERR_PTR(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800465}
466
Kent Overstreet5250b742022-11-25 23:14:30 -0500467static struct bpos alloc_gens_pos(struct bpos pos, unsigned *offset)
468{
469 *offset = pos.offset & KEY_TYPE_BUCKET_GENS_MASK;
470
471 pos.offset >>= KEY_TYPE_BUCKET_GENS_BITS;
472 return pos;
473}
474
475static struct bpos bucket_gens_pos_to_alloc(struct bpos pos, unsigned offset)
476{
477 pos.offset <<= KEY_TYPE_BUCKET_GENS_BITS;
478 pos.offset += offset;
479 return pos;
480}
481
482static unsigned alloc_gen(struct bkey_s_c k, unsigned offset)
483{
484 return k.k->type == KEY_TYPE_bucket_gens
485 ? bkey_s_c_to_bucket_gens(k).v->gens[offset]
486 : 0;
487}
488
Kent Overstreetb65db752023-10-24 20:44:36 -0400489int bch2_bucket_gens_invalid(struct bch_fs *c, struct bkey_s_c k,
Kent Overstreet8726dc92023-07-06 21:16:10 -0400490 enum bkey_invalid_flags flags,
491 struct printbuf *err)
Kent Overstreet5250b742022-11-25 23:14:30 -0500492{
Kent Overstreetb65db752023-10-24 20:44:36 -0400493 int ret = 0;
Kent Overstreet5250b742022-11-25 23:14:30 -0500494
Kent Overstreetb65db752023-10-24 20:44:36 -0400495 bkey_fsck_err_on(bkey_val_bytes(k.k) != sizeof(struct bch_bucket_gens), c, err,
496 bucket_gens_val_size_bad,
497 "bad val size (%zu != %zu)",
498 bkey_val_bytes(k.k), sizeof(struct bch_bucket_gens));
499fsck_err:
500 return ret;
Kent Overstreet5250b742022-11-25 23:14:30 -0500501}
502
503void bch2_bucket_gens_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
504{
505 struct bkey_s_c_bucket_gens g = bkey_s_c_to_bucket_gens(k);
506 unsigned i;
507
508 for (i = 0; i < ARRAY_SIZE(g.v->gens); i++) {
509 if (i)
510 prt_char(out, ' ');
511 prt_printf(out, "%u", g.v->gens[i]);
512 }
513}
514
515int bch2_bucket_gens_init(struct bch_fs *c)
516{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400517 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet5250b742022-11-25 23:14:30 -0500518 struct bkey_i_bucket_gens g;
519 bool have_bucket_gens_key = false;
Kent Overstreet5250b742022-11-25 23:14:30 -0500520 int ret;
521
Kent Overstreet5028b902023-12-07 23:33:11 -0500522 ret = for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
523 BTREE_ITER_PREFETCH, k, ({
Kent Overstreet5250b742022-11-25 23:14:30 -0500524 /*
525 * Not a fsck error because this is checked/repaired by
526 * bch2_check_alloc_key() which runs later:
527 */
528 if (!bch2_dev_bucket_exists(c, k.k->p))
529 continue;
530
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500531 struct bch_alloc_v4 a;
532 u8 gen = bch2_alloc_to_v4(k, &a)->gen;
533 unsigned offset;
534 struct bpos pos = alloc_gens_pos(iter.pos, &offset);
Kent Overstreet1ba6f48f2024-03-17 20:32:36 -0400535 int ret2 = 0;
Kent Overstreet5250b742022-11-25 23:14:30 -0500536
537 if (have_bucket_gens_key && bkey_cmp(iter.pos, pos)) {
Kent Overstreet1ba6f48f2024-03-17 20:32:36 -0400538 ret2 = bch2_btree_insert_trans(trans, BTREE_ID_bucket_gens, &g.k_i, 0) ?:
539 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
540 if (ret2)
541 goto iter_err;
Kent Overstreet5250b742022-11-25 23:14:30 -0500542 have_bucket_gens_key = false;
543 }
544
545 if (!have_bucket_gens_key) {
546 bkey_bucket_gens_init(&g.k_i);
547 g.k.p = pos;
548 have_bucket_gens_key = true;
549 }
550
551 g.v.gens[offset] = gen;
Kent Overstreet1ba6f48f2024-03-17 20:32:36 -0400552iter_err:
553 ret2;
Kent Overstreet27b2df92023-12-07 23:28:26 -0500554 }));
Kent Overstreet5250b742022-11-25 23:14:30 -0500555
556 if (have_bucket_gens_key && !ret)
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400557 ret = commit_do(trans, NULL, NULL,
Kent Overstreet3f0e2972023-11-28 16:36:54 -0500558 BCH_TRANS_COMMIT_no_enospc,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400559 bch2_btree_insert_trans(trans, BTREE_ID_bucket_gens, &g.k_i, 0));
Kent Overstreet5250b742022-11-25 23:14:30 -0500560
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400561 bch2_trans_put(trans);
Kent Overstreet5250b742022-11-25 23:14:30 -0500562
Kent Overstreet27b2df92023-12-07 23:28:26 -0500563 bch_err_fn(c, ret);
Kent Overstreet5250b742022-11-25 23:14:30 -0500564 return ret;
565}
566
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400567int bch2_alloc_read(struct bch_fs *c)
Kent Overstreet5250b742022-11-25 23:14:30 -0500568{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400569 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet5250b742022-11-25 23:14:30 -0500570 int ret;
571
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400572 down_read(&c->gc_lock);
Kent Overstreet5250b742022-11-25 23:14:30 -0500573
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400574 if (c->sb.version_upgrade_complete >= bcachefs_metadata_version_bucket_gens) {
Kent Overstreet5028b902023-12-07 23:33:11 -0500575 ret = for_each_btree_key(trans, iter, BTREE_ID_bucket_gens, POS_MIN,
576 BTREE_ITER_PREFETCH, k, ({
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400577 u64 start = bucket_gens_pos_to_alloc(k.k->p, 0).offset;
578 u64 end = bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0).offset;
Kent Overstreet5250b742022-11-25 23:14:30 -0500579
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400580 if (k.k->type != KEY_TYPE_bucket_gens)
581 continue;
Kent Overstreet5250b742022-11-25 23:14:30 -0500582
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500583 const struct bch_bucket_gens *g = bkey_s_c_to_bucket_gens(k).v;
Kent Overstreet5250b742022-11-25 23:14:30 -0500584
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400585 /*
586 * Not a fsck error because this is checked/repaired by
587 * bch2_check_alloc_key() which runs later:
588 */
589 if (!bch2_dev_exists2(c, k.k->p.inode))
590 continue;
Kent Overstreet5250b742022-11-25 23:14:30 -0500591
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500592 struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400593
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500594 for (u64 b = max_t(u64, ca->mi.first_bucket, start);
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400595 b < min_t(u64, ca->mi.nbuckets, end);
596 b++)
597 *bucket_gen(ca, b) = g->gens[b & KEY_TYPE_BUCKET_GENS_MASK];
Kent Overstreet27b2df92023-12-07 23:28:26 -0500598 0;
599 }));
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400600 } else {
Kent Overstreet5028b902023-12-07 23:33:11 -0500601 ret = for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
602 BTREE_ITER_PREFETCH, k, ({
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400603 /*
604 * Not a fsck error because this is checked/repaired by
605 * bch2_check_alloc_key() which runs later:
606 */
607 if (!bch2_dev_bucket_exists(c, k.k->p))
608 continue;
609
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500610 struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400611
Kent Overstreet80eab7a72023-12-16 22:30:09 -0500612 struct bch_alloc_v4 a;
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400613 *bucket_gen(ca, k.k->p.offset) = bch2_alloc_to_v4(k, &a)->gen;
Kent Overstreet27b2df92023-12-07 23:28:26 -0500614 0;
615 }));
Kent Overstreet5250b742022-11-25 23:14:30 -0500616 }
Kent Overstreet5250b742022-11-25 23:14:30 -0500617
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400618 bch2_trans_put(trans);
Kent Overstreet10a6ced2023-07-08 22:27:03 -0400619 up_read(&c->gc_lock);
Kent Overstreet5250b742022-11-25 23:14:30 -0500620
Kent Overstreet27b2df92023-12-07 23:28:26 -0500621 bch_err_fn(c, ret);
Kent Overstreet5250b742022-11-25 23:14:30 -0500622 return ret;
623}
624
Kent Overstreetc6b28262021-12-11 17:13:09 -0500625/* Free space/discard btree: */
626
627static int bch2_bucket_do_index(struct btree_trans *trans,
628 struct bkey_s_c alloc_k,
Kent Overstreet822835f2022-04-01 01:29:59 -0400629 const struct bch_alloc_v4 *a,
Kent Overstreetc6b28262021-12-11 17:13:09 -0500630 bool set)
631{
632 struct bch_fs *c = trans->c;
633 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
634 struct btree_iter iter;
635 struct bkey_s_c old;
636 struct bkey_i *k;
Kent Overstreetc6b28262021-12-11 17:13:09 -0500637 enum btree_id btree;
638 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
639 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
640 struct printbuf buf = PRINTBUF;
641 int ret;
642
Kent Overstreet822835f2022-04-01 01:29:59 -0400643 if (a->data_type != BCH_DATA_free &&
644 a->data_type != BCH_DATA_need_discard)
Kent Overstreetc6b28262021-12-11 17:13:09 -0500645 return 0;
646
Kent Overstreet994ba472022-11-23 22:13:19 -0500647 k = bch2_trans_kmalloc_nomemzero(trans, sizeof(*k));
Kent Overstreetc6b28262021-12-11 17:13:09 -0500648 if (IS_ERR(k))
649 return PTR_ERR(k);
650
651 bkey_init(&k->k);
652 k->k.type = new_type;
653
Kent Overstreet822835f2022-04-01 01:29:59 -0400654 switch (a->data_type) {
655 case BCH_DATA_free:
Kent Overstreetc6b28262021-12-11 17:13:09 -0500656 btree = BTREE_ID_freespace;
Kent Overstreet822835f2022-04-01 01:29:59 -0400657 k->k.p = alloc_freespace_pos(alloc_k.k->p, *a);
Kent Overstreetc6b28262021-12-11 17:13:09 -0500658 bch2_key_resize(&k->k, 1);
659 break;
Kent Overstreet822835f2022-04-01 01:29:59 -0400660 case BCH_DATA_need_discard:
Kent Overstreetc6b28262021-12-11 17:13:09 -0500661 btree = BTREE_ID_need_discard;
662 k->k.p = alloc_k.k->p;
663 break;
664 default:
665 return 0;
666 }
667
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400668 old = bch2_bkey_get_iter(trans, &iter, btree,
Kent Overstreetc6b28262021-12-11 17:13:09 -0500669 bkey_start_pos(&k->k),
670 BTREE_ITER_INTENT);
Kent Overstreetc6b28262021-12-11 17:13:09 -0500671 ret = bkey_err(old);
672 if (ret)
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400673 return ret;
Kent Overstreetc6b28262021-12-11 17:13:09 -0500674
675 if (ca->mi.freespace_initialized &&
Kent Overstreet067d2282023-07-07 02:42:28 -0400676 c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info &&
Kent Overstreet84befe82022-04-10 18:12:04 -0400677 bch2_trans_inconsistent_on(old.k->type != old_type, trans,
Kent Overstreete96f5a62023-06-18 13:25:35 -0400678 "incorrect key when %s %s:%llu:%llu:0 (got %s should be %s)\n"
Kent Overstreetc6b28262021-12-11 17:13:09 -0500679 " for %s",
680 set ? "setting" : "clearing",
Kent Overstreet88dfe192023-10-19 22:49:08 -0400681 bch2_btree_id_str(btree),
Kent Overstreete96f5a62023-06-18 13:25:35 -0400682 iter.pos.inode,
683 iter.pos.offset,
Kent Overstreetc6b28262021-12-11 17:13:09 -0500684 bch2_bkey_types[old.k->type],
685 bch2_bkey_types[old_type],
686 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
687 ret = -EIO;
688 goto err;
689 }
690
691 ret = bch2_trans_update(trans, &iter, k, 0);
692err:
693 bch2_trans_iter_exit(trans, &iter);
694 printbuf_exit(&buf);
695 return ret;
696}
697
Kent Overstreet5250b742022-11-25 23:14:30 -0500698static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
699 struct bpos bucket, u8 gen)
700{
701 struct btree_iter iter;
702 unsigned offset;
703 struct bpos pos = alloc_gens_pos(bucket, &offset);
704 struct bkey_i_bucket_gens *g;
705 struct bkey_s_c k;
706 int ret;
707
708 g = bch2_trans_kmalloc(trans, sizeof(*g));
709 ret = PTR_ERR_OR_ZERO(g);
710 if (ret)
711 return ret;
712
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400713 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_bucket_gens, pos,
714 BTREE_ITER_INTENT|
715 BTREE_ITER_WITH_UPDATES);
Kent Overstreet5250b742022-11-25 23:14:30 -0500716 ret = bkey_err(k);
717 if (ret)
Kent Overstreetbcb79a52023-04-29 19:33:09 -0400718 return ret;
Kent Overstreet5250b742022-11-25 23:14:30 -0500719
720 if (k.k->type != KEY_TYPE_bucket_gens) {
721 bkey_bucket_gens_init(&g->k_i);
722 g->k.p = iter.pos;
723 } else {
724 bkey_reassemble(&g->k_i, k);
725 }
726
727 g->v.gens[offset] = gen;
728
729 ret = bch2_trans_update(trans, &iter, &g->k_i, 0);
Kent Overstreet5250b742022-11-25 23:14:30 -0500730 bch2_trans_iter_exit(trans, &iter);
731 return ret;
732}
733
Kent Overstreet153d1c62023-12-28 01:21:01 -0500734int bch2_trigger_alloc(struct btree_trans *trans,
735 enum btree_id btree, unsigned level,
736 struct bkey_s_c old, struct bkey_s new,
737 unsigned flags)
Kent Overstreetc6b28262021-12-11 17:13:09 -0500738{
739 struct bch_fs *c = trans->c;
Kent Overstreetc6b28262021-12-11 17:13:09 -0500740 int ret = 0;
741
Kent Overstreet6820ac22023-12-28 00:59:17 -0500742 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
743 "alloc key for invalid device or bucket"))
744 return -EIO;
745
Kent Overstreet153d1c62023-12-28 01:21:01 -0500746 struct bch_dev *ca = bch_dev_bkey_exists(c, new.k->p.inode);
Kent Overstreet6820ac22023-12-28 00:59:17 -0500747
Kent Overstreet153d1c62023-12-28 01:21:01 -0500748 struct bch_alloc_v4 old_a_convert;
749 const struct bch_alloc_v4 *old_a = bch2_alloc_to_v4(old, &old_a_convert);
Kent Overstreet6820ac22023-12-28 00:59:17 -0500750
Kent Overstreet153d1c62023-12-28 01:21:01 -0500751 if (flags & BTREE_TRIGGER_TRANSACTIONAL) {
752 struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
Kent Overstreet6820ac22023-12-28 00:59:17 -0500753
Kent Overstreet153d1c62023-12-28 01:21:01 -0500754 new_a->data_type = alloc_data_type(*new_a, new_a->data_type);
Kent Overstreet6820ac22023-12-28 00:59:17 -0500755
Kent Overstreet153d1c62023-12-28 01:21:01 -0500756 if (bch2_bucket_sectors(*new_a) > bch2_bucket_sectors(*old_a)) {
757 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
758 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
759 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
760 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
761 }
762
763 if (data_type_is_empty(new_a->data_type) &&
764 BCH_ALLOC_V4_NEED_INC_GEN(new_a) &&
765 !bch2_bucket_is_open_safe(c, new.k->p.inode, new.k->p.offset)) {
766 new_a->gen++;
767 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
768 }
769
770 if (old_a->data_type != new_a->data_type ||
771 (new_a->data_type == BCH_DATA_free &&
772 alloc_freespace_genbits(*old_a) != alloc_freespace_genbits(*new_a))) {
773 ret = bch2_bucket_do_index(trans, old, old_a, false) ?:
774 bch2_bucket_do_index(trans, new.s_c, new_a, true);
775 if (ret)
776 return ret;
777 }
778
779 if (new_a->data_type == BCH_DATA_cached &&
780 !new_a->io_time[READ])
781 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
782
783 u64 old_lru = alloc_lru_idx_read(*old_a);
784 u64 new_lru = alloc_lru_idx_read(*new_a);
785 if (old_lru != new_lru) {
786 ret = bch2_lru_change(trans, new.k->p.inode,
787 bucket_to_u64(new.k->p),
788 old_lru, new_lru);
789 if (ret)
790 return ret;
791 }
792
793 new_a->fragmentation_lru = alloc_lru_idx_fragmentation(*new_a,
794 bch_dev_bkey_exists(c, new.k->p.inode));
795 if (old_a->fragmentation_lru != new_a->fragmentation_lru) {
796 ret = bch2_lru_change(trans,
797 BCH_LRU_FRAGMENTATION_START,
798 bucket_to_u64(new.k->p),
799 old_a->fragmentation_lru, new_a->fragmentation_lru);
800 if (ret)
801 return ret;
802 }
803
804 if (old_a->gen != new_a->gen) {
805 ret = bch2_bucket_gen_update(trans, new.k->p, new_a->gen);
806 if (ret)
807 return ret;
808 }
Kent Overstreet6820ac22023-12-28 00:59:17 -0500809
810 /*
Kent Overstreet153d1c62023-12-28 01:21:01 -0500811 * need to know if we're getting called from the invalidate path or
812 * not:
Kent Overstreet6820ac22023-12-28 00:59:17 -0500813 */
Kent Overstreet6820ac22023-12-28 00:59:17 -0500814
Kent Overstreet153d1c62023-12-28 01:21:01 -0500815 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
816 old_a->cached_sectors) {
817 ret = bch2_update_cached_sectors_list(trans, new.k->p.inode,
818 -((s64) old_a->cached_sectors));
819 if (ret)
820 return ret;
Kent Overstreet6820ac22023-12-28 00:59:17 -0500821 }
822 }
823
Kent Overstreet38c23fb2024-01-07 17:14:46 -0500824 if ((flags & BTREE_TRIGGER_ATOMIC) && (flags & BTREE_TRIGGER_INSERT)) {
Kent Overstreet153d1c62023-12-28 01:21:01 -0500825 struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
826 u64 journal_seq = trans->journal_res.seq;
827 u64 bucket_journal_seq = new_a->journal_seq;
Kent Overstreet6820ac22023-12-28 00:59:17 -0500828
Kent Overstreet153d1c62023-12-28 01:21:01 -0500829 if ((flags & BTREE_TRIGGER_INSERT) &&
830 data_type_is_empty(old_a->data_type) !=
831 data_type_is_empty(new_a->data_type) &&
832 new.k->type == KEY_TYPE_alloc_v4) {
833 struct bch_alloc_v4 *v = bkey_s_to_alloc_v4(new).v;
Kent Overstreet6820ac22023-12-28 00:59:17 -0500834
Kent Overstreet153d1c62023-12-28 01:21:01 -0500835 /*
836 * If the btree updates referring to a bucket weren't flushed
837 * before the bucket became empty again, then the we don't have
838 * to wait on a journal flush before we can reuse the bucket:
839 */
840 v->journal_seq = bucket_journal_seq =
841 data_type_is_empty(new_a->data_type) &&
842 (journal_seq == v->journal_seq ||
843 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
844 ? 0 : journal_seq;
845 }
846
847 if (!data_type_is_empty(old_a->data_type) &&
848 data_type_is_empty(new_a->data_type) &&
849 bucket_journal_seq) {
850 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
851 c->journal.flushed_seq_ondisk,
852 new.k->p.inode, new.k->p.offset,
853 bucket_journal_seq);
854 if (ret) {
855 bch2_fs_fatal_error(c,
Kent Overstreet3ed94062024-03-17 21:51:19 -0400856 "setting bucket_needs_journal_commit: %s", bch2_err_str(ret));
Kent Overstreet153d1c62023-12-28 01:21:01 -0500857 return ret;
858 }
859 }
860
861 percpu_down_read(&c->mark_lock);
862 if (new_a->gen != old_a->gen)
863 *bucket_gen(ca, new.k->p.offset) = new_a->gen;
864
865 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, false);
Kent Overstreet6e9d0552024-02-15 21:42:10 -0500866 percpu_up_read(&c->mark_lock);
Kent Overstreet153d1c62023-12-28 01:21:01 -0500867
Kent Overstreet6e9d0552024-02-15 21:42:10 -0500868#define eval_state(_a, expr) ({ const struct bch_alloc_v4 *a = _a; expr; })
869#define statechange(expr) !eval_state(old_a, expr) && eval_state(new_a, expr)
870#define bucket_flushed(a) (!a->journal_seq || a->journal_seq <= c->journal.flushed_seq_ondisk)
871
Kent Overstreeta393f332024-02-16 01:08:25 -0500872 if (statechange(a->data_type == BCH_DATA_free) &&
873 bucket_flushed(new_a))
Kent Overstreet153d1c62023-12-28 01:21:01 -0500874 closure_wake_up(&c->freelist_wait);
875
Kent Overstreeta393f332024-02-16 01:08:25 -0500876 if (statechange(a->data_type == BCH_DATA_need_discard) &&
877 !bch2_bucket_is_open(c, new.k->p.inode, new.k->p.offset) &&
878 bucket_flushed(new_a))
879 bch2_discard_one_bucket_fast(c, new.k->p);
Kent Overstreet153d1c62023-12-28 01:21:01 -0500880
Kent Overstreet6e9d0552024-02-15 21:42:10 -0500881 if (statechange(a->data_type == BCH_DATA_cached) &&
882 !bch2_bucket_is_open(c, new.k->p.inode, new.k->p.offset) &&
Kent Overstreet153d1c62023-12-28 01:21:01 -0500883 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
884 bch2_do_invalidates(c);
885
Kent Overstreet6e9d0552024-02-15 21:42:10 -0500886 if (statechange(a->data_type == BCH_DATA_need_gc_gens))
Kent Overstreet153d1c62023-12-28 01:21:01 -0500887 bch2_do_gc_gens(c);
Kent Overstreet153d1c62023-12-28 01:21:01 -0500888 }
889
890 if ((flags & BTREE_TRIGGER_GC) &&
891 (flags & BTREE_TRIGGER_BUCKET_INVALIDATE)) {
892 struct bch_alloc_v4 new_a_convert;
893 const struct bch_alloc_v4 *new_a = bch2_alloc_to_v4(new.s_c, &new_a_convert);
894
895 percpu_down_read(&c->mark_lock);
Kent Overstreet6820ac22023-12-28 00:59:17 -0500896 struct bucket *g = gc_bucket(ca, new.k->p.offset);
897
898 bucket_lock(g);
899
900 g->gen_valid = 1;
901 g->gen = new_a->gen;
902 g->data_type = new_a->data_type;
903 g->stripe = new_a->stripe;
904 g->stripe_redundancy = new_a->stripe_redundancy;
905 g->dirty_sectors = new_a->dirty_sectors;
906 g->cached_sectors = new_a->cached_sectors;
907
908 bucket_unlock(g);
Kent Overstreet153d1c62023-12-28 01:21:01 -0500909 percpu_up_read(&c->mark_lock);
Kent Overstreet6820ac22023-12-28 00:59:17 -0500910 }
Kent Overstreet6820ac22023-12-28 00:59:17 -0500911
912 return 0;
913}
914
Kent Overstreetd23124c2022-11-30 13:25:17 -0500915/*
916 * This synthesizes deleted extents for holes, similar to BTREE_ITER_SLOTS for
917 * extents style btrees, but works on non-extents btrees:
918 */
Kent Overstreet73bd7742023-07-06 22:47:42 -0400919static struct bkey_s_c bch2_get_key_or_hole(struct btree_iter *iter, struct bpos end, struct bkey *hole)
Kent Overstreetd23124c2022-11-30 13:25:17 -0500920{
921 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
922
923 if (bkey_err(k))
924 return k;
925
926 if (k.k->type) {
927 return k;
928 } else {
929 struct btree_iter iter2;
930 struct bpos next;
931
932 bch2_trans_copy_iter(&iter2, iter);
Kent Overstreet1546cf92023-03-28 19:37:25 -0400933
Kent Overstreet07f383c2023-12-04 00:39:38 -0500934 struct btree_path *path = btree_iter_path(iter->trans, iter);
935 if (!bpos_eq(path->l[0].b->key.k.p, SPOS_MAX))
936 end = bkey_min(end, bpos_nosnap_successor(path->l[0].b->key.k.p));
Kent Overstreet1546cf92023-03-28 19:37:25 -0400937
938 end = bkey_min(end, POS(iter->pos.inode, iter->pos.offset + U32_MAX - 1));
939
940 /*
941 * btree node min/max is a closed interval, upto takes a half
942 * open interval:
943 */
944 k = bch2_btree_iter_peek_upto(&iter2, end);
Kent Overstreetd23124c2022-11-30 13:25:17 -0500945 next = iter2.pos;
946 bch2_trans_iter_exit(iter->trans, &iter2);
947
948 BUG_ON(next.offset >= iter->pos.offset + U32_MAX);
949
950 if (bkey_err(k))
951 return k;
952
953 bkey_init(hole);
954 hole->p = iter->pos;
955
956 bch2_key_resize(hole, next.offset - iter->pos.offset);
957 return (struct bkey_s_c) { hole, NULL };
958 }
959}
960
961static bool next_bucket(struct bch_fs *c, struct bpos *bucket)
962{
963 struct bch_dev *ca;
Kent Overstreetd23124c2022-11-30 13:25:17 -0500964
965 if (bch2_dev_bucket_exists(c, *bucket))
966 return true;
967
968 if (bch2_dev_exists2(c, bucket->inode)) {
969 ca = bch_dev_bkey_exists(c, bucket->inode);
970
971 if (bucket->offset < ca->mi.first_bucket) {
972 bucket->offset = ca->mi.first_bucket;
973 return true;
974 }
975
976 bucket->inode++;
977 bucket->offset = 0;
978 }
979
980 rcu_read_lock();
Kent Overstreet41b84fb2023-12-17 02:34:05 -0500981 ca = __bch2_next_dev_idx(c, bucket->inode, NULL);
Kent Overstreetd23124c2022-11-30 13:25:17 -0500982 if (ca)
Kent Overstreet46e14852023-03-11 15:52:37 -0500983 *bucket = POS(ca->dev_idx, ca->mi.first_bucket);
Kent Overstreetd23124c2022-11-30 13:25:17 -0500984 rcu_read_unlock();
985
986 return ca != NULL;
987}
988
Kent Overstreet73bd7742023-07-06 22:47:42 -0400989static struct bkey_s_c bch2_get_key_or_real_bucket_hole(struct btree_iter *iter, struct bkey *hole)
Kent Overstreetd23124c2022-11-30 13:25:17 -0500990{
991 struct bch_fs *c = iter->trans->c;
992 struct bkey_s_c k;
993again:
994 k = bch2_get_key_or_hole(iter, POS_MAX, hole);
995 if (bkey_err(k))
996 return k;
997
998 if (!k.k->type) {
999 struct bpos bucket = bkey_start_pos(k.k);
1000
1001 if (!bch2_dev_bucket_exists(c, bucket)) {
1002 if (!next_bucket(c, &bucket))
1003 return bkey_s_c_null;
1004
1005 bch2_btree_iter_set_pos(iter, bucket);
1006 goto again;
1007 }
1008
1009 if (!bch2_dev_bucket_exists(c, k.k->p)) {
1010 struct bch_dev *ca = bch_dev_bkey_exists(c, bucket.inode);
1011
1012 bch2_key_resize(hole, ca->mi.nbuckets - bucket.offset);
1013 }
1014 }
1015
1016 return k;
1017}
1018
Kent Overstreet298ac242023-06-26 22:26:04 -04001019static noinline_for_stack
1020int bch2_check_alloc_key(struct btree_trans *trans,
1021 struct bkey_s_c alloc_k,
1022 struct btree_iter *alloc_iter,
1023 struct btree_iter *discard_iter,
1024 struct btree_iter *freespace_iter,
1025 struct btree_iter *bucket_gens_iter)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001026{
1027 struct bch_fs *c = trans->c;
Kent Overstreete1effd42022-04-05 13:44:18 -04001028 struct bch_dev *ca;
Kent Overstreet19a614d2023-01-30 20:58:43 -05001029 struct bch_alloc_v4 a_convert;
1030 const struct bch_alloc_v4 *a;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001031 unsigned discard_key_type, freespace_key_type;
Kent Overstreet5250b742022-11-25 23:14:30 -05001032 unsigned gens_offset;
Kent Overstreetd23124c2022-11-30 13:25:17 -05001033 struct bkey_s_c k;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001034 struct printbuf buf = PRINTBUF;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001035 int ret;
1036
Kent Overstreete1effd42022-04-05 13:44:18 -04001037 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_k.k->p), c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001038 alloc_key_to_missing_dev_bucket,
Kent Overstreeta9c0a4c2022-04-09 15:15:36 -04001039 "alloc key for invalid device:bucket %llu:%llu",
1040 alloc_k.k->p.inode, alloc_k.k->p.offset))
Kent Overstreete1effd42022-04-05 13:44:18 -04001041 return bch2_btree_delete_at(trans, alloc_iter, 0);
1042
1043 ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
1044 if (!ca->mi.freespace_initialized)
1045 return 0;
1046
Kent Overstreet19a614d2023-01-30 20:58:43 -05001047 a = bch2_alloc_to_v4(alloc_k, &a_convert);
Kent Overstreete1effd42022-04-05 13:44:18 -04001048
Kent Overstreet5250b742022-11-25 23:14:30 -05001049 discard_key_type = a->data_type == BCH_DATA_need_discard ? KEY_TYPE_set : 0;
Kent Overstreete34da432022-06-19 22:43:00 -04001050 bch2_btree_iter_set_pos(discard_iter, alloc_k.k->p);
Kent Overstreete34da432022-06-19 22:43:00 -04001051 k = bch2_btree_iter_peek_slot(discard_iter);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001052 ret = bkey_err(k);
1053 if (ret)
1054 goto err;
1055
Kent Overstreetcdce1092024-03-11 21:15:26 -04001056 if (fsck_err_on(k.k->type != discard_key_type,
1057 c, need_discard_key_wrong,
1058 "incorrect key in need_discard btree (got %s should be %s)\n"
1059 " %s",
1060 bch2_bkey_types[k.k->type],
1061 bch2_bkey_types[discard_key_type],
1062 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
Kent Overstreet5add07d2022-02-17 03:11:39 -05001063 struct bkey_i *update =
1064 bch2_trans_kmalloc(trans, sizeof(*update));
1065
1066 ret = PTR_ERR_OR_ZERO(update);
1067 if (ret)
1068 goto err;
1069
1070 bkey_init(&update->k);
1071 update->k.type = discard_key_type;
Kent Overstreete34da432022-06-19 22:43:00 -04001072 update->k.p = discard_iter->pos;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001073
Kent Overstreete34da432022-06-19 22:43:00 -04001074 ret = bch2_trans_update(trans, discard_iter, update, 0);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001075 if (ret)
1076 goto err;
1077 }
1078
Kent Overstreet5250b742022-11-25 23:14:30 -05001079 freespace_key_type = a->data_type == BCH_DATA_free ? KEY_TYPE_set : 0;
1080 bch2_btree_iter_set_pos(freespace_iter, alloc_freespace_pos(alloc_k.k->p, *a));
Kent Overstreete34da432022-06-19 22:43:00 -04001081 k = bch2_btree_iter_peek_slot(freespace_iter);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001082 ret = bkey_err(k);
1083 if (ret)
1084 goto err;
1085
Kent Overstreetcdce1092024-03-11 21:15:26 -04001086 if (fsck_err_on(k.k->type != freespace_key_type,
1087 c, freespace_key_wrong,
1088 "incorrect key in freespace btree (got %s should be %s)\n"
1089 " %s",
1090 bch2_bkey_types[k.k->type],
1091 bch2_bkey_types[freespace_key_type],
1092 (printbuf_reset(&buf),
1093 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
Kent Overstreet5add07d2022-02-17 03:11:39 -05001094 struct bkey_i *update =
1095 bch2_trans_kmalloc(trans, sizeof(*update));
1096
1097 ret = PTR_ERR_OR_ZERO(update);
1098 if (ret)
1099 goto err;
1100
1101 bkey_init(&update->k);
1102 update->k.type = freespace_key_type;
Kent Overstreete34da432022-06-19 22:43:00 -04001103 update->k.p = freespace_iter->pos;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001104 bch2_key_resize(&update->k, 1);
1105
Kent Overstreete34da432022-06-19 22:43:00 -04001106 ret = bch2_trans_update(trans, freespace_iter, update, 0);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001107 if (ret)
1108 goto err;
1109 }
Kent Overstreet5250b742022-11-25 23:14:30 -05001110
1111 bch2_btree_iter_set_pos(bucket_gens_iter, alloc_gens_pos(alloc_k.k->p, &gens_offset));
1112 k = bch2_btree_iter_peek_slot(bucket_gens_iter);
1113 ret = bkey_err(k);
1114 if (ret)
1115 goto err;
1116
Kent Overstreetcdce1092024-03-11 21:15:26 -04001117 if (fsck_err_on(a->gen != alloc_gen(k, gens_offset),
1118 c, bucket_gens_key_wrong,
1119 "incorrect gen in bucket_gens btree (got %u should be %u)\n"
1120 " %s",
1121 alloc_gen(k, gens_offset), a->gen,
1122 (printbuf_reset(&buf),
1123 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
Kent Overstreet5250b742022-11-25 23:14:30 -05001124 struct bkey_i_bucket_gens *g =
1125 bch2_trans_kmalloc(trans, sizeof(*g));
1126
1127 ret = PTR_ERR_OR_ZERO(g);
1128 if (ret)
1129 goto err;
1130
1131 if (k.k->type == KEY_TYPE_bucket_gens) {
1132 bkey_reassemble(&g->k_i, k);
1133 } else {
1134 bkey_bucket_gens_init(&g->k_i);
1135 g->k.p = alloc_gens_pos(alloc_k.k->p, &gens_offset);
1136 }
1137
1138 g->v.gens[gens_offset] = a->gen;
1139
1140 ret = bch2_trans_update(trans, bucket_gens_iter, &g->k_i, 0);
1141 if (ret)
1142 goto err;
1143 }
Kent Overstreet5add07d2022-02-17 03:11:39 -05001144err:
1145fsck_err:
Kent Overstreet5add07d2022-02-17 03:11:39 -05001146 printbuf_exit(&buf);
1147 return ret;
1148}
1149
Kent Overstreet298ac242023-06-26 22:26:04 -04001150static noinline_for_stack
1151int bch2_check_alloc_hole_freespace(struct btree_trans *trans,
1152 struct bpos start,
1153 struct bpos *end,
1154 struct btree_iter *freespace_iter)
Kent Overstreetd23124c2022-11-30 13:25:17 -05001155{
1156 struct bch_fs *c = trans->c;
1157 struct bch_dev *ca;
1158 struct bkey_s_c k;
1159 struct printbuf buf = PRINTBUF;
1160 int ret;
1161
1162 ca = bch_dev_bkey_exists(c, start.inode);
1163 if (!ca->mi.freespace_initialized)
1164 return 0;
1165
1166 bch2_btree_iter_set_pos(freespace_iter, start);
1167
1168 k = bch2_btree_iter_peek_slot(freespace_iter);
1169 ret = bkey_err(k);
1170 if (ret)
1171 goto err;
1172
1173 *end = bkey_min(k.k->p, *end);
1174
Kent Overstreetcdce1092024-03-11 21:15:26 -04001175 if (fsck_err_on(k.k->type != KEY_TYPE_set,
1176 c, freespace_hole_missing,
1177 "hole in alloc btree missing in freespace btree\n"
1178 " device %llu buckets %llu-%llu",
1179 freespace_iter->pos.inode,
1180 freespace_iter->pos.offset,
1181 end->offset)) {
Kent Overstreetd23124c2022-11-30 13:25:17 -05001182 struct bkey_i *update =
1183 bch2_trans_kmalloc(trans, sizeof(*update));
1184
1185 ret = PTR_ERR_OR_ZERO(update);
1186 if (ret)
1187 goto err;
1188
1189 bkey_init(&update->k);
1190 update->k.type = KEY_TYPE_set;
1191 update->k.p = freespace_iter->pos;
1192 bch2_key_resize(&update->k,
1193 min_t(u64, U32_MAX, end->offset -
1194 freespace_iter->pos.offset));
1195
1196 ret = bch2_trans_update(trans, freespace_iter, update, 0);
1197 if (ret)
1198 goto err;
1199 }
1200err:
1201fsck_err:
1202 printbuf_exit(&buf);
1203 return ret;
1204}
1205
Kent Overstreet298ac242023-06-26 22:26:04 -04001206static noinline_for_stack
1207int bch2_check_alloc_hole_bucket_gens(struct btree_trans *trans,
1208 struct bpos start,
1209 struct bpos *end,
1210 struct btree_iter *bucket_gens_iter)
Kent Overstreet5250b742022-11-25 23:14:30 -05001211{
1212 struct bch_fs *c = trans->c;
1213 struct bkey_s_c k;
1214 struct printbuf buf = PRINTBUF;
1215 unsigned i, gens_offset, gens_end_offset;
1216 int ret;
1217
Kent Overstreet5250b742022-11-25 23:14:30 -05001218 bch2_btree_iter_set_pos(bucket_gens_iter, alloc_gens_pos(start, &gens_offset));
1219
1220 k = bch2_btree_iter_peek_slot(bucket_gens_iter);
1221 ret = bkey_err(k);
1222 if (ret)
1223 goto err;
1224
1225 if (bkey_cmp(alloc_gens_pos(start, &gens_offset),
1226 alloc_gens_pos(*end, &gens_end_offset)))
1227 gens_end_offset = KEY_TYPE_BUCKET_GENS_NR;
1228
1229 if (k.k->type == KEY_TYPE_bucket_gens) {
1230 struct bkey_i_bucket_gens g;
1231 bool need_update = false;
1232
1233 bkey_reassemble(&g.k_i, k);
1234
1235 for (i = gens_offset; i < gens_end_offset; i++) {
1236 if (fsck_err_on(g.v.gens[i], c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001237 bucket_gens_hole_wrong,
Kent Overstreet5250b742022-11-25 23:14:30 -05001238 "hole in alloc btree at %llu:%llu with nonzero gen in bucket_gens btree (%u)",
1239 bucket_gens_pos_to_alloc(k.k->p, i).inode,
1240 bucket_gens_pos_to_alloc(k.k->p, i).offset,
1241 g.v.gens[i])) {
1242 g.v.gens[i] = 0;
1243 need_update = true;
1244 }
1245 }
1246
1247 if (need_update) {
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001248 struct bkey_i *u = bch2_trans_kmalloc(trans, sizeof(g));
Kent Overstreet5250b742022-11-25 23:14:30 -05001249
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001250 ret = PTR_ERR_OR_ZERO(u);
Kent Overstreet5250b742022-11-25 23:14:30 -05001251 if (ret)
1252 goto err;
1253
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001254 memcpy(u, &g, sizeof(g));
Kent Overstreet5250b742022-11-25 23:14:30 -05001255
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001256 ret = bch2_trans_update(trans, bucket_gens_iter, u, 0);
Kent Overstreet5250b742022-11-25 23:14:30 -05001257 if (ret)
1258 goto err;
1259 }
1260 }
1261
1262 *end = bkey_min(*end, bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0));
1263err:
1264fsck_err:
1265 printbuf_exit(&buf);
1266 return ret;
1267}
1268
Kent Overstreet7d9ae042023-11-16 17:28:16 -05001269static noinline_for_stack int bch2_check_discard_freespace_key(struct btree_trans *trans,
Kent Overstreete96f5a62023-06-18 13:25:35 -04001270 struct btree_iter *iter)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001271{
1272 struct bch_fs *c = trans->c;
1273 struct btree_iter alloc_iter;
Kent Overstreet445d1842022-07-17 00:44:19 -04001274 struct bkey_s_c alloc_k;
Kent Overstreet19a614d2023-01-30 20:58:43 -05001275 struct bch_alloc_v4 a_convert;
1276 const struct bch_alloc_v4 *a;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001277 u64 genbits;
1278 struct bpos pos;
Kent Overstreet822835f2022-04-01 01:29:59 -04001279 enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
1280 ? BCH_DATA_need_discard
1281 : BCH_DATA_free;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001282 struct printbuf buf = PRINTBUF;
1283 int ret;
1284
Kent Overstreete1effd42022-04-05 13:44:18 -04001285 pos = iter->pos;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001286 pos.offset &= ~(~0ULL << 56);
Kent Overstreete1effd42022-04-05 13:44:18 -04001287 genbits = iter->pos.offset & (~0ULL << 56);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001288
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001289 alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
1290 ret = bkey_err(alloc_k);
1291 if (ret)
1292 return ret;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001293
1294 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001295 need_discard_freespace_key_to_invalid_dev_bucket,
Kent Overstreeta9c0a4c2022-04-09 15:15:36 -04001296 "entry in %s btree for nonexistant dev:bucket %llu:%llu",
Kent Overstreet88dfe192023-10-19 22:49:08 -04001297 bch2_btree_id_str(iter->btree_id), pos.inode, pos.offset))
Kent Overstreet5add07d2022-02-17 03:11:39 -05001298 goto delete;
1299
Kent Overstreet19a614d2023-01-30 20:58:43 -05001300 a = bch2_alloc_to_v4(alloc_k, &a_convert);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001301
Kent Overstreet19a614d2023-01-30 20:58:43 -05001302 if (fsck_err_on(a->data_type != state ||
Kent Overstreet822835f2022-04-01 01:29:59 -04001303 (state == BCH_DATA_free &&
Kent Overstreet19a614d2023-01-30 20:58:43 -05001304 genbits != alloc_freespace_genbits(*a)), c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001305 need_discard_freespace_key_bad,
Kent Overstreete96f5a62023-06-18 13:25:35 -04001306 "%s\n incorrectly set at %s:%llu:%llu:0 (free %u, genbits %llu should be %llu)",
Kent Overstreet445d1842022-07-17 00:44:19 -04001307 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
Kent Overstreet88dfe192023-10-19 22:49:08 -04001308 bch2_btree_id_str(iter->btree_id),
Kent Overstreete96f5a62023-06-18 13:25:35 -04001309 iter->pos.inode,
1310 iter->pos.offset,
Kent Overstreet19a614d2023-01-30 20:58:43 -05001311 a->data_type == state,
1312 genbits >> 56, alloc_freespace_genbits(*a) >> 56))
Kent Overstreet5add07d2022-02-17 03:11:39 -05001313 goto delete;
1314out:
Kent Overstreet5add07d2022-02-17 03:11:39 -05001315fsck_err:
Kent Overstreete96f5a62023-06-18 13:25:35 -04001316 set_btree_iter_dontneed(&alloc_iter);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001317 bch2_trans_iter_exit(trans, &alloc_iter);
1318 printbuf_exit(&buf);
1319 return ret;
1320delete:
Kent Overstreete96f5a62023-06-18 13:25:35 -04001321 ret = bch2_btree_delete_extent_at(trans, iter,
1322 iter->btree_id == BTREE_ID_freespace ? 1 : 0, 0) ?:
1323 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreet3f0e2972023-11-28 16:36:54 -05001324 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001325 goto out;
1326}
1327
Kent Overstreet5250b742022-11-25 23:14:30 -05001328/*
1329 * We've already checked that generation numbers in the bucket_gens btree are
1330 * valid for buckets that exist; this just checks for keys for nonexistent
1331 * buckets.
1332 */
Kent Overstreet298ac242023-06-26 22:26:04 -04001333static noinline_for_stack
1334int bch2_check_bucket_gens_key(struct btree_trans *trans,
1335 struct btree_iter *iter,
1336 struct bkey_s_c k)
Kent Overstreet5250b742022-11-25 23:14:30 -05001337{
1338 struct bch_fs *c = trans->c;
1339 struct bkey_i_bucket_gens g;
1340 struct bch_dev *ca;
1341 u64 start = bucket_gens_pos_to_alloc(k.k->p, 0).offset;
1342 u64 end = bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0).offset;
1343 u64 b;
Brian Foster251babb2023-04-18 13:05:47 -04001344 bool need_update = false, dev_exists;
Kent Overstreet5250b742022-11-25 23:14:30 -05001345 struct printbuf buf = PRINTBUF;
1346 int ret = 0;
1347
1348 BUG_ON(k.k->type != KEY_TYPE_bucket_gens);
1349 bkey_reassemble(&g.k_i, k);
1350
Brian Foster251babb2023-04-18 13:05:47 -04001351 /* if no bch_dev, skip out whether we repair or not */
1352 dev_exists = bch2_dev_exists2(c, k.k->p.inode);
1353 if (!dev_exists) {
1354 if (fsck_err_on(!dev_exists, c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001355 bucket_gens_to_invalid_dev,
Brian Foster251babb2023-04-18 13:05:47 -04001356 "bucket_gens key for invalid device:\n %s",
1357 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1358 ret = bch2_btree_delete_at(trans, iter, 0);
1359 }
Kent Overstreet5250b742022-11-25 23:14:30 -05001360 goto out;
1361 }
1362
1363 ca = bch_dev_bkey_exists(c, k.k->p.inode);
1364 if (fsck_err_on(end <= ca->mi.first_bucket ||
1365 start >= ca->mi.nbuckets, c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001366 bucket_gens_to_invalid_buckets,
Kent Overstreet5250b742022-11-25 23:14:30 -05001367 "bucket_gens key for invalid buckets:\n %s",
1368 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1369 ret = bch2_btree_delete_at(trans, iter, 0);
1370 goto out;
1371 }
1372
1373 for (b = start; b < ca->mi.first_bucket; b++)
1374 if (fsck_err_on(g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK], c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001375 bucket_gens_nonzero_for_invalid_buckets,
Kent Overstreet5250b742022-11-25 23:14:30 -05001376 "bucket_gens key has nonzero gen for invalid bucket")) {
1377 g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK] = 0;
1378 need_update = true;
1379 }
1380
1381 for (b = ca->mi.nbuckets; b < end; b++)
1382 if (fsck_err_on(g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK], c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001383 bucket_gens_nonzero_for_invalid_buckets,
Kent Overstreet5250b742022-11-25 23:14:30 -05001384 "bucket_gens key has nonzero gen for invalid bucket")) {
1385 g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK] = 0;
1386 need_update = true;
1387 }
1388
1389 if (need_update) {
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001390 struct bkey_i *u = bch2_trans_kmalloc(trans, sizeof(g));
Kent Overstreet5250b742022-11-25 23:14:30 -05001391
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001392 ret = PTR_ERR_OR_ZERO(u);
Kent Overstreet5250b742022-11-25 23:14:30 -05001393 if (ret)
1394 goto out;
1395
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001396 memcpy(u, &g, sizeof(g));
1397 ret = bch2_trans_update(trans, iter, u, 0);
Kent Overstreet5250b742022-11-25 23:14:30 -05001398 }
1399out:
1400fsck_err:
1401 printbuf_exit(&buf);
1402 return ret;
1403}
1404
Kent Overstreete1effd42022-04-05 13:44:18 -04001405int bch2_check_alloc_info(struct bch_fs *c)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001406{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001407 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet5250b742022-11-25 23:14:30 -05001408 struct btree_iter iter, discard_iter, freespace_iter, bucket_gens_iter;
Kent Overstreetd23124c2022-11-30 13:25:17 -05001409 struct bkey hole;
Kent Overstreet445d1842022-07-17 00:44:19 -04001410 struct bkey_s_c k;
Kent Overstreete1effd42022-04-05 13:44:18 -04001411 int ret = 0;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001412
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001413 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS_MIN,
Kent Overstreete34da432022-06-19 22:43:00 -04001414 BTREE_ITER_PREFETCH);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001415 bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard, POS_MIN,
Kent Overstreete34da432022-06-19 22:43:00 -04001416 BTREE_ITER_PREFETCH);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001417 bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace, POS_MIN,
Kent Overstreete34da432022-06-19 22:43:00 -04001418 BTREE_ITER_PREFETCH);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001419 bch2_trans_iter_init(trans, &bucket_gens_iter, BTREE_ID_bucket_gens, POS_MIN,
Kent Overstreet5250b742022-11-25 23:14:30 -05001420 BTREE_ITER_PREFETCH);
Kent Overstreetd23124c2022-11-30 13:25:17 -05001421
Kent Overstreete34da432022-06-19 22:43:00 -04001422 while (1) {
Kent Overstreetd23124c2022-11-30 13:25:17 -05001423 struct bpos next;
1424
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001425 bch2_trans_begin(trans);
Kent Overstreetd23124c2022-11-30 13:25:17 -05001426
1427 k = bch2_get_key_or_real_bucket_hole(&iter, &hole);
1428 ret = bkey_err(k);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001429 if (ret)
Kent Overstreetd23124c2022-11-30 13:25:17 -05001430 goto bkey_err;
1431
1432 if (!k.k)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001433 break;
Kent Overstreete34da432022-06-19 22:43:00 -04001434
Kent Overstreetd23124c2022-11-30 13:25:17 -05001435 if (k.k->type) {
1436 next = bpos_nosnap_successor(k.k->p);
1437
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001438 ret = bch2_check_alloc_key(trans,
Kent Overstreetd23124c2022-11-30 13:25:17 -05001439 k, &iter,
1440 &discard_iter,
Kent Overstreet5250b742022-11-25 23:14:30 -05001441 &freespace_iter,
1442 &bucket_gens_iter);
Kent Overstreetd23124c2022-11-30 13:25:17 -05001443 if (ret)
Kent Overstreet5250b742022-11-25 23:14:30 -05001444 goto bkey_err;
Kent Overstreetd23124c2022-11-30 13:25:17 -05001445 } else {
1446 next = k.k->p;
1447
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001448 ret = bch2_check_alloc_hole_freespace(trans,
Kent Overstreetd23124c2022-11-30 13:25:17 -05001449 bkey_start_pos(k.k),
1450 &next,
Kent Overstreet5250b742022-11-25 23:14:30 -05001451 &freespace_iter) ?:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001452 bch2_check_alloc_hole_bucket_gens(trans,
Kent Overstreet5250b742022-11-25 23:14:30 -05001453 bkey_start_pos(k.k),
1454 &next,
1455 &bucket_gens_iter);
Kent Overstreetd23124c2022-11-30 13:25:17 -05001456 if (ret)
1457 goto bkey_err;
1458 }
1459
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001460 ret = bch2_trans_commit(trans, NULL, NULL,
Kent Overstreet3f0e2972023-11-28 16:36:54 -05001461 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreetd23124c2022-11-30 13:25:17 -05001462 if (ret)
1463 goto bkey_err;
1464
1465 bch2_btree_iter_set_pos(&iter, next);
1466bkey_err:
1467 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1468 continue;
1469 if (ret)
1470 break;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001471 }
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001472 bch2_trans_iter_exit(trans, &bucket_gens_iter);
1473 bch2_trans_iter_exit(trans, &freespace_iter);
1474 bch2_trans_iter_exit(trans, &discard_iter);
1475 bch2_trans_iter_exit(trans, &iter);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001476
Kent Overstreete34da432022-06-19 22:43:00 -04001477 if (ret < 0)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001478 goto err;
1479
Kent Overstreet5028b902023-12-07 23:33:11 -05001480 ret = for_each_btree_key(trans, iter,
Kent Overstreet445d1842022-07-17 00:44:19 -04001481 BTREE_ID_need_discard, POS_MIN,
1482 BTREE_ITER_PREFETCH, k,
Kent Overstreet7d9ae042023-11-16 17:28:16 -05001483 bch2_check_discard_freespace_key(trans, &iter));
1484 if (ret)
1485 goto err;
1486
1487 bch2_trans_iter_init(trans, &iter, BTREE_ID_freespace, POS_MIN,
1488 BTREE_ITER_PREFETCH);
1489 while (1) {
1490 bch2_trans_begin(trans);
1491 k = bch2_btree_iter_peek(&iter);
1492 if (!k.k)
1493 break;
1494
1495 ret = bkey_err(k) ?:
1496 bch2_check_discard_freespace_key(trans, &iter);
1497 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1498 ret = 0;
1499 continue;
1500 }
1501 if (ret) {
1502 struct printbuf buf = PRINTBUF;
1503 bch2_bkey_val_to_text(&buf, c, k);
1504
1505 bch_err(c, "while checking %s", buf.buf);
1506 printbuf_exit(&buf);
1507 break;
1508 }
1509
1510 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
1511 }
1512 bch2_trans_iter_exit(trans, &iter);
1513 if (ret)
1514 goto err;
1515
1516 ret = for_each_btree_key_commit(trans, iter,
Kent Overstreet5250b742022-11-25 23:14:30 -05001517 BTREE_ID_bucket_gens, POS_MIN,
1518 BTREE_ITER_PREFETCH, k,
Kent Overstreet3f0e2972023-11-28 16:36:54 -05001519 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001520 bch2_check_bucket_gens_key(trans, &iter, k));
Kent Overstreet5add07d2022-02-17 03:11:39 -05001521err:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001522 bch2_trans_put(trans);
Kent Overstreetcf904c82023-12-16 22:43:41 -05001523 bch_err_fn(c, ret);
Kent Overstreet1bb3c2a2023-06-20 13:49:25 -04001524 return ret;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001525}
1526
1527static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
1528 struct btree_iter *alloc_iter)
1529{
1530 struct bch_fs *c = trans->c;
1531 struct btree_iter lru_iter;
Kent Overstreet19a614d2023-01-30 20:58:43 -05001532 struct bch_alloc_v4 a_convert;
1533 const struct bch_alloc_v4 *a;
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001534 struct bkey_s_c alloc_k, lru_k;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001535 struct printbuf buf = PRINTBUF;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001536 int ret;
1537
1538 alloc_k = bch2_btree_iter_peek(alloc_iter);
1539 if (!alloc_k.k)
1540 return 0;
1541
1542 ret = bkey_err(alloc_k);
1543 if (ret)
1544 return ret;
1545
Kent Overstreet19a614d2023-01-30 20:58:43 -05001546 a = bch2_alloc_to_v4(alloc_k, &a_convert);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001547
Kent Overstreet19a614d2023-01-30 20:58:43 -05001548 if (a->data_type != BCH_DATA_cached)
Kent Overstreet5add07d2022-02-17 03:11:39 -05001549 return 0;
1550
Kent Overstreet3f595472023-11-25 15:46:02 -05001551 if (fsck_err_on(!a->io_time[READ], c,
1552 alloc_key_cached_but_read_time_zero,
1553 "cached bucket with read_time 0\n"
1554 " %s",
1555 (printbuf_reset(&buf),
1556 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1557 struct bkey_i_alloc_v4 *a_mut =
1558 bch2_alloc_to_v4_mut(trans, alloc_k);
1559 ret = PTR_ERR_OR_ZERO(a_mut);
1560 if (ret)
1561 goto err;
1562
1563 a_mut->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1564 ret = bch2_trans_update(trans, alloc_iter,
1565 &a_mut->k_i, BTREE_TRIGGER_NORUN);
1566 if (ret)
1567 goto err;
1568
1569 a = &a_mut->v;
1570 }
1571
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001572 lru_k = bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru,
Kent Overstreet83f33d62022-12-05 16:49:13 -05001573 lru_pos(alloc_k.k->p.inode,
1574 bucket_to_u64(alloc_k.k->p),
1575 a->io_time[READ]), 0);
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001576 ret = bkey_err(lru_k);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001577 if (ret)
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001578 return ret;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001579
Kent Overstreet3f595472023-11-25 15:46:02 -05001580 if (fsck_err_on(lru_k.k->type != KEY_TYPE_set, c,
Kent Overstreetb65db752023-10-24 20:44:36 -04001581 alloc_key_to_missing_lru_entry,
Kent Overstreet83f33d62022-12-05 16:49:13 -05001582 "missing lru entry\n"
Kent Overstreet5add07d2022-02-17 03:11:39 -05001583 " %s",
1584 (printbuf_reset(&buf),
Kent Overstreet83f33d62022-12-05 16:49:13 -05001585 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
Kent Overstreet70035892022-04-10 19:59:26 -04001586 ret = bch2_lru_set(trans,
1587 alloc_k.k->p.inode,
Kent Overstreet83f33d62022-12-05 16:49:13 -05001588 bucket_to_u64(alloc_k.k->p),
Kent Overstreet3f595472023-11-25 15:46:02 -05001589 a->io_time[READ]);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001590 if (ret)
1591 goto err;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001592 }
1593err:
1594fsck_err:
1595 bch2_trans_iter_exit(trans, &lru_iter);
Kent Overstreet5add07d2022-02-17 03:11:39 -05001596 printbuf_exit(&buf);
1597 return ret;
1598}
1599
1600int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
1601{
Kent Overstreet3f595472023-11-25 15:46:02 -05001602 int ret = bch2_trans_run(c,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001603 for_each_btree_key_commit(trans, iter, BTREE_ID_alloc,
Kent Overstreet1bb3c2a2023-06-20 13:49:25 -04001604 POS_MIN, BTREE_ITER_PREFETCH, k,
Kent Overstreet3f0e2972023-11-28 16:36:54 -05001605 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001606 bch2_check_alloc_to_lru_ref(trans, &iter)));
Kent Overstreet3f595472023-11-25 15:46:02 -05001607 bch_err_fn(c, ret);
Kent Overstreet1bb3c2a2023-06-20 13:49:25 -04001608 return ret;
Kent Overstreet5add07d2022-02-17 03:11:39 -05001609}
1610
Kent Overstreeta393f332024-02-16 01:08:25 -05001611static int discard_in_flight_add(struct bch_fs *c, struct bpos bucket)
1612{
1613 int ret;
1614
1615 mutex_lock(&c->discard_buckets_in_flight_lock);
1616 darray_for_each(c->discard_buckets_in_flight, i)
1617 if (bkey_eq(*i, bucket)) {
1618 ret = -EEXIST;
1619 goto out;
1620 }
1621
1622 ret = darray_push(&c->discard_buckets_in_flight, bucket);
1623out:
1624 mutex_unlock(&c->discard_buckets_in_flight_lock);
1625 return ret;
1626}
1627
1628static void discard_in_flight_remove(struct bch_fs *c, struct bpos bucket)
1629{
1630 mutex_lock(&c->discard_buckets_in_flight_lock);
1631 darray_for_each(c->discard_buckets_in_flight, i)
1632 if (bkey_eq(*i, bucket)) {
1633 darray_remove_item(&c->discard_buckets_in_flight, i);
1634 goto found;
1635 }
1636 BUG();
1637found:
1638 mutex_unlock(&c->discard_buckets_in_flight_lock);
1639}
1640
Kent Overstreeta6548c82024-01-15 17:56:22 -05001641struct discard_buckets_state {
1642 u64 seen;
1643 u64 open;
1644 u64 need_journal_commit;
1645 u64 discarded;
1646 struct bch_dev *ca;
1647 u64 need_journal_commit_this_dev;
1648};
1649
1650static void discard_buckets_next_dev(struct bch_fs *c, struct discard_buckets_state *s, struct bch_dev *ca)
1651{
1652 if (s->ca == ca)
1653 return;
1654
1655 if (s->ca && s->need_journal_commit_this_dev >
1656 bch2_dev_usage_read(s->ca).d[BCH_DATA_free].buckets)
1657 bch2_journal_flush_async(&c->journal, NULL);
1658
1659 if (s->ca)
1660 percpu_ref_put(&s->ca->ref);
1661 if (ca)
1662 percpu_ref_get(&ca->ref);
1663 s->ca = ca;
1664 s->need_journal_commit_this_dev = 0;
1665}
1666
Kent Overstreet4910a952022-07-17 00:31:40 -04001667static int bch2_discard_one_bucket(struct btree_trans *trans,
1668 struct btree_iter *need_discard_iter,
1669 struct bpos *discard_pos_done,
Kent Overstreeta6548c82024-01-15 17:56:22 -05001670 struct discard_buckets_state *s)
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001671{
1672 struct bch_fs *c = trans->c;
Kent Overstreet4910a952022-07-17 00:31:40 -04001673 struct bpos pos = need_discard_iter->pos;
1674 struct btree_iter iter = { NULL };
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001675 struct bkey_s_c k;
Kent Overstreet4910a952022-07-17 00:31:40 -04001676 struct bch_dev *ca;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001677 struct bkey_i_alloc_v4 *a;
1678 struct printbuf buf = PRINTBUF;
Kent Overstreeta393f332024-02-16 01:08:25 -05001679 bool discard_locked = false;
Kent Overstreet4910a952022-07-17 00:31:40 -04001680 int ret = 0;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001681
Kent Overstreet4910a952022-07-17 00:31:40 -04001682 ca = bch_dev_bkey_exists(c, pos.inode);
Kent Overstreeta6548c82024-01-15 17:56:22 -05001683
Kent Overstreet4910a952022-07-17 00:31:40 -04001684 if (!percpu_ref_tryget(&ca->io_ref)) {
1685 bch2_btree_iter_set_pos(need_discard_iter, POS(pos.inode + 1, 0));
1686 return 0;
1687 }
1688
Kent Overstreeta6548c82024-01-15 17:56:22 -05001689 discard_buckets_next_dev(c, s, ca);
1690
Kent Overstreet4910a952022-07-17 00:31:40 -04001691 if (bch2_bucket_is_open_safe(c, pos.inode, pos.offset)) {
Kent Overstreeta6548c82024-01-15 17:56:22 -05001692 s->open++;
Kent Overstreet4910a952022-07-17 00:31:40 -04001693 goto out;
1694 }
1695
1696 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
1697 c->journal.flushed_seq_ondisk,
1698 pos.inode, pos.offset)) {
Kent Overstreeta6548c82024-01-15 17:56:22 -05001699 s->need_journal_commit++;
1700 s->need_journal_commit_this_dev++;
Kent Overstreet4910a952022-07-17 00:31:40 -04001701 goto out;
1702 }
1703
Kent Overstreetbcb79a52023-04-29 19:33:09 -04001704 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
1705 need_discard_iter->pos,
1706 BTREE_ITER_CACHED);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001707 ret = bkey_err(k);
1708 if (ret)
1709 goto out;
1710
1711 a = bch2_alloc_to_v4_mut(trans, k);
1712 ret = PTR_ERR_OR_ZERO(a);
1713 if (ret)
1714 goto out;
1715
Kent Overstreet7ee88732024-04-02 18:30:14 -04001716 if (a->v.dirty_sectors) {
1717 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1718 trans, "attempting to discard bucket with dirty data\n%s",
1719 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
Kent Overstreetf2b542b2022-12-11 19:14:30 -05001720 ret = -EIO;
Kent Overstreet822835f2022-04-01 01:29:59 -04001721 goto out;
1722 }
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001723
Kent Overstreetf2b542b2022-12-11 19:14:30 -05001724 if (a->v.data_type != BCH_DATA_need_discard) {
Kent Overstreet7ee88732024-04-02 18:30:14 -04001725 if (data_type_is_empty(a->v.data_type) &&
1726 BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
1727 a->v.gen++;
1728 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1729 goto write;
Kent Overstreetf2b542b2022-12-11 19:14:30 -05001730 }
1731
Kent Overstreet7ee88732024-04-02 18:30:14 -04001732 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1733 trans, "bucket incorrectly set in need_discard btree\n"
1734 "%s",
1735 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1736 ret = -EIO;
1737 goto out;
1738 }
1739
1740 if (a->v.journal_seq > c->journal.flushed_seq_ondisk) {
1741 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1742 trans, "clearing need_discard but journal_seq %llu > flushed_seq %llu\n%s",
1743 a->v.journal_seq,
1744 c->journal.flushed_seq_ondisk,
1745 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1746 ret = -EIO;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001747 goto out;
1748 }
1749
Kent Overstreeta393f332024-02-16 01:08:25 -05001750 if (discard_in_flight_add(c, SPOS(iter.pos.inode, iter.pos.offset, true)))
1751 goto out;
1752
1753 discard_locked = true;
1754
Kent Overstreete88a75e2022-11-24 03:12:22 -05001755 if (!bkey_eq(*discard_pos_done, iter.pos) &&
Kent Overstreet4910a952022-07-17 00:31:40 -04001756 ca->mi.discard && !c->opts.nochanges) {
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001757 /*
1758 * This works without any other locks because this is the only
1759 * thread that removes items from the need_discard tree
1760 */
Kent Overstreet096386a2024-01-22 18:08:51 -05001761 bch2_trans_unlock_long(trans);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001762 blkdev_issue_discard(ca->disk_sb.bdev,
1763 k.k->p.offset * ca->mi.bucket_size,
1764 ca->mi.bucket_size,
1765 GFP_KERNEL);
Kent Overstreet4910a952022-07-17 00:31:40 -04001766 *discard_pos_done = iter.pos;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001767
Kent Overstreet31381632023-01-24 00:26:48 -05001768 ret = bch2_trans_relock_notrace(trans);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001769 if (ret)
1770 goto out;
1771 }
1772
1773 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
Kent Overstreet822835f2022-04-01 01:29:59 -04001774 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001775write:
Kent Overstreet4910a952022-07-17 00:31:40 -04001776 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1777 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreetf33c58f2023-06-27 17:32:48 -04001778 BCH_WATERMARK_btree|
Kent Overstreetcb52d23e2023-11-11 16:31:50 -05001779 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreet4910a952022-07-17 00:31:40 -04001780 if (ret)
1781 goto out;
1782
Kent Overstreet74644032023-11-27 22:37:27 -05001783 count_event(c, bucket_discard);
Kent Overstreeta6548c82024-01-15 17:56:22 -05001784 s->discarded++;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001785out:
Kent Overstreeta393f332024-02-16 01:08:25 -05001786 if (discard_locked)
1787 discard_in_flight_remove(c, iter.pos);
Kent Overstreeta6548c82024-01-15 17:56:22 -05001788 s->seen++;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001789 bch2_trans_iter_exit(trans, &iter);
Kent Overstreet4910a952022-07-17 00:31:40 -04001790 percpu_ref_put(&ca->io_ref);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001791 printbuf_exit(&buf);
1792 return ret;
1793}
1794
1795static void bch2_do_discards_work(struct work_struct *work)
1796{
1797 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
Kent Overstreeta6548c82024-01-15 17:56:22 -05001798 struct discard_buckets_state s = {};
Kent Overstreet4910a952022-07-17 00:31:40 -04001799 struct bpos discard_pos_done = POS_MAX;
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001800 int ret;
1801
Kent Overstreet4910a952022-07-17 00:31:40 -04001802 /*
1803 * We're doing the commit in bch2_discard_one_bucket instead of using
1804 * for_each_btree_key_commit() so that we can increment counters after
1805 * successful commit:
1806 */
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001807 ret = bch2_trans_run(c,
Kent Overstreet5028b902023-12-07 23:33:11 -05001808 for_each_btree_key(trans, iter,
1809 BTREE_ID_need_discard, POS_MIN, 0, k,
Kent Overstreeta6548c82024-01-15 17:56:22 -05001810 bch2_discard_one_bucket(trans, &iter, &discard_pos_done, &s)));
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001811
Kent Overstreeta6548c82024-01-15 17:56:22 -05001812 discard_buckets_next_dev(c, &s, NULL);
1813
1814 trace_discard_buckets(c, s.seen, s.open, s.need_journal_commit, s.discarded,
1815 bch2_err_str(ret));
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001816
Kent Overstreetd94189a2023-02-09 12:21:45 -05001817 bch2_write_ref_put(c, BCH_WRITE_REF_discard);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001818}
1819
1820void bch2_do_discards(struct bch_fs *c)
1821{
Kent Overstreetd94189a2023-02-09 12:21:45 -05001822 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_discard) &&
Brian Foster8bff9872023-03-23 14:09:05 -04001823 !queue_work(c->write_ref_wq, &c->discard_work))
Kent Overstreetd94189a2023-02-09 12:21:45 -05001824 bch2_write_ref_put(c, BCH_WRITE_REF_discard);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05001825}
1826
Kent Overstreeta393f332024-02-16 01:08:25 -05001827static int bch2_clear_bucket_needs_discard(struct btree_trans *trans, struct bpos bucket)
1828{
1829 struct btree_iter iter;
1830 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, bucket, BTREE_ITER_INTENT);
1831 struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter);
1832 int ret = bkey_err(k);
1833 if (ret)
1834 goto err;
1835
1836 struct bkey_i_alloc_v4 *a = bch2_alloc_to_v4_mut(trans, k);
1837 ret = PTR_ERR_OR_ZERO(a);
1838 if (ret)
1839 goto err;
1840
Kent Overstreet7ee88732024-04-02 18:30:14 -04001841 BUG_ON(a->v.dirty_sectors);
Kent Overstreeta393f332024-02-16 01:08:25 -05001842 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
1843 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
1844
1845 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1846err:
1847 bch2_trans_iter_exit(trans, &iter);
1848 return ret;
1849}
1850
1851static void bch2_do_discards_fast_work(struct work_struct *work)
1852{
1853 struct bch_fs *c = container_of(work, struct bch_fs, discard_fast_work);
1854
1855 while (1) {
1856 bool got_bucket = false;
1857 struct bpos bucket;
1858 struct bch_dev *ca;
1859
1860 mutex_lock(&c->discard_buckets_in_flight_lock);
1861 darray_for_each(c->discard_buckets_in_flight, i) {
1862 if (i->snapshot)
1863 continue;
1864
1865 ca = bch_dev_bkey_exists(c, i->inode);
1866
1867 if (!percpu_ref_tryget(&ca->io_ref)) {
1868 darray_remove_item(&c->discard_buckets_in_flight, i);
1869 continue;
1870 }
1871
1872 got_bucket = true;
1873 bucket = *i;
1874 i->snapshot = true;
1875 break;
1876 }
1877 mutex_unlock(&c->discard_buckets_in_flight_lock);
1878
1879 if (!got_bucket)
1880 break;
1881
1882 if (ca->mi.discard && !c->opts.nochanges)
1883 blkdev_issue_discard(ca->disk_sb.bdev,
1884 bucket.offset * ca->mi.bucket_size,
1885 ca->mi.bucket_size,
1886 GFP_KERNEL);
1887
1888 int ret = bch2_trans_do(c, NULL, NULL,
1889 BCH_WATERMARK_btree|
1890 BCH_TRANS_COMMIT_no_enospc,
1891 bch2_clear_bucket_needs_discard(trans, bucket));
1892 bch_err_fn(c, ret);
1893
1894 percpu_ref_put(&ca->io_ref);
1895 discard_in_flight_remove(c, bucket);
1896
1897 if (ret)
1898 break;
1899 }
1900
1901 bch2_write_ref_put(c, BCH_WRITE_REF_discard_fast);
1902}
1903
1904static void bch2_discard_one_bucket_fast(struct bch_fs *c, struct bpos bucket)
1905{
1906 struct bch_dev *ca = bch_dev_bkey_exists(c, bucket.inode);
1907
1908 if (!percpu_ref_is_dying(&ca->io_ref) &&
1909 !discard_in_flight_add(c, bucket) &&
1910 bch2_write_ref_tryget(c, BCH_WRITE_REF_discard_fast) &&
1911 !queue_work(c->write_ref_wq, &c->discard_fast_work))
1912 bch2_write_ref_put(c, BCH_WRITE_REF_discard_fast);
1913}
1914
Kent Overstreetd04801a2022-07-17 00:31:40 -04001915static int invalidate_one_bucket(struct btree_trans *trans,
Kent Overstreet83f33d62022-12-05 16:49:13 -05001916 struct btree_iter *lru_iter,
Kent Overstreet629a21b2023-01-03 23:54:10 -05001917 struct bkey_s_c lru_k,
Kent Overstreet83f33d62022-12-05 16:49:13 -05001918 s64 *nr_to_invalidate)
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001919{
1920 struct bch_fs *c = trans->c;
Kent Overstreetd04801a2022-07-17 00:31:40 -04001921 struct btree_iter alloc_iter = { NULL };
Kent Overstreet629a21b2023-01-03 23:54:10 -05001922 struct bkey_i_alloc_v4 *a = NULL;
Kent Overstreet9b935962022-04-11 17:23:39 -04001923 struct printbuf buf = PRINTBUF;
Kent Overstreet629a21b2023-01-03 23:54:10 -05001924 struct bpos bucket = u64_to_bucket(lru_k.k->p.offset);
Kent Overstreetd04801a2022-07-17 00:31:40 -04001925 unsigned cached_sectors;
1926 int ret = 0;
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001927
Kent Overstreet83f33d62022-12-05 16:49:13 -05001928 if (*nr_to_invalidate <= 0)
Kent Overstreetd04801a2022-07-17 00:31:40 -04001929 return 1;
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001930
Kent Overstreet629a21b2023-01-03 23:54:10 -05001931 if (!bch2_dev_bucket_exists(c, bucket)) {
1932 prt_str(&buf, "lru entry points to invalid bucket");
1933 goto err;
1934 }
1935
Kent Overstreet84ddb8b2023-02-17 20:33:12 -05001936 if (bch2_bucket_is_open_safe(c, bucket.inode, bucket.offset))
1937 return 0;
1938
Kent Overstreetd04801a2022-07-17 00:31:40 -04001939 a = bch2_trans_start_alloc_update(trans, &alloc_iter, bucket);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001940 ret = PTR_ERR_OR_ZERO(a);
1941 if (ret)
1942 goto out;
1943
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -05001944 /* We expect harmless races here due to the btree write buffer: */
Kent Overstreet80c33082022-12-05 10:24:19 -05001945 if (lru_pos_time(lru_iter->pos) != alloc_lru_idx_read(a->v))
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -05001946 goto out;
Kent Overstreet1cab5a82022-04-21 13:13:57 -04001947
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -05001948 BUG_ON(a->v.data_type != BCH_DATA_cached);
Kent Overstreet7ee88732024-04-02 18:30:14 -04001949 BUG_ON(a->v.dirty_sectors);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001950
Kent Overstreet38585362022-06-20 22:26:41 -04001951 if (!a->v.cached_sectors)
1952 bch_err(c, "invalidating empty bucket, confused");
1953
Kent Overstreetd04801a2022-07-17 00:31:40 -04001954 cached_sectors = a->v.cached_sectors;
Kent Overstreet38585362022-06-20 22:26:41 -04001955
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001956 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1957 a->v.gen++;
1958 a->v.data_type = 0;
1959 a->v.dirty_sectors = 0;
1960 a->v.cached_sectors = 0;
1961 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1962 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1963
Kent Overstreetd04801a2022-07-17 00:31:40 -04001964 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1965 BTREE_TRIGGER_BUCKET_INVALIDATE) ?:
1966 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreetf33c58f2023-06-27 17:32:48 -04001967 BCH_WATERMARK_btree|
Kent Overstreetcb52d23e2023-11-11 16:31:50 -05001968 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreet1f937262022-04-17 18:06:31 -04001969 if (ret)
1970 goto out;
Kent Overstreetd04801a2022-07-17 00:31:40 -04001971
Kent Overstreet674cfc22022-08-27 12:48:36 -04001972 trace_and_count(c, bucket_invalidate, c, bucket.inode, bucket.offset, cached_sectors);
Kent Overstreetd04801a2022-07-17 00:31:40 -04001973 --*nr_to_invalidate;
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001974out:
1975 bch2_trans_iter_exit(trans, &alloc_iter);
Kent Overstreet9b935962022-04-11 17:23:39 -04001976 printbuf_exit(&buf);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001977 return ret;
Kent Overstreet629a21b2023-01-03 23:54:10 -05001978err:
1979 prt_str(&buf, "\n lru key: ");
1980 bch2_bkey_val_to_text(&buf, c, lru_k);
1981
1982 prt_str(&buf, "\n lru entry: ");
1983 bch2_lru_pos_to_text(&buf, lru_iter->pos);
1984
1985 prt_str(&buf, "\n alloc key: ");
1986 if (!a)
1987 bch2_bpos_to_text(&buf, bucket);
1988 else
1989 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&a->k_i));
1990
1991 bch_err(c, "%s", buf.buf);
Kent Overstreet067d2282023-07-07 02:42:28 -04001992 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_check_lrus) {
Kent Overstreet629a21b2023-01-03 23:54:10 -05001993 bch2_inconsistent_error(c);
1994 ret = -EINVAL;
1995 }
1996
1997 goto out;
Kent Overstreetcaece7f2022-02-10 18:18:41 -05001998}
1999
2000static void bch2_do_invalidates_work(struct work_struct *work)
2001{
2002 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002003 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05002004 int ret = 0;
2005
Kent Overstreetcb13f472023-11-02 20:36:00 -04002006 ret = bch2_btree_write_buffer_tryflush(trans);
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -05002007 if (ret)
2008 goto err;
2009
Kent Overstreet9fea2272023-12-16 23:47:29 -05002010 for_each_member_device(c, ca) {
Kent Overstreet822835f2022-04-01 01:29:59 -04002011 s64 nr_to_invalidate =
2012 should_invalidate_buckets(ca, bch2_dev_usage_read(ca));
2013
Kent Overstreet3a860b52023-12-16 21:46:23 -05002014 ret = for_each_btree_key_upto(trans, iter, BTREE_ID_lru,
Kent Overstreet83f33d62022-12-05 16:49:13 -05002015 lru_pos(ca->dev_idx, 0, 0),
2016 lru_pos(ca->dev_idx, U64_MAX, LRU_TIME_MAX),
2017 BTREE_ITER_INTENT, k,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002018 invalidate_one_bucket(trans, &iter, k, &nr_to_invalidate));
Kent Overstreet440c15c2022-06-13 19:45:07 -04002019
Kent Overstreetd04801a2022-07-17 00:31:40 -04002020 if (ret < 0) {
2021 percpu_ref_put(&ca->ref);
2022 break;
Kent Overstreet440c15c2022-06-13 19:45:07 -04002023 }
Kent Overstreet822835f2022-04-01 01:29:59 -04002024 }
Kent Overstreet1b30ed5f2023-02-06 18:51:42 -05002025err:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002026 bch2_trans_put(trans);
Kent Overstreetd94189a2023-02-09 12:21:45 -05002027 bch2_write_ref_put(c, BCH_WRITE_REF_invalidate);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05002028}
2029
2030void bch2_do_invalidates(struct bch_fs *c)
2031{
Kent Overstreetd94189a2023-02-09 12:21:45 -05002032 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_invalidate) &&
Brian Foster8bff9872023-03-23 14:09:05 -04002033 !queue_work(c->write_ref_wq, &c->invalidate_work))
Kent Overstreetd94189a2023-02-09 12:21:45 -05002034 bch2_write_ref_put(c, BCH_WRITE_REF_invalidate);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05002035}
2036
Kent Overstreet69d1f052023-09-28 17:57:21 -04002037int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca,
2038 u64 bucket_start, u64 bucket_end)
Kent Overstreetc6b28262021-12-11 17:13:09 -05002039{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002040 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002041 struct btree_iter iter;
2042 struct bkey_s_c k;
Kent Overstreetd23124c2022-11-30 13:25:17 -05002043 struct bkey hole;
Kent Overstreet69d1f052023-09-28 17:57:21 -04002044 struct bpos end = POS(ca->dev_idx, bucket_end);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002045 struct bch_member *m;
Kent Overstreet69d1f052023-09-28 17:57:21 -04002046 unsigned long last_updated = jiffies;
Kent Overstreetc6b28262021-12-11 17:13:09 -05002047 int ret;
2048
Kent Overstreet69d1f052023-09-28 17:57:21 -04002049 BUG_ON(bucket_start > bucket_end);
2050 BUG_ON(bucket_end > ca->mi.nbuckets);
2051
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002052 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
Kent Overstreet69d1f052023-09-28 17:57:21 -04002053 POS(ca->dev_idx, max_t(u64, ca->mi.first_bucket, bucket_start)),
2054 BTREE_ITER_PREFETCH);
Kent Overstreetcc65f562022-11-26 04:37:11 -05002055 /*
2056 * Scan the alloc btree for every bucket on @ca, and add buckets to the
2057 * freespace/need_discard/need_gc_gens btrees as needed:
2058 */
2059 while (1) {
Kent Overstreet69d1f052023-09-28 17:57:21 -04002060 if (last_updated + HZ * 10 < jiffies) {
Kent Overstreete9b9e472023-03-22 21:22:51 -04002061 bch_info(ca, "%s: currently at %llu/%llu",
2062 __func__, iter.pos.offset, ca->mi.nbuckets);
Kent Overstreet69d1f052023-09-28 17:57:21 -04002063 last_updated = jiffies;
Kent Overstreete9b9e472023-03-22 21:22:51 -04002064 }
2065
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002066 bch2_trans_begin(trans);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002067
Kent Overstreetd23124c2022-11-30 13:25:17 -05002068 if (bkey_ge(iter.pos, end)) {
2069 ret = 0;
Kent Overstreetcc65f562022-11-26 04:37:11 -05002070 break;
Kent Overstreetd23124c2022-11-30 13:25:17 -05002071 }
Kent Overstreetcc65f562022-11-26 04:37:11 -05002072
Kent Overstreetd23124c2022-11-30 13:25:17 -05002073 k = bch2_get_key_or_hole(&iter, end, &hole);
Kent Overstreetcc65f562022-11-26 04:37:11 -05002074 ret = bkey_err(k);
2075 if (ret)
2076 goto bkey_err;
2077
2078 if (k.k->type) {
2079 /*
2080 * We process live keys in the alloc btree one at a
2081 * time:
2082 */
2083 struct bch_alloc_v4 a_convert;
2084 const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &a_convert);
2085
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002086 ret = bch2_bucket_do_index(trans, k, a, true) ?:
2087 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreetcb52d23e2023-11-11 16:31:50 -05002088 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreetcc65f562022-11-26 04:37:11 -05002089 if (ret)
2090 goto bkey_err;
2091
2092 bch2_btree_iter_advance(&iter);
2093 } else {
Kent Overstreetcc65f562022-11-26 04:37:11 -05002094 struct bkey_i *freespace;
Kent Overstreetcc65f562022-11-26 04:37:11 -05002095
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002096 freespace = bch2_trans_kmalloc(trans, sizeof(*freespace));
Kent Overstreetcc65f562022-11-26 04:37:11 -05002097 ret = PTR_ERR_OR_ZERO(freespace);
2098 if (ret)
2099 goto bkey_err;
2100
2101 bkey_init(&freespace->k);
Kent Overstreetd23124c2022-11-30 13:25:17 -05002102 freespace->k.type = KEY_TYPE_set;
2103 freespace->k.p = k.k->p;
2104 freespace->k.size = k.k->size;
Kent Overstreetcc65f562022-11-26 04:37:11 -05002105
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002106 ret = bch2_btree_insert_trans(trans, BTREE_ID_freespace, freespace, 0) ?:
2107 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreetcb52d23e2023-11-11 16:31:50 -05002108 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreetcc65f562022-11-26 04:37:11 -05002109 if (ret)
2110 goto bkey_err;
2111
Kent Overstreetd23124c2022-11-30 13:25:17 -05002112 bch2_btree_iter_set_pos(&iter, k.k->p);
Kent Overstreetcc65f562022-11-26 04:37:11 -05002113 }
2114bkey_err:
2115 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
2116 continue;
2117 if (ret)
2118 break;
2119 }
2120
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04002121 bch2_trans_iter_exit(trans, &iter);
2122 bch2_trans_put(trans);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002123
Kent Overstreetca91f402022-07-17 00:44:19 -04002124 if (ret < 0) {
Kent Overstreete46c1812023-09-11 01:37:34 -04002125 bch_err_msg(ca, ret, "initializing free space");
Kent Overstreetc6b28262021-12-11 17:13:09 -05002126 return ret;
2127 }
2128
2129 mutex_lock(&c->sb_lock);
Hunter Shaffer3f7b9712023-09-25 00:02:56 -04002130 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002131 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
2132 mutex_unlock(&c->sb_lock);
2133
Kent Overstreetca91f402022-07-17 00:44:19 -04002134 return 0;
Kent Overstreetc6b28262021-12-11 17:13:09 -05002135}
2136
2137int bch2_fs_freespace_init(struct bch_fs *c)
2138{
Kent Overstreetc6b28262021-12-11 17:13:09 -05002139 int ret = 0;
2140 bool doing_init = false;
2141
2142 /*
2143 * We can crash during the device add path, so we need to check this on
2144 * every mount:
2145 */
2146
Kent Overstreet9fea2272023-12-16 23:47:29 -05002147 for_each_member_device(c, ca) {
Kent Overstreetc6b28262021-12-11 17:13:09 -05002148 if (ca->mi.freespace_initialized)
2149 continue;
2150
2151 if (!doing_init) {
2152 bch_info(c, "initializing freespace");
2153 doing_init = true;
2154 }
2155
Kent Overstreet69d1f052023-09-28 17:57:21 -04002156 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002157 if (ret) {
2158 percpu_ref_put(&ca->ref);
Kent Overstreet1bb3c2a2023-06-20 13:49:25 -04002159 bch_err_fn(c, ret);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002160 return ret;
2161 }
2162 }
2163
2164 if (doing_init) {
2165 mutex_lock(&c->sb_lock);
2166 bch2_write_super(c);
2167 mutex_unlock(&c->sb_lock);
Kent Overstreetc6b28262021-12-11 17:13:09 -05002168 bch_verbose(c, "done initializing freespace");
2169 }
2170
Kent Overstreet1bb3c2a2023-06-20 13:49:25 -04002171 return 0;
Kent Overstreetc6b28262021-12-11 17:13:09 -05002172}
2173
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002174/* Bucket IO clocks: */
2175
Kent Overstreetf30dd862020-10-16 21:39:16 -04002176int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
2177 size_t bucket_nr, int rw)
2178{
2179 struct bch_fs *c = trans->c;
Kent Overstreet67e0dd82021-08-30 15:18:31 -04002180 struct btree_iter iter;
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002181 struct bkey_i_alloc_v4 *a;
2182 u64 now;
Kent Overstreetf30dd862020-10-16 21:39:16 -04002183 int ret = 0;
2184
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002185 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
2186 ret = PTR_ERR_OR_ZERO(a);
Kent Overstreetf30dd862020-10-16 21:39:16 -04002187 if (ret)
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002188 return ret;
Kent Overstreetf30dd862020-10-16 21:39:16 -04002189
Kent Overstreet2abe5422021-01-21 15:28:59 -05002190 now = atomic64_read(&c->io_clock[rw].now);
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002191 if (a->v.io_time[rw] == now)
Kent Overstreetf30dd862020-10-16 21:39:16 -04002192 goto out;
2193
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002194 a->v.io_time[rw] = now;
Kent Overstreetf30dd862020-10-16 21:39:16 -04002195
Kent Overstreet3d48a7f2021-12-31 20:03:29 -05002196 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
Kent Overstreetf30dd862020-10-16 21:39:16 -04002197 bch2_trans_commit(trans, NULL, NULL, 0);
2198out:
Kent Overstreet67e0dd82021-08-30 15:18:31 -04002199 bch2_trans_iter_exit(trans, &iter);
Kent Overstreetf30dd862020-10-16 21:39:16 -04002200 return ret;
2201}
2202
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002203/* Startup/shutdown (ro/rw): */
2204
2205void bch2_recalc_capacity(struct bch_fs *c)
2206{
Kent Overstreetcb66fc52021-04-13 09:49:23 -04002207 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
Kent Overstreetb092dad2018-11-04 21:55:35 -05002208 unsigned bucket_size_max = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002209 unsigned long ra_pages = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002210
2211 lockdep_assert_held(&c->state_lock);
2212
Kent Overstreet9fea2272023-12-16 23:47:29 -05002213 for_each_online_member(c, ca) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002214 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
2215
2216 ra_pages += bdi->ra_pages;
2217 }
2218
2219 bch2_set_ra_pages(c, ra_pages);
2220
Kent Overstreet9fea2272023-12-16 23:47:29 -05002221 for_each_rw_member(c, ca) {
Kent Overstreeta50ed7c2018-07-24 14:55:05 -04002222 u64 dev_reserve = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002223
2224 /*
2225 * We need to reserve buckets (from the number
2226 * of currently available buckets) against
2227 * foreground writes so that mainly copygc can
2228 * make forward progress.
2229 *
2230 * We need enough to refill the various reserves
2231 * from scratch - copygc will use its entire
2232 * reserve all at once, then run against when
2233 * its reserve is refilled (from the formerly
2234 * available buckets).
2235 *
2236 * This reserve is just used when considering if
2237 * allocations for foreground writes must wait -
2238 * not -ENOSPC calculations.
2239 */
Kent Overstreetf25d8212022-01-09 20:48:31 -05002240
2241 dev_reserve += ca->nr_btree_reserve * 2;
2242 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002243
Kent Overstreeta9bec522018-08-01 14:26:55 -04002244 dev_reserve += 1; /* btree write point */
2245 dev_reserve += 1; /* copygc write point */
2246 dev_reserve += 1; /* rebalance write point */
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002247
Kent Overstreeta9bec522018-08-01 14:26:55 -04002248 dev_reserve *= ca->mi.bucket_size;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002249
Kent Overstreeta50ed7c2018-07-24 14:55:05 -04002250 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
2251 ca->mi.first_bucket);
Kent Overstreeta9bec522018-08-01 14:26:55 -04002252
Kent Overstreeta50ed7c2018-07-24 14:55:05 -04002253 reserved_sectors += dev_reserve * 2;
Kent Overstreetb092dad2018-11-04 21:55:35 -05002254
2255 bucket_size_max = max_t(unsigned, bucket_size_max,
2256 ca->mi.bucket_size);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002257 }
2258
Kent Overstreeta50ed7c2018-07-24 14:55:05 -04002259 gc_reserve = c->opts.gc_reserve_bytes
2260 ? c->opts.gc_reserve_bytes >> 9
2261 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002262
Kent Overstreeta50ed7c2018-07-24 14:55:05 -04002263 reserved_sectors = max(gc_reserve, reserved_sectors);
2264
2265 reserved_sectors = min(reserved_sectors, capacity);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002266
Kent Overstreeta9bec522018-08-01 14:26:55 -04002267 c->capacity = capacity - reserved_sectors;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002268
Kent Overstreetb092dad2018-11-04 21:55:35 -05002269 c->bucket_size_max = bucket_size_max;
2270
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002271 /* Wake up case someone was waiting for buckets */
2272 closure_wake_up(&c->freelist_wait);
2273}
2274
Kent Overstreet1f7056b2023-10-30 13:15:36 -04002275u64 bch2_min_rw_member_capacity(struct bch_fs *c)
2276{
Kent Overstreet1f7056b2023-10-30 13:15:36 -04002277 u64 ret = U64_MAX;
2278
Kent Overstreet9fea2272023-12-16 23:47:29 -05002279 for_each_rw_member(c, ca)
Kent Overstreet1f7056b2023-10-30 13:15:36 -04002280 ret = min(ret, ca->mi.nbuckets * ca->mi.bucket_size);
2281 return ret;
2282}
2283
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002284static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
2285{
2286 struct open_bucket *ob;
2287 bool ret = false;
2288
2289 for (ob = c->open_buckets;
2290 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
2291 ob++) {
2292 spin_lock(&ob->lock);
2293 if (ob->valid && !ob->on_partial_list &&
Kent Overstreetabe19d42021-12-25 21:21:46 -05002294 ob->dev == ca->dev_idx)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002295 ret = true;
2296 spin_unlock(&ob->lock);
2297 }
2298
2299 return ret;
2300}
2301
2302/* device goes ro: */
2303void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
2304{
2305 unsigned i;
2306
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002307 /* First, remove device from allocation groups: */
2308
2309 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
2310 clear_bit(ca->dev_idx, c->rw_devs[i].d);
2311
2312 /*
2313 * Capacity is calculated based off of devices in allocation groups:
2314 */
2315 bch2_recalc_capacity(c);
2316
Kent Overstreetb40901b2023-03-13 22:01:47 -04002317 bch2_open_buckets_stop(c, ca, false);
Kent Overstreetcd575dd2018-11-01 15:13:19 -04002318
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002319 /*
2320 * Wake up threads that were blocked on allocation, so they can notice
2321 * the device can no longer be removed and the capacity has changed:
2322 */
2323 closure_wake_up(&c->freelist_wait);
2324
2325 /*
2326 * journal_res_get() can block waiting for free space in the journal -
2327 * it needs to notice there may not be devices to allocate from anymore:
2328 */
2329 wake_up(&c->journal.wait);
2330
2331 /* Now wait for any in flight writes: */
2332
2333 closure_wait_event(&c->open_buckets_wait,
2334 !bch2_dev_has_open_write_point(c, ca));
2335}
2336
2337/* device goes rw: */
2338void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
2339{
2340 unsigned i;
2341
2342 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
2343 if (ca->mi.data_allowed & (1 << i))
2344 set_bit(ca->dev_idx, c->rw_devs[i].d);
2345}
2346
Kent Overstreeta393f332024-02-16 01:08:25 -05002347void bch2_fs_allocator_background_exit(struct bch_fs *c)
2348{
2349 darray_exit(&c->discard_buckets_in_flight);
2350}
2351
Kent Overstreetb092dad2018-11-04 21:55:35 -05002352void bch2_fs_allocator_background_init(struct bch_fs *c)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002353{
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002354 spin_lock_init(&c->freelist_lock);
Kent Overstreeta393f332024-02-16 01:08:25 -05002355 mutex_init(&c->discard_buckets_in_flight_lock);
Kent Overstreet59cc38b2022-02-10 04:32:19 -05002356 INIT_WORK(&c->discard_work, bch2_do_discards_work);
Kent Overstreeta393f332024-02-16 01:08:25 -05002357 INIT_WORK(&c->discard_fast_work, bch2_do_discards_fast_work);
Kent Overstreetcaece7f2022-02-10 18:18:41 -05002358 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002359}