Skip to contentSkip to conversation

iomap: return partial I/O count on error in iomap_dio_bio_actor

PATCH In review Goldwyn Rodrigues 7 participants 13 messages Feb 20, 2020 – Mar 2, 2020

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

PATCH v2 1/1 iomap: return partial I/O count on error in iomap_dio_bio_actor

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

---
 fs/ext4/inode.c      | 2 +-
 fs/iomap/direct-io.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index e60aca791d3f..e50e7414351a 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
 	 * the I/O. Any blocks that may have been allocated in preparation for
 	 * the direct I/O will be reused during buffered I/O.
 	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
 		return -ENOTBLK;
 
 	return 0;
fs/iomap/direct-io.c +1 -1
index 41c1e7c20a1f..01865db1bd09 100644
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;
 		}
 
Signature · 1 line
2.25.0
#1 lore raw
Quoted 8 lines · Goldwyn Rodrigues

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

Quoted 6 lines

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

I really had a hard time understanding this. I think what you meant was:

Currently, I/Os that complete with an error indicate this by passing written == 0 to the iomap_end function. However, btrfs needs to know how many bytes were written for its own accounting. Change the convention to pass the number of bytes which were actually written, and change the only user to check for a short write instead of a zero length write.

Quoted 35 lines
---
 fs/ext4/inode.c      | 2 +-
 fs/iomap/direct-io.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index e60aca791d3f..e50e7414351a 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
 	 * the I/O. Any blocks that may have been allocated in preparation for
 	 * the direct I/O will be reused during buffered I/O.
 	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
 		return -ENOTBLK;
 
 	return 0;
fs/iomap/direct-io.c +1 -1
index 41c1e7c20a1f..01865db1bd09 100644
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;
 		}
 
Signature · 1 line
2.25.0
#2 lore raw
Quoted 16 lines · Matthew Wilcox
Quoted 8 lines · Goldwyn Rodrigues

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

Quoted 6 lines

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

Quoted 8 lines

I really had a hard time understanding this. I think what you meant was:

Currently, I/Os that complete with an error indicate this by passing written == 0 to the iomap_end function. However, btrfs needs to know how many bytes were written for its own accounting. Change the convention to pass the number of bytes which were actually written, and change the only user to check for a short write instead of a zero length write.

Yes, thats right. I was trying to cover base from the previous patch and made a mess of it all. Thanks..

Quoted 36 lines
Quoted 36 lines
---
 fs/ext4/inode.c      | 2 +-
 fs/iomap/direct-io.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index e60aca791d3f..e50e7414351a 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
 	 * the I/O. Any blocks that may have been allocated in preparation for
 	 * the direct I/O will be reused during buffered I/O.
 	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
 		return -ENOTBLK;
 
 	return 0;
fs/iomap/direct-io.c +1 -1
index 41c1e7c20a1f..01865db1bd09 100644
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;
 		}
 
Signature · 1 line
2.25.0
Signature · 1 line
Goldwyn
#3 lore raw
In reply to Goldwyn Rodrigues
Quoted 39 lines · Goldwyn Rodrigues

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

---
  fs/ext4/inode.c      | 2 +-
  fs/iomap/direct-io.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index e60aca791d3f..e50e7414351a 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
  	 * the I/O. Any blocks that may have been allocated in preparation for
  	 * the direct I/O will be reused during buffered I/O.
  	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
  		return -ENOTBLK;
 
  	return 0;
fs/iomap/direct-io.c +0 -0
index 41c1e7c20a1f..01865db1bd09 100644

fs/iomap/direct-io.c iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · line 269

Quoted 7 lines
fs/iomap/direct-io.c +1 -1
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
  		size_t n;
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

Is this the correct flow? Shouldn't the while loop doing iomap_apply in iomap_dio_rw should also break in case of dio->error? Or did I miss anything?

-ritesh

#4 lore raw
Quoted 55 lines · Ritesh Harjani
Quoted 38 lines · Goldwyn Rodrigues

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

---
  fs/ext4/inode.c      | 2 +-
  fs/iomap/direct-io.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index e60aca791d3f..e50e7414351a 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
  	 * the I/O. Any blocks that may have been allocated in preparation for
  	 * the direct I/O will be reused during buffered I/O.
  	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
  		return -ENOTBLK;
  	return 0;
fs/iomap/direct-io.c +0 -0
index 41c1e7c20a1f..01865db1bd09 100644

fs/iomap/direct-io.c iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · line 269

Quoted 7 lines
fs/iomap/direct-io.c +1 -1
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
  		size_t n;
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

Quoted 4 lines

Is this the correct flow? Shouldn't the while loop doing iomap_apply in iomap_dio_rw should also break in case of dio->error? Or did I miss anything?

