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