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