]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/data_update.c
Update bcachefs sources to cd779e0cc5 bcachefs: Skip inode unpack/pack in bch2_extent...
[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 "subvolume.h"
15
16 #include <trace/events/bcachefs.h>
17
18 static int insert_snapshot_whiteouts(struct btree_trans *trans,
19                                      enum btree_id id,
20                                      struct bpos old_pos,
21                                      struct bpos new_pos)
22 {
23         struct bch_fs *c = trans->c;
24         struct btree_iter iter, update_iter;
25         struct bkey_s_c k;
26         snapshot_id_list s;
27         int ret;
28
29         if (!btree_type_has_snapshots(id))
30                 return 0;
31
32         darray_init(&s);
33
34         if (!bkey_cmp(old_pos, new_pos))
35                 return 0;
36
37         if (!snapshot_t(c, old_pos.snapshot)->children[0])
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 (bkey_cmp(old_pos, k.k->p))
50                         break;
51
52                 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
53                         struct bkey_i *update;
54
55                         if (snapshot_list_has_ancestor(c, &s, k.k->p.snapshot))
56                                 continue;
57
58                         update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
59
60                         ret = PTR_ERR_OR_ZERO(update);
61                         if (ret)
62                                 break;
63
64                         bkey_init(&update->k);
65                         update->k.p = new_pos;
66                         update->k.p.snapshot = k.k->p.snapshot;
67
68                         bch2_trans_iter_init(trans, &update_iter, id, update->k.p,
69                                              BTREE_ITER_NOT_EXTENTS|
70                                              BTREE_ITER_ALL_SNAPSHOTS|
71                                              BTREE_ITER_INTENT);
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);
76                         if (ret)
77                                 break;
78
79                         ret = snapshot_list_add(c, &s, k.k->p.snapshot);
80                         if (ret)
81                                 break;
82                 }
83         }
84         bch2_trans_iter_exit(trans, &iter);
85         darray_exit(&s);
86
87         return ret;
88 }
89
90 static void bch2_bkey_mark_dev_cached(struct bkey_s k, unsigned dev)
91 {
92         struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
93         struct bch_extent_ptr *ptr;
94
95         bkey_for_each_ptr(ptrs, ptr)
96                 if (ptr->dev == dev)
97                         ptr->cached = true;
98 }
99
100 static int bch2_data_update_index_update(struct bch_write_op *op)
101 {
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;
109         int ret = 0;
110
111         bch2_bkey_buf_init(&_new);
112         bch2_bkey_buf_init(&_insert);
113         bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
114
115         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
116
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);
120
121         while (1) {
122                 struct bkey_s_c k;
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;
132                 unsigned i;
133
134                 bch2_trans_begin(&trans);
135
136                 k = bch2_btree_iter_peek_slot(&iter);
137                 ret = bkey_err(k);
138                 if (ret)
139                         goto err;
140
141                 new = bkey_i_to_extent(bch2_keylist_front(keys));
142
143                 if (!bch2_extents_match(k, old))
144                         goto nomatch;
145
146                 bkey_reassemble(_insert.k, k);
147                 insert = _insert.k;
148
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);
152
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);
156
157                 /*
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
161                  * other updates
162                  * @new: extent with new pointers that we'll be adding to @insert
163                  *
164                  * Fist, drop rewrite_ptrs from @new:
165                  */
166                 i = 0;
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))) {
170                                 /*
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:
174                                  */
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);
177                                 else
178                                         bch2_bkey_mark_dev_cached(bkey_i_to_s(insert), p.ptr.dev);
179                         }
180                         i++;
181                 }
182
183
184                 /* Add new ptrs: */
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)) {
187                                 /*
188                                  * raced with another move op? extent already
189                                  * has a pointer to the device we just wrote
190                                  * data to
191                                  */
192                                 continue;
193                         }
194
195                         bch2_extent_ptr_decoded_append(insert, &p);
196                         did_work = true;
197                 }
198
199                 if (!did_work)
200                         goto nomatch;
201
202                 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
203                 bch2_extent_normalize(c, bkey_i_to_s(insert));
204
205                 ret = bch2_sum_sector_overwrites(&trans, &iter, insert,
206                                                  &should_check_enospc,
207                                                  &i_sectors_delta,
208                                                  &disk_sectors_delta);
209                 if (ret)
210                         goto err;
211
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,
215                                                 !should_check_enospc
216                                                 ? BCH_DISK_RESERVATION_NOFAIL : 0);
217                         if (ret)
218                                 goto out;
219                 }
220
221                 next_pos = insert->k.p;
222
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,
228                                 op_journal_seq(op),
229                                 BTREE_INSERT_NOFAIL|
230                                 m->data_opts.btree_insert_flags);
231                 if (!ret) {
232                         bch2_btree_iter_set_pos(&iter, next_pos);
233
234                         this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
235                         trace_move_extent_finish(&new->k);
236                 }
237 err:
238                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
239                         ret = 0;
240                 if (ret)
241                         break;
242 next:
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))
246                                 goto out;
247                 }
248                 continue;
249 nomatch:
250                 if (m->ctxt) {
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);
255                 }
256
257                 this_cpu_add(c->counters[BCH_COUNTER_move_extent_race], new->k.size);
258                 trace_move_extent_race(&new->k);
259
260                 bch2_btree_iter_advance(&iter);
261                 goto next;
262         }
263 out:
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));
269         return ret;
270 }
271
272 void bch2_data_update_read_done(struct data_update *m,
273                                 struct bch_extent_crc_unpacked crc,
274                                 struct closure *cl)
275 {
276         /* write bio must own pages: */
277         BUG_ON(!m->op.wbio.bio.bi_vcnt);
278
279         m->op.crc = crc;
280         m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
281
282         closure_call(&m->op.cl, bch2_write, NULL, cl);
283 }
284
285 void bch2_data_update_exit(struct data_update *update)
286 {
287         struct bch_fs *c = update->op.c;
288
289         bch2_bkey_buf_exit(&update->k, c);
290         bch2_disk_reservation_put(c, &update->op.res);
291         bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
292 }
293
294 int bch2_data_update_init(struct bch_fs *c, struct data_update *m,
295                           struct write_point_specifier wp,
296                           struct bch_io_opts io_opts,
297                           struct data_update_opts data_opts,
298                           enum btree_id btree_id,
299                           struct bkey_s_c k)
300 {
301         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
302         const union bch_extent_entry *entry;
303         struct extent_ptr_decoded p;
304         unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
305         int ret;
306
307         bch2_bkey_buf_init(&m->k);
308         bch2_bkey_buf_reassemble(&m->k, c, k);
309         m->btree_id     = btree_id;
310         m->data_opts    = data_opts;
311
312         bch2_write_op_init(&m->op, c, io_opts);
313         m->op.pos       = bkey_start_pos(k.k);
314         m->op.version   = k.k->version;
315         m->op.target    = data_opts.target;
316         m->op.write_point = wp;
317         m->op.flags     |= BCH_WRITE_PAGES_STABLE|
318                 BCH_WRITE_PAGES_OWNED|
319                 BCH_WRITE_DATA_ENCODED|
320                 BCH_WRITE_FROM_INTERNAL|
321                 m->data_opts.write_flags;
322         m->op.compression_type =
323                 bch2_compression_opt_to_type[io_opts.background_compression ?:
324                                              io_opts.compression];
325         if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
326                 m->op.alloc_reserve = RESERVE_movinggc;
327         m->op.index_update_fn   = bch2_data_update_index_update;
328
329         i = 0;
330         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
331                 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
332                     p.ptr.cached)
333                         BUG();
334
335                 if (!((1U << i) & m->data_opts.rewrite_ptrs))
336                         bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
337
338                 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
339                     crc_is_compressed(p.crc))
340                         reserve_sectors += k.k->size;
341
342                 /*
343                  * op->csum_type is normally initialized from the fs/file's
344                  * current options - but if an extent is encrypted, we require
345                  * that it stays encrypted:
346                  */
347                 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
348                         m->op.nonce     = p.crc.nonce + p.crc.offset;
349                         m->op.csum_type = p.crc.csum_type;
350                 }
351
352                 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
353                         m->op.incompressible = true;
354
355                 i++;
356         }
357
358         if (reserve_sectors) {
359                 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
360                                 m->data_opts.extra_replicas
361                                 ? 0
362                                 : BCH_DISK_RESERVATION_NOFAIL);
363                 if (ret)
364                         return ret;
365         }
366
367         m->op.nr_replicas = m->op.nr_replicas_required =
368                 hweight32(m->data_opts.rewrite_ptrs) + m->data_opts.extra_replicas;
369
370         BUG_ON(!m->op.nr_replicas);
371         return 0;
372 }
373
374 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
375 {
376         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
377         const struct bch_extent_ptr *ptr;
378         unsigned i = 0;
379
380         bkey_for_each_ptr(ptrs, ptr) {
381                 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
382                         opts->kill_ptrs |= 1U << i;
383                         opts->rewrite_ptrs ^= 1U << i;
384                 }
385
386                 i++;
387         }
388 }