Patchwork [MTD-UTILS,v2,4/4] ubiattach: fail if kernel ignores max_beb_per1024

login
register
mail settings
Submitter Richard Genoud
Date Aug. 22, 2012, 4:04 p.m.
Message ID <1345651477-5301-5-git-send-email-richard.genoud@gmail.com>
Download mbox | patch
Permalink /patch/179344/
State New
Headers show

Comments

Richard Genoud - Aug. 22, 2012, 4:04 p.m.
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>
---
 ubi-utils/include/libubi.h |    7 +++++--
 ubi-utils/libubi.c         |   34 ++++++++++++++++++++++++++++++++++
 ubi-utils/ubiattach.c      |    9 ++++++++-
 3 files changed, 47 insertions(+), 3 deletions(-)
Artem Bityutskiy - Aug. 23, 2012, 9:55 a.m.
On Wed, 2012-08-22 at 18:04 +0200, Richard Genoud wrote:
> 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>

Pushed with some amendments, thanks!
> +	} else if (err == 1) {
> +		/*
> +		 * The kernel did not support the max_beb_per1024 parameter.
> +		 */
> +		(void) ubi_detach_mtd(libubi, args.node, req.mtd_num);
> +		errmsg("Your UBI driver does not allow changing the reserved PEBs count, probably you run an old kernel ? The support was added in kernel version 3.7.");
> +		goto out_libubi;

Here I've removed the ubi_detach_mtd() call and the goto. Since we have
already spent energy for attaching, I think it is nicer to just print a
warning and not detach, and let the user decide if he wants to detach.
Richard Genoud - Aug. 23, 2012, 10:01 a.m.
2012/8/23 Artem Bityutskiy <dedekind1@gmail.com>:
>> +     } else if (err == 1) {
>> +             /*
>> +              * The kernel did not support the max_beb_per1024 parameter.
>> +              */
>> +             (void) ubi_detach_mtd(libubi, args.node, req.mtd_num);
>> +             errmsg("Your UBI driver does not allow changing the reserved PEBs count, probably you run an old kernel ? The support was added in kernel version 3.7.");
>> +             goto out_libubi;
>
> Here I've removed the ubi_detach_mtd() call and the goto. Since we have
> already spent energy for attaching, I think it is nicer to just print a
> warning and not detach, and let the user decide if he wants to detach.
Yes, I hesitate between error and warning.

Thanks !

Richard.

Patch

diff --git a/ubi-utils/include/libubi.h b/ubi-utils/include/libubi.h
index 28c8782..da3956d 100644
--- a/ubi-utils/include/libubi.h
+++ b/ubi-utils/include/libubi.h
@@ -218,7 +218,9 @@  int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num);
  * @req: MTD attach request.
  *
  * This function creates a new UBI device by attaching an MTD device as
- * described by @req. Returns %0 in case of success and %-1 in case of failure.
+ * described by @req.
+ * 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.
  * The newly created UBI device number is returned in @req->dev_num.
  */
 int ubi_attach_mtd(libubi_t desc, const char *node,
@@ -235,7 +237,8 @@  int ubi_attach_mtd(libubi_t desc, const char *node,
  * device node. Otherwise functionality is similar than in function
  * 'ubi_attach_mtd()' where @req->mtd_num is used.
  *
- * Returns %0 in case of success and %-1 in case of failure (errno is set).
+ * 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.
  * 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)
  */
diff --git a/ubi-utils/libubi.c b/ubi-utils/libubi.c
index 1b62e59..cc20667 100644
--- a/ubi-utils/libubi.c
+++ b/ubi-utils/libubi.c
@@ -719,6 +719,40 @@  int ubi_attach_mtd(libubi_t desc, const char *node,
 	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) {
+		/*
+		 * max_beb_per1024 was provided by user.
+		 * In this case, we have to check if it's supported by kernel
+		 * or not.
+		 * For that, we're going to make a first call with a wrong
+		 * value.
+		 * If the return value is OK, it means that the kernel doesn't
+		 * support the feature.
+		 * If the return value is not OK nor -EINVAL, another error
+		 * happened.
+		 * If the return value is -EINVAL, we're making a second call,
+		 * with the real value this time.
+		 */
+		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. We return 1 to let the
+			 * user know about this.
+			 */
+			return 1;
+		} else {
+			if (errno != EINVAL)
+				return ret;
+			/*
+			 * max_beb_per1024 may be supported,
+			 * let's make the 2nd call.
+			 */
+		}
+	}
 	r.max_beb_per1024 = req->max_beb_per1024;
 
 	ret = do_attach(node, &r);
diff --git a/ubi-utils/ubiattach.c b/ubi-utils/ubiattach.c
index 4521788..1cf0025 100644
--- a/ubi-utils/ubiattach.c
+++ b/ubi-utils/ubiattach.c
@@ -214,12 +214,19 @@  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.
+		 */
+		(void) ubi_detach_mtd(libubi, args.node, req.mtd_num);
+		errmsg("Your UBI driver does not allow changing the reserved PEBs count, probably you run an old kernel ? The support was added in kernel version 3.7.");
+		goto out_libubi;
 	}
 
 	/* Print some information about the new UBI device */