Statistics
| Branch: | Revision:

root / block / sheepdog.c @ 34b5d2c6

History | View | Annotate | Download (64.2 kB)

1 33b1db1c MORITA Kazutaka
/*
2 33b1db1c MORITA Kazutaka
 * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
3 33b1db1c MORITA Kazutaka
 *
4 33b1db1c MORITA Kazutaka
 * This program is free software; you can redistribute it and/or
5 33b1db1c MORITA Kazutaka
 * modify it under the terms of the GNU General Public License version
6 33b1db1c MORITA Kazutaka
 * 2 as published by the Free Software Foundation.
7 33b1db1c MORITA Kazutaka
 *
8 33b1db1c MORITA Kazutaka
 * You should have received a copy of the GNU General Public License
9 33b1db1c MORITA Kazutaka
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
10 6b620ca3 Paolo Bonzini
 *
11 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
12 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
13 33b1db1c MORITA Kazutaka
 */
14 33b1db1c MORITA Kazutaka
15 33b1db1c MORITA Kazutaka
#include "qemu-common.h"
16 5d6768e3 MORITA Kazutaka
#include "qemu/uri.h"
17 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
18 1de7afc9 Paolo Bonzini
#include "qemu/sockets.h"
19 737e150e Paolo Bonzini
#include "block/block_int.h"
20 1de7afc9 Paolo Bonzini
#include "qemu/bitops.h"
21 33b1db1c MORITA Kazutaka
22 33b1db1c MORITA Kazutaka
#define SD_PROTO_VER 0x01
23 33b1db1c MORITA Kazutaka
24 33b1db1c MORITA Kazutaka
#define SD_DEFAULT_ADDR "localhost"
25 25af257d MORITA Kazutaka
#define SD_DEFAULT_PORT 7000
26 33b1db1c MORITA Kazutaka
27 33b1db1c MORITA Kazutaka
#define SD_OP_CREATE_AND_WRITE_OBJ  0x01
28 33b1db1c MORITA Kazutaka
#define SD_OP_READ_OBJ       0x02
29 33b1db1c MORITA Kazutaka
#define SD_OP_WRITE_OBJ      0x03
30 cac8f4a6 Liu Yuan
/* 0x04 is used internally by Sheepdog */
31 cac8f4a6 Liu Yuan
#define SD_OP_DISCARD_OBJ    0x05
32 33b1db1c MORITA Kazutaka
33 33b1db1c MORITA Kazutaka
#define SD_OP_NEW_VDI        0x11
34 33b1db1c MORITA Kazutaka
#define SD_OP_LOCK_VDI       0x12
35 33b1db1c MORITA Kazutaka
#define SD_OP_RELEASE_VDI    0x13
36 33b1db1c MORITA Kazutaka
#define SD_OP_GET_VDI_INFO   0x14
37 33b1db1c MORITA Kazutaka
#define SD_OP_READ_VDIS      0x15
38 47622c44 Liu Yuan
#define SD_OP_FLUSH_VDI      0x16
39 859e5553 Liu Yuan
#define SD_OP_DEL_VDI        0x17
40 33b1db1c MORITA Kazutaka
41 33b1db1c MORITA Kazutaka
#define SD_FLAG_CMD_WRITE    0x01
42 33b1db1c MORITA Kazutaka
#define SD_FLAG_CMD_COW      0x02
43 0e7106d8 Liu Yuan
#define SD_FLAG_CMD_CACHE    0x04 /* Writeback mode for cache */
44 0e7106d8 Liu Yuan
#define SD_FLAG_CMD_DIRECT   0x08 /* Don't use cache */
45 33b1db1c MORITA Kazutaka
46 33b1db1c MORITA Kazutaka
#define SD_RES_SUCCESS       0x00 /* Success */
47 33b1db1c MORITA Kazutaka
#define SD_RES_UNKNOWN       0x01 /* Unknown error */
48 33b1db1c MORITA Kazutaka
#define SD_RES_NO_OBJ        0x02 /* No object found */
49 33b1db1c MORITA Kazutaka
#define SD_RES_EIO           0x03 /* I/O error */
50 33b1db1c MORITA Kazutaka
#define SD_RES_VDI_EXIST     0x04 /* Vdi exists already */
51 33b1db1c MORITA Kazutaka
#define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
52 33b1db1c MORITA Kazutaka
#define SD_RES_SYSTEM_ERROR  0x06 /* System error */
53 33b1db1c MORITA Kazutaka
#define SD_RES_VDI_LOCKED    0x07 /* Vdi is locked */
54 33b1db1c MORITA Kazutaka
#define SD_RES_NO_VDI        0x08 /* No vdi found */
55 33b1db1c MORITA Kazutaka
#define SD_RES_NO_BASE_VDI   0x09 /* No base vdi found */
56 33b1db1c MORITA Kazutaka
#define SD_RES_VDI_READ      0x0A /* Cannot read requested vdi */
57 33b1db1c MORITA Kazutaka
#define SD_RES_VDI_WRITE     0x0B /* Cannot write requested vdi */
58 33b1db1c MORITA Kazutaka
#define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
59 33b1db1c MORITA Kazutaka
#define SD_RES_BASE_VDI_WRITE   0x0D /* Cannot write base vdi */
60 33b1db1c MORITA Kazutaka
#define SD_RES_NO_TAG        0x0E /* Requested tag is not found */
61 33b1db1c MORITA Kazutaka
#define SD_RES_STARTUP       0x0F /* Sheepdog is on starting up */
62 33b1db1c MORITA Kazutaka
#define SD_RES_VDI_NOT_LOCKED   0x10 /* Vdi is not locked */
63 33b1db1c MORITA Kazutaka
#define SD_RES_SHUTDOWN      0x11 /* Sheepdog is shutting down */
64 33b1db1c MORITA Kazutaka
#define SD_RES_NO_MEM        0x12 /* Cannot allocate memory */
65 33b1db1c MORITA Kazutaka
#define SD_RES_FULL_VDI      0x13 /* we already have the maximum vdis */
66 33b1db1c MORITA Kazutaka
#define SD_RES_VER_MISMATCH  0x14 /* Protocol version mismatch */
67 33b1db1c MORITA Kazutaka
#define SD_RES_NO_SPACE      0x15 /* Server has no room for new objects */
68 33b1db1c MORITA Kazutaka
#define SD_RES_WAIT_FOR_FORMAT  0x16 /* Waiting for a format operation */
69 33b1db1c MORITA Kazutaka
#define SD_RES_WAIT_FOR_JOIN    0x17 /* Waiting for other nodes joining */
70 33b1db1c MORITA Kazutaka
#define SD_RES_JOIN_FAILED   0x18 /* Target node had failed to join sheepdog */
71 fca23f0a Liu Yuan
#define SD_RES_HALT          0x19 /* Sheepdog is stopped serving IO request */
72 6a0b5490 MORITA Kazutaka
#define SD_RES_READONLY      0x1A /* Object is read-only */
73 33b1db1c MORITA Kazutaka
74 33b1db1c MORITA Kazutaka
/*
75 33b1db1c MORITA Kazutaka
 * Object ID rules
76 33b1db1c MORITA Kazutaka
 *
77 33b1db1c MORITA Kazutaka
 *  0 - 19 (20 bits): data object space
78 33b1db1c MORITA Kazutaka
 * 20 - 31 (12 bits): reserved data object space
79 33b1db1c MORITA Kazutaka
 * 32 - 55 (24 bits): vdi object space
80 33b1db1c MORITA Kazutaka
 * 56 - 59 ( 4 bits): reserved vdi object space
81 7acae208 Dong Xu Wang
 * 60 - 63 ( 4 bits): object type identifier space
82 33b1db1c MORITA Kazutaka
 */
83 33b1db1c MORITA Kazutaka
84 33b1db1c MORITA Kazutaka
#define VDI_SPACE_SHIFT   32
85 33b1db1c MORITA Kazutaka
#define VDI_BIT (UINT64_C(1) << 63)
86 33b1db1c MORITA Kazutaka
#define VMSTATE_BIT (UINT64_C(1) << 62)
87 33b1db1c MORITA Kazutaka
#define MAX_DATA_OBJS (UINT64_C(1) << 20)
88 33b1db1c MORITA Kazutaka
#define MAX_CHILDREN 1024
89 33b1db1c MORITA Kazutaka
#define SD_MAX_VDI_LEN 256
90 33b1db1c MORITA Kazutaka
#define SD_MAX_VDI_TAG_LEN 256
91 33b1db1c MORITA Kazutaka
#define SD_NR_VDIS   (1U << 24)
92 33b1db1c MORITA Kazutaka
#define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
93 33b1db1c MORITA Kazutaka
#define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
94 33b1db1c MORITA Kazutaka
95 33b1db1c MORITA Kazutaka
#define SD_INODE_SIZE (sizeof(SheepdogInode))
96 33b1db1c MORITA Kazutaka
#define CURRENT_VDI_ID 0
97 33b1db1c MORITA Kazutaka
98 33b1db1c MORITA Kazutaka
typedef struct SheepdogReq {
99 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
100 33b1db1c MORITA Kazutaka
    uint8_t opcode;
101 33b1db1c MORITA Kazutaka
    uint16_t flags;
102 33b1db1c MORITA Kazutaka
    uint32_t epoch;
103 33b1db1c MORITA Kazutaka
    uint32_t id;
104 33b1db1c MORITA Kazutaka
    uint32_t data_length;
105 33b1db1c MORITA Kazutaka
    uint32_t opcode_specific[8];
106 33b1db1c MORITA Kazutaka
} SheepdogReq;
107 33b1db1c MORITA Kazutaka
108 33b1db1c MORITA Kazutaka
typedef struct SheepdogRsp {
109 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
110 33b1db1c MORITA Kazutaka
    uint8_t opcode;
111 33b1db1c MORITA Kazutaka
    uint16_t flags;
112 33b1db1c MORITA Kazutaka
    uint32_t epoch;
113 33b1db1c MORITA Kazutaka
    uint32_t id;
114 33b1db1c MORITA Kazutaka
    uint32_t data_length;
115 33b1db1c MORITA Kazutaka
    uint32_t result;
116 33b1db1c MORITA Kazutaka
    uint32_t opcode_specific[7];
117 33b1db1c MORITA Kazutaka
} SheepdogRsp;
118 33b1db1c MORITA Kazutaka
119 33b1db1c MORITA Kazutaka
typedef struct SheepdogObjReq {
120 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
121 33b1db1c MORITA Kazutaka
    uint8_t opcode;
122 33b1db1c MORITA Kazutaka
    uint16_t flags;
123 33b1db1c MORITA Kazutaka
    uint32_t epoch;
124 33b1db1c MORITA Kazutaka
    uint32_t id;
125 33b1db1c MORITA Kazutaka
    uint32_t data_length;
126 33b1db1c MORITA Kazutaka
    uint64_t oid;
127 33b1db1c MORITA Kazutaka
    uint64_t cow_oid;
128 33b1db1c MORITA Kazutaka
    uint32_t copies;
129 33b1db1c MORITA Kazutaka
    uint32_t rsvd;
130 33b1db1c MORITA Kazutaka
    uint64_t offset;
131 33b1db1c MORITA Kazutaka
} SheepdogObjReq;
132 33b1db1c MORITA Kazutaka
133 33b1db1c MORITA Kazutaka
typedef struct SheepdogObjRsp {
134 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
135 33b1db1c MORITA Kazutaka
    uint8_t opcode;
136 33b1db1c MORITA Kazutaka
    uint16_t flags;
137 33b1db1c MORITA Kazutaka
    uint32_t epoch;
138 33b1db1c MORITA Kazutaka
    uint32_t id;
139 33b1db1c MORITA Kazutaka
    uint32_t data_length;
140 33b1db1c MORITA Kazutaka
    uint32_t result;
141 33b1db1c MORITA Kazutaka
    uint32_t copies;
142 33b1db1c MORITA Kazutaka
    uint32_t pad[6];
143 33b1db1c MORITA Kazutaka
} SheepdogObjRsp;
144 33b1db1c MORITA Kazutaka
145 33b1db1c MORITA Kazutaka
typedef struct SheepdogVdiReq {
146 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
147 33b1db1c MORITA Kazutaka
    uint8_t opcode;
148 33b1db1c MORITA Kazutaka
    uint16_t flags;
149 33b1db1c MORITA Kazutaka
    uint32_t epoch;
150 33b1db1c MORITA Kazutaka
    uint32_t id;
151 33b1db1c MORITA Kazutaka
    uint32_t data_length;
152 33b1db1c MORITA Kazutaka
    uint64_t vdi_size;
153 6f74c260 Liu Yuan
    uint32_t vdi_id;
154 33b1db1c MORITA Kazutaka
    uint32_t copies;
155 33b1db1c MORITA Kazutaka
    uint32_t snapid;
156 33b1db1c MORITA Kazutaka
    uint32_t pad[3];
157 33b1db1c MORITA Kazutaka
} SheepdogVdiReq;
158 33b1db1c MORITA Kazutaka
159 33b1db1c MORITA Kazutaka
typedef struct SheepdogVdiRsp {
160 33b1db1c MORITA Kazutaka
    uint8_t proto_ver;
161 33b1db1c MORITA Kazutaka
    uint8_t opcode;
162 33b1db1c MORITA Kazutaka
    uint16_t flags;
163 33b1db1c MORITA Kazutaka
    uint32_t epoch;
164 33b1db1c MORITA Kazutaka
    uint32_t id;
165 33b1db1c MORITA Kazutaka
    uint32_t data_length;
166 33b1db1c MORITA Kazutaka
    uint32_t result;
167 33b1db1c MORITA Kazutaka
    uint32_t rsvd;
168 33b1db1c MORITA Kazutaka
    uint32_t vdi_id;
169 33b1db1c MORITA Kazutaka
    uint32_t pad[5];
170 33b1db1c MORITA Kazutaka
} SheepdogVdiRsp;
171 33b1db1c MORITA Kazutaka
172 33b1db1c MORITA Kazutaka
typedef struct SheepdogInode {
173 33b1db1c MORITA Kazutaka
    char name[SD_MAX_VDI_LEN];
174 33b1db1c MORITA Kazutaka
    char tag[SD_MAX_VDI_TAG_LEN];
175 33b1db1c MORITA Kazutaka
    uint64_t ctime;
176 33b1db1c MORITA Kazutaka
    uint64_t snap_ctime;
177 33b1db1c MORITA Kazutaka
    uint64_t vm_clock_nsec;
178 33b1db1c MORITA Kazutaka
    uint64_t vdi_size;
179 33b1db1c MORITA Kazutaka
    uint64_t vm_state_size;
180 33b1db1c MORITA Kazutaka
    uint16_t copy_policy;
181 33b1db1c MORITA Kazutaka
    uint8_t nr_copies;
182 33b1db1c MORITA Kazutaka
    uint8_t block_size_shift;
183 33b1db1c MORITA Kazutaka
    uint32_t snap_id;
184 33b1db1c MORITA Kazutaka
    uint32_t vdi_id;
185 33b1db1c MORITA Kazutaka
    uint32_t parent_vdi_id;
186 33b1db1c MORITA Kazutaka
    uint32_t child_vdi_id[MAX_CHILDREN];
187 33b1db1c MORITA Kazutaka
    uint32_t data_vdi_id[MAX_DATA_OBJS];
188 33b1db1c MORITA Kazutaka
} SheepdogInode;
189 33b1db1c MORITA Kazutaka
190 33b1db1c MORITA Kazutaka
/*
191 33b1db1c MORITA Kazutaka
 * 64 bit FNV-1a non-zero initial basis
192 33b1db1c MORITA Kazutaka
 */
193 33b1db1c MORITA Kazutaka
#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
194 33b1db1c MORITA Kazutaka
195 33b1db1c MORITA Kazutaka
/*
196 33b1db1c MORITA Kazutaka
 * 64 bit Fowler/Noll/Vo FNV-1a hash code
197 33b1db1c MORITA Kazutaka
 */
198 33b1db1c MORITA Kazutaka
static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
199 33b1db1c MORITA Kazutaka
{
200 33b1db1c MORITA Kazutaka
    unsigned char *bp = buf;
201 33b1db1c MORITA Kazutaka
    unsigned char *be = bp + len;
202 33b1db1c MORITA Kazutaka
    while (bp < be) {
203 33b1db1c MORITA Kazutaka
        hval ^= (uint64_t) *bp++;
204 33b1db1c MORITA Kazutaka
        hval += (hval << 1) + (hval << 4) + (hval << 5) +
205 33b1db1c MORITA Kazutaka
            (hval << 7) + (hval << 8) + (hval << 40);
206 33b1db1c MORITA Kazutaka
    }
207 33b1db1c MORITA Kazutaka
    return hval;
208 33b1db1c MORITA Kazutaka
}
209 33b1db1c MORITA Kazutaka
210 2f536801 MORITA Kazutaka
static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
211 33b1db1c MORITA Kazutaka
{
212 33b1db1c MORITA Kazutaka
    return inode->vdi_id == inode->data_vdi_id[idx];
213 33b1db1c MORITA Kazutaka
}
214 33b1db1c MORITA Kazutaka
215 2f536801 MORITA Kazutaka
static inline bool is_data_obj(uint64_t oid)
216 33b1db1c MORITA Kazutaka
{
217 33b1db1c MORITA Kazutaka
    return !(VDI_BIT & oid);
218 33b1db1c MORITA Kazutaka
}
219 33b1db1c MORITA Kazutaka
220 33b1db1c MORITA Kazutaka
static inline uint64_t data_oid_to_idx(uint64_t oid)
221 33b1db1c MORITA Kazutaka
{
222 33b1db1c MORITA Kazutaka
    return oid & (MAX_DATA_OBJS - 1);
223 33b1db1c MORITA Kazutaka
}
224 33b1db1c MORITA Kazutaka
225 33b1db1c MORITA Kazutaka
static inline uint64_t vid_to_vdi_oid(uint32_t vid)
226 33b1db1c MORITA Kazutaka
{
227 33b1db1c MORITA Kazutaka
    return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
228 33b1db1c MORITA Kazutaka
}
229 33b1db1c MORITA Kazutaka
230 33b1db1c MORITA Kazutaka
static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
231 33b1db1c MORITA Kazutaka
{
232 33b1db1c MORITA Kazutaka
    return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
233 33b1db1c MORITA Kazutaka
}
234 33b1db1c MORITA Kazutaka
235 33b1db1c MORITA Kazutaka
static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
236 33b1db1c MORITA Kazutaka
{
237 33b1db1c MORITA Kazutaka
    return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
238 33b1db1c MORITA Kazutaka
}
239 33b1db1c MORITA Kazutaka
240 2f536801 MORITA Kazutaka
static inline bool is_snapshot(struct SheepdogInode *inode)
241 33b1db1c MORITA Kazutaka
{
242 33b1db1c MORITA Kazutaka
    return !!inode->snap_ctime;
243 33b1db1c MORITA Kazutaka
}
244 33b1db1c MORITA Kazutaka
245 2440a2c3 Peter Maydell
#undef DPRINTF
246 33b1db1c MORITA Kazutaka
#ifdef DEBUG_SDOG
247 2440a2c3 Peter Maydell
#define DPRINTF(fmt, args...)                                       \
248 33b1db1c MORITA Kazutaka
    do {                                                            \
249 33b1db1c MORITA Kazutaka
        fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
250 33b1db1c MORITA Kazutaka
    } while (0)
