1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Superblock section that contains a list of recovery passes to run when 5 * downgrading past a given version 6 */ 7 8 #include "bcachefs.h" 9 #include "darray.h" 10 #include "recovery_passes.h" 11 #include "sb-downgrade.h" 12 #include "sb-errors.h" 13 #include "super-io.h" 14 15 #define RECOVERY_PASS_ALL_FSCK BIT_ULL(63) 16 17 /* 18 * Upgrade, downgrade tables - run certain recovery passes, fix certain errors 19 * 20 * x(version, recovery_passes, errors...) 21 */ 22 #define UPGRADE_TABLE() \ 23 x(backpointers, \ 24 RECOVERY_PASS_ALL_FSCK) \ 25 x(inode_v3, \ 26 RECOVERY_PASS_ALL_FSCK) \ 27 x(unwritten_extents, \ 28 RECOVERY_PASS_ALL_FSCK) \ 29 x(bucket_gens, \ 30 BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)| \ 31 RECOVERY_PASS_ALL_FSCK) \ 32 x(lru_v2, \ 33 RECOVERY_PASS_ALL_FSCK) \ 34 x(fragmentation_lru, \ 35 RECOVERY_PASS_ALL_FSCK) \ 36 x(no_bps_in_alloc_keys, \ 37 RECOVERY_PASS_ALL_FSCK) \ 38 x(snapshot_trees, \ 39 RECOVERY_PASS_ALL_FSCK) \ 40 x(snapshot_skiplists, \ 41 BIT_ULL(BCH_RECOVERY_PASS_check_snapshots), \ 42 BCH_FSCK_ERR_snapshot_bad_depth, \ 43 BCH_FSCK_ERR_snapshot_bad_skiplist) \ 44 x(deleted_inodes, \ 45 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \ 46 BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list) \ 47 x(rebalance_work, \ 48 BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance)) \ 49 x(subvolume_fs_parent, \ 50 BIT_ULL(BCH_RECOVERY_PASS_check_dirents), \ 51 BCH_FSCK_ERR_subvol_fs_path_parent_wrong) \ 52 x(btree_subvolume_children, \ 53 BIT_ULL(BCH_RECOVERY_PASS_check_subvols), \ 54 BCH_FSCK_ERR_subvol_children_not_set) \ 55 x(mi_btree_bitmap, \ 56 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 57 BCH_FSCK_ERR_btree_bitmap_not_marked) \ 58 x(disk_accounting_v2, \ 59 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 60 BCH_FSCK_ERR_bkey_version_in_future, \ 61 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 62 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 63 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 64 BCH_FSCK_ERR_accounting_mismatch) \ 65 x(disk_accounting_v3, \ 66 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 67 BCH_FSCK_ERR_bkey_version_in_future, \ 68 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 69 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 70 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 71 BCH_FSCK_ERR_accounting_mismatch, \ 72 BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ 73 BCH_FSCK_ERR_accounting_key_replicas_nr_required_bad, \ 74 BCH_FSCK_ERR_accounting_key_replicas_devs_unsorted, \ 75 BCH_FSCK_ERR_accounting_key_junk_at_end) \ 76 x(disk_accounting_inum, \ 77 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 78 BCH_FSCK_ERR_accounting_mismatch) \ 79 x(rebalance_work_acct_fix, \ 80 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 81 BCH_FSCK_ERR_accounting_mismatch) \ 82 x(inode_has_child_snapshots, \ 83 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \ 84 BCH_FSCK_ERR_inode_has_child_snapshots_wrong) \ 85 x(backpointer_bucket_gen, \ 86 BIT_ULL(BCH_RECOVERY_PASS_check_extents_to_backpointers),\ 87 BCH_FSCK_ERR_backpointer_to_missing_ptr, \ 88 BCH_FSCK_ERR_ptr_to_missing_backpointer) \ 89 x(disk_accounting_big_endian, \ 90 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 91 BCH_FSCK_ERR_accounting_mismatch, \ 92 BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ 93 BCH_FSCK_ERR_accounting_key_junk_at_end) \ 94 x(directory_size, \ 95 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \ 96 BCH_FSCK_ERR_directory_size_mismatch) \ 97 98 #define DOWNGRADE_TABLE() \ 99 x(bucket_stripe_sectors, \ 100 0) \ 101 x(disk_accounting_v2, \ 102 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 103 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 104 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 105 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 106 BCH_FSCK_ERR_fs_usage_hidden_wrong, \ 107 BCH_FSCK_ERR_fs_usage_btree_wrong, \ 108 BCH_FSCK_ERR_fs_usage_data_wrong, \ 109 BCH_FSCK_ERR_fs_usage_cached_wrong, \ 110 BCH_FSCK_ERR_fs_usage_reserved_wrong, \ 111 BCH_FSCK_ERR_fs_usage_nr_inodes_wrong, \ 112 BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, \ 113 BCH_FSCK_ERR_fs_usage_replicas_wrong, \ 114 BCH_FSCK_ERR_bkey_version_in_future) \ 115 x(disk_accounting_v3, \ 116 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 117 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 118 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 119 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 120 BCH_FSCK_ERR_fs_usage_hidden_wrong, \ 121 BCH_FSCK_ERR_fs_usage_btree_wrong, \ 122 BCH_FSCK_ERR_fs_usage_data_wrong, \ 123 BCH_FSCK_ERR_fs_usage_cached_wrong, \ 124 BCH_FSCK_ERR_fs_usage_reserved_wrong, \ 125 BCH_FSCK_ERR_fs_usage_nr_inodes_wrong, \ 126 BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, \ 127 BCH_FSCK_ERR_fs_usage_replicas_wrong, \ 128 BCH_FSCK_ERR_accounting_replicas_not_marked, \ 129 BCH_FSCK_ERR_bkey_version_in_future) \ 130 x(rebalance_work_acct_fix, \ 131 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 132 BCH_FSCK_ERR_accounting_mismatch, \ 133 BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ 134 BCH_FSCK_ERR_accounting_key_junk_at_end) \ 135 x(backpointer_bucket_gen, \ 136 BIT_ULL(BCH_RECOVERY_PASS_check_extents_to_backpointers),\ 137 BCH_FSCK_ERR_backpointer_bucket_offset_wrong, \ 138 BCH_FSCK_ERR_backpointer_to_missing_ptr, \ 139 BCH_FSCK_ERR_ptr_to_missing_backpointer) \ 140 x(disk_accounting_big_endian, \ 141 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 142 BCH_FSCK_ERR_accounting_mismatch, \ 143 BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ 144 BCH_FSCK_ERR_accounting_key_junk_at_end) 145 146 struct upgrade_downgrade_entry { 147 u64 recovery_passes; 148 u16 version; 149 u16 nr_errors; 150 const u16 *errors; 151 }; 152 153 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ }; 154 UPGRADE_TABLE() 155 #undef x 156 157 static const struct upgrade_downgrade_entry upgrade_table[] = { 158 #define x(ver, passes, ...) { \ 159 .recovery_passes = passes, \ 160 .version = bcachefs_metadata_version_##ver,\ 161 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \ 162 .errors = upgrade_##ver##_errors, \ 163 }, 164 UPGRADE_TABLE() 165 #undef x 166 }; 167 168 static int have_stripes(struct bch_fs *c) 169 { 170 if (IS_ERR_OR_NULL(c->btree_roots_known[BTREE_ID_stripes].b)) 171 return 0; 172 173 return !btree_node_fake(c->btree_roots_known[BTREE_ID_stripes].b); 174 } 175 176 int bch2_sb_set_upgrade_extra(struct bch_fs *c) 177 { 178 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version; 179 unsigned new_version = c->sb.version; 180 bool write_sb = false; 181 int ret = 0; 182 183 mutex_lock(&c->sb_lock); 184 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 185 186 if (old_version < bcachefs_metadata_version_bucket_stripe_sectors && 187 new_version >= bcachefs_metadata_version_bucket_stripe_sectors && 188 (ret = have_stripes(c) > 0)) { 189 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required); 190 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); 191 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_sectors_wrong, ext->errors_silent); 192 write_sb = true; 193 } 194 195 if (write_sb) 196 bch2_write_super(c); 197 mutex_unlock(&c->sb_lock); 198 199 return ret < 0 ? ret : 0; 200 } 201 202 void bch2_sb_set_upgrade(struct bch_fs *c, 203 unsigned old_version, 204 unsigned new_version) 205 { 206 lockdep_assert_held(&c->sb_lock); 207 208 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 209 210 for (const struct upgrade_downgrade_entry *i = upgrade_table; 211 i < upgrade_table + ARRAY_SIZE(upgrade_table); 212 i++) 213 if (i->version > old_version && i->version <= new_version) { 214 u64 passes = i->recovery_passes; 215 216 if (passes & RECOVERY_PASS_ALL_FSCK) 217 passes |= bch2_fsck_recovery_passes(); 218 passes &= ~RECOVERY_PASS_ALL_FSCK; 219 220 ext->recovery_passes_required[0] |= 221 cpu_to_le64(bch2_recovery_passes_to_stable(passes)); 222 223 for (const u16 *e = i->errors; e < i->errors + i->nr_errors; e++) 224 __set_bit_le64(*e, ext->errors_silent); 225 } 226 } 227 228 #define x(ver, passes, ...) static const u16 downgrade_##ver##_errors[] = { __VA_ARGS__ }; 229 DOWNGRADE_TABLE() 230 #undef x 231 232 static const struct upgrade_downgrade_entry downgrade_table[] = { 233 #define x(ver, passes, ...) { \ 234 .recovery_passes = passes, \ 235 .version = bcachefs_metadata_version_##ver,\ 236 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \ 237 .errors = downgrade_##ver##_errors, \ 238 }, 239 DOWNGRADE_TABLE() 240 #undef x 241 }; 242 243 static int downgrade_table_extra(struct bch_fs *c, darray_char *table) 244 { 245 struct bch_sb_field_downgrade_entry *dst = (void *) &darray_top(*table); 246 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors); 247 int ret = 0; 248 249 unsigned nr_errors = le16_to_cpu(dst->nr_errors); 250 251 switch (le16_to_cpu(dst->version)) { 252 case bcachefs_metadata_version_bucket_stripe_sectors: 253 if (have_stripes(c)) { 254 bytes += sizeof(dst->errors[0]) * 2; 255 256 ret = darray_make_room(table, bytes); 257 if (ret) 258 return ret; 259 260 /* open coded __set_bit_le64, as dst is packed and 261 * dst->recovery_passes is misaligned */ 262 unsigned b = BCH_RECOVERY_PASS_STABLE_check_allocations; 263 dst->recovery_passes[b / 64] |= cpu_to_le64(BIT_ULL(b % 64)); 264 265 dst->errors[nr_errors++] = cpu_to_le16(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong); 266 } 267 break; 268 } 269 270 dst->nr_errors = cpu_to_le16(nr_errors); 271 return ret; 272 } 273 274 static inline const struct bch_sb_field_downgrade_entry * 275 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e) 276 { 277 return (void *) &e->errors[le16_to_cpu(e->nr_errors)]; 278 } 279 280 #define for_each_downgrade_entry(_d, _i) \ 281 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \ 282 (void *) _i < vstruct_end(&(_d)->field) && \ 283 (void *) &_i->errors[0] <= vstruct_end(&(_d)->field) && \ 284 (void *) downgrade_entry_next_c(_i) <= vstruct_end(&(_d)->field); \ 285 _i = downgrade_entry_next_c(_i)) 286 287 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f, 288 enum bch_validate_flags flags, struct printbuf *err) 289 { 290 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 291 292 for (const struct bch_sb_field_downgrade_entry *i = e->entries; 293 (void *) i < vstruct_end(&e->field); 294 i = downgrade_entry_next_c(i)) { 295 /* 296 * Careful: sb_field_downgrade_entry is only 2 byte aligned, but 297 * section sizes are 8 byte aligned - an empty entry spanning 298 * the end of the section is allowed (and ignored): 299 */ 300 if ((void *) &i->errors[0] > vstruct_end(&e->field)) 301 break; 302 303 if (flags & BCH_VALIDATE_write && 304 (void *) downgrade_entry_next_c(i) > vstruct_end(&e->field)) { 305 prt_printf(err, "downgrade entry overruns end of superblock section"); 306 return -BCH_ERR_invalid_sb_downgrade; 307 } 308 309 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) != 310 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) { 311 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)", 312 BCH_VERSION_MAJOR(le16_to_cpu(i->version)), 313 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))); 314 return -BCH_ERR_invalid_sb_downgrade; 315 } 316 } 317 318 return 0; 319 } 320 321 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb, 322 struct bch_sb_field *f) 323 { 324 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 325 326 if (out->nr_tabstops <= 1) 327 printbuf_tabstop_push(out, 16); 328 329 for_each_downgrade_entry(e, i) { 330 prt_str(out, "version:\t"); 331 bch2_version_to_text(out, le16_to_cpu(i->version)); 332 prt_newline(out); 333 334 prt_str(out, "recovery passes:\t"); 335 prt_bitflags(out, bch2_recovery_passes, 336 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0]))); 337 prt_newline(out); 338 339 prt_str(out, "errors:\t"); 340 bool first = true; 341 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 342 if (!first) 343 prt_char(out, ','); 344 first = false; 345 bch2_sb_error_id_to_text(out, le16_to_cpu(i->errors[j])); 346 } 347 prt_newline(out); 348 } 349 } 350 351 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = { 352 .validate = bch2_sb_downgrade_validate, 353 .to_text = bch2_sb_downgrade_to_text, 354 }; 355 356 int bch2_sb_downgrade_update(struct bch_fs *c) 357 { 358 if (!test_bit(BCH_FS_btree_running, &c->flags)) 359 return 0; 360 361 darray_char table = {}; 362 int ret = 0; 363 364 for (const struct upgrade_downgrade_entry *src = downgrade_table; 365 src < downgrade_table + ARRAY_SIZE(downgrade_table); 366 src++) { 367 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version))) 368 continue; 369 370 struct bch_sb_field_downgrade_entry *dst; 371 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors; 372 373 ret = darray_make_room(&table, bytes); 374 if (ret) 375 goto out; 376 377 dst = (void *) &darray_top(table); 378 dst->version = cpu_to_le16(src->version); 379 dst->recovery_passes[0] = cpu_to_le64(bch2_recovery_passes_to_stable(src->recovery_passes)); 380 dst->recovery_passes[1] = 0; 381 dst->nr_errors = cpu_to_le16(src->nr_errors); 382 for (unsigned i = 0; i < src->nr_errors; i++) 383 dst->errors[i] = cpu_to_le16(src->errors[i]); 384 385 ret = downgrade_table_extra(c, &table); 386 if (ret) 387 goto out; 388 389 if (!dst->recovery_passes[0] && 390 !dst->recovery_passes[1] && 391 !dst->nr_errors) 392 continue; 393 394 table.nr += sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors); 395 } 396 397 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 398 399 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64)); 400 401 if (d && le32_to_cpu(d->field.u64s) > sb_u64s) 402 goto out; 403 404 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s); 405 if (!d) { 406 ret = -BCH_ERR_ENOSPC_sb_downgrade; 407 goto out; 408 } 409 410 memcpy(d->entries, table.data, table.nr); 411 memset_u64s_tail(d->entries, 0, table.nr); 412 out: 413 darray_exit(&table); 414 return ret; 415 } 416 417 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor) 418 { 419 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 420 if (!d) 421 return; 422 423 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 424 425 for_each_downgrade_entry(d, i) { 426 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version)); 427 if (new_minor < minor && minor <= old_minor) { 428 ext->recovery_passes_required[0] |= i->recovery_passes[0]; 429 ext->recovery_passes_required[1] |= i->recovery_passes[1]; 430 431 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 432 unsigned e = le16_to_cpu(i->errors[j]); 433 if (e < BCH_FSCK_ERR_MAX) 434 __set_bit(e, c->sb.errors_silent); 435 if (e < sizeof(ext->errors_silent) * 8) 436 __set_bit_le64(e, ext->errors_silent); 437 } 438 } 439 } 440 } 441