1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
6 #include "btree_update.h"
8 #include "data_update.h"
14 #include "nocow_locking.h"
15 #include "subvolume.h"
17 #include <trace/events/bcachefs.h>
19 static int insert_snapshot_whiteouts(struct btree_trans *trans,
24 struct bch_fs *c = trans->c;
25 struct btree_iter iter, iter2;
26 struct bkey_s_c k, k2;
28 struct bkey_i *update;
31 if (!btree_type_has_snapshots(id))
36 if (!bch2_snapshot_has_children(c, old_pos.snapshot))
39 bch2_trans_iter_init(trans, &iter, id, old_pos,
40 BTREE_ITER_NOT_EXTENTS|
41 BTREE_ITER_ALL_SNAPSHOTS);
43 k = bch2_btree_iter_prev(&iter);
51 if (!bkey_eq(old_pos, k.k->p))
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;
58 whiteout_pos.snapshot = k.k->p.snapshot;
60 bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
61 BTREE_ITER_NOT_EXTENTS|
63 k2 = bch2_btree_iter_peek_slot(&iter2);
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);
72 bkey_init(&update->k);
73 update->k.p = whiteout_pos;
74 update->k.type = KEY_TYPE_whiteout;
76 ret = bch2_trans_update(trans, &iter2, update,
77 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
79 bch2_trans_iter_exit(trans, &iter2);
84 ret = snapshot_list_add(c, &s, k.k->p.snapshot);
89 bch2_trans_iter_exit(trans, &iter);
95 static int __bch2_data_update_index_update(struct btree_trans *trans,
96 struct bch_write_op *op)
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;
106 bch2_bkey_buf_init(&_new);
107 bch2_bkey_buf_init(&_insert);
108 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
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);
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;
129 bch2_trans_begin(trans);
131 k = bch2_btree_iter_peek_slot(&iter);
136 new = bkey_i_to_extent(bch2_keylist_front(keys));
138 if (!bch2_extents_match(k, old))
141 bkey_reassemble(_insert.k, k);
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);
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);
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
157 * @new: extent with new pointers that we'll be adding to @insert
159 * Fist, drop rewrite_ptrs from @new:
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))) &&
166 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
167 rewrites_found |= 1U << i;
172 if (m->data_opts.rewrite_ptrs &&
174 bch2_bkey_durability(c, k) >= m->op.opts.data_replicas)
178 * A replica that we just wrote might conflict with a replica
179 * that we want to keep, due to racing with another move:
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)) &&
185 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
186 goto restart_drop_conflicting_replicas;
189 if (!bkey_val_u64s(&new->k))
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);
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));
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);
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;
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);
217 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
218 bch2_extent_normalize(c, bkey_i_to_s(insert));
220 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
221 &should_check_enospc,
223 &disk_sectors_delta);
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,
231 ? BCH_DISK_RESERVATION_NOFAIL : 0);
236 next_pos = insert->k.p;
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));
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);
252 ret = bch2_trans_update(trans, &iter, insert,
253 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
254 bch2_trans_commit(trans, &op->res,
256 BTREE_INSERT_NOCHECK_RW|
258 m->data_opts.btree_insert_flags);
260 bch2_btree_iter_set_pos(&iter, next_pos);
262 this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
263 trace_move_extent_finish(&new->k);
266 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
271 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
272 bch2_keylist_pop_front(keys);
273 if (bch2_keylist_empty(keys))
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);
285 this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
286 trace_move_extent_fail(&new->k);
288 bch2_btree_iter_advance(&iter);
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));
299 int bch2_data_update_index_update(struct bch_write_op *op)
301 return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
304 void bch2_data_update_read_done(struct data_update *m,
305 struct bch_extent_crc_unpacked crc)
307 /* write bio must own pages: */
308 BUG_ON(!m->op.wbio.bio.bi_vcnt);
311 m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
313 closure_call(&m->op.cl, bch2_write, NULL, NULL);
316 void bch2_data_update_exit(struct data_update *update)
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;
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);
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);
335 void bch2_update_unwritten_extent(struct btree_trans *trans,
336 struct data_update *update)
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;
344 struct btree_iter iter;
348 closure_init_stack(&cl);
349 bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
351 while (bio_sectors(bio)) {
352 unsigned sectors = bio_sectors(bio);
354 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
356 ret = lockrestart_do(trans, ({
357 k = bch2_btree_iter_peek_slot(&iter);
360 bch2_trans_iter_exit(trans, &iter);
362 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
365 e = bkey_extent_init(update->op.insert_keys.top);
366 e->k.p = update->op.pos;
368 ret = bch2_alloc_sectors_start_trans(trans,
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,
377 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
378 bch2_trans_unlock(trans);
386 sectors = min(sectors, wp->sectors_free);
388 bch2_key_resize(&e->k, sectors);
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);
394 bio_advance(bio, sectors << 9);
395 update->op.pos.offset += sectors;
397 extent_for_each_ptr(extent_i_to_s(e), ptr)
398 ptr->unwritten = true;
399 bch2_keylist_push(&update->op.insert_keys);
401 ret = __bch2_data_update_index_update(trans, &update->op);
403 bch2_open_buckets_put(c, &update->op.open_buckets);
409 if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
410 bch2_trans_unlock(trans);
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,
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;
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;
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|
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;
455 bkey_for_each_ptr(ptrs, ptr)
456 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
459 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
462 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
463 BUG_ON(p.ptr.cached);
465 if (crc_is_compressed(p.crc))
466 reserve_sectors += k.k->size;
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);
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:
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;
483 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
484 m->op.incompressible = true;
486 if (c->opts.nocow_enabled) {
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));
494 bch2_bucket_nocow_lock(&c->nocow_locks,
495 PTR_BUCKET_POS(c, &p.ptr), 0);
497 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
498 PTR_BUCKET_POS(c, &p.ptr), 0)) {
499 ret = -BCH_ERR_nocow_lock_blocked;
503 ptrs_locked |= (1U << i);
509 if (reserve_sectors) {
510 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
511 m->data_opts.extra_replicas
513 : BCH_DISK_RESERVATION_NOFAIL);
518 m->op.nr_replicas += m->data_opts.extra_replicas;
519 m->op.nr_replicas_required = m->op.nr_replicas;
521 BUG_ON(!m->op.nr_replicas);
523 /* Special handling required: */
524 if (bkey_extent_is_unwritten(k))
525 return -BCH_ERR_unwritten_extent_update;
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);
537 bch2_bkey_buf_exit(&m->k, c);
538 bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
542 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
544 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
545 const struct bch_extent_ptr *ptr;
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;