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