[PATCH v2] fs: align write return codes with POSIX

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Feb 2 05:27:34 PST 2023

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

  barebox at 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

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 at pengutronix.de>
Reviewed-by: Marco Felsch <m.felsch at pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum at 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;

More information about the barebox mailing list