bcachefs: Convert -ENOENT to private error codes
authorKent Overstreet <kent.overstreet@linux.dev>
Sat, 27 May 2023 23:59:59 +0000 (19:59 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:10:03 +0000 (17:10 -0400)
As with previous conversions, replace -ENOENT uses with more informative
private error codes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 files changed:
fs/bcachefs/acl.c
fs/bcachefs/btree_iter.h
fs/bcachefs/buckets.c
fs/bcachefs/chardev.c
fs/bcachefs/errcode.h
fs/bcachefs/fs-common.c
fs/bcachefs/fs-ioctl.c
fs/bcachefs/fs.c
fs/bcachefs/fsck.c
fs/bcachefs/inode.c
fs/bcachefs/move.c
fs/bcachefs/movinggc.c
fs/bcachefs/quota.c
fs/bcachefs/recovery.c
fs/bcachefs/str_hash.h
fs/bcachefs/subvolume.c
fs/bcachefs/super.c
fs/bcachefs/xattr.c

index 2bf58aa89f71603d5e8e257b4fab0693f50adaf6..3fe108bc2f0899c7bba0e3647b0e090784f1fd7a 100644 (file)
@@ -236,7 +236,7 @@ retry:
        if (ret) {
                if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
                        goto retry;
-               if (ret != -ENOENT)
+               if (!bch2_err_matches(ret, ENOENT))
                        acl = ERR_PTR(ret);
                goto out;
        }
@@ -287,7 +287,7 @@ int bch2_set_acl_trans(struct btree_trans *trans, subvol_inum inum,
                                       inum, &search);
        }
 
