]>
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; | |
79 | uint16_t tmp; | |
80 | int rc; | |
81 | ||
82 | TRACE("Receiving negotiation."); | |
83 | ||
84 | rc = -EINVAL; | |
85 | ||
1c778ef7 | 86 | if (read_sync(ioc, buf, 8) != 8) { |
798bfe00 FZ |
87 | error_setg(errp, "Failed to read data"); |
88 | goto fail; | |
89 | } | |
90 | ||
91 | buf[8] = '\0'; | |
92 | if (strlen(buf) == 0) { | |
93 | error_setg(errp, "Server connection closed unexpectedly"); | |
94 | goto fail; | |
95 | } | |
96 | ||
97 | TRACE("Magic is %c%c%c%c%c%c%c%c", | |
98 | qemu_isprint(buf[0]) ? buf[0] : '.', | |
99 | qemu_isprint(buf[1]) ? buf[1] : '.', | |
100 | qemu_isprint(buf[2]) ? buf[2] : '.', | |
101 | qemu_isprint(buf[3]) ? buf[3] : '.', | |
102 | qemu_isprint(buf[4]) ? buf[4] : '.', | |
103 | qemu_isprint(buf[5]) ? buf[5] : '.', | |
104 | qemu_isprint(buf[6]) ? buf[6] : '.', | |
105 | qemu_isprint(buf[7]) ? buf[7] : '.'); | |
106 | ||
107 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
108 | error_setg(errp, "Invalid magic received"); | |
109 | goto fail; | |
110 | } | |
111 | ||
1c778ef7 | 112 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
113 | error_setg(errp, "Failed to read magic"); |
114 | goto fail; | |
115 | } | |
116 | magic = be64_to_cpu(magic); | |
117 | TRACE("Magic is 0x%" PRIx64, magic); | |
118 | ||
f72d705f | 119 | if (magic == NBD_OPTS_MAGIC) { |
798bfe00 FZ |
120 | uint32_t reserved = 0; |
121 | uint32_t opt; | |
122 | uint32_t namesize; | |
123 | ||
1c778ef7 | 124 | if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { |
798bfe00 FZ |
125 | error_setg(errp, "Failed to read server flags"); |
126 | goto fail; | |
127 | } | |
128 | *flags = be16_to_cpu(tmp) << 16; | |
129 | /* reserved for future use */ | |
1c778ef7 | 130 | if (write_sync(ioc, &reserved, sizeof(reserved)) != |
798bfe00 FZ |
131 | sizeof(reserved)) { |
132 | error_setg(errp, "Failed to read reserved field"); | |
133 | goto fail; | |
134 | } | |
135 | /* write the export name */ | |
f72d705f DB |
136 | if (!name) { |
137 | error_setg(errp, "Server requires an export name"); | |
138 | goto fail; | |
139 | } | |
798bfe00 | 140 | magic = cpu_to_be64(magic); |
1c778ef7 | 141 | if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
142 | error_setg(errp, "Failed to send export name magic"); |
143 | goto fail; | |
144 | } | |
145 | opt = cpu_to_be32(NBD_OPT_EXPORT_NAME); | |
1c778ef7 | 146 | if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) { |
798bfe00 FZ |
147 | error_setg(errp, "Failed to send export name option number"); |
148 | goto fail; | |
149 | } | |
150 | namesize = cpu_to_be32(strlen(name)); | |
1c778ef7 | 151 | if (write_sync(ioc, &namesize, sizeof(namesize)) != |
798bfe00 FZ |
152 | sizeof(namesize)) { |
153 | error_setg(errp, "Failed to send export name length"); | |
154 | goto fail; | |
155 | } | |
1c778ef7 | 156 | if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) { |
798bfe00 FZ |
157 | error_setg(errp, "Failed to send export name"); |
158 | goto fail; | |
159 | } | |
f72d705f DB |
160 | |
161 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
162 | error_setg(errp, "Failed to read export length"); | |
798bfe00 FZ |
163 | goto fail; |
164 | } | |
f72d705f DB |
165 | *size = be64_to_cpu(s); |
166 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 167 | |
f72d705f DB |
168 | if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { |
169 | error_setg(errp, "Failed to read export flags"); | |
170 | goto fail; | |
171 | } | |
172 | *flags |= be16_to_cpu(tmp); | |
173 | } else if (magic == NBD_CLIENT_MAGIC) { | |
174 | if (name) { | |
175 | error_setg(errp, "Server does not support export names"); | |
176 | goto fail; | |
177 | } | |
178 | ||
179 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
180 | error_setg(errp, "Failed to read export length"); | |
181 | goto fail; | |
182 | } | |
183 | *size = be64_to_cpu(s); | |
184 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 185 | |
1c778ef7 | 186 | if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { |
798bfe00 FZ |
187 | error_setg(errp, "Failed to read export flags"); |
188 | goto fail; | |
189 | } | |
190 | *flags = be32_to_cpup(flags); | |
191 | } else { | |
f72d705f DB |
192 | error_setg(errp, "Bad magic received"); |
193 | goto fail; | |
798bfe00 | 194 | } |
f72d705f | 195 | |
1c778ef7 | 196 | if (read_sync(ioc, &buf, 124) != 124) { |
798bfe00 FZ |
197 | error_setg(errp, "Failed to read reserved block"); |
198 | goto fail; | |
199 | } | |
200 | rc = 0; | |
201 | ||
202 | fail: | |
203 | return rc; | |
204 | } | |
205 | ||
206 | #ifdef __linux__ | |
1c778ef7 | 207 | int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size) |
798bfe00 FZ |
208 | { |
209 | TRACE("Setting NBD socket"); | |
210 | ||
1c778ef7 | 211 | if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) { |
798bfe00 FZ |
212 | int serrno = errno; |
213 | LOG("Failed to set NBD socket"); | |
214 | return -serrno; | |
215 | } | |
216 | ||
217 | TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE); | |
218 | ||
219 | if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) { | |
220 | int serrno = errno; | |
221 | LOG("Failed setting NBD block size"); | |
222 | return -serrno; | |
223 | } | |
224 | ||
225 | TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE)); | |
226 | ||
227 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) { | |
228 | int serrno = errno; | |
229 | LOG("Failed setting size (in blocks)"); | |
230 | return -serrno; | |
231 | } | |
232 | ||
233 | if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) { | |
234 | if (errno == ENOTTY) { | |
235 | int read_only = (flags & NBD_FLAG_READ_ONLY) != 0; | |
236 | TRACE("Setting readonly attribute"); | |
237 | ||
238 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
239 | int serrno = errno; | |
240 | LOG("Failed setting read-only attribute"); | |
241 | return -serrno; | |
242 | } | |
243 | } else { | |
244 | int serrno = errno; | |
245 | LOG("Failed setting flags"); | |
246 | return -serrno; | |
247 | } | |
248 | } | |
249 | ||
250 | TRACE("Negotiation ended"); | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
255 | int nbd_client(int fd) | |
256 | { | |
257 | int ret; | |
258 | int serrno; | |
259 | ||
260 | TRACE("Doing NBD loop"); | |
261 | ||
262 | ret = ioctl(fd, NBD_DO_IT); | |
263 | if (ret < 0 && errno == EPIPE) { | |
264 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
265 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
266 | * that case. | |
267 | */ | |
268 | ret = 0; | |
269 | } | |
270 | serrno = errno; | |
271 | ||
272 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); | |
273 | ||
274 | TRACE("Clearing NBD queue"); | |
275 | ioctl(fd, NBD_CLEAR_QUE); | |
276 | ||
277 | TRACE("Clearing NBD socket"); | |
278 | ioctl(fd, NBD_CLEAR_SOCK); | |
279 | ||
280 | errno = serrno; | |
281 | return ret; | |
282 | } | |
283 | #else | |
1c778ef7 | 284 | int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size) |
798bfe00 FZ |
285 | { |
286 | return -ENOTSUP; | |
287 | } | |
288 | ||
289 | int nbd_client(int fd) | |
290 | { | |
291 | return -ENOTSUP; | |
292 | } | |
293 | #endif | |
294 | ||
1c778ef7 | 295 | ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request) |
798bfe00 FZ |
296 | { |
297 | uint8_t buf[NBD_REQUEST_SIZE]; | |
298 | ssize_t ret; | |
299 | ||
300 | cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC); | |
301 | cpu_to_be32w((uint32_t*)(buf + 4), request->type); | |
302 | cpu_to_be64w((uint64_t*)(buf + 8), request->handle); | |
303 | cpu_to_be64w((uint64_t*)(buf + 16), request->from); | |
304 | cpu_to_be32w((uint32_t*)(buf + 24), request->len); | |
305 | ||
306 | TRACE("Sending request to client: " | |
307 | "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}", | |
308 | request->from, request->len, request->handle, request->type); | |
309 | ||
1c778ef7 | 310 | ret = write_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
311 | if (ret < 0) { |
312 | return ret; | |
313 | } | |
314 | ||
315 | if (ret != sizeof(buf)) { | |
316 | LOG("writing to socket failed"); | |
317 | return -EINVAL; | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
1c778ef7 | 322 | ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply) |
798bfe00 FZ |
323 | { |
324 | uint8_t buf[NBD_REPLY_SIZE]; | |
325 | uint32_t magic; | |
326 | ssize_t ret; | |
327 | ||
1c778ef7 | 328 | ret = read_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
329 | if (ret < 0) { |
330 | return ret; | |
331 | } | |
332 | ||
333 | if (ret != sizeof(buf)) { | |
334 | LOG("read failed"); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | /* Reply | |
339 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
340 | [ 4 .. 7] error (0 == no error) | |
341 | [ 7 .. 15] handle | |
342 | */ | |
343 | ||
344 | magic = be32_to_cpup((uint32_t*)buf); | |
345 | reply->error = be32_to_cpup((uint32_t*)(buf + 4)); | |
346 | reply->handle = be64_to_cpup((uint64_t*)(buf + 8)); | |
347 | ||
348 | reply->error = nbd_errno_to_system_errno(reply->error); | |
349 | ||
350 | TRACE("Got reply: " | |
351 | "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }", | |
352 | magic, reply->error, reply->handle); | |
353 | ||
354 | if (magic != NBD_REPLY_MAGIC) { | |
355 | LOG("invalid magic (got 0x%x)", magic); | |
356 | return -EINVAL; | |
357 | } | |
358 | return 0; | |
359 | } | |
360 |