mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] cdev: make file operations const
@ 2017-05-30 14:09 Philipp Zabel
  2017-05-30 14:09 ` [PATCH 2/5] commands: mount: combine dev and devstr, which are the same Philipp Zabel
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Philipp Zabel @ 2017-05-30 14:09 UTC (permalink / raw)
  To: barebox

scripts/checkpatch.pl warns that struct file_operations should be const,
but cdev->ops is not const, so without this patch we can choose between
a warning from checkpatch and a warning from the compiler about
discarding the const attribute when assigning the struct file_operations
cdev->ops.

Since there is no reason to modify the contents of cdev->ops after
probing, make it const.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 include/driver.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/driver.h b/include/driver.h
index 52e06f7d62..086e44636b 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -440,7 +440,7 @@ struct file_operations {
 #define MAX_PARTUUID_STR	sizeof("00112233-4455-6677-8899-AABBCCDDEEFF")
 
 struct cdev {
-	struct file_operations *ops;
+	const struct file_operations *ops;
 	void *priv;
 	struct device_d *dev;
 	struct device_node *device_node;
-- 
2.11.0


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

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

* [PATCH 2/5] commands: mount: combine dev and devstr, which are the same
  2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
@ 2017-05-30 14:09 ` Philipp Zabel
  2017-05-30 14:09 ` [PATCH 3/5] fs: add parseopt_b for loop mount option Philipp Zabel
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Philipp Zabel @ 2017-05-30 14:09 UTC (permalink / raw)
  To: barebox

Unify the device path used by the default mount and mount with specified
mount point paths.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 commands/mount.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/commands/mount.c b/commands/mount.c
index aa769d46fe..f05e23b227 100644
--- a/commands/mount.c
+++ b/commands/mount.c
@@ -29,7 +29,7 @@ static int do_mount(int argc, char *argv[])
 	int opt, verbose = 0;
 	struct driver_d *drv;
 	const char *type = NULL;
-	const char *mountpoint, *dev;
+	const char *mountpoint, *devstr;
 	const char *fsoptions = NULL;
 
 	while ((opt = getopt(argc, argv, "ao:t:v")) > 0) {
@@ -70,11 +70,11 @@ static int do_mount(int argc, char *argv[])
 		return 0;
 	}
 
+	devstr = argv[optind];
+
 	if (argc == optind + 1) {
 		struct cdev *cdev;
-		const char *path, *devstr;
-
-		devstr = argv[optind];
+		const char *path;
 
 		if (!strncmp(devstr, "/dev/", 5))
 			devstr += 5;
@@ -97,8 +97,6 @@ static int do_mount(int argc, char *argv[])
 	if (argc < optind + 2)
 		return COMMAND_ERROR_USAGE;
 
-	dev = argv[optind];
-
 	if (argc == optind + 3) {
 		/*
 		 * Old behaviour: mount <dev> <type> <mountpoint>
@@ -109,7 +107,7 @@ static int do_mount(int argc, char *argv[])
 		mountpoint = argv[optind + 1];
 	}
 
-	return mount(dev, type, mountpoint, fsoptions);
+	return mount(devstr, type, mountpoint, fsoptions);
 }
 
 BAREBOX_CMD_HELP_START(mount)
-- 
2.11.0


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

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

* [PATCH 3/5] fs: add parseopt_b for loop mount option
  2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
  2017-05-30 14:09 ` [PATCH 2/5] commands: mount: combine dev and devstr, which are the same Philipp Zabel
@ 2017-05-30 14:09 ` Philipp Zabel
  2017-05-30 14:09 ` [PATCH 4/5] fs: add cdev_create_from_file " Philipp Zabel
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Philipp Zabel @ 2017-05-30 14:09 UTC (permalink / raw)
  To: barebox

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 fs/parseopt.c | 26 ++++++++++++++++++++++++++
 fs/parseopt.h |  1 +
 2 files changed, 27 insertions(+)

diff --git a/fs/parseopt.c b/fs/parseopt.c
index 12dbe1813c..8ff83019a7 100644
--- a/fs/parseopt.c
+++ b/fs/parseopt.c
@@ -2,6 +2,32 @@
 
 #include "parseopt.h"
 
