bcache: set bcache device into read-only mode for BCH_FEATURE_INCOMPAT_OBSO_LARGE_BUCKET
[platform/kernel/linux-starfive.git] / drivers / md / bcache / super.c
index a4752ac..2047a9c 100644 (file)
@@ -64,9 +64,25 @@ static unsigned int get_bucket_size(struct cache_sb *sb, struct cache_sb_disk *s
 {
        unsigned int bucket_size = le16_to_cpu(s->bucket_size);
 
-       if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES &&
-            bch_has_feature_large_bucket(sb))
-               bucket_size |= le16_to_cpu(s->bucket_size_hi) << 16;
+       if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
+               if (bch_has_feature_large_bucket(sb)) {
+                       unsigned int max, order;
+
+                       max = sizeof(unsigned int) * BITS_PER_BYTE - 1;
+                       order = le16_to_cpu(s->bucket_size);
+                       /*
+                        * bcache tool will make sure the overflow won't
+                        * happen, an error message here is enough.
+                        */
+                       if (order > max)
+                               pr_err("Bucket size (1 << %u) overflows\n",
+                                       order);
+                       bucket_size = 1 << order;
+               } else if (bch_has_feature_obso_large_bucket(sb)) {
+                       bucket_size +=
+                               le16_to_cpu(s->obso_bucket_size_hi) << 16;
+               }
+       }
 
        return bucket_size;
 }
@@ -228,6 +244,20 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
                sb->feature_compat = le64_to_cpu(s->feature_compat);
                sb->feature_incompat = le64_to_cpu(s->feature_incompat);
                sb->feature_ro_compat = le64_to_cpu(s->feature_ro_compat);
+
+               /* Check incompatible features */
+               err = "Unsupported compatible feature found";
+               if (bch_has_unknown_compat_features(sb))
+                       goto err;
+
+               err = "Unsupported read-only compatible feature found";
+               if (bch_has_unknown_ro_compat_features(sb))
+                       goto err;
+
+               err = "Unsupported incompatible feature found";
+               if (bch_has_unknown_incompat_features(sb))
+                       goto err;
+
                err = read_super_common(sb, bdev, s);
                if (err)
                        goto err;
@@ -1302,6 +1332,12 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
        bcache_device_link(&dc->disk, c, "bdev");
        atomic_inc(&c->attached_dev_nr);
 
+       if (bch_has_feature_obso_large_bucket(&(c->cache->sb))) {
+               pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n");
+               pr_err("Please update to the latest bcache-tools to create the cache device\n");
+               set_disk_ro(dc->disk.disk, 1);
+       }
+
        /* Allow the writeback thread to proceed */
        up_write(&dc->writeback_lock);
 
@@ -1524,6 +1560,12 @@ static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
 
        bcache_device_link(d, c, "volume");
 
+       if (bch_has_feature_obso_large_bucket(&c->cache->sb)) {
+               pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n");
+               pr_err("Please update to the latest bcache-tools to create the cache device\n");
+               set_disk_ro(d->disk, 1);
+       }
+
        return 0;
 err:
        kobject_put(&d->kobj);
@@ -2083,6 +2125,9 @@ static int run_cache_set(struct cache_set *c)
        c->cache->sb.last_mount = (u32)ktime_get_real_seconds();
        bcache_write_super(c);
 
+       if (bch_has_feature_obso_large_bucket(&c->cache->sb))
+               pr_err("Detect obsoleted large bucket layout, all attached bcache device will be read-only\n");
+
        list_for_each_entry_safe(dc, t, &uncached_devices, list)
                bch_cached_dev_attach(dc, c, NULL);
 
@@ -2644,8 +2689,8 @@ static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
        }
 
        list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
+               char *pdev_set_uuid = pdev->dc->sb.set_uuid;
                list_for_each_entry_safe(c, tc, &bch_cache_sets, list) {
-                       char *pdev_set_uuid = pdev->dc->sb.set_uuid;
                        char *set_uuid = c->set_uuid;
 
                        if (!memcmp(pdev_set_uuid, set_uuid, 16)) {