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 83 #define DOWNGRADE_TABLE() \ 84 x(bucket_stripe_sectors, \ 85 0) \ 86 x(disk_accounting_v2, \ 87 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 88 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 89 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 90 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 91 BCH_FSCK_ERR_fs_usage_hidden_wrong, \ 92 BCH_FSCK_ERR_fs_usage_btree_wrong, \ 93 BCH_FSCK_ERR_fs_usage_data_wrong, \ 94 BCH_FSCK_ERR_fs_usage_cached_wrong, \ 95 BCH_FSCK_ERR_fs_usage_reserved_wrong, \ 96 BCH_FSCK_ERR_fs_usage_nr_inodes_wrong, \ 97 BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, \ 98 BCH_FSCK_ERR_fs_usage_replicas_wrong, \ 99 BCH_FSCK_ERR_bkey_version_in_future) \ 100 x(disk_accounting_v3, \ 101 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 102 BCH_FSCK_ERR_dev_usage_buckets_wrong, \ 103 BCH_FSCK_ERR_dev_usage_sectors_wrong, \ 104 BCH_FSCK_ERR_dev_usage_fragmented_wrong, \ 105 BCH_FSCK_ERR_fs_usage_hidden_wrong, \ 106 BCH_FSCK_ERR_fs_usage_btree_wrong, \ 107 BCH_FSCK_ERR_fs_usage_data_wrong, \ 108 BCH_FSCK_ERR_fs_usage_cached_wrong, \ 109 BCH_FSCK_ERR_fs_usage_reserved_wrong, \ 110 BCH_FSCK_ERR_fs_usage_nr_inodes_wrong, \ 111 BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, \ 112 BCH_FSCK_ERR_fs_usage_replicas_wrong, \ 113 BCH_FSCK_ERR_accounting_replicas_not_marked, \ 114 BCH_FSCK_ERR_bkey_version_in_future) \ 115 x(rebalance_work_acct_fix, \ 116 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ 117 BCH_FSCK_ERR_accounting_mismatch) 118 119 struct upgrade_downgrade_entry { 120 u64 recovery_passes; 121 u16 version; 122 u16 nr_errors; 123 const u16 *errors; 124 }; 125 126 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ }; 127 UPGRADE_TABLE() 128 #undef x 129 130 static const struct upgrade_downgrade_entry upgrade_table[] = { 131 #define x(ver, passes, ...) { \ 132 .recovery_passes = passes, \ 133 .version = bcachefs_metadata_version_##ver,\ 134 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \ 135 .errors = upgrade_##ver##_errors, \ 136 }, 137 UPGRADE_TABLE() 138 #undef x 139 }; 140 141 static int have_stripes(struct bch_fs *c) 142 { 143 return !btree_node_fake(c->btree_roots_known[BTREE_ID_stripes].b); 144 } 145 146 int bch2_sb_set_upgrade_extra(struct bch_fs *c) 147 { 148 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version; 149 unsigned new_version = c->sb.version; 150 bool write_sb = false; 151 int ret = 0; 152 153 mutex_lock(&c->sb_lock); 154 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 155 156 if (old_version < bcachefs_metadata_version_bucket_stripe_sectors && 157 new_version >= bcachefs_metadata_version_bucket_stripe_sectors && 158 (ret = have_stripes(c) > 0)) { 159 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required); 160 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); 161 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_sectors_wrong, ext->errors_silent); 162 write_sb = true; 163 } 164 165 if (write_sb) 166 bch2_write_super(c); 167 mutex_unlock(&c->sb_lock); 168 169 return ret < 0 ? ret : 0; 170 } 171 172 void bch2_sb_set_upgrade(struct bch_fs *c, 173 unsigned old_version, 174 unsigned new_version) 175 { 176 lockdep_assert_held(&c->sb_lock); 177 178 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 179 180 for (const struct upgrade_downgrade_entry *i = upgrade_table; 181 i < upgrade_table + ARRAY_SIZE(upgrade_table); 182 i++) 183 if (i->version > old_version && i->version <= new_version) { 184 u64 passes = i->recovery_passes; 185 186 if (passes & RECOVERY_PASS_ALL_FSCK) 187 passes |= bch2_fsck_recovery_passes(); 188 passes &= ~RECOVERY_PASS_ALL_FSCK; 189 190 ext->recovery_passes_required[0] |= 191 cpu_to_le64(bch2_recovery_passes_to_stable(passes)); 192 193 for (const u16 *e = i->errors; e < i->errors + i->nr_errors; e++) 194 __set_bit_le64(*e, ext->errors_silent); 195 } 196 } 197 198 #define x(ver, passes, ...) static const u16 downgrade_##ver##_errors[] = { __VA_ARGS__ }; 199 DOWNGRADE_TABLE() 200 #undef x 201 202 static const struct upgrade_downgrade_entry downgrade_table[] = { 203 #define x(ver, passes, ...) { \ 204 .recovery_passes = passes, \ 205 .version = bcachefs_metadata_version_##ver,\ 206 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \ 207 .errors = downgrade_##ver##_errors, \ 208 }, 209 DOWNGRADE_TABLE() 210 #undef x 211 }; 212 213 static int downgrade_table_extra(struct bch_fs *c, darray_char *table) 214 { 215 struct bch_sb_field_downgrade_entry *dst = (void *) &darray_top(*table); 216 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors); 217 int ret = 0; 218 219 unsigned nr_errors = le16_to_cpu(dst->nr_errors); 220 221 switch (le16_to_cpu(dst->version)) { 222 case bcachefs_metadata_version_bucket_stripe_sectors: 223 if (have_stripes(c)) { 224 bytes += sizeof(dst->errors[0]) * 2; 225 226 ret = darray_make_room(table, bytes); 227 if (ret) 228 return ret; 229 230 /* open coded __set_bit_le64, as dst is packed and 231 * dst->recovery_passes is misaligned */ 232 unsigned b = BCH_RECOVERY_PASS_STABLE_check_allocations; 233 dst->recovery_passes[b / 64] |= cpu_to_le64(BIT_ULL(b % 64)); 234 235 dst->errors[nr_errors++] = cpu_to_le16(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong); 236 } 237 break; 238 } 239 240 dst->nr_errors = cpu_to_le16(nr_errors); 241 return ret; 242 } 243 244 static inline const struct bch_sb_field_downgrade_entry * 245 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e) 246 { 247 return (void *) &e->errors[le16_to_cpu(e->nr_errors)]; 248 } 249 250 #define for_each_downgrade_entry(_d, _i) \ 251 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \ 252 (void *) _i < vstruct_end(&(_d)->field) && \ 253 (void *) &_i->errors[0] <= vstruct_end(&(_d)->field) && \ 254 (void *) downgrade_entry_next_c(_i) <= vstruct_end(&(_d)->field); \ 255 _i = downgrade_entry_next_c(_i)) 256 257 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f, 258 enum bch_validate_flags flags, struct printbuf *err) 259 { 260 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 261 262 for (const struct bch_sb_field_downgrade_entry *i = e->entries; 263 (void *) i < vstruct_end(&e->field); 264 i = downgrade_entry_next_c(i)) { 265 /* 266 * Careful: sb_field_downgrade_entry is only 2 byte aligned, but 267 * section sizes are 8 byte aligned - an empty entry spanning 268 * the end of the section is allowed (and ignored): 269 */ 270 if ((void *) &i->errors[0] > vstruct_end(&e->field)) 271 break; 272 273 if (flags & BCH_VALIDATE_write && 274 (void *) downgrade_entry_next_c(i) > vstruct_end(&e->field)) { 275 prt_printf(err, "downgrade entry overruns end of superblock section"); 276 return -BCH_ERR_invalid_sb_downgrade; 277 } 278 279 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) != 280 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) { 281 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)", 282 BCH_VERSION_MAJOR(le16_to_cpu(i->version)), 283 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))); 284 return -BCH_ERR_invalid_sb_downgrade; 285 } 286 } 287 288 return 0; 289 } 290 291 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb, 292 struct bch_sb_field *f) 293 { 294 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 295 296 if (out->nr_tabstops <= 1) 297 printbuf_tabstop_push(out, 16); 298 299 for_each_downgrade_entry(e, i) { 300 prt_str(out, "version:\t"); 301 bch2_version_to_text(out, le16_to_cpu(i->version)); 302 prt_newline(out); 303 304 prt_str(out, "recovery passes:\t"); 305 prt_bitflags(out, bch2_recovery_passes, 306 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0]))); 307 prt_newline(out); 308 309 prt_str(out, "errors:\t"); 310 bool first = true; 311 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 312 if (!first) 313 prt_char(out, ','); 314 first = false; 315 unsigned e = le16_to_cpu(i->errors[j]); 316 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)"); 317 } 318 prt_newline(out); 319 } 320 } 321 322 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = { 323 .validate = bch2_sb_downgrade_validate, 324 .to_text = bch2_sb_downgrade_to_text, 325 }; 326 327 int bch2_sb_downgrade_update(struct bch_fs *c) 328 { 329 if (!test_bit(BCH_FS_btree_running, &c->flags)) 330 return 0; 331 332 darray_char table = {}; 333 int ret = 0; 334 335 for (const struct upgrade_downgrade_entry *src = downgrade_table; 336 src < downgrade_table + ARRAY_SIZE(downgrade_table); 337 src++) { 338 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version))) 339 continue; 340 341 struct bch_sb_field_downgrade_entry *dst; 342 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors; 343 344 ret = darray_make_room(&table, bytes); 345 if (ret) 346 goto out; 347 348 dst = (void *) &darray_top(table); 349 dst->version = cpu_to_le16(src->version); 350 dst->recovery_passes[0] = cpu_to_le64(bch2_recovery_passes_to_stable(src->recovery_passes)); 351 dst->recovery_passes[1] = 0; 352 dst->nr_errors = cpu_to_le16(src->nr_errors); 353 for (unsigned i = 0; i < src->nr_errors; i++) 354 dst->errors[i] = cpu_to_le16(src->errors[i]); 355 356 downgrade_table_extra(c, &table); 357 358 if (!dst->recovery_passes[0] && 359 !dst->recovery_passes[1] && 360 !dst->nr_errors) 361 continue; 362 363 table.nr += sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors); 364 } 365 366 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 367 368 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64)); 369 370 if (d && le32_to_cpu(d->field.u64s) > sb_u64s) 371 goto out; 372 373 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s); 374 if (!d) { 375 ret = -BCH_ERR_ENOSPC_sb_downgrade; 376 goto out; 377 } 378 379 memcpy(d->entries, table.data, table.nr); 380 memset_u64s_tail(d->entries, 0, table.nr); 381 out: 382 darray_exit(&table); 383 return ret; 384 } 385 386 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor) 387 { 388 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 389 if (!d) 390 return; 391 392 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 393 394 for_each_downgrade_entry(d, i) { 395 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version)); 396 if (new_minor < minor && minor <= old_minor) { 397 ext->recovery_passes_required[0] |= i->recovery_passes[0]; 398 ext->recovery_passes_required[1] |= i->recovery_passes[1]; 399 400 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 401 unsigned e = le16_to_cpu(i->errors[j]); 402 if (e < BCH_SB_ERR_MAX) 403 __set_bit(e, c->sb.errors_silent); 404 if (e < sizeof(ext->errors_silent) * 8) 405 __set_bit_le64(e, ext->errors_silent); 406 } 407 } 408 } 409 } 410