Skip to contentSkip to conversation

ceph: skip copying the data extends the file EOF

PATCH Superseded [email protected] 3 participants 7 messages Feb 22, 2024 – Mar 19, 2024 Ceph client

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ceph: skip copying the data extends the file EOF

From: Xiubo Li <[email protected]>

V2:
- append a new patch to improve the getattr code

Xiubo Li (2):
ceph: skip copying the data extends the file EOF
ceph: set the correct mask for getattr reqeust for read

 fs/ceph/file.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 ceph: skip copying the data extends the file EOF

From: Xiubo Li <[email protected]>

If hits the EOF it will revise the return value to the i_size instead of the real length read, but it will advance the offset of iovc, then for the next try it may be incorrectly skipped.

This will just skip advancing the iovc's offset more than i_size.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
 fs/ceph/file.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
fs/ceph/file.c +8 -10
index 71d29571712d..2b2b07a0a61b 100644
@@ -1195,7 +1195,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		}
 
 		idx = 0;
-		left = ret > 0 ? ret : 0;
+		left = ret > 0 ? umin(ret, i_size) : 0;
 		while (left > 0) {
 			size_t plen, copied;
 
@@ -1224,15 +1224,13 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 	}
 
 	if (ret > 0) {
-		if (off > *ki_pos) {
-			if (off >= i_size) {
-				*retry_op = CHECK_EOF;
-				ret = i_size - *ki_pos;
-				*ki_pos = i_size;
-			} else {
-				ret = off - *ki_pos;
-				*ki_pos = off;
-			}
+		if (off >= i_size) {
+			*retry_op = CHECK_EOF;
+			ret = i_size - *ki_pos;
+			*ki_pos = i_size;
+		} else {
+			ret = off - *ki_pos;
+			*ki_pos = off;
 		}
 
 		if (last_objver)
Signature · 1 line
2.43.0
Tested-by on v2 1/2
#2 lore raw

________________________________________
寄件者: [email protected] <[email protected]>
寄件日期: 2024年2月22日 下午 09:18
收件者: [email protected]
副本: [email protected]; [email protected]; [email protected]; [email protected]; Xiubo Li; Frank Hsiao 蕭法宣 主旨: [PATCH v2 1/2] ceph: skip copying the data extends the file EOF

From: Xiubo Li <[email protected]>

If hits the EOF it will revise the return value to the i_size instead of the real length read, but it will advance the offset of iovc, then for the next try it may be incorrectly skipped.

This will just skip advancing the iovc's offset more than i_size.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
 fs/ceph/file.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
fs/ceph/file.c +8 -10
index 71d29571712d..2b2b07a0a61b 100644
@@ -1195,7 +1195,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
                }
 
                idx = 0;
-               left = ret > 0 ? ret : 0;
+               left = ret > 0 ? umin(ret, i_size) : 0;
                while (left > 0) {
                        size_t plen, copied;
 
@@ -1224,15 +1224,13 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
        }
 
        if (ret > 0) {
-               if (off > *ki_pos) {
-                       if (off >= i_size) {
-                               *retry_op = CHECK_EOF;
-                               ret = i_size - *ki_pos;
-                               *ki_pos = i_size;
-                       } else {
-                               ret = off - *ki_pos;
-                               *ki_pos = off;
-                       }
+               if (off >= i_size) {
+                       *retry_op = CHECK_EOF;
+                       ret = i_size - *ki_pos;
+                       *ki_pos = i_size;
+               } else {
+                       ret = off - *ki_pos;
+                       *ki_pos = off;
                }
 
                if (last_objver)

--
2.43.0

maintainer
#3 lore raw
In reply to [email protected]
Quoted 21 lines · On Thu, Feb 22, 2024 at 2:21 PM <[email protected]> wrote:

From: Xiubo Li <[email protected]>

If hits the EOF it will revise the return value to the i_size instead of the real length read, but it will advance the offset of iovc, then for the next try it may be incorrectly skipped.

This will just skip advancing the iovc's offset more than i_size.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
 fs/ceph/file.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
fs/ceph/file.c +0 -0
index 71d29571712d..2b2b07a0a61b 100644

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1198

Quoted 5 lines
fs/ceph/file.c +1 -1
@@ -1195,7 +1195,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
                }
 
                idx = 0;
-               left = ret > 0 ? ret : 0;
+               left = ret > 0 ? umin(ret, i_size) : 0;

Hi Xiubo,

Can ret (i.e. the number of bytes actually read) be compared to i_size without taking the offset into account? How does this a handle a case where e.g.

    off = 20
    ret = 10
    i_size = 25

Did you intend the copy_page_to_iter() loop to go over 10 bytes and therefore advance the iovc ("to") by 10 instead of 5 bytes here?

Thanks,

Ilya

author
#4 lore raw
Quoted 32 lines · Ilya Dryomov
Quoted 20 lines · On Thu, Feb 22, 2024 at 2:21 PM <[email protected]> wrote:

From: Xiubo Li <[email protected]>

If hits the EOF it will revise the return value to the i_size instead of the real length read, but it will advance the offset of iovc, then for the next try it may be incorrectly skipped.

This will just skip advancing the iovc's offset more than i_size.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
  fs/ceph/file.c | 18 ++++++++----------
  1 file changed, 8 insertions(+), 10 deletions(-)
fs/ceph/file.c +0 -0
index 71d29571712d..2b2b07a0a61b 100644

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1198

Quoted 5 lines
fs/ceph/file.c +1 -1
@@ -1195,7 +1195,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
                 }
 
                 idx = 0;
-               left = ret > 0 ? ret : 0;
+               left = ret > 0 ? umin(ret, i_size) : 0;

Hi Xiubo,

Can ret (i.e. the number of bytes actually read) be compared to i_size without taking the offset into account? How does this a handle a case where e.g.

Quoted 6 lines
     off = 20
     ret = 10
     i_size = 25

Did you intend the copy_page_to_iter() loop to go over 10 bytes and therefore advance the iovc ("to") by 10 instead of 5 bytes here?

Good catch!

fs/ceph/file.c +0 -0
index 142deb242196..531874935c21 100644
@@ -1204,7 +1204,12 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,

                }

                idx = 0;
