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