mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] fs: align write return codes with POSIX
@ 2023-02-02 13:27 Ahmad Fatoum
  2023-02-03  8:00 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2023-02-02 13:27 UTC (permalink / raw)
  To: barebox; +Cc: Marco Felsch, Ahmad Fatoum

Writing past end of a file results in a cryptic error code:

  barebox@board:/ cp /dev/zero /dev/mmc0.part
  write: Operation not permitted
  cp: Operation not permitted

Because the cdev's truncate is not implemented and as such partition
can't be increased in size. POSIX specifies EPERM as the correct return
code for truncate(2) in such a situation, but for write(2) it is ENOSPC.
Thus most truncate callbacks in barebox instead return ENOSPC, when
according to POSIX, EPERM would have been the correct error code to
propagate.

Switching all truncate drivers is a bit more involved, so for now let's
treat EPERM and ENOSPC instead when truncate fails to enlarge a file.

Reported-by: Marco Felsch <m.felsch@pengutronix.de>
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - fix commit message title (s/truncate/write/)
  - added Marco's Reviewed-by
---
 fs/fs.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/fs.c b/fs/fs.c
index 752688b574a0..c463466e1be0 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -441,6 +441,8 @@ static ssize_t __write(FILE *f, const void *buf, size_t count)
 	if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size) {
 		ret = fsdev_truncate(&f->fsdev->dev, f, f->pos + count);
 		if (ret) {
+			if (ret == -EPERM)
+				ret = -ENOSPC;
 			if (ret != -ENOSPC)
 				goto out;
 			count = f->size - f->pos;
-- 
2.30.2




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

* Re: [PATCH v2] fs: align write return codes with POSIX
  2023-02-02 13:27 [PATCH v2] fs: align write return codes with POSIX Ahmad Fatoum
@ 2023-02-03  8:00 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2023-02-03  8:00 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, Marco Felsch

On Thu, Feb 02, 2023 at 02:27:34PM +0100, Ahmad Fatoum wrote:
> Writing past end of a file results in a cryptic error code:
> 
>   barebox@board:/ cp /dev/zero /dev/mmc0.part
>   write: Operation not permitted
>   cp: Operation not permitted
> 
> Because the cdev's truncate is not implemented and as such partition
> can't be increased in size. POSIX specifies EPERM as the correct return
> code for truncate(2) in such a situation, but for write(2) it is ENOSPC.
> Thus most truncate callbacks in barebox instead return ENOSPC, when
> according to POSIX, EPERM would have been the correct error code to
> propagate.
> 
> Switching all truncate drivers is a bit more involved, so for now let's
> treat EPERM and ENOSPC instead when truncate fails to enlarge a file.
> 
> Reported-by: Marco Felsch <m.felsch@pengutronix.de>
> Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> v1 -> v2:
>   - fix commit message title (s/truncate/write/)
>   - added Marco's Reviewed-by
> ---
>  fs/fs.c | 2 ++
>  1 file changed, 2 insertions(+)

Updated to this version of the patch.

Sascha

> 
> diff --git a/fs/fs.c b/fs/fs.c
> index 752688b574a0..c463466e1be0 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -441,6 +441,8 @@ static ssize_t __write(FILE *f, const void *buf, size_t count)
>  	if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size) {
>  		ret = fsdev_truncate(&f->fsdev->dev, f, f->pos + count);
>  		if (ret) {
> +			if (ret == -EPERM)
> +				ret = -ENOSPC;
>  			if (ret != -ENOSPC)
>  				goto out;
>  			count = f->size - f->pos;
> -- 
> 2.30.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



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

end of thread, other threads:[~2023-02-03  8:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 13:27 [PATCH v2] fs: align write return codes with POSIX Ahmad Fatoum
2023-02-03  8:00 ` Sascha Hauer

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