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