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