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