]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/data_update.c
Update bcachefs sources to 0342eebf85 bcachefs: Improve the backpointer to missing...
[bcachefs-tools-debian] / libbcachefs / data_update.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "alloc_foreground.h"
5 #include "bkey_buf.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "data_update.h"
9 #include "ec.h"
10 #include "extents.h"
11 #include "io.h"
12 #include "keylist.h"
13 #include "move.h"
14 #include "nocow_locking.h"
15 #include "subvolume.h"
16
17 #include <trace/events/bcachefs.h>
18
19 static int insert_snapshot_whiteouts(struct btree_trans *trans,
20                                      enum btree_id id,
21                                      struct bpos old_pos,
22                                      struct bpos new_pos)
23 {
24         struct bch_fs *c = trans->c;
25         struct btree_iter iter, iter2;
26         struct bkey_s_c k, k2;
27         snapshot_id_list s;
28         struct bkey_i *update;
29         int ret;
30
31         if (!btree_type_has_snapshots(id))
32                 return 0;
33
34         darray_init(&s);
35
36         if (!bch2_snapshot_has_children(c, old_pos.snapshot))
37                 return 0;
38
39         bch2_trans_iter_init(trans, &iter, id, old_pos,
40                              BTREE_ITER_NOT_EXTENTS|
41                              BTREE_ITER_ALL_SNAPSHOTS);
42         while (1) {
43                 k = bch2_btree_iter_prev(&iter);
44                 ret = bkey_err(k);
45                 if (ret)
46                         break;
47
48                 if (!k.k)
49                         break;
50
51                 if (!bkey_eq(old_pos, k.k->p))
52                         break;
53
54                 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot) &&
55                     !snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) {
56                         struct bpos whiteout_pos = new_pos;
57
58                         whiteout_pos.snapshot = k.k->p.snapshot;
59
60                         bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
61                                              BTREE_ITER_NOT_EXTENTS|
62                                              BTREE_ITER_INTENT);
63                         k2 = bch2_btree_iter_peek_slot(&iter2);
64                         ret = bkey_err(k2);
65
66                         if (!ret && k2.k->type == KEY_TYPE_deleted) {
67                                 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
68                                 ret = PTR_ERR_OR_ZERO(update);
69                                 if (ret)
70                                         break;
71
72                                 bkey_init(&update->k);
73                                 update->k.p             = whiteout_pos;
74                                 update->k.type          = KEY_TYPE_whiteout;
75
76                                 ret = bch2_trans_update(trans, &iter2, update,
77                                                         BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
78                         }
79                         bch2_trans_iter_exit(trans, &iter2);
80
81                         if (ret)
82                                 break;
83
84                         ret = snapshot_list_add(c, &s, k.k->p.snapshot);
85                         if (ret)
86                                 break;
87                 }
88         }
89         bch2_trans_iter_exit(trans, &iter);
90         darray_exit(&s);
91
92         return ret;
93 }
94
95 static int __bch2_data_update_index_update(struct btree_trans *trans,
96                                            struct bch_write_op *op)
97 {
98         struct bch_fs *c = op->c;
99         struct btree_iter iter;
100         struct data_update *m =
101                 container_of(op, struct data_update, op);
102         struct keylist *keys = &op->insert_keys;
103         struct bkey_buf _new, _insert;
104         int ret = 0;
105
106         bch2_bkey_buf_init(&_new);
107         bch2_bkey_buf_init(&_insert);
108         bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
109
110         bch2_trans_iter_init(trans, &iter, m->btree_id,
111                              bkey_start_pos(&bch2_keylist_front(keys)->k),
112                              BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
113
114         while (1) {
115                 struct bkey_s_c k;
116                 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
117                 struct bkey_i *insert = NULL;
118                 struct bkey_i_extent *new;
119                 const union bch_extent_entry *entry_c;
120                 union bch_extent_entry *entry;
121                 struct extent_ptr_decoded p;
122                 struct bch_extent_ptr *ptr;
123                 const struct bch_extent_ptr *ptr_c;
124                 struct bpos next_pos;
125                 bool should_check_enospc;
126                 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
127                 unsigned rewrites_found = 0, durability, i;
128
129                 bch2_trans_begin(trans);
130
131                 k = bch2_btree_iter_peek_slot(&iter);
132                 ret = bkey_err(k);
133                 if (ret)
134                         goto err;
135
136                 new = bkey_i_to_extent(bch2_keylist_front(keys));
137
138                 if (!bch2_extents_match(k, old))
139                         goto nowork;
140
141                 bkey_reassemble(_insert.k, k);
142                 insert = _insert.k;
143
144                 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
145                 new = bkey_i_to_extent(_new.k);
146                 bch2_cut_front(iter.pos, &new->k_i);
147
148                 bch2_cut_front(iter.pos,        insert);
149                 bch2_cut_back(new->k.p,         insert);
150                 bch2_cut_back(insert->k.p,      &new->k_i);
151
152                 /*
153                  * @old: extent that we read from
154                  * @insert: key that we're going to update, initialized from
155                  * extent currently in btree - same as @old unless we raced with
156                  * other updates
157                  * @new: extent with new pointers that we'll be adding to @insert
158                  *
159                  * Fist, drop rewrite_ptrs from @new:
160                  */
161                 i = 0;
162                 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
163                         if (((1U << i) & m->data_opts.rewrite_ptrs) &&
164                             (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
165                             !ptr->cached) {
166                                 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
167                                 rewrites_found |= 1U << i;
168                         }
169                         i++;
170                 }
171
172                 if (m->data_opts.rewrite_ptrs &&
173                     !rewrites_found &&
174                     bch2_bkey_durability(c, k) >= m->op.opts.data_replicas)
175                         goto nowork;
176
177                 /*
178                  * A replica that we just wrote might conflict with a replica
179                  * that we want to keep, due to racing with another move:
180                  */
181 restart_drop_conflicting_replicas:
182                 extent_for_each_ptr(extent_i_to_s(new), ptr)
183                         if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
184                             !ptr_c->cached) {
185                                 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
186                                 goto restart_drop_conflicting_replicas;
187                         }
188
189                 if (!bkey_val_u64s(&new->k))
190                         goto nowork;
191
192                 /* Now, drop pointers that conflict with what we just wrote: */
193                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
194                         if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
195                                 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
196
197                 durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
198                         bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
199
200                 /* Now, drop excess replicas: */
201 restart_drop_extra_replicas:
202                 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
203                         unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
204
205                         if (!p.ptr.cached &&
206                             durability - ptr_durability >= m->op.opts.data_replicas) {
207                                 durability -= ptr_durability;
208                                 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
209                                 goto restart_drop_extra_replicas;
210                         }
211                 }
212
213                 /* Finally, add the pointers we just wrote: */
214                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
215                         bch2_extent_ptr_decoded_append(insert, &p);
216
217                 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
218                 bch2_extent_normalize(c, bkey_i_to_s(insert));
219
220                 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
221                                                  &should_check_enospc,
222                                                  &i_sectors_delta,
223                                                  &disk_sectors_delta);
224                 if (ret)
225                         goto err;
226
227                 if (disk_sectors_delta > (s64) op->res.sectors) {
228                         ret = bch2_disk_reservation_add(c, &op->res,
229                                                 disk_sectors_delta - op->res.sectors,
230                                                 !should_check_enospc
231                                                 ? BCH_DISK_RESERVATION_NOFAIL : 0);
232                         if (ret)
233                                 goto out;
234                 }
235
236                 next_pos = insert->k.p;
237
238                 if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
239                         ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p,
240                                                         bkey_start_pos(&insert->k));
241                         if (ret)
242                                 goto err;
243                 }
244
245                 if (!bkey_eq(insert->k.p, k.k->p)) {
246                         ret = insert_snapshot_whiteouts(trans, m->btree_id,
247                                                         k.k->p, insert->k.p);
248                         if (ret)
249                                 goto err;
250                 }
251
252                 ret   = bch2_trans_update(trans, &iter, insert,
253                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
254                         bch2_trans_commit(trans, &op->res,
255                                 NULL,
256                                 BTREE_INSERT_NOCHECK_RW|
257                                 BTREE_INSERT_NOFAIL|
258                                 m->data_opts.btree_insert_flags);
259                 if (!ret) {
260                         bch2_btree_iter_set_pos(&iter, next_pos);
261
262                         this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
263                         trace_move_extent_finish(&new->k);
264                 }
265 err:
266                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
267                         ret = 0;
268                 if (ret)
269                         break;
270 next:
271                 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
272                         bch2_keylist_pop_front(keys);
273                         if (bch2_keylist_empty(keys))
274                                 goto out;
275                 }
276                 continue;
277 nowork:
278                 if (m->ctxt && m->ctxt->stats) {
279                         BUG_ON(k.k->p.offset <= iter.pos.offset);
280                         atomic64_inc(&m->ctxt->stats->keys_raced);
281                         atomic64_add(k.k->p.offset - iter.pos.offset,
282                                      &m->ctxt->stats->sectors_raced);
283                 }
284
285                 this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
286                 trace_move_extent_fail(&new->k);
287
288                 bch2_btree_iter_advance(&iter);
289                 goto next;
290         }
291 out:
292         bch2_trans_iter_exit(trans, &iter);
293         bch2_bkey_buf_exit(&_insert, c);
294         bch2_bkey_buf_exit(&_new, c);
295         BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
296         return ret;
297 }
298
299 int bch2_data_update_index_update(struct bch_write_op *op)
300 {
301         return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
302 }
303
304 void bch2_data_update_read_done(struct data_update *m,
305                                 struct bch_extent_crc_unpacked crc)
306 {
307         /* write bio must own pages: */
308         BUG_ON(!m->op.wbio.bio.bi_vcnt);
309
310         m->op.crc = crc;
311         m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
312
313         closure_call(&m->op.cl, bch2_write, NULL, NULL);
314 }
315
316 void bch2_data_update_exit(struct data_update *update)
317 {
318         struct bch_fs *c = update->op.c;
319         struct bkey_ptrs_c ptrs =
320                 bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
321         const struct bch_extent_ptr *ptr;
322
323         bkey_for_each_ptr(ptrs, ptr) {
324                 if (c->opts.nocow_enabled)
325                         bch2_bucket_nocow_unlock(&c->nocow_locks,
326                                                  PTR_BUCKET_POS(c, ptr), 0);
327                 percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
328         }
329
330         bch2_bkey_buf_exit(&update->k, c);
331         bch2_disk_reservation_put(c, &update->op.res);
332         bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
333 }
334
335 void bch2_update_unwritten_extent(struct btree_trans *trans,
336                                   struct data_update *update)
337 {
338         struct bch_fs *c = update->op.c;
339         struct bio *bio = &update->op.wbio.bio;
340         struct bkey_i_extent *e;
341         struct write_point *wp;
342         struct bch_extent_ptr *ptr;
343         struct closure cl;
344         struct btree_iter iter;
345         struct bkey_s_c k;
346         int ret;
347
348         closure_init_stack(&cl);
349         bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
350
351         while (bio_sectors(bio)) {
352                 unsigned sectors = bio_sectors(bio);
353
354                 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
355                                      BTREE_ITER_SLOTS);
356                 ret = lockrestart_do(trans, ({
357                         k = bch2_btree_iter_peek_slot(&iter);
358                         bkey_err(k);
359                 }));
360                 bch2_trans_iter_exit(trans, &iter);
361
362                 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
363                         break;
364
365                 e = bkey_extent_init(update->op.insert_keys.top);
366                 e->k.p = update->op.pos;
367
368                 ret = bch2_alloc_sectors_start_trans(trans,
369                                 update->op.target,
370                                 false,
371                                 update->op.write_point,
372                                 &update->op.devs_have,
373                                 update->op.nr_replicas,
374                                 update->op.nr_replicas,
375                                 update->op.alloc_reserve,
376                                 0, &cl, &wp);
377                 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
378                         bch2_trans_unlock(trans);
379                         closure_sync(&cl);
380                         continue;
381                 }
382
383                 if (ret)
384                         return;
385
386                 sectors = min(sectors, wp->sectors_free);
387
388                 bch2_key_resize(&e->k, sectors);
389
390                 bch2_open_bucket_get(c, wp, &update->op.open_buckets);
391                 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
392                 bch2_alloc_sectors_done(c, wp);
393
394                 bio_advance(bio, sectors << 9);
395                 update->op.pos.offset += sectors;
396
397                 extent_for_each_ptr(extent_i_to_s(e), ptr)
398                         ptr->unwritten = true;
399                 bch2_keylist_push(&update->op.insert_keys);
400
401                 ret = __bch2_data_update_index_update(trans, &update->op);
402
403                 bch2_open_buckets_put(c, &update->op.open_buckets);
404
405                 if (ret)
406                         break;
407         }
408
409         if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
410                 bch2_trans_unlock(trans);
411                 closure_sync(&cl);
412         }
413 }
414
415 int bch2_data_update_init(struct btree_trans *trans,
416                           struct moving_context *ctxt,
417                           struct data_update *m,
418                           struct write_point_specifier wp,
419                           struct bch_io_opts io_opts,
420                           struct data_update_opts data_opts,
421                           enum btree_id btree_id,
422                           struct bkey_s_c k)
423 {
424         struct bch_fs *c = trans->c;
425         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
426         const union bch_extent_entry *entry;
427         struct extent_ptr_decoded p;
428         const struct bch_extent_ptr *ptr;
429         unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
430         unsigned ptrs_locked = 0;
431         int ret;
432
433         bch2_bkey_buf_init(&m->k);
434         bch2_bkey_buf_reassemble(&m->k, c, k);
435         m->btree_id     = btree_id;
436         m->data_opts    = data_opts;
437
438         bch2_write_op_init(&m->op, c, io_opts);
439         m->op.pos       = bkey_start_pos(k.k);
440         m->op.version   = k.k->version;
441         m->op.target    = data_opts.target;
442         m->op.write_point = wp;
443         m->op.nr_replicas = 0;
444         m->op.flags     |= BCH_WRITE_PAGES_STABLE|
445                 BCH_WRITE_PAGES_OWNED|
446                 BCH_WRITE_DATA_ENCODED|
447                 BCH_WRITE_MOVE|
448                 m->data_opts.write_flags;
449         m->op.compression_type =
450                 bch2_compression_opt_to_type[io_opts.background_compression ?:
451                                              io_opts.compression];
452         if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
453                 m->op.alloc_reserve = RESERVE_movinggc;
454
455         bkey_for_each_ptr(ptrs, ptr)
456                 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
457
458         i = 0;
459         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
460                 bool locked;
461
462                 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
463                         BUG_ON(p.ptr.cached);
464
465                         if (crc_is_compressed(p.crc))
466                                 reserve_sectors += k.k->size;
467
468                         m->op.nr_replicas += bch2_extent_ptr_durability(c, &p);
469                 } else if (!p.ptr.cached) {
470                         bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
471                 }
472
473                 /*
474                  * op->csum_type is normally initialized from the fs/file's
475                  * current options - but if an extent is encrypted, we require
476                  * that it stays encrypted:
477                  */
478                 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
479                         m->op.nonce     = p.crc.nonce + p.crc.offset;
480                         m->op.csum_type = p.crc.csum_type;
481                 }
482
483                 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
484                         m->op.incompressible = true;
485
486                 if (c->opts.nocow_enabled) {
487                         if (ctxt) {
488                                 move_ctxt_wait_event(ctxt, trans,
489                                                 (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
490                                                                           PTR_BUCKET_POS(c, &p.ptr), 0)) ||
491                                                 !atomic_read(&ctxt->read_sectors));
492
493                                 if (!locked)
494                                         bch2_bucket_nocow_lock(&c->nocow_locks,
495                                                                PTR_BUCKET_POS(c, &p.ptr), 0);
496                         } else {
497                                 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
498                                                                PTR_BUCKET_POS(c, &p.ptr), 0)) {
499                                         ret = -BCH_ERR_nocow_lock_blocked;
500                                         goto err;
501                                 }
502                         }
503                         ptrs_locked |= (1U << i);
504                 }
505
506                 i++;
507         }
508
509         if (reserve_sectors) {
510                 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
511                                 m->data_opts.extra_replicas
512                                 ? 0
513                                 : BCH_DISK_RESERVATION_NOFAIL);
514                 if (ret)
515                         goto err;
516         }
517
518         m->op.nr_replicas += m->data_opts.extra_replicas;
519         m->op.nr_replicas_required = m->op.nr_replicas;
520
521         BUG_ON(!m->op.nr_replicas);
522
523         /* Special handling required: */
524         if (bkey_extent_is_unwritten(k))
525                 return -BCH_ERR_unwritten_extent_update;
526         return 0;
527 err:
528         i = 0;
529         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
530                 if ((1U << i) & ptrs_locked)
531                         bch2_bucket_nocow_unlock(&c->nocow_locks,
532                                                  PTR_BUCKET_POS(c, &p.ptr), 0);
533                 percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
534                 i++;
535         }
536
537         bch2_bkey_buf_exit(&m->k, c);
538         bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
539         return ret;
540 }
541
542 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
543 {
544         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
545         const struct bch_extent_ptr *ptr;
546         unsigned i = 0;
547
548         bkey_for_each_ptr(ptrs, ptr) {
549                 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
550                         opts->kill_ptrs |= 1U << i;
551                         opts->rewrite_ptrs ^= 1U << i;
552                 }
553
554                 i++;
555         }
556 }