2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version
|
|
|
|
* 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-13 20:44:23 +04:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2013-02-22 07:39:51 +04:00
|
|
|
#include "qemu/uri.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/sockets.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/bitops.h"
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_PROTO_VER 0x01
|
|
|
|
|
|
|
|
#define SD_DEFAULT_ADDR "localhost"
|
2013-02-22 07:39:52 +04:00
|
|
|
#define SD_DEFAULT_PORT 7000
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_OP_CREATE_AND_WRITE_OBJ 0x01
|
|
|
|
#define SD_OP_READ_OBJ 0x02
|
|
|
|
#define SD_OP_WRITE_OBJ 0x03
|
2013-04-23 10:03:33 +04:00
|
|
|
/* 0x04 is used internally by Sheepdog */
|
|
|
|
#define SD_OP_DISCARD_OBJ 0x05
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_OP_NEW_VDI 0x11
|
|
|
|
#define SD_OP_LOCK_VDI 0x12
|
|
|
|
#define SD_OP_RELEASE_VDI 0x13
|
|
|
|
#define SD_OP_GET_VDI_INFO 0x14
|
|
|
|
#define SD_OP_READ_VDIS 0x15
|
2012-04-04 00:03:58 +04:00
|
|
|
#define SD_OP_FLUSH_VDI 0x16
|
2013-04-25 16:49:39 +04:00
|
|
|
#define SD_OP_DEL_VDI 0x17
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_FLAG_CMD_WRITE 0x01
|
|
|
|
#define SD_FLAG_CMD_COW 0x02
|
2013-01-10 12:03:47 +04:00
|
|
|
#define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
|
|
|
|
#define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_RES_SUCCESS 0x00 /* Success */
|
|
|
|
#define SD_RES_UNKNOWN 0x01 /* Unknown error */
|
|
|
|
#define SD_RES_NO_OBJ 0x02 /* No object found */
|
|
|
|
#define SD_RES_EIO 0x03 /* I/O error */
|
|
|
|
#define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
|
|
|
|
#define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
|
|
|
|
#define SD_RES_SYSTEM_ERROR 0x06 /* System error */
|
|
|
|
#define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
|
|
|
|
#define SD_RES_NO_VDI 0x08 /* No vdi found */
|
|
|
|
#define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
|
|
|
|
#define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
|
|
|
|
#define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
|
|
|
|
#define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
|
|
|
|
#define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
|
|
|
|
#define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
|
|
|
|
#define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
|
|
|
|
#define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
|
|
|
|
#define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
|
|
|
|
#define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
|
|
|
|
#define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
|
|
|
|
#define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
|
|
|
|
#define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
|
|
|
|
#define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
|
|
|
|
#define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
|
|
|
|
#define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
|
2013-03-18 10:27:55 +04:00
|
|
|
#define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
|
2013-04-25 20:19:52 +04:00
|
|
|
#define SD_RES_READONLY 0x1A /* Object is read-only */
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Object ID rules
|
|
|
|
*
|
|
|
|
* 0 - 19 (20 bits): data object space
|
|
|
|
* 20 - 31 (12 bits): reserved data object space
|
|
|
|
* 32 - 55 (24 bits): vdi object space
|
|
|
|
* 56 - 59 ( 4 bits): reserved vdi object space
|
2011-10-14 11:41:06 +04:00
|
|
|
* 60 - 63 ( 4 bits): object type identifier space
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define VDI_SPACE_SHIFT 32
|
|
|
|
#define VDI_BIT (UINT64_C(1) << 63)
|
|
|
|
#define VMSTATE_BIT (UINT64_C(1) << 62)
|
|
|
|
#define MAX_DATA_OBJS (UINT64_C(1) << 20)
|
|
|
|
#define MAX_CHILDREN 1024
|
|
|
|
#define SD_MAX_VDI_LEN 256
|
|
|
|
#define SD_MAX_VDI_TAG_LEN 256
|
|
|
|
#define SD_NR_VDIS (1U << 24)
|
|
|
|
#define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
|
|
|
|
#define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
|
2013-11-07 18:56:38 +04:00
|
|
|
/*
|
|
|
|
* For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
|
|
|
|
* (SD_EC_MAX_STRIP - 1) for parity strips
|
|
|
|
*
|
|
|
|
* SD_MAX_COPIES is sum of number of data strips and parity strips.
|
|
|
|
*/
|
|
|
|
#define SD_EC_MAX_STRIP 16
|
|
|
|
#define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
#define SD_INODE_SIZE (sizeof(SheepdogInode))
|
|
|
|
#define CURRENT_VDI_ID 0
|
|
|
|
|
|
|
|
typedef struct SheepdogReq {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint32_t opcode_specific[8];
|
|
|
|
} SheepdogReq;
|
|
|
|
|
|
|
|
typedef struct SheepdogRsp {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint32_t result;
|
|
|
|
uint32_t opcode_specific[7];
|
|
|
|
} SheepdogRsp;
|
|
|
|
|
|
|
|
typedef struct SheepdogObjReq {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint64_t oid;
|
|
|
|
uint64_t cow_oid;
|
2013-10-23 12:51:51 +04:00
|
|
|
uint8_t copies;
|
2013-10-23 12:51:52 +04:00
|
|
|
uint8_t copy_policy;
|
|
|
|
uint8_t reserved[6];
|
2010-06-21 00:01:00 +04:00
|
|
|
uint64_t offset;
|
|
|
|
} SheepdogObjReq;
|
|
|
|
|
|
|
|
typedef struct SheepdogObjRsp {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint32_t result;
|
2013-10-23 12:51:51 +04:00
|
|
|
uint8_t copies;
|
2013-10-23 12:51:52 +04:00
|
|
|
uint8_t copy_policy;
|
|
|
|
uint8_t reserved[2];
|
2010-06-21 00:01:00 +04:00
|
|
|
uint32_t pad[6];
|
|
|
|
} SheepdogObjRsp;
|
|
|
|
|
|
|
|
typedef struct SheepdogVdiReq {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint64_t vdi_size;
|
2013-01-29 13:14:16 +04:00
|
|
|
uint32_t vdi_id;
|
2013-10-23 12:51:51 +04:00
|
|
|
uint8_t copies;
|
2013-10-23 12:51:52 +04:00
|
|
|
uint8_t copy_policy;
|
|
|
|
uint8_t reserved[2];
|
2010-06-21 00:01:00 +04:00
|
|
|
uint32_t snapid;
|
|
|
|
uint32_t pad[3];
|
|
|
|
} SheepdogVdiReq;
|
|
|
|
|
|
|
|
typedef struct SheepdogVdiRsp {
|
|
|
|
uint8_t proto_ver;
|
|
|
|
uint8_t opcode;
|
|
|
|
uint16_t flags;
|
|
|
|
uint32_t epoch;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint32_t result;
|
|
|
|
uint32_t rsvd;
|
|
|
|
uint32_t vdi_id;
|
|
|
|
uint32_t pad[5];
|
|
|
|
} SheepdogVdiRsp;
|
|
|
|
|
|
|
|
typedef struct SheepdogInode {
|
|
|
|
char name[SD_MAX_VDI_LEN];
|
|
|
|
char tag[SD_MAX_VDI_TAG_LEN];
|
|
|
|
uint64_t ctime;
|
|
|
|
uint64_t snap_ctime;
|
|
|
|
uint64_t vm_clock_nsec;
|
|
|
|
uint64_t vdi_size;
|
|
|
|
uint64_t vm_state_size;
|
|
|
|
uint16_t copy_policy;
|
|
|
|
uint8_t nr_copies;
|
|
|
|
uint8_t block_size_shift;
|
|
|
|
uint32_t snap_id;
|
|
|
|
uint32_t vdi_id;
|
|
|
|
uint32_t parent_vdi_id;
|
|
|
|
uint32_t child_vdi_id[MAX_CHILDREN];
|
|
|
|
uint32_t data_vdi_id[MAX_DATA_OBJS];
|
|
|
|
} SheepdogInode;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 64 bit FNV-1a non-zero initial basis
|
|
|
|
*/
|
|
|
|
#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 64 bit Fowler/Noll/Vo FNV-1a hash code
|
|
|
|
*/
|
|
|
|
static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
|
|
|
|
{
|
|
|
|
unsigned char *bp = buf;
|
|
|
|
unsigned char *be = bp + len;
|
|
|
|
while (bp < be) {
|
|
|
|
hval ^= (uint64_t) *bp++;
|
|
|
|
hval += (hval << 1) + (hval << 4) + (hval << 5) +
|
|
|
|
(hval << 7) + (hval << 8) + (hval << 40);
|
|
|
|
}
|
|
|
|
return hval;
|
|
|
|
}
|
|
|
|
|
2012-10-06 20:57:14 +04:00
|
|
|
static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
return inode->vdi_id == inode->data_vdi_id[idx];
|
|
|
|
}
|
|
|
|
|
2012-10-06 20:57:14 +04:00
|
|
|
static inline bool is_data_obj(uint64_t oid)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
return !(VDI_BIT & oid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t data_oid_to_idx(uint64_t oid)
|
|
|
|
{
|
|
|
|
return oid & (MAX_DATA_OBJS - 1);
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:13 +04:00
|
|
|
static inline uint32_t oid_to_vid(uint64_t oid)
|
|
|
|
{
|
|
|
|
return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
static inline uint64_t vid_to_vdi_oid(uint32_t vid)
|
|
|
|
{
|
|
|
|
return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
|
|
|
|
{
|
|
|
|
return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
|
|
|
|
{
|
|
|
|
return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
|
|
|
|
}
|
|
|
|
|
2012-10-06 20:57:14 +04:00
|
|
|
static inline bool is_snapshot(struct SheepdogInode *inode)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
return !!inode->snap_ctime;
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
#undef DPRINTF
|
2010-06-21 00:01:00 +04:00
|
|
|
#ifdef DEBUG_SDOG
|
2013-07-29 16:16:36 +04:00
|
|
|
#define DPRINTF(fmt, args...) \
|
2010-06-21 00:01:00 +04:00
|
|
|
do { \
|
|
|
|
fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
|
|
|
|
} while (0)
|
|
|
|
#else
|
2013-07-29 16:16:36 +04:00
|
|
|
#define DPRINTF(fmt, args...)
|
2010-06-21 00:01:00 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct SheepdogAIOCB SheepdogAIOCB;
|
|
|
|
|
|
|
|
typedef struct AIOReq {
|
|
|
|
SheepdogAIOCB *aiocb;
|
|
|
|
unsigned int iov_offset;
|
|
|
|
|
|
|
|
uint64_t oid;
|
|
|
|
uint64_t base_oid;
|
|
|
|
uint64_t offset;
|
|
|
|
unsigned int data_len;
|
|
|
|
uint8_t flags;
|
|
|
|
uint32_t id;
|
|
|
|
|
2012-06-27 02:26:22 +04:00
|
|
|
QLIST_ENTRY(AIOReq) aio_siblings;
|
2010-06-21 00:01:00 +04:00
|
|
|
} AIOReq;
|
|
|
|
|
|
|
|
enum AIOCBState {
|
|
|
|
AIOCB_WRITE_UDATA,
|
|
|
|
AIOCB_READ_UDATA,
|
2013-01-15 12:28:55 +04:00
|
|
|
AIOCB_FLUSH_CACHE,
|
2013-04-23 10:03:33 +04:00
|
|
|
AIOCB_DISCARD_OBJ,
|
2010-06-21 00:01:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SheepdogAIOCB {
|
|
|
|
BlockDriverAIOCB common;
|
|
|
|
|
|
|
|
QEMUIOVector *qiov;
|
|
|
|
|
|
|
|
int64_t sector_num;
|
|
|
|
int nb_sectors;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
enum AIOCBState aiocb_type;
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
Coroutine *coroutine;
|
2010-06-21 00:01:00 +04:00
|
|
|
void (*aio_done_func)(SheepdogAIOCB *);
|
|
|
|
|
2013-10-24 11:01:17 +04:00
|
|
|
bool cancelable;
|
|
|
|
bool *finished;
|
2012-06-27 02:26:21 +04:00
|
|
|
int nr_pending;
|
2010-06-21 00:01:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct BDRVSheepdogState {
|
2013-10-24 11:01:15 +04:00
|
|
|
BlockDriverState *bs;
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
SheepdogInode inode;
|
|
|
|
|
|
|
|
uint32_t min_dirty_data_idx;
|
|
|
|
uint32_t max_dirty_data_idx;
|
|
|
|
|
|
|
|
char name[SD_MAX_VDI_LEN];
|
2012-10-06 20:57:14 +04:00
|
|
|
bool is_snapshot;
|
2013-01-10 12:03:47 +04:00
|
|
|
uint32_t cache_flags;
|
2013-04-23 10:03:33 +04:00
|
|
|
bool discard_supported;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
char *host_spec;
|
2013-02-22 07:39:53 +04:00
|
|
|
bool is_unix;
|
2010-06-21 00:01:00 +04:00
|
|
|
int fd;
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
CoMutex lock;
|
|
|
|
Coroutine *co_send;
|
|
|
|
Coroutine *co_recv;
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
uint32_t aioreq_seq_num;
|
2013-10-24 11:01:15 +04:00
|
|
|
|
|
|
|
/* Every aio request must be linked to either of these queues. */
|
2012-06-27 02:26:22 +04:00
|
|
|
QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
|
|
|
|
QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head;
|
2013-10-24 11:01:15 +04:00
|
|
|
QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head;
|
2010-06-21 00:01:00 +04:00
|
|
|
} BDRVSheepdogState;
|
|
|
|
|
|
|
|
static const char * sd_strerror(int err)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
int err;
|
|
|
|
const char *desc;
|
|
|
|
} errors[] = {
|
|
|
|
{SD_RES_SUCCESS, "Success"},
|
|
|
|
{SD_RES_UNKNOWN, "Unknown error"},
|
|
|
|
{SD_RES_NO_OBJ, "No object found"},
|
|
|
|
{SD_RES_EIO, "I/O error"},
|
|
|
|
{SD_RES_VDI_EXIST, "VDI exists already"},
|
|
|
|
{SD_RES_INVALID_PARMS, "Invalid parameters"},
|
|
|
|
{SD_RES_SYSTEM_ERROR, "System error"},
|
|
|
|
{SD_RES_VDI_LOCKED, "VDI is already locked"},
|
|
|
|
{SD_RES_NO_VDI, "No vdi found"},
|
|
|
|
{SD_RES_NO_BASE_VDI, "No base VDI found"},
|
|
|
|
{SD_RES_VDI_READ, "Failed read the requested VDI"},
|
|
|
|
{SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
|
|
|
|
{SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
|
|
|
|
{SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
|
|
|
|
{SD_RES_NO_TAG, "Failed to find the requested tag"},
|
|
|
|
{SD_RES_STARTUP, "The system is still booting"},
|
|
|
|
{SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
|
|
|
|
{SD_RES_SHUTDOWN, "The system is shutting down"},
|
|
|
|
{SD_RES_NO_MEM, "Out of memory on the server"},
|
|
|
|
{SD_RES_FULL_VDI, "We already have the maximum vdis"},
|
|
|
|
{SD_RES_VER_MISMATCH, "Protocol version mismatch"},
|
|
|
|
{SD_RES_NO_SPACE, "Server has no space for new objects"},
|
|
|
|
{SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
|
|
|
|
{SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
|
|
|
|
{SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
|
2013-03-18 10:27:55 +04:00
|
|
|
{SD_RES_HALT, "Sheepdog is stopped serving IO request"},
|
2013-04-25 20:19:52 +04:00
|
|
|
{SD_RES_READONLY, "Object is read-only"},
|
2010-06-21 00:01:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(errors); ++i) {
|
|
|
|
if (errors[i].err == err) {
|
|
|
|
return errors[i].desc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Invalid error code";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sheepdog I/O handling:
|
|
|
|
*
|
2011-08-12 16:33:15 +04:00
|
|
|
* 1. In sd_co_rw_vector, we send the I/O requests to the server and
|
2012-06-27 02:26:22 +04:00
|
|
|
* link the requests to the inflight_list in the
|
2011-08-12 16:33:15 +04:00
|
|
|
* BDRVSheepdogState. The function exits without waiting for
|
|
|
|
* receiving the response.
|
2010-06-21 00:01:00 +04:00
|
|
|
*
|
2011-08-12 16:33:15 +04:00
|
|
|
* 2. We receive the response in aio_read_response, the fd handler to
|
2010-06-21 00:01:00 +04:00
|
|
|
* the sheepdog connection. If metadata update is needed, we send
|
|
|
|
* the write request to the vdi object in sd_write_done, the write
|
2011-08-12 16:33:15 +04:00
|
|
|
* completion function. We switch back to sd_co_readv/writev after
|
|
|
|
* all the requests belonging to the AIOCB are finished.
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
|
|
|
|
uint64_t oid, unsigned int data_len,
|
|
|
|
uint64_t offset, uint8_t flags,
|
|
|
|
uint64_t base_oid, unsigned int iov_offset)
|
|
|
|
{
|
|
|
|
AIOReq *aio_req;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
aio_req = g_malloc(sizeof(*aio_req));
|
2010-06-21 00:01:00 +04:00
|
|
|
aio_req->aiocb = acb;
|
|
|
|
aio_req->iov_offset = iov_offset;
|
|
|
|
aio_req->oid = oid;
|
|
|
|
aio_req->base_oid = base_oid;
|
|
|
|
aio_req->offset = offset;
|
|
|
|
aio_req->data_len = data_len;
|
|
|
|
aio_req->flags = flags;
|
|
|
|
aio_req->id = s->aioreq_seq_num++;
|
|
|
|
|
2012-06-27 02:26:21 +04:00
|
|
|
acb->nr_pending++;
|
2010-06-21 00:01:00 +04:00
|
|
|
return aio_req;
|
|
|
|
}
|
|
|
|
|
2012-06-27 02:26:21 +04:00
|
|
|
static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb = aio_req->aiocb;
|
2012-06-27 02:26:21 +04:00
|
|
|
|
2013-10-24 11:01:17 +04:00
|
|
|
acb->cancelable = false;
|
2012-06-27 02:26:22 +04:00
|
|
|
QLIST_REMOVE(aio_req, aio_siblings);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(aio_req);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2012-06-27 02:26:21 +04:00
|
|
|
acb->nr_pending--;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2011-10-05 11:17:31 +04:00
|
|
|
static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2013-10-24 11:01:17 +04:00
|
|
|
qemu_coroutine_enter(acb->coroutine, NULL);
|
|
|
|
if (acb->finished) {
|
|
|
|
*acb->finished = true;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
qemu_aio_release(acb);
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:17 +04:00
|
|
|
/*
|
|
|
|
* Check whether the specified acb can be canceled
|
|
|
|
*
|
|
|
|
* We can cancel aio when any request belonging to the acb is:
|
|
|
|
* - Not processed by the sheepdog server.
|
|
|
|
* - Not linked to the inflight queue.
|
|
|
|
*/
|
|
|
|
static bool sd_acb_cancelable(const SheepdogAIOCB *acb)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = acb->common.bs->opaque;
|
|
|
|
AIOReq *aioreq;
|
|
|
|
|
|
|
|
if (!acb->cancelable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) {
|
|
|
|
if (aioreq->aiocb == acb) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
static void sd_aio_cancel(BlockDriverAIOCB *blockacb)
|
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb;
|
2013-10-24 11:01:17 +04:00
|
|
|
BDRVSheepdogState *s = acb->common.bs->opaque;
|
|
|
|
AIOReq *aioreq, *next;
|
|
|
|
bool finished = false;
|
|
|
|
|
|
|
|
acb->finished = &finished;
|
|
|
|
while (!finished) {
|
|
|
|
if (sd_acb_cancelable(acb)) {
|
|
|
|
/* Remove outstanding requests from pending and failed queues. */
|
|
|
|
QLIST_FOREACH_SAFE(aioreq, &s->pending_aio_head, aio_siblings,
|
|
|
|
next) {
|
|
|
|
if (aioreq->aiocb == acb) {
|
|
|
|
free_aio_req(s, aioreq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QLIST_FOREACH_SAFE(aioreq, &s->failed_aio_head, aio_siblings,
|
|
|
|
next) {
|
|
|
|
if (aioreq->aiocb == acb) {
|
|
|
|
free_aio_req(s, aioreq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(acb->nr_pending == 0);
|
|
|
|
sd_finish_aiocb(acb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qemu_aio_wait();
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-31 19:34:37 +04:00
|
|
|
static const AIOCBInfo sd_aiocb_info = {
|
2010-06-21 00:01:00 +04:00
|
|
|
.aiocb_size = sizeof(SheepdogAIOCB),
|
|
|
|
.cancel = sd_aio_cancel,
|
|
|
|
};
|
|
|
|
|
|
|
|
static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
|
2013-01-14 10:01:03 +04:00
|
|
|
int64_t sector_num, int nb_sectors)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb;
|
|
|
|
|
2013-01-14 10:01:03 +04:00
|
|
|
acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
acb->qiov = qiov;
|
|
|
|
|
|
|
|
acb->sector_num = sector_num;
|
|
|
|
acb->nb_sectors = nb_sectors;
|
|
|
|
|
|
|
|
acb->aio_done_func = NULL;
|
2013-10-24 11:01:17 +04:00
|
|
|
acb->cancelable = true;
|
|
|
|
acb->finished = NULL;
|
2011-08-12 16:33:15 +04:00
|
|
|
acb->coroutine = qemu_coroutine_self();
|
2010-06-21 00:01:00 +04:00
|
|
|
acb->ret = 0;
|
2012-06-27 02:26:21 +04:00
|
|
|
acb->nr_pending = 0;
|
2010-06-21 00:01:00 +04:00
|
|
|
return acb;
|
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
static int connect_to_sdog(BDRVSheepdogState *s)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2013-02-22 07:39:52 +04:00
|
|
|
int fd;
|
|
|
|
Error *err = NULL;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:53 +04:00
|
|
|
if (s->is_unix) {
|
|
|
|
fd = unix_connect(s->host_spec, &err);
|
|
|
|
} else {
|
|
|
|
fd = inet_connect(s->host_spec, &err);
|
|
|
|
|
|
|
|
if (err == NULL) {
|
|
|
|
int ret = socket_set_nodelay(fd);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("%s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
if (err != NULL) {
|
|
|
|
qerror_report_err(err);
|
|
|
|
error_free(err);
|
2013-03-12 11:05:42 +04:00
|
|
|
} else {
|
2013-03-27 13:10:43 +04:00
|
|
|
qemu_set_nonblock(fd);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-05-30 04:03:55 +04:00
|
|
|
static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
|
|
|
|
unsigned int *wlen)
|
2012-04-04 00:03:58 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != sizeof(*hdr)) {
|
2012-04-04 00:03:58 +04:00
|
|
|
error_report("failed to send a req, %s", strerror(errno));
|
2012-04-03 14:04:21 +04:00
|
|
|
return ret;
|
2012-04-04 00:03:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = qemu_co_send(sockfd, data, *wlen);
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != *wlen) {
|
2012-04-04 00:03:58 +04:00
|
|
|
error_report("failed to send a req, %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-05-30 04:03:55 +04:00
|
|
|
|
2012-06-27 02:26:19 +04:00
|
|
|
static void restart_co_req(void *opaque)
|
|
|
|
{
|
|
|
|
Coroutine *co = opaque;
|
|
|
|
|
|
|
|
qemu_coroutine_enter(co, NULL);
|
|
|
|
}
|
|
|
|
|
2012-07-04 20:41:06 +04:00
|
|
|
typedef struct SheepdogReqCo {
|
|
|
|
int sockfd;
|
|
|
|
SheepdogReq *hdr;
|
|
|
|
void *data;
|
|
|
|
unsigned int *wlen;
|
|
|
|
unsigned int *rlen;
|
|
|
|
int ret;
|
|
|
|
bool finished;
|
|
|
|
} SheepdogReqCo;
|
|
|
|
|
|
|
|
static coroutine_fn void do_co_req(void *opaque)
|
2012-04-04 00:03:58 +04:00
|
|
|
{
|
|
|
|
int ret;
|
2012-06-27 02:26:19 +04:00
|
|
|
Coroutine *co;
|
2012-07-04 20:41:06 +04:00
|
|
|
SheepdogReqCo *srco = opaque;
|
|
|
|
int sockfd = srco->sockfd;
|
|
|
|
SheepdogReq *hdr = srco->hdr;
|
|
|
|
void *data = srco->data;
|
|
|
|
unsigned int *wlen = srco->wlen;
|
|
|
|
unsigned int *rlen = srco->rlen;
|
2012-06-27 02:26:19 +04:00
|
|
|
|
|
|
|
co = qemu_coroutine_self();
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, co);
|
2012-04-04 00:03:58 +04:00
|
|
|
|
|
|
|
ret = send_co_req(sockfd, hdr, data, wlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, co);
|
2012-06-27 02:26:19 +04:00
|
|
|
|
2012-04-04 00:03:58 +04:00
|
|
|
ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != sizeof(*hdr)) {
|
2012-04-04 00:03:58 +04:00
|
|
|
error_report("failed to get a rsp, %s", strerror(errno));
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = -errno;
|
2012-04-04 00:03:58 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*rlen > hdr->data_length) {
|
|
|
|
*rlen = hdr->data_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*rlen) {
|
|
|
|
ret = qemu_co_recv(sockfd, data, *rlen);
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != *rlen) {
|
2012-04-04 00:03:58 +04:00
|
|
|
error_report("failed to get the data, %s", strerror(errno));
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = -errno;
|
2012-04-04 00:03:58 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
out:
|
2013-03-12 11:05:43 +04:00
|
|
|
/* there is at most one request for this sockfd, so it is safe to
|
|
|
|
* set each handler to NULL. */
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL);
|
2012-07-04 20:41:06 +04:00
|
|
|
|
|
|
|
srco->ret = ret;
|
|
|
|
srco->finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_req(int sockfd, SheepdogReq *hdr, void *data,
|
|
|
|
unsigned int *wlen, unsigned int *rlen)
|
|
|
|
{
|
|
|
|
Coroutine *co;
|
|
|
|
SheepdogReqCo srco = {
|
|
|
|
.sockfd = sockfd,
|
|
|
|
.hdr = hdr,
|
|
|
|
.data = data,
|
|
|
|
.wlen = wlen,
|
|
|
|
.rlen = rlen,
|
|
|
|
.ret = 0,
|
|
|
|
.finished = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (qemu_in_coroutine()) {
|
|
|
|
do_co_req(&srco);
|
|
|
|
} else {
|
|
|
|
co = qemu_coroutine_create(do_co_req);
|
|
|
|
qemu_coroutine_enter(co, &srco);
|
|
|
|
while (!srco.finished) {
|
|
|
|
qemu_aio_wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return srco.ret;
|
2012-04-04 00:03:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:16 +04:00
|
|
|
static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
|
2012-10-06 20:57:14 +04:00
|
|
|
struct iovec *iov, int niov, bool create,
|
2010-06-21 00:01:00 +04:00
|
|
|
enum AIOCBState aiocb_type);
|
2013-10-24 11:01:16 +04:00
|
|
|
static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);
|
2013-10-24 11:01:13 +04:00
|
|
|
static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag);
|
2013-10-24 11:01:15 +04:00
|
|
|
static int get_sheep_fd(BDRVSheepdogState *s);
|
|
|
|
static void co_write_request(void *opaque);
|
2012-06-27 02:26:23 +04:00
|
|
|
|
|
|
|
static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid)
|
|
|
|
{
|
|
|
|
AIOReq *aio_req;
|
|
|
|
|
|
|
|
QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) {
|
|
|
|
if (aio_req->oid == oid) {
|
|
|
|
return aio_req;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* This function searchs pending requests to the object `oid', and
|
|
|
|
* sends them.
|
|
|
|
*/
|
2012-06-27 02:26:22 +04:00
|
|
|
static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2012-06-27 02:26:23 +04:00
|
|
|
AIOReq *aio_req;
|
2010-06-21 00:01:00 +04:00
|
|
|
SheepdogAIOCB *acb;
|
|
|
|
|
2012-06-27 02:26:23 +04:00
|
|
|
while ((aio_req = find_pending_req(s, oid)) != NULL) {
|
2010-06-21 00:01:00 +04:00
|
|
|
acb = aio_req->aiocb;
|
2012-06-27 02:26:22 +04:00
|
|
|
/* move aio_req from pending list to inflight one */
|
|
|
|
QLIST_REMOVE(aio_req, aio_siblings);
|
|
|
|
QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, false,
|
|
|
|
acb->aiocb_type);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:15 +04:00
|
|
|
static coroutine_fn void reconnect_to_sdog(void *opaque)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = opaque;
|
|
|
|
AIOReq *aio_req, *next;
|
|
|
|
|
|
|
|
qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
|
|
|
|
close(s->fd);
|
|
|
|
s->fd = -1;
|
|
|
|
|
|
|
|
/* Wait for outstanding write requests to be completed. */
|
|
|
|
while (s->co_send != NULL) {
|
|
|
|
co_write_request(opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to reconnect the sheepdog server every one second. */
|
|
|
|
while (s->fd < 0) {
|
|
|
|
s->fd = get_sheep_fd(s);
|
|
|
|
if (s->fd < 0) {
|
|
|
|
DPRINTF("Wait for connection to be established\n");
|
|
|
|
co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME,
|
|
|
|
1000000000ULL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we have to resend all the request in the inflight queue. However,
|
|
|
|
* resend_aioreq() can yield and newly created requests can be added to the
|
|
|
|
* inflight queue before the coroutine is resumed. To avoid mixing them, we
|
|
|
|
* have to move all the inflight requests to the failed queue before
|
|
|
|
* resend_aioreq() is called.
|
|
|
|
*/
|
|
|
|
QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) {
|
|
|
|
QLIST_REMOVE(aio_req, aio_siblings);
|
|
|
|
QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resend all the failed aio requests. */
|
|
|
|
while (!QLIST_EMPTY(&s->failed_aio_head)) {
|
|
|
|
aio_req = QLIST_FIRST(&s->failed_aio_head);
|
|
|
|
QLIST_REMOVE(aio_req, aio_siblings);
|
|
|
|
QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
|
|
|
|
resend_aioreq(s, aio_req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* Receive responses of the I/O requests.
|
|
|
|
*
|
|
|
|
* This function is registered as a fd handler, and called from the
|
|
|
|
* main loop when s->fd is ready for reading responses.
|
|
|
|
*/
|
2011-10-05 11:17:31 +04:00
|
|
|
static void coroutine_fn aio_read_response(void *opaque)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogObjRsp rsp;
|
|
|
|
BDRVSheepdogState *s = opaque;
|
|
|
|
int fd = s->fd;
|
|
|
|
int ret;
|
|
|
|
AIOReq *aio_req = NULL;
|
|
|
|
SheepdogAIOCB *acb;
|
2013-04-23 10:03:33 +04:00
|
|
|
uint64_t idx;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
/* read a header */
|
2011-09-08 15:46:25 +04:00
|
|
|
ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != sizeof(rsp)) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to get the header, %s", strerror(errno));
|
2013-10-24 11:01:15 +04:00
|
|
|
goto err;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-06-27 02:26:22 +04:00
|
|
|
/* find the right aio_req from the inflight aio list */
|
|
|
|
QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
|
2010-06-21 00:01:00 +04:00
|
|
|
if (aio_req->id == rsp.id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!aio_req) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("cannot find aio_req %x", rsp.id);
|
2013-10-24 11:01:15 +04:00
|
|
|
goto err;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
acb = aio_req->aiocb;
|
|
|
|
|
|
|
|
switch (acb->aiocb_type) {
|
|
|
|
case AIOCB_WRITE_UDATA:
|
2012-01-30 21:10:06 +04:00
|
|
|
/* this coroutine context is no longer suitable for co_recv
|
|
|
|
* because we may send data to update vdi objects */
|
|
|
|
s->co_recv = NULL;
|
2010-06-21 00:01:00 +04:00
|
|
|
if (!is_data_obj(aio_req->oid)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx = data_oid_to_idx(aio_req->oid);
|
|
|
|
|
|
|
|
if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) {
|
|
|
|
/*
|
|
|
|
* If the object is newly created one, we need to update
|
|
|
|
* the vdi object (metadata object). min_dirty_data_idx
|
|
|
|
* and max_dirty_data_idx are changed to include updated
|
|
|
|
* index between them.
|
|
|
|
*/
|
2012-12-17 10:17:26 +04:00
|
|
|
if (rsp.result == SD_RES_SUCCESS) {
|
|
|
|
s->inode.data_vdi_id[idx] = s->inode.vdi_id;
|
|
|
|
s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx);
|
|
|
|
s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx);
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* Some requests may be blocked because simultaneous
|
|
|
|
* create requests are not allowed, so we search the
|
|
|
|
* pending requests here.
|
|
|
|
*/
|
2012-12-17 10:17:27 +04:00
|
|
|
send_pending_req(s, aio_req->oid);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AIOCB_READ_UDATA:
|
2012-06-07 20:22:46 +04:00
|
|
|
ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
|
|
|
|
aio_req->iov_offset, rsp.data_length);
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != rsp.data_length) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to get the data, %s", strerror(errno));
|
2013-10-24 11:01:15 +04:00
|
|
|
goto err;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-01-15 12:28:55 +04:00
|
|
|
case AIOCB_FLUSH_CACHE:
|
|
|
|
if (rsp.result == SD_RES_INVALID_PARMS) {
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("disable cache since the server doesn't support it\n");
|
2013-01-15 12:28:55 +04:00
|
|
|
s->cache_flags = SD_FLAG_CMD_DIRECT;
|
|
|
|
rsp.result = SD_RES_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
2013-04-23 10:03:33 +04:00
|
|
|
case AIOCB_DISCARD_OBJ:
|
|
|
|
switch (rsp.result) {
|
|
|
|
case SD_RES_INVALID_PARMS:
|
|
|
|
error_report("sheep(%s) doesn't support discard command",
|
|
|
|
s->host_spec);
|
|
|
|
rsp.result = SD_RES_SUCCESS;
|
|
|
|
s->discard_supported = false;
|
|
|
|
break;
|
|
|
|
case SD_RES_SUCCESS:
|
|
|
|
idx = data_oid_to_idx(aio_req->oid);
|
|
|
|
s->inode.data_vdi_id[idx] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 20:19:54 +04:00
|
|
|
switch (rsp.result) {
|
|
|
|
case SD_RES_SUCCESS:
|
|
|
|
break;
|
|
|
|
case SD_RES_READONLY:
|
2013-10-24 11:01:13 +04:00
|
|
|
if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) {
|
|
|
|
ret = reload_inode(s, 0, "");
|
|
|
|
if (ret < 0) {
|
2013-10-24 11:01:15 +04:00
|
|
|
goto err;
|
2013-10-24 11:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_data_obj(aio_req->oid)) {
|
|
|
|
aio_req->oid = vid_to_data_oid(s->inode.vdi_id,
|
|
|
|
data_oid_to_idx(aio_req->oid));
|
|
|
|
} else {
|
|
|
|
aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id);
|
|
|
|
}
|
2013-10-24 11:01:16 +04:00
|
|
|
resend_aioreq(s, aio_req);
|
|
|
|
goto out;
|
2013-04-25 20:19:54 +04:00
|
|
|
default:
|
2010-06-21 00:01:00 +04:00
|
|
|
acb->ret = -EIO;
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("%s", sd_strerror(rsp.result));
|
2013-04-25 20:19:54 +04:00
|
|
|
break;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-06-27 02:26:21 +04:00
|
|
|
free_aio_req(s, aio_req);
|
|
|
|
if (!acb->nr_pending) {
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* We've finished all requests which belong to the AIOCB, so
|
2011-08-12 16:33:15 +04:00
|
|
|
* we can switch back to sd_co_readv/writev now.
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
|
|
|
acb->aio_done_func(acb);
|
|
|
|
}
|
2011-08-12 16:33:15 +04:00
|
|
|
out:
|
|
|
|
s->co_recv = NULL;
|
2013-10-24 11:01:15 +04:00
|
|
|
return;
|
|
|
|
err:
|
|
|
|
s->co_recv = NULL;
|
|
|
|
reconnect_to_sdog(opaque);
|
2011-08-12 16:33:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void co_read_response(void *opaque)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = opaque;
|
|
|
|
|
|
|
|
if (!s->co_recv) {
|
|
|
|
s->co_recv = qemu_coroutine_create(aio_read_response);
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_coroutine_enter(s->co_recv, opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void co_write_request(void *opaque)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = opaque;
|
|
|
|
|
|
|
|
qemu_coroutine_enter(s->co_send, NULL);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a socket discriptor to read/write objects.
|
|
|
|
*
|
|
|
|
* We cannot use this discriptor for other operations because
|
|
|
|
* the block driver may be on waiting response from the server.
|
|
|
|
*/
|
|
|
|
static int get_sheep_fd(BDRVSheepdogState *s)
|
|
|
|
{
|
2013-02-22 07:39:53 +04:00
|
|
|
int fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(fd, co_read_response, NULL, s);
|
2010-06-21 00:01:00 +04:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
|
|
|
|
char *vdi, uint32_t *snapid, char *tag)
|
|
|
|
{
|
|
|
|
URI *uri;
|
|
|
|
QueryParams *qp = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
uri = uri_parse(filename);
|
|
|
|
if (!uri) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:53 +04:00
|
|
|
/* transport */
|
|
|
|
if (!strcmp(uri->scheme, "sheepdog")) {
|
|
|
|
s->is_unix = false;
|
|
|
|
} else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
|
|
|
|
s->is_unix = false;
|
|
|
|
} else if (!strcmp(uri->scheme, "sheepdog+unix")) {
|
|
|
|
s->is_unix = true;
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
if (uri->path == NULL || !strcmp(uri->path, "/")) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
|
|
|
|
|
2013-02-22 07:39:53 +04:00
|
|
|
qp = query_params_parse(uri->query);
|
|
|
|
if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->is_unix) {
|
|
|
|
/* sheepdog+unix:///vdiname?socket=path */
|
|
|
|
if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
s->host_spec = g_strdup(qp->p[0].value);
|
|
|
|
} else {
|
|
|
|
/* sheepdog[+tcp]://[host:port]/vdiname */
|
|
|
|
s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
|
|
|
|
uri->port ?: SD_DEFAULT_PORT);
|
|
|
|
}
|
2013-02-22 07:39:51 +04:00
|
|
|
|
|
|
|
/* snapshot tag */
|
|
|
|
if (uri->fragment) {
|
|
|
|
*snapid = strtoul(uri->fragment, NULL, 10);
|
|
|
|
if (*snapid == 0) {
|
|
|
|
pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*snapid = CURRENT_VDI_ID; /* search current vdi */
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (qp) {
|
|
|
|
query_params_free(qp);
|
|
|
|
}
|
|
|
|
uri_free(uri);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
2013-02-22 07:39:51 +04:00
|
|
|
* Parse a filename (old syntax)
|
2010-06-21 00:01:00 +04:00
|
|
|
*
|
|
|
|
* filename must be one of the following formats:
|
|
|
|
* 1. [vdiname]
|
|
|
|
* 2. [vdiname]:[snapid]
|
|
|
|
* 3. [vdiname]:[tag]
|
|
|
|
* 4. [hostname]:[port]:[vdiname]
|
|
|
|
* 5. [hostname]:[port]:[vdiname]:[snapid]
|
|
|
|
* 6. [hostname]:[port]:[vdiname]:[tag]
|
|
|
|
*
|
|
|
|
* You can boot from the snapshot images by specifying `snapid` or
|
|
|
|
* `tag'.
|
|
|
|
*
|
|
|
|
* You can run VMs outside the Sheepdog cluster by specifying
|
|
|
|
* `hostname' and `port' (experimental).
|
|
|
|
*/
|
|
|
|
static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
|
|
|
|
char *vdi, uint32_t *snapid, char *tag)
|
|
|
|
{
|
2013-02-22 07:39:51 +04:00
|
|
|
char *p, *q, *uri;
|
|
|
|
const char *host_spec, *vdi_spec;
|
|
|
|
int nr_sep, ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
strstart(filename, "sheepdog:", (const char **)&filename);
|
2011-08-21 07:09:37 +04:00
|
|
|
p = q = g_strdup(filename);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
/* count the number of separators */
|
|
|
|
nr_sep = 0;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == ':') {
|
|
|
|
nr_sep++;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
p = q;
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
/* use the first two tokens as host_spec. */
|
2010-06-21 00:01:00 +04:00
|
|
|
if (nr_sep >= 2) {
|
2013-02-22 07:39:51 +04:00
|
|
|
host_spec = p;
|
2010-06-21 00:01:00 +04:00
|
|
|
p = strchr(p, ':');
|
2013-02-22 07:39:51 +04:00
|
|
|
p++;
|
2010-06-21 00:01:00 +04:00
|
|
|
p = strchr(p, ':');
|
|
|
|
*p++ = '\0';
|
|
|
|
} else {
|
2013-02-22 07:39:51 +04:00
|
|
|
host_spec = "";
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
vdi_spec = p;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
p = strchr(vdi_spec, ':');
|
2010-06-21 00:01:00 +04:00
|
|
|
if (p) {
|
2013-02-22 07:39:51 +04:00
|
|
|
*p++ = '#';
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
ret = sd_parse_uri(s, uri, vdi, snapid, tag);
|
|
|
|
|
|
|
|
g_free(q);
|
|
|
|
g_free(uri);
|
|
|
|
|
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 20:19:51 +04:00
|
|
|
static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
|
|
|
|
uint32_t snapid, const char *tag, uint32_t *vid,
|
|
|
|
bool lock)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
int ret, fd;
|
|
|
|
SheepdogVdiReq hdr;
|
|
|
|
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
|
|
|
|
unsigned int wlen, rlen = 0;
|
|
|
|
char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-04 15:09:47 +04:00
|
|
|
/* This pair of strncpy calls ensures that the buffer is zero-filled,
|
|
|
|
* which is desirable since we'll soon be sending those bytes, and
|
|
|
|
* don't want the send_req to read uninitialized data.
|
|
|
|
*/
|
2010-06-21 00:01:00 +04:00
|
|
|
strncpy(buf, filename, SD_MAX_VDI_LEN);
|
|
|
|
strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
|
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
2013-04-25 20:19:51 +04:00
|
|
|
if (lock) {
|
2010-06-21 00:01:00 +04:00
|
|
|
hdr.opcode = SD_OP_LOCK_VDI;
|
2013-04-25 20:19:51 +04:00
|
|
|
} else {
|
|
|
|
hdr.opcode = SD_OP_GET_VDI_INFO;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
|
|
|
|
hdr.proto_ver = SD_PROTO_VER;
|
|
|
|
hdr.data_length = wlen;
|
|
|
|
hdr.snapid = snapid;
|
|
|
|
hdr.flags = SD_FLAG_CMD_WRITE;
|
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
|
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsp->result != SD_RES_SUCCESS) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("cannot get vdi info, %s, %s %d %s",
|
2010-06-21 00:01:00 +04:00
|
|
|
sd_strerror(rsp->result), filename, snapid, tag);
|
2012-05-16 22:15:33 +04:00
|
|
|
if (rsp->result == SD_RES_NO_VDI) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
} else {
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*vid = rsp->vdi_id;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
closesocket(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:16 +04:00
|
|
|
static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
|
2012-10-06 20:57:14 +04:00
|
|
|
struct iovec *iov, int niov, bool create,
|
2010-06-21 00:01:00 +04:00
|
|
|
enum AIOCBState aiocb_type)
|
|
|
|
{
|
|
|
|
int nr_copies = s->inode.nr_copies;
|
|
|
|
SheepdogObjReq hdr;
|
2013-01-15 12:28:55 +04:00
|
|
|
unsigned int wlen = 0;
|
2010-06-21 00:01:00 +04:00
|
|
|
int ret;
|
|
|
|
uint64_t oid = aio_req->oid;
|
|
|
|
unsigned int datalen = aio_req->data_len;
|
|
|
|
uint64_t offset = aio_req->offset;
|
|
|
|
uint8_t flags = aio_req->flags;
|
|
|
|
uint64_t old_oid = aio_req->base_oid;
|
|
|
|
|
|
|
|
if (!nr_copies) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("bug");
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
|
2013-01-15 12:28:55 +04:00
|
|
|
switch (aiocb_type) {
|
|
|
|
case AIOCB_FLUSH_CACHE:
|
|
|
|
hdr.opcode = SD_OP_FLUSH_VDI;
|
|
|
|
break;
|
|
|
|
case AIOCB_READ_UDATA:
|
2010-06-21 00:01:00 +04:00
|
|
|
hdr.opcode = SD_OP_READ_OBJ;
|
|
|
|
hdr.flags = flags;
|
2013-01-15 12:28:55 +04:00
|
|
|
break;
|
|
|
|
case AIOCB_WRITE_UDATA:
|
|
|
|
if (create) {
|
|
|
|
hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
|
|
|
|
} else {
|
|
|
|
hdr.opcode = SD_OP_WRITE_OBJ;
|
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
wlen = datalen;
|
|
|
|
hdr.flags = SD_FLAG_CMD_WRITE | flags;
|
2013-01-15 12:28:55 +04:00
|
|
|
break;
|
2013-04-23 10:03:33 +04:00
|
|
|
case AIOCB_DISCARD_OBJ:
|
|
|
|
hdr.opcode = SD_OP_DISCARD_OBJ;
|
|
|
|
break;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 12:03:47 +04:00
|
|
|
if (s->cache_flags) {
|
|
|
|
hdr.flags |= s->cache_flags;
|
2012-04-04 00:03:58 +04:00
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
hdr.oid = oid;
|
|
|
|
hdr.cow_oid = old_oid;
|
|
|
|
hdr.copies = s->inode.nr_copies;
|
|
|
|
|
|
|
|
hdr.data_length = datalen;
|
|
|
|
hdr.offset = offset;
|
|
|
|
|
|
|
|
hdr.id = aio_req->id;
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
s->co_send = qemu_coroutine_self();
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(s->fd, co_read_response, co_write_request, s);
|
2011-09-21 14:36:48 +04:00
|
|
|
socket_set_cork(s->fd, 1);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
/* send a header */
|
2011-09-08 15:46:25 +04:00
|
|
|
ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != sizeof(hdr)) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to send a req, %s", strerror(errno));
|
2013-10-24 11:01:15 +04:00
|
|
|
goto out;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wlen) {
|
2012-06-07 20:22:46 +04:00
|
|
|
ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
|
2013-10-24 11:01:11 +04:00
|
|
|
if (ret != wlen) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to send a data, %s", strerror(errno));
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-24 11:01:15 +04:00
|
|
|
out:
|
2011-09-21 14:36:48 +04:00
|
|
|
socket_set_cork(s->fd, 0);
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(s->fd, co_read_response, NULL, s);
|
2013-10-24 11:01:15 +04:00
|
|
|
s->co_send = NULL;
|
2011-08-12 16:33:15 +04:00
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 12:51:51 +04:00
|
|
|
static int read_write_object(int fd, char *buf, uint64_t oid, uint8_t copies,
|
2010-06-21 00:01:00 +04:00
|
|
|
unsigned int datalen, uint64_t offset,
|
2013-01-10 12:03:47 +04:00
|
|
|
bool write, bool create, uint32_t cache_flags)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogObjReq hdr;
|
|
|
|
SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
|
|
|
|
unsigned int wlen, rlen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
|
|
|
|
if (write) {
|
|
|
|
wlen = datalen;
|
|
|
|
rlen = 0;
|
|
|
|
hdr.flags = SD_FLAG_CMD_WRITE;
|
|
|
|
if (create) {
|
|
|
|
hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
|
|
|
|
} else {
|
|
|
|
hdr.opcode = SD_OP_WRITE_OBJ;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wlen = 0;
|
|
|
|
rlen = datalen;
|
|
|
|
hdr.opcode = SD_OP_READ_OBJ;
|
|
|
|
}
|
2012-04-04 00:03:58 +04:00
|
|
|
|
2013-01-10 12:03:47 +04:00
|
|
|
hdr.flags |= cache_flags;
|
2012-04-04 00:03:58 +04:00
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
hdr.oid = oid;
|
|
|
|
hdr.data_length = datalen;
|
|
|
|
hdr.offset = offset;
|
|
|
|
hdr.copies = copies;
|
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
|
|
|
|
if (ret) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to send a request to the sheep");
|
2012-05-16 22:15:33 +04:00
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (rsp->result) {
|
|
|
|
case SD_RES_SUCCESS:
|
|
|
|
return 0;
|
|
|
|
default:
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("%s", sd_strerror(rsp->result));
|
2012-05-16 22:15:33 +04:00
|
|
|
return -EIO;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 12:51:51 +04:00
|
|
|
static int read_object(int fd, char *buf, uint64_t oid, uint8_t copies,
|
2013-01-10 12:03:47 +04:00
|
|
|
unsigned int datalen, uint64_t offset,
|
|
|
|
uint32_t cache_flags)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2012-10-06 20:57:14 +04:00
|
|
|
return read_write_object(fd, buf, oid, copies, datalen, offset, false,
|
2013-01-10 12:03:47 +04:00
|
|
|
false, cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 12:51:51 +04:00
|
|
|
static int write_object(int fd, char *buf, uint64_t oid, uint8_t copies,
|
2012-10-06 20:57:14 +04:00
|
|
|
unsigned int datalen, uint64_t offset, bool create,
|
2013-01-10 12:03:47 +04:00
|
|
|
uint32_t cache_flags)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2012-10-06 20:57:14 +04:00
|
|
|
return read_write_object(fd, buf, oid, copies, datalen, offset, true,
|
2013-01-10 12:03:47 +04:00
|
|
|
create, cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 20:19:53 +04:00
|
|
|
/* update inode with the latest state */
|
|
|
|
static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag)
|
|
|
|
{
|
|
|
|
SheepdogInode *inode;
|
|
|
|
int ret = 0, fd;
|
|
|
|
uint32_t vid = 0;
|
|
|
|
|
|
|
|
fd = connect_to_sdog(s);
|
|
|
|
if (fd < 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = g_malloc(sizeof(s->inode));
|
|
|
|
|
|
|
|
ret = find_vdi_name(s, s->name, snapid, tag, &vid, false);
|
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid),
|
|
|
|
s->inode.nr_copies, sizeof(*inode), 0, s->cache_flags);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inode->vdi_id != s->inode.vdi_id) {
|
|
|
|
memcpy(&s->inode, inode, sizeof(s->inode));
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_free(inode);
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:18 +04:00
|
|
|
/* Return true if the specified request is linked to the pending list. */
|
|
|
|
static bool check_simultaneous_create(BDRVSheepdogState *s, AIOReq *aio_req)
|
|
|
|
{
|
|
|
|
AIOReq *areq;
|
|
|
|
QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
|
|
|
|
if (areq != aio_req && areq->oid == aio_req->oid) {
|
|
|
|
/*
|
|
|
|
* Sheepdog cannot handle simultaneous create requests to the same
|
|
|
|
* object, so we cannot send the request until the previous request
|
|
|
|
* finishes.
|
|
|
|
*/
|
|
|
|
DPRINTF("simultaneous create to %" PRIx64 "\n", aio_req->oid);
|
|
|
|
aio_req->flags = 0;
|
|
|
|
aio_req->base_oid = 0;
|
|
|
|
QLIST_REMOVE(aio_req, aio_siblings);
|
|
|
|
QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req, aio_siblings);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:16 +04:00
|
|
|
static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)
|
2013-04-25 20:19:54 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb = aio_req->aiocb;
|
|
|
|
bool create = false;
|
|
|
|
|
|
|
|
/* check whether this request becomes a CoW one */
|
2013-10-24 11:01:12 +04:00
|
|
|
if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) {
|
2013-04-25 20:19:54 +04:00
|
|
|
int idx = data_oid_to_idx(aio_req->oid);
|
|
|
|
|
|
|
|
if (is_data_obj_writable(&s->inode, idx)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:18 +04:00
|
|
|
if (check_simultaneous_create(s, aio_req)) {
|
|
|
|
return;
|
2013-04-25 20:19:54 +04:00
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:18 +04:00
|
|
|
if (s->inode.data_vdi_id[idx]) {
|
|
|
|
aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx);
|
|
|
|
aio_req->flags |= SD_FLAG_CMD_COW;
|
|
|
|
}
|
2013-04-25 20:19:54 +04:00
|
|
|
create = true;
|
|
|
|
}
|
|
|
|
out:
|
2013-10-24 11:01:12 +04:00
|
|
|
if (is_data_obj(aio_req->oid)) {
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create,
|
|
|
|
acb->aiocb_type);
|
2013-10-24 11:01:12 +04:00
|
|
|
} else {
|
|
|
|
struct iovec iov;
|
|
|
|
iov.iov_base = &s->inode;
|
|
|
|
iov.iov_len = sizeof(s->inode);
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
|
2013-10-24 11:01:12 +04:00
|
|
|
}
|
2013-04-25 20:19:54 +04:00
|
|
|
}
|
|
|
|
|
2013-04-12 20:10:49 +04:00
|
|
|
/* TODO Convert to fine grained options */
|
|
|
|
static QemuOptsList runtime_opts = {
|
|
|
|
.name = "sheepdog",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "filename",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "URL to the sheepdog image",
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int sd_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
int ret, fd;
|
|
|
|
uint32_t vid = 0;
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
|
|
|
|
uint32_t snapid;
|
|
|
|
char *buf = NULL;
|
2013-04-12 20:10:49 +04:00
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
const char *filename;
|
|
|
|
|
2013-10-24 11:01:15 +04:00
|
|
|
s->bs = bs;
|
|
|
|
|
2013-04-12 20:10:49 +04:00
|
|
|
opts = qemu_opts_create_nofail(&runtime_opts);
|
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
qerror_report_err(local_err);
|
|
|
|
error_free(local_err);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = qemu_opt_get(opts, "filename");
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2012-06-27 02:26:22 +04:00
|
|
|
QLIST_INIT(&s->inflight_aio_head);
|
|
|
|
QLIST_INIT(&s->pending_aio_head);
|
2013-10-24 11:01:15 +04:00
|
|
|
QLIST_INIT(&s->failed_aio_head);
|
2010-06-21 00:01:00 +04:00
|
|
|
s->fd = -1;
|
|
|
|
|
|
|
|
memset(vdi, 0, sizeof(vdi));
|
|
|
|
memset(tag, 0, sizeof(tag));
|
2013-02-22 07:39:51 +04:00
|
|
|
|
|
|
|
if (strstr(filename, "://")) {
|
|
|
|
ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
|
|
|
|
} else {
|
|
|
|
ret = parse_vdiname(s, filename, vdi, &snapid, tag);
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
s->fd = get_sheep_fd(s);
|
|
|
|
if (s->fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = s->fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-25 20:19:51 +04:00
|
|
|
ret = find_vdi_name(s, vdi, snapid, tag, &vid, true);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-01-10 12:03:47 +04:00
|
|
|
/*
|
|
|
|
* QEMU block layer emulates writethrough cache as 'writeback + flush', so
|
|
|
|
* we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
|
|
|
|
*/
|
|
|
|
s->cache_flags = SD_FLAG_CMD_CACHE;
|
|
|
|
if (flags & BDRV_O_NOCACHE) {
|
|
|
|
s->cache_flags = SD_FLAG_CMD_DIRECT;
|
|
|
|
}
|
2013-04-23 10:03:33 +04:00
|
|
|
s->discard_supported = true;
|
2013-01-10 12:03:47 +04:00
|
|
|
|
2012-05-16 22:15:31 +04:00
|
|
|
if (snapid || tag[0] != '\0') {
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid);
|
2012-10-06 20:57:14 +04:00
|
|
|
s->is_snapshot = true;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
buf = g_malloc(SD_INODE_SIZE);
|
2012-04-04 00:03:58 +04:00
|
|
|
ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0,
|
2013-01-10 12:03:47 +04:00
|
|
|
s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&s->inode, buf, sizeof(s->inode));
|
|
|
|
s->min_dirty_data_idx = UINT32_MAX;
|
|
|
|
s->max_dirty_data_idx = 0;
|
|
|
|
|
2013-04-23 10:03:34 +04:00
|
|
|
bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
|
2012-10-04 15:09:47 +04:00
|
|
|
pstrcpy(s->name, sizeof(s->name), vdi);
|
2011-08-12 16:33:15 +04:00
|
|
|
qemu_co_mutex_init(&s->lock);
|
2013-04-12 20:10:49 +04:00
|
|
|
qemu_opts_del(opts);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2010-06-21 00:01:00 +04:00
|
|
|
return 0;
|
|
|
|
out:
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (s->fd >= 0) {
|
|
|
|
closesocket(s->fd);
|
|
|
|
}
|
2013-04-12 20:10:49 +04:00
|
|
|
qemu_opts_del(opts);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2012-05-16 22:15:33 +04:00
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 18:56:37 +04:00
|
|
|
static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogVdiReq hdr;
|
|
|
|
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
|
|
|
|
int fd, ret;
|
|
|
|
unsigned int wlen, rlen = 0;
|
|
|
|
char buf[SD_MAX_VDI_LEN];
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-04 15:09:47 +04:00
|
|
|
/* FIXME: would it be better to fail (e.g., return -EIO) when filename
|
|
|
|
* does not fit in buf? For now, just truncate and avoid buffer overrun.
|
|
|
|
*/
|
2010-06-21 00:01:00 +04:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2013-11-07 18:56:37 +04:00
|
|
|
pstrcpy(buf, sizeof(buf), s->name);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
hdr.opcode = SD_OP_NEW_VDI;
|
2013-11-07 18:56:37 +04:00
|
|
|
hdr.vdi_id = s->inode.vdi_id;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
wlen = SD_MAX_VDI_LEN;
|
|
|
|
|
|
|
|
hdr.flags = SD_FLAG_CMD_WRITE;
|
|
|
|
hdr.snapid = snapshot;
|
|
|
|
|
|
|
|
hdr.data_length = wlen;
|
2013-11-07 18:56:37 +04:00
|
|
|
hdr.vdi_size = s->inode.vdi_size;
|
|
|
|
hdr.copy_policy = s->inode.copy_policy;
|
2013-11-07 18:56:38 +04:00
|
|
|
hdr.copies = s->inode.nr_copies;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
|
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
if (ret) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rsp->result != SD_RES_SUCCESS) {
|
2013-11-07 18:56:37 +04:00
|
|
|
error_report("%s, %s", sd_strerror(rsp->result), s->inode.name);
|
2010-06-21 00:01:00 +04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vdi_id) {
|
|
|
|
*vdi_id = rsp->vdi_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:38:48 +04:00
|
|
|
static int sd_prealloc(const char *filename)
|
|
|
|
{
|
|
|
|
BlockDriverState *bs = NULL;
|
|
|
|
uint32_t idx, max_idx;
|
|
|
|
int64_t vdi_size;
|
2011-08-21 07:09:37 +04:00
|
|
|
void *buf = g_malloc0(SD_DATA_OBJ_SIZE);
|
2013-09-05 16:45:29 +04:00
|
|
|
Error *local_err = NULL;
|
2011-07-05 22:38:48 +04:00
|
|
|
int ret;
|
|
|
|
|
2013-09-05 16:45:29 +04:00
|
|
|
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
2011-07-05 22:38:48 +04:00
|
|
|
if (ret < 0) {
|
2013-09-05 16:45:29 +04:00
|
|
|
qerror_report_err(local_err);
|
|
|
|
error_free(local_err);
|
2011-07-05 22:38:48 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
vdi_size = bdrv_getlength(bs);
|
|
|
|
if (vdi_size < 0) {
|
|
|
|
ret = vdi_size;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE);
|
|
|
|
|
|
|
|
for (idx = 0; idx < max_idx; idx++) {
|
|
|
|
/*
|
|
|
|
* The created image can be a cloned image, so we need to read
|
|
|
|
* a data from the source image.
|
|
|
|
*/
|
|
|
|
ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (bs) {
|
2013-08-23 05:14:47 +04:00
|
|
|
bdrv_unref(bs);
|
2011-07-05 22:38:48 +04:00
|
|
|
}
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2011-07-05 22:38:48 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:56:38 +04:00
|
|
|
/*
|
|
|
|
* Sheepdog support two kinds of redundancy, full replication and erasure
|
|
|
|
* coding.
|
|
|
|
*
|
|
|
|
* # create a fully replicated vdi with x copies
|
|
|
|
* -o redundancy=x (1 <= x <= SD_MAX_COPIES)
|
|
|
|
*
|
|
|
|
* # create a erasure coded vdi with x data strips and y parity strips
|
|
|
|
* -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
|
|
|
|
*/
|
|
|
|
static int parse_redundancy(BDRVSheepdogState *s, const char *opt)
|
|
|
|
{
|
|
|
|
struct SheepdogInode *inode = &s->inode;
|
|
|
|
const char *n1, *n2;
|
|
|
|
long copy, parity;
|
|
|
|
char p[10];
|
|
|
|
|
|
|
|
pstrcpy(p, sizeof(p), opt);
|
|
|
|
n1 = strtok(p, ":");
|
|
|
|
n2 = strtok(NULL, ":");
|
|
|
|
|
|
|
|
if (!n1) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy = strtol(n1, NULL, 10);
|
|
|
|
if (copy > SD_MAX_COPIES || copy < 1) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!n2) {
|
|
|
|
inode->copy_policy = 0;
|
|
|
|
inode->nr_copies = copy;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy != 2 && copy != 4 && copy != 8 && copy != 16) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
parity = strtol(n2, NULL, 10);
|
|
|
|
if (parity >= SD_EC_MAX_STRIP || parity < 1) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 4 bits for parity and 4 bits for data.
|
|
|
|
* We have to compress upper data bits because it can't represent 16
|
|
|
|
*/
|
|
|
|
inode->copy_policy = ((copy / 2) << 4) + parity;
|
|
|
|
inode->nr_copies = copy + parity;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:26:05 +04:00
|
|
|
static int sd_create(const char *filename, QEMUOptionParameter *options,
|
|
|
|
Error **errp)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
2012-05-16 22:15:34 +04:00
|
|
|
int ret = 0;
|
2013-11-07 18:56:37 +04:00
|
|
|
uint32_t vid = 0;
|
2010-06-21 00:01:00 +04:00
|
|
|
char *backing_file = NULL;
|
2012-05-16 22:15:34 +04:00
|
|
|
BDRVSheepdogState *s;
|
2013-11-07 18:56:37 +04:00
|
|
|
char tag[SD_MAX_VDI_TAG_LEN];
|
2011-01-27 19:33:10 +03:00
|
|
|
uint32_t snapid;
|
2012-10-06 20:57:14 +04:00
|
|
|
bool prealloc = false;
|
2013-09-05 16:45:29 +04:00
|
|
|
Error *local_err = NULL;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2012-05-16 22:15:34 +04:00
|
|
|
s = g_malloc0(sizeof(BDRVSheepdogState));
|
|
|
|
|
2011-01-27 19:33:10 +03:00
|
|
|
memset(tag, 0, sizeof(tag));
|
2013-02-22 07:39:51 +04:00
|
|
|
if (strstr(filename, "://")) {
|
2013-11-07 18:56:37 +04:00
|
|
|
ret = sd_parse_uri(s, filename, s->name, &snapid, tag);
|
2013-02-22 07:39:51 +04:00
|
|
|
} else {
|
2013-11-07 18:56:37 +04:00
|
|
|
ret = parse_vdiname(s, filename, s->name, &snapid, tag);
|
2013-02-22 07:39:51 +04:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2012-05-16 22:15:34 +04:00
|
|
|
goto out;
|
2011-01-27 19:33:10 +03:00
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
while (options && options->name) {
|
|
|
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
2013-11-07 18:56:37 +04:00
|
|
|
s->inode.vdi_size = options->value.n;
|
2010-06-21 00:01:00 +04:00
|
|
|
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
|
|
|
|
backing_file = options->value.s;
|
2011-07-05 22:38:48 +04:00
|
|
|
} else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
|
|
|
|
if (!options->value.s || !strcmp(options->value.s, "off")) {
|
2012-10-06 20:57:14 +04:00
|
|
|
prealloc = false;
|
2011-07-05 22:38:48 +04:00
|
|
|
} else if (!strcmp(options->value.s, "full")) {
|
2012-10-06 20:57:14 +04:00
|
|
|
prealloc = true;
|
2011-07-05 22:38:48 +04:00
|
|
|
} else {
|
|
|
|
error_report("Invalid preallocation mode: '%s'",
|
|
|
|
options->value.s);
|
2012-05-16 22:15:34 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2011-07-05 22:38:48 +04:00
|
|
|
}
|
2013-11-07 18:56:38 +04:00
|
|
|
} else if (!strcmp(options->name, BLOCK_OPT_REDUNDANCY)) {
|
2013-12-08 20:11:20 +04:00
|
|
|
if (options->value.s) {
|
|
|
|
ret = parse_redundancy(s, options->value.s);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
2013-11-07 18:56:38 +04:00
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
options++;
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:56:37 +04:00
|
|
|
if (s->inode.vdi_size > SD_MAX_VDI_SIZE) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("too big image size");
|
2012-05-16 22:15:34 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (backing_file) {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
BDRVSheepdogState *s;
|
|
|
|
BlockDriver *drv;
|
|
|
|
|
|
|
|
/* Currently, only Sheepdog backing image is supported. */
|
2013-07-10 17:47:39 +04:00
|
|
|
drv = bdrv_find_protocol(backing_file, true);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("backing_file must be a sheepdog image");
|
2012-05-16 22:15:34 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:45:29 +04:00
|
|
|
ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
|
2012-05-16 22:15:33 +04:00
|
|
|
if (ret < 0) {
|
2013-09-05 16:45:29 +04:00
|
|
|
qerror_report_err(local_err);
|
|
|
|
error_free(local_err);
|
2012-05-16 22:15:34 +04:00
|
|
|
goto out;
|
2012-05-16 22:15:33 +04:00
|
|
|
}
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
s = bs->opaque;
|
|
|
|
|
|
|
|
if (!is_snapshot(&s->inode)) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("cannot clone from a non snapshot vdi");
|
2013-08-23 05:14:47 +04:00
|
|
|
bdrv_unref(bs);
|
2012-05-16 22:15:34 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 05:14:47 +04:00
|
|
|
bdrv_unref(bs);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 18:56:37 +04:00
|
|
|
ret = do_sd_create(s, &vid, 0);
|
2011-07-05 22:38:48 +04:00
|
|
|
if (!prealloc || ret) {
|
2012-05-16 22:15:34 +04:00
|
|
|
goto out;
|
2011-07-05 22:38:48 +04:00
|
|
|
}
|
|
|
|
|
2012-05-16 22:15:34 +04:00
|
|
|
ret = sd_prealloc(filename);
|
|
|
|
out:
|
|
|
|
g_free(s);
|
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
SheepdogVdiReq hdr;
|
|
|
|
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
|
|
|
|
unsigned int wlen, rlen = 0;
|
|
|
|
int fd, ret;
|
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%s\n", s->name);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
|
|
|
|
hdr.opcode = SD_OP_RELEASE_VDI;
|
2013-01-29 13:14:16 +04:00
|
|
|
hdr.vdi_id = s->inode.vdi_id;
|
2010-06-21 00:01:00 +04:00
|
|
|
wlen = strlen(s->name) + 1;
|
|
|
|
hdr.data_length = wlen;
|
|
|
|
hdr.flags = SD_FLAG_CMD_WRITE;
|
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
|
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
if (!ret && rsp->result != SD_RES_SUCCESS &&
|
|
|
|
rsp->result != SD_RES_VDI_NOT_LOCKED) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("%s, %s", sd_strerror(rsp->result), s->name);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 19:26:25 +04:00
|
|
|
qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
|
2010-06-21 00:01:00 +04:00
|
|
|
closesocket(s->fd);
|
2013-02-22 07:39:52 +04:00
|
|
|
g_free(s->host_spec);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t sd_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
|
|
|
|
return s->inode.vdi_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_truncate(BlockDriverState *bs, int64_t offset)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
int ret, fd;
|
|
|
|
unsigned int datalen;
|
|
|
|
|
|
|
|
if (offset < s->inode.vdi_size) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("shrinking is not supported");
|
2010-06-21 00:01:00 +04:00
|
|
|
return -EINVAL;
|
|
|
|
} else if (offset > SD_MAX_VDI_SIZE) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("too big image size");
|
2010-06-21 00:01:00 +04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't need to update entire object */
|
|
|
|
datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
|
|
|
|
s->inode.vdi_size = offset;
|
|
|
|
ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
|
2013-01-10 12:03:47 +04:00
|
|
|
s->inode.nr_copies, datalen, 0, false, s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to update an inode.");
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-05-16 22:15:33 +04:00
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called after writing data objects. If we need to
|
|
|
|
* update metadata, this sends a write request to the vdi object.
|
2011-08-12 16:33:15 +04:00
|
|
|
* Otherwise, this switches back to sd_co_readv/writev.
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
2011-10-05 11:17:31 +04:00
|
|
|
static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = acb->common.bs->opaque;
|
|
|
|
struct iovec iov;
|
|
|
|
AIOReq *aio_req;
|
|
|
|
uint32_t offset, data_len, mn, mx;
|
|
|
|
|
|
|
|
mn = s->min_dirty_data_idx;
|
|
|
|
mx = s->max_dirty_data_idx;
|
|
|
|
if (mn <= mx) {
|
|
|
|
/* we need to update the vdi object. */
|
|
|
|
offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
|
|
|
|
mn * sizeof(s->inode.data_vdi_id[0]);
|
|
|
|
data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
|
|
|
|
|
|
|
|
s->min_dirty_data_idx = UINT32_MAX;
|
|
|
|
s->max_dirty_data_idx = 0;
|
|
|
|
|
|
|
|
iov.iov_base = &s->inode;
|
|
|
|
iov.iov_len = sizeof(s->inode);
|
|
|
|
aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
|
|
|
|
data_len, offset, 0, 0, offset);
|
2012-06-27 02:26:22 +04:00
|
|
|
QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
acb->aio_done_func = sd_finish_aiocb;
|
|
|
|
acb->aiocb_type = AIOCB_WRITE_UDATA;
|
|
|
|
return;
|
|
|
|
}
|
2013-10-24 11:01:16 +04:00
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
sd_finish_aiocb(acb);
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:49:39 +04:00
|
|
|
/* Delete current working VDI on the snapshot chain */
|
|
|
|
static bool sd_delete(BDRVSheepdogState *s)
|
|
|
|
{
|
|
|
|
unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0;
|
|
|
|
SheepdogVdiReq hdr = {
|
|
|
|
.opcode = SD_OP_DEL_VDI,
|
|
|
|
.vdi_id = s->inode.vdi_id,
|
|
|
|
.data_length = wlen,
|
|
|
|
.flags = SD_FLAG_CMD_WRITE,
|
|
|
|
};
|
|
|
|
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = connect_to_sdog(s);
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
|
|
|
|
closesocket(fd);
|
|
|
|
if (ret) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (rsp->result) {
|
|
|
|
case SD_RES_NO_VDI:
|
|
|
|
error_report("%s was already deleted", s->name);
|
|
|
|
/* fall through */
|
|
|
|
case SD_RES_SUCCESS:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_report("%s, %s", sd_strerror(rsp->result), s->name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
/*
|
|
|
|
* Create a writable VDI from a snapshot
|
|
|
|
*/
|
|
|
|
static int sd_create_branch(BDRVSheepdogState *s)
|
|
|
|
{
|
|
|
|
int ret, fd;
|
|
|
|
uint32_t vid;
|
|
|
|
char *buf;
|
2013-04-25 16:49:39 +04:00
|
|
|
bool deleted;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
buf = g_malloc(SD_INODE_SIZE);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-04-25 16:49:39 +04:00
|
|
|
/*
|
|
|
|
* Even If deletion fails, we will just create extra snapshot based on
|
|
|
|
* the workding VDI which was supposed to be deleted. So no need to
|
|
|
|
* false bail out.
|
|
|
|
*/
|
|
|
|
deleted = sd_delete(s);
|
2013-11-07 18:56:37 +04:00
|
|
|
ret = do_sd_create(s, &vid, !deleted);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%" PRIx32 " is created.\n", vid);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,
|
2013-01-10 12:03:47 +04:00
|
|
|
SD_INODE_SIZE, 0, s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&s->inode, buf, sizeof(s->inode));
|
|
|
|
|
2012-10-06 20:57:14 +04:00
|
|
|
s->is_snapshot = false;
|
2010-06-21 00:01:00 +04:00
|
|
|
ret = 0;
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
out:
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send I/O requests to the server.
|
|
|
|
*
|
|
|
|
* This function sends requests to the server, links the requests to
|
2012-06-27 02:26:22 +04:00
|
|
|
* the inflight_list in BDRVSheepdogState, and exits without
|
2010-06-21 00:01:00 +04:00
|
|
|
* waiting the response. The responses are received in the
|
|
|
|
* `aio_read_response' function which is called from the main loop as
|
|
|
|
* a fd handler.
|
2011-08-12 16:33:15 +04:00
|
|
|
*
|
|
|
|
* Returns 1 when we need to wait a response, 0 when there is no sent
|
|
|
|
* request and -errno in error cases.
|
2010-06-21 00:01:00 +04:00
|
|
|
*/
|
2011-10-05 11:17:31 +04:00
|
|
|
static int coroutine_fn sd_co_rw_vector(void *p)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb = p;
|
|
|
|
int ret = 0;
|
2013-04-23 10:03:34 +04:00
|
|
|
unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE;
|
|
|
|
unsigned long idx = acb->sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE;
|
2010-06-21 00:01:00 +04:00
|
|
|
uint64_t oid;
|
2013-04-23 10:03:34 +04:00
|
|
|
uint64_t offset = (acb->sector_num * BDRV_SECTOR_SIZE) % SD_DATA_OBJ_SIZE;
|
2010-06-21 00:01:00 +04:00
|
|
|
BDRVSheepdogState *s = acb->common.bs->opaque;
|
|
|
|
SheepdogInode *inode = &s->inode;
|
|
|
|
AIOReq *aio_req;
|
|
|
|
|
|
|
|
if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
|
|
|
|
/*
|
|
|
|
* In the case we open the snapshot VDI, Sheepdog creates the
|
|
|
|
* writable VDI when we do a write operation first.
|
|
|
|
*/
|
|
|
|
ret = sd_create_branch(s);
|
|
|
|
if (ret) {
|
|
|
|
acb->ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 02:26:21 +04:00
|
|
|
/*
|
|
|
|
* Make sure we don't free the aiocb before we are done with all requests.
|
|
|
|
* This additional reference is dropped at the end of this function.
|
|
|
|
*/
|
|
|
|
acb->nr_pending++;
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
while (done != total) {
|
|
|
|
uint8_t flags = 0;
|
|
|
|
uint64_t old_oid = 0;
|
2012-10-06 20:57:14 +04:00
|
|
|
bool create = false;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
|
|
|
|
|
|
|
|
len = MIN(total - done, SD_DATA_OBJ_SIZE - offset);
|
|
|
|
|
2012-07-10 18:12:27 +04:00
|
|
|
switch (acb->aiocb_type) {
|
|
|
|
case AIOCB_READ_UDATA:
|
|
|
|
if (!inode->data_vdi_id[idx]) {
|
|
|
|
qemu_iovec_memset(acb->qiov, done, 0, len);
|
2010-06-21 00:01:00 +04:00
|
|
|
goto done;
|
|
|
|
}
|
2012-07-10 18:12:27 +04:00
|
|
|
break;
|
|
|
|
case AIOCB_WRITE_UDATA:
|
|
|
|
if (!inode->data_vdi_id[idx]) {
|
2012-10-06 20:57:14 +04:00
|
|
|
create = true;
|
2012-07-10 18:12:27 +04:00
|
|
|
} else if (!is_data_obj_writable(inode, idx)) {
|
|
|
|
/* Copy-On-Write */
|
2012-10-06 20:57:14 +04:00
|
|
|
create = true;
|
2012-07-10 18:12:27 +04:00
|
|
|
old_oid = oid;
|
|
|
|
flags = SD_FLAG_CMD_COW;
|
|
|
|
}
|
|
|
|
break;
|
2013-04-23 10:03:33 +04:00
|
|
|
case AIOCB_DISCARD_OBJ:
|
|
|
|
/*
|
|
|
|
* We discard the object only when the whole object is
|
|
|
|
* 1) allocated 2) trimmed. Otherwise, simply skip it.
|
|
|
|
*/
|
|
|
|
if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
2012-07-10 18:12:27 +04:00
|
|
|
default:
|
|
|
|
break;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (create) {
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
|
2012-06-27 02:26:18 +04:00
|
|
|
inode->vdi_id, oid,
|
2010-06-21 00:01:00 +04:00
|
|
|
vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
|
|
|
|
oid = vid_to_data_oid(inode->vdi_id, idx);
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("new oid %" PRIx64 "\n", oid);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done);
|
2013-10-24 11:01:18 +04:00
|
|
|
QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
if (create) {
|
2013-10-24 11:01:18 +04:00
|
|
|
if (check_simultaneous_create(s, aio_req)) {
|
|
|
|
goto done;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create,
|
|
|
|
acb->aiocb_type);
|
2010-06-21 00:01:00 +04:00
|
|
|
done:
|
|
|
|
offset = 0;
|
|
|
|
idx++;
|
|
|
|
done += len;
|
|
|
|
}
|
|
|
|
out:
|
2012-06-27 02:26:21 +04:00
|
|
|
if (!--acb->nr_pending) {
|
2011-08-12 16:33:15 +04:00
|
|
|
return acb->ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
2011-08-12 16:33:15 +04:00
|
|
|
return 1;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 12:23:22 +04:00
|
|
|
static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
|
2011-08-12 16:33:15 +04:00
|
|
|
int nb_sectors, QEMUIOVector *qiov)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb;
|
2011-08-12 16:33:15 +04:00
|
|
|
int ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
if (bs->growable && sector_num + nb_sectors > bs->total_sectors) {
|
2013-04-23 10:03:34 +04:00
|
|
|
ret = sd_truncate(bs, (sector_num + nb_sectors) * BDRV_SECTOR_SIZE);
|
2012-05-16 22:15:33 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
bs->total_sectors = sector_num + nb_sectors;
|
|
|
|
}
|
|
|
|
|
2013-01-14 10:01:03 +04:00
|
|
|
acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
|
2010-06-21 00:01:00 +04:00
|
|
|
acb->aio_done_func = sd_write_done;
|
|
|
|
acb->aiocb_type = AIOCB_WRITE_UDATA;
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
ret = sd_co_rw_vector(acb);
|
|
|
|
if (ret <= 0) {
|
|
|
|
qemu_aio_release(acb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return acb->ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 12:23:22 +04:00
|
|
|
static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
|
2011-08-12 16:33:15 +04:00
|
|
|
int nb_sectors, QEMUIOVector *qiov)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb;
|
2012-07-10 18:12:27 +04:00
|
|
|
int ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-01-14 10:01:03 +04:00
|
|
|
acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
|
2010-06-21 00:01:00 +04:00
|
|
|
acb->aiocb_type = AIOCB_READ_UDATA;
|
|
|
|
acb->aio_done_func = sd_finish_aiocb;
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
ret = sd_co_rw_vector(acb);
|
|
|
|
if (ret <= 0) {
|
|
|
|
qemu_aio_release(acb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return acb->ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-04-04 00:03:58 +04:00
|
|
|
static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
2013-01-15 12:28:55 +04:00
|
|
|
SheepdogAIOCB *acb;
|
|
|
|
AIOReq *aio_req;
|
2012-04-04 00:03:58 +04:00
|
|
|
|
2013-01-10 12:03:47 +04:00
|
|
|
if (s->cache_flags != SD_FLAG_CMD_CACHE) {
|
2012-04-04 00:03:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 10:01:03 +04:00
|
|
|
acb = sd_aio_setup(bs, NULL, 0, 0);
|
2013-01-15 12:28:55 +04:00
|
|
|
acb->aiocb_type = AIOCB_FLUSH_CACHE;
|
|
|
|
acb->aio_done_func = sd_finish_aiocb;
|
2012-04-04 00:03:58 +04:00
|
|
|
|
2013-01-15 12:28:55 +04:00
|
|
|
aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
|
|
|
|
0, 0, 0, 0, 0);
|
|
|
|
QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
|
2013-10-24 11:01:16 +04:00
|
|
|
add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type);
|
2012-04-04 00:03:58 +04:00
|
|
|
|
2013-01-15 12:28:55 +04:00
|
|
|
qemu_coroutine_yield();
|
|
|
|
return acb->ret;
|
2012-04-04 00:03:58 +04:00
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
int ret, fd;
|
|
|
|
uint32_t new_vid;
|
|
|
|
SheepdogInode *inode;
|
|
|
|
unsigned int datalen;
|
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
|
2010-06-21 00:01:00 +04:00
|
|
|
"is_snapshot %d\n", sn_info->name, sn_info->id_str,
|
|
|
|
s->name, sn_info->vm_state_size, s->is_snapshot);
|
|
|
|
|
|
|
|
if (s->is_snapshot) {
|
|
|
|
error_report("You can't create a snapshot of a snapshot VDI, "
|
2011-06-22 16:03:54 +04:00
|
|
|
"%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
s->inode.vm_state_size = sn_info->vm_state_size;
|
|
|
|
s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
|
2012-10-04 15:09:47 +04:00
|
|
|
/* It appears that inode.tag does not require a NUL terminator,
|
|
|
|
* which means this use of strncpy is ok.
|
|
|
|
*/
|
2010-06-21 00:01:00 +04:00
|
|
|
strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
|
|
|
|
/* we don't need to update entire object */
|
|
|
|
datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
|
|
|
|
|
|
|
|
/* refresh inode. */
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
|
2013-01-10 12:03:47 +04:00
|
|
|
s->inode.nr_copies, datalen, 0, false, s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (ret < 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to write snapshot's inode.");
|
2010-06-21 00:01:00 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:56:37 +04:00
|
|
|
ret = do_sd_create(s, &new_vid, 1);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (ret < 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to create inode for snapshot. %s",
|
2010-06-21 00:01:00 +04:00
|
|
|
strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
inode = (SheepdogInode *)g_malloc(datalen);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid),
|
2013-01-10 12:03:47 +04:00
|
|
|
s->inode.nr_copies, datalen, 0, s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to read new inode info. %s", strerror(errno));
|
2010-06-21 00:01:00 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&s->inode, inode, datalen);
|
2013-07-29 16:16:36 +04:00
|
|
|
DPRINTF("s->inode: name %s snap_id %x oid %x\n",
|
2010-06-21 00:01:00 +04:00
|
|
|
s->inode.name, s->inode.snap_id, s->inode.vdi_id);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
closesocket(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:49:39 +04:00
|
|
|
/*
|
|
|
|
* We implement rollback(loadvm) operation to the specified snapshot by
|
|
|
|
* 1) switch to the snapshot
|
|
|
|
* 2) rely on sd_create_branch to delete working VDI and
|
|
|
|
* 3) create a new working VDI based on the speicified snapshot
|
|
|
|
*/
|
2010-06-21 00:01:00 +04:00
|
|
|
static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
BDRVSheepdogState *old_s;
|
2013-04-25 20:19:53 +04:00
|
|
|
char tag[SD_MAX_VDI_TAG_LEN];
|
2010-06-21 00:01:00 +04:00
|
|
|
uint32_t snapid = 0;
|
2013-04-25 20:19:53 +04:00
|
|
|
int ret = 0;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
old_s = g_malloc(sizeof(BDRVSheepdogState));
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
memcpy(old_s, s, sizeof(BDRVSheepdogState));
|
|
|
|
|
|
|
|
snapid = strtoul(snapshot_id, NULL, 10);
|
2012-10-04 15:09:47 +04:00
|
|
|
if (snapid) {
|
|
|
|
tag[0] = 0;
|
|
|
|
} else {
|
2013-06-07 21:54:25 +04:00
|
|
|
pstrcpy(tag, sizeof(tag), snapshot_id);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 20:19:53 +04:00
|
|
|
ret = reload_inode(s, snapid, tag);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-06-07 21:54:26 +04:00
|
|
|
ret = sd_create_branch(s);
|
|
|
|
if (ret) {
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(old_s);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
out:
|
|
|
|
/* recover bdrv_sd_state */
|
|
|
|
memcpy(s, old_s, sizeof(BDRVSheepdogState));
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(old_s);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to open. recover old bdrv_sd_state.");
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 10:04:33 +04:00
|
|
|
static int sd_snapshot_delete(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
/* FIXME: Delete specified snapshot id. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
SheepdogReq req;
|
|
|
|
int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
|
|
|
|
QEMUSnapshotInfo *sn_tab = NULL;
|
|
|
|
unsigned wlen, rlen;
|
|
|
|
int found = 0;
|
|
|
|
static SheepdogInode inode;
|
|
|
|
unsigned long *vdi_inuse;
|
|
|
|
unsigned int start_nr;
|
|
|
|
uint64_t hval;
|
|
|
|
uint32_t vid;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
vdi_inuse = g_malloc(max);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlen = max;
|
|
|
|
wlen = 0;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.opcode = SD_OP_READ_VDIS;
|
|
|
|
req.data_length = max;
|
|
|
|
|
|
|
|
ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen);
|
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
sn_tab = g_malloc0(nr * sizeof(*sn_tab));
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
/* calculate a vdi id with hash function */
|
|
|
|
hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
|
|
|
|
start_nr = hval & (SD_NR_VDIS - 1);
|
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
ret = fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
|
|
|
|
if (!test_bit(vid, vdi_inuse)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't need to read entire object */
|
|
|
|
ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid),
|
2012-04-04 00:03:58 +04:00
|
|
|
0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
|
2013-01-10 12:03:47 +04:00
|
|
|
s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
|
|
|
|
sn_tab[found].date_sec = inode.snap_ctime >> 32;
|
|
|
|
sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
|
|
|
|
sn_tab[found].vm_state_size = inode.vm_state_size;
|
|
|
|
sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
|
|
|
|
|
|
|
|
snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u",
|
|
|
|
inode.snap_id);
|
2012-10-04 15:09:47 +04:00
|
|
|
pstrcpy(sn_tab[found].name,
|
|
|
|
MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
|
|
|
|
inode.tag);
|
2010-06-21 00:01:00 +04:00
|
|
|
found++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
out:
|
|
|
|
*psn_tab = sn_tab;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(vdi_inuse);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2012-05-16 22:15:33 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
|
|
|
|
int64_t pos, int size, int load)
|
|
|
|
{
|
2012-10-06 20:57:14 +04:00
|
|
|
bool create;
|
|
|
|
int fd, ret = 0, remaining = size;
|
2010-06-21 00:01:00 +04:00
|
|
|
unsigned int data_len;
|
|
|
|
uint64_t vmstate_oid;
|
|
|
|
uint64_t offset;
|
2013-06-07 21:54:26 +04:00
|
|
|
uint32_t vdi_index;
|
|
|
|
uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-02-22 07:39:52 +04:00
|
|
|
fd = connect_to_sdog(s);
|
2010-06-21 00:01:00 +04:00
|
|
|
if (fd < 0) {
|
2012-05-16 22:15:33 +04:00
|
|
|
return fd;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
2012-05-29 20:05:15 +04:00
|
|
|
while (remaining) {
|
2010-06-21 00:01:00 +04:00
|
|
|
vdi_index = pos / SD_DATA_OBJ_SIZE;
|
|
|
|
offset = pos % SD_DATA_OBJ_SIZE;
|
|
|
|
|
2012-08-29 22:39:45 +04:00
|
|
|
data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-06-07 21:54:26 +04:00
|
|
|
vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index);
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
create = (offset == 0);
|
|
|
|
if (load) {
|
|
|
|
ret = read_object(fd, (char *)data, vmstate_oid,
|
2012-04-04 00:03:58 +04:00
|
|
|
s->inode.nr_copies, data_len, offset,
|
2013-01-10 12:03:47 +04:00
|
|
|
s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
} else {
|
|
|
|
ret = write_object(fd, (char *)data, vmstate_oid,
|
2012-04-04 00:03:58 +04:00
|
|
|
s->inode.nr_copies, data_len, offset, create,
|
2013-01-10 12:03:47 +04:00
|
|
|
s->cache_flags);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2011-06-22 16:03:54 +04:00
|
|
|
error_report("failed to save vmstate %s", strerror(errno));
|
2010-06-21 00:01:00 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += data_len;
|
2012-08-29 22:39:45 +04:00
|
|
|
data += data_len;
|
2012-05-29 20:05:15 +04:00
|
|
|
remaining -= data_len;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
2012-05-29 20:05:15 +04:00
|
|
|
ret = size;
|
2010-06-21 00:01:00 +04:00
|
|
|
cleanup:
|
|
|
|
closesocket(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:27:53 +04:00
|
|
|
static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
|
|
|
|
int64_t pos)
|
2010-06-21 00:01:00 +04:00
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
2013-04-05 23:27:53 +04:00
|
|
|
void *buf;
|
|
|
|
int ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
|
2013-04-05 23:27:53 +04:00
|
|
|
buf = qemu_blockalign(bs, qiov->size);
|
|
|
|
qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
|
|
|
|
ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0);
|
|
|
|
qemu_vfree(buf);
|
|
|
|
|
|
|
|
return ret;
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
|
|
|
|
int64_t pos, int size)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
|
|
|
|
return do_load_save_vmstate(s, data, pos, size, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-23 10:03:33 +04:00
|
|
|
static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num,
|
|
|
|
int nb_sectors)
|
|
|
|
{
|
|
|
|
SheepdogAIOCB *acb;
|
|
|
|
QEMUIOVector dummy;
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!s->discard_supported) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors);
|
|
|
|
acb->aiocb_type = AIOCB_DISCARD_OBJ;
|
|
|
|
acb->aio_done_func = sd_finish_aiocb;
|
|
|
|
|
|
|
|
ret = sd_co_rw_vector(acb);
|
|
|
|
if (ret <= 0) {
|
|
|
|
qemu_aio_release(acb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return acb->ret;
|
|
|
|
}
|
|
|
|
|
2013-09-04 21:00:28 +04:00
|
|
|
static coroutine_fn int64_t
|
|
|
|
sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
|
|
|
|
int *pnum)
|
2013-04-23 10:03:35 +04:00
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
SheepdogInode *inode = &s->inode;
|
|
|
|
unsigned long start = sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE,
|
|
|
|
end = DIV_ROUND_UP((sector_num + nb_sectors) *
|
|
|
|
BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE);
|
|
|
|
unsigned long idx;
|
2013-09-04 21:00:30 +04:00
|
|
|
int64_t ret = BDRV_BLOCK_DATA;
|
2013-04-23 10:03:35 +04:00
|
|
|
|
|
|
|
for (idx = start; idx < end; idx++) {
|
|
|
|
if (inode->data_vdi_id[idx] == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (idx == start) {
|
|
|
|
/* Get the longest length of unallocated sectors */
|
|
|
|
ret = 0;
|
|
|
|
for (idx = start + 1; idx < end; idx++) {
|
|
|
|
if (inode->data_vdi_id[idx] != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pnum = (idx - start) * SD_DATA_OBJ_SIZE / BDRV_SECTOR_SIZE;
|
|
|
|
if (*pnum > nb_sectors) {
|
|
|
|
*pnum = nb_sectors;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-07 12:59:53 +04:00
|
|
|
static int64_t sd_get_allocated_file_size(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSheepdogState *s = bs->opaque;
|
|
|
|
SheepdogInode *inode = &s->inode;
|
|
|
|
unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, SD_DATA_OBJ_SIZE);
|
|
|
|
uint64_t size = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < last; i++) {
|
|
|
|
if (inode->data_vdi_id[i] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size += SD_DATA_OBJ_SIZE;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
static QEMUOptionParameter sd_create_options[] = {
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_BACKING_FILE,
|
|
|
|
.type = OPT_STRING,
|
|
|
|
.help = "File name of a base image"
|
|
|
|
},
|
2011-07-05 22:38:48 +04:00
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_PREALLOC,
|
|
|
|
.type = OPT_STRING,
|
|
|
|
.help = "Preallocation mode (allowed values: off, full)"
|
|
|
|
},
|
2013-11-07 18:56:38 +04:00
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_REDUNDANCY,
|
|
|
|
.type = OPT_STRING,
|
|
|
|
.help = "Redundancy of the image"
|
|
|
|
},
|
2010-06-21 00:01:00 +04:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
static BlockDriver bdrv_sheepdog = {
|
2010-06-21 00:01:00 +04:00
|
|
|
.format_name = "sheepdog",
|
|
|
|
.protocol_name = "sheepdog",
|
|
|
|
.instance_size = sizeof(BDRVSheepdogState),
|
2013-09-24 19:07:04 +04:00
|
|
|
.bdrv_needs_filename = true,
|
2010-06-21 00:01:00 +04:00
|
|
|
.bdrv_file_open = sd_open,
|
|
|
|
.bdrv_close = sd_close,
|
|
|
|
.bdrv_create = sd_create,
|
2013-08-06 10:44:37 +04:00
|
|
|
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
2010-06-21 00:01:00 +04:00
|
|
|
.bdrv_getlength = sd_getlength,
|
2013-08-07 12:59:53 +04:00
|
|
|
.bdrv_get_allocated_file_size = sd_get_allocated_file_size,
|
2010-06-21 00:01:00 +04:00
|
|
|
.bdrv_truncate = sd_truncate,
|
|
|
|
|
2011-08-12 16:33:15 +04:00
|
|
|
.bdrv_co_readv = sd_co_readv,
|
|
|
|
.bdrv_co_writev = sd_co_writev,
|
2012-04-04 00:03:58 +04:00
|
|
|
.bdrv_co_flush_to_disk = sd_co_flush_to_disk,
|
2013-04-23 10:03:33 +04:00
|
|
|
.bdrv_co_discard = sd_co_discard,
|
2013-09-04 21:00:28 +04:00
|
|
|
.bdrv_co_get_block_status = sd_co_get_block_status,
|
2010-06-21 00:01:00 +04:00
|
|
|
|
|
|
|
.bdrv_snapshot_create = sd_snapshot_create,
|
|
|
|
.bdrv_snapshot_goto = sd_snapshot_goto,
|
|
|
|
.bdrv_snapshot_delete = sd_snapshot_delete,
|
|
|
|
.bdrv_snapshot_list = sd_snapshot_list,
|
|
|
|
|
|
|
|
.bdrv_save_vmstate = sd_save_vmstate,
|
|
|
|
.bdrv_load_vmstate = sd_load_vmstate,
|
|
|
|
|
|
|
|
.create_options = sd_create_options,
|
|
|
|
};
|
|
|
|
|
2013-02-22 07:39:51 +04:00
|
|
|
static BlockDriver bdrv_sheepdog_tcp = {
|
|
|
|
.format_name = "sheepdog",
|
|
|
|
.protocol_name = "sheepdog+tcp",
|
|
|
|
.instance_size = sizeof(BDRVSheepdogState),
|
2013-09-24 19:07:04 +04:00
|
|
|
.bdrv_needs_filename = true,
|
2013-02-22 07:39:51 +04:00
|
|
|
.bdrv_file_open = sd_open,
|
|
|
|
.bdrv_close = sd_close,
|
|
|
|
.bdrv_create = sd_create,
|
2013-08-06 10:44:37 +04:00
|
|
|
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
2013-02-22 07:39:51 +04:00
|
|
|
.bdrv_getlength = sd_getlength,
|
2013-08-07 12:59:53 +04:00
|
|
|
.bdrv_get_allocated_file_size = sd_get_allocated_file_size,
|
2013-02-22 07:39:51 +04:00
|
|
|
.bdrv_truncate = sd_truncate,
|
|
|
|
|
|
|
|
.bdrv_co_readv = sd_co_readv,
|
|
|
|
.bdrv_co_writev = sd_co_writev,
|
|
|
|
.bdrv_co_flush_to_disk = sd_co_flush_to_disk,
|
2013-04-23 10:03:33 +04:00
|
|
|
.bdrv_co_discard = sd_co_discard,
|
2013-09-04 21:00:28 +04:00
|
|
|
.bdrv_co_get_block_status = sd_co_get_block_status,
|
2013-02-22 07:39:51 +04:00
|
|
|
|
|
|
|
.bdrv_snapshot_create = sd_snapshot_create,
|
|
|
|
.bdrv_snapshot_goto = sd_snapshot_goto,
|
|
|
|
.bdrv_snapshot_delete = sd_snapshot_delete,
|
|
|
|
.bdrv_snapshot_list = sd_snapshot_list,
|
|
|
|
|
|
|
|
.bdrv_save_vmstate = sd_save_vmstate,
|
|
|
|
.bdrv_load_vmstate = sd_load_vmstate,
|
|
|
|
|
|
|
|
.create_options = sd_create_options,
|
|
|
|
};
|
|
|
|
|
2013-02-22 07:39:53 +04:00
|
|
|
static BlockDriver bdrv_sheepdog_unix = {
|
|
|
|
.format_name = "sheepdog",
|
|
|
|
.protocol_name = "sheepdog+unix",
|
|
|
|
.instance_size = sizeof(BDRVSheepdogState),
|
2013-09-24 19:07:04 +04:00
|
|
|
.bdrv_needs_filename = true,
|
2013-02-22 07:39:53 +04:00
|
|
|
.bdrv_file_open = sd_open,
|
|
|
|
.bdrv_close = sd_close,
|
|
|
|
.bdrv_create = sd_create,
|
2013-06-28 14:47:42 +04:00
|
|
|
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
2013-02-22 07:39:53 +04:00
|
|
|
.bdrv_getlength = sd_getlength,
|
2013-08-07 12:59:53 +04:00
|
|
|
.bdrv_get_allocated_file_size = sd_get_allocated_file_size,
|
2013-02-22 07:39:53 +04:00
|
|
|
.bdrv_truncate = sd_truncate,
|
|
|
|
|
|
|
|
.bdrv_co_readv = sd_co_readv,
|
|
|
|
.bdrv_co_writev = sd_co_writev,
|
|
|
|
.bdrv_co_flush_to_disk = sd_co_flush_to_disk,
|
2013-04-23 10:03:33 +04:00
|
|
|
.bdrv_co_discard = sd_co_discard,
|
2013-09-04 21:00:28 +04:00
|
|
|
.bdrv_co_get_block_status = sd_co_get_block_status,
|
2013-02-22 07:39:53 +04:00
|
|
|
|
|
|
|
.bdrv_snapshot_create = sd_snapshot_create,
|
|
|
|
.bdrv_snapshot_goto = sd_snapshot_goto,
|
|
|
|
.bdrv_snapshot_delete = sd_snapshot_delete,
|
|
|
|
.bdrv_snapshot_list = sd_snapshot_list,
|
|
|
|
|
|
|
|
.bdrv_save_vmstate = sd_save_vmstate,
|
|
|
|
.bdrv_load_vmstate = sd_load_vmstate,
|
|
|
|
|
|
|
|
.create_options = sd_create_options,
|
|
|
|
};
|
|
|
|
|
2010-06-21 00:01:00 +04:00
|
|
|
static void bdrv_sheepdog_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_sheepdog);
|
2013-02-22 07:39:51 +04:00
|
|
|
bdrv_register(&bdrv_sheepdog_tcp);
|
2013-02-22 07:39:53 +04:00
|
|
|
bdrv_register(&bdrv_sheepdog_unix);
|
2010-06-21 00:01:00 +04:00
|
|
|
}
|
|
|
|
block_init(bdrv_sheepdog_init);
|