]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Add VIRTIO_BLK_T_FLUSH feature to handle flush operation from VM
authorPrasad Joshi <prasadjoshi124@gmail.com>
Fri, 13 May 2011 14:02:46 +0000 (15:02 +0100)
committerPekka Enberg <penberg@kernel.org>
Sat, 14 May 2011 11:53:36 +0000 (14:53 +0300)
The virtual machine calls 'sync' when the machine
is halted. Adding the virtio flush feature will
ensure that the data is synced on to disk before
the virtual machine is halted. This is needed to
ensure the intigrity of the data.

Signed-off-by: Prasad Joshi <prasadjoshi124@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/include/kvm/disk-image.h
tools/kvm/virtio/blk.c

index 9d7b572d47655e69e5bc14e242eacdd0ff8fd950..6cc94417ea2e117481e25f63500474f2fd8cb00d 100644 (file)
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 #include <stdbool.h>
 #include <sys/uio.h>
+#include <unistd.h>
 
 #define SECTOR_SHIFT           9
 #define SECTOR_SIZE            (1UL << SECTOR_SHIFT)
@@ -15,6 +16,7 @@ struct disk_image_operations {
        int (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len);
        ssize_t (*read_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
        ssize_t (*write_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+       int (*flush)(struct disk_image *disk);
        void (*close)(struct disk_image *disk);
 };
 
@@ -72,4 +74,11 @@ static inline ssize_t disk_image__write_sector_iov(struct disk_image *disk, u64
        return sector << SECTOR_SHIFT;
 }
 
+static inline int disk_image__flush(struct disk_image *disk)
+{
+       if (disk->ops->flush)
+               return disk->ops->flush(disk);
+       return fsync(disk->fd);
+}
+
 #endif /* KVM__DISK_IMAGE_H */
index 1c6b6083182e5a5d4a052e92013ca21b6f4cd0d4..6c9eb19a84eeeb46fbc1f14067c7ba6a673cf06c 100644 (file)
@@ -145,6 +145,9 @@ static bool virtio_blk_do_io_request(struct kvm *kvm,
        case VIRTIO_BLK_T_OUT:
                block_cnt       = disk_image__write_sector_iov(bdev->disk, req->sector, iov + 1, in + out - 2);
                break;
+       case VIRTIO_BLK_T_FLUSH:
+               block_cnt       = disk_image__flush(bdev->disk);
+               break;
        default:
                warning("request type %d", req->type);
                block_cnt       = -1;
@@ -304,7 +307,7 @@ void virtio_blk__init(struct kvm *kvm, struct disk_image *disk)
                 * guest kernel will compute disk geometry by own, the
                 * same applies to VIRTIO_BLK_F_BLK_SIZE
                 */
-               .host_features                  = (1UL << VIRTIO_BLK_F_SEG_MAX),
+               .host_features                  = (1UL << VIRTIO_BLK_F_SEG_MAX | 1UL << VIRTIO_BLK_F_FLUSH),
        };
 
        if (irq__register_device(PCI_DEVICE_ID_VIRTIO_BLK, &dev, &pin, &line) < 0)