+void parseopt_b(const char *options, const char *opt, bool *val)
+{
+	const char *start;
+	size_t optlen = strlen(opt);
+
+again:
+	start = strstr(options, opt);
+
+	if (!start) {
+		*val = false;
+		return;
+	}
+
+	if (start > options && start[-1] != ',') {
+		options = start;
+		goto again;
+	}
+
+	if (start[optlen] != ',' && start[optlen] != '\0') {
+		options = start;
+		goto again;
+	}
+
+	*val = true;
+}
+
 void parseopt_hu(const char *options, const char *opt, unsigned short *val)
 {
 	const char *start;
diff --git a/fs/parseopt.h b/fs/parseopt.h
index a8523b6a10..abf3be3f35 100644
--- a/fs/parseopt.h
+++ b/fs/parseopt.h
@@ -1 +1,2 @@
+void parseopt_b(const char *options, const char *opt, bool *val);
 void parseopt_hu(const char *options, const char *opt, unsigned short *val);
-- 
2.11.0


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

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

* [PATCH 4/5] fs: add cdev_create_from_file for loop mount option
  2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
  2017-05-30 14:09 ` [PATCH 2/5] commands: mount: combine dev and devstr, which are the same Philipp Zabel
  2017-05-30 14:09 ` [PATCH 3/5] fs: add parseopt_b for loop mount option Philipp Zabel
@ 2017-05-30 14:09 ` Philipp Zabel
  2017-06-01  6:31   ` Sascha Hauer
  2017-05-30 14:09 ` [PATCH 5/5] commands: mount: mention file loop mounts in the help text Philipp Zabel
  2017-06-01  6:32 ` [PATCH 1/5] cdev: make file operations const Sascha Hauer
  4 siblings, 1 reply; 8+ messages in thread
From: Philipp Zabel @ 2017-05-30 14:09 UTC (permalink / raw)
  To: barebox

Allow to create a loopback cdev from a file.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 fs/devfs-core.c  | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fs.c          | 21 ++++++++++++---
 include/driver.h |  1 +
 3 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 382606f1cf..7ffa1e4327 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -20,11 +20,14 @@
 #include <complete.h>
 #include <driver.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <malloc.h>
 #include <ioctl.h>
 #include <nand.h>
 #include <linux/err.h>
+#include <linux/fs.h>
 #include <linux/mtd/mtd.h>
+#include <unistd.h>
 
 LIST_HEAD(cdev_list);
 
@@ -407,3 +410,81 @@ int devfs_create_partitions(const char *devname,
 
 	return 0;
 }
+
+struct loop_priv {
+	int fd;
+};
+
+static ssize_t loop_read(struct cdev *cdev, void *buf, size_t count,
+		loff_t offset, ulong flags)
+{
+	struct loop_priv *priv = cdev->priv;
+	loff_t ofs;
+
+	ofs = lseek(priv->fd, offset, SEEK_SET);
+	if (ofs < 0)
+		return ofs;
+
+	return read(priv->fd, buf, count);
+}
+
+static ssize_t loop_write(struct cdev *cdev, const void *buf, size_t count,
+		loff_t offset, ulong flags)
+{
+	struct loop_priv *priv = cdev->priv;
+	loff_t ofs;
+
+	ofs = lseek(priv->fd, offset, SEEK_SET);
+	if (ofs < 0)
+		return ofs;
+
+	return write(priv->fd, buf, count);
+}
+
+static const struct file_operations loop_ops = {
+	.read = loop_read,
+	.write = loop_write,
+	.memmap = generic_memmap_rw,
+	.lseek = dev_lseek_default,
+};
+
+struct cdev *cdev_create_from_file(const char *path, ulong flags)
+{
+	struct cdev *new;
+	struct loop_priv *priv;
+	static int loopno;
+	loff_t ofs;
+
+	priv = xzalloc(sizeof(*priv));
+
+	priv->fd = open(path, flags);
+	if (priv->fd < 0) {
+		free(priv);
+		return NULL;
+	}
+
+	new = xzalloc(sizeof(*new));
+
+	new->name = strdup(path);
+
+	new->ops = &loop_ops;
+	new->name = basprintf("loop%u", loopno);
+	new->priv = priv;
+
+	ofs = lseek(priv->fd, 0, SEEK_END);
+	if (ofs < 0) {
+		free(new);
+		free(priv);
+		return NULL;
+	}
+	lseek(priv->fd, 0, SEEK_SET);
+
+	new->size = ofs;
+	new->offset = 0;
+	new->dev = NULL;
+	new->flags = 0;
+
+	devfs_create(new);
+
+	return new;
+}
diff --git a/fs/fs.c b/fs/fs.c
index 1901c94ad1..621ea19749 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -36,6 +36,8 @@
 #include <block.h>
 #include <libfile.h>
 
+#include "parseopt.h"
+
 char *mkmodestr(unsigned long mode, char *str)
 {
 	static const char *l = "xwr";
@@ -1222,13 +1224,18 @@ int register_fs_driver(struct fs_driver_d *fsdrv)
 }
 EXPORT_SYMBOL(register_fs_driver);
 
-static const char *detect_fs(const char *filename)
+static const char *detect_fs(const char *filename, const char *fsoptions)
 {
 	enum filetype type;
 	struct driver_d *drv;
 	struct fs_driver_d *fdrv;
+	bool loop;
 
-	type = cdev_detect_type(filename);
+	parseopt_b(fsoptions, "loop", &loop);
+	if (loop)
+		type = file_name_detect_type(filename);
+	else
+		type = cdev_detect_type(filename);
 
 	if (type == filetype_unknown)
 		return NULL;
@@ -1245,7 +1252,13 @@ static const char *detect_fs(const char *filename)
 
 int fsdev_open_cdev(struct fs_device_d *fsdev)
 {
-	fsdev->cdev = cdev_open(fsdev->backingstore, O_RDWR);
+	bool loop;
+
+	parseopt_b(fsdev->options, "loop", &loop);
+	if (loop)
+		fsdev->cdev = cdev_create_from_file(fsdev->backingstore, O_RDWR);
+	else
+		fsdev->cdev = cdev_open(fsdev->backingstore, O_RDWR);
 	if (!fsdev->cdev)
 		return -EINVAL;
 
@@ -1292,7 +1305,7 @@ int mount(const char *device, const char *fsname, const char *_path,
 	}
 
 	if (!fsname)
-		fsname = detect_fs(device);
+		fsname = detect_fs(device, fsoptions);
 
 	if (!fsname)
 		return -ENOENT;
diff --git a/include/driver.h b/include/driver.h
index 086e44636b..daa9453a3b 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -473,6 +473,7 @@ struct cdev *lcdev_by_name(const char *filename);
 struct cdev *cdev_readlink(struct cdev *cdev);
 struct cdev *cdev_by_device_node(struct device_node *node);
 struct cdev *cdev_open(const char *name, unsigned long flags);
+struct cdev *cdev_create_from_file(const char *path, ulong flags);
 int cdev_do_open(struct cdev *, unsigned long flags);
 void cdev_close(struct cdev *cdev);
 int cdev_flush(struct cdev *cdev);
-- 
2.11.0


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

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

* [PATCH 5/5] commands: mount: mention file loop mounts in the help text
  2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
                   ` (2 preceding siblings ...)
  2017-05-30 14:09 ` [PATCH 4/5] fs: add cdev_create_from_file " Philipp Zabel
@ 2017-05-30 14:09 ` Philipp Zabel
  2017-06-01  6:32 ` [PATCH 1/5] cdev: make file operations const Sascha Hauer
  4 siblings, 0 replies; 8+ messages in thread
From: Philipp Zabel @ 2017-05-30 14:09 UTC (permalink / raw)
  To: barebox

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 commands/mount.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/commands/mount.c b/commands/mount.c
index f05e23b227..4cf1179b7b 100644
--- a/commands/mount.c
+++ b/commands/mount.c
@@ -117,6 +117,7 @@ BAREBOX_CMD_HELP_TEXT("With -a the mount command mounts all block devices whose
 BAREBOX_CMD_HELP_TEXT("can be detected automatically to /mnt/PARTNAME")
 BAREBOX_CMD_HELP_TEXT("If mountpoint is not given, a standard mountpoint of /mnt/DEVICE")
 BAREBOX_CMD_HELP_TEXT("is used. This directoy is created automatically if necessary.")
+BAREBOX_CMD_HELP_TEXT("With -o loop the mount command mounts a file instead of a device.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT("-a\t", "mount all blockdevices")
-- 
2.11.0


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

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

* Re: [PATCH 4/5] fs: add cdev_create_from_file for loop mount option
  2017-05-30 14:09 ` [PATCH 4/5] fs: add cdev_create_from_file " Philipp Zabel
@ 2017-06-01  6:31   ` Sascha Hauer
  2017-06-01  9:37     ` Philipp Zabel
  0 siblings, 1 reply; 8+ messages in thread
From: Sascha Hauer @ 2017-06-01  6:31 UTC (permalink / raw)
  To: Philipp Zabel; +Cc: barebox

Hi Philipp,

On Tue, May 30, 2017 at 04:09:46PM +0200, Philipp Zabel wrote:
> Allow to create a loopback cdev from a file.
> 
> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
> ---
>  fs/devfs-core.c  | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  fs/fs.c          | 21 ++++++++++++---
>  include/driver.h |  1 +
>  3 files changed, 99 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> +static ssize_t loop_write(struct cdev *cdev, const void *buf, size_t count,
> +		loff_t offset, ulong flags)
> +{
> +	struct loop_priv *priv = cdev->priv;
> +	loff_t ofs;
> +
> +	ofs = lseek(priv->fd, offset, SEEK_SET);
> +	if (ofs < 0)
> +		return ofs;
> +
> +	return write(priv->fd, buf, count);
> +}
> +
> +static const struct file_operations loop_ops = {
> +	.read = loop_read,
> +	.write = loop_write,
> +	.memmap = generic_memmap_rw,
> +	.lseek = dev_lseek_default,
> +};
> +
> +struct cdev *cdev_create_from_file(const char *path, ulong flags)

Looks good, but I'm missing the counterpart of this function and its
usage in umount().

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

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

* Re: [PATCH 1/5] cdev: make file operations const
  2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
                   ` (3 preceding siblings ...)
  2017-05-30 14:09 ` [PATCH 5/5] commands: mount: mention file loop mounts in the help text Philipp Zabel
@ 2017-06-01  6:32 ` Sascha Hauer
  4 siblings, 0 replies; 8+ messages in thread
From: Sascha Hauer @ 2017-06-01  6:32 UTC (permalink / raw)
  To: Philipp Zabel; +Cc: barebox

On Tue, May 30, 2017 at 04:09:43PM +0200, Philipp Zabel wrote:
> scripts/checkpatch.pl warns that struct file_operations should be const,
> but cdev->ops is not const, so without this patch we can choose between
> a warning from checkpatch and a warning from the compiler about
> discarding the const attribute when assigning the struct file_operations
> cdev->ops.
> 
> Since there is no reason to modify the contents of cdev->ops after
> probing, make it const.

Applied this one and the second patch.

Sascha

> 
> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
> ---
>  include/driver.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/driver.h b/include/driver.h
> index 52e06f7d62..086e44636b 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -440,7 +440,7 @@ struct file_operations {
>  #define MAX_PARTUUID_STR	sizeof("00112233-4455-6677-8899-AABBCCDDEEFF")
>  
>  struct cdev {
> -	struct file_operations *ops;
> +	const struct file_operations *ops;
>  	void *priv;
>  	struct device_d *dev;
>  	struct device_node *device_node;
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

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

* Re: [PATCH 4/5] fs: add cdev_create_from_file for loop mount option
  2017-06-01  6:31   ` Sascha Hauer
@ 2017-06-01  9:37     ` Philipp Zabel
  0 siblings, 0 replies; 8+ messages in thread
From: Philipp Zabel @ 2017-06-01  9:37 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Thu, 2017-06-01 at 08:31 +0200, Sascha Hauer wrote:
> Hi Philipp,
> 
> On Tue, May 30, 2017 at 04:09:46PM +0200, Philipp Zabel wrote:
> > Allow to create a loopback cdev from a file.
> > 
> > Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
> > ---
> >  fs/devfs-core.c  | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  fs/fs.c          | 21 ++++++++++++---
> >  include/driver.h |  1 +
> >  3 files changed, 99 insertions(+), 4 deletions(-)
> > 
> > diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> > +static ssize_t loop_write(struct cdev *cdev, const void *buf, size_t count,
> > +		loff_t offset, ulong flags)
> > +{
> > +	struct loop_priv *priv = cdev->priv;
> > +	loff_t ofs;
> > +
> > +	ofs = lseek(priv->fd, offset, SEEK_SET);
> > +	if (ofs < 0)
> > +		return ofs;
> > +
> > +	return write(priv->fd, buf, count);
> > +}
> > +
> > +static const struct file_operations loop_ops = {
> > +	.read = loop_read,
> > +	.write = loop_write,
> > +	.memmap = generic_memmap_rw,
> > +	.lseek = dev_lseek_default,
> > +};
> > +
> > +struct cdev *cdev_create_from_file(const char *path, ulong flags)
> 
> Looks good, but I'm missing the counterpart of this function and its
> usage in umount().

Thanks, I'll add a .close callback to loop_ops to call close(priv->fd)
on umount.

regards
Philipp



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

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

end of thread, other threads:[~2017-06-01  9:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-30 14:09 [PATCH 1/5] cdev: make file operations const Philipp Zabel
2017-05-30 14:09 ` [PATCH 2/5] commands: mount: combine dev and devstr, which are the same Philipp Zabel
2017-05-30 14:09 ` [PATCH 3/5] fs: add parseopt_b for loop mount option Philipp Zabel
2017-05-30 14:09 ` [PATCH 4/5] fs: add cdev_create_from_file " Philipp Zabel
2017-06-01  6:31   ` Sascha Hauer
2017-06-01  9:37     ` Philipp Zabel
2017-05-30 14:09 ` [PATCH 5/5] commands: mount: mention file loop mounts in the help text Philipp Zabel
2017-06-01  6:32 ` [PATCH 1/5] cdev: make file operations const Sascha Hauer

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