bcachefs: New error message helpers
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 20 Jun 2023 17:49:25 +0000 (13:49 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:10:04 +0000 (17:10 -0400)
Add two new helpers for printing error messages with __func__ and
bch2_err_str():
 - bch_err_fn
 - bch_err_msg

Also kill the old error strings in the recovery path, which were causing
us to incorrectly report memory allocation failures - they're not needed
anymore.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
14 files changed:
fs/bcachefs/alloc_background.c
fs/bcachefs/backpointers.c
fs/bcachefs/bcachefs.h
fs/bcachefs/btree_gc.c
fs/bcachefs/buckets.c
fs/bcachefs/ec.c
fs/bcachefs/fsck.c
fs/bcachefs/journal.c
fs/bcachefs/lru.c
fs/bcachefs/move.c
fs/bcachefs/quota.c
fs/bcachefs/recovery.c
fs/bcachefs/subvolume.c
fs/bcachefs/tests.c

index 6c8bcb210ad9eafb41b333ace26b064da2ac71f7..f68330b488474163cbcd3140a207280606bc7996 100644 (file)
@@ -577,7 +577,7 @@ int bch2_alloc_read(struct bch_fs *c)
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "error reading alloc info: %s", bch2_err_str(ret));
+               bch_err_fn(c, ret);
 
        return ret;
 }
@@ -684,8 +684,7 @@ int bch2_bucket_gens_init(struct bch_fs *c)
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
-
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -730,7 +729,7 @@ int bch2_bucket_gens_read(struct bch_fs *c)
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "error reading alloc info: %s", bch2_err_str(ret));
+               bch_err_fn(c, ret);
 
        return ret;
 }
@@ -1521,7 +1520,9 @@ bkey_err:
                bch2_check_bucket_gens_key(&trans, &iter, k));
 err:
        bch2_trans_exit(&trans);
-       return ret < 0 ? ret : 0;
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
 }
 
 static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
@@ -1599,20 +1600,18 @@ fsck_err:
 
 int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
 {
-       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        int ret = 0;
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       for_each_btree_key_commit(&trans, iter, BTREE_ID_alloc,
-                       POS_MIN, BTREE_ITER_PREFETCH, k,
-                       NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
-               bch2_check_alloc_to_lru_ref(&trans, &iter));
-
-       bch2_trans_exit(&trans);
-       return ret < 0 ? ret : 0;
+       ret = bch2_trans_run(c,
+               for_each_btree_key_commit(&trans, iter, BTREE_ID_alloc,
+                               POS_MIN, BTREE_ITER_PREFETCH, k,
+                               NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+                       bch2_check_alloc_to_lru_ref(&trans, &iter)));
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
 }
 
 static int bch2_discard_one_bucket(struct btree_trans *trans,
@@ -2024,6 +2023,7 @@ int bch2_fs_freespace_init(struct bch_fs *c)
                ret = bch2_dev_freespace_init(c, ca, &last_updated);
                if (ret) {
                        percpu_ref_put(&ca->ref);
+                       bch_err_fn(c, ret);
                        return ret;
                }
        }
@@ -2032,11 +2032,10 @@ int bch2_fs_freespace_init(struct bch_fs *c)
                mutex_lock(&c->sb_lock);
                bch2_write_super(c);
                mutex_unlock(&c->sb_lock);
-
                bch_verbose(c, "done initializing freespace");
        }
 
-       return ret;
+       return 0;
 }
 
 /* Bucket IO clocks: */
