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 jset_entry *entry; 27 int ret; 28 29 for (entry = clean->start; 30 entry < (struct jset_entry *) vstruct_end(&clean->field); 31 entry = vstruct_next(entry)) { 32 ret = bch2_journal_entry_validate(c, NULL, entry, 33 le16_to_cpu(c->disk_sb.sb->version), 34 BCH_SB_BIG_ENDIAN(c->disk_sb.sb), 35 write); 36 if (ret) 37 return ret; 38 } 39 40 return 0; 41 } 42 43 static struct bkey_i *btree_root_find(struct bch_fs *c, 44 struct bch_sb_field_clean *clean, 45 struct jset *j, 46 enum btree_id id, unsigned *level) 47 { 48 struct bkey_i *k; 49 struct jset_entry *entry, *start, *end; 50 51 if (clean) { 52 start = clean->start; 53 end = vstruct_end(&clean->field); 54 } else { 55 start = j->start; 56 end = vstruct_last(j); 57 } 58 59 for (entry = start; entry < end; entry = vstruct_next(entry)) 60 if (entry->type == BCH_JSET_ENTRY_btree_root && 61 entry->btree_id == id) 62 goto found; 63 64 return NULL; 65 found: 66 if (!entry->u64s) 67 return ERR_PTR(-EINVAL); 68 69 k = entry->start; 70 *level = entry->level; 71 return k; 72 } 73 74 int bch2_verify_superblock_clean(struct bch_fs *c, 75 struct bch_sb_field_clean **cleanp, 76 struct jset *j) 77 { 78 unsigned i; 79 struct bch_sb_field_clean *clean = *cleanp; 80 struct printbuf buf1 = PRINTBUF; 81 struct printbuf buf2 = PRINTBUF; 82 int ret = 0; 83 84 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c, 85 sb_clean_journal_seq_mismatch, 86 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown", 87 le64_to_cpu(clean->journal_seq), 88 le64_to_cpu(j->seq))) { 89 kfree(clean); 90 *cleanp = NULL; 91 return 0; 92 } 93 94 for (i = 0; i < BTREE_ID_NR; i++) { 95 struct bkey_i *k1, *k2; 96 unsigned l1 = 0, l2 = 0; 97 98 k1 = btree_root_find(c, clean, NULL, i, &l1); 99 k2 = btree_root_find(c, NULL, j, i, &l2); 100 101 if (!k1 && !k2) 102 continue; 103 104 printbuf_reset(&buf1); 105 printbuf_reset(&buf2); 106 107 if (k1) 108 bch2_bkey_val_to_text(&buf1, c, bkey_i_to_s_c(k1)); 109 else 110 prt_printf(&buf1, "(none)"); 111 112 if (k2) 113 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(k2)); 114 else 115 prt_printf(&buf2, "(none)"); 116 117 mustfix_fsck_err_on(!k1 || !k2 || 118 IS_ERR(k1) || 119 IS_ERR(k2) || 120 k1->k.u64s != k2->k.u64s || 121 memcmp(k1, k2, bkey_bytes(&k1->k)) || 122 l1 != l2, c, 123 sb_clean_btree_root_mismatch, 124 "superblock btree root %u doesn't match journal after clean shutdown\n" 125 "sb: l=%u %s\n" 126 "journal: l=%u %s\n", i, 127 l1, buf1.buf, 128 l2, buf2.buf); 129 } 130 fsck_err: 131 printbuf_exit(&buf2); 132 printbuf_exit(&buf1); 133 return ret; 134 } 135 136 struct bch_sb_field_clean *bch2_read_superblock_clean(struct bch_fs *c) 137 { 138 struct bch_sb_field_clean *clean, *sb_clean; 139 int ret; 140 141 mutex_lock(&c->sb_lock); 142 sb_clean = bch2_sb_field_get(c->disk_sb.sb, clean); 143 144 if (fsck_err_on(!sb_clean, c, 145 sb_clean_missing, 146 "superblock marked clean but clean section not present")) { 147 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 148 c->sb.clean = false; 149 mutex_unlock(&c->sb_lock); 150 return NULL; 151 } 152 153 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field), 154 GFP_KERNEL); 155 if (!clean) { 156 mutex_unlock(&c->sb_lock); 157 return ERR_PTR(-BCH_ERR_ENOMEM_read_superblock_clean); 158 } 159 160 ret = bch2_sb_clean_validate_late(c, clean, READ); 161 if (ret) { 162 mutex_unlock(&c->sb_lock); 163 return ERR_PTR(ret); 164 } 165 166 mutex_unlock(&c->sb_lock); 167 168 return clean; 169 fsck_err: 170 mutex_unlock(&c->sb_lock); 171 return ERR_PTR(ret); 172 } 173 174 void bch2_journal_super_entries_add_common(struct bch_fs *c, 175 struct jset_entry **end, 176 u64 journal_seq) 177 { 178 percpu_down_read(&c->mark_lock); 179 180 if (!journal_seq) { 181 for (unsigned i = 0; i < ARRAY_SIZE(c->usage); i++) 182 bch2_fs_usage_acc_to_base(c, i); 183 } else { 184 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK); 185 } 186 187 { 188 struct jset_entry_usage *u = 189 container_of(jset_entry_init(end, sizeof(*u)), 190 struct jset_entry_usage, entry); 191 192 u->entry.type = BCH_JSET_ENTRY_usage; 193 u->entry.btree_id = BCH_FS_USAGE_inodes; 194 u->v = cpu_to_le64(c->usage_base->b.nr_inodes); 195 } 196 197 { 198 struct jset_entry_usage *u = 199 container_of(jset_entry_init(end, sizeof(*u)), 200 struct jset_entry_usage, entry); 201 202 u->entry.type = BCH_JSET_ENTRY_usage; 203 u->entry.btree_id = BCH_FS_USAGE_key_version; 204 u->v = cpu_to_le64(atomic64_read(&c->key_version)); 205 } 206 207 for (unsigned i = 0; i < BCH_REPLICAS_MAX; i++) { 208 struct jset_entry_usage *u = 209 container_of(jset_entry_init(end, sizeof(*u)), 210 struct jset_entry_usage, entry); 211 212 u->entry.type = BCH_JSET_ENTRY_usage; 213 u->entry.btree_id = BCH_FS_USAGE_reserved; 214 u->entry.level = i; 215 u->v = cpu_to_le64(c->usage_base->persistent_reserved[i]); 216 } 217 218 for (unsigned i = 0; i < c->replicas.nr; i++) { 219 struct bch_replicas_entry_v1 *e = 220 cpu_replicas_entry(&c->replicas, i); 221 struct jset_entry_data_usage *u = 222 container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs), 223 struct jset_entry_data_usage, entry); 224 225 u->entry.type = BCH_JSET_ENTRY_data_usage; 226 u->v = cpu_to_le64(c->usage_base->replicas[i]); 227 unsafe_memcpy(&u->r, e, replicas_entry_bytes(e), 228 "embedded variable length struct"); 229 } 230 231 for_each_member_device(c, ca) { 232 unsigned b = sizeof(struct jset_entry_dev_usage) + 233 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR; 234 struct jset_entry_dev_usage *u = 235 container_of(jset_entry_init(end, b), 236 struct jset_entry_dev_usage, entry); 237 238 u->entry.type = BCH_JSET_ENTRY_dev_usage; 239 u->dev = cpu_to_le32(ca->dev_idx); 240 241 for (unsigned i = 0; i < BCH_DATA_NR; i++) { 242 u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets); 243 u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors); 244 u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented); 245 } 246 } 247 248 percpu_up_read(&c->mark_lock); 249 250 for (unsigned i = 0; i < 2; i++) { 251 struct jset_entry_clock *clock = 252 container_of(jset_entry_init(end, sizeof(*clock)), 253 struct jset_entry_clock, entry); 254 255 clock->entry.type = BCH_JSET_ENTRY_clock; 256 clock->rw = i; 257 clock->time = cpu_to_le64(atomic64_read(&c->io_clock[i].now)); 258 } 259 } 260 261 static int bch2_sb_clean_validate(struct bch_sb *sb, 262 struct bch_sb_field *f, 263 struct printbuf *err) 264 { 265 struct bch_sb_field_clean *clean = field_to_type(f, clean); 266 267 if (vstruct_bytes(&clean->field) < sizeof(*clean)) { 268 prt_printf(err, "wrong size (got %zu should be %zu)", 269 vstruct_bytes(&clean->field), sizeof(*clean)); 270 return -BCH_ERR_invalid_sb_clean; 271 } 272 273 return 0; 274 } 275 276 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb, 277 struct bch_sb_field *f) 278 { 279 struct bch_sb_field_clean *clean = field_to_type(f, clean); 280 struct jset_entry *entry; 281 282 prt_printf(out, "flags: %x", le32_to_cpu(clean->flags)); 283 prt_newline(out); 284 prt_printf(out, "journal_seq: %llu", le64_to_cpu(clean->journal_seq)); 285 prt_newline(out); 286 287 for (entry = clean->start; 288 entry != vstruct_end(&clean->field); 289 entry = vstruct_next(entry)) { 290 if (entry->type == BCH_JSET_ENTRY_btree_keys && 291 !entry->u64s) 292 continue; 293 294 bch2_journal_entry_to_text(out, NULL, entry); 295 prt_newline(out); 296 } 297 } 298 299 const struct bch_sb_field_ops bch_sb_field_ops_clean = { 300 .validate = bch2_sb_clean_validate, 301 .to_text = bch2_sb_clean_to_text, 302 }; 303 304 int bch2_fs_mark_dirty(struct bch_fs *c) 305 { 306 int ret; 307 308 /* 309 * Unconditionally write superblock, to verify it hasn't changed before 310 * we go rw: 311 */ 312 313 mutex_lock(&c->sb_lock); 314 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 315 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS); 316 317 ret = bch2_write_super(c); 318 mutex_unlock(&c->sb_lock); 319 320 return ret; 321 } 322 323 void bch2_fs_mark_clean(struct bch_fs *c) 324 { 325 struct bch_sb_field_clean *sb_clean; 326 struct jset_entry *entry; 327 unsigned u64s; 328 int ret; 329 330 mutex_lock(&c->sb_lock); 331 if (BCH_SB_CLEAN(c->disk_sb.sb)) 332 goto out; 333 334 SET_BCH_SB_CLEAN(c->disk_sb.sb, true); 335 336 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info); 337 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata); 338 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates)); 339 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled)); 340 341 u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved; 342 343 sb_clean = bch2_sb_field_resize(&c->disk_sb, clean, u64s); 344 if (!sb_clean) { 345 bch_err(c, "error resizing superblock while setting filesystem clean"); 346 goto out; 347 } 348 349 sb_clean->flags = 0; 350 sb_clean->journal_seq = cpu_to_le64(atomic64_read(&c->journal.seq)); 351 352 /* Trying to catch outstanding bug: */ 353 BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX); 354 355 entry = sb_clean->start; 356 bch2_journal_super_entries_add_common(c, &entry, 0); 357 entry = bch2_btree_roots_to_journal_entries(c, entry, 0); 358 BUG_ON((void *) entry > vstruct_end(&sb_clean->field)); 359 360 memset(entry, 0, 361 vstruct_end(&sb_clean->field) - (void *) entry); 362 363 /* 364 * this should be in the write path, and we should be validating every 365 * superblock section: 366 */ 367 ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE); 368 if (ret) { 369 bch_err(c, "error writing marking filesystem clean: validate error"); 370 goto out; 371 } 372 373 bch2_write_super(c); 374 out: 375 mutex_unlock(&c->sb_lock); 376 } 377