251 33b1db1c MORITA Kazutaka
#else
252 2440a2c3 Peter Maydell
#define DPRINTF(fmt, args...)
253 33b1db1c MORITA Kazutaka
#endif
254 33b1db1c MORITA Kazutaka
255 33b1db1c MORITA Kazutaka
typedef struct SheepdogAIOCB SheepdogAIOCB;
256 33b1db1c MORITA Kazutaka
257 33b1db1c MORITA Kazutaka
typedef struct AIOReq {
258 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *aiocb;
259 33b1db1c MORITA Kazutaka
    unsigned int iov_offset;
260 33b1db1c MORITA Kazutaka
261 33b1db1c MORITA Kazutaka
    uint64_t oid;
262 33b1db1c MORITA Kazutaka
    uint64_t base_oid;
263 33b1db1c MORITA Kazutaka
    uint64_t offset;
264 33b1db1c MORITA Kazutaka
    unsigned int data_len;
265 33b1db1c MORITA Kazutaka
    uint8_t flags;
266 33b1db1c MORITA Kazutaka
    uint32_t id;
267 33b1db1c MORITA Kazutaka
268 c292ee6a MORITA Kazutaka
    QLIST_ENTRY(AIOReq) aio_siblings;
269 33b1db1c MORITA Kazutaka
} AIOReq;
270 33b1db1c MORITA Kazutaka
271 33b1db1c MORITA Kazutaka
enum AIOCBState {
272 33b1db1c MORITA Kazutaka
    AIOCB_WRITE_UDATA,
273 33b1db1c MORITA Kazutaka
    AIOCB_READ_UDATA,
274 47783072 Liu Yuan
    AIOCB_FLUSH_CACHE,
275 cac8f4a6 Liu Yuan
    AIOCB_DISCARD_OBJ,
276 33b1db1c MORITA Kazutaka
};
277 33b1db1c MORITA Kazutaka
278 33b1db1c MORITA Kazutaka
struct SheepdogAIOCB {
279 33b1db1c MORITA Kazutaka
    BlockDriverAIOCB common;
280 33b1db1c MORITA Kazutaka
281 33b1db1c MORITA Kazutaka
    QEMUIOVector *qiov;
282 33b1db1c MORITA Kazutaka
283 33b1db1c MORITA Kazutaka
    int64_t sector_num;
284 33b1db1c MORITA Kazutaka
    int nb_sectors;
285 33b1db1c MORITA Kazutaka
286 33b1db1c MORITA Kazutaka
    int ret;
287 33b1db1c MORITA Kazutaka
    enum AIOCBState aiocb_type;
288 33b1db1c MORITA Kazutaka
289 2df46246 MORITA Kazutaka
    Coroutine *coroutine;
290 33b1db1c MORITA Kazutaka
    void (*aio_done_func)(SheepdogAIOCB *);
291 33b1db1c MORITA Kazutaka
292 2f536801 MORITA Kazutaka
    bool canceled;
293 1d732d7d MORITA Kazutaka
    int nr_pending;
294 33b1db1c MORITA Kazutaka
};
295 33b1db1c MORITA Kazutaka
296 33b1db1c MORITA Kazutaka
typedef struct BDRVSheepdogState {
297 33b1db1c MORITA Kazutaka
    SheepdogInode inode;
298 33b1db1c MORITA Kazutaka
299 33b1db1c MORITA Kazutaka
    uint32_t min_dirty_data_idx;
300 33b1db1c MORITA Kazutaka
    uint32_t max_dirty_data_idx;
301 33b1db1c MORITA Kazutaka
302 33b1db1c MORITA Kazutaka
    char name[SD_MAX_VDI_LEN];
303 2f536801 MORITA Kazutaka
    bool is_snapshot;
304 0e7106d8 Liu Yuan
    uint32_t cache_flags;
305 cac8f4a6 Liu Yuan
    bool discard_supported;
306 33b1db1c MORITA Kazutaka
307 25af257d MORITA Kazutaka
    char *host_spec;
308 1b8bbb46 MORITA Kazutaka
    bool is_unix;
309 33b1db1c MORITA Kazutaka
    int fd;
310 33b1db1c MORITA Kazutaka
311 2df46246 MORITA Kazutaka
    CoMutex lock;
312 2df46246 MORITA Kazutaka
    Coroutine *co_send;
313 2df46246 MORITA Kazutaka
    Coroutine *co_recv;
314 2df46246 MORITA Kazutaka
315 33b1db1c MORITA Kazutaka
    uint32_t aioreq_seq_num;
316 c292ee6a MORITA Kazutaka
    QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
317 c292ee6a MORITA Kazutaka
    QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head;
318 33b1db1c MORITA Kazutaka
} BDRVSheepdogState;
319 33b1db1c MORITA Kazutaka
320 33b1db1c MORITA Kazutaka
static const char * sd_strerror(int err)
321 33b1db1c MORITA Kazutaka
{
322 33b1db1c MORITA Kazutaka
    int i;
323 33b1db1c MORITA Kazutaka
324 33b1db1c MORITA Kazutaka
    static const struct {
325 33b1db1c MORITA Kazutaka
        int err;
326 33b1db1c MORITA Kazutaka
        const char *desc;
327 33b1db1c MORITA Kazutaka
    } errors[] = {
328 33b1db1c MORITA Kazutaka
        {SD_RES_SUCCESS, "Success"},
329 33b1db1c MORITA Kazutaka
        {SD_RES_UNKNOWN, "Unknown error"},
330 33b1db1c MORITA Kazutaka
        {SD_RES_NO_OBJ, "No object found"},
331 33b1db1c MORITA Kazutaka
        {SD_RES_EIO, "I/O error"},
332 33b1db1c MORITA Kazutaka
        {SD_RES_VDI_EXIST, "VDI exists already"},
333 33b1db1c MORITA Kazutaka
        {SD_RES_INVALID_PARMS, "Invalid parameters"},
334 33b1db1c MORITA Kazutaka
        {SD_RES_SYSTEM_ERROR, "System error"},
335 33b1db1c MORITA Kazutaka
        {SD_RES_VDI_LOCKED, "VDI is already locked"},
336 33b1db1c MORITA Kazutaka
        {SD_RES_NO_VDI, "No vdi found"},
337 33b1db1c MORITA Kazutaka
        {SD_RES_NO_BASE_VDI, "No base VDI found"},
338 33b1db1c MORITA Kazutaka
        {SD_RES_VDI_READ, "Failed read the requested VDI"},
339 33b1db1c MORITA Kazutaka
        {SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
340 33b1db1c MORITA Kazutaka
        {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
341 33b1db1c MORITA Kazutaka
        {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
342 33b1db1c MORITA Kazutaka
        {SD_RES_NO_TAG, "Failed to find the requested tag"},
343 33b1db1c MORITA Kazutaka
        {SD_RES_STARTUP, "The system is still booting"},
344 33b1db1c MORITA Kazutaka
        {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
345 33b1db1c MORITA Kazutaka
        {SD_RES_SHUTDOWN, "The system is shutting down"},
346 33b1db1c MORITA Kazutaka
        {SD_RES_NO_MEM, "Out of memory on the server"},
347 33b1db1c MORITA Kazutaka
        {SD_RES_FULL_VDI, "We already have the maximum vdis"},
348 33b1db1c MORITA Kazutaka
        {SD_RES_VER_MISMATCH, "Protocol version mismatch"},
349 33b1db1c MORITA Kazutaka
        {SD_RES_NO_SPACE, "Server has no space for new objects"},
350 33b1db1c MORITA Kazutaka
        {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
351 33b1db1c MORITA Kazutaka
        {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
352 33b1db1c MORITA Kazutaka
        {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
353 fca23f0a Liu Yuan
        {SD_RES_HALT, "Sheepdog is stopped serving IO request"},
354 6a0b5490 MORITA Kazutaka
        {SD_RES_READONLY, "Object is read-only"},
355 33b1db1c MORITA Kazutaka
    };
356 33b1db1c MORITA Kazutaka
357 33b1db1c MORITA Kazutaka
    for (i = 0; i < ARRAY_SIZE(errors); ++i) {
358 33b1db1c MORITA Kazutaka
        if (errors[i].err == err) {
359 33b1db1c MORITA Kazutaka
            return errors[i].desc;
360 33b1db1c MORITA Kazutaka
        }
361 33b1db1c MORITA Kazutaka
    }
362 33b1db1c MORITA Kazutaka
363 33b1db1c MORITA Kazutaka
    return "Invalid error code";
364 33b1db1c MORITA Kazutaka
}
365 33b1db1c MORITA Kazutaka
366 33b1db1c MORITA Kazutaka
/*
367 33b1db1c MORITA Kazutaka
 * Sheepdog I/O handling:
368 33b1db1c MORITA Kazutaka
 *
369 2df46246 MORITA Kazutaka
 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
370 c292ee6a MORITA Kazutaka
 *    link the requests to the inflight_list in the
371 2df46246 MORITA Kazutaka
 *    BDRVSheepdogState.  The function exits without waiting for
372 2df46246 MORITA Kazutaka
 *    receiving the response.
373 33b1db1c MORITA Kazutaka
 *
374 2df46246 MORITA Kazutaka
 * 2. We receive the response in aio_read_response, the fd handler to
375 33b1db1c MORITA Kazutaka
 *    the sheepdog connection.  If metadata update is needed, we send
376 33b1db1c MORITA Kazutaka
 *    the write request to the vdi object in sd_write_done, the write
377 2df46246 MORITA Kazutaka
 *    completion function.  We switch back to sd_co_readv/writev after
378 2df46246 MORITA Kazutaka
 *    all the requests belonging to the AIOCB are finished.
379 33b1db1c MORITA Kazutaka
 */
380 33b1db1c MORITA Kazutaka
381 33b1db1c MORITA Kazutaka
static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
382 33b1db1c MORITA Kazutaka
                                    uint64_t oid, unsigned int data_len,
383 33b1db1c MORITA Kazutaka
                                    uint64_t offset, uint8_t flags,
384 33b1db1c MORITA Kazutaka
                                    uint64_t base_oid, unsigned int iov_offset)
385 33b1db1c MORITA Kazutaka
{
386 33b1db1c MORITA Kazutaka
    AIOReq *aio_req;
387 33b1db1c MORITA Kazutaka
388 7267c094 Anthony Liguori
    aio_req = g_malloc(sizeof(*aio_req));
389 33b1db1c MORITA Kazutaka
    aio_req->aiocb = acb;
390 33b1db1c MORITA Kazutaka
    aio_req->iov_offset = iov_offset;
391 33b1db1c MORITA Kazutaka
    aio_req->oid = oid;
392 33b1db1c MORITA Kazutaka
    aio_req->base_oid = base_oid;
393 33b1db1c MORITA Kazutaka
    aio_req->offset = offset;
394 33b1db1c MORITA Kazutaka
    aio_req->data_len = data_len;
395 33b1db1c MORITA Kazutaka
    aio_req->flags = flags;
396 33b1db1c MORITA Kazutaka
    aio_req->id = s->aioreq_seq_num++;
397 33b1db1c MORITA Kazutaka
398 1d732d7d MORITA Kazutaka
    acb->nr_pending++;
399 33b1db1c MORITA Kazutaka
    return aio_req;
400 33b1db1c MORITA Kazutaka
}
401 33b1db1c MORITA Kazutaka
402 1d732d7d MORITA Kazutaka
static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req)
403 33b1db1c MORITA Kazutaka
{
404 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb = aio_req->aiocb;
405 1d732d7d MORITA Kazutaka
406 c292ee6a MORITA Kazutaka
    QLIST_REMOVE(aio_req, aio_siblings);
407 7267c094 Anthony Liguori
    g_free(aio_req);
408 33b1db1c MORITA Kazutaka
409 1d732d7d MORITA Kazutaka
    acb->nr_pending--;
410 33b1db1c MORITA Kazutaka
}
411 33b1db1c MORITA Kazutaka
412 d8716b41 Paolo Bonzini
static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb)
413 33b1db1c MORITA Kazutaka
{
414 33b1db1c MORITA Kazutaka
    if (!acb->canceled) {
415 2df46246 MORITA Kazutaka
        qemu_coroutine_enter(acb->coroutine, NULL);
416 33b1db1c MORITA Kazutaka
    }
417 33b1db1c MORITA Kazutaka
    qemu_aio_release(acb);
418 33b1db1c MORITA Kazutaka
}
419 33b1db1c MORITA Kazutaka
420 33b1db1c MORITA Kazutaka
static void sd_aio_cancel(BlockDriverAIOCB *blockacb)
421 33b1db1c MORITA Kazutaka
{
422 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb;
423 33b1db1c MORITA Kazutaka
424 33b1db1c MORITA Kazutaka
    /*
425 33b1db1c MORITA Kazutaka
     * Sheepdog cannot cancel the requests which are already sent to
426 33b1db1c MORITA Kazutaka
     * the servers, so we just complete the request with -EIO here.
427 33b1db1c MORITA Kazutaka
     */
428 2df46246 MORITA Kazutaka
    acb->ret = -EIO;
429 2df46246 MORITA Kazutaka
    qemu_coroutine_enter(acb->coroutine, NULL);
430 2f536801 MORITA Kazutaka
    acb->canceled = true;
431 33b1db1c MORITA Kazutaka
}
432 33b1db1c MORITA Kazutaka
433 d7331bed Stefan Hajnoczi
static const AIOCBInfo sd_aiocb_info = {
434 33b1db1c MORITA Kazutaka
    .aiocb_size = sizeof(SheepdogAIOCB),
435 33b1db1c MORITA Kazutaka
    .cancel = sd_aio_cancel,
436 33b1db1c MORITA Kazutaka
};
437 33b1db1c MORITA Kazutaka
438 33b1db1c MORITA Kazutaka
static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
439 f700f8e3 Liu Yuan
                                   int64_t sector_num, int nb_sectors)
440 33b1db1c MORITA Kazutaka
{
441 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb;
442 33b1db1c MORITA Kazutaka
443 f700f8e3 Liu Yuan
    acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL);
444 33b1db1c MORITA Kazutaka
445 33b1db1c MORITA Kazutaka
    acb->qiov = qiov;
446 33b1db1c MORITA Kazutaka
447 33b1db1c MORITA Kazutaka
    acb->sector_num = sector_num;
448 33b1db1c MORITA Kazutaka
    acb->nb_sectors = nb_sectors;
449 33b1db1c MORITA Kazutaka
450 33b1db1c MORITA Kazutaka
    acb->aio_done_func = NULL;
451 2f536801 MORITA Kazutaka
    acb->canceled = false;
452 2df46246 MORITA Kazutaka
    acb->coroutine = qemu_coroutine_self();
453 33b1db1c MORITA Kazutaka
    acb->ret = 0;
454 1d732d7d MORITA Kazutaka
    acb->nr_pending = 0;
455 33b1db1c MORITA Kazutaka
    return acb;
456 33b1db1c MORITA Kazutaka
}
457 33b1db1c MORITA Kazutaka
458 25af257d MORITA Kazutaka
static int connect_to_sdog(BDRVSheepdogState *s)
459 33b1db1c MORITA Kazutaka
{
460 25af257d MORITA Kazutaka
    int fd;
461 25af257d MORITA Kazutaka
    Error *err = NULL;
462 33b1db1c MORITA Kazutaka
463 1b8bbb46 MORITA Kazutaka
    if (s->is_unix) {
464 1b8bbb46 MORITA Kazutaka
        fd = unix_connect(s->host_spec, &err);
465 1b8bbb46 MORITA Kazutaka
    } else {
466 1b8bbb46 MORITA Kazutaka
        fd = inet_connect(s->host_spec, &err);
467 1b8bbb46 MORITA Kazutaka
468 1b8bbb46 MORITA Kazutaka
        if (err == NULL) {
469 1b8bbb46 MORITA Kazutaka
            int ret = socket_set_nodelay(fd);
470 1b8bbb46 MORITA Kazutaka
            if (ret < 0) {
471 1b8bbb46 MORITA Kazutaka
                error_report("%s", strerror(errno));
472 1b8bbb46 MORITA Kazutaka
            }
473 1b8bbb46 MORITA Kazutaka
        }
474 1b8bbb46 MORITA Kazutaka
    }
475 33b1db1c MORITA Kazutaka
476 25af257d MORITA Kazutaka
    if (err != NULL) {
477 25af257d MORITA Kazutaka
        qerror_report_err(err);
478 25af257d MORITA Kazutaka
        error_free(err);
479 0d6db300 MORITA Kazutaka
    } else {
480 f9e8cacc Stefan Hajnoczi
        qemu_set_nonblock(fd);
481 33b1db1c MORITA Kazutaka
    }
482 33b1db1c MORITA Kazutaka
483 33b1db1c MORITA Kazutaka
    return fd;
484 33b1db1c MORITA Kazutaka
}
485 33b1db1c MORITA Kazutaka
486 e0d93a89 MORITA Kazutaka
static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
487 e0d93a89 MORITA Kazutaka
                                    unsigned int *wlen)
488 47622c44 Liu Yuan
{
489 47622c44 Liu Yuan
    int ret;
490 47622c44 Liu Yuan
491 47622c44 Liu Yuan
    ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
492 47622c44 Liu Yuan
    if (ret < sizeof(*hdr)) {
493 47622c44 Liu Yuan
        error_report("failed to send a req, %s", strerror(errno));
494 eb092180 Liu Yuan
        return ret;
495 47622c44 Liu Yuan
    }
496 47622c44 Liu Yuan
497 47622c44 Liu Yuan
    ret = qemu_co_send(sockfd, data, *wlen);
498 47622c44 Liu Yuan
    if (ret < *wlen) {
499 47622c44 Liu Yuan
        error_report("failed to send a req, %s", strerror(errno));
500 47622c44 Liu Yuan
    }
501 47622c44 Liu Yuan
502 47622c44 Liu Yuan
    return ret;
503 47622c44 Liu Yuan
}
504 e0d93a89 MORITA Kazutaka
505 2dfcca3b MORITA Kazutaka
static void restart_co_req(void *opaque)
506 2dfcca3b MORITA Kazutaka
{
507 2dfcca3b MORITA Kazutaka
    Coroutine *co = opaque;
508 2dfcca3b MORITA Kazutaka
509 2dfcca3b MORITA Kazutaka
    qemu_coroutine_enter(co, NULL);
510 2dfcca3b MORITA Kazutaka
}
511 2dfcca3b MORITA Kazutaka
512 cddd4ac7 MORITA Kazutaka
typedef struct SheepdogReqCo {
513 cddd4ac7 MORITA Kazutaka
    int sockfd;
514 cddd4ac7 MORITA Kazutaka
    SheepdogReq *hdr;
515 cddd4ac7 MORITA Kazutaka
    void *data;
516 cddd4ac7 MORITA Kazutaka
    unsigned int *wlen;
517 cddd4ac7 MORITA Kazutaka
    unsigned int *rlen;
518 cddd4ac7 MORITA Kazutaka
    int ret;
519 cddd4ac7 MORITA Kazutaka
    bool finished;
520 cddd4ac7 MORITA Kazutaka
} SheepdogReqCo;
521 cddd4ac7 MORITA Kazutaka
522 cddd4ac7 MORITA Kazutaka
static coroutine_fn void do_co_req(void *opaque)
523 47622c44 Liu Yuan
{
524 47622c44 Liu Yuan
    int ret;
525 2dfcca3b MORITA Kazutaka
    Coroutine *co;
526 cddd4ac7 MORITA Kazutaka
    SheepdogReqCo *srco = opaque;
527 cddd4ac7 MORITA Kazutaka
    int sockfd = srco->sockfd;
528 cddd4ac7 MORITA Kazutaka
    SheepdogReq *hdr = srco->hdr;
529 cddd4ac7 MORITA Kazutaka
    void *data = srco->data;
530 cddd4ac7 MORITA Kazutaka
    unsigned int *wlen = srco->wlen;
531 cddd4ac7 MORITA Kazutaka
    unsigned int *rlen = srco->rlen;
532 2dfcca3b MORITA Kazutaka
533 2dfcca3b MORITA Kazutaka
    co = qemu_coroutine_self();
534 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, co);
535 47622c44 Liu Yuan
536 47622c44 Liu Yuan
    ret = send_co_req(sockfd, hdr, data, wlen);
537 47622c44 Liu Yuan
    if (ret < 0) {
538 47622c44 Liu Yuan
        goto out;
539 47622c44 Liu Yuan
    }
540 47622c44 Liu Yuan
541 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, co);
542 2dfcca3b MORITA Kazutaka
543 47622c44 Liu Yuan
    ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
544 47622c44 Liu Yuan
    if (ret < sizeof(*hdr)) {
545 47622c44 Liu Yuan
        error_report("failed to get a rsp, %s", strerror(errno));
546 cb595887 MORITA Kazutaka
        ret = -errno;
547 47622c44 Liu Yuan
        goto out;
548 47622c44 Liu Yuan
    }
549 47622c44 Liu Yuan
550 47622c44 Liu Yuan
    if (*rlen > hdr->data_length) {
551 47622c44 Liu Yuan
        *rlen = hdr->data_length;
552 47622c44 Liu Yuan
    }
553 47622c44 Liu Yuan
554 47622c44 Liu Yuan
    if (*rlen) {
555 47622c44 Liu Yuan
        ret = qemu_co_recv(sockfd, data, *rlen);
556 47622c44 Liu Yuan
        if (ret < *rlen) {
557 47622c44 Liu Yuan
            error_report("failed to get the data, %s", strerror(errno));
558 cb595887 MORITA Kazutaka
            ret = -errno;
559 47622c44 Liu Yuan
            goto out;
560 47622c44 Liu Yuan
        }
561 47622c44 Liu Yuan
    }
562 47622c44 Liu Yuan
    ret = 0;
563 47622c44 Liu Yuan
out:
564 ed9ba724 MORITA Kazutaka
    /* there is at most one request for this sockfd, so it is safe to
565 ed9ba724 MORITA Kazutaka
     * set each handler to NULL. */
566 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL);
567 cddd4ac7 MORITA Kazutaka
568 cddd4ac7 MORITA Kazutaka
    srco->ret = ret;
569 cddd4ac7 MORITA Kazutaka
    srco->finished = true;
