Fix ATAPI read drive structure command, by Alex Williamson.
[qemu/mini2440/sniper_sniper_test.git] / nbd.c
blobc138d362ef18573a1d8ca946a2996a3ee55d370a
1 /*\
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 \*/
20 #include "nbd.h"
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <ctype.h>
26 #include <inttypes.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
33 extern int verbose;
35 #define LOG(msg, ...) do { \
36 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
37 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
38 } while(0)
40 #define TRACE(msg, ...) do { \
41 if (verbose) LOG(msg, ## __VA_ARGS__); \
42 } while(0)
44 /* This is all part of the "official" NBD API */
46 #define NBD_REQUEST_MAGIC 0x25609513
47 #define NBD_REPLY_MAGIC 0x67446698
49 #define NBD_SET_SOCK _IO(0xab, 0)
50 #define NBD_SET_BLKSIZE _IO(0xab, 1)
51 #define NBD_SET_SIZE _IO(0xab, 2)
52 #define NBD_DO_IT _IO(0xab, 3)
53 #define NBD_CLEAR_SOCK _IO(0xab, 4)
54 #define NBD_CLEAR_QUE _IO(0xab, 5)
55 #define NBD_PRINT_DEBUG _IO(0xab, 6)
56 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
57 #define NBD_DISCONNECT _IO(0xab, 8)
59 /* That's all folks */
61 #define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
62 #define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)
64 static size_t wr_sync(int fd, void *buffer, size_t size, bool do_read)
66 size_t offset = 0;
68 while (offset < size) {
69 ssize_t len;
71 if (do_read) {
72 len = read(fd, buffer + offset, size - offset);
73 } else {
74 len = write(fd, buffer + offset, size - offset);
77 /* recoverable error */
78 if (len == -1 && errno == EAGAIN) {
79 continue;
82 /* eof */
83 if (len == 0) {
84 break;
87 /* unrecoverable error */
88 if (len == -1) {
89 return 0;
92 offset += len;
95 return offset;
98 static int tcp_socket_outgoing(const char *address, uint16_t port)
100 int s;
101 struct in_addr in;
102 struct sockaddr_in addr;
103 int serrno;
105 s = socket(PF_INET, SOCK_STREAM, 0);
106 if (s == -1) {
107 return -1;
110 if (inet_aton(address, &in) == 0) {
111 struct hostent *ent;
113 ent = gethostbyname(address);
114 if (ent == NULL) {
115 goto error;
118 memcpy(&in, ent->h_addr, sizeof(in));
121 addr.sin_family = AF_INET;
122 addr.sin_port = htons(port);
123 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
125 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
126 goto error;
129 return s;
130 error:
131 serrno = errno;
132 close(s);
133 errno = serrno;
134 return -1;
137 int tcp_socket_incoming(const char *address, uint16_t port)
139 int s;
140 struct in_addr in;
141 struct sockaddr_in addr;
142 int serrno;
143 int opt;
145 s = socket(PF_INET, SOCK_STREAM, 0);
146 if (s == -1) {
147 return -1;
150 if (inet_aton(address, &in) == 0) {
151 struct hostent *ent;
153 ent = gethostbyname(address);
154 if (ent == NULL) {
155 goto error;
158 memcpy(&in, ent->h_addr, sizeof(in));
161 addr.sin_family = AF_INET;
162 addr.sin_port = htons(port);
163 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
165 opt = 1;
166 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
167 goto error;
170 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
171 goto error;
174 if (listen(s, 128) == -1) {
175 goto error;
178 return s;
179 error:
180 serrno = errno;
181 close(s);
182 errno = serrno;
183 return -1;
186 /* Basic flow
188 Server Client
190 Negotiate
191 Request
192 Response
193 Request
194 Response
197 Request (type == 2)
200 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
202 char buf[8 + 8 + 8 + 128];
204 /* Negotiate
205 [ 0 .. 7] passwd ("NBDMAGIC")
206 [ 8 .. 15] magic (0x00420281861253)
207 [16 .. 23] size
208 [24 .. 151] reserved (0)
211 TRACE("Beginning negotiation.");
212 memcpy(buf, "NBDMAGIC", 8);
213 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
214 cpu_to_be64w((uint64_t*)(buf + 16), size);
215 memset(buf + 24, 0, 128);
217 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
218 LOG("write failed");
219 errno = EINVAL;
220 return -1;
223 TRACE("Negotation succeeded.");
225 return 0;
228 int nbd_receive_negotiate(int fd, int csock)
230 char buf[8 + 8 + 8 + 128];
231 uint64_t magic;
232 off_t size;
233 size_t blocksize;
235 TRACE("Receiving negotation.");
237 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
238 LOG("read failed");
239 errno = EINVAL;
240 return -1;
243 magic = be64_to_cpup((uint64_t*)(buf + 8));
244 size = be64_to_cpup((uint64_t*)(buf + 16));
245 blocksize = 1024;
247 TRACE("Magic is %c%c%c%c%c%c%c%c",
248 isprint(buf[0]) ? buf[0] : '.',
249 isprint(buf[1]) ? buf[1] : '.',
250 isprint(buf[2]) ? buf[2] : '.',
251 isprint(buf[3]) ? buf[3] : '.',
252 isprint(buf[4]) ? buf[4] : '.',
253 isprint(buf[5]) ? buf[5] : '.',
254 isprint(buf[6]) ? buf[6] : '.',
255 isprint(buf[7]) ? buf[7] : '.');
256 TRACE("Magic is 0x%" PRIx64, magic);
257 TRACE("Size is %" PRIu64, size);
259 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
260 LOG("Invalid magic received");
261 errno = EINVAL;
262 return -1;
265 TRACE("Checking magic");
267 if (magic != 0x00420281861253LL) {
268 LOG("Bad magic received");
269 errno = EINVAL;
270 return -1;
273 TRACE("Setting block size to %lu", (unsigned long)blocksize);
275 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
276 int serrno = errno;
277 LOG("Failed setting NBD block size");
278 errno = serrno;
279 return -1;
282 TRACE("Setting size to %llu block(s)",
283 (unsigned long long)(size / blocksize));
285 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
286 int serrno = errno;
287 LOG("Failed setting size (in blocks)");
288 errno = serrno;
289 return -1;
292 TRACE("Clearing NBD socket");
294 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
295 int serrno = errno;
296 LOG("Failed clearing NBD socket");
297 errno = serrno;
298 return -1;
301 TRACE("Setting NBD socket");
303 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
304 int serrno = errno;
305 LOG("Failed to set NBD socket");
306 errno = serrno;
307 return -1;
310 TRACE("Negotiation ended");
312 return 0;
315 int nbd_disconnect(int fd)
317 ioctl(fd, NBD_CLEAR_QUE);
318 ioctl(fd, NBD_DISCONNECT);
319 ioctl(fd, NBD_CLEAR_SOCK);
320 return 0;
323 int nbd_client(int fd, int csock)
325 int ret;
326 int serrno;
328 TRACE("Doing NBD loop");
330 ret = ioctl(fd, NBD_DO_IT);
331 serrno = errno;
333 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
335 TRACE("Clearing NBD queue");
336 ioctl(fd, NBD_CLEAR_QUE);
338 TRACE("Clearing NBD socket");
339 ioctl(fd, NBD_CLEAR_SOCK);
341 errno = serrno;
342 return ret;
345 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, off_t *offset, bool readonly)
347 #ifndef _REENTRANT
348 static uint8_t data[1024 * 1024]; // keep this off of the stack
349 #else
350 uint8_t data[1024 * 1024];
351 #endif
352 uint8_t buf[4 + 4 + 8 + 8 + 4];
353 uint32_t magic;
354 uint32_t type;
355 uint64_t from;
356 uint32_t len;
358 TRACE("Reading request.");
360 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
361 LOG("read failed");
362 errno = EINVAL;
363 return -1;
366 /* Request
367 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
368 [ 4 .. 7] type (0 == READ, 1 == WRITE)
369 [ 8 .. 15] handle
370 [16 .. 23] from
371 [24 .. 27] len
374 magic = be32_to_cpup((uint32_t*)buf);
375 type = be32_to_cpup((uint32_t*)(buf + 4));
376 from = be64_to_cpup((uint64_t*)(buf + 16));
377 len = be32_to_cpup((uint32_t*)(buf + 24));
379 TRACE("Got request: "
380 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
381 magic, type, from, len);
384 if (magic != NBD_REQUEST_MAGIC) {
385 LOG("invalid magic (got 0x%x)", magic);
386 errno = EINVAL;
387 return -1;
390 if (len > sizeof(data)) {
391 LOG("len (%u) is larger than max len (%lu)",
392 len, (unsigned long)sizeof(data));
393 errno = EINVAL;
394 return -1;
397 if ((from + len) < from) {
398 LOG("integer overflow detected! "
399 "you're probably being attacked");
400 errno = EINVAL;
401 return -1;
404 if ((from + len) > size) {
405 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
406 ", Offset: %" PRIu64 "\n",
407 from, len, size, dev_offset);
408 LOG("requested operation past EOF--bad client?");
409 errno = EINVAL;
410 return -1;
413 /* Reply
414 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
415 [ 4 .. 7] error (0 == no error)
416 [ 7 .. 15] handle
418 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
419 cpu_to_be32w((uint32_t*)(buf + 4), 0);
421 TRACE("Decoding type");
423 switch (type) {
424 case 0:
425 TRACE("Request type is READ");
427 if (bdrv_read(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
428 LOG("reading from file failed");
429 errno = EINVAL;
430 return -1;
432 *offset += len;
434 TRACE("Read %u byte(s)", len);
436 TRACE("Sending OK response");
438 if (write_sync(csock, buf, 16) != 16) {
439 LOG("writing to socket failed");
440 errno = EINVAL;
441 return -1;
444 TRACE("Sending data to client");
446 if (write_sync(csock, data, len) != len) {
447 LOG("writing to socket failed");
448 errno = EINVAL;
449 return -1;
451 break;
452 case 1:
453 TRACE("Request type is WRITE");
455 TRACE("Reading %u byte(s)", len);
457 if (read_sync(csock, data, len) != len) {
458 LOG("reading from socket failed");
459 errno = EINVAL;
460 return -1;
463 if (readonly) {
464 TRACE("Server is read-only, return error");
466 cpu_to_be32w((uint32_t*)(buf + 4), 1);
467 } else {
468 TRACE("Writing to device");
470 if (bdrv_write(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
471 LOG("writing to file failed");
472 errno = EINVAL;
473 return -1;
476 *offset += len;
479 TRACE("Sending response to client");
481 if (write_sync(csock, buf, 16) != 16) {
482 LOG("writing to socket failed");
483 errno = EINVAL;
484 return -1;
486 break;
487 case 2:
488 TRACE("Request type is DISCONNECT");
489 errno = 0;
490 return 1;
491 default:
492 LOG("invalid request type (%u) received", type);
493 errno = EINVAL;
494 return -1;
497 TRACE("Request/Reply complete");
499 return 0;