-               left = ret > 0 ? umin(ret, i_size) : 0; +               if (ret <= 0)
+                       left = 0;
+               else if (off + ret > i_size)
+                       left = i_size - off;
+               else
+                       left = ret;
                while (left > 0) {
                        size_t plen, copied;

Let me fix this in V3.

Thanks Ilya!

- Xiubo

Quoted 4 lines

Thanks,

Ilya

#5 lore raw

PATCH v2 2/2 ceph: set the correct mask for getattr reqeust for read

In reply to [email protected]

From: Xiubo Li <[email protected]>

In case of hitting the file EOF the ceph_read_iter() needs to retrieve the file size from MDS, and the Fr caps is not a neccessary.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
 fs/ceph/file.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ceph/file.c +5 -3
index 2b2b07a0a61b..08c918aa403e 100644
@@ -2181,14 +2181,16 @@ static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		int statret;
 		struct page *page = NULL;
 		loff_t i_size;
+		int mask = CEPH_STAT_CAP_SIZE;
 		if (retry_op == READ_INLINE) {
 			page = __page_cache_alloc(GFP_KERNEL);
 			if (!page)
 				return -ENOMEM;
 		}
 
-		statret = __ceph_do_getattr(inode, page,
-					    CEPH_STAT_CAP_INLINE_DATA, !!page);
+		if (retry_op == READ_INLINE)
+			mask = CEPH_STAT_CAP_INLINE_DATA;
+		statret = __ceph_do_getattr(inode, page, mask, !!page);
 		if (statret < 0) {
 			if (page)
 				__free_page(page);
@@ -2229,7 +2231,7 @@ static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		/* hit EOF or hole? */
 		if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
 		    ret < len) {
-			doutc(cl, "hit hole, ppos %lld < size %lld, reading more\n",
+			doutc(cl, "may hit hole, ppos %lld < size %lld, reading more\n",
 			      iocb->ki_pos, i_size);
 
 			read += ret;
Signature · 1 line
2.43.0
Tested-by on v2 2/2
#6 lore raw

回覆: [PATCH v2 2/2] ceph: set the correct mask for getattr reqeust for read

________________________________________
寄件者: [email protected] <[email protected]>
寄件日期: 2024年2月22日 下午 09:19
收件者: [email protected]
副本: [email protected]; [email protected]; [email protected]; [email protected]; Xiubo Li; Frank Hsiao 蕭法宣 主旨: [PATCH v2 2/2] ceph: set the correct mask for getattr reqeust for read

From: Xiubo Li <[email protected]>

In case of hitting the file EOF the ceph_read_iter() needs to retrieve the file size from MDS, and the Fr caps is not a neccessary.

URL: https://patchwork.kernel.org/project/ceph-devel/list/?series=819323

---
 fs/ceph/file.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ceph/file.c +5 -3
index 2b2b07a0a61b..08c918aa403e 100644
@@ -2181,14 +2181,16 @@ static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
                int statret;
                struct page *page = NULL;
                loff_t i_size;
+               int mask = CEPH_STAT_CAP_SIZE;
                if (retry_op == READ_INLINE) {
                        page = __page_cache_alloc(GFP_KERNEL);
                        if (!page)
                                return -ENOMEM;
                }
 
-               statret = __ceph_do_getattr(inode, page,
-                                           CEPH_STAT_CAP_INLINE_DATA, !!page);
+               if (retry_op == READ_INLINE)
+                       mask = CEPH_STAT_CAP_INLINE_DATA;
+               statret = __ceph_do_getattr(inode, page, mask, !!page);
                if (statret < 0) {
                        if (page)
                                __free_page(page);
@@ -2229,7 +2231,7 @@ static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
                /* hit EOF or hole? */
                if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
                    ret < len) {
-                       doutc(cl, "hit hole, ppos %lld < size %lld, reading more\n",
+                       doutc(cl, "may hit hole, ppos %lld < size %lld, reading more\n",
                              iocb->ki_pos, i_size);
 
                        read += ret;

--
2.43.0