1cd831bd7Sths /* 27a5ca864Sbellard * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws> 37a5ca864Sbellard * 47a5ca864Sbellard * Network Block Device 57a5ca864Sbellard * 67a5ca864Sbellard * This program is free software; you can redistribute it and/or modify 77a5ca864Sbellard * it under the terms of the GNU General Public License as published by 87a5ca864Sbellard * the Free Software Foundation; under version 2 of the License. 97a5ca864Sbellard * 107a5ca864Sbellard * This program is distributed in the hope that it will be useful, 117a5ca864Sbellard * but WITHOUT ANY WARRANTY; without even the implied warranty of 127a5ca864Sbellard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 137a5ca864Sbellard * GNU General Public License for more details. 147a5ca864Sbellard * 157a5ca864Sbellard * You should have received a copy of the GNU General Public License 168167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 177a5ca864Sbellard */ 187a5ca864Sbellard 197a5ca864Sbellard #include <qemu-common.h> 207a5ca864Sbellard #include "block_int.h" 217a5ca864Sbellard #include "nbd.h" 227a5ca864Sbellard 237a5ca864Sbellard #include <stdarg.h> 247a5ca864Sbellard #include <stdio.h> 257a5ca864Sbellard #include <getopt.h> 267a5ca864Sbellard #include <err.h> 27cd831bd7Sths #include <sys/types.h> 287a5ca864Sbellard #include <sys/socket.h> 297a5ca864Sbellard #include <netinet/in.h> 307a5ca864Sbellard #include <netinet/tcp.h> 317a5ca864Sbellard #include <arpa/inet.h> 32cd831bd7Sths #include <signal.h> 332bff4b6fSBlue Swirl #include <libgen.h> 34cd831bd7Sths 35cd831bd7Sths #define SOCKET_PATH "/var/lock/qemu-nbd-%s" 367a5ca864Sbellard 372f726488Sths #define NBD_BUFFER_SIZE (1024*1024) 382f726488Sths 39b1d8e52eSblueswir1 static int verbose; 407a5ca864Sbellard 417a5ca864Sbellard static void usage(const char *name) 427a5ca864Sbellard { 437a5ca864Sbellard printf( 447a5ca864Sbellard "Usage: %s [OPTIONS] FILE\n" 457a5ca864Sbellard "QEMU Disk Network Block Device Server\n" 467a5ca864Sbellard "\n" 47*c2e2872bSLaurent Vivier " -p, --port=PORT port to listen on (default `%d')\n" 487a5ca864Sbellard " -o, --offset=OFFSET offset into the image\n" 497a5ca864Sbellard " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" 50cd831bd7Sths " -k, --socket=PATH path to the unix socket\n" 51cd831bd7Sths " (default '"SOCKET_PATH"')\n" 527a5ca864Sbellard " -r, --read-only export read-only\n" 537a5ca864Sbellard " -P, --partition=NUM only expose partition NUM\n" 542f726488Sths " -s, --snapshot use snapshot file\n" 552f726488Sths " -n, --nocache disable host cache\n" 56cd831bd7Sths " -c, --connect=DEV connect FILE to the local NBD device DEV\n" 57cd831bd7Sths " -d, --disconnect disconnect the specified device\n" 583b05a8e9Sths " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" 5975818250Sths " -t, --persistent don't exit on the last connection\n" 607a5ca864Sbellard " -v, --verbose display extra debugging information\n" 617a5ca864Sbellard " -h, --help display this help and exit\n" 627a5ca864Sbellard " -V, --version output version information and exit\n" 637a5ca864Sbellard "\n" 647a5ca864Sbellard "Report bugs to <anthony@codemonkey.ws>\n" 65*c2e2872bSLaurent Vivier , name, NBD_DEFAULT_PORT, "DEVICE"); 667a5ca864Sbellard } 677a5ca864Sbellard 687a5ca864Sbellard static void version(const char *name) 697a5ca864Sbellard { 707a5ca864Sbellard printf( 71315bc7aaSths "%s version 0.0.1\n" 727a5ca864Sbellard "Written by Anthony Liguori.\n" 737a5ca864Sbellard "\n" 747a5ca864Sbellard "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n" 757a5ca864Sbellard "This is free software; see the source for copying conditions. There is NO\n" 767a5ca864Sbellard "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" 77315bc7aaSths , name); 787a5ca864Sbellard } 797a5ca864Sbellard 807a5ca864Sbellard struct partition_record 817a5ca864Sbellard { 827a5ca864Sbellard uint8_t bootable; 837a5ca864Sbellard uint8_t start_head; 847a5ca864Sbellard uint32_t start_cylinder; 857a5ca864Sbellard uint8_t start_sector; 867a5ca864Sbellard uint8_t system; 877a5ca864Sbellard uint8_t end_head; 887a5ca864Sbellard uint8_t end_cylinder; 897a5ca864Sbellard uint8_t end_sector; 907a5ca864Sbellard uint32_t start_sector_abs; 917a5ca864Sbellard uint32_t nb_sectors_abs; 927a5ca864Sbellard }; 937a5ca864Sbellard 947a5ca864Sbellard static void read_partition(uint8_t *p, struct partition_record *r) 957a5ca864Sbellard { 967a5ca864Sbellard r->bootable = p[0]; 977a5ca864Sbellard r->start_head = p[1]; 987a5ca864Sbellard r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); 997a5ca864Sbellard r->start_sector = p[2] & 0x3f; 1007a5ca864Sbellard r->system = p[4]; 1017a5ca864Sbellard r->end_head = p[5]; 1027a5ca864Sbellard r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); 1037a5ca864Sbellard r->end_sector = p[6] & 0x3f; 1047a5ca864Sbellard r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; 1057a5ca864Sbellard r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; 1067a5ca864Sbellard } 1077a5ca864Sbellard 1087a5ca864Sbellard static int find_partition(BlockDriverState *bs, int partition, 1097a5ca864Sbellard off_t *offset, off_t *size) 1107a5ca864Sbellard { 1117a5ca864Sbellard struct partition_record mbr[4]; 1127a5ca864Sbellard uint8_t data[512]; 1137a5ca864Sbellard int i; 1147a5ca864Sbellard int ext_partnum = 4; 115cb7cf0e3SRyota Ozaki int ret; 1167a5ca864Sbellard 117cb7cf0e3SRyota Ozaki if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { 118cb7cf0e3SRyota Ozaki errno = -ret; 119cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "error while reading"); 120cb7cf0e3SRyota Ozaki } 1217a5ca864Sbellard 1227a5ca864Sbellard if (data[510] != 0x55 || data[511] != 0xaa) { 1237a5ca864Sbellard errno = -EINVAL; 1247a5ca864Sbellard return -1; 1257a5ca864Sbellard } 1267a5ca864Sbellard 1277a5ca864Sbellard for (i = 0; i < 4; i++) { 1287a5ca864Sbellard read_partition(&data[446 + 16 * i], &mbr[i]); 1297a5ca864Sbellard 1307a5ca864Sbellard if (!mbr[i].nb_sectors_abs) 1317a5ca864Sbellard continue; 1327a5ca864Sbellard 1337a5ca864Sbellard if (mbr[i].system == 0xF || mbr[i].system == 0x5) { 1347a5ca864Sbellard struct partition_record ext[4]; 1357a5ca864Sbellard uint8_t data1[512]; 1367a5ca864Sbellard int j; 1377a5ca864Sbellard 138cb7cf0e3SRyota Ozaki if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { 139cb7cf0e3SRyota Ozaki errno = -ret; 140cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "error while reading"); 141cb7cf0e3SRyota Ozaki } 1427a5ca864Sbellard 1437a5ca864Sbellard for (j = 0; j < 4; j++) { 1447a5ca864Sbellard read_partition(&data1[446 + 16 * j], &ext[j]); 1457a5ca864Sbellard if (!ext[j].nb_sectors_abs) 1467a5ca864Sbellard continue; 1477a5ca864Sbellard 1487a5ca864Sbellard if ((ext_partnum + j + 1) == partition) { 1497a5ca864Sbellard *offset = (uint64_t)ext[j].start_sector_abs << 9; 1507a5ca864Sbellard *size = (uint64_t)ext[j].nb_sectors_abs << 9; 1517a5ca864Sbellard return 0; 1527a5ca864Sbellard } 1537a5ca864Sbellard } 1547a5ca864Sbellard ext_partnum += 4; 1557a5ca864Sbellard } else if ((i + 1) == partition) { 1567a5ca864Sbellard *offset = (uint64_t)mbr[i].start_sector_abs << 9; 1577a5ca864Sbellard *size = (uint64_t)mbr[i].nb_sectors_abs << 9; 1587a5ca864Sbellard return 0; 1597a5ca864Sbellard } 1607a5ca864Sbellard } 1617a5ca864Sbellard 1627a5ca864Sbellard errno = -ENOENT; 1637a5ca864Sbellard return -1; 1647a5ca864Sbellard } 1657a5ca864Sbellard 166cd831bd7Sths static void show_parts(const char *device) 167cd831bd7Sths { 168cd831bd7Sths if (fork() == 0) { 169cd831bd7Sths int nbd; 170cd831bd7Sths 171cd831bd7Sths /* linux just needs an open() to trigger 172cd831bd7Sths * the partition table update 173cd831bd7Sths * but remember to load the module with max_part != 0 : 174cd831bd7Sths * modprobe nbd max_part=63 175cd831bd7Sths */ 176cd831bd7Sths nbd = open(device, O_RDWR); 177cd831bd7Sths if (nbd != -1) 178cd831bd7Sths close(nbd); 179cd831bd7Sths exit(0); 180cd831bd7Sths } 181cd831bd7Sths } 182cd831bd7Sths 1837a5ca864Sbellard int main(int argc, char **argv) 1847a5ca864Sbellard { 1857a5ca864Sbellard BlockDriverState *bs; 1867a5ca864Sbellard off_t dev_offset = 0; 1877a5ca864Sbellard off_t offset = 0; 1887a5ca864Sbellard bool readonly = false; 189cd831bd7Sths bool disconnect = false; 1907a5ca864Sbellard const char *bindto = "0.0.0.0"; 191*c2e2872bSLaurent Vivier int port = NBD_DEFAULT_PORT; 1927a5ca864Sbellard struct sockaddr_in addr; 1937a5ca864Sbellard socklen_t addr_len = sizeof(addr); 1947a5ca864Sbellard off_t fd_size; 195cd831bd7Sths char *device = NULL; 196cd831bd7Sths char *socket = NULL; 197cd831bd7Sths char sockpath[128]; 198d6aa671fSaliguori const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; 1997a5ca864Sbellard struct option lopt[] = { 200660f11beSBlue Swirl { "help", 0, NULL, 'h' }, 201660f11beSBlue Swirl { "version", 0, NULL, 'V' }, 202660f11beSBlue Swirl { "bind", 1, NULL, 'b' }, 203660f11beSBlue Swirl { "port", 1, NULL, 'p' }, 204660f11beSBlue Swirl { "socket", 1, NULL, 'k' }, 205660f11beSBlue Swirl { "offset", 1, NULL, 'o' }, 206660f11beSBlue Swirl { "read-only", 0, NULL, 'r' }, 207660f11beSBlue Swirl { "partition", 1, NULL, 'P' }, 208660f11beSBlue Swirl { "connect", 1, NULL, 'c' }, 209660f11beSBlue Swirl { "disconnect", 0, NULL, 'd' }, 210660f11beSBlue Swirl { "snapshot", 0, NULL, 's' }, 211660f11beSBlue Swirl { "nocache", 0, NULL, 'n' }, 212660f11beSBlue Swirl { "shared", 1, NULL, 'e' }, 213660f11beSBlue Swirl { "persistent", 0, NULL, 't' }, 214660f11beSBlue Swirl { "verbose", 0, NULL, 'v' }, 215660f11beSBlue Swirl { NULL, 0, NULL, 0 } 2167a5ca864Sbellard }; 2177a5ca864Sbellard int ch; 2187a5ca864Sbellard int opt_ind = 0; 2197a5ca864Sbellard int li; 2207a5ca864Sbellard char *end; 221f5edb014SNaphtali Sprei int flags = BDRV_O_RDWR; 2227a5ca864Sbellard int partition = -1; 223cd831bd7Sths int ret; 2243b05a8e9Sths int shared = 1; 2252f726488Sths uint8_t *data; 2263b05a8e9Sths fd_set fds; 2273b05a8e9Sths int *sharing_fds; 2283b05a8e9Sths int fd; 2293b05a8e9Sths int i; 2303b05a8e9Sths int nb_fds = 0; 2313b05a8e9Sths int max_fd; 23275818250Sths int persistent = 0; 2331d45f8b5SLaurent Vivier uint32_t nbdflags; 2347a5ca864Sbellard 2357a5ca864Sbellard while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { 2367a5ca864Sbellard switch (ch) { 2377a5ca864Sbellard case 's': 2382f726488Sths flags |= BDRV_O_SNAPSHOT; 2392f726488Sths break; 2402f726488Sths case 'n': 2419f7965c7Saliguori flags |= BDRV_O_NOCACHE; 2427a5ca864Sbellard break; 2437a5ca864Sbellard case 'b': 2447a5ca864Sbellard bindto = optarg; 2457a5ca864Sbellard break; 2467a5ca864Sbellard case 'p': 2477a5ca864Sbellard li = strtol(optarg, &end, 0); 2487a5ca864Sbellard if (*end) { 249b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid port `%s'", optarg); 2507a5ca864Sbellard } 2517a5ca864Sbellard if (li < 1 || li > 65535) { 252b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Port out of range `%s'", optarg); 2537a5ca864Sbellard } 2547a5ca864Sbellard port = (uint16_t)li; 2557a5ca864Sbellard break; 2567a5ca864Sbellard case 'o': 2577a5ca864Sbellard dev_offset = strtoll (optarg, &end, 0); 2587a5ca864Sbellard if (*end) { 259b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); 2607a5ca864Sbellard } 2617a5ca864Sbellard if (dev_offset < 0) { 262b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); 2637a5ca864Sbellard } 2647a5ca864Sbellard break; 2657a5ca864Sbellard case 'r': 2667a5ca864Sbellard readonly = true; 26707108b29SNaphtali Sprei flags &= ~BDRV_O_RDWR; 2687a5ca864Sbellard break; 2697a5ca864Sbellard case 'P': 2707a5ca864Sbellard partition = strtol(optarg, &end, 0); 2717a5ca864Sbellard if (*end) 272b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); 2737a5ca864Sbellard if (partition < 1 || partition > 8) 274b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition %d", partition); 2757a5ca864Sbellard break; 276cd831bd7Sths case 'k': 277cd831bd7Sths socket = optarg; 278cd831bd7Sths if (socket[0] != '/') 279b6353beaSRyota Ozaki errx(EXIT_FAILURE, "socket path must be absolute\n"); 280cd831bd7Sths break; 281cd831bd7Sths case 'd': 282cd831bd7Sths disconnect = true; 283cd831bd7Sths break; 284cd831bd7Sths case 'c': 285cd831bd7Sths device = optarg; 286cd831bd7Sths break; 2873b05a8e9Sths case 'e': 2883b05a8e9Sths shared = strtol(optarg, &end, 0); 2893b05a8e9Sths if (*end) { 290b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); 2913b05a8e9Sths } 2923b05a8e9Sths if (shared < 1) { 293b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); 2943b05a8e9Sths } 2953b05a8e9Sths break; 29675818250Sths case 't': 29775818250Sths persistent = 1; 29875818250Sths break; 2997a5ca864Sbellard case 'v': 3007a5ca864Sbellard verbose = 1; 3017a5ca864Sbellard break; 3027a5ca864Sbellard case 'V': 3037a5ca864Sbellard version(argv[0]); 3047a5ca864Sbellard exit(0); 3057a5ca864Sbellard break; 3067a5ca864Sbellard case 'h': 3077a5ca864Sbellard usage(argv[0]); 3087a5ca864Sbellard exit(0); 3097a5ca864Sbellard break; 3107a5ca864Sbellard case '?': 311b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Try `%s --help' for more information.", 3127a5ca864Sbellard argv[0]); 3137a5ca864Sbellard } 3147a5ca864Sbellard } 3157a5ca864Sbellard 3167a5ca864Sbellard if ((argc - optind) != 1) { 317b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid number of argument.\n" 3187a5ca864Sbellard "Try `%s --help' for more information.", 3197a5ca864Sbellard argv[0]); 3207a5ca864Sbellard } 3217a5ca864Sbellard 322cd831bd7Sths if (disconnect) { 323cd831bd7Sths fd = open(argv[optind], O_RDWR); 324cd831bd7Sths if (fd == -1) 325cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Cannot open %s", argv[optind]); 326cd831bd7Sths 327cd831bd7Sths nbd_disconnect(fd); 328cd831bd7Sths 329cd831bd7Sths close(fd); 330cd831bd7Sths 331cd831bd7Sths printf("%s disconnected\n", argv[optind]); 332cd831bd7Sths 333cd831bd7Sths return 0; 334cd831bd7Sths } 335cd831bd7Sths 3367a5ca864Sbellard bdrv_init(); 3377a5ca864Sbellard 3387a5ca864Sbellard bs = bdrv_new("hda"); 3397a5ca864Sbellard if (bs == NULL) 3407a5ca864Sbellard return 1; 3417a5ca864Sbellard 342cb7cf0e3SRyota Ozaki if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) { 343cb7cf0e3SRyota Ozaki errno = -ret; 344cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); 345cb7cf0e3SRyota Ozaki } 3467a5ca864Sbellard 3477a5ca864Sbellard fd_size = bs->total_sectors * 512; 3487a5ca864Sbellard 3497a5ca864Sbellard if (partition != -1 && 3507a5ca864Sbellard find_partition(bs, partition, &dev_offset, &fd_size)) 351cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not find partition %d", partition); 3527a5ca864Sbellard 353cd831bd7Sths if (device) { 354cd831bd7Sths pid_t pid; 3553b05a8e9Sths int sock; 3563b05a8e9Sths 357cb7cf0e3SRyota Ozaki /* want to fail before daemonizing */ 358cb7cf0e3SRyota Ozaki if (access(device, R_OK|W_OK) == -1) { 359cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not access '%s'", device); 360cb7cf0e3SRyota Ozaki } 361cb7cf0e3SRyota Ozaki 362f5de141bSAnthony Liguori if (!verbose) { 363f5de141bSAnthony Liguori /* detach client and server */ 364f5de141bSAnthony Liguori if (daemon(0, 0) == -1) { 365cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to daemonize"); 366f5de141bSAnthony Liguori } 367f5de141bSAnthony Liguori } 368cd831bd7Sths 369cd831bd7Sths if (socket == NULL) { 37022ff51eeSBlue Swirl snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, 37122ff51eeSBlue Swirl basename(device)); 372cd831bd7Sths socket = sockpath; 373cd831bd7Sths } 374cd831bd7Sths 375cd831bd7Sths pid = fork(); 376cd831bd7Sths if (pid < 0) 377cd831bd7Sths return 1; 378cd831bd7Sths if (pid != 0) { 379cd831bd7Sths off_t size; 380cd831bd7Sths size_t blocksize; 381cd831bd7Sths 382cd831bd7Sths ret = 0; 383cd831bd7Sths bdrv_close(bs); 384cd831bd7Sths 385cd831bd7Sths do { 386cd831bd7Sths sock = unix_socket_outgoing(socket); 387cd831bd7Sths if (sock == -1) { 388cb7cf0e3SRyota Ozaki if (errno != ENOENT && errno != ECONNREFUSED) { 389cb7cf0e3SRyota Ozaki ret = 1; 390cd831bd7Sths goto out; 391cb7cf0e3SRyota Ozaki } 392cd831bd7Sths sleep(1); /* wait children */ 393cd831bd7Sths } 394cd831bd7Sths } while (sock == -1); 395cd831bd7Sths 396cd831bd7Sths fd = open(device, O_RDWR); 397cd831bd7Sths if (fd == -1) { 398cd831bd7Sths ret = 1; 399cd831bd7Sths goto out; 400cd831bd7Sths } 401cd831bd7Sths 4021d45f8b5SLaurent Vivier ret = nbd_receive_negotiate(sock, NULL, &nbdflags, 4031d45f8b5SLaurent Vivier &size, &blocksize); 404cd831bd7Sths if (ret == -1) { 405cd831bd7Sths ret = 1; 406cd831bd7Sths goto out; 407cd831bd7Sths } 408cd831bd7Sths 409cd831bd7Sths ret = nbd_init(fd, sock, size, blocksize); 410cd831bd7Sths if (ret == -1) { 411cd831bd7Sths ret = 1; 412cd831bd7Sths goto out; 413cd831bd7Sths } 414cd831bd7Sths 415cd831bd7Sths printf("NBD device %s is now connected to file %s\n", 416cd831bd7Sths device, argv[optind]); 417cd831bd7Sths 418cd831bd7Sths /* update partition table */ 419cd831bd7Sths 420cd831bd7Sths show_parts(device); 421cd831bd7Sths 422e301b13dSJes Sorensen ret = nbd_client(fd); 423e301b13dSJes Sorensen if (ret) { 424e301b13dSJes Sorensen ret = 1; 425e301b13dSJes Sorensen } 426cd831bd7Sths close(fd); 427cd831bd7Sths out: 428cd831bd7Sths kill(pid, SIGTERM); 429cd831bd7Sths unlink(socket); 430cd831bd7Sths 431cd831bd7Sths return ret; 432cd831bd7Sths } 433cd831bd7Sths /* children */ 434cd831bd7Sths } 435cd831bd7Sths 4363b05a8e9Sths sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); 4373b05a8e9Sths 438cd831bd7Sths if (socket) { 4393b05a8e9Sths sharing_fds[0] = unix_socket_incoming(socket); 440cd831bd7Sths } else { 4413b05a8e9Sths sharing_fds[0] = tcp_socket_incoming(bindto, port); 442cd831bd7Sths } 443cd831bd7Sths 4443b05a8e9Sths if (sharing_fds[0] == -1) 4457a5ca864Sbellard return 1; 4463b05a8e9Sths max_fd = sharing_fds[0]; 4473b05a8e9Sths nb_fds++; 4487a5ca864Sbellard 44972aef731SChristoph Hellwig data = qemu_blockalign(bs, NBD_BUFFER_SIZE); 4503b05a8e9Sths if (data == NULL) 451b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Cannot allocate data buffer"); 4523b05a8e9Sths 4533b05a8e9Sths do { 4543b05a8e9Sths 4553b05a8e9Sths FD_ZERO(&fds); 4563b05a8e9Sths for (i = 0; i < nb_fds; i++) 4573b05a8e9Sths FD_SET(sharing_fds[i], &fds); 4583b05a8e9Sths 4593b05a8e9Sths ret = select(max_fd + 1, &fds, NULL, NULL, NULL); 4603b05a8e9Sths if (ret == -1) 4613b05a8e9Sths break; 4623b05a8e9Sths 4633b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) 4643b05a8e9Sths ret--; 4653b05a8e9Sths for (i = 1; i < nb_fds && ret; i++) { 4663b05a8e9Sths if (FD_ISSET(sharing_fds[i], &fds)) { 4673b05a8e9Sths if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, 4683b05a8e9Sths &offset, readonly, data, NBD_BUFFER_SIZE) != 0) { 4693b05a8e9Sths close(sharing_fds[i]); 4703b05a8e9Sths nb_fds--; 4713b05a8e9Sths sharing_fds[i] = sharing_fds[nb_fds]; 4723b05a8e9Sths i--; 4733b05a8e9Sths } 4743b05a8e9Sths ret--; 4753b05a8e9Sths } 4763b05a8e9Sths } 4773b05a8e9Sths /* new connection ? */ 4783b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) { 4793b05a8e9Sths if (nb_fds < shared + 1) { 4803b05a8e9Sths sharing_fds[nb_fds] = accept(sharing_fds[0], 4813b05a8e9Sths (struct sockaddr *)&addr, 4823b05a8e9Sths &addr_len); 4833b05a8e9Sths if (sharing_fds[nb_fds] != -1 && 48427982661Saliguori nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) { 4853b05a8e9Sths if (sharing_fds[nb_fds] > max_fd) 4863b05a8e9Sths max_fd = sharing_fds[nb_fds]; 4873b05a8e9Sths nb_fds++; 4883b05a8e9Sths } 4893b05a8e9Sths } 4903b05a8e9Sths } 49175818250Sths } while (persistent || nb_fds > 1); 492f8a83245SHerve Poussineau qemu_vfree(data); 4937a5ca864Sbellard 4943b05a8e9Sths close(sharing_fds[0]); 4957a5ca864Sbellard bdrv_close(bs); 4963b05a8e9Sths qemu_free(sharing_fds); 497cd831bd7Sths if (socket) 498cd831bd7Sths unlink(socket); 4997a5ca864Sbellard 5007a5ca864Sbellard return 0; 5017a5ca864Sbellard } 502