-       return ret == -ENOENT ? 0 : ret;
+       return bch2_err_matches(ret, ENOENT) ? 0 : ret;
 }
 
 int bch2_set_acl(struct mnt_idmap *idmap,
@@ -368,7 +368,7 @@ int bch2_acl_chmod(struct btree_trans *trans, subvol_inum inum,
                        &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
                        BTREE_ITER_INTENT);
        if (ret)
-               return ret == -ENOENT ? 0 : ret;
+               return bch2_err_matches(ret, ENOENT) ? 0 : ret;
 
        k = bch2_btree_iter_peek_slot(&iter);
        xattr = bkey_s_c_to_xattr(k);
index 9a4dbf358fe566d74f10cc600fda99839a80891a..5e5e2a5c715cc22e948a4b54469f32f883137371 100644 (file)
@@ -516,7 +516,7 @@ static inline struct bkey_s_c __bch2_bkey_get_iter(struct btree_trans *trans,
        k = bch2_btree_iter_peek_slot(iter);
 
        if (!bkey_err(k) && type && k.k->type != type)
-               k = bkey_s_c_err(-ENOENT);
+               k = bkey_s_c_err(-BCH_ERR_ENOENT_bkey_type_mismatch);
        if (unlikely(bkey_err(k)))
                bch2_trans_iter_exit(trans, iter);
        return k;
index bd144182c1e12bc2014f7b1359fa03987e77ebf9..adf3bd0e4a8f8d39d3eb9344900929488988b25a 100644 (file)
@@ -1455,7 +1455,7 @@ static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
                        BTREE_ITER_WITH_UPDATES, stripe);
        ret = PTR_ERR_OR_ZERO(s);
        if (unlikely(ret)) {
-               bch2_trans_inconsistent_on(ret == -ENOENT, trans,
+               bch2_trans_inconsistent_on(bch2_err_matches(ret, ENOENT), trans,
                        "pointer to nonexistent stripe %llu",
                        (u64) p.ec.idx);
                goto err;
index 28854a6c31b963ecf23d7263e3a814793e29816b..fb603df099a5b43d00b6a2bfb783a28403442a97 100644 (file)
@@ -578,7 +578,7 @@ static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
                        return i;
                }
 
-       return -ENOENT;
+       return -BCH_ERR_ENOENT_dev_idx_not_found;
 }
 
 static long bch2_ioctl_disk_resize(struct bch_fs *c,
index c8ac08e5548bcd798c7f0de98e4a3b787a5a10b6..acf9b92f9ab0f64e100e8eca81220b12366a085a 100644 (file)
        x(ENOSPC,                       ENOSPC_sb_crypt)                        \
        x(ENOSPC,                       ENOSPC_btree_slot)                      \
        x(ENOSPC,                       ENOSPC_snapshot_tree)                   \
+       x(ENOENT,                       ENOENT_bkey_type_mismatch)              \
+       x(ENOENT,                       ENOENT_str_hash_lookup)                 \
+       x(ENOENT,                       ENOENT_str_hash_set_must_replace)       \
+       x(ENOENT,                       ENOENT_inode)                           \
+       x(ENOENT,                       ENOENT_not_subvol)                      \
+       x(ENOENT,                       ENOENT_directory_dead)                  \
+       x(ENOENT,                       ENOENT_subvolume)                       \
+       x(ENOENT,                       ENOENT_snapshot_tree)                   \
+       x(ENOENT,                       ENOENT_dirent_doesnt_match_inode)       \
+       x(ENOENT,                       ENOENT_dev_not_found)                   \
+       x(ENOENT,                       ENOENT_dev_idx_not_found)               \
        x(0,                            open_buckets_empty)                     \
        x(0,                            freelist_empty)                         \
        x(BCH_ERR_freelist_empty,       no_buckets_found)                       \
index 1f2e1fc4f6b22292e4932b9f82a45767460647a4..bb5305441f275938d4f76535a9b9bf1aaccf7934 100644 (file)
@@ -281,7 +281,7 @@ int bch2_unlink_trans(struct btree_trans *trans,
        }
 
        if (deleting_snapshot && !inode_u->bi_subvol) {
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_not_subvol;
                goto err;
        }
 
index 269af9393824c14348a2c62b5544e8abb70b2b3c..dfa1bf73c8541187abbe87e7127213d337ee24e7 100644 (file)
@@ -382,7 +382,7 @@ retry:
 
        dir = dst_path.dentry->d_inode;
        if (IS_DEADDIR(dir)) {
-               error = -ENOENT;
+               error = -BCH_ERR_ENOENT_directory_dead;
                goto err3;
        }
 
index 99082820e30b2e78bf816bde557f72c3bb0ac7c1..ba7aff6b8a51b6d20410eba7f6027f6c42c7bb93 100644 (file)
@@ -105,7 +105,7 @@ retry:
        if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
                goto retry;
 
-       bch2_fs_fatal_err_on(ret == -ENOENT, c,
+       bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
                             "inode %u:%llu not found when updating",
                             inode_inum(inode).subvol,
                             inode_inum(inode).inum);
@@ -1261,14 +1261,14 @@ retry:
                        goto err;
 
                if (k.k->type != KEY_TYPE_dirent) {
-                       ret = -ENOENT;
+                       ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
                        goto err;
                }
 
                d = bkey_s_c_to_dirent(k);
                ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
                if (ret > 0)
-                       ret = -ENOENT;
+                       ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
                if (ret)
                        goto err;
 
index 1b3ee66265c95b454c395598c6f390c248cec632..dcc55cbd3808ff21fef4edf0a9eb263af461e106 100644 (file)
@@ -78,7 +78,7 @@ static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
                                          snapshot, &s);
        if (!ret)
                *subvol = le32_to_cpu(s.subvol);
-       else if (ret == -ENOENT)
+       else if (bch2_err_matches(ret, ENOENT))
                bch_err(trans->c, "snapshot %u not fonud", snapshot);
        return ret;
 
@@ -119,7 +119,7 @@ static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
                goto err;
 
        if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_inode;
                goto err;
        }
 
@@ -148,7 +148,7 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
 
        ret = bkey_is_inode(k.k)
                ? bch2_inode_unpack(k, inode)
-               : -ENOENT;
+               : -BCH_ERR_ENOENT_inode;
        if (!ret)
                *snapshot = iter.pos.snapshot;
 err:
@@ -333,7 +333,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
 
        ret = __lookup_dirent(trans, root_hash_info, root_inum,
                            &lostfound_str, &inum, &d_type);
