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