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