bcachefs: bch2_dev_tryget()
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 30 Apr 2024 19:30:35 +0000 (15:30 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Wed, 8 May 2024 21:29:22 +0000 (17:29 -0400)
Most uses of bch2_dev_bkey_exists() are going away, where we assume that
because a key references a device the device most exist - instead, we'll
be explicitly checking if the device exists and getting a reference to
it.

This adds the new helpers.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/sb-members.c
fs/bcachefs/sb-members.h

index e4a9c858ca8e2a47841a19914d35ca7210280b60..2ca557bffc0e7f5f69372a3b4313f0dc3c7535f5 100644 (file)
@@ -9,6 +9,11 @@
 #include "sb-members.h"
 #include "super-io.h"
 
+void bch2_dev_missing(struct bch_fs *c, unsigned dev)
+{
+       bch2_fs_inconsistent(c, "pointer to nonexistent device %u", dev);
+}
+
 #define x(t, n, ...) [n] = #t,
 static const char * const bch2_iops_measurements[] = {
        BCH_IOPS_MEASUREMENTS()
index bea4c2efbb6e82bdc680713973d4dd67fc90f11f..714559e2ef5938ef0ca715fe40397c5075d37d1a 100644 (file)
@@ -217,6 +217,26 @@ static inline struct bch_dev *bch2_dev_rcu(struct bch_fs *c, unsigned dev)
                : NULL;
 }
 
+static inline struct bch_dev *bch2_dev_tryget_noerror(struct bch_fs *c, unsigned dev)
+{
+       rcu_read_lock();
+       struct bch_dev *ca = bch2_dev_rcu(c, dev);
+       if (ca)
+               bch2_dev_get(ca);
+       rcu_read_unlock();
+       return ca;
+}
+
+void bch2_dev_missing(struct bch_fs *, unsigned);
+
+static inline struct bch_dev *bch2_dev_tryget(struct bch_fs *c, unsigned dev)
+{
+       struct bch_dev *ca = bch2_dev_tryget_noerror(c, dev);
+       if (!ca)
+               bch2_dev_missing(c, dev);
+       return ca;
+}
+
 /* XXX kill, move to struct bch_fs */
 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
 {