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