570 cddd4ac7 MORITA Kazutaka
}
571 cddd4ac7 MORITA Kazutaka
572 cddd4ac7 MORITA Kazutaka
static int do_req(int sockfd, SheepdogReq *hdr, void *data,
573 cddd4ac7 MORITA Kazutaka
                  unsigned int *wlen, unsigned int *rlen)
574 cddd4ac7 MORITA Kazutaka
{
575 cddd4ac7 MORITA Kazutaka
    Coroutine *co;
576 cddd4ac7 MORITA Kazutaka
    SheepdogReqCo srco = {
577 cddd4ac7 MORITA Kazutaka
        .sockfd = sockfd,
578 cddd4ac7 MORITA Kazutaka
        .hdr = hdr,
579 cddd4ac7 MORITA Kazutaka
        .data = data,
580 cddd4ac7 MORITA Kazutaka
        .wlen = wlen,
581 cddd4ac7 MORITA Kazutaka
        .rlen = rlen,
582 cddd4ac7 MORITA Kazutaka
        .ret = 0,
583 cddd4ac7 MORITA Kazutaka
        .finished = false,
584 cddd4ac7 MORITA Kazutaka
    };
585 cddd4ac7 MORITA Kazutaka
586 cddd4ac7 MORITA Kazutaka
    if (qemu_in_coroutine()) {
587 cddd4ac7 MORITA Kazutaka
        do_co_req(&srco);
588 cddd4ac7 MORITA Kazutaka
    } else {
589 cddd4ac7 MORITA Kazutaka
        co = qemu_coroutine_create(do_co_req);
590 cddd4ac7 MORITA Kazutaka
        qemu_coroutine_enter(co, &srco);
591 cddd4ac7 MORITA Kazutaka
        while (!srco.finished) {
592 cddd4ac7 MORITA Kazutaka
            qemu_aio_wait();
593 cddd4ac7 MORITA Kazutaka
        }
594 cddd4ac7 MORITA Kazutaka
    }
595 cddd4ac7 MORITA Kazutaka
596 cddd4ac7 MORITA Kazutaka
    return srco.ret;
597 47622c44 Liu Yuan
}
598 47622c44 Liu Yuan
599 d8716b41 Paolo Bonzini
static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
600 2f536801 MORITA Kazutaka
                           struct iovec *iov, int niov, bool create,
601 33b1db1c MORITA Kazutaka
                           enum AIOCBState aiocb_type);
602 13c31de2 MORITA Kazutaka
static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);
603 33b1db1c MORITA Kazutaka
604 7dc1cde0 MORITA Kazutaka
605 7dc1cde0 MORITA Kazutaka
static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid)
606 7dc1cde0 MORITA Kazutaka
{
607 7dc1cde0 MORITA Kazutaka
    AIOReq *aio_req;
608 7dc1cde0 MORITA Kazutaka
609 7dc1cde0 MORITA Kazutaka
    QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) {
610 7dc1cde0 MORITA Kazutaka
        if (aio_req->oid == oid) {
611 7dc1cde0 MORITA Kazutaka
            return aio_req;
612 7dc1cde0 MORITA Kazutaka
        }
613 7dc1cde0 MORITA Kazutaka
    }
614 7dc1cde0 MORITA Kazutaka
615 7dc1cde0 MORITA Kazutaka
    return NULL;
616 7dc1cde0 MORITA Kazutaka
}
617 7dc1cde0 MORITA Kazutaka
618 33b1db1c MORITA Kazutaka
/*
619 33b1db1c MORITA Kazutaka
 * This function searchs pending requests to the object `oid', and
620 33b1db1c MORITA Kazutaka
 * sends them.
621 33b1db1c MORITA Kazutaka
 */
622 c292ee6a MORITA Kazutaka
static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid)
623 33b1db1c MORITA Kazutaka
{
624 7dc1cde0 MORITA Kazutaka
    AIOReq *aio_req;
625 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb;
626 33b1db1c MORITA Kazutaka
    int ret;
627 33b1db1c MORITA Kazutaka
628 7dc1cde0 MORITA Kazutaka
    while ((aio_req = find_pending_req(s, oid)) != NULL) {
629 33b1db1c MORITA Kazutaka
        acb = aio_req->aiocb;
630 c292ee6a MORITA Kazutaka
        /* move aio_req from pending list to inflight one */
631 c292ee6a MORITA Kazutaka
        QLIST_REMOVE(aio_req, aio_siblings);
632 c292ee6a MORITA Kazutaka
        QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
633 33b1db1c MORITA Kazutaka
        ret = add_aio_request(s, aio_req, acb->qiov->iov,
634 2f536801 MORITA Kazutaka
                              acb->qiov->niov, false, acb->aiocb_type);
635 33b1db1c MORITA Kazutaka
        if (ret < 0) {
636 6daf194d Markus Armbruster
            error_report("add_aio_request is failed");
637 33b1db1c MORITA Kazutaka
            free_aio_req(s, aio_req);
638 1d732d7d MORITA Kazutaka
            if (!acb->nr_pending) {
639 33b1db1c MORITA Kazutaka
                sd_finish_aiocb(acb);
640 33b1db1c MORITA Kazutaka
            }
641 33b1db1c MORITA Kazutaka
        }
642 33b1db1c MORITA Kazutaka
    }
643 33b1db1c MORITA Kazutaka
}
644 33b1db1c MORITA Kazutaka
645 33b1db1c MORITA Kazutaka
/*
646 33b1db1c MORITA Kazutaka
 * Receive responses of the I/O requests.
647 33b1db1c MORITA Kazutaka
 *
648 33b1db1c MORITA Kazutaka
 * This function is registered as a fd handler, and called from the
649 33b1db1c MORITA Kazutaka
 * main loop when s->fd is ready for reading responses.
650 33b1db1c MORITA Kazutaka
 */
651 d8716b41 Paolo Bonzini
static void coroutine_fn aio_read_response(void *opaque)
652 33b1db1c MORITA Kazutaka
{
653 33b1db1c MORITA Kazutaka
    SheepdogObjRsp rsp;
654 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = opaque;
655 33b1db1c MORITA Kazutaka
    int fd = s->fd;
656 33b1db1c MORITA Kazutaka
    int ret;
657 33b1db1c MORITA Kazutaka
    AIOReq *aio_req = NULL;
658 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb;
659 cac8f4a6 Liu Yuan
    uint64_t idx;
660 33b1db1c MORITA Kazutaka
661 c292ee6a MORITA Kazutaka
    if (QLIST_EMPTY(&s->inflight_aio_head)) {
662 2df46246 MORITA Kazutaka
        goto out;
663 33b1db1c MORITA Kazutaka
    }
664 33b1db1c MORITA Kazutaka
665 33b1db1c MORITA Kazutaka
    /* read a header */
666 8c5135f9 Paolo Bonzini
    ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
667 8c5135f9 Paolo Bonzini
    if (ret < 0) {
668 6daf194d Markus Armbruster
        error_report("failed to get the header, %s", strerror(errno));
669 2df46246 MORITA Kazutaka
        goto out;
670 33b1db1c MORITA Kazutaka
    }
671 33b1db1c MORITA Kazutaka
672 c292ee6a MORITA Kazutaka
    /* find the right aio_req from the inflight aio list */
673 c292ee6a MORITA Kazutaka
    QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
674 33b1db1c MORITA Kazutaka
        if (aio_req->id == rsp.id) {
675 33b1db1c MORITA Kazutaka
            break;
676 33b1db1c MORITA Kazutaka
        }
677 33b1db1c MORITA Kazutaka
    }
678 33b1db1c MORITA Kazutaka
    if (!aio_req) {
679 6daf194d Markus Armbruster
        error_report("cannot find aio_req %x", rsp.id);
680 2df46246 MORITA Kazutaka
        goto out;
681 33b1db1c MORITA Kazutaka
    }
682 33b1db1c MORITA Kazutaka
683 33b1db1c MORITA Kazutaka
    acb = aio_req->aiocb;
684 33b1db1c MORITA Kazutaka
685 33b1db1c MORITA Kazutaka
    switch (acb->aiocb_type) {
686 33b1db1c MORITA Kazutaka
    case AIOCB_WRITE_UDATA:
687 6d1acda8 MORITA Kazutaka
        /* this coroutine context is no longer suitable for co_recv
688 6d1acda8 MORITA Kazutaka
         * because we may send data to update vdi objects */
689 6d1acda8 MORITA Kazutaka
        s->co_recv = NULL;
690 33b1db1c MORITA Kazutaka
        if (!is_data_obj(aio_req->oid)) {
691 33b1db1c MORITA Kazutaka
            break;
692 33b1db1c MORITA Kazutaka
        }
693 33b1db1c MORITA Kazutaka
        idx = data_oid_to_idx(aio_req->oid);
694 33b1db1c MORITA Kazutaka
695 33b1db1c MORITA Kazutaka
        if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) {
696 33b1db1c MORITA Kazutaka
            /*
697 33b1db1c MORITA Kazutaka
             * If the object is newly created one, we need to update
698 33b1db1c MORITA Kazutaka
             * the vdi object (metadata object).  min_dirty_data_idx
699 33b1db1c MORITA Kazutaka
             * and max_dirty_data_idx are changed to include updated
700 33b1db1c MORITA Kazutaka
             * index between them.
701 33b1db1c MORITA Kazutaka
             */
702 bd751f22 Liu Yuan
            if (rsp.result == SD_RES_SUCCESS) {
703 bd751f22 Liu Yuan
                s->inode.data_vdi_id[idx] = s->inode.vdi_id;
704 bd751f22 Liu Yuan
                s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx);
705 bd751f22 Liu Yuan
                s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx);
706 bd751f22 Liu Yuan
            }
707 33b1db1c MORITA Kazutaka
            /*
708 33b1db1c MORITA Kazutaka
             * Some requests may be blocked because simultaneous
709 33b1db1c MORITA Kazutaka
             * create requests are not allowed, so we search the
710 33b1db1c MORITA Kazutaka
             * pending requests here.
711 33b1db1c MORITA Kazutaka
             */
712 d6b1ef89 Liu Yuan
            send_pending_req(s, aio_req->oid);
713 33b1db1c MORITA Kazutaka
        }
714 33b1db1c MORITA Kazutaka
        break;
715 33b1db1c MORITA Kazutaka
    case AIOCB_READ_UDATA:
716 2fc8ae1d Michael Tokarev
        ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
717 2fc8ae1d Michael Tokarev
                            aio_req->iov_offset, rsp.data_length);
718 8c5135f9 Paolo Bonzini
        if (ret < 0) {
719 6daf194d Markus Armbruster
            error_report("failed to get the data, %s", strerror(errno));
720 2df46246 MORITA Kazutaka
            goto out;
721 33b1db1c MORITA Kazutaka
        }
722 33b1db1c MORITA Kazutaka
        break;
723 47783072 Liu Yuan
    case AIOCB_FLUSH_CACHE:
724 47783072 Liu Yuan
        if (rsp.result == SD_RES_INVALID_PARMS) {
725 2440a2c3 Peter Maydell
            DPRINTF("disable cache since the server doesn't support it\n");
726 47783072 Liu Yuan
            s->cache_flags = SD_FLAG_CMD_DIRECT;
727 47783072 Liu Yuan
            rsp.result = SD_RES_SUCCESS;
728 47783072 Liu Yuan
        }
729 47783072 Liu Yuan
        break;
730 cac8f4a6 Liu Yuan
    case AIOCB_DISCARD_OBJ:
731 cac8f4a6 Liu Yuan
        switch (rsp.result) {
732 cac8f4a6 Liu Yuan
        case SD_RES_INVALID_PARMS:
733 cac8f4a6 Liu Yuan
            error_report("sheep(%s) doesn't support discard command",
734 cac8f4a6 Liu Yuan
                         s->host_spec);
735 cac8f4a6 Liu Yuan
            rsp.result = SD_RES_SUCCESS;
736 cac8f4a6 Liu Yuan
            s->discard_supported = false;
737 cac8f4a6 Liu Yuan
            break;
738 cac8f4a6 Liu Yuan
        case SD_RES_SUCCESS:
739 cac8f4a6 Liu Yuan
            idx = data_oid_to_idx(aio_req->oid);
740 cac8f4a6 Liu Yuan
            s->inode.data_vdi_id[idx] = 0;
741 cac8f4a6 Liu Yuan
            break;
742 cac8f4a6 Liu Yuan
        default:
743 cac8f4a6 Liu Yuan
            break;
744 cac8f4a6 Liu Yuan
        }
745 33b1db1c MORITA Kazutaka
    }
746 33b1db1c MORITA Kazutaka
747 13c31de2 MORITA Kazutaka
    switch (rsp.result) {
748 13c31de2 MORITA Kazutaka
    case SD_RES_SUCCESS:
749 13c31de2 MORITA Kazutaka
        break;
750 13c31de2 MORITA Kazutaka
    case SD_RES_READONLY:
751 13c31de2 MORITA Kazutaka
        ret = resend_aioreq(s, aio_req);
752 13c31de2 MORITA Kazutaka
        if (ret == SD_RES_SUCCESS) {
753 13c31de2 MORITA Kazutaka
            goto out;
754 13c31de2 MORITA Kazutaka
        }
755 13c31de2 MORITA Kazutaka
        /* fall through */
756 13c31de2 MORITA Kazutaka
    default:
757 33b1db1c MORITA Kazutaka
        acb->ret = -EIO;
758 6daf194d Markus Armbruster
        error_report("%s", sd_strerror(rsp.result));
759 13c31de2 MORITA Kazutaka
        break;
760 33b1db1c MORITA Kazutaka
    }
761 33b1db1c MORITA Kazutaka
762 1d732d7d MORITA Kazutaka
    free_aio_req(s, aio_req);
763 1d732d7d MORITA Kazutaka
    if (!acb->nr_pending) {
764 33b1db1c MORITA Kazutaka
        /*
765 33b1db1c MORITA Kazutaka
         * We've finished all requests which belong to the AIOCB, so
766 2df46246 MORITA Kazutaka
         * we can switch back to sd_co_readv/writev now.
767 33b1db1c MORITA Kazutaka
         */
768 33b1db1c MORITA Kazutaka
        acb->aio_done_func(acb);
769 33b1db1c MORITA Kazutaka
    }
770 2df46246 MORITA Kazutaka
out:
771 2df46246 MORITA Kazutaka
    s->co_recv = NULL;
772 2df46246 MORITA Kazutaka
}
773 2df46246 MORITA Kazutaka
774 2df46246 MORITA Kazutaka
static void co_read_response(void *opaque)
775 2df46246 MORITA Kazutaka
{
776 2df46246 MORITA Kazutaka
    BDRVSheepdogState *s = opaque;
777 2df46246 MORITA Kazutaka
778 2df46246 MORITA Kazutaka
    if (!s->co_recv) {
779 2df46246 MORITA Kazutaka
        s->co_recv = qemu_coroutine_create(aio_read_response);
780 2df46246 MORITA Kazutaka
    }
781 2df46246 MORITA Kazutaka
782 2df46246 MORITA Kazutaka
    qemu_coroutine_enter(s->co_recv, opaque);
783 2df46246 MORITA Kazutaka
}
784 2df46246 MORITA Kazutaka
785 2df46246 MORITA Kazutaka
static void co_write_request(void *opaque)
786 2df46246 MORITA Kazutaka
{
787 2df46246 MORITA Kazutaka
    BDRVSheepdogState *s = opaque;
788 2df46246 MORITA Kazutaka
789 2df46246 MORITA Kazutaka
    qemu_coroutine_enter(s->co_send, NULL);
790 33b1db1c MORITA Kazutaka
}
791 33b1db1c MORITA Kazutaka
792 33b1db1c MORITA Kazutaka
/*
793 33b1db1c MORITA Kazutaka
 * Return a socket discriptor to read/write objects.
794 33b1db1c MORITA Kazutaka
 *
795 33b1db1c MORITA Kazutaka
 * We cannot use this discriptor for other operations because
796 33b1db1c MORITA Kazutaka
 * the block driver may be on waiting response from the server.
797 33b1db1c MORITA Kazutaka
 */
798 33b1db1c MORITA Kazutaka
static int get_sheep_fd(BDRVSheepdogState *s)
799 33b1db1c MORITA Kazutaka
{
800 1b8bbb46 MORITA Kazutaka
    int fd;
801 33b1db1c MORITA Kazutaka
802 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
803 33b1db1c MORITA Kazutaka
    if (fd < 0) {
804 cb595887 MORITA Kazutaka
        return fd;
805 33b1db1c MORITA Kazutaka
    }
806 33b1db1c MORITA Kazutaka
807 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(fd, co_read_response, NULL, s);
808 33b1db1c MORITA Kazutaka
    return fd;
809 33b1db1c MORITA Kazutaka
}
810 33b1db1c MORITA Kazutaka
811 5d6768e3 MORITA Kazutaka
static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
812 5d6768e3 MORITA Kazutaka
                        char *vdi, uint32_t *snapid, char *tag)
813 5d6768e3 MORITA Kazutaka
{
814 5d6768e3 MORITA Kazutaka
    URI *uri;
815 5d6768e3 MORITA Kazutaka
    QueryParams *qp = NULL;
816 5d6768e3 MORITA Kazutaka
    int ret = 0;
817 5d6768e3 MORITA Kazutaka
818 5d6768e3 MORITA Kazutaka
    uri = uri_parse(filename);
819 5d6768e3 MORITA Kazutaka
    if (!uri) {
820 5d6768e3 MORITA Kazutaka
        return -EINVAL;
821 5d6768e3 MORITA Kazutaka
    }
822 5d6768e3 MORITA Kazutaka
823 1b8bbb46 MORITA Kazutaka
    /* transport */
824 1b8bbb46 MORITA Kazutaka
    if (!strcmp(uri->scheme, "sheepdog")) {
825 1b8bbb46 MORITA Kazutaka
        s->is_unix = false;
826 1b8bbb46 MORITA Kazutaka
    } else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
827 1b8bbb46 MORITA Kazutaka
        s->is_unix = false;
828 1b8bbb46 MORITA Kazutaka
    } else if (!strcmp(uri->scheme, "sheepdog+unix")) {
829 1b8bbb46 MORITA Kazutaka
        s->is_unix = true;
830 1b8bbb46 MORITA Kazutaka
    } else {
831 1b8bbb46 MORITA Kazutaka
        ret = -EINVAL;
832 1b8bbb46 MORITA Kazutaka
        goto out;
833 1b8bbb46 MORITA Kazutaka
    }
834 1b8bbb46 MORITA Kazutaka
835 5d6768e3 MORITA Kazutaka
    if (uri->path == NULL || !strcmp(uri->path, "/")) {
836 5d6768e3 MORITA Kazutaka
        ret = -EINVAL;
837 5d6768e3 MORITA Kazutaka
        goto out;
838 5d6768e3 MORITA Kazutaka
    }
839 5d6768e3 MORITA Kazutaka
    pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
840 5d6768e3 MORITA Kazutaka
841 1b8bbb46 MORITA Kazutaka
    qp = query_params_parse(uri->query);
842 1b8bbb46 MORITA Kazutaka
    if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
843 1b8bbb46 MORITA Kazutaka
        ret = -EINVAL;
844 1b8bbb46 MORITA Kazutaka
        goto out;
845 1b8bbb46 MORITA Kazutaka
    }
846 1b8bbb46 MORITA Kazutaka
847 1b8bbb46 MORITA Kazutaka
    if (s->is_unix) {
848 1b8bbb46 MORITA Kazutaka
        /* sheepdog+unix:///vdiname?socket=path */
849 1b8bbb46 MORITA Kazutaka
        if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
850 1b8bbb46 MORITA Kazutaka
            ret = -EINVAL;
851 1b8bbb46 MORITA Kazutaka
            goto out;
852 1b8bbb46 MORITA Kazutaka
        }
853 1b8bbb46 MORITA Kazutaka
        s->host_spec = g_strdup(qp->p[0].value);
854 1b8bbb46 MORITA Kazutaka
    } else {
855 1b8bbb46 MORITA Kazutaka
        /* sheepdog[+tcp]://[host:port]/vdiname */
856 1b8bbb46 MORITA Kazutaka
        s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
857 1b8bbb46 MORITA Kazutaka
                                       uri->port ?: SD_DEFAULT_PORT);
858 1b8bbb46 MORITA Kazutaka
    }
859 5d6768e3 MORITA Kazutaka
860 5d6768e3 MORITA Kazutaka
    /* snapshot tag */
