mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* Add and use function API for UBI
@ 2016-09-22  7:49 Sascha Hauer
  2016-09-22  7:49 ` [PATCH 1/4] mtd: ubi: Add API calls to create/remove volumes Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2016-09-22  7:49 UTC (permalink / raw)
  To: Barebox List

We currently use a ioctl API to manipulate UBI devices. This is
suboptimal since we have to carry code for all ioctls with us, even
when some of it may be unused. This series expands the UBI API with
calls to add/remove volumes and uses it inside the UBI commands.
The next step would be to move the commands to separate files and
to make them Kconfig configurable individually.

Sascha

----------------------------------------------------------------
Sascha Hauer (4):
      mtd: ubi: Add API calls to create/remove volumes
      mtd: ubi: introduce barebox specific ioctl to get ubi_num
      mtd: ubi: commands: use function API to access ubi volumes
      mtd: ubi: remove now unused ioctls

 commands/ubi.c            | 40 +++++++++++++++++++++++++++++++---------
 drivers/mtd/ubi/barebox.c | 43 ++++++++++++++++++++++++++++++-------------
 include/linux/mtd/ubi.h   |  3 +++
 include/mtd/ubi-user.h    |  2 ++
 4 files changed, 66 insertions(+), 22 deletions(-)

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] mtd: ubi: Add API calls to create/remove volumes
  2016-09-22  7:49 Add and use function API for UBI Sascha Hauer
@ 2016-09-22  7:49 ` Sascha Hauer
  2016-09-22  7:49 ` [PATCH 2/4] mtd: ubi: introduce barebox specific ioctl to get ubi_num Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2016-09-22  7:49 UTC (permalink / raw)
  To: Barebox List

Currently we use a ioctl API to create/remove ubi volumes. This
means we always have to carry all function code for ubi volume
manipulation when the ioctl is compiled in.
This adds a function API to create/remove volumes so that the linker
can throw the unused code away later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/ubi/barebox.c | 23 +++++++++++++++++++++++
 include/linux/mtd/ubi.h   |  3 +++
 2 files changed, 26 insertions(+)

diff --git a/drivers/mtd/ubi/barebox.c b/drivers/mtd/ubi/barebox.c
index fc60aae..13c2a47 100644
--- a/drivers/mtd/ubi/barebox.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -257,6 +257,29 @@ void ubi_volume_cdev_remove(struct ubi_volume *vol)
 	kfree(priv);
 }
 
+int ubi_api_create_volume(int ubi_num, struct ubi_mkvol_req *req)
+{
+	struct ubi_device *ubi;
+	int ret;
+
+	ubi = ubi_get_device(ubi_num);
+	if (!ubi)
+		return -ENODEV;
+
+	if (!req->bytes)
+		req->bytes = (__s64)ubi->avail_pebs * ubi->leb_size;
+	ret = ubi_create_volume(ubi, req);
+
+	ubi_put_device(ubi);
+
+	return ret;
+}
+
+int ubi_api_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
+{
+	return ubi_remove_volume(desc, no_vtbl);
+}
+
 static int ubi_cdev_ioctl(struct cdev *cdev, int cmd, void *buf)
 {
 	struct ubi_volume_desc *desc;
diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h
index 0614681..c72f95b 100644
--- a/include/linux/mtd/ubi.h
+++ b/include/linux/mtd/ubi.h
@@ -218,6 +218,9 @@ int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
 int ubi_sync(int ubi_num);
 int ubi_flush(int ubi_num, int vol_id, int lnum);
 
+int ubi_api_create_volume(int ubi_num, struct ubi_mkvol_req *req);
+int ubi_api_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
+
 /*
  * This function is the same as the 'ubi_leb_read()' function, but it does not
  * provide the checking capability.
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] mtd: ubi: introduce barebox specific ioctl to get ubi_num
  2016-09-22  7:49 Add and use function API for UBI Sascha Hauer
  2016-09-22  7:49 ` [PATCH 1/4] mtd: ubi: Add API calls to create/remove volumes Sascha Hauer
