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 59 #define DOWNGRADE_TABLE() 60 61 struct upgrade_downgrade_entry { 62 u64 recovery_passes; 63 u16 version; 64 u16 nr_errors; 65 const u16 *errors; 66 }; 67 68 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ }; 69 UPGRADE_TABLE() 70 #undef x 71 72 static const struct upgrade_downgrade_entry upgrade_table[] = { 73 #define x(ver, passes, ...) { \ 74 .recovery_passes = passes, \ 75 .version = bcachefs_metadata_version_##ver,\ 76 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \ 77 .errors = upgrade_##ver##_errors, \ 78 }, 79 UPGRADE_TABLE() 80 #undef x 81 }; 82 83 void bch2_sb_set_upgrade(struct bch_fs *c, 84 unsigned old_version, 85 unsigned new_version) 86 { 87 lockdep_assert_held(&c->sb_lock); 88 89 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 90 91 for (const struct upgrade_downgrade_entry *i = upgrade_table; 92 i < upgrade_table + ARRAY_SIZE(upgrade_table); 93 i++) 94 if (i->version > old_version && i->version <= new_version) { 95 u64 passes = i->recovery_passes; 96 97 if (passes & RECOVERY_PASS_ALL_FSCK) 98 passes |= bch2_fsck_recovery_passes(); 99 passes &= ~RECOVERY_PASS_ALL_FSCK; 100 101 ext->recovery_passes_required[0] |= 102 cpu_to_le64(bch2_recovery_passes_to_stable(passes)); 103 104 for (const u16 *e = i->errors; 105 e < i->errors + i->nr_errors; 106 e++) { 107 __set_bit(*e, c->sb.errors_silent); 108 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64)); 109 } 110 } 111 } 112 113 #define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ }; 114 DOWNGRADE_TABLE() 115 #undef x 116 117 static const struct upgrade_downgrade_entry downgrade_table[] = { 118 #define x(ver, passes, ...) { \ 119 .recovery_passes = passes, \ 120 .version = bcachefs_metadata_version_##ver,\ 121 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \ 122 .errors = downgrade_##ver##_errors, \ 123 }, 124 DOWNGRADE_TABLE() 125 #undef x 126 }; 127 128 static inline const struct bch_sb_field_downgrade_entry * 129 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e) 130 { 131 return (void *) &e->errors[le16_to_cpu(e->nr_errors)]; 132 } 133 134 #define for_each_downgrade_entry(_d, _i) \ 135 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \ 136 (void *) _i < vstruct_end(&(_d)->field) && \ 137 (void *) &_i->errors[0] <= vstruct_end(&(_d)->field) && \ 138 (void *) downgrade_entry_next_c(_i) <= vstruct_end(&(_d)->field); \ 139 _i = downgrade_entry_next_c(_i)) 140 141 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f, 142 enum bch_validate_flags flags, struct printbuf *err) 143 { 144 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 145 146 for (const struct bch_sb_field_downgrade_entry *i = e->entries; 147 (void *) i < vstruct_end(&e->field); 148 i = downgrade_entry_next_c(i)) { 149 if (flags & BCH_VALIDATE_write && 150 ((void *) &i->errors[0] > vstruct_end(&e->field) || 151 (void *) downgrade_entry_next_c(i) > vstruct_end(&e->field))) { 152 prt_printf(err, "downgrade entry overruns end of superblock section)"); 153 return -BCH_ERR_invalid_sb_downgrade; 154 } 155 156 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) != 157 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) { 158 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)", 159 BCH_VERSION_MAJOR(le16_to_cpu(i->version)), 160 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))); 161 return -BCH_ERR_invalid_sb_downgrade; 162 } 163 } 164 165 return 0; 166 } 167 168 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb, 169 struct bch_sb_field *f) 170 { 171 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade); 172 173 if (out->nr_tabstops <= 1) 174 printbuf_tabstop_push(out, 16); 175 176 for_each_downgrade_entry(e, i) { 177 prt_str(out, "version:\t"); 178 bch2_version_to_text(out, le16_to_cpu(i->version)); 179 prt_newline(out); 180 181 prt_str(out, "recovery passes:\t"); 182 prt_bitflags(out, bch2_recovery_passes, 183 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0]))); 184 prt_newline(out); 185 186 prt_str(out, "errors:\t"); 187 bool first = true; 188 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 189 if (!first) 190 prt_char(out, ','); 191 first = false; 192 unsigned e = le16_to_cpu(i->errors[j]); 193 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)"); 194 } 195 prt_newline(out); 196 } 197 } 198 199 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = { 200 .validate = bch2_sb_downgrade_validate, 201 .to_text = bch2_sb_downgrade_to_text, 202 }; 203 204 int bch2_sb_downgrade_update(struct bch_fs *c) 205 { 206 darray_char table = {}; 207 int ret = 0; 208 209 for (const struct upgrade_downgrade_entry *src = downgrade_table; 210 src < downgrade_table + ARRAY_SIZE(downgrade_table); 211 src++) { 212 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version))) 213 continue; 214 215 struct bch_sb_field_downgrade_entry *dst; 216 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors; 217 218 ret = darray_make_room(&table, bytes); 219 if (ret) 220 goto out; 221 222 dst = (void *) &darray_top(table); 223 dst->version = cpu_to_le16(src->version); 224 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes); 225 dst->recovery_passes[1] = 0; 226 dst->nr_errors = cpu_to_le16(src->nr_errors); 227 for (unsigned i = 0; i < src->nr_errors; i++) 228 dst->errors[i] = cpu_to_le16(src->errors[i]); 229 230 table.nr += bytes; 231 } 232 233 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 234 235 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64)); 236 237 if (d && le32_to_cpu(d->field.u64s) > sb_u64s) 238 goto out; 239 240 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s); 241 if (!d) { 242 ret = -BCH_ERR_ENOSPC_sb_downgrade; 243 goto out; 244 } 245 246 memcpy(d->entries, table.data, table.nr); 247 memset_u64s_tail(d->entries, 0, table.nr); 248 out: 249 darray_exit(&table); 250 return ret; 251 } 252 253 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor) 254 { 255 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade); 256 if (!d) 257 return; 258 259 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 260 261 for_each_downgrade_entry(d, i) { 262 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version)); 263 if (new_minor < minor && minor <= old_minor) { 264 ext->recovery_passes_required[0] |= i->recovery_passes[0]; 265 ext->recovery_passes_required[1] |= i->recovery_passes[1]; 266 267 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) { 268 unsigned e = le16_to_cpu(i->errors[j]); 269 if (e < BCH_SB_ERR_MAX) 270 __set_bit(e, c->sb.errors_silent); 271 if (e < sizeof(ext->errors_silent) * 8) 272 __set_bit_le64(e, ext->errors_silent); 273 } 274 } 275 } 276 } 277