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_validate(struct bch_fs *c, struct bkey_s_c k, 31 enum bch_validate_flags flags) 32 { 33 return 0; 34 } 35 36 #define bch2_bkey_ops_deleted ((struct bkey_ops) { \ 37 .key_validate = deleted_key_validate, \ 38 }) 39 40 #define bch2_bkey_ops_whiteout ((struct bkey_ops) { \ 41 .key_validate = deleted_key_validate, \ 42 }) 43 44 static int empty_val_key_validate(struct bch_fs *c, struct bkey_s_c k, 45 enum bch_validate_flags flags) 46 { 47 int ret = 0; 48 49 bkey_fsck_err_on(bkey_val_bytes(k.k), 50 c, 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_validate = empty_val_key_validate, \ 59 }) 60 61 static int key_type_cookie_validate(struct bch_fs *c, struct bkey_s_c k, 62 enum bch_validate_flags flags) 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_validate = key_type_cookie_validate, \ 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_validate = empty_val_key_validate, \ 83 }) 84 85 static int key_type_inline_data_validate(struct bch_fs *c, struct bkey_s_c k, 86 enum bch_validate_flags flags) 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_validate = key_type_inline_data_validate, \ 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_validate = empty_val_key_validate, \ 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_validate(struct bch_fs *c, struct bkey_s_c k, 127 enum bch_validate_flags flags) 128 { 129 if (test_bit(BCH_FS_no_invalid_checks, &c->flags)) 130 return 0; 131 132 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 133 int ret = 0; 134 135 bkey_fsck_err_on(bkey_val_bytes(k.k) < ops->min_val_size, 136 c, bkey_val_size_too_small, 137 "bad val size (%zu < %u)", 138 bkey_val_bytes(k.k), ops->min_val_size); 139 140 if (!ops->key_validate) 141 return 0; 142 143 ret = ops->key_validate(c, k, flags); 144 fsck_err: 145 return ret; 146 } 147 148 static u64 bch2_key_types_allowed[] = { 149 [BKEY_TYPE_btree] = 150 BIT_ULL(KEY_TYPE_deleted)| 151 BIT_ULL(KEY_TYPE_btree_ptr)| 152 BIT_ULL(KEY_TYPE_btree_ptr_v2), 153 #define x(name, nr, flags, keys) [BKEY_TYPE_##name] = BIT_ULL(KEY_TYPE_deleted)|keys, 154 BCH_BTREE_IDS() 155 #undef x 156 }; 157 158 const char *bch2_btree_node_type_str(enum btree_node_type type) 159 { 160 return type == BKEY_TYPE_btree ? "internal btree node" : bch2_btree_id_str(type - 1); 161 } 162 163 int __bch2_bkey_validate(struct bch_fs *c, struct bkey_s_c k, 164 enum btree_node_type type, 165 enum bch_validate_flags flags) 166 { 167 if (test_bit(BCH_FS_no_invalid_checks, &c->flags)) 168 return 0; 169 170 int ret = 0; 171 172 bkey_fsck_err_on(k.k->u64s < BKEY_U64s, 173 c, bkey_u64s_too_small, 174 "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s); 175 176 if (type >= BKEY_TYPE_NR) 177 return 0; 178 179 bkey_fsck_err_on(k.k->type < KEY_TYPE_MAX && 180 (type == BKEY_TYPE_btree || (flags & BCH_VALIDATE_commit)) && 181 !(bch2_key_types_allowed[type] & BIT_ULL(k.k->type)), 182 c, bkey_invalid_type_for_btree, 183 "invalid key type for btree %s (%s)", 184 bch2_btree_node_type_str(type), 185 k.k->type < KEY_TYPE_MAX 186 ? bch2_bkey_types[k.k->type] 187 : "(unknown)"); 188 189 if (btree_node_type_is_extents(type) && !bkey_whiteout(k.k)) { 190 bkey_fsck_err_on(k.k->size == 0, 191 c, bkey_extent_size_zero, 192 "size == 0"); 193 194 bkey_fsck_err_on(k.k->size > k.k->p.offset, 195 c, bkey_extent_size_greater_than_offset, 196 "size greater than offset (%u > %llu)", 197 k.k->size, k.k->p.offset); 198 } else { 199 bkey_fsck_err_on(k.k->size, 200 c, bkey_size_nonzero, 201 "size != 0"); 202 } 203 204 if (type != BKEY_TYPE_btree) { 205 enum btree_id btree = type - 1; 206 207 if (btree_type_has_snapshots(btree)) { 208 bkey_fsck_err_on(!k.k->p.snapshot, 209 c, bkey_snapshot_zero, 210 "snapshot == 0"); 211 } else if (!btree_type_has_snapshot_field(btree)) { 212 bkey_fsck_err_on(k.k->p.snapshot, 213 c, bkey_snapshot_nonzero, 214 "nonzero snapshot"); 215 } else { 216 /* 217 * btree uses snapshot field but it's not required to be 218 * nonzero 219 */ 220 } 221 222 bkey_fsck_err_on(bkey_eq(k.k->p, POS_MAX), 223 c, bkey_at_pos_max, 224 "key at POS_MAX"); 225 } 226 fsck_err: 227 return ret; 228 } 229 230 int bch2_bkey_validate(struct bch_fs *c, struct bkey_s_c k, 231 enum btree_node_type type, 232 enum bch_validate_flags flags) 233 { 234 return __bch2_bkey_validate(c, k, type, flags) ?: 235 bch2_bkey_val_validate(c, k, flags); 236 } 237 238 int bch2_bkey_in_btree_node(struct bch_fs *c, struct btree *b, 239 struct bkey_s_c k, enum bch_validate_flags flags) 240 { 241 int ret = 0; 242 243 bkey_fsck_err_on(bpos_lt(k.k->p, b->data->min_key), 244 c, bkey_before_start_of_btree_node, 245 "key before start of btree node"); 246 247 bkey_fsck_err_on(bpos_gt(k.k->p, b->data->max_key), 248 c, bkey_after_end_of_btree_node, 249 "key past end of btree node"); 250 fsck_err: 251 return ret; 252 } 253 254 void bch2_bpos_to_text(struct printbuf *out, struct bpos pos) 255 { 256 if (bpos_eq(pos, POS_MIN)) 257 prt_printf(out, "POS_MIN"); 258 else if (bpos_eq(pos, POS_MAX)) 259 prt_printf(out, "POS_MAX"); 260 else if (bpos_eq(pos, SPOS_MAX)) 261 prt_printf(out, "SPOS_MAX"); 262 else { 263 if (pos.inode == U64_MAX) 264 prt_printf(out, "U64_MAX"); 265 else 266 prt_printf(out, "%llu", pos.inode); 267 prt_printf(out, ":"); 268 if (pos.offset == U64_MAX) 269 prt_printf(out, "U64_MAX"); 270 else 271 prt_printf(out, "%llu", pos.offset); 272 prt_printf(out, ":"); 273 if (pos.snapshot == U32_MAX) 274 prt_printf(out, "U32_MAX"); 275 else 276 prt_printf(out, "%u", pos.snapshot); 277 } 278 } 279 280 void bch2_bkey_to_text(struct printbuf *out, const struct bkey *k) 281 { 282 if (k) { 283 prt_printf(out, "u64s %u type ", k->u64s); 284 285 if (k->type < KEY_TYPE_MAX) 286 prt_printf(out, "%s ", bch2_bkey_types[k->type]); 287 else 288 prt_printf(out, "%u ", k->type); 289 290 bch2_bpos_to_text(out, k->p); 291 292 prt_printf(out, " len %u ver %llu", k->size, k->version.lo); 293 } else { 294 prt_printf(out, "(null)"); 295 } 296 } 297 298 void bch2_val_to_text(struct printbuf *out, struct bch_fs *c, 299 struct bkey_s_c k) 300 { 301 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 302 303 if (likely(ops->val_to_text)) 304 ops->val_to_text(out, c, k); 305 } 306 307 void bch2_bkey_val_to_text(struct printbuf *out, struct bch_fs *c, 308 struct bkey_s_c k) 309 { 310 bch2_bkey_to_text(out, k.k); 311 312 if (bkey_val_bytes(k.k)) { 313 prt_printf(out, ": "); 314 bch2_val_to_text(out, c, k); 315 } 316 } 317 318 void bch2_bkey_swab_val(struct bkey_s k) 319 { 320 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 321 322 if (ops->swab) 323 ops->swab(k); 324 } 325 326 bool bch2_bkey_normalize(struct bch_fs *c, struct bkey_s k) 327 { 328 const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type); 329 330 return ops->key_normalize 331 ? ops->key_normalize(c, k) 332 : false; 333 } 334 335 bool bch2_bkey_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r) 336 { 337 const struct bkey_ops *ops = bch2_bkey_type_ops(l.k->type); 338 339 return ops->key_merge && 340 bch2_bkey_maybe_mergable(l.k, r.k) && 341 (u64) l.k->size + r.k->size <= KEY_SIZE_MAX && 342 !bch2_key_merging_disabled && 343 ops->key_merge(c, l, r); 344 } 345 346 static const struct old_bkey_type { 347 u8 btree_node_type; 348 u8 old; 349 u8 new; 350 } bkey_renumber_table[] = { 351 {BKEY_TYPE_btree, 128, KEY_TYPE_btree_ptr }, 352 {BKEY_TYPE_extents, 128, KEY_TYPE_extent }, 353 {BKEY_TYPE_extents, 129, KEY_TYPE_extent }, 354 {BKEY_TYPE_extents, 130, KEY_TYPE_reservation }, 355 {BKEY_TYPE_inodes, 128, KEY_TYPE_inode }, 356 {BKEY_TYPE_inodes, 130, KEY_TYPE_inode_generation }, 357 {BKEY_TYPE_dirents, 128, KEY_TYPE_dirent }, 358 {BKEY_TYPE_dirents, 129, KEY_TYPE_hash_whiteout }, 359 {BKEY_TYPE_xattrs, 128, KEY_TYPE_xattr }, 360 {BKEY_TYPE_xattrs, 129, KEY_TYPE_hash_whiteout }, 361 {BKEY_TYPE_alloc, 128, KEY_TYPE_alloc }, 362 {BKEY_TYPE_quotas, 128, KEY_TYPE_quota }, 363 }; 364 365 void bch2_bkey_renumber(enum btree_node_type btree_node_type, 366 struct bkey_packed *k, 367 int write) 368 { 369 const struct old_bkey_type *i; 370 371 for (i = bkey_renumber_table; 372 i < bkey_renumber_table + ARRAY_SIZE(bkey_renumber_table); 373 i++) 374 if (btree_node_type == i->btree_node_type && 375 k->type == (write ? i->new : i->old)) { 376 k->type = write ? i->old : i->new; 377 break; 378 } 379 } 380 381 void __bch2_bkey_compat(unsigned level, enum btree_id btree_id, 382 unsigned version, unsigned big_endian, 383 int write, 384 struct bkey_format *f, 385 struct bkey_packed *k) 386 { 387 const struct bkey_ops *ops; 388 struct bkey uk; 389 unsigned nr_compat = 5; 390 int i; 391 392 /* 393 * Do these operations in reverse order in the write path: 394 */ 395 396 for (i = 0; i < nr_compat; i++) 397 switch (!write ? i : nr_compat - 1 - i) { 398 case 0: 399 if (big_endian != CPU_BIG_ENDIAN) { 400 bch2_bkey_swab_key(f, k); 401 } else if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) { 402 bch2_bkey_swab_key(f, k); 403 bch2_bkey_swab_key(f, k); 404 } 405 break; 406 case 1: 407 if (version < bcachefs_metadata_version_bkey_renumber) 408 bch2_bkey_renumber(__btree_node_type(level, btree_id), k, write); 409 break; 410 case 2: 411 if (version < bcachefs_metadata_version_inode_btree_change && 412 btree_id == BTREE_ID_inodes) { 413 if (!bkey_packed(k)) { 414 struct bkey_i *u = packed_to_bkey(k); 415 416 swap(u->k.p.inode, u->k.p.offset); 417 } else if (f->bits_per_field[BKEY_FIELD_INODE] && 418 f->bits_per_field[BKEY_FIELD_OFFSET]) { 419 struct bkey_format tmp = *f, *in = f, *out = &tmp; 420 421 swap(tmp.bits_per_field[BKEY_FIELD_INODE], 422 tmp.bits_per_field[BKEY_FIELD_OFFSET]); 423 swap(tmp.field_offset[BKEY_FIELD_INODE], 424 tmp.field_offset[BKEY_FIELD_OFFSET]); 425 426 if (!write) 427 swap(in, out); 428 429 uk = __bch2_bkey_unpack_key(in, k); 430 swap(uk.p.inode, uk.p.offset); 431 BUG_ON(!bch2_bkey_pack_key(k, &uk, out)); 432 } 433 } 434 break; 435 case 3: 436 if (version < bcachefs_metadata_version_snapshot && 437 (level || btree_type_has_snapshots(btree_id))) { 438 struct bkey_i *u = packed_to_bkey(k); 439 440 if (u) { 441 u->k.p.snapshot = write 442 ? 0 : U32_MAX; 443 } else { 444 u64 min_packed = le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]); 445 u64 max_packed = min_packed + 446 ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]); 447 448 uk = __bch2_bkey_unpack_key(f, k); 449 uk.p.snapshot = write 450 ? min_packed : min_t(u64, U32_MAX, max_packed); 451 452 BUG_ON(!bch2_bkey_pack_key(k, &uk, f)); 453 } 454 } 455 456 break; 457 case 4: { 458 struct bkey_s u; 459 460 if (!bkey_packed(k)) { 461 u = bkey_i_to_s(packed_to_bkey(k)); 462 } else { 463 uk = __bch2_bkey_unpack_key(f, k); 464 u.k = &uk; 465 u.v = bkeyp_val(f, k); 466 } 467 468 if (big_endian != CPU_BIG_ENDIAN) 469 bch2_bkey_swab_val(u); 470 471 ops = bch2_bkey_type_ops(k->type); 472 473 if (ops->compat) 474 ops->compat(btree_id, version, big_endian, write, u); 475 break; 476 } 477 default: 478 BUG(); 479 } 480 } 481