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