index 11201064d9a4522a373ad7b4e26e8fc0cd5a759a..2641ebef6ae4023e6417fa9ee719007598f9fdb6 100644 (file)
@@ -404,12 +404,16 @@ int bch2_check_btree_backpointers(struct bch_fs *c)
 {
        struct btree_iter iter;
        struct bkey_s_c k;
+       int ret;
 
-       return bch2_trans_run(c,
+       ret = bch2_trans_run(c,
                for_each_btree_key_commit(&trans, iter,
                        BTREE_ID_backpointers, POS_MIN, 0, k,
                        NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
                  bch2_check_btree_backpointer(&trans, &iter, k)));
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
 }
 
 struct bpos_level {
@@ -769,6 +773,8 @@ int bch2_check_extents_to_backpointers(struct bch_fs *c)
        }
        bch2_trans_exit(&trans);
 
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -872,5 +878,7 @@ int bch2_check_backpointers_to_extents(struct bch_fs *c)
        }
        bch2_trans_exit(&trans);
 
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
index 4199b42db64023ff3d433fa38029f9ecf606d306..b8d50fe64b3c9c437083d85c7d814dd6ded24150 100644 (file)
@@ -291,6 +291,11 @@ do {                                                                       \
 #define bch_err_inum_offset_ratelimited(c, _inum, _offset, fmt, ...) \
        printk_ratelimited(KERN_ERR bch2_fmt_inum_offset(c, _inum, _offset, fmt), ##__VA_ARGS__)
 
+#define bch_err_fn(_c, _ret)                                           \
+        bch_err(_c, "%s(): error %s", __func__, bch2_err_str(_ret))
+#define bch_err_msg(_c, _ret, _msg)                                    \
+        bch_err(_c, "%s(): error " _msg " %s", __func__, bch2_err_str(_ret))
+
 #define bch_verbose(c, fmt, ...)                                       \
 do {                                                                   \
        if ((c)->opts.verbose)                                          \
index 8477e721b63c5ec34ac7c444601e72596e103836..4fbd2e545ac246be99b20c9649339513c2e9e423 100644 (file)
@@ -404,8 +404,7 @@ again:
                }
 
                if (ret) {
-                       bch_err(c, "%s: error getting btree node: %s",
-                               __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "getting btree node");
                        break;
                }
 
@@ -473,8 +472,7 @@ again:
                ret = PTR_ERR_OR_ZERO(cur);
 
                if (ret) {
-                       bch_err(c, "%s: error getting btree node: %s",
-                               __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "getting btree node");
                        goto err;
                }
 
@@ -687,7 +685,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
 
                new = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
                if (!new) {
-                       bch_err(c, "%s: error allocating new key", __func__);
+                       bch_err_msg(c, ret, "allocating new key");
                        ret = -BCH_ERR_ENOMEM_gc_repair_key;
                        goto err;
                }
@@ -814,7 +812,7 @@ static int bch2_gc_mark_key(struct btree_trans *trans, enum btree_id btree_id,
 fsck_err:
 err:
        if (ret)
-               bch_err(c, "error from %s(): %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -919,11 +917,8 @@ static int bch2_gc_btree_init_recurse(struct btree_trans *trans, struct btree *b
 
                ret = bch2_gc_mark_key(trans, b->c.btree_id, b->c.level,
                                       false, &k, true);
-               if (ret) {
-                       bch_err(c, "%s: error from bch2_gc_mark_key: %s",
-                               __func__, bch2_err_str(ret));
+               if (ret)
                        goto fsck_err;
-               }
 
                if (b->c.level) {
                        bch2_bkey_buf_reassemble(&cur, c, k);
@@ -981,8 +976,7 @@ static int bch2_gc_btree_init_recurse(struct btree_trans *trans, struct btree *b
                                        continue;
                                }
                        } else if (ret) {
-                               bch_err(c, "%s: error getting btree node: %s",
-                                       __func__, bch2_err_str(ret));
+                               bch_err_msg(c, ret, "getting btree node");
                                break;
                        }
 
@@ -1049,7 +1043,7 @@ fsck_err:
        six_unlock_read(&b->c.lock);
 
        if (ret < 0)
-               bch_err(c, "error from %s(): %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        printbuf_exit(&buf);
        return ret;
 }
@@ -1079,7 +1073,7 @@ static int bch2_gc_btrees(struct bch_fs *c, bool initial, bool metadata_only)
                        : bch2_gc_btree(&trans, ids[i], initial, metadata_only);
 
        if (ret < 0)
-               bch_err(c, "error from %s(): %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
 
        bch2_trans_exit(&trans);
        return ret;
@@ -1277,7 +1271,7 @@ fsck_err:
        if (ca)
                percpu_ref_put(&ca->ref);
        if (ret)
-               bch_err(c, "error from %s(): %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
 
        percpu_up_write(&c->mark_lock);
        printbuf_exit(&buf);
@@ -1883,6 +1877,9 @@ out:
         * allocator thread - issue wakeup in case they blocked on gc_lock:
         */
        closure_wake_up(&c->freelist_wait);
+
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
index fbe0cd0a7de365c4b00ab95446a0f070d3324b77..d770dc949661adf8cf361b242385e4e43c8ad7a2 100644 (file)
@@ -1988,7 +1988,10 @@ static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
 
 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
 {
-       return bch2_trans_run(c, __bch2_trans_mark_dev_sb(&trans, ca));
+       int ret = bch2_trans_run(c, __bch2_trans_mark_dev_sb(&trans, ca));
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
 }
 
 /* Disk reservations: */
index b7e3889b114b402491f308e9cec12a0e0909bf98..0c5c291e844e3715b6400be051d7abb482ccdccb 100644 (file)
@@ -798,7 +798,7 @@ static void ec_stripe_delete_work(struct work_struct *work)
                ret = commit_do(&trans, NULL, NULL, BTREE_INSERT_NOFAIL,
                                ec_stripe_delete(&trans, idx));
                if (ret) {
-                       bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
+                       bch_err_fn(c, ret);
                        break;
                }
        }
@@ -1845,7 +1845,7 @@ int bch2_stripes_read(struct bch_fs *c)
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "error reading stripes: %i", ret);
+               bch_err_fn(c, ret);
 
        return ret;
 }
index dcc55cbd3808ff21fef4edf0a9eb263af461e106..194e8d474e86db2c2552a273ea76ee526820e0e4 100644 (file)
@@ -303,7 +303,7 @@ static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
        bch2_trans_iter_exit(trans, &iter);
 err:
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -983,7 +983,7 @@ static int check_inode(struct btree_trans *trans,
 err:
 fsck_err:
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1009,7 +1009,7 @@ static int check_inodes(struct bch_fs *c, bool full)
        bch2_trans_exit(&trans);
        snapshots_seen_exit(&s);
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1129,7 +1129,7 @@ static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
        }
 fsck_err:
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        if (!ret && trans_was_restarted(trans, restart_count))
                ret = -BCH_ERR_transaction_restart_nested;
        return ret;
@@ -1353,7 +1353,7 @@ fsck_err:
        printbuf_exit(&buf);
 
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1395,7 +1395,7 @@ static int check_extents(struct bch_fs *c)
        snapshots_seen_exit(&s);
 
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1434,7 +1434,7 @@ static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
        }
 fsck_err:
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        if (!ret && trans_was_restarted(trans, restart_count))
                ret = -BCH_ERR_transaction_restart_nested;
        return ret;
@@ -1555,7 +1555,7 @@ fsck_err:
        printbuf_exit(&buf);
 
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1725,7 +1725,7 @@ fsck_err:
        printbuf_exit(&buf);
 
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1764,7 +1764,7 @@ static int check_dirents(struct bch_fs *c)
        inode_walker_exit(&target);
 
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1801,7 +1801,7 @@ static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
        ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
 fsck_err:
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1833,7 +1833,7 @@ static int check_xattrs(struct bch_fs *c)
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1896,12 +1896,18 @@ fsck_err:
 noinline_for_stack
 static int check_root(struct bch_fs *c)
 {
+       int ret;
+
        bch_verbose(c, "checking root directory");
 
-       return bch2_trans_do(c, NULL, NULL,
+       ret = bch2_trans_do(c, NULL, NULL,
                             BTREE_INSERT_NOFAIL|
                             BTREE_INSERT_LAZY_RW,
                check_root_trans(&trans));
+
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
 }
 
 struct pathbuf_entry {
@@ -2038,7 +2044,7 @@ static int check_path(struct btree_trans *trans,
        }
 fsck_err:
        if (ret)
-               bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -2081,10 +2087,11 @@ static int check_directory_structure(struct bch_fs *c)
                        break;
        }
        bch2_trans_iter_exit(&trans, &iter);
-
+       bch2_trans_exit(&trans);
        darray_exit(&path);
 
-       bch2_trans_exit(&trans);
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -2364,6 +2371,8 @@ static int check_nlinks(struct bch_fs *c)
 
        kvfree(links.d);
 
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -2397,7 +2406,6 @@ static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
 noinline_for_stack
 static int fix_reflink_p(struct bch_fs *c)
 {
-       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        int ret;
@@ -2407,15 +2415,16 @@ static int fix_reflink_p(struct bch_fs *c)
 
        bch_verbose(c, "fixing reflink_p keys");
 
-       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
-
-       ret = for_each_btree_key_commit(&trans, iter,
-                       BTREE_ID_extents, POS_MIN,
-                       BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
-                       NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
-               fix_reflink_p_key(&trans, &iter, k));
+       ret = bch2_trans_run(c,
+               for_each_btree_key_commit(&trans, iter,
+                               BTREE_ID_extents, POS_MIN,
+                               BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
+                               BTREE_ITER_ALL_SNAPSHOTS, k,
+                               NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+                       fix_reflink_p_key(&trans, &iter, k)));
 
-       bch2_trans_exit(&trans);
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
index 433c97844f36f7058f5501600e6651f256706add..64332c78a6bbcd460715732949cfb8f11c26ac41 100644 (file)
@@ -978,7 +978,7 @@ int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
        }
 
        if (ret)
-               bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
 unlock:
        up_write(&c->state_lock);
        return ret;
@@ -987,9 +987,12 @@ unlock:
 int bch2_dev_journal_alloc(struct bch_dev *ca)
 {
        unsigned nr;
+       int ret;
 
-       if (dynamic_fault("bcachefs:add:journal_alloc"))
-               return -BCH_ERR_ENOMEM_set_nr_journal_buckets;
+       if (dynamic_fault("bcachefs:add:journal_alloc")) {
+               ret = -BCH_ERR_ENOMEM_set_nr_journal_buckets;
+               goto err;
+       }
 
        /* 1/128th of the device by default: */
        nr = ca->mi.nbuckets >> 7;
@@ -1003,7 +1006,11 @@ int bch2_dev_journal_alloc(struct bch_dev *ca)
                     min(1 << 13,
                         (1 << 24) / ca->mi.bucket_size));
 
-       return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
+       ret = __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
+err:
+       if (ret)
+               bch_err_fn(ca, ret);
+       return ret;
 }
 
 /* startup/shutdown: */
index 4f23e88f6ae18df71a3adfef24b5535a493b4092..e04c037f0c0115bc188e1b8e327665f2efe1164e 100644 (file)
@@ -160,20 +160,18 @@ fsck_err:
 
 int bch2_check_lrus(struct bch_fs *c)
 {
-       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        struct bpos last_flushed_pos = POS_MIN;
        int ret = 0;
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       ret = for_each_btree_key_commit(&trans, iter,
-                       BTREE_ID_lru, POS_MIN, BTREE_ITER_PREFETCH, k,
-                       NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
-               bch2_check_lru_key(&trans, &iter, k, &last_flushed_pos));
-
-       bch2_trans_exit(&trans);
+       ret = bch2_trans_run(c,
+               for_each_btree_key_commit(&trans, iter,
+                               BTREE_ID_lru, POS_MIN, BTREE_ITER_PREFETCH, k,
+                               NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+                       bch2_check_lru_key(&trans, &iter, k, &last_flushed_pos)));
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 
 }
index fd629136824b7414e1eca56238bd8812e08d0147..37fb3784a2f97fa6e18ac72ab7211cc2341b5adc 100644 (file)
@@ -690,7 +690,7 @@ int __bch2_evacuate_bucket(struct btree_trans *trans,
        bch2_trans_iter_exit(trans, &iter);
 
        if (ret) {
-               bch_err(c, "%s: error looking up alloc key: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "looking up alloc key");
                goto err;
        }
 
@@ -701,7 +701,7 @@ int __bch2_evacuate_bucket(struct btree_trans *trans,
 
        ret = bch2_btree_write_buffer_flush(trans);
        if (ret) {
-               bch_err(c, "%s: error flushing btree write buffer: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "flushing btree write buffer");
                goto err;
        }
 
@@ -904,7 +904,7 @@ next:
        bch2_trans_exit(&trans);
 
        if (ret)
-               bch_err(c, "error in %s(): %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
 
        bch2_btree_interior_updates_flush(c);
 
@@ -1029,6 +1029,8 @@ int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
                mutex_unlock(&c->sb_lock);
        }
 
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
index d20ec9764108deb0417d2a7cbda867f850ebb596..7e1f1828ab2074d503f3ce9b3d464e4e710d3c88 100644 (file)
@@ -621,10 +621,11 @@ int bch2_fs_quota_read(struct bch_fs *c)
              for_each_btree_key2(&trans, iter, BTREE_ID_inodes,
                        POS_MIN, BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
                bch2_fs_quota_read_inode(&trans, &iter, k));
-       if (ret)
-               bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
 
        bch2_trans_exit(&trans);
+
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
index e4983d144483072af1ece1e8dc69fc07b752f96a..09c9d4058f829d4e7395712146074ebd7cab7f3c 100644 (file)
@@ -685,6 +685,9 @@ static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
                bch2_journal_log_msg(c, "journal replay finished");
 err:
        kvfree(keys_sorted);
+
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1034,9 +1037,6 @@ static int bch2_fs_initialize_subvolumes(struct bch_fs *c)
        root_tree.k.p.offset            = 1;
        root_tree.v.master_subvol       = cpu_to_le32(1);
        root_tree.v.root_snapshot       = cpu_to_le32(U32_MAX);
-       ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees,
-                               &root_tree.k_i,
-                               NULL, NULL, 0);
 
        bkey_snapshot_init(&root_snapshot.k_i);
        root_snapshot.k.p.offset = U32_MAX;
@@ -1046,28 +1046,27 @@ static int bch2_fs_initialize_subvolumes(struct bch_fs *c)
        root_snapshot.v.tree    = cpu_to_le32(1);
        SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
 
-       ret = bch2_btree_insert(c, BTREE_ID_snapshots,
-                               &root_snapshot.k_i,
-                               NULL, NULL, 0);
-       if (ret)
-               return ret;
-
        bkey_subvolume_init(&root_volume.k_i);
        root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
        root_volume.v.flags     = 0;
        root_volume.v.snapshot  = cpu_to_le32(U32_MAX);
        root_volume.v.inode     = cpu_to_le64(BCACHEFS_ROOT_INO);
 
-       ret = bch2_btree_insert(c, BTREE_ID_subvolumes,
-                               &root_volume.k_i,
-                               NULL, NULL, 0);
+       ret =   bch2_btree_insert(c, BTREE_ID_snapshot_trees,
+                                 &root_tree.k_i,
+                                 NULL, NULL, 0) ?:
+               bch2_btree_insert(c, BTREE_ID_snapshots,
+                                 &root_snapshot.k_i,
+                                 NULL, NULL, 0) ?:
+               bch2_btree_insert(c, BTREE_ID_subvolumes,
+                                 &root_volume.k_i,
+                                 NULL, NULL, 0);
        if (ret)
-               return ret;
-
-       return 0;
+               bch_err_fn(c, ret);
+       return ret;
 }
 
-static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
+static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
 {
        struct btree_iter iter;
        struct bkey_s_c k;
@@ -1097,9 +1096,19 @@ err:
        return ret;
 }
 
+/* set bi_subvol on root inode */
+noinline_for_stack
+static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
+{
+       int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
+                               __bch2_fs_upgrade_for_subvolumes(&trans));
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
+}
+
 int bch2_fs_recovery(struct bch_fs *c)
 {
-       const char *err = "cannot allocate memory";
        struct bch_sb_field_clean *clean = NULL;
        struct jset *last_journal_entry = NULL;
        u64 last_seq, blacklist_seq, journal_seq;
@@ -1137,12 +1146,6 @@ int bch2_fs_recovery(struct bch_fs *c)
                goto err;
        }
 
-       if (!(c->sb.features & (1ULL << BCH_FEATURE_alloc_v2))) {
-               bch_info(c, "alloc_v2 feature bit not set, fsck required");
-               c->opts.fsck = true;
-               c->opts.fix_errors = FSCK_OPT_YES;
-       }
-
        if (!c->opts.nochanges) {
                if (c->sb.version < bcachefs_metadata_required_upgrade_below) {
                        bch_info(c, "version %s (%u) prior to %s (%u), upgrade and fsck required",
@@ -1286,34 +1289,28 @@ use_clean:
                goto err;
 
        bch_verbose(c, "starting alloc read");
-       err = "error reading allocation information";
-
        down_read(&c->gc_lock);
        ret = c->sb.version < bcachefs_metadata_version_bucket_gens
                ? bch2_alloc_read(c)
                : bch2_bucket_gens_read(c);
        up_read(&c->gc_lock);
-
        if (ret)
                goto err;
        bch_verbose(c, "alloc read done");
 
        bch_verbose(c, "starting stripes_read");
-       err = "error reading stripes";
        ret = bch2_stripes_read(c);
        if (ret)
                goto err;
        bch_verbose(c, "stripes_read done");
 
        if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
-               err = "error creating root snapshot node";
                ret = bch2_fs_initialize_subvolumes(c);
                if (ret)
                        goto err;
        }
 
        bch_verbose(c, "reading snapshots table");
-       err = "error reading snapshots table";
        ret = bch2_fs_snapshots_start(c);
        if (ret)
                goto err;
@@ -1323,7 +1320,6 @@ use_clean:
                bool metadata_only = c->opts.norecovery;
 
                bch_info(c, "checking allocations");
-               err = "error checking allocations";
                ret = bch2_gc(c, true, metadata_only);
                if (ret)
                        goto err;
@@ -1334,7 +1330,6 @@ use_clean:
                set_bit(BCH_FS_MAY_GO_RW, &c->flags);
 
                bch_info(c, "starting journal replay, %zu keys", c->journal_keys.nr);
-               err = "journal replay failed";
                ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
@@ -1342,7 +1337,6 @@ use_clean:
                        bch_info(c, "journal replay done");
 
                bch_info(c, "checking need_discard and freespace btrees");
-               err = "error checking need_discard and freespace btrees";
                ret = bch2_check_alloc_info(c);
                if (ret)
                        goto err;
@@ -1351,7 +1345,6 @@ use_clean:
                set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags);
 
                bch_info(c, "checking lrus");
-               err = "error checking lrus";
                ret = bch2_check_lrus(c);
                if (ret)
                        goto err;
@@ -1359,21 +1352,18 @@ use_clean:
                set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags);
 
                bch_info(c, "checking backpointers to alloc keys");
-               err = "error checking backpointers to alloc keys";
                ret = bch2_check_btree_backpointers(c);
                if (ret)
                        goto err;
                bch_verbose(c, "done checking backpointers to alloc keys");
 
                bch_info(c, "checking backpointers to extents");
-               err = "error checking backpointers to extents";
                ret = bch2_check_backpointers_to_extents(c);
                if (ret)
                        goto err;
                bch_verbose(c, "done checking backpointers to extents");
 
                bch_info(c, "checking extents to backpointers");
-               err = "error checking extents to backpointers";
                ret = bch2_check_extents_to_backpointers(c);
                if (ret)
                        goto err;
@@ -1381,7 +1371,6 @@ use_clean:
                set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags);
 
                bch_info(c, "checking alloc to lru refs");
-               err = "error checking alloc to lru refs";
                ret = bch2_check_alloc_to_lru_refs(c);
                if (ret)
                        goto err;
@@ -1401,7 +1390,6 @@ use_clean:
                set_bit(BCH_FS_MAY_GO_RW, &c->flags);
 
                bch_verbose(c, "starting journal replay, %zu keys", c->journal_keys.nr);
-               err = "journal replay failed";
                ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
@@ -1409,7 +1397,6 @@ use_clean:
                        bch_info(c, "journal replay done");
        }
 
-       err = "error initializing freespace";
        ret = bch2_fs_freespace_init(c);
        if (ret)
                goto err;
@@ -1417,7 +1404,6 @@ use_clean:
        if (c->sb.version < bcachefs_metadata_version_bucket_gens &&
            c->opts.version_upgrade) {
                bch_info(c, "initializing bucket_gens");
-               err = "error initializing bucket gens";
                ret = bch2_bucket_gens_init(c);
                if (ret)
                        goto err;
@@ -1425,24 +1411,18 @@ use_clean:
        }
 
        if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
-               /* set bi_subvol on root inode */
-               err = "error upgrade root inode for subvolumes";
-               ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
-                                   bch2_fs_upgrade_for_subvolumes(&trans));
+               ret = bch2_fs_upgrade_for_subvolumes(c);
                if (ret)
                        goto err;
        }
 
        if (c->opts.fsck) {
-               bch_info(c, "starting fsck");
-               err = "error in fsck";
                ret = bch2_fsck_full(c);
                if (ret)
                        goto err;
                bch_verbose(c, "fsck done");
        } else if (!c->sb.clean) {
                bch_verbose(c, "checking for deleted inodes");
-               err = "error in recovery";
                ret = bch2_fsck_walk_inodes_only(c);
                if (ret)
                        goto err;
@@ -1489,11 +1469,8 @@ use_clean:
                bch2_move_stats_init(&stats, "recovery");
 
                bch_info(c, "scanning for old btree nodes");
-               ret = bch2_fs_read_write(c);
-               if (ret)
-                       goto err;
-
-               ret = bch2_scan_old_btree_nodes(c, &stats);
+               ret =   bch2_fs_read_write(c) ?:
+                       bch2_scan_old_btree_nodes(c, &stats);
                if (ret)
                        goto err;
                bch_info(c, "scanning for old btree nodes done");
@@ -1521,7 +1498,7 @@ out:
        }
 
        if (ret)
