]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/data_update.c
Update bcachefs sources to fd381c355c bcachefs: Fix a null ptr deref in fsck check_ex...
[bcachefs-tools-debian] / libbcachefs / data_update.c
index de0575f61cfbcb7920086cca2690c9f46298eab4..6b0a5fbe377ca59607fcdac49dd4c4984332fac0 100644 (file)
@@ -7,6 +7,7 @@
 #include "buckets.h"
 #include "data_update.h"
 #include "ec.h"
+#include "error.h"
 #include "extents.h"
 #include "io.h"
 #include "keylist.h"
@@ -92,14 +93,68 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
        return ret;
 }
 
-static void bch2_bkey_mark_dev_cached(struct bkey_s k, unsigned dev)
+static void trace_move_extent_fail2(struct data_update *m,
+                        struct bkey_s_c new,
+                        struct bkey_s_c wrote,
+                        struct bkey_i *insert,
+                        const char *msg)
 {
-       struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
+       struct bch_fs *c = m->op.c;
+       struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
+       const union bch_extent_entry *entry;
        struct bch_extent_ptr *ptr;
+       struct extent_ptr_decoded p;
+       struct printbuf buf = PRINTBUF;
+       unsigned i, rewrites_found = 0;
 
-       bkey_for_each_ptr(ptrs, ptr)
-               if (ptr->dev == dev)
-                       ptr->cached = true;
+       if (!trace_move_extent_fail_enabled())
+               return;
+
+       prt_str(&buf, msg);
+
+       if (insert) {
+               i = 0;
+               bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
+                       struct bkey_s new_s;
+                       new_s.k = (void *) new.k;
+                       new_s.v = (void *) new.v;
+
+                       if (((1U << i) & m->data_opts.rewrite_ptrs) &&
+                           (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
+                           !ptr->cached)
+                               rewrites_found |= 1U << i;
+                       i++;
+               }
+       }
+
+       prt_printf(&buf, "\nrewrite ptrs:   %u%u%u%u",
+                  (m->data_opts.rewrite_ptrs & (1 << 0)) != 0,
+                  (m->data_opts.rewrite_ptrs & (1 << 1)) != 0,
+                  (m->data_opts.rewrite_ptrs & (1 << 2)) != 0,
+                  (m->data_opts.rewrite_ptrs & (1 << 3)) != 0);
+
+       prt_printf(&buf, "\nrewrites found: %u%u%u%u",
+                  (rewrites_found & (1 << 0)) != 0,
+                  (rewrites_found & (1 << 1)) != 0,
+                  (rewrites_found & (1 << 2)) != 0,
+                  (rewrites_found & (1 << 3)) != 0);
+
+       prt_str(&buf, "\nold:    ");
+       bch2_bkey_val_to_text(&buf, c, old);
+
+       prt_str(&buf, "\nnew:    ");
+       bch2_bkey_val_to_text(&buf, c, new);
+
+       prt_str(&buf, "\nwrote:  ");
+       bch2_bkey_val_to_text(&buf, c, wrote);
+
+       if (insert) {
+               prt_str(&buf, "\ninsert: ");
+               bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
+       }
+
+       trace_move_extent_fail(c, buf.buf);
+       printbuf_exit(&buf);
 }
 
 static int __bch2_data_update_index_update(struct btree_trans *trans,
@@ -124,15 +179,17 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
        while (1) {
                struct bkey_s_c k;
                struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
-               struct bkey_i *insert;
+               struct bkey_i *insert = NULL;
                struct bkey_i_extent *new;
-               const union bch_extent_entry *entry;
+               const union bch_extent_entry *entry_c;
+               union bch_extent_entry *entry;
                struct extent_ptr_decoded p;
+               struct bch_extent_ptr *ptr;
+               const struct bch_extent_ptr *ptr_c;
                struct bpos next_pos;
-               bool did_work = false;
                bool should_check_enospc;
                s64 i_sectors_delta = 0, disk_sectors_delta = 0;
-               unsigned i;
+               unsigned rewrites_found = 0, durability, i;
 
                bch2_trans_begin(trans);
 
@@ -143,8 +200,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 
                new = bkey_i_to_extent(bch2_keylist_front(keys));
 
-               if (!bch2_extents_match(k, old))
-                       goto nomatch;
+               if (!bch2_extents_match(k, old)) {
+                       trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i),
+                                               NULL, "no match:");
+                       goto nowork;
+               }
 
                bkey_reassemble(_insert.k, k);
                insert = _insert.k;
@@ -167,50 +227,75 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
                 * Fist, drop rewrite_ptrs from @new:
                 */
                i = 0;