@ 2016-09-22  7:49 ` Sascha Hauer
  2016-09-22  7:49 ` [PATCH 3/4] mtd: ubi: commands: use function API to access ubi volumes Sascha Hauer
  2016-09-22  7:49 ` [PATCH 4/4] mtd: ubi: remove now unused ioctls Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2016-09-22  7:49 UTC (permalink / raw)
  To: Barebox List

Code wishing to manipulate ubi devices from outside the ubi
layer needs the ubi_num as reference. Add an ioctl to get
the ubi_num from a filedescriptor.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/ubi/barebox.c | 3 +++
 include/mtd/ubi-user.h    | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/drivers/mtd/ubi/barebox.c b/drivers/mtd/ubi/barebox.c
index 13c2a47..cb9223e 100644
--- a/drivers/mtd/ubi/barebox.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -299,6 +299,9 @@ static int ubi_cdev_ioctl(struct cdev *cdev, int cmd, void *buf)
 		if (!req->bytes)
 			req->bytes = (__s64)ubi->avail_pebs * ubi->leb_size;
 		return ubi_create_volume(ubi, req);
+	case UBI_IOCGETUBINUM:
+		*(u32 *)buf = ubi->ubi_num;
+		break;
 	};
 
 	return 0;
diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h
index 8c02f96..9425533 100644
--- a/include/mtd/ubi-user.h
+++ b/include/mtd/ubi-user.h
@@ -161,6 +161,8 @@
 /* Re-name volumes */
 #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req)
 
+#define UBI_IOCGETUBINUM _IOR(UBI_IOC_MAGIC, 32, __u32)
+
 /* ioctl commands of the UBI control character device */
 
 #define UBI_CTRL_IOC_MAGIC 'o'
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] mtd: ubi: commands: use function API to access ubi volumes
  2016-09-22  7:49 Add and use function API for UBI Sascha Hauer
  2016-09-22  7:49 ` [PATCH 1/4] mtd: ubi: Add API calls to create/remove volumes Sascha Hauer
  2016-09-22  7:49 ` [PATCH 2/4] mtd: ubi: introduce barebox specific ioctl to get ubi_num Sascha Hauer
@ 2016-09-22  7:49 ` Sascha Hauer
  2016-09-22  7:49 ` [PATCH 4/4] mtd: ubi: remove now unused ioctls Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2016-09-22  7:49 UTC (permalink / raw)
  To: Barebox List

We have a function API to manipulate ubi volumes, use it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/ubi.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/commands/ubi.c b/commands/ubi.c
index 26b521f..7c55195 100644
--- a/commands/ubi.c
+++ b/commands/ubi.c
@@ -9,6 +9,7 @@
 #include <linux/kernel.h>
 #include <linux/stat.h>
 #include <linux/mtd/mtd-abi.h>
+#include <linux/mtd/ubi.h>
 #include <mtd/ubi-user.h>
 #include <mtd/ubi-media.h>
 
@@ -104,6 +105,7 @@ static int do_ubimkvol(int argc, char *argv[])
 	struct ubi_mkvol_req req;
 	int fd, ret;
 	uint64_t size;
+	uint32_t ubinum;
 
 	req.vol_type = UBI_DYNAMIC_VOLUME;
 
@@ -140,9 +142,14 @@ static int do_ubimkvol(int argc, char *argv[])
 		return 1;
 	}
 
-	ret = ioctl(fd, UBI_IOCMKVOL, &req);
-	if (ret)
-		printf("failed to create: %s\n", strerror(-ret));
+	ret = ioctl(fd, UBI_IOCGETUBINUM, &ubinum);
+	if (ret) {
+		printf("failed to get ubinum: %s\n", strerror(-ret));
+		goto err;
+	}
+
+	ret = ubi_api_create_volume(ubinum, &req);
+err:
 	close(fd);
 
 	return ret ? 1 : 0;
@@ -272,24 +279,39 @@ BAREBOX_CMD_END
 
 static int do_ubirmvol(int argc, char *argv[])
 {
-	struct ubi_mkvol_req req;
+	struct ubi_volume_desc *desc;
+	uint32_t ubinum;
 	int fd, ret;
 
 	if (argc != 3)
 		return COMMAND_ERROR_USAGE;
 
-	strncpy(req.name, argv[2], UBI_VOL_NAME_MAX);
-	req.name[UBI_VOL_NAME_MAX] = 0;
-
 	fd = open(argv[1], O_WRONLY);
 	if (fd < 0) {
 		perror("open");
 		return 1;
 	}
 
-	ret = ioctl(fd, UBI_IOCRMVOL, &req);
+	ret = ioctl(fd, UBI_IOCGETUBINUM, &ubinum);
+	if (ret) {
+		printf("failed to get ubinum: %s\n", strerror(-ret));
+		goto err;
+	}
+
+	desc = ubi_open_volume_nm(ubinum, argv[2], UBI_EXCLUSIVE);
+	if (IS_ERR(desc)) {
+		ret = PTR_ERR(desc);
+		printf("failed to open volume %s: %s\n", argv[2], strerror(-ret));
+		goto err1;
+	}
+
+	ret = ubi_api_remove_volume(desc, 0);
 	if (ret)
-		printf("failed to delete: %s\n", strerror(-ret));
+		printf("failed to remove volume %s: %s\n", argv[2], strerror(-ret));
+
+err1:
+	ubi_close_volume(desc);
+err:
 	close(fd);
 
 	return ret ? 1 : 0;
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] mtd: ubi: remove now unused ioctls
  2016-09-22  7:49 Add and use function API for UBI Sascha Hauer
                   ` (2 preceding siblings ...)
  2016-09-22  7:49 ` [PATCH 3/4] mtd: ubi: commands: use function API to access ubi volumes Sascha Hauer
@ 2016-09-22  7:49 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2016-09-22  7:49 UTC (permalink / raw)
  To: Barebox List

The only ioctl needed is the one to get the ubi_num from a file
descriptor. The remaining ioctls are now implemented as regular
function calls.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/ubi/barebox.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/drivers/mtd/ubi/barebox.c b/drivers/mtd/ubi/barebox.c
index cb9223e..23c4bfd 100644
--- a/drivers/mtd/ubi/barebox.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -282,23 +282,14 @@ int ubi_api_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 
 static int ubi_cdev_ioctl(struct cdev *cdev, int cmd, void *buf)
 {
-	struct ubi_volume_desc *desc;
 	struct ubi_device *ubi = cdev->priv;
-	struct ubi_mkvol_req *req = buf;
 
 	switch (cmd) {
-	case UBI_IOCRMVOL:
-		desc = ubi_open_volume_nm(ubi->ubi_num, req->name,
-                                           UBI_EXCLUSIVE);
-		if (IS_ERR(desc))
-			return PTR_ERR(desc);
-		ubi_remove_volume(desc, 0);
-		ubi_close_volume(desc);
-		break;
-	case UBI_IOCMKVOL:
-		if (!req->bytes)
-			req->bytes = (__s64)ubi->avail_pebs * ubi->leb_size;
-		return ubi_create_volume(ubi, req);
+	/*
+	 * Only supported ioctl is a barebox specific one to get the ubi_num
+	 * from the file descriptor. The rest is implemented as function calls
+	 * directly.
+	 */
 	case UBI_IOCGETUBINUM:
 		*(u32 *)buf = ubi->ubi_num;
 		break;
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-09-22  7:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-22  7:49 Add and use function API for UBI Sascha Hauer
2016-09-22  7:49 ` [PATCH 1/4] mtd: ubi: Add API calls to create/remove volumes Sascha Hauer
2016-09-22  7:49 ` [PATCH 2/4] mtd: ubi: introduce barebox specific ioctl to get ubi_num Sascha Hauer
2016-09-22  7:49 ` [PATCH 3/4] mtd: ubi: commands: use function API to access ubi volumes Sascha Hauer
2016-09-22  7:49 ` [PATCH 4/4] mtd: ubi: remove now unused ioctls Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox