]>
Commit | Line | Data |
---|---|---|
798bfe00 FZ |
1 | /* |
2 | * Copyright (C) 2005 Anthony Liguori <[email protected]> | |
3 | * | |
4 | * Network Block Device Client Side | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
d38ea87a | 19 | #include "qemu/osdep.h" |
798bfe00 FZ |
20 | #include "nbd-internal.h" |
21 | ||
22 | static int nbd_errno_to_system_errno(int err) | |
23 | { | |
24 | switch (err) { | |
25 | case NBD_SUCCESS: | |
26 | return 0; | |
27 | case NBD_EPERM: | |
28 | return EPERM; | |
29 | case NBD_EIO: | |
30 | return EIO; | |
31 | case NBD_ENOMEM: | |
32 | return ENOMEM; | |
33 | case NBD_ENOSPC: | |
34 | return ENOSPC; | |
35 | case NBD_EINVAL: | |
36 | default: | |
37 | return EINVAL; | |
38 | } | |
39 | } | |
40 | ||
41 | /* Definitions for opaque data types */ | |
42 | ||
43 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); | |
44 | ||
45 | /* That's all folks */ | |
46 | ||
47 | /* Basic flow for negotiation | |
48 | ||
49 | Server Client | |
50 | Negotiate | |
51 | ||
52 | or | |
53 | ||
54 | Server Client | |
55 | Negotiate #1 | |
56 | Option | |
57 | Negotiate #2 | |
58 | ||
59 | ---- | |
60 | ||
61 | followed by | |
62 | ||
63 | Server Client | |
64 | Request | |
65 | Response | |
66 | Request | |
67 | Response | |
68 | ... | |
69 | ... | |
70 | Request (type == 2) | |
71 | ||
72 | */ | |
73 | ||
1c778ef7 | 74 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, |
798bfe00 FZ |
75 | off_t *size, Error **errp) |
76 | { | |
77 | char buf[256]; | |
78 | uint64_t magic, s; | |
798bfe00 FZ |
79 | int rc; |
80 | ||
81 | TRACE("Receiving negotiation."); | |
82 | ||
83 | rc = -EINVAL; | |
84 | ||
1c778ef7 | 85 | if (read_sync(ioc, buf, 8) != 8) { |
798bfe00 FZ |
86 | error_setg(errp, "Failed to read data"); |
87 | goto fail; | |
88 | } | |
89 | ||
90 | buf[8] = '\0'; | |
91 | if (strlen(buf) == 0) { | |
92 | error_setg(errp, "Server connection closed unexpectedly"); | |
93 | goto fail; | |
94 | } | |
95 | ||
96 | TRACE("Magic is %c%c%c%c%c%c%c%c", | |
97 | qemu_isprint(buf[0]) ? buf[0] : '.', | |
98 | qemu_isprint(buf[1]) ? buf[1] : '.', | |
99 | qemu_isprint(buf[2]) ? buf[2] : '.', | |
100 | qemu_isprint(buf[3]) ? buf[3] : '.', | |
101 | qemu_isprint(buf[4]) ? buf[4] : '.', | |
102 | qemu_isprint(buf[5]) ? buf[5] : '.', | |
103 | qemu_isprint(buf[6]) ? buf[6] : '.', | |
104 | qemu_isprint(buf[7]) ? buf[7] : '.'); | |
105 | ||
106 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
107 | error_setg(errp, "Invalid magic received"); | |
108 | goto fail; | |
109 | } | |
110 | ||
1c778ef7 | 111 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
112 | error_setg(errp, "Failed to read magic"); |
113 | goto fail; | |
114 | } | |
115 | magic = be64_to_cpu(magic); | |
116 | TRACE("Magic is 0x%" PRIx64, magic); | |
117 | ||
f72d705f | 118 | if (magic == NBD_OPTS_MAGIC) { |
e2a9d9a3 | 119 | uint32_t clientflags = 0; |
798bfe00 FZ |
120 | uint32_t opt; |
121 | uint32_t namesize; | |
e2a9d9a3 DB |
122 | uint16_t globalflags; |
123 | uint16_t exportflags; | |
798bfe00 | 124 | |
e2a9d9a3 DB |
125 | if (read_sync(ioc, &globalflags, sizeof(globalflags)) != |
126 | sizeof(globalflags)) { | |
798bfe00 FZ |
127 | error_setg(errp, "Failed to read server flags"); |
128 | goto fail; | |
129 | } | |
e2a9d9a3 DB |
130 | *flags = be16_to_cpu(globalflags) << 16; |
131 | if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { | |
132 | TRACE("Server supports fixed new style"); | |
133 | clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; | |
134 | } | |
135 | /* client requested flags */ | |
136 | if (write_sync(ioc, &clientflags, sizeof(clientflags)) != | |
137 | sizeof(clientflags)) { | |
138 | error_setg(errp, "Failed to send clientflags field"); | |
798bfe00 FZ |
139 | goto fail; |
140 | } | |
141 | /* write the export name */ | |
f72d705f DB |
142 | if (!name) { |
143 | error_setg(errp, "Server requires an export name"); | |
144 | goto fail; | |
145 | } | |
798bfe00 | 146 | magic = cpu_to_be64(magic); |
1c778ef7 | 147 | if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
148 | error_setg(errp, "Failed to send export name magic"); |
149 | goto fail; | |
150 | } | |
151 | opt = cpu_to_be32(NBD_OPT_EXPORT_NAME); | |
1c778ef7 | 152 | if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) { |
798bfe00 FZ |
153 | error_setg(errp, "Failed to send export name option number"); |
154 | goto fail; | |
155 | } | |
156 | namesize = cpu_to_be32(strlen(name)); | |
1c778ef7 | 157 | if (write_sync(ioc, &namesize, sizeof(namesize)) != |
798bfe00 FZ |
158 | sizeof(namesize)) { |
159 | error_setg(errp, "Failed to send export name length"); | |
160 | goto fail; | |
161 | } | |
1c778ef7 | 162 | if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) { |
798bfe00 FZ |
163 | error_setg(errp, "Failed to send export name"); |
164 | goto fail; | |
165 | } | |
f72d705f DB |
166 | |
167 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
168 | error_setg(errp, "Failed to read export length"); | |
798bfe00 FZ |
169 | goto fail; |
170 | } | |
f72d705f DB |
171 | *size = be64_to_cpu(s); |
172 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 173 | |
e2a9d9a3 DB |
174 | if (read_sync(ioc, &exportflags, sizeof(exportflags)) != |
175 | sizeof(exportflags)) { | |
f72d705f DB |
176 | error_setg(errp, "Failed to read export flags"); |
177 | goto fail; | |
178 | } | |
e2a9d9a3 | 179 | *flags |= be16_to_cpu(exportflags); |
f72d705f DB |
180 | } else if (magic == NBD_CLIENT_MAGIC) { |
181 | if (name) { | |
182 | error_setg(errp, "Server does not support export names"); | |
183 | goto fail; | |
184 | } | |
185 | ||
186 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
187 | error_setg(errp, "Failed to read export length"); | |
188 | goto fail; | |
189 | } | |
190 | *size = be64_to_cpu(s); | |
191 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 192 | |
1c778ef7 | 193 | if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { |
798bfe00 FZ |
194 | error_setg(errp, "Failed to read export flags"); |
195 | goto fail; | |
196 | } | |
197 | *flags = be32_to_cpup(flags); | |
198 | } else { | |
f72d705f DB |
199 | error_setg(errp, "Bad magic received"); |
200 | goto fail; | |
798bfe00 | 201 | } |
f72d705f | 202 | |
1c778ef7 | 203 | if (read_sync(ioc, &buf, 124) != 124) { |
798bfe00 FZ |
204 | error_setg(errp, "Failed to read reserved block"); |
205 | goto fail; | |
206 | } | |
207 | rc = 0; | |
208 | ||
209 | fail: | |
210 | return rc; | |
211 | } | |
212 | ||
213 | #ifdef __linux__ | |
1c778ef7 | 214 | int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size) |
798bfe00 FZ |
215 | { |
216 | TRACE("Setting NBD socket"); | |
217 | ||
1c778ef7 | 218 | if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) { |
798bfe00 FZ |
219 | int serrno = errno; |
220 | LOG("Failed to set NBD socket"); | |
221 | return -serrno; | |
222 | } | |
223 | ||
224 | TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE); | |
225 | ||
226 | if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) { | |
227 | int serrno = errno; | |
228 | LOG("Failed setting NBD block size"); | |
229 | return -serrno; | |
230 | } | |
231 | ||
232 | TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE)); | |
233 | ||
234 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) { | |
235 | int serrno = errno; | |
236 | LOG("Failed setting size (in blocks)"); | |
237 | return -serrno; | |
238 | } | |
239 | ||
240 | if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) { | |
241 | if (errno == ENOTTY) { | |
242 | int read_only = (flags & NBD_FLAG_READ_ONLY) != 0; | |
243 | TRACE("Setting readonly attribute"); | |
244 | ||
245 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
246 | int serrno = errno; | |
247 | LOG("Failed setting read-only attribute"); | |
248 | return -serrno; | |
249 | } | |
250 | } else { | |
251 | int serrno = errno; | |
252 | LOG("Failed setting flags"); | |
253 | return -serrno; | |
254 | } | |
255 | } | |
256 | ||
257 | TRACE("Negotiation ended"); | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | int nbd_client(int fd) | |
263 | { | |
264 | int ret; | |
265 | int serrno; | |
266 | ||
267 | TRACE("Doing NBD loop"); | |
268 | ||
269 | ret = ioctl(fd, NBD_DO_IT); | |
270 | if (ret < 0 && errno == EPIPE) { | |
271 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
272 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
273 | * that case. | |
274 | */ | |
275 | ret = 0; | |
276 | } | |
277 | serrno = errno; | |
278 | ||
279 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); | |
280 | ||
281 | TRACE("Clearing NBD queue"); | |
282 | ioctl(fd, NBD_CLEAR_QUE); | |
283 | ||
284 | TRACE("Clearing NBD socket"); | |
285 | ioctl(fd, NBD_CLEAR_SOCK); | |
286 | ||
287 | errno = serrno; | |
288 | return ret; | |
289 | } | |
290 | #else | |
1c778ef7 | 291 | int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size) |
798bfe00 FZ |
292 | { |
293 | return -ENOTSUP; | |
294 | } | |
295 | ||
296 | int nbd_client(int fd) | |
297 | { | |
298 | return -ENOTSUP; | |
299 | } | |
300 | #endif | |
301 | ||
1c778ef7 | 302 | ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request) |
798bfe00 FZ |
303 | { |
304 | uint8_t buf[NBD_REQUEST_SIZE]; | |
305 | ssize_t ret; | |
306 | ||
307 | cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC); | |
308 | cpu_to_be32w((uint32_t*)(buf + 4), request->type); | |
309 | cpu_to_be64w((uint64_t*)(buf + 8), request->handle); | |
310 | cpu_to_be64w((uint64_t*)(buf + 16), request->from); | |
311 | cpu_to_be32w((uint32_t*)(buf + 24), request->len); | |
312 | ||
313 | TRACE("Sending request to client: " | |
314 | "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}", | |
315 | request->from, request->len, request->handle, request->type); | |
316 | ||
1c778ef7 | 317 | ret = write_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
318 | if (ret < 0) { |
319 | return ret; | |
320 | } | |
321 | ||
322 | if (ret != sizeof(buf)) { | |
323 | LOG("writing to socket failed"); | |
324 | return -EINVAL; | |
325 | } | |
326 | return 0; | |
327 | } | |
328 | ||
1c778ef7 | 329 | ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply) |
798bfe00 FZ |
330 | { |
331 | uint8_t buf[NBD_REPLY_SIZE]; | |
332 | uint32_t magic; | |
333 | ssize_t ret; | |
334 | ||
1c778ef7 | 335 | ret = read_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
336 | if (ret < 0) { |
337 | return ret; | |
338 | } | |
339 | ||
340 | if (ret != sizeof(buf)) { | |
341 | LOG("read failed"); | |
342 | return -EINVAL; | |
343 | } | |
344 | ||
345 | /* Reply | |
346 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
347 | [ 4 .. 7] error (0 == no error) | |
348 | [ 7 .. 15] handle | |
349 | */ | |
350 | ||
351 | magic = be32_to_cpup((uint32_t*)buf); | |
352 | reply->error = be32_to_cpup((uint32_t*)(buf + 4)); | |
353 | reply->handle = be64_to_cpup((uint64_t*)(buf + 8)); | |
354 | ||
355 | reply->error = nbd_errno_to_system_errno(reply->error); | |
356 | ||
357 | TRACE("Got reply: " | |
358 | "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }", | |
359 | magic, reply->error, reply->handle); | |
360 | ||
361 | if (magic != NBD_REPLY_MAGIC) { | |
362 | LOG("invalid magic (got 0x%x)", magic); | |
363 | return -EINVAL; | |
364 | } | |
365 | return 0; | |
366 | } | |
367 |