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