-               bch_err(c, "Error in recovery: %s (%s)", err, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        else
                bch_verbose(c, "ret %s", bch2_err_str(ret));
        return ret;
@@ -1536,7 +1513,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        struct bch_inode_unpacked root_inode, lostfound_inode;
        struct bkey_inode_buf packed_inode;
        struct qstr lostfound = QSTR("lost+found");
-       const char *err = "cannot allocate memory";
        struct bch_dev *ca;
        unsigned i;
        int ret;
@@ -1570,7 +1546,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        for_each_online_member(ca, c, i)
                bch2_dev_usage_init(ca);
 
-       err = "unable to allocate journal buckets";
        for_each_online_member(ca, c, i) {
                ret = bch2_dev_journal_alloc(ca);
                if (ret) {
@@ -1586,7 +1561,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        bch2_fs_journal_start(&c->journal, 1);
        bch2_journal_set_replay_done(&c->journal);
 
-       err = "error going read-write";
        ret = bch2_fs_read_write_early(c);
        if (ret)
                goto err;
@@ -1596,7 +1570,6 @@ int bch2_fs_initialize(struct bch_fs *c)
         * btree updates
         */
        bch_verbose(c, "marking superblocks");
-       err = "error marking superblock and journal";
        for_each_member_device(ca, c, i) {
                ret = bch2_trans_mark_dev_sb(c, ca);
                if (ret) {
@@ -1607,19 +1580,15 @@ int bch2_fs_initialize(struct bch_fs *c)
                ca->new_fs_bucket_idx = 0;
        }
 
-       bch_verbose(c, "initializing freespace");
-       err = "error initializing freespace";
        ret = bch2_fs_freespace_init(c);
        if (ret)
                goto err;
 
-       err = "error creating root snapshot node";
        ret = bch2_fs_initialize_subvolumes(c);
        if (ret)
                goto err;
 
        bch_verbose(c, "reading snapshots table");
-       err = "error reading snapshots table";
        ret = bch2_fs_snapshots_start(c);
        if (ret)
                goto err;
@@ -1631,16 +1600,16 @@ int bch2_fs_initialize(struct bch_fs *c)
        bch2_inode_pack(&packed_inode, &root_inode);
        packed_inode.inode.k.p.snapshot = U32_MAX;
 
-       err = "error creating root directory";
        ret = bch2_btree_insert(c, BTREE_ID_inodes,
                                &packed_inode.inode.k_i,
                                NULL, NULL, 0);
-       if (ret)
+       if (ret) {
+               bch_err_msg(c, ret, "creating root directory");
                goto err;
+       }
 
        bch2_inode_init_early(c, &lostfound_inode);
 
-       err = "error creating lost+found";
        ret = bch2_trans_do(c, NULL, NULL, 0,
                bch2_create_trans(&trans,
                                  BCACHEFS_ROOT_SUBVOL_INUM,
@@ -1649,7 +1618,7 @@ int bch2_fs_initialize(struct bch_fs *c)
                                  0, 0, S_IFDIR|0700, 0,
                                  NULL, NULL, (subvol_inum) { 0 }, 0));
        if (ret) {
-               bch_err(c, "error creating lost+found");
+               bch_err_msg(c, ret, "creating lost+found");
                goto err;
        }
 
@@ -1659,10 +1628,11 @@ int bch2_fs_initialize(struct bch_fs *c)
                        goto err;
        }
 
-       err = "error writing first journal entry";
        ret = bch2_journal_flush(&c->journal);
-       if (ret)
+       if (ret) {
+               bch_err_msg(c, ret, "writing first journal entry");
                goto err;
+       }
 
        mutex_lock(&c->sb_lock);
        SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
@@ -1673,6 +1643,6 @@ int bch2_fs_initialize(struct bch_fs *c)
 
        return 0;
 err:
-       pr_err("Error initializing new filesystem: %s (%s)", err, bch2_err_str(ret));
+       bch_err_fn(ca, ret);
        return ret;
 }
index 4b6631c229ee0233117479905e099797d2da35bb..828644e6c71435b633a10de10a92096f4a841306 100644 (file)
@@ -623,7 +623,7 @@ int bch2_fs_check_snapshots(struct bch_fs *c)
                        NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
                check_snapshot(&trans, &iter, k)));
        if (ret)
