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