861 5d6768e3 MORITA Kazutaka
    if (uri->fragment) {
862 5d6768e3 MORITA Kazutaka
        *snapid = strtoul(uri->fragment, NULL, 10);
863 5d6768e3 MORITA Kazutaka
        if (*snapid == 0) {
864 5d6768e3 MORITA Kazutaka
            pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
865 5d6768e3 MORITA Kazutaka
        }
866 5d6768e3 MORITA Kazutaka
    } else {
867 5d6768e3 MORITA Kazutaka
        *snapid = CURRENT_VDI_ID; /* search current vdi */
868 5d6768e3 MORITA Kazutaka
    }
869 5d6768e3 MORITA Kazutaka
870 5d6768e3 MORITA Kazutaka
out:
871 5d6768e3 MORITA Kazutaka
    if (qp) {
872 5d6768e3 MORITA Kazutaka
        query_params_free(qp);
873 5d6768e3 MORITA Kazutaka
    }
874 5d6768e3 MORITA Kazutaka
    uri_free(uri);
875 5d6768e3 MORITA Kazutaka
    return ret;
876 5d6768e3 MORITA Kazutaka
}
877 5d6768e3 MORITA Kazutaka
878 33b1db1c MORITA Kazutaka
/*
879 5d6768e3 MORITA Kazutaka
 * Parse a filename (old syntax)
880 33b1db1c MORITA Kazutaka
 *
881 33b1db1c MORITA Kazutaka
 * filename must be one of the following formats:
882 33b1db1c MORITA Kazutaka
 *   1. [vdiname]
883 33b1db1c MORITA Kazutaka
 *   2. [vdiname]:[snapid]
884 33b1db1c MORITA Kazutaka
 *   3. [vdiname]:[tag]
885 33b1db1c MORITA Kazutaka
 *   4. [hostname]:[port]:[vdiname]
886 33b1db1c MORITA Kazutaka
 *   5. [hostname]:[port]:[vdiname]:[snapid]
887 33b1db1c MORITA Kazutaka
 *   6. [hostname]:[port]:[vdiname]:[tag]
888 33b1db1c MORITA Kazutaka
 *
889 33b1db1c MORITA Kazutaka
 * You can boot from the snapshot images by specifying `snapid` or
890 33b1db1c MORITA Kazutaka
 * `tag'.
891 33b1db1c MORITA Kazutaka
 *
892 33b1db1c MORITA Kazutaka
 * You can run VMs outside the Sheepdog cluster by specifying
893 33b1db1c MORITA Kazutaka
 * `hostname' and `port' (experimental).
894 33b1db1c MORITA Kazutaka
 */
895 33b1db1c MORITA Kazutaka
static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
896 33b1db1c MORITA Kazutaka
                         char *vdi, uint32_t *snapid, char *tag)
897 33b1db1c MORITA Kazutaka
{
898 5d6768e3 MORITA Kazutaka
    char *p, *q, *uri;
899 5d6768e3 MORITA Kazutaka
    const char *host_spec, *vdi_spec;
900 5d6768e3 MORITA Kazutaka
    int nr_sep, ret;
901 33b1db1c MORITA Kazutaka
902 5d6768e3 MORITA Kazutaka
    strstart(filename, "sheepdog:", (const char **)&filename);
903 7267c094 Anthony Liguori
    p = q = g_strdup(filename);
904 33b1db1c MORITA Kazutaka
905 33b1db1c MORITA Kazutaka
    /* count the number of separators */
906 33b1db1c MORITA Kazutaka
    nr_sep = 0;
907 33b1db1c MORITA Kazutaka
    while (*p) {
908 33b1db1c MORITA Kazutaka
        if (*p == ':') {
909 33b1db1c MORITA Kazutaka
            nr_sep++;
910 33b1db1c MORITA Kazutaka
        }
911 33b1db1c MORITA Kazutaka
        p++;
912 33b1db1c MORITA Kazutaka
    }
913 33b1db1c MORITA Kazutaka
    p = q;
914 33b1db1c MORITA Kazutaka
915 5d6768e3 MORITA Kazutaka
    /* use the first two tokens as host_spec. */
916 33b1db1c MORITA Kazutaka
    if (nr_sep >= 2) {
917 5d6768e3 MORITA Kazutaka
        host_spec = p;
918 33b1db1c MORITA Kazutaka
        p = strchr(p, ':');
919 5d6768e3 MORITA Kazutaka
        p++;
920 33b1db1c MORITA Kazutaka
        p = strchr(p, ':');
921 33b1db1c MORITA Kazutaka
        *p++ = '\0';
922 33b1db1c MORITA Kazutaka
    } else {
923 5d6768e3 MORITA Kazutaka
        host_spec = "";
924 33b1db1c MORITA Kazutaka
    }
925 33b1db1c MORITA Kazutaka
926 5d6768e3 MORITA Kazutaka
    vdi_spec = p;
927 33b1db1c MORITA Kazutaka
928 5d6768e3 MORITA Kazutaka
    p = strchr(vdi_spec, ':');
929 33b1db1c MORITA Kazutaka
    if (p) {
930 5d6768e3 MORITA Kazutaka
        *p++ = '#';
931 33b1db1c MORITA Kazutaka
    }
932 33b1db1c MORITA Kazutaka
933 5d6768e3 MORITA Kazutaka
    uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
934 33b1db1c MORITA Kazutaka
935 5d6768e3 MORITA Kazutaka
    ret = sd_parse_uri(s, uri, vdi, snapid, tag);
936 5d6768e3 MORITA Kazutaka
937 5d6768e3 MORITA Kazutaka
    g_free(q);
938 5d6768e3 MORITA Kazutaka
    g_free(uri);
939 5d6768e3 MORITA Kazutaka
940 5d6768e3 MORITA Kazutaka
    return ret;
941 33b1db1c MORITA Kazutaka
}
942 33b1db1c MORITA Kazutaka
943 982dcbf4 MORITA Kazutaka
static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
944 982dcbf4 MORITA Kazutaka
                         uint32_t snapid, const char *tag, uint32_t *vid,
945 982dcbf4 MORITA Kazutaka
                         bool lock)
946 33b1db1c MORITA Kazutaka
{
947 33b1db1c MORITA Kazutaka
    int ret, fd;
948 33b1db1c MORITA Kazutaka
    SheepdogVdiReq hdr;
949 33b1db1c MORITA Kazutaka
    SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
950 33b1db1c MORITA Kazutaka
    unsigned int wlen, rlen = 0;
951 33b1db1c MORITA Kazutaka
    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
952 33b1db1c MORITA Kazutaka
953 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
954 33b1db1c MORITA Kazutaka
    if (fd < 0) {
955 cb595887 MORITA Kazutaka
        return fd;
956 33b1db1c MORITA Kazutaka
    }
957 33b1db1c MORITA Kazutaka
958 3178e275 Jim Meyering
    /* This pair of strncpy calls ensures that the buffer is zero-filled,
959 3178e275 Jim Meyering
     * which is desirable since we'll soon be sending those bytes, and
960 3178e275 Jim Meyering
     * don't want the send_req to read uninitialized data.
961 3178e275 Jim Meyering
     */
962 33b1db1c MORITA Kazutaka
    strncpy(buf, filename, SD_MAX_VDI_LEN);
963 33b1db1c MORITA Kazutaka
    strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
964 33b1db1c MORITA Kazutaka
965 33b1db1c MORITA Kazutaka
    memset(&hdr, 0, sizeof(hdr));
966 982dcbf4 MORITA Kazutaka
    if (lock) {
967 33b1db1c MORITA Kazutaka
        hdr.opcode = SD_OP_LOCK_VDI;
968 982dcbf4 MORITA Kazutaka
    } else {
969 982dcbf4 MORITA Kazutaka
        hdr.opcode = SD_OP_GET_VDI_INFO;
970 33b1db1c MORITA Kazutaka
    }
971 33b1db1c MORITA Kazutaka
    wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
972 33b1db1c MORITA Kazutaka
    hdr.proto_ver = SD_PROTO_VER;
973 33b1db1c MORITA Kazutaka
    hdr.data_length = wlen;
974 33b1db1c MORITA Kazutaka
    hdr.snapid = snapid;
975 33b1db1c MORITA Kazutaka
    hdr.flags = SD_FLAG_CMD_WRITE;
976 33b1db1c MORITA Kazutaka
977 33b1db1c MORITA Kazutaka
    ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
978 33b1db1c MORITA Kazutaka
    if (ret) {
979 33b1db1c MORITA Kazutaka
        goto out;
980 33b1db1c MORITA Kazutaka
    }
981 33b1db1c MORITA Kazutaka
982 33b1db1c MORITA Kazutaka
    if (rsp->result != SD_RES_SUCCESS) {
983 6daf194d Markus Armbruster
        error_report("cannot get vdi info, %s, %s %d %s",
984 33b1db1c MORITA Kazutaka
                     sd_strerror(rsp->result), filename, snapid, tag);
985 cb595887 MORITA Kazutaka
        if (rsp->result == SD_RES_NO_VDI) {
986 cb595887 MORITA Kazutaka
            ret = -ENOENT;
987 cb595887 MORITA Kazutaka
        } else {
988 cb595887 MORITA Kazutaka
            ret = -EIO;
989 cb595887 MORITA Kazutaka
        }
990 33b1db1c MORITA Kazutaka
        goto out;
991 33b1db1c MORITA Kazutaka
    }
992 33b1db1c MORITA Kazutaka
    *vid = rsp->vdi_id;
993 33b1db1c MORITA Kazutaka
994 33b1db1c MORITA Kazutaka
    ret = 0;
995 33b1db1c MORITA Kazutaka
out:
996 33b1db1c MORITA Kazutaka
    closesocket(fd);
997 33b1db1c MORITA Kazutaka
    return ret;
998 33b1db1c MORITA Kazutaka
}
999 33b1db1c MORITA Kazutaka
1000 d8716b41 Paolo Bonzini
static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
1001 2f536801 MORITA Kazutaka
                           struct iovec *iov, int niov, bool create,
1002 33b1db1c MORITA Kazutaka
                           enum AIOCBState aiocb_type)
1003 33b1db1c MORITA Kazutaka
{
1004 33b1db1c MORITA Kazutaka
    int nr_copies = s->inode.nr_copies;
1005 33b1db1c MORITA Kazutaka
    SheepdogObjReq hdr;
1006 47783072 Liu Yuan
    unsigned int wlen = 0;
1007 33b1db1c MORITA Kazutaka
    int ret;
1008 33b1db1c MORITA Kazutaka
    uint64_t oid = aio_req->oid;
1009 33b1db1c MORITA Kazutaka
    unsigned int datalen = aio_req->data_len;
1010 33b1db1c MORITA Kazutaka
    uint64_t offset = aio_req->offset;
1011 33b1db1c MORITA Kazutaka
    uint8_t flags = aio_req->flags;
1012 33b1db1c MORITA Kazutaka
    uint64_t old_oid = aio_req->base_oid;
1013 33b1db1c MORITA Kazutaka
1014 33b1db1c MORITA Kazutaka
    if (!nr_copies) {
1015 6daf194d Markus Armbruster
        error_report("bug");
1016 33b1db1c MORITA Kazutaka
    }
1017 33b1db1c MORITA Kazutaka
1018 33b1db1c MORITA Kazutaka
    memset(&hdr, 0, sizeof(hdr));
1019 33b1db1c MORITA Kazutaka
1020 47783072 Liu Yuan
    switch (aiocb_type) {
1021 47783072 Liu Yuan
    case AIOCB_FLUSH_CACHE:
1022 47783072 Liu Yuan
        hdr.opcode = SD_OP_FLUSH_VDI;
1023 47783072 Liu Yuan
        break;
1024 47783072 Liu Yuan
    case AIOCB_READ_UDATA:
1025 33b1db1c MORITA Kazutaka
        hdr.opcode = SD_OP_READ_OBJ;
1026 33b1db1c MORITA Kazutaka
        hdr.flags = flags;
1027 47783072 Liu Yuan
        break;
1028 47783072 Liu Yuan
    case AIOCB_WRITE_UDATA:
1029 47783072 Liu Yuan
        if (create) {
1030 47783072 Liu Yuan
            hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1031 47783072 Liu Yuan
        } else {
1032 47783072 Liu Yuan
            hdr.opcode = SD_OP_WRITE_OBJ;
1033 47783072 Liu Yuan
        }
1034 33b1db1c MORITA Kazutaka
        wlen = datalen;
1035 33b1db1c MORITA Kazutaka
        hdr.flags = SD_FLAG_CMD_WRITE | flags;
1036 47783072 Liu Yuan
        break;
1037 cac8f4a6 Liu Yuan
    case AIOCB_DISCARD_OBJ:
1038 cac8f4a6 Liu Yuan
        hdr.opcode = SD_OP_DISCARD_OBJ;
1039 cac8f4a6 Liu Yuan
        break;
1040 33b1db1c MORITA Kazutaka
    }
1041 33b1db1c MORITA Kazutaka
1042 0e7106d8 Liu Yuan
    if (s->cache_flags) {
1043 0e7106d8 Liu Yuan
        hdr.flags |= s->cache_flags;
1044 47622c44 Liu Yuan
    }
1045 47622c44 Liu Yuan
1046 33b1db1c MORITA Kazutaka
    hdr.oid = oid;
1047 33b1db1c MORITA Kazutaka
    hdr.cow_oid = old_oid;
1048 33b1db1c MORITA Kazutaka
    hdr.copies = s->inode.nr_copies;
1049 33b1db1c MORITA Kazutaka
1050 33b1db1c MORITA Kazutaka
    hdr.data_length = datalen;
1051 33b1db1c MORITA Kazutaka
    hdr.offset = offset;
1052 33b1db1c MORITA Kazutaka
1053 33b1db1c MORITA Kazutaka
    hdr.id = aio_req->id;
1054 33b1db1c MORITA Kazutaka
1055 2df46246 MORITA Kazutaka
    qemu_co_mutex_lock(&s->lock);
1056 2df46246 MORITA Kazutaka
    s->co_send = qemu_coroutine_self();
1057 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(s->fd, co_read_response, co_write_request, s);
1058 128aa589 Paolo Bonzini
    socket_set_cork(s->fd, 1);
1059 33b1db1c MORITA Kazutaka
1060 33b1db1c MORITA Kazutaka
    /* send a header */
1061 8c5135f9 Paolo Bonzini
    ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
1062 8c5135f9 Paolo Bonzini
    if (ret < 0) {
1063 c3fecea5 Dong Xu Wang
        qemu_co_mutex_unlock(&s->lock);
1064 6daf194d Markus Armbruster
        error_report("failed to send a req, %s", strerror(errno));
1065 cb595887 MORITA Kazutaka
        return -errno;
1066 33b1db1c MORITA Kazutaka
    }
1067 33b1db1c MORITA Kazutaka
1068 33b1db1c MORITA Kazutaka
    if (wlen) {
1069 2fc8ae1d Michael Tokarev
        ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
1070 8c5135f9 Paolo Bonzini
        if (ret < 0) {
1071 c3fecea5 Dong Xu Wang
            qemu_co_mutex_unlock(&s->lock);
1072 6daf194d Markus Armbruster
            error_report("failed to send a data, %s", strerror(errno));
1073 cb595887 MORITA Kazutaka
            return -errno;
1074 33b1db1c MORITA Kazutaka
        }
1075 33b1db1c MORITA Kazutaka
    }
1076 33b1db1c MORITA Kazutaka
1077 128aa589 Paolo Bonzini
    socket_set_cork(s->fd, 0);
1078 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(s->fd, co_read_response, NULL, s);
1079 2df46246 MORITA Kazutaka
    qemu_co_mutex_unlock(&s->lock);
1080 33b1db1c MORITA Kazutaka
1081 33b1db1c MORITA Kazutaka
    return 0;
1082 33b1db1c MORITA Kazutaka
}
1083 33b1db1c MORITA Kazutaka
1084 33b1db1c MORITA Kazutaka
static int read_write_object(int fd, char *buf, uint64_t oid, int copies,
1085 33b1db1c MORITA Kazutaka
                             unsigned int datalen, uint64_t offset,
1086 0e7106d8 Liu Yuan
                             bool write, bool create, uint32_t cache_flags)
1087 33b1db1c MORITA Kazutaka
{
1088 33b1db1c MORITA Kazutaka
    SheepdogObjReq hdr;
1089 33b1db1c MORITA Kazutaka
    SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
1090 33b1db1c MORITA Kazutaka
    unsigned int wlen, rlen;
1091 33b1db1c MORITA Kazutaka
    int ret;
1092 33b1db1c MORITA Kazutaka
1093 33b1db1c MORITA Kazutaka
    memset(&hdr, 0, sizeof(hdr));
1094 33b1db1c MORITA Kazutaka
1095 33b1db1c MORITA Kazutaka
    if (write) {
1096 33b1db1c MORITA Kazutaka
        wlen = datalen;
1097 33b1db1c MORITA Kazutaka
        rlen = 0;
1098 33b1db1c MORITA Kazutaka
        hdr.flags = SD_FLAG_CMD_WRITE;
1099 33b1db1c MORITA Kazutaka
        if (create) {
1100 33b1db1c MORITA Kazutaka
            hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1101 33b1db1c MORITA Kazutaka
        } else {
1102 33b1db1c MORITA Kazutaka
            hdr.opcode = SD_OP_WRITE_OBJ;
1103 33b1db1c MORITA Kazutaka
        }
1104 33b1db1c MORITA Kazutaka
    } else {
1105 33b1db1c MORITA Kazutaka
        wlen = 0;
1106 33b1db1c MORITA Kazutaka
        rlen = datalen;
1107 33b1db1c MORITA Kazutaka
        hdr.opcode = SD_OP_READ_OBJ;
1108 33b1db1c MORITA Kazutaka
    }
1109 47622c44 Liu Yuan
1110 0e7106d8 Liu Yuan
    hdr.flags |= cache_flags;
1111 47622c44 Liu Yuan
1112 33b1db1c MORITA Kazutaka
    hdr.oid = oid;
1113 33b1db1c MORITA Kazutaka
    hdr.data_length = datalen;
1114 33b1db1c MORITA Kazutaka
    hdr.offset = offset;
1115 33b1db1c MORITA Kazutaka
    hdr.copies = copies;
1116 33b1db1c MORITA Kazutaka
1117 33b1db1c MORITA Kazutaka
    ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1118 33b1db1c MORITA Kazutaka
    if (ret) {
1119 6daf194d Markus Armbruster
        error_report("failed to send a request to the sheep");
1120 cb595887 MORITA Kazutaka
        return ret;
1121 33b1db1c MORITA Kazutaka
    }
1122 33b1db1c MORITA Kazutaka
1123 33b1db1c MORITA Kazutaka
    switch (rsp->result) {
1124 33b1db1c MORITA Kazutaka
    case SD_RES_SUCCESS:
1125 33b1db1c MORITA Kazutaka
        return 0;
1126 33b1db1c MORITA Kazutaka
    default:
1127 6daf194d Markus Armbruster
        error_report("%s", sd_strerror(rsp->result));
1128 cb595887 MORITA Kazutaka
        return -EIO;
1129 33b1db1c MORITA Kazutaka
    }
1130 33b1db1c MORITA Kazutaka
}
1131 33b1db1c MORITA Kazutaka
1132 33b1db1c MORITA Kazutaka
static int read_object(int fd, char *buf, uint64_t oid, int copies,
1133 0e7106d8 Liu Yuan
                       unsigned int datalen, uint64_t offset,
1134 0e7106d8 Liu Yuan
                       uint32_t cache_flags)
1135 33b1db1c MORITA Kazutaka
{
1136 2f536801 MORITA Kazutaka
    return read_write_object(fd, buf, oid, copies, datalen, offset, false,
1137 0e7106d8 Liu Yuan
                             false, cache_flags);
1138 33b1db1c MORITA Kazutaka
}
1139 33b1db1c MORITA Kazutaka
1140 33b1db1c MORITA Kazutaka
static int write_object(int fd, char *buf, uint64_t oid, int copies,
1141 2f536801 MORITA Kazutaka
                        unsigned int datalen, uint64_t offset, bool create,
1142 0e7106d8 Liu Yuan
                        uint32_t cache_flags)
