1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "backpointers.h" 5 #include "bkey_methods.h" 6 #include "btree_cache.h" 7 #include "btree_types.h" 8 #include "alloc_background.h" 9 #include "dirent.h" 10 #include "disk_accounting.h" 11 #include "ec.h" 12 #include "error.h" 13 #include "extents.h" 14 #include "inode.h" 15 #include "io_misc.h" 16 #include "lru.h" 17 #include "quota.h" 18 #include "reflink.h" 19 #include "snapshot.h" 20 #include "subvolume.h" 21 #include "xattr.h" 22 23 const char * const bch2_bkey_types[] = { 24 #define x(name, nr) #name, 25 BCH_BKEY_TYPES() 26 #undef x 27 NULL 28 }; 29 30 static int deleted_key_invalid(struct bch_fs *c, struct bkey_s_c k, 31 enum bch_validate_flags flags, struct printbuf *err) 32 { 33 return 0; 34 } 35 36 #define bch2_bkey_ops_deleted ((struct bkey_ops) { \ 37 .key_invalid = deleted_key_invalid, \ 38 }) 39 40 #define bch2_bkey_ops_whiteout ((struct bkey_ops) { \ 41 .key_invalid = deleted_key_invalid, \ 42 }) 43 44 static int empty_val_key_invalid(struct bch_fs *c, struct bkey_s_c k, 45 enum bch_validate_flags flags, struct printbuf *err) 46 { 47 int ret = 0; 48 49 bkey_fsck_err_on(bkey_val_bytes(k.k), c, err, 50 bkey_val_size_nonzero, 51 "incorrect value size (%zu != 0)", 52 bkey_val_bytes(k.k)); 53 fsck_err: 54 return ret; 55 } 56 57 #define bch2_bkey_ops_error ((struct bkey_ops) { \ 58 .key_invalid = empty_val_key_invalid, \ 59 }) 60 61 static int key_type_cookie_invalid(struct bch_fs *c, struct bkey_s_c k, 62 enum bch_validate_flags flags, struct printbuf *err) 63 { 64 return 0; 65 } 66 67 static void key_type_cookie_to_text(struct printbuf *out, struct bch_fs *c, 68 struct bkey_s_c k) 69 { 70 struct bkey_s_c_cookie ck = bkey_s_c_to_cookie(k); 71 72 prt_printf(out, "%llu", le64_to_cpu(ck.v->cookie)); 73 } 74 75 #define bch2_bkey_ops_cookie ((struct bkey_ops) { \ 76 .key_invalid = key_type_cookie_invalid, \ 77 .val_to_text = key_type_cookie_to_text, \ 78 .min_val_size = 8, \ 79 }) 80 81 #define bch2_bkey_ops_hash_whiteout ((struct bkey_ops) {\ 82 .key_invalid = empty_val_key_invalid, \ 83 }) 84 85 static int key_type_inline_data_invalid(struct bch_fs *c, struct bkey_s_c k, 86 enum bch_validate_flags flags, struct printbuf *err) 87 { 88 return 0; 89 } 90 91 static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c, 92 struct bkey_s_c k) 93 { 94 struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k); 95 unsigned datalen = bkey_inline_data_bytes(k.k); 96 97 prt_printf(out, "datalen %u: %*phN", 98 datalen, min(datalen, 32U), d.v->data); 99 } 100 101 #define bch2_bkey_ops_inline_data ((struct bkey_ops) { \ 102 .key_invalid = key_type_inline_data_invalid, \ 103 .val_to_text = key_type_inline_data_to_text, \ 104 }) 105 106 static bool key_type_set_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r) 107 { 108 bch2_key_resize(l.k, l.k->size + r.k->size); 109 return true; 110 } 111 112 #define bch2_bkey_ops_set ((struct bkey_ops) { \ 113 .key_invalid = empty_val_key_invalid, \ 114 .key_merge = key_type_set_merge, \ 115 }) 116 117 const struct bkey_ops bch2_bkey_ops[] = { 118 #define x(name, nr) [KEY_TYPE_##name] = bch2_bkey_ops_##name, 119 BCH_BKEY_TYPES() 120 #undef x 121 }; 122 123 const struct bkey_ops bch2_bkey_null_ops = { 124 }; 125 126 int bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k, 127 enum bch_validate_flags flags, 128 struct printbuf *err) 129 { 130 if (test_bit(BCH_FS_no_invalid_checks, &c->flags)) 131 return 0; 132 133 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 134 int ret = 0; 135 136 bkey_fsck_err_on(bkey_val_bytes(k.k) < ops->min_val_size, c, err, 137 bkey_val_size_too_small, 138 "bad val size (%zu < %u)", 139 bkey_val_bytes(k.k), ops->min_val_size); 140 141 if (!ops->key_invalid) 142 return 0; 143 144 ret = ops->key_invalid(c, k, flags, err); 145 fsck_err: 146 return ret; 147 } 148 149 static u64 bch2_key_types_allowed[] = { 150 [BKEY_TYPE_btree] = 151 BIT_ULL(KEY_TYPE_deleted)| 152 BIT_ULL(KEY_TYPE_btree_ptr)| 153 BIT_ULL(KEY_TYPE_btree_ptr_v2), 154 #define x(name, nr, flags, keys) [BKEY_TYPE_##name] = BIT_ULL(KEY_TYPE_deleted)|keys, 155 BCH_BTREE_IDS() 156 #undef x 157 }; 158 159 const char *bch2_btree_node_type_str(enum btree_node_type type) 160 { 161 return type == BKEY_TYPE_btree ? "internal btree node" : bch2_btree_id_str(type - 1); 162 } 163 164 int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k, 165 enum btree_node_type type, 166 enum bch_validate_flags flags, 167 struct printbuf *err) 168 { 169 if (test_bit(BCH_FS_no_invalid_checks, &c->flags)) 170 return 0; 171 172 int ret = 0; 173 174 bkey_fsck_err_on(k.k->u64s < BKEY_U64s, c, err, 175 bkey_u64s_too_small, 176 "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s); 177 178 if (type >= BKEY_TYPE_NR) 179 return 0; 180 181 bkey_fsck_err_on(k.k->type < KEY_TYPE_MAX && 182 (type == BKEY_TYPE_btree || (flags & BCH_VALIDATE_commit)) && 183 !(bch2_key_types_allowed[type] & BIT_ULL(k.k->type)), c, err, 184 bkey_invalid_type_for_btree, 185 "invalid key type for btree %s (%s)", 186 bch2_btree_node_type_str(type), 187 k.k->type < KEY_TYPE_MAX 188 ? bch2_bkey_types[k.k->type] 189 : "(unknown)"); 190 191 if (btree_node_type_is_extents(type) && !bkey_whiteout(k.k)) { 192 bkey_fsck_err_on(k.k->size == 0, c, err, 193 bkey_extent_size_zero, 194 "size == 0"); 195 196 bkey_fsck_err_on(k.k->size > k.k->p.offset, c, err, 197 bkey_extent_size_greater_than_offset, 198 "size greater than offset (%u > %llu)", 199 k.k->size, k.k->p.offset); 200 } else { 201 bkey_fsck_err_on(k.k->size, c, err, 202 bkey_size_nonzero, 203 "size != 0"); 204 } 205 206 if (type != BKEY_TYPE_btree) { 207 enum btree_id btree = type - 1; 208 209 if (btree_type_has_snapshots(btree)) { 210 bkey_fsck_err_on(!k.k->p.snapshot, c, err, 211 bkey_snapshot_zero, 212 "snapshot == 0"); 213 } else if (!btree_type_has_snapshot_field(btree)) { 214 bkey_fsck_err_on(k.k->p.snapshot, c, err, 215 bkey_snapshot_nonzero, 216 "nonzero snapshot"); 217 } else { 218 /* 219 * btree uses snapshot field but it's not required to be 220 * nonzero 221 */ 222 } 223 224 bkey_fsck_err_on(bkey_eq(k.k->p, POS_MAX), c, err, 225 bkey_at_pos_max, 226 "key at POS_MAX"); 227 } 228 fsck_err: 229 return ret; 230 } 231 232 int bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k, 233 enum btree_node_type type, 234 enum bch_validate_flags flags, 235 struct printbuf *err) 236 { 237 return __bch2_bkey_invalid(c, k, type, flags, err) ?: 238 bch2_bkey_val_invalid(c, k, flags, err); 239 } 240 241 int bch2_bkey_in_btree_node(struct bch_fs *c, struct btree *b, 242 struct bkey_s_c k, struct printbuf *err) 243 { 244 int ret = 0; 245 246 bkey_fsck_err_on(bpos_lt(k.k->p, b->data->min_key), c, err, 247 bkey_before_start_of_btree_node, 248 "key before start of btree node"); 249 250 bkey_fsck_err_on(bpos_gt(k.k->p, b->data->max_key), c, err, 251 bkey_after_end_of_btree_node, 252 "key past end of btree node"); 253 fsck_err: 254 return ret; 255 } 256 257 void bch2_bpos_to_text(struct printbuf *out, struct bpos pos) 258 { 259 if (bpos_eq(pos, POS_MIN)) 260 prt_printf(out, "POS_MIN"); 261 else if (bpos_eq(pos, POS_MAX)) 262 prt_printf(out, "POS_MAX"); 263 else if (bpos_eq(pos, SPOS_MAX)) 264 prt_printf(out, "SPOS_MAX"); 265 else { 266 if (pos.inode == U64_MAX) 267 prt_printf(out, "U64_MAX"); 268 else 269 prt_printf(out, "%llu", pos.inode); 270 prt_printf(out, ":"); 271 if (pos.offset == U64_MAX) 272 prt_printf(out, "U64_MAX"); 273 else 274 prt_printf(out, "%llu", pos.offset); 275 prt_printf(out, ":"); 276 if (pos.snapshot == U32_MAX) 277 prt_printf(out, "U32_MAX"); 278 else 279 prt_printf(out, "%u", pos.snapshot); 280 } 281 } 282 283 void bch2_bkey_to_text(struct printbuf *out, const struct bkey *k) 284 { 285 if (k) { 286 prt_printf(out, "u64s %u type ", k->u64s); 287 288 if (k->type < KEY_TYPE_MAX) 289 prt_printf(out, "%s ", bch2_bkey_types[k->type]); 290 else 291 prt_printf(out, "%u ", k->type); 292 293 bch2_bpos_to_text(out, k->p); 294 295 prt_printf(out, " len %u ver %llu", k->size, k->version.lo); 296 } else { 297 prt_printf(out, "(null)"); 298 } 299 } 300 301 void bch2_val_to_text(struct printbuf *out, struct bch_fs *c, 302 struct bkey_s_c k) 303 { 304 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 305 306 if (likely(ops->val_to_text)) 307 ops->val_to_text(out, c, k); 308 } 309 310 void bch2_bkey_val_to_text(struct printbuf *out, struct bch_fs *c, 311 struct bkey_s_c k) 312 { 313 bch2_bkey_to_text(out, k.k); 314 315 if (bkey_val_bytes(k.k)) { 316 prt_printf(out, ": "); 317 bch2_val_to_text(out, c, k); 318 } 319 } 320 321 void bch2_bkey_swab_val(struct bkey_s k) 322 { 323 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 324 325 if (ops->swab) 326 ops->swab(k); 327 } 328 329 bool bch2_bkey_normalize(struct bch_fs *c, struct bkey_s k) 330 { 331 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 332 333 return ops->key_normalize 334 ? ops->key_normalize(c, k) 335 : false; 336 } 337 338 bool bch2_bkey_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r) 339 { 340 const struct bkey_ops *ops = bch2_bkey_type_ops(l.k->type); 341 342 return ops->key_merge && 343 bch2_bkey_maybe_mergable(l.k, r.k) && 344 (u64) l.k->size + r.k->size <= KEY_SIZE_MAX && 345 !bch2_key_merging_disabled && 346 ops->key_merge(c, l, r); 347 } 348 349 static const struct old_bkey_type { 350 u8 btree_node_type; 351 u8 old; 352 u8 new; 353 } bkey_renumber_table[] = { 354 {BKEY_TYPE_btree, 128, KEY_TYPE_btree_ptr }, 355 {BKEY_TYPE_extents, 128, KEY_TYPE_extent }, 356 {BKEY_TYPE_extents, 129, KEY_TYPE_extent }, 357 {BKEY_TYPE_extents, 130, KEY_TYPE_reservation }, 358 {BKEY_TYPE_inodes, 128, KEY_TYPE_inode }, 359 {BKEY_TYPE_inodes, 130, KEY_TYPE_inode_generation }, 360 {BKEY_TYPE_dirents, 128, KEY_TYPE_dirent }, 361 {BKEY_TYPE_dirents, 129, KEY_TYPE_hash_whiteout }, 362 {BKEY_TYPE_xattrs, 128, KEY_TYPE_xattr }, 363 {BKEY_TYPE_xattrs, 129, KEY_TYPE_hash_whiteout }, 364 {BKEY_TYPE_alloc, 128, KEY_TYPE_alloc }, 365 {BKEY_TYPE_quotas, 128, KEY_TYPE_quota }, 366 }; 367 368 void bch2_bkey_renumber(enum btree_node_type btree_node_type, 369 struct bkey_packed *k, 370 int write) 371 { 372 const struct old_bkey_type *i; 373 374 for (i = bkey_renumber_table; 375 i < bkey_renumber_table + ARRAY_SIZE(bkey_renumber_table); 376 i++) 377 if (btree_node_type == i->btree_node_type && 378 k->type == (write ? i->new : i->old)) { 379 k->type = write ? i->old : i->new; 380 break; 381 } 382 } 383 384 void __bch2_bkey_compat(unsigned level, enum btree_id btree_id, 385 unsigned version, unsigned big_endian, 386 int write, 387 struct bkey_format *f, 388 struct bkey_packed *k) 389 { 390 const struct bkey_ops *ops; 391 struct bkey uk; 392 unsigned nr_compat = 5; 393 int i; 394 395 /* 396 * Do these operations in reverse order in the write path: 397 */ 398 399 for (i = 0; i < nr_compat; i++) 400 switch (!write ? i : nr_compat - 1 - i) { 401 case 0: 402 if (big_endian != CPU_BIG_ENDIAN) { 403 bch2_bkey_swab_key(f, k); 404 } else if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) { 405 bch2_bkey_swab_key(f, k); 406 bch2_bkey_swab_key(f, k); 407 } 408 break; 409 case 1: 410 if (version < bcachefs_metadata_version_bkey_renumber) 411 bch2_bkey_renumber(__btree_node_type(level, btree_id), k, write); 412 break; 413 case 2: 414 if (version < bcachefs_metadata_version_inode_btree_change && 415 btree_id == BTREE_ID_inodes) { 416 if (!bkey_packed(k)) { 417 struct bkey_i *u = packed_to_bkey(k); 418 419 swap(u->k.p.inode, u->k.p.offset); 420 } else if (f->bits_per_field[BKEY_FIELD_INODE] && 421 f->bits_per_field[BKEY_FIELD_OFFSET]) { 422 struct bkey_format tmp = *f, *in = f, *out = &tmp; 423 424 swap(tmp.bits_per_field[BKEY_FIELD_INODE], 425 tmp.bits_per_field[BKEY_FIELD_OFFSET]); 426 swap(tmp.field_offset[BKEY_FIELD_INODE], 427 tmp.field_offset[BKEY_FIELD_OFFSET]); 428 429 if (!write) 430 swap(in, out); 431 432 uk = __bch2_bkey_unpack_key(in, k); 433 swap(uk.p.inode, uk.p.offset); 434 BUG_ON(!bch2_bkey_pack_key(k, &uk, out)); 435 } 436 } 437 break; 438 case 3: 439 if (version < bcachefs_metadata_version_snapshot && 440 (level || btree_type_has_snapshots(btree_id))) { 441 struct bkey_i *u = packed_to_bkey(k); 442 443 if (u) { 444 u->k.p.snapshot = write 445 ? 0 : U32_MAX; 446 } else { 447 u64 min_packed = le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]); 448 u64 max_packed = min_packed + 449 ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]); 450 451 uk = __bch2_bkey_unpack_key(f, k); 452 uk.p.snapshot = write 453 ? min_packed : min_t(u64, U32_MAX, max_packed); 454 455 BUG_ON(!bch2_bkey_pack_key(k, &uk, f)); 456 } 457 } 458 459 break; 460 case 4: { 461 struct bkey_s u; 462 463 if (!bkey_packed(k)) { 464 u = bkey_i_to_s(packed_to_bkey(k)); 465 } else { 466 uk = __bch2_bkey_unpack_key(f, k); 467 u.k = &uk; 468 u.v = bkeyp_val(f, k); 469 } 470 471 if (big_endian != CPU_BIG_ENDIAN) 472 bch2_bkey_swab_val(u); 473 474 ops = bch2_bkey_type_ops(k->type); 475 476 if (ops->compat) 477 ops->compat(btree_id, version, big_endian, write, u); 478 break; 479 } 480 default: 481 BUG(); 482 } 483 } 484