]> www.infradead.org Git - mtd-utils.git/commitdiff
ubiattach: fail if kernel ignores max_beb_per1024
authorRichard Genoud <richard.genoud@gmail.com>
Wed, 22 Aug 2012 16:04:37 +0000 (18:04 +0200)
committerArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Thu, 23 Aug 2012 09:48:54 +0000 (12:48 +0300)
If the kernel doesn't know the max_beb_per1024 parameter in the attach
ioctl, but the call still succeeded ubi_attach and ubi_attach_mtd will
return 1 instead of 0.

In this case, the ubiattach command will detach the device and fail with
an error message.

Signed-off-by: Richard Genoud <richard.genoud@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
ubi-utils/include/libubi.h
ubi-utils/libubi.c
ubi-utils/ubiattach.c

index 84ac84c6254f42b45b2580ec5d236008dd3081bd..47f40e250d9963d5aee8c7dd63bedfc0c03cd609 100644 (file)
@@ -221,12 +221,14 @@ int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num);
  * @req. If @req->mtd_dev_node is given it should contain path to the MTD
  * device node. Otherwise @req->mtd_num will be used.
  *
- * Returns %0 in case of success and %-1 in case of failure (errno is set).
- * The newly created UBI device number is returned in @req->dev_num.
- * The MTD device number is returned in @req->mtd_num (-1 if not found)
+ * Returns %0 in case of success, %-1 in case of failure (errno is set) and %1
+ * if parameter @req->max_beb_per1024 was ignored by kernel (because the kernel
+ * is old and does not support this feature, which was added in 3.7). The newly
+ * created UBI device number is returned in @req->dev_num. In the MTD device
+ * was specified by its device node path, the MTD device number is returned in
+ * @req->mtd_num.
  */
-int ubi_attach(libubi_t desc, const char *node,
-              struct ubi_attach_request *req);
+int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req);
 
 /**
  * ubi_detach_mtd - detach an MTD device.
index 8e1d3ee96bcfa2773059a3d49c3f1cd06290bcd4..a7463e8d003e81572958f3329c4da3755717a927 100644 (file)
@@ -769,6 +769,35 @@ int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
        r.ubi_num = req->dev_num;
        r.mtd_num = req->mtd_num;
        r.vid_hdr_offset = req->vid_hdr_offset;
+
+       if (req->max_beb_per1024) {
+               /*
+                * We first have to check if the running kernel supports the
+                * 'max_beb_per1024' parameter. To do this, we invoke the
+                * "attach" ioctl 2 times: first with incorrect value %-1 of
+                * 'max_beb_per1024'.
+                *
+                * If the ioctl succeeds, it means that the kernel doesn't
+                * support the feature and just ignored our 'max_beb_per1024'
+                * value.
+                *
+                * If the ioctl returns -EINVAL, we assume this is because
+                * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
+                * the second time with the 'max_beb_per1024' value.
+                */
+               r.max_beb_per1024 = -1;
+               ret = do_attach(node, &r);
+               if (ret == 0) {
+                       req->dev_num = r.ubi_num;
+                       /*
+                        * The call succeeded. It means that the kernel ignored
+                        * 'max_beb_per1024' parameter. 
+                        */
+                       return 1;
+               } else if (errno != EINVAL)
+                       return ret;
+       }
+
        r.max_beb_per1024 = req->max_beb_per1024;
 
        ret = do_attach(node, &r);
index 8dd0bf6692728cb1bb125fae4f940c09c2c222a3..a7c62d08645dfc9d908372b48ea6cb7badf25b32 100644 (file)
@@ -216,12 +216,18 @@ int main(int argc, char * const argv[])
        req.max_beb_per1024 = args.max_beb_per1024;
 
        err = ubi_attach(libubi, args.node, &req);
-       if (err) {
+       if (err < 0) {
                if (args.dev)
                        sys_errmsg("cannot attach \"%s\"", args.dev);
                else
                        sys_errmsg("cannot attach mtd%d", args.mtdn);
                goto out_libubi;
+       } else if (err == 1) {
+               /* The kernel did not support the 'max_beb_per1024' parameter */
+               warnmsg("the --max-beb-per1024=%d parameter was ignored", args.max_beb_per1024);
+               normsg("the UBI kernel driver does not support does not allow changing the reserved PEBs count");
+               normsg("the support was added in kernel version 3.7, probably you are running older kernel?");
+               goto out_libubi;
        }
 
        /* Print some information about the new UBI device */