]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/extents.c
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[bcachefs-tools-debian] / libbcachefs / extents.c
index ca208b060955de307ab18e925182c557fd25d8ee..cb88dd15a86cc55399e359acdaaeac4895d04cac 100644 (file)
@@ -228,7 +228,8 @@ void bch2_extent_debugcheck(struct bch_fs *c, struct bkey_s_c k)
        struct extent_ptr_decoded p;
        char buf[160];
 
-       if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
+       if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) ||
+           !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
                return;
 
        if (!percpu_down_read_trylock(&c->mark_lock))
@@ -336,7 +337,7 @@ enum merge_result bch2_extent_merge(struct bch_fs *c,
                        if (!bch2_checksum_mergeable(crc_l.csum_type))
                                return BCH_MERGE_NOMERGE;
 
-                       if (crc_l.compression_type)
+                       if (crc_is_compressed(crc_l))
                                return BCH_MERGE_NOMERGE;
 
                        if (crc_l.csum_type &&
@@ -344,7 +345,7 @@ enum merge_result bch2_extent_merge(struct bch_fs *c,
                            crc_r.uncompressed_size > c->sb.encoded_extent_max)
                                return BCH_MERGE_NOMERGE;
 
-                       if (crc_l.uncompressed_size + crc_r.uncompressed_size - 1 >
+                       if (crc_l.uncompressed_size + crc_r.uncompressed_size >
                            bch2_crc_field_size_max[extent_entry_type(en_l)])
                                return BCH_MERGE_NOMERGE;
 
@@ -447,7 +448,7 @@ static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
                                  struct bch_extent_crc_unpacked n)
 {
-       return !u.compression_type &&
+       return !crc_is_compressed(u) &&
                u.csum_type &&
                u.uncompressed_size > u.live_size &&
                bch2_csum_type_is_encryption(u.csum_type) ==
@@ -491,7 +492,7 @@ bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
        /* Find a checksum entry that covers only live data: */
        if (!n.csum_type) {
                bkey_for_each_crc(&k->k, ptrs, u, i)
-                       if (!u.compression_type &&
+                       if (!crc_is_compressed(u) &&
                            u.csum_type &&
                            u.live_size == u.uncompressed_size) {
                                n = u;
@@ -500,7 +501,7 @@ bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
                return false;
        }
 found:
-       BUG_ON(n.compression_type);
+       BUG_ON(crc_is_compressed(n));
        BUG_ON(n.offset);
        BUG_ON(n.live_size != k->k.size);
 
@@ -562,15 +563,15 @@ void bch2_extent_crc_append(struct bkey_i *k,
        enum bch_extent_entry_type type;
 
        if (bch_crc_bytes[new.csum_type]        <= 4 &&
-           new.uncompressed_size - 1           <= CRC32_SIZE_MAX &&
+           new.uncompressed_size               <= CRC32_SIZE_MAX &&
            new.nonce                           <= CRC32_NONCE_MAX)
                type = BCH_EXTENT_ENTRY_crc32;
        else if (bch_crc_bytes[new.csum_type]   <= 10 &&
-                  new.uncompressed_size - 1    <= CRC64_SIZE_MAX &&
+                  new.uncompressed_size        <= CRC64_SIZE_MAX &&
                   new.nonce                    <= CRC64_NONCE_MAX)
                type = BCH_EXTENT_ENTRY_crc64;
        else if (bch_crc_bytes[new.csum_type]   <= 16 &&
-                  new.uncompressed_size - 1    <= CRC128_SIZE_MAX &&
+                  new.uncompressed_size        <= CRC128_SIZE_MAX &&
                   new.nonce                    <= CRC128_NONCE_MAX)
                type = BCH_EXTENT_ENTRY_crc128;
        else
@@ -609,8 +610,7 @@ unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
                struct extent_ptr_decoded p;
 
                bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
-                       ret += !p.ptr.cached &&
-                               p.crc.compression_type == BCH_COMPRESSION_TYPE_none;
+                       ret += !p.ptr.cached && !crc_is_compressed(p.crc);
        }
 
        return ret;
@@ -624,13 +624,24 @@ unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
        unsigned ret = 0;
 
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
-               if (!p.ptr.cached &&
-                   p.crc.compression_type != BCH_COMPRESSION_TYPE_none)
+               if (!p.ptr.cached && crc_is_compressed(p.crc))
                        ret += p.crc.compressed_size;
 
        return ret;
 }
 
+bool bch2_bkey_is_incompressible(struct bkey_s_c k)
+{
+       struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
+       const union bch_extent_entry *entry;
+       struct bch_extent_crc_unpacked crc;
+
+       bkey_for_each_crc(k.k, ptrs, crc, entry)
+               if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
+                       return true;
+       return false;
+}
+
 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
                                unsigned nr_replicas)
 {
@@ -738,6 +749,7 @@ void bch2_bkey_append_ptr(struct bkey_i *k,
 
        switch (k->k.type) {
        case KEY_TYPE_btree_ptr:
+       case KEY_TYPE_btree_ptr_v2:
        case KEY_TYPE_extent:
                EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
 
@@ -1020,6 +1032,8 @@ const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
 
        if (k.k->type == KEY_TYPE_btree_ptr)
                size_ondisk = c->opts.btree_node_size;
+       if (k.k->type == KEY_TYPE_btree_ptr_v2)
+               size_ondisk = le16_to_cpu(bkey_s_c_to_btree_ptr_v2(k).v->sectors);
 
        bkey_extent_entry_for_each(ptrs, entry) {
                if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
@@ -1068,17 +1082,19 @@ const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
        return NULL;
 }
 
-void bch2_ptr_swab(const struct bkey_format *f, struct bkey_packed *k)
+void bch2_ptr_swab(struct bkey_s k)
 {
+       struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
        union bch_extent_entry *entry;
-       u64 *d = (u64 *) bkeyp_val(f, k);
-       unsigned i;
+       u64 *d;
 
-       for (i = 0; i < bkeyp_val_u64s(f, k); i++)
-               d[i] = swab64(d[i]);
+       for (d =  (u64 *) ptrs.start;
+            d != (u64 *) ptrs.end;
+            d++)
+               *d = swab64(*d);
 
-       for (entry = (union bch_extent_entry *) d;
-            entry < (union bch_extent_entry *) (d + bkeyp_val_u64s(f, k));
+       for (entry = ptrs.start;
+            entry < ptrs.end;
             entry = extent_entry_next(entry)) {
                switch (extent_entry_type(entry)) {
                case BCH_EXTENT_ENTRY_ptr: