958e1b
From 33af2fc0d1fd87e26c9ed15a3ce07a61cbbaa490 Mon Sep 17 00:00:00 2001
958e1b
From: Laszlo Ersek <lersek@redhat.com>
958e1b
Date: Fri, 7 Nov 2014 17:18:00 +0100
958e1b
Subject: [PATCH 13/41] dump: add APIs to operate DataCache
958e1b
958e1b
Message-id: <1415380693-16593-14-git-send-email-lersek@redhat.com>
958e1b
Patchwork-id: 62199
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH 13/26] dump: add APIs to operate DataCache
958e1b
Bugzilla: 1157798
958e1b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
958e1b
RH-Acked-by: dgibson <dgibson@redhat.com>
958e1b
958e1b
From: qiaonuohan <qiaonuohan@cn.fujitsu.com>
958e1b
958e1b
DataCache is used to store data temporarily, then the data will be written to
958e1b
vmcore. These functions will be called later when writing data of page to
958e1b
vmcore.
958e1b
958e1b
Signed-off-by: Qiao Nuohan <qiaonuohan@cn.fujitsu.com>
958e1b
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
958e1b
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
958e1b
(cherry picked from commit 64cfba6a47411092c941c8d17256fb5673cc8cbf)
958e1b
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 dump.c                | 47 +++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 include/sysemu/dump.h |  9 +++++++++
958e1b
 2 files changed, 56 insertions(+)
958e1b
958e1b
diff --git a/dump.c b/dump.c
958e1b
index f416093..926ab84 100644
958e1b
--- a/dump.c
958e1b
+++ b/dump.c
958e1b
@@ -1171,6 +1171,53 @@ out:
958e1b
     return ret;
958e1b
 }
958e1b
 
958e1b
+static void prepare_data_cache(DataCache *data_cache, DumpState *s,
958e1b
+                               off_t offset)
958e1b
+{
958e1b
+    data_cache->fd = s->fd;
958e1b
+    data_cache->data_size = 0;
958e1b
+    data_cache->buf_size = BUFSIZE_DATA_CACHE;
958e1b
+    data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE);
958e1b
+    data_cache->offset = offset;
958e1b
+}
958e1b
+
958e1b
+static int write_cache(DataCache *dc, const void *buf, size_t size,
958e1b
+                       bool flag_sync)
958e1b
+{
958e1b
+    /*
958e1b
+     * dc->buf_size should not be less than size, otherwise dc will never be
958e1b
+     * enough
958e1b
+     */
958e1b
+    assert(size <= dc->buf_size);
958e1b
+
958e1b
+    /*
958e1b
+     * if flag_sync is set, synchronize data in dc->buf into vmcore.
958e1b
+     * otherwise check if the space is enough for caching data in buf, if not,
958e1b
+     * write the data in dc->buf to dc->fd and reset dc->buf
958e1b
+     */
958e1b
+    if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
958e1b
+        (flag_sync && dc->data_size > 0)) {
958e1b
+        if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
958e1b
+            return -1;
958e1b
+        }
958e1b
+
958e1b
+        dc->offset += dc->data_size;
958e1b
+        dc->data_size = 0;
958e1b
+    }
958e1b
+
958e1b
+    if (!flag_sync) {
958e1b
+        memcpy(dc->buf + dc->data_size, buf, size);
958e1b
+        dc->data_size += size;
958e1b
+    }
958e1b
+
958e1b
+    return 0;
958e1b
+}
958e1b
+
958e1b
+static void free_data_cache(DataCache *data_cache)
958e1b
+{
958e1b
+    g_free(data_cache->buf);
958e1b
+}
958e1b
+
958e1b
 static ram_addr_t get_start_block(DumpState *s)
958e1b
 {
958e1b
     GuestPhysBlock *block;
958e1b
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
958e1b
index 6d4d0bc..92a95e4 100644
958e1b
--- a/include/sysemu/dump.h
958e1b
+++ b/include/sysemu/dump.h
958e1b
@@ -41,6 +41,7 @@
958e1b
 #define DISKDUMP_HEADER_BLOCKS      (1)
958e1b
 #define BUFSIZE_BITMAP              (TARGET_PAGE_SIZE)
958e1b
 #define PFN_BUFBITMAP               (CHAR_BIT * BUFSIZE_BITMAP)
958e1b
+#define BUFSIZE_DATA_CACHE          (TARGET_PAGE_SIZE * 4)
958e1b
 
958e1b
 typedef struct ArchDumpInfo {
958e1b
     int d_machine;  /* Architecture */
958e1b
@@ -142,6 +143,14 @@ typedef struct QEMU_PACKED KdumpSubHeader64 {
958e1b
     uint64_t max_mapnr_64;          /* header_version 6 and later */
958e1b
 } KdumpSubHeader64;
958e1b
 
958e1b
+typedef struct DataCache {
958e1b
+    int fd;             /* fd of the file where to write the cached data */
958e1b
+    uint8_t *buf;       /* buffer for cached data */
958e1b
+    size_t buf_size;    /* size of the buf */
958e1b
+    size_t data_size;   /* size of cached data in buf */
958e1b
+    off_t offset;       /* offset of the file */
958e1b
+} DataCache;
958e1b
+
958e1b
 struct GuestPhysBlockList; /* memory_mapping.h */
958e1b
 int cpu_get_dump_info(ArchDumpInfo *info,
958e1b
                       const struct GuestPhysBlockList *guest_phys_blocks);
958e1b
-- 
958e1b
1.8.3.1
958e1b