-               bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -702,8 +702,7 @@ int bch2_fs_check_subvols(struct bch_fs *c)
                        NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
                check_subvol(&trans, &iter, k)));
        if (ret)
-               bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
-
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -724,7 +723,7 @@ int bch2_fs_snapshots_start(struct bch_fs *c)
                        bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
                        bch2_snapshot_set_equiv(&trans, k)));
        if (ret)
-               bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1123,6 +1122,8 @@ int bch2_delete_dead_snapshots(struct bch_fs *c)
 err:
        darray_exit(&deleted);
        bch2_trans_exit(&trans);
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
index 35df3f940542c2776afaa8299ab476c8a9606ba8..50d69a5634bd4c42ff2bcdaffccddc24680f2736 100644 (file)
@@ -47,7 +47,7 @@ static int test_delete(struct bch_fs *c, u64 nr)
                bch2_btree_iter_traverse(&iter) ?:
                bch2_trans_update(&trans, &iter, &k.k_i, 0));
        if (ret) {
-               bch_err(c, "%s(): update error in: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "update error");
                goto err;
        }
 
@@ -56,7 +56,7 @@ static int test_delete(struct bch_fs *c, u64 nr)
                bch2_btree_iter_traverse(&iter) ?:
                bch2_btree_delete_at(&trans, &iter, 0));
        if (ret) {
-               bch_err(c, "%s(): delete error (first): %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "delete error (first)");
                goto err;
        }
 
@@ -65,7 +65,7 @@ static int test_delete(struct bch_fs *c, u64 nr)
                bch2_btree_iter_traverse(&iter) ?:
                bch2_btree_delete_at(&trans, &iter, 0));
        if (ret) {
-               bch_err(c, "%s(): delete error (second): %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "delete error (second)");
                goto err;
        }
 err:
@@ -93,7 +93,7 @@ static int test_delete_written(struct bch_fs *c, u64 nr)
                bch2_btree_iter_traverse(&iter) ?:
                bch2_trans_update(&trans, &iter, &k.k_i, 0));
        if (ret) {
-               bch_err(c, "%s(): update error: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "update error");
                goto err;
        }
 
@@ -104,7 +104,7 @@ static int test_delete_written(struct bch_fs *c, u64 nr)
                bch2_btree_iter_traverse(&iter) ?:
                bch2_btree_delete_at(&trans, &iter, 0));
        if (ret) {
-               bch_err(c, "%s(): delete error: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "delete error");
                goto err;
        }
 err:
@@ -137,7 +137,7 @@ static int test_iterate(struct bch_fs *c, u64 nr)
                ret = bch2_btree_insert(c, BTREE_ID_xattrs, &k.k_i,
                                        NULL, NULL, 0);
                if (ret) {
-                       bch_err(c, "%s(): insert error: %s", __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "insert error");
                        goto err;
                }
        }
