block device driver broken

Andries.Brouwer@cwi.nl
Sun, 25 Feb 1996 01:43:34 +0100


Linus diagnoses the complaint

: > toyland:~# dd if=/dev/zero of=/dev/fd0h1440
: > dd: /dev/fd0h1440: No such file or directory
: > 2881+0 records in
: > 2880+0 records out

correctly: dd does the analog of

: if (write(fd, buffer, size) != size) {
: perror("device");
: exit(1);
: }

and prints a random error message since errno has not been set.
However, I think that both Linux and dd are buggy here.

POSIX-1 (IEEE 1003.1-1990) says (Section 6.4.2.4):
If any of the following conditions occur, the write() function
shall return -1 and set errno to the corresponding value.
...
ENOSPC There is no free space remaining on the device containing
the file.
...

It seems to me that this requirement is applicable, and hence I
disagree with

: When the floppy fills up, it _should_ return a value of zero when it
: can't write any more.

The patch below changes Linux behaviour to what I think is prescribed
by POSIX, and after making this change I get

# dd if=/dev/zero of=/dev/loop0
dd: /dev/loop0: No space left on device
15+0 records in
14+0 records out
#

as desired.

diff -u --recursive --new-file ../linux-1.3.68/linux/fs/block_dev.c ./linux/fs/block_dev.c
--- ../linux-1.3.68/linux/fs/block_dev.c Sun Nov 26 18:23:10 1995
+++ ./linux/fs/block_dev.c Sun Feb 25 01:05:43 1996
@@ -62,7 +62,7 @@
size = INT_MAX;
while (count>0) {
if (block >= size)
- return written;
+ return written ? written : -ENOSPC;
chars = blocksize - offset;
if (chars > count)
chars=count;