* @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.
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);
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 */