1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "btree_update_interior.h" 5 #include "buckets.h" 6 #include "error.h" 7 #include "journal_io.h" 8 #include "replicas.h" 9 #include "sb-clean.h" 10 #include "super-io.h" 11 12 /* 13 * BCH_SB_FIELD_clean: 14 * 15 * Btree roots, and a few other things, are recovered from the journal after an 16 * unclean shutdown - but after a clean shutdown, to avoid having to read the 17 * journal, we can store them in the superblock. 18 * 19 * bch_sb_field_clean simply contains a list of journal entries, stored exactly 20 * as they would be in the journal: 21 */ 22 23 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean, 24 int write) 25 { 26 struct bkey_validate_context from = { 27 .flags = write, 28 .from = BKEY_VALIDATE_superblock, 29 }; 30 struct jset_entry *entry; 31 int ret; 32 33 for (entry = clean->start; 34 entry < (struct jset_entry *) vstruct_end(&clean->field); 35 entry = vstruct_next(entry)) { 36 if (vstruct_end(entry) > vstruct_end(&clean->field)) { 37 bch_err(c, "journal entry (u64s %u) overran end of superblock clean section (u64s %u) by %zu", 38 le16_to_cpu(entry->u64s), le32_to_cpu(clean->field.u64s), 39 (u64 *) vstruct_end(entry) - (u64 *) vstruct_end(&clean->field)); 40 bch2_sb_error_count(c, BCH_FSCK_ERR_sb_clean_entry_overrun); 41 return -BCH_ERR_fsck_repair_unimplemented; 42 } 43 44 ret = bch2_journal_entry_validate(c, NULL, entry, 45 le16_to_cpu(c->disk_sb.sb->version), 46 BCH_SB_BIG_ENDIAN(c->disk_sb.sb), 47 from); 48 if (ret) 49 return ret; 50 } 51 52 return 0; 53 } 54 55 static struct bkey_i *btree_root_find(struct bch_fs *c, 56 struct bch_sb_field_clean *clean, 57 struct jset *j, 58 enum btree_id id, unsigned *level) 59 { 60 struct bkey_i *k; 61 struct jset_entry *entry, *start, *end; 62 63 if (clean) { 64 start = clean->start; 65 end = vstruct_end(&clean->field); 66 } else { 67 start = j->start; 68 end = vstruct_last(j); 69 } 70 71 for (entry = start; entry < end; entry = vstruct_next(entry)) 72 if (entry->type == BCH_JSET_ENTRY_btree_root && 73 entry->btree_id == id) 74 goto found; 75 76 return NULL; 77 found: 78 if (!entry->u64s) 79 return ERR_PTR(-EINVAL); 80 81 k = entry->start; 82 *level = entry->level; 83 return k; 84 } 85 86 int bch2_verify_superblock_clean(struct bch_fs *c, 87 struct bch_sb_field_clean **cleanp, 88 struct jset *j) 89 { 90 unsigned i; 91 struct bch_sb_field_clean *clean = *cleanp; 92 struct printbuf buf1 = PRINTBUF; 93 struct printbuf buf2 = PRINTBUF; 94 int ret = 0; 95 96 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c, 97 sb_clean_journal_seq_mismatch, 98 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown", 99 le64_to_cpu(clean->journal_seq), 100 le64_to_cpu(j->seq))) { 101 kfree(clean); 102 *cleanp = NULL; 103 return 0; 104 } 105 106 for (i = 0; i < BTREE_ID_NR; i++) { 107 struct bkey_i *k1, *k2; 108 unsigned l1 = 0, l2 = 0; 109 110 k1 = btree_root_find(c, clean, NULL, i, &l1); 111 k2 = btree_root_find(c, NULL, j, i, &l2); 112 113 if (!k1 && !k2) 114 continue; 115 116 printbuf_reset(&buf1); 117 printbuf_reset(&buf2); 118 119 if (k1) 120 bch2_bkey_val_to_text(&buf1, c, bkey_i_to_s_c(k1)); 121 else 122 prt_printf(&buf1, "(none)"); 123 124 if (k2) 125 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(k2)); 126 else 127 prt_printf(&buf2, "(none)"); 128 129 mustfix_fsck_err_on(!k1 || !k2 || 130 IS_ERR(k1) || 131 IS_ERR(k2) || 132 k1->k.u64s != k2->k.u64s || 133 memcmp(k1, k2, bkey_bytes(&k1->k)) || 134 l1 != l2, c, 135 sb_clean_btree_root_mismatch, 136 "superblock btree root %u doesn't match journal after clean shutdown\n" 137 "sb: l=%u %s\n" 138 "journal: l=%u %s\n", i, 139 l1, buf1.buf, 140 l2, buf2.buf); 141 } 142 fsck_err: 143 printbuf_exit(&buf2); 144 printbuf_exit(&buf1); 145 return ret; 146 } 147 148 struct bch_sb_field_clean *bch2_read_superblock_clean(struct bch_fs *c) 149 { 150 struct bch_sb_field_clean *clean, *sb_clean; 151 int ret; 152 153 mutex_lock(&c->sb_lock); 154 sb_clean = bch2_sb_field_get(c->disk_sb.sb, clean); 155 156 if (fsck_err_on(!sb_clean, c, 157 sb_clean_missing, 158 "superblock marked clean but clean section not present")) { 159 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 160 c->sb.clean = false; 161 mutex_unlock(&c->sb_lock); 162 return ERR_PTR(-BCH_ERR_invalid_sb_clean); 163 } 164 165 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field), 166 GFP_KERNEL); 167 if (!clean) { 168 mutex_unlock(&c->sb_lock); 169 return ERR_PTR(-BCH_ERR_ENOMEM_read_superblock_clean); 170 } 171 172 ret = bch2_sb_clean_validate_late(c, clean, READ); 173 if (ret) { 174 kfree(clean); 175 mutex_unlock(&c->sb_lock); 176 return ERR_PTR(ret); 177 } 178 179 mutex_unlock(&c->sb_lock); 180 181 return clean; 182 fsck_err: 183 mutex_unlock(&c->sb_lock); 184 return ERR_PTR(ret); 185 } 186 187 void bch2_journal_super_entries_add_common(struct bch_fs *c, 188 struct jset_entry **end, 189 u64 journal_seq) 190 { 191 { 192 struct jset_entry_usage *u = 193 container_of(jset_entry_init(end, sizeof(*u)), 194 struct jset_entry_usage, entry); 195 196 u->entry.type = BCH_JSET_ENTRY_usage; 197 u->entry.btree_id = BCH_FS_USAGE_key_version; 198 u->v = cpu_to_le64(atomic64_read(&c->key_version)); 199 } 200 201 for (unsigned i = 0; i < 2; i++) { 202 struct jset_entry_clock *clock = 203 container_of(jset_entry_init(end, sizeof(*clock)), 204 struct jset_entry_clock, entry); 205 206 clock->entry.type = BCH_JSET_ENTRY_clock; 207 clock->rw = i; 208 clock->time = cpu_to_le64(atomic64_read(&c->io_clock[i].now)); 209 } 210 } 211 212 static int bch2_sb_clean_validate(struct bch_sb *sb, struct bch_sb_field *f, 213 enum bch_validate_flags flags, struct printbuf *err) 214 { 215 struct bch_sb_field_clean *clean = field_to_type(f, clean); 216 217 if (vstruct_bytes(&clean->field) < sizeof(*clean)) { 218 prt_printf(err, "wrong size (got %zu should be %zu)", 219 vstruct_bytes(&clean->field), sizeof(*clean)); 220 return -BCH_ERR_invalid_sb_clean; 221 } 222 223 for (struct jset_entry *entry = clean->start; 224 entry != vstruct_end(&clean->field); 225 entry = vstruct_next(entry)) { 226 if ((void *) vstruct_next(entry) > vstruct_end(&clean->field)) { 227 prt_str(err, "entry type "); 228 bch2_prt_jset_entry_type(err, entry->type); 229 prt_str(err, " overruns end of section"); 230 return -BCH_ERR_invalid_sb_clean; 231 } 232 } 233 234 return 0; 235 } 236 237 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb, 238 struct bch_sb_field *f) 239 { 240 struct bch_sb_field_clean *clean = field_to_type(f, clean); 241 struct jset_entry *entry; 242 243 prt_printf(out, "flags: %x\n", le32_to_cpu(clean->flags)); 244 prt_printf(out, "journal_seq: %llu\n", le64_to_cpu(clean->journal_seq)); 245 246 for (entry = clean->start; 247 entry != vstruct_end(&clean->field); 248 entry = vstruct_next(entry)) { 249 if ((void *) vstruct_next(entry) > vstruct_end(&clean->field)) 250 break; 251 252 if (entry->type == BCH_JSET_ENTRY_btree_keys && 253 !entry->u64s) 254 continue; 255 256 bch2_journal_entry_to_text(out, NULL, entry); 257 prt_newline(out); 258 } 259 } 260 261 const struct bch_sb_field_ops bch_sb_field_ops_clean = { 262 .validate = bch2_sb_clean_validate, 263 .to_text = bch2_sb_clean_to_text, 264 }; 265 266 int bch2_fs_mark_dirty(struct bch_fs *c) 267 { 268 int ret; 269 270 /* 271 * Unconditionally write superblock, to verify it hasn't changed before 272 * we go rw: 273 */ 274 275 mutex_lock(&c->sb_lock); 276 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 277 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS); 278 279 ret = bch2_write_super(c); 280 mutex_unlock(&c->sb_lock); 281 282 return ret; 283 } 284 285 void bch2_fs_mark_clean(struct bch_fs *c) 286 { 287 struct bch_sb_field_clean *sb_clean; 288 struct jset_entry *entry; 289 unsigned u64s; 290 int ret; 291 292 mutex_lock(&c->sb_lock); 293 if (BCH_SB_CLEAN(c->disk_sb.sb)) 294 goto out; 295 296 SET_BCH_SB_CLEAN(c->disk_sb.sb, true); 297 298 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info); 299 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata); 300 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates)); 301 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled)); 302 303 u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved; 304 305 sb_clean = bch2_sb_field_resize(&c->disk_sb, clean, u64s); 306 if (!sb_clean) { 307 bch_err(c, "error resizing superblock while setting filesystem clean"); 308 goto out; 309 } 310 311 sb_clean->flags = 0; 312 sb_clean->journal_seq = cpu_to_le64(atomic64_read(&c->journal.seq)); 313 314 /* Trying to catch outstanding bug: */ 315 BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX); 316 317 entry = sb_clean->start; 318 bch2_journal_super_entries_add_common(c, &entry, 0); 319 entry = bch2_btree_roots_to_journal_entries(c, entry, 0); 320 BUG_ON((void *) entry > vstruct_end(&sb_clean->field)); 321 322 memset(entry, 0, 323 vstruct_end(&sb_clean->field) - (void *) entry); 324 325 /* 326 * this should be in the write path, and we should be validating every 327 * superblock section: 328 */ 329 ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE); 330 if (ret) { 331 bch_err(c, "error writing marking filesystem clean: validate error"); 332 goto out; 333 } 334 335 bch2_journal_pos_from_member_info_set(c); 336 337 bch2_write_super(c); 338 out: 339 mutex_unlock(&c->sb_lock); 340 } 341