]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/data_update.c
de808fcc64f11f3773239c3277827c336fdf5d7c
[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_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
167                                 /*
168                                  * See comment below:
169                                 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
170                                 */
171                                 rewrites_found |= 1U << i;
172                         }
173                         i++;
174                 }
175
176                 if (m->data_opts.rewrite_ptrs &&
177                     !rewrites_found &&
178                     bch2_bkey_durability(c, k) >= m->op.opts.data_replicas)
179                         goto nowork;
180
181                 /*
182                  * A replica that we just wrote might conflict with a replica
183                  * that we want to keep, due to racing with another move:
184                  */
185 restart_drop_conflicting_replicas:
186                 extent_for_each_ptr(extent_i_to_s(new), ptr)
187                         if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
188                             !ptr_c->cached) {
189                                 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
190                                 goto restart_drop_conflicting_replicas;
191                         }
192
193                 if (!bkey_val_u64s(&new->k))
194                         goto nowork;
195
196                 /* Now, drop pointers that conflict with what we just wrote: */
197                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
198                         if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
199                                 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
200
201                 durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
202                         bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
203
204                 /* Now, drop excess replicas: */
205 restart_drop_extra_replicas:
206                 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
207                         unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
208
209                         if (!p.ptr.cached &&
210                             durability - ptr_durability >= m->op.opts.data_replicas) {
211                                 durability -= ptr_durability;
212                                 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), &entry->ptr);
213                                 /*
214                                  * Currently, we're dropping unneeded replicas
215                                  * instead of marking them as cached, since
216                                  * cached data in stripe buckets prevents them
217                                  * from being reused:
218                                 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
219                                 */
220                                 goto restart_drop_extra_replicas;
221                         }
222                 }
223
224                 /* Finally, add the pointers we just wrote: */
225                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
226                         bch2_extent_ptr_decoded_append(insert, &p);
227
228                 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
229                 bch2_extent_normalize(c, bkey_i_to_s(insert));
230
231                 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
232                                                  &should_check_enospc,
233                                                  &i_sectors_delta,
234                                                  &disk_sectors_delta);
235                 if (ret)
236                         goto err;
237
238                 if (disk_sectors_delta > (s64) op->res.sectors) {
239                         ret = bch2_disk_reservation_add(c, &op->res,
240                                                 disk_sectors_delta - op->res.sectors,
241                                                 !should_check_enospc
242                                                 ? BCH_DISK_RESERVATION_NOFAIL : 0);
243                         if (ret)
244                                 goto out;
245                 }
246
247                 next_pos = insert->k.p;
248
249                 if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
250                         ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p,
251                                                         bkey_start_pos(&insert->k));
252                         if (ret)
253                                 goto err;
254                 }
255
256                 if (!bkey_eq(insert->k.p, k.k->p)) {
257                         ret = insert_snapshot_whiteouts(trans, m->btree_id,
258                                                         k.k->p, insert->k.p);
259                         if (ret)
260                                 goto err;
261                 }
262
263                 ret   = bch2_trans_update(trans, &iter, insert,
264                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
265                         bch2_trans_commit(trans, &op->res,
266                                 NULL,
267                                 BTREE_INSERT_NOCHECK_RW|
268                                 BTREE_INSERT_NOFAIL|
269                                 m->data_opts.btree_insert_flags);
270                 if (!ret) {
271                         bch2_btree_iter_set_pos(&iter, next_pos);
272
273                         this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
274                         trace_move_extent_finish(&new->k);
275                 }
276 err:
277                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
278                         ret = 0;
279                 if (ret)
280                         break;
281 next:
282                 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
283                         bch2_keylist_pop_front(keys);
284                         if (bch2_keylist_empty(keys))
285                                 goto out;
286                 }
287                 continue;
288 nowork:
289                 if (m->ctxt && m->ctxt->stats) {
290                         BUG_ON(k.k->p.offset <= iter.pos.offset);
291                         atomic64_inc(&m->ctxt->stats->keys_raced);
292                         atomic64_add(k.k->p.offset - iter.pos.offset,
293                                      &m->ctxt->stats->sectors_raced);
294                 }
295
296                 this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
297                 trace_move_extent_fail(&new->k);
298
299                 bch2_btree_iter_advance(&iter);
300                 goto next;
301         }
302 out:
303         bch2_trans_iter_exit(trans, &iter);
304         bch2_bkey_buf_exit(&_insert, c);
305         bch2_bkey_buf_exit(&_new, c);
306         BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
307         return ret;
308 }
309
310 int bch2_data_update_index_update(struct bch_write_op *op)
311 {
312         return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
313 }
314
315 void bch2_data_update_read_done(struct data_update *m,
316                                 struct bch_extent_crc_unpacked crc)
317 {
318         /* write bio must own pages: */
319         BUG_ON(!m->op.wbio.bio.bi_vcnt);
320
321         m->op.crc = crc;
322         m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
323
324         closure_call(&m->op.cl, bch2_write, NULL, NULL);
325 }
326
327 void bch2_data_update_exit(struct data_update *update)
328 {
329         struct bch_fs *c = update->op.c;
330         struct bkey_ptrs_c ptrs =
331                 bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
332         const struct bch_extent_ptr *ptr;
333
334         bkey_for_each_ptr(ptrs, ptr) {
335                 if (c->opts.nocow_enabled)
336                         bch2_bucket_nocow_unlock(&c->nocow_locks,
337                                                  PTR_BUCKET_POS(c, ptr), 0);
338                 percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
339         }
340
341         bch2_bkey_buf_exit(&update->k, c);
342         bch2_disk_reservation_put(c, &update->op.res);
343         bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
344 }
345
346 void bch2_update_unwritten_extent(struct btree_trans *trans,
347                                   struct data_update *update)
348 {
349         struct bch_fs *c = update->op.c;
350         struct bio *bio = &update->op.wbio.bio;
351         struct bkey_i_extent *e;
352         struct write_point *wp;
353         struct bch_extent_ptr *ptr;
354         struct closure cl;
355         struct btree_iter iter;
356         struct bkey_s_c k;
357         int ret;
358
359         closure_init_stack(&cl);
360         bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
361
362         while (bio_sectors(bio)) {
363                 unsigned sectors = bio_sectors(bio);
364
365                 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
366                                      BTREE_ITER_SLOTS);
367                 ret = lockrestart_do(trans, ({
368                         k = bch2_btree_iter_peek_slot(&iter);
369                         bkey_err(k);
370                 }));
371                 bch2_trans_iter_exit(trans, &iter);
372
373                 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
374                         break;
375
376                 e = bkey_extent_init(update->op.insert_keys.top);
377                 e->k.p = update->op.pos;
378
379                 ret = bch2_alloc_sectors_start_trans(trans,
380                                 update->op.target,
381                                 false,
382                                 update->op.write_point,
383                                 &update->op.devs_have,
384                                 update->op.nr_replicas,
385                                 update->op.nr_replicas,
386                                 update->op.alloc_reserve,
387                                 0, &cl, &wp);
388                 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
389                         bch2_trans_unlock(trans);
390                         closure_sync(&cl);
391                         continue;
392                 }
393
394                 if (ret)
395                         return;
396
397                 sectors = min(sectors, wp->sectors_free);
398
399                 bch2_key_resize(&e->k, sectors);
400
401                 bch2_open_bucket_get(c, wp, &update->op.open_buckets);
402                 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
403                 bch2_alloc_sectors_done(c, wp);
404
405                 bio_advance(bio, sectors << 9);
406                 update->op.pos.offset += sectors;
407
408                 extent_for_each_ptr(extent_i_to_s(e), ptr)
409                         ptr->unwritten = true;
410                 bch2_keylist_push(&update->op.insert_keys);
411
412                 ret = __bch2_data_update_index_update(trans, &update->op);
413
414                 bch2_open_buckets_put(c, &update->op.open_buckets);
415
416                 if (ret)
417                         break;
418         }
419
420         if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
421                 bch2_trans_unlock(trans);
422                 closure_sync(&cl);
423         }
424 }
425
426 int bch2_data_update_init(struct btree_trans *trans,
427                           struct moving_context *ctxt,
428                           struct data_update *m,
429                           struct write_point_specifier wp,
430                           struct bch_io_opts io_opts,
431                           struct data_update_opts data_opts,
432                           enum btree_id btree_id,
433                           struct bkey_s_c k)
434 {
435         struct bch_fs *c = trans->c;
436         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
437         const union bch_extent_entry *entry;
438         struct extent_ptr_decoded p;
439         const struct bch_extent_ptr *ptr;
440         unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
441         unsigned ptrs_locked = 0;
442         int ret;
443
444         bch2_bkey_buf_init(&m->k);
445         bch2_bkey_buf_reassemble(&m->k, c, k);
446         m->btree_id     = btree_id;
447         m->data_opts    = data_opts;
448
449         bch2_write_op_init(&m->op, c, io_opts);
450         m->op.pos       = bkey_start_pos(k.k);
451         m->op.version   = k.k->version;
452         m->op.target    = data_opts.target;
453         m->op.write_point = wp;
454         m->op.nr_replicas = 0;
455         m->op.flags     |= BCH_WRITE_PAGES_STABLE|
456                 BCH_WRITE_PAGES_OWNED|
457                 BCH_WRITE_DATA_ENCODED|
458                 BCH_WRITE_MOVE|
459                 m->data_opts.write_flags;
460         m->op.compression_type =
461                 bch2_compression_opt_to_type[io_opts.background_compression ?:
462                                              io_opts.compression];
463         if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
464                 m->op.alloc_reserve = RESERVE_movinggc;
465
466         bkey_for_each_ptr(ptrs, ptr)
467                 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
468
469         i = 0;
470         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
471                 bool locked;
472
473                 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
474                         BUG_ON(p.ptr.cached);
475
476                         if (crc_is_compressed(p.crc))
477                                 reserve_sectors += k.k->size;
478
479                         m->op.nr_replicas += bch2_extent_ptr_durability(c, &p);
480                 } else if (!p.ptr.cached) {
481                         bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
482                 }
483
484                 /*
485                  * op->csum_type is normally initialized from the fs/file's
486                  * current options - but if an extent is encrypted, we require
487                  * that it stays encrypted:
488                  */
489                 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
490                         m->op.nonce     = p.crc.nonce + p.crc.offset;
491                         m->op.csum_type = p.crc.csum_type;
492                 }
493
494                 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
495                         m->op.incompressible = true;
496
497                 if (c->opts.nocow_enabled) {
498                         if (ctxt) {
499                                 move_ctxt_wait_event(ctxt, trans,
500                                                 (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
501                                                                           PTR_BUCKET_POS(c, &p.ptr), 0)) ||
502                                                 !atomic_read(&ctxt->read_sectors));
503
504                                 if (!locked)
505                                         bch2_bucket_nocow_lock(&c->nocow_locks,
506                                                                PTR_BUCKET_POS(c, &p.ptr), 0);
507                         } else {
508                                 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
509                                                                PTR_BUCKET_POS(c, &p.ptr), 0)) {
510                                         ret = -BCH_ERR_nocow_lock_blocked;
511                                         goto err;
512                                 }
513                         }
514                         ptrs_locked |= (1U << i);
515                 }
516
517                 i++;
518         }
519
520         if (reserve_sectors) {
521                 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
522                                 m->data_opts.extra_replicas
523                                 ? 0
524                                 : BCH_DISK_RESERVATION_NOFAIL);
525                 if (ret)
526                         goto err;
527         }
528
529         m->op.nr_replicas += m->data_opts.extra_replicas;
530         m->op.nr_replicas_required = m->op.nr_replicas;
531
532         BUG_ON(!m->op.nr_replicas);
533
534         /* Special handling required: */
535         if (bkey_extent_is_unwritten(k))
536                 return -BCH_ERR_unwritten_extent_update;
537         return 0;
538 err:
539         i = 0;
540         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
541                 if ((1U << i) & ptrs_locked)
542                         bch2_bucket_nocow_unlock(&c->nocow_locks,
543                                                  PTR_BUCKET_POS(c, &p.ptr), 0);
544                 percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
545                 i++;
546         }
547
548         bch2_bkey_buf_exit(&m->k, c);
549         bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
550         return ret;
551 }
552
553 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
554 {
555         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
556         const struct bch_extent_ptr *ptr;
557         unsigned i = 0;
558
559         bkey_for_each_ptr(ptrs, ptr) {
560                 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
561                         opts->kill_ptrs |= 1U << i;
562                         opts->rewrite_ptrs ^= 1U << i;
563                 }
564
565                 i++;
566         }
567 }