MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License version |
| 6 | * 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * You should have received a copy of the GNU General Public License |
| 9 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 10 | * |
| 11 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 12 | * GNU GPL, version 2 or (at your option) any later version. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 13 | */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 14 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 16 | #include "qemu-common.h" |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 17 | #include "qemu/uri.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/error-report.h" |
| 19 | #include "qemu/sockets.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 20 | #include "block/block_int.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 21 | #include "qemu/bitops.h" |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 22 | |
| 23 | #define SD_PROTO_VER 0x01 |
| 24 | |
| 25 | #define SD_DEFAULT_ADDR "localhost" |
MORITA Kazutaka | 25af257 | 2013-02-22 12:39:52 +0900 | [diff] [blame] | 26 | #define SD_DEFAULT_PORT 7000 |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 27 | |
| 28 | #define SD_OP_CREATE_AND_WRITE_OBJ 0x01 |
| 29 | #define SD_OP_READ_OBJ 0x02 |
| 30 | #define SD_OP_WRITE_OBJ 0x03 |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 31 | /* 0x04 is used internally by Sheepdog */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 32 | |
| 33 | #define SD_OP_NEW_VDI 0x11 |
| 34 | #define SD_OP_LOCK_VDI 0x12 |
| 35 | #define SD_OP_RELEASE_VDI 0x13 |
| 36 | #define SD_OP_GET_VDI_INFO 0x14 |
| 37 | #define SD_OP_READ_VDIS 0x15 |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 38 | #define SD_OP_FLUSH_VDI 0x16 |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 39 | #define SD_OP_DEL_VDI 0x17 |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 40 | #define SD_OP_GET_CLUSTER_DEFAULT 0x18 |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 41 | |
| 42 | #define SD_FLAG_CMD_WRITE 0x01 |
| 43 | #define SD_FLAG_CMD_COW 0x02 |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 44 | #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */ |
| 45 | #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 46 | |
| 47 | #define SD_RES_SUCCESS 0x00 /* Success */ |
| 48 | #define SD_RES_UNKNOWN 0x01 /* Unknown error */ |
| 49 | #define SD_RES_NO_OBJ 0x02 /* No object found */ |
| 50 | #define SD_RES_EIO 0x03 /* I/O error */ |
| 51 | #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */ |
| 52 | #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */ |
| 53 | #define SD_RES_SYSTEM_ERROR 0x06 /* System error */ |
| 54 | #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */ |
| 55 | #define SD_RES_NO_VDI 0x08 /* No vdi found */ |
| 56 | #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */ |
| 57 | #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */ |
| 58 | #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */ |
| 59 | #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */ |
| 60 | #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */ |
| 61 | #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */ |
| 62 | #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */ |
| 63 | #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */ |
| 64 | #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */ |
| 65 | #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */ |
| 66 | #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */ |
| 67 | #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */ |
| 68 | #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */ |
| 69 | #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */ |
| 70 | #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */ |
| 71 | #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */ |
Liu Yuan | fca23f0 | 2013-03-18 14:27:55 +0800 | [diff] [blame] | 72 | #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */ |
MORITA Kazutaka | 6a0b549 | 2013-04-26 01:19:52 +0900 | [diff] [blame] | 73 | #define SD_RES_READONLY 0x1A /* Object is read-only */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Object ID rules |
| 77 | * |
| 78 | * 0 - 19 (20 bits): data object space |
| 79 | * 20 - 31 (12 bits): reserved data object space |
| 80 | * 32 - 55 (24 bits): vdi object space |
| 81 | * 56 - 59 ( 4 bits): reserved vdi object space |
Dong Xu Wang | 7acae20 | 2011-10-14 15:41:06 +0800 | [diff] [blame] | 82 | * 60 - 63 ( 4 bits): object type identifier space |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 83 | */ |
| 84 | |
| 85 | #define VDI_SPACE_SHIFT 32 |
| 86 | #define VDI_BIT (UINT64_C(1) << 63) |
| 87 | #define VMSTATE_BIT (UINT64_C(1) << 62) |
| 88 | #define MAX_DATA_OBJS (UINT64_C(1) << 20) |
| 89 | #define MAX_CHILDREN 1024 |
| 90 | #define SD_MAX_VDI_LEN 256 |
| 91 | #define SD_MAX_VDI_TAG_LEN 256 |
| 92 | #define SD_NR_VDIS (1U << 24) |
| 93 | #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22) |
| 94 | #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS) |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 95 | #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22 |
Liu Yuan | b3af018 | 2013-11-07 22:56:38 +0800 | [diff] [blame] | 96 | /* |
| 97 | * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and |
| 98 | * (SD_EC_MAX_STRIP - 1) for parity strips |
| 99 | * |
| 100 | * SD_MAX_COPIES is sum of number of data strips and parity strips. |
| 101 | */ |
| 102 | #define SD_EC_MAX_STRIP 16 |
| 103 | #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 104 | |
| 105 | #define SD_INODE_SIZE (sizeof(SheepdogInode)) |
| 106 | #define CURRENT_VDI_ID 0 |
| 107 | |
Hitoshi Mitake | 1dbfafe | 2014-08-11 14:43:45 +0900 | [diff] [blame] | 108 | #define LOCK_TYPE_NORMAL 0 |
| 109 | #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */ |
| 110 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 111 | typedef struct SheepdogReq { |
| 112 | uint8_t proto_ver; |
| 113 | uint8_t opcode; |
| 114 | uint16_t flags; |
| 115 | uint32_t epoch; |
| 116 | uint32_t id; |
| 117 | uint32_t data_length; |
| 118 | uint32_t opcode_specific[8]; |
| 119 | } SheepdogReq; |
| 120 | |
| 121 | typedef struct SheepdogRsp { |
| 122 | uint8_t proto_ver; |
| 123 | uint8_t opcode; |
| 124 | uint16_t flags; |
| 125 | uint32_t epoch; |
| 126 | uint32_t id; |
| 127 | uint32_t data_length; |
| 128 | uint32_t result; |
| 129 | uint32_t opcode_specific[7]; |
| 130 | } SheepdogRsp; |
| 131 | |
| 132 | typedef struct SheepdogObjReq { |
| 133 | uint8_t proto_ver; |
| 134 | uint8_t opcode; |
| 135 | uint16_t flags; |
| 136 | uint32_t epoch; |
| 137 | uint32_t id; |
| 138 | uint32_t data_length; |
| 139 | uint64_t oid; |
| 140 | uint64_t cow_oid; |
Liu Yuan | 29a67f7 | 2013-10-23 16:51:51 +0800 | [diff] [blame] | 141 | uint8_t copies; |
Liu Yuan | 1841f88 | 2013-10-23 16:51:52 +0800 | [diff] [blame] | 142 | uint8_t copy_policy; |
| 143 | uint8_t reserved[6]; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 144 | uint64_t offset; |
| 145 | } SheepdogObjReq; |
| 146 | |
| 147 | typedef struct SheepdogObjRsp { |
| 148 | uint8_t proto_ver; |
| 149 | uint8_t opcode; |
| 150 | uint16_t flags; |
| 151 | uint32_t epoch; |
| 152 | uint32_t id; |
| 153 | uint32_t data_length; |
| 154 | uint32_t result; |
Liu Yuan | 29a67f7 | 2013-10-23 16:51:51 +0800 | [diff] [blame] | 155 | uint8_t copies; |
Liu Yuan | 1841f88 | 2013-10-23 16:51:52 +0800 | [diff] [blame] | 156 | uint8_t copy_policy; |
| 157 | uint8_t reserved[2]; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 158 | uint32_t pad[6]; |
| 159 | } SheepdogObjRsp; |
| 160 | |
| 161 | typedef struct SheepdogVdiReq { |
| 162 | uint8_t proto_ver; |
| 163 | uint8_t opcode; |
| 164 | uint16_t flags; |
| 165 | uint32_t epoch; |
| 166 | uint32_t id; |
| 167 | uint32_t data_length; |
| 168 | uint64_t vdi_size; |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 169 | uint32_t base_vdi_id; |
Liu Yuan | 29a67f7 | 2013-10-23 16:51:51 +0800 | [diff] [blame] | 170 | uint8_t copies; |
Liu Yuan | 1841f88 | 2013-10-23 16:51:52 +0800 | [diff] [blame] | 171 | uint8_t copy_policy; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 172 | uint8_t store_policy; |
| 173 | uint8_t block_size_shift; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 174 | uint32_t snapid; |
Hitoshi Mitake | 1dbfafe | 2014-08-11 14:43:45 +0900 | [diff] [blame] | 175 | uint32_t type; |
| 176 | uint32_t pad[2]; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 177 | } SheepdogVdiReq; |
| 178 | |
| 179 | typedef struct SheepdogVdiRsp { |
| 180 | uint8_t proto_ver; |
| 181 | uint8_t opcode; |
| 182 | uint16_t flags; |
| 183 | uint32_t epoch; |
| 184 | uint32_t id; |
| 185 | uint32_t data_length; |
| 186 | uint32_t result; |
| 187 | uint32_t rsvd; |
| 188 | uint32_t vdi_id; |
| 189 | uint32_t pad[5]; |
| 190 | } SheepdogVdiRsp; |
| 191 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 192 | typedef struct SheepdogClusterRsp { |
| 193 | uint8_t proto_ver; |
| 194 | uint8_t opcode; |
| 195 | uint16_t flags; |
| 196 | uint32_t epoch; |
| 197 | uint32_t id; |
| 198 | uint32_t data_length; |
| 199 | uint32_t result; |
| 200 | uint8_t nr_copies; |
| 201 | uint8_t copy_policy; |
| 202 | uint8_t block_size_shift; |
| 203 | uint8_t __pad1; |
| 204 | uint32_t __pad2[6]; |
| 205 | } SheepdogClusterRsp; |
| 206 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 207 | typedef struct SheepdogInode { |
| 208 | char name[SD_MAX_VDI_LEN]; |
| 209 | char tag[SD_MAX_VDI_TAG_LEN]; |
| 210 | uint64_t ctime; |
| 211 | uint64_t snap_ctime; |
| 212 | uint64_t vm_clock_nsec; |
| 213 | uint64_t vdi_size; |
| 214 | uint64_t vm_state_size; |
| 215 | uint16_t copy_policy; |
| 216 | uint8_t nr_copies; |
| 217 | uint8_t block_size_shift; |
| 218 | uint32_t snap_id; |
| 219 | uint32_t vdi_id; |
| 220 | uint32_t parent_vdi_id; |
| 221 | uint32_t child_vdi_id[MAX_CHILDREN]; |
| 222 | uint32_t data_vdi_id[MAX_DATA_OBJS]; |
| 223 | } SheepdogInode; |
| 224 | |
Hitoshi Mitake | 5d039ba | 2014-06-06 13:35:12 +0900 | [diff] [blame] | 225 | #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id) |
| 226 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 227 | /* |
| 228 | * 64 bit FNV-1a non-zero initial basis |
| 229 | */ |
| 230 | #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) |
| 231 | |
| 232 | /* |
| 233 | * 64 bit Fowler/Noll/Vo FNV-1a hash code |
| 234 | */ |
| 235 | static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval) |
| 236 | { |
| 237 | unsigned char *bp = buf; |
| 238 | unsigned char *be = bp + len; |
| 239 | while (bp < be) { |
| 240 | hval ^= (uint64_t) *bp++; |
| 241 | hval += (hval << 1) + (hval << 4) + (hval << 5) + |
| 242 | (hval << 7) + (hval << 8) + (hval << 40); |
| 243 | } |
| 244 | return hval; |
| 245 | } |
| 246 | |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 247 | static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 248 | { |
| 249 | return inode->vdi_id == inode->data_vdi_id[idx]; |
| 250 | } |
| 251 | |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 252 | static inline bool is_data_obj(uint64_t oid) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 253 | { |
| 254 | return !(VDI_BIT & oid); |
| 255 | } |
| 256 | |
| 257 | static inline uint64_t data_oid_to_idx(uint64_t oid) |
| 258 | { |
| 259 | return oid & (MAX_DATA_OBJS - 1); |
| 260 | } |
| 261 | |
MORITA Kazutaka | 72e0996 | 2013-10-24 16:01:13 +0900 | [diff] [blame] | 262 | static inline uint32_t oid_to_vid(uint64_t oid) |
| 263 | { |
| 264 | return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT; |
| 265 | } |
| 266 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 267 | static inline uint64_t vid_to_vdi_oid(uint32_t vid) |
| 268 | { |
| 269 | return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT); |
| 270 | } |
| 271 | |
| 272 | static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx) |
| 273 | { |
| 274 | return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; |
| 275 | } |
| 276 | |
| 277 | static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx) |
| 278 | { |
| 279 | return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; |
| 280 | } |
| 281 | |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 282 | static inline bool is_snapshot(struct SheepdogInode *inode) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 283 | { |
| 284 | return !!inode->snap_ctime; |
| 285 | } |
| 286 | |
Vasiliy Tolstov | eab8eb8 | 2015-12-23 21:22:26 +0900 | [diff] [blame] | 287 | static inline size_t count_data_objs(const struct SheepdogInode *inode) |
| 288 | { |
| 289 | return DIV_ROUND_UP(inode->vdi_size, |
| 290 | (1UL << inode->block_size_shift)); |
| 291 | } |
| 292 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 293 | #undef DPRINTF |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 294 | #ifdef DEBUG_SDOG |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 295 | #define DPRINTF(fmt, args...) \ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 296 | do { \ |
| 297 | fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \ |
| 298 | } while (0) |
| 299 | #else |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 300 | #define DPRINTF(fmt, args...) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 301 | #endif |
| 302 | |
| 303 | typedef struct SheepdogAIOCB SheepdogAIOCB; |
| 304 | |
| 305 | typedef struct AIOReq { |
| 306 | SheepdogAIOCB *aiocb; |
| 307 | unsigned int iov_offset; |
| 308 | |
| 309 | uint64_t oid; |
| 310 | uint64_t base_oid; |
| 311 | uint64_t offset; |
| 312 | unsigned int data_len; |
| 313 | uint8_t flags; |
| 314 | uint32_t id; |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 315 | bool create; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 316 | |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 317 | QLIST_ENTRY(AIOReq) aio_siblings; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 318 | } AIOReq; |
| 319 | |
| 320 | enum AIOCBState { |
| 321 | AIOCB_WRITE_UDATA, |
| 322 | AIOCB_READ_UDATA, |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 323 | AIOCB_FLUSH_CACHE, |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 324 | AIOCB_DISCARD_OBJ, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 325 | }; |
| 326 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 327 | #define AIOCBOverlapping(x, y) \ |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 328 | (!(x->max_affect_data_idx < y->min_affect_data_idx \ |
| 329 | || y->max_affect_data_idx < x->min_affect_data_idx)) |
| 330 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 331 | struct SheepdogAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 332 | BlockAIOCB common; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 333 | |
| 334 | QEMUIOVector *qiov; |
| 335 | |
| 336 | int64_t sector_num; |
| 337 | int nb_sectors; |
| 338 | |
| 339 | int ret; |
| 340 | enum AIOCBState aiocb_type; |
| 341 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 342 | Coroutine *coroutine; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 343 | void (*aio_done_func)(SheepdogAIOCB *); |
| 344 | |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 345 | bool cancelable; |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 346 | int nr_pending; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 347 | |
| 348 | uint32_t min_affect_data_idx; |
| 349 | uint32_t max_affect_data_idx; |
| 350 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 351 | /* |
| 352 | * The difference between affect_data_idx and dirty_data_idx: |
| 353 | * affect_data_idx represents range of index of all request types. |
| 354 | * dirty_data_idx represents range of index updated by COW requests. |
| 355 | * dirty_data_idx is used for updating an inode object. |
| 356 | */ |
| 357 | uint32_t min_dirty_data_idx; |
| 358 | uint32_t max_dirty_data_idx; |
| 359 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 360 | QLIST_ENTRY(SheepdogAIOCB) aiocb_siblings; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | typedef struct BDRVSheepdogState { |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 364 | BlockDriverState *bs; |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 365 | AioContext *aio_context; |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 366 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 367 | SheepdogInode inode; |
| 368 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 369 | char name[SD_MAX_VDI_LEN]; |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 370 | bool is_snapshot; |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 371 | uint32_t cache_flags; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 372 | bool discard_supported; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 373 | |
MORITA Kazutaka | 25af257 | 2013-02-22 12:39:52 +0900 | [diff] [blame] | 374 | char *host_spec; |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 375 | bool is_unix; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 376 | int fd; |
| 377 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 378 | CoMutex lock; |
| 379 | Coroutine *co_send; |
| 380 | Coroutine *co_recv; |
| 381 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 382 | uint32_t aioreq_seq_num; |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 383 | |
| 384 | /* Every aio request must be linked to either of these queues. */ |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 385 | QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head; |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 386 | QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 387 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 388 | CoQueue overlapping_queue; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 389 | QLIST_HEAD(inflight_aiocb_head, SheepdogAIOCB) inflight_aiocb_head; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 390 | } BDRVSheepdogState; |
| 391 | |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 392 | typedef struct BDRVSheepdogReopenState { |
| 393 | int fd; |
| 394 | int cache_flags; |
| 395 | } BDRVSheepdogReopenState; |
| 396 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 397 | static const char * sd_strerror(int err) |
| 398 | { |
| 399 | int i; |
| 400 | |
| 401 | static const struct { |
| 402 | int err; |
| 403 | const char *desc; |
| 404 | } errors[] = { |
| 405 | {SD_RES_SUCCESS, "Success"}, |
| 406 | {SD_RES_UNKNOWN, "Unknown error"}, |
| 407 | {SD_RES_NO_OBJ, "No object found"}, |
| 408 | {SD_RES_EIO, "I/O error"}, |
| 409 | {SD_RES_VDI_EXIST, "VDI exists already"}, |
| 410 | {SD_RES_INVALID_PARMS, "Invalid parameters"}, |
| 411 | {SD_RES_SYSTEM_ERROR, "System error"}, |
| 412 | {SD_RES_VDI_LOCKED, "VDI is already locked"}, |
| 413 | {SD_RES_NO_VDI, "No vdi found"}, |
| 414 | {SD_RES_NO_BASE_VDI, "No base VDI found"}, |
| 415 | {SD_RES_VDI_READ, "Failed read the requested VDI"}, |
| 416 | {SD_RES_VDI_WRITE, "Failed to write the requested VDI"}, |
| 417 | {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"}, |
| 418 | {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"}, |
| 419 | {SD_RES_NO_TAG, "Failed to find the requested tag"}, |
| 420 | {SD_RES_STARTUP, "The system is still booting"}, |
| 421 | {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"}, |
| 422 | {SD_RES_SHUTDOWN, "The system is shutting down"}, |
| 423 | {SD_RES_NO_MEM, "Out of memory on the server"}, |
| 424 | {SD_RES_FULL_VDI, "We already have the maximum vdis"}, |
| 425 | {SD_RES_VER_MISMATCH, "Protocol version mismatch"}, |
| 426 | {SD_RES_NO_SPACE, "Server has no space for new objects"}, |
| 427 | {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"}, |
| 428 | {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"}, |
| 429 | {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"}, |
Liu Yuan | fca23f0 | 2013-03-18 14:27:55 +0800 | [diff] [blame] | 430 | {SD_RES_HALT, "Sheepdog is stopped serving IO request"}, |
MORITA Kazutaka | 6a0b549 | 2013-04-26 01:19:52 +0900 | [diff] [blame] | 431 | {SD_RES_READONLY, "Object is read-only"}, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | for (i = 0; i < ARRAY_SIZE(errors); ++i) { |
| 435 | if (errors[i].err == err) { |
| 436 | return errors[i].desc; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return "Invalid error code"; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Sheepdog I/O handling: |
| 445 | * |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 446 | * 1. In sd_co_rw_vector, we send the I/O requests to the server and |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 447 | * link the requests to the inflight_list in the |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 448 | * BDRVSheepdogState. The function exits without waiting for |
| 449 | * receiving the response. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 450 | * |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 451 | * 2. We receive the response in aio_read_response, the fd handler to |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 452 | * the sheepdog connection. If metadata update is needed, we send |
| 453 | * the write request to the vdi object in sd_write_done, the write |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 454 | * completion function. We switch back to sd_co_readv/writev after |
| 455 | * all the requests belonging to the AIOCB are finished. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 456 | */ |
| 457 | |
| 458 | static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, |
| 459 | uint64_t oid, unsigned int data_len, |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 460 | uint64_t offset, uint8_t flags, bool create, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 461 | uint64_t base_oid, unsigned int iov_offset) |
| 462 | { |
| 463 | AIOReq *aio_req; |
| 464 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 465 | aio_req = g_malloc(sizeof(*aio_req)); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 466 | aio_req->aiocb = acb; |
| 467 | aio_req->iov_offset = iov_offset; |
| 468 | aio_req->oid = oid; |
| 469 | aio_req->base_oid = base_oid; |
| 470 | aio_req->offset = offset; |
| 471 | aio_req->data_len = data_len; |
| 472 | aio_req->flags = flags; |
| 473 | aio_req->id = s->aioreq_seq_num++; |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 474 | aio_req->create = create; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 475 | |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 476 | acb->nr_pending++; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 477 | return aio_req; |
| 478 | } |
| 479 | |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 480 | static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 481 | { |
| 482 | SheepdogAIOCB *acb = aio_req->aiocb; |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 483 | |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 484 | acb->cancelable = false; |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 485 | QLIST_REMOVE(aio_req, aio_siblings); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 486 | g_free(aio_req); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 487 | |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 488 | acb->nr_pending--; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 489 | } |
| 490 | |
Paolo Bonzini | d8716b4 | 2011-10-05 09:17:31 +0200 | [diff] [blame] | 491 | static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 492 | { |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 493 | qemu_coroutine_enter(acb->coroutine, NULL); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 494 | qemu_aio_unref(acb); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 495 | } |
| 496 | |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 497 | /* |
| 498 | * Check whether the specified acb can be canceled |
| 499 | * |
| 500 | * We can cancel aio when any request belonging to the acb is: |
| 501 | * - Not processed by the sheepdog server. |
| 502 | * - Not linked to the inflight queue. |
| 503 | */ |
| 504 | static bool sd_acb_cancelable(const SheepdogAIOCB *acb) |
| 505 | { |
| 506 | BDRVSheepdogState *s = acb->common.bs->opaque; |
| 507 | AIOReq *aioreq; |
| 508 | |
| 509 | if (!acb->cancelable) { |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) { |
| 514 | if (aioreq->aiocb == acb) { |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return true; |
| 520 | } |
| 521 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 522 | static void sd_aio_cancel(BlockAIOCB *blockacb) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 523 | { |
| 524 | SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb; |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 525 | BDRVSheepdogState *s = acb->common.bs->opaque; |
| 526 | AIOReq *aioreq, *next; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 527 | |
Fam Zheng | 6d24b4d | 2014-09-11 13:41:24 +0800 | [diff] [blame] | 528 | if (sd_acb_cancelable(acb)) { |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 529 | /* Remove outstanding requests from failed queue. */ |
Fam Zheng | 6d24b4d | 2014-09-11 13:41:24 +0800 | [diff] [blame] | 530 | QLIST_FOREACH_SAFE(aioreq, &s->failed_aio_head, aio_siblings, |
| 531 | next) { |
| 532 | if (aioreq->aiocb == acb) { |
| 533 | free_aio_req(s, aioreq); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | assert(acb->nr_pending == 0); |
| 538 | if (acb->common.cb) { |
| 539 | acb->common.cb(acb->common.opaque, -ECANCELED); |
| 540 | } |
| 541 | sd_finish_aiocb(acb); |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 542 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 543 | } |
| 544 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 545 | static const AIOCBInfo sd_aiocb_info = { |
Fam Zheng | 6d24b4d | 2014-09-11 13:41:24 +0800 | [diff] [blame] | 546 | .aiocb_size = sizeof(SheepdogAIOCB), |
| 547 | .cancel_async = sd_aio_cancel, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 548 | }; |
| 549 | |
| 550 | static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, |
Liu Yuan | f700f8e | 2013-01-14 14:01:03 +0800 | [diff] [blame] | 551 | int64_t sector_num, int nb_sectors) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 552 | { |
| 553 | SheepdogAIOCB *acb; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 554 | uint32_t object_size; |
| 555 | BDRVSheepdogState *s = bs->opaque; |
| 556 | |
| 557 | object_size = (UINT32_C(1) << s->inode.block_size_shift); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 558 | |
Liu Yuan | f700f8e | 2013-01-14 14:01:03 +0800 | [diff] [blame] | 559 | acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 560 | |
| 561 | acb->qiov = qiov; |
| 562 | |
| 563 | acb->sector_num = sector_num; |
| 564 | acb->nb_sectors = nb_sectors; |
| 565 | |
| 566 | acb->aio_done_func = NULL; |
MORITA Kazutaka | 3520068 | 2013-10-24 16:01:17 +0900 | [diff] [blame] | 567 | acb->cancelable = true; |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 568 | acb->coroutine = qemu_coroutine_self(); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 569 | acb->ret = 0; |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 570 | acb->nr_pending = 0; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 571 | |
| 572 | acb->min_affect_data_idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size; |
| 573 | acb->max_affect_data_idx = (acb->sector_num * BDRV_SECTOR_SIZE + |
| 574 | acb->nb_sectors * BDRV_SECTOR_SIZE) / object_size; |
| 575 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 576 | acb->min_dirty_data_idx = UINT32_MAX; |
| 577 | acb->max_dirty_data_idx = 0; |
| 578 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 579 | return acb; |
| 580 | } |
| 581 | |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 582 | /* Return -EIO in case of error, file descriptor on success */ |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 583 | static int connect_to_sdog(BDRVSheepdogState *s, Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 584 | { |
MORITA Kazutaka | 25af257 | 2013-02-22 12:39:52 +0900 | [diff] [blame] | 585 | int fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 586 | |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 587 | if (s->is_unix) { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 588 | fd = unix_connect(s->host_spec, errp); |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 589 | } else { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 590 | fd = inet_connect(s->host_spec, errp); |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 591 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 592 | if (fd >= 0) { |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 593 | int ret = socket_set_nodelay(fd); |
| 594 | if (ret < 0) { |
| 595 | error_report("%s", strerror(errno)); |
| 596 | } |
| 597 | } |
| 598 | } |
MORITA Kazutaka | 25af257 | 2013-02-22 12:39:52 +0900 | [diff] [blame] | 599 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 600 | if (fd >= 0) { |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 601 | qemu_set_nonblock(fd); |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 602 | } else { |
| 603 | fd = -EIO; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 604 | } |
| 605 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 606 | return fd; |
| 607 | } |
| 608 | |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 609 | /* Return 0 on success and -errno in case of error */ |
MORITA Kazutaka | e0d93a8 | 2012-05-30 09:03:55 +0900 | [diff] [blame] | 610 | static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, |
| 611 | unsigned int *wlen) |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 612 | { |
| 613 | int ret; |
| 614 | |
| 615 | ret = qemu_co_send(sockfd, hdr, sizeof(*hdr)); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 616 | if (ret != sizeof(*hdr)) { |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 617 | error_report("failed to send a req, %s", strerror(errno)); |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 618 | ret = -socket_error(); |
Liu Yuan | eb09218 | 2012-04-03 18:04:21 +0800 | [diff] [blame] | 619 | return ret; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | ret = qemu_co_send(sockfd, data, *wlen); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 623 | if (ret != *wlen) { |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 624 | ret = -socket_error(); |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 625 | error_report("failed to send a req, %s", strerror(errno)); |
| 626 | } |
| 627 | |
| 628 | return ret; |
| 629 | } |
MORITA Kazutaka | e0d93a8 | 2012-05-30 09:03:55 +0900 | [diff] [blame] | 630 | |
MORITA Kazutaka | 2dfcca3 | 2012-06-27 07:26:19 +0900 | [diff] [blame] | 631 | static void restart_co_req(void *opaque) |
| 632 | { |
| 633 | Coroutine *co = opaque; |
| 634 | |
| 635 | qemu_coroutine_enter(co, NULL); |
| 636 | } |
| 637 | |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 638 | typedef struct SheepdogReqCo { |
| 639 | int sockfd; |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 640 | AioContext *aio_context; |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 641 | SheepdogReq *hdr; |
| 642 | void *data; |
| 643 | unsigned int *wlen; |
| 644 | unsigned int *rlen; |
| 645 | int ret; |
| 646 | bool finished; |
| 647 | } SheepdogReqCo; |
| 648 | |
| 649 | static coroutine_fn void do_co_req(void *opaque) |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 650 | { |
| 651 | int ret; |
MORITA Kazutaka | 2dfcca3 | 2012-06-27 07:26:19 +0900 | [diff] [blame] | 652 | Coroutine *co; |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 653 | SheepdogReqCo *srco = opaque; |
| 654 | int sockfd = srco->sockfd; |
| 655 | SheepdogReq *hdr = srco->hdr; |
| 656 | void *data = srco->data; |
| 657 | unsigned int *wlen = srco->wlen; |
| 658 | unsigned int *rlen = srco->rlen; |
MORITA Kazutaka | 2dfcca3 | 2012-06-27 07:26:19 +0900 | [diff] [blame] | 659 | |
| 660 | co = qemu_coroutine_self(); |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 661 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
| 662 | NULL, restart_co_req, co); |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 663 | |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 664 | ret = send_co_req(sockfd, hdr, data, wlen); |
| 665 | if (ret < 0) { |
| 666 | goto out; |
| 667 | } |
| 668 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 669 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
| 670 | restart_co_req, NULL, co); |
MORITA Kazutaka | 2dfcca3 | 2012-06-27 07:26:19 +0900 | [diff] [blame] | 671 | |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 672 | ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr)); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 673 | if (ret != sizeof(*hdr)) { |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 674 | error_report("failed to get a rsp, %s", strerror(errno)); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 675 | ret = -errno; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 676 | goto out; |
| 677 | } |
| 678 | |
| 679 | if (*rlen > hdr->data_length) { |
| 680 | *rlen = hdr->data_length; |
| 681 | } |
| 682 | |
| 683 | if (*rlen) { |
| 684 | ret = qemu_co_recv(sockfd, data, *rlen); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 685 | if (ret != *rlen) { |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 686 | error_report("failed to get the data, %s", strerror(errno)); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 687 | ret = -errno; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 688 | goto out; |
| 689 | } |
| 690 | } |
| 691 | ret = 0; |
| 692 | out: |
MORITA Kazutaka | ed9ba72 | 2013-03-12 16:05:43 +0900 | [diff] [blame] | 693 | /* there is at most one request for this sockfd, so it is safe to |
| 694 | * set each handler to NULL. */ |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 695 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
| 696 | NULL, NULL, NULL); |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 697 | |
| 698 | srco->ret = ret; |
| 699 | srco->finished = true; |
| 700 | } |
| 701 | |
Liu Yuan | 833a7cc | 2015-02-18 11:57:55 +0800 | [diff] [blame] | 702 | /* |
| 703 | * Send the request to the sheep in a synchronous manner. |
| 704 | * |
| 705 | * Return 0 on success, -errno in case of error. |
| 706 | */ |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 707 | static int do_req(int sockfd, AioContext *aio_context, SheepdogReq *hdr, |
| 708 | void *data, unsigned int *wlen, unsigned int *rlen) |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 709 | { |
| 710 | Coroutine *co; |
| 711 | SheepdogReqCo srco = { |
| 712 | .sockfd = sockfd, |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 713 | .aio_context = aio_context, |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 714 | .hdr = hdr, |
| 715 | .data = data, |
| 716 | .wlen = wlen, |
| 717 | .rlen = rlen, |
| 718 | .ret = 0, |
| 719 | .finished = false, |
| 720 | }; |
| 721 | |
| 722 | if (qemu_in_coroutine()) { |
| 723 | do_co_req(&srco); |
| 724 | } else { |
| 725 | co = qemu_coroutine_create(do_co_req); |
| 726 | qemu_coroutine_enter(co, &srco); |
| 727 | while (!srco.finished) { |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 728 | aio_poll(aio_context, true); |
MORITA Kazutaka | cddd4ac | 2012-07-05 01:41:06 +0900 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | return srco.ret; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 733 | } |
| 734 | |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 735 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 736 | struct iovec *iov, int niov, |
| 737 | enum AIOCBState aiocb_type); |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 738 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); |
MORITA Kazutaka | 72e0996 | 2013-10-24 16:01:13 +0900 | [diff] [blame] | 739 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag); |
Markus Armbruster | 356b4ca | 2014-05-16 11:00:20 +0200 | [diff] [blame] | 740 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 741 | static void co_write_request(void *opaque); |
MORITA Kazutaka | 7dc1cde | 2012-06-27 07:26:23 +0900 | [diff] [blame] | 742 | |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 743 | static coroutine_fn void reconnect_to_sdog(void *opaque) |
| 744 | { |
| 745 | BDRVSheepdogState *s = opaque; |
| 746 | AIOReq *aio_req, *next; |
| 747 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 748 | aio_set_fd_handler(s->aio_context, s->fd, false, NULL, |
| 749 | NULL, NULL); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 750 | close(s->fd); |
| 751 | s->fd = -1; |
| 752 | |
| 753 | /* Wait for outstanding write requests to be completed. */ |
| 754 | while (s->co_send != NULL) { |
| 755 | co_write_request(opaque); |
| 756 | } |
| 757 | |
| 758 | /* Try to reconnect the sheepdog server every one second. */ |
| 759 | while (s->fd < 0) { |
Liu Yuan | a780dea | 2014-08-28 18:27:55 +0800 | [diff] [blame] | 760 | Error *local_err = NULL; |
Markus Armbruster | 356b4ca | 2014-05-16 11:00:20 +0200 | [diff] [blame] | 761 | s->fd = get_sheep_fd(s, &local_err); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 762 | if (s->fd < 0) { |
| 763 | DPRINTF("Wait for connection to be established\n"); |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 764 | error_report_err(local_err); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 765 | co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME, |
| 766 | 1000000000ULL); |
| 767 | } |
| 768 | }; |
| 769 | |
| 770 | /* |
| 771 | * Now we have to resend all the request in the inflight queue. However, |
| 772 | * resend_aioreq() can yield and newly created requests can be added to the |
| 773 | * inflight queue before the coroutine is resumed. To avoid mixing them, we |
| 774 | * have to move all the inflight requests to the failed queue before |
| 775 | * resend_aioreq() is called. |
| 776 | */ |
| 777 | QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) { |
| 778 | QLIST_REMOVE(aio_req, aio_siblings); |
| 779 | QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings); |
| 780 | } |
| 781 | |
| 782 | /* Resend all the failed aio requests. */ |
| 783 | while (!QLIST_EMPTY(&s->failed_aio_head)) { |
| 784 | aio_req = QLIST_FIRST(&s->failed_aio_head); |
| 785 | QLIST_REMOVE(aio_req, aio_siblings); |
| 786 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
| 787 | resend_aioreq(s, aio_req); |
| 788 | } |
| 789 | } |
| 790 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 791 | /* |
| 792 | * Receive responses of the I/O requests. |
| 793 | * |
| 794 | * This function is registered as a fd handler, and called from the |
| 795 | * main loop when s->fd is ready for reading responses. |
| 796 | */ |
Paolo Bonzini | d8716b4 | 2011-10-05 09:17:31 +0200 | [diff] [blame] | 797 | static void coroutine_fn aio_read_response(void *opaque) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 798 | { |
| 799 | SheepdogObjRsp rsp; |
| 800 | BDRVSheepdogState *s = opaque; |
| 801 | int fd = s->fd; |
| 802 | int ret; |
| 803 | AIOReq *aio_req = NULL; |
| 804 | SheepdogAIOCB *acb; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 805 | uint64_t idx; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 806 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 807 | /* read a header */ |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 808 | ret = qemu_co_recv(fd, &rsp, sizeof(rsp)); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 809 | if (ret != sizeof(rsp)) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 810 | error_report("failed to get the header, %s", strerror(errno)); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 811 | goto err; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 812 | } |
| 813 | |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 814 | /* find the right aio_req from the inflight aio list */ |
| 815 | QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 816 | if (aio_req->id == rsp.id) { |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | if (!aio_req) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 821 | error_report("cannot find aio_req %x", rsp.id); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 822 | goto err; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | acb = aio_req->aiocb; |
| 826 | |
| 827 | switch (acb->aiocb_type) { |
| 828 | case AIOCB_WRITE_UDATA: |
MORITA Kazutaka | 6d1acda | 2012-01-31 02:10:06 +0900 | [diff] [blame] | 829 | /* this coroutine context is no longer suitable for co_recv |
| 830 | * because we may send data to update vdi objects */ |
| 831 | s->co_recv = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 832 | if (!is_data_obj(aio_req->oid)) { |
| 833 | break; |
| 834 | } |
| 835 | idx = data_oid_to_idx(aio_req->oid); |
| 836 | |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 837 | if (aio_req->create) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 838 | /* |
| 839 | * If the object is newly created one, we need to update |
| 840 | * the vdi object (metadata object). min_dirty_data_idx |
| 841 | * and max_dirty_data_idx are changed to include updated |
| 842 | * index between them. |
| 843 | */ |
Liu Yuan | bd751f2 | 2012-12-17 14:17:26 +0800 | [diff] [blame] | 844 | if (rsp.result == SD_RES_SUCCESS) { |
| 845 | s->inode.data_vdi_id[idx] = s->inode.vdi_id; |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 846 | acb->max_dirty_data_idx = MAX(idx, acb->max_dirty_data_idx); |
| 847 | acb->min_dirty_data_idx = MIN(idx, acb->min_dirty_data_idx); |
Liu Yuan | bd751f2 | 2012-12-17 14:17:26 +0800 | [diff] [blame] | 848 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 849 | } |
| 850 | break; |
| 851 | case AIOCB_READ_UDATA: |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 852 | ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov, |
| 853 | aio_req->iov_offset, rsp.data_length); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 854 | if (ret != rsp.data_length) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 855 | error_report("failed to get the data, %s", strerror(errno)); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 856 | goto err; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 857 | } |
| 858 | break; |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 859 | case AIOCB_FLUSH_CACHE: |
| 860 | if (rsp.result == SD_RES_INVALID_PARMS) { |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 861 | DPRINTF("disable cache since the server doesn't support it\n"); |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 862 | s->cache_flags = SD_FLAG_CMD_DIRECT; |
| 863 | rsp.result = SD_RES_SUCCESS; |
| 864 | } |
| 865 | break; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 866 | case AIOCB_DISCARD_OBJ: |
| 867 | switch (rsp.result) { |
| 868 | case SD_RES_INVALID_PARMS: |
| 869 | error_report("sheep(%s) doesn't support discard command", |
| 870 | s->host_spec); |
| 871 | rsp.result = SD_RES_SUCCESS; |
| 872 | s->discard_supported = false; |
| 873 | break; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 874 | default: |
| 875 | break; |
| 876 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 877 | } |
| 878 | |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 879 | switch (rsp.result) { |
| 880 | case SD_RES_SUCCESS: |
| 881 | break; |
| 882 | case SD_RES_READONLY: |
MORITA Kazutaka | 72e0996 | 2013-10-24 16:01:13 +0900 | [diff] [blame] | 883 | if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) { |
| 884 | ret = reload_inode(s, 0, ""); |
| 885 | if (ret < 0) { |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 886 | goto err; |
MORITA Kazutaka | 72e0996 | 2013-10-24 16:01:13 +0900 | [diff] [blame] | 887 | } |
| 888 | } |
MORITA Kazutaka | 72e0996 | 2013-10-24 16:01:13 +0900 | [diff] [blame] | 889 | if (is_data_obj(aio_req->oid)) { |
| 890 | aio_req->oid = vid_to_data_oid(s->inode.vdi_id, |
| 891 | data_oid_to_idx(aio_req->oid)); |
| 892 | } else { |
| 893 | aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id); |
| 894 | } |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 895 | resend_aioreq(s, aio_req); |
| 896 | goto out; |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 897 | default: |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 898 | acb->ret = -EIO; |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 899 | error_report("%s", sd_strerror(rsp.result)); |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 900 | break; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 901 | } |
| 902 | |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 903 | free_aio_req(s, aio_req); |
| 904 | if (!acb->nr_pending) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 905 | /* |
| 906 | * We've finished all requests which belong to the AIOCB, so |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 907 | * we can switch back to sd_co_readv/writev now. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 908 | */ |
| 909 | acb->aio_done_func(acb); |
| 910 | } |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 911 | out: |
| 912 | s->co_recv = NULL; |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 913 | return; |
| 914 | err: |
| 915 | s->co_recv = NULL; |
| 916 | reconnect_to_sdog(opaque); |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static void co_read_response(void *opaque) |
| 920 | { |
| 921 | BDRVSheepdogState *s = opaque; |
| 922 | |
| 923 | if (!s->co_recv) { |
| 924 | s->co_recv = qemu_coroutine_create(aio_read_response); |
| 925 | } |
| 926 | |
| 927 | qemu_coroutine_enter(s->co_recv, opaque); |
| 928 | } |
| 929 | |
| 930 | static void co_write_request(void *opaque) |
| 931 | { |
| 932 | BDRVSheepdogState *s = opaque; |
| 933 | |
| 934 | qemu_coroutine_enter(s->co_send, NULL); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 935 | } |
| 936 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 937 | /* |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 938 | * Return a socket descriptor to read/write objects. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 939 | * |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 940 | * We cannot use this descriptor for other operations because |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 941 | * the block driver may be on waiting response from the server. |
| 942 | */ |
Markus Armbruster | 356b4ca | 2014-05-16 11:00:20 +0200 | [diff] [blame] | 943 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 944 | { |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 945 | int fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 946 | |
Markus Armbruster | 356b4ca | 2014-05-16 11:00:20 +0200 | [diff] [blame] | 947 | fd = connect_to_sdog(s, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 948 | if (fd < 0) { |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 949 | return fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 950 | } |
| 951 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 952 | aio_set_fd_handler(s->aio_context, fd, false, |
| 953 | co_read_response, NULL, s); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 954 | return fd; |
| 955 | } |
| 956 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 957 | static int sd_parse_uri(BDRVSheepdogState *s, const char *filename, |
| 958 | char *vdi, uint32_t *snapid, char *tag) |
| 959 | { |
| 960 | URI *uri; |
| 961 | QueryParams *qp = NULL; |
| 962 | int ret = 0; |
| 963 | |
| 964 | uri = uri_parse(filename); |
| 965 | if (!uri) { |
| 966 | return -EINVAL; |
| 967 | } |
| 968 | |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 969 | /* transport */ |
| 970 | if (!strcmp(uri->scheme, "sheepdog")) { |
| 971 | s->is_unix = false; |
| 972 | } else if (!strcmp(uri->scheme, "sheepdog+tcp")) { |
| 973 | s->is_unix = false; |
| 974 | } else if (!strcmp(uri->scheme, "sheepdog+unix")) { |
| 975 | s->is_unix = true; |
| 976 | } else { |
| 977 | ret = -EINVAL; |
| 978 | goto out; |
| 979 | } |
| 980 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 981 | if (uri->path == NULL || !strcmp(uri->path, "/")) { |
| 982 | ret = -EINVAL; |
| 983 | goto out; |
| 984 | } |
| 985 | pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1); |
| 986 | |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 987 | qp = query_params_parse(uri->query); |
| 988 | if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) { |
| 989 | ret = -EINVAL; |
| 990 | goto out; |
| 991 | } |
| 992 | |
| 993 | if (s->is_unix) { |
| 994 | /* sheepdog+unix:///vdiname?socket=path */ |
| 995 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { |
| 996 | ret = -EINVAL; |
| 997 | goto out; |
| 998 | } |
| 999 | s->host_spec = g_strdup(qp->p[0].value); |
| 1000 | } else { |
| 1001 | /* sheepdog[+tcp]://[host:port]/vdiname */ |
| 1002 | s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR, |
| 1003 | uri->port ?: SD_DEFAULT_PORT); |
| 1004 | } |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1005 | |
| 1006 | /* snapshot tag */ |
| 1007 | if (uri->fragment) { |
| 1008 | *snapid = strtoul(uri->fragment, NULL, 10); |
| 1009 | if (*snapid == 0) { |
| 1010 | pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment); |
| 1011 | } |
| 1012 | } else { |
| 1013 | *snapid = CURRENT_VDI_ID; /* search current vdi */ |
| 1014 | } |
| 1015 | |
| 1016 | out: |
| 1017 | if (qp) { |
| 1018 | query_params_free(qp); |
| 1019 | } |
| 1020 | uri_free(uri); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1024 | /* |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1025 | * Parse a filename (old syntax) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1026 | * |
| 1027 | * filename must be one of the following formats: |
| 1028 | * 1. [vdiname] |
| 1029 | * 2. [vdiname]:[snapid] |
| 1030 | * 3. [vdiname]:[tag] |
| 1031 | * 4. [hostname]:[port]:[vdiname] |
| 1032 | * 5. [hostname]:[port]:[vdiname]:[snapid] |
| 1033 | * 6. [hostname]:[port]:[vdiname]:[tag] |
| 1034 | * |
| 1035 | * You can boot from the snapshot images by specifying `snapid` or |
| 1036 | * `tag'. |
| 1037 | * |
| 1038 | * You can run VMs outside the Sheepdog cluster by specifying |
| 1039 | * `hostname' and `port' (experimental). |
| 1040 | */ |
| 1041 | static int parse_vdiname(BDRVSheepdogState *s, const char *filename, |
| 1042 | char *vdi, uint32_t *snapid, char *tag) |
| 1043 | { |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1044 | char *p, *q, *uri; |
| 1045 | const char *host_spec, *vdi_spec; |
| 1046 | int nr_sep, ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1047 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1048 | strstart(filename, "sheepdog:", (const char **)&filename); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1049 | p = q = g_strdup(filename); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1050 | |
| 1051 | /* count the number of separators */ |
| 1052 | nr_sep = 0; |
| 1053 | while (*p) { |
| 1054 | if (*p == ':') { |
| 1055 | nr_sep++; |
| 1056 | } |
| 1057 | p++; |
| 1058 | } |
| 1059 | p = q; |
| 1060 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1061 | /* use the first two tokens as host_spec. */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1062 | if (nr_sep >= 2) { |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1063 | host_spec = p; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1064 | p = strchr(p, ':'); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1065 | p++; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1066 | p = strchr(p, ':'); |
| 1067 | *p++ = '\0'; |
| 1068 | } else { |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1069 | host_spec = ""; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1070 | } |
| 1071 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1072 | vdi_spec = p; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1073 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1074 | p = strchr(vdi_spec, ':'); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1075 | if (p) { |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1076 | *p++ = '#'; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1077 | } |
| 1078 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1079 | uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1080 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1081 | ret = sd_parse_uri(s, uri, vdi, snapid, tag); |
| 1082 | |
| 1083 | g_free(q); |
| 1084 | g_free(uri); |
| 1085 | |
| 1086 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1087 | } |
| 1088 | |
MORITA Kazutaka | 982dcbf | 2013-04-26 01:19:51 +0900 | [diff] [blame] | 1089 | static int find_vdi_name(BDRVSheepdogState *s, const char *filename, |
| 1090 | uint32_t snapid, const char *tag, uint32_t *vid, |
Markus Armbruster | dc83cd4 | 2014-05-16 11:00:23 +0200 | [diff] [blame] | 1091 | bool lock, Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1092 | { |
| 1093 | int ret, fd; |
| 1094 | SheepdogVdiReq hdr; |
| 1095 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; |
| 1096 | unsigned int wlen, rlen = 0; |
| 1097 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; |
| 1098 | |
Markus Armbruster | dc83cd4 | 2014-05-16 11:00:23 +0200 | [diff] [blame] | 1099 | fd = connect_to_sdog(s, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1100 | if (fd < 0) { |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1101 | return fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1102 | } |
| 1103 | |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 1104 | /* This pair of strncpy calls ensures that the buffer is zero-filled, |
| 1105 | * which is desirable since we'll soon be sending those bytes, and |
| 1106 | * don't want the send_req to read uninitialized data. |
| 1107 | */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1108 | strncpy(buf, filename, SD_MAX_VDI_LEN); |
| 1109 | strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN); |
| 1110 | |
| 1111 | memset(&hdr, 0, sizeof(hdr)); |
MORITA Kazutaka | 982dcbf | 2013-04-26 01:19:51 +0900 | [diff] [blame] | 1112 | if (lock) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1113 | hdr.opcode = SD_OP_LOCK_VDI; |
Hitoshi Mitake | 1dbfafe | 2014-08-11 14:43:45 +0900 | [diff] [blame] | 1114 | hdr.type = LOCK_TYPE_NORMAL; |
MORITA Kazutaka | 982dcbf | 2013-04-26 01:19:51 +0900 | [diff] [blame] | 1115 | } else { |
| 1116 | hdr.opcode = SD_OP_GET_VDI_INFO; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1117 | } |
| 1118 | wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN; |
| 1119 | hdr.proto_ver = SD_PROTO_VER; |
| 1120 | hdr.data_length = wlen; |
| 1121 | hdr.snapid = snapid; |
| 1122 | hdr.flags = SD_FLAG_CMD_WRITE; |
| 1123 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1124 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1125 | if (ret) { |
Markus Armbruster | dc83cd4 | 2014-05-16 11:00:23 +0200 | [diff] [blame] | 1126 | error_setg_errno(errp, -ret, "cannot get vdi info"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1127 | goto out; |
| 1128 | } |
| 1129 | |
| 1130 | if (rsp->result != SD_RES_SUCCESS) { |
Markus Armbruster | dc83cd4 | 2014-05-16 11:00:23 +0200 | [diff] [blame] | 1131 | error_setg(errp, "cannot get vdi info, %s, %s %" PRIu32 " %s", |
| 1132 | sd_strerror(rsp->result), filename, snapid, tag); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1133 | if (rsp->result == SD_RES_NO_VDI) { |
| 1134 | ret = -ENOENT; |
Hitoshi Mitake | 38890b2 | 2014-08-11 14:43:46 +0900 | [diff] [blame] | 1135 | } else if (rsp->result == SD_RES_VDI_LOCKED) { |
| 1136 | ret = -EBUSY; |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1137 | } else { |
| 1138 | ret = -EIO; |
| 1139 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1140 | goto out; |
| 1141 | } |
| 1142 | *vid = rsp->vdi_id; |
| 1143 | |
| 1144 | ret = 0; |
| 1145 | out: |
| 1146 | closesocket(fd); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 1150 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1151 | struct iovec *iov, int niov, |
| 1152 | enum AIOCBState aiocb_type) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1153 | { |
| 1154 | int nr_copies = s->inode.nr_copies; |
| 1155 | SheepdogObjReq hdr; |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 1156 | unsigned int wlen = 0; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1157 | int ret; |
| 1158 | uint64_t oid = aio_req->oid; |
| 1159 | unsigned int datalen = aio_req->data_len; |
| 1160 | uint64_t offset = aio_req->offset; |
| 1161 | uint8_t flags = aio_req->flags; |
| 1162 | uint64_t old_oid = aio_req->base_oid; |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1163 | bool create = aio_req->create; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1164 | |
| 1165 | if (!nr_copies) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1166 | error_report("bug"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | memset(&hdr, 0, sizeof(hdr)); |
| 1170 | |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 1171 | switch (aiocb_type) { |
| 1172 | case AIOCB_FLUSH_CACHE: |
| 1173 | hdr.opcode = SD_OP_FLUSH_VDI; |
| 1174 | break; |
| 1175 | case AIOCB_READ_UDATA: |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1176 | hdr.opcode = SD_OP_READ_OBJ; |
| 1177 | hdr.flags = flags; |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 1178 | break; |
| 1179 | case AIOCB_WRITE_UDATA: |
| 1180 | if (create) { |
| 1181 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; |
| 1182 | } else { |
| 1183 | hdr.opcode = SD_OP_WRITE_OBJ; |
| 1184 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1185 | wlen = datalen; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1186 | hdr.flags = SD_FLAG_CMD_WRITE | flags; |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 1187 | break; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 1188 | case AIOCB_DISCARD_OBJ: |
Hitoshi Mitake | e6fd57e | 2015-09-01 12:03:10 +0900 | [diff] [blame] | 1189 | hdr.opcode = SD_OP_WRITE_OBJ; |
| 1190 | hdr.flags = SD_FLAG_CMD_WRITE | flags; |
| 1191 | s->inode.data_vdi_id[data_oid_to_idx(oid)] = 0; |
| 1192 | offset = offsetof(SheepdogInode, |
| 1193 | data_vdi_id[data_oid_to_idx(oid)]); |
| 1194 | oid = vid_to_vdi_oid(s->inode.vdi_id); |
| 1195 | wlen = datalen = sizeof(uint32_t); |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 1196 | break; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1197 | } |
| 1198 | |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1199 | if (s->cache_flags) { |
| 1200 | hdr.flags |= s->cache_flags; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 1201 | } |
| 1202 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1203 | hdr.oid = oid; |
| 1204 | hdr.cow_oid = old_oid; |
| 1205 | hdr.copies = s->inode.nr_copies; |
| 1206 | |
| 1207 | hdr.data_length = datalen; |
| 1208 | hdr.offset = offset; |
| 1209 | |
| 1210 | hdr.id = aio_req->id; |
| 1211 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 1212 | qemu_co_mutex_lock(&s->lock); |
| 1213 | s->co_send = qemu_coroutine_self(); |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1214 | aio_set_fd_handler(s->aio_context, s->fd, false, |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1215 | co_read_response, co_write_request, s); |
Paolo Bonzini | 128aa58 | 2011-09-21 12:36:48 +0200 | [diff] [blame] | 1216 | socket_set_cork(s->fd, 1); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1217 | |
| 1218 | /* send a header */ |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 1219 | ret = qemu_co_send(s->fd, &hdr, sizeof(hdr)); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 1220 | if (ret != sizeof(hdr)) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1221 | error_report("failed to send a req, %s", strerror(errno)); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1222 | goto out; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | if (wlen) { |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 1226 | ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen); |
MORITA Kazutaka | 80731d9 | 2013-10-24 16:01:11 +0900 | [diff] [blame] | 1227 | if (ret != wlen) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1228 | error_report("failed to send a data, %s", strerror(errno)); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1229 | } |
| 1230 | } |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1231 | out: |
Paolo Bonzini | 128aa58 | 2011-09-21 12:36:48 +0200 | [diff] [blame] | 1232 | socket_set_cork(s->fd, 0); |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1233 | aio_set_fd_handler(s->aio_context, s->fd, false, |
| 1234 | co_read_response, NULL, s); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1235 | s->co_send = NULL; |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 1236 | qemu_co_mutex_unlock(&s->lock); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1237 | } |
| 1238 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1239 | static int read_write_object(int fd, AioContext *aio_context, char *buf, |
| 1240 | uint64_t oid, uint8_t copies, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1241 | unsigned int datalen, uint64_t offset, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1242 | bool write, bool create, uint32_t cache_flags) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1243 | { |
| 1244 | SheepdogObjReq hdr; |
| 1245 | SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr; |
| 1246 | unsigned int wlen, rlen; |
| 1247 | int ret; |
| 1248 | |
| 1249 | memset(&hdr, 0, sizeof(hdr)); |
| 1250 | |
| 1251 | if (write) { |
| 1252 | wlen = datalen; |
| 1253 | rlen = 0; |
| 1254 | hdr.flags = SD_FLAG_CMD_WRITE; |
| 1255 | if (create) { |
| 1256 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; |
| 1257 | } else { |
| 1258 | hdr.opcode = SD_OP_WRITE_OBJ; |
| 1259 | } |
| 1260 | } else { |
| 1261 | wlen = 0; |
| 1262 | rlen = datalen; |
| 1263 | hdr.opcode = SD_OP_READ_OBJ; |
| 1264 | } |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 1265 | |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1266 | hdr.flags |= cache_flags; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 1267 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1268 | hdr.oid = oid; |
| 1269 | hdr.data_length = datalen; |
| 1270 | hdr.offset = offset; |
| 1271 | hdr.copies = copies; |
| 1272 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1273 | ret = do_req(fd, aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1274 | if (ret) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1275 | error_report("failed to send a request to the sheep"); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1276 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | switch (rsp->result) { |
| 1280 | case SD_RES_SUCCESS: |
| 1281 | return 0; |
| 1282 | default: |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1283 | error_report("%s", sd_strerror(rsp->result)); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1284 | return -EIO; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1288 | static int read_object(int fd, AioContext *aio_context, char *buf, |
| 1289 | uint64_t oid, uint8_t copies, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1290 | unsigned int datalen, uint64_t offset, |
| 1291 | uint32_t cache_flags) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1292 | { |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1293 | return read_write_object(fd, aio_context, buf, oid, copies, |
| 1294 | datalen, offset, false, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1295 | false, cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1296 | } |
| 1297 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1298 | static int write_object(int fd, AioContext *aio_context, char *buf, |
| 1299 | uint64_t oid, uint8_t copies, |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 1300 | unsigned int datalen, uint64_t offset, bool create, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1301 | uint32_t cache_flags) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1302 | { |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1303 | return read_write_object(fd, aio_context, buf, oid, copies, |
| 1304 | datalen, offset, true, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1305 | create, cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1306 | } |
| 1307 | |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1308 | /* update inode with the latest state */ |
| 1309 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag) |
| 1310 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1311 | Error *local_err = NULL; |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1312 | SheepdogInode *inode; |
| 1313 | int ret = 0, fd; |
| 1314 | uint32_t vid = 0; |
| 1315 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1316 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1317 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 1318 | error_report_err(local_err); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1319 | return -EIO; |
| 1320 | } |
| 1321 | |
Hitoshi Mitake | 5d039ba | 2014-06-06 13:35:12 +0900 | [diff] [blame] | 1322 | inode = g_malloc(SD_INODE_HEADER_SIZE); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1323 | |
Markus Armbruster | dc83cd4 | 2014-05-16 11:00:23 +0200 | [diff] [blame] | 1324 | ret = find_vdi_name(s, s->name, snapid, tag, &vid, false, &local_err); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1325 | if (ret) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 1326 | error_report_err(local_err); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1327 | goto out; |
| 1328 | } |
| 1329 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1330 | ret = read_object(fd, s->aio_context, (char *)inode, vid_to_vdi_oid(vid), |
Hitoshi Mitake | 5d039ba | 2014-06-06 13:35:12 +0900 | [diff] [blame] | 1331 | s->inode.nr_copies, SD_INODE_HEADER_SIZE, 0, |
| 1332 | s->cache_flags); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1333 | if (ret < 0) { |
| 1334 | goto out; |
| 1335 | } |
| 1336 | |
| 1337 | if (inode->vdi_id != s->inode.vdi_id) { |
Hitoshi Mitake | 5d039ba | 2014-06-06 13:35:12 +0900 | [diff] [blame] | 1338 | memcpy(&s->inode, inode, SD_INODE_HEADER_SIZE); |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | out: |
| 1342 | g_free(inode); |
| 1343 | closesocket(fd); |
| 1344 | |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 1348 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1349 | { |
| 1350 | SheepdogAIOCB *acb = aio_req->aiocb; |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1351 | |
| 1352 | aio_req->create = false; |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1353 | |
| 1354 | /* check whether this request becomes a CoW one */ |
MORITA Kazutaka | 2412aec | 2013-10-24 16:01:12 +0900 | [diff] [blame] | 1355 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) { |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1356 | int idx = data_oid_to_idx(aio_req->oid); |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1357 | |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1358 | if (is_data_obj_writable(&s->inode, idx)) { |
| 1359 | goto out; |
| 1360 | } |
| 1361 | |
MORITA Kazutaka | 80308d3 | 2013-10-24 16:01:18 +0900 | [diff] [blame] | 1362 | if (s->inode.data_vdi_id[idx]) { |
| 1363 | aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx); |
| 1364 | aio_req->flags |= SD_FLAG_CMD_COW; |
| 1365 | } |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1366 | aio_req->create = true; |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1367 | } |
| 1368 | out: |
MORITA Kazutaka | 2412aec | 2013-10-24 16:01:12 +0900 | [diff] [blame] | 1369 | if (is_data_obj(aio_req->oid)) { |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1370 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 1371 | acb->aiocb_type); |
MORITA Kazutaka | 2412aec | 2013-10-24 16:01:12 +0900 | [diff] [blame] | 1372 | } else { |
| 1373 | struct iovec iov; |
| 1374 | iov.iov_base = &s->inode; |
| 1375 | iov.iov_len = sizeof(s->inode); |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 1376 | add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA); |
MORITA Kazutaka | 2412aec | 2013-10-24 16:01:12 +0900 | [diff] [blame] | 1377 | } |
MORITA Kazutaka | 13c31de | 2013-04-26 01:19:54 +0900 | [diff] [blame] | 1378 | } |
| 1379 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1380 | static void sd_detach_aio_context(BlockDriverState *bs) |
| 1381 | { |
| 1382 | BDRVSheepdogState *s = bs->opaque; |
| 1383 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1384 | aio_set_fd_handler(s->aio_context, s->fd, false, NULL, |
| 1385 | NULL, NULL); |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static void sd_attach_aio_context(BlockDriverState *bs, |
| 1389 | AioContext *new_context) |
| 1390 | { |
| 1391 | BDRVSheepdogState *s = bs->opaque; |
| 1392 | |
| 1393 | s->aio_context = new_context; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1394 | aio_set_fd_handler(new_context, s->fd, false, |
| 1395 | co_read_response, NULL, s); |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1398 | /* TODO Convert to fine grained options */ |
| 1399 | static QemuOptsList runtime_opts = { |
| 1400 | .name = "sheepdog", |
| 1401 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 1402 | .desc = { |
| 1403 | { |
| 1404 | .name = "filename", |
| 1405 | .type = QEMU_OPT_STRING, |
| 1406 | .help = "URL to the sheepdog image", |
| 1407 | }, |
| 1408 | { /* end of list */ } |
| 1409 | }, |
| 1410 | }; |
| 1411 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 1412 | static int sd_open(BlockDriverState *bs, QDict *options, int flags, |
| 1413 | Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1414 | { |
| 1415 | int ret, fd; |
| 1416 | uint32_t vid = 0; |
| 1417 | BDRVSheepdogState *s = bs->opaque; |
| 1418 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; |
| 1419 | uint32_t snapid; |
| 1420 | char *buf = NULL; |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1421 | QemuOpts *opts; |
| 1422 | Error *local_err = NULL; |
| 1423 | const char *filename; |
| 1424 | |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1425 | s->bs = bs; |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1426 | s->aio_context = bdrv_get_aio_context(bs); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1427 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 1428 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1429 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1430 | if (local_err) { |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1431 | error_propagate(errp, local_err); |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1432 | ret = -EINVAL; |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | filename = qemu_opt_get(opts, "filename"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1437 | |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 1438 | QLIST_INIT(&s->inflight_aio_head); |
MORITA Kazutaka | 011603c | 2013-10-24 16:01:15 +0900 | [diff] [blame] | 1439 | QLIST_INIT(&s->failed_aio_head); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 1440 | QLIST_INIT(&s->inflight_aiocb_head); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1441 | s->fd = -1; |
| 1442 | |
| 1443 | memset(vdi, 0, sizeof(vdi)); |
| 1444 | memset(tag, 0, sizeof(tag)); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1445 | |
| 1446 | if (strstr(filename, "://")) { |
| 1447 | ret = sd_parse_uri(s, filename, vdi, &snapid, tag); |
| 1448 | } else { |
| 1449 | ret = parse_vdiname(s, filename, vdi, &snapid, tag); |
| 1450 | } |
| 1451 | if (ret < 0) { |
Markus Armbruster | efde4b6 | 2014-05-16 11:00:25 +0200 | [diff] [blame] | 1452 | error_setg(errp, "Can't parse filename"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1453 | goto out; |
| 1454 | } |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1455 | s->fd = get_sheep_fd(s, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1456 | if (s->fd < 0) { |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1457 | ret = s->fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1458 | goto out; |
| 1459 | } |
| 1460 | |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1461 | ret = find_vdi_name(s, vdi, snapid, tag, &vid, true, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1462 | if (ret) { |
| 1463 | goto out; |
| 1464 | } |
| 1465 | |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1466 | /* |
| 1467 | * QEMU block layer emulates writethrough cache as 'writeback + flush', so |
| 1468 | * we always set SD_FLAG_CMD_CACHE (writeback cache) as default. |
| 1469 | */ |
| 1470 | s->cache_flags = SD_FLAG_CMD_CACHE; |
| 1471 | if (flags & BDRV_O_NOCACHE) { |
| 1472 | s->cache_flags = SD_FLAG_CMD_DIRECT; |
| 1473 | } |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 1474 | s->discard_supported = true; |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 1475 | |
MORITA Kazutaka | 622b605 | 2012-05-17 03:15:31 +0900 | [diff] [blame] | 1476 | if (snapid || tag[0] != '\0') { |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 1477 | DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid); |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 1478 | s->is_snapshot = true; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1479 | } |
| 1480 | |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1481 | fd = connect_to_sdog(s, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1482 | if (fd < 0) { |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1483 | ret = fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1484 | goto out; |
| 1485 | } |
| 1486 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1487 | buf = g_malloc(SD_INODE_SIZE); |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1488 | ret = read_object(fd, s->aio_context, buf, vid_to_vdi_oid(vid), |
| 1489 | 0, SD_INODE_SIZE, 0, s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1490 | |
| 1491 | closesocket(fd); |
| 1492 | |
| 1493 | if (ret) { |
Markus Armbruster | efde4b6 | 2014-05-16 11:00:25 +0200 | [diff] [blame] | 1494 | error_setg(errp, "Can't read snapshot inode"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1495 | goto out; |
| 1496 | } |
| 1497 | |
| 1498 | memcpy(&s->inode, buf, sizeof(s->inode)); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1499 | |
Liu Yuan | e8bfaa2 | 2013-04-23 14:03:34 +0800 | [diff] [blame] | 1500 | bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE; |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 1501 | pstrcpy(s->name, sizeof(s->name), vdi); |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 1502 | qemu_co_mutex_init(&s->lock); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 1503 | qemu_co_queue_init(&s->overlapping_queue); |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1504 | qemu_opts_del(opts); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1505 | g_free(buf); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1506 | return 0; |
| 1507 | out: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1508 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd, |
| 1509 | false, NULL, NULL, NULL); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1510 | if (s->fd >= 0) { |
| 1511 | closesocket(s->fd); |
| 1512 | } |
Kevin Wolf | c8c9635 | 2013-04-12 18:10:49 +0200 | [diff] [blame] | 1513 | qemu_opts_del(opts); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1514 | g_free(buf); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1515 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1516 | } |
| 1517 | |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 1518 | static int sd_reopen_prepare(BDRVReopenState *state, BlockReopenQueue *queue, |
| 1519 | Error **errp) |
| 1520 | { |
| 1521 | BDRVSheepdogState *s = state->bs->opaque; |
| 1522 | BDRVSheepdogReopenState *re_s; |
| 1523 | int ret = 0; |
| 1524 | |
| 1525 | re_s = state->opaque = g_new0(BDRVSheepdogReopenState, 1); |
| 1526 | |
| 1527 | re_s->cache_flags = SD_FLAG_CMD_CACHE; |
| 1528 | if (state->flags & BDRV_O_NOCACHE) { |
| 1529 | re_s->cache_flags = SD_FLAG_CMD_DIRECT; |
| 1530 | } |
| 1531 | |
| 1532 | re_s->fd = get_sheep_fd(s, errp); |
| 1533 | if (re_s->fd < 0) { |
| 1534 | ret = re_s->fd; |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | static void sd_reopen_commit(BDRVReopenState *state) |
| 1542 | { |
| 1543 | BDRVSheepdogReopenState *re_s = state->opaque; |
| 1544 | BDRVSheepdogState *s = state->bs->opaque; |
| 1545 | |
| 1546 | if (s->fd) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1547 | aio_set_fd_handler(s->aio_context, s->fd, false, |
| 1548 | NULL, NULL, NULL); |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 1549 | closesocket(s->fd); |
| 1550 | } |
| 1551 | |
| 1552 | s->fd = re_s->fd; |
| 1553 | s->cache_flags = re_s->cache_flags; |
| 1554 | |
| 1555 | g_free(state->opaque); |
| 1556 | state->opaque = NULL; |
| 1557 | |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | static void sd_reopen_abort(BDRVReopenState *state) |
| 1562 | { |
| 1563 | BDRVSheepdogReopenState *re_s = state->opaque; |
| 1564 | BDRVSheepdogState *s = state->bs->opaque; |
| 1565 | |
| 1566 | if (re_s == NULL) { |
| 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | if (re_s->fd) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1571 | aio_set_fd_handler(s->aio_context, re_s->fd, false, |
| 1572 | NULL, NULL, NULL); |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 1573 | closesocket(re_s->fd); |
| 1574 | } |
| 1575 | |
| 1576 | g_free(state->opaque); |
| 1577 | state->opaque = NULL; |
| 1578 | |
| 1579 | return; |
| 1580 | } |
| 1581 | |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1582 | static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot, |
| 1583 | Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1584 | { |
| 1585 | SheepdogVdiReq hdr; |
| 1586 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; |
| 1587 | int fd, ret; |
| 1588 | unsigned int wlen, rlen = 0; |
| 1589 | char buf[SD_MAX_VDI_LEN]; |
| 1590 | |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1591 | fd = connect_to_sdog(s, errp); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1592 | if (fd < 0) { |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1593 | return fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1594 | } |
| 1595 | |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 1596 | /* FIXME: would it be better to fail (e.g., return -EIO) when filename |
| 1597 | * does not fit in buf? For now, just truncate and avoid buffer overrun. |
| 1598 | */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1599 | memset(buf, 0, sizeof(buf)); |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1600 | pstrcpy(buf, sizeof(buf), s->name); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1601 | |
| 1602 | memset(&hdr, 0, sizeof(hdr)); |
| 1603 | hdr.opcode = SD_OP_NEW_VDI; |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1604 | hdr.base_vdi_id = s->inode.vdi_id; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1605 | |
| 1606 | wlen = SD_MAX_VDI_LEN; |
| 1607 | |
| 1608 | hdr.flags = SD_FLAG_CMD_WRITE; |
| 1609 | hdr.snapid = snapshot; |
| 1610 | |
| 1611 | hdr.data_length = wlen; |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1612 | hdr.vdi_size = s->inode.vdi_size; |
| 1613 | hdr.copy_policy = s->inode.copy_policy; |
Liu Yuan | b3af018 | 2013-11-07 22:56:38 +0800 | [diff] [blame] | 1614 | hdr.copies = s->inode.nr_copies; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1615 | hdr.block_size_shift = s->inode.block_size_shift; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1616 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1617 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1618 | |
| 1619 | closesocket(fd); |
| 1620 | |
| 1621 | if (ret) { |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1622 | error_setg_errno(errp, -ret, "create failed"); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1623 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | if (rsp->result != SD_RES_SUCCESS) { |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1627 | error_setg(errp, "%s, %s", sd_strerror(rsp->result), s->inode.name); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1628 | return -EIO; |
| 1629 | } |
| 1630 | |
| 1631 | if (vdi_id) { |
| 1632 | *vdi_id = rsp->vdi_id; |
| 1633 | } |
| 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Markus Armbruster | 318df29 | 2014-05-16 11:00:21 +0200 | [diff] [blame] | 1638 | static int sd_prealloc(const char *filename, Error **errp) |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1639 | { |
| 1640 | BlockDriverState *bs = NULL; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1641 | BDRVSheepdogState *base = NULL; |
| 1642 | unsigned long buf_size; |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1643 | uint32_t idx, max_idx; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1644 | uint32_t object_size; |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1645 | int64_t vdi_size; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1646 | void *buf = NULL; |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1647 | int ret; |
| 1648 | |
Max Reitz | 2e40134 | 2014-02-18 18:33:07 +0100 | [diff] [blame] | 1649 | ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, |
Max Reitz | 6ebf9aa | 2015-08-26 19:47:49 +0200 | [diff] [blame] | 1650 | errp); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1651 | if (ret < 0) { |
Markus Armbruster | 318df29 | 2014-05-16 11:00:21 +0200 | [diff] [blame] | 1652 | goto out_with_err_set; |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | vdi_size = bdrv_getlength(bs); |
| 1656 | if (vdi_size < 0) { |
| 1657 | ret = vdi_size; |
| 1658 | goto out; |
| 1659 | } |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1660 | |
| 1661 | base = bs->opaque; |
| 1662 | object_size = (UINT32_C(1) << base->inode.block_size_shift); |
| 1663 | buf_size = MIN(object_size, SD_DATA_OBJ_SIZE); |
| 1664 | buf = g_malloc0(buf_size); |
| 1665 | |
| 1666 | max_idx = DIV_ROUND_UP(vdi_size, buf_size); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1667 | |
| 1668 | for (idx = 0; idx < max_idx; idx++) { |
| 1669 | /* |
| 1670 | * The created image can be a cloned image, so we need to read |
| 1671 | * a data from the source image. |
| 1672 | */ |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1673 | ret = bdrv_pread(bs, idx * buf_size, buf, buf_size); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1674 | if (ret < 0) { |
| 1675 | goto out; |
| 1676 | } |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1677 | ret = bdrv_pwrite(bs, idx * buf_size, buf, buf_size); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1678 | if (ret < 0) { |
| 1679 | goto out; |
| 1680 | } |
| 1681 | } |
Markus Armbruster | 318df29 | 2014-05-16 11:00:21 +0200 | [diff] [blame] | 1682 | |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1683 | out: |
Markus Armbruster | 318df29 | 2014-05-16 11:00:21 +0200 | [diff] [blame] | 1684 | if (ret < 0) { |
| 1685 | error_setg_errno(errp, -ret, "Can't pre-allocate"); |
| 1686 | } |
| 1687 | out_with_err_set: |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1688 | if (bs) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1689 | bdrv_unref(bs); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1690 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1691 | g_free(buf); |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1692 | |
| 1693 | return ret; |
| 1694 | } |
| 1695 | |
Liu Yuan | b3af018 | 2013-11-07 22:56:38 +0800 | [diff] [blame] | 1696 | /* |
| 1697 | * Sheepdog support two kinds of redundancy, full replication and erasure |
| 1698 | * coding. |
| 1699 | * |
| 1700 | * # create a fully replicated vdi with x copies |
| 1701 | * -o redundancy=x (1 <= x <= SD_MAX_COPIES) |
| 1702 | * |
| 1703 | * # create a erasure coded vdi with x data strips and y parity strips |
| 1704 | * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP) |
| 1705 | */ |
| 1706 | static int parse_redundancy(BDRVSheepdogState *s, const char *opt) |
| 1707 | { |
| 1708 | struct SheepdogInode *inode = &s->inode; |
| 1709 | const char *n1, *n2; |
| 1710 | long copy, parity; |
| 1711 | char p[10]; |
| 1712 | |
| 1713 | pstrcpy(p, sizeof(p), opt); |
| 1714 | n1 = strtok(p, ":"); |
| 1715 | n2 = strtok(NULL, ":"); |
| 1716 | |
| 1717 | if (!n1) { |
| 1718 | return -EINVAL; |
| 1719 | } |
| 1720 | |
| 1721 | copy = strtol(n1, NULL, 10); |
| 1722 | if (copy > SD_MAX_COPIES || copy < 1) { |
| 1723 | return -EINVAL; |
| 1724 | } |
| 1725 | if (!n2) { |
| 1726 | inode->copy_policy = 0; |
| 1727 | inode->nr_copies = copy; |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | if (copy != 2 && copy != 4 && copy != 8 && copy != 16) { |
| 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | |
| 1735 | parity = strtol(n2, NULL, 10); |
| 1736 | if (parity >= SD_EC_MAX_STRIP || parity < 1) { |
| 1737 | return -EINVAL; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * 4 bits for parity and 4 bits for data. |
| 1742 | * We have to compress upper data bits because it can't represent 16 |
| 1743 | */ |
| 1744 | inode->copy_policy = ((copy / 2) << 4) + parity; |
| 1745 | inode->nr_copies = copy + parity; |
| 1746 | |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1750 | static int parse_block_size_shift(BDRVSheepdogState *s, QemuOpts *opt) |
| 1751 | { |
| 1752 | struct SheepdogInode *inode = &s->inode; |
| 1753 | uint64_t object_size; |
| 1754 | int obj_order; |
| 1755 | |
| 1756 | object_size = qemu_opt_get_size_del(opt, BLOCK_OPT_OBJECT_SIZE, 0); |
| 1757 | if (object_size) { |
| 1758 | if ((object_size - 1) & object_size) { /* not a power of 2? */ |
| 1759 | return -EINVAL; |
| 1760 | } |
Stefan Hajnoczi | 786a4ea | 2015-03-23 15:29:26 +0000 | [diff] [blame] | 1761 | obj_order = ctz32(object_size); |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1762 | if (obj_order < 20 || obj_order > 31) { |
| 1763 | return -EINVAL; |
| 1764 | } |
| 1765 | inode->block_size_shift = (uint8_t)obj_order; |
| 1766 | } |
| 1767 | |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 1771 | static int sd_create(const char *filename, QemuOpts *opts, |
Max Reitz | d5124c0 | 2013-09-05 14:26:05 +0200 | [diff] [blame] | 1772 | Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1773 | { |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1774 | int ret = 0; |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1775 | uint32_t vid = 0; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1776 | char *backing_file = NULL; |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 1777 | char *buf = NULL; |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1778 | BDRVSheepdogState *s; |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1779 | char tag[SD_MAX_VDI_TAG_LEN]; |
MORITA Kazutaka | b444736 | 2011-01-28 01:33:10 +0900 | [diff] [blame] | 1780 | uint32_t snapid; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1781 | uint64_t max_vdi_size; |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 1782 | bool prealloc = false; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1783 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 1784 | s = g_new0(BDRVSheepdogState, 1); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1785 | |
MORITA Kazutaka | b444736 | 2011-01-28 01:33:10 +0900 | [diff] [blame] | 1786 | memset(tag, 0, sizeof(tag)); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1787 | if (strstr(filename, "://")) { |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1788 | ret = sd_parse_uri(s, filename, s->name, &snapid, tag); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1789 | } else { |
Liu Yuan | c31d482 | 2013-11-07 22:56:37 +0800 | [diff] [blame] | 1790 | ret = parse_vdiname(s, filename, s->name, &snapid, tag); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 1791 | } |
| 1792 | if (ret < 0) { |
Markus Armbruster | efde4b6 | 2014-05-16 11:00:25 +0200 | [diff] [blame] | 1793 | error_setg(errp, "Can't parse filename"); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1794 | goto out; |
MORITA Kazutaka | b444736 | 2011-01-28 01:33:10 +0900 | [diff] [blame] | 1795 | } |
| 1796 | |
Hu Tao | c2eb918 | 2014-09-10 17:05:45 +0800 | [diff] [blame] | 1797 | s->inode.vdi_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 1798 | BDRV_SECTOR_SIZE); |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 1799 | backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
| 1800 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); |
| 1801 | if (!buf || !strcmp(buf, "off")) { |
| 1802 | prealloc = false; |
| 1803 | } else if (!strcmp(buf, "full")) { |
| 1804 | prealloc = true; |
| 1805 | } else { |
| 1806 | error_setg(errp, "Invalid preallocation mode: '%s'", buf); |
| 1807 | ret = -EINVAL; |
| 1808 | goto out; |
| 1809 | } |
| 1810 | |
| 1811 | g_free(buf); |
| 1812 | buf = qemu_opt_get_del(opts, BLOCK_OPT_REDUNDANCY); |
| 1813 | if (buf) { |
| 1814 | ret = parse_redundancy(s, buf); |
| 1815 | if (ret < 0) { |
| 1816 | error_setg(errp, "Invalid redundancy mode: '%s'", buf); |
| 1817 | goto out; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1818 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1819 | } |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1820 | ret = parse_block_size_shift(s, opts); |
| 1821 | if (ret < 0) { |
| 1822 | error_setg(errp, "Invalid object_size." |
| 1823 | " obect_size needs to be power of 2" |
| 1824 | " and be limited from 2^20 to 2^31"); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1825 | goto out; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | if (backing_file) { |
| 1829 | BlockDriverState *bs; |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1830 | BDRVSheepdogState *base; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1831 | BlockDriver *drv; |
| 1832 | |
| 1833 | /* Currently, only Sheepdog backing image is supported. */ |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 1834 | drv = bdrv_find_protocol(backing_file, true, NULL); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1835 | if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1836 | error_setg(errp, "backing_file must be a sheepdog image"); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1837 | ret = -EINVAL; |
| 1838 | goto out; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1839 | } |
| 1840 | |
Max Reitz | 2e40134 | 2014-02-18 18:33:07 +0100 | [diff] [blame] | 1841 | bs = NULL; |
Max Reitz | 6ebf9aa | 2015-08-26 19:47:49 +0200 | [diff] [blame] | 1842 | ret = bdrv_open(&bs, backing_file, NULL, NULL, BDRV_O_PROTOCOL, errp); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1843 | if (ret < 0) { |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1844 | goto out; |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1845 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1846 | |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1847 | base = bs->opaque; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1848 | |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1849 | if (!is_snapshot(&base->inode)) { |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1850 | error_setg(errp, "cannot clone from a non snapshot vdi"); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1851 | bdrv_unref(bs); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1852 | ret = -EINVAL; |
| 1853 | goto out; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1854 | } |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1855 | s->inode.vdi_id = base->inode.vdi_id; |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1856 | bdrv_unref(bs); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1857 | } |
| 1858 | |
Liu Yuan | 5d5da11 | 2014-06-17 13:45:35 +0800 | [diff] [blame] | 1859 | s->aio_context = qemu_get_aio_context(); |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1860 | |
| 1861 | /* if block_size_shift is not specified, get cluster default value */ |
| 1862 | if (s->inode.block_size_shift == 0) { |
| 1863 | SheepdogVdiReq hdr; |
| 1864 | SheepdogClusterRsp *rsp = (SheepdogClusterRsp *)&hdr; |
| 1865 | Error *local_err = NULL; |
| 1866 | int fd; |
| 1867 | unsigned int wlen = 0, rlen = 0; |
| 1868 | |
| 1869 | fd = connect_to_sdog(s, &local_err); |
| 1870 | if (fd < 0) { |
Markus Armbruster | 4fffeb5 | 2015-12-18 16:35:05 +0100 | [diff] [blame] | 1871 | error_report_err(local_err); |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1872 | ret = -EIO; |
| 1873 | goto out; |
| 1874 | } |
| 1875 | |
| 1876 | memset(&hdr, 0, sizeof(hdr)); |
| 1877 | hdr.opcode = SD_OP_GET_CLUSTER_DEFAULT; |
| 1878 | hdr.proto_ver = SD_PROTO_VER; |
| 1879 | |
| 1880 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, |
| 1881 | NULL, &wlen, &rlen); |
| 1882 | closesocket(fd); |
| 1883 | if (ret) { |
| 1884 | error_setg_errno(errp, -ret, "failed to get cluster default"); |
| 1885 | goto out; |
| 1886 | } |
| 1887 | if (rsp->result == SD_RES_SUCCESS) { |
| 1888 | s->inode.block_size_shift = rsp->block_size_shift; |
| 1889 | } else { |
| 1890 | s->inode.block_size_shift = SD_DEFAULT_BLOCK_SIZE_SHIFT; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS; |
| 1895 | |
| 1896 | if (s->inode.vdi_size > max_vdi_size) { |
| 1897 | error_setg(errp, "An image is too large." |
| 1898 | " The maximum image size is %"PRIu64 "GB", |
| 1899 | max_vdi_size / 1024 / 1024 / 1024); |
| 1900 | ret = -EINVAL; |
| 1901 | goto out; |
| 1902 | } |
| 1903 | |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1904 | ret = do_sd_create(s, &vid, 0, errp); |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1905 | if (ret) { |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1906 | goto out; |
MORITA Kazutaka | a8e0fdd | 2011-07-06 03:38:48 +0900 | [diff] [blame] | 1907 | } |
| 1908 | |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 1909 | if (prealloc) { |
Markus Armbruster | e67c399 | 2014-05-16 11:00:24 +0200 | [diff] [blame] | 1910 | ret = sd_prealloc(filename, errp); |
Markus Armbruster | 318df29 | 2014-05-16 11:00:21 +0200 | [diff] [blame] | 1911 | } |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1912 | out: |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 1913 | g_free(backing_file); |
| 1914 | g_free(buf); |
MORITA Kazutaka | b6fc824 | 2012-05-17 03:15:34 +0900 | [diff] [blame] | 1915 | g_free(s); |
| 1916 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | static void sd_close(BlockDriverState *bs) |
| 1920 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1921 | Error *local_err = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1922 | BDRVSheepdogState *s = bs->opaque; |
| 1923 | SheepdogVdiReq hdr; |
| 1924 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; |
| 1925 | unsigned int wlen, rlen = 0; |
| 1926 | int fd, ret; |
| 1927 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 1928 | DPRINTF("%s\n", s->name); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1929 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1930 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1931 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 1932 | error_report_err(local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | memset(&hdr, 0, sizeof(hdr)); |
| 1937 | |
| 1938 | hdr.opcode = SD_OP_RELEASE_VDI; |
Hitoshi Mitake | 1dbfafe | 2014-08-11 14:43:45 +0900 | [diff] [blame] | 1939 | hdr.type = LOCK_TYPE_NORMAL; |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 1940 | hdr.base_vdi_id = s->inode.vdi_id; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1941 | wlen = strlen(s->name) + 1; |
| 1942 | hdr.data_length = wlen; |
| 1943 | hdr.flags = SD_FLAG_CMD_WRITE; |
| 1944 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1945 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, |
| 1946 | s->name, &wlen, &rlen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1947 | |
| 1948 | closesocket(fd); |
| 1949 | |
| 1950 | if (!ret && rsp->result != SD_RES_SUCCESS && |
| 1951 | rsp->result != SD_RES_VDI_NOT_LOCKED) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1952 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1953 | } |
| 1954 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1955 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd, |
| 1956 | false, NULL, NULL, NULL); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1957 | closesocket(s->fd); |
MORITA Kazutaka | 25af257 | 2013-02-22 12:39:52 +0900 | [diff] [blame] | 1958 | g_free(s->host_spec); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | static int64_t sd_getlength(BlockDriverState *bs) |
| 1962 | { |
| 1963 | BDRVSheepdogState *s = bs->opaque; |
| 1964 | |
| 1965 | return s->inode.vdi_size; |
| 1966 | } |
| 1967 | |
| 1968 | static int sd_truncate(BlockDriverState *bs, int64_t offset) |
| 1969 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1970 | Error *local_err = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1971 | BDRVSheepdogState *s = bs->opaque; |
| 1972 | int ret, fd; |
| 1973 | unsigned int datalen; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1974 | uint64_t max_vdi_size; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1975 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1976 | max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1977 | if (offset < s->inode.vdi_size) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1978 | error_report("shrinking is not supported"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1979 | return -EINVAL; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 1980 | } else if (offset > max_vdi_size) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1981 | error_report("too big image size"); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1982 | return -EINVAL; |
| 1983 | } |
| 1984 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 1985 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1986 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 1987 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 1988 | return fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* we don't need to update entire object */ |
| 1992 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); |
| 1993 | s->inode.vdi_size = offset; |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 1994 | ret = write_object(fd, s->aio_context, (char *)&s->inode, |
| 1995 | vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies, |
| 1996 | datalen, 0, false, s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 1997 | close(fd); |
| 1998 | |
| 1999 | if (ret < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2000 | error_report("failed to update an inode."); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2001 | } |
| 2002 | |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2003 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * This function is called after writing data objects. If we need to |
| 2008 | * update metadata, this sends a write request to the vdi object. |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2009 | * Otherwise, this switches back to sd_co_readv/writev. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2010 | */ |
Paolo Bonzini | d8716b4 | 2011-10-05 09:17:31 +0200 | [diff] [blame] | 2011 | static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2012 | { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2013 | BDRVSheepdogState *s = acb->common.bs->opaque; |
| 2014 | struct iovec iov; |
| 2015 | AIOReq *aio_req; |
| 2016 | uint32_t offset, data_len, mn, mx; |
| 2017 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2018 | mn = acb->min_dirty_data_idx; |
| 2019 | mx = acb->max_dirty_data_idx; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2020 | if (mn <= mx) { |
| 2021 | /* we need to update the vdi object. */ |
| 2022 | offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + |
| 2023 | mn * sizeof(s->inode.data_vdi_id[0]); |
| 2024 | data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); |
| 2025 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2026 | acb->min_dirty_data_idx = UINT32_MAX; |
| 2027 | acb->max_dirty_data_idx = 0; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2028 | |
| 2029 | iov.iov_base = &s->inode; |
| 2030 | iov.iov_len = sizeof(s->inode); |
| 2031 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2032 | data_len, offset, 0, false, 0, offset); |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 2033 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2034 | add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2035 | |
| 2036 | acb->aio_done_func = sd_finish_aiocb; |
| 2037 | acb->aiocb_type = AIOCB_WRITE_UDATA; |
| 2038 | return; |
| 2039 | } |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 2040 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2041 | sd_finish_aiocb(acb); |
| 2042 | } |
| 2043 | |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2044 | /* Delete current working VDI on the snapshot chain */ |
| 2045 | static bool sd_delete(BDRVSheepdogState *s) |
| 2046 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2047 | Error *local_err = NULL; |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2048 | unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0; |
| 2049 | SheepdogVdiReq hdr = { |
| 2050 | .opcode = SD_OP_DEL_VDI, |
Liu Yuan | 9f23fce | 2014-01-03 20:13:12 +0800 | [diff] [blame] | 2051 | .base_vdi_id = s->inode.vdi_id, |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2052 | .data_length = wlen, |
| 2053 | .flags = SD_FLAG_CMD_WRITE, |
| 2054 | }; |
| 2055 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; |
| 2056 | int fd, ret; |
| 2057 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2058 | fd = connect_to_sdog(s, &local_err); |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2059 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2060 | error_report_err(local_err); |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2061 | return false; |
| 2062 | } |
| 2063 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2064 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, |
| 2065 | s->name, &wlen, &rlen); |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2066 | closesocket(fd); |
| 2067 | if (ret) { |
| 2068 | return false; |
| 2069 | } |
| 2070 | switch (rsp->result) { |
| 2071 | case SD_RES_NO_VDI: |
| 2072 | error_report("%s was already deleted", s->name); |
| 2073 | /* fall through */ |
| 2074 | case SD_RES_SUCCESS: |
| 2075 | break; |
| 2076 | default: |
| 2077 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
| 2078 | return false; |
| 2079 | } |
| 2080 | |
| 2081 | return true; |
| 2082 | } |
| 2083 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2084 | /* |
| 2085 | * Create a writable VDI from a snapshot |
| 2086 | */ |
| 2087 | static int sd_create_branch(BDRVSheepdogState *s) |
| 2088 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2089 | Error *local_err = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2090 | int ret, fd; |
| 2091 | uint32_t vid; |
| 2092 | char *buf; |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2093 | bool deleted; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2094 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2095 | DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2096 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2097 | buf = g_malloc(SD_INODE_SIZE); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2098 | |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2099 | /* |
| 2100 | * Even If deletion fails, we will just create extra snapshot based on |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 2101 | * the working VDI which was supposed to be deleted. So no need to |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2102 | * false bail out. |
| 2103 | */ |
| 2104 | deleted = sd_delete(s); |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 2105 | ret = do_sd_create(s, &vid, !deleted, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2106 | if (ret) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2107 | error_report_err(local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2108 | goto out; |
| 2109 | } |
| 2110 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2111 | DPRINTF("%" PRIx32 " is created.\n", vid); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2112 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2113 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2114 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2115 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2116 | ret = fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2117 | goto out; |
| 2118 | } |
| 2119 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2120 | ret = read_object(fd, s->aio_context, buf, vid_to_vdi_oid(vid), |
| 2121 | s->inode.nr_copies, SD_INODE_SIZE, 0, s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2122 | |
| 2123 | closesocket(fd); |
| 2124 | |
| 2125 | if (ret < 0) { |
| 2126 | goto out; |
| 2127 | } |
| 2128 | |
| 2129 | memcpy(&s->inode, buf, sizeof(s->inode)); |
| 2130 | |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 2131 | s->is_snapshot = false; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2132 | ret = 0; |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2133 | DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2134 | |
| 2135 | out: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2136 | g_free(buf); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2137 | |
| 2138 | return ret; |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * Send I/O requests to the server. |
| 2143 | * |
| 2144 | * This function sends requests to the server, links the requests to |
MORITA Kazutaka | c292ee6 | 2012-06-27 07:26:22 +0900 | [diff] [blame] | 2145 | * the inflight_list in BDRVSheepdogState, and exits without |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2146 | * waiting the response. The responses are received in the |
| 2147 | * `aio_read_response' function which is called from the main loop as |
| 2148 | * a fd handler. |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2149 | * |
| 2150 | * Returns 1 when we need to wait a response, 0 when there is no sent |
| 2151 | * request and -errno in error cases. |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2152 | */ |
Paolo Bonzini | d8716b4 | 2011-10-05 09:17:31 +0200 | [diff] [blame] | 2153 | static int coroutine_fn sd_co_rw_vector(void *p) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2154 | { |
| 2155 | SheepdogAIOCB *acb = p; |
| 2156 | int ret = 0; |
Liu Yuan | e8bfaa2 | 2013-04-23 14:03:34 +0800 | [diff] [blame] | 2157 | unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2158 | unsigned long idx; |
| 2159 | uint32_t object_size; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2160 | uint64_t oid; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2161 | uint64_t offset; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2162 | BDRVSheepdogState *s = acb->common.bs->opaque; |
| 2163 | SheepdogInode *inode = &s->inode; |
| 2164 | AIOReq *aio_req; |
| 2165 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2166 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) { |
| 2167 | /* |
| 2168 | * In the case we open the snapshot VDI, Sheepdog creates the |
| 2169 | * writable VDI when we do a write operation first. |
| 2170 | */ |
| 2171 | ret = sd_create_branch(s); |
| 2172 | if (ret) { |
| 2173 | acb->ret = -EIO; |
| 2174 | goto out; |
| 2175 | } |
| 2176 | } |
| 2177 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2178 | object_size = (UINT32_C(1) << inode->block_size_shift); |
| 2179 | idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size; |
| 2180 | offset = (acb->sector_num * BDRV_SECTOR_SIZE) % object_size; |
| 2181 | |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 2182 | /* |
| 2183 | * Make sure we don't free the aiocb before we are done with all requests. |
| 2184 | * This additional reference is dropped at the end of this function. |
| 2185 | */ |
| 2186 | acb->nr_pending++; |
| 2187 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2188 | while (done != total) { |
| 2189 | uint8_t flags = 0; |
| 2190 | uint64_t old_oid = 0; |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 2191 | bool create = false; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2192 | |
| 2193 | oid = vid_to_data_oid(inode->data_vdi_id[idx], idx); |
| 2194 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2195 | len = MIN(total - done, object_size - offset); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2196 | |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2197 | switch (acb->aiocb_type) { |
| 2198 | case AIOCB_READ_UDATA: |
| 2199 | if (!inode->data_vdi_id[idx]) { |
| 2200 | qemu_iovec_memset(acb->qiov, done, 0, len); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2201 | goto done; |
| 2202 | } |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2203 | break; |
| 2204 | case AIOCB_WRITE_UDATA: |
| 2205 | if (!inode->data_vdi_id[idx]) { |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 2206 | create = true; |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2207 | } else if (!is_data_obj_writable(inode, idx)) { |
| 2208 | /* Copy-On-Write */ |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 2209 | create = true; |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2210 | old_oid = oid; |
| 2211 | flags = SD_FLAG_CMD_COW; |
| 2212 | } |
| 2213 | break; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2214 | case AIOCB_DISCARD_OBJ: |
| 2215 | /* |
| 2216 | * We discard the object only when the whole object is |
| 2217 | * 1) allocated 2) trimmed. Otherwise, simply skip it. |
| 2218 | */ |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2219 | if (len != object_size || inode->data_vdi_id[idx] == 0) { |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2220 | goto done; |
| 2221 | } |
| 2222 | break; |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2223 | default: |
| 2224 | break; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | if (create) { |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2228 | DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n", |
MORITA Kazutaka | 1b6ac99 | 2012-06-27 07:26:18 +0900 | [diff] [blame] | 2229 | inode->vdi_id, oid, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2230 | vid_to_data_oid(inode->data_vdi_id[idx], idx), idx); |
| 2231 | oid = vid_to_data_oid(inode->vdi_id, idx); |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2232 | DPRINTF("new oid %" PRIx64 "\n", oid); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2233 | } |
| 2234 | |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2235 | aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, create, |
Hitoshi Mitake | e6fd57e | 2015-09-01 12:03:10 +0900 | [diff] [blame] | 2236 | old_oid, |
| 2237 | acb->aiocb_type == AIOCB_DISCARD_OBJ ? |
| 2238 | 0 : done); |
MORITA Kazutaka | 80308d3 | 2013-10-24 16:01:18 +0900 | [diff] [blame] | 2239 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2240 | |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2241 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, |
MORITA Kazutaka | a37dcdf | 2013-10-24 16:01:16 +0900 | [diff] [blame] | 2242 | acb->aiocb_type); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2243 | done: |
| 2244 | offset = 0; |
| 2245 | idx++; |
| 2246 | done += len; |
| 2247 | } |
| 2248 | out: |
MORITA Kazutaka | 1d732d7 | 2012-06-27 07:26:21 +0900 | [diff] [blame] | 2249 | if (!--acb->nr_pending) { |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2250 | return acb->ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2251 | } |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2252 | return 1; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2253 | } |
| 2254 | |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2255 | static bool check_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *aiocb) |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2256 | { |
| 2257 | SheepdogAIOCB *cb; |
| 2258 | |
| 2259 | QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) { |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2260 | if (AIOCBOverlapping(aiocb, cb)) { |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2261 | return true; |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | QLIST_INSERT_HEAD(&s->inflight_aiocb_head, aiocb, aiocb_siblings); |
| 2266 | return false; |
| 2267 | } |
| 2268 | |
Dong Xu Wang | a968168 | 2011-11-10 16:23:22 +0800 | [diff] [blame] | 2269 | static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2270 | int nb_sectors, QEMUIOVector *qiov) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2271 | { |
| 2272 | SheepdogAIOCB *acb; |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2273 | int ret; |
Liu Yuan | e50d760 | 2013-12-14 01:29:28 +0800 | [diff] [blame] | 2274 | int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE; |
| 2275 | BDRVSheepdogState *s = bs->opaque; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2276 | |
Max Reitz | c0191e7 | 2015-02-05 13:58:24 -0500 | [diff] [blame] | 2277 | if (offset > s->inode.vdi_size) { |
Liu Yuan | e50d760 | 2013-12-14 01:29:28 +0800 | [diff] [blame] | 2278 | ret = sd_truncate(bs, offset); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2279 | if (ret < 0) { |
| 2280 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2281 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2282 | } |
| 2283 | |
Liu Yuan | f700f8e | 2013-01-14 14:01:03 +0800 | [diff] [blame] | 2284 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2285 | acb->aio_done_func = sd_write_done; |
| 2286 | acb->aiocb_type = AIOCB_WRITE_UDATA; |
| 2287 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2288 | retry: |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2289 | if (check_overlapping_aiocb(s, acb)) { |
| 2290 | qemu_co_queue_wait(&s->overlapping_queue); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2291 | goto retry; |
| 2292 | } |
| 2293 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2294 | ret = sd_co_rw_vector(acb); |
| 2295 | if (ret <= 0) { |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2296 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2297 | qemu_co_queue_restart_all(&s->overlapping_queue); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 2298 | qemu_aio_unref(acb); |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2299 | return ret; |
| 2300 | } |
| 2301 | |
| 2302 | qemu_coroutine_yield(); |
| 2303 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2304 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2305 | qemu_co_queue_restart_all(&s->overlapping_queue); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2306 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2307 | return acb->ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2308 | } |
| 2309 | |
Dong Xu Wang | a968168 | 2011-11-10 16:23:22 +0800 | [diff] [blame] | 2310 | static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num, |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2311 | int nb_sectors, QEMUIOVector *qiov) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2312 | { |
| 2313 | SheepdogAIOCB *acb; |
Christoph Hellwig | 19db9b9 | 2012-07-10 16:12:27 +0200 | [diff] [blame] | 2314 | int ret; |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2315 | BDRVSheepdogState *s = bs->opaque; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2316 | |
Liu Yuan | f700f8e | 2013-01-14 14:01:03 +0800 | [diff] [blame] | 2317 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2318 | acb->aiocb_type = AIOCB_READ_UDATA; |
| 2319 | acb->aio_done_func = sd_finish_aiocb; |
| 2320 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2321 | retry: |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2322 | if (check_overlapping_aiocb(s, acb)) { |
| 2323 | qemu_co_queue_wait(&s->overlapping_queue); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2324 | goto retry; |
| 2325 | } |
| 2326 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2327 | ret = sd_co_rw_vector(acb); |
| 2328 | if (ret <= 0) { |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2329 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2330 | qemu_co_queue_restart_all(&s->overlapping_queue); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 2331 | qemu_aio_unref(acb); |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2332 | return ret; |
| 2333 | } |
| 2334 | |
| 2335 | qemu_coroutine_yield(); |
| 2336 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2337 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2338 | qemu_co_queue_restart_all(&s->overlapping_queue); |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2339 | return acb->ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2340 | } |
| 2341 | |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2342 | static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) |
| 2343 | { |
| 2344 | BDRVSheepdogState *s = bs->opaque; |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 2345 | SheepdogAIOCB *acb; |
| 2346 | AIOReq *aio_req; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2347 | |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 2348 | if (s->cache_flags != SD_FLAG_CMD_CACHE) { |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2349 | return 0; |
| 2350 | } |
| 2351 | |
Liu Yuan | f700f8e | 2013-01-14 14:01:03 +0800 | [diff] [blame] | 2352 | acb = sd_aio_setup(bs, NULL, 0, 0); |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 2353 | acb->aiocb_type = AIOCB_FLUSH_CACHE; |
| 2354 | acb->aio_done_func = sd_finish_aiocb; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2355 | |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 2356 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2357 | 0, 0, 0, false, 0, 0); |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 2358 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
Hitoshi Mitake | b544c1a | 2014-06-06 13:35:11 +0900 | [diff] [blame] | 2359 | add_aio_request(s, aio_req, NULL, 0, acb->aiocb_type); |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2360 | |
Liu Yuan | 4778307 | 2013-01-15 16:28:55 +0800 | [diff] [blame] | 2361 | qemu_coroutine_yield(); |
| 2362 | return acb->ret; |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2363 | } |
| 2364 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2365 | static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) |
| 2366 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2367 | Error *local_err = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2368 | BDRVSheepdogState *s = bs->opaque; |
| 2369 | int ret, fd; |
| 2370 | uint32_t new_vid; |
| 2371 | SheepdogInode *inode; |
| 2372 | unsigned int datalen; |
| 2373 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2374 | DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " " |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2375 | "is_snapshot %d\n", sn_info->name, sn_info->id_str, |
| 2376 | s->name, sn_info->vm_state_size, s->is_snapshot); |
| 2377 | |
| 2378 | if (s->is_snapshot) { |
| 2379 | error_report("You can't create a snapshot of a snapshot VDI, " |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2380 | "%s (%" PRIu32 ").", s->name, s->inode.vdi_id); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2381 | |
| 2382 | return -EINVAL; |
| 2383 | } |
| 2384 | |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2385 | DPRINTF("%s %s\n", sn_info->name, sn_info->id_str); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2386 | |
| 2387 | s->inode.vm_state_size = sn_info->vm_state_size; |
| 2388 | s->inode.vm_clock_nsec = sn_info->vm_clock_nsec; |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 2389 | /* It appears that inode.tag does not require a NUL terminator, |
| 2390 | * which means this use of strncpy is ok. |
| 2391 | */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2392 | strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag)); |
| 2393 | /* we don't need to update entire object */ |
| 2394 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); |
Markus Armbruster | 2df5fee | 2014-05-28 11:17:06 +0200 | [diff] [blame] | 2395 | inode = g_malloc(datalen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2396 | |
| 2397 | /* refresh inode. */ |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2398 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2399 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2400 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2401 | ret = fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2402 | goto cleanup; |
| 2403 | } |
| 2404 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2405 | ret = write_object(fd, s->aio_context, (char *)&s->inode, |
| 2406 | vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies, |
| 2407 | datalen, 0, false, s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2408 | if (ret < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2409 | error_report("failed to write snapshot's inode."); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2410 | goto cleanup; |
| 2411 | } |
| 2412 | |
Markus Armbruster | 7d2d3e7 | 2014-05-16 11:00:22 +0200 | [diff] [blame] | 2413 | ret = do_sd_create(s, &new_vid, 1, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2414 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2415 | error_reportf_err(local_err, |
| 2416 | "failed to create inode for snapshot: "); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2417 | goto cleanup; |
| 2418 | } |
| 2419 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2420 | ret = read_object(fd, s->aio_context, (char *)inode, |
| 2421 | vid_to_vdi_oid(new_vid), s->inode.nr_copies, datalen, 0, |
| 2422 | s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2423 | |
| 2424 | if (ret < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2425 | error_report("failed to read new inode info. %s", strerror(errno)); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2426 | goto cleanup; |
| 2427 | } |
| 2428 | |
| 2429 | memcpy(&s->inode, inode, datalen); |
Peter Maydell | 2440a2c | 2013-07-29 13:16:36 +0100 | [diff] [blame] | 2430 | DPRINTF("s->inode: name %s snap_id %x oid %x\n", |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2431 | s->inode.name, s->inode.snap_id, s->inode.vdi_id); |
| 2432 | |
| 2433 | cleanup: |
Markus Armbruster | 2df5fee | 2014-05-28 11:17:06 +0200 | [diff] [blame] | 2434 | g_free(inode); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2435 | closesocket(fd); |
| 2436 | return ret; |
| 2437 | } |
| 2438 | |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2439 | /* |
| 2440 | * We implement rollback(loadvm) operation to the specified snapshot by |
| 2441 | * 1) switch to the snapshot |
| 2442 | * 2) rely on sd_create_branch to delete working VDI and |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 2443 | * 3) create a new working VDI based on the specified snapshot |
Liu Yuan | 859e555 | 2013-04-25 20:49:39 +0800 | [diff] [blame] | 2444 | */ |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2445 | static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
| 2446 | { |
| 2447 | BDRVSheepdogState *s = bs->opaque; |
| 2448 | BDRVSheepdogState *old_s; |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 2449 | char tag[SD_MAX_VDI_TAG_LEN]; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2450 | uint32_t snapid = 0; |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 2451 | int ret = 0; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2452 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 2453 | old_s = g_new(BDRVSheepdogState, 1); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2454 | |
| 2455 | memcpy(old_s, s, sizeof(BDRVSheepdogState)); |
| 2456 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2457 | snapid = strtoul(snapshot_id, NULL, 10); |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 2458 | if (snapid) { |
| 2459 | tag[0] = 0; |
| 2460 | } else { |
Liu Yuan | b579ffb | 2013-06-08 01:54:25 +0800 | [diff] [blame] | 2461 | pstrcpy(tag, sizeof(tag), snapshot_id); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2462 | } |
| 2463 | |
MORITA Kazutaka | 9ff53a0 | 2013-04-26 01:19:53 +0900 | [diff] [blame] | 2464 | ret = reload_inode(s, snapid, tag); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2465 | if (ret) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2466 | goto out; |
| 2467 | } |
| 2468 | |
Liu Yuan | cede621 | 2013-06-08 01:54:26 +0800 | [diff] [blame] | 2469 | ret = sd_create_branch(s); |
| 2470 | if (ret) { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2471 | goto out; |
| 2472 | } |
| 2473 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2474 | g_free(old_s); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2475 | |
| 2476 | return 0; |
| 2477 | out: |
| 2478 | /* recover bdrv_sd_state */ |
| 2479 | memcpy(s, old_s, sizeof(BDRVSheepdogState)); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2480 | g_free(old_s); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2481 | |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2482 | error_report("failed to open. recover old bdrv_sd_state."); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2483 | |
| 2484 | return ret; |
| 2485 | } |
| 2486 | |
Vasiliy Tolstov | eab8eb8 | 2015-12-23 21:22:26 +0900 | [diff] [blame] | 2487 | #define NR_BATCHED_DISCARD 128 |
| 2488 | |
| 2489 | static bool remove_objects(BDRVSheepdogState *s) |
| 2490 | { |
| 2491 | int fd, i = 0, nr_objs = 0; |
| 2492 | Error *local_err = NULL; |
| 2493 | int ret = 0; |
| 2494 | bool result = true; |
| 2495 | SheepdogInode *inode = &s->inode; |
| 2496 | |
| 2497 | fd = connect_to_sdog(s, &local_err); |
| 2498 | if (fd < 0) { |
| 2499 | error_report_err(local_err); |
| 2500 | return false; |
| 2501 | } |
| 2502 | |
| 2503 | nr_objs = count_data_objs(inode); |
| 2504 | while (i < nr_objs) { |
| 2505 | int start_idx, nr_filled_idx; |
| 2506 | |
| 2507 | while (i < nr_objs && !inode->data_vdi_id[i]) { |
| 2508 | i++; |
| 2509 | } |
| 2510 | start_idx = i; |
| 2511 | |
| 2512 | nr_filled_idx = 0; |
| 2513 | while (i < nr_objs && nr_filled_idx < NR_BATCHED_DISCARD) { |
| 2514 | if (inode->data_vdi_id[i]) { |
| 2515 | inode->data_vdi_id[i] = 0; |
| 2516 | nr_filled_idx++; |
| 2517 | } |
| 2518 | |
| 2519 | i++; |
| 2520 | } |
| 2521 | |
| 2522 | ret = write_object(fd, s->aio_context, |
| 2523 | (char *)&inode->data_vdi_id[start_idx], |
| 2524 | vid_to_vdi_oid(s->inode.vdi_id), inode->nr_copies, |
| 2525 | (i - start_idx) * sizeof(uint32_t), |
| 2526 | offsetof(struct SheepdogInode, |
| 2527 | data_vdi_id[start_idx]), |
| 2528 | false, s->cache_flags); |
| 2529 | if (ret < 0) { |
| 2530 | error_report("failed to discard snapshot inode."); |
| 2531 | result = false; |
| 2532 | goto out; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | out: |
| 2537 | closesocket(fd); |
| 2538 | return result; |
| 2539 | } |
| 2540 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2541 | static int sd_snapshot_delete(BlockDriverState *bs, |
| 2542 | const char *snapshot_id, |
| 2543 | const char *name, |
| 2544 | Error **errp) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2545 | { |
Vasiliy Tolstov | eab8eb8 | 2015-12-23 21:22:26 +0900 | [diff] [blame] | 2546 | uint32_t snap_id = 0; |
| 2547 | char snap_tag[SD_MAX_VDI_TAG_LEN]; |
| 2548 | Error *local_err = NULL; |
| 2549 | int fd, ret; |
| 2550 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; |
| 2551 | BDRVSheepdogState *s = bs->opaque; |
| 2552 | unsigned int wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN, rlen = 0; |
| 2553 | uint32_t vid; |
| 2554 | SheepdogVdiReq hdr = { |
| 2555 | .opcode = SD_OP_DEL_VDI, |
| 2556 | .data_length = wlen, |
| 2557 | .flags = SD_FLAG_CMD_WRITE, |
| 2558 | }; |
| 2559 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; |
| 2560 | |
| 2561 | if (!remove_objects(s)) { |
| 2562 | return -1; |
| 2563 | } |
| 2564 | |
| 2565 | memset(buf, 0, sizeof(buf)); |
| 2566 | memset(snap_tag, 0, sizeof(snap_tag)); |
| 2567 | pstrcpy(buf, SD_MAX_VDI_LEN, s->name); |
| 2568 | if (qemu_strtoul(snapshot_id, NULL, 10, (unsigned long *)&snap_id)) { |
| 2569 | return -1; |
| 2570 | } |
| 2571 | |
| 2572 | if (snap_id) { |
| 2573 | hdr.snapid = snap_id; |
| 2574 | } else { |
| 2575 | pstrcpy(snap_tag, sizeof(snap_tag), snapshot_id); |
| 2576 | pstrcpy(buf + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag); |
| 2577 | } |
| 2578 | |
| 2579 | ret = find_vdi_name(s, s->name, snap_id, snap_tag, &vid, true, |
| 2580 | &local_err); |
| 2581 | if (ret) { |
| 2582 | return ret; |
| 2583 | } |
| 2584 | |
| 2585 | fd = connect_to_sdog(s, &local_err); |
| 2586 | if (fd < 0) { |
| 2587 | error_report_err(local_err); |
| 2588 | return -1; |
| 2589 | } |
| 2590 | |
| 2591 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, |
| 2592 | buf, &wlen, &rlen); |
| 2593 | closesocket(fd); |
| 2594 | if (ret) { |
| 2595 | return ret; |
| 2596 | } |
| 2597 | |
| 2598 | switch (rsp->result) { |
| 2599 | case SD_RES_NO_VDI: |
| 2600 | error_report("%s was already deleted", s->name); |
| 2601 | case SD_RES_SUCCESS: |
| 2602 | break; |
| 2603 | default: |
| 2604 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
| 2605 | return -1; |
| 2606 | } |
| 2607 | |
| 2608 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2609 | } |
| 2610 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2611 | static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
| 2612 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2613 | Error *local_err = NULL; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2614 | BDRVSheepdogState *s = bs->opaque; |
| 2615 | SheepdogReq req; |
| 2616 | int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long); |
| 2617 | QEMUSnapshotInfo *sn_tab = NULL; |
| 2618 | unsigned wlen, rlen; |
| 2619 | int found = 0; |
| 2620 | static SheepdogInode inode; |
| 2621 | unsigned long *vdi_inuse; |
| 2622 | unsigned int start_nr; |
| 2623 | uint64_t hval; |
| 2624 | uint32_t vid; |
| 2625 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2626 | vdi_inuse = g_malloc(max); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2627 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2628 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2629 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2630 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2631 | ret = fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2632 | goto out; |
| 2633 | } |
| 2634 | |
| 2635 | rlen = max; |
| 2636 | wlen = 0; |
| 2637 | |
| 2638 | memset(&req, 0, sizeof(req)); |
| 2639 | |
| 2640 | req.opcode = SD_OP_READ_VDIS; |
| 2641 | req.data_length = max; |
| 2642 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2643 | ret = do_req(fd, s->aio_context, (SheepdogReq *)&req, |
| 2644 | vdi_inuse, &wlen, &rlen); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2645 | |
| 2646 | closesocket(fd); |
| 2647 | if (ret) { |
| 2648 | goto out; |
| 2649 | } |
| 2650 | |
Markus Armbruster | 02c4f26 | 2014-08-19 10:31:09 +0200 | [diff] [blame] | 2651 | sn_tab = g_new0(QEMUSnapshotInfo, nr); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2652 | |
| 2653 | /* calculate a vdi id with hash function */ |
| 2654 | hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT); |
| 2655 | start_nr = hval & (SD_NR_VDIS - 1); |
| 2656 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2657 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2658 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2659 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2660 | ret = fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2661 | goto out; |
| 2662 | } |
| 2663 | |
| 2664 | for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) { |
| 2665 | if (!test_bit(vid, vdi_inuse)) { |
| 2666 | break; |
| 2667 | } |
| 2668 | |
| 2669 | /* we don't need to read entire object */ |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2670 | ret = read_object(fd, s->aio_context, (char *)&inode, |
| 2671 | vid_to_vdi_oid(vid), |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2672 | 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 2673 | s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2674 | |
| 2675 | if (ret) { |
| 2676 | continue; |
| 2677 | } |
| 2678 | |
| 2679 | if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) { |
| 2680 | sn_tab[found].date_sec = inode.snap_ctime >> 32; |
| 2681 | sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff; |
| 2682 | sn_tab[found].vm_state_size = inode.vm_state_size; |
| 2683 | sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec; |
| 2684 | |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 2685 | snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), |
| 2686 | "%" PRIu32, inode.snap_id); |
Jim Meyering | 3178e27 | 2012-10-04 13:09:47 +0200 | [diff] [blame] | 2687 | pstrcpy(sn_tab[found].name, |
| 2688 | MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)), |
| 2689 | inode.tag); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2690 | found++; |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | closesocket(fd); |
| 2695 | out: |
| 2696 | *psn_tab = sn_tab; |
| 2697 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2698 | g_free(vdi_inuse); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2699 | |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2700 | if (ret < 0) { |
| 2701 | return ret; |
| 2702 | } |
| 2703 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2704 | return found; |
| 2705 | } |
| 2706 | |
| 2707 | static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data, |
| 2708 | int64_t pos, int size, int load) |
| 2709 | { |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2710 | Error *local_err = NULL; |
MORITA Kazutaka | 2f53680 | 2012-10-07 01:57:14 +0900 | [diff] [blame] | 2711 | bool create; |
| 2712 | int fd, ret = 0, remaining = size; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2713 | unsigned int data_len; |
| 2714 | uint64_t vmstate_oid; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2715 | uint64_t offset; |
Liu Yuan | cede621 | 2013-06-08 01:54:26 +0800 | [diff] [blame] | 2716 | uint32_t vdi_index; |
| 2717 | uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2718 | uint32_t object_size = (UINT32_C(1) << s->inode.block_size_shift); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2719 | |
Markus Armbruster | dfb12bf | 2014-05-16 11:00:19 +0200 | [diff] [blame] | 2720 | fd = connect_to_sdog(s, &local_err); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2721 | if (fd < 0) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2722 | error_report_err(local_err); |
MORITA Kazutaka | cb59588 | 2012-05-17 03:15:33 +0900 | [diff] [blame] | 2723 | return fd; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2724 | } |
| 2725 | |
MORITA Kazutaka | 6f3c714 | 2012-05-30 01:05:15 +0900 | [diff] [blame] | 2726 | while (remaining) { |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2727 | vdi_index = pos / object_size; |
| 2728 | offset = pos % object_size; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2729 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2730 | data_len = MIN(remaining, object_size - offset); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2731 | |
Liu Yuan | cede621 | 2013-06-08 01:54:26 +0800 | [diff] [blame] | 2732 | vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2733 | |
| 2734 | create = (offset == 0); |
| 2735 | if (load) { |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2736 | ret = read_object(fd, s->aio_context, (char *)data, vmstate_oid, |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2737 | s->inode.nr_copies, data_len, offset, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 2738 | s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2739 | } else { |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2740 | ret = write_object(fd, s->aio_context, (char *)data, vmstate_oid, |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2741 | s->inode.nr_copies, data_len, offset, create, |
Liu Yuan | 0e7106d | 2013-01-10 16:03:47 +0800 | [diff] [blame] | 2742 | s->cache_flags); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | if (ret < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2746 | error_report("failed to save vmstate %s", strerror(errno)); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2747 | goto cleanup; |
| 2748 | } |
| 2749 | |
| 2750 | pos += data_len; |
MORITA Kazutaka | 1f7a48d | 2012-08-30 03:39:45 +0900 | [diff] [blame] | 2751 | data += data_len; |
MORITA Kazutaka | 6f3c714 | 2012-05-30 01:05:15 +0900 | [diff] [blame] | 2752 | remaining -= data_len; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2753 | } |
MORITA Kazutaka | 6f3c714 | 2012-05-30 01:05:15 +0900 | [diff] [blame] | 2754 | ret = size; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2755 | cleanup: |
| 2756 | closesocket(fd); |
| 2757 | return ret; |
| 2758 | } |
| 2759 | |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 2760 | static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
| 2761 | int64_t pos) |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2762 | { |
| 2763 | BDRVSheepdogState *s = bs->opaque; |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 2764 | void *buf; |
| 2765 | int ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2766 | |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 2767 | buf = qemu_blockalign(bs, qiov->size); |
| 2768 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); |
| 2769 | ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0); |
| 2770 | qemu_vfree(buf); |
| 2771 | |
| 2772 | return ret; |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data, |
| 2776 | int64_t pos, int size) |
| 2777 | { |
| 2778 | BDRVSheepdogState *s = bs->opaque; |
| 2779 | |
| 2780 | return do_load_save_vmstate(s, data, pos, size, 1); |
| 2781 | } |
| 2782 | |
| 2783 | |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2784 | static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num, |
| 2785 | int nb_sectors) |
| 2786 | { |
| 2787 | SheepdogAIOCB *acb; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2788 | BDRVSheepdogState *s = bs->opaque; |
| 2789 | int ret; |
Hitoshi Mitake | e6fd57e | 2015-09-01 12:03:10 +0900 | [diff] [blame] | 2790 | QEMUIOVector discard_iov; |
| 2791 | struct iovec iov; |
| 2792 | uint32_t zero = 0; |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2793 | |
| 2794 | if (!s->discard_supported) { |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
Hitoshi Mitake | e6fd57e | 2015-09-01 12:03:10 +0900 | [diff] [blame] | 2798 | memset(&discard_iov, 0, sizeof(discard_iov)); |
| 2799 | memset(&iov, 0, sizeof(iov)); |
| 2800 | iov.iov_base = &zero; |
| 2801 | iov.iov_len = sizeof(zero); |
| 2802 | discard_iov.iov = &iov; |
| 2803 | discard_iov.niov = 1; |
| 2804 | acb = sd_aio_setup(bs, &discard_iov, sector_num, nb_sectors); |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2805 | acb->aiocb_type = AIOCB_DISCARD_OBJ; |
| 2806 | acb->aio_done_func = sd_finish_aiocb; |
| 2807 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2808 | retry: |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2809 | if (check_overlapping_aiocb(s, acb)) { |
| 2810 | qemu_co_queue_wait(&s->overlapping_queue); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2811 | goto retry; |
| 2812 | } |
| 2813 | |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2814 | ret = sd_co_rw_vector(acb); |
| 2815 | if (ret <= 0) { |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2816 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2817 | qemu_co_queue_restart_all(&s->overlapping_queue); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 2818 | qemu_aio_unref(acb); |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2819 | return ret; |
| 2820 | } |
| 2821 | |
| 2822 | qemu_coroutine_yield(); |
| 2823 | |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2824 | QLIST_REMOVE(acb, aiocb_siblings); |
Hitoshi Mitake | 498f214 | 2015-09-01 12:03:09 +0900 | [diff] [blame] | 2825 | qemu_co_queue_restart_all(&s->overlapping_queue); |
Hitoshi Mitake | 6a55c82 | 2015-07-18 01:44:24 +0900 | [diff] [blame] | 2826 | |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2827 | return acb->ret; |
| 2828 | } |
| 2829 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 2830 | static coroutine_fn int64_t |
| 2831 | sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2832 | int *pnum, BlockDriverState **file) |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2833 | { |
| 2834 | BDRVSheepdogState *s = bs->opaque; |
| 2835 | SheepdogInode *inode = &s->inode; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2836 | uint32_t object_size = (UINT32_C(1) << inode->block_size_shift); |
Liu Yuan | 9cd7673 | 2014-01-22 01:14:11 +0800 | [diff] [blame] | 2837 | uint64_t offset = sector_num * BDRV_SECTOR_SIZE; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2838 | unsigned long start = offset / object_size, |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2839 | end = DIV_ROUND_UP((sector_num + nb_sectors) * |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2840 | BDRV_SECTOR_SIZE, object_size); |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2841 | unsigned long idx; |
Liu Yuan | 9cd7673 | 2014-01-22 01:14:11 +0800 | [diff] [blame] | 2842 | int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2843 | |
| 2844 | for (idx = start; idx < end; idx++) { |
| 2845 | if (inode->data_vdi_id[idx] == 0) { |
| 2846 | break; |
| 2847 | } |
| 2848 | } |
| 2849 | if (idx == start) { |
| 2850 | /* Get the longest length of unallocated sectors */ |
| 2851 | ret = 0; |
| 2852 | for (idx = start + 1; idx < end; idx++) { |
| 2853 | if (inode->data_vdi_id[idx] != 0) { |
| 2854 | break; |
| 2855 | } |
| 2856 | } |
| 2857 | } |
| 2858 | |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2859 | *pnum = (idx - start) * object_size / BDRV_SECTOR_SIZE; |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2860 | if (*pnum > nb_sectors) { |
| 2861 | *pnum = nb_sectors; |
| 2862 | } |
Fam Zheng | d234c92 | 2016-01-26 11:58:55 +0800 | [diff] [blame] | 2863 | if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) { |
| 2864 | *file = bs; |
| 2865 | } |
Liu Yuan | 8d71c63 | 2013-04-23 14:03:35 +0800 | [diff] [blame] | 2866 | return ret; |
| 2867 | } |
| 2868 | |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 2869 | static int64_t sd_get_allocated_file_size(BlockDriverState *bs) |
| 2870 | { |
| 2871 | BDRVSheepdogState *s = bs->opaque; |
| 2872 | SheepdogInode *inode = &s->inode; |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2873 | uint32_t object_size = (UINT32_C(1) << inode->block_size_shift); |
| 2874 | unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, object_size); |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 2875 | uint64_t size = 0; |
| 2876 | |
| 2877 | for (i = 0; i < last; i++) { |
| 2878 | if (inode->data_vdi_id[i] == 0) { |
| 2879 | continue; |
| 2880 | } |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2881 | size += object_size; |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 2882 | } |
| 2883 | return size; |
| 2884 | } |
| 2885 | |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 2886 | static QemuOptsList sd_create_opts = { |
| 2887 | .name = "sheepdog-create-opts", |
| 2888 | .head = QTAILQ_HEAD_INITIALIZER(sd_create_opts.head), |
| 2889 | .desc = { |
| 2890 | { |
| 2891 | .name = BLOCK_OPT_SIZE, |
| 2892 | .type = QEMU_OPT_SIZE, |
| 2893 | .help = "Virtual disk size" |
| 2894 | }, |
| 2895 | { |
| 2896 | .name = BLOCK_OPT_BACKING_FILE, |
| 2897 | .type = QEMU_OPT_STRING, |
| 2898 | .help = "File name of a base image" |
| 2899 | }, |
| 2900 | { |
| 2901 | .name = BLOCK_OPT_PREALLOC, |
| 2902 | .type = QEMU_OPT_STRING, |
| 2903 | .help = "Preallocation mode (allowed values: off, full)" |
| 2904 | }, |
| 2905 | { |
| 2906 | .name = BLOCK_OPT_REDUNDANCY, |
| 2907 | .type = QEMU_OPT_STRING, |
| 2908 | .help = "Redundancy of the image" |
| 2909 | }, |
Teruaki Ishizaki | 876eb1b | 2015-02-13 18:20:53 +0900 | [diff] [blame] | 2910 | { |
| 2911 | .name = BLOCK_OPT_OBJECT_SIZE, |
| 2912 | .type = QEMU_OPT_SIZE, |
| 2913 | .help = "Object size of the image" |
| 2914 | }, |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 2915 | { /* end of list */ } |
| 2916 | } |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2917 | }; |
| 2918 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2919 | static BlockDriver bdrv_sheepdog = { |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2920 | .format_name = "sheepdog", |
| 2921 | .protocol_name = "sheepdog", |
| 2922 | .instance_size = sizeof(BDRVSheepdogState), |
Benoît Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 2923 | .bdrv_needs_filename = true, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2924 | .bdrv_file_open = sd_open, |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 2925 | .bdrv_reopen_prepare = sd_reopen_prepare, |
| 2926 | .bdrv_reopen_commit = sd_reopen_commit, |
| 2927 | .bdrv_reopen_abort = sd_reopen_abort, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2928 | .bdrv_close = sd_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2929 | .bdrv_create = sd_create, |
Liu Yuan | e4f5c1b | 2013-08-06 14:44:37 +0800 | [diff] [blame] | 2930 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2931 | .bdrv_getlength = sd_getlength, |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 2932 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2933 | .bdrv_truncate = sd_truncate, |
| 2934 | |
MORITA Kazutaka | 2df4624 | 2011-08-12 21:33:15 +0900 | [diff] [blame] | 2935 | .bdrv_co_readv = sd_co_readv, |
| 2936 | .bdrv_co_writev = sd_co_writev, |
Liu Yuan | 47622c4 | 2012-04-04 04:03:58 +0800 | [diff] [blame] | 2937 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2938 | .bdrv_co_discard = sd_co_discard, |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 2939 | .bdrv_co_get_block_status = sd_co_get_block_status, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2940 | |
| 2941 | .bdrv_snapshot_create = sd_snapshot_create, |
| 2942 | .bdrv_snapshot_goto = sd_snapshot_goto, |
| 2943 | .bdrv_snapshot_delete = sd_snapshot_delete, |
| 2944 | .bdrv_snapshot_list = sd_snapshot_list, |
| 2945 | |
| 2946 | .bdrv_save_vmstate = sd_save_vmstate, |
| 2947 | .bdrv_load_vmstate = sd_load_vmstate, |
| 2948 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2949 | .bdrv_detach_aio_context = sd_detach_aio_context, |
| 2950 | .bdrv_attach_aio_context = sd_attach_aio_context, |
| 2951 | |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 2952 | .create_opts = &sd_create_opts, |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 2953 | }; |
| 2954 | |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2955 | static BlockDriver bdrv_sheepdog_tcp = { |
| 2956 | .format_name = "sheepdog", |
| 2957 | .protocol_name = "sheepdog+tcp", |
| 2958 | .instance_size = sizeof(BDRVSheepdogState), |
Benoît Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 2959 | .bdrv_needs_filename = true, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2960 | .bdrv_file_open = sd_open, |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 2961 | .bdrv_reopen_prepare = sd_reopen_prepare, |
| 2962 | .bdrv_reopen_commit = sd_reopen_commit, |
| 2963 | .bdrv_reopen_abort = sd_reopen_abort, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2964 | .bdrv_close = sd_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2965 | .bdrv_create = sd_create, |
Liu Yuan | e4f5c1b | 2013-08-06 14:44:37 +0800 | [diff] [blame] | 2966 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2967 | .bdrv_getlength = sd_getlength, |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 2968 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2969 | .bdrv_truncate = sd_truncate, |
| 2970 | |
| 2971 | .bdrv_co_readv = sd_co_readv, |
| 2972 | .bdrv_co_writev = sd_co_writev, |
| 2973 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 2974 | .bdrv_co_discard = sd_co_discard, |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 2975 | .bdrv_co_get_block_status = sd_co_get_block_status, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2976 | |
| 2977 | .bdrv_snapshot_create = sd_snapshot_create, |
| 2978 | .bdrv_snapshot_goto = sd_snapshot_goto, |
| 2979 | .bdrv_snapshot_delete = sd_snapshot_delete, |
| 2980 | .bdrv_snapshot_list = sd_snapshot_list, |
| 2981 | |
| 2982 | .bdrv_save_vmstate = sd_save_vmstate, |
| 2983 | .bdrv_load_vmstate = sd_load_vmstate, |
| 2984 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 2985 | .bdrv_detach_aio_context = sd_detach_aio_context, |
| 2986 | .bdrv_attach_aio_context = sd_attach_aio_context, |
| 2987 | |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 2988 | .create_opts = &sd_create_opts, |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 2989 | }; |
| 2990 | |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 2991 | static BlockDriver bdrv_sheepdog_unix = { |
| 2992 | .format_name = "sheepdog", |
| 2993 | .protocol_name = "sheepdog+unix", |
| 2994 | .instance_size = sizeof(BDRVSheepdogState), |
Benoît Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 2995 | .bdrv_needs_filename = true, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 2996 | .bdrv_file_open = sd_open, |
Liu Yuan | 4da65c8 | 2015-08-28 10:53:58 +0800 | [diff] [blame] | 2997 | .bdrv_reopen_prepare = sd_reopen_prepare, |
| 2998 | .bdrv_reopen_commit = sd_reopen_commit, |
| 2999 | .bdrv_reopen_abort = sd_reopen_abort, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3000 | .bdrv_close = sd_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 3001 | .bdrv_create = sd_create, |
Peter Lieven | 3ac2162 | 2013-06-28 12:47:42 +0200 | [diff] [blame] | 3002 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3003 | .bdrv_getlength = sd_getlength, |
Liu Yuan | 8582972 | 2013-08-07 16:59:53 +0800 | [diff] [blame] | 3004 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3005 | .bdrv_truncate = sd_truncate, |
| 3006 | |
| 3007 | .bdrv_co_readv = sd_co_readv, |
| 3008 | .bdrv_co_writev = sd_co_writev, |
| 3009 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, |
Liu Yuan | cac8f4a | 2013-04-23 14:03:33 +0800 | [diff] [blame] | 3010 | .bdrv_co_discard = sd_co_discard, |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3011 | .bdrv_co_get_block_status = sd_co_get_block_status, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3012 | |
| 3013 | .bdrv_snapshot_create = sd_snapshot_create, |
| 3014 | .bdrv_snapshot_goto = sd_snapshot_goto, |
| 3015 | .bdrv_snapshot_delete = sd_snapshot_delete, |
| 3016 | .bdrv_snapshot_list = sd_snapshot_list, |
| 3017 | |
| 3018 | .bdrv_save_vmstate = sd_save_vmstate, |
| 3019 | .bdrv_load_vmstate = sd_load_vmstate, |
| 3020 | |
Stefan Hajnoczi | 84390be | 2014-05-08 16:34:52 +0200 | [diff] [blame] | 3021 | .bdrv_detach_aio_context = sd_detach_aio_context, |
| 3022 | .bdrv_attach_aio_context = sd_attach_aio_context, |
| 3023 | |
Chunyan Liu | b222237 | 2014-06-05 17:21:05 +0800 | [diff] [blame] | 3024 | .create_opts = &sd_create_opts, |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3025 | }; |
| 3026 | |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 3027 | static void bdrv_sheepdog_init(void) |
| 3028 | { |
| 3029 | bdrv_register(&bdrv_sheepdog); |
MORITA Kazutaka | 5d6768e | 2013-02-22 12:39:51 +0900 | [diff] [blame] | 3030 | bdrv_register(&bdrv_sheepdog_tcp); |
MORITA Kazutaka | 1b8bbb4 | 2013-02-22 12:39:53 +0900 | [diff] [blame] | 3031 | bdrv_register(&bdrv_sheepdog_unix); |
MORITA Kazutaka | 33b1db1 | 2010-06-21 05:01:00 +0900 | [diff] [blame] | 3032 | } |
| 3033 | block_init(bdrv_sheepdog_init); |