root / block / nbd.c @ 73f5e313
History | View | Annotate | Download (7.6 kB)
1 | 75818250 | ths | /*
|
---|---|---|---|
2 | 75818250 | ths | * QEMU Block driver for NBD
|
3 | 75818250 | ths | *
|
4 | 75818250 | ths | * Copyright (C) 2008 Bull S.A.S.
|
5 | bd5921b4 | malc | * Author: Laurent Vivier <Laurent.Vivier@bull.net>
|
6 | 75818250 | ths | *
|
7 | 75818250 | ths | * Some parts:
|
8 | 75818250 | ths | * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
|
9 | 75818250 | ths | *
|
10 | 75818250 | ths | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | 75818250 | ths | * of this software and associated documentation files (the "Software"), to deal
|
12 | 75818250 | ths | * in the Software without restriction, including without limitation the rights
|
13 | 75818250 | ths | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | 75818250 | ths | * copies of the Software, and to permit persons to whom the Software is
|
15 | 75818250 | ths | * furnished to do so, subject to the following conditions:
|
16 | 75818250 | ths | *
|
17 | 75818250 | ths | * The above copyright notice and this permission notice shall be included in
|
18 | 75818250 | ths | * all copies or substantial portions of the Software.
|
19 | 75818250 | ths | *
|
20 | 75818250 | ths | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | 75818250 | ths | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | 75818250 | ths | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | 75818250 | ths | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | 75818250 | ths | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | 75818250 | ths | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | 75818250 | ths | * THE SOFTWARE.
|
27 | 75818250 | ths | */
|
28 | 75818250 | ths | |
29 | 75818250 | ths | #include "qemu-common.h" |
30 | 75818250 | ths | #include "nbd.h" |
31 | ab359cd1 | Markus Armbruster | #include "block_int.h" |
32 | 5efa9d5a | Anthony Liguori | #include "module.h" |
33 | 33897dc7 | Nick Thomas | #include "qemu_socket.h" |
34 | 75818250 | ths | |
35 | 75818250 | ths | #include <sys/types.h> |
36 | 75818250 | ths | #include <unistd.h> |
37 | 75818250 | ths | |
38 | 1d45f8b5 | Laurent Vivier | #define EN_OPTSTR ":exportname=" |
39 | 1d45f8b5 | Laurent Vivier | |
40 | 33897dc7 | Nick Thomas | /* #define DEBUG_NBD */
|
41 | 33897dc7 | Nick Thomas | |
42 | 33897dc7 | Nick Thomas | #if defined(DEBUG_NBD)
|
43 | 33897dc7 | Nick Thomas | #define logout(fmt, ...) \
|
44 | 33897dc7 | Nick Thomas | fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__) |
45 | 33897dc7 | Nick Thomas | #else
|
46 | 33897dc7 | Nick Thomas | #define logout(fmt, ...) ((void)0) |
47 | 33897dc7 | Nick Thomas | #endif
|
48 | 33897dc7 | Nick Thomas | |
49 | 75818250 | ths | typedef struct BDRVNBDState { |
50 | 848c66e8 | Paolo Bonzini | CoMutex lock; |
51 | 75818250 | ths | int sock;
|
52 | b90fb4b8 | Paolo Bonzini | uint32_t nbdflags; |
53 | 75818250 | ths | off_t size; |
54 | 75818250 | ths | size_t blocksize; |
55 | 33897dc7 | Nick Thomas | char *export_name; /* An NBD server may export several devices */ |
56 | 33897dc7 | Nick Thomas | |
57 | 33897dc7 | Nick Thomas | /* If it begins with '/', this is a UNIX domain socket. Otherwise,
|
58 | 33897dc7 | Nick Thomas | * it's a string of the form <hostname|ip4|\[ip6\]>:port
|
59 | 33897dc7 | Nick Thomas | */
|
60 | 33897dc7 | Nick Thomas | char *host_spec;
|
61 | 75818250 | ths | } BDRVNBDState; |
62 | 75818250 | ths | |
63 | 33897dc7 | Nick Thomas | static int nbd_config(BDRVNBDState *s, const char *filename, int flags) |
64 | 75818250 | ths | { |
65 | 1d45f8b5 | Laurent Vivier | char *file;
|
66 | 33897dc7 | Nick Thomas | char *export_name;
|
67 | 33897dc7 | Nick Thomas | const char *host_spec; |
68 | 75818250 | ths | const char *unixpath; |
69 | 1d45f8b5 | Laurent Vivier | int err = -EINVAL;
|
70 | 75818250 | ths | |
71 | 7267c094 | Anthony Liguori | file = g_strdup(filename); |
72 | 1d45f8b5 | Laurent Vivier | |
73 | 33897dc7 | Nick Thomas | export_name = strstr(file, EN_OPTSTR); |
74 | 33897dc7 | Nick Thomas | if (export_name) {
|
75 | 33897dc7 | Nick Thomas | if (export_name[strlen(EN_OPTSTR)] == 0) { |
76 | 1d45f8b5 | Laurent Vivier | goto out;
|
77 | 1d45f8b5 | Laurent Vivier | } |
78 | 33897dc7 | Nick Thomas | export_name[0] = 0; /* truncate 'file' */ |
79 | 33897dc7 | Nick Thomas | export_name += strlen(EN_OPTSTR); |
80 | 7267c094 | Anthony Liguori | s->export_name = g_strdup(export_name); |
81 | 1d45f8b5 | Laurent Vivier | } |
82 | 1d45f8b5 | Laurent Vivier | |
83 | 33897dc7 | Nick Thomas | /* extract the host_spec - fail if it's not nbd:... */
|
84 | 33897dc7 | Nick Thomas | if (!strstart(file, "nbd:", &host_spec)) { |
85 | 1d45f8b5 | Laurent Vivier | goto out;
|
86 | 1d45f8b5 | Laurent Vivier | } |
87 | 75818250 | ths | |
88 | 33897dc7 | Nick Thomas | /* are we a UNIX or TCP socket? */
|
89 | 33897dc7 | Nick Thomas | if (strstart(host_spec, "unix:", &unixpath)) { |
90 | 33897dc7 | Nick Thomas | if (unixpath[0] != '/') { /* We demand an absolute path*/ |
91 | 1d45f8b5 | Laurent Vivier | goto out;
|
92 | 1d45f8b5 | Laurent Vivier | } |
93 | 7267c094 | Anthony Liguori | s->host_spec = g_strdup(unixpath); |
94 | 75818250 | ths | } else {
|
95 | 7267c094 | Anthony Liguori | s->host_spec = g_strdup(host_spec); |
96 | 33897dc7 | Nick Thomas | } |
97 | 75818250 | ths | |
98 | 33897dc7 | Nick Thomas | err = 0;
|
99 | 75818250 | ths | |
100 | 33897dc7 | Nick Thomas | out:
|
101 | 7267c094 | Anthony Liguori | g_free(file); |
102 | 33897dc7 | Nick Thomas | if (err != 0) { |
103 | 7267c094 | Anthony Liguori | g_free(s->export_name); |
104 | 7267c094 | Anthony Liguori | g_free(s->host_spec); |
105 | 33897dc7 | Nick Thomas | } |
106 | 33897dc7 | Nick Thomas | return err;
|
107 | 33897dc7 | Nick Thomas | } |
108 | 1d45f8b5 | Laurent Vivier | |
109 | 33897dc7 | Nick Thomas | static int nbd_establish_connection(BlockDriverState *bs) |
110 | 33897dc7 | Nick Thomas | { |
111 | 33897dc7 | Nick Thomas | BDRVNBDState *s = bs->opaque; |
112 | 33897dc7 | Nick Thomas | int sock;
|
113 | 33897dc7 | Nick Thomas | int ret;
|
114 | 33897dc7 | Nick Thomas | off_t size; |
115 | 33897dc7 | Nick Thomas | size_t blocksize; |
116 | 75818250 | ths | |
117 | 33897dc7 | Nick Thomas | if (s->host_spec[0] == '/') { |
118 | 33897dc7 | Nick Thomas | sock = unix_socket_outgoing(s->host_spec); |
119 | 33897dc7 | Nick Thomas | } else {
|
120 | 33897dc7 | Nick Thomas | sock = tcp_socket_outgoing_spec(s->host_spec); |
121 | 75818250 | ths | } |
122 | 75818250 | ths | |
123 | 33897dc7 | Nick Thomas | /* Failed to establish connection */
|
124 | 1d45f8b5 | Laurent Vivier | if (sock == -1) { |
125 | 33897dc7 | Nick Thomas | logout("Failed to establish connection to NBD server\n");
|
126 | 33897dc7 | Nick Thomas | return -errno;
|
127 | 1d45f8b5 | Laurent Vivier | } |
128 | 75818250 | ths | |
129 | 33897dc7 | Nick Thomas | /* NBD handshake */
|
130 | b90fb4b8 | Paolo Bonzini | ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size, |
131 | 33897dc7 | Nick Thomas | &blocksize); |
132 | 1d45f8b5 | Laurent Vivier | if (ret == -1) { |
133 | 33897dc7 | Nick Thomas | logout("Failed to negotiate with the NBD server\n");
|
134 | 33897dc7 | Nick Thomas | closesocket(sock); |
135 | 33897dc7 | Nick Thomas | return -errno;
|
136 | 1d45f8b5 | Laurent Vivier | } |
137 | 75818250 | ths | |
138 | 33897dc7 | Nick Thomas | /* Now that we're connected, set the socket to be non-blocking */
|
139 | 33897dc7 | Nick Thomas | socket_set_nonblock(sock); |
140 | 33897dc7 | Nick Thomas | |
141 | 75818250 | ths | s->sock = sock; |
142 | 75818250 | ths | s->size = size; |
143 | 75818250 | ths | s->blocksize = blocksize; |
144 | 75818250 | ths | |
145 | 33897dc7 | Nick Thomas | logout("Established connection with NBD server\n");
|
146 | 33897dc7 | Nick Thomas | return 0; |
147 | 33897dc7 | Nick Thomas | } |
148 | 33897dc7 | Nick Thomas | |
149 | 33897dc7 | Nick Thomas | static void nbd_teardown_connection(BlockDriverState *bs) |
150 | 33897dc7 | Nick Thomas | { |
151 | 33897dc7 | Nick Thomas | BDRVNBDState *s = bs->opaque; |
152 | 33897dc7 | Nick Thomas | struct nbd_request request;
|
153 | 33897dc7 | Nick Thomas | |
154 | 33897dc7 | Nick Thomas | request.type = NBD_CMD_DISC; |
155 | 33897dc7 | Nick Thomas | request.handle = (uint64_t)(intptr_t)bs; |
156 | 33897dc7 | Nick Thomas | request.from = 0;
|
157 | 33897dc7 | Nick Thomas | request.len = 0;
|
158 | 33897dc7 | Nick Thomas | nbd_send_request(s->sock, &request); |
159 | 33897dc7 | Nick Thomas | |
160 | 33897dc7 | Nick Thomas | closesocket(s->sock); |
161 | 33897dc7 | Nick Thomas | } |
162 | 33897dc7 | Nick Thomas | |
163 | 33897dc7 | Nick Thomas | static int nbd_open(BlockDriverState *bs, const char* filename, int flags) |
164 | 33897dc7 | Nick Thomas | { |
165 | 33897dc7 | Nick Thomas | BDRVNBDState *s = bs->opaque; |
166 | 33897dc7 | Nick Thomas | int result;
|
167 | 33897dc7 | Nick Thomas | |
168 | 33897dc7 | Nick Thomas | /* Pop the config into our state object. Exit if invalid. */
|
169 | 33897dc7 | Nick Thomas | result = nbd_config(s, filename, flags); |
170 | 33897dc7 | Nick Thomas | if (result != 0) { |
171 | 33897dc7 | Nick Thomas | return result;
|
172 | 33897dc7 | Nick Thomas | } |
173 | 33897dc7 | Nick Thomas | |
174 | 33897dc7 | Nick Thomas | /* establish TCP connection, return error if it fails
|
175 | 33897dc7 | Nick Thomas | * TODO: Configurable retry-until-timeout behaviour.
|
176 | 33897dc7 | Nick Thomas | */
|
177 | 33897dc7 | Nick Thomas | result = nbd_establish_connection(bs); |
178 | 33897dc7 | Nick Thomas | |
179 | 848c66e8 | Paolo Bonzini | qemu_co_mutex_init(&s->lock); |
180 | 33897dc7 | Nick Thomas | return result;
|
181 | 75818250 | ths | } |
182 | 75818250 | ths | |
183 | 75818250 | ths | static int nbd_read(BlockDriverState *bs, int64_t sector_num, |
184 | 75818250 | ths | uint8_t *buf, int nb_sectors)
|
185 | 75818250 | ths | { |
186 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
187 | 75818250 | ths | struct nbd_request request;
|
188 | 75818250 | ths | struct nbd_reply reply;
|
189 | 75818250 | ths | |
190 | 75818250 | ths | request.type = NBD_CMD_READ; |
191 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
192 | 3a93113a | Dong Xu Wang | request.from = sector_num * 512;
|
193 | 75818250 | ths | request.len = nb_sectors * 512;
|
194 | 75818250 | ths | |
195 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
196 | 75818250 | ths | return -errno;
|
197 | 75818250 | ths | |
198 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
199 | 75818250 | ths | return -errno;
|
200 | 75818250 | ths | |
201 | 75818250 | ths | if (reply.error !=0) |
202 | 75818250 | ths | return -reply.error;
|
203 | 75818250 | ths | |
204 | 75818250 | ths | if (reply.handle != request.handle)
|
205 | 75818250 | ths | return -EIO;
|
206 | 75818250 | ths | |
207 | 75818250 | ths | if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len) |
208 | 75818250 | ths | return -EIO;
|
209 | 75818250 | ths | |
210 | 75818250 | ths | return 0; |
211 | 75818250 | ths | } |
212 | 75818250 | ths | |
213 | 75818250 | ths | static int nbd_write(BlockDriverState *bs, int64_t sector_num, |
214 | 75818250 | ths | const uint8_t *buf, int nb_sectors) |
215 | 75818250 | ths | { |
216 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
217 | 75818250 | ths | struct nbd_request request;
|
218 | 75818250 | ths | struct nbd_reply reply;
|
219 | 75818250 | ths | |
220 | 75818250 | ths | request.type = NBD_CMD_WRITE; |
221 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
222 | 3a93113a | Dong Xu Wang | request.from = sector_num * 512;
|
223 | 75818250 | ths | request.len = nb_sectors * 512;
|
224 | 75818250 | ths | |
225 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
226 | 75818250 | ths | return -errno;
|
227 | 75818250 | ths | |
228 | 75818250 | ths | if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len) |
229 | 75818250 | ths | return -EIO;
|
230 | 75818250 | ths | |
231 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
232 | 75818250 | ths | return -errno;
|
233 | 75818250 | ths | |
234 | 75818250 | ths | if (reply.error !=0) |
235 | 75818250 | ths | return -reply.error;
|
236 | 75818250 | ths | |
237 | 75818250 | ths | if (reply.handle != request.handle)
|
238 | 75818250 | ths | return -EIO;
|
239 | 75818250 | ths | |
240 | 75818250 | ths | return 0; |
241 | 75818250 | ths | } |
242 | 75818250 | ths | |
243 | 2914caa0 | Paolo Bonzini | static coroutine_fn int nbd_co_read(BlockDriverState *bs, int64_t sector_num, |
244 | 2914caa0 | Paolo Bonzini | uint8_t *buf, int nb_sectors)
|
245 | 2914caa0 | Paolo Bonzini | { |
246 | 2914caa0 | Paolo Bonzini | int ret;
|
247 | 2914caa0 | Paolo Bonzini | BDRVNBDState *s = bs->opaque; |
248 | 2914caa0 | Paolo Bonzini | qemu_co_mutex_lock(&s->lock); |
249 | 2914caa0 | Paolo Bonzini | ret = nbd_read(bs, sector_num, buf, nb_sectors); |
250 | 2914caa0 | Paolo Bonzini | qemu_co_mutex_unlock(&s->lock); |
251 | 2914caa0 | Paolo Bonzini | return ret;
|
252 | 2914caa0 | Paolo Bonzini | } |
253 | 2914caa0 | Paolo Bonzini | |
254 | e183ef75 | Paolo Bonzini | static coroutine_fn int nbd_co_write(BlockDriverState *bs, int64_t sector_num, |
255 | e183ef75 | Paolo Bonzini | const uint8_t *buf, int nb_sectors) |
256 | e183ef75 | Paolo Bonzini | { |
257 | e183ef75 | Paolo Bonzini | int ret;
|
258 | e183ef75 | Paolo Bonzini | BDRVNBDState *s = bs->opaque; |
259 | e183ef75 | Paolo Bonzini | qemu_co_mutex_lock(&s->lock); |
260 | e183ef75 | Paolo Bonzini | ret = nbd_write(bs, sector_num, buf, nb_sectors); |
261 | e183ef75 | Paolo Bonzini | qemu_co_mutex_unlock(&s->lock); |
262 | e183ef75 | Paolo Bonzini | return ret;
|
263 | e183ef75 | Paolo Bonzini | } |
264 | e183ef75 | Paolo Bonzini | |
265 | 75818250 | ths | static void nbd_close(BlockDriverState *bs) |
266 | 75818250 | ths | { |
267 | d2d979c6 | Nick Thomas | BDRVNBDState *s = bs->opaque; |
268 | 7267c094 | Anthony Liguori | g_free(s->export_name); |
269 | 7267c094 | Anthony Liguori | g_free(s->host_spec); |
270 | d2d979c6 | Nick Thomas | |
271 | 33897dc7 | Nick Thomas | nbd_teardown_connection(bs); |
272 | 75818250 | ths | } |
273 | 75818250 | ths | |
274 | 75818250 | ths | static int64_t nbd_getlength(BlockDriverState *bs)
|
275 | 75818250 | ths | { |
276 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
277 | 75818250 | ths | |
278 | 75818250 | ths | return s->size;
|
279 | 75818250 | ths | } |
280 | 75818250 | ths | |
281 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_nbd = {
|
282 | e60f469c | aurel32 | .format_name = "nbd",
|
283 | e60f469c | aurel32 | .instance_size = sizeof(BDRVNBDState),
|
284 | 66f82cee | Kevin Wolf | .bdrv_file_open = nbd_open, |
285 | 2914caa0 | Paolo Bonzini | .bdrv_read = nbd_co_read, |
286 | e183ef75 | Paolo Bonzini | .bdrv_write = nbd_co_write, |
287 | e60f469c | aurel32 | .bdrv_close = nbd_close, |
288 | e60f469c | aurel32 | .bdrv_getlength = nbd_getlength, |
289 | e60f469c | aurel32 | .protocol_name = "nbd",
|
290 | 75818250 | ths | }; |
291 | 5efa9d5a | Anthony Liguori | |
292 | 5efa9d5a | Anthony Liguori | static void bdrv_nbd_init(void) |
293 | 5efa9d5a | Anthony Liguori | { |
294 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_nbd); |
295 | 5efa9d5a | Anthony Liguori | } |
296 | 5efa9d5a | Anthony Liguori | |
297 | 5efa9d5a | Anthony Liguori | block_init(bdrv_nbd_init); |