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