]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/extents.c
Update bcachefs sources to 84f132d569 bcachefs: fsck: Break walk_inode() up into...
[bcachefs-tools-debian] / libbcachefs / extents.c
index e2c09ea4a3e013bd9bb55c2518da017e59dd69c7..7e00550980de3f4c9a8c1eef3079677f8177c502 100644 (file)
 #include "replicas.h"
 #include "super.h"
 #include "super-io.h"
+#include "trace.h"
 #include "util.h"
 
-#include <trace/events/bcachefs.h>
-
 static unsigned bch2_crc_field_size_max[] = {
        [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
        [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
@@ -184,27 +183,12 @@ void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
 int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
                              unsigned flags, struct printbuf *err)
 {
-       struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
-
-       if (bkey_val_bytes(k.k) <= sizeof(*bp.v)) {
-               prt_printf(err, "value too small (%zu <= %zu)",
-                      bkey_val_bytes(k.k), sizeof(*bp.v));
-               return -BCH_ERR_invalid_bkey;
-       }
-
        if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
                prt_printf(err, "value too big (%zu > %zu)",
                       bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
                return -BCH_ERR_invalid_bkey;
        }
 
-       if (c->sb.version < bcachefs_metadata_version_snapshot &&
-           bp.v->min_key.snapshot) {
-               prt_printf(err, "invalid min_key.snapshot (%u != 0)",
-                      bp.v->min_key.snapshot);
-               return -BCH_ERR_invalid_bkey;
-       }
-
        return bch2_bkey_ptrs_invalid(c, k, flags, err);
 }
 
@@ -391,12 +375,6 @@ int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
 {
        struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
 
-       if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation)) {
-               prt_printf(err, "incorrect value size (%zu != %zu)",
-                      bkey_val_bytes(k.k), sizeof(*r.v));
-               return -BCH_ERR_invalid_bkey;
-       }
-
        if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
                prt_printf(err, "invalid nr_replicas (%u)",
                       r.v->nr_replicas);
@@ -663,9 +641,23 @@ unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
        return replicas;
 }
 
+unsigned bch2_extent_ptr_desired_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
+{
+       struct bch_dev *ca;
+
+       if (p->ptr.cached)
+               return 0;
+
+       ca = bch_dev_bkey_exists(c, p->ptr.dev);
+
+       return ca->mi.durability +
+               (p->has_ec
+                ? p->ec.redundancy
+                : 0);
+}
+
 unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
 {
-       unsigned durability = 0;
        struct bch_dev *ca;
 
        if (p->ptr.cached)
@@ -673,16 +665,29 @@ unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded
 
        ca = bch_dev_bkey_exists(c, p->ptr.dev);
 
-       if (ca->mi.state != BCH_MEMBER_STATE_failed)
-               durability = max_t(unsigned, durability, ca->mi.durability);
+       if (ca->mi.state == BCH_MEMBER_STATE_failed)
+               return 0;
 
-       if (p->has_ec)
-               durability += p->ec.redundancy;
+       return ca->mi.durability +
+               (p->has_ec
+                ? p->ec.redundancy
+                : 0);
+}
+
+unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
+{
+       struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
+       const union bch_extent_entry *entry;
+       struct extent_ptr_decoded p;
+       unsigned durability = 0;
+
+       bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
+               durability += bch2_extent_ptr_durability(c, &p);
 
        return durability;
 }
 
-unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
+static unsigned bch2_bkey_durability_safe(struct bch_fs *c, struct bkey_s_c k)
 {
        struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
        const union bch_extent_entry *entry;
@@ -690,7 +695,8 @@ unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
        unsigned durability = 0;
 
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
-               durability += bch2_extent_ptr_durability(c,& p);
+               if (p.ptr.dev < c->sb.nr_devices && c->devs[p.ptr.dev])
+                       durability += bch2_extent_ptr_durability(c, &p);
 
        return durability;
 }
@@ -990,7 +996,7 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
        bool first = true;
 
        if (c)
-               prt_printf(out, "durability: %u ", bch2_bkey_durability(c, k));
+               prt_printf(out, "durability: %u ", bch2_bkey_durability_safe(c, k));
 
        bkey_extent_entry_for_each(ptrs, entry) {
                if (!first)