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