UBI: do not panic if volume check fails
[pandora-kernel.git] / drivers / mtd / ubi / vmt.c
index 6609c31..1628e1f 100644 (file)
  */
 
 #include <linux/err.h>
-#include <asm/div64.h>
+#include <linux/math64.h>
 #include "ubi.h"
 
 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
-static void paranoid_check_volumes(struct ubi_device *ubi);
+static int paranoid_check_volumes(struct ubi_device *ubi);
 #else
-#define paranoid_check_volumes(ubi)
+#define paranoid_check_volumes(ubi) 0
 #endif
 
 static ssize_t vol_attribute_show(struct device *dev,
@@ -63,21 +63,29 @@ static struct device_attribute attr_vol_upd_marker =
  * B. process 2 removes volume Y;
  * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
  *
- * What we want to do in a situation like that is to return error when the file
- * is read. This is done by means of the 'removed' flag and the 'vol_lock' of
- * the UBI volume description object.
+ * In this situation, this function will return %-ENODEV because it will find
+ * out that the volume was removed from the @ubi->volumes array.
  */
 static ssize_t vol_attribute_show(struct device *dev,
                                  struct device_attribute *attr, char *buf)
 {
-       int ret = -ENODEV;
+       int ret;
        struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
+       struct ubi_device *ubi;
 
-       spin_lock(&vol->ubi->volumes_lock);
-       if (vol->removed) {
-               spin_unlock(&vol->ubi->volumes_lock);
-               return ret;
+       ubi = ubi_get_device(vol->ubi->ubi_num);
+       if (!ubi)
+               return -ENODEV;
+
+       spin_lock(&ubi->volumes_lock);
+       if (!ubi->volumes[vol->vol_id]) {
+               spin_unlock(&ubi->volumes_lock);
+               ubi_put_device(ubi);
+               return -ENODEV;
        }
+       /* Take a reference to prevent volume removal */
+       vol->ref_count += 1;
+       spin_unlock(&ubi->volumes_lock);
 
        if (attr == &attr_vol_reserved_ebs)
                ret = sprintf(buf, "%d\n", vol->reserved_pebs);
@@ -102,8 +110,15 @@ static ssize_t vol_attribute_show(struct device *dev,
        else if (attr == &attr_vol_upd_marker)
                ret = sprintf(buf, "%d\n", vol->upd_marker);
        else
-               BUG();
-       spin_unlock(&vol->ubi->volumes_lock);
+               /* This must be a bug */
+               ret = -EINVAL;
+
+       /* We've done the operation, drop volume and UBI device references */
+       spin_lock(&ubi->volumes_lock);
+       vol->ref_count -= 1;
+       ubi_assert(vol->ref_count >= 0);
+       spin_unlock(&ubi->volumes_lock);
+       ubi_put_device(ubi);
        return ret;
 }
 
@@ -112,7 +127,7 @@ static void vol_release(struct device *dev)
 {
        struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
 
-       ubi_assert(vol->removed);
+       kfree(vol->eba_tbl);
        kfree(vol);
 }
 
@@ -154,9 +169,7 @@ static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
        if (err)
                return err;
        err = device_create_file(&vol->dev, &attr_vol_upd_marker);
-       if (err)
-               return err;
-       return 0;
+       return err;
 }
 
 /**
@@ -182,16 +195,16 @@ static void volume_sysfs_close(struct ubi_volume *vol)
  * @req: volume creation request
  *
  * This function creates volume described by @req. If @req->vol_id id
- * %UBI_VOL_NUM_AUTO, this function automatically assigne ID to the new volume
+ * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
  * and saves it in @req->vol_id. Returns zero in case of success and a negative
- * error code in case of failure.
+ * error code in case of failure. Note, the caller has to have the
+ * @ubi->device_mutex locked.
  */
 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
 {
-       int i, err, vol_id = req->vol_id;
+       int i, err, vol_id = req->vol_id, do_free = 1;
        struct ubi_volume *vol;
        struct ubi_vtbl_record vtbl_rec;
-       uint64_t bytes;
        dev_t dev;
 
        if (ubi->ro_mode)
@@ -202,10 +215,9 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
                return -ENOMEM;
 
        spin_lock(&ubi->volumes_lock);
-
        if (vol_id == UBI_VOL_NUM_AUTO) {
                /* Find unused volume ID */
-               dbg_msg("search for vacant volume ID");
+               dbg_gen("search for vacant volume ID");
                for (i = 0; i < ubi->vtbl_slots; i++)
                        if (!ubi->volumes[i]) {
                                vol_id = i;
@@ -220,8 +232,8 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
                req->vol_id = vol_id;
        }
 
-       dbg_msg("volume ID %d, %llu bytes, type %d, name %s",
-               vol_id, (unsigned long long)req->bytes,
+       dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
+               ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
                (int)req->vol_type, req->name);
 
        /* Ensure that this volume does not exist */
@@ -240,12 +252,10 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
                        goto out_unlock;
                }
 
-        /* Calculate how many eraseblocks are requested */
+       /* Calculate how many eraseblocks are requested */
        vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
-       bytes = req->bytes;
-       if (do_div(bytes, vol->usable_leb_size))
-               vol->reserved_pebs = 1;
-       vol->reserved_pebs += bytes;
+       vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
+                                     vol->usable_leb_size);
 
        /* Reserve physical eraseblocks */
        if (vol->reserved_pebs > ubi->avail_pebs) {
@@ -255,17 +265,15 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
        }
        ubi->avail_pebs -= vol->reserved_pebs;
        ubi->rsvd_pebs += vol->reserved_pebs;
+       spin_unlock(&ubi->volumes_lock);
 
        vol->vol_id    = vol_id;
        vol->alignment = req->alignment;
        vol->data_pad  = ubi->leb_size % vol->alignment;
        vol->vol_type  = req->vol_type;
        vol->name_len  = req->name_len;
-       memcpy(vol->name, req->name, vol->name_len + 1);
-       vol->exclusive = 1;
+       memcpy(vol->name, req->name, vol->name_len);
        vol->ubi = ubi;
-       ubi->volumes[vol_id] = vol;
-       spin_unlock(&ubi->volumes_lock);
 
        /*
         * Finish all pending erases because there may be some LEBs belonging
@@ -290,10 +298,10 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
                vol->used_bytes =
                        (long long)vol->used_ebs * vol->usable_leb_size;
        } else {
-               bytes = vol->used_bytes;
-               vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
-               vol->used_ebs = bytes;
-               if (vol->last_eb_bytes)
+               vol->used_ebs = div_u64_rem(vol->used_bytes,
+                                           vol->usable_leb_size,
+                                           &vol->last_eb_bytes);
+               if (vol->last_eb_bytes != 0)
                        vol->used_ebs += 1;
                else
                        vol->last_eb_bytes = vol->usable_leb_size;
@@ -305,7 +313,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
        dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
        err = cdev_add(&vol->cdev, dev, 1);
        if (err) {
-               ubi_err("cannot add character device for volume %d", vol_id);
+               ubi_err("cannot add character device");
                goto out_mapping;
        }
 
@@ -317,10 +325,13 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
        vol->dev.parent = &ubi->dev;
        vol->dev.devt = dev;
        vol->dev.class = ubi_class;
+
        sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
        err = device_register(&vol->dev);
-       if (err)
+       if (err) {
+               ubi_err("cannot register device");
                goto out_gluebi;
+       }
 
        err = volume_sysfs_init(ubi, vol);
        if (err)
@@ -336,98 +347,109 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
                vtbl_rec.vol_type = UBI_VID_DYNAMIC;
        else
                vtbl_rec.vol_type = UBI_VID_STATIC;
-       memcpy(vtbl_rec.name, vol->name, vol->name_len + 1);
+       memcpy(vtbl_rec.name, vol->name, vol->name_len);
 
        err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
        if (err)
                goto out_sysfs;
 
        spin_lock(&ubi->volumes_lock);
+       ubi->volumes[vol_id] = vol;
        ubi->vol_count += 1;
-       vol->exclusive = 0;
        spin_unlock(&ubi->volumes_lock);
 
-       paranoid_check_volumes(ubi);
-       return 0;
+       if (paranoid_check_volumes(ubi))
+               dbg_err("check failed while creating volume %d", vol_id);
+       return err;
 
+out_sysfs:
+       /*
+        * We have registered our device, we should not free the volume
+        * description object in this function in case of an error - it is
+        * freed by the release function.
+        *
+        * Get device reference to prevent the release function from being
+        * called just after sysfs has been closed.
+        */
+       do_free = 0;
+       get_device(&vol->dev);
+       volume_sysfs_close(vol);
 out_gluebi:
-       err = ubi_destroy_gluebi(vol);
+       if (ubi_destroy_gluebi(vol))
+               dbg_err("cannot destroy gluebi for volume %d:%d",
+                       ubi->ubi_num, vol_id);
 out_cdev:
        cdev_del(&vol->cdev);
 out_mapping:
-       kfree(vol->eba_tbl);
+       if (do_free)
+               kfree(vol->eba_tbl);
 out_acc:
        spin_lock(&ubi->volumes_lock);
        ubi->rsvd_pebs -= vol->reserved_pebs;
        ubi->avail_pebs += vol->reserved_pebs;
-       ubi->volumes[vol_id] = NULL;
 out_unlock:
        spin_unlock(&ubi->volumes_lock);
-       kfree(vol);
-       return err;
-
-       /*
-        * We are registered, so @vol is destroyed in the release function and
-        * we have to de-initialize differently.
-        */
-out_sysfs:
-       err = ubi_destroy_gluebi(vol);
-       cdev_del(&vol->cdev);
-       kfree(vol->eba_tbl);
-       spin_lock(&ubi->volumes_lock);
-       ubi->rsvd_pebs -= vol->reserved_pebs;
-       ubi->avail_pebs += vol->reserved_pebs;
-       ubi->volumes[vol_id] = NULL;
-       spin_unlock(&ubi->volumes_lock);
-       volume_sysfs_close(vol);
+       if (do_free)
+               kfree(vol);
+       else
+               put_device(&vol->dev);
+       ubi_err("cannot create volume %d, error %d", vol_id, err);
        return err;
 }
 
 /**
  * ubi_remove_volume - remove volume.
  * @desc: volume descriptor
+ * @no_vtbl: do not change volume table if not zero
  *
  * This function removes volume described by @desc. The volume has to be opened
  * in "exclusive" mode. Returns zero in case of success and a negative error
- * code in case of failure.
+ * code in case of failure. The caller has to have the @ubi->device_mutex
+ * locked.
  */
-int ubi_remove_volume(struct ubi_volume_desc *desc)
+int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 {
        struct ubi_volume *vol = desc->vol;
        struct ubi_device *ubi = vol->ubi;
        int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
 
-       dbg_msg("remove UBI volume %d", vol_id);
+       dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
        ubi_assert(desc->mode == UBI_EXCLUSIVE);
        ubi_assert(vol == ubi->volumes[vol_id]);
 
        if (ubi->ro_mode)
                return -EROFS;
 
+       spin_lock(&ubi->volumes_lock);
+       if (vol->ref_count > 1) {
+               /*
+                * The volume is busy, probably someone is reading one of its
+                * sysfs files.
+                */
+               err = -EBUSY;
+               goto out_unlock;
+       }
+       ubi->volumes[vol_id] = NULL;
+       spin_unlock(&ubi->volumes_lock);
+
        err = ubi_destroy_gluebi(vol);
        if (err)
-               return err;
+               goto out_err;
 
-       err = ubi_change_vtbl_record(ubi, vol_id, NULL);
-       if (err)
-               return err;
+       if (!no_vtbl) {
+               err = ubi_change_vtbl_record(ubi, vol_id, NULL);
+               if (err)
+                       goto out_err;
+       }
 
        for (i = 0; i < vol->reserved_pebs; i++) {
-               err = ubi_eba_unmap_leb(ubi, vol_id, i);
+               err = ubi_eba_unmap_leb(ubi, vol, i);
                if (err)
-                       return err;
+                       goto out_err;
        }
 
-       spin_lock(&ubi->volumes_lock);
-       vol->removed = 1;
-       ubi->volumes[vol_id] = NULL;
-       spin_unlock(&ubi->volumes_lock);
-
-       kfree(vol->eba_tbl);
-       vol->eba_tbl = NULL;
        cdev_del(&vol->cdev);
        volume_sysfs_close(vol);
-       kfree(desc);
 
        spin_lock(&ubi->volumes_lock);
        ubi->rsvd_pebs -= reserved_pebs;
@@ -444,9 +466,18 @@ int ubi_remove_volume(struct ubi_volume_desc *desc)
        ubi->vol_count -= 1;
        spin_unlock(&ubi->volumes_lock);
 
-       paranoid_check_volumes(ubi);
-       module_put(THIS_MODULE);
-       return 0;
+       if (!no_vtbl && paranoid_check_volumes(ubi))
+               dbg_err("check failed while removing volume %d", vol_id);
+
+       return err;
+
+out_err:
+       ubi_err("cannot remove volume %d, error %d", vol_id, err);
+       spin_lock(&ubi->volumes_lock);
+       ubi->volumes[vol_id] = vol;
+out_unlock:
+       spin_unlock(&ubi->volumes_lock);
+       return err;
 }
 
 /**
@@ -454,8 +485,9 @@ int ubi_remove_volume(struct ubi_volume_desc *desc)
  * @desc: volume descriptor
  * @reserved_pebs: new size in physical eraseblocks
  *
- * This function returns zero in case of success, and a negative error code in
- * case of failure.
+ * This function re-sizes the volume and returns zero in case of success, and a
+ * negative error code in case of failure. The caller has to have the
+ * @ubi->device_mutex locked.
  */
 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
 {
@@ -468,10 +500,8 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
        if (ubi->ro_mode)
                return -EROFS;
 
-       dbg_msg("re-size volume %d to from %d to %d PEBs",
-               vol_id, vol->reserved_pebs, reserved_pebs);
-       ubi_assert(desc->mode == UBI_EXCLUSIVE);
-       ubi_assert(vol == ubi->volumes[vol_id]);
+       dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
+               ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
 
        if (vol->vol_type == UBI_STATIC_VOLUME &&
            reserved_pebs < vol->used_ebs) {
@@ -491,6 +521,14 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
        for (i = 0; i < reserved_pebs; i++)
                new_mapping[i] = UBI_LEB_UNMAPPED;
 
+       spin_lock(&ubi->volumes_lock);
+       if (vol->ref_count > 1) {
+               spin_unlock(&ubi->volumes_lock);
+               err = -EBUSY;
+               goto out_free;
+       }
+       spin_unlock(&ubi->volumes_lock);
+
        /* Reserve physical eraseblocks */
        pebs = reserved_pebs - vol->reserved_pebs;
        if (pebs > 0) {
@@ -520,7 +558,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
 
        if (pebs < 0) {
                for (i = 0; i < -pebs; i++) {
-                       err = ubi_eba_unmap_leb(ubi, vol_id, reserved_pebs + i);
+                       err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
                        if (err)
                                goto out_acc;
                }
@@ -551,8 +589,9 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
                        (long long)vol->used_ebs * vol->usable_leb_size;
        }
 
-       paranoid_check_volumes(ubi);
-       return 0;
+       if (paranoid_check_volumes(ubi))
+               dbg_err("check failed while re-sizing volume %d", vol_id);
+       return err;
 
 out_acc:
        if (pebs > 0) {
@@ -566,24 +605,59 @@ out_free:
        return err;
 }
 
+/**
+ * ubi_rename_volumes - re-name UBI volumes.
+ * @ubi: UBI device description object
+ * @rename_list: list of &struct ubi_rename_entry objects
+ *
+ * This function re-names or removes volumes specified in the re-name list.
+ * Returns zero in case of success and a negative error code in case of
+ * failure.
+ */
+int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
+{
+       int err;
+       struct ubi_rename_entry *re;
+
+       err = ubi_vtbl_rename_volumes(ubi, rename_list);
+       if (err)
+               return err;
+
+       list_for_each_entry(re, rename_list, list) {
+               if (re->remove) {
+                       err = ubi_remove_volume(re->desc, 1);
+                       if (err)
+                               break;
+               } else {
+                       struct ubi_volume *vol = re->desc->vol;
+
+                       spin_lock(&ubi->volumes_lock);
+                       vol->name_len = re->new_name_len;
+                       memcpy(vol->name, re->new_name, re->new_name_len + 1);
+                       spin_unlock(&ubi->volumes_lock);
+               }
+       }
+
+       if (!err && paranoid_check_volumes(ubi))
+               ;
+       return err;
+}
+
 /**
  * ubi_add_volume - add volume.
  * @ubi: UBI device description object
- * @vol_id: volume ID
+ * @vol: volume description object
  *
- * This function adds an existin volume and initializes all its data
- * structures. Returnes zero in case of success and a negative error code in
+ * This function adds an existing volume and initializes all its data
+ * structures. Returns zero in case of success and a negative error code in
  * case of failure.
  */
-int ubi_add_volume(struct ubi_device *ubi, int vol_id)
+int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
 {
-       int err;
+       int err, vol_id = vol->vol_id;
        dev_t dev;
-       struct ubi_volume *vol = ubi->volumes[vol_id];
 
-       dbg_msg("add volume %d", vol_id);
-       ubi_dbg_dump_vol_info(vol);
-       ubi_assert(vol);
+       dbg_gen("add volume %d", vol_id);
 
        /* Register character device for the volume */
        cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
@@ -591,7 +665,8 @@ int ubi_add_volume(struct ubi_device *ubi, int vol_id)
        dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
        err = cdev_add(&vol->cdev, dev, 1);
        if (err) {
-               ubi_err("cannot add character device for volume %d", vol_id);
+               ubi_err("cannot add character device for volume %d, error %d",
+                       vol_id, err);
                return err;
        }
 
@@ -616,8 +691,9 @@ int ubi_add_volume(struct ubi_device *ubi, int vol_id)
                return err;
        }
 
-       paranoid_check_volumes(ubi);
-       return 0;
+       if (paranoid_check_volumes(ubi))
+               dbg_err("check failed while adding volume %d", vol_id);
+       return err;
 
 out_gluebi:
        err = ubi_destroy_gluebi(vol);
@@ -629,22 +705,19 @@ out_cdev:
 /**
  * ubi_free_volume - free volume.
  * @ubi: UBI device description object
- * @vol_id: volume ID
+ * @vol: volume description object
  *
- * This function frees all resources for volume @vol_id but does not remove it.
+ * This function frees all resources for volume @vol but does not remove it.
  * Used only when the UBI device is detached.
  */
-void ubi_free_volume(struct ubi_device *ubi, int vol_id)
+void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
 {
        int err;
-       struct ubi_volume *vol = ubi->volumes[vol_id];
 
-       dbg_msg("free volume %d", vol_id);
-       ubi_assert(vol);
+       dbg_gen("free volume %d", vol->vol_id);
 
-       vol->removed = 1;
+       ubi->volumes[vol->vol_id] = NULL;
        err = ubi_destroy_gluebi(vol);
-       ubi->volumes[vol_id] = NULL;
        cdev_del(&vol->cdev);
        volume_sysfs_close(vol);
 }
@@ -655,8 +728,10 @@ void ubi_free_volume(struct ubi_device *ubi, int vol_id)
  * paranoid_check_volume - check volume information.
  * @ubi: UBI device description object
  * @vol_id: volume ID
+ *
+ * Returns zero if volume is all right and a a negative error code if not.
  */
-static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
+static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
 {
        int idx = vol_id2idx(ubi, vol_id);
        int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
@@ -674,16 +749,7 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
                        goto fail;
                }
                spin_unlock(&ubi->volumes_lock);
-               return;
-       }
-
-       if (vol->exclusive) {
-               /*
-                * The volume may be being created at the moment, do not check
-                * it (e.g., it may be in the middle of ubi_create_volume().
-                */
-               spin_unlock(&ubi->volumes_lock);
-               return;
+               return 0;
        }
 
        if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
@@ -696,7 +762,7 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
                goto fail;
        }
 
-       n = vol->alignment % ubi->min_io_size;
+       n = vol->alignment & (ubi->min_io_size - 1);
        if (vol->alignment != 1 && n) {
                ubi_err("alignment is not multiple of min I/O unit");
                goto fail;
@@ -714,11 +780,6 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
                goto fail;
        }
 
-       if (vol->upd_marker != 0 && vol->upd_marker != 1) {
-               ubi_err("bad upd_marker");
-               goto fail;
-       }
-
        if (vol->upd_marker && vol->corrupted) {
                dbg_err("update marker and corrupted simultaneously");
                goto fail;
@@ -753,7 +814,7 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
 
        n = (long long)vol->used_ebs * vol->usable_leb_size;
        if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
-               if (vol->corrupted != 0) {
+               if (vol->corrupted) {
                        ubi_err("corrupted dynamic volume");
                        goto fail;
                }
@@ -770,10 +831,6 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
                        goto fail;
                }
        } else {
-               if (vol->corrupted != 0 && vol->corrupted != 1) {
-                       ubi_err("bad corrupted");
-                       goto fail;
-               }
                if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
                        ubi_err("bad used_ebs");
                        goto fail;
@@ -802,33 +859,40 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
 
        if (alignment != vol->alignment || data_pad != vol->data_pad ||
            upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
-           name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
+           name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
                ubi_err("volume info is different");
                goto fail;
        }
 
        spin_unlock(&ubi->volumes_lock);
-       return;
+       return 0;
 
 fail:
        ubi_err("paranoid check failed for volume %d", vol_id);
-       ubi_dbg_dump_vol_info(vol);
+       if (vol)
+               ubi_dbg_dump_vol_info(vol);
        ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
+       dump_stack();
        spin_unlock(&ubi->volumes_lock);
-       BUG();
+       return -EINVAL;
 }
 
 /**
  * paranoid_check_volumes - check information about all volumes.
  * @ubi: UBI device description object
+ *
+ * Returns zero if volumes are all right and a a negative error code if not.
  */
-static void paranoid_check_volumes(struct ubi_device *ubi)
+static int paranoid_check_volumes(struct ubi_device *ubi)
 {
-       int i;
+       int i, err = 0;
 
-       mutex_lock(&ubi->vtbl_mutex);
-       for (i = 0; i < ubi->vtbl_slots; i++)
-               paranoid_check_volume(ubi, i);
-       mutex_unlock(&ubi->vtbl_mutex);
+       for (i = 0; i < ubi->vtbl_slots; i++) {
+               err = paranoid_check_volume(ubi, i);
+               if (err)
+                       break;
+       }
+
+       return err;
 }
 #endif