]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal_io.c
Update bcachefs sources to cbb2e45634dd bcachefs: fix simulateously upgrading & downg...
[bcachefs-tools-debian] / libbcachefs / journal_io.c
index 3c3a6c4dc6372f8f8ebc50168e97083237426bfa..b0f4dd491e1205d28c6af528fb59696cdbc4dc9c 100644 (file)
@@ -27,11 +27,15 @@ static struct nonce journal_nonce(const struct jset *jset)
        }};
 }
 
-static bool jset_csum_good(struct bch_fs *c, struct jset *j)
+static bool jset_csum_good(struct bch_fs *c, struct jset *j, struct bch_csum *csum)
 {
-       return bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j)) &&
-               !bch2_crc_cmp(j->csum,
-                             csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j));
+       if (!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j))) {
+               *csum = (struct bch_csum) {};
+               return false;
+       }
+
+       *csum = csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j);
+       return !bch2_crc_cmp(j->csum, *csum);
 }
 
 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
@@ -409,8 +413,10 @@ static int journal_entry_btree_root_validate(struct bch_fs *c,
                return 0;
        }
 
-       return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
-                                   version, big_endian, flags);
+       ret = journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
+                                  version, big_endian, flags);
+       if (ret == FSCK_DELETED_KEY)
+               ret = 0;
 fsck_err:
        return ret;
 }
@@ -781,7 +787,6 @@ void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
                                 enum bkey_invalid_flags flags)
 {
-       struct jset_entry *entry;
        unsigned version = le32_to_cpu(jset->version);
        int ret = 0;
 
@@ -933,6 +938,7 @@ static int journal_read_bucket(struct bch_dev *ca,
        u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
            end = offset + ca->mi.bucket_size;
        bool saw_bad = false, csum_good;
+       struct printbuf err = PRINTBUF;
        int ret = 0;
 
        pr_debug("reading %u", bucket);
@@ -965,7 +971,7 @@ reread:
                                 * found on a different device, and missing or
                                 * no journal entries will be handled later
                                 */
-                               return 0;
+                               goto out;
                        }
 
                        j = buf->data;
@@ -982,12 +988,12 @@ reread:
                                ret = journal_read_buf_realloc(buf,
                                                        vstruct_bytes(j));
                                if (ret)
-                                       return ret;
+                                       goto err;
                        }
                        goto reread;
                case JOURNAL_ENTRY_NONE:
                        if (!saw_bad)
-                               return 0;
+                               goto out;
                        /*
                         * On checksum error we don't really trust the size
                         * field of the journal entry we read, so try reading
@@ -996,7 +1002,7 @@ reread:
                        sectors = block_sectors(c);
                        goto next_block;
                default:
-                       return ret;
+                       goto err;
                }
 
                /*
@@ -1006,20 +1012,28 @@ reread:
                 * bucket:
                 */
                if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
-                       return 0;
+                       goto out;
 
                ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
 
-               csum_good = jset_csum_good(c, j);
+               enum bch_csum_type csum_type = JSET_CSUM_TYPE(j);
+               struct bch_csum csum;
+               csum_good = jset_csum_good(c, j, &csum);
+
                if (bch2_dev_io_err_on(!csum_good, ca, BCH_MEMBER_ERROR_checksum,
-                                      "journal checksum error"))
+                                      "%s",
+                                      (printbuf_reset(&err),
+                                       prt_str(&err, "journal "),
+                                       bch2_csum_err_msg(&err, csum_type, j->csum, csum),
+                                       err.buf)))
                        saw_bad = true;
 
                ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
                             j->encrypted_start,
                             vstruct_end(j) - (void *) j->encrypted_start);
                bch2_fs_fatal_err_on(ret, c,
-                               "error decrypting journal entry: %i", ret);
+                               "error decrypting journal entry: %s",
+                               bch2_err_str(ret));
 
                mutex_lock(&jlist->lock);
                ret = journal_entry_add(c, ca, (struct journal_ptr) {
@@ -1038,7 +1052,7 @@ reread:
                case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
                        break;
                default:
-                       return ret;
+                       goto err;
                }
 next_block:
                pr_debug("next");
@@ -1047,7 +1061,11 @@ next_block:
                j = ((void *) j) + (sectors << 9);
        }
 
-       return 0;
+out:
+       ret = 0;
+err:
+       printbuf_exit(&err);
+       return ret;
 }
 
 static CLOSURE_CALLBACK(bch2_journal_read_device)
@@ -1169,8 +1187,6 @@ int bch2_journal_read(struct bch_fs *c,
        struct journal_list jlist;
        struct journal_replay *i, **_i, *prev = NULL;
        struct genradix_iter radix_iter;
-       struct bch_dev *ca;
-       unsigned iter;
        struct printbuf buf = PRINTBUF;
        bool degraded = false, last_write_torn = false;
        u64 seq;
@@ -1181,7 +1197,7 @@ int bch2_journal_read(struct bch_fs *c,
        jlist.last_seq = 0;
        jlist.ret = 0;
 
-       for_each_member_device(ca, c, iter) {
+       for_each_member_device(c, ca) {
                if (!c->opts.fsck &&
                    !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
                        continue;
@@ -1347,7 +1363,7 @@ int bch2_journal_read(struct bch_fs *c,
                        continue;
 
                for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
-                       ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
+                       struct bch_dev *ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
 
                        if (!i->ptrs[ptr].csum_good)
                                bch_err_dev_offset(ca, i->ptrs[ptr].sector,
@@ -1679,7 +1695,6 @@ static CLOSURE_CALLBACK(do_journal_write)
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
        struct bch_dev *ca;
        struct journal_buf *w = journal_last_unwritten_buf(j);
-       struct bch_extent_ptr *ptr;
        struct bio *bio;
        unsigned sectors = vstruct_sectors(w->data, c->block_bits);
 
@@ -1723,7 +1738,7 @@ static CLOSURE_CALLBACK(do_journal_write)
 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
 {
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
-       struct jset_entry *start, *end, *i;
+       struct jset_entry *start, *end;
        struct jset *jset = w->data;
        struct journal_keys_to_wb wb = { NULL };
        unsigned sectors, bytes, u64s;
@@ -1891,12 +1906,11 @@ CLOSURE_CALLBACK(bch2_journal_write)
 {
        closure_type(j, struct journal, io);
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
-       struct bch_dev *ca;
        struct journal_buf *w = journal_last_unwritten_buf(j);
        struct bch_replicas_padded replicas;
        struct bio *bio;
        struct printbuf journal_debug_buf = PRINTBUF;
-       unsigned i, nr_rw_members = 0;
+       unsigned nr_rw_members = 0;
        int ret;
 
        BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
@@ -1956,7 +1970,7 @@ CLOSURE_CALLBACK(bch2_journal_write)
        if (c->opts.nochanges)
                goto no_io;
 
-       for_each_rw_member(ca, c, i)
+       for_each_rw_member(c, ca)
                nr_rw_members++;
 
        if (nr_rw_members > 1)
@@ -1973,7 +1987,7 @@ CLOSURE_CALLBACK(bch2_journal_write)
                goto err;
 
        if (!JSET_NO_FLUSH(w->data) && w->separate_flush) {
-               for_each_rw_member(ca, c, i) {
+               for_each_rw_member(c, ca) {
                        percpu_ref_get(&ca->io_ref);
 
                        bio = ca->journal.bio;