bcachefs: More errcode cleanup
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 20 Nov 2022 03:39:08 +0000 (22:39 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:48 +0000 (17:09 -0400)
We shouldn't be overloading standard error codes now that we have
provisions for bcachefs-specific errorcodes: this patch converts super.c
and super-io.c to per error site errcodes, with a bit of cleanup.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
19 files changed:
fs/bcachefs/alloc_background.c
fs/bcachefs/bkey_methods.c
fs/bcachefs/dirent.c
fs/bcachefs/disk_groups.c
fs/bcachefs/ec.c
fs/bcachefs/errcode.h
fs/bcachefs/extents.c
fs/bcachefs/fs.c
fs/bcachefs/inode.c
fs/bcachefs/journal_sb.c
fs/bcachefs/journal_seq_blacklist.c
fs/bcachefs/lru.c
fs/bcachefs/quota.c
fs/bcachefs/reflink.c
fs/bcachefs/replicas.c
fs/bcachefs/subvolume.c
fs/bcachefs/super-io.c
fs/bcachefs/super.c
fs/bcachefs/xattr.c

index a0b9fa30260afaeca97c233a5f9cf4460c551f8e..cef5de13a6e41d755b2425d4389639555a0c0582 100644 (file)
@@ -302,7 +302,7 @@ int bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v)) {
                prt_printf(err, "incorrect value size (%zu < %u)",
                       bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -315,7 +315,7 @@ int bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
 
        if (bch2_alloc_unpack_v2(&u, k)) {
                prt_printf(err, "unpack error");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -328,7 +328,7 @@ int bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
 
        if (bch2_alloc_unpack_v3(&u, k)) {
                prt_printf(err, "unpack error");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -342,14 +342,14 @@ int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) != sizeof(struct bch_alloc_v4)) {
                prt_printf(err, "bad val size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_alloc_v4));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (rw == WRITE) {
                if (alloc_data_type(*a.v, a.v->data_type) != a.v->data_type) {
                        prt_printf(err, "invalid data type (got %u should be %u)",
                               a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
 
                switch (a.v->data_type) {
@@ -360,7 +360,7 @@ int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
                            a.v->cached_sectors ||
                            a.v->stripe) {
                                prt_printf(err, "empty data type free but have data");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
                        break;
                case BCH_DATA_sb:
@@ -371,7 +371,7 @@ int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
                        if (!a.v->dirty_sectors) {
                                prt_printf(err, "data_type %s but dirty_sectors==0",
                                       bch2_data_types[a.v->data_type]);
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
                        break;
                case BCH_DATA_cached:
@@ -379,20 +379,20 @@ int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
                            a.v->dirty_sectors ||
                            a.v->stripe) {
                                prt_printf(err, "data type inconsistency");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
 
                        if (!a.v->io_time[READ] &&
                            test_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags)) {
                                prt_printf(err, "cached bucket with read_time == 0");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
                        break;
                case BCH_DATA_stripe:
                        if (!a.v->stripe) {
                                prt_printf(err, "data_type %s but stripe==0",
                                       bch2_data_types[a.v->data_type]);
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
                        break;
                }
index 7fcd6ca40b9311dc32aa71bb00e0a0142ed9e052..29809da5e9cf229e7f2365d963f3133faf5d3ee5 100644 (file)
@@ -42,7 +42,7 @@ static int empty_val_key_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k)) {
                prt_printf(err, "incorrect value size (%zu != 0)",
                       bkey_val_bytes(k.k));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -58,7 +58,7 @@ static int key_type_cookie_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) != sizeof(struct bch_cookie)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_cookie));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -99,7 +99,7 @@ static int key_type_set_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_cookie));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -127,7 +127,7 @@ int bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k,
 {
        if (k.k->type >= KEY_TYPE_MAX) {
                prt_printf(err, "invalid type (%u >= %u)", k.k->type, KEY_TYPE_MAX);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return bch2_bkey_ops[k.k->type].key_invalid(c, k, rw, err);
@@ -203,30 +203,30 @@ int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
 {
        if (k.k->u64s < BKEY_U64s) {
                prt_printf(err, "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (!(bch2_key_types_allowed[type] & (1U << k.k->type))) {
                prt_printf(err, "invalid key type for btree %s (%s)",
                           bch2_btree_ids[type], bch2_bkey_types[k.k->type]);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (btree_node_type_is_extents(type) && !bkey_whiteout(k.k)) {
                if (k.k->size == 0) {
                        prt_printf(err, "size == 0");
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
 
                if (k.k->size > k.k->p.offset) {
                        prt_printf(err, "size greater than offset (%u > %llu)",
                               k.k->size, k.k->p.offset);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
        } else {
                if (k.k->size) {
                        prt_printf(err, "size != 0");
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
        }
 
@@ -234,20 +234,20 @@ int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
            !btree_type_has_snapshots(type) &&
            k.k->p.snapshot) {
                prt_printf(err, "nonzero snapshot");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (type != BKEY_TYPE_btree &&
            btree_type_has_snapshots(type) &&
            !k.k->p.snapshot) {
                prt_printf(err, "snapshot == 0");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (type != BKEY_TYPE_btree &&
            bkey_eq(k.k->p, POS_MAX)) {
                prt_printf(err, "key at POS_MAX");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -266,12 +266,12 @@ int bch2_bkey_in_btree_node(struct btree *b, struct bkey_s_c k,
 {
        if (bpos_lt(k.k->p, b->data->min_key)) {
                prt_printf(err, "key before start of btree node");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bpos_gt(k.k->p, b->data->max_key)) {
                prt_printf(err, "key past end of btree node");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index c2126f39369bed737b6225cb5bcd6f39ee7b51a9..f1838b7c45eeafd752f6ed229e97dc5d445c9771 100644 (file)
@@ -92,46 +92,46 @@ int bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*d.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        len = bch2_dirent_name_bytes(d);
        if (!len) {
                prt_printf(err, "empty name");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_u64s(k.k) > dirent_val_u64s(len)) {
                prt_printf(err, "value too big (%zu > %u)",
                       bkey_val_u64s(k.k), dirent_val_u64s(len));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (len > BCH_NAME_MAX) {
                prt_printf(err, "dirent name too big (%u > %u)",
                       len, BCH_NAME_MAX);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (len == 1 && !memcmp(d.v->d_name, ".", 1)) {
                prt_printf(err, "invalid name");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (len == 2 && !memcmp(d.v->d_name, "..", 2)) {
                prt_printf(err, "invalid name");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (memchr(d.v->d_name, '/', len)) {
                prt_printf(err, "invalid name");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (d.v->d_type != DT_SUBVOL &&
            le64_to_cpu(d.v->d_inum) == d.k->p.inode) {
                prt_printf(err, "dirent points to own directory");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index 6b81f35861aca327dd6b50e42b2194edf8fc7cec..fcd5dbff248d2d70c6afa88eb056b933c98fc83e 100644 (file)
@@ -27,7 +27,7 @@ static int bch2_sb_disk_groups_validate(struct bch_sb *sb,
        struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
        unsigned nr_groups = disk_groups_nr(groups);
        unsigned i, len;
-       int ret = -EINVAL;
+       int ret = 0;
 
        for (i = 0; i < sb->nr_devices; i++) {
                struct bch_member *m = mi->members + i;
@@ -41,12 +41,12 @@ static int bch2_sb_disk_groups_validate(struct bch_sb *sb,
                if (g >= nr_groups) {
                        prt_printf(err, "disk %u has invalid label %u (have %u)",
                               i, g, nr_groups);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_disk_groups;
                }
 
                if (BCH_GROUP_DELETED(&groups->entries[g])) {
                        prt_printf(err, "disk %u has deleted label %u", i, g);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_disk_groups;
                }
        }
 
@@ -62,7 +62,7 @@ static int bch2_sb_disk_groups_validate(struct bch_sb *sb,
                len = strnlen(g->label, sizeof(g->label));
                if (!len) {
                        prt_printf(err, "label %u empty", i);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_disk_groups;
                }
        }
 
@@ -79,13 +79,12 @@ static int bch2_sb_disk_groups_validate(struct bch_sb *sb,
                        prt_printf(err, "duplicate label %llu.%.*s",
                               BCH_GROUP_PARENT(g),
                               (int) sizeof(g->label), g->label);
+                       ret = -BCH_ERR_invalid_sb_disk_groups;
                        goto err;
                }
-
-       ret = 0;
 err:
        kfree(sorted);
-       return 0;
+       return ret;
 }
 
 static void bch2_sb_disk_groups_to_text(struct printbuf *out,
index 503a47b39ad14d6968f86db71bbf2a988c39a261..c855ea025f0e336e7aeb8701cdbb025144cb35e3 100644 (file)
@@ -109,24 +109,24 @@ int bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k,
 
        if (bkey_eq(k.k->p, POS_MIN)) {
                prt_printf(err, "stripe at POS_MIN");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (k.k->p.inode) {
                prt_printf(err, "nonzero inode field");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_bytes(k.k) < sizeof(*s)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*s));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
                prt_printf(err, "incorrect value size (%zu < %u)",
                       bkey_val_u64s(k.k), stripe_val_u64s(s));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return bch2_bkey_ptrs_invalid(c, k, rw, err);
index 3ec5808dcbd9681d5cf5b69580ea1f68d182e799..dc388864be6f074e5746424eaee6e15d67277bc9 100644 (file)
        x(BCH_ERR_fsck,                 fsck_repair_unimplemented)              \
        x(BCH_ERR_fsck,                 fsck_repair_impossible)                 \
        x(0,                            need_snapshot_cleanup)                  \
-       x(0,                            need_topology_repair)
+       x(0,                            need_topology_repair)                   \
+       x(EINVAL,                       device_state_not_allowed)               \
+       x(EINVAL,                       member_info_missing)                    \
+       x(EINVAL,                       mismatched_block_size)                  \
+       x(EINVAL,                       block_size_too_small)                   \
+       x(EINVAL,                       bucket_size_too_small)                  \
+       x(EINVAL,                       device_size_too_small)                  \
+       x(EINVAL,                       device_not_a_member_of_filesystem)      \
+       x(EINVAL,                       device_has_been_removed)                \
+       x(EINVAL,                       device_already_online)                  \
+       x(EINVAL,                       insufficient_devices_to_start)          \
+       x(EINVAL,                       invalid)                                \
+       x(BCH_ERR_invalid,              invalid_sb)                             \
+       x(BCH_ERR_invalid_sb,           invalid_sb_magic)                       \
+       x(BCH_ERR_invalid_sb,           invalid_sb_version)                     \
+       x(BCH_ERR_invalid_sb,           invalid_sb_features)                    \
+       x(BCH_ERR_invalid_sb,           invalid_sb_too_big)                     \
+       x(BCH_ERR_invalid_sb,           invalid_sb_csum_type)                   \
+       x(BCH_ERR_invalid_sb,           invalid_sb_csum)                        \
+       x(BCH_ERR_invalid_sb,           invalid_sb_block_size)                  \
+       x(BCH_ERR_invalid_sb,           invalid_sb_uuid)                        \
+       x(BCH_ERR_invalid_sb,           invalid_sb_too_many_members)            \
+       x(BCH_ERR_invalid_sb,           invalid_sb_dev_idx)                     \
+       x(BCH_ERR_invalid_sb,           invalid_sb_time_precision)              \
+       x(BCH_ERR_invalid_sb,           invalid_sb_field_size)                  \
+       x(BCH_ERR_invalid_sb,           invalid_sb_layout)                      \
+       x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_type)                 \
+       x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_nr_superblocks)       \
+       x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_superblocks_overlap)  \
+       x(BCH_ERR_invalid_sb,           invalid_sb_members_missing)             \
+       x(BCH_ERR_invalid_sb,           invalid_sb_members)                     \
+       x(BCH_ERR_invalid_sb,           invalid_sb_disk_groups)                 \
+       x(BCH_ERR_invalid_sb,           invalid_sb_replicas)                    \
+       x(BCH_ERR_invalid_sb,           invalid_sb_journal)                     \
+       x(BCH_ERR_invalid_sb,           invalid_sb_journal_seq_blacklist)       \
+       x(BCH_ERR_invalid_sb,           invalid_sb_crypt)                       \
+       x(BCH_ERR_invalid_sb,           invalid_sb_clean)                       \
+       x(BCH_ERR_invalid_sb,           invalid_sb_quota)                       \
+       x(BCH_ERR_invalid,              invalid_bkey)                           \
 
 enum bch_errcode {
        BCH_ERR_START           = 2048,
index e3bc39bee197927a2f4363d45c44f17e2bc550ce..422adca7230b4f5d927af0cc2dfe4e7395735179 100644 (file)
@@ -163,7 +163,7 @@ int bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX) {
                prt_printf(err, "value too big (%zu > %u)",
                       bkey_val_u64s(k.k), BCH_REPLICAS_MAX);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return bch2_bkey_ptrs_invalid(c, k, rw, err);
@@ -183,20 +183,20 @@ int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) <= sizeof(*bp.v)) {
                prt_printf(err, "value too small (%zu <= %zu)",
                       bkey_val_bytes(k.k), sizeof(*bp.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
                prt_printf(err, "value too big (%zu > %zu)",
                       bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (c->sb.version < bcachefs_metadata_version_snapshot &&
            bp.v->min_key.snapshot) {
                prt_printf(err, "invalid min_key.snapshot (%u != 0)",
                       bp.v->min_key.snapshot);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return bch2_bkey_ptrs_invalid(c, k, rw, err);
@@ -387,13 +387,13 @@ int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(*r.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
                prt_printf(err, "invalid nr_replicas (%u)",
                       r.v->nr_replicas);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -1054,14 +1054,14 @@ static int extent_ptr_invalid(const struct bch_fs *c,
 
        if (!bch2_dev_exists2(c, ptr->dev)) {
                prt_printf(err, "pointer to invalid device (%u)", ptr->dev);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        ca = bch_dev_bkey_exists(c, ptr->dev);
        bkey_for_each_ptr(ptrs, ptr2)
                if (ptr != ptr2 && ptr->dev == ptr2->dev) {
                        prt_printf(err, "multiple pointers to same device (%u)", ptr->dev);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
 
        bucket = sector_to_bucket_and_offset(ca, ptr->offset, &bucket_offset);
@@ -1069,19 +1069,19 @@ static int extent_ptr_invalid(const struct bch_fs *c,
        if (bucket >= ca->mi.nbuckets) {
                prt_printf(err, "pointer past last bucket (%llu > %llu)",
                       bucket, ca->mi.nbuckets);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket)) {
                prt_printf(err, "pointer before first bucket (%llu < %u)",
                       bucket, ca->mi.first_bucket);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bucket_offset + size_ondisk > ca->mi.bucket_size) {
                prt_printf(err, "pointer spans multiple buckets (%u + %u > %u)",
                       bucket_offset, size_ondisk, ca->mi.bucket_size);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -1105,13 +1105,13 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
                if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX) {
                        prt_printf(err, "invalid extent entry type (got %u, max %u)",
                               __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
 
                if (bkey_is_btree_ptr(k.k) &&
                    !extent_entry_is_ptr(entry)) {
                        prt_printf(err, "has non ptr field");
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
 
                switch (extent_entry_type(entry)) {
@@ -1130,19 +1130,19 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
                        if (crc.offset + crc.live_size >
                            crc.uncompressed_size) {
                                prt_printf(err, "checksum offset + key size > uncompressed size");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
 
                        size_ondisk = crc.compressed_size;
 
                        if (!bch2_checksum_type_valid(c, crc.csum_type)) {
                                prt_printf(err, "invalid checksum type");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
 
                        if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR) {
                                prt_printf(err, "invalid compression type");
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_bkey;
                        }
 
                        if (bch2_csum_type_is_encryption(crc.csum_type)) {
@@ -1150,7 +1150,7 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
                                        nonce = crc.offset + crc.nonce;
                                else if (nonce != crc.offset + crc.nonce) {
                                        prt_printf(err, "incorrect nonce");
-                                       return -EINVAL;
+                                       return -BCH_ERR_invalid_bkey;
                                }
                        }
                        break;
@@ -1161,7 +1161,7 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
 
        if (nr_ptrs >= BCH_BKEY_PTRS_MAX) {
                prt_str(err, "too many ptrs");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index 90297cfc79348ace03d8a808bee728d1e9a75b1e..cc41472a335e00874d3a920a9f86d3096cf7ecaa 100644 (file)
@@ -1767,8 +1767,11 @@ got_sb:
        kfree(devs[0]);
        kfree(devs);
 
-       if (IS_ERR(sb))
-               return ERR_CAST(sb);
+       if (IS_ERR(sb)) {
+               ret = PTR_ERR(sb);
+               ret = bch2_err_class(ret);
+               return ERR_PTR(ret);
+       }
 
        c = sb->s_fs_info;
 
index 4ca70c6c3a4f70682fa7dcdbcdb8cff472de0a6f..cf453edcb5ab6602d673aab4811262aacca0c188 100644 (file)
@@ -306,40 +306,40 @@ static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
 
        if (k.k->p.inode) {
                prt_printf(err, "nonzero k.p.inode");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
                prt_printf(err, "fs inode in blockdev range");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bch2_inode_unpack(k, &unpacked)) {
                prt_printf(err, "invalid variable length fields");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
                prt_printf(err, "invalid data checksum type (%u >= %u",
                        unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
                prt_printf(err, "invalid data checksum type (%u >= %u)",
                       unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
            unpacked.bi_nlink != 0) {
                prt_printf(err, "flagged as unlinked but bi_nlink != 0");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
                prt_printf(err, "subvolume root but not a directory");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
@@ -353,13 +353,13 @@ int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*inode.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
                prt_printf(err, "invalid str hash type (%llu >= %u)",
                       INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return __bch2_inode_invalid(k, err);
@@ -373,13 +373,13 @@ int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*inode.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
                prt_printf(err, "invalid str hash type (%llu >= %u)",
                       INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return __bch2_inode_invalid(k, err);
@@ -421,13 +421,13 @@ int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
 {
        if (k.k->p.inode) {
                prt_printf(err, "nonzero k.p.inode");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index c19db0425dd7effc7982f52753827b74a6cdcc98..9b933330a4c374e0aa9b00fd08a0ee178c582a41 100644 (file)
@@ -22,7 +22,7 @@ static int bch2_sb_journal_validate(struct bch_sb *sb,
 {
        struct bch_sb_field_journal *journal = field_to_type(f, journal);
        struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
-       int ret = -EINVAL;
+       int ret = -BCH_ERR_invalid_sb_journal;
        unsigned nr;
        unsigned i;
        u64 *b;
@@ -105,7 +105,7 @@ static int bch2_sb_journal_v2_validate(struct bch_sb *sb,
 {
        struct bch_sb_field_journal_v2 *journal = field_to_type(f, journal_v2);
        struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
-       int ret = -EINVAL;
+       int ret = -BCH_ERR_invalid_sb_journal;
        unsigned nr;
        unsigned i;
        struct u64_range *b;
index 5c555b3703c0947006176f11ce9736717e9275d9..012c870acce043d2c71159c43c913ba556510a9a 100644 (file)
@@ -203,7 +203,7 @@ static int bch2_sb_journal_seq_blacklist_validate(struct bch_sb *sb,
                    le64_to_cpu(e->end)) {
                        prt_printf(err, "entry %u start >= end (%llu >= %llu)",
                               i, le64_to_cpu(e->start), le64_to_cpu(e->end));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_journal_seq_blacklist;
                }
 
                if (i + 1 < nr &&
@@ -211,7 +211,7 @@ static int bch2_sb_journal_seq_blacklist_validate(struct bch_sb *sb,
                    le64_to_cpu(e[1].start)) {
                        prt_printf(err, "entry %u out of order with next entry (%llu > %llu)",
                               i + 1, le64_to_cpu(e[0].end), le64_to_cpu(e[1].start));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_journal_seq_blacklist;
                }
        }
 
index 53e607d72274cd5458a95d7a846d2bc17a6ba20b..db1674ef1d22687a56cbdabccc02b89eb6d0ca8c 100644 (file)
@@ -16,7 +16,7 @@ int bch2_lru_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(*lru)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*lru));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index 7f74c026e9dae910d4751e480b8bf572632240bf..ededc826e9a05a5cd4676b4d480cfe19cc443fe2 100644 (file)
@@ -26,7 +26,7 @@ static int bch2_sb_quota_validate(struct bch_sb *sb, struct bch_sb_field *f,
        if (vstruct_bytes(&q->field) < sizeof(*q)) {
                prt_printf(err, "wrong size (got %zu should be %zu)",
                       vstruct_bytes(&q->field), sizeof(*q));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_quota;
        }
 
        return 0;
@@ -64,13 +64,13 @@ int bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (k.k->p.inode >= QTYP_NR) {
                prt_printf(err, "invalid quota type (%llu >= %u)",
                       k.k->p.inode, QTYP_NR);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_bytes(k.k) != sizeof(struct bch_quota)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_quota));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index aebed671c43a9b5ac5639e2ea69cdb53e6b8f3c7..8c426d6440c96989b97d5a47d1a88f3164e2d015 100644 (file)
@@ -85,7 +85,7 @@ int bch2_reflink_v_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(r.k) < sizeof(*r.v)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(r.k), sizeof(*r.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return bch2_bkey_ptrs_invalid(c, k, rw, err);
@@ -136,7 +136,7 @@ int bch2_indirect_inline_data_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(struct bch_indirect_inline_data)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_indirect_inline_data));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index e540c1aa91ba8a5f9804667a2401c70c4effe13f..482bedf4be8ba2cd696dd5afae09230281ae8aa0 100644 (file)
@@ -841,27 +841,27 @@ static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
                if (e->data_type >= BCH_DATA_NR) {
                        prt_printf(err, "invalid data type in entry ");
                        bch2_replicas_entry_to_text(err, e);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_replicas;
                }
 
                if (!e->nr_devs) {
                        prt_printf(err, "no devices in entry ");
                        bch2_replicas_entry_to_text(err, e);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_replicas;
                }
 
                if (e->nr_required > 1 &&
                    e->nr_required >= e->nr_devs) {
                        prt_printf(err, "bad nr_required in entry ");
                        bch2_replicas_entry_to_text(err, e);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_replicas;
                }
 
                for (j = 0; j < e->nr_devs; j++)
                        if (!bch2_dev_exists(sb, mi, e->devs[j])) {
                                prt_printf(err, "invalid device %u in entry ", e->devs[j]);
                                bch2_replicas_entry_to_text(err, e);
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_sb_replicas;
                        }
 
                if (i + 1 < cpu_r->nr) {
@@ -873,7 +873,7 @@ static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
                        if (!memcmp(e, n, cpu_r->entry_size)) {
                                prt_printf(err, "duplicate replicas entry ");
                                bch2_replicas_entry_to_text(err, e);
-                               return -EINVAL;
+                               return -BCH_ERR_invalid_sb_replicas;
                        }
                }
        }
index e37ffaad58837c3cfe8a6d8f90f9da4fd5b25f95..f19f6f8d32337eefc7214ada938bb090c2899316 100644 (file)
@@ -33,13 +33,13 @@ int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
            bkey_lt(k.k->p, POS(0, 1))) {
                prt_printf(err, "bad pos");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_bytes(k.k) != sizeof(struct bch_snapshot)) {
                prt_printf(err, "bad val size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_snapshot));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        s = bkey_s_c_to_snapshot(k);
@@ -48,18 +48,18 @@ int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (id && id <= k.k->p.offset) {
                prt_printf(err, "bad parent node (%u <= %llu)",
                       id, k.k->p.offset);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
                prt_printf(err, "children not normalized");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (s.v->children[0] &&
            s.v->children[0] == s.v->children[1]) {
                prt_printf(err, "duplicate child nodes");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        for (i = 0; i < 2; i++) {
@@ -68,7 +68,7 @@ int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
                if (id >= k.k->p.offset) {
                        prt_printf(err, "bad child node (%u >= %llu)",
                               id, k.k->p.offset);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_bkey;
                }
        }
 
@@ -773,13 +773,13 @@ int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
            bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
                prt_printf(err, "invalid pos");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_bytes(k.k) != sizeof(struct bch_subvolume)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
                       bkey_val_bytes(k.k), sizeof(struct bch_subvolume));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;
index e27b301432b1fdc6845d2c7f798aa7c0b50ac565..8dfe92d7eb771b1537a4073e10a25cd5d5f105c4 100644 (file)
@@ -216,23 +216,23 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
        if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
            !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
                prt_printf(out, "Not a bcachefs superblock layout");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_layout;
        }
 
        if (layout->layout_type != 0) {
                prt_printf(out, "Invalid superblock layout type %u",
                       layout->layout_type);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_layout_type;
        }
 
        if (!layout->nr_superblocks) {
                prt_printf(out, "Invalid superblock layout: no superblocks");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_layout_nr_superblocks;
        }
 
        if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
                prt_printf(out, "Invalid superblock layout: too many superblocks");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_layout_nr_superblocks;
        }
 
        max_sectors = 1 << layout->sb_max_size_bits;
@@ -246,7 +246,7 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
                        prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
                               "  (sb %u ends at %llu next starts at %llu",
                               i - 1, prev_offset + max_sectors, offset);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
                }
                prev_offset = offset;
        }
@@ -273,25 +273,25 @@ static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
        if (version    >= bcachefs_metadata_version_max) {
                prt_printf(out, "Unsupported superblock version %u (min %u, max %u)",
                       version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_version;
        }
 
        if (version_min < bcachefs_metadata_version_min) {
                prt_printf(out, "Unsupported superblock version %u (min %u, max %u)",
                       version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_version;
        }
 
        if (version_min > version) {
                prt_printf(out, "Bad minimum version %u, greater than version field %u",
                       version_min, version);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_version;
        }
 
        if (sb->features[1] ||
            (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
                prt_printf(out, "Filesystem has incompatible features");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_features;
        }
 
        block_size = le16_to_cpu(sb->block_size);
@@ -299,37 +299,37 @@ static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
        if (block_size > PAGE_SECTORS) {
                prt_printf(out, "Block size too big (got %u, max %u)",
                       block_size, PAGE_SECTORS);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_block_size;
        }
 
        if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
                prt_printf(out, "Bad user UUID (got zeroes)");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_uuid;
        }
 
        if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
                prt_printf(out, "Bad intenal UUID (got zeroes)");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_uuid;
        }
 
        if (!sb->nr_devices ||
            sb->nr_devices > BCH_SB_MEMBERS_MAX) {
                prt_printf(out, "Bad number of member devices %u (max %u)",
                       sb->nr_devices, BCH_SB_MEMBERS_MAX);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_too_many_members;
        }
 
        if (sb->dev_idx >= sb->nr_devices) {
                prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
                       sb->dev_idx, sb->nr_devices);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_dev_idx;
        }
 
        if (!sb->time_precision ||
            le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
                prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
                       le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_time_precision;
        }
 
        if (rw == READ) {
@@ -366,15 +366,15 @@ static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
 
        vstruct_for_each(sb, f) {
                if (!f->u64s) {
-                       prt_printf(out, "Invalid superblock: optional with size 0 (type %u)",
+                       prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
                               le32_to_cpu(f->type));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_field_size;
                }
 
                if (vstruct_next(f) > vstruct_last(sb)) {
                        prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
                               le32_to_cpu(f->type));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_field_size;
                }
        }
 
@@ -382,7 +382,7 @@ static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
        mi = bch2_sb_get_members(sb);
        if (!mi) {
                prt_printf(out, "Invalid superblock: member info area missing");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_members_missing;
        }
 
        ret = bch2_sb_field_validate(sb, &mi->field, out);
@@ -544,7 +544,7 @@ reread:
        if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
            !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
                prt_printf(err, "Not a bcachefs superblock");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_magic;
        }
 
        version         = le16_to_cpu(sb->sb->version);
@@ -555,13 +555,13 @@ reread:
        if (version    >= bcachefs_metadata_version_max) {
                prt_printf(err, "Unsupported superblock version %u (min %u, max %u)",
                       version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_version;
        }
 
        if (version_min < bcachefs_metadata_version_min) {
                prt_printf(err, "Unsupported superblock version %u (min %u, max %u)",
                       version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_version;
        }
 
        bytes = vstruct_bytes(sb->sb);
@@ -569,7 +569,7 @@ reread:
        if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
                prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
                       bytes, 512UL << sb->sb->layout.sb_max_size_bits);
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_too_big;
        }
 
        if (bytes > sb->buffer_size) {
@@ -580,7 +580,7 @@ reread:
 
        if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
                prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_csum_type;
        }
 
        /* XXX: verify MACs */
@@ -589,7 +589,7 @@ reread:
 
        if (bch2_crc_cmp(csum, sb->sb->csum)) {
                prt_printf(err, "bad checksum");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_csum;
        }
 
        sb->seq = le64_to_cpu(sb->sb->seq);
@@ -703,7 +703,7 @@ got_super:
                prt_printf(&err, "block size (%u) smaller than device block size (%u)",
                       le16_to_cpu(sb->sb->block_size) << 9,
                       bdev_logical_block_size(sb->bdev));
-               ret = -EINVAL;
+               ret = -BCH_ERR_block_size_too_small;
                goto err;
        }
 
@@ -958,7 +958,7 @@ static int bch2_sb_members_validate(struct bch_sb *sb,
        if ((void *) (mi->members + sb->nr_devices) >
            vstruct_end(&mi->field)) {
                prt_printf(err, "too many devices for section size");
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_members;
        }
 
        for (i = 0; i < sb->nr_devices; i++) {
@@ -970,28 +970,28 @@ static int bch2_sb_members_validate(struct bch_sb *sb,
                if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
                        prt_printf(err, "device %u: too many buckets (got %llu, max %lu)",
                               i, le64_to_cpu(m->nbuckets), LONG_MAX);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_members;
                }
 
                if (le64_to_cpu(m->nbuckets) -
                    le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
                        prt_printf(err, "device %u: not enough buckets (got %llu, max %u)",
                               i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_members;
                }
 
                if (le16_to_cpu(m->bucket_size) <
                    le16_to_cpu(sb->block_size)) {
                        prt_printf(err, "device %u: bucket size %u smaller than block size %u",
                               i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_members;
                }
 
                if (le16_to_cpu(m->bucket_size) <
                    BCH_SB_BTREE_NODE_SIZE(sb)) {
                        prt_printf(err, "device %u: bucket size %u smaller than btree node size %llu",
                               i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
-                       return -EINVAL;
+                       return -BCH_ERR_invalid_sb_members;
                }
        }
 
@@ -1123,12 +1123,12 @@ static int bch2_sb_crypt_validate(struct bch_sb *sb,
        if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
                prt_printf(err, "wrong size (got %zu should be %zu)",
                       vstruct_bytes(&crypt->field), sizeof(*crypt));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_crypt;
        }
 
        if (BCH_CRYPT_KDF_TYPE(crypt)) {
                prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_crypt;
        }
 
        return 0;
@@ -1365,7 +1365,7 @@ static int bch2_sb_clean_validate(struct bch_sb *sb,
        if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
                prt_printf(err, "wrong size (got %zu should be %zu)",
                       vstruct_bytes(&clean->field), sizeof(*clean));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_clean;
        }
 
        return 0;
index 234dab15fa6358d259b9cc74de5bc602a5d4a476..37dce3e3cccbfcfe2874f7d011a957697d806334 100644 (file)
@@ -880,7 +880,7 @@ int bch2_fs_start(struct bch_fs *c)
        struct bch_dev *ca;
        time64_t now = ktime_get_real_seconds();
        unsigned i;
-       int ret = -EINVAL;
+       int ret;
 
        down_write(&c->state_lock);
 
@@ -917,9 +917,9 @@ int bch2_fs_start(struct bch_fs *c)
        if (ret)
                goto err;
 
-       ret = -EINVAL;
        if (bch2_fs_init_fault("fs_start")) {
                bch_err(c, "fs_start fault injected");
+               ret = -EINVAL;
                goto err;
        }
 
@@ -942,46 +942,43 @@ out:
        return ret;
 err:
        bch_err(c, "error starting filesystem: %s", bch2_err_str(ret));
-
-       if (ret < -BCH_ERR_START)
-               ret = -EINVAL;
        goto out;
 }
 
-static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
+static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
 {
        struct bch_sb_field_members *sb_mi;
 
        sb_mi = bch2_sb_get_members(sb);
        if (!sb_mi)
-               return "Invalid superblock: member info area missing";
+               return -BCH_ERR_member_info_missing;
 
        if (le16_to_cpu(sb->block_size) != block_sectors(c))
-               return "mismatched block size";
+               return -BCH_ERR_mismatched_block_size;
 
        if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
            BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
-               return "new cache bucket size is too small";
+               return -BCH_ERR_bucket_size_too_small;
 
-       return NULL;
+       return 0;
 }
 
-static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
+static int bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
 {
        struct bch_sb *newest =
                le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
        struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
 
        if (!uuid_equal(&fs->uuid, &sb->uuid))
-               return "device not a member of filesystem";
+               return -BCH_ERR_device_not_a_member_of_filesystem;
 
        if (!bch2_dev_exists(newest, mi, sb->dev_idx))
-               return "device has been removed";
+               return -BCH_ERR_device_has_been_removed;
 
        if (fs->block_size != sb->block_size)
-               return "mismatched block size";
+               return -BCH_ERR_mismatched_block_size;
 
-       return NULL;
+       return 0;
 }
 
 /* Device startup/shutdown: */
@@ -1179,23 +1176,17 @@ static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
        if (bch2_dev_is_online(ca)) {
                bch_err(ca, "already have device online in slot %u",
                        sb->sb->dev_idx);
-               return -EINVAL;
+               return -BCH_ERR_device_already_online;
        }
 
        if (get_capacity(sb->bdev->bd_disk) <
            ca->mi.bucket_size * ca->mi.nbuckets) {
                bch_err(ca, "cannot online: device too small");
-               return -EINVAL;
+               return -BCH_ERR_device_size_too_small;
        }
 
        BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
 
-       if (get_capacity(sb->bdev->bd_disk) <
-           ca->mi.bucket_size * ca->mi.nbuckets) {
-               bch_err(ca, "device too small");
-               return -EINVAL;
-       }
-
        ret = bch2_dev_journal_init(ca, sb->sb);
        if (ret)
                return ret;
@@ -1370,7 +1361,7 @@ int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
                return 0;
 
        if (!bch2_dev_state_allowed(c, ca, new_state, flags))
-               return -EINVAL;
+               return -BCH_ERR_device_state_not_allowed;
 
        if (new_state != BCH_MEMBER_STATE_rw)
                __bch2_dev_read_only(c, ca);
@@ -1433,7 +1424,7 @@ int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
 {
        struct bch_sb_field_members *mi;
        unsigned dev_idx = ca->dev_idx, data;
-       int ret = -EINVAL;
+       int ret;
 
        down_write(&c->state_lock);
 
@@ -1445,6 +1436,7 @@ int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
 
        if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
                bch_err(ca, "Cannot remove without losing data");
+               ret = -BCH_ERR_device_state_not_allowed;
                goto err;
        }
 
@@ -1530,7 +1522,6 @@ int bch2_dev_add(struct bch_fs *c, const char *path)
 {
        struct bch_opts opts = bch2_opts_empty();
        struct bch_sb_handle sb;
-       const char *err;
        struct bch_dev *ca = NULL;
        struct bch_sb_field_members *mi;
        struct bch_member dev_mi;
@@ -1555,10 +1546,9 @@ int bch2_dev_add(struct bch_fs *c, const char *path)
                }
        }
 
-       err = bch2_dev_may_add(sb.sb, c);
-       if (err) {
-               bch_err(c, "device add error: %s", err);
-               ret = -EINVAL;
+       ret = bch2_dev_may_add(sb.sb, c);
+       if (ret) {
+               bch_err(c, "device add error: %s", bch2_err_str(ret));
                goto err;
        }
 
@@ -1692,7 +1682,6 @@ int bch2_dev_online(struct bch_fs *c, const char *path)
        struct bch_sb_field_members *mi;
        struct bch_dev *ca;
        unsigned dev_idx;
-       const char *err;
        int ret;
 
        down_write(&c->state_lock);
@@ -1705,9 +1694,9 @@ int bch2_dev_online(struct bch_fs *c, const char *path)
 
        dev_idx = sb.sb->dev_idx;
 
-       err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
-       if (err) {
-               bch_err(c, "error bringing %s online: %s", path, err);
+       ret = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
+       if (ret) {
+               bch_err(c, "error bringing %s online: %s", path, bch2_err_str(ret));
                goto err;
        }
 
@@ -1741,7 +1730,7 @@ int bch2_dev_online(struct bch_fs *c, const char *path)
 err:
        up_write(&c->state_lock);
        bch2_free_super(&sb);
-       return -EINVAL;
+       return ret;
 }
 
 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
@@ -1757,7 +1746,7 @@ int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
        if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
                bch_err(ca, "Cannot offline required disk");
                up_write(&c->state_lock);
-               return -EINVAL;
+               return -BCH_ERR_device_state_not_allowed;
        }
 
        __bch2_dev_offline(c, ca);
@@ -1783,7 +1772,7 @@ int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
            get_capacity(ca->disk_sb.bdev->bd_disk) <
            ca->mi.bucket_size * nbuckets) {
                bch_err(ca, "New size larger than device");
-               ret = -EINVAL;
+               ret = -BCH_ERR_device_size_too_small;
                goto err;
        }
 
@@ -1836,7 +1825,6 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
        struct bch_fs *c = NULL;
        struct bch_sb_field_members *mi;
        unsigned i, best_sb = 0;
-       const char *err;
        struct printbuf errbuf = PRINTBUF;
        int ret = 0;
 
@@ -1880,8 +1868,8 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
                        continue;
                }
 
-               err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
-               if (err)
+               ret = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
+               if (ret)
                        goto err_print;
                i++;
        }
@@ -1902,9 +1890,10 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
        }
        up_write(&c->state_lock);
 
-       err = "insufficient devices";
-       if (!bch2_fs_may_start(c))
+       if (!bch2_fs_may_start(c)) {
+               ret = -BCH_ERR_insufficient_devices_to_start;
                goto err_print;
+       }
 
        if (!c->opts.nostart) {
                ret = bch2_fs_start(c);
@@ -1919,8 +1908,7 @@ out:
        return c;
 err_print:
        pr_err("bch_fs_open err opening %s: %s",
-              devices[0], err);
-       ret = -EINVAL;
+              devices[0], bch2_err_str(ret));
 err:
        if (!IS_ERR_OR_NULL(c))
                bch2_fs_stop(c);
index bd118f6ea08b98f33893e5a1a57de3ad60b1c139..448737be045c04337df44e829e5cacc23a6e40ce 100644 (file)
@@ -78,7 +78,7 @@ int bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k,
        if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr)) {
                prt_printf(err, "incorrect value size (%zu < %zu)",
                       bkey_val_bytes(k.k), sizeof(*xattr.v));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (bkey_val_u64s(k.k) <
@@ -88,7 +88,7 @@ int bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k,
                       bkey_val_u64s(k.k),
                       xattr_val_u64s(xattr.v->x_name_len,
                                      le16_to_cpu(xattr.v->x_val_len)));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        /* XXX why +4 ? */
@@ -99,18 +99,18 @@ int bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k,
                       bkey_val_u64s(k.k),
                       xattr_val_u64s(xattr.v->x_name_len,
                                      le16_to_cpu(xattr.v->x_val_len) + 4));
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        handler = bch2_xattr_type_to_handler(xattr.v->x_type);
        if (!handler) {
                prt_printf(err, "invalid type (%u)", xattr.v->x_type);
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) {
                prt_printf(err, "xattr name has invalid characters");
-               return -EINVAL;
+               return -BCH_ERR_invalid_bkey;
        }
 
        return 0;