]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/checksum.h
Update bcachefs sources to 9b3aa5ec6c bcachefs: Add tabstops to printbufs
[bcachefs-tools-debian] / libbcachefs / checksum.h
index f540e30507499cf2f528287e01ac2ac475967462..c86c3c05d62054a66faffa4c5c2cde81ab5de1e8 100644 (file)
@@ -1,12 +1,29 @@
-#ifndef _BCACHE_CHECKSUM_H
-#define _BCACHE_CHECKSUM_H
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _BCACHEFS_CHECKSUM_H
+#define _BCACHEFS_CHECKSUM_H
 
 #include "bcachefs.h"
+#include "extents_types.h"
 #include "super-io.h"
 
-#include <crypto/chacha20.h>
+#include <linux/crc64.h>
+#include <crypto/chacha.h>
 
-u64 bch2_crc64_update(u64, const void *, size_t);
+static inline bool bch2_checksum_mergeable(unsigned type)
+{
+
+       switch (type) {
+       case BCH_CSUM_none:
+       case BCH_CSUM_crc32c:
+       case BCH_CSUM_crc64:
+               return true;
+       default:
+               return false;
+       }
+}
+
+struct bch_csum bch2_checksum_merge(unsigned, struct bch_csum,
+                                   struct bch_csum, size_t);
 
 #define BCH_NONCE_EXTENT       cpu_to_le32(1 << 28)
 #define BCH_NONCE_BTREE                cpu_to_le32(2 << 28)
@@ -32,13 +49,23 @@ struct bch_csum bch2_checksum(struct bch_fs *, unsigned, struct nonce,
 int bch2_chacha_encrypt_key(struct bch_key *, struct nonce, void *, size_t);
 int bch2_request_key(struct bch_sb *, struct bch_key *);
 
-void bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
+int bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
                 void *data, size_t);
 
 struct bch_csum bch2_checksum_bio(struct bch_fs *, unsigned,
-                                struct nonce, struct bio *);
-void bch2_encrypt_bio(struct bch_fs *, unsigned,
-                   struct nonce, struct bio *);
+                                 struct nonce, struct bio *);
+
+int bch2_rechecksum_bio(struct bch_fs *, struct bio *, struct bversion,
+                       struct bch_extent_crc_unpacked,
+                       struct bch_extent_crc_unpacked *,
+                       struct bch_extent_crc_unpacked *,
+                       unsigned, unsigned, unsigned);
+
+int bch2_encrypt_bio(struct bch_fs *, unsigned,
+                    struct nonce, struct bio *);
+
+int bch2_decrypt_sb_key(struct bch_fs *, struct bch_sb_field_crypt *,
+                       struct bch_key *);
 
 int bch2_disable_encryption(struct bch_fs *);
 int bch2_enable_encryption(struct bch_fs *, bool);
@@ -46,23 +73,48 @@ int bch2_enable_encryption(struct bch_fs *, bool);
 void bch2_fs_encryption_exit(struct bch_fs *);
 int bch2_fs_encryption_init(struct bch_fs *);
 
-static inline unsigned bch2_data_checksum_type(struct bch_fs *c)
+static inline enum bch_csum_type bch2_csum_opt_to_type(enum bch_csum_opts type,
+                                                      bool data)
+{
+       switch (type) {
+       case BCH_CSUM_OPT_none:
+            return BCH_CSUM_none;
+       case BCH_CSUM_OPT_crc32c:
+            return data ? BCH_CSUM_crc32c : BCH_CSUM_crc32c_nonzero;
+       case BCH_CSUM_OPT_crc64:
+            return data ? BCH_CSUM_crc64 : BCH_CSUM_crc64_nonzero;
+       case BCH_CSUM_OPT_xxhash:
+            return BCH_CSUM_xxhash;
+       default:
+            BUG();
+       }
+}
+
+static inline enum bch_csum_type bch2_data_checksum_type(struct bch_fs *c,
+                                                        unsigned opt)
 {
        if (c->sb.encryption_type)
                return c->opts.wide_macs
-                       ? BCH_CSUM_CHACHA20_POLY1305_128
-                       : BCH_CSUM_CHACHA20_POLY1305_80;
+                       ? BCH_CSUM_chacha20_poly1305_128
+                       : BCH_CSUM_chacha20_poly1305_80;
 
-       return c->opts.data_checksum;
+       return bch2_csum_opt_to_type(opt, true);
 }
 
-static inline unsigned bch2_meta_checksum_type(struct bch_fs *c)
+static inline enum bch_csum_type bch2_meta_checksum_type(struct bch_fs *c)
 {
-       return c->sb.encryption_type
-               ? BCH_CSUM_CHACHA20_POLY1305_128
-               : c->opts.metadata_checksum;
+       if (c->sb.encryption_type)
+               return BCH_CSUM_chacha20_poly1305_128;
+
+       return bch2_csum_opt_to_type(c->opts.metadata_checksum, false);
 }
 
+static const unsigned bch2_compression_opt_to_type[] = {
+#define x(t, n) [BCH_COMPRESSION_OPT_##t] = BCH_COMPRESSION_TYPE_##t,
+       BCH_COMPRESSION_OPTS()
+#undef x
+};
+
 static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
                                           unsigned type)
 {
@@ -75,14 +127,7 @@ static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
        return true;
 }
 
-static const unsigned bch_crc_bytes[] = {
-       [BCH_CSUM_NONE]                         = 0,
-       [BCH_CSUM_CRC32C]                       = 4,
-       [BCH_CSUM_CRC64]                        = 8,
-       [BCH_CSUM_CHACHA20_POLY1305_80]         = 10,
-       [BCH_CSUM_CHACHA20_POLY1305_128]        = 16,
-};
-
+/* returns true if not equal */
 static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
 {
        /*
@@ -95,12 +140,38 @@ static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
 /* for skipping ahead and encrypting/decrypting at an offset: */
 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
 {
-       EBUG_ON(offset & (CHACHA20_BLOCK_SIZE - 1));
+       EBUG_ON(offset & (CHACHA_BLOCK_SIZE - 1));
 
-       le32_add_cpu(&nonce.d[0], offset / CHACHA20_BLOCK_SIZE);
+       le32_add_cpu(&nonce.d[0], offset / CHACHA_BLOCK_SIZE);
        return nonce;
 }
 
+static inline struct nonce null_nonce(void)
+{
+       struct nonce ret;
+
+       memset(&ret, 0, sizeof(ret));
+       return ret;
+}
+
+static inline struct nonce extent_nonce(struct bversion version,
+                                       struct bch_extent_crc_unpacked crc)
+{
+       unsigned compression_type = crc_is_compressed(crc)
+               ? crc.compression_type
+               : 0;
+       unsigned size = compression_type ? crc.uncompressed_size : 0;
+       struct nonce nonce = (struct nonce) {{
+               [0] = cpu_to_le32(size << 22),
+               [1] = cpu_to_le32(version.lo),
+               [2] = cpu_to_le32(version.lo >> 32),
+               [3] = cpu_to_le32(version.hi|
+                                 (compression_type << 24))^BCH_NONCE_EXTENT,
+       }};
+
+       return nonce_add(nonce, crc.nonce << 9);
+}
+
 static inline bool bch2_key_is_encrypted(struct bch_encrypted_key *key)
 {
        return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
@@ -130,4 +201,4 @@ static inline struct nonce bch2_sb_key_nonce(struct bch_fs *c)
        }};
 }
 
-#endif /* _BCACHE_CHECKSUM_H */
+#endif /* _BCACHEFS_CHECKSUM_H */