]>
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; | |
9f23fce7 | 164 | uint32_t base_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 | ||
dfb12bf8 | 529 | static int connect_to_sdog(BDRVSheepdogState *s, Error **errp) |
33b1db1c | 530 | { |
25af257d | 531 | int fd; |
33b1db1c | 532 | |
1b8bbb46 | 533 | if (s->is_unix) { |
dfb12bf8 | 534 | fd = unix_connect(s->host_spec, errp); |
1b8bbb46 | 535 | } else { |
dfb12bf8 | 536 | fd = inet_connect(s->host_spec, errp); |
1b8bbb46 | 537 | |
dfb12bf8 | 538 | if (fd >= 0) { |
1b8bbb46 MK |
539 | int ret = socket_set_nodelay(fd); |
540 | if (ret < 0) { | |
541 | error_report("%s", strerror(errno)); | |
542 | } | |
543 | } | |
544 | } | |
33b1db1c | 545 | |
dfb12bf8 | 546 | if (fd >= 0) { |
f9e8cacc | 547 | qemu_set_nonblock(fd); |
33b1db1c MK |
548 | } |
549 | ||
33b1db1c MK |
550 | return fd; |
551 | } | |
552 | ||
e0d93a89 MK |
553 | static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, |
554 | unsigned int *wlen) | |
47622c44 LY |
555 | { |
556 | int ret; | |
557 | ||
558 | ret = qemu_co_send(sockfd, hdr, sizeof(*hdr)); | |
80731d9d | 559 | if (ret != sizeof(*hdr)) { |
47622c44 | 560 | error_report("failed to send a req, %s", strerror(errno)); |
eb092180 | 561 | return ret; |
47622c44 LY |
562 | } |
563 | ||
564 | ret = qemu_co_send(sockfd, data, *wlen); | |
80731d9d | 565 | if (ret != *wlen) { |
47622c44 LY |
566 | error_report("failed to send a req, %s", strerror(errno)); |
567 | } | |
568 | ||
569 | return ret; | |
570 | } | |
e0d93a89 | 571 | |
2dfcca3b MK |
572 | static void restart_co_req(void *opaque) |
573 | { | |
574 | Coroutine *co = opaque; | |
575 | ||
576 | qemu_coroutine_enter(co, NULL); | |
577 | } | |
578 | ||
cddd4ac7 MK |
579 | typedef struct SheepdogReqCo { |
580 | int sockfd; | |
581 | SheepdogReq *hdr; | |
582 | void *data; | |
583 | unsigned int *wlen; | |
584 | unsigned int *rlen; | |
585 | int ret; | |
586 | bool finished; | |
587 | } SheepdogReqCo; | |
588 | ||
589 | static coroutine_fn void do_co_req(void *opaque) | |
47622c44 LY |
590 | { |
591 | int ret; | |
2dfcca3b | 592 | Coroutine *co; |
cddd4ac7 MK |
593 | SheepdogReqCo *srco = opaque; |
594 | int sockfd = srco->sockfd; | |
595 | SheepdogReq *hdr = srco->hdr; | |
596 | void *data = srco->data; | |
597 | unsigned int *wlen = srco->wlen; | |
598 | unsigned int *rlen = srco->rlen; | |
2dfcca3b MK |
599 | |
600 | co = qemu_coroutine_self(); | |
f2e5dca4 | 601 | qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, co); |
47622c44 | 602 | |
47622c44 LY |
603 | ret = send_co_req(sockfd, hdr, data, wlen); |
604 | if (ret < 0) { | |
605 | goto out; | |
606 | } | |
607 | ||
f2e5dca4 | 608 | qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, co); |
2dfcca3b | 609 | |
47622c44 | 610 | ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr)); |
80731d9d | 611 | if (ret != sizeof(*hdr)) { |
47622c44 | 612 | error_report("failed to get a rsp, %s", strerror(errno)); |
cb595887 | 613 | ret = -errno; |
47622c44 LY |
614 | goto out; |
615 | } | |
616 | ||
617 | if (*rlen > hdr->data_length) { | |
618 | *rlen = hdr->data_length; | |
619 | } | |
620 | ||
621 | if (*rlen) { | |
622 | ret = qemu_co_recv(sockfd, data, *rlen); | |
80731d9d | 623 | if (ret != *rlen) { |
47622c44 | 624 | error_report("failed to get the data, %s", strerror(errno)); |
cb595887 | 625 | ret = -errno; |
47622c44 LY |
626 | goto out; |
627 | } | |
628 | } | |
629 | ret = 0; | |
630 | out: | |
ed9ba724 MK |
631 | /* there is at most one request for this sockfd, so it is safe to |
632 | * set each handler to NULL. */ | |
f2e5dca4 | 633 | qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL); |
cddd4ac7 MK |
634 | |
635 | srco->ret = ret; | |
636 | srco->finished = true; | |
637 | } | |
638 | ||
639 | static int do_req(int sockfd, SheepdogReq *hdr, void *data, | |
640 | unsigned int *wlen, unsigned int *rlen) | |
641 | { | |
642 | Coroutine *co; | |
643 | SheepdogReqCo srco = { | |
644 | .sockfd = sockfd, | |
645 | .hdr = hdr, | |
646 | .data = data, | |
647 | .wlen = wlen, | |
648 | .rlen = rlen, | |
649 | .ret = 0, | |
650 | .finished = false, | |
651 | }; | |
652 | ||
653 | if (qemu_in_coroutine()) { | |
654 | do_co_req(&srco); | |
655 | } else { | |
656 | co = qemu_coroutine_create(do_co_req); | |
657 | qemu_coroutine_enter(co, &srco); | |
658 | while (!srco.finished) { | |
659 | qemu_aio_wait(); | |
660 | } | |
661 | } | |
662 | ||
663 | return srco.ret; | |
47622c44 LY |
664 | } |
665 | ||
a37dcdf9 | 666 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
2f536801 | 667 | struct iovec *iov, int niov, bool create, |
33b1db1c | 668 | enum AIOCBState aiocb_type); |
a37dcdf9 | 669 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); |
72e0996c | 670 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag); |
356b4ca2 | 671 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp); |
011603ca | 672 | static void co_write_request(void *opaque); |
7dc1cde0 MK |
673 | |
674 | static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid) | |
675 | { | |
676 | AIOReq *aio_req; | |
677 | ||
678 | QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) { | |
679 | if (aio_req->oid == oid) { | |
680 | return aio_req; | |
681 | } | |
682 | } | |
683 | ||
684 | return NULL; | |
685 | } | |
686 | ||
33b1db1c MK |
687 | /* |
688 | * This function searchs pending requests to the object `oid', and | |
689 | * sends them. | |
690 | */ | |
c292ee6a | 691 | static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid) |
33b1db1c | 692 | { |
7dc1cde0 | 693 | AIOReq *aio_req; |
33b1db1c | 694 | SheepdogAIOCB *acb; |
33b1db1c | 695 | |
7dc1cde0 | 696 | while ((aio_req = find_pending_req(s, oid)) != NULL) { |
33b1db1c | 697 | acb = aio_req->aiocb; |
c292ee6a MK |
698 | /* move aio_req from pending list to inflight one */ |
699 | QLIST_REMOVE(aio_req, aio_siblings); | |
700 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
a37dcdf9 MK |
701 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, false, |
702 | acb->aiocb_type); | |
33b1db1c MK |
703 | } |
704 | } | |
705 | ||
011603ca MK |
706 | static coroutine_fn void reconnect_to_sdog(void *opaque) |
707 | { | |
356b4ca2 | 708 | Error *local_err = NULL; |
011603ca MK |
709 | BDRVSheepdogState *s = opaque; |
710 | AIOReq *aio_req, *next; | |
711 | ||
712 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); | |
713 | close(s->fd); | |
714 | s->fd = -1; | |
715 | ||
716 | /* Wait for outstanding write requests to be completed. */ | |
717 | while (s->co_send != NULL) { | |
718 | co_write_request(opaque); | |
719 | } | |
720 | ||
721 | /* Try to reconnect the sheepdog server every one second. */ | |
722 | while (s->fd < 0) { | |
356b4ca2 | 723 | s->fd = get_sheep_fd(s, &local_err); |
011603ca MK |
724 | if (s->fd < 0) { |
725 | DPRINTF("Wait for connection to be established\n"); | |
356b4ca2 MA |
726 | qerror_report_err(local_err); |
727 | error_free(local_err); | |
011603ca MK |
728 | co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME, |
729 | 1000000000ULL); | |
730 | } | |
731 | }; | |
732 | ||
733 | /* | |
734 | * Now we have to resend all the request in the inflight queue. However, | |
735 | * resend_aioreq() can yield and newly created requests can be added to the | |
736 | * inflight queue before the coroutine is resumed. To avoid mixing them, we | |
737 | * have to move all the inflight requests to the failed queue before | |
738 | * resend_aioreq() is called. | |
739 | */ | |
740 | QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) { | |
741 | QLIST_REMOVE(aio_req, aio_siblings); | |
742 | QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings); | |
743 | } | |
744 | ||
745 | /* Resend all the failed aio requests. */ | |
746 | while (!QLIST_EMPTY(&s->failed_aio_head)) { | |
747 | aio_req = QLIST_FIRST(&s->failed_aio_head); | |
748 | QLIST_REMOVE(aio_req, aio_siblings); | |
749 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
750 | resend_aioreq(s, aio_req); | |
751 | } | |
752 | } | |
753 | ||
33b1db1c MK |
754 | /* |
755 | * Receive responses of the I/O requests. | |
756 | * | |
757 | * This function is registered as a fd handler, and called from the | |
758 | * main loop when s->fd is ready for reading responses. | |
759 | */ | |
d8716b41 | 760 | static void coroutine_fn aio_read_response(void *opaque) |
33b1db1c MK |
761 | { |
762 | SheepdogObjRsp rsp; | |
763 | BDRVSheepdogState *s = opaque; | |
764 | int fd = s->fd; | |
765 | int ret; | |
766 | AIOReq *aio_req = NULL; | |
767 | SheepdogAIOCB *acb; | |
cac8f4a6 | 768 | uint64_t idx; |
33b1db1c | 769 | |
33b1db1c | 770 | /* read a header */ |
8c5135f9 | 771 | ret = qemu_co_recv(fd, &rsp, sizeof(rsp)); |
80731d9d | 772 | if (ret != sizeof(rsp)) { |
6daf194d | 773 | error_report("failed to get the header, %s", strerror(errno)); |
011603ca | 774 | goto err; |
33b1db1c MK |
775 | } |
776 | ||
c292ee6a MK |
777 | /* find the right aio_req from the inflight aio list */ |
778 | QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) { | |
33b1db1c MK |
779 | if (aio_req->id == rsp.id) { |
780 | break; | |
781 | } | |
782 | } | |
783 | if (!aio_req) { | |
6daf194d | 784 | error_report("cannot find aio_req %x", rsp.id); |
011603ca | 785 | goto err; |
33b1db1c MK |
786 | } |
787 | ||
788 | acb = aio_req->aiocb; | |
789 | ||
790 | switch (acb->aiocb_type) { | |
791 | case AIOCB_WRITE_UDATA: | |
6d1acda8 MK |
792 | /* this coroutine context is no longer suitable for co_recv |
793 | * because we may send data to update vdi objects */ | |
794 | s->co_recv = NULL; | |
33b1db1c MK |
795 | if (!is_data_obj(aio_req->oid)) { |
796 | break; | |
797 | } | |
798 | idx = data_oid_to_idx(aio_req->oid); | |
799 | ||
800 | if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) { | |
801 | /* | |
802 | * If the object is newly created one, we need to update | |
803 | * the vdi object (metadata object). min_dirty_data_idx | |
804 | * and max_dirty_data_idx are changed to include updated | |
805 | * index between them. | |
806 | */ | |
bd751f22 LY |
807 | if (rsp.result == SD_RES_SUCCESS) { |
808 | s->inode.data_vdi_id[idx] = s->inode.vdi_id; | |
809 | s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx); | |
810 | s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx); | |
811 | } | |
33b1db1c MK |
812 | /* |
813 | * Some requests may be blocked because simultaneous | |
814 | * create requests are not allowed, so we search the | |
815 | * pending requests here. | |
816 | */ | |
d6b1ef89 | 817 | send_pending_req(s, aio_req->oid); |
33b1db1c MK |
818 | } |
819 | break; | |
820 | case AIOCB_READ_UDATA: | |
2fc8ae1d MT |
821 | ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov, |
822 | aio_req->iov_offset, rsp.data_length); | |
80731d9d | 823 | if (ret != rsp.data_length) { |
6daf194d | 824 | error_report("failed to get the data, %s", strerror(errno)); |
011603ca | 825 | goto err; |
33b1db1c MK |
826 | } |
827 | break; | |
47783072 LY |
828 | case AIOCB_FLUSH_CACHE: |
829 | if (rsp.result == SD_RES_INVALID_PARMS) { | |
2440a2c3 | 830 | DPRINTF("disable cache since the server doesn't support it\n"); |
47783072 LY |
831 | s->cache_flags = SD_FLAG_CMD_DIRECT; |
832 | rsp.result = SD_RES_SUCCESS; | |
833 | } | |
834 | break; | |
cac8f4a6 LY |
835 | case AIOCB_DISCARD_OBJ: |
836 | switch (rsp.result) { | |
837 | case SD_RES_INVALID_PARMS: | |
838 | error_report("sheep(%s) doesn't support discard command", | |
839 | s->host_spec); | |
840 | rsp.result = SD_RES_SUCCESS; | |
841 | s->discard_supported = false; | |
842 | break; | |
843 | case SD_RES_SUCCESS: | |
844 | idx = data_oid_to_idx(aio_req->oid); | |
845 | s->inode.data_vdi_id[idx] = 0; | |
846 | break; | |
847 | default: | |
848 | break; | |
849 | } | |
33b1db1c MK |
850 | } |
851 | ||
13c31de2 MK |
852 | switch (rsp.result) { |
853 | case SD_RES_SUCCESS: | |
854 | break; | |
855 | case SD_RES_READONLY: | |
72e0996c MK |
856 | if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) { |
857 | ret = reload_inode(s, 0, ""); | |
858 | if (ret < 0) { | |
011603ca | 859 | goto err; |
72e0996c MK |
860 | } |
861 | } | |
72e0996c MK |
862 | if (is_data_obj(aio_req->oid)) { |
863 | aio_req->oid = vid_to_data_oid(s->inode.vdi_id, | |
864 | data_oid_to_idx(aio_req->oid)); | |
865 | } else { | |
866 | aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id); | |
867 | } | |
a37dcdf9 MK |
868 | resend_aioreq(s, aio_req); |
869 | goto out; | |
13c31de2 | 870 | default: |
33b1db1c | 871 | acb->ret = -EIO; |
6daf194d | 872 | error_report("%s", sd_strerror(rsp.result)); |
13c31de2 | 873 | break; |
33b1db1c MK |
874 | } |
875 | ||
1d732d7d MK |
876 | free_aio_req(s, aio_req); |
877 | if (!acb->nr_pending) { | |
33b1db1c MK |
878 | /* |
879 | * We've finished all requests which belong to the AIOCB, so | |
2df46246 | 880 | * we can switch back to sd_co_readv/writev now. |
33b1db1c MK |
881 | */ |
882 | acb->aio_done_func(acb); | |
883 | } | |
2df46246 MK |
884 | out: |
885 | s->co_recv = NULL; | |
011603ca MK |
886 | return; |
887 | err: | |
888 | s->co_recv = NULL; | |
889 | reconnect_to_sdog(opaque); | |
2df46246 MK |
890 | } |
891 | ||
892 | static void co_read_response(void *opaque) | |
893 | { | |
894 | BDRVSheepdogState *s = opaque; | |
895 | ||
896 | if (!s->co_recv) { | |
897 | s->co_recv = qemu_coroutine_create(aio_read_response); | |
898 | } | |
899 | ||
900 | qemu_coroutine_enter(s->co_recv, opaque); | |
901 | } | |
902 | ||
903 | static void co_write_request(void *opaque) | |
904 | { | |
905 | BDRVSheepdogState *s = opaque; | |
906 | ||
907 | qemu_coroutine_enter(s->co_send, NULL); | |
33b1db1c MK |
908 | } |
909 | ||
33b1db1c | 910 | /* |
dc6fb73d | 911 | * Return a socket descriptor to read/write objects. |
33b1db1c | 912 | * |
dc6fb73d | 913 | * We cannot use this descriptor for other operations because |
33b1db1c MK |
914 | * the block driver may be on waiting response from the server. |
915 | */ | |
356b4ca2 | 916 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp) |
33b1db1c | 917 | { |
1b8bbb46 | 918 | int fd; |
33b1db1c | 919 | |
356b4ca2 | 920 | fd = connect_to_sdog(s, errp); |
33b1db1c | 921 | if (fd < 0) { |
cb595887 | 922 | return fd; |
33b1db1c MK |
923 | } |
924 | ||
f2e5dca4 | 925 | qemu_aio_set_fd_handler(fd, co_read_response, NULL, s); |
33b1db1c MK |
926 | return fd; |
927 | } | |
928 | ||
5d6768e3 MK |
929 | static int sd_parse_uri(BDRVSheepdogState *s, const char *filename, |
930 | char *vdi, uint32_t *snapid, char *tag) | |
931 | { | |
932 | URI *uri; | |
933 | QueryParams *qp = NULL; | |
934 | int ret = 0; | |
935 | ||
936 | uri = uri_parse(filename); | |
937 | if (!uri) { | |
938 | return -EINVAL; | |
939 | } | |
940 | ||
1b8bbb46 MK |
941 | /* transport */ |
942 | if (!strcmp(uri->scheme, "sheepdog")) { | |
943 | s->is_unix = false; | |
944 | } else if (!strcmp(uri->scheme, "sheepdog+tcp")) { | |
945 | s->is_unix = false; | |
946 | } else if (!strcmp(uri->scheme, "sheepdog+unix")) { | |
947 | s->is_unix = true; | |
948 | } else { | |
949 | ret = -EINVAL; | |
950 | goto out; | |
951 | } | |
952 | ||
5d6768e3 MK |
953 | if (uri->path == NULL || !strcmp(uri->path, "/")) { |
954 | ret = -EINVAL; | |
955 | goto out; | |
956 | } | |
957 | pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1); | |
958 | ||
1b8bbb46 MK |
959 | qp = query_params_parse(uri->query); |
960 | if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) { | |
961 | ret = -EINVAL; | |
962 | goto out; | |
963 | } | |
964 | ||
965 | if (s->is_unix) { | |
966 | /* sheepdog+unix:///vdiname?socket=path */ | |
967 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
968 | ret = -EINVAL; | |
969 | goto out; | |
970 | } | |
971 | s->host_spec = g_strdup(qp->p[0].value); | |
972 | } else { | |
973 | /* sheepdog[+tcp]://[host:port]/vdiname */ | |
974 | s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR, | |
975 | uri->port ?: SD_DEFAULT_PORT); | |
976 | } | |
5d6768e3 MK |
977 | |
978 | /* snapshot tag */ | |
979 | if (uri->fragment) { | |
980 | *snapid = strtoul(uri->fragment, NULL, 10); | |
981 | if (*snapid == 0) { | |
982 | pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment); | |
983 | } | |
984 | } else { | |
985 | *snapid = CURRENT_VDI_ID; /* search current vdi */ | |
986 | } | |
987 | ||
988 | out: | |
989 | if (qp) { | |
990 | query_params_free(qp); | |
991 | } | |
992 | uri_free(uri); | |
993 | return ret; | |
994 | } | |
995 | ||
33b1db1c | 996 | /* |
5d6768e3 | 997 | * Parse a filename (old syntax) |
33b1db1c MK |
998 | * |
999 | * filename must be one of the following formats: | |
1000 | * 1. [vdiname] | |
1001 | * 2. [vdiname]:[snapid] | |
1002 | * 3. [vdiname]:[tag] | |
1003 | * 4. [hostname]:[port]:[vdiname] | |
1004 | * 5. [hostname]:[port]:[vdiname]:[snapid] | |
1005 | * 6. [hostname]:[port]:[vdiname]:[tag] | |
1006 | * | |
1007 | * You can boot from the snapshot images by specifying `snapid` or | |
1008 | * `tag'. | |
1009 | * | |
1010 | * You can run VMs outside the Sheepdog cluster by specifying | |
1011 | * `hostname' and `port' (experimental). | |
1012 | */ | |
1013 | static int parse_vdiname(BDRVSheepdogState *s, const char *filename, | |
1014 | char *vdi, uint32_t *snapid, char *tag) | |
1015 | { | |
5d6768e3 MK |
1016 | char *p, *q, *uri; |
1017 | const char *host_spec, *vdi_spec; | |
1018 | int nr_sep, ret; | |
33b1db1c | 1019 | |
5d6768e3 | 1020 | strstart(filename, "sheepdog:", (const char **)&filename); |
7267c094 | 1021 | p = q = g_strdup(filename); |
33b1db1c MK |
1022 | |
1023 | /* count the number of separators */ | |
1024 | nr_sep = 0; | |
1025 | while (*p) { | |
1026 | if (*p == ':') { | |
1027 | nr_sep++; | |
1028 | } | |
1029 | p++; | |
1030 | } | |
1031 | p = q; | |
1032 | ||
5d6768e3 | 1033 | /* use the first two tokens as host_spec. */ |
33b1db1c | 1034 | if (nr_sep >= 2) { |
5d6768e3 | 1035 | host_spec = p; |
33b1db1c | 1036 | p = strchr(p, ':'); |
5d6768e3 | 1037 | p++; |
33b1db1c MK |
1038 | p = strchr(p, ':'); |
1039 | *p++ = '\0'; | |
1040 | } else { | |
5d6768e3 | 1041 | host_spec = ""; |
33b1db1c MK |
1042 | } |
1043 | ||
5d6768e3 | 1044 | vdi_spec = p; |
33b1db1c | 1045 | |
5d6768e3 | 1046 | p = strchr(vdi_spec, ':'); |
33b1db1c | 1047 | if (p) { |
5d6768e3 | 1048 | *p++ = '#'; |
33b1db1c MK |
1049 | } |
1050 | ||
5d6768e3 | 1051 | uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec); |
33b1db1c | 1052 | |
5d6768e3 MK |
1053 | ret = sd_parse_uri(s, uri, vdi, snapid, tag); |
1054 | ||
1055 | g_free(q); | |
1056 | g_free(uri); | |
1057 | ||
1058 | return ret; | |
33b1db1c MK |
1059 | } |
1060 | ||
982dcbf4 MK |
1061 | static int find_vdi_name(BDRVSheepdogState *s, const char *filename, |
1062 | uint32_t snapid, const char *tag, uint32_t *vid, | |
dc83cd42 | 1063 | bool lock, Error **errp) |
33b1db1c MK |
1064 | { |
1065 | int ret, fd; | |
1066 | SheepdogVdiReq hdr; | |
1067 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1068 | unsigned int wlen, rlen = 0; | |
1069 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; | |
1070 | ||
dc83cd42 | 1071 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1072 | if (fd < 0) { |
cb595887 | 1073 | return fd; |
33b1db1c MK |
1074 | } |
1075 | ||
3178e275 JM |
1076 | /* This pair of strncpy calls ensures that the buffer is zero-filled, |
1077 | * which is desirable since we'll soon be sending those bytes, and | |
1078 | * don't want the send_req to read uninitialized data. | |
1079 | */ | |
33b1db1c MK |
1080 | strncpy(buf, filename, SD_MAX_VDI_LEN); |
1081 | strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN); | |
1082 | ||
1083 | memset(&hdr, 0, sizeof(hdr)); | |
982dcbf4 | 1084 | if (lock) { |
33b1db1c | 1085 | hdr.opcode = SD_OP_LOCK_VDI; |
982dcbf4 MK |
1086 | } else { |
1087 | hdr.opcode = SD_OP_GET_VDI_INFO; | |
33b1db1c MK |
1088 | } |
1089 | wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN; | |
1090 | hdr.proto_ver = SD_PROTO_VER; | |
1091 | hdr.data_length = wlen; | |
1092 | hdr.snapid = snapid; | |
1093 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1094 | ||
1095 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1096 | if (ret) { | |
dc83cd42 | 1097 | error_setg_errno(errp, -ret, "cannot get vdi info"); |
33b1db1c MK |
1098 | goto out; |
1099 | } | |
1100 | ||
1101 | if (rsp->result != SD_RES_SUCCESS) { | |
dc83cd42 MA |
1102 | error_setg(errp, "cannot get vdi info, %s, %s %" PRIu32 " %s", |
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 | { | |
dfb12bf8 | 1266 | Error *local_err = NULL; |
9ff53a0e MK |
1267 | SheepdogInode *inode; |
1268 | int ret = 0, fd; | |
1269 | uint32_t vid = 0; | |
1270 | ||
dfb12bf8 | 1271 | fd = connect_to_sdog(s, &local_err); |
9ff53a0e | 1272 | if (fd < 0) { |
dfb12bf8 MA |
1273 | qerror_report_err(local_err); |
1274 | error_free(local_err); | |
9ff53a0e MK |
1275 | return -EIO; |
1276 | } | |
1277 | ||
1278 | inode = g_malloc(sizeof(s->inode)); | |
1279 | ||
dc83cd42 | 1280 | ret = find_vdi_name(s, s->name, snapid, tag, &vid, false, &local_err); |
9ff53a0e | 1281 | if (ret) { |
dc83cd42 MA |
1282 | qerror_report_err(local_err); |
1283 | error_free(local_err); | |
9ff53a0e MK |
1284 | goto out; |
1285 | } | |
1286 | ||
1287 | ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid), | |
1288 | s->inode.nr_copies, sizeof(*inode), 0, s->cache_flags); | |
1289 | if (ret < 0) { | |
1290 | goto out; | |
1291 | } | |
1292 | ||
1293 | if (inode->vdi_id != s->inode.vdi_id) { | |
1294 | memcpy(&s->inode, inode, sizeof(s->inode)); | |
1295 | } | |
1296 | ||
1297 | out: | |
1298 | g_free(inode); | |
1299 | closesocket(fd); | |
1300 | ||
1301 | return ret; | |
1302 | } | |
1303 | ||
80308d33 MK |
1304 | /* Return true if the specified request is linked to the pending list. */ |
1305 | static bool check_simultaneous_create(BDRVSheepdogState *s, AIOReq *aio_req) | |
1306 | { | |
1307 | AIOReq *areq; | |
1308 | QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) { | |
1309 | if (areq != aio_req && areq->oid == aio_req->oid) { | |
1310 | /* | |
1311 | * Sheepdog cannot handle simultaneous create requests to the same | |
1312 | * object, so we cannot send the request until the previous request | |
1313 | * finishes. | |
1314 | */ | |
1315 | DPRINTF("simultaneous create to %" PRIx64 "\n", aio_req->oid); | |
1316 | aio_req->flags = 0; | |
1317 | aio_req->base_oid = 0; | |
1318 | QLIST_REMOVE(aio_req, aio_siblings); | |
1319 | QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req, aio_siblings); | |
1320 | return true; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | return false; | |
1325 | } | |
1326 | ||
a37dcdf9 | 1327 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) |
13c31de2 MK |
1328 | { |
1329 | SheepdogAIOCB *acb = aio_req->aiocb; | |
1330 | bool create = false; | |
13c31de2 MK |
1331 | |
1332 | /* check whether this request becomes a CoW one */ | |
2412aec7 | 1333 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) { |
13c31de2 | 1334 | int idx = data_oid_to_idx(aio_req->oid); |
13c31de2 | 1335 | |
13c31de2 MK |
1336 | if (is_data_obj_writable(&s->inode, idx)) { |
1337 | goto out; | |
1338 | } | |
1339 | ||
80308d33 MK |
1340 | if (check_simultaneous_create(s, aio_req)) { |
1341 | return; | |
13c31de2 MK |
1342 | } |
1343 | ||
80308d33 MK |
1344 | if (s->inode.data_vdi_id[idx]) { |
1345 | aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx); | |
1346 | aio_req->flags |= SD_FLAG_CMD_COW; | |
1347 | } | |
13c31de2 MK |
1348 | create = true; |
1349 | } | |
1350 | out: | |
2412aec7 | 1351 | if (is_data_obj(aio_req->oid)) { |
a37dcdf9 MK |
1352 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create, |
1353 | acb->aiocb_type); | |
2412aec7 MK |
1354 | } else { |
1355 | struct iovec iov; | |
1356 | iov.iov_base = &s->inode; | |
1357 | iov.iov_len = sizeof(s->inode); | |
a37dcdf9 | 1358 | add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA); |
2412aec7 | 1359 | } |
13c31de2 MK |
1360 | } |
1361 | ||
c8c96350 KW |
1362 | /* TODO Convert to fine grained options */ |
1363 | static QemuOptsList runtime_opts = { | |
1364 | .name = "sheepdog", | |
1365 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1366 | .desc = { | |
1367 | { | |
1368 | .name = "filename", | |
1369 | .type = QEMU_OPT_STRING, | |
1370 | .help = "URL to the sheepdog image", | |
1371 | }, | |
1372 | { /* end of list */ } | |
1373 | }, | |
1374 | }; | |
1375 | ||
015a1036 HR |
1376 | static int sd_open(BlockDriverState *bs, QDict *options, int flags, |
1377 | Error **errp) | |
33b1db1c MK |
1378 | { |
1379 | int ret, fd; | |
1380 | uint32_t vid = 0; | |
1381 | BDRVSheepdogState *s = bs->opaque; | |
1382 | char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; | |
1383 | uint32_t snapid; | |
1384 | char *buf = NULL; | |
c8c96350 KW |
1385 | QemuOpts *opts; |
1386 | Error *local_err = NULL; | |
1387 | const char *filename; | |
1388 | ||
011603ca MK |
1389 | s->bs = bs; |
1390 | ||
87ea75d5 | 1391 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
c8c96350 | 1392 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 1393 | if (local_err) { |
e67c3993 | 1394 | error_propagate(errp, local_err); |
c8c96350 KW |
1395 | ret = -EINVAL; |
1396 | goto out; | |
1397 | } | |
1398 | ||
1399 | filename = qemu_opt_get(opts, "filename"); | |
33b1db1c | 1400 | |
c292ee6a MK |
1401 | QLIST_INIT(&s->inflight_aio_head); |
1402 | QLIST_INIT(&s->pending_aio_head); | |
011603ca | 1403 | QLIST_INIT(&s->failed_aio_head); |
33b1db1c MK |
1404 | s->fd = -1; |
1405 | ||
1406 | memset(vdi, 0, sizeof(vdi)); | |
1407 | memset(tag, 0, sizeof(tag)); | |
5d6768e3 MK |
1408 | |
1409 | if (strstr(filename, "://")) { | |
1410 | ret = sd_parse_uri(s, filename, vdi, &snapid, tag); | |
1411 | } else { | |
1412 | ret = parse_vdiname(s, filename, vdi, &snapid, tag); | |
1413 | } | |
1414 | if (ret < 0) { | |
33b1db1c MK |
1415 | goto out; |
1416 | } | |
e67c3993 | 1417 | s->fd = get_sheep_fd(s, errp); |
33b1db1c | 1418 | if (s->fd < 0) { |
cb595887 | 1419 | ret = s->fd; |
33b1db1c MK |
1420 | goto out; |
1421 | } | |
1422 | ||
e67c3993 | 1423 | ret = find_vdi_name(s, vdi, snapid, tag, &vid, true, errp); |
33b1db1c MK |
1424 | if (ret) { |
1425 | goto out; | |
1426 | } | |
1427 | ||
0e7106d8 LY |
1428 | /* |
1429 | * QEMU block layer emulates writethrough cache as 'writeback + flush', so | |
1430 | * we always set SD_FLAG_CMD_CACHE (writeback cache) as default. | |
1431 | */ | |
1432 | s->cache_flags = SD_FLAG_CMD_CACHE; | |
1433 | if (flags & BDRV_O_NOCACHE) { | |
1434 | s->cache_flags = SD_FLAG_CMD_DIRECT; | |
1435 | } | |
cac8f4a6 | 1436 | s->discard_supported = true; |
0e7106d8 | 1437 | |
622b6057 | 1438 | if (snapid || tag[0] != '\0') { |
2440a2c3 | 1439 | DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid); |
2f536801 | 1440 | s->is_snapshot = true; |
33b1db1c MK |
1441 | } |
1442 | ||
e67c3993 | 1443 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1444 | if (fd < 0) { |
cb595887 | 1445 | ret = fd; |
33b1db1c MK |
1446 | goto out; |
1447 | } | |
1448 | ||
7267c094 | 1449 | buf = g_malloc(SD_INODE_SIZE); |
47622c44 | 1450 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0, |
0e7106d8 | 1451 | s->cache_flags); |
33b1db1c MK |
1452 | |
1453 | closesocket(fd); | |
1454 | ||
1455 | if (ret) { | |
1456 | goto out; | |
1457 | } | |
1458 | ||
1459 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1460 | s->min_dirty_data_idx = UINT32_MAX; | |
1461 | s->max_dirty_data_idx = 0; | |
1462 | ||
e8bfaa2f | 1463 | bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE; |
3178e275 | 1464 | pstrcpy(s->name, sizeof(s->name), vdi); |
2df46246 | 1465 | qemu_co_mutex_init(&s->lock); |
c8c96350 | 1466 | qemu_opts_del(opts); |
7267c094 | 1467 | g_free(buf); |
33b1db1c MK |
1468 | return 0; |
1469 | out: | |
f2e5dca4 | 1470 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); |
33b1db1c MK |
1471 | if (s->fd >= 0) { |
1472 | closesocket(s->fd); | |
1473 | } | |
c8c96350 | 1474 | qemu_opts_del(opts); |
7267c094 | 1475 | g_free(buf); |
cb595887 | 1476 | return ret; |
33b1db1c MK |
1477 | } |
1478 | ||
7d2d3e74 MA |
1479 | static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot, |
1480 | Error **errp) | |
33b1db1c MK |
1481 | { |
1482 | SheepdogVdiReq hdr; | |
1483 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1484 | int fd, ret; | |
1485 | unsigned int wlen, rlen = 0; | |
1486 | char buf[SD_MAX_VDI_LEN]; | |
1487 | ||
7d2d3e74 | 1488 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1489 | if (fd < 0) { |
cb595887 | 1490 | return fd; |
33b1db1c MK |
1491 | } |
1492 | ||
3178e275 JM |
1493 | /* FIXME: would it be better to fail (e.g., return -EIO) when filename |
1494 | * does not fit in buf? For now, just truncate and avoid buffer overrun. | |
1495 | */ | |
33b1db1c | 1496 | memset(buf, 0, sizeof(buf)); |
c31d482f | 1497 | pstrcpy(buf, sizeof(buf), s->name); |
33b1db1c MK |
1498 | |
1499 | memset(&hdr, 0, sizeof(hdr)); | |
1500 | hdr.opcode = SD_OP_NEW_VDI; | |
9f23fce7 | 1501 | hdr.base_vdi_id = s->inode.vdi_id; |
33b1db1c MK |
1502 | |
1503 | wlen = SD_MAX_VDI_LEN; | |
1504 | ||
1505 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1506 | hdr.snapid = snapshot; | |
1507 | ||
1508 | hdr.data_length = wlen; | |
c31d482f LY |
1509 | hdr.vdi_size = s->inode.vdi_size; |
1510 | hdr.copy_policy = s->inode.copy_policy; | |
b3af018f | 1511 | hdr.copies = s->inode.nr_copies; |
33b1db1c MK |
1512 | |
1513 | ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen); | |
1514 | ||
1515 | closesocket(fd); | |
1516 | ||
1517 | if (ret) { | |
7d2d3e74 | 1518 | error_setg_errno(errp, -ret, "create failed"); |
cb595887 | 1519 | return ret; |
33b1db1c MK |
1520 | } |
1521 | ||
1522 | if (rsp->result != SD_RES_SUCCESS) { | |
7d2d3e74 | 1523 | error_setg(errp, "%s, %s", sd_strerror(rsp->result), s->inode.name); |
33b1db1c MK |
1524 | return -EIO; |
1525 | } | |
1526 | ||
1527 | if (vdi_id) { | |
1528 | *vdi_id = rsp->vdi_id; | |
1529 | } | |
1530 | ||
1531 | return 0; | |
1532 | } | |
1533 | ||
318df29e | 1534 | static int sd_prealloc(const char *filename, Error **errp) |
a8e0fdd7 MK |
1535 | { |
1536 | BlockDriverState *bs = NULL; | |
1537 | uint32_t idx, max_idx; | |
1538 | int64_t vdi_size; | |
7267c094 | 1539 | void *buf = g_malloc0(SD_DATA_OBJ_SIZE); |
a8e0fdd7 MK |
1540 | int ret; |
1541 | ||
2e40134b | 1542 | ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, |
318df29e | 1543 | NULL, errp); |
a8e0fdd7 | 1544 | if (ret < 0) { |
318df29e | 1545 | goto out_with_err_set; |
a8e0fdd7 MK |
1546 | } |
1547 | ||
1548 | vdi_size = bdrv_getlength(bs); | |
1549 | if (vdi_size < 0) { | |
1550 | ret = vdi_size; | |
1551 | goto out; | |
1552 | } | |
1553 | max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE); | |
1554 | ||
1555 | for (idx = 0; idx < max_idx; idx++) { | |
1556 | /* | |
1557 | * The created image can be a cloned image, so we need to read | |
1558 | * a data from the source image. | |
1559 | */ | |
1560 | ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE); | |
1561 | if (ret < 0) { | |
1562 | goto out; | |
1563 | } | |
1564 | ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE); | |
1565 | if (ret < 0) { | |
1566 | goto out; | |
1567 | } | |
1568 | } | |
318df29e | 1569 | |
a8e0fdd7 | 1570 | out: |
318df29e MA |
1571 | if (ret < 0) { |
1572 | error_setg_errno(errp, -ret, "Can't pre-allocate"); | |
1573 | } | |
1574 | out_with_err_set: | |
a8e0fdd7 | 1575 | if (bs) { |
4f6fd349 | 1576 | bdrv_unref(bs); |
a8e0fdd7 | 1577 | } |
7267c094 | 1578 | g_free(buf); |
a8e0fdd7 MK |
1579 | |
1580 | return ret; | |
1581 | } | |
1582 | ||
b3af018f LY |
1583 | /* |
1584 | * Sheepdog support two kinds of redundancy, full replication and erasure | |
1585 | * coding. | |
1586 | * | |
1587 | * # create a fully replicated vdi with x copies | |
1588 | * -o redundancy=x (1 <= x <= SD_MAX_COPIES) | |
1589 | * | |
1590 | * # create a erasure coded vdi with x data strips and y parity strips | |
1591 | * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP) | |
1592 | */ | |
1593 | static int parse_redundancy(BDRVSheepdogState *s, const char *opt) | |
1594 | { | |
1595 | struct SheepdogInode *inode = &s->inode; | |
1596 | const char *n1, *n2; | |
1597 | long copy, parity; | |
1598 | char p[10]; | |
1599 | ||
1600 | pstrcpy(p, sizeof(p), opt); | |
1601 | n1 = strtok(p, ":"); | |
1602 | n2 = strtok(NULL, ":"); | |
1603 | ||
1604 | if (!n1) { | |
1605 | return -EINVAL; | |
1606 | } | |
1607 | ||
1608 | copy = strtol(n1, NULL, 10); | |
1609 | if (copy > SD_MAX_COPIES || copy < 1) { | |
1610 | return -EINVAL; | |
1611 | } | |
1612 | if (!n2) { | |
1613 | inode->copy_policy = 0; | |
1614 | inode->nr_copies = copy; | |
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | if (copy != 2 && copy != 4 && copy != 8 && copy != 16) { | |
1619 | return -EINVAL; | |
1620 | } | |
1621 | ||
1622 | parity = strtol(n2, NULL, 10); | |
1623 | if (parity >= SD_EC_MAX_STRIP || parity < 1) { | |
1624 | return -EINVAL; | |
1625 | } | |
1626 | ||
1627 | /* | |
1628 | * 4 bits for parity and 4 bits for data. | |
1629 | * We have to compress upper data bits because it can't represent 16 | |
1630 | */ | |
1631 | inode->copy_policy = ((copy / 2) << 4) + parity; | |
1632 | inode->nr_copies = copy + parity; | |
1633 | ||
1634 | return 0; | |
1635 | } | |
1636 | ||
d5124c00 HR |
1637 | static int sd_create(const char *filename, QEMUOptionParameter *options, |
1638 | Error **errp) | |
33b1db1c | 1639 | { |
b6fc8245 | 1640 | int ret = 0; |
c31d482f | 1641 | uint32_t vid = 0; |
33b1db1c | 1642 | char *backing_file = NULL; |
b6fc8245 | 1643 | BDRVSheepdogState *s; |
c31d482f | 1644 | char tag[SD_MAX_VDI_TAG_LEN]; |
b4447363 | 1645 | uint32_t snapid; |
2f536801 | 1646 | bool prealloc = false; |
33b1db1c | 1647 | |
b6fc8245 MK |
1648 | s = g_malloc0(sizeof(BDRVSheepdogState)); |
1649 | ||
b4447363 | 1650 | memset(tag, 0, sizeof(tag)); |
5d6768e3 | 1651 | if (strstr(filename, "://")) { |
c31d482f | 1652 | ret = sd_parse_uri(s, filename, s->name, &snapid, tag); |
5d6768e3 | 1653 | } else { |
c31d482f | 1654 | ret = parse_vdiname(s, filename, s->name, &snapid, tag); |
5d6768e3 MK |
1655 | } |
1656 | if (ret < 0) { | |
b6fc8245 | 1657 | goto out; |
b4447363 MK |
1658 | } |
1659 | ||
33b1db1c MK |
1660 | while (options && options->name) { |
1661 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
c31d482f | 1662 | s->inode.vdi_size = options->value.n; |
33b1db1c MK |
1663 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
1664 | backing_file = options->value.s; | |
a8e0fdd7 MK |
1665 | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { |
1666 | if (!options->value.s || !strcmp(options->value.s, "off")) { | |
2f536801 | 1667 | prealloc = false; |
a8e0fdd7 | 1668 | } else if (!strcmp(options->value.s, "full")) { |
2f536801 | 1669 | prealloc = true; |
a8e0fdd7 | 1670 | } else { |
e67c3993 MA |
1671 | error_setg(errp, "Invalid preallocation mode: '%s'", |
1672 | options->value.s); | |
b6fc8245 MK |
1673 | ret = -EINVAL; |
1674 | goto out; | |
a8e0fdd7 | 1675 | } |
b3af018f | 1676 | } else if (!strcmp(options->name, BLOCK_OPT_REDUNDANCY)) { |
a3120dee LY |
1677 | if (options->value.s) { |
1678 | ret = parse_redundancy(s, options->value.s); | |
1679 | if (ret < 0) { | |
1680 | goto out; | |
1681 | } | |
b3af018f | 1682 | } |
33b1db1c MK |
1683 | } |
1684 | options++; | |
1685 | } | |
1686 | ||
c31d482f | 1687 | if (s->inode.vdi_size > SD_MAX_VDI_SIZE) { |
e67c3993 | 1688 | error_setg(errp, "too big image size"); |
b6fc8245 MK |
1689 | ret = -EINVAL; |
1690 | goto out; | |
33b1db1c MK |
1691 | } |
1692 | ||
1693 | if (backing_file) { | |
1694 | BlockDriverState *bs; | |
9f23fce7 | 1695 | BDRVSheepdogState *base; |
33b1db1c MK |
1696 | BlockDriver *drv; |
1697 | ||
1698 | /* Currently, only Sheepdog backing image is supported. */ | |
98289620 | 1699 | drv = bdrv_find_protocol(backing_file, true); |
33b1db1c | 1700 | if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { |
e67c3993 | 1701 | error_setg(errp, "backing_file must be a sheepdog image"); |
b6fc8245 MK |
1702 | ret = -EINVAL; |
1703 | goto out; | |
33b1db1c MK |
1704 | } |
1705 | ||
2e40134b HR |
1706 | bs = NULL; |
1707 | ret = bdrv_open(&bs, backing_file, NULL, NULL, BDRV_O_PROTOCOL, NULL, | |
e67c3993 | 1708 | errp); |
cb595887 | 1709 | if (ret < 0) { |
b6fc8245 | 1710 | goto out; |
cb595887 | 1711 | } |
33b1db1c | 1712 | |
9f23fce7 | 1713 | base = bs->opaque; |
33b1db1c | 1714 | |
9f23fce7 | 1715 | if (!is_snapshot(&base->inode)) { |
e67c3993 | 1716 | error_setg(errp, "cannot clone from a non snapshot vdi"); |
4f6fd349 | 1717 | bdrv_unref(bs); |
b6fc8245 MK |
1718 | ret = -EINVAL; |
1719 | goto out; | |
33b1db1c | 1720 | } |
9f23fce7 | 1721 | s->inode.vdi_id = base->inode.vdi_id; |
4f6fd349 | 1722 | bdrv_unref(bs); |
33b1db1c MK |
1723 | } |
1724 | ||
e67c3993 | 1725 | ret = do_sd_create(s, &vid, 0, errp); |
7d2d3e74 | 1726 | if (ret) { |
b6fc8245 | 1727 | goto out; |
a8e0fdd7 MK |
1728 | } |
1729 | ||
7d2d3e74 | 1730 | if (prealloc) { |
e67c3993 | 1731 | ret = sd_prealloc(filename, errp); |
318df29e | 1732 | } |
b6fc8245 MK |
1733 | out: |
1734 | g_free(s); | |
1735 | return ret; | |
33b1db1c MK |
1736 | } |
1737 | ||
1738 | static void sd_close(BlockDriverState *bs) | |
1739 | { | |
dfb12bf8 | 1740 | Error *local_err = NULL; |
33b1db1c MK |
1741 | BDRVSheepdogState *s = bs->opaque; |
1742 | SheepdogVdiReq hdr; | |
1743 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1744 | unsigned int wlen, rlen = 0; | |
1745 | int fd, ret; | |
1746 | ||
2440a2c3 | 1747 | DPRINTF("%s\n", s->name); |
33b1db1c | 1748 | |
dfb12bf8 | 1749 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 1750 | if (fd < 0) { |
dfb12bf8 MA |
1751 | qerror_report_err(local_err); |
1752 | error_free(local_err); | |
33b1db1c MK |
1753 | return; |
1754 | } | |
1755 | ||
1756 | memset(&hdr, 0, sizeof(hdr)); | |
1757 | ||
1758 | hdr.opcode = SD_OP_RELEASE_VDI; | |
9f23fce7 | 1759 | hdr.base_vdi_id = s->inode.vdi_id; |
33b1db1c MK |
1760 | wlen = strlen(s->name) + 1; |
1761 | hdr.data_length = wlen; | |
1762 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1763 | ||
1764 | ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen); | |
1765 | ||
1766 | closesocket(fd); | |
1767 | ||
1768 | if (!ret && rsp->result != SD_RES_SUCCESS && | |
1769 | rsp->result != SD_RES_VDI_NOT_LOCKED) { | |
6daf194d | 1770 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
33b1db1c MK |
1771 | } |
1772 | ||
f2e5dca4 | 1773 | qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL); |
33b1db1c | 1774 | closesocket(s->fd); |
25af257d | 1775 | g_free(s->host_spec); |
33b1db1c MK |
1776 | } |
1777 | ||
1778 | static int64_t sd_getlength(BlockDriverState *bs) | |
1779 | { | |
1780 | BDRVSheepdogState *s = bs->opaque; | |
1781 | ||
1782 | return s->inode.vdi_size; | |
1783 | } | |
1784 | ||
1785 | static int sd_truncate(BlockDriverState *bs, int64_t offset) | |
1786 | { | |
dfb12bf8 | 1787 | Error *local_err = NULL; |
33b1db1c MK |
1788 | BDRVSheepdogState *s = bs->opaque; |
1789 | int ret, fd; | |
1790 | unsigned int datalen; | |
1791 | ||
1792 | if (offset < s->inode.vdi_size) { | |
6daf194d | 1793 | error_report("shrinking is not supported"); |
33b1db1c MK |
1794 | return -EINVAL; |
1795 | } else if (offset > SD_MAX_VDI_SIZE) { | |
6daf194d | 1796 | error_report("too big image size"); |
33b1db1c MK |
1797 | return -EINVAL; |
1798 | } | |
1799 | ||
dfb12bf8 | 1800 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 1801 | if (fd < 0) { |
dfb12bf8 MA |
1802 | qerror_report_err(local_err); |
1803 | error_free(local_err); | |
cb595887 | 1804 | return fd; |
33b1db1c MK |
1805 | } |
1806 | ||
1807 | /* we don't need to update entire object */ | |
1808 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
1809 | s->inode.vdi_size = offset; | |
1810 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
0e7106d8 | 1811 | s->inode.nr_copies, datalen, 0, false, s->cache_flags); |
33b1db1c MK |
1812 | close(fd); |
1813 | ||
1814 | if (ret < 0) { | |
6daf194d | 1815 | error_report("failed to update an inode."); |
33b1db1c MK |
1816 | } |
1817 | ||
cb595887 | 1818 | return ret; |
33b1db1c MK |
1819 | } |
1820 | ||
1821 | /* | |
1822 | * This function is called after writing data objects. If we need to | |
1823 | * update metadata, this sends a write request to the vdi object. | |
2df46246 | 1824 | * Otherwise, this switches back to sd_co_readv/writev. |
33b1db1c | 1825 | */ |
d8716b41 | 1826 | static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) |
33b1db1c | 1827 | { |
33b1db1c MK |
1828 | BDRVSheepdogState *s = acb->common.bs->opaque; |
1829 | struct iovec iov; | |
1830 | AIOReq *aio_req; | |
1831 | uint32_t offset, data_len, mn, mx; | |
1832 | ||
1833 | mn = s->min_dirty_data_idx; | |
1834 | mx = s->max_dirty_data_idx; | |
1835 | if (mn <= mx) { | |
1836 | /* we need to update the vdi object. */ | |
1837 | offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + | |
1838 | mn * sizeof(s->inode.data_vdi_id[0]); | |
1839 | data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); | |
1840 | ||
1841 | s->min_dirty_data_idx = UINT32_MAX; | |
1842 | s->max_dirty_data_idx = 0; | |
1843 | ||
1844 | iov.iov_base = &s->inode; | |
1845 | iov.iov_len = sizeof(s->inode); | |
1846 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), | |
1847 | data_len, offset, 0, 0, offset); | |
c292ee6a | 1848 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
a37dcdf9 | 1849 | add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA); |
33b1db1c MK |
1850 | |
1851 | acb->aio_done_func = sd_finish_aiocb; | |
1852 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
1853 | return; | |
1854 | } | |
a37dcdf9 | 1855 | |
33b1db1c MK |
1856 | sd_finish_aiocb(acb); |
1857 | } | |
1858 | ||
859e5553 LY |
1859 | /* Delete current working VDI on the snapshot chain */ |
1860 | static bool sd_delete(BDRVSheepdogState *s) | |
1861 | { | |
dfb12bf8 | 1862 | Error *local_err = NULL; |
859e5553 LY |
1863 | unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0; |
1864 | SheepdogVdiReq hdr = { | |
1865 | .opcode = SD_OP_DEL_VDI, | |
9f23fce7 | 1866 | .base_vdi_id = s->inode.vdi_id, |
859e5553 LY |
1867 | .data_length = wlen, |
1868 | .flags = SD_FLAG_CMD_WRITE, | |
1869 | }; | |
1870 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1871 | int fd, ret; | |
1872 | ||
dfb12bf8 | 1873 | fd = connect_to_sdog(s, &local_err); |
859e5553 | 1874 | if (fd < 0) { |
dfb12bf8 MA |
1875 | qerror_report_err(local_err); |
1876 | error_free(local_err); | |
859e5553 LY |
1877 | return false; |
1878 | } | |
1879 | ||
1880 | ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen); | |
1881 | closesocket(fd); | |
1882 | if (ret) { | |
1883 | return false; | |
1884 | } | |
1885 | switch (rsp->result) { | |
1886 | case SD_RES_NO_VDI: | |
1887 | error_report("%s was already deleted", s->name); | |
1888 | /* fall through */ | |
1889 | case SD_RES_SUCCESS: | |
1890 | break; | |
1891 | default: | |
1892 | error_report("%s, %s", sd_strerror(rsp->result), s->name); | |
1893 | return false; | |
1894 | } | |
1895 | ||
1896 | return true; | |
1897 | } | |
1898 | ||
33b1db1c MK |
1899 | /* |
1900 | * Create a writable VDI from a snapshot | |
1901 | */ | |
1902 | static int sd_create_branch(BDRVSheepdogState *s) | |
1903 | { | |
dfb12bf8 | 1904 | Error *local_err = NULL; |
33b1db1c MK |
1905 | int ret, fd; |
1906 | uint32_t vid; | |
1907 | char *buf; | |
859e5553 | 1908 | bool deleted; |
33b1db1c | 1909 | |
2440a2c3 | 1910 | DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id); |
33b1db1c | 1911 | |
7267c094 | 1912 | buf = g_malloc(SD_INODE_SIZE); |
33b1db1c | 1913 | |
859e5553 LY |
1914 | /* |
1915 | * Even If deletion fails, we will just create extra snapshot based on | |
dc6fb73d | 1916 | * the working VDI which was supposed to be deleted. So no need to |
859e5553 LY |
1917 | * false bail out. |
1918 | */ | |
1919 | deleted = sd_delete(s); | |
7d2d3e74 | 1920 | ret = do_sd_create(s, &vid, !deleted, &local_err); |
33b1db1c | 1921 | if (ret) { |
7d2d3e74 MA |
1922 | qerror_report_err(local_err); |
1923 | error_free(local_err); | |
33b1db1c MK |
1924 | goto out; |
1925 | } | |
1926 | ||
2440a2c3 | 1927 | DPRINTF("%" PRIx32 " is created.\n", vid); |
33b1db1c | 1928 | |
dfb12bf8 | 1929 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 1930 | if (fd < 0) { |
dfb12bf8 MA |
1931 | qerror_report_err(local_err); |
1932 | error_free(local_err); | |
cb595887 | 1933 | ret = fd; |
33b1db1c MK |
1934 | goto out; |
1935 | } | |
1936 | ||
1937 | ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies, | |
0e7106d8 | 1938 | SD_INODE_SIZE, 0, s->cache_flags); |
33b1db1c MK |
1939 | |
1940 | closesocket(fd); | |
1941 | ||
1942 | if (ret < 0) { | |
1943 | goto out; | |
1944 | } | |
1945 | ||
1946 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
1947 | ||
2f536801 | 1948 | s->is_snapshot = false; |
33b1db1c | 1949 | ret = 0; |
2440a2c3 | 1950 | DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id); |
33b1db1c MK |
1951 | |
1952 | out: | |
7267c094 | 1953 | g_free(buf); |
33b1db1c MK |
1954 | |
1955 | return ret; | |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * Send I/O requests to the server. | |
1960 | * | |
1961 | * This function sends requests to the server, links the requests to | |
c292ee6a | 1962 | * the inflight_list in BDRVSheepdogState, and exits without |
33b1db1c MK |
1963 | * waiting the response. The responses are received in the |
1964 | * `aio_read_response' function which is called from the main loop as | |
1965 | * a fd handler. | |
2df46246 MK |
1966 | * |
1967 | * Returns 1 when we need to wait a response, 0 when there is no sent | |
1968 | * request and -errno in error cases. | |
33b1db1c | 1969 | */ |
d8716b41 | 1970 | static int coroutine_fn sd_co_rw_vector(void *p) |
33b1db1c MK |
1971 | { |
1972 | SheepdogAIOCB *acb = p; | |
1973 | int ret = 0; | |
e8bfaa2f LY |
1974 | unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE; |
1975 | unsigned long idx = acb->sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE; | |
33b1db1c | 1976 | uint64_t oid; |
e8bfaa2f | 1977 | uint64_t offset = (acb->sector_num * BDRV_SECTOR_SIZE) % SD_DATA_OBJ_SIZE; |
33b1db1c MK |
1978 | BDRVSheepdogState *s = acb->common.bs->opaque; |
1979 | SheepdogInode *inode = &s->inode; | |
1980 | AIOReq *aio_req; | |
1981 | ||
33b1db1c MK |
1982 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) { |
1983 | /* | |
1984 | * In the case we open the snapshot VDI, Sheepdog creates the | |
1985 | * writable VDI when we do a write operation first. | |
1986 | */ | |
1987 | ret = sd_create_branch(s); | |
1988 | if (ret) { | |
1989 | acb->ret = -EIO; | |
1990 | goto out; | |
1991 | } | |
1992 | } | |
1993 | ||
1d732d7d MK |
1994 | /* |
1995 | * Make sure we don't free the aiocb before we are done with all requests. | |
1996 | * This additional reference is dropped at the end of this function. | |
1997 | */ | |
1998 | acb->nr_pending++; | |
1999 | ||
33b1db1c MK |
2000 | while (done != total) { |
2001 | uint8_t flags = 0; | |
2002 | uint64_t old_oid = 0; | |
2f536801 | 2003 | bool create = false; |
33b1db1c MK |
2004 | |
2005 | oid = vid_to_data_oid(inode->data_vdi_id[idx], idx); | |
2006 | ||
2007 | len = MIN(total - done, SD_DATA_OBJ_SIZE - offset); | |
2008 | ||
19db9b90 CH |
2009 | switch (acb->aiocb_type) { |
2010 | case AIOCB_READ_UDATA: | |
2011 | if (!inode->data_vdi_id[idx]) { | |
2012 | qemu_iovec_memset(acb->qiov, done, 0, len); | |
33b1db1c MK |
2013 | goto done; |
2014 | } | |
19db9b90 CH |
2015 | break; |
2016 | case AIOCB_WRITE_UDATA: | |
2017 | if (!inode->data_vdi_id[idx]) { | |
2f536801 | 2018 | create = true; |
19db9b90 CH |
2019 | } else if (!is_data_obj_writable(inode, idx)) { |
2020 | /* Copy-On-Write */ | |
2f536801 | 2021 | create = true; |
19db9b90 CH |
2022 | old_oid = oid; |
2023 | flags = SD_FLAG_CMD_COW; | |
2024 | } | |
2025 | break; | |
cac8f4a6 LY |
2026 | case AIOCB_DISCARD_OBJ: |
2027 | /* | |
2028 | * We discard the object only when the whole object is | |
2029 | * 1) allocated 2) trimmed. Otherwise, simply skip it. | |
2030 | */ | |
2031 | if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) { | |
2032 | goto done; | |
2033 | } | |
2034 | break; | |
19db9b90 CH |
2035 | default: |
2036 | break; | |
33b1db1c MK |
2037 | } |
2038 | ||
2039 | if (create) { | |
2440a2c3 | 2040 | DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n", |
1b6ac998 | 2041 | inode->vdi_id, oid, |
33b1db1c MK |
2042 | vid_to_data_oid(inode->data_vdi_id[idx], idx), idx); |
2043 | oid = vid_to_data_oid(inode->vdi_id, idx); | |
2440a2c3 | 2044 | DPRINTF("new oid %" PRIx64 "\n", oid); |
33b1db1c MK |
2045 | } |
2046 | ||
2047 | aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done); | |
80308d33 | 2048 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
33b1db1c MK |
2049 | |
2050 | if (create) { | |
80308d33 MK |
2051 | if (check_simultaneous_create(s, aio_req)) { |
2052 | goto done; | |
33b1db1c MK |
2053 | } |
2054 | } | |
2055 | ||
a37dcdf9 MK |
2056 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, create, |
2057 | acb->aiocb_type); | |
33b1db1c MK |
2058 | done: |
2059 | offset = 0; | |
2060 | idx++; | |
2061 | done += len; | |
2062 | } | |
2063 | out: | |
1d732d7d | 2064 | if (!--acb->nr_pending) { |
2df46246 | 2065 | return acb->ret; |
33b1db1c | 2066 | } |
2df46246 | 2067 | return 1; |
33b1db1c MK |
2068 | } |
2069 | ||
a968168c | 2070 | static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, |
2df46246 | 2071 | int nb_sectors, QEMUIOVector *qiov) |
33b1db1c MK |
2072 | { |
2073 | SheepdogAIOCB *acb; | |
2df46246 | 2074 | int ret; |
e50d7607 LY |
2075 | int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE; |
2076 | BDRVSheepdogState *s = bs->opaque; | |
33b1db1c | 2077 | |
e50d7607 LY |
2078 | if (bs->growable && offset > s->inode.vdi_size) { |
2079 | ret = sd_truncate(bs, offset); | |
cb595887 MK |
2080 | if (ret < 0) { |
2081 | return ret; | |
33b1db1c | 2082 | } |
33b1db1c MK |
2083 | } |
2084 | ||
f700f8e3 | 2085 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
33b1db1c MK |
2086 | acb->aio_done_func = sd_write_done; |
2087 | acb->aiocb_type = AIOCB_WRITE_UDATA; | |
2088 | ||
2df46246 MK |
2089 | ret = sd_co_rw_vector(acb); |
2090 | if (ret <= 0) { | |
2091 | qemu_aio_release(acb); | |
2092 | return ret; | |
2093 | } | |
2094 | ||
2095 | qemu_coroutine_yield(); | |
2096 | ||
2097 | return acb->ret; | |
33b1db1c MK |
2098 | } |
2099 | ||
a968168c | 2100 | static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num, |
2df46246 | 2101 | int nb_sectors, QEMUIOVector *qiov) |
33b1db1c MK |
2102 | { |
2103 | SheepdogAIOCB *acb; | |
19db9b90 | 2104 | int ret; |
33b1db1c | 2105 | |
f700f8e3 | 2106 | acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); |
33b1db1c MK |
2107 | acb->aiocb_type = AIOCB_READ_UDATA; |
2108 | acb->aio_done_func = sd_finish_aiocb; | |
2109 | ||
2df46246 MK |
2110 | ret = sd_co_rw_vector(acb); |
2111 | if (ret <= 0) { | |
2112 | qemu_aio_release(acb); | |
2113 | return ret; | |
2114 | } | |
2115 | ||
2116 | qemu_coroutine_yield(); | |
2117 | ||
2118 | return acb->ret; | |
33b1db1c MK |
2119 | } |
2120 | ||
47622c44 LY |
2121 | static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) |
2122 | { | |
2123 | BDRVSheepdogState *s = bs->opaque; | |
47783072 LY |
2124 | SheepdogAIOCB *acb; |
2125 | AIOReq *aio_req; | |
47622c44 | 2126 | |
0e7106d8 | 2127 | if (s->cache_flags != SD_FLAG_CMD_CACHE) { |
47622c44 LY |
2128 | return 0; |
2129 | } | |
2130 | ||
f700f8e3 | 2131 | acb = sd_aio_setup(bs, NULL, 0, 0); |
47783072 LY |
2132 | acb->aiocb_type = AIOCB_FLUSH_CACHE; |
2133 | acb->aio_done_func = sd_finish_aiocb; | |
47622c44 | 2134 | |
47783072 LY |
2135 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), |
2136 | 0, 0, 0, 0, 0); | |
2137 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); | |
a37dcdf9 | 2138 | add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type); |
47622c44 | 2139 | |
47783072 LY |
2140 | qemu_coroutine_yield(); |
2141 | return acb->ret; | |
47622c44 LY |
2142 | } |
2143 | ||
33b1db1c MK |
2144 | static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) |
2145 | { | |
dfb12bf8 | 2146 | Error *local_err = NULL; |
33b1db1c MK |
2147 | BDRVSheepdogState *s = bs->opaque; |
2148 | int ret, fd; | |
2149 | uint32_t new_vid; | |
2150 | SheepdogInode *inode; | |
2151 | unsigned int datalen; | |
2152 | ||
2440a2c3 | 2153 | DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " " |
33b1db1c MK |
2154 | "is_snapshot %d\n", sn_info->name, sn_info->id_str, |
2155 | s->name, sn_info->vm_state_size, s->is_snapshot); | |
2156 | ||
2157 | if (s->is_snapshot) { | |
2158 | error_report("You can't create a snapshot of a snapshot VDI, " | |
6daf194d | 2159 | "%s (%" PRIu32 ").", s->name, s->inode.vdi_id); |
33b1db1c MK |
2160 | |
2161 | return -EINVAL; | |
2162 | } | |
2163 | ||
2440a2c3 | 2164 | DPRINTF("%s %s\n", sn_info->name, sn_info->id_str); |
33b1db1c MK |
2165 | |
2166 | s->inode.vm_state_size = sn_info->vm_state_size; | |
2167 | s->inode.vm_clock_nsec = sn_info->vm_clock_nsec; | |
3178e275 JM |
2168 | /* It appears that inode.tag does not require a NUL terminator, |
2169 | * which means this use of strncpy is ok. | |
2170 | */ | |
33b1db1c MK |
2171 | strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag)); |
2172 | /* we don't need to update entire object */ | |
2173 | datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id); | |
2174 | ||
2175 | /* refresh inode. */ | |
dfb12bf8 | 2176 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2177 | if (fd < 0) { |
dfb12bf8 MA |
2178 | qerror_report_err(local_err); |
2179 | error_free(local_err); | |
cb595887 | 2180 | ret = fd; |
33b1db1c MK |
2181 | goto cleanup; |
2182 | } | |
2183 | ||
2184 | ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), | |
0e7106d8 | 2185 | s->inode.nr_copies, datalen, 0, false, s->cache_flags); |
33b1db1c | 2186 | if (ret < 0) { |
6daf194d | 2187 | error_report("failed to write snapshot's inode."); |
33b1db1c MK |
2188 | goto cleanup; |
2189 | } | |
2190 | ||
7d2d3e74 | 2191 | ret = do_sd_create(s, &new_vid, 1, &local_err); |
33b1db1c | 2192 | if (ret < 0) { |
7d2d3e74 MA |
2193 | qerror_report_err(local_err); |
2194 | error_free(local_err); | |
6daf194d | 2195 | error_report("failed to create inode for snapshot. %s", |
33b1db1c | 2196 | strerror(errno)); |
33b1db1c MK |
2197 | goto cleanup; |
2198 | } | |
2199 | ||
7267c094 | 2200 | inode = (SheepdogInode *)g_malloc(datalen); |
33b1db1c MK |
2201 | |
2202 | ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid), | |
0e7106d8 | 2203 | s->inode.nr_copies, datalen, 0, s->cache_flags); |
33b1db1c MK |
2204 | |
2205 | if (ret < 0) { | |
6daf194d | 2206 | error_report("failed to read new inode info. %s", strerror(errno)); |
33b1db1c MK |
2207 | goto cleanup; |
2208 | } | |
2209 | ||
2210 | memcpy(&s->inode, inode, datalen); | |
2440a2c3 | 2211 | DPRINTF("s->inode: name %s snap_id %x oid %x\n", |
33b1db1c MK |
2212 | s->inode.name, s->inode.snap_id, s->inode.vdi_id); |
2213 | ||
2214 | cleanup: | |
2215 | closesocket(fd); | |
2216 | return ret; | |
2217 | } | |
2218 | ||
859e5553 LY |
2219 | /* |
2220 | * We implement rollback(loadvm) operation to the specified snapshot by | |
2221 | * 1) switch to the snapshot | |
2222 | * 2) rely on sd_create_branch to delete working VDI and | |
dc6fb73d | 2223 | * 3) create a new working VDI based on the specified snapshot |
859e5553 | 2224 | */ |
33b1db1c MK |
2225 | static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
2226 | { | |
2227 | BDRVSheepdogState *s = bs->opaque; | |
2228 | BDRVSheepdogState *old_s; | |
9ff53a0e | 2229 | char tag[SD_MAX_VDI_TAG_LEN]; |
33b1db1c | 2230 | uint32_t snapid = 0; |
9ff53a0e | 2231 | int ret = 0; |
33b1db1c | 2232 | |
7267c094 | 2233 | old_s = g_malloc(sizeof(BDRVSheepdogState)); |
33b1db1c MK |
2234 | |
2235 | memcpy(old_s, s, sizeof(BDRVSheepdogState)); | |
2236 | ||
33b1db1c | 2237 | snapid = strtoul(snapshot_id, NULL, 10); |
3178e275 JM |
2238 | if (snapid) { |
2239 | tag[0] = 0; | |
2240 | } else { | |
b579ffb3 | 2241 | pstrcpy(tag, sizeof(tag), snapshot_id); |
33b1db1c MK |
2242 | } |
2243 | ||
9ff53a0e | 2244 | ret = reload_inode(s, snapid, tag); |
33b1db1c | 2245 | if (ret) { |
33b1db1c MK |
2246 | goto out; |
2247 | } | |
2248 | ||
cede621f LY |
2249 | ret = sd_create_branch(s); |
2250 | if (ret) { | |
33b1db1c MK |
2251 | goto out; |
2252 | } | |
2253 | ||
7267c094 | 2254 | g_free(old_s); |
33b1db1c MK |
2255 | |
2256 | return 0; | |
2257 | out: | |
2258 | /* recover bdrv_sd_state */ | |
2259 | memcpy(s, old_s, sizeof(BDRVSheepdogState)); | |
7267c094 | 2260 | g_free(old_s); |
33b1db1c | 2261 | |
6daf194d | 2262 | error_report("failed to open. recover old bdrv_sd_state."); |
33b1db1c MK |
2263 | |
2264 | return ret; | |
2265 | } | |
2266 | ||
a89d89d3 WX |
2267 | static int sd_snapshot_delete(BlockDriverState *bs, |
2268 | const char *snapshot_id, | |
2269 | const char *name, | |
2270 | Error **errp) | |
33b1db1c MK |
2271 | { |
2272 | /* FIXME: Delete specified snapshot id. */ | |
2273 | return 0; | |
2274 | } | |
2275 | ||
33b1db1c MK |
2276 | static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
2277 | { | |
dfb12bf8 | 2278 | Error *local_err = NULL; |
33b1db1c MK |
2279 | BDRVSheepdogState *s = bs->opaque; |
2280 | SheepdogReq req; | |
2281 | int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long); | |
2282 | QEMUSnapshotInfo *sn_tab = NULL; | |
2283 | unsigned wlen, rlen; | |
2284 | int found = 0; | |
2285 | static SheepdogInode inode; | |
2286 | unsigned long *vdi_inuse; | |
2287 | unsigned int start_nr; | |
2288 | uint64_t hval; | |
2289 | uint32_t vid; | |
2290 | ||
7267c094 | 2291 | vdi_inuse = g_malloc(max); |
33b1db1c | 2292 | |
dfb12bf8 | 2293 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2294 | if (fd < 0) { |
dfb12bf8 MA |
2295 | qerror_report_err(local_err); |
2296 | error_free(local_err); | |
cb595887 | 2297 | ret = fd; |
33b1db1c MK |
2298 | goto out; |
2299 | } | |
2300 | ||
2301 | rlen = max; | |
2302 | wlen = 0; | |
2303 | ||
2304 | memset(&req, 0, sizeof(req)); | |
2305 | ||
2306 | req.opcode = SD_OP_READ_VDIS; | |
2307 | req.data_length = max; | |
2308 | ||
2309 | ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen); | |
2310 | ||
2311 | closesocket(fd); | |
2312 | if (ret) { | |
2313 | goto out; | |
2314 | } | |
2315 | ||
7267c094 | 2316 | sn_tab = g_malloc0(nr * sizeof(*sn_tab)); |
33b1db1c MK |
2317 | |
2318 | /* calculate a vdi id with hash function */ | |
2319 | hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT); | |
2320 | start_nr = hval & (SD_NR_VDIS - 1); | |
2321 | ||
dfb12bf8 | 2322 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2323 | if (fd < 0) { |
dfb12bf8 MA |
2324 | qerror_report_err(local_err); |
2325 | error_free(local_err); | |
cb595887 | 2326 | ret = fd; |
33b1db1c MK |
2327 | goto out; |
2328 | } | |
2329 | ||
2330 | for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) { | |
2331 | if (!test_bit(vid, vdi_inuse)) { | |
2332 | break; | |
2333 | } | |
2334 | ||
2335 | /* we don't need to read entire object */ | |
2336 | ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid), | |
47622c44 | 2337 | 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0, |
0e7106d8 | 2338 | s->cache_flags); |
33b1db1c MK |
2339 | |
2340 | if (ret) { | |
2341 | continue; | |
2342 | } | |
2343 | ||
2344 | if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) { | |
2345 | sn_tab[found].date_sec = inode.snap_ctime >> 32; | |
2346 | sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff; | |
2347 | sn_tab[found].vm_state_size = inode.vm_state_size; | |
2348 | sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec; | |
2349 | ||
521b2b5d HR |
2350 | snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), |
2351 | "%" PRIu32, inode.snap_id); | |
3178e275 JM |
2352 | pstrcpy(sn_tab[found].name, |
2353 | MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)), | |
2354 | inode.tag); | |
33b1db1c MK |
2355 | found++; |
2356 | } | |
2357 | } | |
2358 | ||
2359 | closesocket(fd); | |
2360 | out: | |
2361 | *psn_tab = sn_tab; | |
2362 | ||
7267c094 | 2363 | g_free(vdi_inuse); |
33b1db1c | 2364 | |
cb595887 MK |
2365 | if (ret < 0) { |
2366 | return ret; | |
2367 | } | |
2368 | ||
33b1db1c MK |
2369 | return found; |
2370 | } | |
2371 | ||
2372 | static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data, | |
2373 | int64_t pos, int size, int load) | |
2374 | { | |
dfb12bf8 | 2375 | Error *local_err = NULL; |
2f536801 MK |
2376 | bool create; |
2377 | int fd, ret = 0, remaining = size; | |
33b1db1c MK |
2378 | unsigned int data_len; |
2379 | uint64_t vmstate_oid; | |
33b1db1c | 2380 | uint64_t offset; |
cede621f LY |
2381 | uint32_t vdi_index; |
2382 | uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id; | |
33b1db1c | 2383 | |
dfb12bf8 | 2384 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2385 | if (fd < 0) { |
dfb12bf8 MA |
2386 | qerror_report_err(local_err); |
2387 | error_free(local_err); | |
cb595887 | 2388 | return fd; |
33b1db1c MK |
2389 | } |
2390 | ||
6f3c714e | 2391 | while (remaining) { |
33b1db1c MK |
2392 | vdi_index = pos / SD_DATA_OBJ_SIZE; |
2393 | offset = pos % SD_DATA_OBJ_SIZE; | |
2394 | ||
1f7a48de | 2395 | data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset); |
33b1db1c | 2396 | |
cede621f | 2397 | vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index); |
33b1db1c MK |
2398 | |
2399 | create = (offset == 0); | |
2400 | if (load) { | |
2401 | ret = read_object(fd, (char *)data, vmstate_oid, | |
47622c44 | 2402 | s->inode.nr_copies, data_len, offset, |
0e7106d8 | 2403 | s->cache_flags); |
33b1db1c MK |
2404 | } else { |
2405 | ret = write_object(fd, (char *)data, vmstate_oid, | |
47622c44 | 2406 | s->inode.nr_copies, data_len, offset, create, |
0e7106d8 | 2407 | s->cache_flags); |
33b1db1c MK |
2408 | } |
2409 | ||
2410 | if (ret < 0) { | |
6daf194d | 2411 | error_report("failed to save vmstate %s", strerror(errno)); |
33b1db1c MK |
2412 | goto cleanup; |
2413 | } | |
2414 | ||
2415 | pos += data_len; | |
1f7a48de | 2416 | data += data_len; |
6f3c714e | 2417 | remaining -= data_len; |
33b1db1c | 2418 | } |
6f3c714e | 2419 | ret = size; |
33b1db1c MK |
2420 | cleanup: |
2421 | closesocket(fd); | |
2422 | return ret; | |
2423 | } | |
2424 | ||
cf8074b3 KW |
2425 | static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
2426 | int64_t pos) | |
33b1db1c MK |
2427 | { |
2428 | BDRVSheepdogState *s = bs->opaque; | |
cf8074b3 KW |
2429 | void *buf; |
2430 | int ret; | |
33b1db1c | 2431 | |
cf8074b3 KW |
2432 | buf = qemu_blockalign(bs, qiov->size); |
2433 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); | |
2434 | ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0); | |
2435 | qemu_vfree(buf); | |
2436 | ||
2437 | return ret; | |
33b1db1c MK |
2438 | } |
2439 | ||
2440 | static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data, | |
2441 | int64_t pos, int size) | |
2442 | { | |
2443 | BDRVSheepdogState *s = bs->opaque; | |
2444 | ||
2445 | return do_load_save_vmstate(s, data, pos, size, 1); | |
2446 | } | |
2447 | ||
2448 | ||
cac8f4a6 LY |
2449 | static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num, |
2450 | int nb_sectors) | |
2451 | { | |
2452 | SheepdogAIOCB *acb; | |
2453 | QEMUIOVector dummy; | |
2454 | BDRVSheepdogState *s = bs->opaque; | |
2455 | int ret; | |
2456 | ||
2457 | if (!s->discard_supported) { | |
2458 | return 0; | |
2459 | } | |
2460 | ||
2461 | acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors); | |
2462 | acb->aiocb_type = AIOCB_DISCARD_OBJ; | |
2463 | acb->aio_done_func = sd_finish_aiocb; | |
2464 | ||
2465 | ret = sd_co_rw_vector(acb); | |
2466 | if (ret <= 0) { | |
2467 | qemu_aio_release(acb); | |
2468 | return ret; | |
2469 | } | |
2470 | ||
2471 | qemu_coroutine_yield(); | |
2472 | ||
2473 | return acb->ret; | |
2474 | } | |
2475 | ||
b6b8a333 PB |
2476 | static coroutine_fn int64_t |
2477 | sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
2478 | int *pnum) | |
8d71c631 LY |
2479 | { |
2480 | BDRVSheepdogState *s = bs->opaque; | |
2481 | SheepdogInode *inode = &s->inode; | |
9cd76737 LY |
2482 | uint64_t offset = sector_num * BDRV_SECTOR_SIZE; |
2483 | unsigned long start = offset / SD_DATA_OBJ_SIZE, | |
8d71c631 LY |
2484 | end = DIV_ROUND_UP((sector_num + nb_sectors) * |
2485 | BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE); | |
2486 | unsigned long idx; | |
9cd76737 | 2487 | int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; |
8d71c631 LY |
2488 | |
2489 | for (idx = start; idx < end; idx++) { | |
2490 | if (inode->data_vdi_id[idx] == 0) { | |
2491 | break; | |
2492 | } | |
2493 | } | |
2494 | if (idx == start) { | |
2495 | /* Get the longest length of unallocated sectors */ | |
2496 | ret = 0; | |
2497 | for (idx = start + 1; idx < end; idx++) { | |
2498 | if (inode->data_vdi_id[idx] != 0) { | |
2499 | break; | |
2500 | } | |
2501 | } | |
2502 | } | |
2503 | ||
2504 | *pnum = (idx - start) * SD_DATA_OBJ_SIZE / BDRV_SECTOR_SIZE; | |
2505 | if (*pnum > nb_sectors) { | |
2506 | *pnum = nb_sectors; | |
2507 | } | |
2508 | return ret; | |
2509 | } | |
2510 | ||
85829722 LY |
2511 | static int64_t sd_get_allocated_file_size(BlockDriverState *bs) |
2512 | { | |
2513 | BDRVSheepdogState *s = bs->opaque; | |
2514 | SheepdogInode *inode = &s->inode; | |
2515 | unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, SD_DATA_OBJ_SIZE); | |
2516 | uint64_t size = 0; | |
2517 | ||
2518 | for (i = 0; i < last; i++) { | |
2519 | if (inode->data_vdi_id[i] == 0) { | |
2520 | continue; | |
2521 | } | |
2522 | size += SD_DATA_OBJ_SIZE; | |
2523 | } | |
2524 | return size; | |
2525 | } | |
2526 | ||
33b1db1c MK |
2527 | static QEMUOptionParameter sd_create_options[] = { |
2528 | { | |
2529 | .name = BLOCK_OPT_SIZE, | |
2530 | .type = OPT_SIZE, | |
2531 | .help = "Virtual disk size" | |
2532 | }, | |
2533 | { | |
2534 | .name = BLOCK_OPT_BACKING_FILE, | |
2535 | .type = OPT_STRING, | |
2536 | .help = "File name of a base image" | |
2537 | }, | |
a8e0fdd7 MK |
2538 | { |
2539 | .name = BLOCK_OPT_PREALLOC, | |
2540 | .type = OPT_STRING, | |
2541 | .help = "Preallocation mode (allowed values: off, full)" | |
2542 | }, | |
b3af018f LY |
2543 | { |
2544 | .name = BLOCK_OPT_REDUNDANCY, | |
2545 | .type = OPT_STRING, | |
2546 | .help = "Redundancy of the image" | |
2547 | }, | |
33b1db1c MK |
2548 | { NULL } |
2549 | }; | |
2550 | ||
5d6768e3 | 2551 | static BlockDriver bdrv_sheepdog = { |
33b1db1c MK |
2552 | .format_name = "sheepdog", |
2553 | .protocol_name = "sheepdog", | |
2554 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2555 | .bdrv_needs_filename = true, |
33b1db1c MK |
2556 | .bdrv_file_open = sd_open, |
2557 | .bdrv_close = sd_close, | |
2558 | .bdrv_create = sd_create, | |
e4f5c1bf | 2559 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
33b1db1c | 2560 | .bdrv_getlength = sd_getlength, |
85829722 | 2561 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
33b1db1c MK |
2562 | .bdrv_truncate = sd_truncate, |
2563 | ||
2df46246 MK |
2564 | .bdrv_co_readv = sd_co_readv, |
2565 | .bdrv_co_writev = sd_co_writev, | |
47622c44 | 2566 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, |
cac8f4a6 | 2567 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2568 | .bdrv_co_get_block_status = sd_co_get_block_status, |
33b1db1c MK |
2569 | |
2570 | .bdrv_snapshot_create = sd_snapshot_create, | |
2571 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2572 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2573 | .bdrv_snapshot_list = sd_snapshot_list, | |
2574 | ||
2575 | .bdrv_save_vmstate = sd_save_vmstate, | |
2576 | .bdrv_load_vmstate = sd_load_vmstate, | |
2577 | ||
2578 | .create_options = sd_create_options, | |
2579 | }; | |
2580 | ||
5d6768e3 MK |
2581 | static BlockDriver bdrv_sheepdog_tcp = { |
2582 | .format_name = "sheepdog", | |
2583 | .protocol_name = "sheepdog+tcp", | |
2584 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2585 | .bdrv_needs_filename = true, |
5d6768e3 MK |
2586 | .bdrv_file_open = sd_open, |
2587 | .bdrv_close = sd_close, | |
2588 | .bdrv_create = sd_create, | |
e4f5c1bf | 2589 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
5d6768e3 | 2590 | .bdrv_getlength = sd_getlength, |
85829722 | 2591 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
5d6768e3 MK |
2592 | .bdrv_truncate = sd_truncate, |
2593 | ||
2594 | .bdrv_co_readv = sd_co_readv, | |
2595 | .bdrv_co_writev = sd_co_writev, | |
2596 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
cac8f4a6 | 2597 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2598 | .bdrv_co_get_block_status = sd_co_get_block_status, |
5d6768e3 MK |
2599 | |
2600 | .bdrv_snapshot_create = sd_snapshot_create, | |
2601 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2602 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2603 | .bdrv_snapshot_list = sd_snapshot_list, | |
2604 | ||
2605 | .bdrv_save_vmstate = sd_save_vmstate, | |
2606 | .bdrv_load_vmstate = sd_load_vmstate, | |
2607 | ||
2608 | .create_options = sd_create_options, | |
2609 | }; | |
2610 | ||
1b8bbb46 MK |
2611 | static BlockDriver bdrv_sheepdog_unix = { |
2612 | .format_name = "sheepdog", | |
2613 | .protocol_name = "sheepdog+unix", | |
2614 | .instance_size = sizeof(BDRVSheepdogState), | |
030be321 | 2615 | .bdrv_needs_filename = true, |
1b8bbb46 MK |
2616 | .bdrv_file_open = sd_open, |
2617 | .bdrv_close = sd_close, | |
2618 | .bdrv_create = sd_create, | |
3ac21627 | 2619 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
1b8bbb46 | 2620 | .bdrv_getlength = sd_getlength, |
85829722 | 2621 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
1b8bbb46 MK |
2622 | .bdrv_truncate = sd_truncate, |
2623 | ||
2624 | .bdrv_co_readv = sd_co_readv, | |
2625 | .bdrv_co_writev = sd_co_writev, | |
2626 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
cac8f4a6 | 2627 | .bdrv_co_discard = sd_co_discard, |
b6b8a333 | 2628 | .bdrv_co_get_block_status = sd_co_get_block_status, |
1b8bbb46 MK |
2629 | |
2630 | .bdrv_snapshot_create = sd_snapshot_create, | |
2631 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
2632 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
2633 | .bdrv_snapshot_list = sd_snapshot_list, | |
2634 | ||
2635 | .bdrv_save_vmstate = sd_save_vmstate, | |
2636 | .bdrv_load_vmstate = sd_load_vmstate, | |
2637 | ||
2638 | .create_options = sd_create_options, | |
2639 | }; | |
2640 | ||
33b1db1c MK |
2641 | static void bdrv_sheepdog_init(void) |
2642 | { | |
2643 | bdrv_register(&bdrv_sheepdog); | |
5d6768e3 | 2644 | bdrv_register(&bdrv_sheepdog_tcp); |
1b8bbb46 | 2645 | bdrv_register(&bdrv_sheepdog_unix); |
33b1db1c MK |
2646 | } |
2647 | block_init(bdrv_sheepdog_init); |