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