]>
Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com> | |
4 | * | |
5 | * Code for managing the extent btree and dynamically updating the writeback | |
6 | * dirty sector count. | |
7 | */ | |
8 | ||
9 | #include "bcachefs.h" | |
10 | #include "bkey_methods.h" | |
11 | #include "btree_gc.h" | |
39fb2983 | 12 | #include "btree_io.h" |
08c07fea | 13 | #include "btree_iter.h" |
1c6fdbd8 KO |
14 | #include "buckets.h" |
15 | #include "checksum.h" | |
fb3f57bb | 16 | #include "compress.h" |
1c6fdbd8 | 17 | #include "debug.h" |
1c6fdbd8 KO |
18 | #include "disk_groups.h" |
19 | #include "error.h" | |
20 | #include "extents.h" | |
21 | #include "inode.h" | |
22 | #include "journal.h" | |
23 | #include "replicas.h" | |
24 | #include "super.h" | |
25 | #include "super-io.h" | |
26 | #include "trace.h" | |
27 | #include "util.h" | |
1c6fdbd8 | 28 | |
4de77495 KO |
29 | static unsigned bch2_crc_field_size_max[] = { |
30 | [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX, | |
31 | [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX, | |
32 | [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX, | |
33 | }; | |
1c6fdbd8 | 34 | |
4de77495 KO |
35 | static void bch2_extent_crc_pack(union bch_extent_crc *, |
36 | struct bch_extent_crc_unpacked, | |
37 | enum bch_extent_entry_type); | |
1c6fdbd8 | 38 | |
26609b61 KO |
39 | static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f, |
40 | unsigned dev) | |
41 | { | |
42 | struct bch_dev_io_failures *i; | |
43 | ||
44 | for (i = f->devs; i < f->devs + f->nr; i++) | |
45 | if (i->dev == dev) | |
46 | return i; | |
47 | ||
48 | return NULL; | |
49 | } | |
50 | ||
51 | void bch2_mark_io_failure(struct bch_io_failures *failed, | |
52 | struct extent_ptr_decoded *p) | |
53 | { | |
54 | struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev); | |
55 | ||
56 | if (!f) { | |
57 | BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs)); | |
58 | ||
59 | f = &failed->devs[failed->nr++]; | |
60 | f->dev = p->ptr.dev; | |
61 | f->idx = p->idx; | |
62 | f->nr_failed = 1; | |
63 | f->nr_retries = 0; | |
64 | } else if (p->idx != f->idx) { | |
65 | f->idx = p->idx; | |
66 | f->nr_failed = 1; | |
67 | f->nr_retries = 0; | |
68 | } else { | |
69 | f->nr_failed++; | |
70 | } | |
71 | } | |
72 | ||
73 | /* | |
74 | * returns true if p1 is better than p2: | |
75 | */ | |
76 | static inline bool ptr_better(struct bch_fs *c, | |
77 | const struct extent_ptr_decoded p1, | |
78 | const struct extent_ptr_decoded p2) | |
79 | { | |
80 | if (likely(!p1.idx && !p2.idx)) { | |
81 | struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev); | |
82 | struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev); | |
83 | ||
84 | u64 l1 = atomic64_read(&dev1->cur_latency[READ]); | |
85 | u64 l2 = atomic64_read(&dev2->cur_latency[READ]); | |
86 | ||
87 | /* Pick at random, biased in favor of the faster device: */ | |
88 | ||
89 | return bch2_rand_range(l1 + l2) > l1; | |
90 | } | |
91 | ||
29364f34 | 92 | if (bch2_force_reconstruct_read) |
26609b61 KO |
93 | return p1.idx > p2.idx; |
94 | ||
95 | return p1.idx < p2.idx; | |
96 | } | |
97 | ||
98 | /* | |
99 | * This picks a non-stale pointer, preferably from a device other than @avoid. | |
100 | * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to | |
101 | * other devices, it will still pick a pointer from avoid. | |
102 | */ | |
103 | int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k, | |
104 | struct bch_io_failures *failed, | |
105 | struct extent_ptr_decoded *pick) | |
106 | { | |
107 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
108 | const union bch_extent_entry *entry; | |
109 | struct extent_ptr_decoded p; | |
110 | struct bch_dev_io_failures *f; | |
111 | struct bch_dev *ca; | |
112 | int ret = 0; | |
113 | ||
114 | if (k.k->type == KEY_TYPE_error) | |
115 | return -EIO; | |
116 | ||
117 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { | |
79203111 KO |
118 | /* |
119 | * Unwritten extent: no need to actually read, treat it as a | |
120 | * hole and return 0s: | |
121 | */ | |
122 | if (p.ptr.unwritten) | |
123 | return 0; | |
124 | ||
26609b61 KO |
125 | ca = bch_dev_bkey_exists(c, p.ptr.dev); |
126 | ||
127 | /* | |
128 | * If there are any dirty pointers it's an error if we can't | |
129 | * read: | |
130 | */ | |
131 | if (!ret && !p.ptr.cached) | |
132 | ret = -EIO; | |
133 | ||
134 | if (p.ptr.cached && ptr_stale(ca, &p.ptr)) | |
135 | continue; | |
136 | ||
137 | f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL; | |
138 | if (f) | |
139 | p.idx = f->nr_failed < f->nr_retries | |
140 | ? f->idx | |
141 | : f->idx + 1; | |
142 | ||
143 | if (!p.idx && | |
144 | !bch2_dev_is_readable(ca)) | |
145 | p.idx++; | |
146 | ||
29364f34 | 147 | if (bch2_force_reconstruct_read && |
37954a27 | 148 | !p.idx && p.has_ec) |
26609b61 KO |
149 | p.idx++; |
150 | ||
37954a27 | 151 | if (p.idx >= (unsigned) p.has_ec + 1) |
26609b61 KO |
152 | continue; |
153 | ||
154 | if (ret > 0 && !ptr_better(c, p, *pick)) | |
155 | continue; | |
156 | ||
157 | *pick = p; | |
158 | ret = 1; | |
159 | } | |
160 | ||
161 | return ret; | |
162 | } | |
163 | ||
4de77495 | 164 | /* KEY_TYPE_btree_ptr: */ |
26609b61 | 165 | |
b65db750 | 166 | int bch2_btree_ptr_invalid(struct bch_fs *c, struct bkey_s_c k, |
8726dc93 KO |
167 | enum bkey_invalid_flags flags, |
168 | struct printbuf *err) | |
26609b61 | 169 | { |
b65db750 | 170 | int ret = 0; |
26609b61 | 171 | |
b65db750 KO |
172 | bkey_fsck_err_on(bkey_val_u64s(k.k) > BCH_REPLICAS_MAX, c, err, |
173 | btree_ptr_val_too_big, | |
174 | "value too big (%zu > %u)", bkey_val_u64s(k.k), BCH_REPLICAS_MAX); | |
175 | ||
176 | ret = bch2_bkey_ptrs_invalid(c, k, flags, err); | |
177 | fsck_err: | |
178 | return ret; | |
26609b61 KO |
179 | } |
180 | ||
4de77495 KO |
181 | void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c, |
182 | struct bkey_s_c k) | |
26609b61 | 183 | { |
4de77495 KO |
184 | bch2_bkey_ptrs_to_text(out, c, k); |
185 | } | |
26609b61 | 186 | |
b65db750 | 187 | int bch2_btree_ptr_v2_invalid(struct bch_fs *c, struct bkey_s_c k, |
8726dc93 KO |
188 | enum bkey_invalid_flags flags, |
189 | struct printbuf *err) | |
fad7cfed | 190 | { |
b65db750 | 191 | int ret = 0; |
fad7cfed | 192 | |
b65db750 KO |
193 | bkey_fsck_err_on(bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX, c, err, |
194 | btree_ptr_v2_val_too_big, | |
195 | "value too big (%zu > %zu)", | |
196 | bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX); | |
197 | ||
198 | ret = bch2_bkey_ptrs_invalid(c, k, flags, err); | |
199 | fsck_err: | |
200 | return ret; | |
fad7cfed KO |
201 | } |
202 | ||
59a38a38 | 203 | void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c, |
275c8426 | 204 | struct bkey_s_c k) |
59a38a38 KO |
205 | { |
206 | struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k); | |
207 | ||
401ec4db | 208 | prt_printf(out, "seq %llx written %u min_key %s", |
59a38a38 | 209 | le64_to_cpu(bp.v->seq), |
f8f86c6a KO |
210 | le16_to_cpu(bp.v->sectors_written), |
211 | BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : ""); | |
59a38a38 KO |
212 | |
213 | bch2_bpos_to_text(out, bp.v->min_key); | |
401ec4db | 214 | prt_printf(out, " "); |
59a38a38 KO |
215 | bch2_bkey_ptrs_to_text(out, c, k); |
216 | } | |
217 | ||
39fb2983 KO |
218 | void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version, |
219 | unsigned big_endian, int write, | |
220 | struct bkey_s k) | |
221 | { | |
222 | struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k); | |
223 | ||
224 | compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key); | |
225 | ||
226 | if (version < bcachefs_metadata_version_inode_btree_change && | |
73bd774d | 227 | btree_id_is_extents(btree_id) && |
e88a75eb | 228 | !bkey_eq(bp.v->min_key, POS_MIN)) |
39fb2983 | 229 | bp.v->min_key = write |
e751c01a KO |
230 | ? bpos_nosnap_predecessor(bp.v->min_key) |
231 | : bpos_nosnap_successor(bp.v->min_key); | |
39fb2983 KO |
232 | } |
233 | ||
4de77495 | 234 | /* KEY_TYPE_extent: */ |
26609b61 | 235 | |
b058ac20 | 236 | bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r) |
1c6fdbd8 | 237 | { |
b058ac20 KO |
238 | struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l); |
239 | struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r); | |
c2177e4d KO |
240 | union bch_extent_entry *en_l; |
241 | const union bch_extent_entry *en_r; | |
242 | struct extent_ptr_decoded lp, rp; | |
243 | bool use_right_ptr; | |
244 | struct bch_dev *ca; | |
a2753581 | 245 | |
b058ac20 KO |
246 | en_l = l_ptrs.start; |
247 | en_r = r_ptrs.start; | |
248 | while (en_l < l_ptrs.end && en_r < r_ptrs.end) { | |
4de77495 | 249 | if (extent_entry_type(en_l) != extent_entry_type(en_r)) |
59ba21d9 | 250 | return false; |
b058ac20 KO |
251 | |
252 | en_l = extent_entry_next(en_l); | |
253 | en_r = extent_entry_next(en_r); | |
c2177e4d | 254 | } |
4de77495 | 255 | |
b058ac20 KO |
256 | if (en_l < l_ptrs.end || en_r < r_ptrs.end) |
257 | return false; | |
258 | ||
259 | en_l = l_ptrs.start; | |
260 | en_r = r_ptrs.start; | |
c2177e4d KO |
261 | lp.crc = bch2_extent_crc_unpack(l.k, NULL); |
262 | rp.crc = bch2_extent_crc_unpack(r.k, NULL); | |
263 | ||
b058ac20 KO |
264 | while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) && |
265 | __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) { | |
c2177e4d KO |
266 | if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size != |
267 | rp.ptr.offset + rp.crc.offset || | |
268 | lp.ptr.dev != rp.ptr.dev || | |
269 | lp.ptr.gen != rp.ptr.gen || | |
79203111 | 270 | lp.ptr.unwritten != rp.ptr.unwritten || |
c2177e4d KO |
271 | lp.has_ec != rp.has_ec) |
272 | return false; | |
4de77495 | 273 | |
c2177e4d KO |
274 | /* Extents may not straddle buckets: */ |
275 | ca = bch_dev_bkey_exists(c, lp.ptr.dev); | |
276 | if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr)) | |
277 | return false; | |
4de77495 | 278 | |
c2177e4d KO |
279 | if (lp.has_ec != rp.has_ec || |
280 | (lp.has_ec && | |
281 | (lp.ec.block != rp.ec.block || | |
282 | lp.ec.redundancy != rp.ec.redundancy || | |
283 | lp.ec.idx != rp.ec.idx))) | |
284 | return false; | |
4de77495 | 285 | |
c2177e4d KO |
286 | if (lp.crc.compression_type != rp.crc.compression_type || |
287 | lp.crc.nonce != rp.crc.nonce) | |
288 | return false; | |
4de77495 | 289 | |
c2177e4d KO |
290 | if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <= |
291 | lp.crc.uncompressed_size) { | |
292 | /* can use left extent's crc entry */ | |
3e3e02e6 | 293 | } else if (lp.crc.live_size <= rp.crc.offset) { |
c2177e4d KO |
294 | /* can use right extent's crc entry */ |
295 | } else { | |
296 | /* check if checksums can be merged: */ | |
297 | if (lp.crc.csum_type != rp.crc.csum_type || | |
298 | lp.crc.nonce != rp.crc.nonce || | |
299 | crc_is_compressed(lp.crc) || | |
300 | !bch2_checksum_mergeable(lp.crc.csum_type)) | |
59ba21d9 | 301 | return false; |
4de77495 | 302 | |
c2177e4d KO |
303 | if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size || |
304 | rp.crc.offset) | |
59ba21d9 | 305 | return false; |
4de77495 | 306 | |
c2177e4d KO |
307 | if (lp.crc.csum_type && |
308 | lp.crc.uncompressed_size + | |
e4099990 | 309 | rp.crc.uncompressed_size > (c->opts.encoded_extent_max >> 9)) |
59ba21d9 | 310 | return false; |
e320b42d KO |
311 | } |
312 | ||
313 | en_l = extent_entry_next(en_l); | |
314 | en_r = extent_entry_next(en_r); | |
315 | } | |
316 | ||
317 | en_l = l_ptrs.start; | |
318 | en_r = r_ptrs.start; | |
319 | while (en_l < l_ptrs.end && en_r < r_ptrs.end) { | |
320 | if (extent_entry_is_crc(en_l)) { | |
321 | struct bch_extent_crc_unpacked crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l)); | |
322 | struct bch_extent_crc_unpacked crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r)); | |
4de77495 | 323 | |
e320b42d | 324 | if (crc_l.uncompressed_size + crc_r.uncompressed_size > |
4de77495 | 325 | bch2_crc_field_size_max[extent_entry_type(en_l)]) |
59ba21d9 | 326 | return false; |
a2753581 | 327 | } |
c2177e4d KO |
328 | |
329 | en_l = extent_entry_next(en_l); | |
330 | en_r = extent_entry_next(en_r); | |
4de77495 | 331 | } |
cd575ddf | 332 | |
c2177e4d | 333 | use_right_ptr = false; |
b058ac20 KO |
334 | en_l = l_ptrs.start; |
335 | en_r = r_ptrs.start; | |
336 | while (en_l < l_ptrs.end) { | |
c2177e4d KO |
337 | if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr && |
338 | use_right_ptr) | |
339 | en_l->ptr = en_r->ptr; | |
340 | ||
b058ac20 KO |
341 | if (extent_entry_is_crc(en_l)) { |
342 | struct bch_extent_crc_unpacked crc_l = | |
343 | bch2_extent_crc_unpack(l.k, entry_to_crc(en_l)); | |
344 | struct bch_extent_crc_unpacked crc_r = | |
345 | bch2_extent_crc_unpack(r.k, entry_to_crc(en_r)); | |
346 | ||
347 | use_right_ptr = false; | |
348 | ||
349 | if (crc_l.offset + crc_l.live_size + crc_r.live_size <= | |
350 | crc_l.uncompressed_size) { | |
351 | /* can use left extent's crc entry */ | |
3e3e02e6 | 352 | } else if (crc_l.live_size <= crc_r.offset) { |
b058ac20 KO |
353 | /* can use right extent's crc entry */ |
354 | crc_r.offset -= crc_l.live_size; | |
355 | bch2_extent_crc_pack(entry_to_crc(en_l), crc_r, | |
356 | extent_entry_type(en_l)); | |
357 | use_right_ptr = true; | |
358 | } else { | |
359 | crc_l.csum = bch2_checksum_merge(crc_l.csum_type, | |
360 | crc_l.csum, | |
361 | crc_r.csum, | |
362 | crc_r.uncompressed_size << 9); | |
363 | ||
364 | crc_l.uncompressed_size += crc_r.uncompressed_size; | |
365 | crc_l.compressed_size += crc_r.compressed_size; | |
366 | bch2_extent_crc_pack(entry_to_crc(en_l), crc_l, | |
367 | extent_entry_type(en_l)); | |
368 | } | |
c2177e4d | 369 | } |
b058ac20 KO |
370 | |
371 | en_l = extent_entry_next(en_l); | |
372 | en_r = extent_entry_next(en_r); | |
a2753581 KO |
373 | } |
374 | ||
4de77495 | 375 | bch2_key_resize(l.k, l.k->size + r.k->size); |
59ba21d9 | 376 | return true; |
4de77495 KO |
377 | } |
378 | ||
379 | /* KEY_TYPE_reservation: */ | |
380 | ||
b65db750 | 381 | int bch2_reservation_invalid(struct bch_fs *c, struct bkey_s_c k, |
8726dc93 KO |
382 | enum bkey_invalid_flags flags, |
383 | struct printbuf *err) | |
4de77495 KO |
384 | { |
385 | struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k); | |
b65db750 | 386 | int ret = 0; |
4de77495 | 387 | |
b65db750 KO |
388 | bkey_fsck_err_on(!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX, c, err, |
389 | reservation_key_nr_replicas_invalid, | |
390 | "invalid nr_replicas (%u)", r.v->nr_replicas); | |
391 | fsck_err: | |
392 | return ret; | |
4de77495 KO |
393 | } |
394 | ||
395 | void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c, | |
396 | struct bkey_s_c k) | |
397 | { | |
398 | struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k); | |
399 | ||
401ec4db | 400 | prt_printf(out, "generation %u replicas %u", |
4de77495 KO |
401 | le32_to_cpu(r.v->generation), |
402 | r.v->nr_replicas); | |
403 | } | |
404 | ||
59ba21d9 | 405 | bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r) |
4de77495 KO |
406 | { |
407 | struct bkey_s_reservation l = bkey_s_to_reservation(_l); | |
59ba21d9 | 408 | struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r); |
4de77495 KO |
409 | |
410 | if (l.v->generation != r.v->generation || | |
411 | l.v->nr_replicas != r.v->nr_replicas) | |
59ba21d9 | 412 | return false; |
4de77495 KO |
413 | |
414 | bch2_key_resize(l.k, l.k->size + r.k->size); | |
59ba21d9 | 415 | return true; |
4de77495 KO |
416 | } |
417 | ||
418 | /* Extent checksum entries: */ | |
419 | ||
420 | /* returns true if not equal */ | |
421 | static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l, | |
422 | struct bch_extent_crc_unpacked r) | |
423 | { | |
424 | return (l.csum_type != r.csum_type || | |
425 | l.compression_type != r.compression_type || | |
426 | l.compressed_size != r.compressed_size || | |
427 | l.uncompressed_size != r.uncompressed_size || | |
428 | l.offset != r.offset || | |
429 | l.live_size != r.live_size || | |
430 | l.nonce != r.nonce || | |
431 | bch2_crc_cmp(l.csum, r.csum)); | |
1c6fdbd8 KO |
432 | } |
433 | ||
434 | static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u, | |
435 | struct bch_extent_crc_unpacked n) | |
436 | { | |
ab05de4c | 437 | return !crc_is_compressed(u) && |
1c6fdbd8 KO |
438 | u.csum_type && |
439 | u.uncompressed_size > u.live_size && | |
440 | bch2_csum_type_is_encryption(u.csum_type) == | |
441 | bch2_csum_type_is_encryption(n.csum_type); | |
442 | } | |
443 | ||
99aaf570 | 444 | bool bch2_can_narrow_extent_crcs(struct bkey_s_c k, |
1c6fdbd8 KO |
445 | struct bch_extent_crc_unpacked n) |
446 | { | |
99aaf570 | 447 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); |
1c6fdbd8 KO |
448 | struct bch_extent_crc_unpacked crc; |
449 | const union bch_extent_entry *i; | |
450 | ||
451 | if (!n.csum_type) | |
452 | return false; | |
453 | ||
99aaf570 | 454 | bkey_for_each_crc(k.k, ptrs, crc, i) |
1c6fdbd8 KO |
455 | if (can_narrow_crc(crc, n)) |
456 | return true; | |
457 | ||
458 | return false; | |
459 | } | |
460 | ||
461 | /* | |
462 | * We're writing another replica for this extent, so while we've got the data in | |
463 | * memory we'll be computing a new checksum for the currently live data. | |
464 | * | |
465 | * If there are other replicas we aren't moving, and they are checksummed but | |
466 | * not compressed, we can modify them to point to only the data that is | |
467 | * currently live (so that readers won't have to bounce) while we've got the | |
468 | * checksum we need: | |
469 | */ | |
99aaf570 | 470 | bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n) |
1c6fdbd8 | 471 | { |
99aaf570 | 472 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k)); |
1c6fdbd8 | 473 | struct bch_extent_crc_unpacked u; |
1742237b | 474 | struct extent_ptr_decoded p; |
1c6fdbd8 | 475 | union bch_extent_entry *i; |
71c9e0ba | 476 | bool ret = false; |
1c6fdbd8 KO |
477 | |
478 | /* Find a checksum entry that covers only live data: */ | |
71c9e0ba | 479 | if (!n.csum_type) { |
99aaf570 | 480 | bkey_for_each_crc(&k->k, ptrs, u, i) |
ab05de4c | 481 | if (!crc_is_compressed(u) && |
1c6fdbd8 KO |
482 | u.csum_type && |
483 | u.live_size == u.uncompressed_size) { | |
484 | n = u; | |
71c9e0ba | 485 | goto found; |
1c6fdbd8 | 486 | } |
1c6fdbd8 | 487 | return false; |
71c9e0ba KO |
488 | } |
489 | found: | |
ab05de4c | 490 | BUG_ON(crc_is_compressed(n)); |
1c6fdbd8 | 491 | BUG_ON(n.offset); |
99aaf570 | 492 | BUG_ON(n.live_size != k->k.size); |
1c6fdbd8 | 493 | |
1c6fdbd8 | 494 | restart_narrow_pointers: |
f698a957 KO |
495 | ptrs = bch2_bkey_ptrs(bkey_i_to_s(k)); |
496 | ||
99aaf570 | 497 | bkey_for_each_ptr_decode(&k->k, ptrs, p, i) |
1742237b | 498 | if (can_narrow_crc(p.crc, n)) { |
702ffea2 | 499 | bch2_bkey_drop_ptr_noerror(bkey_i_to_s(k), &i->ptr); |
71c9e0ba KO |
500 | p.ptr.offset += p.crc.offset; |
501 | p.crc = n; | |
99aaf570 | 502 | bch2_extent_ptr_decoded_append(k, &p); |
71c9e0ba | 503 | ret = true; |
1c6fdbd8 KO |
504 | goto restart_narrow_pointers; |
505 | } | |
506 | ||
71c9e0ba | 507 | return ret; |
1c6fdbd8 KO |
508 | } |
509 | ||
4de77495 KO |
510 | static void bch2_extent_crc_pack(union bch_extent_crc *dst, |
511 | struct bch_extent_crc_unpacked src, | |
512 | enum bch_extent_entry_type type) | |
1c6fdbd8 | 513 | { |
4de77495 KO |
514 | #define set_common_fields(_dst, _src) \ |
515 | _dst.type = 1 << type; \ | |
516 | _dst.csum_type = _src.csum_type, \ | |
517 | _dst.compression_type = _src.compression_type, \ | |
518 | _dst._compressed_size = _src.compressed_size - 1, \ | |
519 | _dst._uncompressed_size = _src.uncompressed_size - 1, \ | |
520 | _dst.offset = _src.offset | |
521 | ||
522 | switch (type) { | |
523 | case BCH_EXTENT_ENTRY_crc32: | |
524 | set_common_fields(dst->crc32, src); | |
1e81f89b | 525 | dst->crc32.csum = (u32 __force) *((__le32 *) &src.csum.lo); |
4de77495 KO |
526 | break; |
527 | case BCH_EXTENT_ENTRY_crc64: | |
528 | set_common_fields(dst->crc64, src); | |
529 | dst->crc64.nonce = src.nonce; | |
73bd774d KO |
530 | dst->crc64.csum_lo = (u64 __force) src.csum.lo; |
531 | dst->crc64.csum_hi = (u64 __force) *((__le16 *) &src.csum.hi); | |
4de77495 KO |
532 | break; |
533 | case BCH_EXTENT_ENTRY_crc128: | |
534 | set_common_fields(dst->crc128, src); | |
535 | dst->crc128.nonce = src.nonce; | |
536 | dst->crc128.csum = src.csum; | |
537 | break; | |
538 | default: | |
539 | BUG(); | |
540 | } | |
541 | #undef set_common_fields | |
1c6fdbd8 KO |
542 | } |
543 | ||
4de77495 KO |
544 | void bch2_extent_crc_append(struct bkey_i *k, |
545 | struct bch_extent_crc_unpacked new) | |
1c6fdbd8 | 546 | { |
4de77495 KO |
547 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k)); |
548 | union bch_extent_crc *crc = (void *) ptrs.end; | |
549 | enum bch_extent_entry_type type; | |
1c6fdbd8 | 550 | |
4de77495 | 551 | if (bch_crc_bytes[new.csum_type] <= 4 && |
e65fcb43 | 552 | new.uncompressed_size <= CRC32_SIZE_MAX && |
4de77495 KO |
553 | new.nonce <= CRC32_NONCE_MAX) |
554 | type = BCH_EXTENT_ENTRY_crc32; | |
555 | else if (bch_crc_bytes[new.csum_type] <= 10 && | |
e65fcb43 | 556 | new.uncompressed_size <= CRC64_SIZE_MAX && |
4de77495 KO |
557 | new.nonce <= CRC64_NONCE_MAX) |
558 | type = BCH_EXTENT_ENTRY_crc64; | |
559 | else if (bch_crc_bytes[new.csum_type] <= 16 && | |
e65fcb43 | 560 | new.uncompressed_size <= CRC128_SIZE_MAX && |
4de77495 KO |
561 | new.nonce <= CRC128_NONCE_MAX) |
562 | type = BCH_EXTENT_ENTRY_crc128; | |
563 | else | |
564 | BUG(); | |
1c6fdbd8 | 565 | |
4de77495 KO |
566 | bch2_extent_crc_pack(crc, new, type); |
567 | ||
568 | k->k.u64s += extent_entry_u64s(ptrs.end); | |
569 | ||
570 | EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX); | |
1c6fdbd8 KO |
571 | } |
572 | ||
4de77495 | 573 | /* Generic code for keys with pointers: */ |
1c6fdbd8 | 574 | |
4de77495 KO |
575 | unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k) |
576 | { | |
577 | return bch2_bkey_devs(k).nr; | |
1c6fdbd8 KO |
578 | } |
579 | ||
4de77495 | 580 | unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k) |
3811aa6d | 581 | { |
4de77495 KO |
582 | return k.k->type == KEY_TYPE_reservation |
583 | ? bkey_s_c_to_reservation(k).v->nr_replicas | |
584 | : bch2_bkey_dirty_devs(k).nr; | |
3811aa6d KO |
585 | } |
586 | ||
4de77495 | 587 | unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k) |
1c6fdbd8 | 588 | { |
4de77495 | 589 | unsigned ret = 0; |
3811aa6d | 590 | |
4de77495 KO |
591 | if (k.k->type == KEY_TYPE_reservation) { |
592 | ret = bkey_s_c_to_reservation(k).v->nr_replicas; | |
593 | } else { | |
594 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
595 | const union bch_extent_entry *entry; | |
596 | struct extent_ptr_decoded p; | |
3811aa6d | 597 | |
4de77495 | 598 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) |
ab05de4c | 599 | ret += !p.ptr.cached && !crc_is_compressed(p.crc); |
1c6fdbd8 | 600 | } |
26609b61 | 601 | |
4de77495 | 602 | return ret; |
3811aa6d KO |
603 | } |
604 | ||
4de77495 | 605 | unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k) |
1c6fdbd8 | 606 | { |
26609b61 | 607 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); |
4de77495 KO |
608 | const union bch_extent_entry *entry; |
609 | struct extent_ptr_decoded p; | |
610 | unsigned ret = 0; | |
1c6fdbd8 | 611 | |
4de77495 | 612 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) |
ab05de4c | 613 | if (!p.ptr.cached && crc_is_compressed(p.crc)) |
4de77495 | 614 | ret += p.crc.compressed_size; |
1c6fdbd8 | 615 | |
4de77495 | 616 | return ret; |
1c6fdbd8 KO |
617 | } |
618 | ||
ab05de4c KO |
619 | bool bch2_bkey_is_incompressible(struct bkey_s_c k) |
620 | { | |
621 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
622 | const union bch_extent_entry *entry; | |
623 | struct bch_extent_crc_unpacked crc; | |
624 | ||
625 | bkey_for_each_crc(k.k, ptrs, crc, entry) | |
626 | if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible) | |
627 | return true; | |
628 | return false; | |
629 | } | |
630 | ||
35a067b4 KO |
631 | unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k) |
632 | { | |
633 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
634 | const union bch_extent_entry *entry; | |
33a391a2 | 635 | struct extent_ptr_decoded p = { 0 }; |
35a067b4 KO |
636 | unsigned replicas = 0; |
637 | ||
638 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { | |
639 | if (p.ptr.cached) | |
640 | continue; | |
641 | ||
7f4e1d5d KO |
642 | if (p.has_ec) |
643 | replicas += p.ec.redundancy; | |
35a067b4 KO |
644 | |
645 | replicas++; | |
646 | ||
647 | } | |
648 | ||
649 | return replicas; | |
650 | } | |
651 | ||
e4f72bb4 | 652 | static inline unsigned __extent_ptr_durability(struct bch_dev *ca, struct extent_ptr_decoded *p) |
1c6fdbd8 | 653 | { |
a8b3a677 | 654 | if (p->ptr.cached) |
085ab693 | 655 | return 0; |
1c6fdbd8 | 656 | |
e4f72bb4 KO |
657 | return p->has_ec |
658 | ? p->ec.redundancy + 1 | |
659 | : ca->mi.durability; | |
91ecd41b | 660 | } |
1c6fdbd8 | 661 | |
e4f72bb4 | 662 | unsigned bch2_extent_ptr_desired_durability(struct bch_fs *c, struct extent_ptr_decoded *p) |
91ecd41b | 663 | { |
e4f72bb4 | 664 | struct bch_dev *ca = bch_dev_bkey_exists(c, p->ptr.dev); |
085ab693 | 665 | |
e4f72bb4 KO |
666 | return __extent_ptr_durability(ca, p); |
667 | } | |
91ecd41b | 668 | |
e4f72bb4 KO |
669 | unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded *p) |
670 | { | |
671 | struct bch_dev *ca = bch_dev_bkey_exists(c, p->ptr.dev); | |
91ecd41b KO |
672 | |
673 | if (ca->mi.state == BCH_MEMBER_STATE_failed) | |
674 | return 0; | |
675 | ||
e4f72bb4 | 676 | return __extent_ptr_durability(ca, p); |
1c6fdbd8 KO |
677 | } |
678 | ||
4de77495 | 679 | unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k) |
1c6fdbd8 | 680 | { |
4de77495 | 681 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); |
45dbb73e KO |
682 | const union bch_extent_entry *entry; |
683 | struct extent_ptr_decoded p; | |
4de77495 | 684 | unsigned durability = 0; |
45dbb73e | 685 | |
4de77495 | 686 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) |
702ffea2 KO |
687 | durability += bch2_extent_ptr_durability(c, &p); |
688 | ||
689 | return durability; | |
690 | } | |
691 | ||
692 | static unsigned bch2_bkey_durability_safe(struct bch_fs *c, struct bkey_s_c k) | |
693 | { | |
694 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
695 | const union bch_extent_entry *entry; | |
696 | struct extent_ptr_decoded p; | |
697 | unsigned durability = 0; | |
698 | ||
699 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) | |
700 | if (p.ptr.dev < c->sb.nr_devices && c->devs[p.ptr.dev]) | |
701 | durability += bch2_extent_ptr_durability(c, &p); | |
45dbb73e | 702 | |
4de77495 | 703 | return durability; |
1c6fdbd8 KO |
704 | } |
705 | ||
0507962f KO |
706 | void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry) |
707 | { | |
708 | union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k)); | |
709 | union bch_extent_entry *next = extent_entry_next(entry); | |
710 | ||
711 | memmove_u64s(entry, next, (u64 *) end - (u64 *) next); | |
712 | k->k.u64s -= extent_entry_u64s(entry); | |
713 | } | |
714 | ||
99aaf570 | 715 | void bch2_extent_ptr_decoded_append(struct bkey_i *k, |
71c9e0ba KO |
716 | struct extent_ptr_decoded *p) |
717 | { | |
99aaf570 KO |
718 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k)); |
719 | struct bch_extent_crc_unpacked crc = | |
720 | bch2_extent_crc_unpack(&k->k, NULL); | |
71c9e0ba | 721 | union bch_extent_entry *pos; |
1c6fdbd8 | 722 | |
71c9e0ba | 723 | if (!bch2_crc_unpacked_cmp(crc, p->crc)) { |
99aaf570 | 724 | pos = ptrs.start; |
71c9e0ba KO |
725 | goto found; |
726 | } | |
727 | ||
99aaf570 | 728 | bkey_for_each_crc(&k->k, ptrs, crc, pos) |
71c9e0ba KO |
729 | if (!bch2_crc_unpacked_cmp(crc, p->crc)) { |
730 | pos = extent_entry_next(pos); | |
731 | goto found; | |
732 | } | |
733 | ||
99aaf570 KO |
734 | bch2_extent_crc_append(k, p->crc); |
735 | pos = bkey_val_end(bkey_i_to_s(k)); | |
71c9e0ba KO |
736 | found: |
737 | p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr; | |
99aaf570 | 738 | __extent_entry_insert(k, pos, to_entry(&p->ptr)); |
cd575ddf | 739 | |
37954a27 KO |
740 | if (p->has_ec) { |
741 | p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr; | |
742 | __extent_entry_insert(k, pos, to_entry(&p->ec)); | |
cd575ddf | 743 | } |
1c6fdbd8 KO |
744 | } |
745 | ||
4de77495 KO |
746 | static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs, |
747 | union bch_extent_entry *entry) | |
748 | { | |
749 | union bch_extent_entry *i = ptrs.start; | |
750 | ||
751 | if (i == entry) | |
752 | return NULL; | |
753 | ||
754 | while (extent_entry_next(i) != entry) | |
755 | i = extent_entry_next(i); | |
756 | return i; | |
757 | } | |
758 | ||
b9a7d8ac KO |
759 | /* |
760 | * Returns pointer to the next entry after the one being dropped: | |
761 | */ | |
702ffea2 KO |
762 | union bch_extent_entry *bch2_bkey_drop_ptr_noerror(struct bkey_s k, |
763 | struct bch_extent_ptr *ptr) | |
4de77495 KO |
764 | { |
765 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(k); | |
b9a7d8ac KO |
766 | union bch_extent_entry *entry = to_entry(ptr), *next; |
767 | union bch_extent_entry *ret = entry; | |
4de77495 KO |
768 | bool drop_crc = true; |
769 | ||
770 | EBUG_ON(ptr < &ptrs.start->ptr || | |
771 | ptr >= &ptrs.end->ptr); | |
772 | EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr); | |
773 | ||
b9a7d8ac KO |
774 | for (next = extent_entry_next(entry); |
775 | next != ptrs.end; | |
776 | next = extent_entry_next(next)) { | |
777 | if (extent_entry_is_crc(next)) { | |
4de77495 | 778 | break; |
b9a7d8ac KO |
779 | } else if (extent_entry_is_ptr(next)) { |
780 | drop_crc = false; | |
4de77495 KO |
781 | break; |
782 | } | |
b9a7d8ac KO |
783 | } |
784 | ||
785 | extent_entry_drop(k, entry); | |
4de77495 | 786 | |
b9a7d8ac KO |
787 | while ((entry = extent_entry_prev(ptrs, entry))) { |
788 | if (extent_entry_is_ptr(entry)) | |
789 | break; | |
790 | ||
791 | if ((extent_entry_is_crc(entry) && drop_crc) || | |
792 | extent_entry_is_stripe_ptr(entry)) { | |
793 | ret = (void *) ret - extent_entry_bytes(entry); | |
794 | extent_entry_drop(k, entry); | |
795 | } | |
4de77495 KO |
796 | } |
797 | ||
b9a7d8ac KO |
798 | return ret; |
799 | } | |
800 | ||
801 | union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k, | |
802 | struct bch_extent_ptr *ptr) | |
803 | { | |
804 | bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr; | |
805 | union bch_extent_entry *ret = | |
702ffea2 | 806 | bch2_bkey_drop_ptr_noerror(k, ptr); |
b9a7d8ac KO |
807 | |
808 | /* | |
809 | * If we deleted all the dirty pointers and there's still cached | |
810 | * pointers, we could set the cached pointers to dirty if they're not | |
811 | * stale - but to do that correctly we'd need to grab an open_bucket | |
812 | * reference so that we don't race with bucket reuse: | |
813 | */ | |
814 | if (have_dirty && | |
815 | !bch2_bkey_dirty_devs(k.s_c).nr) { | |
816 | k.k->type = KEY_TYPE_error; | |
817 | set_bkey_val_u64s(k.k, 0); | |
818 | ret = NULL; | |
819 | } else if (!bch2_bkey_nr_ptrs(k.s_c)) { | |
820 | k.k->type = KEY_TYPE_deleted; | |
821 | set_bkey_val_u64s(k.k, 0); | |
822 | ret = NULL; | |
823 | } | |
4de77495 | 824 | |
b9a7d8ac | 825 | return ret; |
4de77495 KO |
826 | } |
827 | ||
828 | void bch2_bkey_drop_device(struct bkey_s k, unsigned dev) | |
829 | { | |
830 | struct bch_extent_ptr *ptr; | |
831 | ||
832 | bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev); | |
833 | } | |
834 | ||
7f5c5d20 KO |
835 | void bch2_bkey_drop_device_noerror(struct bkey_s k, unsigned dev) |
836 | { | |
702ffea2 | 837 | struct bch_extent_ptr *ptr = bch2_bkey_has_device(k, dev); |
7f5c5d20 KO |
838 | |
839 | if (ptr) | |
702ffea2 | 840 | bch2_bkey_drop_ptr_noerror(k, ptr); |
7f5c5d20 KO |
841 | } |
842 | ||
702ffea2 | 843 | const struct bch_extent_ptr *bch2_bkey_has_device_c(struct bkey_s_c k, unsigned dev) |
4de77495 KO |
844 | { |
845 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
846 | const struct bch_extent_ptr *ptr; | |
847 | ||
848 | bkey_for_each_ptr(ptrs, ptr) | |
849 | if (ptr->dev == dev) | |
850 | return ptr; | |
851 | ||
852 | return NULL; | |
853 | } | |
854 | ||
855 | bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target) | |
856 | { | |
857 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
858 | const struct bch_extent_ptr *ptr; | |
859 | ||
860 | bkey_for_each_ptr(ptrs, ptr) | |
861 | if (bch2_dev_in_target(c, ptr->dev, target) && | |
862 | (!ptr->cached || | |
863 | !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr))) | |
864 | return true; | |
865 | ||
866 | return false; | |
867 | } | |
868 | ||
869 | bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k, | |
870 | struct bch_extent_ptr m, u64 offset) | |
871 | { | |
872 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
873 | const union bch_extent_entry *entry; | |
874 | struct extent_ptr_decoded p; | |
875 | ||
876 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) | |
877 | if (p.ptr.dev == m.dev && | |
878 | p.ptr.gen == m.gen && | |
879 | (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) == | |
880 | (s64) m.offset - offset) | |
881 | return true; | |
882 | ||
883 | return false; | |
884 | } | |
885 | ||
7f5c5d20 KO |
886 | /* |
887 | * Returns true if two extents refer to the same data: | |
888 | */ | |
889 | bool bch2_extents_match(struct bkey_s_c k1, struct bkey_s_c k2) | |
890 | { | |
a8b3a677 | 891 | if (k1.k->type != k2.k->type) |
79203111 KO |
892 | return false; |
893 | ||
a8b3a677 KO |
894 | if (bkey_extent_is_direct_data(k1.k)) { |
895 | struct bkey_ptrs_c ptrs1 = bch2_bkey_ptrs_c(k1); | |
896 | struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2); | |
897 | const union bch_extent_entry *entry1, *entry2; | |
898 | struct extent_ptr_decoded p1, p2; | |
899 | ||
900 | if (bkey_extent_is_unwritten(k1) != bkey_extent_is_unwritten(k2)) | |
901 | return false; | |
902 | ||
903 | bkey_for_each_ptr_decode(k1.k, ptrs1, p1, entry1) | |
904 | bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2) | |
1e81f89b KO |
905 | if (p1.ptr.dev == p2.ptr.dev && |
906 | p1.ptr.gen == p2.ptr.gen && | |
907 | (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) == | |
908 | (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k)) | |
909 | return true; | |
7f5c5d20 | 910 | |
a8b3a677 KO |
911 | return false; |
912 | } else { | |
913 | /* KEY_TYPE_deleted, etc. */ | |
914 | return true; | |
915 | } | |
7f5c5d20 KO |
916 | } |
917 | ||
702ffea2 KO |
918 | struct bch_extent_ptr * |
919 | bch2_extent_has_ptr(struct bkey_s_c k1, struct extent_ptr_decoded p1, struct bkey_s k2) | |
7f5c5d20 | 920 | { |
702ffea2 KO |
921 | struct bkey_ptrs ptrs2 = bch2_bkey_ptrs(k2); |
922 | union bch_extent_entry *entry2; | |
7f5c5d20 KO |
923 | struct extent_ptr_decoded p2; |
924 | ||
925 | bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2) | |
926 | if (p1.ptr.dev == p2.ptr.dev && | |
927 | p1.ptr.gen == p2.ptr.gen && | |
928 | (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) == | |
929 | (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k)) | |
702ffea2 | 930 | return &entry2->ptr; |
7f5c5d20 | 931 | |
702ffea2 | 932 | return NULL; |
7f5c5d20 KO |
933 | } |
934 | ||
c9163bb0 KO |
935 | void bch2_extent_ptr_set_cached(struct bkey_s k, struct bch_extent_ptr *ptr) |
936 | { | |
937 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(k); | |
938 | union bch_extent_entry *entry; | |
939 | union bch_extent_entry *ec = NULL; | |
940 | ||
941 | bkey_extent_entry_for_each(ptrs, entry) { | |
942 | if (&entry->ptr == ptr) { | |
943 | ptr->cached = true; | |
944 | if (ec) | |
945 | extent_entry_drop(k, ec); | |
946 | return; | |
947 | } | |
948 | ||
949 | if (extent_entry_is_stripe_ptr(entry)) | |
950 | ec = entry; | |
951 | else if (extent_entry_is_ptr(entry)) | |
952 | ec = NULL; | |
953 | } | |
954 | ||
955 | BUG(); | |
956 | } | |
957 | ||
1c6fdbd8 KO |
958 | /* |
959 | * bch_extent_normalize - clean up an extent, dropping stale pointers etc. | |
960 | * | |
961 | * Returns true if @k should be dropped entirely | |
962 | * | |
963 | * For existing keys, only called when btree nodes are being rewritten, not when | |
964 | * they're merely being compacted/resorted in memory. | |
965 | */ | |
966 | bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k) | |
967 | { | |
26609b61 | 968 | struct bch_extent_ptr *ptr; |
1c6fdbd8 | 969 | |
26609b61 KO |
970 | bch2_bkey_drop_ptrs(k, ptr, |
971 | ptr->cached && | |
972 | ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)); | |
1c6fdbd8 | 973 | |
c052cf82 | 974 | return bkey_deleted(k.k); |
1c6fdbd8 KO |
975 | } |
976 | ||
4de77495 KO |
977 | void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c, |
978 | struct bkey_s_c k) | |
1c6fdbd8 | 979 | { |
4de77495 KO |
980 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); |
981 | const union bch_extent_entry *entry; | |
4de77495 | 982 | bool first = true; |
1c6fdbd8 | 983 | |
702ffea2 KO |
984 | if (c) |
985 | prt_printf(out, "durability: %u ", bch2_bkey_durability_safe(c, k)); | |
986 | ||
4de77495 KO |
987 | bkey_extent_entry_for_each(ptrs, entry) { |
988 | if (!first) | |
401ec4db | 989 | prt_printf(out, " "); |
1c6fdbd8 | 990 | |
4de77495 | 991 | switch (__extent_entry_type(entry)) { |
ef435abd KO |
992 | case BCH_EXTENT_ENTRY_ptr: { |
993 | const struct bch_extent_ptr *ptr = entry_to_ptr(entry); | |
994 | struct bch_dev *ca = c && ptr->dev < c->sb.nr_devices && c->devs[ptr->dev] | |
1296ab55 KO |
995 | ? bch_dev_bkey_exists(c, ptr->dev) |
996 | : NULL; | |
997 | ||
998 | if (!ca) { | |
401ec4db | 999 | prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev, |
1296ab55 KO |
1000 | (u64) ptr->offset, ptr->gen, |
1001 | ptr->cached ? " cached" : ""); | |
1002 | } else { | |
1003 | u32 offset; | |
1004 | u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset); | |
1c6fdbd8 | 1005 | |
79203111 KO |
1006 | prt_printf(out, "ptr: %u:%llu:%u gen %u", |
1007 | ptr->dev, b, offset, ptr->gen); | |
1008 | if (ptr->cached) | |
1009 | prt_str(out, " cached"); | |
1010 | if (ptr->unwritten) | |
1011 | prt_str(out, " unwritten"); | |
12bf93a4 | 1012 | if (ca && ptr_stale(ca, ptr)) |
401ec4db | 1013 | prt_printf(out, " stale"); |
12bf93a4 | 1014 | } |
4de77495 | 1015 | break; |
ef435abd | 1016 | } |
4de77495 KO |
1017 | case BCH_EXTENT_ENTRY_crc32: |
1018 | case BCH_EXTENT_ENTRY_crc64: | |
ef435abd KO |
1019 | case BCH_EXTENT_ENTRY_crc128: { |
1020 | struct bch_extent_crc_unpacked crc = | |
1021 | bch2_extent_crc_unpack(k.k, entry_to_crc(entry)); | |
1c6fdbd8 | 1022 | |
401ec4db | 1023 | prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s", |
4de77495 KO |
1024 | crc.compressed_size, |
1025 | crc.uncompressed_size, | |
1026 | crc.offset, crc.nonce, | |
6404dcc9 KO |
1027 | bch2_csum_types[crc.csum_type], |
1028 | bch2_compression_types[crc.compression_type]); | |
4de77495 | 1029 | break; |
ef435abd KO |
1030 | } |
1031 | case BCH_EXTENT_ENTRY_stripe_ptr: { | |
1032 | const struct bch_extent_stripe_ptr *ec = &entry->stripe_ptr; | |
4de77495 | 1033 | |
401ec4db | 1034 | prt_printf(out, "ec: idx %llu block %u", |
4de77495 KO |
1035 | (u64) ec->idx, ec->block); |
1036 | break; | |
ef435abd | 1037 | } |
fb3f57bb KO |
1038 | case BCH_EXTENT_ENTRY_rebalance: { |
1039 | const struct bch_extent_rebalance *r = &entry->rebalance; | |
1040 | ||
1041 | prt_str(out, "rebalance: target "); | |
1042 | if (c) | |
1043 | bch2_target_to_text(out, c, r->target); | |
1044 | else | |
1045 | prt_printf(out, "%u", r->target); | |
1046 | prt_str(out, " compression "); | |
1047 | bch2_compression_opt_to_text(out, r->compression); | |
1048 | break; | |
1049 | } | |
4de77495 | 1050 | default: |
401ec4db | 1051 | prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry)); |
4de77495 | 1052 | return; |
1c6fdbd8 | 1053 | } |
4de77495 KO |
1054 | |
1055 | first = false; | |
1056 | } | |
1c6fdbd8 KO |
1057 | } |
1058 | ||
b65db750 | 1059 | static int extent_ptr_invalid(struct bch_fs *c, |
f0ac7df2 | 1060 | struct bkey_s_c k, |
e9679b4a | 1061 | enum bkey_invalid_flags flags, |
f0ac7df2 KO |
1062 | const struct bch_extent_ptr *ptr, |
1063 | unsigned size_ondisk, | |
1064 | bool metadata, | |
1065 | struct printbuf *err) | |
1c6fdbd8 | 1066 | { |
4de77495 KO |
1067 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); |
1068 | const struct bch_extent_ptr *ptr2; | |
f0ac7df2 KO |
1069 | u64 bucket; |
1070 | u32 bucket_offset; | |
4de77495 | 1071 | struct bch_dev *ca; |
b65db750 | 1072 | int ret = 0; |
1c6fdbd8 | 1073 | |
f0ac7df2 | 1074 | if (!bch2_dev_exists2(c, ptr->dev)) { |
e9679b4a KO |
1075 | /* |
1076 | * If we're in the write path this key might have already been | |
1077 | * overwritten, and we could be seeing a device that doesn't | |
1078 | * exist anymore due to racing with device removal: | |
1079 | */ | |
1080 | if (flags & BKEY_INVALID_WRITE) | |
1081 | return 0; | |
1082 | ||
b65db750 KO |
1083 | bkey_fsck_err(c, err, ptr_to_invalid_device, |
1084 | "pointer to invalid device (%u)", ptr->dev); | |
f0ac7df2 | 1085 | } |
a62c78a7 | 1086 | |
4de77495 | 1087 | ca = bch_dev_bkey_exists(c, ptr->dev); |
4de77495 | 1088 | bkey_for_each_ptr(ptrs, ptr2) |
b65db750 KO |
1089 | bkey_fsck_err_on(ptr != ptr2 && ptr->dev == ptr2->dev, c, err, |
1090 | ptr_to_duplicate_device, | |
1091 | "multiple pointers to same device (%u)", ptr->dev); | |
1c6fdbd8 | 1092 | |
f0ac7df2 | 1093 | bucket = sector_to_bucket_and_offset(ca, ptr->offset, &bucket_offset); |
1c6fdbd8 | 1094 | |
b65db750 KO |
1095 | bkey_fsck_err_on(bucket >= ca->mi.nbuckets, c, err, |
1096 | ptr_after_last_bucket, | |
1097 | "pointer past last bucket (%llu > %llu)", bucket, ca->mi.nbuckets); | |
1098 | bkey_fsck_err_on(ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket), c, err, | |
1099 | ptr_before_first_bucket, | |
1100 | "pointer before first bucket (%llu < %u)", bucket, ca->mi.first_bucket); | |
1101 | bkey_fsck_err_on(bucket_offset + size_ondisk > ca->mi.bucket_size, c, err, | |
1102 | ptr_spans_multiple_buckets, | |
1103 | "pointer spans multiple buckets (%u + %u > %u)", | |
f0ac7df2 | 1104 | bucket_offset, size_ondisk, ca->mi.bucket_size); |
b65db750 KO |
1105 | fsck_err: |
1106 | return ret; | |
4de77495 | 1107 | } |
1c6fdbd8 | 1108 | |
b65db750 | 1109 | int bch2_bkey_ptrs_invalid(struct bch_fs *c, struct bkey_s_c k, |
8726dc93 KO |
1110 | enum bkey_invalid_flags flags, |
1111 | struct printbuf *err) | |
4de77495 KO |
1112 | { |
1113 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
1114 | const union bch_extent_entry *entry; | |
1115 | struct bch_extent_crc_unpacked crc; | |
1116 | unsigned size_ondisk = k.k->size; | |
4de77495 | 1117 | unsigned nonce = UINT_MAX; |
b5f73fd7 | 1118 | unsigned nr_ptrs = 0; |
b65db750 KO |
1119 | bool have_written = false, have_unwritten = false, have_ec = false, crc_since_last_ptr = false; |
1120 | int ret = 0; | |
4de77495 | 1121 | |
f0ac7df2 | 1122 | if (bkey_is_btree_ptr(k.k)) |
8244f320 | 1123 | size_ondisk = btree_sectors(c); |
4de77495 KO |
1124 | |
1125 | bkey_extent_entry_for_each(ptrs, entry) { | |
b65db750 KO |
1126 | bkey_fsck_err_on(__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX, c, err, |
1127 | extent_ptrs_invalid_entry, | |
1128 | "invalid extent entry type (got %u, max %u)", | |
1129 | __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX); | |
6009b4e5 | 1130 | |
b65db750 KO |
1131 | bkey_fsck_err_on(bkey_is_btree_ptr(k.k) && |
1132 | !extent_entry_is_ptr(entry), c, err, | |
1133 | btree_ptr_has_non_ptr, | |
1134 | "has non ptr field"); | |
4de77495 KO |
1135 | |
1136 | switch (extent_entry_type(entry)) { | |
1137 | case BCH_EXTENT_ENTRY_ptr: | |
e9679b4a KO |
1138 | ret = extent_ptr_invalid(c, k, flags, &entry->ptr, |
1139 | size_ondisk, false, err); | |
f0ac7df2 KO |
1140 | if (ret) |
1141 | return ret; | |
79203111 | 1142 | |
b65db750 KO |
1143 | bkey_fsck_err_on(entry->ptr.cached && have_ec, c, err, |
1144 | ptr_cached_and_erasure_coded, | |
1145 | "cached, erasure coded ptr"); | |
79203111 | 1146 | |
b65db750 KO |
1147 | if (!entry->ptr.unwritten) |
1148 | have_written = true; | |
1149 | else | |
1150 | have_unwritten = true; | |
c9163bb0 | 1151 | |
c9163bb0 | 1152 | have_ec = false; |
43b0e878 | 1153 | crc_since_last_ptr = false; |
b5f73fd7 | 1154 | nr_ptrs++; |
6009b4e5 KO |
1155 | break; |
1156 | case BCH_EXTENT_ENTRY_crc32: | |
1157 | case BCH_EXTENT_ENTRY_crc64: | |
1158 | case BCH_EXTENT_ENTRY_crc128: | |
4de77495 | 1159 | crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry)); |
6009b4e5 | 1160 | |
b65db750 KO |
1161 | bkey_fsck_err_on(crc.offset + crc.live_size > crc.uncompressed_size, c, err, |
1162 | ptr_crc_uncompressed_size_too_small, | |
1163 | "checksum offset + key size > uncompressed size"); | |
1164 | bkey_fsck_err_on(!bch2_checksum_type_valid(c, crc.csum_type), c, err, | |
1165 | ptr_crc_csum_type_unknown, | |
1166 | "invalid checksum type"); | |
1167 | bkey_fsck_err_on(crc.compression_type >= BCH_COMPRESSION_TYPE_NR, c, err, | |
1168 | ptr_crc_compression_type_unknown, | |
1169 | "invalid compression type"); | |
6009b4e5 | 1170 | |
4de77495 KO |
1171 | if (bch2_csum_type_is_encryption(crc.csum_type)) { |
1172 | if (nonce == UINT_MAX) | |
1173 | nonce = crc.offset + crc.nonce; | |
b65db750 KO |
1174 | else if (nonce != crc.offset + crc.nonce) |
1175 | bkey_fsck_err(c, err, ptr_crc_nonce_mismatch, | |
1176 | "incorrect nonce"); | |
4de77495 | 1177 | } |
43b0e878 | 1178 | |
b65db750 KO |
1179 | bkey_fsck_err_on(crc_since_last_ptr, c, err, |
1180 | ptr_crc_redundant, | |
1181 | "redundant crc entry"); | |
43b0e878 | 1182 | crc_since_last_ptr = true; |
9db2f860 | 1183 | |
b65db750 KO |
1184 | bkey_fsck_err_on(crc_is_encoded(crc) && |
1185 | (crc.uncompressed_size > c->opts.encoded_extent_max >> 9) && | |
1186 | (flags & (BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT)), c, err, | |
1187 | ptr_crc_uncompressed_size_too_big, | |
1188 | "too large encoded extent"); | |
9db2f860 | 1189 | |
b65db750 | 1190 | size_ondisk = crc.compressed_size; |
4de77495 KO |
1191 | break; |
1192 | case BCH_EXTENT_ENTRY_stripe_ptr: | |
b65db750 KO |
1193 | bkey_fsck_err_on(have_ec, c, err, |
1194 | ptr_stripe_redundant, | |
1195 | "redundant stripe entry"); | |
c9163bb0 | 1196 | have_ec = true; |
6009b4e5 | 1197 | break; |
fb3f57bb KO |
1198 | case BCH_EXTENT_ENTRY_rebalance: { |
1199 | const struct bch_extent_rebalance *r = &entry->rebalance; | |
1200 | ||
1201 | if (!bch2_compression_opt_valid(r->compression)) { | |
1202 | struct bch_compression_opt opt = __bch2_compression_decode(r->compression); | |
1203 | prt_printf(err, "invalid compression opt %u:%u", | |
1204 | opt.type, opt.level); | |
1205 | return -BCH_ERR_invalid_bkey; | |
1206 | } | |
2766876d | 1207 | break; |
6009b4e5 | 1208 | } |
fb3f57bb | 1209 | } |
1c6fdbd8 KO |
1210 | } |
1211 | ||
b65db750 KO |
1212 | bkey_fsck_err_on(!nr_ptrs, c, err, |
1213 | extent_ptrs_no_ptrs, | |
1214 | "no ptrs"); | |
1215 | bkey_fsck_err_on(nr_ptrs > BCH_BKEY_PTRS_MAX, c, err, | |
1216 | extent_ptrs_too_many_ptrs, | |
1217 | "too many ptrs: %u > %u", nr_ptrs, BCH_BKEY_PTRS_MAX); | |
1218 | bkey_fsck_err_on(have_written && have_unwritten, c, err, | |
1219 | extent_ptrs_written_and_unwritten, | |
1220 | "extent with unwritten and written ptrs"); | |
1221 | bkey_fsck_err_on(k.k->type != KEY_TYPE_extent && have_unwritten, c, err, | |
1222 | extent_ptrs_unwritten, | |
1223 | "has unwritten ptrs"); | |
1224 | bkey_fsck_err_on(crc_since_last_ptr, c, err, | |
1225 | extent_ptrs_redundant_crc, | |
1226 | "redundant crc entry"); | |
1227 | bkey_fsck_err_on(have_ec, c, err, | |
1228 | extent_ptrs_redundant_stripe, | |
1229 | "redundant stripe entry"); | |
1230 | fsck_err: | |
1231 | return ret; | |
4de77495 | 1232 | } |
6009b4e5 | 1233 | |
1f49dafc | 1234 | void bch2_ptr_swab(struct bkey_s k) |
4de77495 | 1235 | { |
1f49dafc | 1236 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(k); |
4de77495 | 1237 | union bch_extent_entry *entry; |
1f49dafc | 1238 | u64 *d; |
6009b4e5 | 1239 | |
1f49dafc KO |
1240 | for (d = (u64 *) ptrs.start; |
1241 | d != (u64 *) ptrs.end; | |
1242 | d++) | |
1243 | *d = swab64(*d); | |
6009b4e5 | 1244 | |
1f49dafc KO |
1245 | for (entry = ptrs.start; |
1246 | entry < ptrs.end; | |
4de77495 KO |
1247 | entry = extent_entry_next(entry)) { |
1248 | switch (extent_entry_type(entry)) { | |
1249 | case BCH_EXTENT_ENTRY_ptr: | |
1250 | break; | |
1251 | case BCH_EXTENT_ENTRY_crc32: | |
1252 | entry->crc32.csum = swab32(entry->crc32.csum); | |
1253 | break; | |
1254 | case BCH_EXTENT_ENTRY_crc64: | |
1255 | entry->crc64.csum_hi = swab16(entry->crc64.csum_hi); | |
1256 | entry->crc64.csum_lo = swab64(entry->crc64.csum_lo); | |
1257 | break; | |
1258 | case BCH_EXTENT_ENTRY_crc128: | |
1259 | entry->crc128.csum.hi = (__force __le64) | |
1260 | swab64((__force u64) entry->crc128.csum.hi); | |
1261 | entry->crc128.csum.lo = (__force __le64) | |
1262 | swab64((__force u64) entry->crc128.csum.lo); | |
1263 | break; | |
1264 | case BCH_EXTENT_ENTRY_stripe_ptr: | |
1265 | break; | |
2766876d KO |
1266 | case BCH_EXTENT_ENTRY_rebalance: |
1267 | break; | |
4de77495 | 1268 | } |
1c6fdbd8 | 1269 | } |
1c6fdbd8 KO |
1270 | } |
1271 | ||
fb3f57bb KO |
1272 | const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c k) |
1273 | { | |
1274 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
1275 | const union bch_extent_entry *entry; | |
1276 | ||
1277 | bkey_extent_entry_for_each(ptrs, entry) | |
1278 | if (__extent_entry_type(entry) == BCH_EXTENT_ENTRY_rebalance) | |
1279 | return &entry->rebalance; | |
1280 | ||
1281 | return NULL; | |
1282 | } | |
1283 | ||
1284 | unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k, | |
1285 | unsigned target, unsigned compression) | |
1286 | { | |
1287 | struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); | |
1288 | unsigned rewrite_ptrs = 0; | |
1289 | ||
1290 | if (compression) { | |
1291 | unsigned compression_type = bch2_compression_opt_to_type(compression); | |
1292 | const union bch_extent_entry *entry; | |
1293 | struct extent_ptr_decoded p; | |
1294 | unsigned i = 0; | |
1295 | ||
1296 | bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { | |
e5972888 DH |
1297 | if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible || |
1298 | p.ptr.unwritten) { | |
fb3f57bb KO |
1299 | rewrite_ptrs = 0; |
1300 | goto incompressible; | |
1301 | } | |
1302 | ||
1303 | if (!p.ptr.cached && p.crc.compression_type != compression_type) | |
1304 | rewrite_ptrs |= 1U << i; | |
1305 | i++; | |
1306 | } | |
1307 | } | |
1308 | incompressible: | |
1309 | if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) { | |
1310 | const struct bch_extent_ptr *ptr; | |
1311 | unsigned i = 0; | |
1312 | ||
1313 | bkey_for_each_ptr(ptrs, ptr) { | |
1314 | if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, target)) | |
1315 | rewrite_ptrs |= 1U << i; | |
1316 | i++; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | return rewrite_ptrs; | |
1321 | } | |
1322 | ||
1323 | bool bch2_bkey_needs_rebalance(struct bch_fs *c, struct bkey_s_c k) | |
1324 | { | |
1325 | const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k); | |
1326 | ||
1327 | /* | |
1328 | * If it's an indirect extent, we don't delete the rebalance entry when | |
1329 | * done so that we know what options were applied - check if it still | |
1330 | * needs work done: | |
1331 | */ | |
1332 | if (r && | |
1333 | k.k->type == KEY_TYPE_reflink_v && | |
1334 | !bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression)) | |
1335 | r = NULL; | |
1336 | ||
1337 | return r != NULL; | |
1338 | } | |
1339 | ||
1340 | int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k, | |
1341 | unsigned target, unsigned compression) | |
1342 | { | |
1343 | struct bkey_s k = bkey_i_to_s(_k); | |
1344 | struct bch_extent_rebalance *r; | |
1345 | bool needs_rebalance; | |
1346 | ||
1347 | if (!bkey_extent_is_direct_data(k.k)) | |
1348 | return 0; | |
1349 | ||
1350 | /* get existing rebalance entry: */ | |
1351 | r = (struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c); | |
1352 | if (r) { | |
1353 | if (k.k->type == KEY_TYPE_reflink_v) { | |
1354 | /* | |
1355 | * indirect extents: existing options take precedence, | |
1356 | * so that we don't move extents back and forth if | |
1357 | * they're referenced by different inodes with different | |
1358 | * options: | |
1359 | */ | |
1360 | if (r->target) | |
1361 | target = r->target; | |
1362 | if (r->compression) | |
1363 | compression = r->compression; | |
1364 | } | |
1365 | ||
1366 | r->target = target; | |
1367 | r->compression = compression; | |
1368 | } | |
1369 | ||
1370 | needs_rebalance = bch2_bkey_ptrs_need_rebalance(c, k.s_c, target, compression); | |
1371 | ||
1372 | if (needs_rebalance && !r) { | |
1373 | union bch_extent_entry *new = bkey_val_end(k); | |
1374 | ||
1375 | new->rebalance.type = 1U << BCH_EXTENT_ENTRY_rebalance; | |
1376 | new->rebalance.compression = compression; | |
1377 | new->rebalance.target = target; | |
1378 | new->rebalance.unused = 0; | |
1379 | k.k->u64s += extent_entry_u64s(new); | |
1380 | } else if (!needs_rebalance && r && k.k->type != KEY_TYPE_reflink_v) { | |
1381 | /* | |
1382 | * For indirect extents, don't delete the rebalance entry when | |
1383 | * we're finished so that we know we specifically moved it or | |
1384 | * compressed it to its current location/compression type | |
1385 | */ | |
1386 | extent_entry_drop(k, (union bch_extent_entry *) r); | |
1387 | } | |
1388 | ||
1389 | return 0; | |
1390 | } | |
1391 | ||
4de77495 KO |
1392 | /* Generic extent code: */ |
1393 | ||
1394 | int bch2_cut_front_s(struct bpos where, struct bkey_s k) | |
1c6fdbd8 | 1395 | { |
4de77495 KO |
1396 | unsigned new_val_u64s = bkey_val_u64s(k.k); |
1397 | int val_u64s_delta; | |
1398 | u64 sub; | |
1c6fdbd8 | 1399 | |
e88a75eb | 1400 | if (bkey_le(where, bkey_start_pos(k.k))) |
4de77495 | 1401 | return 0; |
1c6fdbd8 | 1402 | |
e88a75eb | 1403 | EBUG_ON(bkey_gt(where, k.k->p)); |
424eb881 | 1404 | |
4de77495 | 1405 | sub = where.offset - bkey_start_offset(k.k); |
1c6fdbd8 | 1406 | |
4de77495 KO |
1407 | k.k->size -= sub; |
1408 | ||
1409 | if (!k.k->size) { | |
1410 | k.k->type = KEY_TYPE_deleted; | |
1411 | new_val_u64s = 0; | |
1c6fdbd8 | 1412 | } |
1c6fdbd8 | 1413 | |
4de77495 KO |
1414 | switch (k.k->type) { |
1415 | case KEY_TYPE_extent: | |
1416 | case KEY_TYPE_reflink_v: { | |
1417 | struct bkey_ptrs ptrs = bch2_bkey_ptrs(k); | |
1418 | union bch_extent_entry *entry; | |
1419 | bool seen_crc = false; | |
26609b61 | 1420 | |
4de77495 KO |
1421 | bkey_extent_entry_for_each(ptrs, entry) { |
1422 | switch (extent_entry_type(entry)) { | |
1423 | case BCH_EXTENT_ENTRY_ptr: | |
1424 | if (!seen_crc) | |
1425 | entry->ptr.offset += sub; | |
1426 | break; | |
1427 | case BCH_EXTENT_ENTRY_crc32: | |
1428 | entry->crc32.offset += sub; | |
1429 | break; | |
1430 | case BCH_EXTENT_ENTRY_crc64: | |
1431 | entry->crc64.offset += sub; | |
1432 | break; | |
1433 | case BCH_EXTENT_ENTRY_crc128: | |
1434 | entry->crc128.offset += sub; | |
1435 | break; | |
1436 | case BCH_EXTENT_ENTRY_stripe_ptr: | |
1437 | break; | |
2766876d KO |
1438 | case BCH_EXTENT_ENTRY_rebalance: |
1439 | break; | |
4de77495 | 1440 | } |
7ef2a73a | 1441 | |
4de77495 KO |
1442 | if (extent_entry_is_crc(entry)) |
1443 | seen_crc = true; | |
1444 | } | |
7ef2a73a | 1445 | |
7ef2a73a KO |
1446 | break; |
1447 | } | |
4de77495 KO |
1448 | case KEY_TYPE_reflink_p: { |
1449 | struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k); | |
1450 | ||
1451 | le64_add_cpu(&p.v->idx, sub); | |
7ef2a73a KO |
1452 | break; |
1453 | } | |
801a3de6 KO |
1454 | case KEY_TYPE_inline_data: |
1455 | case KEY_TYPE_indirect_inline_data: { | |
1456 | void *p = bkey_inline_data_p(k); | |
1457 | unsigned bytes = bkey_inline_data_bytes(k.k); | |
7ef2a73a | 1458 | |
801a3de6 | 1459 | sub = min_t(u64, sub << 9, bytes); |
26609b61 | 1460 | |
801a3de6 | 1461 | memmove(p, p + sub, bytes - sub); |
26609b61 | 1462 | |
4de77495 KO |
1463 | new_val_u64s -= sub >> 3; |
1464 | break; | |
1465 | } | |
1466 | } | |
26609b61 | 1467 | |
4de77495 KO |
1468 | val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s; |
1469 | BUG_ON(val_u64s_delta < 0); | |
26609b61 | 1470 | |
4de77495 KO |
1471 | set_bkey_val_u64s(k.k, new_val_u64s); |
1472 | memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64)); | |
1473 | return -val_u64s_delta; | |
26609b61 KO |
1474 | } |
1475 | ||
4de77495 | 1476 | int bch2_cut_back_s(struct bpos where, struct bkey_s k) |
26609b61 | 1477 | { |
4de77495 KO |
1478 | unsigned new_val_u64s = bkey_val_u64s(k.k); |
1479 | int val_u64s_delta; | |
1480 | u64 len = 0; | |
26609b61 | 1481 | |
e88a75eb | 1482 | if (bkey_ge(where, k.k->p)) |
4de77495 | 1483 | return 0; |
26609b61 | 1484 | |
e88a75eb | 1485 | EBUG_ON(bkey_lt(where, bkey_start_pos(k.k))); |
26609b61 | 1486 | |
4de77495 | 1487 | len = where.offset - bkey_start_offset(k.k); |
26609b61 | 1488 | |
d361a26d | 1489 | k.k->p.offset = where.offset; |
4de77495 KO |
1490 | k.k->size = len; |
1491 | ||
1492 | if (!len) { | |
1493 | k.k->type = KEY_TYPE_deleted; | |
1494 | new_val_u64s = 0; | |
26609b61 KO |
1495 | } |
1496 | ||
4de77495 KO |
1497 | switch (k.k->type) { |
1498 | case KEY_TYPE_inline_data: | |
801a3de6 KO |
1499 | case KEY_TYPE_indirect_inline_data: |
1500 | new_val_u64s = (bkey_inline_data_offset(k.k) + | |
1501 | min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3; | |
4de77495 KO |
1502 | break; |
1503 | } | |
26609b61 | 1504 | |
4de77495 KO |
1505 | val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s; |
1506 | BUG_ON(val_u64s_delta < 0); | |
1507 | ||
1508 | set_bkey_val_u64s(k.k, new_val_u64s); | |
1509 | memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64)); | |
1510 | return -val_u64s_delta; | |
26609b61 | 1511 | } |