1143 33b1db1c MORITA Kazutaka
{
1144 2f536801 MORITA Kazutaka
    return read_write_object(fd, buf, oid, copies, datalen, offset, true,
1145 0e7106d8 Liu Yuan
                             create, cache_flags);
1146 33b1db1c MORITA Kazutaka
}
1147 33b1db1c MORITA Kazutaka
1148 9ff53a0e MORITA Kazutaka
/* update inode with the latest state */
1149 9ff53a0e MORITA Kazutaka
static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag)
1150 9ff53a0e MORITA Kazutaka
{
1151 9ff53a0e MORITA Kazutaka
    SheepdogInode *inode;
1152 9ff53a0e MORITA Kazutaka
    int ret = 0, fd;
1153 9ff53a0e MORITA Kazutaka
    uint32_t vid = 0;
1154 9ff53a0e MORITA Kazutaka
1155 9ff53a0e MORITA Kazutaka
    fd = connect_to_sdog(s);
1156 9ff53a0e MORITA Kazutaka
    if (fd < 0) {
1157 9ff53a0e MORITA Kazutaka
        return -EIO;
1158 9ff53a0e MORITA Kazutaka
    }
1159 9ff53a0e MORITA Kazutaka
1160 9ff53a0e MORITA Kazutaka
    inode = g_malloc(sizeof(s->inode));
1161 9ff53a0e MORITA Kazutaka
1162 9ff53a0e MORITA Kazutaka
    ret = find_vdi_name(s, s->name, snapid, tag, &vid, false);
1163 9ff53a0e MORITA Kazutaka
    if (ret) {
1164 9ff53a0e MORITA Kazutaka
        goto out;
1165 9ff53a0e MORITA Kazutaka
    }
1166 9ff53a0e MORITA Kazutaka
1167 9ff53a0e MORITA Kazutaka
    ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid),
1168 9ff53a0e MORITA Kazutaka
                      s->inode.nr_copies, sizeof(*inode), 0, s->cache_flags);
1169 9ff53a0e MORITA Kazutaka
    if (ret < 0) {
1170 9ff53a0e MORITA Kazutaka
        goto out;
1171 9ff53a0e MORITA Kazutaka
    }
1172 9ff53a0e MORITA Kazutaka
1173 9ff53a0e MORITA Kazutaka
    if (inode->vdi_id != s->inode.vdi_id) {
1174 9ff53a0e MORITA Kazutaka
        memcpy(&s->inode, inode, sizeof(s->inode));
1175 9ff53a0e MORITA Kazutaka
    }
1176 9ff53a0e MORITA Kazutaka
1177 9ff53a0e MORITA Kazutaka
out:
1178 9ff53a0e MORITA Kazutaka
    g_free(inode);
1179 9ff53a0e MORITA Kazutaka
    closesocket(fd);
1180 9ff53a0e MORITA Kazutaka
1181 9ff53a0e MORITA Kazutaka
    return ret;
1182 9ff53a0e MORITA Kazutaka
}
1183 9ff53a0e MORITA Kazutaka
1184 13c31de2 MORITA Kazutaka
static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)
1185 13c31de2 MORITA Kazutaka
{
1186 13c31de2 MORITA Kazutaka
    SheepdogAIOCB *acb = aio_req->aiocb;
1187 13c31de2 MORITA Kazutaka
    bool create = false;
1188 13c31de2 MORITA Kazutaka
    int ret;
1189 13c31de2 MORITA Kazutaka
1190 13c31de2 MORITA Kazutaka
    ret = reload_inode(s, 0, "");
1191 13c31de2 MORITA Kazutaka
    if (ret < 0) {
1192 13c31de2 MORITA Kazutaka
        return ret;
1193 13c31de2 MORITA Kazutaka
    }
1194 13c31de2 MORITA Kazutaka
1195 13c31de2 MORITA Kazutaka
    aio_req->oid = vid_to_data_oid(s->inode.vdi_id,
1196 13c31de2 MORITA Kazutaka
                                   data_oid_to_idx(aio_req->oid));
1197 13c31de2 MORITA Kazutaka
1198 13c31de2 MORITA Kazutaka
    /* check whether this request becomes a CoW one */
1199 13c31de2 MORITA Kazutaka
    if (acb->aiocb_type == AIOCB_WRITE_UDATA) {
1200 13c31de2 MORITA Kazutaka
        int idx = data_oid_to_idx(aio_req->oid);
1201 13c31de2 MORITA Kazutaka
        AIOReq *areq;
1202 13c31de2 MORITA Kazutaka
1203 13c31de2 MORITA Kazutaka
        if (s->inode.data_vdi_id[idx] == 0) {
1204 13c31de2 MORITA Kazutaka
            create = true;
1205 13c31de2 MORITA Kazutaka
            goto out;
1206 13c31de2 MORITA Kazutaka
        }
1207 13c31de2 MORITA Kazutaka
        if (is_data_obj_writable(&s->inode, idx)) {
1208 13c31de2 MORITA Kazutaka
            goto out;
1209 13c31de2 MORITA Kazutaka
        }
1210 13c31de2 MORITA Kazutaka
1211 13c31de2 MORITA Kazutaka
        /* link to the pending list if there is another CoW request to
1212 13c31de2 MORITA Kazutaka
         * the same object */
1213 13c31de2 MORITA Kazutaka
        QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
1214 13c31de2 MORITA Kazutaka
            if (areq != aio_req && areq->oid == aio_req->oid) {
1215 2440a2c3 Peter Maydell
                DPRINTF("simultaneous CoW to %" PRIx64 "\n", aio_req->oid);
1216 13c31de2 MORITA Kazutaka
                QLIST_REMOVE(aio_req, aio_siblings);
1217 13c31de2 MORITA Kazutaka
                QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req, aio_siblings);
1218 13c31de2 MORITA Kazutaka
                return SD_RES_SUCCESS;
1219 13c31de2 MORITA Kazutaka
            }
1220 13c31de2 MORITA Kazutaka
        }
1221 13c31de2 MORITA Kazutaka
1222 13c31de2 MORITA Kazutaka
        aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx);
1223 13c31de2 MORITA Kazutaka
        aio_req->flags |= SD_FLAG_CMD_COW;
1224 13c31de2 MORITA Kazutaka
        create = true;
1225 13c31de2 MORITA Kazutaka
    }
1226 13c31de2 MORITA Kazutaka
out:
1227 13c31de2 MORITA Kazutaka
    return add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1228 13c31de2 MORITA Kazutaka
                           create, acb->aiocb_type);
1229 13c31de2 MORITA Kazutaka
}
1230 13c31de2 MORITA Kazutaka
1231 c8c96350 Kevin Wolf
/* TODO Convert to fine grained options */
1232 c8c96350 Kevin Wolf
static QemuOptsList runtime_opts = {
1233 c8c96350 Kevin Wolf
    .name = "sheepdog",
1234 c8c96350 Kevin Wolf
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1235 c8c96350 Kevin Wolf
    .desc = {
1236 c8c96350 Kevin Wolf
        {
1237 c8c96350 Kevin Wolf
            .name = "filename",
1238 c8c96350 Kevin Wolf
            .type = QEMU_OPT_STRING,
1239 c8c96350 Kevin Wolf
            .help = "URL to the sheepdog image",
1240 c8c96350 Kevin Wolf
        },
1241 c8c96350 Kevin Wolf
        { /* end of list */ }
1242 c8c96350 Kevin Wolf
    },
1243 c8c96350 Kevin Wolf
};
1244 c8c96350 Kevin Wolf
1245 015a1036 Max Reitz
static int sd_open(BlockDriverState *bs, QDict *options, int flags,
1246 015a1036 Max Reitz
                   Error **errp)
1247 33b1db1c MORITA Kazutaka
{
1248 33b1db1c MORITA Kazutaka
    int ret, fd;
1249 33b1db1c MORITA Kazutaka
    uint32_t vid = 0;
1250 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
1251 33b1db1c MORITA Kazutaka
    char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1252 33b1db1c MORITA Kazutaka
    uint32_t snapid;
1253 33b1db1c MORITA Kazutaka
    char *buf = NULL;
1254 c8c96350 Kevin Wolf
    QemuOpts *opts;
1255 c8c96350 Kevin Wolf
    Error *local_err = NULL;
1256 c8c96350 Kevin Wolf
    const char *filename;
1257 c8c96350 Kevin Wolf
1258 c8c96350 Kevin Wolf
    opts = qemu_opts_create_nofail(&runtime_opts);
1259 c8c96350 Kevin Wolf
    qemu_opts_absorb_qdict(opts, options, &local_err);
1260 c8c96350 Kevin Wolf
    if (error_is_set(&local_err)) {
1261 c8c96350 Kevin Wolf
        qerror_report_err(local_err);
1262 c8c96350 Kevin Wolf
        error_free(local_err);
1263 c8c96350 Kevin Wolf
        ret = -EINVAL;
1264 c8c96350 Kevin Wolf
        goto out;
1265 c8c96350 Kevin Wolf
    }
1266 c8c96350 Kevin Wolf
1267 c8c96350 Kevin Wolf
    filename = qemu_opt_get(opts, "filename");
1268 33b1db1c MORITA Kazutaka
1269 c292ee6a MORITA Kazutaka
    QLIST_INIT(&s->inflight_aio_head);
1270 c292ee6a MORITA Kazutaka
    QLIST_INIT(&s->pending_aio_head);
1271 33b1db1c MORITA Kazutaka
    s->fd = -1;
1272 33b1db1c MORITA Kazutaka
1273 33b1db1c MORITA Kazutaka
    memset(vdi, 0, sizeof(vdi));
1274 33b1db1c MORITA Kazutaka
    memset(tag, 0, sizeof(tag));
1275 5d6768e3 MORITA Kazutaka
1276 5d6768e3 MORITA Kazutaka
    if (strstr(filename, "://")) {
1277 5d6768e3 MORITA Kazutaka
        ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1278 5d6768e3 MORITA Kazutaka
    } else {
1279 5d6768e3 MORITA Kazutaka
        ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1280 5d6768e3 MORITA Kazutaka
    }
1281 5d6768e3 MORITA Kazutaka
    if (ret < 0) {
1282 33b1db1c MORITA Kazutaka
        goto out;
1283 33b1db1c MORITA Kazutaka
    }
1284 33b1db1c MORITA Kazutaka
    s->fd = get_sheep_fd(s);
1285 33b1db1c MORITA Kazutaka
    if (s->fd < 0) {
1286 cb595887 MORITA Kazutaka
        ret = s->fd;
1287 33b1db1c MORITA Kazutaka
        goto out;
1288 33b1db1c MORITA Kazutaka
    }
1289 33b1db1c MORITA Kazutaka
1290 982dcbf4 MORITA Kazutaka
    ret = find_vdi_name(s, vdi, snapid, tag, &vid, true);
1291 33b1db1c MORITA Kazutaka
    if (ret) {
1292 33b1db1c MORITA Kazutaka
        goto out;
1293 33b1db1c MORITA Kazutaka
    }
1294 33b1db1c MORITA Kazutaka
1295 0e7106d8 Liu Yuan
    /*
1296 0e7106d8 Liu Yuan
     * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1297 0e7106d8 Liu Yuan
     * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1298 0e7106d8 Liu Yuan
     */
1299 0e7106d8 Liu Yuan
    s->cache_flags = SD_FLAG_CMD_CACHE;
1300 0e7106d8 Liu Yuan
    if (flags & BDRV_O_NOCACHE) {
1301 0e7106d8 Liu Yuan
        s->cache_flags = SD_FLAG_CMD_DIRECT;
1302 0e7106d8 Liu Yuan
    }
1303 cac8f4a6 Liu Yuan
    s->discard_supported = true;
1304 0e7106d8 Liu Yuan
1305 622b6057 MORITA Kazutaka
    if (snapid || tag[0] != '\0') {
1306 2440a2c3 Peter Maydell
        DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid);
1307 2f536801 MORITA Kazutaka
        s->is_snapshot = true;
1308 33b1db1c MORITA Kazutaka
    }
1309 33b1db1c MORITA Kazutaka
1310 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1311 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1312 cb595887 MORITA Kazutaka
        ret = fd;
1313 33b1db1c MORITA Kazutaka
        goto out;
1314 33b1db1c MORITA Kazutaka
    }
1315 33b1db1c MORITA Kazutaka
1316 7267c094 Anthony Liguori
    buf = g_malloc(SD_INODE_SIZE);
1317 47622c44 Liu Yuan
    ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0,
1318 0e7106d8 Liu Yuan
                      s->cache_flags);
1319 33b1db1c MORITA Kazutaka
1320 33b1db1c MORITA Kazutaka
    closesocket(fd);
1321 33b1db1c MORITA Kazutaka
1322 33b1db1c MORITA Kazutaka
    if (ret) {
1323 33b1db1c MORITA Kazutaka
        goto out;
1324 33b1db1c MORITA Kazutaka
    }
1325 33b1db1c MORITA Kazutaka
1326 33b1db1c MORITA Kazutaka
    memcpy(&s->inode, buf, sizeof(s->inode));
1327 33b1db1c MORITA Kazutaka
    s->min_dirty_data_idx = UINT32_MAX;
1328 33b1db1c MORITA Kazutaka
    s->max_dirty_data_idx = 0;
1329 33b1db1c MORITA Kazutaka
1330 e8bfaa2f Liu Yuan
    bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
1331 3178e275 Jim Meyering
    pstrcpy(s->name, sizeof(s->name), vdi);
1332 2df46246 MORITA Kazutaka
    qemu_co_mutex_init(&s->lock);
1333 c8c96350 Kevin Wolf
    qemu_opts_del(opts);
1334 7267c094 Anthony Liguori
    g_free(buf);
1335 33b1db1c MORITA Kazutaka
    return 0;
1336 33b1db1c MORITA Kazutaka
out:
1337 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
1338 33b1db1c MORITA Kazutaka
    if (s->fd >= 0) {
1339 33b1db1c MORITA Kazutaka
        closesocket(s->fd);
1340 33b1db1c MORITA Kazutaka
    }
1341 c8c96350 Kevin Wolf
    qemu_opts_del(opts);
1342 7267c094 Anthony Liguori
    g_free(buf);
1343 cb595887 MORITA Kazutaka
    return ret;
1344 33b1db1c MORITA Kazutaka
}
1345 33b1db1c MORITA Kazutaka
1346 25af257d MORITA Kazutaka
static int do_sd_create(BDRVSheepdogState *s, char *filename, int64_t vdi_size,
1347 25af257d MORITA Kazutaka
                        uint32_t base_vid, uint32_t *vdi_id, int snapshot)
1348 33b1db1c MORITA Kazutaka
{
1349 33b1db1c MORITA Kazutaka
    SheepdogVdiReq hdr;
1350 33b1db1c MORITA Kazutaka
    SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1351 33b1db1c MORITA Kazutaka
    int fd, ret;
1352 33b1db1c MORITA Kazutaka
    unsigned int wlen, rlen = 0;
1353 33b1db1c MORITA Kazutaka
    char buf[SD_MAX_VDI_LEN];
1354 33b1db1c MORITA Kazutaka
1355 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1356 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1357 cb595887 MORITA Kazutaka
        return fd;
1358 33b1db1c MORITA Kazutaka
    }
1359 33b1db1c MORITA Kazutaka
1360 3178e275 Jim Meyering
    /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1361 3178e275 Jim Meyering
     * does not fit in buf?  For now, just truncate and avoid buffer overrun.
1362 3178e275 Jim Meyering
     */
1363 33b1db1c MORITA Kazutaka
    memset(buf, 0, sizeof(buf));
1364 3178e275 Jim Meyering
    pstrcpy(buf, sizeof(buf), filename);
1365 33b1db1c MORITA Kazutaka
1366 33b1db1c MORITA Kazutaka
    memset(&hdr, 0, sizeof(hdr));
1367 33b1db1c MORITA Kazutaka
    hdr.opcode = SD_OP_NEW_VDI;
1368 6f74c260 Liu Yuan
    hdr.vdi_id = base_vid;
1369 33b1db1c MORITA Kazutaka
1370 33b1db1c MORITA Kazutaka
    wlen = SD_MAX_VDI_LEN;
1371 33b1db1c MORITA Kazutaka
1372 33b1db1c MORITA Kazutaka
    hdr.flags = SD_FLAG_CMD_WRITE;
1373 33b1db1c MORITA Kazutaka
    hdr.snapid = snapshot;
1374 33b1db1c MORITA Kazutaka
1375 33b1db1c MORITA Kazutaka
    hdr.data_length = wlen;
1376 33b1db1c MORITA Kazutaka
    hdr.vdi_size = vdi_size;
1377 33b1db1c MORITA Kazutaka
1378 33b1db1c MORITA Kazutaka
    ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1379 33b1db1c MORITA Kazutaka
1380 33b1db1c MORITA Kazutaka
    closesocket(fd);
1381 33b1db1c MORITA Kazutaka
1382 33b1db1c MORITA Kazutaka
    if (ret) {
1383 cb595887 MORITA Kazutaka
        return ret;
1384 33b1db1c MORITA Kazutaka
    }
1385 33b1db1c MORITA Kazutaka
1386 33b1db1c MORITA Kazutaka
    if (rsp->result != SD_RES_SUCCESS) {
1387 6daf194d Markus Armbruster
        error_report("%s, %s", sd_strerror(rsp->result), filename);
1388 33b1db1c MORITA Kazutaka
        return -EIO;
1389 33b1db1c MORITA Kazutaka
    }
1390 33b1db1c MORITA Kazutaka
1391 33b1db1c MORITA Kazutaka
    if (vdi_id) {
1392 33b1db1c MORITA Kazutaka
        *vdi_id = rsp->vdi_id;
1393 33b1db1c MORITA Kazutaka
    }
1394 33b1db1c MORITA Kazutaka
1395 33b1db1c MORITA Kazutaka
    return 0;
1396 33b1db1c MORITA Kazutaka
}
1397 33b1db1c MORITA Kazutaka
1398 a8e0fdd7 MORITA Kazutaka
static int sd_prealloc(const char *filename)
1399 a8e0fdd7 MORITA Kazutaka
{
1400 a8e0fdd7 MORITA Kazutaka
    BlockDriverState *bs = NULL;
1401 a8e0fdd7 MORITA Kazutaka
    uint32_t idx, max_idx;
1402 a8e0fdd7 MORITA Kazutaka
    int64_t vdi_size;
1403 7267c094 Anthony Liguori
    void *buf = g_malloc0(SD_DATA_OBJ_SIZE);
1404 34b5d2c6 Max Reitz
    Error *local_err = NULL;
1405 a8e0fdd7 MORITA Kazutaka
    int ret;
1406 a8e0fdd7 MORITA Kazutaka
1407 34b5d2c6 Max Reitz
    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
1408 a8e0fdd7 MORITA Kazutaka
    if (ret < 0) {
1409 34b5d2c6 Max Reitz
        qerror_report_err(local_err);
1410 34b5d2c6 Max Reitz
        error_free(local_err);
1411 a8e0fdd7 MORITA Kazutaka
        goto out;
1412 a8e0fdd7 MORITA Kazutaka
    }
1413 a8e0fdd7 MORITA Kazutaka
1414 a8e0fdd7 MORITA Kazutaka
    vdi_size = bdrv_getlength(bs);
1415 a8e0fdd7 MORITA Kazutaka
    if (vdi_size < 0) {
1416 a8e0fdd7 MORITA Kazutaka
        ret = vdi_size;
1417 a8e0fdd7 MORITA Kazutaka
        goto out;
1418 a8e0fdd7 MORITA Kazutaka
    }
1419 a8e0fdd7 MORITA Kazutaka
    max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE);
1420 a8e0fdd7 MORITA Kazutaka
1421 a8e0fdd7 MORITA Kazutaka
    for (idx = 0; idx < max_idx; idx++) {
1422 a8e0fdd7 MORITA Kazutaka
        /*
1423 a8e0fdd7 MORITA Kazutaka
         * The created image can be a cloned image, so we need to read
1424 a8e0fdd7 MORITA Kazutaka
         * a data from the source image.
1425 a8e0fdd7 MORITA Kazutaka
         */
1426 a8e0fdd7 MORITA Kazutaka
        ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1427 a8e0fdd7 MORITA Kazutaka
        if (ret < 0) {
1428 a8e0fdd7 MORITA Kazutaka
            goto out;
1429 a8e0fdd7 MORITA Kazutaka
        }
1430 a8e0fdd7 MORITA Kazutaka
        ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1431 a8e0fdd7 MORITA Kazutaka
        if (ret < 0) {
1432 a8e0fdd7 MORITA Kazutaka
            goto out;
1433 a8e0fdd7 MORITA Kazutaka
        }
1434 a8e0fdd7 MORITA Kazutaka
    }
1435 a8e0fdd7 MORITA Kazutaka
out:
1436 a8e0fdd7 MORITA Kazutaka
    if (bs) {
1437 4f6fd349 Fam Zheng
        bdrv_unref(bs);
1438 a8e0fdd7 MORITA Kazutaka
    }
1439 7267c094 Anthony Liguori
    g_free(buf);
1440 a8e0fdd7 MORITA Kazutaka
1441 a8e0fdd7 MORITA Kazutaka
    return ret;
1442 a8e0fdd7 MORITA Kazutaka
}
1443 a8e0fdd7 MORITA Kazutaka
1444 d5124c00 Max Reitz
static int sd_create(const char *filename, QEMUOptionParameter *options,
1445 d5124c00 Max Reitz
                     Error **errp)
