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