@@ -153,7 +153,7 @@ static int test_iterate(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret) {
-               bch_err(c, "%s(): error iterating forwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards");
                goto err;
        }
 
@@ -168,7 +168,7 @@ static int test_iterate(struct bch_fs *c, u64 nr)
                        0;
                }));
        if (ret) {
-               bch_err(c, "%s(): error iterating backwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating backwards");
                goto err;
        }
 
@@ -204,7 +204,7 @@ static int test_iterate_extents(struct bch_fs *c, u64 nr)
                ret = bch2_btree_insert(c, BTREE_ID_extents, &k.k_i,
                                        NULL, NULL, 0);
                if (ret) {
-                       bch_err(c, "%s(): insert error: %s", __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "insert error");
                        goto err;
                }
        }
@@ -221,7 +221,7 @@ static int test_iterate_extents(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret) {
-               bch_err(c, "%s(): error iterating forwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards");
                goto err;
        }
 
@@ -237,7 +237,7 @@ static int test_iterate_extents(struct bch_fs *c, u64 nr)
                        0;
                }));
        if (ret) {
-               bch_err(c, "%s(): error iterating backwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating backwards");
                goto err;
        }
 
@@ -272,7 +272,7 @@ static int test_iterate_slots(struct bch_fs *c, u64 nr)
                ret = bch2_btree_insert(c, BTREE_ID_xattrs, &k.k_i,
                                        NULL, NULL, 0);
                if (ret) {
-                       bch_err(c, "%s(): insert error: %s", __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "insert error");
                        goto err;
                }
        }
