]>
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 | |
69 | * 60 - 63 ( 4 bits): object type indentifier space | |
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 | ||
277 | QEMUBH *bh; | |
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 | ||
298 | uint32_t aioreq_seq_num; | |
299 | QLIST_HEAD(outstanding_aio_head, AIOReq) outstanding_aio_head; | |
300 | } BDRVSheepdogState; | |
301 | ||
302 | static const char * sd_strerror(int err) | |
303 | { | |
304 | int i; | |
305 | ||
306 | static const struct { | |
307 | int err; | |
308 | const char *desc; | |
309 | } errors[] = { | |
310 | {SD_RES_SUCCESS, "Success"}, | |
311 | {SD_RES_UNKNOWN, "Unknown error"}, | |
312 | {SD_RES_NO_OBJ, "No object found"}, | |
313 | {SD_RES_EIO, "I/O error"}, | |
314 | {SD_RES_VDI_EXIST, "VDI exists already"}, | |
315 | {SD_RES_INVALID_PARMS, "Invalid parameters"}, | |
316 | {SD_RES_SYSTEM_ERROR, "System error"}, | |
317 | {SD_RES_VDI_LOCKED, "VDI is already locked"}, | |
318 | {SD_RES_NO_VDI, "No vdi found"}, | |
319 | {SD_RES_NO_BASE_VDI, "No base VDI found"}, | |
320 | {SD_RES_VDI_READ, "Failed read the requested VDI"}, | |
321 | {SD_RES_VDI_WRITE, "Failed to write the requested VDI"}, | |
322 | {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"}, | |
323 | {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"}, | |
324 | {SD_RES_NO_TAG, "Failed to find the requested tag"}, | |
325 | {SD_RES_STARTUP, "The system is still booting"}, | |
326 | {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"}, | |
327 | {SD_RES_SHUTDOWN, "The system is shutting down"}, | |
328 | {SD_RES_NO_MEM, "Out of memory on the server"}, | |
329 | {SD_RES_FULL_VDI, "We already have the maximum vdis"}, | |
330 | {SD_RES_VER_MISMATCH, "Protocol version mismatch"}, | |
331 | {SD_RES_NO_SPACE, "Server has no space for new objects"}, | |
332 | {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"}, | |
333 | {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"}, | |
334 | {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"}, | |
335 | }; | |
336 | ||
337 | for (i = 0; i < ARRAY_SIZE(errors); ++i) { | |
338 | if (errors[i].err == err) { | |
339 | return errors[i].desc; | |
340 | } | |
341 | } | |
342 | ||
343 | return "Invalid error code"; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Sheepdog I/O handling: | |
348 | * | |
349 | * 1. In the sd_aio_readv/writev, read/write requests are added to the | |
350 | * QEMU Bottom Halves. | |
351 | * | |
352 | * 2. In sd_readv_writev_bh_cb, the callbacks of BHs, we send the I/O | |
353 | * requests to the server and link the requests to the | |
354 | * outstanding_list in the BDRVSheepdogState. we exits the | |
355 | * function without waiting for receiving the response. | |
356 | * | |
357 | * 3. We receive the response in aio_read_response, the fd handler to | |
358 | * the sheepdog connection. If metadata update is needed, we send | |
359 | * the write request to the vdi object in sd_write_done, the write | |
360 | * completion function. The AIOCB callback is not called until all | |
361 | * the requests belonging to the AIOCB are finished. | |
362 | */ | |
363 | ||
364 | static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, | |
365 | uint64_t oid, unsigned int data_len, | |
366 | uint64_t offset, uint8_t flags, | |
367 | uint64_t base_oid, unsigned int iov_offset) | |
368 | { | |
369 | AIOReq *aio_req; | |
370 | ||
371 | aio_req = qemu_malloc(sizeof(*aio_req)); | |
372 | aio_req->aiocb = acb; | |
373 | aio_req->iov_offset = iov_offset; | |
374 | aio_req->oid = oid; | |
375 | aio_req->base_oid = base_oid; | |
376 | aio_req->offset = offset; | |
377 | aio_req->data_len = data_len; | |
378 | aio_req->flags = flags; | |
379 | aio_req->id = s->aioreq_seq_num++; | |
380 | ||
381 | QLIST_INSERT_HEAD(&s->outstanding_aio_head, aio_req, | |
382 | outstanding_aio_siblings); | |
383 | QLIST_INSERT_HEAD(&acb->aioreq_head, aio_req, aioreq_siblings); | |
384 | ||
385 | return aio_req; | |
386 | } | |
387 | ||
388 | static inline int free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) | |
389 | { | |
390 | SheepdogAIOCB *acb = aio_req->aiocb; | |
391 | QLIST_REMOVE(aio_req, outstanding_aio_siblings); | |
392 | QLIST_REMOVE(aio_req, aioreq_siblings); | |
393 | qemu_free(aio_req); | |
394 | ||
395 | return !QLIST_EMPTY(&acb->aioreq_head); | |
396 | } | |
397 | ||
398 | static void sd_finish_aiocb(SheepdogAIOCB *acb) | |
399 | { | |
400 | if (!acb->canceled) { | |
401 | acb->common.cb(acb->common.opaque, acb->ret); | |
402 | } | |
403 | qemu_aio_release(acb); | |
404 | } | |
405 | ||
406 | static void sd_aio_cancel(BlockDriverAIOCB *blockacb) | |
407 | { | |
408 | SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb; | |
409 | ||
410 | /* | |
411 | * Sheepdog cannot cancel the requests which are already sent to | |
412 | * the servers, so we just complete the request with -EIO here. | |
413 | */ | |
414 | acb->common.cb(acb->common.opaque, -EIO); | |
415 | acb->canceled = 1; | |
416 | } | |
417 | ||
418 | static AIOPool sd_aio_pool = { | |
419 | .aiocb_size = sizeof(SheepdogAIOCB), | |
420 | .cancel = sd_aio_cancel, | |
421 | }; | |
422 | ||
423 | static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, | |
424 | int64_t sector_num, int nb_sectors, | |
425 | BlockDriverCompletionFunc *cb, void *opaque) | |
426 | { | |
427 | SheepdogAIOCB *acb; | |
428 | ||
429 | acb = qemu_aio_get(&sd_aio_pool, bs, cb, opaque); | |
430 | ||
431 | acb->qiov = qiov; | |
432 | ||
433 | acb->sector_num = sector_num; | |
434 | acb->nb_sectors = nb_sectors; | |
435 | ||
436 | acb->aio_done_func = NULL; | |
437 | acb->canceled = 0; | |
438 | acb->bh = NULL; | |
439 | acb->ret = 0; | |
440 | QLIST_INIT(&acb->aioreq_head); | |
441 | return acb; | |
442 | } | |
443 | ||
444 | static int sd_schedule_bh(QEMUBHFunc *cb, SheepdogAIOCB *acb) | |
445 | { | |
446 | if (acb->bh) { | |
447 | error_report("bug: %d %d\n", acb->aiocb_type, acb->aiocb_type); | |
448 | return -EIO; | |
449 | } | |
450 | ||
451 | acb->bh = qemu_bh_new(cb, acb); | |
452 | if (!acb->bh) { | |
453 | error_report("oom: %d %d\n", acb->aiocb_type, acb->aiocb_type); | |
454 | return -EIO; | |
455 | } | |
456 | ||
457 | qemu_bh_schedule(acb->bh); | |
458 | ||
459 | return 0; | |
460 | } | |
461 | ||
462 | #ifdef _WIN32 | |
463 | ||
464 | struct msghdr { | |
465 | struct iovec *msg_iov; | |
466 | size_t msg_iovlen; | |
467 | }; | |
468 | ||
469 | static ssize_t sendmsg(int s, const struct msghdr *msg, int flags) | |
470 | { | |
471 | size_t size = 0; | |
472 | char *buf, *p; | |
473 | int i, ret; | |
474 | ||
475 | /* count the msg size */ | |
476 | for (i = 0; i < msg->msg_iovlen; i++) { | |
477 | size += msg->msg_iov[i].iov_len; | |
478 | } | |
479 | buf = qemu_malloc(size); | |
480 | ||
481 | p = buf; | |
482 | for (i = 0; i < msg->msg_iovlen; i++) { | |
483 | memcpy(p, msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len); | |
484 | p += msg->msg_iov[i].iov_len; | |
485 | } | |
486 | ||
487 | ret = send(s, buf, size, flags); | |
488 | ||
489 | qemu_free(buf); | |
490 | return ret; | |
491 | } | |
492 | ||
493 | static ssize_t recvmsg(int s, struct msghdr *msg, int flags) | |
494 | { | |
495 | size_t size = 0; | |
496 | char *buf, *p; | |
497 | int i, ret; | |
498 | ||
499 | /* count the msg size */ | |
500 | for (i = 0; i < msg->msg_iovlen; i++) { | |
501 | size += msg->msg_iov[i].iov_len; | |
502 | } | |
503 | buf = qemu_malloc(size); | |
504 | ||
505 | ret = recv(s, buf, size, flags); | |
506 | if (ret < 0) { | |
507 | goto out; | |
508 | } | |
509 | ||
510 | p = buf; | |
511 | for (i = 0; i < msg->msg_iovlen; i++) { | |
512 | memcpy(msg->msg_iov[i].iov_base, p, msg->msg_iov[i].iov_len); | |
513 | p += msg->msg_iov[i].iov_len; | |
514 | } | |
515 | out: | |
516 | qemu_free(buf); | |
517 | return ret; | |
518 | } | |
519 | ||
520 | #endif | |
521 | ||
522 | /* | |
523 | * Send/recv data with iovec buffers | |
524 | * | |
525 | * This function send/recv data from/to the iovec buffer directly. | |
526 | * The first `offset' bytes in the iovec buffer are skipped and next | |
527 | * `len' bytes are used. | |
528 | * | |
529 | * For example, | |
530 | * | |
531 | * do_send_recv(sockfd, iov, len, offset, 1); | |
532 | * | |
533 | * is equals to | |
534 | * | |
535 | * char *buf = malloc(size); | |
536 | * iov_to_buf(iov, iovcnt, buf, offset, size); | |
537 | * send(sockfd, buf, size, 0); | |
538 | * free(buf); | |
539 | */ | |
540 | static int do_send_recv(int sockfd, struct iovec *iov, int len, int offset, | |
541 | int write) | |
542 | { | |
543 | struct msghdr msg; | |
544 | int ret, diff; | |
545 | ||
546 | memset(&msg, 0, sizeof(msg)); | |
547 | msg.msg_iov = iov; | |
548 | msg.msg_iovlen = 1; | |
549 | ||
550 | len += offset; | |
551 | ||
552 | while (iov->iov_len < len) { | |
553 | len -= iov->iov_len; | |
554 | ||
555 | iov++; | |
556 | msg.msg_iovlen++; | |
557 | } | |
558 | ||
559 | diff = iov->iov_len - len; | |
560 | iov->iov_len -= diff; | |
561 | ||
562 | while (msg.msg_iov->iov_len <= offset) { | |
563 | offset -= msg.msg_iov->iov_len; | |
564 | ||
565 | msg.msg_iov++; | |
566 | msg.msg_iovlen--; | |
567 | } | |
568 | ||
569 | msg.msg_iov->iov_base = (char *) msg.msg_iov->iov_base + offset; | |
570 | msg.msg_iov->iov_len -= offset; | |
571 | ||
572 | if (write) { | |
573 | ret = sendmsg(sockfd, &msg, 0); | |
574 | } else { | |
575 | ret = recvmsg(sockfd, &msg, 0); | |
576 | } | |
577 | ||
578 | msg.msg_iov->iov_base = (char *) msg.msg_iov->iov_base - offset; | |
579 | msg.msg_iov->iov_len += offset; | |
580 | ||
581 | iov->iov_len += diff; | |
582 | return ret; | |
583 | } | |
584 | ||
585 | static int connect_to_sdog(const char *addr, const char *port) | |
586 | { | |
587 | char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; | |
588 | int fd, ret; | |
589 | struct addrinfo hints, *res, *res0; | |
590 | ||
591 | if (!addr) { | |
592 | addr = SD_DEFAULT_ADDR; | |
593 | port = SD_DEFAULT_PORT; | |
594 | } | |
595 | ||
596 | memset(&hints, 0, sizeof(hints)); | |
597 | hints.ai_socktype = SOCK_STREAM; | |
598 | ||
599 | ret = getaddrinfo(addr, port, &hints, &res0); | |
600 | if (ret) { | |
601 | error_report("unable to get address info %s, %s\n", | |
602 | addr, strerror(errno)); | |
603 | return -1; | |
604 | } | |
605 | ||
606 | for (res = res0; res; res = res->ai_next) { | |
607 | ret = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), | |
608 | sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); | |
609 | if (ret) { | |
610 | continue; | |
611 | } | |
612 | ||
613 | fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
614 | if (fd < 0) { | |
615 | continue; | |
616 | } | |
617 | ||
618 | reconnect: | |
619 | ret = connect(fd, res->ai_addr, res->ai_addrlen); | |
620 | if (ret < 0) { | |
621 | if (errno == EINTR) { | |
622 | goto reconnect; | |
623 | } | |
624 | break; | |
625 | } | |
626 | ||
627 | dprintf("connected to %s:%s\n", addr, port); | |
628 | goto success; | |
629 | } | |
630 | fd = -1; | |
631 | error_report("failed connect to %s:%s\n", addr, port); | |
632 | success: | |
633 | freeaddrinfo(res0); | |
634 | return fd; | |
635 | } | |
636 | ||
637 | static int do_readv_writev(int sockfd, struct iovec *iov, int len, | |
638 | int iov_offset, int write) | |
639 | { | |
640 | int ret; | |
641 | again: | |
642 | ret = do_send_recv(sockfd, iov, len, iov_offset, write); | |
643 | if (ret < 0) { | |
644 | if (errno == EINTR || errno == EAGAIN) { | |
645 | goto again; | |
646 | } | |
647 | error_report("failed to recv a rsp, %s\n", strerror(errno)); | |
648 | return 1; | |
649 | } | |
650 | ||
651 | iov_offset += ret; | |
652 | len -= ret; | |
653 | if (len) { | |
654 | goto again; | |
655 | } | |
656 | ||
657 | return 0; | |
658 | } | |
659 | ||
660 | static int do_readv(int sockfd, struct iovec *iov, int len, int iov_offset) | |
661 | { | |
662 | return do_readv_writev(sockfd, iov, len, iov_offset, 0); | |
663 | } | |
664 | ||
665 | static int do_writev(int sockfd, struct iovec *iov, int len, int iov_offset) | |
666 | { | |
667 | return do_readv_writev(sockfd, iov, len, iov_offset, 1); | |
668 | } | |
669 | ||
670 | static int do_read_write(int sockfd, void *buf, int len, int write) | |
671 | { | |
672 | struct iovec iov; | |
673 | ||
674 | iov.iov_base = buf; | |
675 | iov.iov_len = len; | |
676 | ||
677 | return do_readv_writev(sockfd, &iov, len, 0, write); | |
678 | } | |
679 | ||
680 | static int do_read(int sockfd, void *buf, int len) | |
681 | { | |
682 | return do_read_write(sockfd, buf, len, 0); | |
683 | } | |
684 | ||
685 | static int do_write(int sockfd, void *buf, int len) | |
686 | { | |
687 | return do_read_write(sockfd, buf, len, 1); | |
688 | } | |
689 | ||
690 | static int send_req(int sockfd, SheepdogReq *hdr, void *data, | |
691 | unsigned int *wlen) | |
692 | { | |
693 | int ret; | |
694 | struct iovec iov[2]; | |
695 | ||
696 | iov[0].iov_base = hdr; | |
697 | iov[0].iov_len = sizeof(*hdr); | |
698 | ||
699 | if (*wlen) { | |
700 | iov[1].iov_base = data; | |
701 | iov[1].iov_len = *wlen; | |
702 | } | |
703 | ||
704 | ret = do_writev(sockfd, iov, sizeof(*hdr) + *wlen, 0); | |
705 | if (ret) { | |
706 | error_report("failed to send a req, %s\n", strerror(errno)); | |
707 | ret = -1; | |
708 | } | |
709 | ||
710 | return ret; | |
711 | } | |
712 | ||
713 | static int do_req(int sockfd, SheepdogReq *hdr, void *data, | |
714 | unsigned int *wlen, unsigned int *rlen) | |
715 | { | |
716 | int ret; | |
717 | ||
718 | ret = send_req(sockfd, hdr, data, wlen); | |
719 | if (ret) { | |
720 | ret = -1; | |
721 | goto out; | |
722 | } | |
723 | ||
724 | ret = do_read(sockfd, hdr, sizeof(*hdr)); | |
725 | if (ret) { | |
726 | error_report("failed to get a rsp, %s\n", strerror(errno)); | |
727 | ret = -1; | |
728 | goto out; | |
729 | } | |
730 | ||
731 | if (*rlen > hdr->data_length) { | |
732 | *rlen = hdr->data_length; | |
733 | } | |
734 | ||
735 | if (*rlen) { | |
736 | ret = do_read(sockfd, data, *rlen); | |
737 | if (ret) { | |
738 | error_report("failed to get the data, %s\n", strerror(errno)); | |
739 | ret = -1; | |
740 | goto out; | |
741 | } | |
742 | } | |
743 | ret = 0; | |
744 | out: | |
745 | return ret; | |
746 | } | |
747 | ||
748 | static int add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, | |
749 | struct iovec *iov, int niov, int create, | |
750 | enum AIOCBState aiocb_type); | |
751 | ||
752 | /* | |
753 | * This function searchs pending requests to the object `oid', and | |
754 | * sends them. | |
755 | */ | |
756 | static void send_pending_req(BDRVSheepdogState *s, uint64_t oid, uint32_t id) | |
757 | { | |
758 | AIOReq *aio_req, *next; | |
759 | SheepdogAIOCB *acb; | |
760 | int ret; | |
761 | ||
762 | QLIST_FOREACH_SAFE(aio_req, &s->outstanding_aio_head, | |
763 | outstanding_aio_siblings, next) { | |
764 | if (id == aio_req->id) { | |
765 | continue; | |
766 | } | |
767 | if (aio_req->oid != oid) { | |
768 | continue; | |
769 | } | |
770 | ||
771 | acb = aio_req->aiocb; | |
772 | ret = add_aio_request(s, aio_req, acb->qiov->iov, | |
773 | acb->qiov->niov, 0, acb->aiocb_type); | |
774 | if (ret < 0) { | |
775 | error_report("add_aio_request is failed\n"); | |
776 | free_aio_req(s, aio_req); | |
777 | if (QLIST_EMPTY(&acb->aioreq_head)) { | |
778 | sd_finish_aiocb(acb); | |
779 | } | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | /* | |
785 | * Receive responses of the I/O requests. | |
786 | * | |
787 | * This function is registered as a fd handler, and called from the | |
788 | * main loop when s->fd is ready for reading responses. | |
789 | */ | |
790 | static void aio_read_response(void *opaque) | |
791 | { | |
792 | SheepdogObjRsp rsp; | |
793 | BDRVSheepdogState *s = opaque; | |
794 | int fd = s->fd; | |
795 | int ret; | |
796 | AIOReq *aio_req = NULL; | |
797 | SheepdogAIOCB *acb; | |
798 | int rest; | |
799 | unsigned long idx; | |
800 | ||
801 | if (QLIST_EMPTY(&s->outstanding_aio_head)) { | |
802 | return; | |
803 | } | |
804 | ||
805 | /* read a header */ | |
806 | ret = do_read(fd, &rsp, sizeof(rsp)); | |
807 | if (ret) { | |
808 | error_report("failed to get the header, %s\n", strerror(errno)); | |
809 | return; | |
810 | } | |
811 | ||
812 | /* find the right aio_req from the outstanding_aio list */ | |
813 | QLIST_FOREACH(aio_req, &s->outstanding_aio_head, outstanding_aio_siblings) { | |
814 | if (aio_req->id == rsp.id) { | |
815 | break; | |
816 | } | |
817 | } | |
818 | if (!aio_req) { | |
819 | error_report("cannot find aio_req %x\n", rsp.id); | |
820 | return; | |
821 | } | |
822 | ||
823 | acb = aio_req->aiocb; | |
824 | ||
825 | switch (acb->aiocb_type) { | |
826 | case AIOCB_WRITE_UDATA: | |
827 | if (!is_data_obj(aio_req->oid)) { | |
828 | break; | |
829 | } | |
830 | idx = data_oid_to_idx(aio_req->oid); | |
831 | ||
832 | if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) { | |
833 | /* | |
834 | * If the object is newly created one, we need to update | |
835 | * the vdi object (metadata object). min_dirty_data_idx | |
836 | * and max_dirty_data_idx are changed to include updated | |
837 | * index between them. | |
838 | */ | |
839 | s->inode.data_vdi_id[idx] = s->inode.vdi_id; | |
840 | s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx); | |
841 | s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx); | |
842 | ||
843 | /* | |
844 | * Some requests may be blocked because simultaneous | |
845 | * create requests are not allowed, so we search the | |
846 | * pending requests here. | |
847 | */ | |
848 | send_pending_req(s, vid_to_data_oid(s->inode.vdi_id, idx), rsp.id); | |
849 | } | |
850 | break; | |
851 | case AIOCB_READ_UDATA: | |
852 | ret = do_readv(fd, acb->qiov->iov, rsp.data_length, | |
853 | aio_req->iov_offset); | |
854 | if (ret) { | |
855 | error_report("failed to get the data, %s\n", strerror(errno)); | |
856 | return; | |
857 | } | |
858 | break; | |
859 | } | |
860 | ||
861 | if (rsp.result != SD_RES_SUCCESS) { | |
862 | acb->ret = -EIO; | |
863 | error_report("%s\n", sd_strerror(rsp.result)); | |
864 | } | |
865 | ||
866 | rest = free_aio_req(s, aio_req); | |
867 | if (!rest) { | |
868 | /* | |
869 | * We've finished all requests which belong to the AIOCB, so | |
870 | * we can call the callback now. | |
871 | */ | |
872 | acb->aio_done_func(acb); | |
873 | } | |
874 | } | |
875 | ||
876 | static int aio_flush_request(void *opaque) | |
877 | { | |
878 | BDRVSheepdogState *s = opaque; | |
879 | ||
880 | return !QLIST_EMPTY(&s->outstanding_aio_head); | |
881 | } | |
882 | ||
6defcc37 | 883 | #if !defined(SOL_TCP) || !defined(TCP_CORK) |
33b1db1c MK |
884 | |
885 | static int set_cork(int fd, int v) | |
886 | { | |
887 | return 0; | |
888 | } | |
889 | ||
890 | #else | |
891 | ||
892 | static int set_cork(int fd, int v) | |
893 | { | |
894 | return setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v)); | |
895 | } | |
896 | ||
897 | #endif | |
898 | ||
899 | static int set_nodelay(int fd) | |
900 | { | |
901 | int ret, opt; | |
902 | ||
903 | opt = 1; | |
904 | ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt)); | |
905 | return ret; | |
906 | } | |
907 | ||
908 | /* | |
909 | * Return a socket discriptor to read/write objects. | |
910 | * | |
911 | * We cannot use this discriptor for other operations because | |
912 | * the block driver may be on waiting response from the server. | |
913 | */ | |
914 | static int get_sheep_fd(BDRVSheepdogState *s) | |
915 | { | |
916 | int ret, fd; | |
917 | ||
918 | fd = connect_to_sdog(s->addr, s->port); | |
919 | if (fd < 0) { | |
920 | error_report("%s\n", strerror(errno)); | |
921 | return -1; | |
922 | } | |
923 | ||
924 | socket_set_nonblock(fd); | |
925 | ||
926 | ret = set_nodelay(fd); | |
927 | if (ret) { | |
928 | error_report("%s\n", strerror(errno)); | |
929 | closesocket(fd); | |
930 | return -1; | |
931 | } | |
932 | ||
933 | qemu_aio_set_fd_handler(fd, aio_read_response, NULL, aio_flush_request, | |
934 | NULL, s); | |
935 | return fd; | |
936 | } | |
937 | ||
938 | /* | |
939 | * Parse a filename | |
940 | * | |
941 | * filename must be one of the following formats: | |
942 | * 1. [vdiname] | |
943 | * 2. [vdiname]:[snapid] | |
944 | * 3. [vdiname]:[tag] | |
945 | * 4. [hostname]:[port]:[vdiname] | |
946 | * 5. [hostname]:[port]:[vdiname]:[snapid] | |
947 | * 6. [hostname]:[port]:[vdiname]:[tag] | |
948 | * | |
949 | * You can boot from the snapshot images by specifying `snapid` or | |
950 | * `tag'. | |
951 | * | |
952 | * You can run VMs outside the Sheepdog cluster by specifying | |
953 | * `hostname' and `port' (experimental). | |
954 | */ | |
955 | static int parse_vdiname(BDRVSheepdogState *s, const char *filename, | |
956 | char *vdi, uint32_t *snapid, char *tag) | |
957 | { | |
958 | char *p, *q; | |
959 | int nr_sep; | |
960 | ||
961 | p = q = qemu_strdup(filename); | |
962 | ||
963 | /* count the number of separators */ | |
964 | nr_sep = 0; | |
965 | while (*p) { | |
966 | if (*p == ':') { | |
967 | nr_sep++; | |
968 | } | |
969 | p++; | |
970 | } | |
971 | p = q; | |
972 | ||
973 | /* use the first two tokens as hostname and port number. */ | |
974 | if (nr_sep >= 2) { | |
975 | s->addr = p; | |
976 | p = strchr(p, ':'); | |
977 | *p++ = '\0'; | |
978 | ||
979 | s->port = p; | |
980 | p = strchr(p, ':'); | |
981 | *p++ = '\0'; | |
982 | } else { | |
983 | s->addr = NULL; | |
984 | s->port = 0; | |
985 | } | |
986 | ||
987 | strncpy(vdi, p, SD_MAX_VDI_LEN); | |
988 | ||
989 | p = strchr(vdi, ':'); | |
990 | if (p) { | |
991 | *p++ = '\0'; | |
992 | *snapid = strtoul(p, NULL, 10); | |
993 | if (*snapid == 0) { | |
994 | strncpy(tag, p, SD_MAX_VDI_TAG_LEN); | |
995 | } | |
996 | } else { | |
997 | *snapid = CURRENT_VDI_ID; /* search current vdi */ | |
998 | } | |
999 | ||
1000 | if (s->addr == NULL) { | |
1001 | qemu_free(q); | |
1002 | } | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | static int find_vdi_name(BDRVSheepdogState *s, char *filename, uint32_t snapid, | |
1008 | char *tag, uint32_t *vid, int for_snapshot) | |
1009 | { | |
1010 | int ret, fd; | |
1011 | SheepdogVdiReq hdr; | |
1012 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1013 | unsigned int wlen, rlen = 0; | |
1014 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; | |
1015 | ||
1016 | fd = connect_to_sdog(s->addr, s->port); | |
1017 | if (fd < 0) { | |
1018 | return -1; | |
1019 | } | |
1020 | ||
1021 | memset(buf, 0, sizeof(buf)); | |
1022 | strncpy(buf, filename, SD_MAX_VDI_LEN); | |
1023 | strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN); | |
1024 | ||
1025 | memset(&hdr, 0, sizeof(hdr)); | |
1026 | if (for_snapshot) { | |
1027 | hdr.opcode = SD_OP_GET_VDI_INFO; | |
1028 | } else { | |
1029 | hdr.opcode = SD_OP_LOCK_VDI; | |
1030 | } | |
1031 | wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN; | |
1032 | hdr.proto_ver = SD_PROTO_VER; | |
1033 | hdr.data_length = wlen; | |
1034 | hdr.snapid = snapid; | |
1035 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1036 | ||
1037 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1038 | if (ret) { | |
1039 | ret = -1; | |
1040 | goto out; | |
1041 | } | |
1042 | ||
1043 | if (rsp->result != SD_RES_SUCCESS) { | |
1044 | error_report("cannot get vdi info, %s, %s %d %s\n", | |
1045 | sd_strerror(rsp->result), filename, snapid, tag); | |
1046 | ret = -1; | |
1047 | goto out; | |
1048 | } | |
1049 | *vid = rsp->vdi_id; | |
1050 | ||
1051 | ret = 0; | |
1052 | out: | |
1053 | closesocket(fd); | |
1054 | return ret; | |
1055 | } | |
1056 | ||
1057 | static int add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, | |
1058 | struct iovec *iov, int niov, int create, | |
1059 | enum AIOCBState aiocb_type) | |
1060 | { | |
1061 | int nr_copies = s->inode.nr_copies; | |
1062 | SheepdogObjReq hdr; | |
1063 | unsigned int wlen; | |
1064 | int ret; | |
1065 | uint64_t oid = aio_req->oid; | |
1066 | unsigned int datalen = aio_req->data_len; | |
1067 | uint64_t offset = aio_req->offset; | |
1068 | uint8_t flags = aio_req->flags; | |
1069 | uint64_t old_oid = aio_req->base_oid; | |
1070 | ||
1071 | if (!nr_copies) { | |
1072 | error_report("bug\n"); | |
1073 | } | |
1074 | ||
1075 | memset(&hdr, 0, sizeof(hdr)); | |
1076 | ||
1077 | if (aiocb_type == AIOCB_READ_UDATA) { | |
1078 | wlen = 0; | |
1079 | hdr.opcode = SD_OP_READ_OBJ; | |
1080 | hdr.flags = flags; | |
1081 | } else if (create) { | |
1082 | wlen = datalen; | |
1083 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1084 | hdr.flags = SD_FLAG_CMD_WRITE | flags; | |
1085 | } else { | |
1086 | wlen = datalen; | |
1087 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1088 | hdr.flags = SD_FLAG_CMD_WRITE | flags; | |
1089 | } | |
1090 | ||
1091 | hdr.oid = oid; | |
1092 | hdr.cow_oid = old_oid; | |
1093 | hdr.copies = s->inode.nr_copies; | |
1094 | ||
1095 | hdr.data_length = datalen; | |
1096 | hdr.offset = offset; | |
1097 | ||
1098 | hdr.id = aio_req->id; | |
1099 | ||
1100 | set_cork(s->fd, 1); | |
1101 | ||
1102 | /* send a header */ | |
1103 | ret = do_write(s->fd, &hdr, sizeof(hdr)); | |
1104 | if (ret) { | |
1105 | error_report("failed to send a req, %s\n", strerror(errno)); | |
1106 | return -EIO; | |
1107 | } | |
1108 | ||
1109 | if (wlen) { | |
1110 | ret = do_writev(s->fd, iov, wlen, aio_req->iov_offset); | |
1111 | if (ret) { | |
1112 | error_report("failed to send a data, %s\n", strerror(errno)); | |
1113 | return -EIO; | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | set_cork(s->fd, 0); | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | static int read_write_object(int fd, char *buf, uint64_t oid, int copies, | |
1123 | unsigned int datalen, uint64_t offset, | |
1124 | int write, int create) | |
1125 | { | |
1126 | SheepdogObjReq hdr; | |
1127 | SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr; | |
1128 | unsigned int wlen, rlen; | |
1129 | int ret; | |
1130 | ||
1131 | memset(&hdr, 0, sizeof(hdr)); | |
1132 | ||
1133 | if (write) { | |
1134 | wlen = datalen; | |
1135 | rlen = 0; | |
1136 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1137 | if (create) { | |
1138 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1139 | } else { | |
1140 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1141 | } | |
1142 | } else { | |
1143 | wlen = 0; | |
1144 | rlen = datalen; | |
1145 | hdr.opcode = SD_OP_READ_OBJ; | |
1146 | } | |
1147 | hdr.oid = oid; | |
1148 | hdr.data_length = datalen; | |
1149 | hdr.offset = offset; | |
1150 | hdr.copies = copies; | |
1151 | ||
1152 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1153 | if (ret) { | |
1154 | error_report("failed to send a request to the sheep\n"); | |
1155 | return -1; | |
1156 | } | |
1157 | ||
1158 | switch (rsp->result) { | |
1159 | case SD_RES_SUCCESS: | |
1160 | return 0; | |
1161 | default: | |
1162 | error_report("%s\n", sd_strerror(rsp->result)); | |
1163 | return -1; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | static int read_object(int fd, char *buf, uint64_t oid, int copies, | |
1168 | unsigned int datalen, uint64_t offset) | |
1169 | { | |
1170 | return read_write_object(fd, buf, oid, copies, datalen, offset, 0, 0); | |
1171 | } | |
1172 | ||
1173 | static int write_object(int fd, char *buf, uint64_t oid, int copies, | |
1174 | unsigned int datalen, uint64_t offset, int create) | |
1175 | { | |
1176 | return read_write_object(fd, buf, oid, copies, datalen, offset, 1, create); | |
1177 | } | |
1178 | ||
1179 | static int sd_open(BlockDriverState *bs, const char *filename, int flags) | |
1180 | { | |
1181 | int ret, fd; | |
1182 | uint32_t vid = 0; | |
1183 | BDRVSheepdogState *s = bs->opaque; | |
1184 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; | |
1185 | uint32_t snapid; | |
1186 | char *buf = NULL; | |
1187 | ||
1188 | strstart(filename, "sheepdog:", (const char **)&filename); | |
1189 | ||
1190 | QLIST_INIT(&s->outstanding_aio_head); | |
1191 | s->fd = -1; | |
1192 | ||
1193 | memset(vdi, 0, sizeof(vdi)); | |
1194 | memset(tag, 0, sizeof(tag)); | |
1195 | if (parse_vdiname(s, filename, vdi, &snapid, tag) < 0) { | |
1196 | goto out; | |
1197 | } | |
1198 | s->fd = get_sheep_fd(s); | |
1199 | if (s->fd < 0) { | |
1200 | goto out; | |
1201 | } | |
1202 | ||
1203 | ret = find_vdi_name(s, vdi, snapid, tag, &vid, 0); | |
1204 | if (ret) { | |
1205 | goto out; | |
1206 | } | |
1207 | ||
1208 | if (snapid) { | |
1209 | dprintf("%" PRIx32 " snapshot inode was open.\n", vid); | |
1210 | s->is_snapshot = 1; | |
1211 | } | |
1212 | ||
1213 | fd = connect_to_sdog(s->addr, s->port); | |
1214 | if (fd < 0) { | |
1215 | error_report("failed to connect\n"); | |
1216 | goto out; | |
1217 | } | |
1218 | ||
1219 | buf = qemu_malloc(SD_INODE_SIZE); | |
1220 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0); | |
1221 | ||
1222 | closesocket(fd); | |
1223 | ||
1224 | if (ret) { | |
1225 | goto out; | |
1226 | } | |
1227 | ||
1228 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1229 | s->min_dirty_data_idx = UINT32_MAX; | |
1230 | s->max_dirty_data_idx = 0; | |
1231 | ||
1232 | bs->total_sectors = s->inode.vdi_size / SECTOR_SIZE; | |
1233 | strncpy(s->name, vdi, sizeof(s->name)); | |
1234 | qemu_free(buf); | |
1235 | return 0; | |
1236 | out: | |
1237 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL, NULL, NULL); | |
1238 | if (s->fd >= 0) { | |
1239 | closesocket(s->fd); | |
1240 | } | |
1241 | qemu_free(buf); | |
1242 | return -1; | |
1243 | } | |
1244 | ||
1245 | static int do_sd_create(char *filename, int64_t vdi_size, | |
1246 | uint32_t base_vid, uint32_t *vdi_id, int snapshot, | |
1247 | const char *addr, const char *port) | |
1248 | { | |
1249 | SheepdogVdiReq hdr; | |
1250 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1251 | int fd, ret; | |
1252 | unsigned int wlen, rlen = 0; | |
1253 | char buf[SD_MAX_VDI_LEN]; | |
1254 | ||
1255 | fd = connect_to_sdog(addr, port); | |
1256 | if (fd < 0) { | |
1257 | return -EIO; | |
1258 | } | |
1259 | ||
1260 | memset(buf, 0, sizeof(buf)); | |
1261 | strncpy(buf, filename, SD_MAX_VDI_LEN); | |
1262 | ||
1263 | memset(&hdr, 0, sizeof(hdr)); | |
1264 | hdr.opcode = SD_OP_NEW_VDI; | |
1265 | hdr.base_vdi_id = base_vid; | |
1266 | ||
1267 | wlen = SD_MAX_VDI_LEN; | |
1268 | ||
1269 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1270 | hdr.snapid = snapshot; | |
1271 | ||
1272 | hdr.data_length = wlen; | |
1273 | hdr.vdi_size = vdi_size; | |
1274 | ||
1275 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1276 | ||
1277 | closesocket(fd); | |
1278 | ||
1279 | if (ret) { | |
1280 | return -EIO; | |
1281 | } | |
1282 | ||
1283 | if (rsp->result != SD_RES_SUCCESS) { | |
1284 | error_report("%s, %s\n", sd_strerror(rsp->result), filename); | |
1285 | return -EIO; | |
1286 | } | |
1287 | ||
1288 | if (vdi_id) { | |
1289 | *vdi_id = rsp->vdi_id; | |
1290 | } | |
1291 | ||
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | static int sd_create(const char *filename, QEMUOptionParameter *options) | |
1296 | { | |
1297 | int ret; | |
b4447363 | 1298 | uint32_t vid = 0, base_vid = 0; |
33b1db1c MK |
1299 | int64_t vdi_size = 0; |
1300 | char *backing_file = NULL; | |
b4447363 MK |
1301 | BDRVSheepdogState s; |
1302 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; | |
1303 | uint32_t snapid; | |
33b1db1c MK |
1304 | |
1305 | strstart(filename, "sheepdog:", (const char **)&filename); | |
1306 | ||
b4447363 MK |
1307 | memset(&s, 0, sizeof(s)); |
1308 | memset(vdi, 0, sizeof(vdi)); | |
1309 | memset(tag, 0, sizeof(tag)); | |
1310 | if (parse_vdiname(&s, filename, vdi, &snapid, tag) < 0) { | |
1311 | error_report("invalid filename\n"); | |
1312 | return -EINVAL; | |
1313 | } | |
1314 | ||
33b1db1c MK |
1315 | while (options && options->name) { |
1316 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
1317 | vdi_size = options->value.n; | |
1318 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
1319 | backing_file = options->value.s; | |
1320 | } | |
1321 | options++; | |
1322 | } | |
1323 | ||
1324 | if (vdi_size > SD_MAX_VDI_SIZE) { | |
1325 | error_report("too big image size\n"); | |
1326 | return -EINVAL; | |
1327 | } | |
1328 | ||
1329 | if (backing_file) { | |
1330 | BlockDriverState *bs; | |
1331 | BDRVSheepdogState *s; | |
1332 | BlockDriver *drv; | |
1333 | ||
1334 | /* Currently, only Sheepdog backing image is supported. */ | |
1335 | drv = bdrv_find_protocol(backing_file); | |
1336 | if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { | |
1337 | error_report("backing_file must be a sheepdog image\n"); | |
1338 | return -EINVAL; | |
1339 | } | |
1340 | ||
1341 | ret = bdrv_file_open(&bs, backing_file, 0); | |
1342 | if (ret < 0) | |
1343 | return -EIO; | |
1344 | ||
1345 | s = bs->opaque; | |
1346 | ||
1347 | if (!is_snapshot(&s->inode)) { | |
1348 | error_report("cannot clone from a non snapshot vdi\n"); | |
1349 | bdrv_delete(bs); | |
1350 | return -EINVAL; | |
1351 | } | |
1352 | ||
b4447363 | 1353 | base_vid = s->inode.vdi_id; |
33b1db1c MK |
1354 | bdrv_delete(bs); |
1355 | } | |
1356 | ||
b4447363 | 1357 | return do_sd_create((char *)vdi, vdi_size, base_vid, &vid, 0, s.addr, s.port); |
33b1db1c MK |
1358 | } |
1359 | ||
1360 | static void sd_close(BlockDriverState *bs) | |
1361 | { | |
1362 | BDRVSheepdogState *s = bs->opaque; | |
1363 | SheepdogVdiReq hdr; | |
1364 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1365 | unsigned int wlen, rlen = 0; | |
1366 | int fd, ret; | |
1367 | ||
1368 | dprintf("%s\n", s->name); | |
1369 | ||
1370 | fd = connect_to_sdog(s->addr, s->port); | |
1371 | if (fd < 0) { | |
1372 | return; | |
1373 | } | |
1374 | ||
1375 | memset(&hdr, 0, sizeof(hdr)); | |
1376 | ||
1377 | hdr.opcode = SD_OP_RELEASE_VDI; | |
1378 | wlen = strlen(s->name) + 1; | |
1379 | hdr.data_length = wlen; | |
1380 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1381 | ||
1382 | ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen); | |
1383 | ||
1384 | closesocket(fd); | |
1385 | ||
1386 | if (!ret && rsp->result != SD_RES_SUCCESS && | |
1387 | rsp->result != SD_RES_VDI_NOT_LOCKED) { | |
1388 | error_report("%s, %s\n", sd_strerror(rsp->result), s->name); | |
1389 | } | |
1390 | ||
1391 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL, NULL, NULL); | |
1392 | closesocket(s->fd); | |
1393 | qemu_free(s->addr); | |
1394 | } | |
1395 | ||
1396 | static int64_t sd_getlength(BlockDriverState *bs) | |
1397 | { | |
1398 | BDRVSheepdogState *s = bs->opaque; | |
1399 | ||
1400 | return s->inode.vdi_size; | |
1401 | } | |
1402 | ||
1403 | static int sd_truncate(BlockDriverState *bs, int64_t offset) | |
1404 | { | |
1405 | BDRVSheepdogState *s = bs->opaque; | |
1406 | int ret, fd; | |
1407 | unsigned int datalen; | |
1408 | ||
1409 | if (offset < s->inode.vdi_size) { | |
1410 | error_report("shrinking is not supported\n"); | |
1411 | return -EINVAL; | |
1412 | } else if (offset > SD_MAX_VDI_SIZE) { | |
1413 | error_report("too big image size\n"); | |
1414 | return -EINVAL; | |
1415 | } | |
1416 | ||
1417 | fd = connect_to_sdog(s->addr, s->port); | |
1418 | if (fd < 0) { | |
1419 | return -EIO; | |
1420 | } | |
1421 | ||
1422 | /* we don't need to update entire object */ | |
1423 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
1424 | s->inode.vdi_size = offset; | |
1425 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
1426 | s->inode.nr_copies, datalen, 0, 0); | |
1427 | close(fd); | |
1428 | ||
1429 | if (ret < 0) { | |
1430 | error_report("failed to update an inode.\n"); | |
1431 | return -EIO; | |
1432 | } | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * This function is called after writing data objects. If we need to | |
1439 | * update metadata, this sends a write request to the vdi object. | |
1440 | * Otherwise, this calls the AIOCB callback. | |
1441 | */ | |
1442 | static void sd_write_done(SheepdogAIOCB *acb) | |
1443 | { | |
1444 | int ret; | |
1445 | BDRVSheepdogState *s = acb->common.bs->opaque; | |
1446 | struct iovec iov; | |
1447 | AIOReq *aio_req; | |
1448 | uint32_t offset, data_len, mn, mx; | |
1449 | ||
1450 | mn = s->min_dirty_data_idx; | |
1451 | mx = s->max_dirty_data_idx; | |
1452 | if (mn <= mx) { | |
1453 | /* we need to update the vdi object. */ | |
1454 | offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + | |
1455 | mn * sizeof(s->inode.data_vdi_id[0]); | |
1456 | data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); | |
1457 | ||
1458 | s->min_dirty_data_idx = UINT32_MAX; | |
1459 | s->max_dirty_data_idx = 0; | |
1460 | ||
1461 | iov.iov_base = &s->inode; | |
1462 | iov.iov_len = sizeof(s->inode); | |
1463 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), | |
1464 | data_len, offset, 0, 0, offset); | |
1465 | ret = add_aio_request(s, aio_req, &iov, 1, 0, AIOCB_WRITE_UDATA); | |
1466 | if (ret) { | |
1467 | free_aio_req(s, aio_req); | |
1468 | acb->ret = -EIO; | |
1469 | goto out; | |
1470 | } | |
1471 | ||
1472 | acb->aio_done_func = sd_finish_aiocb; | |
1473 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
1474 | return; | |
1475 | } | |
1476 | out: | |
1477 | sd_finish_aiocb(acb); | |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * Create a writable VDI from a snapshot | |
1482 | */ | |
1483 | static int sd_create_branch(BDRVSheepdogState *s) | |
1484 | { | |
1485 | int ret, fd; | |
1486 | uint32_t vid; | |
1487 | char *buf; | |
1488 | ||
1489 | dprintf("%" PRIx32 " is snapshot.\n", s->inode.vdi_id); | |
1490 | ||
1491 | buf = qemu_malloc(SD_INODE_SIZE); | |
1492 | ||
1493 | ret = do_sd_create(s->name, s->inode.vdi_size, s->inode.vdi_id, &vid, 1, | |
1494 | s->addr, s->port); | |
1495 | if (ret) { | |
1496 | goto out; | |
1497 | } | |
1498 | ||
1499 | dprintf("%" PRIx32 " is created.\n", vid); | |
1500 | ||
1501 | fd = connect_to_sdog(s->addr, s->port); | |
1502 | if (fd < 0) { | |
1503 | error_report("failed to connect\n"); | |
1504 | goto out; | |
1505 | } | |
1506 | ||
1507 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies, | |
1508 | SD_INODE_SIZE, 0); | |
1509 | ||
1510 | closesocket(fd); | |
1511 | ||
1512 | if (ret < 0) { | |
1513 | goto out; | |
1514 | } | |
1515 | ||
1516 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1517 | ||
1518 | s->is_snapshot = 0; | |
1519 | ret = 0; | |
1520 | dprintf("%" PRIx32 " was newly created.\n", s->inode.vdi_id); | |
1521 | ||
1522 | out: | |
1523 | qemu_free(buf); | |
1524 | ||
1525 | return ret; | |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * Send I/O requests to the server. | |
1530 | * | |
1531 | * This function sends requests to the server, links the requests to | |
1532 | * the outstanding_list in BDRVSheepdogState, and exits without | |
1533 | * waiting the response. The responses are received in the | |
1534 | * `aio_read_response' function which is called from the main loop as | |
1535 | * a fd handler. | |
1536 | */ | |
1537 | static void sd_readv_writev_bh_cb(void *p) | |
1538 | { | |
1539 | SheepdogAIOCB *acb = p; | |
1540 | int ret = 0; | |
1541 | unsigned long len, done = 0, total = acb->nb_sectors * SECTOR_SIZE; | |
1542 | unsigned long idx = acb->sector_num * SECTOR_SIZE / SD_DATA_OBJ_SIZE; | |
1543 | uint64_t oid; | |
1544 | uint64_t offset = (acb->sector_num * SECTOR_SIZE) % SD_DATA_OBJ_SIZE; | |
1545 | BDRVSheepdogState *s = acb->common.bs->opaque; | |
1546 | SheepdogInode *inode = &s->inode; | |
1547 | AIOReq *aio_req; | |
1548 | ||
1549 | qemu_bh_delete(acb->bh); | |
1550 | acb->bh = NULL; | |
1551 | ||
1552 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) { | |
1553 | /* | |
1554 | * In the case we open the snapshot VDI, Sheepdog creates the | |
1555 | * writable VDI when we do a write operation first. | |
1556 | */ | |
1557 | ret = sd_create_branch(s); | |
1558 | if (ret) { | |
1559 | acb->ret = -EIO; | |
1560 | goto out; | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | while (done != total) { | |
1565 | uint8_t flags = 0; | |
1566 | uint64_t old_oid = 0; | |
1567 | int create = 0; | |
1568 | ||
1569 | oid = vid_to_data_oid(inode->data_vdi_id[idx], idx); | |
1570 | ||
1571 | len = MIN(total - done, SD_DATA_OBJ_SIZE - offset); | |
1572 | ||
1573 | if (!inode->data_vdi_id[idx]) { | |
1574 | if (acb->aiocb_type == AIOCB_READ_UDATA) { | |
1575 | goto done; | |
1576 | } | |
1577 | ||
1578 | create = 1; | |
1579 | } else if (acb->aiocb_type == AIOCB_WRITE_UDATA | |
ebabb67a | 1580 | && !is_data_obj_writable(inode, idx)) { |
33b1db1c MK |
1581 | /* Copy-On-Write */ |
1582 | create = 1; | |
1583 | old_oid = oid; | |
1584 | flags = SD_FLAG_CMD_COW; | |
1585 | } | |
1586 | ||
1587 | if (create) { | |
1588 | dprintf("update ino (%" PRIu32") %" PRIu64 " %" PRIu64 | |
1589 | " %" PRIu64 "\n", inode->vdi_id, oid, | |
1590 | vid_to_data_oid(inode->data_vdi_id[idx], idx), idx); | |
1591 | oid = vid_to_data_oid(inode->vdi_id, idx); | |
1592 | dprintf("new oid %lx\n", oid); | |
1593 | } | |
1594 | ||
1595 | aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done); | |
1596 | ||
1597 | if (create) { | |
1598 | AIOReq *areq; | |
1599 | QLIST_FOREACH(areq, &s->outstanding_aio_head, | |
1600 | outstanding_aio_siblings) { | |
1601 | if (areq == aio_req) { | |
1602 | continue; | |
1603 | } | |
1604 | if (areq->oid == oid) { | |
1605 | /* | |
1606 | * Sheepdog cannot handle simultaneous create | |
1607 | * requests to the same object. So we cannot send | |
1608 | * the request until the previous request | |
1609 | * finishes. | |
1610 | */ | |
1611 | aio_req->flags = 0; | |
1612 | aio_req->base_oid = 0; | |
1613 | goto done; | |
1614 | } | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | ret = add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, | |
1619 | create, acb->aiocb_type); | |
1620 | if (ret < 0) { | |
1621 | error_report("add_aio_request is failed\n"); | |
1622 | free_aio_req(s, aio_req); | |
1623 | acb->ret = -EIO; | |
1624 | goto out; | |
1625 | } | |
1626 | done: | |
1627 | offset = 0; | |
1628 | idx++; | |
1629 | done += len; | |
1630 | } | |
1631 | out: | |
1632 | if (QLIST_EMPTY(&acb->aioreq_head)) { | |
1633 | sd_finish_aiocb(acb); | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | static BlockDriverAIOCB *sd_aio_writev(BlockDriverState *bs, int64_t sector_num, | |
1638 | QEMUIOVector *qiov, int nb_sectors, | |
1639 | BlockDriverCompletionFunc *cb, | |
1640 | void *opaque) | |
1641 | { | |
1642 | SheepdogAIOCB *acb; | |
1643 | ||
1644 | if (bs->growable && sector_num + nb_sectors > bs->total_sectors) { | |
1645 | /* TODO: shouldn't block here */ | |
1646 | if (sd_truncate(bs, (sector_num + nb_sectors) * SECTOR_SIZE) < 0) { | |
1647 | return NULL; | |
1648 | } | |
1649 | bs->total_sectors = sector_num + nb_sectors; | |
1650 | } | |
1651 | ||
1652 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors, cb, opaque); | |
1653 | acb->aio_done_func = sd_write_done; | |
1654 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
1655 | ||
1656 | sd_schedule_bh(sd_readv_writev_bh_cb, acb); | |
1657 | return &acb->common; | |
1658 | } | |
1659 | ||
1660 | static BlockDriverAIOCB *sd_aio_readv(BlockDriverState *bs, int64_t sector_num, | |
1661 | QEMUIOVector *qiov, int nb_sectors, | |
1662 | BlockDriverCompletionFunc *cb, | |
1663 | void *opaque) | |
1664 | { | |
1665 | SheepdogAIOCB *acb; | |
1666 | int i; | |
1667 | ||
1668 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors, cb, opaque); | |
1669 | acb->aiocb_type = AIOCB_READ_UDATA; | |
1670 | acb->aio_done_func = sd_finish_aiocb; | |
1671 | ||
1672 | /* | |
1673 | * TODO: we can do better; we don't need to initialize | |
1674 | * blindly. | |
1675 | */ | |
1676 | for (i = 0; i < qiov->niov; i++) { | |
1677 | memset(qiov->iov[i].iov_base, 0, qiov->iov[i].iov_len); | |
1678 | } | |
1679 | ||
1680 | sd_schedule_bh(sd_readv_writev_bh_cb, acb); | |
1681 | return &acb->common; | |
1682 | } | |
1683 | ||
1684 | static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) | |
1685 | { | |
1686 | BDRVSheepdogState *s = bs->opaque; | |
1687 | int ret, fd; | |
1688 | uint32_t new_vid; | |
1689 | SheepdogInode *inode; | |
1690 | unsigned int datalen; | |
1691 | ||
1692 | dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d " | |
1693 | "is_snapshot %d\n", sn_info->name, sn_info->id_str, | |
1694 | s->name, sn_info->vm_state_size, s->is_snapshot); | |
1695 | ||
1696 | if (s->is_snapshot) { | |
1697 | error_report("You can't create a snapshot of a snapshot VDI, " | |
1698 | "%s (%" PRIu32 ").\n", s->name, s->inode.vdi_id); | |
1699 | ||
1700 | return -EINVAL; | |
1701 | } | |
1702 | ||
1703 | dprintf("%s %s\n", sn_info->name, sn_info->id_str); | |
1704 | ||
1705 | s->inode.vm_state_size = sn_info->vm_state_size; | |
1706 | s->inode.vm_clock_nsec = sn_info->vm_clock_nsec; | |
1707 | strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag)); | |
1708 | /* we don't need to update entire object */ | |
1709 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
1710 | ||
1711 | /* refresh inode. */ | |
1712 | fd = connect_to_sdog(s->addr, s->port); | |
1713 | if (fd < 0) { | |
1714 | ret = -EIO; | |
1715 | goto cleanup; | |
1716 | } | |
1717 | ||
1718 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
1719 | s->inode.nr_copies, datalen, 0, 0); | |
1720 | if (ret < 0) { | |
1721 | error_report("failed to write snapshot's inode.\n"); | |
1722 | ret = -EIO; | |
1723 | goto cleanup; | |
1724 | } | |
1725 | ||
1726 | ret = do_sd_create(s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid, 1, | |
1727 | s->addr, s->port); | |
1728 | if (ret < 0) { | |
1729 | error_report("failed to create inode for snapshot. %s\n", | |
1730 | strerror(errno)); | |
1731 | ret = -EIO; | |
1732 | goto cleanup; | |
1733 | } | |
1734 | ||
1735 | inode = (SheepdogInode *)qemu_malloc(datalen); | |
1736 | ||
1737 | ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid), | |
1738 | s->inode.nr_copies, datalen, 0); | |
1739 | ||
1740 | if (ret < 0) { | |
1741 | error_report("failed to read new inode info. %s\n", strerror(errno)); | |
1742 | ret = -EIO; | |
1743 | goto cleanup; | |
1744 | } | |
1745 | ||
1746 | memcpy(&s->inode, inode, datalen); | |
1747 | dprintf("s->inode: name %s snap_id %x oid %x\n", | |
1748 | s->inode.name, s->inode.snap_id, s->inode.vdi_id); | |
1749 | ||
1750 | cleanup: | |
1751 | closesocket(fd); | |
1752 | return ret; | |
1753 | } | |
1754 | ||
1755 | static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) | |
1756 | { | |
1757 | BDRVSheepdogState *s = bs->opaque; | |
1758 | BDRVSheepdogState *old_s; | |
1759 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; | |
1760 | char *buf = NULL; | |
1761 | uint32_t vid; | |
1762 | uint32_t snapid = 0; | |
1763 | int ret = -ENOENT, fd; | |
1764 | ||
1765 | old_s = qemu_malloc(sizeof(BDRVSheepdogState)); | |
1766 | ||
1767 | memcpy(old_s, s, sizeof(BDRVSheepdogState)); | |
1768 | ||
1769 | memset(vdi, 0, sizeof(vdi)); | |
1770 | strncpy(vdi, s->name, sizeof(vdi)); | |
1771 | ||
1772 | memset(tag, 0, sizeof(tag)); | |
1773 | snapid = strtoul(snapshot_id, NULL, 10); | |
1774 | if (!snapid) { | |
1775 | strncpy(tag, s->name, sizeof(tag)); | |
1776 | } | |
1777 | ||
1778 | ret = find_vdi_name(s, vdi, snapid, tag, &vid, 1); | |
1779 | if (ret) { | |
1780 | error_report("Failed to find_vdi_name\n"); | |
1781 | ret = -ENOENT; | |
1782 | goto out; | |
1783 | } | |
1784 | ||
1785 | fd = connect_to_sdog(s->addr, s->port); | |
1786 | if (fd < 0) { | |
1787 | error_report("failed to connect\n"); | |
1788 | goto out; | |
1789 | } | |
1790 | ||
1791 | buf = qemu_malloc(SD_INODE_SIZE); | |
1792 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies, | |
1793 | SD_INODE_SIZE, 0); | |
1794 | ||
1795 | closesocket(fd); | |
1796 | ||
1797 | if (ret) { | |
1798 | ret = -ENOENT; | |
1799 | goto out; | |
1800 | } | |
1801 | ||
1802 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1803 | ||
1804 | if (!s->inode.vm_state_size) { | |
1805 | error_report("Invalid snapshot\n"); | |
1806 | ret = -ENOENT; | |
1807 | goto out; | |
1808 | } | |
1809 | ||
1810 | s->is_snapshot = 1; | |
1811 | ||
1812 | qemu_free(buf); | |
1813 | qemu_free(old_s); | |
1814 | ||
1815 | return 0; | |
1816 | out: | |
1817 | /* recover bdrv_sd_state */ | |
1818 | memcpy(s, old_s, sizeof(BDRVSheepdogState)); | |
1819 | qemu_free(buf); | |
1820 | qemu_free(old_s); | |
1821 | ||
1822 | error_report("failed to open. recover old bdrv_sd_state.\n"); | |
1823 | ||
1824 | return ret; | |
1825 | } | |
1826 | ||
1827 | static int sd_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
1828 | { | |
1829 | /* FIXME: Delete specified snapshot id. */ | |
1830 | return 0; | |
1831 | } | |
1832 | ||
33b1db1c MK |
1833 | static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
1834 | { | |
1835 | BDRVSheepdogState *s = bs->opaque; | |
1836 | SheepdogReq req; | |
1837 | int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long); | |
1838 | QEMUSnapshotInfo *sn_tab = NULL; | |
1839 | unsigned wlen, rlen; | |
1840 | int found = 0; | |
1841 | static SheepdogInode inode; | |
1842 | unsigned long *vdi_inuse; | |
1843 | unsigned int start_nr; | |
1844 | uint64_t hval; | |
1845 | uint32_t vid; | |
1846 | ||
1847 | vdi_inuse = qemu_malloc(max); | |
1848 | ||
1849 | fd = connect_to_sdog(s->addr, s->port); | |
1850 | if (fd < 0) { | |
1851 | goto out; | |
1852 | } | |
1853 | ||
1854 | rlen = max; | |
1855 | wlen = 0; | |
1856 | ||
1857 | memset(&req, 0, sizeof(req)); | |
1858 | ||
1859 | req.opcode = SD_OP_READ_VDIS; | |
1860 | req.data_length = max; | |
1861 | ||
1862 | ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen); | |
1863 | ||
1864 | closesocket(fd); | |
1865 | if (ret) { | |
1866 | goto out; | |
1867 | } | |
1868 | ||
1869 | sn_tab = qemu_mallocz(nr * sizeof(*sn_tab)); | |
1870 | ||
1871 | /* calculate a vdi id with hash function */ | |
1872 | hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT); | |
1873 | start_nr = hval & (SD_NR_VDIS - 1); | |
1874 | ||
1875 | fd = connect_to_sdog(s->addr, s->port); | |
1876 | if (fd < 0) { | |
1877 | error_report("failed to connect\n"); | |
1878 | goto out; | |
1879 | } | |
1880 | ||
1881 | for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) { | |
1882 | if (!test_bit(vid, vdi_inuse)) { | |
1883 | break; | |
1884 | } | |
1885 | ||
1886 | /* we don't need to read entire object */ | |
1887 | ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid), | |
1888 | 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0); | |
1889 | ||
1890 | if (ret) { | |
1891 | continue; | |
1892 | } | |
1893 | ||
1894 | if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) { | |
1895 | sn_tab[found].date_sec = inode.snap_ctime >> 32; | |
1896 | sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff; | |
1897 | sn_tab[found].vm_state_size = inode.vm_state_size; | |
1898 | sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec; | |
1899 | ||
1900 | snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u", | |
1901 | inode.snap_id); | |
1902 | strncpy(sn_tab[found].name, inode.tag, | |
1903 | MIN(sizeof(sn_tab[found].name), sizeof(inode.tag))); | |
1904 | found++; | |
1905 | } | |
1906 | } | |
1907 | ||
1908 | closesocket(fd); | |
1909 | out: | |
1910 | *psn_tab = sn_tab; | |
1911 | ||
1912 | qemu_free(vdi_inuse); | |
1913 | ||
1914 | return found; | |
1915 | } | |
1916 | ||
1917 | static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data, | |
1918 | int64_t pos, int size, int load) | |
1919 | { | |
1920 | int fd, create; | |
1921 | int ret = 0; | |
1922 | unsigned int data_len; | |
1923 | uint64_t vmstate_oid; | |
1924 | uint32_t vdi_index; | |
1925 | uint64_t offset; | |
1926 | ||
1927 | fd = connect_to_sdog(s->addr, s->port); | |
1928 | if (fd < 0) { | |
1929 | ret = -EIO; | |
1930 | goto cleanup; | |
1931 | } | |
1932 | ||
1933 | while (size) { | |
1934 | vdi_index = pos / SD_DATA_OBJ_SIZE; | |
1935 | offset = pos % SD_DATA_OBJ_SIZE; | |
1936 | ||
1937 | data_len = MIN(size, SD_DATA_OBJ_SIZE); | |
1938 | ||
1939 | vmstate_oid = vid_to_vmstate_oid(s->inode.vdi_id, vdi_index); | |
1940 | ||
1941 | create = (offset == 0); | |
1942 | if (load) { | |
1943 | ret = read_object(fd, (char *)data, vmstate_oid, | |
1944 | s->inode.nr_copies, data_len, offset); | |
1945 | } else { | |
1946 | ret = write_object(fd, (char *)data, vmstate_oid, | |
1947 | s->inode.nr_copies, data_len, offset, create); | |
1948 | } | |
1949 | ||
1950 | if (ret < 0) { | |
1951 | error_report("failed to save vmstate %s\n", strerror(errno)); | |
1952 | ret = -EIO; | |
1953 | goto cleanup; | |
1954 | } | |
1955 | ||
1956 | pos += data_len; | |
1957 | size -= data_len; | |
1958 | ret += data_len; | |
1959 | } | |
1960 | cleanup: | |
1961 | closesocket(fd); | |
1962 | return ret; | |
1963 | } | |
1964 | ||
1965 | static int sd_save_vmstate(BlockDriverState *bs, const uint8_t *data, | |
1966 | int64_t pos, int size) | |
1967 | { | |
1968 | BDRVSheepdogState *s = bs->opaque; | |
1969 | ||
1970 | return do_load_save_vmstate(s, (uint8_t *)data, pos, size, 0); | |
1971 | } | |
1972 | ||
1973 | static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data, | |
1974 | int64_t pos, int size) | |
1975 | { | |
1976 | BDRVSheepdogState *s = bs->opaque; | |
1977 | ||
1978 | return do_load_save_vmstate(s, data, pos, size, 1); | |
1979 | } | |
1980 | ||
1981 | ||
1982 | static QEMUOptionParameter sd_create_options[] = { | |
1983 | { | |
1984 | .name = BLOCK_OPT_SIZE, | |
1985 | .type = OPT_SIZE, | |
1986 | .help = "Virtual disk size" | |
1987 | }, | |
1988 | { | |
1989 | .name = BLOCK_OPT_BACKING_FILE, | |
1990 | .type = OPT_STRING, | |
1991 | .help = "File name of a base image" | |
1992 | }, | |
1993 | { NULL } | |
1994 | }; | |
1995 | ||
1996 | BlockDriver bdrv_sheepdog = { | |
1997 | .format_name = "sheepdog", | |
1998 | .protocol_name = "sheepdog", | |
1999 | .instance_size = sizeof(BDRVSheepdogState), | |
2000 | .bdrv_file_open = sd_open, | |
2001 | .bdrv_close = sd_close, | |
2002 | .bdrv_create = sd_create, | |
2003 | .bdrv_getlength = sd_getlength, | |
2004 | .bdrv_truncate = sd_truncate, | |
2005 | ||
2006 | .bdrv_aio_readv = sd_aio_readv, | |
2007 | .bdrv_aio_writev = sd_aio_writev, | |
2008 | ||
2009 | .bdrv_snapshot_create = sd_snapshot_create, | |
2010 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2011 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2012 | .bdrv_snapshot_list = sd_snapshot_list, | |
2013 | ||
2014 | .bdrv_save_vmstate = sd_save_vmstate, | |
2015 | .bdrv_load_vmstate = sd_load_vmstate, | |
2016 | ||
2017 | .create_options = sd_create_options, | |
2018 | }; | |
2019 | ||
2020 | static void bdrv_sheepdog_init(void) | |
2021 | { | |
2022 | bdrv_register(&bdrv_sheepdog); | |
2023 | } | |
2024 | block_init(bdrv_sheepdog_init); |