Re: [PATCH v2 2/3] block/export/fuse: Extract fuse_fallocate_zero_range()

2022-01-25 Thread Thomas Huth

On 24/01/2022 23.03, Philippe Mathieu-Daudé via wrote:

Signed-off-by: Philippe Mathieu-Daudé 
---
  block/export/fuse.c | 48 +
  1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/block/export/fuse.c b/block/export/fuse.c
index 31cb0503adc..3a158342c75 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -603,6 +603,35 @@ static void fuse_write(fuse_req_t req, fuse_ino_t inode, 
const char *buf,
  }
  }
  
+static bool fuse_fallocate_punch_hole(fuse_req_t req, fuse_ino_t inode,

+  int mode, int64_t blk_len,
+  off_t offset, off_t *length)
+{
+FuseExport *exp = fuse_req_userdata(req);
+
+if (mode & FALLOC_FL_KEEP_SIZE) {
+*length = MIN(*length, blk_len - offset);
+}
+
+if (mode & FALLOC_FL_PUNCH_HOLE) {
+int ret;
+
+if (!(mode & FALLOC_FL_KEEP_SIZE)) {
+fuse_reply_err(req, EINVAL);
+return false;
+}
+
+do {
+int size = MIN(*length, BDRV_REQUEST_MAX_BYTES);
+
+ret = blk_pdiscard(exp->common.blk, offset, size);
+offset += size;
+*length -= size;
+} while (ret == 0 && *length > 0);
+}
+return true;
+}
+
  static bool fuse_fallocate_zero_range(fuse_req_t req, fuse_ino_t inode,
int mode, int64_t blk_len,
off_t offset, off_t *length)
@@ -657,23 +686,8 @@ static void fuse_fallocate(fuse_req_t req, fuse_ino_t 
inode, int mode,
  return;
  }
  
-if (mode & FALLOC_FL_KEEP_SIZE) {

-length = MIN(length, blk_len - offset);
-}
-
-if (mode & FALLOC_FL_PUNCH_HOLE) {
-if (!(mode & FALLOC_FL_KEEP_SIZE)) {
-fuse_reply_err(req, EINVAL);
-return;
-}
-
-do {
-int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
-
-ret = blk_pdiscard(exp->common.blk, offset, size);
-offset += size;
-length -= size;
-} while (ret == 0 && length > 0);
+if (!fuse_fallocate_punch_hole(req, inode, mode, blk_len, offset, 
)) {
+return;


Same issue as with the previous patch? If the do-while loop has been 
executed, the else branches below should not be called...


 Thomas


  } else if (!fuse_fallocate_zero_range(req, inode, blk_len, mode, offset, 
)) {
  return;
  } else if (!mode) {





[PATCH v2 2/3] block/export/fuse: Extract fuse_fallocate_zero_range()

2022-01-24 Thread Philippe Mathieu-Daudé via
Signed-off-by: Philippe Mathieu-Daudé 
---
 block/export/fuse.c | 48 +
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/block/export/fuse.c b/block/export/fuse.c
index 31cb0503adc..3a158342c75 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -603,6 +603,35 @@ static void fuse_write(fuse_req_t req, fuse_ino_t inode, 
const char *buf,
 }
 }
 
+static bool fuse_fallocate_punch_hole(fuse_req_t req, fuse_ino_t inode,
+  int mode, int64_t blk_len,
+  off_t offset, off_t *length)
+{
+FuseExport *exp = fuse_req_userdata(req);
+
+if (mode & FALLOC_FL_KEEP_SIZE) {
+*length = MIN(*length, blk_len - offset);
+}
+
+if (mode & FALLOC_FL_PUNCH_HOLE) {
+int ret;
+
+if (!(mode & FALLOC_FL_KEEP_SIZE)) {
+fuse_reply_err(req, EINVAL);
+return false;
+}
+
+do {
+int size = MIN(*length, BDRV_REQUEST_MAX_BYTES);
+
+ret = blk_pdiscard(exp->common.blk, offset, size);
+offset += size;
+*length -= size;
+} while (ret == 0 && *length > 0);
+}
+return true;
+}
+
 static bool fuse_fallocate_zero_range(fuse_req_t req, fuse_ino_t inode,
   int mode, int64_t blk_len,
   off_t offset, off_t *length)
@@ -657,23 +686,8 @@ static void fuse_fallocate(fuse_req_t req, fuse_ino_t 
inode, int mode,
 return;
 }
 
-if (mode & FALLOC_FL_KEEP_SIZE) {
-length = MIN(length, blk_len - offset);
-}
-
-if (mode & FALLOC_FL_PUNCH_HOLE) {
-if (!(mode & FALLOC_FL_KEEP_SIZE)) {
-fuse_reply_err(req, EINVAL);
-return;
-}
-
-do {
-int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
-
-ret = blk_pdiscard(exp->common.blk, offset, size);
-offset += size;
-length -= size;
-} while (ret == 0 && length > 0);
+if (!fuse_fallocate_punch_hole(req, inode, mode, blk_len, offset, 
)) {
+return;
 } else if (!fuse_fallocate_zero_range(req, inode, blk_len, mode, offset, 
)) {
 return;
 } else if (!mode) {
-- 
2.34.1