UBI: fix error path

2008-02-07 Thread Linux Kernel Mailing List
Gitweb: 
http://git.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=fc75a1e166268e0c3366c3b30888a024125f6665
Commit: fc75a1e166268e0c3366c3b30888a024125f6665
Parent: 450f872a8e1763c883c9f723e6937b7ed223e6d3
Author: Artem Bityutskiy [EMAIL PROTECTED]
AuthorDate: Mon Dec 17 14:02:09 2007 +0200
Committer:  Artem Bityutskiy [EMAIL PROTECTED]
CommitDate: Wed Dec 26 19:15:15 2007 +0200

UBI: fix error path

Error path in volume creation is bogus. First of, it ovverrides the
'err' variable and returns zero to the caller. Second, ubi_assert()
in the release function is wrong.

Signed-off-by: Artem Bityutskiy [EMAIL PROTECTED]
---
 drivers/mtd/ubi/vmt.c |   45 -
 1 files changed, 20 insertions(+), 25 deletions(-)

diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 9dd3689..3d6ac02 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -112,7 +112,6 @@ static void vol_release(struct device *dev)
 {
struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
 
-   ubi_assert(vol-removed);
kfree(vol);
 }
 
@@ -154,9 +153,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;
 }
 
 /**
@@ -188,7 +185,7 @@ static void volume_sysfs_close(struct ubi_volume *vol)
  */
 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, dont_free = 0;
struct ubi_volume *vol;
struct ubi_vtbl_record vtbl_rec;
uint64_t bytes;
@@ -317,6 +314,7 @@ 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) {
@@ -353,8 +351,20 @@ int ubi_create_volume(struct ubi_device *ubi, struct 
ubi_mkvol_req *req)
mutex_unlock(ubi-volumes_mutex);
return 0;
 
+out_sysfs:
+   /*
+* We have degistered 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.
+*/
+   dont_free = 1;
+   get_device(vol-dev);
+   volume_sysfs_close(vol);
 out_gluebi:
-   err = ubi_destroy_gluebi(vol);
+   ubi_destroy_gluebi(vol);
 out_cdev:
cdev_del(vol-cdev);
 out_mapping:
@@ -367,25 +377,10 @@ out_acc:
 out_unlock:
spin_unlock(ubi-volumes_lock);
mutex_unlock(ubi-volumes_mutex);
-   kfree(vol);
-   ubi_err(cannot create volume %d, error %d, vol_id, err);
-   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);
-   mutex_unlock(ubi-volumes_mutex);
-   volume_sysfs_close(vol);
+   if (dont_free)
+   put_device(vol-dev);
+   else
+   kfree(vol);
ubi_err(cannot create volume %d, error %d, vol_id, err);
return err;
 }
-
To unsubscribe from this list: send the line unsubscribe git-commits-head in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


UBI: fix error path in create_vtbl()

2007-07-18 Thread Linux Kernel Mailing List
Gitweb: 
http://git.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=78d87c95b89ccf86c142494beada3082810ed368
Commit: 78d87c95b89ccf86c142494beada3082810ed368
Parent: c4e90ec0134d7bedebbe3fe58ed5d431293886d4
Author: Artem Bityutskiy [EMAIL PROTECTED]
AuthorDate: Sat May 5 11:24:02 2007 +0300
Committer:  Artem Bityutskiy [EMAIL PROTECTED]
CommitDate: Wed Jul 18 16:52:32 2007 +0300

UBI: fix error path in create_vtbl()

There were several bugs in volume table creation error path. Thanks to
Satyam Sharma [EMAIL PROTECTED] and Florin Malita [EMAIL PROTECTED]
for finding and analysing them: http://lkml.org/lkml/2007/5/3/274

This patch makes ubi_scan_add_to_list() static and renames it to
add_to_list(), just because it is not needed outside scan.c anymore.

Signed-off-by: Artem Bityutskiy [EMAIL PROTECTED]
---
 drivers/mtd/ubi/scan.c |   46 +++---
 drivers/mtd/ubi/scan.h |2 --
 drivers/mtd/ubi/vtbl.c |   14 --
 3 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index e445686..30d536e 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -24,7 +24,7 @@
  * This unit is responsible for scanning the flash media, checking UBI
  * headers and providing complete information about the UBI flash image.
  *
- * The scanning information is reoresented by a struct ubi_scan_info' object.
+ * The scanning information is represented by a struct ubi_scan_info' object.
  * Information about found volumes is represented by struct ubi_scan_volume
  * objects which are kept in volume RB-tree with root at the @volumes field.
  * The RB-tree is indexed by the volume ID.
@@ -55,8 +55,19 @@ static int paranoid_check_si(const struct ubi_device *ubi,
 static struct ubi_ec_hdr *ech;
 static struct ubi_vid_hdr *vidh;
 
-int ubi_scan_add_to_list(struct ubi_scan_info *si, int pnum, int ec,
-struct list_head *list)
+/*
+ * add_to_list - add physical eraseblock to a list.
+ * @si: scanning information
+ * @pnum: physical eraseblock number to add
+ * @ec: erase counter of the physical eraseblock
+ * @list: the list to add to
+ *
+ * This function adds physical eraseblock @pnum to free, erase, corrupted or
+ * alien lists. Returns zero in case of success and a negative error code in
+ * case of failure.
+ */
+static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
+  struct list_head *list)
 {
struct ubi_scan_leb *seb;
 
@@ -492,11 +503,11 @@ int ubi_scan_add_used(const struct ubi_device *ubi, 
struct ubi_scan_info *si,
return err;
 
if (cmp_res  4)
-   err = ubi_scan_add_to_list(si, seb-pnum,
-  seb-ec, si-corr);
+   err = add_to_list(si, seb-pnum, seb-ec,
+ si-corr);
else
-   err = ubi_scan_add_to_list(si, seb-pnum,
-  seb-ec, si-erase);
+   err = add_to_list(si, seb-pnum, seb-ec,
+ si-erase);
if (err)
return err;
 
@@ -517,11 +528,9 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct 
ubi_scan_info *si,
 * previously.
 */
if (cmp_res  4)
-   return ubi_scan_add_to_list(si, pnum, ec,
-   si-corr);
+   return add_to_list(si, pnum, ec, si-corr);
else
-   return ubi_scan_add_to_list(si, pnum, ec,
-   si-erase);
+   return add_to_list(si, pnum, ec, si-erase);
}
}
 
@@ -754,7 +763,7 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(const struct 
ubi_device *ubi,
  * @si: scanning information
  * @pnum: the physical eraseblock number
  *
- * This function returns a zero if the physical eraseblock was succesfully
+ * This function returns a zero if the physical eraseblock was successfully
  * handled and a negative error code in case of failure.
  */
 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int 
pnum)
@@ -783,8 +792,7 @@ static int process_eb(struct ubi_device *ubi, struct 
ubi_scan_info *si, int pnum
else if (err == UBI_IO_BITFLIPS)
bitflips = 1;
else if (err == UBI_IO_PEB_EMPTY)
-   return ubi_scan_add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC,
-   si-erase