ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 1 | /* |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws> |
| 3 | * |
| 4 | * Network Block Device |
| 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 17 | */ |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 18 | |
| 19 | #include "nbd.h" |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 23 | #ifndef _WIN32 |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 25 | #endif |
Andreas Färber | 5dc2eec | 2010-09-20 00:50:46 +0200 | [diff] [blame] | 26 | #if defined(__sun__) || defined(__HAIKU__) |
aliguori | 7e00eb9 | 2008-08-02 01:57:02 +0000 | [diff] [blame] | 27 | #include <sys/ioccom.h> |
| 28 | #endif |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
| 30 | #include <inttypes.h> |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 31 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 32 | #include "qemu_socket.h" |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 33 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 34 | //#define DEBUG_NBD |
| 35 | |
| 36 | #ifdef DEBUG_NBD |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 37 | #define TRACE(msg, ...) do { \ |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 38 | LOG(msg, ## __VA_ARGS__); \ |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 39 | } while(0) |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 40 | #else |
| 41 | #define TRACE(msg, ...) \ |
| 42 | do { } while (0) |
| 43 | #endif |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 44 | |
| 45 | #define LOG(msg, ...) do { \ |
| 46 | fprintf(stderr, "%s:%s():L%d: " msg "\n", \ |
| 47 | __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \ |
| 48 | } while(0) |
| 49 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 50 | /* This is all part of the "official" NBD API */ |
| 51 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 52 | #define NBD_REPLY_SIZE (4 + 4 + 8) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 53 | #define NBD_REQUEST_MAGIC 0x25609513 |
| 54 | #define NBD_REPLY_MAGIC 0x67446698 |
| 55 | |
| 56 | #define NBD_SET_SOCK _IO(0xab, 0) |
| 57 | #define NBD_SET_BLKSIZE _IO(0xab, 1) |
| 58 | #define NBD_SET_SIZE _IO(0xab, 2) |
| 59 | #define NBD_DO_IT _IO(0xab, 3) |
| 60 | #define NBD_CLEAR_SOCK _IO(0xab, 4) |
| 61 | #define NBD_CLEAR_QUE _IO(0xab, 5) |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 62 | #define NBD_PRINT_DEBUG _IO(0xab, 6) |
| 63 | #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 64 | #define NBD_DISCONNECT _IO(0xab, 8) |
| 65 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 66 | #define NBD_OPT_EXPORT_NAME (1 << 0) |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 67 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 68 | /* That's all folks */ |
| 69 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 70 | #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true) |
| 71 | #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 72 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 73 | size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 74 | { |
| 75 | size_t offset = 0; |
| 76 | |
| 77 | while (offset < size) { |
| 78 | ssize_t len; |
| 79 | |
| 80 | if (do_read) { |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 81 | len = recv(fd, buffer + offset, size - offset, 0); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 82 | } else { |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 83 | len = send(fd, buffer + offset, size - offset, 0); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 84 | } |
| 85 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 86 | if (len == -1) |
| 87 | errno = socket_error(); |
| 88 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 89 | /* recoverable error */ |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 90 | if (len == -1 && (errno == EAGAIN || errno == EINTR)) { |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
| 94 | /* eof */ |
| 95 | if (len == 0) { |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | /* unrecoverable error */ |
| 100 | if (len == -1) { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | offset += len; |
| 105 | } |
| 106 | |
| 107 | return offset; |
| 108 | } |
| 109 | |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 110 | static void combine_addr(char *buf, size_t len, const char* address, |
| 111 | uint16_t port) |
| 112 | { |
| 113 | /* If the address-part contains a colon, it's an IPv6 IP so needs [] */ |
| 114 | if (strstr(address, ":")) { |
| 115 | snprintf(buf, len, "[%s]:%u", address, port); |
| 116 | } else { |
| 117 | snprintf(buf, len, "%s:%u", address, port); |
| 118 | } |
| 119 | } |
| 120 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 121 | int tcp_socket_outgoing(const char *address, uint16_t port) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 122 | { |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 123 | char address_and_port[128]; |
| 124 | combine_addr(address_and_port, 128, address, port); |
| 125 | return tcp_socket_outgoing_spec(address_and_port); |
| 126 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 127 | |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 128 | int tcp_socket_outgoing_spec(const char *address_and_port) |
| 129 | { |
| 130 | return inet_connect(address_and_port, SOCK_STREAM); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int tcp_socket_incoming(const char *address, uint16_t port) |
| 134 | { |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 135 | char address_and_port[128]; |
| 136 | combine_addr(address_and_port, 128, address, port); |
| 137 | return tcp_socket_incoming_spec(address_and_port); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 140 | int tcp_socket_incoming_spec(const char *address_and_port) |
| 141 | { |
| 142 | char *ostr = NULL; |
| 143 | int olen = 0; |
| 144 | return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0); |
| 145 | } |
| 146 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 147 | int unix_socket_incoming(const char *path) |
| 148 | { |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 149 | char *ostr = NULL; |
| 150 | int olen = 0; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 151 | |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 152 | return unix_listen(path, ostr, olen); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int unix_socket_outgoing(const char *path) |
| 156 | { |
Nick Thomas | c12504c | 2011-02-22 15:44:53 +0000 | [diff] [blame] | 157 | return unix_connect(path); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 158 | } |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 159 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 160 | /* Basic flow |
| 161 | |
| 162 | Server Client |
| 163 | |
| 164 | Negotiate |
| 165 | Request |
| 166 | Response |
| 167 | Request |
| 168 | Response |
| 169 | ... |
| 170 | ... |
| 171 | Request (type == 2) |
| 172 | */ |
| 173 | |
aliguori | 2798266 | 2008-09-10 15:23:19 +0000 | [diff] [blame] | 174 | int nbd_negotiate(int csock, off_t size) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 175 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 176 | char buf[8 + 8 + 8 + 128]; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 177 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 178 | /* Negotiate |
| 179 | [ 0 .. 7] passwd ("NBDMAGIC") |
| 180 | [ 8 .. 15] magic (0x00420281861253) |
| 181 | [16 .. 23] size |
| 182 | [24 .. 151] reserved (0) |
| 183 | */ |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 184 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 185 | TRACE("Beginning negotiation."); |
| 186 | memcpy(buf, "NBDMAGIC", 8); |
| 187 | cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL); |
| 188 | cpu_to_be64w((uint64_t*)(buf + 16), size); |
| 189 | memset(buf + 24, 0, 128); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 190 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 191 | if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { |
| 192 | LOG("write failed"); |
| 193 | errno = EINVAL; |
| 194 | return -1; |
| 195 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 196 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 197 | TRACE("Negotation succeeded."); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 198 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 199 | return 0; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 202 | int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, |
| 203 | off_t *size, size_t *blocksize) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 204 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 205 | char buf[256]; |
| 206 | uint64_t magic, s; |
| 207 | uint16_t tmp; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 208 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 209 | TRACE("Receiving negotation."); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 210 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 211 | if (read_sync(csock, buf, 8) != 8) { |
| 212 | LOG("read failed"); |
| 213 | errno = EINVAL; |
| 214 | return -1; |
| 215 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 216 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 217 | buf[8] = '\0'; |
| 218 | if (strlen(buf) == 0) { |
| 219 | LOG("server connection closed"); |
| 220 | errno = EINVAL; |
| 221 | return -1; |
| 222 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 223 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 224 | TRACE("Magic is %c%c%c%c%c%c%c%c", |
| 225 | qemu_isprint(buf[0]) ? buf[0] : '.', |
| 226 | qemu_isprint(buf[1]) ? buf[1] : '.', |
| 227 | qemu_isprint(buf[2]) ? buf[2] : '.', |
| 228 | qemu_isprint(buf[3]) ? buf[3] : '.', |
| 229 | qemu_isprint(buf[4]) ? buf[4] : '.', |
| 230 | qemu_isprint(buf[5]) ? buf[5] : '.', |
| 231 | qemu_isprint(buf[6]) ? buf[6] : '.', |
| 232 | qemu_isprint(buf[7]) ? buf[7] : '.'); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 233 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 234 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { |
| 235 | LOG("Invalid magic received"); |
| 236 | errno = EINVAL; |
| 237 | return -1; |
| 238 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 239 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 240 | if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) { |
| 241 | LOG("read failed"); |
| 242 | errno = EINVAL; |
| 243 | return -1; |
| 244 | } |
| 245 | magic = be64_to_cpu(magic); |
| 246 | TRACE("Magic is 0x%" PRIx64, magic); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 247 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 248 | if (name) { |
| 249 | uint32_t reserved = 0; |
| 250 | uint32_t opt; |
| 251 | uint32_t namesize; |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 252 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 253 | TRACE("Checking magic (opts_magic)"); |
| 254 | if (magic != 0x49484156454F5054LL) { |
| 255 | LOG("Bad magic received"); |
| 256 | errno = EINVAL; |
| 257 | return -1; |
| 258 | } |
| 259 | if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) { |
| 260 | LOG("flags read failed"); |
| 261 | errno = EINVAL; |
| 262 | return -1; |
| 263 | } |
| 264 | *flags = be16_to_cpu(tmp) << 16; |
| 265 | /* reserved for future use */ |
| 266 | if (write_sync(csock, &reserved, sizeof(reserved)) != |
| 267 | sizeof(reserved)) { |
| 268 | LOG("write failed (reserved)"); |
| 269 | errno = EINVAL; |
| 270 | return -1; |
| 271 | } |
| 272 | /* write the export name */ |
| 273 | magic = cpu_to_be64(magic); |
| 274 | if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) { |
| 275 | LOG("write failed (magic)"); |
| 276 | errno = EINVAL; |
| 277 | return -1; |
| 278 | } |
| 279 | opt = cpu_to_be32(NBD_OPT_EXPORT_NAME); |
| 280 | if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) { |
| 281 | LOG("write failed (opt)"); |
| 282 | errno = EINVAL; |
| 283 | return -1; |
| 284 | } |
| 285 | namesize = cpu_to_be32(strlen(name)); |
| 286 | if (write_sync(csock, &namesize, sizeof(namesize)) != |
| 287 | sizeof(namesize)) { |
| 288 | LOG("write failed (namesize)"); |
| 289 | errno = EINVAL; |
| 290 | return -1; |
| 291 | } |
| 292 | if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) { |
| 293 | LOG("write failed (name)"); |
| 294 | errno = EINVAL; |
| 295 | return -1; |
| 296 | } |
| 297 | } else { |
| 298 | TRACE("Checking magic (cli_magic)"); |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 299 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 300 | if (magic != 0x00420281861253LL) { |
| 301 | LOG("Bad magic received"); |
| 302 | errno = EINVAL; |
| 303 | return -1; |
| 304 | } |
| 305 | } |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 306 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 307 | if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) { |
| 308 | LOG("read failed"); |
| 309 | errno = EINVAL; |
| 310 | return -1; |
| 311 | } |
| 312 | *size = be64_to_cpu(s); |
| 313 | *blocksize = 1024; |
| 314 | TRACE("Size is %" PRIu64, *size); |
Laurent Vivier | 1d45f8b | 2010-08-25 22:48:33 +0200 | [diff] [blame] | 315 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 316 | if (!name) { |
| 317 | if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) { |
| 318 | LOG("read failed (flags)"); |
| 319 | errno = EINVAL; |
| 320 | return -1; |
| 321 | } |
| 322 | *flags = be32_to_cpup(flags); |
| 323 | } else { |
| 324 | if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) { |
| 325 | LOG("read failed (tmp)"); |
| 326 | errno = EINVAL; |
| 327 | return -1; |
| 328 | } |
| 329 | *flags |= be32_to_cpu(tmp); |
| 330 | } |
| 331 | if (read_sync(csock, &buf, 124) != 124) { |
| 332 | LOG("read failed (buf)"); |
| 333 | errno = EINVAL; |
| 334 | return -1; |
| 335 | } |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 336 | return 0; |
| 337 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 338 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 339 | #ifndef _WIN32 |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 340 | int nbd_init(int fd, int csock, off_t size, size_t blocksize) |
| 341 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 342 | TRACE("Setting block size to %lu", (unsigned long)blocksize); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 343 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 344 | if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) { |
| 345 | int serrno = errno; |
| 346 | LOG("Failed setting NBD block size"); |
| 347 | errno = serrno; |
| 348 | return -1; |
| 349 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 350 | |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 351 | TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize)); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 352 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 353 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) { |
| 354 | int serrno = errno; |
| 355 | LOG("Failed setting size (in blocks)"); |
| 356 | errno = serrno; |
| 357 | return -1; |
| 358 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 359 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 360 | TRACE("Clearing NBD socket"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 361 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 362 | if (ioctl(fd, NBD_CLEAR_SOCK) == -1) { |
| 363 | int serrno = errno; |
| 364 | LOG("Failed clearing NBD socket"); |
| 365 | errno = serrno; |
| 366 | return -1; |
| 367 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 368 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 369 | TRACE("Setting NBD socket"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 370 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 371 | if (ioctl(fd, NBD_SET_SOCK, csock) == -1) { |
| 372 | int serrno = errno; |
| 373 | LOG("Failed to set NBD socket"); |
| 374 | errno = serrno; |
| 375 | return -1; |
| 376 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 377 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 378 | TRACE("Negotiation ended"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 379 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 380 | return 0; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | int nbd_disconnect(int fd) |
| 384 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 385 | ioctl(fd, NBD_CLEAR_QUE); |
| 386 | ioctl(fd, NBD_DISCONNECT); |
| 387 | ioctl(fd, NBD_CLEAR_SOCK); |
| 388 | return 0; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Jes Sorensen | 0a4eb86 | 2010-08-31 09:30:33 +0200 | [diff] [blame] | 391 | int nbd_client(int fd) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 392 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 393 | int ret; |
| 394 | int serrno; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 395 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 396 | TRACE("Doing NBD loop"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 397 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 398 | ret = ioctl(fd, NBD_DO_IT); |
| 399 | serrno = errno; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 400 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 401 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 402 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 403 | TRACE("Clearing NBD queue"); |
| 404 | ioctl(fd, NBD_CLEAR_QUE); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 405 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 406 | TRACE("Clearing NBD socket"); |
| 407 | ioctl(fd, NBD_CLEAR_SOCK); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 408 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 409 | errno = serrno; |
| 410 | return ret; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 411 | } |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 412 | #else |
| 413 | int nbd_init(int fd, int csock, off_t size, size_t blocksize) |
| 414 | { |
| 415 | errno = ENOTSUP; |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | int nbd_disconnect(int fd) |
| 420 | { |
| 421 | errno = ENOTSUP; |
| 422 | return -1; |
| 423 | } |
| 424 | |
Jes Sorensen | 0a4eb86 | 2010-08-31 09:30:33 +0200 | [diff] [blame] | 425 | int nbd_client(int fd) |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 426 | { |
| 427 | errno = ENOTSUP; |
| 428 | return -1; |
| 429 | } |
| 430 | #endif |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 431 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 432 | int nbd_send_request(int csock, struct nbd_request *request) |
| 433 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 434 | uint8_t buf[4 + 4 + 8 + 8 + 4]; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 435 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 436 | cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC); |
| 437 | cpu_to_be32w((uint32_t*)(buf + 4), request->type); |
| 438 | cpu_to_be64w((uint64_t*)(buf + 8), request->handle); |
| 439 | cpu_to_be64w((uint64_t*)(buf + 16), request->from); |
| 440 | cpu_to_be32w((uint32_t*)(buf + 24), request->len); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 441 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 442 | TRACE("Sending request to client: " |
| 443 | "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}", |
| 444 | request->from, request->len, request->handle, request->type); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 445 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 446 | if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { |
| 447 | LOG("writing to socket failed"); |
| 448 | errno = EINVAL; |
| 449 | return -1; |
| 450 | } |
| 451 | return 0; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 452 | } |
| 453 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 454 | static int nbd_receive_request(int csock, struct nbd_request *request) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 455 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 456 | uint8_t buf[4 + 4 + 8 + 8 + 4]; |
| 457 | uint32_t magic; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 458 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 459 | if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { |
| 460 | LOG("read failed"); |
| 461 | errno = EINVAL; |
| 462 | return -1; |
| 463 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 464 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 465 | /* Request |
| 466 | [ 0 .. 3] magic (NBD_REQUEST_MAGIC) |
| 467 | [ 4 .. 7] type (0 == READ, 1 == WRITE) |
| 468 | [ 8 .. 15] handle |
| 469 | [16 .. 23] from |
| 470 | [24 .. 27] len |
| 471 | */ |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 472 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 473 | magic = be32_to_cpup((uint32_t*)buf); |
| 474 | request->type = be32_to_cpup((uint32_t*)(buf + 4)); |
| 475 | request->handle = be64_to_cpup((uint64_t*)(buf + 8)); |
| 476 | request->from = be64_to_cpup((uint64_t*)(buf + 16)); |
| 477 | request->len = be32_to_cpup((uint32_t*)(buf + 24)); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 478 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 479 | TRACE("Got request: " |
| 480 | "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }", |
| 481 | magic, request->type, request->from, request->len); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 482 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 483 | if (magic != NBD_REQUEST_MAGIC) { |
| 484 | LOG("invalid magic (got 0x%x)", magic); |
| 485 | errno = EINVAL; |
| 486 | return -1; |
| 487 | } |
| 488 | return 0; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 489 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 490 | |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 491 | int nbd_receive_reply(int csock, struct nbd_reply *reply) |
| 492 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 493 | uint8_t buf[NBD_REPLY_SIZE]; |
| 494 | uint32_t magic; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 495 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 496 | memset(buf, 0xAA, sizeof(buf)); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 497 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 498 | if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { |
| 499 | LOG("read failed"); |
| 500 | errno = EINVAL; |
| 501 | return -1; |
| 502 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 503 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 504 | /* Reply |
| 505 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) |
| 506 | [ 4 .. 7] error (0 == no error) |
| 507 | [ 7 .. 15] handle |
| 508 | */ |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 509 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 510 | magic = be32_to_cpup((uint32_t*)buf); |
| 511 | reply->error = be32_to_cpup((uint32_t*)(buf + 4)); |
| 512 | reply->handle = be64_to_cpup((uint64_t*)(buf + 8)); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 513 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 514 | TRACE("Got reply: " |
| 515 | "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }", |
| 516 | magic, reply->error, reply->handle); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 517 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 518 | if (magic != NBD_REPLY_MAGIC) { |
| 519 | LOG("invalid magic (got 0x%x)", magic); |
| 520 | errno = EINVAL; |
| 521 | return -1; |
| 522 | } |
| 523 | return 0; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static int nbd_send_reply(int csock, struct nbd_reply *reply) |
| 527 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 528 | uint8_t buf[4 + 4 + 8]; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 529 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 530 | /* Reply |
| 531 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) |
| 532 | [ 4 .. 7] error (0 == no error) |
| 533 | [ 7 .. 15] handle |
| 534 | */ |
| 535 | cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC); |
| 536 | cpu_to_be32w((uint32_t*)(buf + 4), reply->error); |
| 537 | cpu_to_be64w((uint64_t*)(buf + 8), reply->handle); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 538 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 539 | TRACE("Sending response to client"); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 540 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 541 | if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { |
| 542 | LOG("writing to socket failed"); |
| 543 | errno = EINVAL; |
| 544 | return -1; |
| 545 | } |
| 546 | return 0; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, |
| 550 | off_t *offset, bool readonly, uint8_t *data, int data_size) |
| 551 | { |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 552 | struct nbd_request request; |
| 553 | struct nbd_reply reply; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 554 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 555 | TRACE("Reading request."); |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 556 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 557 | if (nbd_receive_request(csock, &request) == -1) |
| 558 | return -1; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 559 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 560 | if (request.len + NBD_REPLY_SIZE > data_size) { |
| 561 | LOG("len (%u) is larger than max len (%u)", |
| 562 | request.len + NBD_REPLY_SIZE, data_size); |
| 563 | errno = EINVAL; |
| 564 | return -1; |
| 565 | } |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 566 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 567 | if ((request.from + request.len) < request.from) { |
| 568 | LOG("integer overflow detected! " |
| 569 | "you're probably being attacked"); |
| 570 | errno = EINVAL; |
| 571 | return -1; |
| 572 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 573 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 574 | if ((request.from + request.len) > size) { |
| 575 | LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64 |
| 576 | ", Offset: %" PRIu64 "\n", |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 577 | request.from, request.len, (uint64_t)size, dev_offset); |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 578 | LOG("requested operation past EOF--bad client?"); |
| 579 | errno = EINVAL; |
| 580 | return -1; |
| 581 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 582 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 583 | TRACE("Decoding type"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 584 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 585 | reply.handle = request.handle; |
| 586 | reply.error = 0; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 587 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 588 | switch (request.type) { |
| 589 | case NBD_CMD_READ: |
| 590 | TRACE("Request type is READ"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 591 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 592 | if (bdrv_read(bs, (request.from + dev_offset) / 512, |
| 593 | data + NBD_REPLY_SIZE, |
| 594 | request.len / 512) == -1) { |
| 595 | LOG("reading from file failed"); |
| 596 | errno = EINVAL; |
| 597 | return -1; |
| 598 | } |
| 599 | *offset += request.len; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 600 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 601 | TRACE("Read %u byte(s)", request.len); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 602 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 603 | /* Reply |
| 604 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) |
| 605 | [ 4 .. 7] error (0 == no error) |
| 606 | [ 7 .. 15] handle |
| 607 | */ |
Laurent Vivier | 5fe1688 | 2010-09-17 18:13:57 +0200 | [diff] [blame] | 608 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 609 | cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC); |
| 610 | cpu_to_be32w((uint32_t*)(data + 4), reply.error); |
| 611 | cpu_to_be64w((uint64_t*)(data + 8), reply.handle); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 612 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 613 | TRACE("Sending data to client"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 614 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 615 | if (write_sync(csock, data, |
| 616 | request.len + NBD_REPLY_SIZE) != |
| 617 | request.len + NBD_REPLY_SIZE) { |
| 618 | LOG("writing to socket failed"); |
| 619 | errno = EINVAL; |
| 620 | return -1; |
| 621 | } |
| 622 | break; |
| 623 | case NBD_CMD_WRITE: |
| 624 | TRACE("Request type is WRITE"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 625 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 626 | TRACE("Reading %u byte(s)", request.len); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 627 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 628 | if (read_sync(csock, data, request.len) != request.len) { |
| 629 | LOG("reading from socket failed"); |
| 630 | errno = EINVAL; |
| 631 | return -1; |
| 632 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 633 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 634 | if (readonly) { |
| 635 | TRACE("Server is read-only, return error"); |
| 636 | reply.error = 1; |
| 637 | } else { |
| 638 | TRACE("Writing to device"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 639 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 640 | if (bdrv_write(bs, (request.from + dev_offset) / 512, |
| 641 | data, request.len / 512) == -1) { |
| 642 | LOG("writing to file failed"); |
| 643 | errno = EINVAL; |
| 644 | return -1; |
| 645 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 646 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 647 | *offset += request.len; |
| 648 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 649 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 650 | if (nbd_send_reply(csock, &reply) == -1) |
| 651 | return -1; |
| 652 | break; |
| 653 | case NBD_CMD_DISC: |
| 654 | TRACE("Request type is DISCONNECT"); |
| 655 | errno = 0; |
| 656 | return 1; |
| 657 | default: |
| 658 | LOG("invalid request type (%u) received", request.type); |
| 659 | errno = EINVAL; |
| 660 | return -1; |
| 661 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 662 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 663 | TRACE("Request/Reply complete"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 664 | |
Nick Thomas | b2e3d87 | 2011-02-22 15:44:51 +0000 | [diff] [blame] | 665 | return 0; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 666 | } |