9ae3a8
From db9d361c509aea4ce8aca7b7f16a1808880bad29 Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Wed, 20 Nov 2013 19:43:59 +0100
9ae3a8
Subject: [PATCH 16/25] block: vhdx - add log write support
9ae3a8
9ae3a8
RH-Author: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Message-id: <376bced13e2a7e1069a50bcbd09f3d822306865a.1384975172.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 55809
9ae3a8
O-Subject: [RHEL7 qemu-kvm PATCH 16/26] block: vhdx - add log write support
9ae3a8
Bugzilla: 879234
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
9ae3a8
This adds support for writing to the VHDX log.
9ae3a8
9ae3a8
For spec details, see VHDX Specification Format v1.00:
9ae3a8
https://www.microsoft.com/en-us/download/details.aspx?id=34750
9ae3a8
9ae3a8
There are a few limitations to this log support:
9ae3a8
1.) There is no caching yet
9ae3a8
2.) The log is flushed after each entry
9ae3a8
9ae3a8
The primary write interface, vhdx_log_write_and_flush(), performs a log
9ae3a8
write followed by an immediate flush of the log.
9ae3a8
9ae3a8
As each log entry sector is a minimum of 4KB, partial sector writes are
9ae3a8
filled in with data from the disk write destination.
9ae3a8
9ae3a8
If the current file log GUID is 0, a new GUID is generated and updated
9ae3a8
in the header.
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 8adc52336d9c44ab4c7b9358a7be22ac0ef962bf)
9ae3a8
---
9ae3a8
 block/vhdx-log.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 block/vhdx.h     |   3 +
9ae3a8
 2 files changed, 285 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/vhdx-log.c |  282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 block/vhdx.h     |    3 +
9ae3a8
 2 files changed, 285 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/vhdx-log.c b/block/vhdx-log.c
9ae3a8
index 0284729..ee5583c 100644
9ae3a8
--- a/block/vhdx-log.c
9ae3a8
+++ b/block/vhdx-log.c
9ae3a8
@@ -156,6 +156,55 @@ exit:
9ae3a8
     return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
+/* Writes num_sectors to the log (all log sectors are 4096 bytes),
9ae3a8
+ * from buffer 'buffer'.  Upon return, *sectors_written will contain
9ae3a8
+ * the number of sectors successfully written.
9ae3a8
+ *
9ae3a8
+ * It is assumed that 'buffer' is at least 4096*num_sectors large.
9ae3a8
+ *
9ae3a8
+ * 0 is returned on success, -errno otherwise */
9ae3a8
+static int vhdx_log_write_sectors(BlockDriverState *bs, VHDXLogEntries *log,
9ae3a8
+                                  uint32_t *sectors_written, void *buffer,
9ae3a8
+                                  uint32_t num_sectors)
9ae3a8
+{
9ae3a8
+    int ret = 0;
9ae3a8
+    uint64_t offset;
9ae3a8
+    uint32_t write;
9ae3a8
+    void *buffer_tmp;
9ae3a8
+    BDRVVHDXState *s = bs->opaque;
9ae3a8
+
9ae3a8
+    ret = vhdx_user_visible_write(bs, s);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    write = log->write;
9ae3a8
+
9ae3a8
+    buffer_tmp = buffer;
9ae3a8
+    while (num_sectors) {
9ae3a8
+
9ae3a8
+        offset = log->offset + write;
9ae3a8
+        write = vhdx_log_inc_idx(write, log->length);
9ae3a8
+        if (write == log->read) {
9ae3a8
+            /* full */
9ae3a8
+            break;
9ae3a8
+        }
9ae3a8
+        ret = bdrv_pwrite(bs->file, offset, buffer_tmp, VHDX_LOG_SECTOR_SIZE);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            goto exit;
9ae3a8
+        }
9ae3a8
+        buffer_tmp += VHDX_LOG_SECTOR_SIZE;
9ae3a8
+
9ae3a8
+        log->write = write;
9ae3a8
+        *sectors_written = *sectors_written + 1;
9ae3a8
+        num_sectors--;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+exit:
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
+
9ae3a8
+
9ae3a8
 /* Validates a log entry header */
9ae3a8
 static bool vhdx_log_hdr_is_valid(VHDXLogEntries *log, VHDXLogEntryHeader *hdr,
9ae3a8
                                   BDRVVHDXState *s)
9ae3a8
@@ -726,3 +775,236 @@ exit:
9ae3a8
 }