1446 33b1db1c MORITA Kazutaka
{
1447 b6fc8245 MORITA Kazutaka
    int ret = 0;
1448 b4447363 MORITA Kazutaka
    uint32_t vid = 0, base_vid = 0;
1449 33b1db1c MORITA Kazutaka
    int64_t vdi_size = 0;
1450 33b1db1c MORITA Kazutaka
    char *backing_file = NULL;
1451 b6fc8245 MORITA Kazutaka
    BDRVSheepdogState *s;
1452 b4447363 MORITA Kazutaka
    char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1453 b4447363 MORITA Kazutaka
    uint32_t snapid;
1454 2f536801 MORITA Kazutaka
    bool prealloc = false;
1455 34b5d2c6 Max Reitz
    Error *local_err = NULL;
1456 33b1db1c MORITA Kazutaka
1457 b6fc8245 MORITA Kazutaka
    s = g_malloc0(sizeof(BDRVSheepdogState));
1458 b6fc8245 MORITA Kazutaka
1459 b4447363 MORITA Kazutaka
    memset(vdi, 0, sizeof(vdi));
1460 b4447363 MORITA Kazutaka
    memset(tag, 0, sizeof(tag));
1461 5d6768e3 MORITA Kazutaka
    if (strstr(filename, "://")) {
1462 5d6768e3 MORITA Kazutaka
        ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1463 5d6768e3 MORITA Kazutaka
    } else {
1464 5d6768e3 MORITA Kazutaka
        ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1465 5d6768e3 MORITA Kazutaka
    }
1466 5d6768e3 MORITA Kazutaka
    if (ret < 0) {
1467 b6fc8245 MORITA Kazutaka
        goto out;
1468 b4447363 MORITA Kazutaka
    }
1469 b4447363 MORITA Kazutaka
1470 33b1db1c MORITA Kazutaka
    while (options && options->name) {
1471 33b1db1c MORITA Kazutaka
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1472 33b1db1c MORITA Kazutaka
            vdi_size = options->value.n;
1473 33b1db1c MORITA Kazutaka
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1474 33b1db1c MORITA Kazutaka
            backing_file = options->value.s;
1475 a8e0fdd7 MORITA Kazutaka
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1476 a8e0fdd7 MORITA Kazutaka
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1477 2f536801 MORITA Kazutaka
                prealloc = false;
1478 a8e0fdd7 MORITA Kazutaka
            } else if (!strcmp(options->value.s, "full")) {
1479 2f536801 MORITA Kazutaka
                prealloc = true;
1480 a8e0fdd7 MORITA Kazutaka
            } else {
1481 a8e0fdd7 MORITA Kazutaka
                error_report("Invalid preallocation mode: '%s'",
1482 a8e0fdd7 MORITA Kazutaka
                             options->value.s);
1483 b6fc8245 MORITA Kazutaka
                ret = -EINVAL;
1484 b6fc8245 MORITA Kazutaka
                goto out;
1485 a8e0fdd7 MORITA Kazutaka
            }
1486 33b1db1c MORITA Kazutaka
        }
1487 33b1db1c MORITA Kazutaka
        options++;
1488 33b1db1c MORITA Kazutaka
    }
1489 33b1db1c MORITA Kazutaka
1490 33b1db1c MORITA Kazutaka
    if (vdi_size > SD_MAX_VDI_SIZE) {
1491 6daf194d Markus Armbruster
        error_report("too big image size");
1492 b6fc8245 MORITA Kazutaka
        ret = -EINVAL;
1493 b6fc8245 MORITA Kazutaka
        goto out;
1494 33b1db1c MORITA Kazutaka
    }
1495 33b1db1c MORITA Kazutaka
1496 33b1db1c MORITA Kazutaka
    if (backing_file) {
1497 33b1db1c MORITA Kazutaka
        BlockDriverState *bs;
1498 33b1db1c MORITA Kazutaka
        BDRVSheepdogState *s;
1499 33b1db1c MORITA Kazutaka
        BlockDriver *drv;
1500 33b1db1c MORITA Kazutaka
1501 33b1db1c MORITA Kazutaka
        /* Currently, only Sheepdog backing image is supported. */
1502 98289620 Kevin Wolf
        drv = bdrv_find_protocol(backing_file, true);
1503 33b1db1c MORITA Kazutaka
        if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
1504 6daf194d Markus Armbruster
            error_report("backing_file must be a sheepdog image");
1505 b6fc8245 MORITA Kazutaka
            ret = -EINVAL;
1506 b6fc8245 MORITA Kazutaka
            goto out;
1507 33b1db1c MORITA Kazutaka
        }
1508 33b1db1c MORITA Kazutaka
1509 34b5d2c6 Max Reitz
        ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
1510 cb595887 MORITA Kazutaka
        if (ret < 0) {
1511 34b5d2c6 Max Reitz
            qerror_report_err(local_err);
1512 34b5d2c6 Max Reitz
            error_free(local_err);
1513 b6fc8245 MORITA Kazutaka
            goto out;
1514 cb595887 MORITA Kazutaka
        }
1515 33b1db1c MORITA Kazutaka
1516 33b1db1c MORITA Kazutaka
        s = bs->opaque;
1517 33b1db1c MORITA Kazutaka
1518 33b1db1c MORITA Kazutaka
        if (!is_snapshot(&s->inode)) {
1519 6daf194d Markus Armbruster
            error_report("cannot clone from a non snapshot vdi");
1520 4f6fd349 Fam Zheng
            bdrv_unref(bs);
1521 b6fc8245 MORITA Kazutaka
            ret = -EINVAL;
1522 b6fc8245 MORITA Kazutaka
            goto out;
1523 33b1db1c MORITA Kazutaka
        }
1524 33b1db1c MORITA Kazutaka
1525 b4447363 MORITA Kazutaka
        base_vid = s->inode.vdi_id;
1526 4f6fd349 Fam Zheng
        bdrv_unref(bs);
1527 33b1db1c MORITA Kazutaka
    }
1528 33b1db1c MORITA Kazutaka
1529 25af257d MORITA Kazutaka
    ret = do_sd_create(s, vdi, vdi_size, base_vid, &vid, 0);
1530 a8e0fdd7 MORITA Kazutaka
    if (!prealloc || ret) {
1531 b6fc8245 MORITA Kazutaka
        goto out;
1532 a8e0fdd7 MORITA Kazutaka
    }
1533 a8e0fdd7 MORITA Kazutaka
1534 b6fc8245 MORITA Kazutaka
    ret = sd_prealloc(filename);
1535 b6fc8245 MORITA Kazutaka
out:
1536 b6fc8245 MORITA Kazutaka
    g_free(s);
1537 b6fc8245 MORITA Kazutaka
    return ret;
1538 33b1db1c MORITA Kazutaka
}
1539 33b1db1c MORITA Kazutaka
1540 33b1db1c MORITA Kazutaka
static void sd_close(BlockDriverState *bs)
1541 33b1db1c MORITA Kazutaka
{
1542 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
1543 33b1db1c MORITA Kazutaka
    SheepdogVdiReq hdr;
1544 33b1db1c MORITA Kazutaka
    SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1545 33b1db1c MORITA Kazutaka
    unsigned int wlen, rlen = 0;
1546 33b1db1c MORITA Kazutaka
    int fd, ret;
1547 33b1db1c MORITA Kazutaka
1548 2440a2c3 Peter Maydell
    DPRINTF("%s\n", s->name);
1549 33b1db1c MORITA Kazutaka
1550 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1551 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1552 33b1db1c MORITA Kazutaka
        return;
1553 33b1db1c MORITA Kazutaka
    }
1554 33b1db1c MORITA Kazutaka
1555 33b1db1c MORITA Kazutaka
    memset(&hdr, 0, sizeof(hdr));
1556 33b1db1c MORITA Kazutaka
1557 33b1db1c MORITA Kazutaka
    hdr.opcode = SD_OP_RELEASE_VDI;
1558 6f74c260 Liu Yuan
    hdr.vdi_id = s->inode.vdi_id;
1559 33b1db1c MORITA Kazutaka
    wlen = strlen(s->name) + 1;
1560 33b1db1c MORITA Kazutaka
    hdr.data_length = wlen;
1561 33b1db1c MORITA Kazutaka
    hdr.flags = SD_FLAG_CMD_WRITE;
1562 33b1db1c MORITA Kazutaka
1563 33b1db1c MORITA Kazutaka
    ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
1564 33b1db1c MORITA Kazutaka
1565 33b1db1c MORITA Kazutaka
    closesocket(fd);
1566 33b1db1c MORITA Kazutaka
1567 33b1db1c MORITA Kazutaka
    if (!ret && rsp->result != SD_RES_SUCCESS &&
1568 33b1db1c MORITA Kazutaka
        rsp->result != SD_RES_VDI_NOT_LOCKED) {
1569 6daf194d Markus Armbruster
        error_report("%s, %s", sd_strerror(rsp->result), s->name);
1570 33b1db1c MORITA Kazutaka
    }
1571 33b1db1c MORITA Kazutaka
1572 f2e5dca4 Stefan Hajnoczi
    qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
1573 33b1db1c MORITA Kazutaka
    closesocket(s->fd);
1574 25af257d MORITA Kazutaka
    g_free(s->host_spec);
1575 33b1db1c MORITA Kazutaka
}
1576 33b1db1c MORITA Kazutaka
1577 33b1db1c MORITA Kazutaka
static int64_t sd_getlength(BlockDriverState *bs)
1578 33b1db1c MORITA Kazutaka
{
1579 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
1580 33b1db1c MORITA Kazutaka
1581 33b1db1c MORITA Kazutaka
    return s->inode.vdi_size;
1582 33b1db1c MORITA Kazutaka
}
1583 33b1db1c MORITA Kazutaka
1584 33b1db1c MORITA Kazutaka
static int sd_truncate(BlockDriverState *bs, int64_t offset)
1585 33b1db1c MORITA Kazutaka
{
1586 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
1587 33b1db1c MORITA Kazutaka
    int ret, fd;
1588 33b1db1c MORITA Kazutaka
    unsigned int datalen;
1589 33b1db1c MORITA Kazutaka
1590 33b1db1c MORITA Kazutaka
    if (offset < s->inode.vdi_size) {
1591 6daf194d Markus Armbruster
        error_report("shrinking is not supported");
1592 33b1db1c MORITA Kazutaka
        return -EINVAL;
1593 33b1db1c MORITA Kazutaka
    } else if (offset > SD_MAX_VDI_SIZE) {
1594 6daf194d Markus Armbruster
        error_report("too big image size");
1595 33b1db1c MORITA Kazutaka
        return -EINVAL;
1596 33b1db1c MORITA Kazutaka
    }
1597 33b1db1c MORITA Kazutaka
1598 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1599 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1600 cb595887 MORITA Kazutaka
        return fd;
1601 33b1db1c MORITA Kazutaka
    }
1602 33b1db1c MORITA Kazutaka
1603 33b1db1c MORITA Kazutaka
    /* we don't need to update entire object */
1604 33b1db1c MORITA Kazutaka
    datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1605 33b1db1c MORITA Kazutaka
    s->inode.vdi_size = offset;
1606 33b1db1c MORITA Kazutaka
    ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
1607 0e7106d8 Liu Yuan
                       s->inode.nr_copies, datalen, 0, false, s->cache_flags);
1608 33b1db1c MORITA Kazutaka
    close(fd);
1609 33b1db1c MORITA Kazutaka
1610 33b1db1c MORITA Kazutaka
    if (ret < 0) {
1611 6daf194d Markus Armbruster
        error_report("failed to update an inode.");
1612 33b1db1c MORITA Kazutaka
    }
1613 33b1db1c MORITA Kazutaka
1614 cb595887 MORITA Kazutaka
    return ret;
1615 33b1db1c MORITA Kazutaka
}
1616 33b1db1c MORITA Kazutaka
1617 33b1db1c MORITA Kazutaka
/*
1618 33b1db1c MORITA Kazutaka
 * This function is called after writing data objects.  If we need to
1619 33b1db1c MORITA Kazutaka
 * update metadata, this sends a write request to the vdi object.
1620 2df46246 MORITA Kazutaka
 * Otherwise, this switches back to sd_co_readv/writev.
1621 33b1db1c MORITA Kazutaka
 */
1622 d8716b41 Paolo Bonzini
static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
1623 33b1db1c MORITA Kazutaka
{
1624 33b1db1c MORITA Kazutaka
    int ret;
1625 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = acb->common.bs->opaque;
1626 33b1db1c MORITA Kazutaka
    struct iovec iov;
1627 33b1db1c MORITA Kazutaka
    AIOReq *aio_req;
1628 33b1db1c MORITA Kazutaka
    uint32_t offset, data_len, mn, mx;
1629 33b1db1c MORITA Kazutaka
1630 33b1db1c MORITA Kazutaka
    mn = s->min_dirty_data_idx;
1631 33b1db1c MORITA Kazutaka
    mx = s->max_dirty_data_idx;
1632 33b1db1c MORITA Kazutaka
    if (mn <= mx) {
1633 33b1db1c MORITA Kazutaka
        /* we need to update the vdi object. */
1634 33b1db1c MORITA Kazutaka
        offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
1635 33b1db1c MORITA Kazutaka
            mn * sizeof(s->inode.data_vdi_id[0]);
1636 33b1db1c MORITA Kazutaka
        data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
1637 33b1db1c MORITA Kazutaka
1638 33b1db1c MORITA Kazutaka
        s->min_dirty_data_idx = UINT32_MAX;
1639 33b1db1c MORITA Kazutaka
        s->max_dirty_data_idx = 0;
1640 33b1db1c MORITA Kazutaka
1641 33b1db1c MORITA Kazutaka
        iov.iov_base = &s->inode;
1642 33b1db1c MORITA Kazutaka
        iov.iov_len = sizeof(s->inode);
1643 33b1db1c MORITA Kazutaka
        aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1644 33b1db1c MORITA Kazutaka
                                data_len, offset, 0, 0, offset);
1645 c292ee6a MORITA Kazutaka
        QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1646 2f536801 MORITA Kazutaka
        ret = add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
1647 33b1db1c MORITA Kazutaka
        if (ret) {
1648 33b1db1c MORITA Kazutaka
            free_aio_req(s, aio_req);
1649 33b1db1c MORITA Kazutaka
            acb->ret = -EIO;
1650 33b1db1c MORITA Kazutaka
            goto out;
1651 33b1db1c MORITA Kazutaka
        }
1652 33b1db1c MORITA Kazutaka
1653 33b1db1c MORITA Kazutaka
        acb->aio_done_func = sd_finish_aiocb;
1654 33b1db1c MORITA Kazutaka
        acb->aiocb_type = AIOCB_WRITE_UDATA;
1655 33b1db1c MORITA Kazutaka
        return;
1656 33b1db1c MORITA Kazutaka
    }
1657 33b1db1c MORITA Kazutaka
out:
1658 33b1db1c MORITA Kazutaka
    sd_finish_aiocb(acb);
1659 33b1db1c MORITA Kazutaka
}
1660 33b1db1c MORITA Kazutaka
1661 859e5553 Liu Yuan
/* Delete current working VDI on the snapshot chain */
1662 859e5553 Liu Yuan
static bool sd_delete(BDRVSheepdogState *s)
1663 859e5553 Liu Yuan
{
1664 859e5553 Liu Yuan
    unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0;
1665 859e5553 Liu Yuan
    SheepdogVdiReq hdr = {
1666 859e5553 Liu Yuan
        .opcode = SD_OP_DEL_VDI,
1667 859e5553 Liu Yuan
        .vdi_id = s->inode.vdi_id,
1668 859e5553 Liu Yuan
        .data_length = wlen,
1669 859e5553 Liu Yuan
        .flags = SD_FLAG_CMD_WRITE,
1670 859e5553 Liu Yuan
    };
1671 859e5553 Liu Yuan
    SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1672 859e5553 Liu Yuan
    int fd, ret;
1673 859e5553 Liu Yuan
1674 859e5553 Liu Yuan
    fd = connect_to_sdog(s);
1675 859e5553 Liu Yuan
    if (fd < 0) {
1676 859e5553 Liu Yuan
        return false;
1677 859e5553 Liu Yuan
    }
1678 859e5553 Liu Yuan
1679 859e5553 Liu Yuan
    ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
1680 859e5553 Liu Yuan
    closesocket(fd);
1681 859e5553 Liu Yuan
    if (ret) {
1682 859e5553 Liu Yuan
        return false;
1683 859e5553 Liu Yuan
    }
1684 859e5553 Liu Yuan
    switch (rsp->result) {
1685 859e5553 Liu Yuan
    case SD_RES_NO_VDI:
1686 859e5553 Liu Yuan
        error_report("%s was already deleted", s->name);
1687 859e5553 Liu Yuan
        /* fall through */
1688 859e5553 Liu Yuan
    case SD_RES_SUCCESS:
1689 859e5553 Liu Yuan
        break;
1690 859e5553 Liu Yuan
    default:
1691 859e5553 Liu Yuan
        error_report("%s, %s", sd_strerror(rsp->result), s->name);
1692 859e5553 Liu Yuan
        return false;
1693 859e5553 Liu Yuan
    }
1694 859e5553 Liu Yuan
1695 859e5553 Liu Yuan
    return true;
1696 859e5553 Liu Yuan
}
1697 859e5553 Liu Yuan
1698 33b1db1c MORITA Kazutaka
/*
1699 33b1db1c MORITA Kazutaka
 * Create a writable VDI from a snapshot
1700 33b1db1c MORITA Kazutaka
 */
1701 33b1db1c MORITA Kazutaka
static int sd_create_branch(BDRVSheepdogState *s)
1702 33b1db1c MORITA Kazutaka
{
1703 33b1db1c MORITA Kazutaka
    int ret, fd;
1704 33b1db1c MORITA Kazutaka
    uint32_t vid;
1705 33b1db1c MORITA Kazutaka
    char *buf;
1706 859e5553 Liu Yuan
    bool deleted;
1707 33b1db1c MORITA Kazutaka
1708 2440a2c3 Peter Maydell
    DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
1709 33b1db1c MORITA Kazutaka
1710 7267c094 Anthony Liguori
    buf = g_malloc(SD_INODE_SIZE);
1711 33b1db1c MORITA Kazutaka
1712 859e5553 Liu Yuan
    /*
1713 859e5553 Liu Yuan
     * Even If deletion fails, we will just create extra snapshot based on
1714 859e5553 Liu Yuan
     * the workding VDI which was supposed to be deleted. So no need to
1715 859e5553 Liu Yuan
     * false bail out.
1716 859e5553 Liu Yuan
     */
1717 859e5553 Liu Yuan
    deleted = sd_delete(s);
1718 859e5553 Liu Yuan
    ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &vid,
1719 859e5553 Liu Yuan
                       !deleted);
1720 33b1db1c MORITA Kazutaka
    if (ret) {
1721 33b1db1c MORITA Kazutaka
        goto out;
1722 33b1db1c MORITA Kazutaka
    }
1723 33b1db1c MORITA Kazutaka
1724 2440a2c3 Peter Maydell
    DPRINTF("%" PRIx32 " is created.\n", vid);
1725 33b1db1c MORITA Kazutaka
1726 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1727 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1728 cb595887 MORITA Kazutaka
        ret = fd;
1729 33b1db1c MORITA Kazutaka
        goto out;
1730 33b1db1c MORITA Kazutaka
    }
1731 33b1db1c MORITA Kazutaka
1732 33b1db1c MORITA Kazutaka
    ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,
1733 0e7106d8 Liu Yuan
                      SD_INODE_SIZE, 0, s->cache_flags);
1734 33b1db1c MORITA Kazutaka
1735 33b1db1c MORITA Kazutaka
    closesocket(fd);
1736 33b1db1c MORITA Kazutaka
1737 33b1db1c MORITA Kazutaka
    if (ret < 0) {
1738 33b1db1c MORITA Kazutaka
        goto out;
1739 33b1db1c MORITA Kazutaka
    }
1740 33b1db1c MORITA Kazutaka
1741 33b1db1c MORITA Kazutaka
    memcpy(&s->inode, buf, sizeof(s->inode));
1742 33b1db1c MORITA Kazutaka
1743 2f536801 MORITA Kazutaka
    s->is_snapshot = false;
1744 33b1db1c MORITA Kazutaka
    ret = 0;
1745 2440a2c3 Peter Maydell
    DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
1746 33b1db1c MORITA Kazutaka
1747 33b1db1c MORITA Kazutaka
out:
1748 7267c094 Anthony Liguori
    g_free(buf);
1749 33b1db1c MORITA Kazutaka
1750 33b1db1c MORITA Kazutaka
    return ret;
