1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "btree_cache.h" 5 #include "btree_io.h" 6 #include "btree_journal_iter.h" 7 #include "btree_node_scan.h" 8 #include "btree_update_interior.h" 9 #include "buckets.h" 10 #include "error.h" 11 #include "journal_io.h" 12 #include "recovery_passes.h" 13 14 #include <linux/kthread.h> 15 #include <linux/sort.h> 16 17 struct find_btree_nodes_worker { 18 struct closure *cl; 19 struct find_btree_nodes *f; 20 struct bch_dev *ca; 21 }; 22 23 static void found_btree_node_to_text(struct printbuf *out, struct bch_fs *c, const struct found_btree_node *n) 24 { 25 prt_printf(out, "%s l=%u seq=%u cookie=%llx ", bch2_btree_id_str(n->btree_id), n->level, n->seq, n->cookie); 26 bch2_bpos_to_text(out, n->min_key); 27 prt_str(out, "-"); 28 bch2_bpos_to_text(out, n->max_key); 29 30 if (n->range_updated) 31 prt_str(out, " range updated"); 32 if (n->overwritten) 33 prt_str(out, " overwritten"); 34 35 for (unsigned i = 0; i < n->nr_ptrs; i++) { 36 prt_char(out, ' '); 37 bch2_extent_ptr_to_text(out, c, n->ptrs + i); 38 } 39 } 40 41 static void found_btree_nodes_to_text(struct printbuf *out, struct bch_fs *c, found_btree_nodes nodes) 42 { 43 printbuf_indent_add(out, 2); 44 darray_for_each(nodes, i) { 45 found_btree_node_to_text(out, c, i); 46 prt_newline(out); 47 } 48 printbuf_indent_sub(out, 2); 49 } 50 51 static void found_btree_node_to_key(struct bkey_i *k, const struct found_btree_node *f) 52 { 53 struct bkey_i_btree_ptr_v2 *bp = bkey_btree_ptr_v2_init(k); 54 55 set_bkey_val_u64s(&bp->k, sizeof(struct bch_btree_ptr_v2) / sizeof(u64) + f->nr_ptrs); 56 bp->k.p = f->max_key; 57 bp->v.seq = cpu_to_le64(f->cookie); 58 bp->v.sectors_written = 0; 59 bp->v.flags = 0; 60 bp->v.min_key = f->min_key; 61 SET_BTREE_PTR_RANGE_UPDATED(&bp->v, f->range_updated); 62 memcpy(bp->v.start, f->ptrs, sizeof(struct bch_extent_ptr) * f->nr_ptrs); 63 } 64 65 static bool found_btree_node_is_readable(struct btree_trans *trans, 66 const struct found_btree_node *f) 67 { 68 struct { __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX); } k; 69 70 found_btree_node_to_key(&k.k, f); 71 72 struct btree *b = bch2_btree_node_get_noiter(trans, &k.k, f->btree_id, f->level, false); 73 bool ret = !IS_ERR_OR_NULL(b); 74 if (ret) 75 six_unlock_read(&b->c.lock); 76 77 /* 78 * We might update this node's range; if that happens, we need the node 79 * to be re-read so the read path can trim keys that are no longer in 80 * this node 81 */ 82 if (b != btree_node_root(trans->c, b)) 83 bch2_btree_node_evict(trans, &k.k); 84 return ret; 85 } 86 87 static int found_btree_node_cmp_cookie(const void *_l, const void *_r) 88 { 89 const struct found_btree_node *l = _l; 90 const struct found_btree_node *r = _r; 91 92 return cmp_int(l->btree_id, r->btree_id) ?: 93 cmp_int(l->level, r->level) ?: 94 cmp_int(l->cookie, r->cookie); 95 } 96 97 /* 98 * Given two found btree nodes, if their sequence numbers are equal, take the 99 * one that's readable: 100 */ 101 static int found_btree_node_cmp_time(const struct found_btree_node *l, 102 const struct found_btree_node *r) 103 { 104 return cmp_int(l->seq, r->seq); 105 } 106 107 static int found_btree_node_cmp_pos(const void *_l, const void *_r) 108 { 109 const struct found_btree_node *l = _l; 110 const struct found_btree_node *r = _r; 111 112 return cmp_int(l->btree_id, r->btree_id) ?: 113 -cmp_int(l->level, r->level) ?: 114 bpos_cmp(l->min_key, r->min_key) ?: 115 -found_btree_node_cmp_time(l, r); 116 } 117 118 static void try_read_btree_node(struct find_btree_nodes *f, struct bch_dev *ca, 119 struct bio *bio, struct btree_node *bn, u64 offset) 120 { 121 struct bch_fs *c = container_of(f, struct bch_fs, found_btree_nodes); 122 123 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ); 124 bio->bi_iter.bi_sector = offset; 125 bch2_bio_map(bio, bn, PAGE_SIZE); 126 127 submit_bio_wait(bio); 128 if (bch2_dev_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_read, 129 "IO error in try_read_btree_node() at %llu: %s", 130 offset, bch2_blk_status_to_str(bio->bi_status))) 131 return; 132 133 if (le64_to_cpu(bn->magic) != bset_magic(c)) 134 return; 135 136 rcu_read_lock(); 137 struct found_btree_node n = { 138 .btree_id = BTREE_NODE_ID(bn), 139 .level = BTREE_NODE_LEVEL(bn), 140 .seq = BTREE_NODE_SEQ(bn), 141 .cookie = le64_to_cpu(bn->keys.seq), 142 .min_key = bn->min_key, 143 .max_key = bn->max_key, 144 .nr_ptrs = 1, 145 .ptrs[0].type = 1 << BCH_EXTENT_ENTRY_ptr, 146 .ptrs[0].offset = offset, 147 .ptrs[0].dev = ca->dev_idx, 148 .ptrs[0].gen = *bucket_gen(ca, sector_to_bucket(ca, offset)), 149 }; 150 rcu_read_unlock(); 151 152 if (bch2_trans_run(c, found_btree_node_is_readable(trans, &n))) { 153 mutex_lock(&f->lock); 154 if (BSET_BIG_ENDIAN(&bn->keys) != CPU_BIG_ENDIAN) { 155 bch_err(c, "try_read_btree_node() can't handle endian conversion"); 156 f->ret = -EINVAL; 157 goto unlock; 158 } 159 160 if (darray_push(&f->nodes, n)) 161 f->ret = -ENOMEM; 162 unlock: 163 mutex_unlock(&f->lock); 164 } 165 } 166 167 static int read_btree_nodes_worker(void *p) 168 { 169 struct find_btree_nodes_worker *w = p; 170 struct bch_fs *c = container_of(w->f, struct bch_fs, found_btree_nodes); 171 struct bch_dev *ca = w->ca; 172 void *buf = (void *) __get_free_page(GFP_KERNEL); 173 struct bio *bio = bio_alloc(NULL, 1, 0, GFP_KERNEL); 174 unsigned long last_print = jiffies; 175 176 if (!buf || !bio) { 177 bch_err(c, "read_btree_nodes_worker: error allocating bio/buf"); 178 w->f->ret = -ENOMEM; 179 goto err; 180 } 181 182 for (u64 bucket = ca->mi.first_bucket; bucket < ca->mi.nbuckets; bucket++) 183 for (unsigned bucket_offset = 0; 184 bucket_offset + btree_sectors(c) <= ca->mi.bucket_size; 185 bucket_offset += btree_sectors(c)) { 186 if (time_after(jiffies, last_print + HZ * 30)) { 187 u64 cur_sector = bucket * ca->mi.bucket_size + bucket_offset; 188 u64 end_sector = ca->mi.nbuckets * ca->mi.bucket_size; 189 190 bch_info(ca, "%s: %2u%% done", __func__, 191 (unsigned) div64_u64(cur_sector * 100, end_sector)); 192 last_print = jiffies; 193 } 194 195 try_read_btree_node(w->f, ca, bio, buf, 196 bucket * ca->mi.bucket_size + bucket_offset); 197 } 198 err: 199 bio_put(bio); 200 free_page((unsigned long) buf); 201 percpu_ref_get(&ca->io_ref); 202 closure_put(w->cl); 203 kfree(w); 204 return 0; 205 } 206 207 static int read_btree_nodes(struct find_btree_nodes *f) 208 { 209 struct bch_fs *c = container_of(f, struct bch_fs, found_btree_nodes); 210 struct closure cl; 211 int ret = 0; 212 213 closure_init_stack(&cl); 214 215 for_each_online_member(c, ca) { 216 struct find_btree_nodes_worker *w = kmalloc(sizeof(*w), GFP_KERNEL); 217 struct task_struct *t; 218 219 if (!w) { 220 percpu_ref_put(&ca->io_ref); 221 ret = -ENOMEM; 222 goto err; 223 } 224 225 percpu_ref_get(&ca->io_ref); 226 closure_get(&cl); 227 w->cl = &cl; 228 w->f = f; 229 w->ca = ca; 230 231 t = kthread_run(read_btree_nodes_worker, w, "read_btree_nodes/%s", ca->name); 232 ret = IS_ERR_OR_NULL(t); 233 if (ret) { 234 percpu_ref_put(&ca->io_ref); 235 closure_put(&cl); 236 f->ret = ret; 237 bch_err(c, "error starting kthread: %i", ret); 238 break; 239 } 240 } 241 err: 242 closure_sync(&cl); 243 return f->ret ?: ret; 244 } 245 246 static void bubble_up(struct found_btree_node *n, struct found_btree_node *end) 247 { 248 while (n + 1 < end && 249 found_btree_node_cmp_pos(n, n + 1) > 0) { 250 swap(n[0], n[1]); 251 n++; 252 } 253 } 254 255 static int handle_overwrites(struct bch_fs *c, 256 struct found_btree_node *start, 257 struct found_btree_node *end) 258 { 259 struct found_btree_node *n; 260 again: 261 for (n = start + 1; 262 n < end && 263 n->btree_id == start->btree_id && 264 n->level == start->level && 265 bpos_lt(n->min_key, start->max_key); 266 n++) { 267 int cmp = found_btree_node_cmp_time(start, n); 268 269 if (cmp > 0) { 270 if (bpos_cmp(start->max_key, n->max_key) >= 0) 271 n->overwritten = true; 272 else { 273 n->range_updated = true; 274 n->min_key = bpos_successor(start->max_key); 275 n->range_updated = true; 276 bubble_up(n, end); 277 goto again; 278 } 279 } else if (cmp < 0) { 280 BUG_ON(bpos_cmp(n->min_key, start->min_key) <= 0); 281 282 start->max_key = bpos_predecessor(n->min_key); 283 start->range_updated = true; 284 } else { 285 struct printbuf buf = PRINTBUF; 286 287 prt_str(&buf, "overlapping btree nodes with same seq! halting\n "); 288 found_btree_node_to_text(&buf, c, start); 289 prt_str(&buf, "\n "); 290 found_btree_node_to_text(&buf, c, n); 291 bch_err(c, "%s", buf.buf); 292 printbuf_exit(&buf); 293 return -1; 294 } 295 } 296 297 return 0; 298 } 299 300 int bch2_scan_for_btree_nodes(struct bch_fs *c) 301 { 302 struct find_btree_nodes *f = &c->found_btree_nodes; 303 struct printbuf buf = PRINTBUF; 304 size_t dst; 305 int ret = 0; 306 307 if (f->nodes.nr) 308 return 0; 309 310 mutex_init(&f->lock); 311 312 ret = read_btree_nodes(f); 313 if (ret) 314 return ret; 315 316 if (!f->nodes.nr) { 317 bch_err(c, "%s: no btree nodes found", __func__); 318 ret = -EINVAL; 319 goto err; 320 } 321 322 if (0 && c->opts.verbose) { 323 printbuf_reset(&buf); 324 prt_printf(&buf, "%s: nodes found:\n", __func__); 325 found_btree_nodes_to_text(&buf, c, f->nodes); 326 bch2_print_string_as_lines(KERN_INFO, buf.buf); 327 } 328 329 sort(f->nodes.data, f->nodes.nr, sizeof(f->nodes.data[0]), found_btree_node_cmp_cookie, NULL); 330 331 dst = 0; 332 darray_for_each(f->nodes, i) { 333 struct found_btree_node *prev = dst ? f->nodes.data + dst - 1 : NULL; 334 335 if (prev && 336 prev->cookie == i->cookie) { 337 if (prev->nr_ptrs == ARRAY_SIZE(prev->ptrs)) { 338 bch_err(c, "%s: found too many replicas for btree node", __func__); 339 ret = -EINVAL; 340 goto err; 341 } 342 prev->ptrs[prev->nr_ptrs++] = i->ptrs[0]; 343 } else { 344 f->nodes.data[dst++] = *i; 345 } 346 } 347 f->nodes.nr = dst; 348 349 sort(f->nodes.data, f->nodes.nr, sizeof(f->nodes.data[0]), found_btree_node_cmp_pos, NULL); 350 351 if (0 && c->opts.verbose) { 352 printbuf_reset(&buf); 353 prt_printf(&buf, "%s: nodes after merging replicas:\n", __func__); 354 found_btree_nodes_to_text(&buf, c, f->nodes); 355 bch2_print_string_as_lines(KERN_INFO, buf.buf); 356 } 357 358 dst = 0; 359 darray_for_each(f->nodes, i) { 360 if (i->overwritten) 361 continue; 362 363 ret = handle_overwrites(c, i, &darray_top(f->nodes)); 364 if (ret) 365 goto err; 366 367 BUG_ON(i->overwritten); 368 f->nodes.data[dst++] = *i; 369 } 370 f->nodes.nr = dst; 371 372 if (c->opts.verbose) { 373 printbuf_reset(&buf); 374 prt_printf(&buf, "%s: nodes found after overwrites:\n", __func__); 375 found_btree_nodes_to_text(&buf, c, f->nodes); 376 bch2_print_string_as_lines(KERN_INFO, buf.buf); 377 } 378 379 eytzinger0_sort(f->nodes.data, f->nodes.nr, sizeof(f->nodes.data[0]), found_btree_node_cmp_pos, NULL); 380 err: 381 printbuf_exit(&buf); 382 return ret; 383 } 384 385 static int found_btree_node_range_start_cmp(const void *_l, const void *_r) 386 { 387 const struct found_btree_node *l = _l; 388 const struct found_btree_node *r = _r; 389 390 return cmp_int(l->btree_id, r->btree_id) ?: 391 -cmp_int(l->level, r->level) ?: 392 bpos_cmp(l->max_key, r->min_key); 393 } 394 395 #define for_each_found_btree_node_in_range(_f, _search, _idx) \ 396 for (size_t _idx = eytzinger0_find_gt((_f)->nodes.data, (_f)->nodes.nr, \ 397 sizeof((_f)->nodes.data[0]), \ 398 found_btree_node_range_start_cmp, &search); \ 399 _idx < (_f)->nodes.nr && \ 400 (_f)->nodes.data[_idx].btree_id == _search.btree_id && \ 401 (_f)->nodes.data[_idx].level == _search.level && \ 402 bpos_lt((_f)->nodes.data[_idx].min_key, _search.max_key); \ 403 _idx = eytzinger0_next(_idx, (_f)->nodes.nr)) 404 405 bool bch2_btree_node_is_stale(struct bch_fs *c, struct btree *b) 406 { 407 struct find_btree_nodes *f = &c->found_btree_nodes; 408 409 struct found_btree_node search = { 410 .btree_id = b->c.btree_id, 411 .level = b->c.level, 412 .min_key = b->data->min_key, 413 .max_key = b->key.k.p, 414 }; 415 416 for_each_found_btree_node_in_range(f, search, idx) 417 if (f->nodes.data[idx].seq > BTREE_NODE_SEQ(b->data)) 418 return true; 419 return false; 420 } 421 422 bool bch2_btree_has_scanned_nodes(struct bch_fs *c, enum btree_id btree) 423 { 424 struct found_btree_node search = { 425 .btree_id = btree, 426 .level = 0, 427 .min_key = POS_MIN, 428 .max_key = SPOS_MAX, 429 }; 430 431 for_each_found_btree_node_in_range(&c->found_btree_nodes, search, idx) 432 return true; 433 return false; 434 } 435 436 int bch2_get_scanned_nodes(struct bch_fs *c, enum btree_id btree, 437 unsigned level, struct bpos node_min, struct bpos node_max) 438 { 439 struct find_btree_nodes *f = &c->found_btree_nodes; 440 441 int ret = bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_scan_for_btree_nodes); 442 if (ret) 443 return ret; 444 445 if (c->opts.verbose) { 446 struct printbuf buf = PRINTBUF; 447 448 prt_printf(&buf, "recovering %s l=%u ", bch2_btree_id_str(btree), level); 449 bch2_bpos_to_text(&buf, node_min); 450 prt_str(&buf, " - "); 451 bch2_bpos_to_text(&buf, node_max); 452 453 bch_info(c, "%s(): %s", __func__, buf.buf); 454 printbuf_exit(&buf); 455 } 456 457 struct found_btree_node search = { 458 .btree_id = btree, 459 .level = level, 460 .min_key = node_min, 461 .max_key = node_max, 462 }; 463 464 for_each_found_btree_node_in_range(f, search, idx) { 465 struct found_btree_node n = f->nodes.data[idx]; 466 467 n.range_updated |= bpos_lt(n.min_key, node_min); 468 n.min_key = bpos_max(n.min_key, node_min); 469 470 n.range_updated |= bpos_gt(n.max_key, node_max); 471 n.max_key = bpos_min(n.max_key, node_max); 472 473 struct { __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX); } tmp; 474 475 found_btree_node_to_key(&tmp.k, &n); 476 477 struct printbuf buf = PRINTBUF; 478 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&tmp.k)); 479 bch_verbose(c, "%s(): recovering %s", __func__, buf.buf); 480 printbuf_exit(&buf); 481 482 BUG_ON(bch2_bkey_invalid(c, bkey_i_to_s_c(&tmp.k), BKEY_TYPE_btree, 0, NULL)); 483 484 ret = bch2_journal_key_insert(c, btree, level + 1, &tmp.k); 485 if (ret) 486 return ret; 487 } 488 489 return 0; 490 } 491 492 void bch2_find_btree_nodes_exit(struct find_btree_nodes *f) 493 { 494 darray_exit(&f->nodes); 495 } 496