yeahuh / rpms / qemu-kvm

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