]>
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 | 14 | |
80c71a24 | 15 | #include "qemu/osdep.h" |
da34e65c | 16 | #include "qapi/error.h" |
9af23989 | 17 | #include "qapi/qapi-visit-sockets.h" |
63fd65a0 | 18 | #include "qapi/qapi-visit-block-core.h" |
831acdc9 | 19 | #include "qapi/qmp/qdict.h" |
d1c13688 | 20 | #include "qapi/qobject-input-visitor.h" |
63fd65a0 | 21 | #include "qapi/qobject-output-visitor.h" |
5d6768e3 | 22 | #include "qemu/uri.h" |
1de7afc9 | 23 | #include "qemu/error-report.h" |
922a01a0 | 24 | #include "qemu/option.h" |
1de7afc9 | 25 | #include "qemu/sockets.h" |
737e150e | 26 | #include "block/block_int.h" |
609f45ea | 27 | #include "block/qdict.h" |
fba98d45 | 28 | #include "sysemu/block-backend.h" |
1de7afc9 | 29 | #include "qemu/bitops.h" |
f348b6d1 | 30 | #include "qemu/cutils.h" |
70018a14 | 31 | #include "trace.h" |
33b1db1c MK |
32 | |
33 | #define SD_PROTO_VER 0x01 | |
34 | ||
35 | #define SD_DEFAULT_ADDR "localhost" | |
25af257d | 36 | #define SD_DEFAULT_PORT 7000 |
33b1db1c MK |
37 | |
38 | #define SD_OP_CREATE_AND_WRITE_OBJ 0x01 | |
39 | #define SD_OP_READ_OBJ 0x02 | |
40 | #define SD_OP_WRITE_OBJ 0x03 | |
cac8f4a6 | 41 | /* 0x04 is used internally by Sheepdog */ |
33b1db1c MK |
42 | |
43 | #define SD_OP_NEW_VDI 0x11 | |
44 | #define SD_OP_LOCK_VDI 0x12 | |
45 | #define SD_OP_RELEASE_VDI 0x13 | |
46 | #define SD_OP_GET_VDI_INFO 0x14 | |
47 | #define SD_OP_READ_VDIS 0x15 | |
47622c44 | 48 | #define SD_OP_FLUSH_VDI 0x16 |
859e5553 | 49 | #define SD_OP_DEL_VDI 0x17 |
876eb1b0 | 50 | #define SD_OP_GET_CLUSTER_DEFAULT 0x18 |
33b1db1c MK |
51 | |
52 | #define SD_FLAG_CMD_WRITE 0x01 | |
53 | #define SD_FLAG_CMD_COW 0x02 | |
0e7106d8 LY |
54 | #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */ |
55 | #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */ | |
33b1db1c MK |
56 | |
57 | #define SD_RES_SUCCESS 0x00 /* Success */ | |
58 | #define SD_RES_UNKNOWN 0x01 /* Unknown error */ | |
59 | #define SD_RES_NO_OBJ 0x02 /* No object found */ | |
60 | #define SD_RES_EIO 0x03 /* I/O error */ | |
61 | #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */ | |
62 | #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */ | |
63 | #define SD_RES_SYSTEM_ERROR 0x06 /* System error */ | |
64 | #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */ | |
65 | #define SD_RES_NO_VDI 0x08 /* No vdi found */ | |
66 | #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */ | |
67 | #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */ | |
68 | #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */ | |
69 | #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */ | |
70 | #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */ | |
71 | #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */ | |
72 | #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */ | |
73 | #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */ | |
74 | #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */ | |
75 | #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */ | |
76 | #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */ | |
77 | #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */ | |
78 | #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */ | |
79 | #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */ | |
80 | #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */ | |
81 | #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */ | |
fca23f0a | 82 | #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */ |
6a0b5490 | 83 | #define SD_RES_READONLY 0x1A /* Object is read-only */ |
33b1db1c MK |
84 | |
85 | /* | |
86 | * Object ID rules | |
87 | * | |
88 | * 0 - 19 (20 bits): data object space | |
89 | * 20 - 31 (12 bits): reserved data object space | |
90 | * 32 - 55 (24 bits): vdi object space | |
91 | * 56 - 59 ( 4 bits): reserved vdi object space | |
7acae208 | 92 | * 60 - 63 ( 4 bits): object type identifier space |
33b1db1c MK |
93 | */ |
94 | ||
95 | #define VDI_SPACE_SHIFT 32 | |
96 | #define VDI_BIT (UINT64_C(1) << 63) | |
97 | #define VMSTATE_BIT (UINT64_C(1) << 62) | |
98 | #define MAX_DATA_OBJS (UINT64_C(1) << 20) | |
99 | #define MAX_CHILDREN 1024 | |
100 | #define SD_MAX_VDI_LEN 256 | |
101 | #define SD_MAX_VDI_TAG_LEN 256 | |
102 | #define SD_NR_VDIS (1U << 24) | |
103 | #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22) | |
104 | #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS) | |
876eb1b0 | 105 | #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22 |
b3af018f LY |
106 | /* |
107 | * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and | |
108 | * (SD_EC_MAX_STRIP - 1) for parity strips | |
109 | * | |
110 | * SD_MAX_COPIES is sum of number of data strips and parity strips. | |
111 | */ | |
112 | #define SD_EC_MAX_STRIP 16 | |
113 | #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1) | |
33b1db1c MK |
114 | |
115 | #define SD_INODE_SIZE (sizeof(SheepdogInode)) | |
116 | #define CURRENT_VDI_ID 0 | |
117 | ||
1dbfafed HM |
118 | #define LOCK_TYPE_NORMAL 0 |
119 | #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */ | |
120 | ||
33b1db1c MK |
121 | typedef struct SheepdogReq { |
122 | uint8_t proto_ver; | |
123 | uint8_t opcode; | |
124 | uint16_t flags; | |
125 | uint32_t epoch; | |
126 | uint32_t id; | |
127 | uint32_t data_length; | |
128 | uint32_t opcode_specific[8]; | |
129 | } SheepdogReq; | |
130 | ||
131 | typedef struct SheepdogRsp { | |
132 | uint8_t proto_ver; | |
133 | uint8_t opcode; | |
134 | uint16_t flags; | |
135 | uint32_t epoch; | |
136 | uint32_t id; | |
137 | uint32_t data_length; | |
138 | uint32_t result; | |
139 | uint32_t opcode_specific[7]; | |
140 | } SheepdogRsp; | |
141 | ||
142 | typedef struct SheepdogObjReq { | |
143 | uint8_t proto_ver; | |
144 | uint8_t opcode; | |
145 | uint16_t flags; | |
146 | uint32_t epoch; | |
147 | uint32_t id; | |
148 | uint32_t data_length; | |
149 | uint64_t oid; | |
150 | uint64_t cow_oid; | |
29a67f7e | 151 | uint8_t copies; |
1841f880 LY |
152 | uint8_t copy_policy; |
153 | uint8_t reserved[6]; | |
33b1db1c MK |
154 | uint64_t offset; |
155 | } SheepdogObjReq; | |
156 | ||
157 | typedef struct SheepdogObjRsp { | |
158 | uint8_t proto_ver; | |
159 | uint8_t opcode; | |
160 | uint16_t flags; | |
161 | uint32_t epoch; | |
162 | uint32_t id; | |
163 | uint32_t data_length; | |
164 | uint32_t result; | |
29a67f7e | 165 | uint8_t copies; |
1841f880 LY |
166 | uint8_t copy_policy; |
167 | uint8_t reserved[2]; | |
33b1db1c MK |
168 | uint32_t pad[6]; |
169 | } SheepdogObjRsp; | |
170 | ||
171 | typedef struct SheepdogVdiReq { | |
172 | uint8_t proto_ver; | |
173 | uint8_t opcode; | |
174 | uint16_t flags; | |
175 | uint32_t epoch; | |
176 | uint32_t id; | |
177 | uint32_t data_length; | |
178 | uint64_t vdi_size; | |
9f23fce7 | 179 | uint32_t base_vdi_id; |
29a67f7e | 180 | uint8_t copies; |
1841f880 | 181 | uint8_t copy_policy; |
876eb1b0 TI |
182 | uint8_t store_policy; |
183 | uint8_t block_size_shift; | |
33b1db1c | 184 | uint32_t snapid; |
1dbfafed HM |
185 | uint32_t type; |
186 | uint32_t pad[2]; | |
33b1db1c MK |
187 | } SheepdogVdiReq; |
188 | ||
189 | typedef struct SheepdogVdiRsp { | |
190 | uint8_t proto_ver; | |
191 | uint8_t opcode; | |
192 | uint16_t flags; | |
193 | uint32_t epoch; | |
194 | uint32_t id; | |
195 | uint32_t data_length; | |
196 | uint32_t result; | |
197 | uint32_t rsvd; | |
198 | uint32_t vdi_id; | |
199 | uint32_t pad[5]; | |
200 | } SheepdogVdiRsp; | |
201 | ||
876eb1b0 TI |
202 | typedef struct SheepdogClusterRsp { |
203 | uint8_t proto_ver; | |
204 | uint8_t opcode; | |
205 | uint16_t flags; | |
206 | uint32_t epoch; | |
207 | uint32_t id; | |
208 | uint32_t data_length; | |
209 | uint32_t result; | |
210 | uint8_t nr_copies; | |
211 | uint8_t copy_policy; | |
212 | uint8_t block_size_shift; | |
213 | uint8_t __pad1; | |
214 | uint32_t __pad2[6]; | |
215 | } SheepdogClusterRsp; | |
216 | ||
33b1db1c MK |
217 | typedef struct SheepdogInode { |
218 | char name[SD_MAX_VDI_LEN]; | |
219 | char tag[SD_MAX_VDI_TAG_LEN]; | |
220 | uint64_t ctime; | |
221 | uint64_t snap_ctime; | |
222 | uint64_t vm_clock_nsec; | |
223 | uint64_t vdi_size; | |
224 | uint64_t vm_state_size; | |
225 | uint16_t copy_policy; | |
226 | uint8_t nr_copies; | |
227 | uint8_t block_size_shift; | |
228 | uint32_t snap_id; | |
229 | uint32_t vdi_id; | |
230 | uint32_t parent_vdi_id; | |
231 | uint32_t child_vdi_id[MAX_CHILDREN]; | |
232 | uint32_t data_vdi_id[MAX_DATA_OBJS]; | |
233 | } SheepdogInode; | |
234 | ||
5d039bab HM |
235 | #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id) |
236 | ||
33b1db1c MK |
237 | /* |
238 | * 64 bit FNV-1a non-zero initial basis | |
239 | */ | |
240 | #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) | |
241 | ||
242 | /* | |
243 | * 64 bit Fowler/Noll/Vo FNV-1a hash code | |
244 | */ | |
245 | static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval) | |
246 | { | |
247 | unsigned char *bp = buf; | |
248 | unsigned char *be = bp + len; | |
249 | while (bp < be) { | |
250 | hval ^= (uint64_t) *bp++; | |
251 | hval += (hval << 1) + (hval << 4) + (hval << 5) + | |
252 | (hval << 7) + (hval << 8) + (hval << 40); | |
253 | } | |
254 | return hval; | |
255 | } | |
256 | ||
2f536801 | 257 | static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx) |
33b1db1c MK |
258 | { |
259 | return inode->vdi_id == inode->data_vdi_id[idx]; | |
260 | } | |
261 | ||
2f536801 | 262 | static inline bool is_data_obj(uint64_t oid) |
33b1db1c MK |
263 | { |
264 | return !(VDI_BIT & oid); | |
265 | } | |
266 | ||
267 | static inline uint64_t data_oid_to_idx(uint64_t oid) | |
268 | { | |
269 | return oid & (MAX_DATA_OBJS - 1); | |
270 | } | |
271 | ||
72e0996c MK |
272 | static inline uint32_t oid_to_vid(uint64_t oid) |
273 | { | |
274 | return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT; | |
275 | } | |
276 | ||
33b1db1c MK |
277 | static inline uint64_t vid_to_vdi_oid(uint32_t vid) |
278 | { | |
279 | return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT); | |
280 | } | |
281 | ||
282 | static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx) | |
283 | { | |
284 | return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; | |
285 | } | |
286 | ||
287 | static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx) | |
288 | { | |
289 | return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx; | |
290 | } | |
291 | ||
2f536801 | 292 | static inline bool is_snapshot(struct SheepdogInode *inode) |
33b1db1c MK |
293 | { |
294 | return !!inode->snap_ctime; | |
295 | } | |
296 | ||
eab8eb8d VT |
297 | static inline size_t count_data_objs(const struct SheepdogInode *inode) |
298 | { | |
299 | return DIV_ROUND_UP(inode->vdi_size, | |
300 | (1UL << inode->block_size_shift)); | |
301 | } | |
302 | ||
33b1db1c | 303 | typedef struct SheepdogAIOCB SheepdogAIOCB; |
28ddd08c | 304 | typedef struct BDRVSheepdogState BDRVSheepdogState; |
33b1db1c MK |
305 | |
306 | typedef struct AIOReq { | |
307 | SheepdogAIOCB *aiocb; | |
308 | unsigned int iov_offset; | |
309 | ||
310 | uint64_t oid; | |
311 | uint64_t base_oid; | |
312 | uint64_t offset; | |
313 | unsigned int data_len; | |
314 | uint8_t flags; | |
315 | uint32_t id; | |
b544c1ab | 316 | bool create; |
33b1db1c | 317 | |
c292ee6a | 318 | QLIST_ENTRY(AIOReq) aio_siblings; |
33b1db1c MK |
319 | } AIOReq; |
320 | ||
321 | enum AIOCBState { | |
322 | AIOCB_WRITE_UDATA, | |
323 | AIOCB_READ_UDATA, | |
47783072 | 324 | AIOCB_FLUSH_CACHE, |
cac8f4a6 | 325 | AIOCB_DISCARD_OBJ, |
33b1db1c MK |
326 | }; |
327 | ||
498f2140 | 328 | #define AIOCBOverlapping(x, y) \ |
6a55c82c HM |
329 | (!(x->max_affect_data_idx < y->min_affect_data_idx \ |
330 | || y->max_affect_data_idx < x->min_affect_data_idx)) | |
331 | ||
33b1db1c | 332 | struct SheepdogAIOCB { |
28ddd08c | 333 | BDRVSheepdogState *s; |
33b1db1c MK |
334 | |
335 | QEMUIOVector *qiov; | |
336 | ||
337 | int64_t sector_num; | |
338 | int nb_sectors; | |
339 | ||
340 | int ret; | |
341 | enum AIOCBState aiocb_type; | |
342 | ||
2df46246 | 343 | Coroutine *coroutine; |
1d732d7d | 344 | int nr_pending; |
6a55c82c HM |
345 | |
346 | uint32_t min_affect_data_idx; | |
347 | uint32_t max_affect_data_idx; | |
348 | ||
498f2140 HM |
349 | /* |
350 | * The difference between affect_data_idx and dirty_data_idx: | |
351 | * affect_data_idx represents range of index of all request types. | |
352 | * dirty_data_idx represents range of index updated by COW requests. | |
353 | * dirty_data_idx is used for updating an inode object. | |
354 | */ | |
355 | uint32_t min_dirty_data_idx; | |
356 | uint32_t max_dirty_data_idx; | |
357 | ||
6a55c82c | 358 | QLIST_ENTRY(SheepdogAIOCB) aiocb_siblings; |
33b1db1c MK |
359 | }; |
360 | ||
28ddd08c | 361 | struct BDRVSheepdogState { |
011603ca | 362 | BlockDriverState *bs; |
84390bed | 363 | AioContext *aio_context; |
011603ca | 364 | |
33b1db1c MK |
365 | SheepdogInode inode; |
366 | ||
33b1db1c | 367 | char name[SD_MAX_VDI_LEN]; |
2f536801 | 368 | bool is_snapshot; |
0e7106d8 | 369 | uint32_t cache_flags; |
cac8f4a6 | 370 | bool discard_supported; |
33b1db1c | 371 | |
bd269ebc | 372 | SocketAddress *addr; |
33b1db1c MK |
373 | int fd; |
374 | ||
2df46246 MK |
375 | CoMutex lock; |
376 | Coroutine *co_send; | |
377 | Coroutine *co_recv; | |
378 | ||
33b1db1c | 379 | uint32_t aioreq_seq_num; |
011603ca MK |
380 | |
381 | /* Every aio request must be linked to either of these queues. */ | |
b58deb34 PB |
382 | QLIST_HEAD(, AIOReq) inflight_aio_head; |
383 | QLIST_HEAD(, AIOReq) failed_aio_head; | |
6a55c82c | 384 | |
f1af3251 | 385 | CoMutex queue_lock; |
498f2140 | 386 | CoQueue overlapping_queue; |
b58deb34 | 387 | QLIST_HEAD(, SheepdogAIOCB) inflight_aiocb_head; |
28ddd08c | 388 | }; |
33b1db1c | 389 | |
4da65c80 LY |
390 | typedef struct BDRVSheepdogReopenState { |
391 | int fd; | |
392 | int cache_flags; | |
393 | } BDRVSheepdogReopenState; | |
394 | ||
d507c5f6 | 395 | static const char *sd_strerror(int err) |
33b1db1c MK |
396 | { |
397 | int i; | |
398 | ||
399 | static const struct { | |
400 | int err; | |
401 | const char *desc; | |
402 | } errors[] = { | |
403 | {SD_RES_SUCCESS, "Success"}, | |
404 | {SD_RES_UNKNOWN, "Unknown error"}, | |
405 | {SD_RES_NO_OBJ, "No object found"}, | |
406 | {SD_RES_EIO, "I/O error"}, | |
407 | {SD_RES_VDI_EXIST, "VDI exists already"}, | |
408 | {SD_RES_INVALID_PARMS, "Invalid parameters"}, | |
409 | {SD_RES_SYSTEM_ERROR, "System error"}, | |
410 | {SD_RES_VDI_LOCKED, "VDI is already locked"}, | |
411 | {SD_RES_NO_VDI, "No vdi found"}, | |
412 | {SD_RES_NO_BASE_VDI, "No base VDI found"}, | |
413 | {SD_RES_VDI_READ, "Failed read the requested VDI"}, | |
414 | {SD_RES_VDI_WRITE, "Failed to write the requested VDI"}, | |
415 | {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"}, | |
416 | {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"}, | |
417 | {SD_RES_NO_TAG, "Failed to find the requested tag"}, | |
418 | {SD_RES_STARTUP, "The system is still booting"}, | |
419 | {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"}, | |
420 | {SD_RES_SHUTDOWN, "The system is shutting down"}, | |
421 | {SD_RES_NO_MEM, "Out of memory on the server"}, | |
422 | {SD_RES_FULL_VDI, "We already have the maximum vdis"}, | |
423 | {SD_RES_VER_MISMATCH, "Protocol version mismatch"}, | |
424 | {SD_RES_NO_SPACE, "Server has no space for new objects"}, | |
425 | {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"}, | |
426 | {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"}, | |
427 | {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"}, | |
fca23f0a | 428 | {SD_RES_HALT, "Sheepdog is stopped serving IO request"}, |
6a0b5490 | 429 | {SD_RES_READONLY, "Object is read-only"}, |
33b1db1c MK |
430 | }; |
431 | ||
432 | for (i = 0; i < ARRAY_SIZE(errors); ++i) { | |
433 | if (errors[i].err == err) { | |
434 | return errors[i].desc; | |
435 | } | |
436 | } | |
437 | ||
438 | return "Invalid error code"; | |
439 | } | |
440 | ||
441 | /* | |
442 | * Sheepdog I/O handling: | |
443 | * | |
2df46246 | 444 | * 1. In sd_co_rw_vector, we send the I/O requests to the server and |
c292ee6a | 445 | * link the requests to the inflight_list in the |
e80ab33d | 446 | * BDRVSheepdogState. The function yields while waiting for |
2df46246 | 447 | * receiving the response. |
33b1db1c | 448 | * |
2df46246 | 449 | * 2. We receive the response in aio_read_response, the fd handler to |
e80ab33d PB |
450 | * the sheepdog connection. We switch back to sd_co_readv/sd_writev |
451 | * after all the requests belonging to the AIOCB are finished. If | |
452 | * needed, sd_co_writev will send another requests for the vdi object. | |
33b1db1c MK |
453 | */ |
454 | ||
455 | static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, | |
456 | uint64_t oid, unsigned int data_len, | |
b544c1ab | 457 | uint64_t offset, uint8_t flags, bool create, |
33b1db1c MK |
458 | uint64_t base_oid, unsigned int iov_offset) |
459 | { | |
460 | AIOReq *aio_req; | |
461 | ||
7267c094 | 462 | aio_req = g_malloc(sizeof(*aio_req)); |
33b1db1c MK |
463 | aio_req->aiocb = acb; |
464 | aio_req->iov_offset = iov_offset; | |
465 | aio_req->oid = oid; | |
466 | aio_req->base_oid = base_oid; | |
467 | aio_req->offset = offset; | |
468 | aio_req->data_len = data_len; | |
469 | aio_req->flags = flags; | |
470 | aio_req->id = s->aioreq_seq_num++; | |
b544c1ab | 471 | aio_req->create = create; |
33b1db1c | 472 | |
1d732d7d | 473 | acb->nr_pending++; |
33b1db1c MK |
474 | return aio_req; |
475 | } | |
476 | ||
acf6e5f0 PB |
477 | static void wait_for_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *acb) |
478 | { | |
479 | SheepdogAIOCB *cb; | |
480 | ||
481 | retry: | |
482 | QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) { | |
483 | if (AIOCBOverlapping(acb, cb)) { | |
f1af3251 | 484 | qemu_co_queue_wait(&s->overlapping_queue, &s->queue_lock); |
acf6e5f0 PB |
485 | goto retry; |
486 | } | |
487 | } | |
488 | } | |
489 | ||
28ddd08c PB |
490 | static void sd_aio_setup(SheepdogAIOCB *acb, BDRVSheepdogState *s, |
491 | QEMUIOVector *qiov, int64_t sector_num, int nb_sectors, | |
492 | int type) | |
33b1db1c | 493 | { |
6a55c82c | 494 | uint32_t object_size; |
6a55c82c HM |
495 | |
496 | object_size = (UINT32_C(1) << s->inode.block_size_shift); | |
33b1db1c | 497 | |
28ddd08c | 498 | acb->s = s; |
33b1db1c MK |
499 | |
500 | acb->qiov = qiov; | |
501 | ||
502 | acb->sector_num = sector_num; | |
503 | acb->nb_sectors = nb_sectors; | |
504 | ||
2df46246 | 505 | acb->coroutine = qemu_coroutine_self(); |
33b1db1c | 506 | acb->ret = 0; |
1d732d7d | 507 | acb->nr_pending = 0; |
6a55c82c HM |
508 | |
509 | acb->min_affect_data_idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size; | |
510 | acb->max_affect_data_idx = (acb->sector_num * BDRV_SECTOR_SIZE + | |
511 | acb->nb_sectors * BDRV_SECTOR_SIZE) / object_size; | |
512 | ||
498f2140 HM |
513 | acb->min_dirty_data_idx = UINT32_MAX; |
514 | acb->max_dirty_data_idx = 0; | |
28ddd08c | 515 | acb->aiocb_type = type; |
acf6e5f0 PB |
516 | |
517 | if (type == AIOCB_FLUSH_CACHE) { | |
518 | return; | |
519 | } | |
520 | ||
f1af3251 | 521 | qemu_co_mutex_lock(&s->queue_lock); |
acf6e5f0 PB |
522 | wait_for_overlapping_aiocb(s, acb); |
523 | QLIST_INSERT_HEAD(&s->inflight_aiocb_head, acb, aiocb_siblings); | |
f1af3251 | 524 | qemu_co_mutex_unlock(&s->queue_lock); |
33b1db1c MK |
525 | } |
526 | ||
bd269ebc | 527 | static SocketAddress *sd_server_config(QDict *options, Error **errp) |
d1c13688 MA |
528 | { |
529 | QDict *server = NULL; | |
d1c13688 | 530 | Visitor *iv = NULL; |
bd269ebc | 531 | SocketAddress *saddr = NULL; |
d1c13688 MA |
532 | Error *local_err = NULL; |
533 | ||
534 | qdict_extract_subqdict(options, &server, "server."); | |
535 | ||
af91062e MA |
536 | iv = qobject_input_visitor_new_flat_confused(server, errp); |
537 | if (!iv) { | |
d1c13688 MA |
538 | goto done; |
539 | } | |
540 | ||
bd269ebc | 541 | visit_type_SocketAddress(iv, NULL, &saddr, &local_err); |
d1c13688 MA |
542 | if (local_err) { |
543 | error_propagate(errp, local_err); | |
544 | goto done; | |
545 | } | |
546 | ||
d1c13688 | 547 | done: |
d1c13688 | 548 | visit_free(iv); |
cb3e7f08 | 549 | qobject_unref(server); |
d1c13688 MA |
550 | return saddr; |
551 | } | |
552 | ||
833a7cc3 | 553 | /* Return -EIO in case of error, file descriptor on success */ |
dfb12bf8 | 554 | static int connect_to_sdog(BDRVSheepdogState *s, Error **errp) |
33b1db1c | 555 | { |
25af257d | 556 | int fd; |
33b1db1c | 557 | |
b2587932 | 558 | fd = socket_connect(s->addr, errp); |
1b8bbb46 | 559 | |
bd269ebc | 560 | if (s->addr->type == SOCKET_ADDRESS_TYPE_INET && fd >= 0) { |
8ecc2f9e MA |
561 | int ret = socket_set_nodelay(fd); |
562 | if (ret < 0) { | |
5197f445 | 563 | warn_report("can't set TCP_NODELAY: %s", strerror(errno)); |
1b8bbb46 MK |
564 | } |
565 | } | |
33b1db1c | 566 | |
dfb12bf8 | 567 | if (fd >= 0) { |
f9e8cacc | 568 | qemu_set_nonblock(fd); |
833a7cc3 LY |
569 | } else { |
570 | fd = -EIO; | |
33b1db1c MK |
571 | } |
572 | ||
33b1db1c MK |
573 | return fd; |
574 | } | |
575 | ||
833a7cc3 | 576 | /* Return 0 on success and -errno in case of error */ |
e0d93a89 MK |
577 | static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, |
578 | unsigned int *wlen) | |
47622c44 LY |
579 | { |
580 | int ret; | |
581 | ||
582 | ret = qemu_co_send(sockfd, hdr, sizeof(*hdr)); | |
80731d9d | 583 | if (ret != sizeof(*hdr)) { |
47622c44 | 584 | error_report("failed to send a req, %s", strerror(errno)); |
b16a44e1 | 585 | return -errno; |
47622c44 LY |
586 | } |
587 | ||
588 | ret = qemu_co_send(sockfd, data, *wlen); | |
80731d9d | 589 | if (ret != *wlen) { |
47622c44 | 590 | error_report("failed to send a req, %s", strerror(errno)); |
b16a44e1 | 591 | return -errno; |
47622c44 LY |
592 | } |
593 | ||
594 | return ret; | |
595 | } | |
e0d93a89 | 596 | |
cddd4ac7 MK |
597 | typedef struct SheepdogReqCo { |
598 | int sockfd; | |
f11672db | 599 | BlockDriverState *bs; |
84390bed | 600 | AioContext *aio_context; |
cddd4ac7 MK |
601 | SheepdogReq *hdr; |
602 | void *data; | |
603 | unsigned int *wlen; | |
604 | unsigned int *rlen; | |
605 | int ret; | |
606 | bool finished; | |
9d456654 | 607 | Coroutine *co; |
cddd4ac7 MK |
608 | } SheepdogReqCo; |
609 | ||
9d456654 PB |
610 | static void restart_co_req(void *opaque) |
611 | { | |
612 | SheepdogReqCo *srco = opaque; | |
613 | ||
614 | aio_co_wake(srco->co); | |
615 | } | |
616 | ||
cddd4ac7 | 617 | static coroutine_fn void do_co_req(void *opaque) |
47622c44 LY |
618 | { |
619 | int ret; | |
cddd4ac7 MK |
620 | SheepdogReqCo *srco = opaque; |
621 | int sockfd = srco->sockfd; | |
622 | SheepdogReq *hdr = srco->hdr; | |
623 | void *data = srco->data; | |
624 | unsigned int *wlen = srco->wlen; | |
625 | unsigned int *rlen = srco->rlen; | |
2dfcca3b | 626 | |
9d456654 | 627 | srco->co = qemu_coroutine_self(); |
dca21ef2 | 628 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
9d456654 | 629 | NULL, restart_co_req, NULL, srco); |
47622c44 | 630 | |
47622c44 LY |
631 | ret = send_co_req(sockfd, hdr, data, wlen); |
632 | if (ret < 0) { | |
633 | goto out; | |
634 | } | |
635 | ||
dca21ef2 | 636 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
9d456654 | 637 | restart_co_req, NULL, NULL, srco); |
2dfcca3b | 638 | |
47622c44 | 639 | ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr)); |
80731d9d | 640 | if (ret != sizeof(*hdr)) { |
47622c44 | 641 | error_report("failed to get a rsp, %s", strerror(errno)); |
cb595887 | 642 | ret = -errno; |
47622c44 LY |
643 | goto out; |
644 | } | |
645 | ||
646 | if (*rlen > hdr->data_length) { | |
647 | *rlen = hdr->data_length; | |
648 | } | |
649 | ||
650 | if (*rlen) { | |
651 | ret = qemu_co_recv(sockfd, data, *rlen); | |
80731d9d | 652 | if (ret != *rlen) { |
47622c44 | 653 | error_report("failed to get the data, %s", strerror(errno)); |
cb595887 | 654 | ret = -errno; |
47622c44 LY |
655 | goto out; |
656 | } | |
657 | } | |
658 | ret = 0; | |
659 | out: | |
ed9ba724 MK |
660 | /* there is at most one request for this sockfd, so it is safe to |
661 | * set each handler to NULL. */ | |
dca21ef2 | 662 | aio_set_fd_handler(srco->aio_context, sockfd, false, |
f6a51c84 | 663 | NULL, NULL, NULL, NULL); |
cddd4ac7 | 664 | |
9d456654 | 665 | srco->co = NULL; |
cddd4ac7 | 666 | srco->ret = ret; |
e2a6ae7f PB |
667 | /* Set srco->finished before reading bs->wakeup. */ |
668 | atomic_mb_set(&srco->finished, true); | |
c9d1a561 PB |
669 | if (srco->bs) { |
670 | bdrv_wakeup(srco->bs); | |
671 | } | |
cddd4ac7 MK |
672 | } |
673 | ||
833a7cc3 LY |
674 | /* |
675 | * Send the request to the sheep in a synchronous manner. | |
676 | * | |
677 | * Return 0 on success, -errno in case of error. | |
678 | */ | |
f11672db | 679 | static int do_req(int sockfd, BlockDriverState *bs, SheepdogReq *hdr, |
84390bed | 680 | void *data, unsigned int *wlen, unsigned int *rlen) |
cddd4ac7 MK |
681 | { |
682 | Coroutine *co; | |
683 | SheepdogReqCo srco = { | |
684 | .sockfd = sockfd, | |
f11672db PB |
685 | .aio_context = bs ? bdrv_get_aio_context(bs) : qemu_get_aio_context(), |
686 | .bs = bs, | |
cddd4ac7 MK |
687 | .hdr = hdr, |
688 | .data = data, | |
689 | .wlen = wlen, | |
690 | .rlen = rlen, | |
691 | .ret = 0, | |
692 | .finished = false, | |
693 | }; | |
694 | ||
695 | if (qemu_in_coroutine()) { | |
696 | do_co_req(&srco); | |
697 | } else { | |
0b8b8753 | 698 | co = qemu_coroutine_create(do_co_req, &srco); |
f11672db | 699 | if (bs) { |
76296dff | 700 | bdrv_coroutine_enter(bs, co); |
f11672db PB |
701 | BDRV_POLL_WHILE(bs, !srco.finished); |
702 | } else { | |
703 | qemu_coroutine_enter(co); | |
704 | while (!srco.finished) { | |
705 | aio_poll(qemu_get_aio_context(), true); | |
706 | } | |
cddd4ac7 MK |
707 | } |
708 | } | |
709 | ||
710 | return srco.ret; | |
47622c44 LY |
711 | } |
712 | ||
a37dcdf9 | 713 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
b544c1ab HM |
714 | struct iovec *iov, int niov, |
715 | enum AIOCBState aiocb_type); | |
a37dcdf9 | 716 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); |
72e0996c | 717 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag); |
356b4ca2 | 718 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp); |
011603ca | 719 | static void co_write_request(void *opaque); |
7dc1cde0 | 720 | |
011603ca MK |
721 | static coroutine_fn void reconnect_to_sdog(void *opaque) |
722 | { | |
723 | BDRVSheepdogState *s = opaque; | |
724 | AIOReq *aio_req, *next; | |
725 | ||
dca21ef2 | 726 | aio_set_fd_handler(s->aio_context, s->fd, false, NULL, |
f6a51c84 | 727 | NULL, NULL, NULL); |
011603ca MK |
728 | close(s->fd); |
729 | s->fd = -1; | |
730 | ||
731 | /* Wait for outstanding write requests to be completed. */ | |
732 | while (s->co_send != NULL) { | |
733 | co_write_request(opaque); | |
734 | } | |
735 | ||
736 | /* Try to reconnect the sheepdog server every one second. */ | |
737 | while (s->fd < 0) { | |
a780dea0 | 738 | Error *local_err = NULL; |
356b4ca2 | 739 | s->fd = get_sheep_fd(s, &local_err); |
011603ca | 740 | if (s->fd < 0) { |
70018a14 | 741 | trace_sheepdog_reconnect_to_sdog(); |
565f65d2 | 742 | error_report_err(local_err); |
78f1d3d6 | 743 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000000ULL); |
011603ca MK |
744 | } |
745 | }; | |
746 | ||
747 | /* | |
748 | * Now we have to resend all the request in the inflight queue. However, | |
749 | * resend_aioreq() can yield and newly created requests can be added to the | |
750 | * inflight queue before the coroutine is resumed. To avoid mixing them, we | |
751 | * have to move all the inflight requests to the failed queue before | |
752 | * resend_aioreq() is called. | |
753 | */ | |
f1af3251 | 754 | qemu_co_mutex_lock(&s->queue_lock); |
011603ca MK |
755 | QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) { |
756 | QLIST_REMOVE(aio_req, aio_siblings); | |
757 | QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings); | |
758 | } | |
759 | ||
760 | /* Resend all the failed aio requests. */ | |
761 | while (!QLIST_EMPTY(&s->failed_aio_head)) { | |
762 | aio_req = QLIST_FIRST(&s->failed_aio_head); | |
763 | QLIST_REMOVE(aio_req, aio_siblings); | |
f1af3251 | 764 | qemu_co_mutex_unlock(&s->queue_lock); |
011603ca | 765 | resend_aioreq(s, aio_req); |
f1af3251 | 766 | qemu_co_mutex_lock(&s->queue_lock); |
011603ca | 767 | } |
f1af3251 | 768 | qemu_co_mutex_unlock(&s->queue_lock); |
011603ca MK |
769 | } |
770 | ||
33b1db1c MK |
771 | /* |
772 | * Receive responses of the I/O requests. | |
773 | * | |
774 | * This function is registered as a fd handler, and called from the | |
775 | * main loop when s->fd is ready for reading responses. | |
776 | */ | |
d8716b41 | 777 | static void coroutine_fn aio_read_response(void *opaque) |
33b1db1c MK |
778 | { |
779 | SheepdogObjRsp rsp; | |
780 | BDRVSheepdogState *s = opaque; | |
781 | int fd = s->fd; | |
782 | int ret; | |
783 | AIOReq *aio_req = NULL; | |
784 | SheepdogAIOCB *acb; | |
cac8f4a6 | 785 | uint64_t idx; |
33b1db1c | 786 | |
33b1db1c | 787 | /* read a header */ |
8c5135f9 | 788 | ret = qemu_co_recv(fd, &rsp, sizeof(rsp)); |
80731d9d | 789 | if (ret != sizeof(rsp)) { |
6daf194d | 790 | error_report("failed to get the header, %s", strerror(errno)); |
011603ca | 791 | goto err; |
33b1db1c MK |
792 | } |
793 | ||
c292ee6a MK |
794 | /* find the right aio_req from the inflight aio list */ |
795 | QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) { | |
33b1db1c MK |
796 | if (aio_req->id == rsp.id) { |
797 | break; | |
798 | } | |
799 | } | |
800 | if (!aio_req) { | |
6daf194d | 801 | error_report("cannot find aio_req %x", rsp.id); |
011603ca | 802 | goto err; |
33b1db1c MK |
803 | } |
804 | ||
805 | acb = aio_req->aiocb; | |
806 | ||
807 | switch (acb->aiocb_type) { | |
808 | case AIOCB_WRITE_UDATA: | |
809 | if (!is_data_obj(aio_req->oid)) { | |
810 | break; | |
811 | } | |
812 | idx = data_oid_to_idx(aio_req->oid); | |
813 | ||
b544c1ab | 814 | if (aio_req->create) { |
33b1db1c MK |
815 | /* |
816 | * If the object is newly created one, we need to update | |
817 | * the vdi object (metadata object). min_dirty_data_idx | |
818 | * and max_dirty_data_idx are changed to include updated | |
819 | * index between them. | |
820 | */ | |
bd751f22 LY |
821 | if (rsp.result == SD_RES_SUCCESS) { |
822 | s->inode.data_vdi_id[idx] = s->inode.vdi_id; | |
498f2140 HM |
823 | acb->max_dirty_data_idx = MAX(idx, acb->max_dirty_data_idx); |
824 | acb->min_dirty_data_idx = MIN(idx, acb->min_dirty_data_idx); | |
bd751f22 | 825 | } |
33b1db1c MK |
826 | } |
827 | break; | |
828 | case AIOCB_READ_UDATA: | |
2fc8ae1d MT |
829 | ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov, |
830 | aio_req->iov_offset, rsp.data_length); | |
80731d9d | 831 | if (ret != rsp.data_length) { |
6daf194d | 832 | error_report("failed to get the data, %s", strerror(errno)); |
011603ca | 833 | goto err; |
33b1db1c MK |
834 | } |
835 | break; | |
47783072 LY |
836 | case AIOCB_FLUSH_CACHE: |
837 | if (rsp.result == SD_RES_INVALID_PARMS) { | |
70018a14 | 838 | trace_sheepdog_aio_read_response(); |
47783072 LY |
839 | s->cache_flags = SD_FLAG_CMD_DIRECT; |
840 | rsp.result = SD_RES_SUCCESS; | |
841 | } | |
842 | break; | |
cac8f4a6 LY |
843 | case AIOCB_DISCARD_OBJ: |
844 | switch (rsp.result) { | |
845 | case SD_RES_INVALID_PARMS: | |
8ecc2f9e | 846 | error_report("server doesn't support discard command"); |
cac8f4a6 LY |
847 | rsp.result = SD_RES_SUCCESS; |
848 | s->discard_supported = false; | |
849 | break; | |
cac8f4a6 LY |
850 | default: |
851 | break; | |
852 | } | |
33b1db1c MK |
853 | } |
854 | ||
e80ab33d PB |
855 | /* No more data for this aio_req (reload_inode below uses its own file |
856 | * descriptor handler which doesn't use co_recv). | |
857 | */ | |
858 | s->co_recv = NULL; | |
859 | ||
f1af3251 | 860 | qemu_co_mutex_lock(&s->queue_lock); |
c4080e93 | 861 | QLIST_REMOVE(aio_req, aio_siblings); |
f1af3251 PB |
862 | qemu_co_mutex_unlock(&s->queue_lock); |
863 | ||
13c31de2 MK |
864 | switch (rsp.result) { |
865 | case SD_RES_SUCCESS: | |
866 | break; | |
867 | case SD_RES_READONLY: | |
72e0996c MK |
868 | if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) { |
869 | ret = reload_inode(s, 0, ""); | |
870 | if (ret < 0) { | |
011603ca | 871 | goto err; |
72e0996c MK |
872 | } |
873 | } | |
72e0996c MK |
874 | if (is_data_obj(aio_req->oid)) { |
875 | aio_req->oid = vid_to_data_oid(s->inode.vdi_id, | |
876 | data_oid_to_idx(aio_req->oid)); | |
877 | } else { | |
878 | aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id); | |
879 | } | |
a37dcdf9 | 880 | resend_aioreq(s, aio_req); |
e80ab33d | 881 | return; |
13c31de2 | 882 | default: |
33b1db1c | 883 | acb->ret = -EIO; |
6daf194d | 884 | error_report("%s", sd_strerror(rsp.result)); |
13c31de2 | 885 | break; |
33b1db1c MK |
886 | } |
887 | ||
c4080e93 PB |
888 | g_free(aio_req); |
889 | ||
890 | if (!--acb->nr_pending) { | |
33b1db1c MK |
891 | /* |
892 | * We've finished all requests which belong to the AIOCB, so | |
2df46246 | 893 | * we can switch back to sd_co_readv/writev now. |
33b1db1c | 894 | */ |
9d456654 | 895 | aio_co_wake(acb->coroutine); |
33b1db1c | 896 | } |
e80ab33d | 897 | |
011603ca | 898 | return; |
e80ab33d | 899 | |
011603ca | 900 | err: |
011603ca | 901 | reconnect_to_sdog(opaque); |
2df46246 MK |
902 | } |
903 | ||
904 | static void co_read_response(void *opaque) | |
905 | { | |
906 | BDRVSheepdogState *s = opaque; | |
907 | ||
908 | if (!s->co_recv) { | |
0b8b8753 | 909 | s->co_recv = qemu_coroutine_create(aio_read_response, opaque); |
2df46246 MK |
910 | } |
911 | ||
5eceb01a | 912 | aio_co_enter(s->aio_context, s->co_recv); |
2df46246 MK |
913 | } |
914 | ||
915 | static void co_write_request(void *opaque) | |
916 | { | |
917 | BDRVSheepdogState *s = opaque; | |
918 | ||
9d456654 | 919 | aio_co_wake(s->co_send); |
33b1db1c MK |
920 | } |
921 | ||
33b1db1c | 922 | /* |
dc6fb73d | 923 | * Return a socket descriptor to read/write objects. |
33b1db1c | 924 | * |
dc6fb73d | 925 | * We cannot use this descriptor for other operations because |
33b1db1c MK |
926 | * the block driver may be on waiting response from the server. |
927 | */ | |
356b4ca2 | 928 | static int get_sheep_fd(BDRVSheepdogState *s, Error **errp) |
33b1db1c | 929 | { |
1b8bbb46 | 930 | int fd; |
33b1db1c | 931 | |
356b4ca2 | 932 | fd = connect_to_sdog(s, errp); |
33b1db1c | 933 | if (fd < 0) { |
cb595887 | 934 | return fd; |
33b1db1c MK |
935 | } |
936 | ||
dca21ef2 | 937 | aio_set_fd_handler(s->aio_context, fd, false, |
f6a51c84 | 938 | co_read_response, NULL, NULL, s); |
33b1db1c MK |
939 | return fd; |
940 | } | |
941 | ||
89e2a31d MA |
942 | /* |
943 | * Parse numeric snapshot ID in @str | |
944 | * If @str can't be parsed as number, return false. | |
945 | * Else, if the number is zero or too large, set *@snapid to zero and | |
946 | * return true. | |
947 | * Else, set *@snapid to the number and return true. | |
948 | */ | |
949 | static bool sd_parse_snapid(const char *str, uint32_t *snapid) | |
950 | { | |
951 | unsigned long ul; | |
952 | int ret; | |
953 | ||
954 | ret = qemu_strtoul(str, NULL, 10, &ul); | |
955 | if (ret == -ERANGE) { | |
956 | ul = ret = 0; | |
957 | } | |
958 | if (ret) { | |
959 | return false; | |
960 | } | |
961 | if (ul > UINT32_MAX) { | |
962 | ul = 0; | |
963 | } | |
964 | ||
965 | *snapid = ul; | |
966 | return true; | |
967 | } | |
968 | ||
969 | static bool sd_parse_snapid_or_tag(const char *str, | |
970 | uint32_t *snapid, char tag[]) | |
971 | { | |
972 | if (!sd_parse_snapid(str, snapid)) { | |
973 | *snapid = 0; | |
974 | if (g_strlcpy(tag, str, SD_MAX_VDI_TAG_LEN) >= SD_MAX_VDI_TAG_LEN) { | |
975 | return false; | |
976 | } | |
977 | } else if (!*snapid) { | |
978 | return false; | |
979 | } else { | |
980 | tag[0] = 0; | |
981 | } | |
982 | return true; | |
983 | } | |
984 | ||
831acdc9 MA |
985 | typedef struct { |
986 | const char *path; /* non-null iff transport is tcp */ | |
987 | const char *host; /* valid when transport is tcp */ | |
988 | int port; /* valid when transport is tcp */ | |
989 | char vdi[SD_MAX_VDI_LEN]; | |
990 | char tag[SD_MAX_VDI_TAG_LEN]; | |
991 | uint32_t snap_id; | |
992 | /* Remainder is only for sd_config_done() */ | |
993 | URI *uri; | |
994 | QueryParams *qp; | |
995 | } SheepdogConfig; | |
996 | ||
997 | static void sd_config_done(SheepdogConfig *cfg) | |
998 | { | |
999 | if (cfg->qp) { | |
1000 | query_params_free(cfg->qp); | |
1001 | } | |
1002 | uri_free(cfg->uri); | |
1003 | } | |
1004 | ||
1005 | static void sd_parse_uri(SheepdogConfig *cfg, const char *filename, | |
36bcac16 | 1006 | Error **errp) |
5d6768e3 | 1007 | { |
36bcac16 | 1008 | Error *err = NULL; |
5d6768e3 | 1009 | QueryParams *qp = NULL; |
8ecc2f9e MA |
1010 | bool is_unix; |
1011 | URI *uri; | |
5d6768e3 | 1012 | |
831acdc9 MA |
1013 | memset(cfg, 0, sizeof(*cfg)); |
1014 | ||
1015 | cfg->uri = uri = uri_parse(filename); | |
5d6768e3 | 1016 | if (!uri) { |
44acd46f | 1017 | error_setg(&err, "invalid URI '%s'", filename); |
36bcac16 | 1018 | goto out; |
5d6768e3 MK |
1019 | } |
1020 | ||
1b8bbb46 | 1021 | /* transport */ |
f69165a8 | 1022 | if (!g_strcmp0(uri->scheme, "sheepdog")) { |
8ecc2f9e | 1023 | is_unix = false; |
f69165a8 | 1024 | } else if (!g_strcmp0(uri->scheme, "sheepdog+tcp")) { |
8ecc2f9e | 1025 | is_unix = false; |
f69165a8 | 1026 | } else if (!g_strcmp0(uri->scheme, "sheepdog+unix")) { |
8ecc2f9e | 1027 | is_unix = true; |
1b8bbb46 | 1028 | } else { |
36bcac16 MA |
1029 | error_setg(&err, "URI scheme must be 'sheepdog', 'sheepdog+tcp'," |
1030 | " or 'sheepdog+unix'"); | |
1b8bbb46 MK |
1031 | goto out; |
1032 | } | |
1033 | ||
5d6768e3 | 1034 | if (uri->path == NULL || !strcmp(uri->path, "/")) { |
36bcac16 | 1035 | error_setg(&err, "missing file path in URI"); |
5d6768e3 MK |
1036 | goto out; |
1037 | } | |
831acdc9 MA |
1038 | if (g_strlcpy(cfg->vdi, uri->path + 1, SD_MAX_VDI_LEN) |
1039 | >= SD_MAX_VDI_LEN) { | |
36bcac16 | 1040 | error_setg(&err, "VDI name is too long"); |
daa0b0d4 MA |
1041 | goto out; |
1042 | } | |
5d6768e3 | 1043 | |
831acdc9 | 1044 | cfg->qp = qp = query_params_parse(uri->query); |
1b8bbb46 | 1045 | |
8ecc2f9e | 1046 | if (is_unix) { |
1b8bbb46 | 1047 | /* sheepdog+unix:///vdiname?socket=path */ |
36bcac16 MA |
1048 | if (uri->server || uri->port) { |
1049 | error_setg(&err, "URI scheme %s doesn't accept a server address", | |
1050 | uri->scheme); | |
1051 | goto out; | |
1052 | } | |
1053 | if (!qp->n) { | |
1054 | error_setg(&err, | |
1055 | "URI scheme %s requires query parameter 'socket'", | |
1056 | uri->scheme); | |
1057 | goto out; | |
1058 | } | |
1059 | if (qp->n != 1 || strcmp(qp->p[0].name, "socket")) { | |
1060 | error_setg(&err, "unexpected query parameters"); | |
1b8bbb46 MK |
1061 | goto out; |
1062 | } | |
831acdc9 | 1063 | cfg->path = qp->p[0].value; |
1b8bbb46 MK |
1064 | } else { |
1065 | /* sheepdog[+tcp]://[host:port]/vdiname */ | |
36bcac16 MA |
1066 | if (qp->n) { |
1067 | error_setg(&err, "unexpected query parameters"); | |
1068 | goto out; | |
1069 | } | |
831acdc9 MA |
1070 | cfg->host = uri->server; |
1071 | cfg->port = uri->port; | |
1b8bbb46 | 1072 | } |
5d6768e3 MK |
1073 | |
1074 | /* snapshot tag */ | |
1075 | if (uri->fragment) { | |
831acdc9 MA |
1076 | if (!sd_parse_snapid_or_tag(uri->fragment, |
1077 | &cfg->snap_id, cfg->tag)) { | |
36bcac16 MA |
1078 | error_setg(&err, "'%s' is not a valid snapshot ID", |
1079 | uri->fragment); | |
89e2a31d | 1080 | goto out; |
5d6768e3 MK |
1081 | } |
1082 | } else { | |
831acdc9 | 1083 | cfg->snap_id = CURRENT_VDI_ID; /* search current vdi */ |
5d6768e3 MK |
1084 | } |
1085 | ||
1086 | out: | |
8ecc2f9e MA |
1087 | if (err) { |
1088 | error_propagate(errp, err); | |
831acdc9 | 1089 | sd_config_done(cfg); |
5d6768e3 | 1090 | } |
5d6768e3 MK |
1091 | } |
1092 | ||
33b1db1c | 1093 | /* |
5d6768e3 | 1094 | * Parse a filename (old syntax) |
33b1db1c MK |
1095 | * |
1096 | * filename must be one of the following formats: | |
1097 | * 1. [vdiname] | |
1098 | * 2. [vdiname]:[snapid] | |
1099 | * 3. [vdiname]:[tag] | |
1100 | * 4. [hostname]:[port]:[vdiname] | |
1101 | * 5. [hostname]:[port]:[vdiname]:[snapid] | |
1102 | * 6. [hostname]:[port]:[vdiname]:[tag] | |
1103 | * | |
1104 | * You can boot from the snapshot images by specifying `snapid` or | |
1105 | * `tag'. | |
1106 | * | |
1107 | * You can run VMs outside the Sheepdog cluster by specifying | |
1108 | * `hostname' and `port' (experimental). | |
1109 | */ | |
831acdc9 | 1110 | static void parse_vdiname(SheepdogConfig *cfg, const char *filename, |
36bcac16 | 1111 | Error **errp) |
33b1db1c | 1112 | { |
36bcac16 | 1113 | Error *err = NULL; |
5d6768e3 MK |
1114 | char *p, *q, *uri; |
1115 | const char *host_spec, *vdi_spec; | |
36bcac16 | 1116 | int nr_sep; |
33b1db1c | 1117 | |
11d816a5 | 1118 | strstart(filename, "sheepdog:", &filename); |
7267c094 | 1119 | p = q = g_strdup(filename); |
33b1db1c MK |
1120 | |
1121 | /* count the number of separators */ | |
1122 | nr_sep = 0; | |
1123 | while (*p) { | |
1124 | if (*p == ':') { | |
1125 | nr_sep++; | |
1126 | } | |
1127 | p++; | |
1128 | } | |
1129 | p = q; | |
1130 | ||
5d6768e3 | 1131 | /* use the first two tokens as host_spec. */ |
33b1db1c | 1132 | if (nr_sep >= 2) { |
5d6768e3 | 1133 | host_spec = p; |
33b1db1c | 1134 | p = strchr(p, ':'); |
5d6768e3 | 1135 | p++; |
33b1db1c MK |
1136 | p = strchr(p, ':'); |
1137 | *p++ = '\0'; | |
1138 | } else { | |
5d6768e3 | 1139 | host_spec = ""; |
33b1db1c MK |
1140 | } |
1141 | ||
5d6768e3 | 1142 | vdi_spec = p; |
33b1db1c | 1143 | |
5d6768e3 | 1144 | p = strchr(vdi_spec, ':'); |
33b1db1c | 1145 | if (p) { |
5d6768e3 | 1146 | *p++ = '#'; |
33b1db1c MK |
1147 | } |
1148 | ||
5d6768e3 | 1149 | uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec); |
33b1db1c | 1150 | |
36bcac16 MA |
1151 | /* |
1152 | * FIXME We to escape URI meta-characters, e.g. "x?y=z" | |
1153 | * produces "sheepdog://x?y=z". Because of that ... | |
1154 | */ | |
831acdc9 | 1155 | sd_parse_uri(cfg, uri, &err); |
36bcac16 MA |
1156 | if (err) { |
1157 | /* | |
1158 | * ... this can fail, but the error message is misleading. | |
1159 | * Replace it by the traditional useless one until the | |
1160 | * escaping is fixed. | |
1161 | */ | |
1162 | error_free(err); | |
1163 | error_setg(errp, "Can't parse filename"); | |
1164 | } | |
5d6768e3 MK |
1165 | |
1166 | g_free(q); | |
1167 | g_free(uri); | |
33b1db1c MK |
1168 | } |
1169 | ||
831acdc9 MA |
1170 | static void sd_parse_filename(const char *filename, QDict *options, |
1171 | Error **errp) | |
1172 | { | |
1173 | Error *err = NULL; | |
1174 | SheepdogConfig cfg; | |
1175 | char buf[32]; | |
1176 | ||
1177 | if (strstr(filename, "://")) { | |
1178 | sd_parse_uri(&cfg, filename, &err); | |
1179 | } else { | |
1180 | parse_vdiname(&cfg, filename, &err); | |
1181 | } | |
1182 | if (err) { | |
1183 | error_propagate(errp, err); | |
1184 | return; | |
1185 | } | |
1186 | ||
831acdc9 | 1187 | if (cfg.path) { |
d1c13688 MA |
1188 | qdict_set_default_str(options, "server.path", cfg.path); |
1189 | qdict_set_default_str(options, "server.type", "unix"); | |
1190 | } else { | |
1191 | qdict_set_default_str(options, "server.type", "inet"); | |
1192 | qdict_set_default_str(options, "server.host", | |
1193 | cfg.host ?: SD_DEFAULT_ADDR); | |
1194 | snprintf(buf, sizeof(buf), "%d", cfg.port ?: SD_DEFAULT_PORT); | |
1195 | qdict_set_default_str(options, "server.port", buf); | |
831acdc9 MA |
1196 | } |
1197 | qdict_set_default_str(options, "vdi", cfg.vdi); | |
1198 | qdict_set_default_str(options, "tag", cfg.tag); | |
1199 | if (cfg.snap_id) { | |
1200 | snprintf(buf, sizeof(buf), "%d", cfg.snap_id); | |
1201 | qdict_set_default_str(options, "snap-id", buf); | |
1202 | } | |
1203 | ||
1204 | sd_config_done(&cfg); | |
1205 | } | |
1206 | ||
982dcbf4 MK |
1207 | static int find_vdi_name(BDRVSheepdogState *s, const char *filename, |
1208 | uint32_t snapid, const char *tag, uint32_t *vid, | |
dc83cd42 | 1209 | bool lock, Error **errp) |
33b1db1c MK |
1210 | { |
1211 | int ret, fd; | |
1212 | SheepdogVdiReq hdr; | |
1213 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1214 | unsigned int wlen, rlen = 0; | |
97b583f4 | 1215 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] QEMU_NONSTRING; |
33b1db1c | 1216 | |
dc83cd42 | 1217 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1218 | if (fd < 0) { |
cb595887 | 1219 | return fd; |
33b1db1c MK |
1220 | } |
1221 | ||
3178e275 JM |
1222 | /* This pair of strncpy calls ensures that the buffer is zero-filled, |
1223 | * which is desirable since we'll soon be sending those bytes, and | |
1224 | * don't want the send_req to read uninitialized data. | |
1225 | */ | |
33b1db1c MK |
1226 | strncpy(buf, filename, SD_MAX_VDI_LEN); |
1227 | strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN); | |
1228 | ||
1229 | memset(&hdr, 0, sizeof(hdr)); | |
982dcbf4 | 1230 | if (lock) { |
33b1db1c | 1231 | hdr.opcode = SD_OP_LOCK_VDI; |
1dbfafed | 1232 | hdr.type = LOCK_TYPE_NORMAL; |
982dcbf4 MK |
1233 | } else { |
1234 | hdr.opcode = SD_OP_GET_VDI_INFO; | |
33b1db1c MK |
1235 | } |
1236 | wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN; | |
1237 | hdr.proto_ver = SD_PROTO_VER; | |
1238 | hdr.data_length = wlen; | |
1239 | hdr.snapid = snapid; | |
1240 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1241 | ||
f11672db | 1242 | ret = do_req(fd, s->bs, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
33b1db1c | 1243 | if (ret) { |
dc83cd42 | 1244 | error_setg_errno(errp, -ret, "cannot get vdi info"); |
33b1db1c MK |
1245 | goto out; |
1246 | } | |
1247 | ||
1248 | if (rsp->result != SD_RES_SUCCESS) { | |
dc83cd42 MA |
1249 | error_setg(errp, "cannot get vdi info, %s, %s %" PRIu32 " %s", |
1250 | sd_strerror(rsp->result), filename, snapid, tag); | |
cb595887 MK |
1251 | if (rsp->result == SD_RES_NO_VDI) { |
1252 | ret = -ENOENT; | |
38890b24 HM |
1253 | } else if (rsp->result == SD_RES_VDI_LOCKED) { |
1254 | ret = -EBUSY; | |
cb595887 MK |
1255 | } else { |
1256 | ret = -EIO; | |
1257 | } | |
33b1db1c MK |
1258 | goto out; |
1259 | } | |
1260 | *vid = rsp->vdi_id; | |
1261 | ||
1262 | ret = 0; | |
1263 | out: | |
1264 | closesocket(fd); | |
1265 | return ret; | |
1266 | } | |
1267 | ||
a37dcdf9 | 1268 | static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, |
b544c1ab HM |
1269 | struct iovec *iov, int niov, |
1270 | enum AIOCBState aiocb_type) | |
33b1db1c MK |
1271 | { |
1272 | int nr_copies = s->inode.nr_copies; | |
1273 | SheepdogObjReq hdr; | |
47783072 | 1274 | unsigned int wlen = 0; |
33b1db1c MK |
1275 | int ret; |
1276 | uint64_t oid = aio_req->oid; | |
1277 | unsigned int datalen = aio_req->data_len; | |
1278 | uint64_t offset = aio_req->offset; | |
1279 | uint8_t flags = aio_req->flags; | |
1280 | uint64_t old_oid = aio_req->base_oid; | |
b544c1ab | 1281 | bool create = aio_req->create; |
33b1db1c | 1282 | |
f1af3251 | 1283 | qemu_co_mutex_lock(&s->queue_lock); |
c4080e93 | 1284 | QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); |
f1af3251 | 1285 | qemu_co_mutex_unlock(&s->queue_lock); |
c4080e93 | 1286 | |
33b1db1c | 1287 | if (!nr_copies) { |
6daf194d | 1288 | error_report("bug"); |
33b1db1c MK |
1289 | } |
1290 | ||
1291 | memset(&hdr, 0, sizeof(hdr)); | |
1292 | ||
47783072 LY |
1293 | switch (aiocb_type) { |
1294 | case AIOCB_FLUSH_CACHE: | |
1295 | hdr.opcode = SD_OP_FLUSH_VDI; | |
1296 | break; | |
1297 | case AIOCB_READ_UDATA: | |
33b1db1c MK |
1298 | hdr.opcode = SD_OP_READ_OBJ; |
1299 | hdr.flags = flags; | |
47783072 LY |
1300 | break; |
1301 | case AIOCB_WRITE_UDATA: | |
1302 | if (create) { | |
1303 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1304 | } else { | |
1305 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1306 | } | |
33b1db1c | 1307 | wlen = datalen; |
33b1db1c | 1308 | hdr.flags = SD_FLAG_CMD_WRITE | flags; |
47783072 | 1309 | break; |
cac8f4a6 | 1310 | case AIOCB_DISCARD_OBJ: |
e6fd57ea HM |
1311 | hdr.opcode = SD_OP_WRITE_OBJ; |
1312 | hdr.flags = SD_FLAG_CMD_WRITE | flags; | |
1313 | s->inode.data_vdi_id[data_oid_to_idx(oid)] = 0; | |
1314 | offset = offsetof(SheepdogInode, | |
1315 | data_vdi_id[data_oid_to_idx(oid)]); | |
1316 | oid = vid_to_vdi_oid(s->inode.vdi_id); | |
1317 | wlen = datalen = sizeof(uint32_t); | |
cac8f4a6 | 1318 | break; |
33b1db1c MK |
1319 | } |
1320 | ||
0e7106d8 LY |
1321 | if (s->cache_flags) { |
1322 | hdr.flags |= s->cache_flags; | |
47622c44 LY |
1323 | } |
1324 | ||
33b1db1c MK |
1325 | hdr.oid = oid; |
1326 | hdr.cow_oid = old_oid; | |
1327 | hdr.copies = s->inode.nr_copies; | |
1328 | ||
1329 | hdr.data_length = datalen; | |
1330 | hdr.offset = offset; | |
1331 | ||
1332 | hdr.id = aio_req->id; | |
1333 | ||
2df46246 MK |
1334 | qemu_co_mutex_lock(&s->lock); |
1335 | s->co_send = qemu_coroutine_self(); | |
dca21ef2 | 1336 | aio_set_fd_handler(s->aio_context, s->fd, false, |
f6a51c84 | 1337 | co_read_response, co_write_request, NULL, s); |
128aa589 | 1338 | socket_set_cork(s->fd, 1); |
33b1db1c MK |
1339 | |
1340 | /* send a header */ | |
8c5135f9 | 1341 | ret = qemu_co_send(s->fd, &hdr, sizeof(hdr)); |
80731d9d | 1342 | if (ret != sizeof(hdr)) { |
6daf194d | 1343 | error_report("failed to send a req, %s", strerror(errno)); |
011603ca | 1344 | goto out; |
33b1db1c MK |
1345 | } |
1346 | ||
1347 | if (wlen) { | |
2fc8ae1d | 1348 | ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen); |
80731d9d | 1349 | if (ret != wlen) { |
6daf194d | 1350 | error_report("failed to send a data, %s", strerror(errno)); |
33b1db1c MK |
1351 | } |
1352 | } | |
011603ca | 1353 | out: |
128aa589 | 1354 | socket_set_cork(s->fd, 0); |
dca21ef2 | 1355 | aio_set_fd_handler(s->aio_context, s->fd, false, |
f6a51c84 | 1356 | co_read_response, NULL, NULL, s); |
011603ca | 1357 | s->co_send = NULL; |
2df46246 | 1358 | qemu_co_mutex_unlock(&s->lock); |
33b1db1c MK |
1359 | } |
1360 | ||
f11672db | 1361 | static int read_write_object(int fd, BlockDriverState *bs, char *buf, |
84390bed | 1362 | uint64_t oid, uint8_t copies, |
33b1db1c | 1363 | unsigned int datalen, uint64_t offset, |
0e7106d8 | 1364 | bool write, bool create, uint32_t cache_flags) |
33b1db1c MK |
1365 | { |
1366 | SheepdogObjReq hdr; | |
1367 | SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr; | |
1368 | unsigned int wlen, rlen; | |
1369 | int ret; | |
1370 | ||
1371 | memset(&hdr, 0, sizeof(hdr)); | |
1372 | ||
1373 | if (write) { | |
1374 | wlen = datalen; | |
1375 | rlen = 0; | |
1376 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1377 | if (create) { | |
1378 | hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ; | |
1379 | } else { | |
1380 | hdr.opcode = SD_OP_WRITE_OBJ; | |
1381 | } | |
1382 | } else { | |
1383 | wlen = 0; | |
1384 | rlen = datalen; | |
1385 | hdr.opcode = SD_OP_READ_OBJ; | |
1386 | } | |
47622c44 | 1387 | |
0e7106d8 | 1388 | hdr.flags |= cache_flags; |
47622c44 | 1389 | |
33b1db1c MK |
1390 | hdr.oid = oid; |
1391 | hdr.data_length = datalen; | |
1392 | hdr.offset = offset; | |
1393 | hdr.copies = copies; | |
1394 | ||
f11672db | 1395 | ret = do_req(fd, bs, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
33b1db1c | 1396 | if (ret) { |
6daf194d | 1397 | error_report("failed to send a request to the sheep"); |
cb595887 | 1398 | return ret; |
33b1db1c MK |
1399 | } |
1400 | ||
1401 | switch (rsp->result) { | |
1402 | case SD_RES_SUCCESS: | |
1403 | return 0; | |
1404 | default: | |
6daf194d | 1405 | error_report("%s", sd_strerror(rsp->result)); |
cb595887 | 1406 | return -EIO; |
33b1db1c MK |
1407 | } |
1408 | } | |
1409 | ||
f11672db | 1410 | static int read_object(int fd, BlockDriverState *bs, char *buf, |
84390bed | 1411 | uint64_t oid, uint8_t copies, |
0e7106d8 LY |
1412 | unsigned int datalen, uint64_t offset, |
1413 | uint32_t cache_flags) | |
33b1db1c | 1414 | { |
f11672db | 1415 | return read_write_object(fd, bs, buf, oid, copies, |
84390bed | 1416 | datalen, offset, false, |
0e7106d8 | 1417 | false, cache_flags); |
33b1db1c MK |
1418 | } |
1419 | ||
f11672db | 1420 | static int write_object(int fd, BlockDriverState *bs, char *buf, |
84390bed | 1421 | uint64_t oid, uint8_t copies, |
2f536801 | 1422 | unsigned int datalen, uint64_t offset, bool create, |
0e7106d8 | 1423 | uint32_t cache_flags) |
33b1db1c | 1424 | { |
f11672db | 1425 | return read_write_object(fd, bs, buf, oid, copies, |
84390bed | 1426 | datalen, offset, true, |
0e7106d8 | 1427 | create, cache_flags); |
33b1db1c MK |
1428 | } |
1429 | ||
9ff53a0e MK |
1430 | /* update inode with the latest state */ |
1431 | static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag) | |
1432 | { | |
dfb12bf8 | 1433 | Error *local_err = NULL; |
9ff53a0e MK |
1434 | SheepdogInode *inode; |
1435 | int ret = 0, fd; | |
1436 | uint32_t vid = 0; | |
1437 | ||
dfb12bf8 | 1438 | fd = connect_to_sdog(s, &local_err); |
9ff53a0e | 1439 | if (fd < 0) { |
565f65d2 | 1440 | error_report_err(local_err); |
9ff53a0e MK |
1441 | return -EIO; |
1442 | } | |
1443 | ||
5d039bab | 1444 | inode = g_malloc(SD_INODE_HEADER_SIZE); |
9ff53a0e | 1445 | |
dc83cd42 | 1446 | ret = find_vdi_name(s, s->name, snapid, tag, &vid, false, &local_err); |
9ff53a0e | 1447 | if (ret) { |
565f65d2 | 1448 | error_report_err(local_err); |
9ff53a0e MK |
1449 | goto out; |
1450 | } | |
1451 | ||
f11672db | 1452 | ret = read_object(fd, s->bs, (char *)inode, vid_to_vdi_oid(vid), |
5d039bab HM |
1453 | s->inode.nr_copies, SD_INODE_HEADER_SIZE, 0, |
1454 | s->cache_flags); | |
9ff53a0e MK |
1455 | if (ret < 0) { |
1456 | goto out; | |
1457 | } | |
1458 | ||
1459 | if (inode->vdi_id != s->inode.vdi_id) { | |
5d039bab | 1460 | memcpy(&s->inode, inode, SD_INODE_HEADER_SIZE); |
9ff53a0e MK |
1461 | } |
1462 | ||
1463 | out: | |
1464 | g_free(inode); | |
1465 | closesocket(fd); | |
1466 | ||
1467 | return ret; | |
1468 | } | |
1469 | ||
a37dcdf9 | 1470 | static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) |
13c31de2 MK |
1471 | { |
1472 | SheepdogAIOCB *acb = aio_req->aiocb; | |
b544c1ab HM |
1473 | |
1474 | aio_req->create = false; | |
13c31de2 MK |
1475 | |
1476 | /* check whether this request becomes a CoW one */ | |
2412aec7 | 1477 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) { |
13c31de2 | 1478 | int idx = data_oid_to_idx(aio_req->oid); |
13c31de2 | 1479 | |
13c31de2 MK |
1480 | if (is_data_obj_writable(&s->inode, idx)) { |
1481 | goto out; | |
1482 | } | |
1483 | ||
80308d33 MK |
1484 | if (s->inode.data_vdi_id[idx]) { |
1485 | aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx); | |
1486 | aio_req->flags |= SD_FLAG_CMD_COW; | |
1487 | } | |
b544c1ab | 1488 | aio_req->create = true; |
13c31de2 MK |
1489 | } |
1490 | out: | |
2412aec7 | 1491 | if (is_data_obj(aio_req->oid)) { |
b544c1ab | 1492 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, |
a37dcdf9 | 1493 | acb->aiocb_type); |
2412aec7 MK |
1494 | } else { |
1495 | struct iovec iov; | |
1496 | iov.iov_base = &s->inode; | |
1497 | iov.iov_len = sizeof(s->inode); | |
b544c1ab | 1498 | add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA); |
2412aec7 | 1499 | } |
13c31de2 MK |
1500 | } |
1501 | ||
84390bed SH |
1502 | static void sd_detach_aio_context(BlockDriverState *bs) |
1503 | { | |
1504 | BDRVSheepdogState *s = bs->opaque; | |
1505 | ||
dca21ef2 | 1506 | aio_set_fd_handler(s->aio_context, s->fd, false, NULL, |
f6a51c84 | 1507 | NULL, NULL, NULL); |
84390bed SH |
1508 | } |
1509 | ||
1510 | static void sd_attach_aio_context(BlockDriverState *bs, | |
1511 | AioContext *new_context) | |
1512 | { | |
1513 | BDRVSheepdogState *s = bs->opaque; | |
1514 | ||
1515 | s->aio_context = new_context; | |
dca21ef2 | 1516 | aio_set_fd_handler(new_context, s->fd, false, |
f6a51c84 | 1517 | co_read_response, NULL, NULL, s); |
84390bed SH |
1518 | } |
1519 | ||
c8c96350 KW |
1520 | static QemuOptsList runtime_opts = { |
1521 | .name = "sheepdog", | |
1522 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1523 | .desc = { | |
831acdc9 MA |
1524 | { |
1525 | .name = "vdi", | |
1526 | .type = QEMU_OPT_STRING, | |
1527 | }, | |
1528 | { | |
1529 | .name = "snap-id", | |
1530 | .type = QEMU_OPT_NUMBER, | |
1531 | }, | |
1532 | { | |
1533 | .name = "tag", | |
c8c96350 | 1534 | .type = QEMU_OPT_STRING, |
c8c96350 KW |
1535 | }, |
1536 | { /* end of list */ } | |
1537 | }, | |
1538 | }; | |
1539 | ||
015a1036 HR |
1540 | static int sd_open(BlockDriverState *bs, QDict *options, int flags, |
1541 | Error **errp) | |
33b1db1c MK |
1542 | { |
1543 | int ret, fd; | |
1544 | uint32_t vid = 0; | |
1545 | BDRVSheepdogState *s = bs->opaque; | |
d1c13688 | 1546 | const char *vdi, *snap_id_str, *tag; |
831acdc9 | 1547 | uint64_t snap_id; |
33b1db1c | 1548 | char *buf = NULL; |
c8c96350 KW |
1549 | QemuOpts *opts; |
1550 | Error *local_err = NULL; | |
c8c96350 | 1551 | |
011603ca | 1552 | s->bs = bs; |
84390bed | 1553 | s->aio_context = bdrv_get_aio_context(bs); |
011603ca | 1554 | |
87ea75d5 | 1555 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
c8c96350 | 1556 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 1557 | if (local_err) { |
e67c3993 | 1558 | error_propagate(errp, local_err); |
c8c96350 | 1559 | ret = -EINVAL; |
cbc488ee | 1560 | goto err_no_fd; |
c8c96350 KW |
1561 | } |
1562 | ||
d1c13688 MA |
1563 | s->addr = sd_server_config(options, errp); |
1564 | if (!s->addr) { | |
1565 | ret = -EINVAL; | |
1566 | goto err_no_fd; | |
1567 | } | |
1568 | ||
831acdc9 MA |
1569 | vdi = qemu_opt_get(opts, "vdi"); |
1570 | snap_id_str = qemu_opt_get(opts, "snap-id"); | |
1571 | snap_id = qemu_opt_get_number(opts, "snap-id", CURRENT_VDI_ID); | |
1572 | tag = qemu_opt_get(opts, "tag"); | |
33b1db1c | 1573 | |
831acdc9 MA |
1574 | if (!vdi) { |
1575 | error_setg(errp, "parameter 'vdi' is missing"); | |
1576 | ret = -EINVAL; | |
1577 | goto err_no_fd; | |
1578 | } | |
1579 | if (strlen(vdi) >= SD_MAX_VDI_LEN) { | |
1580 | error_setg(errp, "value of parameter 'vdi' is too long"); | |
1581 | ret = -EINVAL; | |
1582 | goto err_no_fd; | |
1583 | } | |
33b1db1c | 1584 | |
831acdc9 MA |
1585 | if (snap_id > UINT32_MAX) { |
1586 | snap_id = 0; | |
1587 | } | |
1588 | if (snap_id_str && !snap_id) { | |
1589 | error_setg(errp, "'snap-id=%s' is not a valid snapshot ID", | |
1590 | snap_id_str); | |
1591 | ret = -EINVAL; | |
1592 | goto err_no_fd; | |
1593 | } | |
5d6768e3 | 1594 | |
831acdc9 MA |
1595 | if (!tag) { |
1596 | tag = ""; | |
5d6768e3 | 1597 | } |
ac90dad9 | 1598 | if (strlen(tag) >= SD_MAX_VDI_TAG_LEN) { |
831acdc9 | 1599 | error_setg(errp, "value of parameter 'tag' is too long"); |
36bcac16 | 1600 | ret = -EINVAL; |
cbc488ee | 1601 | goto err_no_fd; |
33b1db1c | 1602 | } |
831acdc9 | 1603 | |
831acdc9 MA |
1604 | QLIST_INIT(&s->inflight_aio_head); |
1605 | QLIST_INIT(&s->failed_aio_head); | |
1606 | QLIST_INIT(&s->inflight_aiocb_head); | |
1607 | ||
e67c3993 | 1608 | s->fd = get_sheep_fd(s, errp); |
33b1db1c | 1609 | if (s->fd < 0) { |
cb595887 | 1610 | ret = s->fd; |
cbc488ee | 1611 | goto err_no_fd; |
33b1db1c MK |
1612 | } |
1613 | ||
831acdc9 | 1614 | ret = find_vdi_name(s, vdi, (uint32_t)snap_id, tag, &vid, true, errp); |
33b1db1c | 1615 | if (ret) { |
cbc488ee | 1616 | goto err; |
33b1db1c MK |
1617 | } |
1618 | ||
0e7106d8 LY |
1619 | /* |
1620 | * QEMU block layer emulates writethrough cache as 'writeback + flush', so | |
1621 | * we always set SD_FLAG_CMD_CACHE (writeback cache) as default. | |
1622 | */ | |
1623 | s->cache_flags = SD_FLAG_CMD_CACHE; | |
1624 | if (flags & BDRV_O_NOCACHE) { | |
1625 | s->cache_flags = SD_FLAG_CMD_DIRECT; | |
1626 | } | |
cac8f4a6 | 1627 | s->discard_supported = true; |
0e7106d8 | 1628 | |
831acdc9 | 1629 | if (snap_id || tag[0]) { |
70018a14 | 1630 | trace_sheepdog_open(vid); |
2f536801 | 1631 | s->is_snapshot = true; |
33b1db1c MK |
1632 | } |
1633 | ||
e67c3993 | 1634 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1635 | if (fd < 0) { |
cb595887 | 1636 | ret = fd; |
cbc488ee | 1637 | goto err; |
33b1db1c MK |
1638 | } |
1639 | ||
7267c094 | 1640 | buf = g_malloc(SD_INODE_SIZE); |
f11672db | 1641 | ret = read_object(fd, s->bs, buf, vid_to_vdi_oid(vid), |
84390bed | 1642 | 0, SD_INODE_SIZE, 0, s->cache_flags); |
33b1db1c MK |
1643 | |
1644 | closesocket(fd); | |
1645 | ||
1646 | if (ret) { | |
efde4b62 | 1647 | error_setg(errp, "Can't read snapshot inode"); |
cbc488ee | 1648 | goto err; |
33b1db1c MK |
1649 | } |
1650 | ||
1651 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
33b1db1c | 1652 | |
e8bfaa2f | 1653 | bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE; |
3178e275 | 1654 | pstrcpy(s->name, sizeof(s->name), vdi); |
2df46246 | 1655 | qemu_co_mutex_init(&s->lock); |
f1af3251 | 1656 | qemu_co_mutex_init(&s->queue_lock); |
498f2140 | 1657 | qemu_co_queue_init(&s->overlapping_queue); |
c8c96350 | 1658 | qemu_opts_del(opts); |
7267c094 | 1659 | g_free(buf); |
33b1db1c | 1660 | return 0; |
cbc488ee MA |
1661 | |
1662 | err: | |
dca21ef2 | 1663 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd, |
f6a51c84 | 1664 | false, NULL, NULL, NULL, NULL); |
cbc488ee MA |
1665 | closesocket(s->fd); |
1666 | err_no_fd: | |
c8c96350 | 1667 | qemu_opts_del(opts); |
7267c094 | 1668 | g_free(buf); |
cb595887 | 1669 | return ret; |
33b1db1c MK |
1670 | } |
1671 | ||
4da65c80 LY |
1672 | static int sd_reopen_prepare(BDRVReopenState *state, BlockReopenQueue *queue, |
1673 | Error **errp) | |
1674 | { | |
1675 | BDRVSheepdogState *s = state->bs->opaque; | |
1676 | BDRVSheepdogReopenState *re_s; | |
1677 | int ret = 0; | |
1678 | ||
1679 | re_s = state->opaque = g_new0(BDRVSheepdogReopenState, 1); | |
1680 | ||
1681 | re_s->cache_flags = SD_FLAG_CMD_CACHE; | |
1682 | if (state->flags & BDRV_O_NOCACHE) { | |
1683 | re_s->cache_flags = SD_FLAG_CMD_DIRECT; | |
1684 | } | |
1685 | ||
1686 | re_s->fd = get_sheep_fd(s, errp); | |
1687 | if (re_s->fd < 0) { | |
1688 | ret = re_s->fd; | |
1689 | return ret; | |
1690 | } | |
1691 | ||
1692 | return ret; | |
1693 | } | |
1694 | ||
1695 | static void sd_reopen_commit(BDRVReopenState *state) | |
1696 | { | |
1697 | BDRVSheepdogReopenState *re_s = state->opaque; | |
1698 | BDRVSheepdogState *s = state->bs->opaque; | |
1699 | ||
1700 | if (s->fd) { | |
dca21ef2 | 1701 | aio_set_fd_handler(s->aio_context, s->fd, false, |
f6a51c84 | 1702 | NULL, NULL, NULL, NULL); |
4da65c80 LY |
1703 | closesocket(s->fd); |
1704 | } | |
1705 | ||
1706 | s->fd = re_s->fd; | |
1707 | s->cache_flags = re_s->cache_flags; | |
1708 | ||
1709 | g_free(state->opaque); | |
1710 | state->opaque = NULL; | |
1711 | ||
1712 | return; | |
1713 | } | |
1714 | ||
1715 | static void sd_reopen_abort(BDRVReopenState *state) | |
1716 | { | |
1717 | BDRVSheepdogReopenState *re_s = state->opaque; | |
1718 | BDRVSheepdogState *s = state->bs->opaque; | |
1719 | ||
1720 | if (re_s == NULL) { | |
1721 | return; | |
1722 | } | |
1723 | ||
1724 | if (re_s->fd) { | |
dca21ef2 | 1725 | aio_set_fd_handler(s->aio_context, re_s->fd, false, |
f6a51c84 | 1726 | NULL, NULL, NULL, NULL); |
4da65c80 LY |
1727 | closesocket(re_s->fd); |
1728 | } | |
1729 | ||
1730 | g_free(state->opaque); | |
1731 | state->opaque = NULL; | |
1732 | ||
1733 | return; | |
1734 | } | |
1735 | ||
7d2d3e74 MA |
1736 | static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot, |
1737 | Error **errp) | |
33b1db1c MK |
1738 | { |
1739 | SheepdogVdiReq hdr; | |
1740 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
1741 | int fd, ret; | |
1742 | unsigned int wlen, rlen = 0; | |
1743 | char buf[SD_MAX_VDI_LEN]; | |
1744 | ||
7d2d3e74 | 1745 | fd = connect_to_sdog(s, errp); |
33b1db1c | 1746 | if (fd < 0) { |
cb595887 | 1747 | return fd; |
33b1db1c MK |
1748 | } |
1749 | ||
3178e275 JM |
1750 | /* FIXME: would it be better to fail (e.g., return -EIO) when filename |
1751 | * does not fit in buf? For now, just truncate and avoid buffer overrun. | |
1752 | */ | |
33b1db1c | 1753 | memset(buf, 0, sizeof(buf)); |
c31d482f | 1754 | pstrcpy(buf, sizeof(buf), s->name); |
33b1db1c MK |
1755 | |
1756 | memset(&hdr, 0, sizeof(hdr)); | |
1757 | hdr.opcode = SD_OP_NEW_VDI; | |
9f23fce7 | 1758 | hdr.base_vdi_id = s->inode.vdi_id; |
33b1db1c MK |
1759 | |
1760 | wlen = SD_MAX_VDI_LEN; | |
1761 | ||
1762 | hdr.flags = SD_FLAG_CMD_WRITE; | |
1763 | hdr.snapid = snapshot; | |
1764 | ||
1765 | hdr.data_length = wlen; | |
c31d482f LY |
1766 | hdr.vdi_size = s->inode.vdi_size; |
1767 | hdr.copy_policy = s->inode.copy_policy; | |
b3af018f | 1768 | hdr.copies = s->inode.nr_copies; |
876eb1b0 | 1769 | hdr.block_size_shift = s->inode.block_size_shift; |
33b1db1c | 1770 | |
f11672db | 1771 | ret = do_req(fd, NULL, (SheepdogReq *)&hdr, buf, &wlen, &rlen); |
33b1db1c MK |
1772 | |
1773 | closesocket(fd); | |
1774 | ||
1775 | if (ret) { | |
7d2d3e74 | 1776 | error_setg_errno(errp, -ret, "create failed"); |
cb595887 | 1777 | return ret; |
33b1db1c MK |
1778 | } |
1779 | ||
1780 | if (rsp->result != SD_RES_SUCCESS) { | |
7d2d3e74 | 1781 | error_setg(errp, "%s, %s", sd_strerror(rsp->result), s->inode.name); |
33b1db1c MK |
1782 | return -EIO; |
1783 | } | |
1784 | ||
1785 | if (vdi_id) { | |
1786 | *vdi_id = rsp->vdi_id; | |
1787 | } | |
1788 | ||
1789 | return 0; | |
1790 | } | |
1791 | ||
1a62baf6 HR |
1792 | static int sd_prealloc(BlockDriverState *bs, int64_t old_size, int64_t new_size, |
1793 | Error **errp) | |
a8e0fdd7 | 1794 | { |
fba98d45 | 1795 | BlockBackend *blk = NULL; |
8b9ad56e | 1796 | BDRVSheepdogState *base = bs->opaque; |
876eb1b0 | 1797 | unsigned long buf_size; |
a8e0fdd7 | 1798 | uint32_t idx, max_idx; |
876eb1b0 | 1799 | uint32_t object_size; |
876eb1b0 | 1800 | void *buf = NULL; |
a8e0fdd7 MK |
1801 | int ret; |
1802 | ||
8b9ad56e HR |
1803 | blk = blk_new(BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE, |
1804 | BLK_PERM_ALL); | |
1805 | ||
1806 | ret = blk_insert_bs(blk, bs, errp); | |
1807 | if (ret < 0) { | |
318df29e | 1808 | goto out_with_err_set; |
a8e0fdd7 MK |
1809 | } |
1810 | ||
fba98d45 KW |
1811 | blk_set_allow_write_beyond_eof(blk, true); |
1812 | ||
876eb1b0 TI |
1813 | object_size = (UINT32_C(1) << base->inode.block_size_shift); |
1814 | buf_size = MIN(object_size, SD_DATA_OBJ_SIZE); | |
1815 | buf = g_malloc0(buf_size); | |
1816 | ||
1a62baf6 | 1817 | max_idx = DIV_ROUND_UP(new_size, buf_size); |
a8e0fdd7 | 1818 | |
1a62baf6 | 1819 | for (idx = old_size / buf_size; idx < max_idx; idx++) { |
a8e0fdd7 MK |
1820 | /* |
1821 | * The created image can be a cloned image, so we need to read | |
1822 | * a data from the source image. | |
1823 | */ | |
fba98d45 | 1824 | ret = blk_pread(blk, idx * buf_size, buf, buf_size); |
a8e0fdd7 MK |
1825 | if (ret < 0) { |
1826 | goto out; | |
1827 | } | |
8341f00d | 1828 | ret = blk_pwrite(blk, idx * buf_size, buf, buf_size, 0); |
a8e0fdd7 MK |
1829 | if (ret < 0) { |
1830 | goto out; | |
1831 | } | |
1832 | } | |
318df29e | 1833 | |
fba98d45 | 1834 | ret = 0; |
a8e0fdd7 | 1835 | out: |
318df29e MA |
1836 | if (ret < 0) { |
1837 | error_setg_errno(errp, -ret, "Can't pre-allocate"); | |
1838 | } | |
1839 | out_with_err_set: | |
ae8622ec | 1840 | blk_unref(blk); |
7267c094 | 1841 | g_free(buf); |
a8e0fdd7 MK |
1842 | |
1843 | return ret; | |
1844 | } | |
1845 | ||
63fd65a0 KW |
1846 | static int sd_create_prealloc(BlockdevOptionsSheepdog *location, int64_t size, |
1847 | Error **errp) | |
1848 | { | |
1849 | BlockDriverState *bs; | |
1850 | Visitor *v; | |
1851 | QObject *obj = NULL; | |
1852 | QDict *qdict; | |
1853 | Error *local_err = NULL; | |
1854 | int ret; | |
1855 | ||
1856 | v = qobject_output_visitor_new(&obj); | |
1857 | visit_type_BlockdevOptionsSheepdog(v, NULL, &location, &local_err); | |
1858 | visit_free(v); | |
1859 | ||
1860 | if (local_err) { | |
1861 | error_propagate(errp, local_err); | |
cb3e7f08 | 1862 | qobject_unref(obj); |
63fd65a0 KW |
1863 | return -EINVAL; |
1864 | } | |
1865 | ||
7dc847eb | 1866 | qdict = qobject_to(QDict, obj); |
63fd65a0 KW |
1867 | qdict_flatten(qdict); |
1868 | ||
1869 | qdict_put_str(qdict, "driver", "sheepdog"); | |
1870 | ||
1871 | bs = bdrv_open(NULL, NULL, qdict, BDRV_O_PROTOCOL | BDRV_O_RDWR, errp); | |
1872 | if (bs == NULL) { | |
1873 | ret = -EIO; | |
1874 | goto fail; | |
1875 | } | |
1876 | ||
1877 | ret = sd_prealloc(bs, 0, size, errp); | |
1878 | fail: | |
1879 | bdrv_unref(bs); | |
cb3e7f08 | 1880 | qobject_unref(qdict); |
63fd65a0 KW |
1881 | return ret; |
1882 | } | |
1883 | ||
a595e4bc KW |
1884 | static int parse_redundancy(BDRVSheepdogState *s, SheepdogRedundancy *opt) |
1885 | { | |
1886 | struct SheepdogInode *inode = &s->inode; | |
1887 | ||
1888 | switch (opt->type) { | |
1889 | case SHEEPDOG_REDUNDANCY_TYPE_FULL: | |
1890 | if (opt->u.full.copies > SD_MAX_COPIES || opt->u.full.copies < 1) { | |
1891 | return -EINVAL; | |
1892 | } | |
1893 | inode->copy_policy = 0; | |
1894 | inode->nr_copies = opt->u.full.copies; | |
1895 | return 0; | |
1896 | ||
1897 | case SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED: | |
1898 | { | |
1899 | int64_t copy = opt->u.erasure_coded.data_strips; | |
1900 | int64_t parity = opt->u.erasure_coded.parity_strips; | |
1901 | ||
1902 | if (copy != 2 && copy != 4 && copy != 8 && copy != 16) { | |
1903 | return -EINVAL; | |
1904 | } | |
1905 | ||
1906 | if (parity >= SD_EC_MAX_STRIP || parity < 1) { | |
1907 | return -EINVAL; | |
1908 | } | |
1909 | ||
1910 | /* | |
1911 | * 4 bits for parity and 4 bits for data. | |
1912 | * We have to compress upper data bits because it can't represent 16 | |
1913 | */ | |
1914 | inode->copy_policy = ((copy / 2) << 4) + parity; | |
1915 | inode->nr_copies = copy + parity; | |
1916 | return 0; | |
1917 | } | |
1918 | ||
1919 | default: | |
1920 | g_assert_not_reached(); | |
1921 | } | |
1922 | ||
1923 | return -EINVAL; | |
1924 | } | |
1925 | ||
b3af018f LY |
1926 | /* |
1927 | * Sheepdog support two kinds of redundancy, full replication and erasure | |
1928 | * coding. | |
1929 | * | |
1930 | * # create a fully replicated vdi with x copies | |
1931 | * -o redundancy=x (1 <= x <= SD_MAX_COPIES) | |
1932 | * | |
1933 | * # create a erasure coded vdi with x data strips and y parity strips | |
1934 | * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP) | |
1935 | */ | |
63fd65a0 | 1936 | static SheepdogRedundancy *parse_redundancy_str(const char *opt) |
b3af018f | 1937 | { |
63fd65a0 | 1938 | SheepdogRedundancy *redundancy; |
b3af018f LY |
1939 | const char *n1, *n2; |
1940 | long copy, parity; | |
1941 | char p[10]; | |
a595e4bc | 1942 | int ret; |
b3af018f LY |
1943 | |
1944 | pstrcpy(p, sizeof(p), opt); | |
1945 | n1 = strtok(p, ":"); | |
1946 | n2 = strtok(NULL, ":"); | |
1947 | ||
1948 | if (!n1) { | |
63fd65a0 | 1949 | return NULL; |
b3af018f LY |
1950 | } |
1951 | ||
a595e4bc KW |
1952 | ret = qemu_strtol(n1, NULL, 10, ©); |
1953 | if (ret < 0) { | |
63fd65a0 | 1954 | return NULL; |
b3af018f LY |
1955 | } |
1956 | ||
63fd65a0 | 1957 | redundancy = g_new0(SheepdogRedundancy, 1); |
a595e4bc | 1958 | if (!n2) { |
63fd65a0 | 1959 | *redundancy = (SheepdogRedundancy) { |
a595e4bc KW |
1960 | .type = SHEEPDOG_REDUNDANCY_TYPE_FULL, |
1961 | .u.full.copies = copy, | |
1962 | }; | |
1963 | } else { | |
1964 | ret = qemu_strtol(n2, NULL, 10, &parity); | |
1965 | if (ret < 0) { | |
a2cb9239 | 1966 | g_free(redundancy); |
63fd65a0 | 1967 | return NULL; |
a595e4bc | 1968 | } |
b3af018f | 1969 | |
63fd65a0 | 1970 | *redundancy = (SheepdogRedundancy) { |
a595e4bc KW |
1971 | .type = SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED, |
1972 | .u.erasure_coded = { | |
1973 | .data_strips = copy, | |
1974 | .parity_strips = parity, | |
1975 | }, | |
1976 | }; | |
b3af018f LY |
1977 | } |
1978 | ||
63fd65a0 | 1979 | return redundancy; |
b3af018f LY |
1980 | } |
1981 | ||
63fd65a0 KW |
1982 | static int parse_block_size_shift(BDRVSheepdogState *s, |
1983 | BlockdevCreateOptionsSheepdog *opts) | |
876eb1b0 TI |
1984 | { |
1985 | struct SheepdogInode *inode = &s->inode; | |
1986 | uint64_t object_size; | |
1987 | int obj_order; | |
1988 | ||
63fd65a0 KW |
1989 | if (opts->has_object_size) { |
1990 | object_size = opts->object_size; | |
1991 | ||
876eb1b0 TI |
1992 | if ((object_size - 1) & object_size) { /* not a power of 2? */ |
1993 | return -EINVAL; | |
1994 | } | |
786a4ea8 | 1995 | obj_order = ctz32(object_size); |
876eb1b0 TI |
1996 | if (obj_order < 20 || obj_order > 31) { |
1997 | return -EINVAL; | |
1998 | } | |
1999 | inode->block_size_shift = (uint8_t)obj_order; | |
2000 | } | |
2001 | ||
2002 | return 0; | |
2003 | } | |
2004 | ||
63fd65a0 | 2005 | static int sd_co_create(BlockdevCreateOptions *options, Error **errp) |
33b1db1c | 2006 | { |
63fd65a0 | 2007 | BlockdevCreateOptionsSheepdog *opts = &options->u.sheepdog; |
b6fc8245 | 2008 | int ret = 0; |
c31d482f | 2009 | uint32_t vid = 0; |
33b1db1c | 2010 | char *backing_file = NULL; |
b222237b | 2011 | char *buf = NULL; |
b6fc8245 | 2012 | BDRVSheepdogState *s; |
876eb1b0 | 2013 | uint64_t max_vdi_size; |
2f536801 | 2014 | bool prealloc = false; |
33b1db1c | 2015 | |
63fd65a0 KW |
2016 | assert(options->driver == BLOCKDEV_DRIVER_SHEEPDOG); |
2017 | ||
5839e53b | 2018 | s = g_new0(BDRVSheepdogState, 1); |
b6fc8245 | 2019 | |
63fd65a0 KW |
2020 | /* Steal SocketAddress from QAPI, set NULL to prevent double free */ |
2021 | s->addr = opts->location->server; | |
2022 | opts->location->server = NULL; | |
2023 | ||
2024 | if (strlen(opts->location->vdi) >= sizeof(s->name)) { | |
2025 | error_setg(errp, "'vdi' string too long"); | |
2026 | ret = -EINVAL; | |
b6fc8245 | 2027 | goto out; |
b4447363 | 2028 | } |
63fd65a0 | 2029 | pstrcpy(s->name, sizeof(s->name), opts->location->vdi); |
b4447363 | 2030 | |
63fd65a0 KW |
2031 | s->inode.vdi_size = opts->size; |
2032 | backing_file = opts->backing_file; | |
831acdc9 | 2033 | |
63fd65a0 KW |
2034 | if (!opts->has_preallocation) { |
2035 | opts->preallocation = PREALLOC_MODE_OFF; | |
2036 | } | |
2037 | switch (opts->preallocation) { | |
2038 | case PREALLOC_MODE_OFF: | |
b222237b | 2039 | prealloc = false; |
63fd65a0 KW |
2040 | break; |
2041 | case PREALLOC_MODE_FULL: | |
b222237b | 2042 | prealloc = true; |
63fd65a0 KW |
2043 | break; |
2044 | default: | |
2045 | error_setg(errp, "Preallocation mode not supported for Sheepdog"); | |
b222237b CL |
2046 | ret = -EINVAL; |
2047 | goto out; | |
2048 | } | |
2049 | ||
63fd65a0 KW |
2050 | if (opts->has_redundancy) { |
2051 | ret = parse_redundancy(s, opts->redundancy); | |
b222237b | 2052 | if (ret < 0) { |
63fd65a0 | 2053 | error_setg(errp, "Invalid redundancy mode"); |
b222237b | 2054 | goto out; |
33b1db1c | 2055 | } |
33b1db1c | 2056 | } |
876eb1b0 TI |
2057 | ret = parse_block_size_shift(s, opts); |
2058 | if (ret < 0) { | |
2059 | error_setg(errp, "Invalid object_size." | |
2060 | " obect_size needs to be power of 2" | |
2061 | " and be limited from 2^20 to 2^31"); | |
b6fc8245 | 2062 | goto out; |
33b1db1c MK |
2063 | } |
2064 | ||
63fd65a0 | 2065 | if (opts->has_backing_file) { |
fba98d45 | 2066 | BlockBackend *blk; |
9f23fce7 | 2067 | BDRVSheepdogState *base; |
33b1db1c MK |
2068 | BlockDriver *drv; |
2069 | ||
2070 | /* Currently, only Sheepdog backing image is supported. */ | |
63fd65a0 | 2071 | drv = bdrv_find_protocol(opts->backing_file, true, NULL); |
33b1db1c | 2072 | if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { |
e67c3993 | 2073 | error_setg(errp, "backing_file must be a sheepdog image"); |
b6fc8245 MK |
2074 | ret = -EINVAL; |
2075 | goto out; | |
33b1db1c MK |
2076 | } |
2077 | ||
63fd65a0 | 2078 | blk = blk_new_open(opts->backing_file, NULL, NULL, |
72e775c7 | 2079 | BDRV_O_PROTOCOL, errp); |
fba98d45 KW |
2080 | if (blk == NULL) { |
2081 | ret = -EIO; | |
b6fc8245 | 2082 | goto out; |
cb595887 | 2083 | } |
33b1db1c | 2084 | |
fba98d45 | 2085 | base = blk_bs(blk)->opaque; |
33b1db1c | 2086 | |
9f23fce7 | 2087 | if (!is_snapshot(&base->inode)) { |
e67c3993 | 2088 | error_setg(errp, "cannot clone from a non snapshot vdi"); |
fba98d45 | 2089 | blk_unref(blk); |
b6fc8245 MK |
2090 | ret = -EINVAL; |
2091 | goto out; | |
33b1db1c | 2092 | } |
9f23fce7 | 2093 | s->inode.vdi_id = base->inode.vdi_id; |
fba98d45 | 2094 | blk_unref(blk); |
33b1db1c MK |
2095 | } |
2096 | ||
5d5da114 | 2097 | s->aio_context = qemu_get_aio_context(); |
876eb1b0 TI |
2098 | |
2099 | /* if block_size_shift is not specified, get cluster default value */ | |
2100 | if (s->inode.block_size_shift == 0) { | |
2101 | SheepdogVdiReq hdr; | |
2102 | SheepdogClusterRsp *rsp = (SheepdogClusterRsp *)&hdr; | |
876eb1b0 TI |
2103 | int fd; |
2104 | unsigned int wlen = 0, rlen = 0; | |
2105 | ||
48d7c4af | 2106 | fd = connect_to_sdog(s, errp); |
876eb1b0 | 2107 | if (fd < 0) { |
48d7c4af | 2108 | ret = fd; |
876eb1b0 TI |
2109 | goto out; |
2110 | } | |
2111 | ||
2112 | memset(&hdr, 0, sizeof(hdr)); | |
2113 | hdr.opcode = SD_OP_GET_CLUSTER_DEFAULT; | |
2114 | hdr.proto_ver = SD_PROTO_VER; | |
2115 | ||
f11672db | 2116 | ret = do_req(fd, NULL, (SheepdogReq *)&hdr, |
876eb1b0 TI |
2117 | NULL, &wlen, &rlen); |
2118 | closesocket(fd); | |
2119 | if (ret) { | |
2120 | error_setg_errno(errp, -ret, "failed to get cluster default"); | |
2121 | goto out; | |
2122 | } | |
2123 | if (rsp->result == SD_RES_SUCCESS) { | |
2124 | s->inode.block_size_shift = rsp->block_size_shift; | |
2125 | } else { | |
2126 | s->inode.block_size_shift = SD_DEFAULT_BLOCK_SIZE_SHIFT; | |
2127 | } | |
2128 | } | |
2129 | ||
2130 | max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS; | |
2131 | ||
2132 | if (s->inode.vdi_size > max_vdi_size) { | |
2133 | error_setg(errp, "An image is too large." | |
2134 | " The maximum image size is %"PRIu64 "GB", | |
2135 | max_vdi_size / 1024 / 1024 / 1024); | |
2136 | ret = -EINVAL; | |
2137 | goto out; | |
2138 | } | |
2139 | ||
e67c3993 | 2140 | ret = do_sd_create(s, &vid, 0, errp); |
7d2d3e74 | 2141 | if (ret) { |
b6fc8245 | 2142 | goto out; |
a8e0fdd7 MK |
2143 | } |
2144 | ||
7d2d3e74 | 2145 | if (prealloc) { |
63fd65a0 | 2146 | ret = sd_create_prealloc(opts->location, opts->size, errp); |
318df29e | 2147 | } |
b6fc8245 | 2148 | out: |
b222237b CL |
2149 | g_free(backing_file); |
2150 | g_free(buf); | |
63fd65a0 | 2151 | g_free(s->addr); |
b6fc8245 MK |
2152 | g_free(s); |
2153 | return ret; | |
33b1db1c MK |
2154 | } |
2155 | ||
63fd65a0 KW |
2156 | static int coroutine_fn sd_co_create_opts(const char *filename, QemuOpts *opts, |
2157 | Error **errp) | |
2158 | { | |
2159 | BlockdevCreateOptions *create_options = NULL; | |
2160 | QDict *qdict, *location_qdict; | |
63fd65a0 | 2161 | Visitor *v; |
a2cb9239 | 2162 | char *redundancy; |
63fd65a0 KW |
2163 | Error *local_err = NULL; |
2164 | int ret; | |
2165 | ||
2166 | redundancy = qemu_opt_get_del(opts, BLOCK_OPT_REDUNDANCY); | |
2167 | ||
2168 | qdict = qemu_opts_to_qdict(opts, NULL); | |
2169 | qdict_put_str(qdict, "driver", "sheepdog"); | |
2170 | ||
2171 | location_qdict = qdict_new(); | |
2172 | qdict_put(qdict, "location", location_qdict); | |
2173 | ||
2174 | sd_parse_filename(filename, location_qdict, &local_err); | |
2175 | if (local_err) { | |
2176 | error_propagate(errp, local_err); | |
2177 | ret = -EINVAL; | |
2178 | goto fail; | |
2179 | } | |
2180 | ||
2181 | qdict_flatten(qdict); | |
2182 | ||
2183 | /* Change legacy command line options into QMP ones */ | |
2184 | static const QDictRenames opt_renames[] = { | |
2185 | { BLOCK_OPT_BACKING_FILE, "backing-file" }, | |
2186 | { BLOCK_OPT_OBJECT_SIZE, "object-size" }, | |
2187 | { NULL, NULL }, | |
2188 | }; | |
2189 | ||
2190 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { | |
2191 | ret = -EINVAL; | |
2192 | goto fail; | |
2193 | } | |
2194 | ||
2195 | /* Get the QAPI object */ | |
af91062e MA |
2196 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
2197 | if (!v) { | |
63fd65a0 KW |
2198 | ret = -EINVAL; |
2199 | goto fail; | |
2200 | } | |
2201 | ||
63fd65a0 KW |
2202 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); |
2203 | visit_free(v); | |
63fd65a0 KW |
2204 | |
2205 | if (local_err) { | |
2206 | error_propagate(errp, local_err); | |
2207 | ret = -EINVAL; | |
2208 | goto fail; | |
2209 | } | |
2210 | ||
2211 | assert(create_options->driver == BLOCKDEV_DRIVER_SHEEPDOG); | |
2212 | create_options->u.sheepdog.size = | |
2213 | ROUND_UP(create_options->u.sheepdog.size, BDRV_SECTOR_SIZE); | |
2214 | ||
2215 | if (redundancy) { | |
2216 | create_options->u.sheepdog.has_redundancy = true; | |
2217 | create_options->u.sheepdog.redundancy = | |
2218 | parse_redundancy_str(redundancy); | |
2219 | if (create_options->u.sheepdog.redundancy == NULL) { | |
2220 | error_setg(errp, "Invalid redundancy mode"); | |
2221 | ret = -EINVAL; | |
2222 | goto fail; | |
2223 | } | |
2224 | } | |
2225 | ||
2226 | ret = sd_co_create(create_options, errp); | |
2227 | fail: | |
2228 | qapi_free_BlockdevCreateOptions(create_options); | |
cb3e7f08 | 2229 | qobject_unref(qdict); |
a2cb9239 | 2230 | g_free(redundancy); |
63fd65a0 KW |
2231 | return ret; |
2232 | } | |
2233 | ||
33b1db1c MK |
2234 | static void sd_close(BlockDriverState *bs) |
2235 | { | |
dfb12bf8 | 2236 | Error *local_err = NULL; |
33b1db1c MK |
2237 | BDRVSheepdogState *s = bs->opaque; |
2238 | SheepdogVdiReq hdr; | |
2239 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
2240 | unsigned int wlen, rlen = 0; | |
2241 | int fd, ret; | |
2242 | ||
70018a14 | 2243 | trace_sheepdog_close(s->name); |
33b1db1c | 2244 | |
dfb12bf8 | 2245 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2246 | if (fd < 0) { |
565f65d2 | 2247 | error_report_err(local_err); |
33b1db1c MK |
2248 | return; |
2249 | } | |
2250 | ||
2251 | memset(&hdr, 0, sizeof(hdr)); | |
2252 | ||
2253 | hdr.opcode = SD_OP_RELEASE_VDI; | |
1dbfafed | 2254 | hdr.type = LOCK_TYPE_NORMAL; |
9f23fce7 | 2255 | hdr.base_vdi_id = s->inode.vdi_id; |
33b1db1c MK |
2256 | wlen = strlen(s->name) + 1; |
2257 | hdr.data_length = wlen; | |
2258 | hdr.flags = SD_FLAG_CMD_WRITE; | |
2259 | ||
f11672db | 2260 | ret = do_req(fd, s->bs, (SheepdogReq *)&hdr, |
84390bed | 2261 | s->name, &wlen, &rlen); |
33b1db1c MK |
2262 | |
2263 | closesocket(fd); | |
2264 | ||
2265 | if (!ret && rsp->result != SD_RES_SUCCESS && | |
2266 | rsp->result != SD_RES_VDI_NOT_LOCKED) { | |
6daf194d | 2267 | error_report("%s, %s", sd_strerror(rsp->result), s->name); |
33b1db1c MK |
2268 | } |
2269 | ||
dca21ef2 | 2270 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd, |
f6a51c84 | 2271 | false, NULL, NULL, NULL, NULL); |
33b1db1c | 2272 | closesocket(s->fd); |
bd269ebc | 2273 | qapi_free_SocketAddress(s->addr); |
33b1db1c MK |
2274 | } |
2275 | ||
2276 | static int64_t sd_getlength(BlockDriverState *bs) | |
2277 | { | |
2278 | BDRVSheepdogState *s = bs->opaque; | |
2279 | ||
2280 | return s->inode.vdi_size; | |
2281 | } | |
2282 | ||
061ca8a3 KW |
2283 | static int coroutine_fn sd_co_truncate(BlockDriverState *bs, int64_t offset, |
2284 | PreallocMode prealloc, Error **errp) | |
33b1db1c MK |
2285 | { |
2286 | BDRVSheepdogState *s = bs->opaque; | |
2287 | int ret, fd; | |
2288 | unsigned int datalen; | |
876eb1b0 | 2289 | uint64_t max_vdi_size; |
74f1eabf | 2290 | int64_t old_size = s->inode.vdi_size; |
33b1db1c | 2291 | |
74f1eabf | 2292 | if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_FULL) { |
8243ccb7 | 2293 | error_setg(errp, "Unsupported preallocation mode '%s'", |
977c736f | 2294 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
2295 | return -ENOTSUP; |
2296 | } | |
2297 | ||
876eb1b0 | 2298 | max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS; |
74f1eabf | 2299 | if (offset < old_size) { |
4bff28b8 | 2300 | error_setg(errp, "shrinking is not supported"); |
33b1db1c | 2301 | return -EINVAL; |
876eb1b0 | 2302 | } else if (offset > max_vdi_size) { |
4bff28b8 | 2303 | error_setg(errp, "too big image size"); |
33b1db1c MK |
2304 | return -EINVAL; |
2305 | } | |
2306 | ||
4bff28b8 | 2307 | fd = connect_to_sdog(s, errp); |
33b1db1c | 2308 | if (fd < 0) { |
cb595887 | 2309 | return fd; |
33b1db1c MK |
2310 | } |
2311 | ||
2312 | /* we don't need to update entire object */ | |
03b036cc | 2313 | datalen = SD_INODE_HEADER_SIZE; |
33b1db1c | 2314 | s->inode.vdi_size = offset; |
f11672db | 2315 | ret = write_object(fd, s->bs, (char *)&s->inode, |
84390bed SH |
2316 | vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies, |
2317 | datalen, 0, false, s->cache_flags); | |
33b1db1c MK |
2318 | close(fd); |
2319 | ||
2320 | if (ret < 0) { | |
4bff28b8 | 2321 | error_setg_errno(errp, -ret, "failed to update an inode"); |
74f1eabf | 2322 | return ret; |
33b1db1c MK |
2323 | } |
2324 | ||
74f1eabf HR |
2325 | if (prealloc == PREALLOC_MODE_FULL) { |
2326 | ret = sd_prealloc(bs, old_size, offset, errp); | |
2327 | if (ret < 0) { | |
2328 | return ret; | |
2329 | } | |
2330 | } | |
2331 | ||
2332 | return 0; | |
33b1db1c MK |
2333 | } |
2334 | ||
2335 | /* | |
2336 | * This function is called after writing data objects. If we need to | |
2337 | * update metadata, this sends a write request to the vdi object. | |
33b1db1c | 2338 | */ |
d8716b41 | 2339 | static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) |
33b1db1c | 2340 | { |
28ddd08c | 2341 | BDRVSheepdogState *s = acb->s; |
33b1db1c MK |
2342 | struct iovec iov; |
2343 | AIOReq *aio_req; | |
2344 | uint32_t offset, data_len, mn, mx; | |
2345 | ||
498f2140 HM |
2346 | mn = acb->min_dirty_data_idx; |
2347 | mx = acb->max_dirty_data_idx; | |
33b1db1c MK |
2348 | if (mn <= mx) { |
2349 | /* we need to update the vdi object. */ | |
e80ab33d | 2350 | ++acb->nr_pending; |
33b1db1c MK |
2351 | offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + |
2352 | mn * sizeof(s->inode.data_vdi_id[0]); | |
2353 | data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); | |
2354 | ||
498f2140 HM |
2355 | acb->min_dirty_data_idx = UINT32_MAX; |
2356 | acb->max_dirty_data_idx = 0; | |
33b1db1c MK |
2357 | |
2358 | iov.iov_base = &s->inode; | |
2359 | iov.iov_len = sizeof(s->inode); | |
2360 | aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), | |
b544c1ab | 2361 | data_len, offset, 0, false, 0, offset); |
b544c1ab | 2362 | add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA); |
e80ab33d PB |
2363 | if (--acb->nr_pending) { |
2364 | qemu_coroutine_yield(); | |
2365 | } | |
33b1db1c | 2366 | } |
33b1db1c MK |
2367 | } |
2368 | ||
859e5553 LY |
2369 | /* Delete current working VDI on the snapshot chain */ |
2370 | static bool sd_delete(BDRVSheepdogState *s) | |
2371 | { | |
dfb12bf8 | 2372 | Error *local_err = NULL; |
859e5553 LY |
2373 | unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0; |
2374 | SheepdogVdiReq hdr = { | |
2375 | .opcode = SD_OP_DEL_VDI, | |
9f23fce7 | 2376 | .base_vdi_id = s->inode.vdi_id, |
859e5553 LY |
2377 | .data_length = wlen, |
2378 | .flags = SD_FLAG_CMD_WRITE, | |
2379 | }; | |
2380 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
2381 | int fd, ret; | |
2382 | ||
dfb12bf8 | 2383 | fd = connect_to_sdog(s, &local_err); |
859e5553 | 2384 | if (fd < 0) { |
565f65d2 | 2385 | error_report_err(local_err); |
859e5553 LY |
2386 | return false; |
2387 | } | |
2388 | ||
f11672db | 2389 | ret = do_req(fd, s->bs, (SheepdogReq *)&hdr, |
84390bed | 2390 | s->name, &wlen, &rlen); |
859e5553 LY |
2391 | closesocket(fd); |
2392 | if (ret) { | |
2393 | return false; | |
2394 | } | |
2395 | switch (rsp->result) { | |
2396 | case SD_RES_NO_VDI: | |
2397 | error_report("%s was already deleted", s->name); | |
2398 | /* fall through */ | |
2399 | case SD_RES_SUCCESS: | |
2400 | break; | |
2401 | default: | |
2402 | error_report("%s, %s", sd_strerror(rsp->result), s->name); | |
2403 | return false; | |
2404 | } | |
2405 | ||
2406 | return true; | |
2407 | } | |
2408 | ||
33b1db1c MK |
2409 | /* |
2410 | * Create a writable VDI from a snapshot | |
2411 | */ | |
2412 | static int sd_create_branch(BDRVSheepdogState *s) | |
2413 | { | |
dfb12bf8 | 2414 | Error *local_err = NULL; |
33b1db1c MK |
2415 | int ret, fd; |
2416 | uint32_t vid; | |
2417 | char *buf; | |
859e5553 | 2418 | bool deleted; |
33b1db1c | 2419 | |
70018a14 | 2420 | trace_sheepdog_create_branch_snapshot(s->inode.vdi_id); |
33b1db1c | 2421 | |
7267c094 | 2422 | buf = g_malloc(SD_INODE_SIZE); |
33b1db1c | 2423 | |
859e5553 LY |
2424 | /* |
2425 | * Even If deletion fails, we will just create extra snapshot based on | |
dc6fb73d | 2426 | * the working VDI which was supposed to be deleted. So no need to |
859e5553 LY |
2427 | * false bail out. |
2428 | */ | |
2429 | deleted = sd_delete(s); | |
7d2d3e74 | 2430 | ret = do_sd_create(s, &vid, !deleted, &local_err); |
33b1db1c | 2431 | if (ret) { |
565f65d2 | 2432 | error_report_err(local_err); |
33b1db1c MK |
2433 | goto out; |
2434 | } | |
2435 | ||
70018a14 | 2436 | trace_sheepdog_create_branch_created(vid); |
33b1db1c | 2437 | |
dfb12bf8 | 2438 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2439 | if (fd < 0) { |
565f65d2 | 2440 | error_report_err(local_err); |
cb595887 | 2441 | ret = fd; |
33b1db1c MK |
2442 | goto out; |
2443 | } | |
2444 | ||
f11672db | 2445 | ret = read_object(fd, s->bs, buf, vid_to_vdi_oid(vid), |
84390bed | 2446 | s->inode.nr_copies, SD_INODE_SIZE, 0, s->cache_flags); |
33b1db1c MK |
2447 | |
2448 | closesocket(fd); | |
2449 | ||
2450 | if (ret < 0) { | |
2451 | goto out; | |
2452 | } | |
2453 | ||
2454 | memcpy(&s->inode, buf, sizeof(s->inode)); | |
2455 | ||
2f536801 | 2456 | s->is_snapshot = false; |
33b1db1c | 2457 | ret = 0; |
70018a14 | 2458 | trace_sheepdog_create_branch_new(s->inode.vdi_id); |
33b1db1c MK |
2459 | |
2460 | out: | |
7267c094 | 2461 | g_free(buf); |
33b1db1c MK |
2462 | |
2463 | return ret; | |
2464 | } | |
2465 | ||
2466 | /* | |
2467 | * Send I/O requests to the server. | |
2468 | * | |
2469 | * This function sends requests to the server, links the requests to | |
c292ee6a | 2470 | * the inflight_list in BDRVSheepdogState, and exits without |
33b1db1c MK |
2471 | * waiting the response. The responses are received in the |
2472 | * `aio_read_response' function which is called from the main loop as | |
2473 | * a fd handler. | |
2df46246 MK |
2474 | * |
2475 | * Returns 1 when we need to wait a response, 0 when there is no sent | |
2476 | * request and -errno in error cases. | |
33b1db1c | 2477 | */ |
28ddd08c | 2478 | static void coroutine_fn sd_co_rw_vector(SheepdogAIOCB *acb) |
33b1db1c | 2479 | { |
33b1db1c | 2480 | int ret = 0; |
e8bfaa2f | 2481 | unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE; |
876eb1b0 TI |
2482 | unsigned long idx; |
2483 | uint32_t object_size; | |
33b1db1c | 2484 | uint64_t oid; |
876eb1b0 | 2485 | uint64_t offset; |
28ddd08c | 2486 | BDRVSheepdogState *s = acb->s; |
33b1db1c MK |
2487 | SheepdogInode *inode = &s->inode; |
2488 | AIOReq *aio_req; | |
2489 | ||
33b1db1c MK |
2490 | if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) { |
2491 | /* | |
2492 | * In the case we open the snapshot VDI, Sheepdog creates the | |
2493 | * writable VDI when we do a write operation first. | |
2494 | */ | |
2495 | ret = sd_create_branch(s); | |
2496 | if (ret) { | |
2497 | acb->ret = -EIO; | |
e80ab33d | 2498 | return; |
33b1db1c MK |
2499 | } |
2500 | } | |
2501 | ||
876eb1b0 TI |
2502 | object_size = (UINT32_C(1) << inode->block_size_shift); |
2503 | idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size; | |
2504 | offset = (acb->sector_num * BDRV_SECTOR_SIZE) % object_size; | |
2505 | ||
1d732d7d MK |
2506 | /* |
2507 | * Make sure we don't free the aiocb before we are done with all requests. | |
2508 | * This additional reference is dropped at the end of this function. | |
2509 | */ | |
2510 | acb->nr_pending++; | |
2511 | ||
33b1db1c MK |
2512 | while (done != total) { |
2513 | uint8_t flags = 0; | |
2514 | uint64_t old_oid = 0; | |
2f536801 | 2515 | bool create = false; |
33b1db1c MK |
2516 | |
2517 | oid = vid_to_data_oid(inode->data_vdi_id[idx], idx); | |
2518 | ||
876eb1b0 | 2519 | len = MIN(total - done, object_size - offset); |
33b1db1c | 2520 | |
19db9b90 CH |
2521 | switch (acb->aiocb_type) { |
2522 | case AIOCB_READ_UDATA: | |
2523 | if (!inode->data_vdi_id[idx]) { | |
2524 | qemu_iovec_memset(acb->qiov, done, 0, len); | |
33b1db1c MK |
2525 | goto done; |
2526 | } | |
19db9b90 CH |
2527 | break; |
2528 | case AIOCB_WRITE_UDATA: | |
2529 | if (!inode->data_vdi_id[idx]) { | |
2f536801 | 2530 | create = true; |
19db9b90 CH |
2531 | } else if (!is_data_obj_writable(inode, idx)) { |
2532 | /* Copy-On-Write */ | |
2f536801 | 2533 | create = true; |
19db9b90 CH |
2534 | old_oid = oid; |
2535 | flags = SD_FLAG_CMD_COW; | |
2536 | } | |
2537 | break; | |
cac8f4a6 LY |
2538 | case AIOCB_DISCARD_OBJ: |
2539 | /* | |
2540 | * We discard the object only when the whole object is | |
2541 | * 1) allocated 2) trimmed. Otherwise, simply skip it. | |
2542 | */ | |
876eb1b0 | 2543 | if (len != object_size || inode->data_vdi_id[idx] == 0) { |
cac8f4a6 LY |
2544 | goto done; |
2545 | } | |
2546 | break; | |
19db9b90 CH |
2547 | default: |
2548 | break; | |
33b1db1c MK |
2549 | } |
2550 | ||
2551 | if (create) { | |
70018a14 LV |
2552 | trace_sheepdog_co_rw_vector_update(inode->vdi_id, oid, |
2553 | vid_to_data_oid(inode->data_vdi_id[idx], idx), | |
2554 | idx); | |
33b1db1c | 2555 | oid = vid_to_data_oid(inode->vdi_id, idx); |
70018a14 | 2556 | trace_sheepdog_co_rw_vector_new(oid); |
33b1db1c MK |
2557 | } |
2558 | ||
b544c1ab | 2559 | aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, create, |
e6fd57ea HM |
2560 | old_oid, |
2561 | acb->aiocb_type == AIOCB_DISCARD_OBJ ? | |
2562 | 0 : done); | |
b544c1ab | 2563 | add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, |
a37dcdf9 | 2564 | acb->aiocb_type); |
33b1db1c MK |
2565 | done: |
2566 | offset = 0; | |
2567 | idx++; | |
2568 | done += len; | |
2569 | } | |
e80ab33d PB |
2570 | if (--acb->nr_pending) { |
2571 | qemu_coroutine_yield(); | |
33b1db1c MK |
2572 | } |
2573 | } | |
2574 | ||
acf6e5f0 | 2575 | static void sd_aio_complete(SheepdogAIOCB *acb) |
6a55c82c | 2576 | { |
f1af3251 | 2577 | BDRVSheepdogState *s; |
acf6e5f0 PB |
2578 | if (acb->aiocb_type == AIOCB_FLUSH_CACHE) { |
2579 | return; | |
6a55c82c HM |
2580 | } |
2581 | ||
f1af3251 PB |
2582 | s = acb->s; |
2583 | qemu_co_mutex_lock(&s->queue_lock); | |
acf6e5f0 | 2584 | QLIST_REMOVE(acb, aiocb_siblings); |
f1af3251 PB |
2585 | qemu_co_queue_restart_all(&s->overlapping_queue); |
2586 | qemu_co_mutex_unlock(&s->queue_lock); | |
6a55c82c HM |
2587 | } |
2588 | ||
a968168c | 2589 | static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, |
e18a58b4 EB |
2590 | int nb_sectors, QEMUIOVector *qiov, |
2591 | int flags) | |
33b1db1c | 2592 | { |
28ddd08c | 2593 | SheepdogAIOCB acb; |
2df46246 | 2594 | int ret; |
e50d7607 LY |
2595 | int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE; |
2596 | BDRVSheepdogState *s = bs->opaque; | |
33b1db1c | 2597 | |
e18a58b4 | 2598 | assert(!flags); |
c0191e76 | 2599 | if (offset > s->inode.vdi_size) { |
061ca8a3 | 2600 | ret = sd_co_truncate(bs, offset, PREALLOC_MODE_OFF, NULL); |
cb595887 MK |
2601 | if (ret < 0) { |
2602 | return ret; | |
33b1db1c | 2603 | } |
33b1db1c MK |
2604 | } |
2605 | ||
28ddd08c | 2606 | sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_WRITE_UDATA); |
28ddd08c PB |
2607 | sd_co_rw_vector(&acb); |
2608 | sd_write_done(&acb); | |
acf6e5f0 | 2609 | sd_aio_complete(&acb); |
2df46246 | 2610 | |
28ddd08c | 2611 | return acb.ret; |
33b1db1c MK |
2612 | } |
2613 | ||
a968168c | 2614 | static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num, |
2df46246 | 2615 | int nb_sectors, QEMUIOVector *qiov) |
33b1db1c | 2616 | { |
28ddd08c | 2617 | SheepdogAIOCB acb; |
6a55c82c | 2618 | BDRVSheepdogState *s = bs->opaque; |
33b1db1c | 2619 | |
28ddd08c | 2620 | sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_READ_UDATA); |
28ddd08c | 2621 | sd_co_rw_vector(&acb); |
acf6e5f0 | 2622 | sd_aio_complete(&acb); |
2df46246 | 2623 | |
28ddd08c | 2624 | return acb.ret; |
33b1db1c MK |
2625 | } |
2626 | ||
47622c44 LY |
2627 | static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) |
2628 | { | |
2629 | BDRVSheepdogState *s = bs->opaque; | |
28ddd08c | 2630 | SheepdogAIOCB acb; |
47783072 | 2631 | AIOReq *aio_req; |
47622c44 | 2632 | |
0e7106d8 | 2633 | if (s->cache_flags != SD_FLAG_CMD_CACHE) { |
47622c44 LY |
2634 | return 0; |
2635 | } | |
2636 | ||
28ddd08c | 2637 | sd_aio_setup(&acb, s, NULL, 0, 0, AIOCB_FLUSH_CACHE); |
47622c44 | 2638 | |
28ddd08c PB |
2639 | acb.nr_pending++; |
2640 | aio_req = alloc_aio_req(s, &acb, vid_to_vdi_oid(s->inode.vdi_id), | |
b544c1ab | 2641 | 0, 0, 0, false, 0, 0); |
28ddd08c | 2642 | add_aio_request(s, aio_req, NULL, 0, acb.aiocb_type); |
47622c44 | 2643 | |
28ddd08c | 2644 | if (--acb.nr_pending) { |
e80ab33d PB |
2645 | qemu_coroutine_yield(); |
2646 | } | |
acf6e5f0 PB |
2647 | |
2648 | sd_aio_complete(&acb); | |
28ddd08c | 2649 | return acb.ret; |
47622c44 LY |
2650 | } |
2651 | ||
33b1db1c MK |
2652 | static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) |
2653 | { | |
dfb12bf8 | 2654 | Error *local_err = NULL; |
33b1db1c MK |
2655 | BDRVSheepdogState *s = bs->opaque; |
2656 | int ret, fd; | |
2657 | uint32_t new_vid; | |
2658 | SheepdogInode *inode; | |
2659 | unsigned int datalen; | |
2660 | ||
70018a14 LV |
2661 | trace_sheepdog_snapshot_create_info(sn_info->name, sn_info->id_str, s->name, |
2662 | sn_info->vm_state_size, s->is_snapshot); | |
33b1db1c MK |
2663 | |
2664 | if (s->is_snapshot) { | |
2665 | error_report("You can't create a snapshot of a snapshot VDI, " | |
6daf194d | 2666 | "%s (%" PRIu32 ").", s->name, s->inode.vdi_id); |
33b1db1c MK |
2667 | |
2668 | return -EINVAL; | |
2669 | } | |
2670 | ||
70018a14 | 2671 | trace_sheepdog_snapshot_create(sn_info->name, sn_info->id_str); |
33b1db1c MK |
2672 | |
2673 | s->inode.vm_state_size = sn_info->vm_state_size; | |
2674 | s->inode.vm_clock_nsec = sn_info->vm_clock_nsec; | |
3178e275 JM |
2675 | /* It appears that inode.tag does not require a NUL terminator, |
2676 | * which means this use of strncpy is ok. | |
2677 | */ | |
33b1db1c MK |
2678 | strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag)); |
2679 | /* we don't need to update entire object */ | |
03b036cc | 2680 | datalen = SD_INODE_HEADER_SIZE; |
2df5fee2 | 2681 | inode = g_malloc(datalen); |
33b1db1c MK |
2682 | |
2683 | /* refresh inode. */ | |
dfb12bf8 | 2684 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2685 | if (fd < 0) { |
565f65d2 | 2686 | error_report_err(local_err); |
cb595887 | 2687 | ret = fd; |
33b1db1c MK |
2688 | goto cleanup; |
2689 | } | |
2690 | ||
f11672db | 2691 | ret = write_object(fd, s->bs, (char *)&s->inode, |
84390bed SH |
2692 | vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies, |
2693 | datalen, 0, false, s->cache_flags); | |
33b1db1c | 2694 | if (ret < 0) { |
6daf194d | 2695 | error_report("failed to write snapshot's inode."); |
33b1db1c MK |
2696 | goto cleanup; |
2697 | } | |
2698 | ||
7d2d3e74 | 2699 | ret = do_sd_create(s, &new_vid, 1, &local_err); |
33b1db1c | 2700 | if (ret < 0) { |
c29b77f9 MA |
2701 | error_reportf_err(local_err, |
2702 | "failed to create inode for snapshot: "); | |
33b1db1c MK |
2703 | goto cleanup; |
2704 | } | |
2705 | ||
f11672db | 2706 | ret = read_object(fd, s->bs, (char *)inode, |
84390bed SH |
2707 | vid_to_vdi_oid(new_vid), s->inode.nr_copies, datalen, 0, |
2708 | s->cache_flags); | |
33b1db1c MK |
2709 | |
2710 | if (ret < 0) { | |
6daf194d | 2711 | error_report("failed to read new inode info. %s", strerror(errno)); |
33b1db1c MK |
2712 | goto cleanup; |
2713 | } | |
2714 | ||
2715 | memcpy(&s->inode, inode, datalen); | |
70018a14 LV |
2716 | trace_sheepdog_snapshot_create_inode(s->inode.name, s->inode.snap_id, |
2717 | s->inode.vdi_id); | |
33b1db1c MK |
2718 | |
2719 | cleanup: | |
2df5fee2 | 2720 | g_free(inode); |
33b1db1c MK |
2721 | closesocket(fd); |
2722 | return ret; | |
2723 | } | |
2724 | ||
859e5553 LY |
2725 | /* |
2726 | * We implement rollback(loadvm) operation to the specified snapshot by | |
2727 | * 1) switch to the snapshot | |
2728 | * 2) rely on sd_create_branch to delete working VDI and | |
dc6fb73d | 2729 | * 3) create a new working VDI based on the specified snapshot |
859e5553 | 2730 | */ |
33b1db1c MK |
2731 | static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
2732 | { | |
2733 | BDRVSheepdogState *s = bs->opaque; | |
2734 | BDRVSheepdogState *old_s; | |
9ff53a0e | 2735 | char tag[SD_MAX_VDI_TAG_LEN]; |
33b1db1c | 2736 | uint32_t snapid = 0; |
89e2a31d MA |
2737 | int ret; |
2738 | ||
2739 | if (!sd_parse_snapid_or_tag(snapshot_id, &snapid, tag)) { | |
2740 | return -EINVAL; | |
2741 | } | |
33b1db1c | 2742 | |
5839e53b | 2743 | old_s = g_new(BDRVSheepdogState, 1); |
33b1db1c MK |
2744 | |
2745 | memcpy(old_s, s, sizeof(BDRVSheepdogState)); | |
2746 | ||
9ff53a0e | 2747 | ret = reload_inode(s, snapid, tag); |
33b1db1c | 2748 | if (ret) { |
33b1db1c MK |
2749 | goto out; |
2750 | } | |
2751 | ||
cede621f LY |
2752 | ret = sd_create_branch(s); |
2753 | if (ret) { | |
33b1db1c MK |
2754 | goto out; |
2755 | } | |
2756 | ||
7267c094 | 2757 | g_free(old_s); |
33b1db1c MK |
2758 | |
2759 | return 0; | |
2760 | out: | |
2761 | /* recover bdrv_sd_state */ | |
2762 | memcpy(s, old_s, sizeof(BDRVSheepdogState)); | |
7267c094 | 2763 | g_free(old_s); |
33b1db1c | 2764 | |
6daf194d | 2765 | error_report("failed to open. recover old bdrv_sd_state."); |
33b1db1c MK |
2766 | |
2767 | return ret; | |
2768 | } | |
2769 | ||
eab8eb8d VT |
2770 | #define NR_BATCHED_DISCARD 128 |
2771 | ||
e25cad69 | 2772 | static int remove_objects(BDRVSheepdogState *s, Error **errp) |
eab8eb8d VT |
2773 | { |
2774 | int fd, i = 0, nr_objs = 0; | |
e25cad69 | 2775 | int ret; |
eab8eb8d VT |
2776 | SheepdogInode *inode = &s->inode; |
2777 | ||
e25cad69 | 2778 | fd = connect_to_sdog(s, errp); |
eab8eb8d | 2779 | if (fd < 0) { |
e25cad69 | 2780 | return fd; |
eab8eb8d VT |
2781 | } |
2782 | ||
2783 | nr_objs = count_data_objs(inode); | |
2784 | while (i < nr_objs) { | |
2785 | int start_idx, nr_filled_idx; | |
2786 | ||
2787 | while (i < nr_objs && !inode->data_vdi_id[i]) { | |
2788 | i++; | |
2789 | } | |
2790 | start_idx = i; | |
2791 | ||
2792 | nr_filled_idx = 0; | |
2793 | while (i < nr_objs && nr_filled_idx < NR_BATCHED_DISCARD) { | |
2794 | if (inode->data_vdi_id[i]) { | |
2795 | inode->data_vdi_id[i] = 0; | |
2796 | nr_filled_idx++; | |
2797 | } | |
2798 | ||
2799 | i++; | |
2800 | } | |
2801 | ||
f11672db | 2802 | ret = write_object(fd, s->bs, |
eab8eb8d VT |
2803 | (char *)&inode->data_vdi_id[start_idx], |
2804 | vid_to_vdi_oid(s->inode.vdi_id), inode->nr_copies, | |
2805 | (i - start_idx) * sizeof(uint32_t), | |
2806 | offsetof(struct SheepdogInode, | |
2807 | data_vdi_id[start_idx]), | |
2808 | false, s->cache_flags); | |
2809 | if (ret < 0) { | |
e25cad69 | 2810 | error_setg(errp, "Failed to discard snapshot inode"); |
eab8eb8d VT |
2811 | goto out; |
2812 | } | |
2813 | } | |
2814 | ||
e25cad69 | 2815 | ret = 0; |
eab8eb8d VT |
2816 | out: |
2817 | closesocket(fd); | |
e25cad69 | 2818 | return ret; |
eab8eb8d VT |
2819 | } |
2820 | ||
a89d89d3 WX |
2821 | static int sd_snapshot_delete(BlockDriverState *bs, |
2822 | const char *snapshot_id, | |
2823 | const char *name, | |
2824 | Error **errp) | |
33b1db1c | 2825 | { |
a0dc0e2b MA |
2826 | /* |
2827 | * FIXME should delete the snapshot matching both @snapshot_id and | |
2828 | * @name, but @name not used here | |
2829 | */ | |
03c698f0 | 2830 | unsigned long snap_id = 0; |
eab8eb8d | 2831 | char snap_tag[SD_MAX_VDI_TAG_LEN]; |
eab8eb8d VT |
2832 | int fd, ret; |
2833 | char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN]; | |
2834 | BDRVSheepdogState *s = bs->opaque; | |
2835 | unsigned int wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN, rlen = 0; | |
2836 | uint32_t vid; | |
2837 | SheepdogVdiReq hdr = { | |
2838 | .opcode = SD_OP_DEL_VDI, | |
2839 | .data_length = wlen, | |
2840 | .flags = SD_FLAG_CMD_WRITE, | |
2841 | }; | |
2842 | SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr; | |
2843 | ||
e25cad69 MA |
2844 | ret = remove_objects(s, errp); |
2845 | if (ret) { | |
2846 | return ret; | |
eab8eb8d VT |
2847 | } |
2848 | ||
2849 | memset(buf, 0, sizeof(buf)); | |
2850 | memset(snap_tag, 0, sizeof(snap_tag)); | |
2851 | pstrcpy(buf, SD_MAX_VDI_LEN, s->name); | |
89e2a31d | 2852 | /* TODO Use sd_parse_snapid() once this mess is cleaned up */ |
03c698f0 JC |
2853 | ret = qemu_strtoul(snapshot_id, NULL, 10, &snap_id); |
2854 | if (ret || snap_id > UINT32_MAX) { | |
a0dc0e2b MA |
2855 | /* |
2856 | * FIXME Since qemu_strtoul() returns -EINVAL when | |
2857 | * @snapshot_id is null, @snapshot_id is mandatory. Correct | |
2858 | * would be to require at least one of @snapshot_id and @name. | |
2859 | */ | |
03c698f0 JC |
2860 | error_setg(errp, "Invalid snapshot ID: %s", |
2861 | snapshot_id ? snapshot_id : "<null>"); | |
2862 | return -EINVAL; | |
eab8eb8d VT |
2863 | } |
2864 | ||
2865 | if (snap_id) { | |
03c698f0 | 2866 | hdr.snapid = (uint32_t) snap_id; |
eab8eb8d | 2867 | } else { |
a0dc0e2b | 2868 | /* FIXME I suspect we should use @name here */ |
89e2a31d | 2869 | /* FIXME don't truncate silently */ |
eab8eb8d VT |
2870 | pstrcpy(snap_tag, sizeof(snap_tag), snapshot_id); |
2871 | pstrcpy(buf + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag); | |
2872 | } | |
2873 | ||
e25cad69 | 2874 | ret = find_vdi_name(s, s->name, snap_id, snap_tag, &vid, true, errp); |
eab8eb8d VT |
2875 | if (ret) { |
2876 | return ret; | |
2877 | } | |
2878 | ||
e25cad69 | 2879 | fd = connect_to_sdog(s, errp); |
eab8eb8d | 2880 | if (fd < 0) { |
e25cad69 | 2881 | return fd; |
eab8eb8d VT |
2882 | } |
2883 | ||
f11672db | 2884 | ret = do_req(fd, s->bs, (SheepdogReq *)&hdr, |
eab8eb8d VT |
2885 | buf, &wlen, &rlen); |
2886 | closesocket(fd); | |
2887 | if (ret) { | |
e25cad69 | 2888 | error_setg_errno(errp, -ret, "Couldn't send request to server"); |
eab8eb8d VT |
2889 | return ret; |
2890 | } | |
2891 | ||
2892 | switch (rsp->result) { | |
2893 | case SD_RES_NO_VDI: | |
e25cad69 MA |
2894 | error_setg(errp, "Can't find the snapshot"); |
2895 | return -ENOENT; | |
eab8eb8d VT |
2896 | case SD_RES_SUCCESS: |
2897 | break; | |
2898 | default: | |
e25cad69 MA |
2899 | error_setg(errp, "%s", sd_strerror(rsp->result)); |
2900 | return -EIO; | |
eab8eb8d VT |
2901 | } |
2902 | ||
e25cad69 | 2903 | return 0; |
33b1db1c MK |
2904 | } |
2905 | ||
33b1db1c MK |
2906 | static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
2907 | { | |
dfb12bf8 | 2908 | Error *local_err = NULL; |
33b1db1c MK |
2909 | BDRVSheepdogState *s = bs->opaque; |
2910 | SheepdogReq req; | |
2911 | int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long); | |
2912 | QEMUSnapshotInfo *sn_tab = NULL; | |
2913 | unsigned wlen, rlen; | |
2914 | int found = 0; | |
68acc99f | 2915 | SheepdogInode *inode; |
33b1db1c MK |
2916 | unsigned long *vdi_inuse; |
2917 | unsigned int start_nr; | |
2918 | uint64_t hval; | |
2919 | uint32_t vid; | |
2920 | ||
7267c094 | 2921 | vdi_inuse = g_malloc(max); |
68acc99f | 2922 | inode = g_malloc(SD_INODE_HEADER_SIZE); |
33b1db1c | 2923 | |
dfb12bf8 | 2924 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2925 | if (fd < 0) { |
565f65d2 | 2926 | error_report_err(local_err); |
cb595887 | 2927 | ret = fd; |
33b1db1c MK |
2928 | goto out; |
2929 | } | |
2930 | ||
2931 | rlen = max; | |
2932 | wlen = 0; | |
2933 | ||
2934 | memset(&req, 0, sizeof(req)); | |
2935 | ||
2936 | req.opcode = SD_OP_READ_VDIS; | |
2937 | req.data_length = max; | |
2938 | ||
f11672db | 2939 | ret = do_req(fd, s->bs, &req, vdi_inuse, &wlen, &rlen); |
33b1db1c MK |
2940 | |
2941 | closesocket(fd); | |
2942 | if (ret) { | |
2943 | goto out; | |
2944 | } | |
2945 | ||
02c4f26b | 2946 | sn_tab = g_new0(QEMUSnapshotInfo, nr); |
33b1db1c MK |
2947 | |
2948 | /* calculate a vdi id with hash function */ | |
2949 | hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT); | |
2950 | start_nr = hval & (SD_NR_VDIS - 1); | |
2951 | ||
dfb12bf8 | 2952 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 2953 | if (fd < 0) { |
565f65d2 | 2954 | error_report_err(local_err); |
cb595887 | 2955 | ret = fd; |
33b1db1c MK |
2956 | goto out; |
2957 | } | |
2958 | ||
2959 | for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) { | |
2960 | if (!test_bit(vid, vdi_inuse)) { | |
2961 | break; | |
2962 | } | |
2963 | ||
2964 | /* we don't need to read entire object */ | |
68acc99f | 2965 | ret = read_object(fd, s->bs, (char *)inode, |
84390bed | 2966 | vid_to_vdi_oid(vid), |
03b036cc | 2967 | 0, SD_INODE_HEADER_SIZE, 0, |
0e7106d8 | 2968 | s->cache_flags); |
33b1db1c MK |
2969 | |
2970 | if (ret) { | |
2971 | continue; | |
2972 | } | |
2973 | ||
68acc99f PB |
2974 | if (!strcmp(inode->name, s->name) && is_snapshot(inode)) { |
2975 | sn_tab[found].date_sec = inode->snap_ctime >> 32; | |
2976 | sn_tab[found].date_nsec = inode->snap_ctime & 0xffffffff; | |
2977 | sn_tab[found].vm_state_size = inode->vm_state_size; | |
2978 | sn_tab[found].vm_clock_nsec = inode->vm_clock_nsec; | |
33b1db1c | 2979 | |
521b2b5d | 2980 | snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), |
68acc99f | 2981 | "%" PRIu32, inode->snap_id); |
3178e275 | 2982 | pstrcpy(sn_tab[found].name, |
68acc99f PB |
2983 | MIN(sizeof(sn_tab[found].name), sizeof(inode->tag)), |
2984 | inode->tag); | |
33b1db1c MK |
2985 | found++; |
2986 | } | |
2987 | } | |
2988 | ||
2989 | closesocket(fd); | |
2990 | out: | |
2991 | *psn_tab = sn_tab; | |
2992 | ||
7267c094 | 2993 | g_free(vdi_inuse); |
68acc99f | 2994 | g_free(inode); |
33b1db1c | 2995 | |
cb595887 MK |
2996 | if (ret < 0) { |
2997 | return ret; | |
2998 | } | |
2999 | ||
33b1db1c MK |
3000 | return found; |
3001 | } | |
3002 | ||
3003 | static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data, | |
3004 | int64_t pos, int size, int load) | |
3005 | { | |
dfb12bf8 | 3006 | Error *local_err = NULL; |
2f536801 MK |
3007 | bool create; |
3008 | int fd, ret = 0, remaining = size; | |
33b1db1c MK |
3009 | unsigned int data_len; |
3010 | uint64_t vmstate_oid; | |
33b1db1c | 3011 | uint64_t offset; |
cede621f LY |
3012 | uint32_t vdi_index; |
3013 | uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id; | |
876eb1b0 | 3014 | uint32_t object_size = (UINT32_C(1) << s->inode.block_size_shift); |
33b1db1c | 3015 | |
dfb12bf8 | 3016 | fd = connect_to_sdog(s, &local_err); |
33b1db1c | 3017 | if (fd < 0) { |
565f65d2 | 3018 | error_report_err(local_err); |
cb595887 | 3019 | return fd; |
33b1db1c MK |
3020 | } |
3021 | ||
6f3c714e | 3022 | while (remaining) { |
876eb1b0 TI |
3023 | vdi_index = pos / object_size; |
3024 | offset = pos % object_size; | |
33b1db1c | 3025 | |
876eb1b0 | 3026 | data_len = MIN(remaining, object_size - offset); |
33b1db1c | 3027 | |
cede621f | 3028 | vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index); |
33b1db1c MK |
3029 | |
3030 | create = (offset == 0); | |
3031 | if (load) { | |
f11672db | 3032 | ret = read_object(fd, s->bs, (char *)data, vmstate_oid, |
47622c44 | 3033 | s->inode.nr_copies, data_len, offset, |
0e7106d8 | 3034 | s->cache_flags); |
33b1db1c | 3035 | } else { |
f11672db | 3036 | ret = write_object(fd, s->bs, (char *)data, vmstate_oid, |
47622c44 | 3037 | s->inode.nr_copies, data_len, offset, create, |
0e7106d8 | 3038 | s->cache_flags); |
33b1db1c MK |
3039 | } |
3040 | ||
3041 | if (ret < 0) { | |
6daf194d | 3042 | error_report("failed to save vmstate %s", strerror(errno)); |
33b1db1c MK |
3043 | goto cleanup; |
3044 | } | |
3045 | ||
3046 | pos += data_len; | |
1f7a48de | 3047 | data += data_len; |
6f3c714e | 3048 | remaining -= data_len; |
33b1db1c | 3049 | } |
6f3c714e | 3050 | ret = size; |
33b1db1c MK |
3051 | cleanup: |
3052 | closesocket(fd); | |
3053 | return ret; | |
3054 | } | |
3055 | ||
cf8074b3 KW |
3056 | static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
3057 | int64_t pos) | |
33b1db1c MK |
3058 | { |
3059 | BDRVSheepdogState *s = bs->opaque; | |
cf8074b3 KW |
3060 | void *buf; |
3061 | int ret; | |
33b1db1c | 3062 | |
cf8074b3 KW |
3063 | buf = qemu_blockalign(bs, qiov->size); |
3064 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); | |
3065 | ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0); | |
3066 | qemu_vfree(buf); | |
3067 | ||
3068 | return ret; | |
33b1db1c MK |
3069 | } |
3070 | ||
5ddda0b8 KW |
3071 | static int sd_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
3072 | int64_t pos) | |
33b1db1c MK |
3073 | { |
3074 | BDRVSheepdogState *s = bs->opaque; | |
5ddda0b8 KW |
3075 | void *buf; |
3076 | int ret; | |
33b1db1c | 3077 | |
5ddda0b8 KW |
3078 | buf = qemu_blockalign(bs, qiov->size); |
3079 | ret = do_load_save_vmstate(s, buf, pos, qiov->size, 1); | |
3080 | qemu_iovec_from_buf(qiov, 0, buf, qiov->size); | |
3081 | qemu_vfree(buf); | |
3082 | ||
3083 | return ret; | |
33b1db1c MK |
3084 | } |
3085 | ||
3086 | ||
dde47537 | 3087 | static coroutine_fn int sd_co_pdiscard(BlockDriverState *bs, int64_t offset, |
f5a5ca79 | 3088 | int bytes) |
cac8f4a6 | 3089 | { |
28ddd08c | 3090 | SheepdogAIOCB acb; |
cac8f4a6 | 3091 | BDRVSheepdogState *s = bs->opaque; |
e6fd57ea HM |
3092 | QEMUIOVector discard_iov; |
3093 | struct iovec iov; | |
3094 | uint32_t zero = 0; | |
cac8f4a6 LY |
3095 | |
3096 | if (!s->discard_supported) { | |
dde47537 | 3097 | return 0; |
cac8f4a6 LY |
3098 | } |
3099 | ||
e6fd57ea HM |
3100 | memset(&discard_iov, 0, sizeof(discard_iov)); |
3101 | memset(&iov, 0, sizeof(iov)); | |
3102 | iov.iov_base = &zero; | |
3103 | iov.iov_len = sizeof(zero); | |
3104 | discard_iov.iov = &iov; | |
3105 | discard_iov.niov = 1; | |
f5a5ca79 | 3106 | if (!QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE)) { |
49228d1e EB |
3107 | return -ENOTSUP; |
3108 | } | |
28ddd08c | 3109 | sd_aio_setup(&acb, s, &discard_iov, offset >> BDRV_SECTOR_BITS, |
f5a5ca79 | 3110 | bytes >> BDRV_SECTOR_BITS, AIOCB_DISCARD_OBJ); |
28ddd08c | 3111 | sd_co_rw_vector(&acb); |
acf6e5f0 | 3112 | sd_aio_complete(&acb); |
cac8f4a6 | 3113 | |
28ddd08c | 3114 | return acb.ret; |
cac8f4a6 LY |
3115 | } |
3116 | ||
47943e98 EB |
3117 | static coroutine_fn int |
3118 | sd_co_block_status(BlockDriverState *bs, bool want_zero, int64_t offset, | |
3119 | int64_t bytes, int64_t *pnum, int64_t *map, | |
3120 | BlockDriverState **file) | |
8d71c631 LY |
3121 | { |
3122 | BDRVSheepdogState *s = bs->opaque; | |
3123 | SheepdogInode *inode = &s->inode; | |
876eb1b0 | 3124 | uint32_t object_size = (UINT32_C(1) << inode->block_size_shift); |
876eb1b0 | 3125 | unsigned long start = offset / object_size, |
47943e98 | 3126 | end = DIV_ROUND_UP(offset + bytes, object_size); |
8d71c631 | 3127 | unsigned long idx; |
47943e98 EB |
3128 | *map = offset; |
3129 | int ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
8d71c631 LY |
3130 | |
3131 | for (idx = start; idx < end; idx++) { | |
3132 | if (inode->data_vdi_id[idx] == 0) { | |
3133 | break; | |
3134 | } | |
3135 | } | |
3136 | if (idx == start) { | |
3137 | /* Get the longest length of unallocated sectors */ | |
3138 | ret = 0; | |
3139 | for (idx = start + 1; idx < end; idx++) { | |
3140 | if (inode->data_vdi_id[idx] != 0) { | |
3141 | break; | |
3142 | } | |
3143 | } | |
3144 | } | |
3145 | ||
47943e98 EB |
3146 | *pnum = (idx - start) * object_size; |
3147 | if (*pnum > bytes) { | |
3148 | *pnum = bytes; | |
8d71c631 | 3149 | } |
d234c929 FZ |
3150 | if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) { |
3151 | *file = bs; | |
3152 | } | |
8d71c631 LY |
3153 | return ret; |
3154 | } | |
3155 | ||
85829722 LY |
3156 | static int64_t sd_get_allocated_file_size(BlockDriverState *bs) |
3157 | { | |
3158 | BDRVSheepdogState *s = bs->opaque; | |
3159 | SheepdogInode *inode = &s->inode; | |
876eb1b0 TI |
3160 | uint32_t object_size = (UINT32_C(1) << inode->block_size_shift); |
3161 | unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, object_size); | |
85829722 LY |
3162 | uint64_t size = 0; |
3163 | ||
3164 | for (i = 0; i < last; i++) { | |
3165 | if (inode->data_vdi_id[i] == 0) { | |
3166 | continue; | |
3167 | } | |
876eb1b0 | 3168 | size += object_size; |
85829722 LY |
3169 | } |
3170 | return size; | |
3171 | } | |
3172 | ||
b222237b CL |
3173 | static QemuOptsList sd_create_opts = { |
3174 | .name = "sheepdog-create-opts", | |
3175 | .head = QTAILQ_HEAD_INITIALIZER(sd_create_opts.head), | |
3176 | .desc = { | |
3177 | { | |
3178 | .name = BLOCK_OPT_SIZE, | |
3179 | .type = QEMU_OPT_SIZE, | |
3180 | .help = "Virtual disk size" | |
3181 | }, | |
3182 | { | |
3183 | .name = BLOCK_OPT_BACKING_FILE, | |
3184 | .type = QEMU_OPT_STRING, | |
3185 | .help = "File name of a base image" | |
3186 | }, | |
3187 | { | |
3188 | .name = BLOCK_OPT_PREALLOC, | |
3189 | .type = QEMU_OPT_STRING, | |
3190 | .help = "Preallocation mode (allowed values: off, full)" | |
3191 | }, | |
3192 | { | |
3193 | .name = BLOCK_OPT_REDUNDANCY, | |
3194 | .type = QEMU_OPT_STRING, | |
3195 | .help = "Redundancy of the image" | |
3196 | }, | |
876eb1b0 TI |
3197 | { |
3198 | .name = BLOCK_OPT_OBJECT_SIZE, | |
3199 | .type = QEMU_OPT_SIZE, | |
3200 | .help = "Object size of the image" | |
3201 | }, | |
b222237b CL |
3202 | { /* end of list */ } |
3203 | } | |
33b1db1c MK |
3204 | }; |
3205 | ||
5d6768e3 | 3206 | static BlockDriver bdrv_sheepdog = { |
d507c5f6 JC |
3207 | .format_name = "sheepdog", |
3208 | .protocol_name = "sheepdog", | |
3209 | .instance_size = sizeof(BDRVSheepdogState), | |
3210 | .bdrv_parse_filename = sd_parse_filename, | |
3211 | .bdrv_file_open = sd_open, | |
3212 | .bdrv_reopen_prepare = sd_reopen_prepare, | |
3213 | .bdrv_reopen_commit = sd_reopen_commit, | |
3214 | .bdrv_reopen_abort = sd_reopen_abort, | |
3215 | .bdrv_close = sd_close, | |
63fd65a0 | 3216 | .bdrv_co_create = sd_co_create, |
efc75e2a | 3217 | .bdrv_co_create_opts = sd_co_create_opts, |
d507c5f6 JC |
3218 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
3219 | .bdrv_getlength = sd_getlength, | |
85829722 | 3220 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
061ca8a3 | 3221 | .bdrv_co_truncate = sd_co_truncate, |
33b1db1c | 3222 | |
d507c5f6 JC |
3223 | .bdrv_co_readv = sd_co_readv, |
3224 | .bdrv_co_writev = sd_co_writev, | |
3225 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
3226 | .bdrv_co_pdiscard = sd_co_pdiscard, | |
47943e98 | 3227 | .bdrv_co_block_status = sd_co_block_status, |
33b1db1c | 3228 | |
d507c5f6 JC |
3229 | .bdrv_snapshot_create = sd_snapshot_create, |
3230 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
3231 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
3232 | .bdrv_snapshot_list = sd_snapshot_list, | |
33b1db1c | 3233 | |
d507c5f6 JC |
3234 | .bdrv_save_vmstate = sd_save_vmstate, |
3235 | .bdrv_load_vmstate = sd_load_vmstate, | |
33b1db1c | 3236 | |
d507c5f6 JC |
3237 | .bdrv_detach_aio_context = sd_detach_aio_context, |
3238 | .bdrv_attach_aio_context = sd_attach_aio_context, | |
84390bed | 3239 | |
d507c5f6 | 3240 | .create_opts = &sd_create_opts, |
33b1db1c MK |
3241 | }; |
3242 | ||
5d6768e3 | 3243 | static BlockDriver bdrv_sheepdog_tcp = { |
d507c5f6 JC |
3244 | .format_name = "sheepdog", |
3245 | .protocol_name = "sheepdog+tcp", | |
3246 | .instance_size = sizeof(BDRVSheepdogState), | |
3247 | .bdrv_parse_filename = sd_parse_filename, | |
3248 | .bdrv_file_open = sd_open, | |
3249 | .bdrv_reopen_prepare = sd_reopen_prepare, | |
3250 | .bdrv_reopen_commit = sd_reopen_commit, | |
3251 | .bdrv_reopen_abort = sd_reopen_abort, | |
3252 | .bdrv_close = sd_close, | |
63fd65a0 | 3253 | .bdrv_co_create = sd_co_create, |
efc75e2a | 3254 | .bdrv_co_create_opts = sd_co_create_opts, |
d507c5f6 JC |
3255 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
3256 | .bdrv_getlength = sd_getlength, | |
85829722 | 3257 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
061ca8a3 | 3258 | .bdrv_co_truncate = sd_co_truncate, |
5d6768e3 | 3259 | |
d507c5f6 JC |
3260 | .bdrv_co_readv = sd_co_readv, |
3261 | .bdrv_co_writev = sd_co_writev, | |
3262 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
3263 | .bdrv_co_pdiscard = sd_co_pdiscard, | |
47943e98 | 3264 | .bdrv_co_block_status = sd_co_block_status, |
5d6768e3 | 3265 | |
d507c5f6 JC |
3266 | .bdrv_snapshot_create = sd_snapshot_create, |
3267 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
3268 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
3269 | .bdrv_snapshot_list = sd_snapshot_list, | |
5d6768e3 | 3270 | |
d507c5f6 JC |
3271 | .bdrv_save_vmstate = sd_save_vmstate, |
3272 | .bdrv_load_vmstate = sd_load_vmstate, | |
5d6768e3 | 3273 | |
d507c5f6 JC |
3274 | .bdrv_detach_aio_context = sd_detach_aio_context, |
3275 | .bdrv_attach_aio_context = sd_attach_aio_context, | |
84390bed | 3276 | |
d507c5f6 | 3277 | .create_opts = &sd_create_opts, |
5d6768e3 MK |
3278 | }; |
3279 | ||
1b8bbb46 | 3280 | static BlockDriver bdrv_sheepdog_unix = { |
d507c5f6 JC |
3281 | .format_name = "sheepdog", |
3282 | .protocol_name = "sheepdog+unix", | |
3283 | .instance_size = sizeof(BDRVSheepdogState), | |
3284 | .bdrv_parse_filename = sd_parse_filename, | |
3285 | .bdrv_file_open = sd_open, | |
3286 | .bdrv_reopen_prepare = sd_reopen_prepare, | |
3287 | .bdrv_reopen_commit = sd_reopen_commit, | |
3288 | .bdrv_reopen_abort = sd_reopen_abort, | |
3289 | .bdrv_close = sd_close, | |
63fd65a0 | 3290 | .bdrv_co_create = sd_co_create, |
efc75e2a | 3291 | .bdrv_co_create_opts = sd_co_create_opts, |
d507c5f6 JC |
3292 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
3293 | .bdrv_getlength = sd_getlength, | |
85829722 | 3294 | .bdrv_get_allocated_file_size = sd_get_allocated_file_size, |
061ca8a3 | 3295 | .bdrv_co_truncate = sd_co_truncate, |
1b8bbb46 | 3296 | |
d507c5f6 JC |
3297 | .bdrv_co_readv = sd_co_readv, |
3298 | .bdrv_co_writev = sd_co_writev, | |
3299 | .bdrv_co_flush_to_disk = sd_co_flush_to_disk, | |
3300 | .bdrv_co_pdiscard = sd_co_pdiscard, | |
47943e98 | 3301 | .bdrv_co_block_status = sd_co_block_status, |
1b8bbb46 | 3302 | |
d507c5f6 JC |
3303 | .bdrv_snapshot_create = sd_snapshot_create, |
3304 | .bdrv_snapshot_goto = sd_snapshot_goto, | |
3305 | .bdrv_snapshot_delete = sd_snapshot_delete, | |
3306 | .bdrv_snapshot_list = sd_snapshot_list, | |
1b8bbb46 | 3307 | |
d507c5f6 JC |
3308 | .bdrv_save_vmstate = sd_save_vmstate, |
3309 | .bdrv_load_vmstate = sd_load_vmstate, | |
1b8bbb46 | 3310 | |
d507c5f6 JC |
3311 | .bdrv_detach_aio_context = sd_detach_aio_context, |
3312 | .bdrv_attach_aio_context = sd_attach_aio_context, | |
84390bed | 3313 | |
d507c5f6 | 3314 | .create_opts = &sd_create_opts, |
1b8bbb46 MK |
3315 | }; |
3316 | ||
33b1db1c MK |
3317 | static void bdrv_sheepdog_init(void) |
3318 | { | |
3319 | bdrv_register(&bdrv_sheepdog); | |
5d6768e3 | 3320 | bdrv_register(&bdrv_sheepdog_tcp); |
1b8bbb46 | 3321 | bdrv_register(&bdrv_sheepdog_unix); |
33b1db1c MK |
3322 | } |
3323 | block_init(bdrv_sheepdog_init); |