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