mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/6] ubi: remove character device flag from static volumes
@ 2016-11-25  8:06 Teresa Remmet
  2016-11-25  8:06 ` [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC Teresa Remmet
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Instead of marking static volume device files as character devices 
introduced a truncate callback for device files.
This makes it possible to dump static ubi volumes from flash.

Teresa Remmet (6):
  libfile: copy_file: Only open regular files with O_TRUNC
  devfs: Add optional truncate callback for device files
  ubi: Add truncate callback
  fs: Remove O_TRUNC check for devices when open files
  commands: ubi: ubiupdatevol: Open device with O_TRUNC
  ubi: barebox: Remove character device flag from static volumes

 commands/ubi.c            |  2 +-
 drivers/mtd/ubi/barebox.c | 19 ++++++++++++++++---
 fs/devfs.c                |  5 +++++
 fs/fs.c                   |  2 +-
 include/driver.h          |  1 +
 lib/libfile.c             | 22 ++++++++++++++++------
 6 files changed, 40 insertions(+), 11 deletions(-)

-- 
1.9.1


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

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

* [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-11-25  8:06 ` [PATCH 2/6] devfs: Add optional truncate callback for device files Teresa Remmet
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Device files can not truncate in the most cases. Check if the
destination is a regular file and open only those with O_TRUNC.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 lib/libfile.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index cba2f02..049ec32 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -263,9 +263,10 @@ int copy_file(const char *src, const char *dst, int verbose)
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1, err1 = 0;
+	int mode;
 	void *buf;
 	int total = 0;
-	struct stat statbuf;
+	struct stat srcstat, dststat;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -275,17 +276,26 @@ int copy_file(const char *src, const char *dst, int verbose)
 		goto out;
 	}
 
-	dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC);
+	ret = stat(dst, &dststat);
+	if (ret)
+		goto out;
+
+	mode = O_WRONLY | O_CREAT;
+
+	if (S_ISREG(dststat.st_mode))
+		mode |= O_TRUNC;
+
+	dstfd = open(dst, mode);
 	if (dstfd < 0) {
 		printf("could not open %s: %s\n", dst, errno_str());
 		goto out;
 	}
 
 	if (verbose) {
-		if (stat(src, &statbuf) < 0)
-			statbuf.st_size = 0;
+		if (stat(src, &srcstat) < 0)
+			srcstat.st_size = 0;
 
-		init_progression_bar(statbuf.st_size);
+		init_progression_bar(srcstat.st_size);
 	}
 
 	while (1) {
@@ -310,7 +320,7 @@ int copy_file(const char *src, const char *dst, int verbose)
 		}
 
 		if (verbose) {
-			if (statbuf.st_size && statbuf.st_size != FILESIZE_MAX)
+			if (srcstat.st_size && srcstat.st_size != FILESIZE_MAX)
 				show_progress(total);
 			else
 				show_progress(total / 16384);
-- 
1.9.1


_______________________________________________
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/6] devfs: Add optional truncate callback for device files
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
  2016-11-25  8:06 ` [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-11-25  8:06 ` [PATCH 3/6] ubi: Add truncate callback Teresa Remmet
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Not all device files have trivial fix device sizes like static
ubi volumes. Here the device file size equals the image size it contains.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 fs/devfs.c       | 5 +++++
 include/driver.h | 1 +
 2 files changed, 6 insertions(+)