1751 33b1db1c MORITA Kazutaka
}
1752 33b1db1c MORITA Kazutaka
1753 33b1db1c MORITA Kazutaka
/*
1754 33b1db1c MORITA Kazutaka
 * Send I/O requests to the server.
1755 33b1db1c MORITA Kazutaka
 *
1756 33b1db1c MORITA Kazutaka
 * This function sends requests to the server, links the requests to
1757 c292ee6a MORITA Kazutaka
 * the inflight_list in BDRVSheepdogState, and exits without
1758 33b1db1c MORITA Kazutaka
 * waiting the response.  The responses are received in the
1759 33b1db1c MORITA Kazutaka
 * `aio_read_response' function which is called from the main loop as
1760 33b1db1c MORITA Kazutaka
 * a fd handler.
1761 2df46246 MORITA Kazutaka
 *
1762 2df46246 MORITA Kazutaka
 * Returns 1 when we need to wait a response, 0 when there is no sent
1763 2df46246 MORITA Kazutaka
 * request and -errno in error cases.
1764 33b1db1c MORITA Kazutaka
 */
1765 d8716b41 Paolo Bonzini
static int coroutine_fn sd_co_rw_vector(void *p)
1766 33b1db1c MORITA Kazutaka
{
1767 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb = p;
1768 33b1db1c MORITA Kazutaka
    int ret = 0;
1769 e8bfaa2f Liu Yuan
    unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE;
1770 e8bfaa2f Liu Yuan
    unsigned long idx = acb->sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE;
1771 33b1db1c MORITA Kazutaka
    uint64_t oid;
1772 e8bfaa2f Liu Yuan
    uint64_t offset = (acb->sector_num * BDRV_SECTOR_SIZE) % SD_DATA_OBJ_SIZE;
1773 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = acb->common.bs->opaque;
1774 33b1db1c MORITA Kazutaka
    SheepdogInode *inode = &s->inode;
1775 33b1db1c MORITA Kazutaka
    AIOReq *aio_req;
1776 33b1db1c MORITA Kazutaka
1777 33b1db1c MORITA Kazutaka
    if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
1778 33b1db1c MORITA Kazutaka
        /*
1779 33b1db1c MORITA Kazutaka
         * In the case we open the snapshot VDI, Sheepdog creates the
1780 33b1db1c MORITA Kazutaka
         * writable VDI when we do a write operation first.
1781 33b1db1c MORITA Kazutaka
         */
1782 33b1db1c MORITA Kazutaka
        ret = sd_create_branch(s);
1783 33b1db1c MORITA Kazutaka
        if (ret) {
1784 33b1db1c MORITA Kazutaka
            acb->ret = -EIO;
1785 33b1db1c MORITA Kazutaka
            goto out;
1786 33b1db1c MORITA Kazutaka
        }
1787 33b1db1c MORITA Kazutaka
    }
1788 33b1db1c MORITA Kazutaka
1789 1d732d7d MORITA Kazutaka
    /*
1790 1d732d7d MORITA Kazutaka
     * Make sure we don't free the aiocb before we are done with all requests.
1791 1d732d7d MORITA Kazutaka
     * This additional reference is dropped at the end of this function.
1792 1d732d7d MORITA Kazutaka
     */
1793 1d732d7d MORITA Kazutaka
    acb->nr_pending++;
1794 1d732d7d MORITA Kazutaka
1795 33b1db1c MORITA Kazutaka
    while (done != total) {
1796 33b1db1c MORITA Kazutaka
        uint8_t flags = 0;
1797 33b1db1c MORITA Kazutaka
        uint64_t old_oid = 0;
1798 2f536801 MORITA Kazutaka
        bool create = false;
1799 33b1db1c MORITA Kazutaka
1800 33b1db1c MORITA Kazutaka
        oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
1801 33b1db1c MORITA Kazutaka
1802 33b1db1c MORITA Kazutaka
        len = MIN(total - done, SD_DATA_OBJ_SIZE - offset);
1803 33b1db1c MORITA Kazutaka
1804 19db9b90 Christoph Hellwig
        switch (acb->aiocb_type) {
1805 19db9b90 Christoph Hellwig
        case AIOCB_READ_UDATA:
1806 19db9b90 Christoph Hellwig
            if (!inode->data_vdi_id[idx]) {
1807 19db9b90 Christoph Hellwig
                qemu_iovec_memset(acb->qiov, done, 0, len);
1808 33b1db1c MORITA Kazutaka
                goto done;
1809 33b1db1c MORITA Kazutaka
            }
1810 19db9b90 Christoph Hellwig
            break;
1811 19db9b90 Christoph Hellwig
        case AIOCB_WRITE_UDATA:
1812 19db9b90 Christoph Hellwig
            if (!inode->data_vdi_id[idx]) {
1813 2f536801 MORITA Kazutaka
                create = true;
1814 19db9b90 Christoph Hellwig
            } else if (!is_data_obj_writable(inode, idx)) {
1815 19db9b90 Christoph Hellwig
                /* Copy-On-Write */
1816 2f536801 MORITA Kazutaka
                create = true;
1817 19db9b90 Christoph Hellwig
                old_oid = oid;
1818 19db9b90 Christoph Hellwig
                flags = SD_FLAG_CMD_COW;
1819 19db9b90 Christoph Hellwig
            }
1820 19db9b90 Christoph Hellwig
            break;
1821 cac8f4a6 Liu Yuan
        case AIOCB_DISCARD_OBJ:
1822 cac8f4a6 Liu Yuan
            /*
1823 cac8f4a6 Liu Yuan
             * We discard the object only when the whole object is
1824 cac8f4a6 Liu Yuan
             * 1) allocated 2) trimmed. Otherwise, simply skip it.
1825 cac8f4a6 Liu Yuan
             */
1826 cac8f4a6 Liu Yuan
            if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) {
1827 cac8f4a6 Liu Yuan
                goto done;
1828 cac8f4a6 Liu Yuan
            }
1829 cac8f4a6 Liu Yuan
            break;
1830 19db9b90 Christoph Hellwig
        default:
1831 19db9b90 Christoph Hellwig
            break;
1832 33b1db1c MORITA Kazutaka
        }
1833 33b1db1c MORITA Kazutaka
1834 33b1db1c MORITA Kazutaka
        if (create) {
1835 2440a2c3 Peter Maydell
            DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
1836 1b6ac998 MORITA Kazutaka
                    inode->vdi_id, oid,
1837 33b1db1c MORITA Kazutaka
                    vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
1838 33b1db1c MORITA Kazutaka
            oid = vid_to_data_oid(inode->vdi_id, idx);
1839 2440a2c3 Peter Maydell
            DPRINTF("new oid %" PRIx64 "\n", oid);
1840 33b1db1c MORITA Kazutaka
        }
1841 33b1db1c MORITA Kazutaka
1842 33b1db1c MORITA Kazutaka
        aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done);
1843 33b1db1c MORITA Kazutaka
1844 33b1db1c MORITA Kazutaka
        if (create) {
1845 33b1db1c MORITA Kazutaka
            AIOReq *areq;
1846 c292ee6a MORITA Kazutaka
            QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
1847 33b1db1c MORITA Kazutaka
                if (areq->oid == oid) {
1848 33b1db1c MORITA Kazutaka
                    /*
1849 33b1db1c MORITA Kazutaka
                     * Sheepdog cannot handle simultaneous create
1850 33b1db1c MORITA Kazutaka
                     * requests to the same object.  So we cannot send
1851 33b1db1c MORITA Kazutaka
                     * the request until the previous request
1852 33b1db1c MORITA Kazutaka
                     * finishes.
1853 33b1db1c MORITA Kazutaka
                     */
1854 33b1db1c MORITA Kazutaka
                    aio_req->flags = 0;
1855 33b1db1c MORITA Kazutaka
                    aio_req->base_oid = 0;
1856 c292ee6a MORITA Kazutaka
                    QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req,
1857 c292ee6a MORITA Kazutaka
                                      aio_siblings);
1858 33b1db1c MORITA Kazutaka
                    goto done;
1859 33b1db1c MORITA Kazutaka
                }
1860 33b1db1c MORITA Kazutaka
            }
1861 33b1db1c MORITA Kazutaka
        }
1862 33b1db1c MORITA Kazutaka
1863 c292ee6a MORITA Kazutaka
        QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1864 33b1db1c MORITA Kazutaka
        ret = add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1865 33b1db1c MORITA Kazutaka
                              create, acb->aiocb_type);
1866 33b1db1c MORITA Kazutaka
        if (ret < 0) {
1867 6daf194d Markus Armbruster
            error_report("add_aio_request is failed");
1868 33b1db1c MORITA Kazutaka
            free_aio_req(s, aio_req);
1869 33b1db1c MORITA Kazutaka
            acb->ret = -EIO;
1870 33b1db1c MORITA Kazutaka
            goto out;
1871 33b1db1c MORITA Kazutaka
        }
1872 33b1db1c MORITA Kazutaka
    done:
1873 33b1db1c MORITA Kazutaka
        offset = 0;
1874 33b1db1c MORITA Kazutaka
        idx++;
1875 33b1db1c MORITA Kazutaka
        done += len;
1876 33b1db1c MORITA Kazutaka
    }
1877 33b1db1c MORITA Kazutaka
out:
1878 1d732d7d MORITA Kazutaka
    if (!--acb->nr_pending) {
1879 2df46246 MORITA Kazutaka
        return acb->ret;
1880 33b1db1c MORITA Kazutaka
    }
1881 2df46246 MORITA Kazutaka
    return 1;
1882 33b1db1c MORITA Kazutaka
}
1883 33b1db1c MORITA Kazutaka
1884 a968168c Dong Xu Wang
static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
1885 2df46246 MORITA Kazutaka
                        int nb_sectors, QEMUIOVector *qiov)
1886 33b1db1c MORITA Kazutaka
{
1887 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb;
1888 2df46246 MORITA Kazutaka
    int ret;
1889 33b1db1c MORITA Kazutaka
1890 33b1db1c MORITA Kazutaka
    if (bs->growable && sector_num + nb_sectors > bs->total_sectors) {
1891 e8bfaa2f Liu Yuan
        ret = sd_truncate(bs, (sector_num + nb_sectors) * BDRV_SECTOR_SIZE);
1892 cb595887 MORITA Kazutaka
        if (ret < 0) {
1893 cb595887 MORITA Kazutaka
            return ret;
1894 33b1db1c MORITA Kazutaka
        }
1895 33b1db1c MORITA Kazutaka
        bs->total_sectors = sector_num + nb_sectors;
1896 33b1db1c MORITA Kazutaka
    }
1897 33b1db1c MORITA Kazutaka
1898 f700f8e3 Liu Yuan
    acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
1899 33b1db1c MORITA Kazutaka
    acb->aio_done_func = sd_write_done;
1900 33b1db1c MORITA Kazutaka
    acb->aiocb_type = AIOCB_WRITE_UDATA;
1901 33b1db1c MORITA Kazutaka
1902 2df46246 MORITA Kazutaka
    ret = sd_co_rw_vector(acb);
1903 2df46246 MORITA Kazutaka
    if (ret <= 0) {
1904 2df46246 MORITA Kazutaka
        qemu_aio_release(acb);
1905 2df46246 MORITA Kazutaka
        return ret;
1906 2df46246 MORITA Kazutaka
    }
1907 2df46246 MORITA Kazutaka
1908 2df46246 MORITA Kazutaka
    qemu_coroutine_yield();
1909 2df46246 MORITA Kazutaka
1910 2df46246 MORITA Kazutaka
    return acb->ret;
1911 33b1db1c MORITA Kazutaka
}
1912 33b1db1c MORITA Kazutaka
1913 a968168c Dong Xu Wang
static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
1914 2df46246 MORITA Kazutaka
                       int nb_sectors, QEMUIOVector *qiov)
1915 33b1db1c MORITA Kazutaka
{
1916 33b1db1c MORITA Kazutaka
    SheepdogAIOCB *acb;
1917 19db9b90 Christoph Hellwig
    int ret;
1918 33b1db1c MORITA Kazutaka
1919 f700f8e3 Liu Yuan
    acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
1920 33b1db1c MORITA Kazutaka
    acb->aiocb_type = AIOCB_READ_UDATA;
1921 33b1db1c MORITA Kazutaka
    acb->aio_done_func = sd_finish_aiocb;
1922 33b1db1c MORITA Kazutaka
1923 2df46246 MORITA Kazutaka
    ret = sd_co_rw_vector(acb);
1924 2df46246 MORITA Kazutaka
    if (ret <= 0) {
1925 2df46246 MORITA Kazutaka
        qemu_aio_release(acb);
1926 2df46246 MORITA Kazutaka
        return ret;
1927 2df46246 MORITA Kazutaka
    }
1928 2df46246 MORITA Kazutaka
1929 2df46246 MORITA Kazutaka
    qemu_coroutine_yield();
1930 2df46246 MORITA Kazutaka
1931 2df46246 MORITA Kazutaka
    return acb->ret;
1932 33b1db1c MORITA Kazutaka
}
1933 33b1db1c MORITA Kazutaka
1934 47622c44 Liu Yuan
static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
1935 47622c44 Liu Yuan
{
1936 47622c44 Liu Yuan
    BDRVSheepdogState *s = bs->opaque;
1937 47783072 Liu Yuan
    SheepdogAIOCB *acb;
1938 47783072 Liu Yuan
    AIOReq *aio_req;
1939 47622c44 Liu Yuan
    int ret;
1940 47622c44 Liu Yuan
1941 0e7106d8 Liu Yuan
    if (s->cache_flags != SD_FLAG_CMD_CACHE) {
1942 47622c44 Liu Yuan
        return 0;
1943 47622c44 Liu Yuan
    }
1944 47622c44 Liu Yuan
1945 f700f8e3 Liu Yuan
    acb = sd_aio_setup(bs, NULL, 0, 0);
1946 47783072 Liu Yuan
    acb->aiocb_type = AIOCB_FLUSH_CACHE;
1947 47783072 Liu Yuan
    acb->aio_done_func = sd_finish_aiocb;
1948 47622c44 Liu Yuan
1949 47783072 Liu Yuan
    aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1950 47783072 Liu Yuan
                            0, 0, 0, 0, 0);
1951 47783072 Liu Yuan
    QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1952 47783072 Liu Yuan
    ret = add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type);
1953 47783072 Liu Yuan
    if (ret < 0) {
1954 47783072 Liu Yuan
        error_report("add_aio_request is failed");
1955 47783072 Liu Yuan
        free_aio_req(s, aio_req);
1956 47783072 Liu Yuan
        qemu_aio_release(acb);
1957 47622c44 Liu Yuan
        return ret;
1958 47622c44 Liu Yuan
    }
1959 47622c44 Liu Yuan
1960 47783072 Liu Yuan
    qemu_coroutine_yield();
1961 47783072 Liu Yuan
    return acb->ret;
1962 47622c44 Liu Yuan
}
1963 47622c44 Liu Yuan
1964 33b1db1c MORITA Kazutaka
static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
1965 33b1db1c MORITA Kazutaka
{
1966 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
1967 33b1db1c MORITA Kazutaka
    int ret, fd;
1968 33b1db1c MORITA Kazutaka
    uint32_t new_vid;
1969 33b1db1c MORITA Kazutaka
    SheepdogInode *inode;
1970 33b1db1c MORITA Kazutaka
    unsigned int datalen;
1971 33b1db1c MORITA Kazutaka
1972 2440a2c3 Peter Maydell
    DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
1973 33b1db1c MORITA Kazutaka
            "is_snapshot %d\n", sn_info->name, sn_info->id_str,
1974 33b1db1c MORITA Kazutaka
            s->name, sn_info->vm_state_size, s->is_snapshot);
1975 33b1db1c MORITA Kazutaka
1976 33b1db1c MORITA Kazutaka
    if (s->is_snapshot) {
1977 33b1db1c MORITA Kazutaka
        error_report("You can't create a snapshot of a snapshot VDI, "
1978 6daf194d Markus Armbruster
                     "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
1979 33b1db1c MORITA Kazutaka
1980 33b1db1c MORITA Kazutaka
        return -EINVAL;
1981 33b1db1c MORITA Kazutaka
    }
1982 33b1db1c MORITA Kazutaka
1983 2440a2c3 Peter Maydell
    DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
1984 33b1db1c MORITA Kazutaka
1985 33b1db1c MORITA Kazutaka
    s->inode.vm_state_size = sn_info->vm_state_size;
1986 33b1db1c MORITA Kazutaka
    s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
1987 3178e275 Jim Meyering
    /* It appears that inode.tag does not require a NUL terminator,
1988 3178e275 Jim Meyering
     * which means this use of strncpy is ok.
1989 3178e275 Jim Meyering
     */
1990 33b1db1c MORITA Kazutaka
    strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
1991 33b1db1c MORITA Kazutaka
    /* we don't need to update entire object */
1992 33b1db1c MORITA Kazutaka
    datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1993 33b1db1c MORITA Kazutaka
1994 33b1db1c MORITA Kazutaka
    /* refresh inode. */
1995 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
1996 33b1db1c MORITA Kazutaka
    if (fd < 0) {
1997 cb595887 MORITA Kazutaka
        ret = fd;
1998 33b1db1c MORITA Kazutaka
        goto cleanup;
1999 33b1db1c MORITA Kazutaka
    }
2000 33b1db1c MORITA Kazutaka
2001 33b1db1c MORITA Kazutaka
    ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
2002 0e7106d8 Liu Yuan
                       s->inode.nr_copies, datalen, 0, false, s->cache_flags);
2003 33b1db1c MORITA Kazutaka
    if (ret < 0) {
2004 6daf194d Markus Armbruster
        error_report("failed to write snapshot's inode.");
2005 33b1db1c MORITA Kazutaka
        goto cleanup;
2006 33b1db1c MORITA Kazutaka
    }
2007 33b1db1c MORITA Kazutaka
2008 25af257d MORITA Kazutaka
    ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid,
2009 25af257d MORITA Kazutaka
                       1);
2010 33b1db1c MORITA Kazutaka
    if (ret < 0) {
2011 6daf194d Markus Armbruster
        error_report("failed to create inode for snapshot. %s",
2012 33b1db1c MORITA Kazutaka
                     strerror(errno));
2013 33b1db1c MORITA Kazutaka
        goto cleanup;
2014 33b1db1c MORITA Kazutaka
    }
2015 33b1db1c MORITA Kazutaka
2016 7267c094 Anthony Liguori
    inode = (SheepdogInode *)g_malloc(datalen);
2017 33b1db1c MORITA Kazutaka
2018 33b1db1c MORITA Kazutaka
    ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid),
2019 0e7106d8 Liu Yuan
                      s->inode.nr_copies, datalen, 0, s->cache_flags);
2020 33b1db1c MORITA Kazutaka
2021 33b1db1c MORITA Kazutaka
    if (ret < 0) {
2022 6daf194d Markus Armbruster
        error_report("failed to read new inode info. %s", strerror(errno));
2023 33b1db1c MORITA Kazutaka
        goto cleanup;
2024 33b1db1c MORITA Kazutaka
    }
2025 33b1db1c MORITA Kazutaka
2026 33b1db1c MORITA Kazutaka
    memcpy(&s->inode, inode, datalen);
2027 2440a2c3 Peter Maydell
    DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2028 33b1db1c MORITA Kazutaka
            s->inode.name, s->inode.snap_id, s->inode.vdi_id);
2029 33b1db1c MORITA Kazutaka
2030 33b1db1c MORITA Kazutaka
cleanup:
2031 33b1db1c MORITA Kazutaka
    closesocket(fd);
2032 33b1db1c MORITA Kazutaka
    return ret;
2033 33b1db1c MORITA Kazutaka
}
2034 33b1db1c MORITA Kazutaka
2035 859e5553 Liu Yuan
/*
2036 859e5553 Liu Yuan
 * We implement rollback(loadvm) operation to the specified snapshot by
2037 859e5553 Liu Yuan
 * 1) switch to the snapshot
2038 859e5553 Liu Yuan
 * 2) rely on sd_create_branch to delete working VDI and
2039 859e5553 Liu Yuan
 * 3) create a new working VDI based on the speicified snapshot
2040 859e5553 Liu Yuan
 */
