aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathias Kresin <dev@kresin.me>2018-07-08 01:38:08 +0200
committerJo-Philipp Wich <jo@mein.io>2018-12-18 09:50:47 +0100
commitedc2af2be931c43a1ee111800737b08bacc60126 (patch)
treea1b7fa94aa9872a954a87b4a2140d63211e22218
parentc0673db23f1c98ccb9e5f71ec21b94153684fc80 (diff)
downloadupstream-edc2af2be931c43a1ee111800737b08bacc60126.tar.gz
upstream-edc2af2be931c43a1ee111800737b08bacc60126.tar.bz2
upstream-edc2af2be931c43a1ee111800737b08bacc60126.zip
kernel: improve ubi auto attach code readability
Move the put_mtd_device() called on multiple error conditions to a goto label to use it later for more error conditions. The early return on failed open of the mtd device and mismatching mtd type allows to get rid of one level of indentation. By jumping to the cleanup code, a refcount bug is fixed for the wrong flash type condition. While at it, make clear that we only check for the UBI magic if the read from flash was successful. Signed-off-by: Mathias Kresin <dev@kresin.me> (backported from fdf6760cda15b2710287c66c9969a3ed527793d8) (rebased patches) Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch57
-rw-r--r--target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch57
2 files changed, 62 insertions, 52 deletions
diff --git a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index 5d8f117dfc..89bac64830 100644
--- a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
-@@ -1172,6 +1172,49 @@ static struct mtd_info * __init open_mtd
+@@ -1172,6 +1172,54 @@ static struct mtd_info * __init open_mtd
return mtd;
}
@@ -20,45 +20,50 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+{
+ int err;
+ struct mtd_info *mtd;
++ size_t len;
++ char magic[4];
+
+ /* try attaching mtd device named "ubi" or "data" */
+ mtd = open_mtd_device("ubi");
+ if (IS_ERR(mtd))
+ mtd = open_mtd_device("data");
+
-+ if (!IS_ERR(mtd)) {
-+ size_t len;
-+ char magic[4];
++ if (IS_ERR(mtd))
++ return;
++
++ /* check for a valid ubi magic if read from flash was successful */
++ err = mtd_read(mtd, 0, 4, &len, (void *) magic);
++ if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
++ pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
++ goto cleanup;
++ }
+
-+ /* check for a valid ubi magic */
-+ err = mtd_read(mtd, 0, 4, &len, (void *) magic);
-+ if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
-+ pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
-+ put_mtd_device(mtd);
-+ return;
-+ }
++ /* don't auto-add media types where UBI doesn't makes sense */
++ if (mtd->type != MTD_NANDFLASH &&
++ mtd->type != MTD_NORFLASH &&
++ mtd->type != MTD_DATAFLASH &&
++ mtd->type != MTD_MLCNANDFLASH)
++ goto cleanup;
+
-+ /* auto-add only media types where UBI makes sense */
-+ if (mtd->type == MTD_NANDFLASH ||
-+ mtd->type == MTD_NORFLASH ||
-+ mtd->type == MTD_DATAFLASH ||
-+ mtd->type == MTD_MLCNANDFLASH) {
-+ mutex_lock(&ubi_devices_mutex);
-+ pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
-+ err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
-+ mutex_unlock(&ubi_devices_mutex);
-+ if (err < 0) {
-+ pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
-+ put_mtd_device(mtd);
-+ }
-+ }
++ mutex_lock(&ubi_devices_mutex);
++ pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
++ err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
++ mutex_unlock(&ubi_devices_mutex);
++ if (err < 0) {
++ pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
++ goto cleanup;
+ }
++
++ return;
++
++cleanup:
++ put_mtd_device(mtd);
+}
+
static int __init ubi_init(void)
{
int err, i, k;
-@@ -1255,6 +1298,12 @@ static int __init ubi_init(void)
+@@ -1255,6 +1303,12 @@ static int __init ubi_init(void)
}
}
diff --git a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index 8b37dba034..1436e486dd 100644
--- a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
-@@ -1226,6 +1226,49 @@ static struct mtd_info * __init open_mtd
+@@ -1226,6 +1226,54 @@ static struct mtd_info * __init open_mtd
return mtd;
}
@@ -20,45 +20,50 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+{
+ int err;
+ struct mtd_info *mtd;
++ size_t len;
++ char magic[4];
+
+ /* try attaching mtd device named "ubi" or "data" */
+ mtd = open_mtd_device("ubi");
+ if (IS_ERR(mtd))
+ mtd = open_mtd_device("data");
+
-+ if (!IS_ERR(mtd)) {
-+ size_t len;
-+ char magic[4];
++ if (IS_ERR(mtd))
++ return;
++
++ /* check for a valid ubi magic if read from flash was successful */
++ err = mtd_read(mtd, 0, 4, &len, (void *) magic);
++ if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
++ pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
++ goto cleanup;
++ }
+
-+ /* check for a valid ubi magic */
-+ err = mtd_read(mtd, 0, 4, &len, (void *) magic);
-+ if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
-+ pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
-+ put_mtd_device(mtd);
-+ return;
-+ }
++ /* don't auto-add media types where UBI doesn't makes sense */
++ if (mtd->type != MTD_NANDFLASH &&
++ mtd->type != MTD_NORFLASH &&
++ mtd->type != MTD_DATAFLASH &&
++ mtd->type != MTD_MLCNANDFLASH)
++ goto cleanup;
+
-+ /* auto-add only media types where UBI makes sense */
-+ if (mtd->type == MTD_NANDFLASH ||
-+ mtd->type == MTD_NORFLASH ||
-+ mtd->type == MTD_DATAFLASH ||
-+ mtd->type == MTD_MLCNANDFLASH) {
-+ mutex_lock(&ubi_devices_mutex);
-+ pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
-+ err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
-+ mutex_unlock(&ubi_devices_mutex);
-+ if (err < 0) {
-+ pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
-+ put_mtd_device(mtd);
-+ }
-+ }
++ mutex_lock(&ubi_devices_mutex);
++ pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
++ err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
++ mutex_unlock(&ubi_devices_mutex);
++ if (err < 0) {
++ pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
++ goto cleanup;
+ }
++
++ return;
++
++cleanup:
++ put_mtd_device(mtd);
+}
+
static int __init ubi_init(void)
{
int err, i, k;
-@@ -1309,6 +1352,12 @@ static int __init ubi_init(void)
+@@ -1309,6 +1357,12 @@ static int __init ubi_init(void)
}
}