diff --git a/fs/devfs.c b/fs/devfs.c
index 6fabcf8..2a7b1b3 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -168,6 +168,11 @@ static int devfs_ioctl(struct device_d *_dev, FILE *f, int request, void *buf)
 
 static int devfs_truncate(struct device_d *dev, FILE *f, ulong size)
 {
+	struct cdev *cdev = f->priv;
+
+	if (cdev->ops->truncate)
+		return cdev->ops->truncate(cdev, size);
+
 	if (f->fsdev->dev.num_resources < 1)
 		return -ENOSPC;
 	if (size > resource_size(&f->fsdev->dev.resource[0]))
diff --git a/include/driver.h b/include/driver.h
index 80aa8d8..6abaaad 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -434,6 +434,7 @@ struct file_operations {
 	int (*erase)(struct cdev*, loff_t count, loff_t offset);
 	int (*protect)(struct cdev*, size_t count, loff_t offset, int prot);
 	int (*memmap)(struct cdev*, void **map, int flags);
+	int (*truncate)(struct cdev*, size_t size);
 };
 
 #define MAX_PARTUUID_STR	sizeof("00112233-4455-6677-8899-AABBCCDDEEFF")
-- 
1.9.1


_______________________________________________
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/6] ubi: Add truncate callback
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
  2016-11-25  8:06 ` [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC Teresa Remmet
  2016-11-25  8:06 ` [PATCH 2/6] devfs: Add optional truncate callback for device files Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-11-25  8:06 ` [PATCH 4/6] fs: Remove O_TRUNC check for devices when open files Teresa Remmet
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

The size of static ubi volumes changes depending on the content.
Add truncate callback to handle resizes.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 drivers/mtd/ubi/barebox.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/mtd/ubi/barebox.c b/drivers/mtd/ubi/barebox.c
index 329cf45..2ad731a 100644
--- a/drivers/mtd/ubi/barebox.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -162,6 +162,21 @@ static loff_t ubi_volume_cdev_lseek(struct cdev *cdev, loff_t ofs)
 	return ofs;
 }
 
+static int ubi_volume_cdev_truncate(struct cdev *cdev, size_t size)
+{
+	struct ubi_volume_cdev_priv *priv = cdev->priv;
+	struct ubi_device *ubi = priv->ubi;
+	struct ubi_volume *vol = priv->vol;
+	uint64_t rsvd_bytes;
+
+	rsvd_bytes = (long long)vol->reserved_pebs *
+			ubi->leb_size - vol->data_pad;
+	if (size > rsvd_bytes)
+		return -ENOSPC;
+
+	return 0;
+}
+
 static int ubi_volume_cdev_ioctl(struct cdev *cdev, int cmd, void *buf)
 {
 	struct ubi_volume_cdev_priv *priv = cdev->priv;
@@ -210,6 +225,7 @@ static struct file_operations ubi_volume_fops = {
 	.write  = ubi_volume_cdev_write,
 	.lseek	= ubi_volume_cdev_lseek,
 	.ioctl  = ubi_volume_cdev_ioctl,
+	.truncate = ubi_volume_cdev_truncate,
 };
 
 int ubi_volume_cdev_add(struct ubi_device *ubi, struct ubi_volume *vol)
-- 
1.9.1


_______________________________________________
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/6] fs: Remove O_TRUNC check for devices when open files
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
                   ` (2 preceding siblings ...)
  2016-11-25  8:06 ` [PATCH 3/6] ubi: Add truncate callback Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-11-25  8:06 ` [PATCH 5/6] commands: ubi: ubiupdatevol: Open device with O_TRUNC Teresa Remmet
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Don't check for the file type here. Devices may have truncate
callbacks. Otherwise the caller should check if O_TRUNC is possible.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 fs/fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/fs.c b/fs/fs.c
index b7e7c63..2b4659c 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -682,7 +682,7 @@ int open(const char *pathname, int flags, ...)
 	if (ret)
 		goto out;
 
-	if (!(s.st_mode & S_IFCHR) && (flags & O_TRUNC)) {
+	if (flags & O_TRUNC) {
 		ret = fsdrv->truncate(&fsdev->dev, f, 0);
 		f->size = 0;
 		if (ret)
-- 
1.9.1


_______________________________________________
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/6] commands: ubi: ubiupdatevol: Open device with O_TRUNC
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
                   ` (3 preceding siblings ...)
  2016-11-25  8:06 ` [PATCH 4/6] fs: Remove O_TRUNC check for devices when open files Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-11-25  8:06 ` [PATCH 6/6] ubi: barebox: Remove character device flag from static volumes Teresa Remmet
  2016-12-07 20:13 ` [PATCH 0/6] ubi: remove " Sascha Hauer
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Static ubi volumes should be opend with O_TRUNC as the device file
size equals the content.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 commands/ubi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/ubi.c b/commands/ubi.c
index 4c445d2..5e27584 100644
--- a/commands/ubi.c
+++ b/commands/ubi.c
@@ -42,7 +42,7 @@ static int do_ubiupdatevol(int argc, char *argv[])
 		return 1;
 	}
 
