]> err.no Git - linux-2.6/blobdiff - drivers/mtd/ubi/kapi.c
UBI: add ubi_leb_map interface
[linux-2.6] / drivers / mtd / ubi / kapi.c
index d352c4575c3da3d6f3f834116daa72062eb2bf86..e1ef802a03a76fedcaaec03cd93797803200c67f 100644 (file)
@@ -37,14 +37,9 @@ int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
 {
        const struct ubi_device *ubi;
 
-       if (!try_module_get(THIS_MODULE))
-               return -ENODEV;
-
        if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
-           !ubi_devices[ubi_num]) {
-               module_put(THIS_MODULE);
+           !ubi_devices[ubi_num])
                return -ENODEV;
-       }
 
        ubi = ubi_devices[ubi_num];
        di->ubi_num = ubi->ubi_num;
@@ -52,7 +47,6 @@ int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
        di->min_io_size = ubi->min_io_size;
        di->ro_mode = ubi->ro_mode;
        di->cdev = MKDEV(ubi->major, 0);
-       module_put(THIS_MODULE);
        return 0;
 }
 EXPORT_SYMBOL_GPL(ubi_get_device_info);
@@ -105,16 +99,21 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
 {
        int err;
        struct ubi_volume_desc *desc;
-       struct ubi_device *ubi = ubi_devices[ubi_num];
+       struct ubi_device *ubi;
        struct ubi_volume *vol;
 
        dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
 
        err = -ENODEV;
+       if (ubi_num < 0)
+               return ERR_PTR(err);
+
+       ubi = ubi_devices[ubi_num];
+
        if (!try_module_get(THIS_MODULE))
                return ERR_PTR(err);
 
-       if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
+       if (ubi_num >= UBI_MAX_DEVICES || !ubi)
                goto out_put;
 
        err = -EINVAL;
@@ -319,9 +318,14 @@ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
            offset + len > vol->usable_leb_size)
                return -EINVAL;
 
-       if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 &&
-           offset + len > vol->last_eb_bytes)
-               return -EINVAL;
+       if (vol->vol_type == UBI_STATIC_VOLUME) {
+               if (vol->used_ebs == 0)
+                       /* Empty static UBI volume */
+                       return 0;
+               if (lnum == vol->used_ebs - 1 &&
+                   offset + len > vol->last_eb_bytes)
+                       return -EINVAL;
+       }
 
        if (vol->upd_marker)
                return -EBADF;
@@ -542,6 +546,51 @@ int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
 }
 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
 
+/**
+ * ubi_leb_map - map logical erasblock to a physical eraseblock.
+ * @desc: volume descriptor
+ * @lnum: logical eraseblock number
+ * @dtype: expected data type
+ *
+ * This function maps an un-mapped logical eraseblock @lnum to a physical
+ * eraseblock. This means, that after a successfull invocation of this
+ * function the logical eraseblock @lnum will be empty (contain only %0xFF
+ * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
+ * happens.
+ *
+ * This function returns zero in case of success, %-EBADF if the volume is
+ * damaged because of an interrupted update, %-EBADMSG if the logical
+ * eraseblock is already mapped, and other negative error codes in case of
+ * other failures.
+ */
+int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
+{
+       struct ubi_volume *vol = desc->vol;
+       struct ubi_device *ubi = vol->ubi;
+       int vol_id = vol->vol_id;
+
+       dbg_msg("unmap LEB %d:%d", vol_id, lnum);
+
+       if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
+               return -EROFS;
+
+       if (lnum < 0 || lnum >= vol->reserved_pebs)
+               return -EINVAL;
+
+       if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
+           dtype != UBI_UNKNOWN)
+               return -EINVAL;
+
+       if (vol->upd_marker)
+               return -EBADF;
+
+       if (vol->eba_tbl[lnum] >= 0)
+               return -EBADMSG;
+
+       return ubi_eba_write_leb(ubi, vol_id, lnum, NULL, 0, 0, dtype);
+}
+EXPORT_SYMBOL_GPL(ubi_leb_map);
+
 /**
  * ubi_is_mapped - check if logical eraseblock is mapped.
  * @desc: volume descriptor