Yes, We can save an extra iteration by checking for dio->error in the while loop of iomap_dio_rw().

Signature · 1 line
Goldwyn
#5 lore raw
In reply to Ritesh Harjani
Quoted 4 lines · Ritesh Harjani
Quoted 4 lines
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
fs/iomap/direct-io.c +1 -1
-			copied = ret = 0;
+			ret = 0;
Quoted 8 lines
Quoted 2 lines
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

In addition to that copied is also iov_iter_reexpand call. We don't really need the re-expand in case of errors, and in fact we also have the iov_iter_revert call before jumping out, so this will need a little bit more of an audit and properly documented in the commit log.

Quoted 4 lines

Is this the correct flow? Shouldn't the while loop doing iomap_apply in iomap_dio_rw should also break in case of dio->error? Or did I miss anything?

We'd need something there iff we care about a good number of written in case of the error. Goldwyn, can you explain what you need this number for in btrfs? Maybe with a pointer to the current code base?

#6 lore raw
Quoted 21 lines · Christoph Hellwig
Quoted 4 lines · Ritesh Harjani
Quoted 4 lines
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
fs/iomap/direct-io.c +1 -1
-			copied = ret = 0;
+			ret = 0;
Quoted 8 lines
Quoted 2 lines
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

In addition to that copied is also iov_iter_reexpand call. We don't really need the re-expand in case of errors, and in fact we also have the iov_iter_revert call before jumping out, so this will need a little bit more of an audit and properly documented in the commit log.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

Is this the correct flow? Shouldn't the while loop doing iomap_apply in iomap_dio_rw should also break in case of dio->error? Or did I miss anything?

We'd need something there iff we care about a good number of written in case of the error. Goldwyn, can you explain what you need this number for in btrfs? Maybe with a pointer to the current code base?

Not sure about btrfs, but for zonefs, getting the partial I/O count done for a failed large dio would also be useful to avoid having to do the error recovery dance with report zones for getting the current zone write pointer.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#7 lore raw
In reply to Christoph Hellwig
Quoted 21 lines · Christoph Hellwig
Quoted 4 lines · Ritesh Harjani
Quoted 4 lines
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
fs/iomap/direct-io.c +1 -1
-			copied = ret = 0;
+			ret = 0;
Quoted 8 lines
Quoted 2 lines
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

In addition to that copied is also iov_iter_reexpand call. We don't really need the re-expand in case of errors, and in fact we also have the iov_iter_revert call before jumping out, so this will need a little bit more of an audit and properly documented in the commit log.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

Is this the correct flow? Shouldn't the while loop doing iomap_apply in iomap_dio_rw should also break in case of dio->error? Or did I miss anything?

We'd need something there iff we care about a good number of written in case of the error. Goldwyn, can you explain what you need this number for in btrfs? Maybe with a pointer to the current code base?

btrfs needs to account for the bytes "processed", failed or uptodate. This is currently performed in fs/btrfs/inode.c:__end_write_update_ordered().

For the current development version, how I am using it is in my git branch btrfs-iomap-dio [1]. The related commit besides this patch is:

9aeb2b31d10b ("btrfs: Use ->iomap_end() instead of btrfs_dio_data")

[1] https://github.com/goldwynr/linux/tree/btrfs-iomap-dio

Signature · 1 line
Goldwyn
#8 lore raw
In reply to Christoph Hellwig
Quoted 14 lines · Christoph Hellwig
Quoted 4 lines · Ritesh Harjani
Quoted 4 lines
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
(quoted diff) +1 -1
-			copied = ret = 0;
+			ret = 0;
Quoted 8 lines
Quoted 2 lines
  			goto out;
  		}

But if I am seeing this correctly, even after there was a dio->error if you return copied > 0, then the loop in iomap_dio_rw will continue for next iteration as well. Until the second time it won't copy anything since dio->error is set and from there I guess it may return 0 which will break the loop.

Reading the code again, there are a few clarifications.

If iomap_end() handles (written < length) as an error, iomap_apply() will return an error immediately. It will not execute the loop a second time.

On the other hand, if there is no ->iomap_end() defined by the filesystem such as in the case of XFS, we will need to check for dio->error in the do {} while loop of iomap_dio_rw().

Quoted 5 lines

In addition to that copied is also iov_iter_reexpand call. We don't really need the re-expand in case of errors, and in fact we also have the iov_iter_revert call before jumping out, so this will need a little bit more of an audit and properly documented in the commit log.

We are still handling this as an error, so why are we concerned about expanding? There is no success/written returned in iomap_dio_rw() call in case of an error.

Attached is an updated patch.

Signature · 1 line
Goldwyn