-       if (ret == -ENOENT) {
+       if (bch2_err_matches(ret, ENOENT)) {
                bch_notice(c, "creating lost+found");
                goto create_lostfound;
        }
@@ -1088,7 +1088,7 @@ static int inode_backpointer_exists(struct btree_trans *trans,
                        SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
        ret = bkey_err(d);
        if (ret)
-               return ret == -ENOENT ? 0 : ret;
+               return bch2_err_matches(ret, ENOENT) ? 0 : ret;
 
        ret = dirent_points_to_inode(d, inode);
        bch2_trans_iter_exit(trans, &iter);
@@ -1653,7 +1653,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
 
                ret = __subvol_lookup(trans, target_subvol,
                                      &target_snapshot, &target_inum);
-               if (ret && ret != -ENOENT)
+               if (ret && !bch2_err_matches(ret, ENOENT))
                        goto err;
 
                if (fsck_err_on(ret, c,
@@ -1665,7 +1665,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
 
                ret = __lookup_inode(trans, target_inum,
                                   &subvol_root, &target_snapshot);
-               if (ret && ret != -ENOENT)
+               if (ret && !bch2_err_matches(ret, ENOENT))
                        goto err;
 
                if (fsck_err_on(ret, c,
@@ -1846,7 +1846,7 @@ static int check_root_trans(struct btree_trans *trans)
        int ret;
 
        ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
-       if (ret && ret != -ENOENT)
+       if (ret && !bch2_err_matches(ret, ENOENT))
                return ret;
 
        if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
@@ -1873,7 +1873,7 @@ static int check_root_trans(struct btree_trans *trans)
        }
 
        ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
-       if (ret && ret != -ENOENT)
+       if (ret && !bch2_err_matches(ret, ENOENT))
                return ret;
 
        if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
@@ -1972,15 +1972,15 @@ static int check_path(struct btree_trans *trans,
                        PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
                                          SPOS(inode->bi_dir, inode->bi_dir_offset,
                                               parent_snapshot))).k));
-               if (ret && ret != -ENOENT)
+               if (ret && !bch2_err_matches(ret, ENOENT))
                        break;
 
                if (!ret && !dirent_points_to_inode(d, inode)) {
                        bch2_trans_iter_exit(trans, &dirent_iter);
-                       ret = -ENOENT;
+                       ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
                }
 
-               if (ret == -ENOENT) {
+               if (bch2_err_matches(ret, ENOENT)) {
                        if (fsck_err(c,  "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
                                     inode->bi_inum, snapshot,
                                     bch2_d_type_str(inode_d_type(inode)),
index ddcd7b125f3223014721b79ec4b32dafee642893..64e8d1f8a2fa791885891e071f67656f1e1774f0 100644 (file)
@@ -336,7 +336,7 @@ int bch2_inode_peek(struct btree_trans *trans,
        if (ret)
                return ret;
 
-       ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
+       ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
        if (ret)
                goto err;
 
index 2ec30a3fd19377937768172386b49cf2ef093610..fd629136824b7414e1eca56238bd8812e08d0147 100644 (file)
@@ -427,7 +427,7 @@ static int lookup_inode(struct btree_trans *trans, struct bpos pos,
                goto err;
 
        if (!k.k || !bkey_eq(k.k->p, pos)) {
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_inode;
                goto err;
        }
 
index 0d96346d5040a76a073880d4f274f6e691d9c2c8..6750767276f29ca1edfd1328e60fc99b6494036b 100644 (file)
@@ -236,7 +236,7 @@ err:
        darray_exit(&buckets);
 
        /* no entries in LRU btree found, or got to end: */
-       if (ret == -ENOENT)
+       if (bch2_err_matches(ret, ENOENT))
                ret = 0;
 
        if (ret < 0 && !bch2_err_matches(ret, EROFS))
index 56ba82cae19d5f0ef76678be8067a61aa246fa1d..d20ec9764108deb0417d2a7cbda867f850ebb596 100644 (file)
@@ -900,7 +900,7 @@ static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
        ret = -ENOENT;
 found:
        mutex_unlock(&q->lock);
-       return ret;
+       return bch2_err_class(ret);
 }
 
 static int bch2_set_quota_trans(struct btree_trans *trans,
@@ -960,7 +960,7 @@ static int bch2_set_quota(struct super_block *sb, struct kqid qid,
                            bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
                __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
 
-       return ret;
+       return bch2_err_class(ret);
 }
 
 const struct quotactl_ops bch2_quotactl_operations = {
index af76c029fb6a530cfb5589dd1a7dfc1213a9c382..e4983d144483072af1ece1e8dc69fc07b752f96a 100644 (file)
@@ -1082,7 +1082,7 @@ static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
 
        if (!bkey_is_inode(k.k)) {
                bch_err(trans->c, "root inode not found");
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_inode;
                goto err;
        }
 
index 6178ae620ff1fdb8c1999cdbd5664538fc9a7578..ae21a8cca1b49d4d9bbfe2b38a330c78b9abc023 100644 (file)
@@ -181,7 +181,7 @@ bch2_hash_lookup(struct btree_trans *trans,
        }
        bch2_trans_iter_exit(trans, iter);
 
-       return ret ?: -ENOENT;
+       return ret ?: -BCH_ERR_ENOENT_str_hash_lookup;
 }
 
 static __always_inline int
@@ -288,7 +288,7 @@ found:
 not_found:
 
        if (!found && (flags & BCH_HASH_SET_MUST_REPLACE)) {
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_str_hash_set_must_replace;
        } else if (found && (flags & BCH_HASH_SET_MUST_CREATE)) {
                ret = -EEXIST;
        } else {
index 388fa12bbd8b497e9eac733461553c487547978e..f07b3e2b3226a0c7918ce7032f08509c28adc2f2 100644 (file)
@@ -37,8 +37,12 @@ int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
                              struct bch_snapshot_tree *s)
 {
-       return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
-                                      BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
+       int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
+                                         BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
+
+       if (bch2_err_matches(ret, ENOENT))
+               ret = -BCH_ERR_ENOENT_snapshot_tree;
+       return ret;
 }
 
 static struct bkey_i_snapshot_tree *
@@ -284,6 +288,7 @@ static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
        struct btree_iter iter;
        struct bkey_s_c k;
        struct bkey_s_c_subvolume s;
+       bool found = false;
        int ret;
 
        for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
@@ -296,14 +301,14 @@ static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
                        continue;
                if (!BCH_SUBVOLUME_SNAP(s.v)) {
                        *subvol_id = s.k->p.offset;
-                       goto found;
+                       found = true;
+                       break;
                }
        }
-       ret = ret ?: -ENOENT;
-found:
+
        bch2_trans_iter_exit(trans, &iter);
 
-       if (bch2_err_matches(ret, ENOENT)) {
+       if (!ret && !found) {
                struct bkey_i_subvolume *s;
 
                *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
@@ -1217,7 +1222,7 @@ int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
        k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
                               BTREE_ITER_CACHED|
                               BTREE_ITER_WITH_UPDATES);
-       ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
+       ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -BCH_ERR_ENOENT_subvolume;
 
        if (likely(!ret))
                *snapid = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
@@ -1444,7 +1449,7 @@ int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
                                BTREE_ITER_CACHED, subvolume);
                ret = PTR_ERR_OR_ZERO(src_subvol);
                if (unlikely(ret)) {
-                       bch2_fs_inconsistent_on(ret == -ENOENT, c,
+                       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
                                                "subvolume %u not found", src_subvolid);
                        goto err;
                }
index af6cc73d93561811b006fd4b776f84bd713276f2..8f0cbd7ada82f9464ddced2114d172d0cbe9a987 100644 (file)
@@ -1833,7 +1833,7 @@ struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
        for_each_member_device_rcu(ca, c, i, NULL)
                if (!strcmp(name, ca->name))
                        goto found;
-       ca = ERR_PTR(-ENOENT);
+       ca = ERR_PTR(-BCH_ERR_ENOENT_dev_not_found);
 found:
        rcu_read_unlock();
 
index 448eb446946b11c8bd715f2158c73ac082bd0965..05c65d94c00fc03090520290037689cff83f0d86 100644 (file)
@@ -163,7 +163,7 @@ static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info
 err2:
        bch2_trans_iter_exit(trans, &iter);
 err1:
-       return ret == -ENOENT ? -ENODATA : ret;
+       return ret < 0 && bch2_err_matches(ret, ENOENT) ? -ENODATA : ret;
 }
 
 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
@@ -229,7 +229,7 @@ int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
                                       hash_info, inum, &search);
        }
 
-       if (ret == -ENOENT)
+       if (bch2_err_matches(ret, ENOENT))
                ret = flags & XATTR_REPLACE ? -ENODATA : 0;
 
        return ret;
@@ -589,7 +589,7 @@ err:
             opt_id == Opt_background_target))
                bch2_rebalance_add_work(c, inode->v.i_blocks);
 
-       return ret;
+       return bch2_err_class(ret);
 }
 
 static const struct xattr_handler bch_xattr_bcachefs_handler = {