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