-	fd_vol = open(argv[optind], O_WRONLY);
+	fd_vol = open(argv[optind], O_WRONLY | O_TRUNC);
 	if (fd_vol < 0) {
 		perror("open volume");
 		ret = 1;
-- 
1.9.1


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

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

* [PATCH 6/6] ubi: barebox: Remove character device flag from static volumes
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
                   ` (4 preceding siblings ...)
  2016-11-25  8:06 ` [PATCH 5/6] commands: ubi: ubiupdatevol: Open device with O_TRUNC Teresa Remmet
@ 2016-11-25  8:06 ` Teresa Remmet
  2016-12-07 20:13 ` [PATCH 0/6] ubi: remove " Sascha Hauer
  6 siblings, 0 replies; 8+ messages in thread
From: Teresa Remmet @ 2016-11-25  8:06 UTC (permalink / raw)
  To: barebox

Character device flag was set for ubi static volumes to vary the device
file size. This is now done with the truncate option. So no need for
the character device flag. This makes it also possible to dump a image from
the static volume.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 drivers/mtd/ubi/barebox.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/mtd/ubi/barebox.c b/drivers/mtd/ubi/barebox.c
index 2ad731a..d67e566 100644
--- a/drivers/mtd/ubi/barebox.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -244,9 +244,6 @@ int ubi_volume_cdev_add(struct ubi_device *ubi, struct ubi_volume *vol)
 	cdev->priv = priv;
 	cdev->size = vol->used_bytes;
 
-	if (vol->vol_type == UBI_STATIC_VOLUME)
-		cdev->flags = DEVFS_IS_CHARACTER_DEV;
-
 	cdev->dev = &vol->dev;
 	ubi_msg(ubi, "registering %s as /dev/%s", vol->name, cdev->name);
 	ret = devfs_create(cdev);
-- 
1.9.1


_______________________________________________
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 0/6] ubi: remove character device flag from static volumes
  2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
                   ` (5 preceding siblings ...)
  2016-11-25  8:06 ` [PATCH 6/6] ubi: barebox: Remove character device flag from static volumes Teresa Remmet
@ 2016-12-07 20:13 ` Sascha Hauer
  6 siblings, 0 replies; 8+ messages in thread
From: Sascha Hauer @ 2016-12-07 20:13 UTC (permalink / raw)
  To: Teresa Remmet; +Cc: barebox

On Fri, Nov 25, 2016 at 09:06:01AM +0100, Teresa Remmet wrote:
> Instead of marking static volume device files as character devices 
> introduced a truncate callback for device files.
> This makes it possible to dump static ubi volumes from flash.
> 
> Teresa Remmet (6):
>   libfile: copy_file: Only open regular files with O_TRUNC
>   devfs: Add optional truncate callback for device files
>   ubi: Add truncate callback
>   fs: Remove O_TRUNC check for devices when open files
>   commands: ubi: ubiupdatevol: Open device with O_TRUNC
>   ubi: barebox: Remove character device flag from static volumes

Applied, thanks

Sascha

> 
>  commands/ubi.c            |  2 +-
>  drivers/mtd/ubi/barebox.c | 19 ++++++++++++++++---
>  fs/devfs.c                |  5 +++++
>  fs/fs.c                   |  2 +-
>  include/driver.h          |  1 +
>  lib/libfile.c             | 22 ++++++++++++++++------
>  6 files changed, 40 insertions(+), 11 deletions(-)
> 
> -- 
> 1.9.1
> 
> 
> _______________________________________________
> 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

end of thread, other threads:[~2016-12-07 20:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
2016-11-25  8:06 ` [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC Teresa Remmet
2016-11-25  8:06 ` [PATCH 2/6] devfs: Add optional truncate callback for device files Teresa Remmet
2016-11-25  8:06 ` [PATCH 3/6] ubi: Add truncate callback Teresa Remmet
2016-11-25  8:06 ` [PATCH 4/6] fs: Remove O_TRUNC check for devices when open files Teresa Remmet
2016-11-25  8:06 ` [PATCH 5/6] commands: ubi: ubiupdatevol: Open device with O_TRUNC Teresa Remmet
2016-11-25  8:06 ` [PATCH 6/6] ubi: barebox: Remove character device flag from static volumes Teresa Remmet
2016-12-07 20:13 ` [PATCH 0/6] ubi: remove " Sascha Hauer

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