9ae3a8
 
9ae3a8
 
9ae3a8
+
9ae3a8
+static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor *desc,
9ae3a8
+                                      VHDXLogDataSector *sector, void *data,
9ae3a8
+                                      uint64_t seq)
9ae3a8
+{
9ae3a8
+    /* 8 + 4084 + 4 = 4096, 1 log sector */
9ae3a8
+    memcpy(&desc->leading_bytes, data, 8);
9ae3a8
+    data += 8;
9ae3a8
+    cpu_to_le64s(&desc->leading_bytes);
9ae3a8
+    memcpy(sector->data, data, 4084);
9ae3a8
+    data += 4084;
9ae3a8
+    memcpy(&desc->trailing_bytes, data, 4);
9ae3a8
+    cpu_to_le32s(&desc->trailing_bytes);
9ae3a8
+    data += 4;
9ae3a8
+
9ae3a8
+    sector->sequence_high  = (uint32_t) (seq >> 32);
9ae3a8
+    sector->sequence_low   = (uint32_t) (seq & 0xffffffff);
9ae3a8
+    sector->data_signature = VHDX_LOG_DATA_SIGNATURE;
9ae3a8
+
9ae3a8
+    vhdx_log_desc_le_export(desc);
9ae3a8
+    vhdx_log_data_le_export(sector);
9ae3a8
+}
9ae3a8
+
9ae3a8
+
9ae3a8
+static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
+                          void *data, uint32_t length, uint64_t offset)
9ae3a8
+{
9ae3a8
+    int ret = 0;
9ae3a8
+    void *buffer = NULL;
9ae3a8
+    void *merged_sector = NULL;
9ae3a8
+    void *data_tmp, *sector_write;
9ae3a8
+    unsigned int i;
9ae3a8
+    int sector_offset;
9ae3a8
+    uint32_t desc_sectors, sectors, total_length;
9ae3a8
+    uint32_t sectors_written = 0;
9ae3a8
+    uint32_t aligned_length;
9ae3a8
+    uint32_t leading_length = 0;
9ae3a8
+    uint32_t trailing_length = 0;
9ae3a8
+    uint32_t partial_sectors = 0;
9ae3a8
+    uint32_t bytes_written = 0;
9ae3a8
+    uint64_t file_offset;
9ae3a8
+    VHDXHeader *header;
9ae3a8
+    VHDXLogEntryHeader new_hdr;
9ae3a8
+    VHDXLogDescriptor *new_desc = NULL;
9ae3a8
+    VHDXLogDataSector *data_sector = NULL;
9ae3a8
+    MSGUID new_guid = { 0 };
9ae3a8
+
9ae3a8
+    header = s->headers[s->curr_header];
9ae3a8
+
9ae3a8
+    /* need to have offset read data, and be on 4096 byte boundary */
9ae3a8
+
9ae3a8
+    if (length > header->log_length) {
9ae3a8
+        /* no log present.  we could create a log here instead of failing */
9ae3a8
+        ret = -EINVAL;
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (guid_eq(header->log_guid, zero_guid)) {
9ae3a8
+        vhdx_guid_generate(&new_guid);
9ae3a8
+        vhdx_update_headers(bs, s, false, &new_guid);
9ae3a8
+    } else {
9ae3a8
+        /* currently, we require that the log be flushed after
9ae3a8
+         * every write. */
9ae3a8
+        ret = -ENOTSUP;
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* 0 is an invalid sequence number, but may also represent the first
9ae3a8
+     * log write (or a wrapped seq) */
9ae3a8
+    if (s->log.sequence == 0) {
9ae3a8
+        s->log.sequence = 1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    sector_offset = offset % VHDX_LOG_SECTOR_SIZE;
9ae3a8
+    file_offset = (offset / VHDX_LOG_SECTOR_SIZE) * VHDX_LOG_SECTOR_SIZE;
9ae3a8
+
9ae3a8
+    aligned_length = length;
9ae3a8
+
9ae3a8
+    /* add in the unaligned head and tail bytes */
9ae3a8
+    if (sector_offset) {
9ae3a8
+        leading_length = (VHDX_LOG_SECTOR_SIZE - sector_offset);
9ae3a8
+        leading_length = leading_length > length ? length : leading_length;
9ae3a8
+        aligned_length -= leading_length;
9ae3a8
+        partial_sectors++;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    sectors = aligned_length / VHDX_LOG_SECTOR_SIZE;
9ae3a8
+    trailing_length = aligned_length - (sectors * VHDX_LOG_SECTOR_SIZE);
9ae3a8
+    if (trailing_length) {
9ae3a8
+        partial_sectors++;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    sectors += partial_sectors;
9ae3a8
+
9ae3a8
+    /* sectors is now how many sectors the data itself takes, not
9ae3a8
+     * including the header and descriptor metadata */
9ae3a8
+
9ae3a8
+    new_hdr = (VHDXLogEntryHeader) {
9ae3a8
+                .signature           = VHDX_LOG_SIGNATURE,
9ae3a8
+                .tail                = s->log.tail,
9ae3a8
+                .sequence_number     = s->log.sequence,
9ae3a8
+                .descriptor_count    = sectors,
9ae3a8
+                .reserved            = 0,
9ae3a8
+                .flushed_file_offset = bdrv_getlength(bs->file),
9ae3a8
+                .last_file_offset    = bdrv_getlength(bs->file),
9ae3a8
+              };
9ae3a8
+
9ae3a8
+    new_hdr.log_guid = header->log_guid;
9ae3a8
+
9ae3a8
+    desc_sectors = vhdx_compute_desc_sectors(new_hdr.descriptor_count);
9ae3a8
+
9ae3a8
+    total_length = (desc_sectors + sectors) * VHDX_LOG_SECTOR_SIZE;
9ae3a8
+    new_hdr.entry_length = total_length;
9ae3a8
+
9ae3a8
+    vhdx_log_entry_hdr_le_export(&new_hdr);
9ae3a8
+
9ae3a8
+    buffer = qemu_blockalign(bs, total_length);
9ae3a8
+    memcpy(buffer, &new_hdr, sizeof(new_hdr));
9ae3a8
+
9ae3a8
+    new_desc = (VHDXLogDescriptor *) (buffer + sizeof(new_hdr));
9ae3a8
+    data_sector = buffer + (desc_sectors * VHDX_LOG_SECTOR_SIZE);
9ae3a8
+    data_tmp = data;
9ae3a8
+
9ae3a8
+    /* All log sectors are 4KB, so for any partial sectors we must
9ae3a8
+     * merge the data with preexisting data from the final file
9ae3a8
+     * destination */
9ae3a8
+    merged_sector = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE);
9ae3a8
+
9ae3a8
+    for (i = 0; i < sectors; i++) {
9ae3a8
+        new_desc->signature       = VHDX_LOG_DESC_SIGNATURE;
9ae3a8
+        new_desc->sequence_number = s->log.sequence;
9ae3a8
+        new_desc->file_offset     = file_offset;
9ae3a8
+
9ae3a8
+        if (i == 0 && leading_length) {
9ae3a8
+            /* partial sector at the front of the buffer */
9ae3a8
+            ret = bdrv_pread(bs->file, file_offset, merged_sector,
9ae3a8
+                             VHDX_LOG_SECTOR_SIZE);
9ae3a8
+            if (ret < 0) {
9ae3a8
+                goto exit;
9ae3a8
+            }
9ae3a8
+            memcpy(merged_sector + sector_offset, data_tmp, leading_length);
9ae3a8
+            bytes_written = leading_length;
9ae3a8
+            sector_write = merged_sector;
9ae3a8
+        } else if (i == sectors - 1 && trailing_length) {
9ae3a8
+            /* partial sector at the end of the buffer */
9ae3a8
+            ret = bdrv_pread(bs->file,
9ae3a8
+                            file_offset,
9ae3a8
+                            merged_sector + trailing_length,
9ae3a8
+                            VHDX_LOG_SECTOR_SIZE - trailing_length);
9ae3a8
+            if (ret < 0) {
9ae3a8
+                goto exit;
9ae3a8
+            }
9ae3a8
+            memcpy(merged_sector, data_tmp, trailing_length);
9ae3a8
+            bytes_written = trailing_length;
9ae3a8
+            sector_write = merged_sector;
9ae3a8
+        } else {
9ae3a8
+            bytes_written = VHDX_LOG_SECTOR_SIZE;
9ae3a8
+            sector_write = data_tmp;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        /* populate the raw sector data into the proper structures,
9ae3a8
+         * as well as update the descriptor, and convert to proper
9ae3a8
+         * endianness */
9ae3a8
+        vhdx_log_raw_to_le_sector(new_desc, data_sector, sector_write,
9ae3a8
+                                  s->log.sequence);
9ae3a8
+
9ae3a8
+        data_tmp += bytes_written;
9ae3a8
+        data_sector++;
9ae3a8
+        new_desc++;
9ae3a8
+        file_offset += VHDX_LOG_SECTOR_SIZE;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* checksum covers entire entry, from the log header through the
9ae3a8
+     * last data sector */
9ae3a8
+    vhdx_update_checksum(buffer, total_length,
9ae3a8
+                         offsetof(VHDXLogEntryHeader, checksum));
9ae3a8
+    cpu_to_le32s((uint32_t *)(buffer + 4));
9ae3a8
+
9ae3a8
+    /* now write to the log */
9ae3a8
+    vhdx_log_write_sectors(bs, &s->log, &sectors_written, buffer,
9ae3a8
+                           desc_sectors + sectors);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (sectors_written != desc_sectors + sectors) {
9ae3a8
+        /* instead of failing, we could flush the log here */
9ae3a8
+        ret = -EINVAL;
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    s->log.sequence++;
9ae3a8
+    /* write new tail */
9ae3a8
+    s->log.tail = s->log.write;
9ae3a8
+
9ae3a8
+exit:
9ae3a8
+    qemu_vfree(buffer);
9ae3a8
+    qemu_vfree(merged_sector);
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
+
9ae3a8
+/* Perform a log write, and then immediately flush the entire log */
9ae3a8
+int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
+                             void *data, uint32_t length, uint64_t offset)
9ae3a8
+{
9ae3a8
+    int ret = 0;
9ae3a8
+    VHDXLogSequence logs = { .valid = true,
9ae3a8
+                             .count = 1,
9ae3a8
+                             .hdr = { 0 } };
9ae3a8
+
9ae3a8
+
9ae3a8
+    /* Make sure data written (new and/or changed blocks) is stable
9ae3a8
+     * on disk, before creating log entry */
9ae3a8
+    bdrv_flush(bs);
9ae3a8
+    ret = vhdx_log_write(bs, s, data, length, offset);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+    logs.log = s->log;
9ae3a8
+
9ae3a8
+    /* Make sure log is stable on disk */
9ae3a8
+    bdrv_flush(bs);
9ae3a8
+    ret = vhdx_log_flush(bs, s, &logs);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    s->log = logs.log;
9ae3a8
+
9ae3a8
+exit:
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
+
9ae3a8
diff --git a/block/vhdx.h b/block/vhdx.h
9ae3a8
index 91ef8fe..4bb83de 100644
9ae3a8
--- a/block/vhdx.h
9ae3a8
+++ b/block/vhdx.h
9ae3a8
@@ -398,6 +398,9 @@ bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
9ae3a8
 
9ae3a8
 int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed);
9ae3a8
 
9ae3a8
+int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
+                             void *data, uint32_t length, uint64_t offset);
9ae3a8
+
9ae3a8
 static inline void leguid_to_cpus(MSGUID *guid)
9ae3a8
 {
9ae3a8
     le32_to_cpus(&guid->data1);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8