1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
6 #include "btree_update.h"
8 #include "data_update.h"
14 #include "subvolume.h"
16 #include <trace/events/bcachefs.h>
18 static int insert_snapshot_whiteouts(struct btree_trans *trans,
23 struct bch_fs *c = trans->c;
24 struct btree_iter iter, update_iter;
29 if (!btree_type_has_snapshots(id))
34 if (!bkey_cmp(old_pos, new_pos))
37 if (!snapshot_t(c, old_pos.snapshot)->children[0])
40 bch2_trans_iter_init(trans, &iter, id, old_pos,
41 BTREE_ITER_NOT_EXTENTS|
42 BTREE_ITER_ALL_SNAPSHOTS);
44 k = bch2_btree_iter_prev(&iter);
49 if (bkey_cmp(old_pos, k.k->p))
52 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
53 struct bkey_i *update;
55 if (snapshot_list_has_ancestor(c, &s, k.k->p.snapshot))
58 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
60 ret = PTR_ERR_OR_ZERO(update);
64 bkey_init(&update->k);
65 update->k.p = new_pos;
66 update->k.p.snapshot = k.k->p.snapshot;
68 bch2_trans_iter_init(trans, &update_iter, id, update->k.p,
69 BTREE_ITER_NOT_EXTENTS|
70 BTREE_ITER_ALL_SNAPSHOTS|
72 ret = bch2_btree_iter_traverse(&update_iter) ?:
73 bch2_trans_update(trans, &update_iter, update,
74 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
75 bch2_trans_iter_exit(trans, &update_iter);
79 ret = snapshot_list_add(c, &s, k.k->p.snapshot);
84 bch2_trans_iter_exit(trans, &iter);
90 static void bch2_bkey_mark_dev_cached(struct bkey_s k, unsigned dev)
92 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
93 struct bch_extent_ptr *ptr;
95 bkey_for_each_ptr(ptrs, ptr)
100 int bch2_data_update_index_update(struct bch_write_op *op)
102 struct bch_fs *c = op->c;
103 struct btree_trans trans;
104 struct btree_iter iter;
105 struct data_update *m =
106 container_of(op, struct data_update, op);
107 struct keylist *keys = &op->insert_keys;
108 struct bkey_buf _new, _insert;
111 bch2_bkey_buf_init(&_new);
112 bch2_bkey_buf_init(&_insert);
113 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
115 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
117 bch2_trans_iter_init(&trans, &iter, m->btree_id,
118 bkey_start_pos(&bch2_keylist_front(keys)->k),
119 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
123 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
124 struct bkey_i *insert;
125 struct bkey_i_extent *new;
126 const union bch_extent_entry *entry;
127 struct extent_ptr_decoded p;
128 struct bpos next_pos;
129 bool did_work = false;
130 bool should_check_enospc;
131 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
134 bch2_trans_begin(&trans);
136 k = bch2_btree_iter_peek_slot(&iter);
141 new = bkey_i_to_extent(bch2_keylist_front(keys));
143 if (!bch2_extents_match(k, old))
146 bkey_reassemble(_insert.k, k);
149 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
150 new = bkey_i_to_extent(_new.k);
151 bch2_cut_front(iter.pos, &new->k_i);
153 bch2_cut_front(iter.pos, insert);
154 bch2_cut_back(new->k.p, insert);
155 bch2_cut_back(insert->k.p, &new->k_i);
158 * @old: extent that we read from
159 * @insert: key that we're going to update, initialized from
160 * extent currently in btree - same as @old unless we raced with
162 * @new: extent with new pointers that we'll be adding to @insert
164 * Fist, drop rewrite_ptrs from @new:
167 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
168 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
169 bch2_extent_has_ptr(old, p, bkey_i_to_s_c(insert))) {
171 * If we're going to be adding a pointer to the
172 * same device, we have to drop the old one -
173 * otherwise, we can just mark it cached:
175 if (bch2_bkey_has_device(bkey_i_to_s_c(&new->k_i), p.ptr.dev))
176 bch2_bkey_drop_device_noerror(bkey_i_to_s(insert), p.ptr.dev);
178 bch2_bkey_mark_dev_cached(bkey_i_to_s(insert), p.ptr.dev);
185 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
186 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
188 * raced with another move op? extent already
189 * has a pointer to the device we just wrote
195 bch2_extent_ptr_decoded_append(insert, &p);
202 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
203 bch2_extent_normalize(c, bkey_i_to_s(insert));
205 ret = bch2_sum_sector_overwrites(&trans, &iter, insert,
206 &should_check_enospc,
208 &disk_sectors_delta);
212 if (disk_sectors_delta > (s64) op->res.sectors) {
213 ret = bch2_disk_reservation_add(c, &op->res,
214 disk_sectors_delta - op->res.sectors,
216 ? BCH_DISK_RESERVATION_NOFAIL : 0);
221 next_pos = insert->k.p;
223 ret = insert_snapshot_whiteouts(&trans, m->btree_id,
224 k.k->p, insert->k.p) ?:
225 bch2_trans_update(&trans, &iter, insert,
226 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
227 bch2_trans_commit(&trans, &op->res,
230 m->data_opts.btree_insert_flags);
232 bch2_btree_iter_set_pos(&iter, next_pos);
234 this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
235 trace_move_extent_finish(&new->k);
238 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
243 while (bkey_cmp(iter.pos, bch2_keylist_front(keys)->k.p) >= 0) {
244 bch2_keylist_pop_front(keys);
245 if (bch2_keylist_empty(keys))
251 BUG_ON(k.k->p.offset <= iter.pos.offset);
252 atomic64_inc(&m->ctxt->stats->keys_raced);
253 atomic64_add(k.k->p.offset - iter.pos.offset,
254 &m->ctxt->stats->sectors_raced);
257 this_cpu_add(c->counters[BCH_COUNTER_move_extent_race], new->k.size);
258 trace_move_extent_race(&new->k);
260 bch2_btree_iter_advance(&iter);
264 bch2_trans_iter_exit(&trans, &iter);
265 bch2_trans_exit(&trans);
266 bch2_bkey_buf_exit(&_insert, c);
267 bch2_bkey_buf_exit(&_new, c);
268 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
272 void bch2_data_update_read_done(struct data_update *m,
273 struct bch_extent_crc_unpacked crc)
275 /* write bio must own pages: */
276 BUG_ON(!m->op.wbio.bio.bi_vcnt);
279 m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
281 closure_call(&m->op.cl, bch2_write, NULL, NULL);
284 void bch2_data_update_exit(struct data_update *update)
286 struct bch_fs *c = update->op.c;
288 bch2_bkey_buf_exit(&update->k, c);
289 bch2_disk_reservation_put(c, &update->op.res);
290 bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
293 int bch2_data_update_init(struct bch_fs *c, struct data_update *m,
294 struct write_point_specifier wp,
295 struct bch_io_opts io_opts,
296 struct data_update_opts data_opts,
297 enum btree_id btree_id,
300 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
301 const union bch_extent_entry *entry;
302 struct extent_ptr_decoded p;
303 unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
306 bch2_bkey_buf_init(&m->k);
307 bch2_bkey_buf_reassemble(&m->k, c, k);
308 m->btree_id = btree_id;
309 m->data_opts = data_opts;
311 bch2_write_op_init(&m->op, c, io_opts);
312 m->op.pos = bkey_start_pos(k.k);
313 m->op.version = k.k->version;
314 m->op.target = data_opts.target;
315 m->op.write_point = wp;
316 m->op.flags |= BCH_WRITE_PAGES_STABLE|
317 BCH_WRITE_PAGES_OWNED|
318 BCH_WRITE_DATA_ENCODED|
320 m->data_opts.write_flags;
321 m->op.compression_type =
322 bch2_compression_opt_to_type[io_opts.background_compression ?:
323 io_opts.compression];
324 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
325 m->op.alloc_reserve = RESERVE_movinggc;
328 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
329 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
333 if (!((1U << i) & m->data_opts.rewrite_ptrs))
334 bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
336 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
337 crc_is_compressed(p.crc))
338 reserve_sectors += k.k->size;
341 * op->csum_type is normally initialized from the fs/file's
342 * current options - but if an extent is encrypted, we require
343 * that it stays encrypted:
345 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
346 m->op.nonce = p.crc.nonce + p.crc.offset;
347 m->op.csum_type = p.crc.csum_type;
350 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
351 m->op.incompressible = true;
356 if (reserve_sectors) {
357 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
358 m->data_opts.extra_replicas
360 : BCH_DISK_RESERVATION_NOFAIL);
365 m->op.nr_replicas = m->op.nr_replicas_required =
366 hweight32(m->data_opts.rewrite_ptrs) + m->data_opts.extra_replicas;
368 BUG_ON(!m->op.nr_replicas);
372 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
374 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
375 const struct bch_extent_ptr *ptr;
378 bkey_for_each_ptr(ptrs, ptr) {
379 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
380 opts->kill_ptrs |= 1U << i;
381 opts->rewrite_ptrs ^= 1U << i;