@@ -289,7 +289,7 @@ static int test_iterate_slots(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret) {
-               bch_err(c, "%s(): error iterating forwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards");
                goto err;
        }
 
@@ -312,7 +312,7 @@ static int test_iterate_slots(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret < 0) {
-               bch_err(c, "%s(): error iterating forwards by slots: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards by slots");
                goto err;
        }
        ret = 0;
@@ -346,7 +346,7 @@ static int test_iterate_slots_extents(struct bch_fs *c, u64 nr)
                ret = bch2_btree_insert(c, BTREE_ID_extents, &k.k_i,
                                        NULL, NULL, 0);
                if (ret) {
-                       bch_err(c, "%s(): insert error: %s", __func__, bch2_err_str(ret));
+                       bch_err_msg(c, ret, "insert error");
                        goto err;
                }
        }
@@ -364,7 +364,7 @@ static int test_iterate_slots_extents(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret) {
-               bch_err(c, "%s(): error iterating forwards: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards");
                goto err;
        }
 
@@ -387,7 +387,7 @@ static int test_iterate_slots_extents(struct bch_fs *c, u64 nr)
                0;
        }));
        if (ret) {
-               bch_err(c, "%s(): error iterating forwards by slots: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "error iterating forwards by slots");
                goto err;
        }
        ret = 0;
@@ -461,7 +461,7 @@ static int insert_test_extent(struct bch_fs *c,
        ret = bch2_btree_insert(c, BTREE_ID_extents, &k.k_i,
                                NULL, NULL, 0);
        if (ret)
-               bch_err(c, "%s(): insert error: %s", __func__, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -560,7 +560,7 @@ static int test_snapshots(struct bch_fs *c, u64 nr)
 
        ret = test_snapshot_filter(c, snapids[0], snapids[1]);
        if (ret) {
-               bch_err(c, "%s(): err from test_snapshot_filter: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(c, ret, "from test_snapshot_filter");
                return ret;
        }
 
@@ -674,7 +674,7 @@ static int rand_mixed_trans(struct btree_trans *trans,
        k = bch2_btree_iter_peek(iter);
        ret = bkey_err(k);
        if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
-               bch_err(trans->c, "%s(): lookup error: %s", __func__, bch2_err_str(ret));
+               bch_err_msg(trans->c, ret, "lookup error");
        if (ret)
                return ret;