2041 33b1db1c MORITA Kazutaka
static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
2042 33b1db1c MORITA Kazutaka
{
2043 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
2044 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *old_s;
2045 9ff53a0e MORITA Kazutaka
    char tag[SD_MAX_VDI_TAG_LEN];
2046 33b1db1c MORITA Kazutaka
    uint32_t snapid = 0;
2047 9ff53a0e MORITA Kazutaka
    int ret = 0;
2048 33b1db1c MORITA Kazutaka
2049 7267c094 Anthony Liguori
    old_s = g_malloc(sizeof(BDRVSheepdogState));
2050 33b1db1c MORITA Kazutaka
2051 33b1db1c MORITA Kazutaka
    memcpy(old_s, s, sizeof(BDRVSheepdogState));
2052 33b1db1c MORITA Kazutaka
2053 33b1db1c MORITA Kazutaka
    snapid = strtoul(snapshot_id, NULL, 10);
2054 3178e275 Jim Meyering
    if (snapid) {
2055 3178e275 Jim Meyering
        tag[0] = 0;
2056 3178e275 Jim Meyering
    } else {
2057 b579ffb3 Liu Yuan
        pstrcpy(tag, sizeof(tag), snapshot_id);
2058 33b1db1c MORITA Kazutaka
    }
2059 33b1db1c MORITA Kazutaka
2060 9ff53a0e MORITA Kazutaka
    ret = reload_inode(s, snapid, tag);
2061 33b1db1c MORITA Kazutaka
    if (ret) {
2062 33b1db1c MORITA Kazutaka
        goto out;
2063 33b1db1c MORITA Kazutaka
    }
2064 33b1db1c MORITA Kazutaka
2065 cede621f Liu Yuan
    ret = sd_create_branch(s);
2066 cede621f Liu Yuan
    if (ret) {
2067 33b1db1c MORITA Kazutaka
        goto out;
2068 33b1db1c MORITA Kazutaka
    }
2069 33b1db1c MORITA Kazutaka
2070 7267c094 Anthony Liguori
    g_free(old_s);
2071 33b1db1c MORITA Kazutaka
2072 33b1db1c MORITA Kazutaka
    return 0;
2073 33b1db1c MORITA Kazutaka
out:
2074 33b1db1c MORITA Kazutaka
    /* recover bdrv_sd_state */
2075 33b1db1c MORITA Kazutaka
    memcpy(s, old_s, sizeof(BDRVSheepdogState));
2076 7267c094 Anthony Liguori
    g_free(old_s);
2077 33b1db1c MORITA Kazutaka
2078 6daf194d Markus Armbruster
    error_report("failed to open. recover old bdrv_sd_state.");
2079 33b1db1c MORITA Kazutaka
2080 33b1db1c MORITA Kazutaka
    return ret;
2081 33b1db1c MORITA Kazutaka
}
2082 33b1db1c MORITA Kazutaka
2083 a89d89d3 Wenchao Xia
static int sd_snapshot_delete(BlockDriverState *bs,
2084 a89d89d3 Wenchao Xia
                              const char *snapshot_id,
2085 a89d89d3 Wenchao Xia
                              const char *name,
2086 a89d89d3 Wenchao Xia
                              Error **errp)
2087 33b1db1c MORITA Kazutaka
{
2088 33b1db1c MORITA Kazutaka
    /* FIXME: Delete specified snapshot id.  */
2089 33b1db1c MORITA Kazutaka
    return 0;
2090 33b1db1c MORITA Kazutaka
}
2091 33b1db1c MORITA Kazutaka
2092 33b1db1c MORITA Kazutaka
static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
2093 33b1db1c MORITA Kazutaka
{
2094 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
2095 33b1db1c MORITA Kazutaka
    SheepdogReq req;
2096 33b1db1c MORITA Kazutaka
    int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
2097 33b1db1c MORITA Kazutaka
    QEMUSnapshotInfo *sn_tab = NULL;
2098 33b1db1c MORITA Kazutaka
    unsigned wlen, rlen;
2099 33b1db1c MORITA Kazutaka
    int found = 0;
2100 33b1db1c MORITA Kazutaka
    static SheepdogInode inode;
2101 33b1db1c MORITA Kazutaka
    unsigned long *vdi_inuse;
2102 33b1db1c MORITA Kazutaka
    unsigned int start_nr;
2103 33b1db1c MORITA Kazutaka
    uint64_t hval;
2104 33b1db1c MORITA Kazutaka
    uint32_t vid;
2105 33b1db1c MORITA Kazutaka
2106 7267c094 Anthony Liguori
    vdi_inuse = g_malloc(max);
2107 33b1db1c MORITA Kazutaka
2108 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
2109 33b1db1c MORITA Kazutaka
    if (fd < 0) {
2110 cb595887 MORITA Kazutaka
        ret = fd;
2111 33b1db1c MORITA Kazutaka
        goto out;
2112 33b1db1c MORITA Kazutaka
    }
2113 33b1db1c MORITA Kazutaka
2114 33b1db1c MORITA Kazutaka
    rlen = max;
2115 33b1db1c MORITA Kazutaka
    wlen = 0;
2116 33b1db1c MORITA Kazutaka
2117 33b1db1c MORITA Kazutaka
    memset(&req, 0, sizeof(req));
2118 33b1db1c MORITA Kazutaka
2119 33b1db1c MORITA Kazutaka
    req.opcode = SD_OP_READ_VDIS;
2120 33b1db1c MORITA Kazutaka
    req.data_length = max;
2121 33b1db1c MORITA Kazutaka
2122 33b1db1c MORITA Kazutaka
    ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen);
2123 33b1db1c MORITA Kazutaka
2124 33b1db1c MORITA Kazutaka
    closesocket(fd);
2125 33b1db1c MORITA Kazutaka
    if (ret) {
2126 33b1db1c MORITA Kazutaka
        goto out;
2127 33b1db1c MORITA Kazutaka
    }
2128 33b1db1c MORITA Kazutaka
2129 7267c094 Anthony Liguori
    sn_tab = g_malloc0(nr * sizeof(*sn_tab));
2130 33b1db1c MORITA Kazutaka
2131 33b1db1c MORITA Kazutaka
    /* calculate a vdi id with hash function */
2132 33b1db1c MORITA Kazutaka
    hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
2133 33b1db1c MORITA Kazutaka
    start_nr = hval & (SD_NR_VDIS - 1);
2134 33b1db1c MORITA Kazutaka
2135 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
2136 33b1db1c MORITA Kazutaka
    if (fd < 0) {
2137 cb595887 MORITA Kazutaka
        ret = fd;
2138 33b1db1c MORITA Kazutaka
        goto out;
2139 33b1db1c MORITA Kazutaka
    }
2140 33b1db1c MORITA Kazutaka
2141 33b1db1c MORITA Kazutaka
    for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
2142 33b1db1c MORITA Kazutaka
        if (!test_bit(vid, vdi_inuse)) {
2143 33b1db1c MORITA Kazutaka
            break;
2144 33b1db1c MORITA Kazutaka
        }
2145 33b1db1c MORITA Kazutaka
2146 33b1db1c MORITA Kazutaka
        /* we don't need to read entire object */
2147 33b1db1c MORITA Kazutaka
        ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid),
2148 47622c44 Liu Yuan
                          0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
2149 0e7106d8 Liu Yuan
                          s->cache_flags);
2150 33b1db1c MORITA Kazutaka
2151 33b1db1c MORITA Kazutaka
        if (ret) {
2152 33b1db1c MORITA Kazutaka
            continue;
2153 33b1db1c MORITA Kazutaka
        }
2154 33b1db1c MORITA Kazutaka
2155 33b1db1c MORITA Kazutaka
        if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
2156 33b1db1c MORITA Kazutaka
            sn_tab[found].date_sec = inode.snap_ctime >> 32;
2157 33b1db1c MORITA Kazutaka
            sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
2158 33b1db1c MORITA Kazutaka
            sn_tab[found].vm_state_size = inode.vm_state_size;
2159 33b1db1c MORITA Kazutaka
            sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
2160 33b1db1c MORITA Kazutaka
2161 33b1db1c MORITA Kazutaka
            snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u",
2162 33b1db1c MORITA Kazutaka
                     inode.snap_id);
2163 3178e275 Jim Meyering
            pstrcpy(sn_tab[found].name,
2164 3178e275 Jim Meyering
                    MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
2165 3178e275 Jim Meyering
                    inode.tag);
2166 33b1db1c MORITA Kazutaka
            found++;
2167 33b1db1c MORITA Kazutaka
        }
2168 33b1db1c MORITA Kazutaka
    }
2169 33b1db1c MORITA Kazutaka
2170 33b1db1c MORITA Kazutaka
    closesocket(fd);
2171 33b1db1c MORITA Kazutaka
out:
2172 33b1db1c MORITA Kazutaka
    *psn_tab = sn_tab;
2173 33b1db1c MORITA Kazutaka
2174 7267c094 Anthony Liguori
    g_free(vdi_inuse);
2175 33b1db1c MORITA Kazutaka
2176 cb595887 MORITA Kazutaka
    if (ret < 0) {
2177 cb595887 MORITA Kazutaka
        return ret;
2178 cb595887 MORITA Kazutaka
    }
2179 cb595887 MORITA Kazutaka
2180 33b1db1c MORITA Kazutaka
    return found;
2181 33b1db1c MORITA Kazutaka
}
2182 33b1db1c MORITA Kazutaka
2183 33b1db1c MORITA Kazutaka
static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
2184 33b1db1c MORITA Kazutaka
                                int64_t pos, int size, int load)
2185 33b1db1c MORITA Kazutaka
{
2186 2f536801 MORITA Kazutaka
    bool create;
2187 2f536801 MORITA Kazutaka
    int fd, ret = 0, remaining = size;
2188 33b1db1c MORITA Kazutaka
    unsigned int data_len;
2189 33b1db1c MORITA Kazutaka
    uint64_t vmstate_oid;
2190 33b1db1c MORITA Kazutaka
    uint64_t offset;
2191 cede621f Liu Yuan
    uint32_t vdi_index;
2192 cede621f Liu Yuan
    uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id;
2193 33b1db1c MORITA Kazutaka
2194 25af257d MORITA Kazutaka
    fd = connect_to_sdog(s);
2195 33b1db1c MORITA Kazutaka
    if (fd < 0) {
2196 cb595887 MORITA Kazutaka
        return fd;
2197 33b1db1c MORITA Kazutaka
    }
2198 33b1db1c MORITA Kazutaka
2199 6f3c714e MORITA Kazutaka
    while (remaining) {
2200 33b1db1c MORITA Kazutaka
        vdi_index = pos / SD_DATA_OBJ_SIZE;
2201 33b1db1c MORITA Kazutaka
        offset = pos % SD_DATA_OBJ_SIZE;
2202 33b1db1c MORITA Kazutaka
2203 1f7a48de MORITA Kazutaka
        data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);
2204 33b1db1c MORITA Kazutaka
2205 cede621f Liu Yuan
        vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index);
2206 33b1db1c MORITA Kazutaka
2207 33b1db1c MORITA Kazutaka
        create = (offset == 0);
2208 33b1db1c MORITA Kazutaka
        if (load) {
2209 33b1db1c MORITA Kazutaka
            ret = read_object(fd, (char *)data, vmstate_oid,
2210 47622c44 Liu Yuan
                              s->inode.nr_copies, data_len, offset,
2211 0e7106d8 Liu Yuan
                              s->cache_flags);
2212 33b1db1c MORITA Kazutaka
        } else {
2213 33b1db1c MORITA Kazutaka
            ret = write_object(fd, (char *)data, vmstate_oid,
2214 47622c44 Liu Yuan
                               s->inode.nr_copies, data_len, offset, create,
2215 0e7106d8 Liu Yuan
                               s->cache_flags);
2216 33b1db1c MORITA Kazutaka
        }
2217 33b1db1c MORITA Kazutaka
2218 33b1db1c MORITA Kazutaka
        if (ret < 0) {
2219 6daf194d Markus Armbruster
            error_report("failed to save vmstate %s", strerror(errno));
2220 33b1db1c MORITA Kazutaka
            goto cleanup;
2221 33b1db1c MORITA Kazutaka
        }
2222 33b1db1c MORITA Kazutaka
2223 33b1db1c MORITA Kazutaka
        pos += data_len;
2224 1f7a48de MORITA Kazutaka
        data += data_len;
2225 6f3c714e MORITA Kazutaka
        remaining -= data_len;
2226 33b1db1c MORITA Kazutaka
    }
2227 6f3c714e MORITA Kazutaka
    ret = size;
2228 33b1db1c MORITA Kazutaka
cleanup:
2229 33b1db1c MORITA Kazutaka
    closesocket(fd);
2230 33b1db1c MORITA Kazutaka
    return ret;
2231 33b1db1c MORITA Kazutaka
}
2232 33b1db1c MORITA Kazutaka
2233 cf8074b3 Kevin Wolf
static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2234 cf8074b3 Kevin Wolf
                           int64_t pos)
2235 33b1db1c MORITA Kazutaka
{
2236 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
2237 cf8074b3 Kevin Wolf
    void *buf;
2238 cf8074b3 Kevin Wolf
    int ret;
2239 33b1db1c MORITA Kazutaka
2240 cf8074b3 Kevin Wolf
    buf = qemu_blockalign(bs, qiov->size);
2241 cf8074b3 Kevin Wolf
    qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
2242 cf8074b3 Kevin Wolf
    ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0);
2243 cf8074b3 Kevin Wolf
    qemu_vfree(buf);
2244 cf8074b3 Kevin Wolf
2245 cf8074b3 Kevin Wolf
    return ret;
2246 33b1db1c MORITA Kazutaka
}
2247 33b1db1c MORITA Kazutaka
2248 33b1db1c MORITA Kazutaka
static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
2249 33b1db1c MORITA Kazutaka
                           int64_t pos, int size)
2250 33b1db1c MORITA Kazutaka
{
2251 33b1db1c MORITA Kazutaka
    BDRVSheepdogState *s = bs->opaque;
2252 33b1db1c MORITA Kazutaka
2253 33b1db1c MORITA Kazutaka
    return do_load_save_vmstate(s, data, pos, size, 1);
2254 33b1db1c MORITA Kazutaka
}
2255 33b1db1c MORITA Kazutaka
2256 33b1db1c MORITA Kazutaka
2257 cac8f4a6 Liu Yuan
static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num,
2258 cac8f4a6 Liu Yuan
                                      int nb_sectors)
2259 cac8f4a6 Liu Yuan
{
2260 cac8f4a6 Liu Yuan
    SheepdogAIOCB *acb;
2261 cac8f4a6 Liu Yuan
    QEMUIOVector dummy;
2262 cac8f4a6 Liu Yuan
    BDRVSheepdogState *s = bs->opaque;
2263 cac8f4a6 Liu Yuan
    int ret;
2264 cac8f4a6 Liu Yuan
2265 cac8f4a6 Liu Yuan
    if (!s->discard_supported) {
2266 cac8f4a6 Liu Yuan
            return 0;
2267 cac8f4a6 Liu Yuan
    }
2268 cac8f4a6 Liu Yuan
2269 cac8f4a6 Liu Yuan
    acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors);
2270 cac8f4a6 Liu Yuan
    acb->aiocb_type = AIOCB_DISCARD_OBJ;
2271 cac8f4a6 Liu Yuan
    acb->aio_done_func = sd_finish_aiocb;
2272 cac8f4a6 Liu Yuan
2273 cac8f4a6 Liu Yuan
    ret = sd_co_rw_vector(acb);
2274 cac8f4a6 Liu Yuan
    if (ret <= 0) {
2275 cac8f4a6 Liu Yuan
        qemu_aio_release(acb);
2276 cac8f4a6 Liu Yuan
        return ret;
2277 cac8f4a6 Liu Yuan
    }
2278 cac8f4a6 Liu Yuan
2279 cac8f4a6 Liu Yuan
    qemu_coroutine_yield();
2280 cac8f4a6 Liu Yuan
2281 cac8f4a6 Liu Yuan
    return acb->ret;
2282 cac8f4a6 Liu Yuan
}
2283 cac8f4a6 Liu Yuan
2284 b6b8a333 Paolo Bonzini
static coroutine_fn int64_t
2285 b6b8a333 Paolo Bonzini
sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2286 b6b8a333 Paolo Bonzini
                       int *pnum)
2287 8d71c631 Liu Yuan
{
2288 8d71c631 Liu Yuan
    BDRVSheepdogState *s = bs->opaque;
2289 8d71c631 Liu Yuan
    SheepdogInode *inode = &s->inode;
2290 8d71c631 Liu Yuan
    unsigned long start = sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE,
2291 8d71c631 Liu Yuan
                  end = DIV_ROUND_UP((sector_num + nb_sectors) *
2292 8d71c631 Liu Yuan
                                     BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE);
2293 8d71c631 Liu Yuan
    unsigned long idx;
2294 4bc74be9 Paolo Bonzini
    int64_t ret = BDRV_BLOCK_DATA;
2295 8d71c631 Liu Yuan
2296 8d71c631 Liu Yuan
    for (idx = start; idx < end; idx++) {
2297 8d71c631 Liu Yuan
        if (inode->data_vdi_id[idx] == 0) {
2298 8d71c631 Liu Yuan
            break;
2299 8d71c631 Liu Yuan
        }
2300 8d71c631 Liu Yuan
    }
2301 8d71c631 Liu Yuan
    if (idx == start) {
2302 8d71c631 Liu Yuan
        /* Get the longest length of unallocated sectors */
2303 8d71c631 Liu Yuan
        ret = 0;
2304 8d71c631 Liu Yuan
        for (idx = start + 1; idx < end; idx++) {
2305 8d71c631 Liu Yuan
            if (inode->data_vdi_id[idx] != 0) {
2306 8d71c631 Liu Yuan
                break;
2307 8d71c631 Liu Yuan
            }
2308 8d71c631 Liu Yuan
        }
2309 8d71c631 Liu Yuan
    }
2310 8d71c631 Liu Yuan
2311 8d71c631 Liu Yuan
    *pnum = (idx - start) * SD_DATA_OBJ_SIZE / BDRV_SECTOR_SIZE;
2312 8d71c631 Liu Yuan
    if (*pnum > nb_sectors) {
2313 8d71c631 Liu Yuan
        *pnum = nb_sectors;
2314 8d71c631 Liu Yuan
    }
2315 8d71c631 Liu Yuan
    return ret;
2316 8d71c631 Liu Yuan
}
2317 8d71c631 Liu Yuan
2318 33b1db1c MORITA Kazutaka
static QEMUOptionParameter sd_create_options[] = {
2319 33b1db1c MORITA Kazutaka
    {
2320 33b1db1c MORITA Kazutaka
        .name = BLOCK_OPT_SIZE,
2321 33b1db1c MORITA Kazutaka
        .type = OPT_SIZE,
2322 33b1db1c MORITA Kazutaka
        .help = "Virtual disk size"
2323 33b1db1c MORITA Kazutaka
    },
2324 33b1db1c MORITA Kazutaka
    {
2325 33b1db1c MORITA Kazutaka
        .name = BLOCK_OPT_BACKING_FILE,
2326 33b1db1c MORITA Kazutaka
        .type = OPT_STRING,
2327 33b1db1c MORITA Kazutaka
        .help = "File name of a base image"
2328 33b1db1c MORITA Kazutaka
    },
2329 a8e0fdd7 MORITA Kazutaka
    {
2330 a8e0fdd7 MORITA Kazutaka
        .name = BLOCK_OPT_PREALLOC,
2331 a8e0fdd7 MORITA Kazutaka
        .type = OPT_STRING,
2332 a8e0fdd7 MORITA Kazutaka
        .help = "Preallocation mode (allowed values: off, full)"
2333 a8e0fdd7 MORITA Kazutaka
    },
2334 33b1db1c MORITA Kazutaka
    { NULL }
2335 33b1db1c MORITA Kazutaka
};
2336 33b1db1c MORITA Kazutaka
2337 5d6768e3 MORITA Kazutaka
static BlockDriver bdrv_sheepdog = {
2338 33b1db1c MORITA Kazutaka
    .format_name    = "sheepdog",
2339 33b1db1c MORITA Kazutaka
    .protocol_name  = "sheepdog",
2340 33b1db1c MORITA Kazutaka
    .instance_size  = sizeof(BDRVSheepdogState),
2341 33b1db1c MORITA Kazutaka
    .bdrv_file_open = sd_open,
2342 33b1db1c MORITA Kazutaka
    .bdrv_close     = sd_close,
2343 33b1db1c MORITA Kazutaka
    .bdrv_create    = sd_create,
2344 e4f5c1bf Liu Yuan
    .bdrv_has_zero_init = bdrv_has_zero_init_1,
2345 33b1db1c MORITA Kazutaka
    .bdrv_getlength = sd_getlength,
2346 33b1db1c MORITA Kazutaka
    .bdrv_truncate  = sd_truncate,
2347 33b1db1c MORITA Kazutaka
2348 2df46246 MORITA Kazutaka
    .bdrv_co_readv  = sd_co_readv,
2349 2df46246 MORITA Kazutaka
    .bdrv_co_writev = sd_co_writev,
2350 47622c44 Liu Yuan
    .bdrv_co_flush_to_disk  = sd_co_flush_to_disk,
2351 cac8f4a6 Liu Yuan
    .bdrv_co_discard = sd_co_discard,
2352 b6b8a333 Paolo Bonzini
    .bdrv_co_get_block_status = sd_co_get_block_status,
2353 33b1db1c MORITA Kazutaka
2354 33b1db1c MORITA Kazutaka
    .bdrv_snapshot_create   = sd_snapshot_create,
2355 33b1db1c MORITA Kazutaka