-               bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
+               bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
                        if (((1U << i) & m->data_opts.rewrite_ptrs) &&
-                           bch2_extent_has_ptr(old, p, bkey_i_to_s_c(insert))) {
+                           (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
+                           !ptr->cached) {
+                               bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
                                /*
-                                * If we're going to be adding a pointer to the
-                                * same device, we have to drop the old one -
-                                * otherwise, we can just mark it cached:
-                                */
-                               if (bch2_bkey_has_device(bkey_i_to_s_c(&new->k_i), p.ptr.dev))
-                                       bch2_bkey_drop_device_noerror(bkey_i_to_s(insert), p.ptr.dev);
-                               else
-                                       bch2_bkey_mark_dev_cached(bkey_i_to_s(insert), p.ptr.dev);
+                                * See comment below:
+                               bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
+                               */
+                               rewrites_found |= 1U << i;
                        }
                        i++;
                }
 
+               if (m->data_opts.rewrite_ptrs &&
+                   !rewrites_found &&
+                   bch2_bkey_durability(c, k) >= m->op.opts.data_replicas) {
+                       trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "no rewrites found:");
+                       goto nowork;
+               }
+
+               /*
+                * A replica that we just wrote might conflict with a replica
+                * that we want to keep, due to racing with another move:
+                */
+restart_drop_conflicting_replicas:
+               extent_for_each_ptr(extent_i_to_s(new), ptr)
+                       if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
+                           !ptr_c->cached) {
+                               bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
+                               goto restart_drop_conflicting_replicas;
+                       }
 
-               /* Add new ptrs: */
-               extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
-                       const struct bch_extent_ptr *existing_ptr =
-                               bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev);
+               if (!bkey_val_u64s(&new->k)) {
+                       trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "new replicas conflicted:");
+                       goto nowork;
+               }
 
-                       if (existing_ptr && existing_ptr->cached) {
-                               /*
-                                * We're replacing a cached pointer with a non
-                                * cached pointer:
-                                */
-                               bch2_bkey_drop_device_noerror(bkey_i_to_s(insert),
-                                                             existing_ptr->dev);
-                       } else if (existing_ptr) {
+               /* Now, drop pointers that conflict with what we just wrote: */
+               extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
+                       if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
+                               bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
+
+               durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
+                       bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
+
+               /* Now, drop excess replicas: */
+restart_drop_extra_replicas:
+               bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
+                       unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
+
+                       if (!p.ptr.cached &&
+                           durability - ptr_durability >= m->op.opts.data_replicas) {
+                               durability -= ptr_durability;
+                               bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), &entry->ptr);
                                /*
-                                * raced with another move op? extent already
-                                * has a pointer to the device we just wrote
-                                * data to
-                                */
-                               continue;
+                                * Currently, we're dropping unneeded replicas
+                                * instead of marking them as cached, since
+                                * cached data in stripe buckets prevents them
+                                * from being reused:
+                               bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
+                               */
+                               goto restart_drop_extra_replicas;
                        }
-
-                       bch2_extent_ptr_decoded_append(insert, &p);
-                       did_work = true;
                }
 
-               if (!did_work)
-                       goto nomatch;
+               /* Finally, add the pointers we just wrote: */
+               extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
+                       bch2_extent_ptr_decoded_append(insert, &p);
 
                bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
                bch2_extent_normalize(c, bkey_i_to_s(insert));
@@ -251,6 +336,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
                                BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
                        bch2_trans_commit(trans, &op->res,
                                NULL,
+                               BTREE_INSERT_NOCHECK_RW|
                                BTREE_INSERT_NOFAIL|
                                m->data_opts.btree_insert_flags);
                if (!ret) {
@@ -271,8 +357,8 @@ next:
                                goto out;
                }
                continue;
