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