blob: 8739accddd7dd214e25a64e35747db97c5e04dc4 [file] [log] [blame]
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001/*
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/>.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010010 *
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.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090013 */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090014
Peter Maydell80c71a22016-01-18 18:01:42 +000015#include "qemu/osdep.h"
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090016#include "qemu-common.h"
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +090017#include "qemu/uri.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/error-report.h"
19#include "qemu/sockets.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010020#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/bitops.h"
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090022
23#define SD_PROTO_VER 0x01
24
25#define SD_DEFAULT_ADDR "localhost"
MORITA Kazutaka25af2572013-02-22 12:39:52 +090026#define SD_DEFAULT_PORT 7000
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090027
28#define SD_OP_CREATE_AND_WRITE_OBJ 0x01
29#define SD_OP_READ_OBJ 0x02
30#define SD_OP_WRITE_OBJ 0x03
Liu Yuancac8f4a2013-04-23 14:03:33 +080031/* 0x04 is used internally by Sheepdog */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090032
33#define SD_OP_NEW_VDI 0x11
34#define SD_OP_LOCK_VDI 0x12
35#define SD_OP_RELEASE_VDI 0x13
36#define SD_OP_GET_VDI_INFO 0x14
37#define SD_OP_READ_VDIS 0x15
Liu Yuan47622c42012-04-04 04:03:58 +080038#define SD_OP_FLUSH_VDI 0x16
Liu Yuan859e5552013-04-25 20:49:39 +080039#define SD_OP_DEL_VDI 0x17
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +090040#define SD_OP_GET_CLUSTER_DEFAULT 0x18
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090041
42#define SD_FLAG_CMD_WRITE 0x01
43#define SD_FLAG_CMD_COW 0x02
Liu Yuan0e7106d2013-01-10 16:03:47 +080044#define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
45#define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090046
47#define SD_RES_SUCCESS 0x00 /* Success */
48#define SD_RES_UNKNOWN 0x01 /* Unknown error */
49#define SD_RES_NO_OBJ 0x02 /* No object found */
50#define SD_RES_EIO 0x03 /* I/O error */
51#define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
52#define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
53#define SD_RES_SYSTEM_ERROR 0x06 /* System error */
54#define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
55#define SD_RES_NO_VDI 0x08 /* No vdi found */
56#define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
57#define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
58#define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
59#define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
60#define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
61#define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
62#define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
63#define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
64#define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
65#define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
66#define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
67#define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
68#define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
69#define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
70#define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
71#define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
Liu Yuanfca23f02013-03-18 14:27:55 +080072#define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
MORITA Kazutaka6a0b5492013-04-26 01:19:52 +090073#define SD_RES_READONLY 0x1A /* Object is read-only */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090074
75/*
76 * Object ID rules
77 *
78 * 0 - 19 (20 bits): data object space
79 * 20 - 31 (12 bits): reserved data object space
80 * 32 - 55 (24 bits): vdi object space
81 * 56 - 59 ( 4 bits): reserved vdi object space
Dong Xu Wang7acae202011-10-14 15:41:06 +080082 * 60 - 63 ( 4 bits): object type identifier space
MORITA Kazutaka33b1db12010-06-21 05:01:00 +090083 */
84
85#define VDI_SPACE_SHIFT 32
86#define VDI_BIT (UINT64_C(1) << 63)
87#define VMSTATE_BIT (UINT64_C(1) << 62)
88#define MAX_DATA_OBJS (UINT64_C(1) << 20)
89#define MAX_CHILDREN 1024
90#define SD_MAX_VDI_LEN 256
91#define SD_MAX_VDI_TAG_LEN 256
92#define SD_NR_VDIS (1U << 24)
93#define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
94#define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +090095#define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
Liu Yuanb3af0182013-11-07 22:56:38 +080096/*
97 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
98 * (SD_EC_MAX_STRIP - 1) for parity strips
99 *
100 * SD_MAX_COPIES is sum of number of data strips and parity strips.
101 */
102#define SD_EC_MAX_STRIP 16
103#define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900104
105#define SD_INODE_SIZE (sizeof(SheepdogInode))
106#define CURRENT_VDI_ID 0
107
Hitoshi Mitake1dbfafe2014-08-11 14:43:45 +0900108#define LOCK_TYPE_NORMAL 0
109#define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
110
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900111typedef struct SheepdogReq {
112 uint8_t proto_ver;
113 uint8_t opcode;
114 uint16_t flags;
115 uint32_t epoch;
116 uint32_t id;
117 uint32_t data_length;
118 uint32_t opcode_specific[8];
119} SheepdogReq;
120
121typedef struct SheepdogRsp {
122 uint8_t proto_ver;
123 uint8_t opcode;
124 uint16_t flags;
125 uint32_t epoch;
126 uint32_t id;
127 uint32_t data_length;
128 uint32_t result;
129 uint32_t opcode_specific[7];
130} SheepdogRsp;
131
132typedef struct SheepdogObjReq {
133 uint8_t proto_ver;
134 uint8_t opcode;
135 uint16_t flags;
136 uint32_t epoch;
137 uint32_t id;
138 uint32_t data_length;
139 uint64_t oid;
140 uint64_t cow_oid;
Liu Yuan29a67f72013-10-23 16:51:51 +0800141 uint8_t copies;
Liu Yuan1841f882013-10-23 16:51:52 +0800142 uint8_t copy_policy;
143 uint8_t reserved[6];
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900144 uint64_t offset;
145} SheepdogObjReq;
146
147typedef struct SheepdogObjRsp {
148 uint8_t proto_ver;
149 uint8_t opcode;
150 uint16_t flags;
151 uint32_t epoch;
152 uint32_t id;
153 uint32_t data_length;
154 uint32_t result;
Liu Yuan29a67f72013-10-23 16:51:51 +0800155 uint8_t copies;
Liu Yuan1841f882013-10-23 16:51:52 +0800156 uint8_t copy_policy;
157 uint8_t reserved[2];
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900158 uint32_t pad[6];
159} SheepdogObjRsp;
160
161typedef struct SheepdogVdiReq {
162 uint8_t proto_ver;
163 uint8_t opcode;
164 uint16_t flags;
165 uint32_t epoch;
166 uint32_t id;
167 uint32_t data_length;
168 uint64_t vdi_size;
Liu Yuan9f23fce2014-01-03 20:13:12 +0800169 uint32_t base_vdi_id;
Liu Yuan29a67f72013-10-23 16:51:51 +0800170 uint8_t copies;
Liu Yuan1841f882013-10-23 16:51:52 +0800171 uint8_t copy_policy;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +0900172 uint8_t store_policy;
173 uint8_t block_size_shift;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900174 uint32_t snapid;
Hitoshi Mitake1dbfafe2014-08-11 14:43:45 +0900175 uint32_t type;
176 uint32_t pad[2];
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900177} SheepdogVdiReq;
178
179typedef struct SheepdogVdiRsp {
180 uint8_t proto_ver;
181 uint8_t opcode;
182 uint16_t flags;
183 uint32_t epoch;
184 uint32_t id;
185 uint32_t data_length;
186 uint32_t result;
187 uint32_t rsvd;
188 uint32_t vdi_id;
189 uint32_t pad[5];
190} SheepdogVdiRsp;
191
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +0900192typedef struct SheepdogClusterRsp {
193 uint8_t proto_ver;
194 uint8_t opcode;
195 uint16_t flags;
196 uint32_t epoch;
197 uint32_t id;
198 uint32_t data_length;
199 uint32_t result;
200 uint8_t nr_copies;
201 uint8_t copy_policy;
202 uint8_t block_size_shift;
203 uint8_t __pad1;
204 uint32_t __pad2[6];
205} SheepdogClusterRsp;
206
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900207typedef struct SheepdogInode {
208 char name[SD_MAX_VDI_LEN];
209 char tag[SD_MAX_VDI_TAG_LEN];
210 uint64_t ctime;
211 uint64_t snap_ctime;
212 uint64_t vm_clock_nsec;
213 uint64_t vdi_size;
214 uint64_t vm_state_size;
215 uint16_t copy_policy;
216 uint8_t nr_copies;
217 uint8_t block_size_shift;
218 uint32_t snap_id;
219 uint32_t vdi_id;
220 uint32_t parent_vdi_id;
221 uint32_t child_vdi_id[MAX_CHILDREN];
222 uint32_t data_vdi_id[MAX_DATA_OBJS];
223} SheepdogInode;
224
Hitoshi Mitake5d039ba2014-06-06 13:35:12 +0900225#define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
226
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900227/*
228 * 64 bit FNV-1a non-zero initial basis
229 */
230#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
231
232/*
233 * 64 bit Fowler/Noll/Vo FNV-1a hash code
234 */
235static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
236{
237 unsigned char *bp = buf;
238 unsigned char *be = bp + len;
239 while (bp < be) {
240 hval ^= (uint64_t) *bp++;
241 hval += (hval << 1) + (hval << 4) + (hval << 5) +
242 (hval << 7) + (hval << 8) + (hval << 40);
243 }
244 return hval;
245}
246
MORITA Kazutaka2f536802012-10-07 01:57:14 +0900247static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900248{
249 return inode->vdi_id == inode->data_vdi_id[idx];
250}
251
MORITA Kazutaka2f536802012-10-07 01:57:14 +0900252static inline bool is_data_obj(uint64_t oid)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900253{
254 return !(VDI_BIT & oid);
255}
256
257static inline uint64_t data_oid_to_idx(uint64_t oid)
258{
259 return oid & (MAX_DATA_OBJS - 1);
260}
261
MORITA Kazutaka72e09962013-10-24 16:01:13 +0900262static inline uint32_t oid_to_vid(uint64_t oid)
263{
264 return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT;
265}
266
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900267static inline uint64_t vid_to_vdi_oid(uint32_t vid)
268{
269 return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
270}
271
272static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
273{
274 return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
275}
276
277static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
278{
279 return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
280}
281
MORITA Kazutaka2f536802012-10-07 01:57:14 +0900282static inline bool is_snapshot(struct SheepdogInode *inode)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900283{
284 return !!inode->snap_ctime;
285}
286
Vasiliy Tolstoveab8eb82015-12-23 21:22:26 +0900287static inline size_t count_data_objs(const struct SheepdogInode *inode)
288{
289 return DIV_ROUND_UP(inode->vdi_size,
290 (1UL << inode->block_size_shift));
291}
292
Peter Maydell2440a2c2013-07-29 13:16:36 +0100293#undef DPRINTF
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900294#ifdef DEBUG_SDOG
Peter Maydell2440a2c2013-07-29 13:16:36 +0100295#define DPRINTF(fmt, args...) \
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900296 do { \
297 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
298 } while (0)
299#else
Peter Maydell2440a2c2013-07-29 13:16:36 +0100300#define DPRINTF(fmt, args...)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900301#endif
302
303typedef struct SheepdogAIOCB SheepdogAIOCB;
304
305typedef struct AIOReq {
306 SheepdogAIOCB *aiocb;
307 unsigned int iov_offset;
308
309 uint64_t oid;
310 uint64_t base_oid;
311 uint64_t offset;
312 unsigned int data_len;
313 uint8_t flags;
314 uint32_t id;
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +0900315 bool create;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900316
MORITA Kazutakac292ee62012-06-27 07:26:22 +0900317 QLIST_ENTRY(AIOReq) aio_siblings;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900318} AIOReq;
319
320enum AIOCBState {
321 AIOCB_WRITE_UDATA,
322 AIOCB_READ_UDATA,
Liu Yuan47783072013-01-15 16:28:55 +0800323 AIOCB_FLUSH_CACHE,
Liu Yuancac8f4a2013-04-23 14:03:33 +0800324 AIOCB_DISCARD_OBJ,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900325};
326
Hitoshi Mitake498f2142015-09-01 12:03:09 +0900327#define AIOCBOverlapping(x, y) \
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900328 (!(x->max_affect_data_idx < y->min_affect_data_idx \
329 || y->max_affect_data_idx < x->min_affect_data_idx))
330
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900331struct SheepdogAIOCB {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200332 BlockAIOCB common;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900333
334 QEMUIOVector *qiov;
335
336 int64_t sector_num;
337 int nb_sectors;
338
339 int ret;
340 enum AIOCBState aiocb_type;
341
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900342 Coroutine *coroutine;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900343 void (*aio_done_func)(SheepdogAIOCB *);
344
MORITA Kazutaka35200682013-10-24 16:01:17 +0900345 bool cancelable;
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900346 int nr_pending;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900347
348 uint32_t min_affect_data_idx;
349 uint32_t max_affect_data_idx;
350
Hitoshi Mitake498f2142015-09-01 12:03:09 +0900351 /*
352 * The difference between affect_data_idx and dirty_data_idx:
353 * affect_data_idx represents range of index of all request types.
354 * dirty_data_idx represents range of index updated by COW requests.
355 * dirty_data_idx is used for updating an inode object.
356 */
357 uint32_t min_dirty_data_idx;
358 uint32_t max_dirty_data_idx;
359
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900360 QLIST_ENTRY(SheepdogAIOCB) aiocb_siblings;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900361};
362
363typedef struct BDRVSheepdogState {
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900364 BlockDriverState *bs;
Stefan Hajnoczi84390be2014-05-08 16:34:52 +0200365 AioContext *aio_context;
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900366
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900367 SheepdogInode inode;
368
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900369 char name[SD_MAX_VDI_LEN];
MORITA Kazutaka2f536802012-10-07 01:57:14 +0900370 bool is_snapshot;
Liu Yuan0e7106d2013-01-10 16:03:47 +0800371 uint32_t cache_flags;
Liu Yuancac8f4a2013-04-23 14:03:33 +0800372 bool discard_supported;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900373
MORITA Kazutaka25af2572013-02-22 12:39:52 +0900374 char *host_spec;
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900375 bool is_unix;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900376 int fd;
377
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900378 CoMutex lock;
379 Coroutine *co_send;
380 Coroutine *co_recv;
381
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900382 uint32_t aioreq_seq_num;
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900383
384 /* Every aio request must be linked to either of these queues. */
MORITA Kazutakac292ee62012-06-27 07:26:22 +0900385 QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900386 QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900387
Hitoshi Mitake498f2142015-09-01 12:03:09 +0900388 CoQueue overlapping_queue;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900389 QLIST_HEAD(inflight_aiocb_head, SheepdogAIOCB) inflight_aiocb_head;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900390} BDRVSheepdogState;
391
Liu Yuan4da65c82015-08-28 10:53:58 +0800392typedef struct BDRVSheepdogReopenState {
393 int fd;
394 int cache_flags;
395} BDRVSheepdogReopenState;
396
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900397static const char * sd_strerror(int err)
398{
399 int i;
400
401 static const struct {
402 int err;
403 const char *desc;
404 } errors[] = {
405 {SD_RES_SUCCESS, "Success"},
406 {SD_RES_UNKNOWN, "Unknown error"},
407 {SD_RES_NO_OBJ, "No object found"},
408 {SD_RES_EIO, "I/O error"},
409 {SD_RES_VDI_EXIST, "VDI exists already"},
410 {SD_RES_INVALID_PARMS, "Invalid parameters"},
411 {SD_RES_SYSTEM_ERROR, "System error"},
412 {SD_RES_VDI_LOCKED, "VDI is already locked"},
413 {SD_RES_NO_VDI, "No vdi found"},
414 {SD_RES_NO_BASE_VDI, "No base VDI found"},
415 {SD_RES_VDI_READ, "Failed read the requested VDI"},
416 {SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
417 {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
418 {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
419 {SD_RES_NO_TAG, "Failed to find the requested tag"},
420 {SD_RES_STARTUP, "The system is still booting"},
421 {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
422 {SD_RES_SHUTDOWN, "The system is shutting down"},
423 {SD_RES_NO_MEM, "Out of memory on the server"},
424 {SD_RES_FULL_VDI, "We already have the maximum vdis"},
425 {SD_RES_VER_MISMATCH, "Protocol version mismatch"},
426 {SD_RES_NO_SPACE, "Server has no space for new objects"},
427 {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
428 {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
429 {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
Liu Yuanfca23f02013-03-18 14:27:55 +0800430 {SD_RES_HALT, "Sheepdog is stopped serving IO request"},
MORITA Kazutaka6a0b5492013-04-26 01:19:52 +0900431 {SD_RES_READONLY, "Object is read-only"},
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900432 };
433
434 for (i = 0; i < ARRAY_SIZE(errors); ++i) {
435 if (errors[i].err == err) {
436 return errors[i].desc;
437 }
438 }
439
440 return "Invalid error code";
441}
442
443/*
444 * Sheepdog I/O handling:
445 *
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900446 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
MORITA Kazutakac292ee62012-06-27 07:26:22 +0900447 * link the requests to the inflight_list in the
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900448 * BDRVSheepdogState. The function exits without waiting for
449 * receiving the response.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900450 *
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900451 * 2. We receive the response in aio_read_response, the fd handler to
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900452 * the sheepdog connection. If metadata update is needed, we send
453 * the write request to the vdi object in sd_write_done, the write
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900454 * completion function. We switch back to sd_co_readv/writev after
455 * all the requests belonging to the AIOCB are finished.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900456 */
457
458static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
459 uint64_t oid, unsigned int data_len,
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +0900460 uint64_t offset, uint8_t flags, bool create,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900461 uint64_t base_oid, unsigned int iov_offset)
462{
463 AIOReq *aio_req;
464
Anthony Liguori7267c092011-08-20 22:09:37 -0500465 aio_req = g_malloc(sizeof(*aio_req));
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900466 aio_req->aiocb = acb;
467 aio_req->iov_offset = iov_offset;
468 aio_req->oid = oid;
469 aio_req->base_oid = base_oid;
470 aio_req->offset = offset;
471 aio_req->data_len = data_len;
472 aio_req->flags = flags;
473 aio_req->id = s->aioreq_seq_num++;
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +0900474 aio_req->create = create;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900475
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900476 acb->nr_pending++;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900477 return aio_req;
478}
479
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900480static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900481{
482 SheepdogAIOCB *acb = aio_req->aiocb;
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900483
MORITA Kazutaka35200682013-10-24 16:01:17 +0900484 acb->cancelable = false;
MORITA Kazutakac292ee62012-06-27 07:26:22 +0900485 QLIST_REMOVE(aio_req, aio_siblings);
Anthony Liguori7267c092011-08-20 22:09:37 -0500486 g_free(aio_req);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900487
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900488 acb->nr_pending--;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900489}
490
Paolo Bonzinid8716b42011-10-05 09:17:31 +0200491static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900492{
MORITA Kazutaka35200682013-10-24 16:01:17 +0900493 qemu_coroutine_enter(acb->coroutine, NULL);
Fam Zheng80074292014-09-11 13:41:28 +0800494 qemu_aio_unref(acb);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900495}
496
MORITA Kazutaka35200682013-10-24 16:01:17 +0900497/*
498 * Check whether the specified acb can be canceled
499 *
500 * We can cancel aio when any request belonging to the acb is:
501 * - Not processed by the sheepdog server.
502 * - Not linked to the inflight queue.
503 */
504static bool sd_acb_cancelable(const SheepdogAIOCB *acb)
505{
506 BDRVSheepdogState *s = acb->common.bs->opaque;
507 AIOReq *aioreq;
508
509 if (!acb->cancelable) {
510 return false;
511 }
512
513 QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) {
514 if (aioreq->aiocb == acb) {
515 return false;
516 }
517 }
518
519 return true;
520}
521
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200522static void sd_aio_cancel(BlockAIOCB *blockacb)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900523{
524 SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb;
MORITA Kazutaka35200682013-10-24 16:01:17 +0900525 BDRVSheepdogState *s = acb->common.bs->opaque;
526 AIOReq *aioreq, *next;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900527
Fam Zheng6d24b4d2014-09-11 13:41:24 +0800528 if (sd_acb_cancelable(acb)) {
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900529 /* Remove outstanding requests from failed queue. */
Fam Zheng6d24b4d2014-09-11 13:41:24 +0800530 QLIST_FOREACH_SAFE(aioreq, &s->failed_aio_head, aio_siblings,
531 next) {
532 if (aioreq->aiocb == acb) {
533 free_aio_req(s, aioreq);
534 }
535 }
536
537 assert(acb->nr_pending == 0);
538 if (acb->common.cb) {
539 acb->common.cb(acb->common.opaque, -ECANCELED);
540 }
541 sd_finish_aiocb(acb);
MORITA Kazutaka35200682013-10-24 16:01:17 +0900542 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900543}
544
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100545static const AIOCBInfo sd_aiocb_info = {
Fam Zheng6d24b4d2014-09-11 13:41:24 +0800546 .aiocb_size = sizeof(SheepdogAIOCB),
547 .cancel_async = sd_aio_cancel,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900548};
549
550static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
Liu Yuanf700f8e2013-01-14 14:01:03 +0800551 int64_t sector_num, int nb_sectors)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900552{
553 SheepdogAIOCB *acb;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900554 uint32_t object_size;
555 BDRVSheepdogState *s = bs->opaque;
556
557 object_size = (UINT32_C(1) << s->inode.block_size_shift);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900558
Liu Yuanf700f8e2013-01-14 14:01:03 +0800559 acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900560
561 acb->qiov = qiov;
562
563 acb->sector_num = sector_num;
564 acb->nb_sectors = nb_sectors;
565
566 acb->aio_done_func = NULL;
MORITA Kazutaka35200682013-10-24 16:01:17 +0900567 acb->cancelable = true;
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900568 acb->coroutine = qemu_coroutine_self();
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900569 acb->ret = 0;
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900570 acb->nr_pending = 0;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +0900571
572 acb->min_affect_data_idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size;
573 acb->max_affect_data_idx = (acb->sector_num * BDRV_SECTOR_SIZE +
574 acb->nb_sectors * BDRV_SECTOR_SIZE) / object_size;
575
Hitoshi Mitake498f2142015-09-01 12:03:09 +0900576 acb->min_dirty_data_idx = UINT32_MAX;
577 acb->max_dirty_data_idx = 0;
578
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900579 return acb;
580}
581
Liu Yuan833a7cc2015-02-18 11:57:55 +0800582/* Return -EIO in case of error, file descriptor on success */
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +0200583static int connect_to_sdog(BDRVSheepdogState *s, Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900584{
MORITA Kazutaka25af2572013-02-22 12:39:52 +0900585 int fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900586
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900587 if (s->is_unix) {
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +0200588 fd = unix_connect(s->host_spec, errp);
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900589 } else {
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +0200590 fd = inet_connect(s->host_spec, errp);
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900591
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +0200592 if (fd >= 0) {
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900593 int ret = socket_set_nodelay(fd);
594 if (ret < 0) {
595 error_report("%s", strerror(errno));
596 }
597 }
598 }
MORITA Kazutaka25af2572013-02-22 12:39:52 +0900599
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +0200600 if (fd >= 0) {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100601 qemu_set_nonblock(fd);
Liu Yuan833a7cc2015-02-18 11:57:55 +0800602 } else {
603 fd = -EIO;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900604 }
605
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900606 return fd;
607}
608
Liu Yuan833a7cc2015-02-18 11:57:55 +0800609/* Return 0 on success and -errno in case of error */
MORITA Kazutakae0d93a82012-05-30 09:03:55 +0900610static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
611 unsigned int *wlen)
Liu Yuan47622c42012-04-04 04:03:58 +0800612{
613 int ret;
614
615 ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900616 if (ret != sizeof(*hdr)) {
Liu Yuan47622c42012-04-04 04:03:58 +0800617 error_report("failed to send a req, %s", strerror(errno));
Liu Yuan833a7cc2015-02-18 11:57:55 +0800618 ret = -socket_error();
Liu Yuaneb092182012-04-03 18:04:21 +0800619 return ret;
Liu Yuan47622c42012-04-04 04:03:58 +0800620 }
621
622 ret = qemu_co_send(sockfd, data, *wlen);
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900623 if (ret != *wlen) {
Liu Yuan833a7cc2015-02-18 11:57:55 +0800624 ret = -socket_error();
Liu Yuan47622c42012-04-04 04:03:58 +0800625 error_report("failed to send a req, %s", strerror(errno));
626 }
627
628 return ret;
629}
MORITA Kazutakae0d93a82012-05-30 09:03:55 +0900630
MORITA Kazutaka2dfcca32012-06-27 07:26:19 +0900631static void restart_co_req(void *opaque)
632{
633 Coroutine *co = opaque;
634
635 qemu_coroutine_enter(co, NULL);
636}
637
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900638typedef struct SheepdogReqCo {
639 int sockfd;
Stefan Hajnoczi84390be2014-05-08 16:34:52 +0200640 AioContext *aio_context;
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900641 SheepdogReq *hdr;
642 void *data;
643 unsigned int *wlen;
644 unsigned int *rlen;
645 int ret;
646 bool finished;
647} SheepdogReqCo;
648
649static coroutine_fn void do_co_req(void *opaque)
Liu Yuan47622c42012-04-04 04:03:58 +0800650{
651 int ret;
MORITA Kazutaka2dfcca32012-06-27 07:26:19 +0900652 Coroutine *co;
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900653 SheepdogReqCo *srco = opaque;
654 int sockfd = srco->sockfd;
655 SheepdogReq *hdr = srco->hdr;
656 void *data = srco->data;
657 unsigned int *wlen = srco->wlen;
658 unsigned int *rlen = srco->rlen;
MORITA Kazutaka2dfcca32012-06-27 07:26:19 +0900659
660 co = qemu_coroutine_self();
Fam Zhengdca21ef2015-10-23 11:08:05 +0800661 aio_set_fd_handler(srco->aio_context, sockfd, false,
662 NULL, restart_co_req, co);
Liu Yuan47622c42012-04-04 04:03:58 +0800663
Liu Yuan47622c42012-04-04 04:03:58 +0800664 ret = send_co_req(sockfd, hdr, data, wlen);
665 if (ret < 0) {
666 goto out;
667 }
668
Fam Zhengdca21ef2015-10-23 11:08:05 +0800669 aio_set_fd_handler(srco->aio_context, sockfd, false,
670 restart_co_req, NULL, co);
MORITA Kazutaka2dfcca32012-06-27 07:26:19 +0900671
Liu Yuan47622c42012-04-04 04:03:58 +0800672 ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900673 if (ret != sizeof(*hdr)) {
Liu Yuan47622c42012-04-04 04:03:58 +0800674 error_report("failed to get a rsp, %s", strerror(errno));
MORITA Kazutakacb595882012-05-17 03:15:33 +0900675 ret = -errno;
Liu Yuan47622c42012-04-04 04:03:58 +0800676 goto out;
677 }
678
679 if (*rlen > hdr->data_length) {
680 *rlen = hdr->data_length;
681 }
682
683 if (*rlen) {
684 ret = qemu_co_recv(sockfd, data, *rlen);
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900685 if (ret != *rlen) {
Liu Yuan47622c42012-04-04 04:03:58 +0800686 error_report("failed to get the data, %s", strerror(errno));
MORITA Kazutakacb595882012-05-17 03:15:33 +0900687 ret = -errno;
Liu Yuan47622c42012-04-04 04:03:58 +0800688 goto out;
689 }
690 }
691 ret = 0;
692out:
MORITA Kazutakaed9ba722013-03-12 16:05:43 +0900693 /* there is at most one request for this sockfd, so it is safe to
694 * set each handler to NULL. */
Fam Zhengdca21ef2015-10-23 11:08:05 +0800695 aio_set_fd_handler(srco->aio_context, sockfd, false,
696 NULL, NULL, NULL);
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900697
698 srco->ret = ret;
699 srco->finished = true;
700}
701
Liu Yuan833a7cc2015-02-18 11:57:55 +0800702/*
703 * Send the request to the sheep in a synchronous manner.
704 *
705 * Return 0 on success, -errno in case of error.
706 */
Stefan Hajnoczi84390be2014-05-08 16:34:52 +0200707static int do_req(int sockfd, AioContext *aio_context, SheepdogReq *hdr,
708 void *data, unsigned int *wlen, unsigned int *rlen)
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900709{
710 Coroutine *co;
711 SheepdogReqCo srco = {
712 .sockfd = sockfd,
Stefan Hajnoczi84390be2014-05-08 16:34:52 +0200713 .aio_context = aio_context,
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900714 .hdr = hdr,
715 .data = data,
716 .wlen = wlen,
717 .rlen = rlen,
718 .ret = 0,
719 .finished = false,
720 };
721
722 if (qemu_in_coroutine()) {
723 do_co_req(&srco);
724 } else {
725 co = qemu_coroutine_create(do_co_req);
726 qemu_coroutine_enter(co, &srco);
727 while (!srco.finished) {
Stefan Hajnoczi84390be2014-05-08 16:34:52 +0200728 aio_poll(aio_context, true);
MORITA Kazutakacddd4ac2012-07-05 01:41:06 +0900729 }
730 }
731
732 return srco.ret;
Liu Yuan47622c42012-04-04 04:03:58 +0800733}
734
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +0900735static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +0900736 struct iovec *iov, int niov,
737 enum AIOCBState aiocb_type);
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +0900738static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);
MORITA Kazutaka72e09962013-10-24 16:01:13 +0900739static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag);
Markus Armbruster356b4ca2014-05-16 11:00:20 +0200740static int get_sheep_fd(BDRVSheepdogState *s, Error **errp);
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900741static void co_write_request(void *opaque);
MORITA Kazutaka7dc1cde2012-06-27 07:26:23 +0900742
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900743static coroutine_fn void reconnect_to_sdog(void *opaque)
744{
745 BDRVSheepdogState *s = opaque;
746 AIOReq *aio_req, *next;
747
Fam Zhengdca21ef2015-10-23 11:08:05 +0800748 aio_set_fd_handler(s->aio_context, s->fd, false, NULL,
749 NULL, NULL);
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900750 close(s->fd);
751 s->fd = -1;
752
753 /* Wait for outstanding write requests to be completed. */
754 while (s->co_send != NULL) {
755 co_write_request(opaque);
756 }
757
758 /* Try to reconnect the sheepdog server every one second. */
759 while (s->fd < 0) {
Liu Yuana780dea2014-08-28 18:27:55 +0800760 Error *local_err = NULL;
Markus Armbruster356b4ca2014-05-16 11:00:20 +0200761 s->fd = get_sheep_fd(s, &local_err);
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900762 if (s->fd < 0) {
763 DPRINTF("Wait for connection to be established\n");
Markus Armbruster565f65d2015-02-12 13:55:05 +0100764 error_report_err(local_err);
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900765 co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME,
766 1000000000ULL);
767 }
768 };
769
770 /*
771 * Now we have to resend all the request in the inflight queue. However,
772 * resend_aioreq() can yield and newly created requests can be added to the
773 * inflight queue before the coroutine is resumed. To avoid mixing them, we
774 * have to move all the inflight requests to the failed queue before
775 * resend_aioreq() is called.
776 */
777 QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) {
778 QLIST_REMOVE(aio_req, aio_siblings);
779 QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings);
780 }
781
782 /* Resend all the failed aio requests. */
783 while (!QLIST_EMPTY(&s->failed_aio_head)) {
784 aio_req = QLIST_FIRST(&s->failed_aio_head);
785 QLIST_REMOVE(aio_req, aio_siblings);
786 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
787 resend_aioreq(s, aio_req);
788 }
789}
790
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900791/*
792 * Receive responses of the I/O requests.
793 *
794 * This function is registered as a fd handler, and called from the
795 * main loop when s->fd is ready for reading responses.
796 */
Paolo Bonzinid8716b42011-10-05 09:17:31 +0200797static void coroutine_fn aio_read_response(void *opaque)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900798{
799 SheepdogObjRsp rsp;
800 BDRVSheepdogState *s = opaque;
801 int fd = s->fd;
802 int ret;
803 AIOReq *aio_req = NULL;
804 SheepdogAIOCB *acb;
Liu Yuancac8f4a2013-04-23 14:03:33 +0800805 uint64_t idx;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900806
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900807 /* read a header */
Paolo Bonzini8c5135f2011-09-08 13:46:25 +0200808 ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900809 if (ret != sizeof(rsp)) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200810 error_report("failed to get the header, %s", strerror(errno));
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900811 goto err;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900812 }
813
MORITA Kazutakac292ee62012-06-27 07:26:22 +0900814 /* find the right aio_req from the inflight aio list */
815 QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900816 if (aio_req->id == rsp.id) {
817 break;
818 }
819 }
820 if (!aio_req) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200821 error_report("cannot find aio_req %x", rsp.id);
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900822 goto err;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900823 }
824
825 acb = aio_req->aiocb;
826
827 switch (acb->aiocb_type) {
828 case AIOCB_WRITE_UDATA:
MORITA Kazutaka6d1acda2012-01-31 02:10:06 +0900829 /* this coroutine context is no longer suitable for co_recv
830 * because we may send data to update vdi objects */
831 s->co_recv = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900832 if (!is_data_obj(aio_req->oid)) {
833 break;
834 }
835 idx = data_oid_to_idx(aio_req->oid);
836
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +0900837 if (aio_req->create) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900838 /*
839 * If the object is newly created one, we need to update
840 * the vdi object (metadata object). min_dirty_data_idx
841 * and max_dirty_data_idx are changed to include updated
842 * index between them.
843 */
Liu Yuanbd751f22012-12-17 14:17:26 +0800844 if (rsp.result == SD_RES_SUCCESS) {
845 s->inode.data_vdi_id[idx] = s->inode.vdi_id;
Hitoshi Mitake498f2142015-09-01 12:03:09 +0900846 acb->max_dirty_data_idx = MAX(idx, acb->max_dirty_data_idx);
847 acb->min_dirty_data_idx = MIN(idx, acb->min_dirty_data_idx);
Liu Yuanbd751f22012-12-17 14:17:26 +0800848 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900849 }
850 break;
851 case AIOCB_READ_UDATA:
Michael Tokarev2fc8ae12012-06-07 20:22:46 +0400852 ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
853 aio_req->iov_offset, rsp.data_length);
MORITA Kazutaka80731d92013-10-24 16:01:11 +0900854 if (ret != rsp.data_length) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200855 error_report("failed to get the data, %s", strerror(errno));
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900856 goto err;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900857 }
858 break;
Liu Yuan47783072013-01-15 16:28:55 +0800859 case AIOCB_FLUSH_CACHE:
860 if (rsp.result == SD_RES_INVALID_PARMS) {
Peter Maydell2440a2c2013-07-29 13:16:36 +0100861 DPRINTF("disable cache since the server doesn't support it\n");
Liu Yuan47783072013-01-15 16:28:55 +0800862 s->cache_flags = SD_FLAG_CMD_DIRECT;
863 rsp.result = SD_RES_SUCCESS;
864 }
865 break;
Liu Yuancac8f4a2013-04-23 14:03:33 +0800866 case AIOCB_DISCARD_OBJ:
867 switch (rsp.result) {
868 case SD_RES_INVALID_PARMS:
869 error_report("sheep(%s) doesn't support discard command",
870 s->host_spec);
871 rsp.result = SD_RES_SUCCESS;
872 s->discard_supported = false;
873 break;
Liu Yuancac8f4a2013-04-23 14:03:33 +0800874 default:
875 break;
876 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900877 }
878
MORITA Kazutaka13c31de2013-04-26 01:19:54 +0900879 switch (rsp.result) {
880 case SD_RES_SUCCESS:
881 break;
882 case SD_RES_READONLY:
MORITA Kazutaka72e09962013-10-24 16:01:13 +0900883 if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) {
884 ret = reload_inode(s, 0, "");
885 if (ret < 0) {
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900886 goto err;
MORITA Kazutaka72e09962013-10-24 16:01:13 +0900887 }
888 }
MORITA Kazutaka72e09962013-10-24 16:01:13 +0900889 if (is_data_obj(aio_req->oid)) {
890 aio_req->oid = vid_to_data_oid(s->inode.vdi_id,
891 data_oid_to_idx(aio_req->oid));
892 } else {
893 aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id);
894 }
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +0900895 resend_aioreq(s, aio_req);
896 goto out;
MORITA Kazutaka13c31de2013-04-26 01:19:54 +0900897 default:
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900898 acb->ret = -EIO;
Markus Armbruster6daf1942011-06-22 14:03:54 +0200899 error_report("%s", sd_strerror(rsp.result));
MORITA Kazutaka13c31de2013-04-26 01:19:54 +0900900 break;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900901 }
902
MORITA Kazutaka1d732d72012-06-27 07:26:21 +0900903 free_aio_req(s, aio_req);
904 if (!acb->nr_pending) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900905 /*
906 * We've finished all requests which belong to the AIOCB, so
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900907 * we can switch back to sd_co_readv/writev now.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900908 */
909 acb->aio_done_func(acb);
910 }
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900911out:
912 s->co_recv = NULL;
MORITA Kazutaka011603c2013-10-24 16:01:15 +0900913 return;
914err:
915 s->co_recv = NULL;
916 reconnect_to_sdog(opaque);
MORITA Kazutaka2df46242011-08-12 21:33:15 +0900917}
918
919static void co_read_response(void *opaque)
920{
921 BDRVSheepdogState *s = opaque;
922
923 if (!s->co_recv) {
924 s->co_recv = qemu_coroutine_create(aio_read_response);
925 }
926
927 qemu_coroutine_enter(s->co_recv, opaque);
928}
929
930static void co_write_request(void *opaque)
931{
932 BDRVSheepdogState *s = opaque;
933
934 qemu_coroutine_enter(s->co_send, NULL);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900935}
936
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900937/*
Deepak Kathayatdc6fb732014-03-24 16:30:17 +0800938 * Return a socket descriptor to read/write objects.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900939 *
Deepak Kathayatdc6fb732014-03-24 16:30:17 +0800940 * We cannot use this descriptor for other operations because
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900941 * the block driver may be on waiting response from the server.
942 */
Markus Armbruster356b4ca2014-05-16 11:00:20 +0200943static int get_sheep_fd(BDRVSheepdogState *s, Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900944{
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900945 int fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900946
Markus Armbruster356b4ca2014-05-16 11:00:20 +0200947 fd = connect_to_sdog(s, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900948 if (fd < 0) {
MORITA Kazutakacb595882012-05-17 03:15:33 +0900949 return fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900950 }
951
Fam Zhengdca21ef2015-10-23 11:08:05 +0800952 aio_set_fd_handler(s->aio_context, fd, false,
953 co_read_response, NULL, s);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +0900954 return fd;
955}
956
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +0900957static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
958 char *vdi, uint32_t *snapid, char *tag)
959{
960 URI *uri;
961 QueryParams *qp = NULL;
962 int ret = 0;
963
964 uri = uri_parse(filename);
965 if (!uri) {
966 return -EINVAL;
967 }
968
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900969 /* transport */
970 if (!strcmp(uri->scheme, "sheepdog")) {
971 s->is_unix = false;
972 } else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
973 s->is_unix = false;
974 } else if (!strcmp(uri->scheme, "sheepdog+unix")) {
975 s->is_unix = true;
976 } else {
977 ret = -EINVAL;
978 goto out;
979 }
980
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +0900981 if (uri->path == NULL || !strcmp(uri->path, "/")) {
982 ret = -EINVAL;
983 goto out;
984 }
985 pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
986
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +0900987 qp = query_params_parse(uri->query);
988 if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
989 ret = -EINVAL;
990 goto out;
991 }
992
993 if (s->is_unix) {
994 /* sheepdog+unix:///vdiname?socket=path */
995 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
996 ret = -EINVAL;
997 goto out;
998 }
999 s->host_spec = g_strdup(qp->p[0].value);
1000 } else {
1001 /* sheepdog[+tcp]://[host:port]/vdiname */
1002 s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
1003 uri->port ?: SD_DEFAULT_PORT);
1004 }
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001005
1006 /* snapshot tag */
1007 if (uri->fragment) {
1008 *snapid = strtoul(uri->fragment, NULL, 10);
1009 if (*snapid == 0) {
1010 pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
1011 }
1012 } else {
1013 *snapid = CURRENT_VDI_ID; /* search current vdi */
1014 }
1015
1016out:
1017 if (qp) {
1018 query_params_free(qp);
1019 }
1020 uri_free(uri);
1021 return ret;
1022}
1023
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001024/*
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001025 * Parse a filename (old syntax)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001026 *
1027 * filename must be one of the following formats:
1028 * 1. [vdiname]
1029 * 2. [vdiname]:[snapid]
1030 * 3. [vdiname]:[tag]
1031 * 4. [hostname]:[port]:[vdiname]
1032 * 5. [hostname]:[port]:[vdiname]:[snapid]
1033 * 6. [hostname]:[port]:[vdiname]:[tag]
1034 *
1035 * You can boot from the snapshot images by specifying `snapid` or
1036 * `tag'.
1037 *
1038 * You can run VMs outside the Sheepdog cluster by specifying
1039 * `hostname' and `port' (experimental).
1040 */
1041static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
1042 char *vdi, uint32_t *snapid, char *tag)
1043{
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001044 char *p, *q, *uri;
1045 const char *host_spec, *vdi_spec;
1046 int nr_sep, ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001047
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001048 strstart(filename, "sheepdog:", (const char **)&filename);
Anthony Liguori7267c092011-08-20 22:09:37 -05001049 p = q = g_strdup(filename);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001050
1051 /* count the number of separators */
1052 nr_sep = 0;
1053 while (*p) {
1054 if (*p == ':') {
1055 nr_sep++;
1056 }
1057 p++;
1058 }
1059 p = q;
1060
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001061 /* use the first two tokens as host_spec. */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001062 if (nr_sep >= 2) {
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001063 host_spec = p;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001064 p = strchr(p, ':');
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001065 p++;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001066 p = strchr(p, ':');
1067 *p++ = '\0';
1068 } else {
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001069 host_spec = "";
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001070 }
1071
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001072 vdi_spec = p;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001073
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001074 p = strchr(vdi_spec, ':');
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001075 if (p) {
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001076 *p++ = '#';
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001077 }
1078
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001079 uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001080
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001081 ret = sd_parse_uri(s, uri, vdi, snapid, tag);
1082
1083 g_free(q);
1084 g_free(uri);
1085
1086 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001087}
1088
MORITA Kazutaka982dcbf2013-04-26 01:19:51 +09001089static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
1090 uint32_t snapid, const char *tag, uint32_t *vid,
Markus Armbrusterdc83cd42014-05-16 11:00:23 +02001091 bool lock, Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001092{
1093 int ret, fd;
1094 SheepdogVdiReq hdr;
1095 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1096 unsigned int wlen, rlen = 0;
1097 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
1098
Markus Armbrusterdc83cd42014-05-16 11:00:23 +02001099 fd = connect_to_sdog(s, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001100 if (fd < 0) {
MORITA Kazutakacb595882012-05-17 03:15:33 +09001101 return fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001102 }
1103
Jim Meyering3178e272012-10-04 13:09:47 +02001104 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1105 * which is desirable since we'll soon be sending those bytes, and
1106 * don't want the send_req to read uninitialized data.
1107 */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001108 strncpy(buf, filename, SD_MAX_VDI_LEN);
1109 strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
1110
1111 memset(&hdr, 0, sizeof(hdr));
MORITA Kazutaka982dcbf2013-04-26 01:19:51 +09001112 if (lock) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001113 hdr.opcode = SD_OP_LOCK_VDI;
Hitoshi Mitake1dbfafe2014-08-11 14:43:45 +09001114 hdr.type = LOCK_TYPE_NORMAL;
MORITA Kazutaka982dcbf2013-04-26 01:19:51 +09001115 } else {
1116 hdr.opcode = SD_OP_GET_VDI_INFO;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001117 }
1118 wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
1119 hdr.proto_ver = SD_PROTO_VER;
1120 hdr.data_length = wlen;
1121 hdr.snapid = snapid;
1122 hdr.flags = SD_FLAG_CMD_WRITE;
1123
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001124 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001125 if (ret) {
Markus Armbrusterdc83cd42014-05-16 11:00:23 +02001126 error_setg_errno(errp, -ret, "cannot get vdi info");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001127 goto out;
1128 }
1129
1130 if (rsp->result != SD_RES_SUCCESS) {
Markus Armbrusterdc83cd42014-05-16 11:00:23 +02001131 error_setg(errp, "cannot get vdi info, %s, %s %" PRIu32 " %s",
1132 sd_strerror(rsp->result), filename, snapid, tag);
MORITA Kazutakacb595882012-05-17 03:15:33 +09001133 if (rsp->result == SD_RES_NO_VDI) {
1134 ret = -ENOENT;
Hitoshi Mitake38890b22014-08-11 14:43:46 +09001135 } else if (rsp->result == SD_RES_VDI_LOCKED) {
1136 ret = -EBUSY;
MORITA Kazutakacb595882012-05-17 03:15:33 +09001137 } else {
1138 ret = -EIO;
1139 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001140 goto out;
1141 }
1142 *vid = rsp->vdi_id;
1143
1144 ret = 0;
1145out:
1146 closesocket(fd);
1147 return ret;
1148}
1149
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +09001150static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001151 struct iovec *iov, int niov,
1152 enum AIOCBState aiocb_type)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001153{
1154 int nr_copies = s->inode.nr_copies;
1155 SheepdogObjReq hdr;
Liu Yuan47783072013-01-15 16:28:55 +08001156 unsigned int wlen = 0;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001157 int ret;
1158 uint64_t oid = aio_req->oid;
1159 unsigned int datalen = aio_req->data_len;
1160 uint64_t offset = aio_req->offset;
1161 uint8_t flags = aio_req->flags;
1162 uint64_t old_oid = aio_req->base_oid;
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001163 bool create = aio_req->create;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001164
1165 if (!nr_copies) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001166 error_report("bug");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001167 }
1168
1169 memset(&hdr, 0, sizeof(hdr));
1170
Liu Yuan47783072013-01-15 16:28:55 +08001171 switch (aiocb_type) {
1172 case AIOCB_FLUSH_CACHE:
1173 hdr.opcode = SD_OP_FLUSH_VDI;
1174 break;
1175 case AIOCB_READ_UDATA:
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001176 hdr.opcode = SD_OP_READ_OBJ;
1177 hdr.flags = flags;
Liu Yuan47783072013-01-15 16:28:55 +08001178 break;
1179 case AIOCB_WRITE_UDATA:
1180 if (create) {
1181 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1182 } else {
1183 hdr.opcode = SD_OP_WRITE_OBJ;
1184 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001185 wlen = datalen;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001186 hdr.flags = SD_FLAG_CMD_WRITE | flags;
Liu Yuan47783072013-01-15 16:28:55 +08001187 break;
Liu Yuancac8f4a2013-04-23 14:03:33 +08001188 case AIOCB_DISCARD_OBJ:
Hitoshi Mitakee6fd57e2015-09-01 12:03:10 +09001189 hdr.opcode = SD_OP_WRITE_OBJ;
1190 hdr.flags = SD_FLAG_CMD_WRITE | flags;
1191 s->inode.data_vdi_id[data_oid_to_idx(oid)] = 0;
1192 offset = offsetof(SheepdogInode,
1193 data_vdi_id[data_oid_to_idx(oid)]);
1194 oid = vid_to_vdi_oid(s->inode.vdi_id);
1195 wlen = datalen = sizeof(uint32_t);
Liu Yuancac8f4a2013-04-23 14:03:33 +08001196 break;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001197 }
1198
Liu Yuan0e7106d2013-01-10 16:03:47 +08001199 if (s->cache_flags) {
1200 hdr.flags |= s->cache_flags;
Liu Yuan47622c42012-04-04 04:03:58 +08001201 }
1202
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001203 hdr.oid = oid;
1204 hdr.cow_oid = old_oid;
1205 hdr.copies = s->inode.nr_copies;
1206
1207 hdr.data_length = datalen;
1208 hdr.offset = offset;
1209
1210 hdr.id = aio_req->id;
1211
MORITA Kazutaka2df46242011-08-12 21:33:15 +09001212 qemu_co_mutex_lock(&s->lock);
1213 s->co_send = qemu_coroutine_self();
Fam Zhengdca21ef2015-10-23 11:08:05 +08001214 aio_set_fd_handler(s->aio_context, s->fd, false,
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001215 co_read_response, co_write_request, s);
Paolo Bonzini128aa582011-09-21 12:36:48 +02001216 socket_set_cork(s->fd, 1);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001217
1218 /* send a header */
Paolo Bonzini8c5135f2011-09-08 13:46:25 +02001219 ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
MORITA Kazutaka80731d92013-10-24 16:01:11 +09001220 if (ret != sizeof(hdr)) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001221 error_report("failed to send a req, %s", strerror(errno));
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001222 goto out;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001223 }
1224
1225 if (wlen) {
Michael Tokarev2fc8ae12012-06-07 20:22:46 +04001226 ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
MORITA Kazutaka80731d92013-10-24 16:01:11 +09001227 if (ret != wlen) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001228 error_report("failed to send a data, %s", strerror(errno));
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001229 }
1230 }
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001231out:
Paolo Bonzini128aa582011-09-21 12:36:48 +02001232 socket_set_cork(s->fd, 0);
Fam Zhengdca21ef2015-10-23 11:08:05 +08001233 aio_set_fd_handler(s->aio_context, s->fd, false,
1234 co_read_response, NULL, s);
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001235 s->co_send = NULL;
MORITA Kazutaka2df46242011-08-12 21:33:15 +09001236 qemu_co_mutex_unlock(&s->lock);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001237}
1238
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001239static int read_write_object(int fd, AioContext *aio_context, char *buf,
1240 uint64_t oid, uint8_t copies,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001241 unsigned int datalen, uint64_t offset,
Liu Yuan0e7106d2013-01-10 16:03:47 +08001242 bool write, bool create, uint32_t cache_flags)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001243{
1244 SheepdogObjReq hdr;
1245 SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
1246 unsigned int wlen, rlen;
1247 int ret;
1248
1249 memset(&hdr, 0, sizeof(hdr));
1250
1251 if (write) {
1252 wlen = datalen;
1253 rlen = 0;
1254 hdr.flags = SD_FLAG_CMD_WRITE;
1255 if (create) {
1256 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1257 } else {
1258 hdr.opcode = SD_OP_WRITE_OBJ;
1259 }
1260 } else {
1261 wlen = 0;
1262 rlen = datalen;
1263 hdr.opcode = SD_OP_READ_OBJ;
1264 }
Liu Yuan47622c42012-04-04 04:03:58 +08001265
Liu Yuan0e7106d2013-01-10 16:03:47 +08001266 hdr.flags |= cache_flags;
Liu Yuan47622c42012-04-04 04:03:58 +08001267
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001268 hdr.oid = oid;
1269 hdr.data_length = datalen;
1270 hdr.offset = offset;
1271 hdr.copies = copies;
1272
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001273 ret = do_req(fd, aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001274 if (ret) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001275 error_report("failed to send a request to the sheep");
MORITA Kazutakacb595882012-05-17 03:15:33 +09001276 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001277 }
1278
1279 switch (rsp->result) {
1280 case SD_RES_SUCCESS:
1281 return 0;
1282 default:
Markus Armbruster6daf1942011-06-22 14:03:54 +02001283 error_report("%s", sd_strerror(rsp->result));
MORITA Kazutakacb595882012-05-17 03:15:33 +09001284 return -EIO;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001285 }
1286}
1287
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001288static int read_object(int fd, AioContext *aio_context, char *buf,
1289 uint64_t oid, uint8_t copies,
Liu Yuan0e7106d2013-01-10 16:03:47 +08001290 unsigned int datalen, uint64_t offset,
1291 uint32_t cache_flags)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001292{
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001293 return read_write_object(fd, aio_context, buf, oid, copies,
1294 datalen, offset, false,
Liu Yuan0e7106d2013-01-10 16:03:47 +08001295 false, cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001296}
1297
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001298static int write_object(int fd, AioContext *aio_context, char *buf,
1299 uint64_t oid, uint8_t copies,
MORITA Kazutaka2f536802012-10-07 01:57:14 +09001300 unsigned int datalen, uint64_t offset, bool create,
Liu Yuan0e7106d2013-01-10 16:03:47 +08001301 uint32_t cache_flags)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001302{
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001303 return read_write_object(fd, aio_context, buf, oid, copies,
1304 datalen, offset, true,
Liu Yuan0e7106d2013-01-10 16:03:47 +08001305 create, cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001306}
1307
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001308/* update inode with the latest state */
1309static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag)
1310{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001311 Error *local_err = NULL;
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001312 SheepdogInode *inode;
1313 int ret = 0, fd;
1314 uint32_t vid = 0;
1315
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001316 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001317 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001318 error_report_err(local_err);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001319 return -EIO;
1320 }
1321
Hitoshi Mitake5d039ba2014-06-06 13:35:12 +09001322 inode = g_malloc(SD_INODE_HEADER_SIZE);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001323
Markus Armbrusterdc83cd42014-05-16 11:00:23 +02001324 ret = find_vdi_name(s, s->name, snapid, tag, &vid, false, &local_err);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001325 if (ret) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001326 error_report_err(local_err);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001327 goto out;
1328 }
1329
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001330 ret = read_object(fd, s->aio_context, (char *)inode, vid_to_vdi_oid(vid),
Hitoshi Mitake5d039ba2014-06-06 13:35:12 +09001331 s->inode.nr_copies, SD_INODE_HEADER_SIZE, 0,
1332 s->cache_flags);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001333 if (ret < 0) {
1334 goto out;
1335 }
1336
1337 if (inode->vdi_id != s->inode.vdi_id) {
Hitoshi Mitake5d039ba2014-06-06 13:35:12 +09001338 memcpy(&s->inode, inode, SD_INODE_HEADER_SIZE);
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09001339 }
1340
1341out:
1342 g_free(inode);
1343 closesocket(fd);
1344
1345 return ret;
1346}
1347
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +09001348static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001349{
1350 SheepdogAIOCB *acb = aio_req->aiocb;
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001351
1352 aio_req->create = false;
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001353
1354 /* check whether this request becomes a CoW one */
MORITA Kazutaka2412aec2013-10-24 16:01:12 +09001355 if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) {
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001356 int idx = data_oid_to_idx(aio_req->oid);
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001357
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001358 if (is_data_obj_writable(&s->inode, idx)) {
1359 goto out;
1360 }
1361
MORITA Kazutaka80308d32013-10-24 16:01:18 +09001362 if (s->inode.data_vdi_id[idx]) {
1363 aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx);
1364 aio_req->flags |= SD_FLAG_CMD_COW;
1365 }
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001366 aio_req->create = true;
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001367 }
1368out:
MORITA Kazutaka2412aec2013-10-24 16:01:12 +09001369 if (is_data_obj(aio_req->oid)) {
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001370 add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +09001371 acb->aiocb_type);
MORITA Kazutaka2412aec2013-10-24 16:01:12 +09001372 } else {
1373 struct iovec iov;
1374 iov.iov_base = &s->inode;
1375 iov.iov_len = sizeof(s->inode);
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09001376 add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA);
MORITA Kazutaka2412aec2013-10-24 16:01:12 +09001377 }
MORITA Kazutaka13c31de2013-04-26 01:19:54 +09001378}
1379
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001380static void sd_detach_aio_context(BlockDriverState *bs)
1381{
1382 BDRVSheepdogState *s = bs->opaque;
1383
Fam Zhengdca21ef2015-10-23 11:08:05 +08001384 aio_set_fd_handler(s->aio_context, s->fd, false, NULL,
1385 NULL, NULL);
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001386}
1387
1388static void sd_attach_aio_context(BlockDriverState *bs,
1389 AioContext *new_context)
1390{
1391 BDRVSheepdogState *s = bs->opaque;
1392
1393 s->aio_context = new_context;
Fam Zhengdca21ef2015-10-23 11:08:05 +08001394 aio_set_fd_handler(new_context, s->fd, false,
1395 co_read_response, NULL, s);
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001396}
1397
Kevin Wolfc8c96352013-04-12 18:10:49 +02001398/* TODO Convert to fine grained options */
1399static QemuOptsList runtime_opts = {
1400 .name = "sheepdog",
1401 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1402 .desc = {
1403 {
1404 .name = "filename",
1405 .type = QEMU_OPT_STRING,
1406 .help = "URL to the sheepdog image",
1407 },
1408 { /* end of list */ }
1409 },
1410};
1411
Max Reitz015a1032013-09-05 14:22:29 +02001412static int sd_open(BlockDriverState *bs, QDict *options, int flags,
1413 Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001414{
1415 int ret, fd;
1416 uint32_t vid = 0;
1417 BDRVSheepdogState *s = bs->opaque;
1418 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1419 uint32_t snapid;
1420 char *buf = NULL;
Kevin Wolfc8c96352013-04-12 18:10:49 +02001421 QemuOpts *opts;
1422 Error *local_err = NULL;
1423 const char *filename;
1424
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001425 s->bs = bs;
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001426 s->aio_context = bdrv_get_aio_context(bs);
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001427
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001428 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolfc8c96352013-04-12 18:10:49 +02001429 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001430 if (local_err) {
Markus Armbrustere67c3992014-05-16 11:00:24 +02001431 error_propagate(errp, local_err);
Kevin Wolfc8c96352013-04-12 18:10:49 +02001432 ret = -EINVAL;
1433 goto out;
1434 }
1435
1436 filename = qemu_opt_get(opts, "filename");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001437
MORITA Kazutakac292ee62012-06-27 07:26:22 +09001438 QLIST_INIT(&s->inflight_aio_head);
MORITA Kazutaka011603c2013-10-24 16:01:15 +09001439 QLIST_INIT(&s->failed_aio_head);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09001440 QLIST_INIT(&s->inflight_aiocb_head);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001441 s->fd = -1;
1442
1443 memset(vdi, 0, sizeof(vdi));
1444 memset(tag, 0, sizeof(tag));
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001445
1446 if (strstr(filename, "://")) {
1447 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1448 } else {
1449 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1450 }
1451 if (ret < 0) {
Markus Armbrusterefde4b62014-05-16 11:00:25 +02001452 error_setg(errp, "Can't parse filename");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001453 goto out;
1454 }
Markus Armbrustere67c3992014-05-16 11:00:24 +02001455 s->fd = get_sheep_fd(s, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001456 if (s->fd < 0) {
MORITA Kazutakacb595882012-05-17 03:15:33 +09001457 ret = s->fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001458 goto out;
1459 }
1460
Markus Armbrustere67c3992014-05-16 11:00:24 +02001461 ret = find_vdi_name(s, vdi, snapid, tag, &vid, true, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001462 if (ret) {
1463 goto out;
1464 }
1465
Liu Yuan0e7106d2013-01-10 16:03:47 +08001466 /*
1467 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1468 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1469 */
1470 s->cache_flags = SD_FLAG_CMD_CACHE;
1471 if (flags & BDRV_O_NOCACHE) {
1472 s->cache_flags = SD_FLAG_CMD_DIRECT;
1473 }
Liu Yuancac8f4a2013-04-23 14:03:33 +08001474 s->discard_supported = true;
Liu Yuan0e7106d2013-01-10 16:03:47 +08001475
MORITA Kazutaka622b6052012-05-17 03:15:31 +09001476 if (snapid || tag[0] != '\0') {
Peter Maydell2440a2c2013-07-29 13:16:36 +01001477 DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid);
MORITA Kazutaka2f536802012-10-07 01:57:14 +09001478 s->is_snapshot = true;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001479 }
1480
Markus Armbrustere67c3992014-05-16 11:00:24 +02001481 fd = connect_to_sdog(s, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001482 if (fd < 0) {
MORITA Kazutakacb595882012-05-17 03:15:33 +09001483 ret = fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001484 goto out;
1485 }
1486
Anthony Liguori7267c092011-08-20 22:09:37 -05001487 buf = g_malloc(SD_INODE_SIZE);
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001488 ret = read_object(fd, s->aio_context, buf, vid_to_vdi_oid(vid),
1489 0, SD_INODE_SIZE, 0, s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001490
1491 closesocket(fd);
1492
1493 if (ret) {
Markus Armbrusterefde4b62014-05-16 11:00:25 +02001494 error_setg(errp, "Can't read snapshot inode");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001495 goto out;
1496 }
1497
1498 memcpy(&s->inode, buf, sizeof(s->inode));
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001499
Liu Yuane8bfaa22013-04-23 14:03:34 +08001500 bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
Jim Meyering3178e272012-10-04 13:09:47 +02001501 pstrcpy(s->name, sizeof(s->name), vdi);
MORITA Kazutaka2df46242011-08-12 21:33:15 +09001502 qemu_co_mutex_init(&s->lock);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09001503 qemu_co_queue_init(&s->overlapping_queue);
Kevin Wolfc8c96352013-04-12 18:10:49 +02001504 qemu_opts_del(opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05001505 g_free(buf);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001506 return 0;
1507out:
Fam Zhengdca21ef2015-10-23 11:08:05 +08001508 aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd,
1509 false, NULL, NULL, NULL);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001510 if (s->fd >= 0) {
1511 closesocket(s->fd);
1512 }
Kevin Wolfc8c96352013-04-12 18:10:49 +02001513 qemu_opts_del(opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05001514 g_free(buf);
MORITA Kazutakacb595882012-05-17 03:15:33 +09001515 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001516}
1517
Liu Yuan4da65c82015-08-28 10:53:58 +08001518static int sd_reopen_prepare(BDRVReopenState *state, BlockReopenQueue *queue,
1519 Error **errp)
1520{
1521 BDRVSheepdogState *s = state->bs->opaque;
1522 BDRVSheepdogReopenState *re_s;
1523 int ret = 0;
1524
1525 re_s = state->opaque = g_new0(BDRVSheepdogReopenState, 1);
1526
1527 re_s->cache_flags = SD_FLAG_CMD_CACHE;
1528 if (state->flags & BDRV_O_NOCACHE) {
1529 re_s->cache_flags = SD_FLAG_CMD_DIRECT;
1530 }
1531
1532 re_s->fd = get_sheep_fd(s, errp);
1533 if (re_s->fd < 0) {
1534 ret = re_s->fd;
1535 return ret;
1536 }
1537
1538 return ret;
1539}
1540
1541static void sd_reopen_commit(BDRVReopenState *state)
1542{
1543 BDRVSheepdogReopenState *re_s = state->opaque;
1544 BDRVSheepdogState *s = state->bs->opaque;
1545
1546 if (s->fd) {
Fam Zhengdca21ef2015-10-23 11:08:05 +08001547 aio_set_fd_handler(s->aio_context, s->fd, false,
1548 NULL, NULL, NULL);
Liu Yuan4da65c82015-08-28 10:53:58 +08001549 closesocket(s->fd);
1550 }
1551
1552 s->fd = re_s->fd;
1553 s->cache_flags = re_s->cache_flags;
1554
1555 g_free(state->opaque);
1556 state->opaque = NULL;
1557
1558 return;
1559}
1560
1561static void sd_reopen_abort(BDRVReopenState *state)
1562{
1563 BDRVSheepdogReopenState *re_s = state->opaque;
1564 BDRVSheepdogState *s = state->bs->opaque;
1565
1566 if (re_s == NULL) {
1567 return;
1568 }
1569
1570 if (re_s->fd) {
Fam Zhengdca21ef2015-10-23 11:08:05 +08001571 aio_set_fd_handler(s->aio_context, re_s->fd, false,
1572 NULL, NULL, NULL);
Liu Yuan4da65c82015-08-28 10:53:58 +08001573 closesocket(re_s->fd);
1574 }
1575
1576 g_free(state->opaque);
1577 state->opaque = NULL;
1578
1579 return;
1580}
1581
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001582static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot,
1583 Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001584{
1585 SheepdogVdiReq hdr;
1586 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1587 int fd, ret;
1588 unsigned int wlen, rlen = 0;
1589 char buf[SD_MAX_VDI_LEN];
1590
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001591 fd = connect_to_sdog(s, errp);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001592 if (fd < 0) {
MORITA Kazutakacb595882012-05-17 03:15:33 +09001593 return fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001594 }
1595
Jim Meyering3178e272012-10-04 13:09:47 +02001596 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1597 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1598 */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001599 memset(buf, 0, sizeof(buf));
Liu Yuanc31d4822013-11-07 22:56:37 +08001600 pstrcpy(buf, sizeof(buf), s->name);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001601
1602 memset(&hdr, 0, sizeof(hdr));
1603 hdr.opcode = SD_OP_NEW_VDI;
Liu Yuan9f23fce2014-01-03 20:13:12 +08001604 hdr.base_vdi_id = s->inode.vdi_id;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001605
1606 wlen = SD_MAX_VDI_LEN;
1607
1608 hdr.flags = SD_FLAG_CMD_WRITE;
1609 hdr.snapid = snapshot;
1610
1611 hdr.data_length = wlen;
Liu Yuanc31d4822013-11-07 22:56:37 +08001612 hdr.vdi_size = s->inode.vdi_size;
1613 hdr.copy_policy = s->inode.copy_policy;
Liu Yuanb3af0182013-11-07 22:56:38 +08001614 hdr.copies = s->inode.nr_copies;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001615 hdr.block_size_shift = s->inode.block_size_shift;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001616
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001617 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001618
1619 closesocket(fd);
1620
1621 if (ret) {
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001622 error_setg_errno(errp, -ret, "create failed");
MORITA Kazutakacb595882012-05-17 03:15:33 +09001623 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001624 }
1625
1626 if (rsp->result != SD_RES_SUCCESS) {
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001627 error_setg(errp, "%s, %s", sd_strerror(rsp->result), s->inode.name);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001628 return -EIO;
1629 }
1630
1631 if (vdi_id) {
1632 *vdi_id = rsp->vdi_id;
1633 }
1634
1635 return 0;
1636}
1637
Markus Armbruster318df292014-05-16 11:00:21 +02001638static int sd_prealloc(const char *filename, Error **errp)
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001639{
1640 BlockDriverState *bs = NULL;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001641 BDRVSheepdogState *base = NULL;
1642 unsigned long buf_size;
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001643 uint32_t idx, max_idx;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001644 uint32_t object_size;
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001645 int64_t vdi_size;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001646 void *buf = NULL;
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001647 int ret;
1648
Max Reitz2e401342014-02-18 18:33:07 +01001649 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
Max Reitz6ebf9aa2015-08-26 19:47:49 +02001650 errp);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001651 if (ret < 0) {
Markus Armbruster318df292014-05-16 11:00:21 +02001652 goto out_with_err_set;
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001653 }
1654
1655 vdi_size = bdrv_getlength(bs);
1656 if (vdi_size < 0) {
1657 ret = vdi_size;
1658 goto out;
1659 }
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001660
1661 base = bs->opaque;
1662 object_size = (UINT32_C(1) << base->inode.block_size_shift);
1663 buf_size = MIN(object_size, SD_DATA_OBJ_SIZE);
1664 buf = g_malloc0(buf_size);
1665
1666 max_idx = DIV_ROUND_UP(vdi_size, buf_size);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001667
1668 for (idx = 0; idx < max_idx; idx++) {
1669 /*
1670 * The created image can be a cloned image, so we need to read
1671 * a data from the source image.
1672 */
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001673 ret = bdrv_pread(bs, idx * buf_size, buf, buf_size);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001674 if (ret < 0) {
1675 goto out;
1676 }
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001677 ret = bdrv_pwrite(bs, idx * buf_size, buf, buf_size);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001678 if (ret < 0) {
1679 goto out;
1680 }
1681 }
Markus Armbruster318df292014-05-16 11:00:21 +02001682
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001683out:
Markus Armbruster318df292014-05-16 11:00:21 +02001684 if (ret < 0) {
1685 error_setg_errno(errp, -ret, "Can't pre-allocate");
1686 }
1687out_with_err_set:
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001688 if (bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001689 bdrv_unref(bs);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001690 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001691 g_free(buf);
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001692
1693 return ret;
1694}
1695
Liu Yuanb3af0182013-11-07 22:56:38 +08001696/*
1697 * Sheepdog support two kinds of redundancy, full replication and erasure
1698 * coding.
1699 *
1700 * # create a fully replicated vdi with x copies
1701 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1702 *
1703 * # create a erasure coded vdi with x data strips and y parity strips
1704 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1705 */
1706static int parse_redundancy(BDRVSheepdogState *s, const char *opt)
1707{
1708 struct SheepdogInode *inode = &s->inode;
1709 const char *n1, *n2;
1710 long copy, parity;
1711 char p[10];
1712
1713 pstrcpy(p, sizeof(p), opt);
1714 n1 = strtok(p, ":");
1715 n2 = strtok(NULL, ":");
1716
1717 if (!n1) {
1718 return -EINVAL;
1719 }
1720
1721 copy = strtol(n1, NULL, 10);
1722 if (copy > SD_MAX_COPIES || copy < 1) {
1723 return -EINVAL;
1724 }
1725 if (!n2) {
1726 inode->copy_policy = 0;
1727 inode->nr_copies = copy;
1728 return 0;
1729 }
1730
1731 if (copy != 2 && copy != 4 && copy != 8 && copy != 16) {
1732 return -EINVAL;
1733 }
1734
1735 parity = strtol(n2, NULL, 10);
1736 if (parity >= SD_EC_MAX_STRIP || parity < 1) {
1737 return -EINVAL;
1738 }
1739
1740 /*
1741 * 4 bits for parity and 4 bits for data.
1742 * We have to compress upper data bits because it can't represent 16
1743 */
1744 inode->copy_policy = ((copy / 2) << 4) + parity;
1745 inode->nr_copies = copy + parity;
1746
1747 return 0;
1748}
1749
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001750static int parse_block_size_shift(BDRVSheepdogState *s, QemuOpts *opt)
1751{
1752 struct SheepdogInode *inode = &s->inode;
1753 uint64_t object_size;
1754 int obj_order;
1755
1756 object_size = qemu_opt_get_size_del(opt, BLOCK_OPT_OBJECT_SIZE, 0);
1757 if (object_size) {
1758 if ((object_size - 1) & object_size) { /* not a power of 2? */
1759 return -EINVAL;
1760 }
Stefan Hajnoczi786a4ea2015-03-23 15:29:26 +00001761 obj_order = ctz32(object_size);
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001762 if (obj_order < 20 || obj_order > 31) {
1763 return -EINVAL;
1764 }
1765 inode->block_size_shift = (uint8_t)obj_order;
1766 }
1767
1768 return 0;
1769}
1770
Chunyan Liub2222372014-06-05 17:21:05 +08001771static int sd_create(const char *filename, QemuOpts *opts,
Max Reitzd5124c02013-09-05 14:26:05 +02001772 Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001773{
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001774 int ret = 0;
Liu Yuanc31d4822013-11-07 22:56:37 +08001775 uint32_t vid = 0;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001776 char *backing_file = NULL;
Chunyan Liub2222372014-06-05 17:21:05 +08001777 char *buf = NULL;
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001778 BDRVSheepdogState *s;
Liu Yuanc31d4822013-11-07 22:56:37 +08001779 char tag[SD_MAX_VDI_TAG_LEN];
MORITA Kazutakab4447362011-01-28 01:33:10 +09001780 uint32_t snapid;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001781 uint64_t max_vdi_size;
MORITA Kazutaka2f536802012-10-07 01:57:14 +09001782 bool prealloc = false;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001783
Markus Armbruster5839e532014-08-19 10:31:08 +02001784 s = g_new0(BDRVSheepdogState, 1);
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001785
MORITA Kazutakab4447362011-01-28 01:33:10 +09001786 memset(tag, 0, sizeof(tag));
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001787 if (strstr(filename, "://")) {
Liu Yuanc31d4822013-11-07 22:56:37 +08001788 ret = sd_parse_uri(s, filename, s->name, &snapid, tag);
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001789 } else {
Liu Yuanc31d4822013-11-07 22:56:37 +08001790 ret = parse_vdiname(s, filename, s->name, &snapid, tag);
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09001791 }
1792 if (ret < 0) {
Markus Armbrusterefde4b62014-05-16 11:00:25 +02001793 error_setg(errp, "Can't parse filename");
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001794 goto out;
MORITA Kazutakab4447362011-01-28 01:33:10 +09001795 }
1796
Hu Taoc2eb9182014-09-10 17:05:45 +08001797 s->inode.vdi_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1798 BDRV_SECTOR_SIZE);
Chunyan Liub2222372014-06-05 17:21:05 +08001799 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
1800 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1801 if (!buf || !strcmp(buf, "off")) {
1802 prealloc = false;
1803 } else if (!strcmp(buf, "full")) {
1804 prealloc = true;
1805 } else {
1806 error_setg(errp, "Invalid preallocation mode: '%s'", buf);
1807 ret = -EINVAL;
1808 goto out;
1809 }
1810
1811 g_free(buf);
1812 buf = qemu_opt_get_del(opts, BLOCK_OPT_REDUNDANCY);
1813 if (buf) {
1814 ret = parse_redundancy(s, buf);
1815 if (ret < 0) {
1816 error_setg(errp, "Invalid redundancy mode: '%s'", buf);
1817 goto out;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001818 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001819 }
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001820 ret = parse_block_size_shift(s, opts);
1821 if (ret < 0) {
1822 error_setg(errp, "Invalid object_size."
1823 " obect_size needs to be power of 2"
1824 " and be limited from 2^20 to 2^31");
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001825 goto out;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001826 }
1827
1828 if (backing_file) {
1829 BlockDriverState *bs;
Liu Yuan9f23fce2014-01-03 20:13:12 +08001830 BDRVSheepdogState *base;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001831 BlockDriver *drv;
1832
1833 /* Currently, only Sheepdog backing image is supported. */
Max Reitzb65a5e12015-02-05 13:58:12 -05001834 drv = bdrv_find_protocol(backing_file, true, NULL);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001835 if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
Markus Armbrustere67c3992014-05-16 11:00:24 +02001836 error_setg(errp, "backing_file must be a sheepdog image");
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001837 ret = -EINVAL;
1838 goto out;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001839 }
1840
Max Reitz2e401342014-02-18 18:33:07 +01001841 bs = NULL;
Max Reitz6ebf9aa2015-08-26 19:47:49 +02001842 ret = bdrv_open(&bs, backing_file, NULL, NULL, BDRV_O_PROTOCOL, errp);
MORITA Kazutakacb595882012-05-17 03:15:33 +09001843 if (ret < 0) {
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001844 goto out;
MORITA Kazutakacb595882012-05-17 03:15:33 +09001845 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001846
Liu Yuan9f23fce2014-01-03 20:13:12 +08001847 base = bs->opaque;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001848
Liu Yuan9f23fce2014-01-03 20:13:12 +08001849 if (!is_snapshot(&base->inode)) {
Markus Armbrustere67c3992014-05-16 11:00:24 +02001850 error_setg(errp, "cannot clone from a non snapshot vdi");
Fam Zheng4f6fd342013-08-23 09:14:47 +08001851 bdrv_unref(bs);
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001852 ret = -EINVAL;
1853 goto out;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001854 }
Liu Yuan9f23fce2014-01-03 20:13:12 +08001855 s->inode.vdi_id = base->inode.vdi_id;
Fam Zheng4f6fd342013-08-23 09:14:47 +08001856 bdrv_unref(bs);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001857 }
1858
Liu Yuan5d5da112014-06-17 13:45:35 +08001859 s->aio_context = qemu_get_aio_context();
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001860
1861 /* if block_size_shift is not specified, get cluster default value */
1862 if (s->inode.block_size_shift == 0) {
1863 SheepdogVdiReq hdr;
1864 SheepdogClusterRsp *rsp = (SheepdogClusterRsp *)&hdr;
1865 Error *local_err = NULL;
1866 int fd;
1867 unsigned int wlen = 0, rlen = 0;
1868
1869 fd = connect_to_sdog(s, &local_err);
1870 if (fd < 0) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +01001871 error_report_err(local_err);
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001872 ret = -EIO;
1873 goto out;
1874 }
1875
1876 memset(&hdr, 0, sizeof(hdr));
1877 hdr.opcode = SD_OP_GET_CLUSTER_DEFAULT;
1878 hdr.proto_ver = SD_PROTO_VER;
1879
1880 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr,
1881 NULL, &wlen, &rlen);
1882 closesocket(fd);
1883 if (ret) {
1884 error_setg_errno(errp, -ret, "failed to get cluster default");
1885 goto out;
1886 }
1887 if (rsp->result == SD_RES_SUCCESS) {
1888 s->inode.block_size_shift = rsp->block_size_shift;
1889 } else {
1890 s->inode.block_size_shift = SD_DEFAULT_BLOCK_SIZE_SHIFT;
1891 }
1892 }
1893
1894 max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS;
1895
1896 if (s->inode.vdi_size > max_vdi_size) {
1897 error_setg(errp, "An image is too large."
1898 " The maximum image size is %"PRIu64 "GB",
1899 max_vdi_size / 1024 / 1024 / 1024);
1900 ret = -EINVAL;
1901 goto out;
1902 }
1903
Markus Armbrustere67c3992014-05-16 11:00:24 +02001904 ret = do_sd_create(s, &vid, 0, errp);
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001905 if (ret) {
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001906 goto out;
MORITA Kazutakaa8e0fdd2011-07-06 03:38:48 +09001907 }
1908
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02001909 if (prealloc) {
Markus Armbrustere67c3992014-05-16 11:00:24 +02001910 ret = sd_prealloc(filename, errp);
Markus Armbruster318df292014-05-16 11:00:21 +02001911 }
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001912out:
Chunyan Liub2222372014-06-05 17:21:05 +08001913 g_free(backing_file);
1914 g_free(buf);
MORITA Kazutakab6fc8242012-05-17 03:15:34 +09001915 g_free(s);
1916 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001917}
1918
1919static void sd_close(BlockDriverState *bs)
1920{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001921 Error *local_err = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001922 BDRVSheepdogState *s = bs->opaque;
1923 SheepdogVdiReq hdr;
1924 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1925 unsigned int wlen, rlen = 0;
1926 int fd, ret;
1927
Peter Maydell2440a2c2013-07-29 13:16:36 +01001928 DPRINTF("%s\n", s->name);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001929
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001930 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001931 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001932 error_report_err(local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001933 return;
1934 }
1935
1936 memset(&hdr, 0, sizeof(hdr));
1937
1938 hdr.opcode = SD_OP_RELEASE_VDI;
Hitoshi Mitake1dbfafe2014-08-11 14:43:45 +09001939 hdr.type = LOCK_TYPE_NORMAL;
Liu Yuan9f23fce2014-01-03 20:13:12 +08001940 hdr.base_vdi_id = s->inode.vdi_id;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001941 wlen = strlen(s->name) + 1;
1942 hdr.data_length = wlen;
1943 hdr.flags = SD_FLAG_CMD_WRITE;
1944
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001945 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr,
1946 s->name, &wlen, &rlen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001947
1948 closesocket(fd);
1949
1950 if (!ret && rsp->result != SD_RES_SUCCESS &&
1951 rsp->result != SD_RES_VDI_NOT_LOCKED) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001952 error_report("%s, %s", sd_strerror(rsp->result), s->name);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001953 }
1954
Fam Zhengdca21ef2015-10-23 11:08:05 +08001955 aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd,
1956 false, NULL, NULL, NULL);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001957 closesocket(s->fd);
MORITA Kazutaka25af2572013-02-22 12:39:52 +09001958 g_free(s->host_spec);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001959}
1960
1961static int64_t sd_getlength(BlockDriverState *bs)
1962{
1963 BDRVSheepdogState *s = bs->opaque;
1964
1965 return s->inode.vdi_size;
1966}
1967
1968static int sd_truncate(BlockDriverState *bs, int64_t offset)
1969{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001970 Error *local_err = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001971 BDRVSheepdogState *s = bs->opaque;
1972 int ret, fd;
1973 unsigned int datalen;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001974 uint64_t max_vdi_size;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001975
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001976 max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001977 if (offset < s->inode.vdi_size) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001978 error_report("shrinking is not supported");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001979 return -EINVAL;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09001980 } else if (offset > max_vdi_size) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001981 error_report("too big image size");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001982 return -EINVAL;
1983 }
1984
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02001985 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001986 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001987 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09001988 return fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001989 }
1990
1991 /* we don't need to update entire object */
1992 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1993 s->inode.vdi_size = offset;
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02001994 ret = write_object(fd, s->aio_context, (char *)&s->inode,
1995 vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies,
1996 datalen, 0, false, s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09001997 close(fd);
1998
1999 if (ret < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002000 error_report("failed to update an inode.");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002001 }
2002
MORITA Kazutakacb595882012-05-17 03:15:33 +09002003 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002004}
2005
2006/*
2007 * This function is called after writing data objects. If we need to
2008 * update metadata, this sends a write request to the vdi object.
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002009 * Otherwise, this switches back to sd_co_readv/writev.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002010 */
Paolo Bonzinid8716b42011-10-05 09:17:31 +02002011static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002012{
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002013 BDRVSheepdogState *s = acb->common.bs->opaque;
2014 struct iovec iov;
2015 AIOReq *aio_req;
2016 uint32_t offset, data_len, mn, mx;
2017
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002018 mn = acb->min_dirty_data_idx;
2019 mx = acb->max_dirty_data_idx;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002020 if (mn <= mx) {
2021 /* we need to update the vdi object. */
2022 offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
2023 mn * sizeof(s->inode.data_vdi_id[0]);
2024 data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
2025
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002026 acb->min_dirty_data_idx = UINT32_MAX;
2027 acb->max_dirty_data_idx = 0;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002028
2029 iov.iov_base = &s->inode;
2030 iov.iov_len = sizeof(s->inode);
2031 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002032 data_len, offset, 0, false, 0, offset);
MORITA Kazutakac292ee62012-06-27 07:26:22 +09002033 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002034 add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002035
2036 acb->aio_done_func = sd_finish_aiocb;
2037 acb->aiocb_type = AIOCB_WRITE_UDATA;
2038 return;
2039 }
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +09002040
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002041 sd_finish_aiocb(acb);
2042}
2043
Liu Yuan859e5552013-04-25 20:49:39 +08002044/* Delete current working VDI on the snapshot chain */
2045static bool sd_delete(BDRVSheepdogState *s)
2046{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002047 Error *local_err = NULL;
Liu Yuan859e5552013-04-25 20:49:39 +08002048 unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0;
2049 SheepdogVdiReq hdr = {
2050 .opcode = SD_OP_DEL_VDI,
Liu Yuan9f23fce2014-01-03 20:13:12 +08002051 .base_vdi_id = s->inode.vdi_id,
Liu Yuan859e5552013-04-25 20:49:39 +08002052 .data_length = wlen,
2053 .flags = SD_FLAG_CMD_WRITE,
2054 };
2055 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
2056 int fd, ret;
2057
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002058 fd = connect_to_sdog(s, &local_err);
Liu Yuan859e5552013-04-25 20:49:39 +08002059 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002060 error_report_err(local_err);
Liu Yuan859e5552013-04-25 20:49:39 +08002061 return false;
2062 }
2063
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002064 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr,
2065 s->name, &wlen, &rlen);
Liu Yuan859e5552013-04-25 20:49:39 +08002066 closesocket(fd);
2067 if (ret) {
2068 return false;
2069 }
2070 switch (rsp->result) {
2071 case SD_RES_NO_VDI:
2072 error_report("%s was already deleted", s->name);
2073 /* fall through */
2074 case SD_RES_SUCCESS:
2075 break;
2076 default:
2077 error_report("%s, %s", sd_strerror(rsp->result), s->name);
2078 return false;
2079 }
2080
2081 return true;
2082}
2083
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002084/*
2085 * Create a writable VDI from a snapshot
2086 */
2087static int sd_create_branch(BDRVSheepdogState *s)
2088{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002089 Error *local_err = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002090 int ret, fd;
2091 uint32_t vid;
2092 char *buf;
Liu Yuan859e5552013-04-25 20:49:39 +08002093 bool deleted;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002094
Peter Maydell2440a2c2013-07-29 13:16:36 +01002095 DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002096
Anthony Liguori7267c092011-08-20 22:09:37 -05002097 buf = g_malloc(SD_INODE_SIZE);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002098
Liu Yuan859e5552013-04-25 20:49:39 +08002099 /*
2100 * Even If deletion fails, we will just create extra snapshot based on
Deepak Kathayatdc6fb732014-03-24 16:30:17 +08002101 * the working VDI which was supposed to be deleted. So no need to
Liu Yuan859e5552013-04-25 20:49:39 +08002102 * false bail out.
2103 */
2104 deleted = sd_delete(s);
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02002105 ret = do_sd_create(s, &vid, !deleted, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002106 if (ret) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002107 error_report_err(local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002108 goto out;
2109 }
2110
Peter Maydell2440a2c2013-07-29 13:16:36 +01002111 DPRINTF("%" PRIx32 " is created.\n", vid);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002112
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002113 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002114 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002115 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002116 ret = fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002117 goto out;
2118 }
2119
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002120 ret = read_object(fd, s->aio_context, buf, vid_to_vdi_oid(vid),
2121 s->inode.nr_copies, SD_INODE_SIZE, 0, s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002122
2123 closesocket(fd);
2124
2125 if (ret < 0) {
2126 goto out;
2127 }
2128
2129 memcpy(&s->inode, buf, sizeof(s->inode));
2130
MORITA Kazutaka2f536802012-10-07 01:57:14 +09002131 s->is_snapshot = false;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002132 ret = 0;
Peter Maydell2440a2c2013-07-29 13:16:36 +01002133 DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002134
2135out:
Anthony Liguori7267c092011-08-20 22:09:37 -05002136 g_free(buf);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002137
2138 return ret;
2139}
2140
2141/*
2142 * Send I/O requests to the server.
2143 *
2144 * This function sends requests to the server, links the requests to
MORITA Kazutakac292ee62012-06-27 07:26:22 +09002145 * the inflight_list in BDRVSheepdogState, and exits without
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002146 * waiting the response. The responses are received in the
2147 * `aio_read_response' function which is called from the main loop as
2148 * a fd handler.
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002149 *
2150 * Returns 1 when we need to wait a response, 0 when there is no sent
2151 * request and -errno in error cases.
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002152 */
Paolo Bonzinid8716b42011-10-05 09:17:31 +02002153static int coroutine_fn sd_co_rw_vector(void *p)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002154{
2155 SheepdogAIOCB *acb = p;
2156 int ret = 0;
Liu Yuane8bfaa22013-04-23 14:03:34 +08002157 unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002158 unsigned long idx;
2159 uint32_t object_size;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002160 uint64_t oid;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002161 uint64_t offset;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002162 BDRVSheepdogState *s = acb->common.bs->opaque;
2163 SheepdogInode *inode = &s->inode;
2164 AIOReq *aio_req;
2165
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002166 if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
2167 /*
2168 * In the case we open the snapshot VDI, Sheepdog creates the
2169 * writable VDI when we do a write operation first.
2170 */
2171 ret = sd_create_branch(s);
2172 if (ret) {
2173 acb->ret = -EIO;
2174 goto out;
2175 }
2176 }
2177
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002178 object_size = (UINT32_C(1) << inode->block_size_shift);
2179 idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size;
2180 offset = (acb->sector_num * BDRV_SECTOR_SIZE) % object_size;
2181
MORITA Kazutaka1d732d72012-06-27 07:26:21 +09002182 /*
2183 * Make sure we don't free the aiocb before we are done with all requests.
2184 * This additional reference is dropped at the end of this function.
2185 */
2186 acb->nr_pending++;
2187
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002188 while (done != total) {
2189 uint8_t flags = 0;
2190 uint64_t old_oid = 0;
MORITA Kazutaka2f536802012-10-07 01:57:14 +09002191 bool create = false;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002192
2193 oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
2194
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002195 len = MIN(total - done, object_size - offset);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002196
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002197 switch (acb->aiocb_type) {
2198 case AIOCB_READ_UDATA:
2199 if (!inode->data_vdi_id[idx]) {
2200 qemu_iovec_memset(acb->qiov, done, 0, len);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002201 goto done;
2202 }
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002203 break;
2204 case AIOCB_WRITE_UDATA:
2205 if (!inode->data_vdi_id[idx]) {
MORITA Kazutaka2f536802012-10-07 01:57:14 +09002206 create = true;
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002207 } else if (!is_data_obj_writable(inode, idx)) {
2208 /* Copy-On-Write */
MORITA Kazutaka2f536802012-10-07 01:57:14 +09002209 create = true;
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002210 old_oid = oid;
2211 flags = SD_FLAG_CMD_COW;
2212 }
2213 break;
Liu Yuancac8f4a2013-04-23 14:03:33 +08002214 case AIOCB_DISCARD_OBJ:
2215 /*
2216 * We discard the object only when the whole object is
2217 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2218 */
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002219 if (len != object_size || inode->data_vdi_id[idx] == 0) {
Liu Yuancac8f4a2013-04-23 14:03:33 +08002220 goto done;
2221 }
2222 break;
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002223 default:
2224 break;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002225 }
2226
2227 if (create) {
Peter Maydell2440a2c2013-07-29 13:16:36 +01002228 DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
MORITA Kazutaka1b6ac992012-06-27 07:26:18 +09002229 inode->vdi_id, oid,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002230 vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
2231 oid = vid_to_data_oid(inode->vdi_id, idx);
Peter Maydell2440a2c2013-07-29 13:16:36 +01002232 DPRINTF("new oid %" PRIx64 "\n", oid);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002233 }
2234
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002235 aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, create,
Hitoshi Mitakee6fd57e2015-09-01 12:03:10 +09002236 old_oid,
2237 acb->aiocb_type == AIOCB_DISCARD_OBJ ?
2238 0 : done);
MORITA Kazutaka80308d32013-10-24 16:01:18 +09002239 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002240
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002241 add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
MORITA Kazutakaa37dcdf2013-10-24 16:01:16 +09002242 acb->aiocb_type);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002243 done:
2244 offset = 0;
2245 idx++;
2246 done += len;
2247 }
2248out:
MORITA Kazutaka1d732d72012-06-27 07:26:21 +09002249 if (!--acb->nr_pending) {
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002250 return acb->ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002251 }
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002252 return 1;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002253}
2254
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002255static bool check_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *aiocb)
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002256{
2257 SheepdogAIOCB *cb;
2258
2259 QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) {
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002260 if (AIOCBOverlapping(aiocb, cb)) {
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002261 return true;
2262 }
2263 }
2264
2265 QLIST_INSERT_HEAD(&s->inflight_aiocb_head, aiocb, aiocb_siblings);
2266 return false;
2267}
2268
Dong Xu Wanga9681682011-11-10 16:23:22 +08002269static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002270 int nb_sectors, QEMUIOVector *qiov)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002271{
2272 SheepdogAIOCB *acb;
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002273 int ret;
Liu Yuane50d7602013-12-14 01:29:28 +08002274 int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE;
2275 BDRVSheepdogState *s = bs->opaque;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002276
Max Reitzc0191e72015-02-05 13:58:24 -05002277 if (offset > s->inode.vdi_size) {
Liu Yuane50d7602013-12-14 01:29:28 +08002278 ret = sd_truncate(bs, offset);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002279 if (ret < 0) {
2280 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002281 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002282 }
2283
Liu Yuanf700f8e2013-01-14 14:01:03 +08002284 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002285 acb->aio_done_func = sd_write_done;
2286 acb->aiocb_type = AIOCB_WRITE_UDATA;
2287
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002288retry:
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002289 if (check_overlapping_aiocb(s, acb)) {
2290 qemu_co_queue_wait(&s->overlapping_queue);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002291 goto retry;
2292 }
2293
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002294 ret = sd_co_rw_vector(acb);
2295 if (ret <= 0) {
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002296 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002297 qemu_co_queue_restart_all(&s->overlapping_queue);
Fam Zheng80074292014-09-11 13:41:28 +08002298 qemu_aio_unref(acb);
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002299 return ret;
2300 }
2301
2302 qemu_coroutine_yield();
2303
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002304 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002305 qemu_co_queue_restart_all(&s->overlapping_queue);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002306
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002307 return acb->ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002308}
2309
Dong Xu Wanga9681682011-11-10 16:23:22 +08002310static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002311 int nb_sectors, QEMUIOVector *qiov)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002312{
2313 SheepdogAIOCB *acb;
Christoph Hellwig19db9b92012-07-10 16:12:27 +02002314 int ret;
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002315 BDRVSheepdogState *s = bs->opaque;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002316
Liu Yuanf700f8e2013-01-14 14:01:03 +08002317 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002318 acb->aiocb_type = AIOCB_READ_UDATA;
2319 acb->aio_done_func = sd_finish_aiocb;
2320
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002321retry:
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002322 if (check_overlapping_aiocb(s, acb)) {
2323 qemu_co_queue_wait(&s->overlapping_queue);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002324 goto retry;
2325 }
2326
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002327 ret = sd_co_rw_vector(acb);
2328 if (ret <= 0) {
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002329 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002330 qemu_co_queue_restart_all(&s->overlapping_queue);
Fam Zheng80074292014-09-11 13:41:28 +08002331 qemu_aio_unref(acb);
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002332 return ret;
2333 }
2334
2335 qemu_coroutine_yield();
2336
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002337 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002338 qemu_co_queue_restart_all(&s->overlapping_queue);
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002339 return acb->ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002340}
2341
Liu Yuan47622c42012-04-04 04:03:58 +08002342static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
2343{
2344 BDRVSheepdogState *s = bs->opaque;
Liu Yuan47783072013-01-15 16:28:55 +08002345 SheepdogAIOCB *acb;
2346 AIOReq *aio_req;
Liu Yuan47622c42012-04-04 04:03:58 +08002347
Liu Yuan0e7106d2013-01-10 16:03:47 +08002348 if (s->cache_flags != SD_FLAG_CMD_CACHE) {
Liu Yuan47622c42012-04-04 04:03:58 +08002349 return 0;
2350 }
2351
Liu Yuanf700f8e2013-01-14 14:01:03 +08002352 acb = sd_aio_setup(bs, NULL, 0, 0);
Liu Yuan47783072013-01-15 16:28:55 +08002353 acb->aiocb_type = AIOCB_FLUSH_CACHE;
2354 acb->aio_done_func = sd_finish_aiocb;
Liu Yuan47622c42012-04-04 04:03:58 +08002355
Liu Yuan47783072013-01-15 16:28:55 +08002356 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002357 0, 0, 0, false, 0, 0);
Liu Yuan47783072013-01-15 16:28:55 +08002358 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
Hitoshi Mitakeb544c1a2014-06-06 13:35:11 +09002359 add_aio_request(s, aio_req, NULL, 0, acb->aiocb_type);
Liu Yuan47622c42012-04-04 04:03:58 +08002360
Liu Yuan47783072013-01-15 16:28:55 +08002361 qemu_coroutine_yield();
2362 return acb->ret;
Liu Yuan47622c42012-04-04 04:03:58 +08002363}
2364
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002365static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
2366{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002367 Error *local_err = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002368 BDRVSheepdogState *s = bs->opaque;
2369 int ret, fd;
2370 uint32_t new_vid;
2371 SheepdogInode *inode;
2372 unsigned int datalen;
2373
Peter Maydell2440a2c2013-07-29 13:16:36 +01002374 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002375 "is_snapshot %d\n", sn_info->name, sn_info->id_str,
2376 s->name, sn_info->vm_state_size, s->is_snapshot);
2377
2378 if (s->is_snapshot) {
2379 error_report("You can't create a snapshot of a snapshot VDI, "
Markus Armbruster6daf1942011-06-22 14:03:54 +02002380 "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002381
2382 return -EINVAL;
2383 }
2384
Peter Maydell2440a2c2013-07-29 13:16:36 +01002385 DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002386
2387 s->inode.vm_state_size = sn_info->vm_state_size;
2388 s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
Jim Meyering3178e272012-10-04 13:09:47 +02002389 /* It appears that inode.tag does not require a NUL terminator,
2390 * which means this use of strncpy is ok.
2391 */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002392 strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
2393 /* we don't need to update entire object */
2394 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
Markus Armbruster2df5fee2014-05-28 11:17:06 +02002395 inode = g_malloc(datalen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002396
2397 /* refresh inode. */
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002398 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002399 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002400 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002401 ret = fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002402 goto cleanup;
2403 }
2404
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002405 ret = write_object(fd, s->aio_context, (char *)&s->inode,
2406 vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies,
2407 datalen, 0, false, s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002408 if (ret < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002409 error_report("failed to write snapshot's inode.");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002410 goto cleanup;
2411 }
2412
Markus Armbruster7d2d3e72014-05-16 11:00:22 +02002413 ret = do_sd_create(s, &new_vid, 1, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002414 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002415 error_reportf_err(local_err,
2416 "failed to create inode for snapshot: ");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002417 goto cleanup;
2418 }
2419
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002420 ret = read_object(fd, s->aio_context, (char *)inode,
2421 vid_to_vdi_oid(new_vid), s->inode.nr_copies, datalen, 0,
2422 s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002423
2424 if (ret < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002425 error_report("failed to read new inode info. %s", strerror(errno));
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002426 goto cleanup;
2427 }
2428
2429 memcpy(&s->inode, inode, datalen);
Peter Maydell2440a2c2013-07-29 13:16:36 +01002430 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002431 s->inode.name, s->inode.snap_id, s->inode.vdi_id);
2432
2433cleanup:
Markus Armbruster2df5fee2014-05-28 11:17:06 +02002434 g_free(inode);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002435 closesocket(fd);
2436 return ret;
2437}
2438
Liu Yuan859e5552013-04-25 20:49:39 +08002439/*
2440 * We implement rollback(loadvm) operation to the specified snapshot by
2441 * 1) switch to the snapshot
2442 * 2) rely on sd_create_branch to delete working VDI and
Deepak Kathayatdc6fb732014-03-24 16:30:17 +08002443 * 3) create a new working VDI based on the specified snapshot
Liu Yuan859e5552013-04-25 20:49:39 +08002444 */
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002445static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
2446{
2447 BDRVSheepdogState *s = bs->opaque;
2448 BDRVSheepdogState *old_s;
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09002449 char tag[SD_MAX_VDI_TAG_LEN];
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002450 uint32_t snapid = 0;
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09002451 int ret = 0;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002452
Markus Armbruster5839e532014-08-19 10:31:08 +02002453 old_s = g_new(BDRVSheepdogState, 1);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002454
2455 memcpy(old_s, s, sizeof(BDRVSheepdogState));
2456
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002457 snapid = strtoul(snapshot_id, NULL, 10);
Jim Meyering3178e272012-10-04 13:09:47 +02002458 if (snapid) {
2459 tag[0] = 0;
2460 } else {
Liu Yuanb579ffb2013-06-08 01:54:25 +08002461 pstrcpy(tag, sizeof(tag), snapshot_id);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002462 }
2463
MORITA Kazutaka9ff53a02013-04-26 01:19:53 +09002464 ret = reload_inode(s, snapid, tag);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002465 if (ret) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002466 goto out;
2467 }
2468
Liu Yuancede6212013-06-08 01:54:26 +08002469 ret = sd_create_branch(s);
2470 if (ret) {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002471 goto out;
2472 }
2473
Anthony Liguori7267c092011-08-20 22:09:37 -05002474 g_free(old_s);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002475
2476 return 0;
2477out:
2478 /* recover bdrv_sd_state */
2479 memcpy(s, old_s, sizeof(BDRVSheepdogState));
Anthony Liguori7267c092011-08-20 22:09:37 -05002480 g_free(old_s);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002481
Markus Armbruster6daf1942011-06-22 14:03:54 +02002482 error_report("failed to open. recover old bdrv_sd_state.");
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002483
2484 return ret;
2485}
2486
Vasiliy Tolstoveab8eb82015-12-23 21:22:26 +09002487#define NR_BATCHED_DISCARD 128
2488
2489static bool remove_objects(BDRVSheepdogState *s)
2490{
2491 int fd, i = 0, nr_objs = 0;
2492 Error *local_err = NULL;
2493 int ret = 0;
2494 bool result = true;
2495 SheepdogInode *inode = &s->inode;
2496
2497 fd = connect_to_sdog(s, &local_err);
2498 if (fd < 0) {
2499 error_report_err(local_err);
2500 return false;
2501 }
2502
2503 nr_objs = count_data_objs(inode);
2504 while (i < nr_objs) {
2505 int start_idx, nr_filled_idx;
2506
2507 while (i < nr_objs && !inode->data_vdi_id[i]) {
2508 i++;
2509 }
2510 start_idx = i;
2511
2512 nr_filled_idx = 0;
2513 while (i < nr_objs && nr_filled_idx < NR_BATCHED_DISCARD) {
2514 if (inode->data_vdi_id[i]) {
2515 inode->data_vdi_id[i] = 0;
2516 nr_filled_idx++;
2517 }
2518
2519 i++;
2520 }
2521
2522 ret = write_object(fd, s->aio_context,
2523 (char *)&inode->data_vdi_id[start_idx],
2524 vid_to_vdi_oid(s->inode.vdi_id), inode->nr_copies,
2525 (i - start_idx) * sizeof(uint32_t),
2526 offsetof(struct SheepdogInode,
2527 data_vdi_id[start_idx]),
2528 false, s->cache_flags);
2529 if (ret < 0) {
2530 error_report("failed to discard snapshot inode.");
2531 result = false;
2532 goto out;
2533 }
2534 }
2535
2536out:
2537 closesocket(fd);
2538 return result;
2539}
2540
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002541static int sd_snapshot_delete(BlockDriverState *bs,
2542 const char *snapshot_id,
2543 const char *name,
2544 Error **errp)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002545{
Vasiliy Tolstoveab8eb82015-12-23 21:22:26 +09002546 uint32_t snap_id = 0;
2547 char snap_tag[SD_MAX_VDI_TAG_LEN];
2548 Error *local_err = NULL;
2549 int fd, ret;
2550 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
2551 BDRVSheepdogState *s = bs->opaque;
2552 unsigned int wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN, rlen = 0;
2553 uint32_t vid;
2554 SheepdogVdiReq hdr = {
2555 .opcode = SD_OP_DEL_VDI,
2556 .data_length = wlen,
2557 .flags = SD_FLAG_CMD_WRITE,
2558 };
2559 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
2560
2561 if (!remove_objects(s)) {
2562 return -1;
2563 }
2564
2565 memset(buf, 0, sizeof(buf));
2566 memset(snap_tag, 0, sizeof(snap_tag));
2567 pstrcpy(buf, SD_MAX_VDI_LEN, s->name);
2568 if (qemu_strtoul(snapshot_id, NULL, 10, (unsigned long *)&snap_id)) {
2569 return -1;
2570 }
2571
2572 if (snap_id) {
2573 hdr.snapid = snap_id;
2574 } else {
2575 pstrcpy(snap_tag, sizeof(snap_tag), snapshot_id);
2576 pstrcpy(buf + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag);
2577 }
2578
2579 ret = find_vdi_name(s, s->name, snap_id, snap_tag, &vid, true,
2580 &local_err);
2581 if (ret) {
2582 return ret;
2583 }
2584
2585 fd = connect_to_sdog(s, &local_err);
2586 if (fd < 0) {
2587 error_report_err(local_err);
2588 return -1;
2589 }
2590
2591 ret = do_req(fd, s->aio_context, (SheepdogReq *)&hdr,
2592 buf, &wlen, &rlen);
2593 closesocket(fd);
2594 if (ret) {
2595 return ret;
2596 }
2597
2598 switch (rsp->result) {
2599 case SD_RES_NO_VDI:
2600 error_report("%s was already deleted", s->name);
2601 case SD_RES_SUCCESS:
2602 break;
2603 default:
2604 error_report("%s, %s", sd_strerror(rsp->result), s->name);
2605 return -1;
2606 }
2607
2608 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002609}
2610
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002611static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
2612{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002613 Error *local_err = NULL;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002614 BDRVSheepdogState *s = bs->opaque;
2615 SheepdogReq req;
2616 int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
2617 QEMUSnapshotInfo *sn_tab = NULL;
2618 unsigned wlen, rlen;
2619 int found = 0;
2620 static SheepdogInode inode;
2621 unsigned long *vdi_inuse;
2622 unsigned int start_nr;
2623 uint64_t hval;
2624 uint32_t vid;
2625
Anthony Liguori7267c092011-08-20 22:09:37 -05002626 vdi_inuse = g_malloc(max);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002627
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002628 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002629 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002630 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002631 ret = fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002632 goto out;
2633 }
2634
2635 rlen = max;
2636 wlen = 0;
2637
2638 memset(&req, 0, sizeof(req));
2639
2640 req.opcode = SD_OP_READ_VDIS;
2641 req.data_length = max;
2642
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002643 ret = do_req(fd, s->aio_context, (SheepdogReq *)&req,
2644 vdi_inuse, &wlen, &rlen);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002645
2646 closesocket(fd);
2647 if (ret) {
2648 goto out;
2649 }
2650
Markus Armbruster02c4f262014-08-19 10:31:09 +02002651 sn_tab = g_new0(QEMUSnapshotInfo, nr);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002652
2653 /* calculate a vdi id with hash function */
2654 hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
2655 start_nr = hval & (SD_NR_VDIS - 1);
2656
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002657 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002658 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002659 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002660 ret = fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002661 goto out;
2662 }
2663
2664 for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
2665 if (!test_bit(vid, vdi_inuse)) {
2666 break;
2667 }
2668
2669 /* we don't need to read entire object */
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002670 ret = read_object(fd, s->aio_context, (char *)&inode,
2671 vid_to_vdi_oid(vid),
Liu Yuan47622c42012-04-04 04:03:58 +08002672 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
Liu Yuan0e7106d2013-01-10 16:03:47 +08002673 s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002674
2675 if (ret) {
2676 continue;
2677 }
2678
2679 if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
2680 sn_tab[found].date_sec = inode.snap_ctime >> 32;
2681 sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
2682 sn_tab[found].vm_state_size = inode.vm_state_size;
2683 sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
2684
Max Reitz521b2b52014-04-29 19:03:12 +02002685 snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str),
2686 "%" PRIu32, inode.snap_id);
Jim Meyering3178e272012-10-04 13:09:47 +02002687 pstrcpy(sn_tab[found].name,
2688 MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
2689 inode.tag);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002690 found++;
2691 }
2692 }
2693
2694 closesocket(fd);
2695out:
2696 *psn_tab = sn_tab;
2697
Anthony Liguori7267c092011-08-20 22:09:37 -05002698 g_free(vdi_inuse);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002699
MORITA Kazutakacb595882012-05-17 03:15:33 +09002700 if (ret < 0) {
2701 return ret;
2702 }
2703
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002704 return found;
2705}
2706
2707static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
2708 int64_t pos, int size, int load)
2709{
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002710 Error *local_err = NULL;
MORITA Kazutaka2f536802012-10-07 01:57:14 +09002711 bool create;
2712 int fd, ret = 0, remaining = size;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002713 unsigned int data_len;
2714 uint64_t vmstate_oid;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002715 uint64_t offset;
Liu Yuancede6212013-06-08 01:54:26 +08002716 uint32_t vdi_index;
2717 uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002718 uint32_t object_size = (UINT32_C(1) << s->inode.block_size_shift);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002719
Markus Armbrusterdfb12bf2014-05-16 11:00:19 +02002720 fd = connect_to_sdog(s, &local_err);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002721 if (fd < 0) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002722 error_report_err(local_err);
MORITA Kazutakacb595882012-05-17 03:15:33 +09002723 return fd;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002724 }
2725
MORITA Kazutaka6f3c7142012-05-30 01:05:15 +09002726 while (remaining) {
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002727 vdi_index = pos / object_size;
2728 offset = pos % object_size;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002729
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002730 data_len = MIN(remaining, object_size - offset);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002731
Liu Yuancede6212013-06-08 01:54:26 +08002732 vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002733
2734 create = (offset == 0);
2735 if (load) {
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002736 ret = read_object(fd, s->aio_context, (char *)data, vmstate_oid,
Liu Yuan47622c42012-04-04 04:03:58 +08002737 s->inode.nr_copies, data_len, offset,
Liu Yuan0e7106d2013-01-10 16:03:47 +08002738 s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002739 } else {
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002740 ret = write_object(fd, s->aio_context, (char *)data, vmstate_oid,
Liu Yuan47622c42012-04-04 04:03:58 +08002741 s->inode.nr_copies, data_len, offset, create,
Liu Yuan0e7106d2013-01-10 16:03:47 +08002742 s->cache_flags);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002743 }
2744
2745 if (ret < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002746 error_report("failed to save vmstate %s", strerror(errno));
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002747 goto cleanup;
2748 }
2749
2750 pos += data_len;
MORITA Kazutaka1f7a48d2012-08-30 03:39:45 +09002751 data += data_len;
MORITA Kazutaka6f3c7142012-05-30 01:05:15 +09002752 remaining -= data_len;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002753 }
MORITA Kazutaka6f3c7142012-05-30 01:05:15 +09002754 ret = size;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002755cleanup:
2756 closesocket(fd);
2757 return ret;
2758}
2759
Kevin Wolfcf8074b2013-04-05 21:27:53 +02002760static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2761 int64_t pos)
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002762{
2763 BDRVSheepdogState *s = bs->opaque;
Kevin Wolfcf8074b2013-04-05 21:27:53 +02002764 void *buf;
2765 int ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002766
Kevin Wolfcf8074b2013-04-05 21:27:53 +02002767 buf = qemu_blockalign(bs, qiov->size);
2768 qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
2769 ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0);
2770 qemu_vfree(buf);
2771
2772 return ret;
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002773}
2774
2775static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
2776 int64_t pos, int size)
2777{
2778 BDRVSheepdogState *s = bs->opaque;
2779
2780 return do_load_save_vmstate(s, data, pos, size, 1);
2781}
2782
2783
Liu Yuancac8f4a2013-04-23 14:03:33 +08002784static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num,
2785 int nb_sectors)
2786{
2787 SheepdogAIOCB *acb;
Liu Yuancac8f4a2013-04-23 14:03:33 +08002788 BDRVSheepdogState *s = bs->opaque;
2789 int ret;
Hitoshi Mitakee6fd57e2015-09-01 12:03:10 +09002790 QEMUIOVector discard_iov;
2791 struct iovec iov;
2792 uint32_t zero = 0;
Liu Yuancac8f4a2013-04-23 14:03:33 +08002793
2794 if (!s->discard_supported) {
2795 return 0;
2796 }
2797
Hitoshi Mitakee6fd57e2015-09-01 12:03:10 +09002798 memset(&discard_iov, 0, sizeof(discard_iov));
2799 memset(&iov, 0, sizeof(iov));
2800 iov.iov_base = &zero;
2801 iov.iov_len = sizeof(zero);
2802 discard_iov.iov = &iov;
2803 discard_iov.niov = 1;
2804 acb = sd_aio_setup(bs, &discard_iov, sector_num, nb_sectors);
Liu Yuancac8f4a2013-04-23 14:03:33 +08002805 acb->aiocb_type = AIOCB_DISCARD_OBJ;
2806 acb->aio_done_func = sd_finish_aiocb;
2807
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002808retry:
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002809 if (check_overlapping_aiocb(s, acb)) {
2810 qemu_co_queue_wait(&s->overlapping_queue);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002811 goto retry;
2812 }
2813
Liu Yuancac8f4a2013-04-23 14:03:33 +08002814 ret = sd_co_rw_vector(acb);
2815 if (ret <= 0) {
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002816 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002817 qemu_co_queue_restart_all(&s->overlapping_queue);
Fam Zheng80074292014-09-11 13:41:28 +08002818 qemu_aio_unref(acb);
Liu Yuancac8f4a2013-04-23 14:03:33 +08002819 return ret;
2820 }
2821
2822 qemu_coroutine_yield();
2823
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002824 QLIST_REMOVE(acb, aiocb_siblings);
Hitoshi Mitake498f2142015-09-01 12:03:09 +09002825 qemu_co_queue_restart_all(&s->overlapping_queue);
Hitoshi Mitake6a55c822015-07-18 01:44:24 +09002826
Liu Yuancac8f4a2013-04-23 14:03:33 +08002827 return acb->ret;
2828}
2829
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02002830static coroutine_fn int64_t
2831sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
Fam Zheng67a0fd22016-01-26 11:58:48 +08002832 int *pnum, BlockDriverState **file)
Liu Yuan8d71c632013-04-23 14:03:35 +08002833{
2834 BDRVSheepdogState *s = bs->opaque;
2835 SheepdogInode *inode = &s->inode;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002836 uint32_t object_size = (UINT32_C(1) << inode->block_size_shift);
Liu Yuan9cd76732014-01-22 01:14:11 +08002837 uint64_t offset = sector_num * BDRV_SECTOR_SIZE;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002838 unsigned long start = offset / object_size,
Liu Yuan8d71c632013-04-23 14:03:35 +08002839 end = DIV_ROUND_UP((sector_num + nb_sectors) *
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002840 BDRV_SECTOR_SIZE, object_size);
Liu Yuan8d71c632013-04-23 14:03:35 +08002841 unsigned long idx;
Liu Yuan9cd76732014-01-22 01:14:11 +08002842 int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
Liu Yuan8d71c632013-04-23 14:03:35 +08002843
2844 for (idx = start; idx < end; idx++) {
2845 if (inode->data_vdi_id[idx] == 0) {
2846 break;
2847 }
2848 }
2849 if (idx == start) {
2850 /* Get the longest length of unallocated sectors */
2851 ret = 0;
2852 for (idx = start + 1; idx < end; idx++) {
2853 if (inode->data_vdi_id[idx] != 0) {
2854 break;
2855 }
2856 }
2857 }
2858
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002859 *pnum = (idx - start) * object_size / BDRV_SECTOR_SIZE;
Liu Yuan8d71c632013-04-23 14:03:35 +08002860 if (*pnum > nb_sectors) {
2861 *pnum = nb_sectors;
2862 }
Fam Zhengd234c922016-01-26 11:58:55 +08002863 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
2864 *file = bs;
2865 }
Liu Yuan8d71c632013-04-23 14:03:35 +08002866 return ret;
2867}
2868
Liu Yuan85829722013-08-07 16:59:53 +08002869static int64_t sd_get_allocated_file_size(BlockDriverState *bs)
2870{
2871 BDRVSheepdogState *s = bs->opaque;
2872 SheepdogInode *inode = &s->inode;
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002873 uint32_t object_size = (UINT32_C(1) << inode->block_size_shift);
2874 unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, object_size);
Liu Yuan85829722013-08-07 16:59:53 +08002875 uint64_t size = 0;
2876
2877 for (i = 0; i < last; i++) {
2878 if (inode->data_vdi_id[i] == 0) {
2879 continue;
2880 }
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002881 size += object_size;
Liu Yuan85829722013-08-07 16:59:53 +08002882 }
2883 return size;
2884}
2885
Chunyan Liub2222372014-06-05 17:21:05 +08002886static QemuOptsList sd_create_opts = {
2887 .name = "sheepdog-create-opts",
2888 .head = QTAILQ_HEAD_INITIALIZER(sd_create_opts.head),
2889 .desc = {
2890 {
2891 .name = BLOCK_OPT_SIZE,
2892 .type = QEMU_OPT_SIZE,
2893 .help = "Virtual disk size"
2894 },
2895 {
2896 .name = BLOCK_OPT_BACKING_FILE,
2897 .type = QEMU_OPT_STRING,
2898 .help = "File name of a base image"
2899 },
2900 {
2901 .name = BLOCK_OPT_PREALLOC,
2902 .type = QEMU_OPT_STRING,
2903 .help = "Preallocation mode (allowed values: off, full)"
2904 },
2905 {
2906 .name = BLOCK_OPT_REDUNDANCY,
2907 .type = QEMU_OPT_STRING,
2908 .help = "Redundancy of the image"
2909 },
Teruaki Ishizaki876eb1b2015-02-13 18:20:53 +09002910 {
2911 .name = BLOCK_OPT_OBJECT_SIZE,
2912 .type = QEMU_OPT_SIZE,
2913 .help = "Object size of the image"
2914 },
Chunyan Liub2222372014-06-05 17:21:05 +08002915 { /* end of list */ }
2916 }
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002917};
2918
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002919static BlockDriver bdrv_sheepdog = {
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002920 .format_name = "sheepdog",
2921 .protocol_name = "sheepdog",
2922 .instance_size = sizeof(BDRVSheepdogState),
Benoît Canet030be322013-09-24 17:07:04 +02002923 .bdrv_needs_filename = true,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002924 .bdrv_file_open = sd_open,
Liu Yuan4da65c82015-08-28 10:53:58 +08002925 .bdrv_reopen_prepare = sd_reopen_prepare,
2926 .bdrv_reopen_commit = sd_reopen_commit,
2927 .bdrv_reopen_abort = sd_reopen_abort,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002928 .bdrv_close = sd_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002929 .bdrv_create = sd_create,
Liu Yuane4f5c1b2013-08-06 14:44:37 +08002930 .bdrv_has_zero_init = bdrv_has_zero_init_1,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002931 .bdrv_getlength = sd_getlength,
Liu Yuan85829722013-08-07 16:59:53 +08002932 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002933 .bdrv_truncate = sd_truncate,
2934
MORITA Kazutaka2df46242011-08-12 21:33:15 +09002935 .bdrv_co_readv = sd_co_readv,
2936 .bdrv_co_writev = sd_co_writev,
Liu Yuan47622c42012-04-04 04:03:58 +08002937 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
Liu Yuancac8f4a2013-04-23 14:03:33 +08002938 .bdrv_co_discard = sd_co_discard,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02002939 .bdrv_co_get_block_status = sd_co_get_block_status,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002940
2941 .bdrv_snapshot_create = sd_snapshot_create,
2942 .bdrv_snapshot_goto = sd_snapshot_goto,
2943 .bdrv_snapshot_delete = sd_snapshot_delete,
2944 .bdrv_snapshot_list = sd_snapshot_list,
2945
2946 .bdrv_save_vmstate = sd_save_vmstate,
2947 .bdrv_load_vmstate = sd_load_vmstate,
2948
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002949 .bdrv_detach_aio_context = sd_detach_aio_context,
2950 .bdrv_attach_aio_context = sd_attach_aio_context,
2951
Chunyan Liub2222372014-06-05 17:21:05 +08002952 .create_opts = &sd_create_opts,
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09002953};
2954
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002955static BlockDriver bdrv_sheepdog_tcp = {
2956 .format_name = "sheepdog",
2957 .protocol_name = "sheepdog+tcp",
2958 .instance_size = sizeof(BDRVSheepdogState),
Benoît Canet030be322013-09-24 17:07:04 +02002959 .bdrv_needs_filename = true,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002960 .bdrv_file_open = sd_open,
Liu Yuan4da65c82015-08-28 10:53:58 +08002961 .bdrv_reopen_prepare = sd_reopen_prepare,
2962 .bdrv_reopen_commit = sd_reopen_commit,
2963 .bdrv_reopen_abort = sd_reopen_abort,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002964 .bdrv_close = sd_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002965 .bdrv_create = sd_create,
Liu Yuane4f5c1b2013-08-06 14:44:37 +08002966 .bdrv_has_zero_init = bdrv_has_zero_init_1,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002967 .bdrv_getlength = sd_getlength,
Liu Yuan85829722013-08-07 16:59:53 +08002968 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002969 .bdrv_truncate = sd_truncate,
2970
2971 .bdrv_co_readv = sd_co_readv,
2972 .bdrv_co_writev = sd_co_writev,
2973 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
Liu Yuancac8f4a2013-04-23 14:03:33 +08002974 .bdrv_co_discard = sd_co_discard,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02002975 .bdrv_co_get_block_status = sd_co_get_block_status,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002976
2977 .bdrv_snapshot_create = sd_snapshot_create,
2978 .bdrv_snapshot_goto = sd_snapshot_goto,
2979 .bdrv_snapshot_delete = sd_snapshot_delete,
2980 .bdrv_snapshot_list = sd_snapshot_list,
2981
2982 .bdrv_save_vmstate = sd_save_vmstate,
2983 .bdrv_load_vmstate = sd_load_vmstate,
2984
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02002985 .bdrv_detach_aio_context = sd_detach_aio_context,
2986 .bdrv_attach_aio_context = sd_attach_aio_context,
2987
Chunyan Liub2222372014-06-05 17:21:05 +08002988 .create_opts = &sd_create_opts,
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09002989};
2990
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09002991static BlockDriver bdrv_sheepdog_unix = {
2992 .format_name = "sheepdog",
2993 .protocol_name = "sheepdog+unix",
2994 .instance_size = sizeof(BDRVSheepdogState),
Benoît Canet030be322013-09-24 17:07:04 +02002995 .bdrv_needs_filename = true,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09002996 .bdrv_file_open = sd_open,
Liu Yuan4da65c82015-08-28 10:53:58 +08002997 .bdrv_reopen_prepare = sd_reopen_prepare,
2998 .bdrv_reopen_commit = sd_reopen_commit,
2999 .bdrv_reopen_abort = sd_reopen_abort,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003000 .bdrv_close = sd_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003001 .bdrv_create = sd_create,
Peter Lieven3ac21622013-06-28 12:47:42 +02003002 .bdrv_has_zero_init = bdrv_has_zero_init_1,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003003 .bdrv_getlength = sd_getlength,
Liu Yuan85829722013-08-07 16:59:53 +08003004 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003005 .bdrv_truncate = sd_truncate,
3006
3007 .bdrv_co_readv = sd_co_readv,
3008 .bdrv_co_writev = sd_co_writev,
3009 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
Liu Yuancac8f4a2013-04-23 14:03:33 +08003010 .bdrv_co_discard = sd_co_discard,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003011 .bdrv_co_get_block_status = sd_co_get_block_status,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003012
3013 .bdrv_snapshot_create = sd_snapshot_create,
3014 .bdrv_snapshot_goto = sd_snapshot_goto,
3015 .bdrv_snapshot_delete = sd_snapshot_delete,
3016 .bdrv_snapshot_list = sd_snapshot_list,
3017
3018 .bdrv_save_vmstate = sd_save_vmstate,
3019 .bdrv_load_vmstate = sd_load_vmstate,
3020
Stefan Hajnoczi84390be2014-05-08 16:34:52 +02003021 .bdrv_detach_aio_context = sd_detach_aio_context,
3022 .bdrv_attach_aio_context = sd_attach_aio_context,
3023
Chunyan Liub2222372014-06-05 17:21:05 +08003024 .create_opts = &sd_create_opts,
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003025};
3026
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09003027static void bdrv_sheepdog_init(void)
3028{
3029 bdrv_register(&bdrv_sheepdog);
MORITA Kazutaka5d6768e2013-02-22 12:39:51 +09003030 bdrv_register(&bdrv_sheepdog_tcp);
MORITA Kazutaka1b8bbb42013-02-22 12:39:53 +09003031 bdrv_register(&bdrv_sheepdog_unix);
MORITA Kazutaka33b1db12010-06-21 05:01:00 +09003032}
3033block_init(bdrv_sheepdog_init);