Skip to contentSkip to conversation

aio: partial write should not return error code.

PATCH In review Rusty Russell 2 participants 6 messages Jan 3, 2008 – Jan 4, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio: partial write should not return error code.

When an AIO write gets an error after writing some data (eg. ENOSPC), it should return the amount written already, not the error. Just like write() is supposed to.

This was found by the libaio test suite.

fs/aio.c +7 -0
diff -r 18802689361a fs/aio.c
@@ -1346,6 +1350,13 @@ static ssize_t aio_rw_vect_retry(struct 
 	/* This means we must have transferred all that we could */
 	/* No need to retry anymore */
 	if ((ret == 0) || (iocb->ki_left == 0))
+		ret = iocb->ki_nbytes - iocb->ki_left;
+
+	/* If we managed to write some out we return that, rather than
+	 * the eventual error. */
+	if (opcode == IOCB_CMD_PWRITEV
+	    && ret < 0
+	    && iocb->ki_nbytes - iocb->ki_left)
 		ret = iocb->ki_nbytes - iocb->ki_left;
 
 	return ret;
#1 lore raw

PATCH 1/1 aio: negative offset should return -EINVAL

An AIO read or write should return -EINVAL if the offset is negative. This check matches the one in pread and pwrite.

This was found by the libaio test suite.

fs/aio.c +4 -0
diff -r 18802689361a fs/aio.c
@@ -1330,6 +1330,10 @@ static ssize_t aio_rw_vect_retry(struct 
 		opcode = IOCB_CMD_PWRITEV;
 	}
 
+	/* This matches the pread()/pwrite() logic */
+	if (iocb->ki_pos < 0)
+		return -EINVAL;
+
 	do {
 		ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
 			    iocb->ki_nr_segs - iocb->ki_cur_seg,
#3 lore raw
In reply to Rusty Russell
Quoted 3 lines · Rusty Russell

When an AIO write gets an error after writing some data (eg. ENOSPC), it should return the amount written already, not the error. Just like write() is supposed to.

Andrew, please don't queue this fix. I think the bug is valid but the patch is subtly dangerous.

Quoted 5 lines

diff -r 18802689361a fs/aio.c

fs/aio.c +0 -0
@@ -1346,6 +1350,13 @@ static ssize_t aio_rw_vect_retry(struct 
 	/* This means we must have transferred all that we could */

fs/aio.c static ssize_t aio_rw_vect_retry(struct · line 1360

Quoted 10 lines
fs/aio.c +7 -0
 	/* No need to retry anymore */
 	if ((ret == 0) || (iocb->ki_left == 0))
+		ret = iocb->ki_nbytes - iocb->ki_left;
+
+	/* If we managed to write some out we return that, rather than
+	 * the eventual error. */
+	if (opcode == IOCB_CMD_PWRITEV
+	    && ret < 0
+	    && iocb->ki_nbytes - iocb->ki_left)
 		ret = iocb->ki_nbytes - iocb->ki_left;

This doesn't account for the (sigh) -EIOCB* error codes. They must be returned to the caller so that it can properly handle the iocb reference counting. Failure to do so can lead to oopses.

To be fair, I think you'll have a really hard time finding an ->aio_write() implementation which would return partial progress and *then* one of the magical errnos. But the infrastructure does allow it.

So maybe we could get a helper in aio.h that abstracts out the

	(ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY)

condition. Then I think this patch would be fine.

I assigned a bug to remind myself to revisit this if you aren't excited by continuing with the patch:

http://bugzilla.kernel.org/show_bug.cgi?id=9681

- z

#4 lore raw
In reply to Zach Brown
Quoted 28 lines · On Friday 04 January 2008 07:04:30 Zach Brown wrote:
Quoted 3 lines · Rusty Russell

When an AIO write gets an error after writing some data (eg. ENOSPC), it should return the amount written already, not the error. Just like write() is supposed to.

Andrew, please don't queue this fix. I think the bug is valid but the patch is subtly dangerous.

Quoted 5 lines

diff -r 18802689361a fs/aio.c

fs/aio.c +0 -0
@@ -1346,6 +1350,13 @@ static ssize_t aio_rw_vect_retry(struct
 	/* This means we must have transferred all that we could */

fs/aio.c static ssize_t aio_rw_vect_retry(struct · line 1360

Quoted 10 lines
fs/aio.c +7 -0
 	/* No need to retry anymore */
 	if ((ret == 0) || (iocb->ki_left == 0))
+		ret = iocb->ki_nbytes - iocb->ki_left;
+
+	/* If we managed to write some out we return that, rather than
+	 * the eventual error. */
+	if (opcode == IOCB_CMD_PWRITEV
+	    && ret < 0
+	    && iocb->ki_nbytes - iocb->ki_left)
 		ret = iocb->ki_nbytes - iocb->ki_left;

This doesn't account for the (sigh) -EIOCB* error codes. They must be returned to the caller so that it can properly handle the iocb reference counting. Failure to do so can lead to oopses.

Quoted 3 lines

To be fair, I think you'll have a really hard time finding an ->aio_write() implementation which would return partial progress and *then* one of the magical errnos. But the infrastructure does allow it.

Erk, thanks.

Quoted 10 lines

So maybe we could get a helper in aio.h that abstracts out the

	(ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY)

condition. Then I think this patch would be fine.

I assigned a bug to remind myself to revisit this if you aren't excited by continuing with the patch:

http://bugzilla.kernel.org/show_bug.cgi?id=9681

No, that's fine, here is the new one:

When an AIO write gets a non-retry error after writing some data (eg. ENOSPC), it should return the amount written already, not the error. Just like write() is supposed to.

This was found by the libaio test suite.

---
 fs/aio.c |    7 +++++++
 1 file changed, 7 insertions(+)
fs/aio.c +7 -0
diff -r 18802689361a fs/aio.c
@@ -1346,6 +1350,13 @@ static ssize_t aio_rw_vect_retry(struct 
 	/* This means we must have transferred all that we could */
 	/* No need to retry anymore */
 	if ((ret == 0) || (iocb->ki_left == 0))
+		ret = iocb->ki_nbytes - iocb->ki_left;
+
+	/* If we managed to write some out we return that, rather than
+	 * the eventual error. */
+	if (opcode == IOCB_CMD_PWRITEV
+	    && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
+	    && iocb->ki_nbytes - iocb->ki_left)
 		ret = iocb->ki_nbytes - iocb->ki_left;
 
 	return ret;
Acked-by
#5 lore raw
Quoted 11 lines

No, that's fine, here is the new one:

When an AIO write gets a non-retry error after writing some data (eg. ENOSPC), it should return the amount written already, not the error. Just like write() is supposed to.

This was found by the libaio test suite.

This looks good, feel free to push this from your tree.

- z