902636
From 1b0edd3d0a2ee5c097bcf3501c1dfa937f02e473 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:21 +0100
902636
Subject: [PATCH 050/116] virtiofsd: add fuse_mbuf_iter API
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-47-dgilbert@redhat.com>
902636
Patchwork-id: 93502
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 046/112] virtiofsd: add fuse_mbuf_iter API
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: Stefan Hajnoczi <stefanha@redhat.com>
902636
902636
Introduce an API for consuming bytes from a buffer with size checks.
902636
All FUSE operations will be converted to use this safe API instead of
902636
void *inarg.
902636
902636
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit dad157e880416ab3a0e45beaa0e81977516568bc)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/buffer.c      | 28 +++++++++++++++++++++++++
902636
 tools/virtiofsd/fuse_common.h | 49 ++++++++++++++++++++++++++++++++++++++++++-
902636
 2 files changed, 76 insertions(+), 1 deletion(-)
902636
902636
diff --git a/tools/virtiofsd/buffer.c b/tools/virtiofsd/buffer.c
902636
index 772efa9..42a608f 100644
902636
--- a/tools/virtiofsd/buffer.c
902636
+++ b/tools/virtiofsd/buffer.c
902636
@@ -267,3 +267,31 @@ ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv)
902636
 
902636
     return copied;
902636
 }
902636
+
902636
+void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len)
902636
+{
902636
+    void *ptr;
902636
+
902636
+    if (len > iter->size - iter->pos) {
902636
+        return NULL;
902636
+    }
902636
+
902636
+    ptr = iter->mem + iter->pos;
902636
+    iter->pos += len;
902636
+    return ptr;
902636
+}
902636
+
902636
+const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter)
902636
+{
902636
+    const char *str = iter->mem + iter->pos;
902636
+    size_t remaining = iter->size - iter->pos;
902636
+    size_t i;
902636
+
902636
+    for (i = 0; i < remaining; i++) {
902636
+        if (str[i] == '\0') {
902636
+            iter->pos += i + 1;
902636
+            return str;
902636
+        }
902636
+    }
902636
+    return NULL;
902636
+}
902636
diff --git a/tools/virtiofsd/fuse_common.h b/tools/virtiofsd/fuse_common.h
902636
index 0cb33ac..f8f6433 100644
902636
--- a/tools/virtiofsd/fuse_common.h
902636
+++ b/tools/virtiofsd/fuse_common.h
902636
@@ -703,10 +703,57 @@ size_t fuse_buf_size(const struct fuse_bufvec *bufv);
902636
  */
902636
 ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src);
902636
 
902636
+/**
902636
+ * Memory buffer iterator
902636
+ *
902636
+ */
902636
+struct fuse_mbuf_iter {
902636
+    /**
902636
+     * Data pointer
902636
+     */
902636
+    void *mem;
902636
+
902636
+    /**
902636
+     * Total length, in bytes
902636
+     */
902636
+    size_t size;
902636
+
902636
+    /**
902636
+     * Offset from start of buffer
902636
+     */
902636
+    size_t pos;
902636
+};
902636
+
902636
+/* Initialize memory buffer iterator from a fuse_buf */
902636
+#define FUSE_MBUF_ITER_INIT(fbuf) \
902636
+    ((struct fuse_mbuf_iter){     \
902636
+        .mem = fbuf->mem,         \
902636
+        .size = fbuf->size,       \
902636
+        .pos = 0,                 \
902636
+    })
902636
+
902636
+/**
902636
+ * Consume bytes from a memory buffer iterator
902636
+ *
902636
+ * @param iter memory buffer iterator
902636
+ * @param len number of bytes to consume
902636
+ * @return pointer to start of consumed bytes or
902636
+ *         NULL if advancing beyond end of buffer
902636
+ */
902636
+void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len);
902636
+
902636
+/**
902636
+ * Consume a NUL-terminated string from a memory buffer iterator
902636
+ *
902636
+ * @param iter memory buffer iterator
902636
+ * @return pointer to the string or
902636
+ *         NULL if advancing beyond end of buffer or there is no NUL-terminator
902636
+ */
902636
+const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter);
902636
+
902636
 /*
902636
  * Signal handling
902636
  */
902636
-
902636
 /**
902636
  * Exit session on HUP, TERM and INT signals and ignore PIPE signal
902636
  *
902636
-- 
902636
1.8.3.1
902636