]>
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 | |
cac8f4a6 LY |
30 | /* 0x04 is used internally by Sheepdog */ |
31 | #define SD_OP_DISCARD_OBJ 0x05 | |
33b1db1c MK |
32 | |
33 | #define SD_OP_NEW_VDI 0x11 | |
34 | #define SD_OP_LOCK_VDI 0x12 | |
35 | #define SD_OP_RELEASE_VDI 0x13 | |
36 | #define SD_OP_GET_VDI_INFO 0x14 | |
37 | #define SD_OP_READ_VDIS 0x15 | |
47622c44 | 38 | #define SD_OP_FLUSH_VDI 0x16 |
859e5553 | 39 | #define SD_OP_DEL_VDI 0x17 |
33b1db1c MK |
40 | |
41 | #define SD_FLAG_CMD_WRITE 0x01 | |
42 | #define SD_FLAG_CMD_COW 0x02 | |
0e7106d8 LY |
43 | #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */ |
44 | #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */ | |
33b1db1c MK |
45 | |
46 | #define SD_RES_SUCCESS 0x00 /* Success */ | |
47 | #define SD_RES_UNKNOWN 0x01 /* Unknown error */ | |
48 | #define SD_RES_NO_OBJ 0x02 /* No object found */ | |
49 | #define SD_RES_EIO 0x03 /* I/O error */ | |
50 | #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */ | |
51 | #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */ | |
52 | #define SD_RES_SYSTEM_ERROR 0x06 /* System error */ | |
53 | #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */ | |
54 | #define SD_RES_NO_VDI 0x08 /* No vdi found */ | |
55 | #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */ | |
56 | #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */ | |
57 | #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */ | |
58 | #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */ | |
59 | #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */ | |
60 | #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */ | |
61 | #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */ | |
62 | #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */ | |
63 | #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */ | |
64 | #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */ | |
65 | #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */ | |
66 | #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */ | |
67 | #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */ | |
68 | #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */ | |
69 | #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */ | |
70 | #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */ | |
fca23f0a | 71 | #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */ |
6a0b5490 | 72 | #define SD_RES_READONLY 0x1A /* Object is read-only */ |
33b1db1c MK |
73 | |
74 | /* | |
75 | * Object ID rules | |
76 | * | |
77 | * 0 - 19 (20 bits): data object space | |
78 | * 20 - 31 (12 bits): reserved data object space | |
79 | * 32 - 55 (24 bits): vdi object space | |
80 | * 56 - 59 ( 4 bits): reserved vdi object space | |
7acae208 | 81 | * 60 - 63 ( 4 bits): object type identifier space |
33b1db1c MK |
82 | */ |
83 | ||
84 | #define VDI_SPACE_SHIFT 32 | |
85 | #define VDI_BIT (UINT64_C(1) << 63) | |
86 | #define VMSTATE_BIT (UINT64_C(1) << 62) | |
87 | #define MAX_DATA_OBJS (UINT64_C(1) << 20) | |
88 | #define MAX_CHILDREN 1024 | |
89 | #define SD_MAX_VDI_LEN 256 | |
90 | #define SD_MAX_VDI_TAG_LEN 256 | |
91 | #define SD_NR_VDIS (1U << 24) | |
92 | #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22) | |
93 | #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS) | |
b3af018f LY |
94 | /* |
95 | * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and | |
96 | * (SD_EC_MAX_STRIP - 1) for parity strips | |
97 | * | |
98 | * SD_MAX_COPIES is sum of number of data strips and parity strips. | |
99 | */ | |
100 | #define SD_EC_MAX_STRIP 16 | |
101 | #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1) | |
33b1db1c MK |
102 | |
103 | #define SD_INODE_SIZE (sizeof(SheepdogInode)) | |
104 | #define CURRENT_VDI_ID 0 | |
105 | ||
106 | typedef struct SheepdogReq { | |
107 | uint8_t proto_ver; | |
108 | uint8_t opcode; | |
109 | uint16_t flags; | |
110 | uint32_t epoch; | |
111 | uint32_t id; | |
112 | uint32_t data_length; | |
113 | uint32_t opcode_specific[8]; | |
114 | } SheepdogReq; | |
115 | ||
116 | typedef struct SheepdogRsp { | |
117 | uint8_t proto_ver; | |
118 | uint8_t opcode; | |
119 | uint16_t flags; | |
120 | uint32_t epoch; | |
121 | uint32_t id; | |
122 | uint32_t data_length; | |
123 | uint32_t result; | |
124 | uint32_t opcode_specific[7]; | |
125 | } SheepdogRsp; | |
126 | ||
127 | typedef struct SheepdogObjReq { | |
128 | uint8_t proto_ver; | |
129 | uint8_t opcode; | |
130 | uint16_t flags; | |
131 | uint32_t epoch; | |
132 | uint32_t id; | |
133 | uint32_t data_length; | |
134 | uint64_t oid; | |
135 | uint64_t cow_oid; | |
29a67f7e | 136 | uint8_t copies; |
1841f880 LY |
137 | uint8_t copy_policy; |
138 | uint8_t reserved[6]; | |
33b1db1c MK |
139 | uint64_t offset; |
140 | } SheepdogObjReq; | |
141 | ||
142 | typedef struct SheepdogObjRsp { | |
143 | uint8_t proto_ver; | |
144 | uint8_t opcode; | |
145 | uint16_t flags; | |
146 | uint32_t epoch; | |
147 | uint32_t id; | |
148 | uint32_t data_length; | |
149 | uint32_t result; | |
29a67f7e | 150 | uint8_t copies; |
1841f880 LY |
151 | uint8_t copy_policy; |
152 | uint8_t reserved[2]; | |
33b1db1c MK |
153 | uint32_t pad[6]; |
154 | } SheepdogObjRsp; | |
155 | ||
156 | typedef struct SheepdogVdiReq { | |
157 | uint8_t proto_ver; | |
158 | uint8_t opcode; | |
159 | uint16_t flags; | |
160 | uint32_t epoch; | |
161 | uint32_t id; | |
162 | uint32_t data_length; | |
163 | uint64_t vdi_size; | |
6f74c260 | 164 | uint32_t vdi_id; |
29a67f7e | 165 | uint8_t copies; |
1841f880 LY |
166 | uint8_t copy_policy; |
167 | uint8_t reserved[2]; | |
33b1db1c MK |
168 | uint32_t snapid; |
169 | uint32_t pad[3]; | |
170 | } SheepdogVdiReq; | |
171 | ||
172 | typedef struct SheepdogVdiRsp { | |
173 | uint8_t proto_ver; | |
174 | uint8_t opcode; | |
175 | uint16_t flags; | |
176 | uint32_t epoch; | |
177 | uint32_t id; | |
178 | uint32_t data_length; | |
179 | uint32_t result; | |
180 | uint32_t rsvd; | |
181 | uint32_t vdi_id; | |
182 | uint32_t pad[5]; | |
183 | } SheepdogVdiRsp; | |
184 | ||
185 | typedef struct SheepdogInode { | |
186 | char name[SD_MAX_VDI_LEN]; | |
187 | char tag[SD_MAX_VDI_TAG_LEN]; | |
188 | uint64_t ctime; | |
189 | uint64_t snap_ctime; | |
190 | uint64_t vm_clock_nsec; | |
191 | uint64_t vdi_size; | |
192 | uint64_t vm_state_size; | |
193 | uint16_t copy_policy; | |
194 | uint8_t nr_copies; | |
195 | uint8_t block_size_shift; | |
196 | uint32_t snap_id; | |
197 | uint32_t vdi_id; | |
198 | uint32_t parent_vdi_id; | |
199 | uint32_t child_vdi_id[MAX_CHILDREN]; | |
200 | uint32_t data_vdi_id[MAX_DATA_OBJS]; | |
201 | } SheepdogInode; | |
202 | ||
203 | /* | |
204 | * 64 bit FNV-1a non-zero initial basis | |
205 | */ | |
206 | #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) | |
207 | ||
208 | /* | |
209 | * 64 bit Fowler/Noll/Vo FNV-1a hash code | |
210 | */ | |
211 | static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval) | |
212 | { | |
213 | unsigned char *bp = buf; | |
214 | unsigned char *be = bp + len; | |
215 | while (bp < be) { | |
216 | hval ^= (uint64_t) *bp++; | |
217 | hval += (hval << 1) + (hval << 4) + (hval << 5) + | |
218 | (hval << 7) + (hval << 8) + (hval << 40); | |
219 | } | |
220 | return hval; | |
221 | } | |
222 | ||
2f536801 | 223 | static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx) |
33b1db1c MK |
224 | { |
225 | return inode->vdi_id == inode->data_vdi_id[idx]; | |
226 | } | |
227 | ||
2f536801 | 228 | static inline bool is_data_obj(uint64_t oid) |
33b1db1c MK |
229 | { |
230 | return !(VDI_BIT & oid); | |
231 | } | |
232 | ||
233 | static inline uint64_t data_oid_to_idx(uint64_t oid) | |
234 | { | |
235 | return oid & (MAX_DATA_OBJS - 1); | |
236 | } | |
237 | ||
72e0996c MK |
238 | static inline uint32_t oid_to_vid(uint64_t oid) |
239 | { | |
240 | return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT; | |
241 | } | |
242 | ||
33b1db1c MK |
243 | static inline uint64_t vid_to_vdi_oid(uint32_t vid) |
244 | { | |
245 | return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT); | |
246 | } | |
247 | ||
248 | static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx) | |
249 | { | |
250 | return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; | |
251 | } | |
252 | ||
253 | static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx) | |
254 | { | |
255 | return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; | |
256 | } | |
257 | ||
2f536801 | 258 | static inline bool is_snapshot(struct SheepdogInode *inode) |
33b1db1c MK |
259 | { |
260 | return !!inode->snap_ctime; | |
261 | } | |
262 | ||
2440a2c3 | 263 | #undef DPRINTF |
33b1db1c | 264 | #ifdef DEBUG_SDOG |
2440a2c3 | 265 | #define DPRINTF(fmt, args...) \ |
33b1db1c MK |
266 | do { \ |
267 | fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \ | |
268 | } while (0) | |
269 | #else | |
2440a2c3 | 270 | #define DPRINTF(fmt, args...) |
33b1db1c MK |
271 | #endif |
272 | ||
273 | typedef struct SheepdogAIOCB SheepdogAIOCB; | |
274 | ||
275 | typedef struct AIOReq { | |
276 | SheepdogAIOCB *aiocb; | |
277 | unsigned int iov_offset; | |
278 | ||
279 | uint64_t oid; | |
280 | uint64_t base_oid; | |
281 | uint64_t offset; | |
282 | unsigned int data_len; | |
283 | uint8_t flags; | |
284 | uint32_t id; | |
285 | ||
c292ee6a | 286 | QLIST_ENTRY(AIOReq) aio_siblings; |
33b1db1c MK |
287 | } AIOReq; |
288 | ||
289 | enum AIOCBState { | |
290 | AIOCB_WRITE_UDATA, | |
291 | AIOCB_READ_UDATA, | |
47783072 | 292 | AIOCB_FLUSH_CACHE, |
cac8f4a6 | 293 | AIOCB_DISCARD_OBJ, |
33b1db1c MK |
294 | }; |
295 | ||
296 | struct SheepdogAIOCB { | |
297 | BlockDriverAIOCB common; | |
298 | ||
299 | QEMUIOVector *qiov; | |
300 | ||
301 | int64_t sector_num; | |
302 | int nb_sectors; | |
303 | ||
304 | int ret; | |
305 | enum AIOCBState aiocb_type; | |
306 | ||
2df46246 | 307 | Coroutine *coroutine; |
33b1db1c MK |
308 | void (*aio_done_func)(SheepdogAIOCB *); |
309 | ||
35200687 MK |
310 | bool cancelable; |
311 | bool *finished; | |
1d732d7d | 312 | int nr_pending; |
33b1db1c MK |
313 | }; |
314 | ||
315 | typedef struct BDRVSheepdogState { | |
011603ca MK |
316 | BlockDriverState *bs; |
317 | ||
33b1db1c MK |
318 | SheepdogInode inode; |
319 | ||
320 | uint32_t min_dirty_data_idx; | |
321 | uint32_t max_dirty_data_idx; | |
322 | ||
323 | char name[SD_MAX_VDI_LEN]; | |
2f536801 | 324 | bool is_snapshot; |
0e7106d8 | 325 | uint32_t cache_flags; |
cac8f4a6 | 326 | bool discard_supported; |
33b1db1c | 327 | |
25af257d | 328 | char *host_spec; |
1b8bbb46 | 329 | bool is_unix; |
33b1db1c MK |
330 | int fd; |
331 | ||
2df46246 MK |
332 | CoMutex lock; |
333 | Coroutine *co_send; | |
334 | Coroutine *co_recv; | |
335 | ||
33b1db1c | 336 | uint32_t aioreq_seq_num; |
011603ca MK |
337 | |
338 | /* Every aio request must be linked to either of these queues. */ | |
c292ee6a MK |
339 | QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head; |
340 | QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head; | |
011603ca | 341 | QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head; |
33b1db1c MK |
342 | } BDRVSheepdogState; |
343 | ||
344 | static const char * sd_strerror(int err) | |
345 | { | |
346 | int i; | |
347 | ||
348 | static const struct { | |
349 | int err; | |
350 | const char *desc; | |
351 | } errors[] = { | |
352 | {SD_RES_SUCCESS, "Success"}, | |
353 | {SD_RES_UNKNOWN, "Unknown error"}, | |
354 | {SD_RES_NO_OBJ, "No object found"}, | |
355 | {SD_RES_EIO, "I/O error"}, | |
356 | {SD_RES_VDI_EXIST, "VDI exists already"}, | |
357 | {SD_RES_INVALID_PARMS, "Invalid parameters"}, | |
358 | {SD_RES_SYSTEM_ERROR, "System error"}, | |
359 | {SD_RES_VDI_LOCKED, "VDI is already locked"}, | |
360 | {SD_RES_NO_VDI, "No vdi found"}, | |
361 | {SD_RES_NO_BASE_VDI, "No base VDI found"}, | |
362 | {SD_RES_VDI_READ, "Failed read the requested VDI"}, | |
363 | {SD_RES_VDI_WRITE, "Failed to write the requested VDI"}, | |
364 | {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"}, | |
365 | {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"}, | |
366 | {SD_RES_NO_TAG, "Failed to find the requested tag"}, | |
367 | {SD_RES_STARTUP, "The system is still booting"}, | |
368 | {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"}, | |
369 | {SD_RES_SHUTDOWN, "The system is shutting down"}, | |
370 | {SD_RES_NO_MEM, "Out of memory on the server"}, | |
371 | {SD_RES_FULL_VDI, "We already have the maximum vdis"}, | |
372 | {SD_RES_VER_MISMATCH, "Protocol version mismatch"}, | |
373 | {SD_RES_NO_SPACE, "Server has no space for new objects"}, | |
374 | {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"}, | |
375 | {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"}, | |
376 | {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"}, | |
fca23f0a | 377 | {SD_RES_HALT, "Sheepdog is stopped serving IO request"}, |
6a0b5490 | 378 | {SD_RES_READONLY, "Object is read-only"}, |
33b1db1c MK |
379 | }; |
380 | ||
381 | for (i = 0; i < ARRAY_SIZE(errors); ++i) { | |
382 | if (errors[i].err == err) { | |
383 | return errors[i].desc; | |
384 | } | |
385 | } | |
386 | ||
387 | return "Invalid error code"; | |
388 | } | |
389 | ||
390 | /* | |
391 | * Sheepdog I/O handling: | |
392 | * | |
2df46246 | 393 | * 1. In sd_co_rw_vector, we send the I/O requests to the server and |
c292ee6a | 394 | * link the requests to the inflight_list in the |
2df46246 MK |
395 | * BDRVSheepdogState. The function exits without waiting for |
396 | * receiving the response. | |
33b1db1c | 397 | * |
2df46246 | 398 | * 2. We receive the response in aio_read_response, the fd handler to |
33b1db1c MK |
399 | * the sheepdog connection. If metadata update is needed, we send |
400 | * the write request to the vdi object in sd_write_done, the write | |
2df46246 MK |
401 | * completion function. We switch back to sd_co_readv/writev after |
402 | * all the requests belonging to the AIOCB are finished. | |
33b1db1c MK |
403 | */ |
404 | ||
405 | static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, | |
406 | uint64_t oid, unsigned int data_len, | |
407 | uint64_t offset, uint8_t flags, | |
408 | uint64_t base_oid, unsigned int iov_offset) | |
409 | { | |
410 | AIOReq *aio_req; | |
411 | ||
7267c094 | 412 | aio_req = g_malloc(sizeof(*aio_req)); |
33b1db1c MK |
413 | aio_req->aiocb = acb; |
414 | aio_req->iov_offset = iov_offset; | |
415 | aio_req->oid = oid; | |
416 | aio_req->base_oid = base_oid; | |
417 | aio_req->offset = offset; | |
418 | aio_req->data_len = data_len; | |
419 | aio_req->flags = flags; | |
420 | aio_req->id = s->aioreq_seq_num++; | |
421 | ||
1d732d7d | 422 | acb->nr_pending++; |
33b1db1c MK |
423 | return aio_req; |
424 | } | |
425 | ||
1d732d7d | 426 | static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) |
33b1db1c MK |
427 | { |
428 | SheepdogAIOCB *acb = aio_req->aiocb; | |
1d732d7d | 429 | |
35200687 | 430 | acb->cancelable = false; |
c292ee6a | 431 | QLIST_REMOVE(aio_req, aio_siblings); |
7267c094 | 432 | g_free(aio_req); |
33b1db1c | 433 | |
1d732d7d | 434 | acb->nr_pending--; |
33b1db1c MK |
435 | } |
436 | ||
d8716b41 | 437 | static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb) |
33b1db1c | 438 | { |
35200687 MK |
439 | qemu_coroutine_enter(acb->coroutine, NULL); |
440 | if (acb->finished) { | |
441 | *acb->finished = true; | |
33b1db1c MK |
442 | } |
443 | qemu_aio_release(acb); | |
444 | } | |
445 | ||
35200687 MK |
446 | /* |
447 | * Check whether the specified acb can be canceled | |
448 | * | |
449 | * We can cancel aio when any request belonging to the acb is: | |
450 | * - Not processed by the sheepdog server. | |
451 | * - Not linked to the inflight queue. | |
452 | */ | |
453 | static bool sd_acb_cancelable(const SheepdogAIOCB *acb) | |
454 | { | |
455 | BDRVSheepdogState *s = acb->common.bs->opaque; | |
456 | AIOReq *aioreq; | |
457 | ||
458 | if (!acb->cancelable) { | |
459 | return false; | |
460 | } | |
461 | ||
462 | QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) { | |
463 | if (aioreq->aiocb == acb) { | |
464 | return false; | |
465 | } | |
466 | } | |
467 | ||
468 | return true; | |
469 | } | |
470 | ||
33b1db1c MK |
471 | static void sd_aio_cancel(BlockDriverAIOCB *blockacb) |
472 | { | |
473 | SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb; | |
35200687 MK |
474 | BDRVSheepdogState *s = acb->common.bs->opaque; |
475 | AIOReq *aioreq, *next; | |
476 | bool finished = false; | |
477 | ||
478 | acb->finished = &finished; | |
479 | while (!finished) { | |
480 | if (sd_acb_cancelable(acb)) { | |
481 | /* Remove outstanding requests from pending and failed queues. */ | |
482 | QLIST_FOREACH_SAFE(aioreq, &s->pending_aio_head, aio_siblings, | |
483 | next) { | |
484 | if (aioreq->aiocb == acb) { | |
485 | free_aio_req(s, aioreq); | |
486 | } | |
487 | } | |
488 | QLIST_FOREACH_SAFE(aioreq, &s->failed_aio_head, aio_siblings, | |
489 | next) { | |
490 | if (aioreq->aiocb == acb) { | |
491 | free_aio_req(s, aioreq); | |
492 | } | |
493 | } | |
494 | ||
495 | assert(acb->nr_pending == 0); | |
496 | sd_finish_aiocb(acb); | |
497 | return; | |
498 | } | |
499 | qemu_aio_wait(); | |
500 | } | |
33b1db1c MK |
501 | } |
502 | ||
d7331bed | 503 | static const AIOCBInfo sd_aiocb_info = { |
33b1db1c MK |
504 | .aiocb_size = sizeof(SheepdogAIOCB), |
505 | .cancel = sd_aio_cancel, | |
506 | }; | |
507 | ||
508 | static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, | |
f700f8e3 | 509 | int64_t sector_num, int nb_sectors) |
33b1db1c MK |
510 | { |
511 | SheepdogAIOCB *acb; | |
512 | ||
f700f8e3 | 513 | acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL); |
33b1db1c MK |
514 | |
515 | acb->qiov = qiov; | |
516 | ||
517 | acb->sector_num = sector_num; | |
518 | acb->nb_sectors = nb_sectors; | |
519 | ||
520 | acb->aio_done_func = NULL; | |
35200687 MK |
521 | acb->cancelable = true; |
522 | acb->finished = NULL; | |
2df46246 | 523 | acb->coroutine = qemu_coroutine_self(); |
33b1db1c | 524 | acb->ret = 0; |
1d732d7d | 525 | acb->nr_pending = 0; |
33b1db1c MK |
526 | return acb; |
527 | } | |
528 | ||
25af257d | 529 | static int connect_to_sdog(BDRVSheepdogState *s) |
33b1db1c | 530 | { |
25af257d MK |
531 | int fd; |
532 | Error *err = NULL; | |
33b1db1c | 533 | |
1b8bbb46 MK |
534 | if (s->is_unix) { |
535 | fd = unix_connect(s->host_spec, &err); | |
536 | } else { | |
537 | fd = inet_connect(s->host_spec, &err); | |
538 | ||
539 | if (err == NULL) { | |
540 | int ret = socket_set_nodelay(fd); | |
541 | if (ret < 0) { | |
542 | error_report("%s", strerror(errno)); | |
543 | } | |
544 | } | |
545 | } | |
33b1db1c | 546 | |
25af257d MK |
547 | if (err != NULL) { |
548 | qerror_report_err(err); | |
549 | error_free(err); | |
0d6db300 | 550 | } else { |
f9e8cacc | 551 | qemu_set_nonblock(fd); |
33b1db1c MK |
552 | } |
553 | ||
33b1db1c MK |
554 | return fd; |
555 | } | |
556 | ||
e0d93a89 MK |
557 | static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, |
558 | unsigned int *wlen) | |
47622c44 LY |
559 | { |
560 | int ret; | |
561 | ||
562 | ret = qemu_co_send(sockfd, hdr, sizeof(*hdr)); | |
80731d9d | 563 | if (ret != sizeof(*hdr)) { |
47622c44 | 564 | error_report("failed to send a req, %s", strerror(errno)); |
eb092180 | 565 | return ret; |
47622c44 LY |
566 | } |
567 | ||
568 | ret = qemu_co_send(sockfd, data, *wlen); | |
80731d9d | 569 | if (ret != *wlen) { |
47622c44 LY |
570 | error_report("failed to send a req, %s", strerror(errno)); |
571 | } | |
572 | ||
573 | return ret; | |
574 | } | |
e0d93a89 | 575 | |
2dfcca3b MK |
576 | static void restart_co_req(void *opaque) |
577 | { | |
578 | Coroutine *co = opaque; | |
579 | ||
580 | qemu_coroutine_enter(co, NULL); | |
581 | } | |
582 | ||
cddd4ac7 MK |
583 | typedef struct SheepdogReqCo { |
584 | int sockfd; | |
585 | SheepdogReq *hdr; | |
586 | void *data; | |
587 | unsigned int *wlen; | |
588 | unsigned int *rlen; | |
589 | int ret; | |
590 | bool finished; | |
591 | } SheepdogReqCo; | |
592 | ||
593 | static coroutine_fn void do_co_req(void *opaque) | |
47622c44 LY |
594 | { |
595 | int ret; | |
2dfcca3b | 596 | Coroutine *co; |
cddd4ac7 MK |
597 | SheepdogReqCo *srco = opaque; |
598 | int sockfd = srco->sockfd; | |
599 | SheepdogReq *hdr = srco->hdr; | |
600 | void *data = srco->data; | |
601 | unsigned int *wlen = srco->wlen; | |
602 | unsigned int *rlen = srco->rlen; | |
2dfcca3b MK |
603 | |
604 | co = qemu_coroutine_self(); | |
f2e5dca4 | 605 | qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, co); |
47622c44 | 606 | |
47622c44 LY |
607 | ret = send_co_req(sockfd, hdr, data, wlen); |
608 | if (ret < 0) { | |
609 | goto out; | |
610 | } | |
611 | ||
f2e5dca4 | 612 | qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, co); |
2dfcca3b | 613 | |
47622c44 | 614 | ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr)); |
80731d9d | 615 | if (ret != sizeof(*hdr)) { |
47622c44 | 616 | error_report("failed to get a rsp, %s", strerror(errno)); |
cb595887 | 617 | ret = -errno; |
47622c44 LY |
618 | goto out; |
619 | } | |
620 | ||
621 | if (*rlen > hdr->data_length) { | |
622 | *rlen = hdr->data_length; | |
623 | } | |
624 | ||
625 | if (*rlen) { | |
626 | ret = qemu_co_recv(sockfd, data, *rlen); | |
80731d9d | 627 | if (ret != *rlen) { |
47622c44 | 628 | error_report("failed to get the data, %s", strerror(errno)); |
cb595887 | 629 | ret = -errno; |
47622c44 LY |
630 | goto out; |
631 | } | |
632 | } | |
633 | ret = 0; | |
634 | out: | |
ed9ba724 MK |
635 | /* there is at most one request for this sockfd, so it is safe to |
636 | * set each handler to NULL. */ | |
f2e5dca4 | 637 | qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL); |
cddd4ac7 MK |
638 | |
639 | srco->ret = ret; | |
640 | srco->finished = true; | |
641 | } | |
642 | ||
643 | static int do_req(int sockfd, SheepdogReq *hdr, void *data, | |
644 | unsigned int *wlen, unsigned int *rlen) | |
645 | { | |
646 | Coroutine *co; | |
647 | SheepdogReqCo srco = { | |
648 | .sockfd = sockfd, | |
649 | .hdr = hdr, | |
650 | .data = data, | |
651 | .wlen = wlen, | |
652 | .rlen = rlen, | |
653 | .ret = 0, | |
654 | .finished = false, | |
655 | }; | |
656 | ||
657 | if (qemu_in_coroutine()) { | |
658 | do_co_req(&srco); | |
659 | } else { | |
660 | co = qemu_coroutine_create(do_co_req); | |
661 | qemu_coroutine_enter(co, &srco); | |
662 | while (!srco.finished) { | |
663 | qemu_aio_wait(); | |
664 | } | |
665 | } | |
666 | ||
667 | return srco.ret; | |
47622c44 LY |
668 | } |
669 | ||
a37dcdf9 | 670 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
2f536801 | 671 | struct iovec *iov, int niov, bool create, |
33b1db1c | 672 | enum AIOCBState aiocb_type); |
a37dcdf9 | 673 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); |
72e0996c | 674 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag); |
011603ca MK |
675 | static int get_sheep_fd(BDRVSheepdogState *s); |
676 | static void co_write_request(void *opaque); | |
7dc1cde0 MK |
677 | |
678 | static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid) | |
679 | { | |
680 | AIOReq *aio_req; | |
681 | ||
682 | QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) { | |
683 | if (aio_req->oid == oid) { | |
684 | return aio_req; | |
685 | } | |
686 | } | |
687 | ||
688 | return NULL; | |
689 | } | |
690 | ||
33b1db1c MK |
691 | /* |
692 | * This function searchs pending requests to the object `oid', and | |
693 | * sends them. | |
694 | */ | |
c292ee6a | 695 | static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid) |
33b1db1c | 696 | { |
7dc1cde0 | 697 | AIOReq *aio_req; |
33b1db1c | 698 | SheepdogAIOCB *acb; |
33b1db1c | 699 | |
7dc1cde0 | 700 | while ((aio_req = find_pending_req(s, oid)) != NULL) { |
33b1db1c | 701 | acb = aio_req->aiocb; |
c292ee6a MK |
702 | /* move aio_req from pending list to inflight one */ |
703 | QLIST_REMOVE(aio_req, aio_siblings); | |
704 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
a37dcdf9 MK |
705 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, false, |
706 | acb->aiocb_type); | |
33b1db1c MK |
707 | } |
708 | } | |
709 | ||
011603ca MK |
710 | static coroutine_fn void reconnect_to_sdog(void *opaque) |
711 | { | |
712 | BDRVSheepdogState *s = opaque; | |
713 | AIOReq *aio_req, *next; | |
714 | ||
715 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); | |
716 | close(s->fd); | |
717 | s->fd = -1; | |
718 | ||
719 | /* Wait for outstanding write requests to be completed. */ | |
720 | while (s->co_send != NULL) { | |
721 | co_write_request(opaque); | |
722 | } | |
723 | ||
724 | /* Try to reconnect the sheepdog server every one second. */ | |
725 | while (s->fd < 0) { | |
726 | s->fd = get_sheep_fd(s); | |
727 | if (s->fd < 0) { | |
728 | DPRINTF("Wait for connection to be established\n"); | |
729 | co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME, | |
730 | 1000000000ULL); | |
731 | } | |
732 | }; | |
733 | ||
734 | /* | |
735 | * Now we have to resend all the request in the inflight queue. However, | |
736 | * resend_aioreq() can yield and newly created requests can be added to the | |
737 | * inflight queue before the coroutine is resumed. To avoid mixing them, we | |
738 | * have to move all the inflight requests to the failed queue before | |
739 | * resend_aioreq() is called. | |
740 | */ | |
741 | QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) { | |
742 | QLIST_REMOVE(aio_req, aio_siblings); | |
743 | QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings); | |
744 | } | |
745 | ||
746 | /* Resend all the failed aio requests. */ | |
747 | while (!QLIST_EMPTY(&s->failed_aio_head)) { | |
748 | aio_req = QLIST_FIRST(&s->failed_aio_head); | |
749 | QLIST_REMOVE(aio_req, aio_siblings); | |
750 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
751 | resend_aioreq(s, aio_req); | |
752 | } | |
753 | } | |
754 | ||
33b1db1c MK |
755 | /* |
756 | * Receive responses of the I/O requests. | |
757 | * | |
758 | * This function is registered as a fd handler, and called from the | |
759 | * main loop when s->fd is ready for reading responses. | |
760 | */ | |
d8716b41 | 761 | static void coroutine_fn aio_read_response(void *opaque) |
33b1db1c MK |
762 | { |
763 | SheepdogObjRsp rsp; | |
764 | BDRVSheepdogState *s = opaque; | |
765 | int fd = s->fd; | |
766 | int ret; | |
767 | AIOReq *aio_req = NULL; | |
768 | SheepdogAIOCB *acb; | |
cac8f4a6 | 769 | uint64_t idx; |
33b1db1c | 770 | |
33b1db1c | 771 | /* read a header */ |
8c5135f9 | 772 | ret = qemu_co_recv(fd, &rsp, sizeof(rsp)); |
80731d9d | 773 | if (ret != sizeof(rsp)) { |
6daf194d | 774 | error_report("failed to get the header, %s", strerror(errno)); |
011603ca | 775 | goto err; |
33b1db1c MK |
776 | } |
777 | ||
c292ee6a MK |
778 | /* find the right aio_req from the inflight aio list */ |
779 | QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) { | |
33b1db1c MK |
780 | if (aio_req->id == rsp.id) { |
781 | break; | |
782 | } | |
783 | } | |
784 | if (!aio_req) { | |
6daf194d | 785 | error_report("cannot find aio_req %x", rsp.id); |
011603ca | 786 | goto err; |
33b1db1c MK |
787 | } |
788 | ||
789 | acb = aio_req->aiocb; | |
790 | ||
791 | switch (acb->aiocb_type) { | |
792 | case AIOCB_WRITE_UDATA: | |
6d1acda8 MK |
793 | /* this coroutine context is no longer suitable for co_recv |
794 | * because we may send data to update vdi objects */ | |
795 | s->co_recv = NULL; | |
33b1db1c MK |
796 | if (!is_data_obj(aio_req->oid)) { |
797 | break; | |
798 | } | |
799 | idx = data_oid_to_idx(aio_req->oid); | |
800 | ||
801 | if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) { | |
802 | /* | |
803 | * If the object is newly created one, we need to update | |
804 | * the vdi object (metadata object). min_dirty_data_idx | |
805 | * and max_dirty_data_idx are changed to include updated | |
806 | * index between them. | |
807 | */ | |
bd751f22 LY |
808 | if (rsp.result == SD_RES_SUCCESS) { |
809 | s->inode.data_vdi_id[idx] = s->inode.vdi_id; | |
810 | s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx); | |
811 | s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx); | |
812 | } | |
33b1db1c MK |
813 | /* |
814 | * Some requests may be blocked because simultaneous | |
815 | * create requests are not allowed, so we search the | |
816 | * pending requests here. | |
817 | */ | |
d6b1ef89 | 818 | send_pending_req(s, aio_req->oid); |
33b1db1c MK |
819 | } |
820 | break; | |
821 | case AIOCB_READ_UDATA: | |
2fc8ae1d MT |
822 | ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov, |
823 | aio_req->iov_offset, rsp.data_length); | |
80731d9d | 824 | if (ret != rsp.data_length) { |
6daf194d | 825 | error_report("failed to get the data, %s", strerror(errno)); |
011603ca | 826 | goto err; |
33b1db1c MK |
827 | } |
828 | break; | |
47783072 LY |
829 | case AIOCB_FLUSH_CACHE: |
830 | if (rsp.result == SD_RES_INVALID_PARMS) { | |
2440a2c3 | 831 | DPRINTF("disable cache since the server doesn't support it\n"); |
47783072 LY |
832 | s->cache_flags = SD_FLAG_CMD_DIRECT; |
833 | rsp.result = SD_RES_SUCCESS; | |
834 | } | |
835 | break; | |
cac8f4a6 LY |
836 | case AIOCB_DISCARD_OBJ: |
837 | switch (rsp.result) { | |
838 | case SD_RES_INVALID_PARMS: | |
839 | error_report("sheep(%s) doesn't support discard command", | |
840 | s->host_spec); | |
841 | rsp.result = SD_RES_SUCCESS; | |
842 | s->discard_supported = false; | |
843 | break; | |
844 | case SD_RES_SUCCESS: | |
845 | idx = data_oid_to_idx(aio_req->oid); | |
846 | s->inode.data_vdi_id[idx] = 0; | |
847 | break; | |
848 | default: | |
849 | break; | |
850 | } | |
33b1db1c MK |
851 | } |
852 | ||
13c31de2 MK |
853 | switch (rsp.result) { |
854 | case SD_RES_SUCCESS: | |
855 | break; | |
856 | case SD_RES_READONLY: | |
72e0996c MK |
857 | if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) { |
858 | ret = reload_inode(s, 0, ""); | |
859 | if (ret < 0) { | |
011603ca | 860 | goto err; |
72e0996c MK |
861 | } |
862 | } | |
72e0996c MK |
863 | if (is_data_obj(aio_req->oid)) { |
864 | aio_req->oid = vid_to_data_oid(s->inode.vdi_id, | |
865 | data_oid_to_idx(aio_req->oid)); | |
866 | } else { | |
867 | aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id); | |
868 | } | |
a37dcdf9 MK |
869 | resend_aioreq(s, aio_req); |
870 | goto out; | |
13c31de2 | 871 | default: |
33b1db1c | 872 | acb->ret = -EIO; |
6daf194d | 873 | error_report("%s", sd_strerror(rsp.result)); |
13c31de2 | 874 | break; |
33b1db1c MK |
875 | } |
876 | ||
1d732d7d MK |
877 | free_aio_req(s, aio_req); |
878 | if (!acb->nr_pending) { | |
33b1db1c MK |
879 | /* |
880 | * We've finished all requests which belong to the AIOCB, so | |
2df46246 | 881 | * we can switch back to sd_co_readv/writev now. |
33b1db1c MK |
882 | */ |
883 | acb->aio_done_func(acb); | |
884 | } | |
2df46246 MK |
885 | out: |
886 | s->co_recv = NULL; | |
011603ca MK |
887 | return; |
888 | err: | |
889 | s->co_recv = NULL; | |
890 | reconnect_to_sdog(opaque); | |
2df46246 MK |
891 | } |
892 | ||
893 | static void co_read_response(void *opaque) | |
894 | { | |
895 | BDRVSheepdogState *s = opaque; | |
896 | ||
897 | if (!s->co_recv) { | |
898 | s->co_recv = qemu_coroutine_create(aio_read_response); | |
899 | } | |
900 | ||
901 | qemu_coroutine_enter(s->co_recv, opaque); | |
902 | } | |
903 | ||
904 | static void co_write_request(void *opaque) | |
905 | { | |
906 | BDRVSheepdogState *s = opaque; | |
907 | ||
908 | qemu_coroutine_enter(s->co_send, NULL); | |
33b1db1c MK |
909 | } |
910 | ||
33b1db1c MK |
911 | /* |
912 | * Return a socket discriptor to read/write objects. | |
913 | * | |
914 | * We cannot use this discriptor for other operations because | |
915 | * the block driver may be on waiting response from the server. | |
916 | */ | |
917 | static int get_sheep_fd(BDRVSheepdogState *s) | |
918 | { | |
1b8bbb46 | 919 | int fd; |
33b1db1c | 920 | |
25af257d | 921 | fd = connect_to_sdog(s); |
33b1db1c | 922 | if (fd < 0) { |
cb595887 | 923 | return fd; |
33b1db1c MK |
924 | } |
925 | ||
f2e5dca4 | 926 | qemu_aio_set_fd_handler(fd, co_read_response, NULL, s); |
33b1db1c MK |
927 | return fd; |
928 | } | |
929 | ||
5d6768e3 MK |
930 | static int sd_parse_uri(BDRVSheepdogState *s, const char *filename, |
931 | char *vdi, uint32_t *snapid, char *tag) | |
932 | { | |
933 | URI *uri; | |
934 | QueryParams *qp = NULL; | |
935 | int ret = 0; | |
936 | ||
937 | uri = uri_parse(filename); | |
938 | if (!uri) { | |
939 | return -EINVAL; | |
940 | } | |
941 | ||
1b8bbb46 MK |
942 | /* transport */ |
943 | if (!strcmp(uri->scheme, "sheepdog")) { | |
944 | s->is_unix = false; | |
945 | } else if (!strcmp(uri->scheme, "sheepdog+tcp")) { | |
946 | s->is_unix = false; | |
947 | } else if (!strcmp(uri->scheme, "sheepdog+unix")) { | |
948 | s->is_unix = true; | |
949 | } else { | |
950 | ret = -EINVAL; | |
951 | goto out; | |
952 | } | |
953 | ||
5d6768e3 MK |
954 | if (uri->path == NULL || !strcmp(uri->path, "/")) { |
955 | ret = -EINVAL; | |
956 | goto out; | |
957 | } | |
958 | pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1); | |
959 | ||
1b8bbb46 MK |
960 | qp = query_params_parse(uri->query); |
961 | if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) { | |
962 | ret = -EINVAL; | |
963 | goto out; | |
964 | } | |
965 | ||
966 | if (s->is_unix) { | |
967 | /* sheepdog+unix:///vdiname?socket=path */ | |
968 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
969 | ret = -EINVAL; | |
970 | goto out; | |
971 | } | |
972 | s->host_spec = g_strdup(qp->p[0].value); | |
973 | } else { | |
974 | /* sheepdog[+tcp]://[host:port]/vdiname */ | |
975 | s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR, | |
976 | uri->port ?: SD_DEFAULT_PORT); | |
977 | } | |
5d6768e3 MK |
978 | |
979 | /* snapshot tag */ | |
980 | if (uri->fragment) { | |
981 | *snapid = strtoul(uri->fragment, NULL, 10); | |
982 | if (*snapid == 0) { | |
983 | pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment); | |
984 | } | |
985 | } else { | |
986 | *snapid = CURRENT_VDI_ID; /* search current vdi */ | |
987 | } | |
988 | ||
989 | out: | |
990 | if (qp) { | |
991 | query_params_free(qp); | |
992 | } | |
993 | uri_free(uri); | |
994 | return ret; | |
995 | } | |
996 | ||
33b1db1c | 997 | /* |
5d6768e3 | 998 | * Parse a filename (old syntax) |
33b1db1c MK |
999 | * |
1000 | * filename must be one of the following formats: | |
1001 | * 1. [vdiname] | |
1002 | * 2. [vdiname]:[snapid] | |
1003 | * 3. [vdiname]:[tag] | |
1004 | * 4. [hostname]:[port]:[vdiname] | |
1005 | * 5. [hostname]:[port]:[vdiname]:[snapid] | |
1006 | * 6. [hostname]:[port]:[vdiname]:[tag] | |
1007 | * | |
1008 | * You can boot from the snapshot images by specifying `snapid` or | |
1009 | * `tag'. | |
1010 | * | |
1011 | * You can run VMs outside the Sheepdog cluster by specifying | |
1012 | * `hostname' and `port' (experimental). | |
1013 | */ | |
1014 | static int parse_vdiname(BDRVSheepdogState *s, const char *filename, | |
1015 | char *vdi, uint32_t *snapid, char *tag) | |
1016 | { | |
5d6768e3 MK |
1017 | char *p, *q, *uri; |
1018 | const char *host_spec, *vdi_spec; | |
1019 | int nr_sep, ret; | |
33b1db1c | 1020 | |
5d6768e3 | 1021 | strstart(filename, "sheepdog:", (const char **)&filename); |
7267c094 | 1022 | p = q = g_strdup(filename); |
33b1db1c MK |
1023 | |
1024 | /* count the number of separators */ | |
1025 | nr_sep = 0; | |
1026 | while (*p) { | |
1027 | if (*p == ':') { | |
1028 | nr_sep++; | |
1029 | } | |
1030 | p++; | |
1031 | } | |
1032 | p = q; | |
1033 | ||
5d6768e3 | 1034 | /* use the first two tokens as host_spec. */ |
33b1db1c | 1035 | if (nr_sep >= 2) { |
5d6768e3 | 1036 | host_spec = p; |
33b1db1c | 1037 | p = strchr(p, ':'); |
5d6768e3 | 1038 | p++; |
33b1db1c MK |
1039 | p = strchr(p, ':'); |
1040 | *p++ = '\0'; | |
1041 | } else { | |
5d6768e3 | 1042 | host_spec = ""; |
33b1db1c MK |
1043 | } |
1044 | ||
5d6768e3 | 1045 | vdi_spec = p; |
33b1db1c | 1046 | |
5d6768e3 | 1047 | p = strchr(vdi_spec, ':'); |
33b1db1c | 1048 | if (p) { |
5d6768e3 | 1049 | *p++ = '#'; |
33b1db1c MK |
1050 | } |
1051 | ||
5d6768e3 | 1052 | uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec); |
33b1db1c | 1053 | |
5d6768e3 MK |
1054 | ret = sd_parse_uri(s, uri, vdi, snapid, tag); |
1055 | ||
1056 | g_free(q); | |
1057 | g_free(uri); | |
1058 | ||
1059 | return ret; | |
33b1db1c MK |
1060 | } |
1061 | ||
982dcbf4 MK |
1062 | static int find_vdi_name(BDRVSheepdogState *s, const char *filename, |
1063 | uint32_t snapid, const char *tag, uint32_t *vid, | |
1064 | bool lock) | |
33b1db1c MK |
1065 | { |
1066 | int ret, fd; | |
1067 | SheepdogVdiReq hdr; | |
1068 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1069 | unsigned int wlen, rlen = 0; | |
1070 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; | |
1071 | ||
25af257d | 1072 | fd = connect_to_sdog(s); |
33b1db1c | 1073 | if (fd < 0) { |
cb595887 | 1074 | return fd; |
33b1db1c MK |
1075 | } |
1076 | ||
3178e275 JM |
1077 | /* This pair of strncpy calls ensures that the buffer is zero-filled, |
1078 | * which is desirable since we'll soon be sending those bytes, and | |
1079 | * don't want the send_req to read uninitialized data. | |
1080 | */ | |
33b1db1c MK |
1081 | strncpy(buf, filename, SD_MAX_VDI_LEN); |
1082 | strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN); | |
1083 | ||
1084 | memset(&hdr, 0, sizeof(hdr)); | |
982dcbf4 | 1085 | if (lock) { |
33b1db1c | 1086 | hdr.opcode = SD_OP_LOCK_VDI; |
982dcbf4 MK |
1087 | } else { |
1088 | hdr.opcode = SD_OP_GET_VDI_INFO; | |
33b1db1c MK |
1089 | } |
1090 | wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN; | |
1091 | hdr.proto_ver = SD_PROTO_VER; | |
1092 | hdr.data_length = wlen; | |
1093 | hdr.snapid = snapid; | |
1094 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1095 | ||
1096 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1097 | if (ret) { | |
33b1db1c MK |
1098 | goto out; |
1099 | } | |
1100 | ||
1101 | if (rsp->result != SD_RES_SUCCESS) { | |
6daf194d | 1102 | error_report("cannot get vdi info, %s, %s %d %s", |
33b1db1c | 1103 | sd_strerror(rsp->result), filename, snapid, tag); |
cb595887 MK |
1104 | if (rsp->result == SD_RES_NO_VDI) { |
1105 | ret = -ENOENT; | |
1106 | } else { | |
1107 | ret = -EIO; | |
1108 | } | |
33b1db1c MK |
1109 | goto out; |
1110 | } | |
1111 | *vid = rsp->vdi_id; | |
1112 | ||
1113 | ret = 0; | |
1114 | out: | |
1115 | closesocket(fd); | |
1116 | return ret; | |
1117 | } | |
1118 | ||
a37dcdf9 | 1119 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
2f536801 | 1120 | struct iovec *iov, int niov, bool create, |
33b1db1c MK |
1121 | enum AIOCBState aiocb_type) |
1122 | { | |
1123 | int nr_copies = s->inode.nr_copies; | |
1124 | SheepdogObjReq hdr; | |
47783072 | 1125 | unsigned int wlen = 0; |
33b1db1c MK |
1126 | int ret; |
1127 | uint64_t oid = aio_req->oid; | |
1128 | unsigned int datalen = aio_req->data_len; | |
1129 | uint64_t offset = aio_req->offset; | |
1130 | uint8_t flags = aio_req->flags; | |
1131 | uint64_t old_oid = aio_req->base_oid; | |
1132 | ||
1133 | if (!nr_copies) { | |
6daf194d | 1134 | error_report("bug"); |
33b1db1c MK |
1135 | } |
1136 | ||
1137 | memset(&hdr, 0, sizeof(hdr)); | |
1138 | ||
47783072 LY |
1139 | switch (aiocb_type) { |
1140 | case AIOCB_FLUSH_CACHE: | |
1141 | hdr.opcode = SD_OP_FLUSH_VDI; | |
1142 | break; | |
1143 | case AIOCB_READ_UDATA: | |
33b1db1c MK |
1144 | hdr.opcode = SD_OP_READ_OBJ; |
1145 | hdr.flags = flags; | |
47783072 LY |
1146 | break; |
1147 | case AIOCB_WRITE_UDATA: | |
1148 | if (create) { | |
1149 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1150 | } else { | |
1151 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1152 | } | |
33b1db1c | 1153 | wlen = datalen; |
33b1db1c | 1154 | hdr.flags = SD_FLAG_CMD_WRITE | flags; |
47783072 | 1155 | break; |
cac8f4a6 LY |
1156 | case AIOCB_DISCARD_OBJ: |
1157 | hdr.opcode = SD_OP_DISCARD_OBJ; | |
1158 | break; | |
33b1db1c MK |
1159 | } |
1160 | ||
0e7106d8 LY |
1161 | if (s->cache_flags) { |
1162 | hdr.flags |= s->cache_flags; | |
47622c44 LY |
1163 | } |
1164 | ||
33b1db1c MK |
1165 | hdr.oid = oid; |
1166 | hdr.cow_oid = old_oid; | |
1167 | hdr.copies = s->inode.nr_copies; | |
1168 | ||
1169 | hdr.data_length = datalen; | |
1170 | hdr.offset = offset; | |
1171 | ||
1172 | hdr.id = aio_req->id; | |
1173 | ||
2df46246 MK |
1174 | qemu_co_mutex_lock(&s->lock); |
1175 | s->co_send = qemu_coroutine_self(); | |
f2e5dca4 | 1176 | qemu_aio_set_fd_handler(s->fd, co_read_response, co_write_request, s); |
128aa589 | 1177 | socket_set_cork(s->fd, 1); |
33b1db1c MK |
1178 | |
1179 | /* send a header */ | |
8c5135f9 | 1180 | ret = qemu_co_send(s->fd, &hdr, sizeof(hdr)); |
80731d9d | 1181 | if (ret != sizeof(hdr)) { |
6daf194d | 1182 | error_report("failed to send a req, %s", strerror(errno)); |
011603ca | 1183 | goto out; |
33b1db1c MK |
1184 | } |
1185 | ||
1186 | if (wlen) { | |
2fc8ae1d | 1187 | ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen); |
80731d9d | 1188 | if (ret != wlen) { |
6daf194d | 1189 | error_report("failed to send a data, %s", strerror(errno)); |
33b1db1c MK |
1190 | } |
1191 | } | |
011603ca | 1192 | out: |
128aa589 | 1193 | socket_set_cork(s->fd, 0); |
f2e5dca4 | 1194 | qemu_aio_set_fd_handler(s->fd, co_read_response, NULL, s); |
011603ca | 1195 | s->co_send = NULL; |
2df46246 | 1196 | qemu_co_mutex_unlock(&s->lock); |
33b1db1c MK |
1197 | } |
1198 | ||
29a67f7e | 1199 | static int read_write_object(int fd, char *buf, uint64_t oid, uint8_t copies, |
33b1db1c | 1200 | unsigned int datalen, uint64_t offset, |
0e7106d8 | 1201 | bool write, bool create, uint32_t cache_flags) |
33b1db1c MK |
1202 | { |
1203 | SheepdogObjReq hdr; | |
1204 | SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr; | |
1205 | unsigned int wlen, rlen; | |
1206 | int ret; | |
1207 | ||
1208 | memset(&hdr, 0, sizeof(hdr)); | |
1209 | ||
1210 | if (write) { | |
1211 | wlen = datalen; | |
1212 | rlen = 0; | |
1213 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1214 | if (create) { | |
1215 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1216 | } else { | |
1217 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1218 | } | |
1219 | } else { | |
1220 | wlen = 0; | |
1221 | rlen = datalen; | |
1222 | hdr.opcode = SD_OP_READ_OBJ; | |
1223 | } | |
47622c44 | 1224 | |
0e7106d8 | 1225 | hdr.flags |= cache_flags; |
47622c44 | 1226 | |
33b1db1c MK |
1227 | hdr.oid = oid; |
1228 | hdr.data_length = datalen; | |
1229 | hdr.offset = offset; | |
1230 | hdr.copies = copies; | |
1231 | ||
1232 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1233 | if (ret) { | |
6daf194d | 1234 | error_report("failed to send a request to the sheep"); |
cb595887 | 1235 | return ret; |
33b1db1c MK |
1236 | } |
1237 | ||
1238 | switch (rsp->result) { | |
1239 | case SD_RES_SUCCESS: | |
1240 | return 0; | |
1241 | default: | |
6daf194d | 1242 | error_report("%s", sd_strerror(rsp->result)); |
cb595887 | 1243 | return -EIO; |
33b1db1c MK |
1244 | } |
1245 | } | |
1246 | ||
29a67f7e | 1247 | static int read_object(int fd, char *buf, uint64_t oid, uint8_t copies, |
0e7106d8 LY |
1248 | unsigned int datalen, uint64_t offset, |
1249 | uint32_t cache_flags) | |
33b1db1c | 1250 | { |
2f536801 | 1251 | return read_write_object(fd, buf, oid, copies, datalen, offset, false, |
0e7106d8 | 1252 | false, cache_flags); |
33b1db1c MK |
1253 | } |
1254 | ||
29a67f7e | 1255 | static int write_object(int fd, char *buf, uint64_t oid, uint8_t copies, |
2f536801 | 1256 | unsigned int datalen, uint64_t offset, bool create, |
0e7106d8 | 1257 | uint32_t cache_flags) |
33b1db1c | 1258 | { |
2f536801 | 1259 | return read_write_object(fd, buf, oid, copies, datalen, offset, true, |
0e7106d8 | 1260 | create, cache_flags); |
33b1db1c MK |
1261 | } |
1262 | ||
9ff53a0e MK |
1263 | /* update inode with the latest state */ |
1264 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag) | |
1265 | { | |
1266 | SheepdogInode *inode; | |
1267 | int ret = 0, fd; | |
1268 | uint32_t vid = 0; | |
1269 | ||
1270 | fd = connect_to_sdog(s); | |
1271 | if (fd < 0) { | |
1272 | return -EIO; | |
1273 | } | |
1274 | ||
1275 | inode = g_malloc(sizeof(s->inode)); | |
1276 | ||
1277 | ret = find_vdi_name(s, s->name, snapid, tag, &vid, false); | |
1278 | if (ret) { | |
1279 | goto out; | |
1280 | } | |
1281 | ||
1282 | ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid), | |
1283 | s->inode.nr_copies, sizeof(*inode), 0, s->cache_flags); | |
1284 | if (ret < 0) { | |
1285 | goto out; | |
1286 | } | |
1287 | ||
1288 | if (inode->vdi_id != s->inode.vdi_id) { | |
1289 | memcpy(&s->inode, inode, sizeof(s->inode)); | |
1290 | } | |
1291 | ||
1292 | out: | |
1293 | g_free(inode); | |
1294 | closesocket(fd); | |
1295 | ||
1296 | return ret; | |
1297 | } | |
1298 | ||
80308d33 MK |
1299 | /* Return true if the specified request is linked to the pending list. */ |
1300 | static bool check_simultaneous_create(BDRVSheepdogState *s, AIOReq *aio_req) | |
1301 | { | |
1302 | AIOReq *areq; | |
1303 | QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) { | |
1304 | if (areq != aio_req && areq->oid == aio_req->oid) { | |
1305 | /* | |
1306 | * Sheepdog cannot handle simultaneous create requests to the same | |
1307 | * object, so we cannot send the request until the previous request | |
1308 | * finishes. | |
1309 | */ | |
1310 | DPRINTF("simultaneous create to %" PRIx64 "\n", aio_req->oid); | |
1311 | aio_req->flags = 0; | |
1312 | aio_req->base_oid = 0; | |
1313 | QLIST_REMOVE(aio_req, aio_siblings); | |
1314 | QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req, aio_siblings); | |
1315 | return true; | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | return false; | |
1320 | } | |
1321 | ||
a37dcdf9 | 1322 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) |
13c31de2 MK |
1323 | { |
1324 | SheepdogAIOCB *acb = aio_req->aiocb; | |
1325 | bool create = false; | |
13c31de2 MK |
1326 | |
1327 | /* check whether this request becomes a CoW one */ | |
2412aec7 | 1328 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) { |
13c31de2 | 1329 | int idx = data_oid_to_idx(aio_req->oid); |
13c31de2 | 1330 | |
13c31de2 MK |
1331 | if (is_data_obj_writable(&s->inode, idx)) { |
1332 | goto out; | |
1333 | } | |
1334 | ||
80308d33 MK |
1335 | if (check_simultaneous_create(s, aio_req)) { |
1336 | return; | |
13c31de2 MK |
1337 | } |
1338 | ||
80308d33 MK |
1339 | if (s->inode.data_vdi_id[idx]) { |
1340 | aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx); | |
1341 | aio_req->flags |= SD_FLAG_CMD_COW; | |
1342 | } | |
13c31de2 MK |
1343 | create = true; |
1344 | } | |
1345 | out: | |
2412aec7 | 1346 | if (is_data_obj(aio_req->oid)) { |
a37dcdf9 MK |
1347 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create, |
1348 | acb->aiocb_type); | |
2412aec7 MK |
1349 | } else { |
1350 | struct iovec iov; | |
1351 | iov.iov_base = &s->inode; | |
1352 | iov.iov_len = sizeof(s->inode); | |
a37dcdf9 | 1353 | add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA); |
2412aec7 | 1354 | } |
13c31de2 MK |
1355 | } |
1356 | ||
c8c96350 KW |
1357 | /* TODO Convert to fine grained options */ |
1358 | static QemuOptsList runtime_opts = { | |
1359 | .name = "sheepdog", | |
1360 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1361 | .desc = { | |
1362 | { | |
1363 | .name = "filename", | |
1364 | .type = QEMU_OPT_STRING, | |
1365 | .help = "URL to the sheepdog image", | |
1366 | }, | |
1367 | { /* end of list */ } | |
1368 | }, | |
1369 | }; | |
1370 | ||
015a1036 HR |
1371 | static int sd_open(BlockDriverState *bs, QDict *options, int flags, |
1372 | Error **errp) | |
33b1db1c MK |
1373 | { |
1374 | int ret, fd; | |
1375 | uint32_t vid = 0; | |
1376 | BDRVSheepdogState *s = bs->opaque; | |
1377 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; | |
1378 | uint32_t snapid; | |
1379 | char *buf = NULL; | |
c8c96350 KW |
1380 | QemuOpts *opts; |
1381 | Error *local_err = NULL; | |
1382 | const char *filename; | |
1383 | ||
011603ca MK |
1384 | s->bs = bs; |
1385 | ||
c8c96350 KW |
1386 | opts = qemu_opts_create_nofail(&runtime_opts); |
1387 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
1388 | if (error_is_set(&local_err)) { | |
1389 | qerror_report_err(local_err); | |
1390 | error_free(local_err); | |
1391 | ret = -EINVAL; | |
1392 | goto out; | |
1393 | } | |
1394 | ||
1395 | filename = qemu_opt_get(opts, "filename"); | |
33b1db1c | 1396 | |
c292ee6a MK |
1397 | QLIST_INIT(&s->inflight_aio_head); |
1398 | QLIST_INIT(&s->pending_aio_head); | |
011603ca | 1399 | QLIST_INIT(&s->failed_aio_head); |
33b1db1c MK |
1400 | s->fd = -1; |
1401 | ||
1402 | memset(vdi, 0, sizeof(vdi)); | |
1403 | memset(tag, 0, sizeof(tag)); | |
5d6768e3 MK |
1404 | |
1405 | if (strstr(filename, "://")) { | |
1406 | ret = sd_parse_uri(s, filename, vdi, &snapid, tag); | |
1407 | } else { | |
1408 | ret = parse_vdiname(s, filename, vdi, &snapid, tag); | |
1409 | } | |
1410 | if (ret < 0) { | |
33b1db1c MK |
1411 | goto out; |
1412 | } | |
1413 | s->fd = get_sheep_fd(s); | |
1414 | if (s->fd < 0) { | |
cb595887 | 1415 | ret = s->fd; |
33b1db1c MK |
1416 | goto out; |
1417 | } | |
1418 | ||
982dcbf4 | 1419 | ret = find_vdi_name(s, vdi, snapid, tag, &vid, true); |
33b1db1c MK |
1420 | if (ret) { |
1421 | goto out; | |
1422 | } | |
1423 | ||
0e7106d8 LY |
1424 | /* |
1425 | * QEMU block layer emulates writethrough cache as 'writeback + flush', so | |
1426 | * we always set SD_FLAG_CMD_CACHE (writeback cache) as default. | |
1427 | */ | |
1428 | s->cache_flags = SD_FLAG_CMD_CACHE; | |
1429 | if (flags & BDRV_O_NOCACHE) { | |
1430 | s->cache_flags = SD_FLAG_CMD_DIRECT; | |
1431 | } | |
cac8f4a6 | 1432 | s->discard_supported = true; |
0e7106d8 | 1433 | |
622b6057 | 1434 | if (snapid || tag[0] != '\0') { |
2440a2c3 | 1435 | DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid); |
2f536801 | 1436 | s->is_snapshot = true; |
33b1db1c MK |
1437 | } |
1438 | ||
25af257d | 1439 | fd = connect_to_sdog(s); |
33b1db1c | 1440 | if (fd < 0) { |
cb595887 | 1441 | ret = fd; |
33b1db1c MK |
1442 | goto out; |
1443 | } | |
1444 | ||
7267c094 | 1445 | buf = g_malloc(SD_INODE_SIZE); |
47622c44 | 1446 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0, |
0e7106d8 | 1447 | s->cache_flags); |
33b1db1c MK |
1448 | |
1449 | closesocket(fd); | |
1450 | ||
1451 | if (ret) { | |
1452 | goto out; | |
1453 | } | |
1454 | ||
1455 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1456 | s->min_dirty_data_idx = UINT32_MAX; | |
1457 | s->max_dirty_data_idx = 0; | |
1458 | ||
e8bfaa2f | 1459 | bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE; |
3178e275 | 1460 | pstrcpy(s->name, sizeof(s->name), vdi); |
2df46246 | 1461 | qemu_co_mutex_init(&s->lock); |
c8c96350 | 1462 | qemu_opts_del(opts); |
7267c094 | 1463 | g_free(buf); |
33b1db1c MK |
1464 | return 0; |
1465 | out: | |
f2e5dca4 | 1466 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); |
33b1db1c MK |
1467 | if (s->fd >= 0) { |
1468 | closesocket(s->fd); | |
1469 | } | |
c8c96350 | 1470 | qemu_opts_del(opts); |
7267c094 | 1471 | g_free(buf); |
cb595887 | 1472 | return ret; |
33b1db1c MK |
1473 | } |
1474 | ||
c31d482f | 1475 | static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot) |
33b1db1c MK |
1476 | { |
1477 | SheepdogVdiReq hdr; | |
1478 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1479 | int fd, ret; | |
1480 | unsigned int wlen, rlen = 0; | |
1481 | char buf[SD_MAX_VDI_LEN]; | |
1482 | ||
25af257d | 1483 | fd = connect_to_sdog(s); |
33b1db1c | 1484 | if (fd < 0) { |
cb595887 | 1485 | return fd; |
33b1db1c MK |
1486 | } |
1487 | ||
3178e275 JM |
1488 | /* FIXME: would it be better to fail (e.g., return -EIO) when filename |
1489 | * does not fit in buf? For now, just truncate and avoid buffer overrun. | |
1490 | */ | |
33b1db1c | 1491 | memset(buf, 0, sizeof(buf)); |
c31d482f | 1492 | pstrcpy(buf, sizeof(buf), s->name); |
33b1db1c MK |
1493 | |
1494 | memset(&hdr, 0, sizeof(hdr)); | |
1495 | hdr.opcode = SD_OP_NEW_VDI; | |
c31d482f | 1496 | hdr.vdi_id = s->inode.vdi_id; |
33b1db1c MK |
1497 | |
1498 | wlen = SD_MAX_VDI_LEN; | |
1499 | ||
1500 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1501 | hdr.snapid = snapshot; | |
1502 | ||
1503 | hdr.data_length = wlen; | |
c31d482f LY |
1504 | hdr.vdi_size = s->inode.vdi_size; |
1505 | hdr.copy_policy = s->inode.copy_policy; | |
b3af018f | 1506 | hdr.copies = s->inode.nr_copies; |
33b1db1c MK |
1507 | |
1508 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1509 | ||
1510 | closesocket(fd); | |
1511 | ||
1512 | if (ret) { | |
cb595887 | 1513 | return ret; |
33b1db1c MK |
1514 | } |
1515 | ||
1516 | if (rsp->result != SD_RES_SUCCESS) { | |
c31d482f | 1517 | error_report("%s, %s", sd_strerror(rsp->result), s->inode.name); |
33b1db1c MK |
1518 | return -EIO; |
1519 | } | |
1520 | ||
1521 | if (vdi_id) { | |
1522 | *vdi_id = rsp->vdi_id; | |
1523 | } | |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
a8e0fdd7 MK |
1528 | static int sd_prealloc(const char *filename) |
1529 | { | |
1530 | BlockDriverState *bs = NULL; | |
1531 | uint32_t idx, max_idx; | |
1532 | int64_t vdi_size; | |
7267c094 | 1533 | void *buf = g_malloc0(SD_DATA_OBJ_SIZE); |
34b5d2c6 | 1534 | Error *local_err = NULL; |
a8e0fdd7 MK |
1535 | int ret; |
1536 | ||
34b5d2c6 | 1537 | ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err); |
a8e0fdd7 | 1538 | if (ret < 0) { |
34b5d2c6 HR |
1539 | qerror_report_err(local_err); |
1540 | error_free(local_err); | |
a8e0fdd7 MK |
1541 | goto out; |
1542 | } | |
1543 | ||
1544 | vdi_size = bdrv_getlength(bs); | |
1545 | if (vdi_size < 0) { | |
1546 | ret = vdi_size; | |
1547 | goto out; | |
1548 | } | |
1549 | max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE); | |
1550 | ||
1551 | for (idx = 0; idx < max_idx; idx++) { | |
1552 | /* | |
1553 | * The created image can be a cloned image, so we need to read | |
1554 | * a data from the source image. | |
1555 | */ | |
1556 | ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE); | |
1557 | if (ret < 0) { | |
1558 | goto out; | |
1559 | } | |
1560 | ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE); | |
1561 | if (ret < 0) { | |
1562 | goto out; | |
1563 | } | |
1564 | } | |
1565 | out: | |
1566 | if (bs) { | |
4f6fd349 | 1567 | bdrv_unref(bs); |
a8e0fdd7 | 1568 | } |
7267c094 | 1569 | g_free(buf); |
a8e0fdd7 MK |
1570 | |
1571 | return ret; | |
1572 | } | |
1573 | ||
b3af018f LY |
1574 | /* |
1575 | * Sheepdog support two kinds of redundancy, full replication and erasure | |
1576 | * coding. | |
1577 | * | |
1578 | * # create a fully replicated vdi with x copies | |
1579 | * -o redundancy=x (1 <= x <= SD_MAX_COPIES) | |
1580 | * | |
1581 | * # create a erasure coded vdi with x data strips and y parity strips | |
1582 | * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP) | |
1583 | */ | |
1584 | static int parse_redundancy(BDRVSheepdogState *s, const char *opt) | |
1585 | { | |
1586 | struct SheepdogInode *inode = &s->inode; | |
1587 | const char *n1, *n2; | |
1588 | long copy, parity; | |
1589 | char p[10]; | |
1590 | ||
1591 | pstrcpy(p, sizeof(p), opt); | |
1592 | n1 = strtok(p, ":"); | |
1593 | n2 = strtok(NULL, ":"); | |
1594 | ||
1595 | if (!n1) { | |
1596 | return -EINVAL; | |
1597 | } | |
1598 | ||
1599 | copy = strtol(n1, NULL, 10); | |
1600 | if (copy > SD_MAX_COPIES || copy < 1) { | |
1601 | return -EINVAL; | |
1602 | } | |
1603 | if (!n2) { | |
1604 | inode->copy_policy = 0; | |
1605 | inode->nr_copies = copy; | |
1606 | return 0; | |
1607 | } | |
1608 | ||
1609 | if (copy != 2 && copy != 4 && copy != 8 && copy != 16) { | |
1610 | return -EINVAL; | |
1611 | } | |
1612 | ||
1613 | parity = strtol(n2, NULL, 10); | |
1614 | if (parity >= SD_EC_MAX_STRIP || parity < 1) { | |
1615 | return -EINVAL; | |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * 4 bits for parity and 4 bits for data. | |
1620 | * We have to compress upper data bits because it can't represent 16 | |
1621 | */ | |
1622 | inode->copy_policy = ((copy / 2) << 4) + parity; | |
1623 | inode->nr_copies = copy + parity; | |
1624 | ||
1625 | return 0; | |
1626 | } | |
1627 | ||
d5124c00 HR |
1628 | static int sd_create(const char *filename, QEMUOptionParameter *options, |
1629 | Error **errp) | |
33b1db1c | 1630 | { |
b6fc8245 | 1631 | int ret = 0; |
c31d482f | 1632 | uint32_t vid = 0; |
33b1db1c | 1633 | char *backing_file = NULL; |
b6fc8245 | 1634 | BDRVSheepdogState *s; |
c31d482f | 1635 | char tag[SD_MAX_VDI_TAG_LEN]; |
b4447363 | 1636 | uint32_t snapid; |
2f536801 | 1637 | bool prealloc = false; |
34b5d2c6 | 1638 | Error *local_err = NULL; |
33b1db1c | 1639 | |
b6fc8245 MK |
1640 | s = g_malloc0(sizeof(BDRVSheepdogState)); |
1641 | ||
b4447363 | 1642 | memset(tag, 0, sizeof(tag)); |
5d6768e3 | 1643 | if (strstr(filename, "://")) { |
c31d482f | 1644 | ret = sd_parse_uri(s, filename, s->name, &snapid, tag); |
5d6768e3 | 1645 | } else { |
c31d482f | 1646 | ret = parse_vdiname(s, filename, s->name, &snapid, tag); |
5d6768e3 MK |
1647 | } |
1648 | if (ret < 0) { | |
b6fc8245 | 1649 | goto out; |
b4447363 MK |
1650 | } |
1651 | ||
33b1db1c MK |
1652 | while (options && options->name) { |
1653 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
c31d482f | 1654 | s->inode.vdi_size = options->value.n; |
33b1db1c MK |
1655 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
1656 | backing_file = options->value.s; | |
a8e0fdd7 MK |
1657 | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { |
1658 | if (!options->value.s || !strcmp(options->value.s, "off")) { | |
2f536801 | 1659 | prealloc = false; |
a8e0fdd7 | 1660 | } else if (!strcmp(options->value.s, "full")) { |
2f536801 | 1661 | prealloc = true; |
a8e0fdd7 MK |
1662 | } else { |
1663 | error_report("Invalid preallocation mode: '%s'", | |
1664 | options->value.s); | |
b6fc8245 MK |
1665 | ret = -EINVAL; |
1666 | goto out; | |
a8e0fdd7 | 1667 | } |
b3af018f | 1668 | } else if (!strcmp(options->name, BLOCK_OPT_REDUNDANCY)) { |
a3120dee LY |
1669 | if (options->value.s) { |
1670 | ret = parse_redundancy(s, options->value.s); | |
1671 | if (ret < 0) { | |
1672 | goto out; | |
1673 | } | |
b3af018f | 1674 | } |
33b1db1c MK |
1675 | } |
1676 | options++; | |
1677 | } | |
1678 | ||
c31d482f | 1679 | if (s->inode.vdi_size > SD_MAX_VDI_SIZE) { |
6daf194d | 1680 | error_report("too big image size"); |
b6fc8245 MK |
1681 | ret = -EINVAL; |
1682 | goto out; | |
33b1db1c MK |
1683 | } |
1684 | ||
1685 | if (backing_file) { | |
1686 | BlockDriverState *bs; | |
1687 | BDRVSheepdogState *s; | |
1688 | BlockDriver *drv; | |
1689 | ||
1690 | /* Currently, only Sheepdog backing image is supported. */ | |
98289620 | 1691 | drv = bdrv_find_protocol(backing_file, true); |
33b1db1c | 1692 | if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { |
6daf194d | 1693 | error_report("backing_file must be a sheepdog image"); |
b6fc8245 MK |
1694 | ret = -EINVAL; |
1695 | goto out; | |
33b1db1c MK |
1696 | } |
1697 | ||
34b5d2c6 | 1698 | ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err); |
cb595887 | 1699 | if (ret < 0) { |
34b5d2c6 HR |
1700 | qerror_report_err(local_err); |
1701 | error_free(local_err); | |
b6fc8245 | 1702 | goto out; |
cb595887 | 1703 | } |
33b1db1c MK |
1704 | |
1705 | s = bs->opaque; | |
1706 | ||
1707 | if (!is_snapshot(&s->inode)) { | |
6daf194d | 1708 | error_report("cannot clone from a non snapshot vdi"); |
4f6fd349 | 1709 | bdrv_unref(bs); |
b6fc8245 MK |
1710 | ret = -EINVAL; |
1711 | goto out; | |
33b1db1c MK |
1712 | } |
1713 | ||
4f6fd349 | 1714 | bdrv_unref(bs); |
33b1db1c MK |
1715 | } |
1716 | ||
c31d482f | 1717 | ret = do_sd_create(s, &vid, 0); |
a8e0fdd7 | 1718 | if (!prealloc || ret) { |
b6fc8245 | 1719 | goto out; |
a8e0fdd7 MK |
1720 | } |
1721 | ||
b6fc8245 MK |
1722 | ret = sd_prealloc(filename); |
1723 | out: | |
1724 | g_free(s); | |
1725 | return ret; | |
33b1db1c MK |
1726 | } |
1727 | ||
1728 | static void sd_close(BlockDriverState *bs) | |
1729 | { | |
1730 | BDRVSheepdogState *s = bs->opaque; | |
1731 | SheepdogVdiReq hdr; | |
1732 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1733 | unsigned int wlen, rlen = 0; | |
1734 | int fd, ret; | |
1735 | ||
2440a2c3 | 1736 | DPRINTF("%s\n", s->name); |
33b1db1c | 1737 | |
25af257d | 1738 | fd = connect_to_sdog(s); |
33b1db1c MK |
1739 | if (fd < 0) { |
1740 | return; | |
1741 | } | |
1742 | ||
1743 | memset(&hdr, 0, sizeof(hdr)); | |
1744 | ||
1745 | hdr.opcode = SD_OP_RELEASE_VDI; | |
6f74c260 | 1746 | hdr.vdi_id = s->inode.vdi_id; |
33b1db1c MK |
1747 | wlen = strlen(s->name) + 1; |
1748 | hdr.data_length = wlen; | |
1749 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1750 | ||
1751 | ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen); | |
1752 | ||
1753 | closesocket(fd); | |
1754 | ||
1755 | if (!ret && rsp->result != SD_RES_SUCCESS && | |
1756 | rsp->result != SD_RES_VDI_NOT_LOCKED) { | |
6daf194d | 1757 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
33b1db1c MK |
1758 | } |
1759 | ||
f2e5dca4 | 1760 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); |
33b1db1c | 1761 | closesocket(s->fd); |
25af257d | 1762 | g_free(s->host_spec); |
33b1db1c MK |
1763 | } |
1764 | ||
1765 | static int64_t sd_getlength(BlockDriverState *bs) | |
1766 | { | |
1767 | BDRVSheepdogState *s = bs->opaque; | |
1768 | ||
1769 | return s->inode.vdi_size; | |
1770 | } | |
1771 | ||
1772 | static int sd_truncate(BlockDriverState *bs, int64_t offset) | |
1773 | { | |
1774 | BDRVSheepdogState *s = bs->opaque; | |
1775 | int ret, fd; | |
1776 | unsigned int datalen; | |
1777 | ||
1778 | if (offset < s->inode.vdi_size) { | |
6daf194d | 1779 | error_report("shrinking is not supported"); |
33b1db1c MK |
1780 | return -EINVAL; |
1781 | } else if (offset > SD_MAX_VDI_SIZE) { | |
6daf194d | 1782 | error_report("too big image size"); |
33b1db1c MK |
1783 | return -EINVAL; |
1784 | } | |
1785 | ||
25af257d | 1786 | fd = connect_to_sdog(s); |
33b1db1c | 1787 | if (fd < 0) { |
cb595887 | 1788 | return fd; |
33b1db1c MK |
1789 | } |
1790 | ||
1791 | /* we don't need to update entire object */ | |
1792 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
1793 | s->inode.vdi_size = offset; | |
1794 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
0e7106d8 | 1795 | s->inode.nr_copies, datalen, 0, false, s->cache_flags); |
33b1db1c MK |
1796 | close(fd); |
1797 | ||
1798 | if (ret < 0) { | |
6daf194d | 1799 | error_report("failed to update an inode."); |
33b1db1c MK |
1800 | } |
1801 | ||
cb595887 | 1802 | return ret; |
33b1db1c MK |
1803 | } |
1804 | ||
1805 | /* | |
1806 | * This function is called after writing data objects. If we need to | |
1807 | * update metadata, this sends a write request to the vdi object. | |
2df46246 | 1808 | * Otherwise, this switches back to sd_co_readv/writev. |
33b1db1c | 1809 | */ |
d8716b41 | 1810 | static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) |
33b1db1c | 1811 | { |
33b1db1c MK |
1812 | BDRVSheepdogState *s = acb->common.bs->opaque; |
1813 | struct iovec iov; | |
1814 | AIOReq *aio_req; | |
1815 | uint32_t offset, data_len, mn, mx; | |
1816 | ||
1817 | mn = s->min_dirty_data_idx; | |
1818 | mx = s->max_dirty_data_idx; | |
1819 | if (mn <= mx) { | |
1820 | /* we need to update the vdi object. */ | |
1821 | offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + | |
1822 | mn * sizeof(s->inode.data_vdi_id[0]); | |
1823 | data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); | |
1824 | ||
1825 | s->min_dirty_data_idx = UINT32_MAX; | |
1826 | s->max_dirty_data_idx = 0; | |
1827 | ||
1828 | iov.iov_base = &s->inode; | |
1829 | iov.iov_len = sizeof(s->inode); | |
1830 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), | |
1831 | data_len, offset, 0, 0, offset); | |
c292ee6a | 1832 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
a37dcdf9 | 1833 | add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA); |
33b1db1c MK |
1834 | |
1835 | acb->aio_done_func = sd_finish_aiocb; | |
1836 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
1837 | return; | |
1838 | } | |
a37dcdf9 | 1839 | |
33b1db1c MK |
1840 | sd_finish_aiocb(acb); |
1841 | } | |
1842 | ||
859e5553 LY |
1843 | /* Delete current working VDI on the snapshot chain */ |
1844 | static bool sd_delete(BDRVSheepdogState *s) | |
1845 | { | |
1846 | unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0; | |
1847 | SheepdogVdiReq hdr = { | |
1848 | .opcode = SD_OP_DEL_VDI, | |
1849 | .vdi_id = s->inode.vdi_id, | |
1850 | .data_length = wlen, | |
1851 | .flags = SD_FLAG_CMD_WRITE, | |
1852 | }; | |
1853 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1854 | int fd, ret; | |
1855 | ||
1856 | fd = connect_to_sdog(s); | |
1857 | if (fd < 0) { | |
1858 | return false; | |
1859 | } | |
1860 | ||
1861 | ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen); | |
1862 | closesocket(fd); | |
1863 | if (ret) { | |
1864 | return false; | |
1865 | } | |
1866 | switch (rsp->result) { | |
1867 | case SD_RES_NO_VDI: | |
1868 | error_report("%s was already deleted", s->name); | |
1869 | /* fall through */ | |
1870 | case SD_RES_SUCCESS: | |
1871 | break; | |
1872 | default: | |
1873 | error_report("%s, %s", sd_strerror(rsp->result), s->name); | |
1874 | return false; | |
1875 | } | |
1876 | ||
1877 | return true; | |
1878 | } | |
1879 | ||
33b1db1c MK |
1880 | /* |
1881 | * Create a writable VDI from a snapshot | |
1882 | */ | |
1883 | static int sd_create_branch(BDRVSheepdogState *s) | |
1884 | { | |
1885 | int ret, fd; | |
1886 | uint32_t vid; | |
1887 | char *buf; | |
859e5553 | 1888 | bool deleted; |
33b1db1c | 1889 | |
2440a2c3 | 1890 | DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id); |
33b1db1c | 1891 | |
7267c094 | 1892 | buf = g_malloc(SD_INODE_SIZE); |
33b1db1c | 1893 | |
859e5553 LY |
1894 | /* |
1895 | * Even If deletion fails, we will just create extra snapshot based on | |
1896 | * the workding VDI which was supposed to be deleted. So no need to | |
1897 | * false bail out. | |
1898 | */ | |
1899 | deleted = sd_delete(s); | |
c31d482f | 1900 | ret = do_sd_create(s, &vid, !deleted); |
33b1db1c MK |
1901 | if (ret) { |
1902 | goto out; | |
1903 | } | |
1904 | ||
2440a2c3 | 1905 | DPRINTF("%" PRIx32 " is created.\n", vid); |
33b1db1c | 1906 | |
25af257d | 1907 | fd = connect_to_sdog(s); |
33b1db1c | 1908 | if (fd < 0) { |
cb595887 | 1909 | ret = fd; |
33b1db1c MK |
1910 | goto out; |
1911 | } | |
1912 | ||
1913 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies, | |
0e7106d8 | 1914 | SD_INODE_SIZE, 0, s->cache_flags); |
33b1db1c MK |
1915 | |
1916 | closesocket(fd); | |
1917 | ||
1918 | if (ret < 0) { | |
1919 | goto out; | |
1920 | } | |
1921 | ||
1922 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1923 | ||
2f536801 | 1924 | s->is_snapshot = false; |
33b1db1c | 1925 | ret = 0; |
2440a2c3 | 1926 | DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id); |
33b1db1c MK |
1927 | |
1928 | out: | |
7267c094 | 1929 | g_free(buf); |
33b1db1c MK |
1930 | |
1931 | return ret; | |
1932 | } | |
1933 | ||
1934 | /* | |
1935 | * Send I/O requests to the server. | |
1936 | * | |
1937 | * This function sends requests to the server, links the requests to | |
c292ee6a | 1938 | * the inflight_list in BDRVSheepdogState, and exits without |
33b1db1c MK |
1939 | * waiting the response. The responses are received in the |
1940 | * `aio_read_response' function which is called from the main loop as | |
1941 | * a fd handler. | |
2df46246 MK |
1942 | * |
1943 | * Returns 1 when we need to wait a response, 0 when there is no sent | |
1944 | * request and -errno in error cases. | |
33b1db1c | 1945 | */ |
d8716b41 | 1946 | static int coroutine_fn sd_co_rw_vector(void *p) |
33b1db1c MK |
1947 | { |
1948 | SheepdogAIOCB *acb = p; | |
1949 | int ret = 0; | |
e8bfaa2f LY |
1950 | unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE; |
1951 | unsigned long idx = acb->sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE; | |
33b1db1c | 1952 | uint64_t oid; |
e8bfaa2f | 1953 | uint64_t offset = (acb->sector_num * BDRV_SECTOR_SIZE) % SD_DATA_OBJ_SIZE; |
33b1db1c MK |
1954 | BDRVSheepdogState *s = acb->common.bs->opaque; |
1955 | SheepdogInode *inode = &s->inode; | |
1956 | AIOReq *aio_req; | |
1957 | ||
33b1db1c MK |
1958 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) { |
1959 | /* | |
1960 | * In the case we open the snapshot VDI, Sheepdog creates the | |
1961 | * writable VDI when we do a write operation first. | |
1962 | */ | |
1963 | ret = sd_create_branch(s); | |
1964 | if (ret) { | |
1965 | acb->ret = -EIO; | |
1966 | goto out; | |
1967 | } | |
1968 | } | |
1969 | ||
1d732d7d MK |
1970 | /* |
1971 | * Make sure we don't free the aiocb before we are done with all requests. | |
1972 | * This additional reference is dropped at the end of this function. | |
1973 | */ | |
1974 | acb->nr_pending++; | |
1975 | ||
33b1db1c MK |
1976 | while (done != total) { |
1977 | uint8_t flags = 0; | |
1978 | uint64_t old_oid = 0; | |
2f536801 | 1979 | bool create = false; |
33b1db1c MK |
1980 | |
1981 | oid = vid_to_data_oid(inode->data_vdi_id[idx], idx); | |
1982 | ||
1983 | len = MIN(total - done, SD_DATA_OBJ_SIZE - offset); | |
1984 | ||
19db9b90 CH |
1985 | switch (acb->aiocb_type) { |
1986 | case AIOCB_READ_UDATA: | |
1987 | if (!inode->data_vdi_id[idx]) { | |
1988 | qemu_iovec_memset(acb->qiov, done, 0, len); | |
33b1db1c MK |
1989 | goto done; |
1990 | } | |
19db9b90 CH |
1991 | break; |
1992 | case AIOCB_WRITE_UDATA: | |
1993 | if (!inode->data_vdi_id[idx]) { | |
2f536801 | 1994 | create = true; |
19db9b90 CH |
1995 | } else if (!is_data_obj_writable(inode, idx)) { |
1996 | /* Copy-On-Write */ | |
2f536801 | 1997 | create = true; |
19db9b90 CH |
1998 | old_oid = oid; |
1999 | flags = SD_FLAG_CMD_COW; | |
2000 | } | |
2001 | break; | |
cac8f4a6 LY |
2002 | case AIOCB_DISCARD_OBJ: |
2003 | /* | |
2004 | * We discard the object only when the whole object is | |
2005 | * 1) allocated 2) trimmed. Otherwise, simply skip it. | |
2006 | */ | |
2007 | if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) { | |
2008 | goto done; | |
2009 | } | |
2010 | break; | |
19db9b90 CH |
2011 | default: |
2012 | break; | |
33b1db1c MK |
2013 | } |
2014 | ||
2015 | if (create) { | |
2440a2c3 | 2016 | DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n", |
1b6ac998 | 2017 | inode->vdi_id, oid, |
33b1db1c MK |
2018 | vid_to_data_oid(inode->data_vdi_id[idx], idx), idx); |
2019 | oid = vid_to_data_oid(inode->vdi_id, idx); | |
2440a2c3 | 2020 | DPRINTF("new oid %" PRIx64 "\n", oid); |
33b1db1c MK |
2021 | } |
2022 | ||
2023 | aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done); | |
80308d33 | 2024 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
33b1db1c MK |
2025 | |
2026 | if (create) { | |
80308d33 MK |
2027 | if (check_simultaneous_create(s, aio_req)) { |
2028 | goto done; | |
33b1db1c MK |
2029 | } |
2030 | } | |
2031 | ||
a37dcdf9 MK |
2032 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create, |
2033 | acb->aiocb_type); | |
33b1db1c MK |
2034 | done: |
2035 | offset = 0; | |
2036 | idx++; | |
2037 | done += len; | |
2038 | } | |
2039 | out: | |
1d732d7d | 2040 | if (!--acb->nr_pending) { |
2df46246 | 2041 | return acb->ret; |
33b1db1c | 2042 | } |
2df46246 | 2043 | return 1; |
33b1db1c MK |
2044 | } |
2045 | ||
a968168c | 2046 | static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, |
2df46246 | 2047 | int nb_sectors, QEMUIOVector *qiov) |
33b1db1c MK |
2048 | { |
2049 | SheepdogAIOCB *acb; | |
2df46246 | 2050 | int ret; |
33b1db1c MK |
2051 | |
2052 | if (bs->growable && sector_num + nb_sectors > bs->total_sectors) { | |
e8bfaa2f | 2053 | ret = sd_truncate(bs, (sector_num + nb_sectors) * BDRV_SECTOR_SIZE); |
cb595887 MK |
2054 | if (ret < 0) { |
2055 | return ret; | |
33b1db1c MK |
2056 | } |
2057 | bs->total_sectors = sector_num + nb_sectors; | |
2058 | } | |
2059 | ||
f700f8e3 | 2060 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
33b1db1c MK |
2061 | acb->aio_done_func = sd_write_done; |
2062 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
2063 | ||
2df46246 MK |
2064 | ret = sd_co_rw_vector(acb); |
2065 | if (ret <= 0) { | |
2066 | qemu_aio_release(acb); | |
2067 | return ret; | |
2068 | } | |
2069 | ||
2070 | qemu_coroutine_yield(); | |
2071 | ||
2072 | return acb->ret; | |
33b1db1c MK |
2073 | } |
2074 | ||
a968168c | 2075 | static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num, |
2df46246 | 2076 | int nb_sectors, QEMUIOVector *qiov) |
33b1db1c MK |
2077 | { |
2078 | SheepdogAIOCB *acb; | |
19db9b90 | 2079 | int ret; |
33b1db1c | 2080 | |
f700f8e3 | 2081 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
33b1db1c MK |
2082 | acb->aiocb_type = AIOCB_READ_UDATA; |
2083 | acb->aio_done_func = sd_finish_aiocb; | |
2084 | ||
2df46246 MK |
2085 | ret = sd_co_rw_vector(acb); |
2086 | if (ret <= 0) { | |
2087 | qemu_aio_release(acb); | |
2088 | return ret; | |
2089 | } | |
2090 | ||
2091 | qemu_coroutine_yield(); | |
2092 | ||
2093 | return acb->ret; | |
33b1db1c MK |
2094 | } |
2095 | ||
47622c44 LY |
2096 | static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) |
2097 | { | |
2098 | BDRVSheepdogState *s = bs->opaque; | |
47783072 LY |
2099 | SheepdogAIOCB *acb; |
2100 | AIOReq *aio_req; | |
47622c44 | 2101 | |
0e7106d8 | 2102 | if (s->cache_flags != SD_FLAG_CMD_CACHE) { |
47622c44 LY |
2103 | return 0; |
2104 | } | |
2105 | ||
f700f8e3 | 2106 | acb = sd_aio_setup(bs, NULL, 0, 0); |
47783072 LY |
2107 | acb->aiocb_type = AIOCB_FLUSH_CACHE; |
2108 | acb->aio_done_func = sd_finish_aiocb; | |
47622c44 | 2109 | |
47783072 LY |
2110 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), |
2111 | 0, 0, 0, 0, 0); | |
2112 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
a37dcdf9 | 2113 | add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type); |
47622c44 | 2114 | |
47783072 LY |
2115 | qemu_coroutine_yield(); |
2116 | return acb->ret; | |
47622c44 LY |
2117 | } |
2118 | ||
33b1db1c MK |
2119 | static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) |
2120 | { | |
2121 | BDRVSheepdogState *s = bs->opaque; | |
2122 | int ret, fd; | |
2123 | uint32_t new_vid; | |
2124 | SheepdogInode *inode; | |
2125 | unsigned int datalen; | |
2126 | ||
2440a2c3 | 2127 | DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " " |
33b1db1c MK |
2128 | "is_snapshot %d\n", sn_info->name, sn_info->id_str, |
2129 | s->name, sn_info->vm_state_size, s->is_snapshot); | |
2130 | ||
2131 | if (s->is_snapshot) { | |
2132 | error_report("You can't create a snapshot of a snapshot VDI, " | |
6daf194d | 2133 | "%s (%" PRIu32 ").", s->name, s->inode.vdi_id); |
33b1db1c MK |
2134 | |
2135 | return -EINVAL; | |
2136 | } | |
2137 | ||
2440a2c3 | 2138 | DPRINTF("%s %s\n", sn_info->name, sn_info->id_str); |
33b1db1c MK |
2139 | |
2140 | s->inode.vm_state_size = sn_info->vm_state_size; | |
2141 | s->inode.vm_clock_nsec = sn_info->vm_clock_nsec; | |
3178e275 JM |
2142 | /* It appears that inode.tag does not require a NUL terminator, |
2143 | * which means this use of strncpy is ok. | |
2144 | */ | |
33b1db1c MK |
2145 | strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag)); |
2146 | /* we don't need to update entire object */ | |
2147 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
2148 | ||
2149 | /* refresh inode. */ | |
25af257d | 2150 | fd = connect_to_sdog(s); |
33b1db1c | 2151 | if (fd < 0) { |
cb595887 | 2152 | ret = fd; |
33b1db1c MK |
2153 | goto cleanup; |
2154 | } | |
2155 | ||
2156 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
0e7106d8 | 2157 | s->inode.nr_copies, datalen, 0, false, s->cache_flags); |
33b1db1c | 2158 | if (ret < 0) { |
6daf194d | 2159 | error_report("failed to write snapshot's inode."); |
33b1db1c MK |
2160 | goto cleanup; |
2161 | } | |
2162 | ||
c31d482f | 2163 | ret = do_sd_create(s, &new_vid, 1); |
33b1db1c | 2164 | if (ret < 0) { |
6daf194d | 2165 | error_report("failed to create inode for snapshot. %s", |
33b1db1c | 2166 | strerror(errno)); |
33b1db1c MK |
2167 | goto cleanup; |
2168 | } | |
2169 | ||
7267c094 | 2170 | inode = (SheepdogInode *)g_malloc(datalen); |
33b1db1c MK |
2171 | |
2172 | ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid), | |
0e7106d8 | 2173 | s->inode.nr_copies, datalen, 0, s->cache_flags); |
33b1db1c MK |
2174 | |
2175 | if (ret < 0) { | |
6daf194d | 2176 | error_report("failed to read new inode info. %s", strerror(errno)); |
33b1db1c MK |
2177 | goto cleanup; |
2178 | } | |
2179 | ||
2180 | memcpy(&s->inode, inode, datalen); | |
2440a2c3 | 2181 | DPRINTF("s->inode: name %s snap_id %x oid %x\n", |
33b1db1c MK |
2182 | s->inode.name, s->inode.snap_id, s->inode.vdi_id); |
2183 | ||
2184 | cleanup: | |
2185 | closesocket(fd); | |
2186 | return ret; | |
2187 | } | |
2188 | ||
859e5553 LY |
2189 | /* |
2190 | * We implement rollback(loadvm) operation to the specified snapshot by | |
2191 | * 1) switch to the snapshot | |
2192 | * 2) rely on sd_create_branch to delete working VDI and | |
2193 | * 3) create a new working VDI based on the speicified snapshot | |
2194 | */ | |
33b1db1c MK |
2195 | static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
2196 | { | |
2197 | BDRVSheepdogState *s = bs->opaque; | |
2198 | BDRVSheepdogState *old_s; | |
9ff53a0e | 2199 | char tag[SD_MAX_VDI_TAG_LEN]; |
33b1db1c | 2200 | uint32_t snapid = 0; |
9ff53a0e | 2201 | int ret = 0; |
33b1db1c | 2202 | |
7267c094 | 2203 | old_s = g_malloc(sizeof(BDRVSheepdogState)); |
33b1db1c MK |
2204 | |
2205 | memcpy(old_s, s, sizeof(BDRVSheepdogState)); | |
2206 | ||
33b1db1c | 2207 | snapid = strtoul(snapshot_id, NULL, 10); |
3178e275 JM |
2208 | if (snapid) { |
2209 | tag[0] = 0; | |
2210 | } else { | |
b579ffb3 | 2211 | pstrcpy(tag, sizeof(tag), snapshot_id); |
33b1db1c MK |
2212 | } |
2213 | ||
9ff53a0e | 2214 | ret = reload_inode(s, snapid, tag); |
33b1db1c | 2215 | if (ret) { |
33b1db1c MK |
2216 | goto out; |
2217 | } | |
2218 | ||
cede621f LY |
2219 | ret = sd_create_branch(s); |
2220 | if (ret) { | |
33b1db1c MK |
2221 | goto out; |
2222 | } | |
2223 | ||
7267c094 | 2224 | g_free(old_s); |
33b1db1c MK |
2225 | |
2226 | return 0; | |
2227 | out: | |
2228 | /* recover bdrv_sd_state */ | |
2229 | memcpy(s, old_s, sizeof(BDRVSheepdogState)); | |
7267c094 | 2230 | g_free(old_s); |
33b1db1c | 2231 | |
6daf194d | 2232 | error_report("failed to open. recover old bdrv_sd_state."); |
33b1db1c MK |
2233 | |
2234 | return ret; | |
2235 | } | |
2236 | ||
a89d89d3 WX |
2237 | static int sd_snapshot_delete(BlockDriverState *bs, |
2238 | const char *snapshot_id, | |
2239 | const char *name, | |
2240 | Error **errp) | |
33b1db1c MK |
2241 | { |
2242 | /* FIXME: Delete specified snapshot id. */ | |
2243 | return 0; | |
2244 | } | |
2245 | ||
33b1db1c MK |
2246 | static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
2247 | { | |
2248 | BDRVSheepdogState *s = bs->opaque; | |
2249 | SheepdogReq req; | |
2250 | int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long); | |
2251 | QEMUSnapshotInfo *sn_tab = NULL; | |
2252 | unsigned wlen, rlen; | |
2253 | int found = 0; | |
2254 | static SheepdogInode inode; | |
2255 | unsigned long *vdi_inuse; | |
2256 | unsigned int start_nr; | |
2257 | uint64_t hval; | |
2258 | uint32_t vid; | |
2259 | ||
7267c094 | 2260 | vdi_inuse = g_malloc(max); |
33b1db1c | 2261 | |
25af257d | 2262 | fd = connect_to_sdog(s); |
33b1db1c | 2263 | if (fd < 0) { |
cb595887 | 2264 | ret = fd; |
33b1db1c MK |
2265 | goto out; |
2266 | } | |
2267 | ||
2268 | rlen = max; | |
2269 | wlen = 0; | |
2270 | ||
2271 | memset(&req, 0, sizeof(req)); | |
2272 | ||
2273 | req.opcode = SD_OP_READ_VDIS; | |
2274 | req.data_length = max; | |
2275 | ||
2276 | ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen); | |
2277 | ||
2278 | closesocket(fd); | |
2279 | if (ret) { | |
2280 | goto out; | |
2281 | } | |
2282 | ||
7267c094 | 2283 | sn_tab = g_malloc0(nr * sizeof(*sn_tab)); |
33b1db1c MK |
2284 | |
2285 | /* calculate a vdi id with hash function */ | |
2286 | hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT); | |
2287 | start_nr = hval & (SD_NR_VDIS - 1); | |
2288 | ||
25af257d | 2289 | fd = connect_to_sdog(s); |
33b1db1c | 2290 | if (fd < 0) { |
cb595887 | 2291 | ret = fd; |
33b1db1c MK |
2292 | goto out; |
2293 | } | |
2294 | ||
2295 | for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) { | |
2296 | if (!test_bit(vid, vdi_inuse)) { | |
2297 | break; | |
2298 | } | |
2299 | ||
2300 | /* we don't need to read entire object */ | |
2301 | ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid), | |
47622c44 | 2302 | 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0, |
0e7106d8 | 2303 | s->cache_flags); |
33b1db1c MK |
2304 | |
2305 | if (ret) { | |
2306 | continue; | |
2307 | } | |
2308 | ||
2309 | if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) { | |
2310 | sn_tab[found].date_sec = inode.snap_ctime >> 32; | |
2311 | sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff; | |
2312 | sn_tab[found].vm_state_size = inode.vm_state_size; | |
2313 | sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec; | |
2314 | ||
2315 | snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u", | |
2316 | inode.snap_id); | |
3178e275 JM |
2317 | pstrcpy(sn_tab[found].name, |
2318 | MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)), | |
2319 | inode.tag); | |
33b1db1c MK |
2320 | found++; |
2321 | } | |
2322 | } | |
2323 | ||
2324 | closesocket(fd); | |
2325 | out: | |
2326 | *psn_tab = sn_tab; | |
2327 | ||
7267c094 | 2328 | g_free(vdi_inuse); |
33b1db1c | 2329 | |
cb595887 MK |
2330 | if (ret < 0) { |
2331 | return ret; | |
2332 | } | |
2333 | ||
33b1db1c MK |
2334 | return found; |
2335 | } | |
2336 | ||
2337 | static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data, | |
2338 | int64_t pos, int size, int load) | |
2339 | { | |
2f536801 MK |
2340 | bool create; |
2341 | int fd, ret = 0, remaining = size; | |
33b1db1c MK |
2342 | unsigned int data_len; |
2343 | uint64_t vmstate_oid; | |
33b1db1c | 2344 | uint64_t offset; |
cede621f LY |
2345 | uint32_t vdi_index; |
2346 | uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id; | |
33b1db1c | 2347 | |
25af257d | 2348 | fd = connect_to_sdog(s); |
33b1db1c | 2349 | if (fd < 0) { |
cb595887 | 2350 | return fd; |
33b1db1c MK |
2351 | } |
2352 | ||
6f3c714e | 2353 | while (remaining) { |
33b1db1c MK |
2354 | vdi_index = pos / SD_DATA_OBJ_SIZE; |
2355 | offset = pos % SD_DATA_OBJ_SIZE; | |
2356 | ||
1f7a48de | 2357 | data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset); |
33b1db1c | 2358 | |
cede621f | 2359 | vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index); |
33b1db1c MK |
2360 | |
2361 | create = (offset == 0); | |
2362 | if (load) { | |
2363 | ret = read_object(fd, (char *)data, vmstate_oid, | |
47622c44 | 2364 | s->inode.nr_copies, data_len, offset, |
0e7106d8 | 2365 | s->cache_flags); |
33b1db1c MK |
2366 | } else { |
2367 | ret = write_object(fd, (char *)data, vmstate_oid, | |
47622c44 | 2368 | s->inode.nr_copies, data_len, offset, create, |
0e7106d8 | 2369 | s->cache_flags); |
33b1db1c MK |
2370 | } |
2371 | ||
2372 | if (ret < 0) { | |
6daf194d | 2373 | error_report("failed to save vmstate %s", strerror(errno)); |
33b1db1c MK |
2374 | goto cleanup; |
2375 | } | |
2376 | ||
2377 | pos += data_len; | |
1f7a48de | 2378 | data += data_len; |
6f3c714e | 2379 | remaining -= data_len; |
33b1db1c | 2380 | } |
6f3c714e | 2381 | ret = size; |
33b1db1c MK |
2382 | cleanup: |
2383 | closesocket(fd); | |
2384 | return ret; | |
2385 | } | |
2386 | ||
cf8074b3 KW |
2387 | static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
2388 | int64_t pos) | |
33b1db1c MK |
2389 | { |
2390 | BDRVSheepdogState *s = bs->opaque; | |
cf8074b3 KW |
2391 | void *buf; |
2392 | int ret; | |
33b1db1c | 2393 | |
cf8074b3 KW |
2394 | buf = qemu_blockalign(bs, qiov->size); |
2395 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); | |
2396 | ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0); | |
2397 | qemu_vfree(buf); | |
2398 | ||
2399 | return ret; | |
33b1db1c MK |
2400 | } |
2401 | ||
2402 | static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data, | |
2403 | int64_t pos, int size) | |
2404 | { | |
2405 | BDRVSheepdogState *s = bs->opaque; | |
2406 | ||
2407 | return do_load_save_vmstate(s, data, pos, size, 1); | |
2408 | } | |
2409 | ||
2410 | ||
cac8f4a6 LY |
2411 | static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num, |
2412 | int nb_sectors) | |
2413 | { | |
2414 | SheepdogAIOCB *acb; | |
2415 | QEMUIOVector dummy; | |
2416 | BDRVSheepdogState *s = bs->opaque; | |
2417 | int ret; | |
2418 | ||
2419 | if (!s->discard_supported) { | |
2420 | return 0; | |
2421 | } | |
2422 | ||
2423 | acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors); | |
2424 | acb->aiocb_type = AIOCB_DISCARD_OBJ; | |
2425 | acb->aio_done_func = sd_finish_aiocb; | |
2426 | ||
2427 | ret = sd_co_rw_vector(acb); | |
2428 | if (ret <= 0) { | |
2429 | qemu_aio_release(acb); | |
2430 | return ret; | |
2431 | } | |
2432 | ||
2433 | qemu_coroutine_yield(); | |
2434 | ||
2435 | return acb->ret; | |
2436 | } | |
2437 | ||
b6b8a333 PB |
2438 | static coroutine_fn int64_t |
2439 | sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
2440 | int *pnum) | |
8d71c631 LY |
2441 | { |
2442 | BDRVSheepdogState *s = bs->opaque; | |
2443 | SheepdogInode *inode = &s->inode; | |
2444 | unsigned long start = sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE, | |
2445 | end = DIV_ROUND_UP((sector_num + nb_sectors) * | |
2446 | BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE); | |
2447 | unsigned long idx; | |
4bc74be9 | 2448 | int64_t ret = BDRV_BLOCK_DATA; |
8d71c631 LY |
2449 | |
2450 | for (idx = start; idx < end; idx++) { | |
2451 | if (inode->data_vdi_id[idx] == 0) { | |
2452 | break; | |
2453 | } | |
2454 | } | |
2455 | if (idx == start) { | |
2456 | /* Get the longest length of unallocated sectors */ | |
2457 | ret = 0; | |
2458 | for (idx = start + 1; idx < end; idx++) { | |
2459 | if (inode->data_vdi_id[idx] != 0) { | |
2460 | break; | |
2461 | } | |
2462 | } | |
2463 | } | |
2464 | ||
2465 | *pnum = (idx - start) * SD_DATA_OBJ_SIZE / BDRV_SECTOR_SIZE; | |
2466 | if (*pnum > nb_sectors) { | |
2467 | *pnum = nb_sectors; | |
2468 | } | |
2469 | return ret; | |
2470 | } | |
2471 | ||
85829722 LY |
2472 | static int64_t sd_get_allocated_file_size(BlockDriverState *bs) |
2473 | { | |
2474 | BDRVSheepdogState *s = bs->opaque; | |
2475 | SheepdogInode *inode = &s->inode; | |
2476 | unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, SD_DATA_OBJ_SIZE); | |
2477 | uint64_t size = 0; | |
2478 | ||
2479 | for (i = 0; i < last; i++) { | |
2480 | if (inode->data_vdi_id[i] == 0) { | |
2481 | continue; | |
2482 | } | |
2483 | size += SD_DATA_OBJ_SIZE; | |
2484 | } | |
2485 | return size; | |
2486 | } | |
2487 | ||
33b1db1c MK |
2488 | static QEMUOptionParameter sd_create_options[] = { |
2489 | { | |
2490 | .name = BLOCK_OPT_SIZE, | |
2491 | .type = OPT_SIZE, | |
2492 | .help = "Virtual disk size" | |
2493 | }, | |
2494 | { | |
2495 | .name = BLOCK_OPT_BACKING_FILE, | |
2496 | .type = OPT_STRING, | |
2497 | .help = "File name of a base image" | |
2498 | }, | |
a8e0fdd7 MK |
2499 | { |
2500 | .name = BLOCK_OPT_PREALLOC, | |
2501 | .type = OPT_STRING, | |
2502 | .help = "Preallocation mode (allowed values: off, full)" | |
2503 | }, | |
b3af018f LY |
2504 | { |
2505 | .name = BLOCK_OPT_REDUNDANCY, | |
2506 | .type = OPT_STRING, | |
2507 | .help = "Redundancy of the image" | |
2508 | }, | |
33b1db1c MK |
2509 | { NULL } |
2510 | }; | |
2511 | ||
5d6768e3 | 2512 | static BlockDriver bdrv_sheepdog = { |
33b1db1c MK |
2513 | .format_name = "sheepdog", |
2514 | .protocol_name = "sheepdog", | |
2515 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2516 | .bdrv_needs_filename = true, |
33b1db1c MK |
2517 | .bdrv_file_open = sd_open, |
2518 | .bdrv_close = sd_close, | |
2519 | .bdrv_create = sd_create, | |
e4f5c1bf | 2520 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
33b1db1c | 2521 | .bdrv_getlength = sd_getlength, |
85829722 | 2522 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
33b1db1c MK |
2523 | .bdrv_truncate = sd_truncate, |
2524 | ||
2df46246 MK |
2525 | .bdrv_co_readv = sd_co_readv, |
2526 | .bdrv_co_writev = sd_co_writev, | |
47622c44 | 2527 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, |
cac8f4a6 | 2528 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2529 | .bdrv_co_get_block_status = sd_co_get_block_status, |
33b1db1c MK |
2530 | |
2531 | .bdrv_snapshot_create = sd_snapshot_create, | |
2532 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2533 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2534 | .bdrv_snapshot_list = sd_snapshot_list, | |
2535 | ||
2536 | .bdrv_save_vmstate = sd_save_vmstate, | |
2537 | .bdrv_load_vmstate = sd_load_vmstate, | |
2538 | ||
2539 | .create_options = sd_create_options, | |
2540 | }; | |
2541 | ||
5d6768e3 MK |
2542 | static BlockDriver bdrv_sheepdog_tcp = { |
2543 | .format_name = "sheepdog", | |
2544 | .protocol_name = "sheepdog+tcp", | |
2545 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2546 | .bdrv_needs_filename = true, |
5d6768e3 MK |
2547 | .bdrv_file_open = sd_open, |
2548 | .bdrv_close = sd_close, | |
2549 | .bdrv_create = sd_create, | |
e4f5c1bf | 2550 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
5d6768e3 | 2551 | .bdrv_getlength = sd_getlength, |
85829722 | 2552 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
5d6768e3 MK |
2553 | .bdrv_truncate = sd_truncate, |
2554 | ||
2555 | .bdrv_co_readv = sd_co_readv, | |
2556 | .bdrv_co_writev = sd_co_writev, | |
2557 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
cac8f4a6 | 2558 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2559 | .bdrv_co_get_block_status = sd_co_get_block_status, |
5d6768e3 MK |
2560 | |
2561 | .bdrv_snapshot_create = sd_snapshot_create, | |
2562 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2563 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2564 | .bdrv_snapshot_list = sd_snapshot_list, | |
2565 | ||
2566 | .bdrv_save_vmstate = sd_save_vmstate, | |
2567 | .bdrv_load_vmstate = sd_load_vmstate, | |
2568 | ||
2569 | .create_options = sd_create_options, | |
2570 | }; | |
2571 | ||
1b8bbb46 MK |
2572 | static BlockDriver bdrv_sheepdog_unix = { |
2573 | .format_name = "sheepdog", | |
2574 | .protocol_name = "sheepdog+unix", | |
2575 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2576 | .bdrv_needs_filename = true, |
1b8bbb46 MK |
2577 | .bdrv_file_open = sd_open, |
2578 | .bdrv_close = sd_close, | |
2579 | .bdrv_create = sd_create, | |
3ac21627 | 2580 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
1b8bbb46 | 2581 | .bdrv_getlength = sd_getlength, |
85829722 | 2582 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
1b8bbb46 MK |
2583 | .bdrv_truncate = sd_truncate, |
2584 | ||
2585 | .bdrv_co_readv = sd_co_readv, | |
2586 | .bdrv_co_writev = sd_co_writev, | |
2587 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
cac8f4a6 | 2588 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2589 | .bdrv_co_get_block_status = sd_co_get_block_status, |
1b8bbb46 MK |
2590 | |
2591 | .bdrv_snapshot_create = sd_snapshot_create, | |
2592 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2593 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2594 | .bdrv_snapshot_list = sd_snapshot_list, | |
2595 | ||
2596 | .bdrv_save_vmstate = sd_save_vmstate, | |
2597 | .bdrv_load_vmstate = sd_load_vmstate, | |
2598 | ||
2599 | .create_options = sd_create_options, | |
2600 | }; | |
2601 | ||
33b1db1c MK |
2602 | static void bdrv_sheepdog_init(void) |
2603 | { | |
2604 | bdrv_register(&bdrv_sheepdog); | |
5d6768e3 | 2605 | bdrv_register(&bdrv_sheepdog_tcp); |
1b8bbb46 | 2606 | bdrv_register(&bdrv_sheepdog_unix); |
33b1db1c MK |
2607 | } |
2608 | block_init(bdrv_sheepdog_init); |