]> git.proxmox.com Git - qemu.git/blob - block/sheepdog.c
pci: refuse empty ROM files
[qemu.git] / block / sheepdog.c
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/>.
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.
13 */
14
15 #include "qemu-common.h"
16 #include "qemu/uri.h"
17 #include "qemu/error-report.h"
18 #include "qemu/sockets.h"
19 #include "block/block_int.h"
20 #include "qemu/bitops.h"
21
22 #define SD_PROTO_VER 0x01
23
24 #define SD_DEFAULT_ADDR "localhost"
25 #define SD_DEFAULT_PORT 7000
26
27 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
28 #define SD_OP_READ_OBJ 0x02
29 #define SD_OP_WRITE_OBJ 0x03
30
31 #define SD_OP_NEW_VDI 0x11
32 #define SD_OP_LOCK_VDI 0x12
33 #define SD_OP_RELEASE_VDI 0x13
34 #define SD_OP_GET_VDI_INFO 0x14
35 #define SD_OP_READ_VDIS 0x15
36 #define SD_OP_FLUSH_VDI 0x16
37
38 #define SD_FLAG_CMD_WRITE 0x01
39 #define SD_FLAG_CMD_COW 0x02
40 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
41 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
42
43 #define SD_RES_SUCCESS 0x00 /* Success */
44 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
45 #define SD_RES_NO_OBJ 0x02 /* No object found */
46 #define SD_RES_EIO 0x03 /* I/O error */
47 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
48 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
49 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
50 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
51 #define SD_RES_NO_VDI 0x08 /* No vdi found */
52 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
53 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
54 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
55 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
56 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
57 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
58 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
59 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
60 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
61 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
62 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
63 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
64 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
65 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
66 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
67 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
68
69 /*
70 * Object ID rules
71 *
72 * 0 - 19 (20 bits): data object space
73 * 20 - 31 (12 bits): reserved data object space
74 * 32 - 55 (24 bits): vdi object space
75 * 56 - 59 ( 4 bits): reserved vdi object space
76 * 60 - 63 ( 4 bits): object type identifier space
77 */
78
79 #define VDI_SPACE_SHIFT 32
80 #define VDI_BIT (UINT64_C(1) << 63)
81 #define VMSTATE_BIT (UINT64_C(1) << 62)
82 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
83 #define MAX_CHILDREN 1024
84 #define SD_MAX_VDI_LEN 256
85 #define SD_MAX_VDI_TAG_LEN 256
86 #define SD_NR_VDIS (1U << 24)
87 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
88 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
89 #define SECTOR_SIZE 512
90
91 #define SD_INODE_SIZE (sizeof(SheepdogInode))
92 #define CURRENT_VDI_ID 0
93
94 typedef struct SheepdogReq {
95 uint8_t proto_ver;
96 uint8_t opcode;
97 uint16_t flags;
98 uint32_t epoch;
99 uint32_t id;
100 uint32_t data_length;
101 uint32_t opcode_specific[8];
102 } SheepdogReq;
103
104 typedef struct SheepdogRsp {
105 uint8_t proto_ver;
106 uint8_t opcode;
107 uint16_t flags;
108 uint32_t epoch;
109 uint32_t id;
110 uint32_t data_length;
111 uint32_t result;
112 uint32_t opcode_specific[7];
113 } SheepdogRsp;
114
115 typedef struct SheepdogObjReq {
116 uint8_t proto_ver;
117 uint8_t opcode;
118 uint16_t flags;
119 uint32_t epoch;
120 uint32_t id;
121 uint32_t data_length;
122 uint64_t oid;
123 uint64_t cow_oid;
124 uint32_t copies;
125 uint32_t rsvd;
126 uint64_t offset;
127 } SheepdogObjReq;
128
129 typedef struct SheepdogObjRsp {
130 uint8_t proto_ver;
131 uint8_t opcode;
132 uint16_t flags;
133 uint32_t epoch;
134 uint32_t id;
135 uint32_t data_length;
136 uint32_t result;
137 uint32_t copies;
138 uint32_t pad[6];
139 } SheepdogObjRsp;
140
141 typedef struct SheepdogVdiReq {
142 uint8_t proto_ver;
143 uint8_t opcode;
144 uint16_t flags;
145 uint32_t epoch;
146 uint32_t id;
147 uint32_t data_length;
148 uint64_t vdi_size;
149 uint32_t vdi_id;
150 uint32_t copies;
151 uint32_t snapid;
152 uint32_t pad[3];
153 } SheepdogVdiReq;
154
155 typedef struct SheepdogVdiRsp {
156 uint8_t proto_ver;
157 uint8_t opcode;
158 uint16_t flags;
159 uint32_t epoch;
160 uint32_t id;
161 uint32_t data_length;
162 uint32_t result;
163 uint32_t rsvd;
164 uint32_t vdi_id;
165 uint32_t pad[5];
166 } SheepdogVdiRsp;
167
168 typedef struct SheepdogInode {
169 char name[SD_MAX_VDI_LEN];
170 char tag[SD_MAX_VDI_TAG_LEN];
171 uint64_t ctime;
172 uint64_t snap_ctime;
173 uint64_t vm_clock_nsec;
174 uint64_t vdi_size;
175 uint64_t vm_state_size;
176 uint16_t copy_policy;
177 uint8_t nr_copies;
178 uint8_t block_size_shift;
179 uint32_t snap_id;
180 uint32_t vdi_id;
181 uint32_t parent_vdi_id;
182 uint32_t child_vdi_id[MAX_CHILDREN];
183 uint32_t data_vdi_id[MAX_DATA_OBJS];
184 } SheepdogInode;
185
186 /*
187 * 64 bit FNV-1a non-zero initial basis
188 */
189 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
190
191 /*
192 * 64 bit Fowler/Noll/Vo FNV-1a hash code
193 */
194 static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
195 {
196 unsigned char *bp = buf;
197 unsigned char *be = bp + len;
198 while (bp < be) {
199 hval ^= (uint64_t) *bp++;
200 hval += (hval << 1) + (hval << 4) + (hval << 5) +
201 (hval << 7) + (hval << 8) + (hval << 40);
202 }
203 return hval;
204 }
205
206 static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
207 {
208 return inode->vdi_id == inode->data_vdi_id[idx];
209 }
210
211 static inline bool is_data_obj(uint64_t oid)
212 {
213 return !(VDI_BIT & oid);
214 }
215
216 static inline uint64_t data_oid_to_idx(uint64_t oid)
217 {
218 return oid & (MAX_DATA_OBJS - 1);
219 }
220
221 static inline uint64_t vid_to_vdi_oid(uint32_t vid)
222 {
223 return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
224 }
225
226 static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
227 {
228 return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
229 }
230
231 static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
232 {
233 return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
234 }
235
236 static inline bool is_snapshot(struct SheepdogInode *inode)
237 {
238 return !!inode->snap_ctime;
239 }
240
241 #undef dprintf
242 #ifdef DEBUG_SDOG
243 #define dprintf(fmt, args...) \
244 do { \
245 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
246 } while (0)
247 #else
248 #define dprintf(fmt, args...)
249 #endif
250
251 typedef struct SheepdogAIOCB SheepdogAIOCB;
252
253 typedef struct AIOReq {
254 SheepdogAIOCB *aiocb;
255 unsigned int iov_offset;
256
257 uint64_t oid;
258 uint64_t base_oid;
259 uint64_t offset;
260 unsigned int data_len;
261 uint8_t flags;
262 uint32_t id;
263
264 QLIST_ENTRY(AIOReq) aio_siblings;
265 } AIOReq;
266
267 enum AIOCBState {
268 AIOCB_WRITE_UDATA,
269 AIOCB_READ_UDATA,
270 AIOCB_FLUSH_CACHE,
271 };
272
273 struct SheepdogAIOCB {
274 BlockDriverAIOCB common;
275
276 QEMUIOVector *qiov;
277
278 int64_t sector_num;
279 int nb_sectors;
280
281 int ret;
282 enum AIOCBState aiocb_type;
283
284 Coroutine *coroutine;
285 void (*aio_done_func)(SheepdogAIOCB *);
286
287 bool canceled;
288 int nr_pending;
289 };
290
291 typedef struct BDRVSheepdogState {
292 SheepdogInode inode;
293
294 uint32_t min_dirty_data_idx;
295 uint32_t max_dirty_data_idx;
296
297 char name[SD_MAX_VDI_LEN];
298 bool is_snapshot;
299 uint32_t cache_flags;
300
301 char *host_spec;
302 bool is_unix;
303 int fd;
304
305 CoMutex lock;
306 Coroutine *co_send;
307 Coroutine *co_recv;
308
309 uint32_t aioreq_seq_num;
310 QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
311 QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head;
312 } BDRVSheepdogState;
313
314 static const char * sd_strerror(int err)
315 {
316 int i;
317
318 static const struct {
319 int err;
320 const char *desc;
321 } errors[] = {
322 {SD_RES_SUCCESS, "Success"},
323 {SD_RES_UNKNOWN, "Unknown error"},
324 {SD_RES_NO_OBJ, "No object found"},
325 {SD_RES_EIO, "I/O error"},
326 {SD_RES_VDI_EXIST, "VDI exists already"},
327 {SD_RES_INVALID_PARMS, "Invalid parameters"},
328 {SD_RES_SYSTEM_ERROR, "System error"},
329 {SD_RES_VDI_LOCKED, "VDI is already locked"},
330 {SD_RES_NO_VDI, "No vdi found"},
331 {SD_RES_NO_BASE_VDI, "No base VDI found"},
332 {SD_RES_VDI_READ, "Failed read the requested VDI"},
333 {SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
334 {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
335 {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
336 {SD_RES_NO_TAG, "Failed to find the requested tag"},
337 {SD_RES_STARTUP, "The system is still booting"},
338 {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
339 {SD_RES_SHUTDOWN, "The system is shutting down"},
340 {SD_RES_NO_MEM, "Out of memory on the server"},
341 {SD_RES_FULL_VDI, "We already have the maximum vdis"},
342 {SD_RES_VER_MISMATCH, "Protocol version mismatch"},
343 {SD_RES_NO_SPACE, "Server has no space for new objects"},
344 {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
345 {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
346 {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
347 };
348
349 for (i = 0; i < ARRAY_SIZE(errors); ++i) {
350 if (errors[i].err == err) {
351 return errors[i].desc;
352 }
353 }
354
355 return "Invalid error code";
356 }
357
358 /*
359 * Sheepdog I/O handling:
360 *
361 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
362 * link the requests to the inflight_list in the
363 * BDRVSheepdogState. The function exits without waiting for
364 * receiving the response.
365 *
366 * 2. We receive the response in aio_read_response, the fd handler to
367 * the sheepdog connection. If metadata update is needed, we send
368 * the write request to the vdi object in sd_write_done, the write
369 * completion function. We switch back to sd_co_readv/writev after
370 * all the requests belonging to the AIOCB are finished.
371 */
372
373 static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
374 uint64_t oid, unsigned int data_len,
375 uint64_t offset, uint8_t flags,
376 uint64_t base_oid, unsigned int iov_offset)
377 {
378 AIOReq *aio_req;
379
380 aio_req = g_malloc(sizeof(*aio_req));
381 aio_req->aiocb = acb;
382 aio_req->iov_offset = iov_offset;
383 aio_req->oid = oid;
384 aio_req->base_oid = base_oid;
385 aio_req->offset = offset;
386 aio_req->data_len = data_len;
387 aio_req->flags = flags;
388 aio_req->id = s->aioreq_seq_num++;
389
390 acb->nr_pending++;
391 return aio_req;
392 }
393
394 static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req)
395 {
396 SheepdogAIOCB *acb = aio_req->aiocb;
397
398 QLIST_REMOVE(aio_req, aio_siblings);
399 g_free(aio_req);
400
401 acb->nr_pending--;
402 }
403
404 static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb)
405 {
406 if (!acb->canceled) {
407 qemu_coroutine_enter(acb->coroutine, NULL);
408 }
409 qemu_aio_release(acb);
410 }
411
412 static void sd_aio_cancel(BlockDriverAIOCB *blockacb)
413 {
414 SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb;
415
416 /*
417 * Sheepdog cannot cancel the requests which are already sent to
418 * the servers, so we just complete the request with -EIO here.
419 */
420 acb->ret = -EIO;
421 qemu_coroutine_enter(acb->coroutine, NULL);
422 acb->canceled = true;
423 }
424
425 static const AIOCBInfo sd_aiocb_info = {
426 .aiocb_size = sizeof(SheepdogAIOCB),
427 .cancel = sd_aio_cancel,
428 };
429
430 static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
431 int64_t sector_num, int nb_sectors)
432 {
433 SheepdogAIOCB *acb;
434
435 acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL);
436
437 acb->qiov = qiov;
438
439 acb->sector_num = sector_num;
440 acb->nb_sectors = nb_sectors;
441
442 acb->aio_done_func = NULL;
443 acb->canceled = false;
444 acb->coroutine = qemu_coroutine_self();
445 acb->ret = 0;
446 acb->nr_pending = 0;
447 return acb;
448 }
449
450 static int connect_to_sdog(BDRVSheepdogState *s)
451 {
452 int fd;
453 Error *err = NULL;
454
455 if (s->is_unix) {
456 fd = unix_connect(s->host_spec, &err);
457 } else {
458 fd = inet_connect(s->host_spec, &err);
459
460 if (err == NULL) {
461 int ret = socket_set_nodelay(fd);
462 if (ret < 0) {
463 error_report("%s", strerror(errno));
464 }
465 }
466 }
467
468 if (err != NULL) {
469 qerror_report_err(err);
470 error_free(err);
471 } else {
472 socket_set_nonblock(fd);
473 }
474
475 return fd;
476 }
477
478 static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
479 unsigned int *wlen)
480 {
481 int ret;
482
483 ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
484 if (ret < sizeof(*hdr)) {
485 error_report("failed to send a req, %s", strerror(errno));
486 return ret;
487 }
488
489 ret = qemu_co_send(sockfd, data, *wlen);
490 if (ret < *wlen) {
491 error_report("failed to send a req, %s", strerror(errno));
492 }
493
494 return ret;
495 }
496
497 static void restart_co_req(void *opaque)
498 {
499 Coroutine *co = opaque;
500
501 qemu_coroutine_enter(co, NULL);
502 }
503
504 static int have_co_req(void *opaque)
505 {
506 /* this handler is set only when there is a pending request, so
507 * always returns 1. */
508 return 1;
509 }
510
511 typedef struct SheepdogReqCo {
512 int sockfd;
513 SheepdogReq *hdr;
514 void *data;
515 unsigned int *wlen;
516 unsigned int *rlen;
517 int ret;
518 bool finished;
519 } SheepdogReqCo;
520
521 static coroutine_fn void do_co_req(void *opaque)
522 {
523 int ret;
524 Coroutine *co;
525 SheepdogReqCo *srco = opaque;
526 int sockfd = srco->sockfd;
527 SheepdogReq *hdr = srco->hdr;
528 void *data = srco->data;
529 unsigned int *wlen = srco->wlen;
530 unsigned int *rlen = srco->rlen;
531
532 co = qemu_coroutine_self();
533 qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, have_co_req, co);
534
535 ret = send_co_req(sockfd, hdr, data, wlen);
536 if (ret < 0) {
537 goto out;
538 }
539
540 qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, have_co_req, co);
541
542 ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
543 if (ret < sizeof(*hdr)) {
544 error_report("failed to get a rsp, %s", strerror(errno));
545 ret = -errno;
546 goto out;
547 }
548
549 if (*rlen > hdr->data_length) {
550 *rlen = hdr->data_length;
551 }
552
553 if (*rlen) {
554 ret = qemu_co_recv(sockfd, data, *rlen);
555 if (ret < *rlen) {
556 error_report("failed to get the data, %s", strerror(errno));
557 ret = -errno;
558 goto out;
559 }
560 }
561 ret = 0;
562 out:
563 /* there is at most one request for this sockfd, so it is safe to
564 * set each handler to NULL. */
565 qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL, NULL);
566
567 srco->ret = ret;
568 srco->finished = true;
569 }
570
571 static int do_req(int sockfd, SheepdogReq *hdr, void *data,
572 unsigned int *wlen, unsigned int *rlen)
573 {
574 Coroutine *co;
575 SheepdogReqCo srco = {
576 .sockfd = sockfd,
577 .hdr = hdr,
578 .data = data,
579 .wlen = wlen,
580 .rlen = rlen,
581 .ret = 0,
582 .finished = false,
583 };
584
585 if (qemu_in_coroutine()) {
586 do_co_req(&srco);
587 } else {
588 co = qemu_coroutine_create(do_co_req);
589 qemu_coroutine_enter(co, &srco);
590 while (!srco.finished) {
591 qemu_aio_wait();
592 }
593 }
594
595 return srco.ret;
596 }
597
598 static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
599 struct iovec *iov, int niov, bool create,
600 enum AIOCBState aiocb_type);
601
602
603 static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid)
604 {
605 AIOReq *aio_req;
606
607 QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) {
608 if (aio_req->oid == oid) {
609 return aio_req;
610 }
611 }
612
613 return NULL;
614 }
615
616 /*
617 * This function searchs pending requests to the object `oid', and
618 * sends them.
619 */
620 static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid)
621 {
622 AIOReq *aio_req;
623 SheepdogAIOCB *acb;
624 int ret;
625
626 while ((aio_req = find_pending_req(s, oid)) != NULL) {
627 acb = aio_req->aiocb;
628 /* move aio_req from pending list to inflight one */
629 QLIST_REMOVE(aio_req, aio_siblings);
630 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
631 ret = add_aio_request(s, aio_req, acb->qiov->iov,
632 acb->qiov->niov, false, acb->aiocb_type);
633 if (ret < 0) {
634 error_report("add_aio_request is failed");
635 free_aio_req(s, aio_req);
636 if (!acb->nr_pending) {
637 sd_finish_aiocb(acb);
638 }
639 }
640 }
641 }
642
643 /*
644 * Receive responses of the I/O requests.
645 *
646 * This function is registered as a fd handler, and called from the
647 * main loop when s->fd is ready for reading responses.
648 */
649 static void coroutine_fn aio_read_response(void *opaque)
650 {
651 SheepdogObjRsp rsp;
652 BDRVSheepdogState *s = opaque;
653 int fd = s->fd;
654 int ret;
655 AIOReq *aio_req = NULL;
656 SheepdogAIOCB *acb;
657 unsigned long idx;
658
659 if (QLIST_EMPTY(&s->inflight_aio_head)) {
660 goto out;
661 }
662
663 /* read a header */
664 ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
665 if (ret < 0) {
666 error_report("failed to get the header, %s", strerror(errno));
667 goto out;
668 }
669
670 /* find the right aio_req from the inflight aio list */
671 QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
672 if (aio_req->id == rsp.id) {
673 break;
674 }
675 }
676 if (!aio_req) {
677 error_report("cannot find aio_req %x", rsp.id);
678 goto out;
679 }
680
681 acb = aio_req->aiocb;
682
683 switch (acb->aiocb_type) {
684 case AIOCB_WRITE_UDATA:
685 /* this coroutine context is no longer suitable for co_recv
686 * because we may send data to update vdi objects */
687 s->co_recv = NULL;
688 if (!is_data_obj(aio_req->oid)) {
689 break;
690 }
691 idx = data_oid_to_idx(aio_req->oid);
692
693 if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) {
694 /*
695 * If the object is newly created one, we need to update
696 * the vdi object (metadata object). min_dirty_data_idx
697 * and max_dirty_data_idx are changed to include updated
698 * index between them.
699 */
700 if (rsp.result == SD_RES_SUCCESS) {
701 s->inode.data_vdi_id[idx] = s->inode.vdi_id;
702 s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx);
703 s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx);
704 }
705 /*
706 * Some requests may be blocked because simultaneous
707 * create requests are not allowed, so we search the
708 * pending requests here.
709 */
710 send_pending_req(s, aio_req->oid);
711 }
712 break;
713 case AIOCB_READ_UDATA:
714 ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
715 aio_req->iov_offset, rsp.data_length);
716 if (ret < 0) {
717 error_report("failed to get the data, %s", strerror(errno));
718 goto out;
719 }
720 break;
721 case AIOCB_FLUSH_CACHE:
722 if (rsp.result == SD_RES_INVALID_PARMS) {
723 dprintf("disable cache since the server doesn't support it\n");
724 s->cache_flags = SD_FLAG_CMD_DIRECT;
725 rsp.result = SD_RES_SUCCESS;
726 }
727 break;
728 }
729
730 if (rsp.result != SD_RES_SUCCESS) {
731 acb->ret = -EIO;
732 error_report("%s", sd_strerror(rsp.result));
733 }
734
735 free_aio_req(s, aio_req);
736 if (!acb->nr_pending) {
737 /*
738 * We've finished all requests which belong to the AIOCB, so
739 * we can switch back to sd_co_readv/writev now.
740 */
741 acb->aio_done_func(acb);
742 }
743 out:
744 s->co_recv = NULL;
745 }
746
747 static void co_read_response(void *opaque)
748 {
749 BDRVSheepdogState *s = opaque;
750
751 if (!s->co_recv) {
752 s->co_recv = qemu_coroutine_create(aio_read_response);
753 }
754
755 qemu_coroutine_enter(s->co_recv, opaque);
756 }
757
758 static void co_write_request(void *opaque)
759 {
760 BDRVSheepdogState *s = opaque;
761
762 qemu_coroutine_enter(s->co_send, NULL);
763 }
764
765 static int aio_flush_request(void *opaque)
766 {
767 BDRVSheepdogState *s = opaque;
768
769 return !QLIST_EMPTY(&s->inflight_aio_head) ||
770 !QLIST_EMPTY(&s->pending_aio_head);
771 }
772
773 /*
774 * Return a socket discriptor to read/write objects.
775 *
776 * We cannot use this discriptor for other operations because
777 * the block driver may be on waiting response from the server.
778 */
779 static int get_sheep_fd(BDRVSheepdogState *s)
780 {
781 int fd;
782
783 fd = connect_to_sdog(s);
784 if (fd < 0) {
785 return fd;
786 }
787
788 qemu_aio_set_fd_handler(fd, co_read_response, NULL, aio_flush_request, s);
789 return fd;
790 }
791
792 static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
793 char *vdi, uint32_t *snapid, char *tag)
794 {
795 URI *uri;
796 QueryParams *qp = NULL;
797 int ret = 0;
798
799 uri = uri_parse(filename);
800 if (!uri) {
801 return -EINVAL;
802 }
803
804 /* transport */
805 if (!strcmp(uri->scheme, "sheepdog")) {
806 s->is_unix = false;
807 } else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
808 s->is_unix = false;
809 } else if (!strcmp(uri->scheme, "sheepdog+unix")) {
810 s->is_unix = true;
811 } else {
812 ret = -EINVAL;
813 goto out;
814 }
815
816 if (uri->path == NULL || !strcmp(uri->path, "/")) {
817 ret = -EINVAL;
818 goto out;
819 }
820 pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
821
822 qp = query_params_parse(uri->query);
823 if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
824 ret = -EINVAL;
825 goto out;
826 }
827
828 if (s->is_unix) {
829 /* sheepdog+unix:///vdiname?socket=path */
830 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
831 ret = -EINVAL;
832 goto out;
833 }
834 s->host_spec = g_strdup(qp->p[0].value);
835 } else {
836 /* sheepdog[+tcp]://[host:port]/vdiname */
837 s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
838 uri->port ?: SD_DEFAULT_PORT);
839 }
840
841 /* snapshot tag */
842 if (uri->fragment) {
843 *snapid = strtoul(uri->fragment, NULL, 10);
844 if (*snapid == 0) {
845 pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
846 }
847 } else {
848 *snapid = CURRENT_VDI_ID; /* search current vdi */
849 }
850
851 out:
852 if (qp) {
853 query_params_free(qp);
854 }
855 uri_free(uri);
856 return ret;
857 }
858
859 /*
860 * Parse a filename (old syntax)
861 *
862 * filename must be one of the following formats:
863 * 1. [vdiname]
864 * 2. [vdiname]:[snapid]
865 * 3. [vdiname]:[tag]
866 * 4. [hostname]:[port]:[vdiname]
867 * 5. [hostname]:[port]:[vdiname]:[snapid]
868 * 6. [hostname]:[port]:[vdiname]:[tag]
869 *
870 * You can boot from the snapshot images by specifying `snapid` or
871 * `tag'.
872 *
873 * You can run VMs outside the Sheepdog cluster by specifying
874 * `hostname' and `port' (experimental).
875 */
876 static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
877 char *vdi, uint32_t *snapid, char *tag)
878 {
879 char *p, *q, *uri;
880 const char *host_spec, *vdi_spec;
881 int nr_sep, ret;
882
883 strstart(filename, "sheepdog:", (const char **)&filename);
884 p = q = g_strdup(filename);
885
886 /* count the number of separators */
887 nr_sep = 0;
888 while (*p) {
889 if (*p == ':') {
890 nr_sep++;
891 }
892 p++;
893 }
894 p = q;
895
896 /* use the first two tokens as host_spec. */
897 if (nr_sep >= 2) {
898 host_spec = p;
899 p = strchr(p, ':');
900 p++;
901 p = strchr(p, ':');
902 *p++ = '\0';
903 } else {
904 host_spec = "";
905 }
906
907 vdi_spec = p;
908
909 p = strchr(vdi_spec, ':');
910 if (p) {
911 *p++ = '#';
912 }
913
914 uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
915
916 ret = sd_parse_uri(s, uri, vdi, snapid, tag);
917
918 g_free(q);
919 g_free(uri);
920
921 return ret;
922 }
923
924 static int find_vdi_name(BDRVSheepdogState *s, char *filename, uint32_t snapid,
925 char *tag, uint32_t *vid, int for_snapshot)
926 {
927 int ret, fd;
928 SheepdogVdiReq hdr;
929 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
930 unsigned int wlen, rlen = 0;
931 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
932
933 fd = connect_to_sdog(s);
934 if (fd < 0) {
935 return fd;
936 }
937
938 /* This pair of strncpy calls ensures that the buffer is zero-filled,
939 * which is desirable since we'll soon be sending those bytes, and
940 * don't want the send_req to read uninitialized data.
941 */
942 strncpy(buf, filename, SD_MAX_VDI_LEN);
943 strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
944
945 memset(&hdr, 0, sizeof(hdr));
946 if (for_snapshot) {
947 hdr.opcode = SD_OP_GET_VDI_INFO;
948 } else {
949 hdr.opcode = SD_OP_LOCK_VDI;
950 }
951 wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
952 hdr.proto_ver = SD_PROTO_VER;
953 hdr.data_length = wlen;
954 hdr.snapid = snapid;
955 hdr.flags = SD_FLAG_CMD_WRITE;
956
957 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
958 if (ret) {
959 goto out;
960 }
961
962 if (rsp->result != SD_RES_SUCCESS) {
963 error_report("cannot get vdi info, %s, %s %d %s",
964 sd_strerror(rsp->result), filename, snapid, tag);
965 if (rsp->result == SD_RES_NO_VDI) {
966 ret = -ENOENT;
967 } else {
968 ret = -EIO;
969 }
970 goto out;
971 }
972 *vid = rsp->vdi_id;
973
974 ret = 0;
975 out:
976 closesocket(fd);
977 return ret;
978 }
979
980 static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
981 struct iovec *iov, int niov, bool create,
982 enum AIOCBState aiocb_type)
983 {
984 int nr_copies = s->inode.nr_copies;
985 SheepdogObjReq hdr;
986 unsigned int wlen = 0;
987 int ret;
988 uint64_t oid = aio_req->oid;
989 unsigned int datalen = aio_req->data_len;
990 uint64_t offset = aio_req->offset;
991 uint8_t flags = aio_req->flags;
992 uint64_t old_oid = aio_req->base_oid;
993
994 if (!nr_copies) {
995 error_report("bug");
996 }
997
998 memset(&hdr, 0, sizeof(hdr));
999
1000 switch (aiocb_type) {
1001 case AIOCB_FLUSH_CACHE:
1002 hdr.opcode = SD_OP_FLUSH_VDI;
1003 break;
1004 case AIOCB_READ_UDATA:
1005 hdr.opcode = SD_OP_READ_OBJ;
1006 hdr.flags = flags;
1007 break;
1008 case AIOCB_WRITE_UDATA:
1009 if (create) {
1010 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1011 } else {
1012 hdr.opcode = SD_OP_WRITE_OBJ;
1013 }
1014 wlen = datalen;
1015 hdr.flags = SD_FLAG_CMD_WRITE | flags;
1016 break;
1017 }
1018
1019 if (s->cache_flags) {
1020 hdr.flags |= s->cache_flags;
1021 }
1022
1023 hdr.oid = oid;
1024 hdr.cow_oid = old_oid;
1025 hdr.copies = s->inode.nr_copies;
1026
1027 hdr.data_length = datalen;
1028 hdr.offset = offset;
1029
1030 hdr.id = aio_req->id;
1031
1032 qemu_co_mutex_lock(&s->lock);
1033 s->co_send = qemu_coroutine_self();
1034 qemu_aio_set_fd_handler(s->fd, co_read_response, co_write_request,
1035 aio_flush_request, s);
1036 socket_set_cork(s->fd, 1);
1037
1038 /* send a header */
1039 ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
1040 if (ret < 0) {
1041 qemu_co_mutex_unlock(&s->lock);
1042 error_report("failed to send a req, %s", strerror(errno));
1043 return -errno;
1044 }
1045
1046 if (wlen) {
1047 ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
1048 if (ret < 0) {
1049 qemu_co_mutex_unlock(&s->lock);
1050 error_report("failed to send a data, %s", strerror(errno));
1051 return -errno;
1052 }
1053 }
1054
1055 socket_set_cork(s->fd, 0);
1056 qemu_aio_set_fd_handler(s->fd, co_read_response, NULL,
1057 aio_flush_request, s);
1058 qemu_co_mutex_unlock(&s->lock);
1059
1060 return 0;
1061 }
1062
1063 static int read_write_object(int fd, char *buf, uint64_t oid, int copies,
1064 unsigned int datalen, uint64_t offset,
1065 bool write, bool create, uint32_t cache_flags)
1066 {
1067 SheepdogObjReq hdr;
1068 SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
1069 unsigned int wlen, rlen;
1070 int ret;
1071
1072 memset(&hdr, 0, sizeof(hdr));
1073
1074 if (write) {
1075 wlen = datalen;
1076 rlen = 0;
1077 hdr.flags = SD_FLAG_CMD_WRITE;
1078 if (create) {
1079 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1080 } else {
1081 hdr.opcode = SD_OP_WRITE_OBJ;
1082 }
1083 } else {
1084 wlen = 0;
1085 rlen = datalen;
1086 hdr.opcode = SD_OP_READ_OBJ;
1087 }
1088
1089 hdr.flags |= cache_flags;
1090
1091 hdr.oid = oid;
1092 hdr.data_length = datalen;
1093 hdr.offset = offset;
1094 hdr.copies = copies;
1095
1096 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1097 if (ret) {
1098 error_report("failed to send a request to the sheep");
1099 return ret;
1100 }
1101
1102 switch (rsp->result) {
1103 case SD_RES_SUCCESS:
1104 return 0;
1105 default:
1106 error_report("%s", sd_strerror(rsp->result));
1107 return -EIO;
1108 }
1109 }
1110
1111 static int read_object(int fd, char *buf, uint64_t oid, int copies,
1112 unsigned int datalen, uint64_t offset,
1113 uint32_t cache_flags)
1114 {
1115 return read_write_object(fd, buf, oid, copies, datalen, offset, false,
1116 false, cache_flags);
1117 }
1118
1119 static int write_object(int fd, char *buf, uint64_t oid, int copies,
1120 unsigned int datalen, uint64_t offset, bool create,
1121 uint32_t cache_flags)
1122 {
1123 return read_write_object(fd, buf, oid, copies, datalen, offset, true,
1124 create, cache_flags);
1125 }
1126
1127 static int sd_open(BlockDriverState *bs, const char *filename, int flags)
1128 {
1129 int ret, fd;
1130 uint32_t vid = 0;
1131 BDRVSheepdogState *s = bs->opaque;
1132 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1133 uint32_t snapid;
1134 char *buf = NULL;
1135
1136 QLIST_INIT(&s->inflight_aio_head);
1137 QLIST_INIT(&s->pending_aio_head);
1138 s->fd = -1;
1139
1140 memset(vdi, 0, sizeof(vdi));
1141 memset(tag, 0, sizeof(tag));
1142
1143 if (strstr(filename, "://")) {
1144 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1145 } else {
1146 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1147 }
1148 if (ret < 0) {
1149 goto out;
1150 }
1151 s->fd = get_sheep_fd(s);
1152 if (s->fd < 0) {
1153 ret = s->fd;
1154 goto out;
1155 }
1156
1157 ret = find_vdi_name(s, vdi, snapid, tag, &vid, 0);
1158 if (ret) {
1159 goto out;
1160 }
1161
1162 /*
1163 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1164 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1165 */
1166 s->cache_flags = SD_FLAG_CMD_CACHE;
1167 if (flags & BDRV_O_NOCACHE) {
1168 s->cache_flags = SD_FLAG_CMD_DIRECT;
1169 }
1170
1171 if (snapid || tag[0] != '\0') {
1172 dprintf("%" PRIx32 " snapshot inode was open.\n", vid);
1173 s->is_snapshot = true;
1174 }
1175
1176 fd = connect_to_sdog(s);
1177 if (fd < 0) {
1178 ret = fd;
1179 goto out;
1180 }
1181
1182 buf = g_malloc(SD_INODE_SIZE);
1183 ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0,
1184 s->cache_flags);
1185
1186 closesocket(fd);
1187
1188 if (ret) {
1189 goto out;
1190 }
1191
1192 memcpy(&s->inode, buf, sizeof(s->inode));
1193 s->min_dirty_data_idx = UINT32_MAX;
1194 s->max_dirty_data_idx = 0;
1195
1196 bs->total_sectors = s->inode.vdi_size / SECTOR_SIZE;
1197 pstrcpy(s->name, sizeof(s->name), vdi);
1198 qemu_co_mutex_init(&s->lock);
1199 g_free(buf);
1200 return 0;
1201 out:
1202 qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL, NULL);
1203 if (s->fd >= 0) {
1204 closesocket(s->fd);
1205 }
1206 g_free(buf);
1207 return ret;
1208 }
1209
1210 static int do_sd_create(BDRVSheepdogState *s, char *filename, int64_t vdi_size,
1211 uint32_t base_vid, uint32_t *vdi_id, int snapshot)
1212 {
1213 SheepdogVdiReq hdr;
1214 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1215 int fd, ret;
1216 unsigned int wlen, rlen = 0;
1217 char buf[SD_MAX_VDI_LEN];
1218
1219 fd = connect_to_sdog(s);
1220 if (fd < 0) {
1221 return fd;
1222 }
1223
1224 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1225 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1226 */
1227 memset(buf, 0, sizeof(buf));
1228 pstrcpy(buf, sizeof(buf), filename);
1229
1230 memset(&hdr, 0, sizeof(hdr));
1231 hdr.opcode = SD_OP_NEW_VDI;
1232 hdr.vdi_id = base_vid;
1233
1234 wlen = SD_MAX_VDI_LEN;
1235
1236 hdr.flags = SD_FLAG_CMD_WRITE;
1237 hdr.snapid = snapshot;
1238
1239 hdr.data_length = wlen;
1240 hdr.vdi_size = vdi_size;
1241
1242 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1243
1244 closesocket(fd);
1245
1246 if (ret) {
1247 return ret;
1248 }
1249
1250 if (rsp->result != SD_RES_SUCCESS) {
1251 error_report("%s, %s", sd_strerror(rsp->result), filename);
1252 return -EIO;
1253 }
1254
1255 if (vdi_id) {
1256 *vdi_id = rsp->vdi_id;
1257 }
1258
1259 return 0;
1260 }
1261
1262 static int sd_prealloc(const char *filename)
1263 {
1264 BlockDriverState *bs = NULL;
1265 uint32_t idx, max_idx;
1266 int64_t vdi_size;
1267 void *buf = g_malloc0(SD_DATA_OBJ_SIZE);
1268 int ret;
1269
1270 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
1271 if (ret < 0) {
1272 goto out;
1273 }
1274
1275 vdi_size = bdrv_getlength(bs);
1276 if (vdi_size < 0) {
1277 ret = vdi_size;
1278 goto out;
1279 }
1280 max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE);
1281
1282 for (idx = 0; idx < max_idx; idx++) {
1283 /*
1284 * The created image can be a cloned image, so we need to read
1285 * a data from the source image.
1286 */
1287 ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1288 if (ret < 0) {
1289 goto out;
1290 }
1291 ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1292 if (ret < 0) {
1293 goto out;
1294 }
1295 }
1296 out:
1297 if (bs) {
1298 bdrv_delete(bs);
1299 }
1300 g_free(buf);
1301
1302 return ret;
1303 }
1304
1305 static int sd_create(const char *filename, QEMUOptionParameter *options)
1306 {
1307 int ret = 0;
1308 uint32_t vid = 0, base_vid = 0;
1309 int64_t vdi_size = 0;
1310 char *backing_file = NULL;
1311 BDRVSheepdogState *s;
1312 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1313 uint32_t snapid;
1314 bool prealloc = false;
1315
1316 s = g_malloc0(sizeof(BDRVSheepdogState));
1317
1318 memset(vdi, 0, sizeof(vdi));
1319 memset(tag, 0, sizeof(tag));
1320 if (strstr(filename, "://")) {
1321 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1322 } else {
1323 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1324 }
1325 if (ret < 0) {
1326 goto out;
1327 }
1328
1329 while (options && options->name) {
1330 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1331 vdi_size = options->value.n;
1332 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1333 backing_file = options->value.s;
1334 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1335 if (!options->value.s || !strcmp(options->value.s, "off")) {
1336 prealloc = false;
1337 } else if (!strcmp(options->value.s, "full")) {
1338 prealloc = true;
1339 } else {
1340 error_report("Invalid preallocation mode: '%s'",
1341 options->value.s);
1342 ret = -EINVAL;
1343 goto out;
1344 }
1345 }
1346 options++;
1347 }
1348
1349 if (vdi_size > SD_MAX_VDI_SIZE) {
1350 error_report("too big image size");
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354
1355 if (backing_file) {
1356 BlockDriverState *bs;
1357 BDRVSheepdogState *s;
1358 BlockDriver *drv;
1359
1360 /* Currently, only Sheepdog backing image is supported. */
1361 drv = bdrv_find_protocol(backing_file);
1362 if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
1363 error_report("backing_file must be a sheepdog image");
1364 ret = -EINVAL;
1365 goto out;
1366 }
1367
1368 ret = bdrv_file_open(&bs, backing_file, 0);
1369 if (ret < 0) {
1370 goto out;
1371 }
1372
1373 s = bs->opaque;
1374
1375 if (!is_snapshot(&s->inode)) {
1376 error_report("cannot clone from a non snapshot vdi");
1377 bdrv_delete(bs);
1378 ret = -EINVAL;
1379 goto out;
1380 }
1381
1382 base_vid = s->inode.vdi_id;
1383 bdrv_delete(bs);
1384 }
1385
1386 ret = do_sd_create(s, vdi, vdi_size, base_vid, &vid, 0);
1387 if (!prealloc || ret) {
1388 goto out;
1389 }
1390
1391 ret = sd_prealloc(filename);
1392 out:
1393 g_free(s);
1394 return ret;
1395 }
1396
1397 static void sd_close(BlockDriverState *bs)
1398 {
1399 BDRVSheepdogState *s = bs->opaque;
1400 SheepdogVdiReq hdr;
1401 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1402 unsigned int wlen, rlen = 0;
1403 int fd, ret;
1404
1405 dprintf("%s\n", s->name);
1406
1407 fd = connect_to_sdog(s);
1408 if (fd < 0) {
1409 return;
1410 }
1411
1412 memset(&hdr, 0, sizeof(hdr));
1413
1414 hdr.opcode = SD_OP_RELEASE_VDI;
1415 hdr.vdi_id = s->inode.vdi_id;
1416 wlen = strlen(s->name) + 1;
1417 hdr.data_length = wlen;
1418 hdr.flags = SD_FLAG_CMD_WRITE;
1419
1420 ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
1421
1422 closesocket(fd);
1423
1424 if (!ret && rsp->result != SD_RES_SUCCESS &&
1425 rsp->result != SD_RES_VDI_NOT_LOCKED) {
1426 error_report("%s, %s", sd_strerror(rsp->result), s->name);
1427 }
1428
1429 qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL, NULL);
1430 closesocket(s->fd);
1431 g_free(s->host_spec);
1432 }
1433
1434 static int64_t sd_getlength(BlockDriverState *bs)
1435 {
1436 BDRVSheepdogState *s = bs->opaque;
1437
1438 return s->inode.vdi_size;
1439 }
1440
1441 static int sd_truncate(BlockDriverState *bs, int64_t offset)
1442 {
1443 BDRVSheepdogState *s = bs->opaque;
1444 int ret, fd;
1445 unsigned int datalen;
1446
1447 if (offset < s->inode.vdi_size) {
1448 error_report("shrinking is not supported");
1449 return -EINVAL;
1450 } else if (offset > SD_MAX_VDI_SIZE) {
1451 error_report("too big image size");
1452 return -EINVAL;
1453 }
1454
1455 fd = connect_to_sdog(s);
1456 if (fd < 0) {
1457 return fd;
1458 }
1459
1460 /* we don't need to update entire object */
1461 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1462 s->inode.vdi_size = offset;
1463 ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
1464 s->inode.nr_copies, datalen, 0, false, s->cache_flags);
1465 close(fd);
1466
1467 if (ret < 0) {
1468 error_report("failed to update an inode.");
1469 }
1470
1471 return ret;
1472 }
1473
1474 /*
1475 * This function is called after writing data objects. If we need to
1476 * update metadata, this sends a write request to the vdi object.
1477 * Otherwise, this switches back to sd_co_readv/writev.
1478 */
1479 static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
1480 {
1481 int ret;
1482 BDRVSheepdogState *s = acb->common.bs->opaque;
1483 struct iovec iov;
1484 AIOReq *aio_req;
1485 uint32_t offset, data_len, mn, mx;
1486
1487 mn = s->min_dirty_data_idx;
1488 mx = s->max_dirty_data_idx;
1489 if (mn <= mx) {
1490 /* we need to update the vdi object. */
1491 offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
1492 mn * sizeof(s->inode.data_vdi_id[0]);
1493 data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
1494
1495 s->min_dirty_data_idx = UINT32_MAX;
1496 s->max_dirty_data_idx = 0;
1497
1498 iov.iov_base = &s->inode;
1499 iov.iov_len = sizeof(s->inode);
1500 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1501 data_len, offset, 0, 0, offset);
1502 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1503 ret = add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
1504 if (ret) {
1505 free_aio_req(s, aio_req);
1506 acb->ret = -EIO;
1507 goto out;
1508 }
1509
1510 acb->aio_done_func = sd_finish_aiocb;
1511 acb->aiocb_type = AIOCB_WRITE_UDATA;
1512 return;
1513 }
1514 out:
1515 sd_finish_aiocb(acb);
1516 }
1517
1518 /*
1519 * Create a writable VDI from a snapshot
1520 */
1521 static int sd_create_branch(BDRVSheepdogState *s)
1522 {
1523 int ret, fd;
1524 uint32_t vid;
1525 char *buf;
1526
1527 dprintf("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
1528
1529 buf = g_malloc(SD_INODE_SIZE);
1530
1531 ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &vid, 1);
1532 if (ret) {
1533 goto out;
1534 }
1535
1536 dprintf("%" PRIx32 " is created.\n", vid);
1537
1538 fd = connect_to_sdog(s);
1539 if (fd < 0) {
1540 ret = fd;
1541 goto out;
1542 }
1543
1544 ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,
1545 SD_INODE_SIZE, 0, s->cache_flags);
1546
1547 closesocket(fd);
1548
1549 if (ret < 0) {
1550 goto out;
1551 }
1552
1553 memcpy(&s->inode, buf, sizeof(s->inode));
1554
1555 s->is_snapshot = false;
1556 ret = 0;
1557 dprintf("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
1558
1559 out:
1560 g_free(buf);
1561
1562 return ret;
1563 }
1564
1565 /*
1566 * Send I/O requests to the server.
1567 *
1568 * This function sends requests to the server, links the requests to
1569 * the inflight_list in BDRVSheepdogState, and exits without
1570 * waiting the response. The responses are received in the
1571 * `aio_read_response' function which is called from the main loop as
1572 * a fd handler.
1573 *
1574 * Returns 1 when we need to wait a response, 0 when there is no sent
1575 * request and -errno in error cases.
1576 */
1577 static int coroutine_fn sd_co_rw_vector(void *p)
1578 {
1579 SheepdogAIOCB *acb = p;
1580 int ret = 0;
1581 unsigned long len, done = 0, total = acb->nb_sectors * SECTOR_SIZE;
1582 unsigned long idx = acb->sector_num * SECTOR_SIZE / SD_DATA_OBJ_SIZE;
1583 uint64_t oid;
1584 uint64_t offset = (acb->sector_num * SECTOR_SIZE) % SD_DATA_OBJ_SIZE;
1585 BDRVSheepdogState *s = acb->common.bs->opaque;
1586 SheepdogInode *inode = &s->inode;
1587 AIOReq *aio_req;
1588
1589 if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
1590 /*
1591 * In the case we open the snapshot VDI, Sheepdog creates the
1592 * writable VDI when we do a write operation first.
1593 */
1594 ret = sd_create_branch(s);
1595 if (ret) {
1596 acb->ret = -EIO;
1597 goto out;
1598 }
1599 }
1600
1601 /*
1602 * Make sure we don't free the aiocb before we are done with all requests.
1603 * This additional reference is dropped at the end of this function.
1604 */
1605 acb->nr_pending++;
1606
1607 while (done != total) {
1608 uint8_t flags = 0;
1609 uint64_t old_oid = 0;
1610 bool create = false;
1611
1612 oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
1613
1614 len = MIN(total - done, SD_DATA_OBJ_SIZE - offset);
1615
1616 switch (acb->aiocb_type) {
1617 case AIOCB_READ_UDATA:
1618 if (!inode->data_vdi_id[idx]) {
1619 qemu_iovec_memset(acb->qiov, done, 0, len);
1620 goto done;
1621 }
1622 break;
1623 case AIOCB_WRITE_UDATA:
1624 if (!inode->data_vdi_id[idx]) {
1625 create = true;
1626 } else if (!is_data_obj_writable(inode, idx)) {
1627 /* Copy-On-Write */
1628 create = true;
1629 old_oid = oid;
1630 flags = SD_FLAG_CMD_COW;
1631 }
1632 break;
1633 default:
1634 break;
1635 }
1636
1637 if (create) {
1638 dprintf("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
1639 inode->vdi_id, oid,
1640 vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
1641 oid = vid_to_data_oid(inode->vdi_id, idx);
1642 dprintf("new oid %" PRIx64 "\n", oid);
1643 }
1644
1645 aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done);
1646
1647 if (create) {
1648 AIOReq *areq;
1649 QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
1650 if (areq->oid == oid) {
1651 /*
1652 * Sheepdog cannot handle simultaneous create
1653 * requests to the same object. So we cannot send
1654 * the request until the previous request
1655 * finishes.
1656 */
1657 aio_req->flags = 0;
1658 aio_req->base_oid = 0;
1659 QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req,
1660 aio_siblings);
1661 goto done;
1662 }
1663 }
1664 }
1665
1666 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1667 ret = add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1668 create, acb->aiocb_type);
1669 if (ret < 0) {
1670 error_report("add_aio_request is failed");
1671 free_aio_req(s, aio_req);
1672 acb->ret = -EIO;
1673 goto out;
1674 }
1675 done:
1676 offset = 0;
1677 idx++;
1678 done += len;
1679 }
1680 out:
1681 if (!--acb->nr_pending) {
1682 return acb->ret;
1683 }
1684 return 1;
1685 }
1686
1687 static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
1688 int nb_sectors, QEMUIOVector *qiov)
1689 {
1690 SheepdogAIOCB *acb;
1691 int ret;
1692
1693 if (bs->growable && sector_num + nb_sectors > bs->total_sectors) {
1694 ret = sd_truncate(bs, (sector_num + nb_sectors) * SECTOR_SIZE);
1695 if (ret < 0) {
1696 return ret;
1697 }
1698 bs->total_sectors = sector_num + nb_sectors;
1699 }
1700
1701 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
1702 acb->aio_done_func = sd_write_done;
1703 acb->aiocb_type = AIOCB_WRITE_UDATA;
1704
1705 ret = sd_co_rw_vector(acb);
1706 if (ret <= 0) {
1707 qemu_aio_release(acb);
1708 return ret;
1709 }
1710
1711 qemu_coroutine_yield();
1712
1713 return acb->ret;
1714 }
1715
1716 static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
1717 int nb_sectors, QEMUIOVector *qiov)
1718 {
1719 SheepdogAIOCB *acb;
1720 int ret;
1721
1722 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
1723 acb->aiocb_type = AIOCB_READ_UDATA;
1724 acb->aio_done_func = sd_finish_aiocb;
1725
1726 ret = sd_co_rw_vector(acb);
1727 if (ret <= 0) {
1728 qemu_aio_release(acb);
1729 return ret;
1730 }
1731
1732 qemu_coroutine_yield();
1733
1734 return acb->ret;
1735 }
1736
1737 static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
1738 {
1739 BDRVSheepdogState *s = bs->opaque;
1740 SheepdogAIOCB *acb;
1741 AIOReq *aio_req;
1742 int ret;
1743
1744 if (s->cache_flags != SD_FLAG_CMD_CACHE) {
1745 return 0;
1746 }
1747
1748 acb = sd_aio_setup(bs, NULL, 0, 0);
1749 acb->aiocb_type = AIOCB_FLUSH_CACHE;
1750 acb->aio_done_func = sd_finish_aiocb;
1751
1752 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1753 0, 0, 0, 0, 0);
1754 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1755 ret = add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type);
1756 if (ret < 0) {
1757 error_report("add_aio_request is failed");
1758 free_aio_req(s, aio_req);
1759 qemu_aio_release(acb);
1760 return ret;
1761 }
1762
1763 qemu_coroutine_yield();
1764 return acb->ret;
1765 }
1766
1767 static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
1768 {
1769 BDRVSheepdogState *s = bs->opaque;
1770 int ret, fd;
1771 uint32_t new_vid;
1772 SheepdogInode *inode;
1773 unsigned int datalen;
1774
1775 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
1776 "is_snapshot %d\n", sn_info->name, sn_info->id_str,
1777 s->name, sn_info->vm_state_size, s->is_snapshot);
1778
1779 if (s->is_snapshot) {
1780 error_report("You can't create a snapshot of a snapshot VDI, "
1781 "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
1782
1783 return -EINVAL;
1784 }
1785
1786 dprintf("%s %s\n", sn_info->name, sn_info->id_str);
1787
1788 s->inode.vm_state_size = sn_info->vm_state_size;
1789 s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
1790 /* It appears that inode.tag does not require a NUL terminator,
1791 * which means this use of strncpy is ok.
1792 */
1793 strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
1794 /* we don't need to update entire object */
1795 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1796
1797 /* refresh inode. */
1798 fd = connect_to_sdog(s);
1799 if (fd < 0) {
1800 ret = fd;
1801 goto cleanup;
1802 }
1803
1804 ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
1805 s->inode.nr_copies, datalen, 0, false, s->cache_flags);
1806 if (ret < 0) {
1807 error_report("failed to write snapshot's inode.");
1808 goto cleanup;
1809 }
1810
1811 ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid,
1812 1);
1813 if (ret < 0) {
1814 error_report("failed to create inode for snapshot. %s",
1815 strerror(errno));
1816 goto cleanup;
1817 }
1818
1819 inode = (SheepdogInode *)g_malloc(datalen);
1820
1821 ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid),
1822 s->inode.nr_copies, datalen, 0, s->cache_flags);
1823
1824 if (ret < 0) {
1825 error_report("failed to read new inode info. %s", strerror(errno));
1826 goto cleanup;
1827 }
1828
1829 memcpy(&s->inode, inode, datalen);
1830 dprintf("s->inode: name %s snap_id %x oid %x\n",
1831 s->inode.name, s->inode.snap_id, s->inode.vdi_id);
1832
1833 cleanup:
1834 closesocket(fd);
1835 return ret;
1836 }
1837
1838 static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
1839 {
1840 BDRVSheepdogState *s = bs->opaque;
1841 BDRVSheepdogState *old_s;
1842 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1843 char *buf = NULL;
1844 uint32_t vid;
1845 uint32_t snapid = 0;
1846 int ret = 0, fd;
1847
1848 old_s = g_malloc(sizeof(BDRVSheepdogState));
1849
1850 memcpy(old_s, s, sizeof(BDRVSheepdogState));
1851
1852 pstrcpy(vdi, sizeof(vdi), s->name);
1853
1854 snapid = strtoul(snapshot_id, NULL, 10);
1855 if (snapid) {
1856 tag[0] = 0;
1857 } else {
1858 pstrcpy(tag, sizeof(tag), s->name);
1859 }
1860
1861 ret = find_vdi_name(s, vdi, snapid, tag, &vid, 1);
1862 if (ret) {
1863 error_report("Failed to find_vdi_name");
1864 goto out;
1865 }
1866
1867 fd = connect_to_sdog(s);
1868 if (fd < 0) {
1869 ret = fd;
1870 goto out;
1871 }
1872
1873 buf = g_malloc(SD_INODE_SIZE);
1874 ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,
1875 SD_INODE_SIZE, 0, s->cache_flags);
1876
1877 closesocket(fd);
1878
1879 if (ret) {
1880 goto out;
1881 }
1882
1883 memcpy(&s->inode, buf, sizeof(s->inode));
1884
1885 if (!s->inode.vm_state_size) {
1886 error_report("Invalid snapshot");
1887 ret = -ENOENT;
1888 goto out;
1889 }
1890
1891 s->is_snapshot = true;
1892
1893 g_free(buf);
1894 g_free(old_s);
1895
1896 return 0;
1897 out:
1898 /* recover bdrv_sd_state */
1899 memcpy(s, old_s, sizeof(BDRVSheepdogState));
1900 g_free(buf);
1901 g_free(old_s);
1902
1903 error_report("failed to open. recover old bdrv_sd_state.");
1904
1905 return ret;
1906 }
1907
1908 static int sd_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1909 {
1910 /* FIXME: Delete specified snapshot id. */
1911 return 0;
1912 }
1913
1914 static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
1915 {
1916 BDRVSheepdogState *s = bs->opaque;
1917 SheepdogReq req;
1918 int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
1919 QEMUSnapshotInfo *sn_tab = NULL;
1920 unsigned wlen, rlen;
1921 int found = 0;
1922 static SheepdogInode inode;
1923 unsigned long *vdi_inuse;
1924 unsigned int start_nr;
1925 uint64_t hval;
1926 uint32_t vid;
1927
1928 vdi_inuse = g_malloc(max);
1929
1930 fd = connect_to_sdog(s);
1931 if (fd < 0) {
1932 ret = fd;
1933 goto out;
1934 }
1935
1936 rlen = max;
1937 wlen = 0;
1938
1939 memset(&req, 0, sizeof(req));
1940
1941 req.opcode = SD_OP_READ_VDIS;
1942 req.data_length = max;
1943
1944 ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen);
1945
1946 closesocket(fd);
1947 if (ret) {
1948 goto out;
1949 }
1950
1951 sn_tab = g_malloc0(nr * sizeof(*sn_tab));
1952
1953 /* calculate a vdi id with hash function */
1954 hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
1955 start_nr = hval & (SD_NR_VDIS - 1);
1956
1957 fd = connect_to_sdog(s);
1958 if (fd < 0) {
1959 ret = fd;
1960 goto out;
1961 }
1962
1963 for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
1964 if (!test_bit(vid, vdi_inuse)) {
1965 break;
1966 }
1967
1968 /* we don't need to read entire object */
1969 ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid),
1970 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
1971 s->cache_flags);
1972
1973 if (ret) {
1974 continue;
1975 }
1976
1977 if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
1978 sn_tab[found].date_sec = inode.snap_ctime >> 32;
1979 sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
1980 sn_tab[found].vm_state_size = inode.vm_state_size;
1981 sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
1982
1983 snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u",
1984 inode.snap_id);
1985 pstrcpy(sn_tab[found].name,
1986 MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
1987 inode.tag);
1988 found++;
1989 }
1990 }
1991
1992 closesocket(fd);
1993 out:
1994 *psn_tab = sn_tab;
1995
1996 g_free(vdi_inuse);
1997
1998 if (ret < 0) {
1999 return ret;
2000 }
2001
2002 return found;
2003 }
2004
2005 static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
2006 int64_t pos, int size, int load)
2007 {
2008 bool create;
2009 int fd, ret = 0, remaining = size;
2010 unsigned int data_len;
2011 uint64_t vmstate_oid;
2012 uint32_t vdi_index;
2013 uint64_t offset;
2014
2015 fd = connect_to_sdog(s);
2016 if (fd < 0) {
2017 return fd;
2018 }
2019
2020 while (remaining) {
2021 vdi_index = pos / SD_DATA_OBJ_SIZE;
2022 offset = pos % SD_DATA_OBJ_SIZE;
2023
2024 data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);
2025
2026 vmstate_oid = vid_to_vmstate_oid(s->inode.vdi_id, vdi_index);
2027
2028 create = (offset == 0);
2029 if (load) {
2030 ret = read_object(fd, (char *)data, vmstate_oid,
2031 s->inode.nr_copies, data_len, offset,
2032 s->cache_flags);
2033 } else {
2034 ret = write_object(fd, (char *)data, vmstate_oid,
2035 s->inode.nr_copies, data_len, offset, create,
2036 s->cache_flags);
2037 }
2038
2039 if (ret < 0) {
2040 error_report("failed to save vmstate %s", strerror(errno));
2041 goto cleanup;
2042 }
2043
2044 pos += data_len;
2045 data += data_len;
2046 remaining -= data_len;
2047 }
2048 ret = size;
2049 cleanup:
2050 closesocket(fd);
2051 return ret;
2052 }
2053
2054 static int sd_save_vmstate(BlockDriverState *bs, const uint8_t *data,
2055 int64_t pos, int size)
2056 {
2057 BDRVSheepdogState *s = bs->opaque;
2058
2059 return do_load_save_vmstate(s, (uint8_t *)data, pos, size, 0);
2060 }
2061
2062 static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
2063 int64_t pos, int size)
2064 {
2065 BDRVSheepdogState *s = bs->opaque;
2066
2067 return do_load_save_vmstate(s, data, pos, size, 1);
2068 }
2069
2070
2071 static QEMUOptionParameter sd_create_options[] = {
2072 {
2073 .name = BLOCK_OPT_SIZE,
2074 .type = OPT_SIZE,
2075 .help = "Virtual disk size"
2076 },
2077 {
2078 .name = BLOCK_OPT_BACKING_FILE,
2079 .type = OPT_STRING,
2080 .help = "File name of a base image"
2081 },
2082 {
2083 .name = BLOCK_OPT_PREALLOC,
2084 .type = OPT_STRING,
2085 .help = "Preallocation mode (allowed values: off, full)"
2086 },
2087 { NULL }
2088 };
2089
2090 static BlockDriver bdrv_sheepdog = {
2091 .format_name = "sheepdog",
2092 .protocol_name = "sheepdog",
2093 .instance_size = sizeof(BDRVSheepdogState),
2094 .bdrv_file_open = sd_open,
2095 .bdrv_close = sd_close,
2096 .bdrv_create = sd_create,
2097 .bdrv_getlength = sd_getlength,
2098 .bdrv_truncate = sd_truncate,
2099
2100 .bdrv_co_readv = sd_co_readv,
2101 .bdrv_co_writev = sd_co_writev,
2102 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2103
2104 .bdrv_snapshot_create = sd_snapshot_create,
2105 .bdrv_snapshot_goto = sd_snapshot_goto,
2106 .bdrv_snapshot_delete = sd_snapshot_delete,
2107 .bdrv_snapshot_list = sd_snapshot_list,
2108
2109 .bdrv_save_vmstate = sd_save_vmstate,
2110 .bdrv_load_vmstate = sd_load_vmstate,
2111
2112 .create_options = sd_create_options,
2113 };
2114
2115 static BlockDriver bdrv_sheepdog_tcp = {
2116 .format_name = "sheepdog",
2117 .protocol_name = "sheepdog+tcp",
2118 .instance_size = sizeof(BDRVSheepdogState),
2119 .bdrv_file_open = sd_open,
2120 .bdrv_close = sd_close,
2121 .bdrv_create = sd_create,
2122 .bdrv_getlength = sd_getlength,
2123 .bdrv_truncate = sd_truncate,
2124
2125 .bdrv_co_readv = sd_co_readv,
2126 .bdrv_co_writev = sd_co_writev,
2127 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2128
2129 .bdrv_snapshot_create = sd_snapshot_create,
2130 .bdrv_snapshot_goto = sd_snapshot_goto,
2131 .bdrv_snapshot_delete = sd_snapshot_delete,
2132 .bdrv_snapshot_list = sd_snapshot_list,
2133
2134 .bdrv_save_vmstate = sd_save_vmstate,
2135 .bdrv_load_vmstate = sd_load_vmstate,
2136
2137 .create_options = sd_create_options,
2138 };
2139
2140 static BlockDriver bdrv_sheepdog_unix = {
2141 .format_name = "sheepdog",
2142 .protocol_name = "sheepdog+unix",
2143 .instance_size = sizeof(BDRVSheepdogState),
2144 .bdrv_file_open = sd_open,
2145 .bdrv_close = sd_close,
2146 .bdrv_create = sd_create,
2147 .bdrv_getlength = sd_getlength,
2148 .bdrv_truncate = sd_truncate,
2149
2150 .bdrv_co_readv = sd_co_readv,
2151 .bdrv_co_writev = sd_co_writev,
2152 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2153
2154 .bdrv_snapshot_create = sd_snapshot_create,
2155 .bdrv_snapshot_goto = sd_snapshot_goto,
2156 .bdrv_snapshot_delete = sd_snapshot_delete,
2157 .bdrv_snapshot_list = sd_snapshot_list,
2158
2159 .bdrv_save_vmstate = sd_save_vmstate,
2160 .bdrv_load_vmstate = sd_load_vmstate,
2161
2162 .create_options = sd_create_options,
2163 };
2164
2165 static void bdrv_sheepdog_init(void)
2166 {
2167 bdrv_register(&bdrv_sheepdog);
2168 bdrv_register(&bdrv_sheepdog_tcp);
2169 bdrv_register(&bdrv_sheepdog_unix);
2170 }
2171 block_init(bdrv_sheepdog_init);