From af694f4fc662daf5c62a78391ced5f8e2d4beed2 Mon Sep 17 00:00:00 2001
From: Goldwyn Rodrigues <[email protected]>
Date: Thu, 13 Feb 2020 13:28:55 -0600
Subject: [PATCH] iomap: return partial I/O count on error in
iomap_dio_bio_actor

Currently, I/Os that complete with an error indicate this by passing written == 0 to the iomap_end function. However, btrfs needs to know how many bytes were written for its own accounting. Change the convention to pass the number of bytes which were actually written, and change the only user (ext4) to check for a short write instead of a zero length write.

In case a filesystem does not define an ->iomap_end(), check for dio->error after the iomap_apply() call to diagnose the error.

---
 fs/ext4/inode.c      | 2 +-
 fs/iomap/direct-io.c | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
fs/ext4/inode.c +1 -1
index fa0ff78dc033..d52c70f851e6 100644
@@ -3475,7 +3475,7 @@ static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
 	 * the I/O. Any blocks that may have been allocated in preparation for
 	 * the direct I/O will be reused during buffered I/O.
 	 */
-	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
+	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written < length)
 		return -ENOTBLK;
 
 	return 0;
fs/iomap/direct-io.c +5 -1
index 41c1e7c20a1f..a0002311cc20 100644
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;
 		}
 
@@ -499,6 +499,10 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	do {
 		ret = iomap_apply(inode, pos, count, flags, ops, dio,
 				iomap_dio_actor);
+
+		if (ret >= 0 && dio->error)
+			ret = dio->error;
+
 		if (ret <= 0) {
 			/* magic error code to fall back to buffered I/O */
 			if (ret == -ENOTBLK) {
Signature · 1 line
2.25.0
#9 lore raw

fs/iomap/direct-io.c iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · line 268

Quoted 8 lines · Goldwyn Rodrigues

+++ b/fs/iomap/direct-io.c

fs/iomap/direct-io.c +1 -1
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;

There's another change here ... look at the out label

out:

        /* Undo iter limitation to current extent */
        iov_iter_reexpand(dio->submit.iter, orig_count - copied);
        if (copied)
                return copied;
        return ret;

so you're also changing by how much the iter is reexpanded. I don't know if it's the appropriate amount; I still don't quite get the iov_iter complexities.

#10 lore raw
Quoted 19 lines · Matthew Wilcox

fs/iomap/direct-io.c iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · line 268

Quoted 8 lines · Goldwyn Rodrigues

+++ b/fs/iomap/direct-io.c

fs/iomap/direct-io.c +1 -1
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;

There's another change here ... look at the out label

out:

        /* Undo iter limitation to current extent */
        iov_iter_reexpand(dio->submit.iter, orig_count - copied);
        if (copied)
                return copied;
        return ret;
Quoted 4 lines

so you're also changing by how much the iter is reexpanded. I don't know if it's the appropriate amount; I still don't quite get the iov_iter complexities.

Ah, okay. Now I understand what Christoph was saying.

I suppose it is safe to remove iov_iter_reexpand(). I don't see any other goto to this label which will have a non-zero copied value. And we have already performed the iov_iter_revert().

Signature · 1 line
Goldwyn
#11 lore raw
Quoted 6 lines · Goldwyn Rodrigues

Ah, okay. Now I understand what Christoph was saying.

I suppose it is safe to remove iov_iter_reexpand(). I don't see any other goto to this label which will have a non-zero copied value. And we have already performed the iov_iter_revert().

I don't really understand the iov_iter complexities either, at least not without spending sifnificant time with the implementation. But the important thing is that you document the changes in behavior and your findings on why you think it is safe.

#12 lore raw
In reply to Goldwyn Rodrigues
Quoted 22 lines · Goldwyn Rodrigues

In case of a block device error, written parameter in iomap_end() is zero as opposed to the amount of submitted I/O. Filesystems such as btrfs need to account for the I/O in ordered extents, even if it resulted in an error. Having (incomplete) submitted bytes in written gives the filesystem the amount of data which has been submitted before the error occurred, and the filesystem code can choose how to use it.

The final returned error for iomap_dio_rw() is set by iomap_dio_complete().

Partial writes in direct I/O are considered an error. So, ->iomap_end() using written == 0 as error must be changed to written < length. In this case, ext4 is the only user.

fs/iomap/direct-io.c +0 -0
index 41c1e7c20a1f..01865db1bd09 100644

fs/iomap/direct-io.c iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · line 269

Quoted 7 lines
fs/iomap/direct-io.c +1 -1
@@ -264,7 +264,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 		size_t n;
 		if (dio->error) {
 			iov_iter_revert(dio->submit.iter, copied);
-			copied = ret = 0;
+			ret = 0;
 			goto out;
 		}

This part fixes problems I saw with the dio-iomap btrfs conversion patchset, thanks.