-nomatch:
-               if (m->ctxt) {
+nowork:
+               if (m->ctxt && m->ctxt->stats) {
                        BUG_ON(k.k->p.offset <= iter.pos.offset);
                        atomic64_inc(&m->ctxt->stats->keys_raced);
                        atomic64_add(k.k->p.offset - iter.pos.offset,
@@ -280,7 +366,6 @@ nomatch:
                }
 
                this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
-               trace_move_extent_fail(&new->k);
 
                bch2_btree_iter_advance(&iter);
                goto next;
@@ -295,15 +380,7 @@ out:
 
 int bch2_data_update_index_update(struct bch_write_op *op)
 {
-       struct bch_fs *c = op->c;
-       struct btree_trans trans;
-       int ret;
-
-       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
-       ret = __bch2_data_update_index_update(&trans, op);
-       bch2_trans_exit(&trans);
-
-       return ret;
+       return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
 }
 
 void bch2_data_update_read_done(struct data_update *m,
@@ -326,8 +403,9 @@ void bch2_data_update_exit(struct data_update *update)
        const struct bch_extent_ptr *ptr;
 
        bkey_for_each_ptr(ptrs, ptr) {
-               bch2_bucket_nocow_unlock(&c->nocow_locks,
-                                        PTR_BUCKET_POS(c, ptr), 0);
+               if (c->opts.nocow_enabled)
+                       bch2_bucket_nocow_unlock(&c->nocow_locks,
+                                                PTR_BUCKET_POS(c, ptr), 0);
                percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
        }
 
@@ -431,7 +509,7 @@ int bch2_data_update_init(struct btree_trans *trans,
        struct extent_ptr_decoded p;
        const struct bch_extent_ptr *ptr;
        unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
-       unsigned int ptrs_locked = 0;
+       unsigned ptrs_locked = 0;
        int ret;
 
        bch2_bkey_buf_init(&m->k);
@@ -444,6 +522,7 @@ int bch2_data_update_init(struct btree_trans *trans,
        m->op.version   = k.k->version;
        m->op.target    = data_opts.target;
        m->op.write_point = wp;
+       m->op.nr_replicas = 0;
        m->op.flags     |= BCH_WRITE_PAGES_STABLE|
                BCH_WRITE_PAGES_OWNED|
                BCH_WRITE_DATA_ENCODED|
@@ -462,17 +541,16 @@ int bch2_data_update_init(struct btree_trans *trans,
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
                bool locked;
 
-               if (((1U << i) & m->data_opts.rewrite_ptrs) &&
-                   p.ptr.cached)
-                       BUG();
+               if (((1U << i) & m->data_opts.rewrite_ptrs)) {
+                       BUG_ON(p.ptr.cached);
 
-               if (!((1U << i) & m->data_opts.rewrite_ptrs) &&
-                   !p.ptr.cached)
-                       bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
+                       if (crc_is_compressed(p.crc))
+                               reserve_sectors += k.k->size;
 
-               if (((1U << i) & m->data_opts.rewrite_ptrs) &&
-                   crc_is_compressed(p.crc))
-                       reserve_sectors += k.k->size;
+                       m->op.nr_replicas += bch2_extent_ptr_durability(c, &p);
+               } else if (!p.ptr.cached) {
+                       bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
+               }
 
                /*
                 * op->csum_type is normally initialized from the fs/file's
@@ -487,23 +565,26 @@ int bch2_data_update_init(struct btree_trans *trans,
                if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
                        m->op.incompressible = true;
 
-               if (ctxt) {
-                       move_ctxt_wait_event(ctxt, trans,
-                                       (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
-                                                                 PTR_BUCKET_POS(c, &p.ptr), 0)) ||
-                                       !atomic_read(&ctxt->read_sectors));
-
-                       if (!locked)
-                               bch2_bucket_nocow_lock(&c->nocow_locks,
-                                                      PTR_BUCKET_POS(c, &p.ptr), 0);
-               } else {
-                       if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
-                                                      PTR_BUCKET_POS(c, &p.ptr), 0)) {
-                               ret = -BCH_ERR_nocow_lock_blocked;
-                               goto err;
+               if (c->opts.nocow_enabled) {
+                       if (ctxt) {
+                               move_ctxt_wait_event(ctxt, trans,
+                                               (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
+                                                                         PTR_BUCKET_POS(c, &p.ptr), 0)) ||
+                                               !atomic_read(&ctxt->read_sectors));
+
+                               if (!locked)
+                                       bch2_bucket_nocow_lock(&c->nocow_locks,
+                                                              PTR_BUCKET_POS(c, &p.ptr), 0);
+                       } else {
+                               if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
+                                                              PTR_BUCKET_POS(c, &p.ptr), 0)) {
+                                       ret = -BCH_ERR_nocow_lock_blocked;
+                                       goto err;
+                               }
                        }
+                       ptrs_locked |= (1U << i);
                }
-               ptrs_locked |= (1U << i);
+
                i++;
        }
 
@@ -516,8 +597,8 @@ int bch2_data_update_init(struct btree_trans *trans,
                        goto err;
        }
 
-       m->op.nr_replicas = m->op.nr_replicas_required =
-               hweight32(m->data_opts.rewrite_ptrs) + m->data_opts.extra_replicas;
+       m->op.nr_replicas += m->data_opts.extra_replicas;
+       m->op.nr_replicas_required = m->op.nr_replicas;
 
        BUG_ON(!m->op.nr_replicas);