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