1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Code for manipulating bucket marks for garbage collection. 4 * 5 * Copyright 2014 Datera, Inc. 6 */ 7 8 #include "bcachefs.h" 9 #include "alloc_background.h" 10 #include "backpointers.h" 11 #include "bset.h" 12 #include "btree_gc.h" 13 #include "btree_update.h" 14 #include "buckets.h" 15 #include "buckets_waiting_for_journal.h" 16 #include "ec.h" 17 #include "error.h" 18 #include "inode.h" 19 #include "movinggc.h" 20 #include "recovery.h" 21 #include "reflink.h" 22 #include "replicas.h" 23 #include "subvolume.h" 24 #include "trace.h" 25 26 #include <linux/preempt.h> 27 28 static inline void fs_usage_data_type_to_base(struct bch_fs_usage_base *fs_usage, 29 enum bch_data_type data_type, 30 s64 sectors) 31 { 32 switch (data_type) { 33 case BCH_DATA_btree: 34 fs_usage->btree += sectors; 35 break; 36 case BCH_DATA_user: 37 case BCH_DATA_parity: 38 fs_usage->data += sectors; 39 break; 40 case BCH_DATA_cached: 41 fs_usage->cached += sectors; 42 break; 43 default: 44 break; 45 } 46 } 47 48 void bch2_fs_usage_initialize(struct bch_fs *c) 49 { 50 percpu_down_write(&c->mark_lock); 51 struct bch_fs_usage *usage = c->usage_base; 52 53 for (unsigned i = 0; i < ARRAY_SIZE(c->usage); i++) 54 bch2_fs_usage_acc_to_base(c, i); 55 56 for (unsigned i = 0; i < BCH_REPLICAS_MAX; i++) 57 usage->b.reserved += usage->persistent_reserved[i]; 58 59 for (unsigned i = 0; i < c->replicas.nr; i++) { 60 struct bch_replicas_entry_v1 *e = 61 cpu_replicas_entry(&c->replicas, i); 62 63 fs_usage_data_type_to_base(&usage->b, e->data_type, usage->replicas[i]); 64 } 65 66 for_each_member_device(c, ca) { 67 struct bch_dev_usage dev = bch2_dev_usage_read(ca); 68 69 usage->b.hidden += (dev.d[BCH_DATA_sb].buckets + 70 dev.d[BCH_DATA_journal].buckets) * 71 ca->mi.bucket_size; 72 } 73 74 percpu_up_write(&c->mark_lock); 75 } 76 77 static inline struct bch_dev_usage *dev_usage_ptr(struct bch_dev *ca, 78 unsigned journal_seq, 79 bool gc) 80 { 81 BUG_ON(!gc && !journal_seq); 82 83 return this_cpu_ptr(gc 84 ? ca->usage_gc 85 : ca->usage[journal_seq & JOURNAL_BUF_MASK]); 86 } 87 88 void bch2_dev_usage_read_fast(struct bch_dev *ca, struct bch_dev_usage *usage) 89 { 90 struct bch_fs *c = ca->fs; 91 unsigned seq, i, u64s = dev_usage_u64s(); 92 93 do { 94 seq = read_seqcount_begin(&c->usage_lock); 95 memcpy(usage, ca->usage_base, u64s * sizeof(u64)); 96 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) 97 acc_u64s_percpu((u64 *) usage, (u64 __percpu *) ca->usage[i], u64s); 98 } while (read_seqcount_retry(&c->usage_lock, seq)); 99 } 100 101 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v) 102 { 103 ssize_t offset = v - (u64 *) c->usage_base; 104 unsigned i, seq; 105 u64 ret; 106 107 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c)); 108 percpu_rwsem_assert_held(&c->mark_lock); 109 110 do { 111 seq = read_seqcount_begin(&c->usage_lock); 112 ret = *v; 113 114 for (i = 0; i < ARRAY_SIZE(c->usage); i++) 115 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset); 116 } while (read_seqcount_retry(&c->usage_lock, seq)); 117 118 return ret; 119 } 120 121 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c) 122 { 123 struct bch_fs_usage_online *ret; 124 unsigned nr_replicas = READ_ONCE(c->replicas.nr); 125 unsigned seq, i; 126 retry: 127 ret = kmalloc(__fs_usage_online_u64s(nr_replicas) * sizeof(u64), GFP_KERNEL); 128 if (unlikely(!ret)) 129 return NULL; 130 131 percpu_down_read(&c->mark_lock); 132 133 if (nr_replicas != c->replicas.nr) { 134 nr_replicas = c->replicas.nr; 135 percpu_up_read(&c->mark_lock); 136 kfree(ret); 137 goto retry; 138 } 139 140 ret->online_reserved = percpu_u64_get(c->online_reserved); 141 142 do { 143 seq = read_seqcount_begin(&c->usage_lock); 144 unsafe_memcpy(&ret->u, c->usage_base, 145 __fs_usage_u64s(nr_replicas) * sizeof(u64), 146 "embedded variable length struct"); 147 for (i = 0; i < ARRAY_SIZE(c->usage); i++) 148 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], 149 __fs_usage_u64s(nr_replicas)); 150 } while (read_seqcount_retry(&c->usage_lock, seq)); 151 152 return ret; 153 } 154 155 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx) 156 { 157 unsigned u64s = fs_usage_u64s(c); 158 159 BUG_ON(idx >= ARRAY_SIZE(c->usage)); 160 161 preempt_disable(); 162 write_seqcount_begin(&c->usage_lock); 163 164 acc_u64s_percpu((u64 *) c->usage_base, 165 (u64 __percpu *) c->usage[idx], u64s); 166 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64)); 167 168 rcu_read_lock(); 169 for_each_member_device_rcu(c, ca, NULL) { 170 u64s = dev_usage_u64s(); 171 172 acc_u64s_percpu((u64 *) ca->usage_base, 173 (u64 __percpu *) ca->usage[idx], u64s); 174 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64)); 175 } 176 rcu_read_unlock(); 177 178 write_seqcount_end(&c->usage_lock); 179 preempt_enable(); 180 } 181 182 void bch2_fs_usage_to_text(struct printbuf *out, 183 struct bch_fs *c, 184 struct bch_fs_usage_online *fs_usage) 185 { 186 unsigned i; 187 188 prt_printf(out, "capacity:\t\t\t%llu\n", c->capacity); 189 190 prt_printf(out, "hidden:\t\t\t\t%llu\n", 191 fs_usage->u.b.hidden); 192 prt_printf(out, "data:\t\t\t\t%llu\n", 193 fs_usage->u.b.data); 194 prt_printf(out, "cached:\t\t\t\t%llu\n", 195 fs_usage->u.b.cached); 196 prt_printf(out, "reserved:\t\t\t%llu\n", 197 fs_usage->u.b.reserved); 198 prt_printf(out, "nr_inodes:\t\t\t%llu\n", 199 fs_usage->u.b.nr_inodes); 200 prt_printf(out, "online reserved:\t\t%llu\n", 201 fs_usage->online_reserved); 202 203 for (i = 0; 204 i < ARRAY_SIZE(fs_usage->u.persistent_reserved); 205 i++) { 206 prt_printf(out, "%u replicas:\n", i + 1); 207 prt_printf(out, "\treserved:\t\t%llu\n", 208 fs_usage->u.persistent_reserved[i]); 209 } 210 211 for (i = 0; i < c->replicas.nr; i++) { 212 struct bch_replicas_entry_v1 *e = 213 cpu_replicas_entry(&c->replicas, i); 214 215 prt_printf(out, "\t"); 216 bch2_replicas_entry_to_text(out, e); 217 prt_printf(out, ":\t%llu\n", fs_usage->u.replicas[i]); 218 } 219 } 220 221 static u64 reserve_factor(u64 r) 222 { 223 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR); 224 } 225 226 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage) 227 { 228 return min(fs_usage->u.b.hidden + 229 fs_usage->u.b.btree + 230 fs_usage->u.b.data + 231 reserve_factor(fs_usage->u.b.reserved + 232 fs_usage->online_reserved), 233 c->capacity); 234 } 235 236 static struct bch_fs_usage_short 237 __bch2_fs_usage_read_short(struct bch_fs *c) 238 { 239 struct bch_fs_usage_short ret; 240 u64 data, reserved; 241 242 ret.capacity = c->capacity - 243 bch2_fs_usage_read_one(c, &c->usage_base->b.hidden); 244 245 data = bch2_fs_usage_read_one(c, &c->usage_base->b.data) + 246 bch2_fs_usage_read_one(c, &c->usage_base->b.btree); 247 reserved = bch2_fs_usage_read_one(c, &c->usage_base->b.reserved) + 248 percpu_u64_get(c->online_reserved); 249 250 ret.used = min(ret.capacity, data + reserve_factor(reserved)); 251 ret.free = ret.capacity - ret.used; 252 253 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->b.nr_inodes); 254 255 return ret; 256 } 257 258 struct bch_fs_usage_short 259 bch2_fs_usage_read_short(struct bch_fs *c) 260 { 261 struct bch_fs_usage_short ret; 262 263 percpu_down_read(&c->mark_lock); 264 ret = __bch2_fs_usage_read_short(c); 265 percpu_up_read(&c->mark_lock); 266 267 return ret; 268 } 269 270 void bch2_dev_usage_init(struct bch_dev *ca) 271 { 272 ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket; 273 } 274 275 void bch2_dev_usage_to_text(struct printbuf *out, struct bch_dev_usage *usage) 276 { 277 prt_printf(out, "\tbuckets\rsectors\rfragmented\r\n"); 278 279 for (unsigned i = 0; i < BCH_DATA_NR; i++) { 280 bch2_prt_data_type(out, i); 281 prt_printf(out, "\t%llu\r%llu\r%llu\r\n", 282 usage->d[i].buckets, 283 usage->d[i].sectors, 284 usage->d[i].fragmented); 285 } 286 } 287 288 void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca, 289 const struct bch_alloc_v4 *old, 290 const struct bch_alloc_v4 *new, 291 u64 journal_seq, bool gc) 292 { 293 struct bch_fs_usage *fs_usage; 294 struct bch_dev_usage *u; 295 296 preempt_disable(); 297 fs_usage = fs_usage_ptr(c, journal_seq, gc); 298 299 if (data_type_is_hidden(old->data_type)) 300 fs_usage->b.hidden -= ca->mi.bucket_size; 301 if (data_type_is_hidden(new->data_type)) 302 fs_usage->b.hidden += ca->mi.bucket_size; 303 304 u = dev_usage_ptr(ca, journal_seq, gc); 305 306 u->d[old->data_type].buckets--; 307 u->d[new->data_type].buckets++; 308 309 u->d[old->data_type].sectors -= bch2_bucket_sectors_dirty(*old); 310 u->d[new->data_type].sectors += bch2_bucket_sectors_dirty(*new); 311 312 u->d[BCH_DATA_cached].sectors += new->cached_sectors; 313 u->d[BCH_DATA_cached].sectors -= old->cached_sectors; 314 315 u->d[old->data_type].fragmented -= bch2_bucket_sectors_fragmented(ca, *old); 316 u->d[new->data_type].fragmented += bch2_bucket_sectors_fragmented(ca, *new); 317 318 preempt_enable(); 319 } 320 321 static inline int __update_replicas(struct bch_fs *c, 322 struct bch_fs_usage *fs_usage, 323 struct bch_replicas_entry_v1 *r, 324 s64 sectors) 325 { 326 int idx = bch2_replicas_entry_idx(c, r); 327 328 if (idx < 0) 329 return -1; 330 331 fs_usage_data_type_to_base(&fs_usage->b, r->data_type, sectors); 332 fs_usage->replicas[idx] += sectors; 333 return 0; 334 } 335 336 int bch2_update_replicas(struct bch_fs *c, struct bkey_s_c k, 337 struct bch_replicas_entry_v1 *r, s64 sectors, 338 unsigned journal_seq, bool gc) 339 { 340 struct bch_fs_usage *fs_usage; 341 int idx, ret = 0; 342 struct printbuf buf = PRINTBUF; 343 344 percpu_down_read(&c->mark_lock); 345 346 idx = bch2_replicas_entry_idx(c, r); 347 if (idx < 0 && 348 fsck_err(c, ptr_to_missing_replicas_entry, 349 "no replicas entry\n while marking %s", 350 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { 351 percpu_up_read(&c->mark_lock); 352 ret = bch2_mark_replicas(c, r); 353 percpu_down_read(&c->mark_lock); 354 355 if (ret) 356 goto err; 357 idx = bch2_replicas_entry_idx(c, r); 358 } 359 if (idx < 0) { 360 ret = -1; 361 goto err; 362 } 363 364 preempt_disable(); 365 fs_usage = fs_usage_ptr(c, journal_seq, gc); 366 fs_usage_data_type_to_base(&fs_usage->b, r->data_type, sectors); 367 fs_usage->replicas[idx] += sectors; 368 preempt_enable(); 369 err: 370 fsck_err: 371 percpu_up_read(&c->mark_lock); 372 printbuf_exit(&buf); 373 return ret; 374 } 375 376 static inline int update_cached_sectors(struct bch_fs *c, 377 struct bkey_s_c k, 378 unsigned dev, s64 sectors, 379 unsigned journal_seq, bool gc) 380 { 381 struct bch_replicas_padded r; 382 383 bch2_replicas_entry_cached(&r.e, dev); 384 385 return bch2_update_replicas(c, k, &r.e, sectors, journal_seq, gc); 386 } 387 388 static int __replicas_deltas_realloc(struct btree_trans *trans, unsigned more, 389 gfp_t gfp) 390 { 391 struct replicas_delta_list *d = trans->fs_usage_deltas; 392 unsigned new_size = d ? (d->size + more) * 2 : 128; 393 unsigned alloc_size = sizeof(*d) + new_size; 394 395 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX); 396 397 if (!d || d->used + more > d->size) { 398 d = krealloc(d, alloc_size, gfp|__GFP_ZERO); 399 400 if (unlikely(!d)) { 401 if (alloc_size > REPLICAS_DELTA_LIST_MAX) 402 return -ENOMEM; 403 404 d = mempool_alloc(&trans->c->replicas_delta_pool, gfp); 405 if (!d) 406 return -ENOMEM; 407 408 memset(d, 0, REPLICAS_DELTA_LIST_MAX); 409 410 if (trans->fs_usage_deltas) 411 memcpy(d, trans->fs_usage_deltas, 412 trans->fs_usage_deltas->size + sizeof(*d)); 413 414 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d); 415 kfree(trans->fs_usage_deltas); 416 } 417 418 d->size = new_size; 419 trans->fs_usage_deltas = d; 420 } 421 422 return 0; 423 } 424 425 int bch2_replicas_deltas_realloc(struct btree_trans *trans, unsigned more) 426 { 427 return allocate_dropping_locks_errcode(trans, 428 __replicas_deltas_realloc(trans, more, _gfp)); 429 } 430 431 int bch2_update_replicas_list(struct btree_trans *trans, 432 struct bch_replicas_entry_v1 *r, 433 s64 sectors) 434 { 435 struct replicas_delta_list *d; 436 struct replicas_delta *n; 437 unsigned b; 438 int ret; 439 440 if (!sectors) 441 return 0; 442 443 b = replicas_entry_bytes(r) + 8; 444 ret = bch2_replicas_deltas_realloc(trans, b); 445 if (ret) 446 return ret; 447 448 d = trans->fs_usage_deltas; 449 n = (void *) d->d + d->used; 450 n->delta = sectors; 451 unsafe_memcpy((void *) n + offsetof(struct replicas_delta, r), 452 r, replicas_entry_bytes(r), 453 "flexible array member embedded in strcuct with padding"); 454 bch2_replicas_entry_sort(&n->r); 455 d->used += b; 456 return 0; 457 } 458 459 int bch2_update_cached_sectors_list(struct btree_trans *trans, unsigned dev, s64 sectors) 460 { 461 struct bch_replicas_padded r; 462 463 bch2_replicas_entry_cached(&r.e, dev); 464 465 return bch2_update_replicas_list(trans, &r.e, sectors); 466 } 467 468 int bch2_check_fix_ptrs(struct btree_trans *trans, 469 enum btree_id btree, unsigned level, struct bkey_s_c k, 470 enum btree_iter_update_trigger_flags flags) 471 { 472 struct bch_fs *c = trans->c; 473 struct bkey_ptrs_c ptrs_c = bch2_bkey_ptrs_c(k); 474 const union bch_extent_entry *entry_c; 475 struct extent_ptr_decoded p = { 0 }; 476 bool do_update = false; 477 struct printbuf buf = PRINTBUF; 478 int ret = 0; 479 480 percpu_down_read(&c->mark_lock); 481 482 rcu_read_lock(); 483 bkey_for_each_ptr_decode(k.k, ptrs_c, p, entry_c) { 484 struct bch_dev *ca = bch2_dev_rcu(c, p.ptr.dev); 485 if (!ca) { 486 if (fsck_err(c, ptr_to_invalid_device, 487 "pointer to missing device %u\n" 488 "while marking %s", 489 p.ptr.dev, 490 (printbuf_reset(&buf), 491 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) 492 do_update = true; 493 continue; 494 } 495 496 struct bucket *g = PTR_GC_BUCKET(ca, &p.ptr); 497 enum bch_data_type data_type = bch2_bkey_ptr_data_type(k, p, entry_c); 498 499 if (fsck_err_on(!g->gen_valid, 500 c, ptr_to_missing_alloc_key, 501 "bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n" 502 "while marking %s", 503 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), 504 bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), 505 p.ptr.gen, 506 (printbuf_reset(&buf), 507 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { 508 if (!p.ptr.cached) { 509 g->gen_valid = true; 510 g->gen = p.ptr.gen; 511 } else { 512 do_update = true; 513 } 514 } 515 516 if (fsck_err_on(gen_cmp(p.ptr.gen, g->gen) > 0, 517 c, ptr_gen_newer_than_bucket_gen, 518 "bucket %u:%zu data type %s ptr gen in the future: %u > %u\n" 519 "while marking %s", 520 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), 521 bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), 522 p.ptr.gen, g->gen, 523 (printbuf_reset(&buf), 524 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { 525 if (!p.ptr.cached && 526 (g->data_type != BCH_DATA_btree || 527 data_type == BCH_DATA_btree)) { 528 g->gen_valid = true; 529 g->gen = p.ptr.gen; 530 g->data_type = 0; 531 g->dirty_sectors = 0; 532 g->cached_sectors = 0; 533 } else { 534 do_update = true; 535 } 536 } 537 538 if (fsck_err_on(gen_cmp(g->gen, p.ptr.gen) > BUCKET_GC_GEN_MAX, 539 c, ptr_gen_newer_than_bucket_gen, 540 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n" 541 "while marking %s", 542 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen, 543 bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), 544 p.ptr.gen, 545 (printbuf_reset(&buf), 546 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) 547 do_update = true; 548 549 if (fsck_err_on(!p.ptr.cached && gen_cmp(p.ptr.gen, g->gen) < 0, 550 c, stale_dirty_ptr, 551 "bucket %u:%zu data type %s stale dirty ptr: %u < %u\n" 552 "while marking %s", 553 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), 554 bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), 555 p.ptr.gen, g->gen, 556 (printbuf_reset(&buf), 557 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) 558 do_update = true; 559 560 if (data_type != BCH_DATA_btree && p.ptr.gen != g->gen) 561 continue; 562 563 if (fsck_err_on(bucket_data_type_mismatch(g->data_type, data_type), 564 c, ptr_bucket_data_type_mismatch, 565 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n" 566 "while marking %s", 567 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen, 568 bch2_data_type_str(g->data_type), 569 bch2_data_type_str(data_type), 570 (printbuf_reset(&buf), 571 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { 572 if (data_type == BCH_DATA_btree) { 573 g->gen_valid = true; 574 g->gen = p.ptr.gen; 575 g->data_type = data_type; 576 g->dirty_sectors = 0; 577 g->cached_sectors = 0; 578 } else { 579 do_update = true; 580 } 581 } 582 583 if (p.has_ec) { 584 struct gc_stripe *m = genradix_ptr(&c->gc_stripes, p.ec.idx); 585 586 if (fsck_err_on(!m || !m->alive, c, 587 ptr_to_missing_stripe, 588 "pointer to nonexistent stripe %llu\n" 589 "while marking %s", 590 (u64) p.ec.idx, 591 (printbuf_reset(&buf), 592 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) 593 do_update = true; 594 595 if (fsck_err_on(m && m->alive && !bch2_ptr_matches_stripe_m(m, p), c, 596 ptr_to_incorrect_stripe, 597 "pointer does not match stripe %llu\n" 598 "while marking %s", 599 (u64) p.ec.idx, 600 (printbuf_reset(&buf), 601 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) 602 do_update = true; 603 } 604 } 605 rcu_read_unlock(); 606 607 if (do_update) { 608 if (flags & BTREE_TRIGGER_is_root) { 609 bch_err(c, "cannot update btree roots yet"); 610 ret = -EINVAL; 611 goto err; 612 } 613 614 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k); 615 ret = PTR_ERR_OR_ZERO(new); 616 if (ret) 617 goto err; 618 619 rcu_read_lock(); 620 bch2_bkey_drop_ptrs(bkey_i_to_s(new), ptr, !bch2_dev_rcu(c, ptr->dev)); 621 rcu_read_unlock(); 622 623 if (level) { 624 /* 625 * We don't want to drop btree node pointers - if the 626 * btree node isn't there anymore, the read path will 627 * sort it out: 628 */ 629 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(new)); 630 rcu_read_lock(); 631 bkey_for_each_ptr(ptrs, ptr) { 632 struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev); 633 struct bucket *g = PTR_GC_BUCKET(ca, ptr); 634 635 ptr->gen = g->gen; 636 } 637 rcu_read_unlock(); 638 } else { 639 struct bkey_ptrs ptrs; 640 union bch_extent_entry *entry; 641 restart_drop_ptrs: 642 ptrs = bch2_bkey_ptrs(bkey_i_to_s(new)); 643 rcu_read_lock(); 644 bkey_for_each_ptr_decode(bkey_i_to_s(new).k, ptrs, p, entry) { 645 struct bch_dev *ca = bch2_dev_rcu(c, p.ptr.dev); 646 struct bucket *g = PTR_GC_BUCKET(ca, &p.ptr); 647 enum bch_data_type data_type = bch2_bkey_ptr_data_type(bkey_i_to_s_c(new), p, entry); 648 649 if ((p.ptr.cached && 650 (!g->gen_valid || gen_cmp(p.ptr.gen, g->gen) > 0)) || 651 (!p.ptr.cached && 652 gen_cmp(p.ptr.gen, g->gen) < 0) || 653 gen_cmp(g->gen, p.ptr.gen) > BUCKET_GC_GEN_MAX || 654 (g->data_type && 655 g->data_type != data_type)) { 656 bch2_bkey_drop_ptr(bkey_i_to_s(new), &entry->ptr); 657 goto restart_drop_ptrs; 658 } 659 } 660 rcu_read_unlock(); 661 again: 662 ptrs = bch2_bkey_ptrs(bkey_i_to_s(new)); 663 bkey_extent_entry_for_each(ptrs, entry) { 664 if (extent_entry_type(entry) == BCH_EXTENT_ENTRY_stripe_ptr) { 665 struct gc_stripe *m = genradix_ptr(&c->gc_stripes, 666 entry->stripe_ptr.idx); 667 union bch_extent_entry *next_ptr; 668 669 bkey_extent_entry_for_each_from(ptrs, next_ptr, entry) 670 if (extent_entry_type(next_ptr) == BCH_EXTENT_ENTRY_ptr) 671 goto found; 672 next_ptr = NULL; 673 found: 674 if (!next_ptr) { 675 bch_err(c, "aieee, found stripe ptr with no data ptr"); 676 continue; 677 } 678 679 if (!m || !m->alive || 680 !__bch2_ptr_matches_stripe(&m->ptrs[entry->stripe_ptr.block], 681 &next_ptr->ptr, 682 m->sectors)) { 683 bch2_bkey_extent_entry_drop(new, entry); 684 goto again; 685 } 686 } 687 } 688 } 689 690 if (0) { 691 printbuf_reset(&buf); 692 bch2_bkey_val_to_text(&buf, c, k); 693 bch_info(c, "updated %s", buf.buf); 694 695 printbuf_reset(&buf); 696 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(new)); 697 bch_info(c, "new key %s", buf.buf); 698 } 699 700 percpu_up_read(&c->mark_lock); 701 struct btree_iter iter; 702 bch2_trans_node_iter_init(trans, &iter, btree, new->k.p, 0, level, 703 BTREE_ITER_intent|BTREE_ITER_all_snapshots); 704 ret = bch2_btree_iter_traverse(&iter) ?: 705 bch2_trans_update(trans, &iter, new, 706 BTREE_UPDATE_internal_snapshot_node| 707 BTREE_TRIGGER_norun); 708 bch2_trans_iter_exit(trans, &iter); 709 percpu_down_read(&c->mark_lock); 710 711 if (ret) 712 goto err; 713 714 if (level) 715 bch2_btree_node_update_key_early(trans, btree, level - 1, k, new); 716 } 717 err: 718 fsck_err: 719 percpu_up_read(&c->mark_lock); 720 printbuf_exit(&buf); 721 return ret; 722 } 723 724 int bch2_bucket_ref_update(struct btree_trans *trans, struct bch_dev *ca, 725 struct bkey_s_c k, 726 const struct bch_extent_ptr *ptr, 727 s64 sectors, enum bch_data_type ptr_data_type, 728 u8 b_gen, u8 bucket_data_type, 729 u32 *bucket_sectors) 730 { 731 struct bch_fs *c = trans->c; 732 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr); 733 struct printbuf buf = PRINTBUF; 734 bool inserting = sectors > 0; 735 int ret = 0; 736 737 BUG_ON(!sectors); 738 739 if (gen_after(ptr->gen, b_gen)) { 740 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 741 BCH_FSCK_ERR_ptr_gen_newer_than_bucket_gen, 742 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n" 743 "while marking %s", 744 ptr->dev, bucket_nr, b_gen, 745 bch2_data_type_str(bucket_data_type ?: ptr_data_type), 746 ptr->gen, 747 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 748 if (inserting) 749 goto err; 750 goto out; 751 } 752 753 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) { 754 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 755 BCH_FSCK_ERR_ptr_too_stale, 756 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n" 757 "while marking %s", 758 ptr->dev, bucket_nr, b_gen, 759 bch2_data_type_str(bucket_data_type ?: ptr_data_type), 760 ptr->gen, 761 (printbuf_reset(&buf), 762 bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 763 if (inserting) 764 goto err; 765 goto out; 766 } 767 768 if (b_gen != ptr->gen && ptr->cached) { 769 ret = 1; 770 goto out; 771 } 772 773 if (b_gen != ptr->gen) { 774 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 775 BCH_FSCK_ERR_stale_dirty_ptr, 776 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n" 777 "while marking %s", 778 ptr->dev, bucket_nr, b_gen, 779 *bucket_gen(ca, bucket_nr), 780 bch2_data_type_str(bucket_data_type ?: ptr_data_type), 781 ptr->gen, 782 (printbuf_reset(&buf), 783 bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 784 if (inserting) 785 goto err; 786 goto out; 787 } 788 789 if (bucket_data_type_mismatch(bucket_data_type, ptr_data_type)) { 790 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 791 BCH_FSCK_ERR_ptr_bucket_data_type_mismatch, 792 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n" 793 "while marking %s", 794 ptr->dev, bucket_nr, b_gen, 795 bch2_data_type_str(bucket_data_type), 796 bch2_data_type_str(ptr_data_type), 797 (printbuf_reset(&buf), 798 bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 799 if (inserting) 800 goto err; 801 goto out; 802 } 803 804 if ((u64) *bucket_sectors + sectors > U32_MAX) { 805 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 806 BCH_FSCK_ERR_bucket_sector_count_overflow, 807 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U32_MAX\n" 808 "while marking %s", 809 ptr->dev, bucket_nr, b_gen, 810 bch2_data_type_str(bucket_data_type ?: ptr_data_type), 811 *bucket_sectors, sectors, 812 (printbuf_reset(&buf), 813 bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 814 if (inserting) 815 goto err; 816 sectors = -*bucket_sectors; 817 } 818 819 *bucket_sectors += sectors; 820 out: 821 printbuf_exit(&buf); 822 return ret; 823 err: 824 bch2_dump_trans_updates(trans); 825 ret = -EIO; 826 goto out; 827 } 828 829 void bch2_trans_fs_usage_revert(struct btree_trans *trans, 830 struct replicas_delta_list *deltas) 831 { 832 struct bch_fs *c = trans->c; 833 struct bch_fs_usage *dst; 834 struct replicas_delta *d, *top = (void *) deltas->d + deltas->used; 835 s64 added = 0; 836 unsigned i; 837 838 percpu_down_read(&c->mark_lock); 839 preempt_disable(); 840 dst = fs_usage_ptr(c, trans->journal_res.seq, false); 841 842 /* revert changes: */ 843 for (d = deltas->d; d != top; d = replicas_delta_next(d)) { 844 switch (d->r.data_type) { 845 case BCH_DATA_btree: 846 case BCH_DATA_user: 847 case BCH_DATA_parity: 848 added += d->delta; 849 } 850 BUG_ON(__update_replicas(c, dst, &d->r, -d->delta)); 851 } 852 853 dst->b.nr_inodes -= deltas->nr_inodes; 854 855 for (i = 0; i < BCH_REPLICAS_MAX; i++) { 856 added -= deltas->persistent_reserved[i]; 857 dst->b.reserved -= deltas->persistent_reserved[i]; 858 dst->persistent_reserved[i] -= deltas->persistent_reserved[i]; 859 } 860 861 if (added > 0) { 862 trans->disk_res->sectors += added; 863 this_cpu_add(*c->online_reserved, added); 864 } 865 866 preempt_enable(); 867 percpu_up_read(&c->mark_lock); 868 } 869 870 void bch2_trans_account_disk_usage_change(struct btree_trans *trans) 871 { 872 struct bch_fs *c = trans->c; 873 u64 disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0; 874 static int warned_disk_usage = 0; 875 bool warn = false; 876 877 percpu_down_read(&c->mark_lock); 878 preempt_disable(); 879 struct bch_fs_usage_base *dst = &fs_usage_ptr(c, trans->journal_res.seq, false)->b; 880 struct bch_fs_usage_base *src = &trans->fs_usage_delta; 881 882 s64 added = src->btree + src->data + src->reserved; 883 884 /* 885 * Not allowed to reduce sectors_available except by getting a 886 * reservation: 887 */ 888 s64 should_not_have_added = added - (s64) disk_res_sectors; 889 if (unlikely(should_not_have_added > 0)) { 890 u64 old, new, v = atomic64_read(&c->sectors_available); 891 892 do { 893 old = v; 894 new = max_t(s64, 0, old - should_not_have_added); 895 } while ((v = atomic64_cmpxchg(&c->sectors_available, 896 old, new)) != old); 897 898 added -= should_not_have_added; 899 warn = true; 900 } 901 902 if (added > 0) { 903 trans->disk_res->sectors -= added; 904 this_cpu_sub(*c->online_reserved, added); 905 } 906 907 dst->hidden += src->hidden; 908 dst->btree += src->btree; 909 dst->data += src->data; 910 dst->cached += src->cached; 911 dst->reserved += src->reserved; 912 dst->nr_inodes += src->nr_inodes; 913 914 preempt_enable(); 915 percpu_up_read(&c->mark_lock); 916 917 if (unlikely(warn) && !xchg(&warned_disk_usage, 1)) 918 bch2_trans_inconsistent(trans, 919 "disk usage increased %lli more than %llu sectors reserved)", 920 should_not_have_added, disk_res_sectors); 921 } 922 923 int bch2_trans_fs_usage_apply(struct btree_trans *trans, 924 struct replicas_delta_list *deltas) 925 { 926 struct bch_fs *c = trans->c; 927 struct replicas_delta *d, *d2; 928 struct replicas_delta *top = (void *) deltas->d + deltas->used; 929 struct bch_fs_usage *dst; 930 unsigned i; 931 932 percpu_down_read(&c->mark_lock); 933 preempt_disable(); 934 dst = fs_usage_ptr(c, trans->journal_res.seq, false); 935 936 for (d = deltas->d; d != top; d = replicas_delta_next(d)) 937 if (__update_replicas(c, dst, &d->r, d->delta)) 938 goto need_mark; 939 940 dst->b.nr_inodes += deltas->nr_inodes; 941 942 for (i = 0; i < BCH_REPLICAS_MAX; i++) { 943 dst->b.reserved += deltas->persistent_reserved[i]; 944 dst->persistent_reserved[i] += deltas->persistent_reserved[i]; 945 } 946 947 preempt_enable(); 948 percpu_up_read(&c->mark_lock); 949 return 0; 950 need_mark: 951 /* revert changes: */ 952 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2)) 953 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta)); 954 955 preempt_enable(); 956 percpu_up_read(&c->mark_lock); 957 return -1; 958 } 959 960 /* KEY_TYPE_extent: */ 961 962 static int __mark_pointer(struct btree_trans *trans, struct bch_dev *ca, 963 struct bkey_s_c k, 964 const struct bch_extent_ptr *ptr, 965 s64 sectors, enum bch_data_type ptr_data_type, 966 struct bch_alloc_v4 *a) 967 { 968 u32 *dst_sectors = !ptr->cached 969 ? &a->dirty_sectors 970 : &a->cached_sectors; 971 int ret = bch2_bucket_ref_update(trans, ca, k, ptr, sectors, ptr_data_type, 972 a->gen, a->data_type, dst_sectors); 973 974 if (ret) 975 return ret; 976 977 alloc_data_type_set(a, ptr_data_type); 978 return 0; 979 } 980 981 static int bch2_trigger_pointer(struct btree_trans *trans, 982 enum btree_id btree_id, unsigned level, 983 struct bkey_s_c k, struct extent_ptr_decoded p, 984 const union bch_extent_entry *entry, 985 s64 *sectors, 986 enum btree_iter_update_trigger_flags flags) 987 { 988 bool insert = !(flags & BTREE_TRIGGER_overwrite); 989 int ret = 0; 990 991 struct bch_fs *c = trans->c; 992 struct bch_dev *ca = bch2_dev_tryget(c, p.ptr.dev); 993 if (unlikely(!ca)) { 994 if (insert) 995 ret = -EIO; 996 goto err; 997 } 998 999 struct bpos bucket; 1000 struct bch_backpointer bp; 1001 bch2_extent_ptr_to_bp(trans->c, ca, btree_id, level, k, p, entry, &bucket, &bp); 1002 *sectors = insert ? bp.bucket_len : -((s64) bp.bucket_len); 1003 1004 if (flags & BTREE_TRIGGER_transactional) { 1005 struct bkey_i_alloc_v4 *a = bch2_trans_start_alloc_update(trans, bucket); 1006 ret = PTR_ERR_OR_ZERO(a) ?: 1007 __mark_pointer(trans, ca, k, &p.ptr, *sectors, bp.data_type, &a->v); 1008 if (ret) 1009 goto err; 1010 1011 if (!p.ptr.cached) { 1012 ret = bch2_bucket_backpointer_mod(trans, ca, bucket, bp, k, insert); 1013 if (ret) 1014 goto err; 1015 } 1016 } 1017 1018 if (flags & BTREE_TRIGGER_gc) { 1019 percpu_down_read(&c->mark_lock); 1020 struct bucket *g = gc_bucket(ca, bucket.offset); 1021 bucket_lock(g); 1022 struct bch_alloc_v4 old = bucket_m_to_alloc(*g), new = old; 1023 ret = __mark_pointer(trans, ca, k, &p.ptr, *sectors, bp.data_type, &new); 1024 if (!ret) { 1025 alloc_to_bucket(g, new); 1026 bch2_dev_usage_update(c, ca, &old, &new, 0, true); 1027 } 1028 bucket_unlock(g); 1029 percpu_up_read(&c->mark_lock); 1030 } 1031 err: 1032 bch2_dev_put(ca); 1033 return ret; 1034 } 1035 1036 static int bch2_trigger_stripe_ptr(struct btree_trans *trans, 1037 struct bkey_s_c k, 1038 struct extent_ptr_decoded p, 1039 enum bch_data_type data_type, 1040 s64 sectors, 1041 enum btree_iter_update_trigger_flags flags) 1042 { 1043 if (flags & BTREE_TRIGGER_transactional) { 1044 struct btree_iter iter; 1045 struct bkey_i_stripe *s = bch2_bkey_get_mut_typed(trans, &iter, 1046 BTREE_ID_stripes, POS(0, p.ec.idx), 1047 BTREE_ITER_with_updates, stripe); 1048 int ret = PTR_ERR_OR_ZERO(s); 1049 if (unlikely(ret)) { 1050 bch2_trans_inconsistent_on(bch2_err_matches(ret, ENOENT), trans, 1051 "pointer to nonexistent stripe %llu", 1052 (u64) p.ec.idx); 1053 goto err; 1054 } 1055 1056 if (!bch2_ptr_matches_stripe(&s->v, p)) { 1057 bch2_trans_inconsistent(trans, 1058 "stripe pointer doesn't match stripe %llu", 1059 (u64) p.ec.idx); 1060 ret = -EIO; 1061 goto err; 1062 } 1063 1064 stripe_blockcount_set(&s->v, p.ec.block, 1065 stripe_blockcount_get(&s->v, p.ec.block) + 1066 sectors); 1067 1068 struct bch_replicas_padded r; 1069 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i)); 1070 r.e.data_type = data_type; 1071 ret = bch2_update_replicas_list(trans, &r.e, sectors); 1072 err: 1073 bch2_trans_iter_exit(trans, &iter); 1074 return ret; 1075 } 1076 1077 if (flags & BTREE_TRIGGER_gc) { 1078 struct bch_fs *c = trans->c; 1079 1080 BUG_ON(!(flags & BTREE_TRIGGER_gc)); 1081 1082 struct gc_stripe *m = genradix_ptr_alloc(&c->gc_stripes, p.ec.idx, GFP_KERNEL); 1083 if (!m) { 1084 bch_err(c, "error allocating memory for gc_stripes, idx %llu", 1085 (u64) p.ec.idx); 1086 return -BCH_ERR_ENOMEM_mark_stripe_ptr; 1087 } 1088 1089 mutex_lock(&c->ec_stripes_heap_lock); 1090 1091 if (!m || !m->alive) { 1092 mutex_unlock(&c->ec_stripes_heap_lock); 1093 struct printbuf buf = PRINTBUF; 1094 bch2_bkey_val_to_text(&buf, c, k); 1095 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu\n while marking %s", 1096 (u64) p.ec.idx, buf.buf); 1097 printbuf_exit(&buf); 1098 bch2_inconsistent_error(c); 1099 return -EIO; 1100 } 1101 1102 m->block_sectors[p.ec.block] += sectors; 1103 1104 struct bch_replicas_padded r = m->r; 1105 mutex_unlock(&c->ec_stripes_heap_lock); 1106 1107 r.e.data_type = data_type; 1108 bch2_update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true); 1109 } 1110 1111 return 0; 1112 } 1113 1114 static int __trigger_extent(struct btree_trans *trans, 1115 enum btree_id btree_id, unsigned level, 1116 struct bkey_s_c k, 1117 enum btree_iter_update_trigger_flags flags) 1118 { 1119 bool gc = flags & BTREE_TRIGGER_gc; 1120 struct bch_fs *c = trans->c; 1121 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 1122 const union bch_extent_entry *entry; 1123 struct extent_ptr_decoded p; 1124 struct bch_replicas_padded r; 1125 enum bch_data_type data_type = bkey_is_btree_ptr(k.k) 1126 ? BCH_DATA_btree 1127 : BCH_DATA_user; 1128 s64 replicas_sectors = 0; 1129 int ret = 0; 1130 1131 r.e.data_type = data_type; 1132 r.e.nr_devs = 0; 1133 r.e.nr_required = 1; 1134 1135 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { 1136 s64 disk_sectors; 1137 ret = bch2_trigger_pointer(trans, btree_id, level, k, p, entry, &disk_sectors, flags); 1138 if (ret < 0) 1139 return ret; 1140 1141 bool stale = ret > 0; 1142 1143 if (p.ptr.cached) { 1144 if (!stale) { 1145 ret = !gc 1146 ? bch2_update_cached_sectors_list(trans, p.ptr.dev, disk_sectors) 1147 : update_cached_sectors(c, k, p.ptr.dev, disk_sectors, 0, true); 1148 bch2_fs_fatal_err_on(ret && gc, c, "%s: no replicas entry while updating cached sectors", 1149 bch2_err_str(ret)); 1150 if (ret) 1151 return ret; 1152 } 1153 } else if (!p.has_ec) { 1154 replicas_sectors += disk_sectors; 1155 r.e.devs[r.e.nr_devs++] = p.ptr.dev; 1156 } else { 1157 ret = bch2_trigger_stripe_ptr(trans, k, p, data_type, disk_sectors, flags); 1158 if (ret) 1159 return ret; 1160 1161 /* 1162 * There may be other dirty pointers in this extent, but 1163 * if so they're not required for mounting if we have an 1164 * erasure coded pointer in this extent: 1165 */ 1166 r.e.nr_required = 0; 1167 } 1168 } 1169 1170 if (r.e.nr_devs) { 1171 ret = !gc 1172 ? bch2_update_replicas_list(trans, &r.e, replicas_sectors) 1173 : bch2_update_replicas(c, k, &r.e, replicas_sectors, 0, true); 1174 if (unlikely(ret && gc)) { 1175 struct printbuf buf = PRINTBUF; 1176 1177 bch2_bkey_val_to_text(&buf, c, k); 1178 bch2_fs_fatal_error(c, ": no replicas entry for %s", buf.buf); 1179 printbuf_exit(&buf); 1180 } 1181 if (ret) 1182 return ret; 1183 } 1184 1185 return 0; 1186 } 1187 1188 int bch2_trigger_extent(struct btree_trans *trans, 1189 enum btree_id btree, unsigned level, 1190 struct bkey_s_c old, struct bkey_s new, 1191 enum btree_iter_update_trigger_flags flags) 1192 { 1193 struct bkey_ptrs_c new_ptrs = bch2_bkey_ptrs_c(new.s_c); 1194 struct bkey_ptrs_c old_ptrs = bch2_bkey_ptrs_c(old); 1195 unsigned new_ptrs_bytes = (void *) new_ptrs.end - (void *) new_ptrs.start; 1196 unsigned old_ptrs_bytes = (void *) old_ptrs.end - (void *) old_ptrs.start; 1197 1198 if (unlikely(flags & BTREE_TRIGGER_check_repair)) 1199 return bch2_check_fix_ptrs(trans, btree, level, new.s_c, flags); 1200 1201 /* if pointers aren't changing - nothing to do: */ 1202 if (new_ptrs_bytes == old_ptrs_bytes && 1203 !memcmp(new_ptrs.start, 1204 old_ptrs.start, 1205 new_ptrs_bytes)) 1206 return 0; 1207 1208 if (flags & BTREE_TRIGGER_transactional) { 1209 struct bch_fs *c = trans->c; 1210 int mod = (int) bch2_bkey_needs_rebalance(c, new.s_c) - 1211 (int) bch2_bkey_needs_rebalance(c, old); 1212 1213 if (mod) { 1214 int ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_rebalance_work, 1215 new.k->p, mod > 0); 1216 if (ret) 1217 return ret; 1218 } 1219 } 1220 1221 if (flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) 1222 return trigger_run_overwrite_then_insert(__trigger_extent, trans, btree, level, old, new, flags); 1223 1224 return 0; 1225 } 1226 1227 /* KEY_TYPE_reservation */ 1228 1229 static int __trigger_reservation(struct btree_trans *trans, 1230 enum btree_id btree_id, unsigned level, struct bkey_s_c k, 1231 enum btree_iter_update_trigger_flags flags) 1232 { 1233 struct bch_fs *c = trans->c; 1234 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas; 1235 s64 sectors = (s64) k.k->size * replicas; 1236 1237 if (flags & BTREE_TRIGGER_overwrite) 1238 sectors = -sectors; 1239 1240 if (flags & BTREE_TRIGGER_transactional) { 1241 int ret = bch2_replicas_deltas_realloc(trans, 0); 1242 if (ret) 1243 return ret; 1244 1245 struct replicas_delta_list *d = trans->fs_usage_deltas; 1246 replicas = min(replicas, ARRAY_SIZE(d->persistent_reserved)); 1247 1248 d->persistent_reserved[replicas - 1] += sectors; 1249 } 1250 1251 if (flags & BTREE_TRIGGER_gc) { 1252 percpu_down_read(&c->mark_lock); 1253 preempt_disable(); 1254 1255 struct bch_fs_usage *fs_usage = this_cpu_ptr(c->usage_gc); 1256 1257 replicas = min(replicas, ARRAY_SIZE(fs_usage->persistent_reserved)); 1258 fs_usage->b.reserved += sectors; 1259 fs_usage->persistent_reserved[replicas - 1] += sectors; 1260 1261 preempt_enable(); 1262 percpu_up_read(&c->mark_lock); 1263 } 1264 1265 return 0; 1266 } 1267 1268 int bch2_trigger_reservation(struct btree_trans *trans, 1269 enum btree_id btree_id, unsigned level, 1270 struct bkey_s_c old, struct bkey_s new, 1271 enum btree_iter_update_trigger_flags flags) 1272 { 1273 return trigger_run_overwrite_then_insert(__trigger_reservation, trans, btree_id, level, old, new, flags); 1274 } 1275 1276 /* Mark superblocks: */ 1277 1278 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans, 1279 struct bch_dev *ca, u64 b, 1280 enum bch_data_type type, 1281 unsigned sectors) 1282 { 1283 struct bch_fs *c = trans->c; 1284 struct btree_iter iter; 1285 int ret = 0; 1286 1287 struct bkey_i_alloc_v4 *a = 1288 bch2_trans_start_alloc_update_noupdate(trans, &iter, POS(ca->dev_idx, b)); 1289 if (IS_ERR(a)) 1290 return PTR_ERR(a); 1291 1292 if (a->v.data_type && type && a->v.data_type != type) { 1293 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, 1294 BCH_FSCK_ERR_bucket_metadata_type_mismatch, 1295 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n" 1296 "while marking %s", 1297 iter.pos.inode, iter.pos.offset, a->v.gen, 1298 bch2_data_type_str(a->v.data_type), 1299 bch2_data_type_str(type), 1300 bch2_data_type_str(type)); 1301 ret = -EIO; 1302 goto err; 1303 } 1304 1305 if (a->v.data_type != type || 1306 a->v.dirty_sectors != sectors) { 1307 a->v.data_type = type; 1308 a->v.dirty_sectors = sectors; 1309 ret = bch2_trans_update(trans, &iter, &a->k_i, 0); 1310 } 1311 err: 1312 bch2_trans_iter_exit(trans, &iter); 1313 return ret; 1314 } 1315 1316 static int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca, 1317 u64 b, enum bch_data_type data_type, unsigned sectors, 1318 enum btree_iter_update_trigger_flags flags) 1319 { 1320 int ret = 0; 1321 1322 percpu_down_read(&c->mark_lock); 1323 struct bucket *g = gc_bucket(ca, b); 1324 1325 bucket_lock(g); 1326 struct bch_alloc_v4 old = bucket_m_to_alloc(*g); 1327 1328 if (bch2_fs_inconsistent_on(g->data_type && 1329 g->data_type != data_type, c, 1330 "different types of data in same bucket: %s, %s", 1331 bch2_data_type_str(g->data_type), 1332 bch2_data_type_str(data_type))) { 1333 ret = -EIO; 1334 goto err; 1335 } 1336 1337 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c, 1338 "bucket %u:%llu gen %u data type %s sector count overflow: %u + %u > bucket size", 1339 ca->dev_idx, b, g->gen, 1340 bch2_data_type_str(g->data_type ?: data_type), 1341 g->dirty_sectors, sectors)) { 1342 ret = -EIO; 1343 goto err; 1344 } 1345 1346 g->data_type = data_type; 1347 g->dirty_sectors += sectors; 1348 struct bch_alloc_v4 new = bucket_m_to_alloc(*g); 1349 err: 1350 bucket_unlock(g); 1351 if (!ret) 1352 bch2_dev_usage_update(c, ca, &old, &new, 0, true); 1353 percpu_up_read(&c->mark_lock); 1354 return ret; 1355 } 1356 1357 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans, 1358 struct bch_dev *ca, u64 b, 1359 enum bch_data_type type, unsigned sectors, 1360 enum btree_iter_update_trigger_flags flags) 1361 { 1362 BUG_ON(type != BCH_DATA_free && 1363 type != BCH_DATA_sb && 1364 type != BCH_DATA_journal); 1365 1366 /* 1367 * Backup superblock might be past the end of our normal usable space: 1368 */ 1369 if (b >= ca->mi.nbuckets) 1370 return 0; 1371 1372 if (flags & BTREE_TRIGGER_gc) 1373 return bch2_mark_metadata_bucket(trans->c, ca, b, type, sectors, flags); 1374 else if (flags & BTREE_TRIGGER_transactional) 1375 return commit_do(trans, NULL, NULL, 0, 1376 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors)); 1377 else 1378 BUG(); 1379 } 1380 1381 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans, 1382 struct bch_dev *ca, u64 start, u64 end, 1383 enum bch_data_type type, u64 *bucket, unsigned *bucket_sectors, 1384 enum btree_iter_update_trigger_flags flags) 1385 { 1386 do { 1387 u64 b = sector_to_bucket(ca, start); 1388 unsigned sectors = 1389 min_t(u64, bucket_to_sector(ca, b + 1), end) - start; 1390 1391 if (b != *bucket && *bucket_sectors) { 1392 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket, 1393 type, *bucket_sectors, flags); 1394 if (ret) 1395 return ret; 1396 1397 *bucket_sectors = 0; 1398 } 1399 1400 *bucket = b; 1401 *bucket_sectors += sectors; 1402 start += sectors; 1403 } while (start < end); 1404 1405 return 0; 1406 } 1407 1408 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans, struct bch_dev *ca, 1409 enum btree_iter_update_trigger_flags flags) 1410 { 1411 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout; 1412 u64 bucket = 0; 1413 unsigned i, bucket_sectors = 0; 1414 int ret; 1415 1416 for (i = 0; i < layout->nr_superblocks; i++) { 1417 u64 offset = le64_to_cpu(layout->sb_offset[i]); 1418 1419 if (offset == BCH_SB_SECTOR) { 1420 ret = bch2_trans_mark_metadata_sectors(trans, ca, 1421 0, BCH_SB_SECTOR, 1422 BCH_DATA_sb, &bucket, &bucket_sectors, flags); 1423 if (ret) 1424 return ret; 1425 } 1426 1427 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset, 1428 offset + (1 << layout->sb_max_size_bits), 1429 BCH_DATA_sb, &bucket, &bucket_sectors, flags); 1430 if (ret) 1431 return ret; 1432 } 1433 1434 if (bucket_sectors) { 1435 ret = bch2_trans_mark_metadata_bucket(trans, ca, 1436 bucket, BCH_DATA_sb, bucket_sectors, flags); 1437 if (ret) 1438 return ret; 1439 } 1440 1441 for (i = 0; i < ca->journal.nr; i++) { 1442 ret = bch2_trans_mark_metadata_bucket(trans, ca, 1443 ca->journal.buckets[i], 1444 BCH_DATA_journal, ca->mi.bucket_size, flags); 1445 if (ret) 1446 return ret; 1447 } 1448 1449 return 0; 1450 } 1451 1452 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca, 1453 enum btree_iter_update_trigger_flags flags) 1454 { 1455 int ret = bch2_trans_run(c, 1456 __bch2_trans_mark_dev_sb(trans, ca, flags)); 1457 bch_err_fn(c, ret); 1458 return ret; 1459 } 1460 1461 int bch2_trans_mark_dev_sbs_flags(struct bch_fs *c, 1462 enum btree_iter_update_trigger_flags flags) 1463 { 1464 for_each_online_member(c, ca) { 1465 int ret = bch2_trans_mark_dev_sb(c, ca, flags); 1466 if (ret) { 1467 bch2_dev_put(ca); 1468 return ret; 1469 } 1470 } 1471 1472 return 0; 1473 } 1474 1475 int bch2_trans_mark_dev_sbs(struct bch_fs *c) 1476 { 1477 return bch2_trans_mark_dev_sbs_flags(c, BTREE_TRIGGER_transactional); 1478 } 1479 1480 /* Disk reservations: */ 1481 1482 #define SECTORS_CACHE 1024 1483 1484 int __bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res, 1485 u64 sectors, int flags) 1486 { 1487 struct bch_fs_pcpu *pcpu; 1488 u64 old, v, get; 1489 s64 sectors_available; 1490 int ret; 1491 1492 percpu_down_read(&c->mark_lock); 1493 preempt_disable(); 1494 pcpu = this_cpu_ptr(c->pcpu); 1495 1496 if (sectors <= pcpu->sectors_available) 1497 goto out; 1498 1499 v = atomic64_read(&c->sectors_available); 1500 do { 1501 old = v; 1502 get = min((u64) sectors + SECTORS_CACHE, old); 1503 1504 if (get < sectors) { 1505 preempt_enable(); 1506 goto recalculate; 1507 } 1508 } while ((v = atomic64_cmpxchg(&c->sectors_available, 1509 old, old - get)) != old); 1510 1511 pcpu->sectors_available += get; 1512 1513 out: 1514 pcpu->sectors_available -= sectors; 1515 this_cpu_add(*c->online_reserved, sectors); 1516 res->sectors += sectors; 1517 1518 preempt_enable(); 1519 percpu_up_read(&c->mark_lock); 1520 return 0; 1521 1522 recalculate: 1523 mutex_lock(&c->sectors_available_lock); 1524 1525 percpu_u64_set(&c->pcpu->sectors_available, 0); 1526 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free); 1527 1528 if (sectors <= sectors_available || 1529 (flags & BCH_DISK_RESERVATION_NOFAIL)) { 1530 atomic64_set(&c->sectors_available, 1531 max_t(s64, 0, sectors_available - sectors)); 1532 this_cpu_add(*c->online_reserved, sectors); 1533 res->sectors += sectors; 1534 ret = 0; 1535 } else { 1536 atomic64_set(&c->sectors_available, sectors_available); 1537 ret = -BCH_ERR_ENOSPC_disk_reservation; 1538 } 1539 1540 mutex_unlock(&c->sectors_available_lock); 1541 percpu_up_read(&c->mark_lock); 1542 1543 return ret; 1544 } 1545 1546 /* Startup/shutdown: */ 1547 1548 void bch2_buckets_nouse_free(struct bch_fs *c) 1549 { 1550 for_each_member_device(c, ca) { 1551 kvfree_rcu_mightsleep(ca->buckets_nouse); 1552 ca->buckets_nouse = NULL; 1553 } 1554 } 1555 1556 int bch2_buckets_nouse_alloc(struct bch_fs *c) 1557 { 1558 for_each_member_device(c, ca) { 1559 BUG_ON(ca->buckets_nouse); 1560 1561 ca->buckets_nouse = kvmalloc(BITS_TO_LONGS(ca->mi.nbuckets) * 1562 sizeof(unsigned long), 1563 GFP_KERNEL|__GFP_ZERO); 1564 if (!ca->buckets_nouse) { 1565 bch2_dev_put(ca); 1566 return -BCH_ERR_ENOMEM_buckets_nouse; 1567 } 1568 } 1569 1570 return 0; 1571 } 1572 1573 static void bucket_gens_free_rcu(struct rcu_head *rcu) 1574 { 1575 struct bucket_gens *buckets = 1576 container_of(rcu, struct bucket_gens, rcu); 1577 1578 kvfree(buckets); 1579 } 1580 1581 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 1582 { 1583 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL; 1584 bool resize = ca->bucket_gens != NULL; 1585 int ret; 1586 1587 BUG_ON(resize && ca->buckets_nouse); 1588 1589 if (!(bucket_gens = kvmalloc(sizeof(struct bucket_gens) + nbuckets, 1590 GFP_KERNEL|__GFP_ZERO))) { 1591 ret = -BCH_ERR_ENOMEM_bucket_gens; 1592 goto err; 1593 } 1594 1595 bucket_gens->first_bucket = ca->mi.first_bucket; 1596 bucket_gens->nbuckets = nbuckets; 1597 1598 if (resize) { 1599 down_write(&c->gc_lock); 1600 down_write(&ca->bucket_lock); 1601 percpu_down_write(&c->mark_lock); 1602 } 1603 1604 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1); 1605 1606 if (resize) { 1607 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets); 1608 1609 memcpy(bucket_gens->b, 1610 old_bucket_gens->b, 1611 n); 1612 } 1613 1614 rcu_assign_pointer(ca->bucket_gens, bucket_gens); 1615 bucket_gens = old_bucket_gens; 1616 1617 nbuckets = ca->mi.nbuckets; 1618 1619 if (resize) { 1620 percpu_up_write(&c->mark_lock); 1621 up_write(&ca->bucket_lock); 1622 up_write(&c->gc_lock); 1623 } 1624 1625 ret = 0; 1626 err: 1627 if (bucket_gens) 1628 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu); 1629 1630 return ret; 1631 } 1632 1633 void bch2_dev_buckets_free(struct bch_dev *ca) 1634 { 1635 kvfree(ca->buckets_nouse); 1636 kvfree(rcu_dereference_protected(ca->bucket_gens, 1)); 1637 1638 for (unsigned i = 0; i < ARRAY_SIZE(ca->usage); i++) 1639 free_percpu(ca->usage[i]); 1640 kfree(ca->usage_base); 1641 } 1642 1643 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca) 1644 { 1645 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL); 1646 if (!ca->usage_base) 1647 return -BCH_ERR_ENOMEM_usage_init; 1648 1649 for (unsigned i = 0; i < ARRAY_SIZE(ca->usage); i++) { 1650 ca->usage[i] = alloc_percpu(struct bch_dev_usage); 1651 if (!ca->usage[i]) 1652 return -BCH_ERR_ENOMEM_usage_init; 1653 } 1654 1655 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets); 1656 } 1657