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