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 195a61cb60SStefan Weil #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" 47c2e2872bSLaurent 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" 65c2e2872bSLaurent 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; 188*b90fb4b8SPaolo Bonzini uint32_t nbdflags = 0; 189cd831bd7Sths bool disconnect = false; 1907a5ca864Sbellard const char *bindto = "0.0.0.0"; 191c2e2872bSLaurent 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; 2337a5ca864Sbellard 2347a5ca864Sbellard while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { 2357a5ca864Sbellard switch (ch) { 2367a5ca864Sbellard case 's': 2372f726488Sths flags |= BDRV_O_SNAPSHOT; 2382f726488Sths break; 2392f726488Sths case 'n': 240a6599793SChristoph Hellwig flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 2417a5ca864Sbellard break; 2427a5ca864Sbellard case 'b': 2437a5ca864Sbellard bindto = optarg; 2447a5ca864Sbellard break; 2457a5ca864Sbellard case 'p': 2467a5ca864Sbellard li = strtol(optarg, &end, 0); 2477a5ca864Sbellard if (*end) { 248b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid port `%s'", optarg); 2497a5ca864Sbellard } 2507a5ca864Sbellard if (li < 1 || li > 65535) { 251b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Port out of range `%s'", optarg); 2527a5ca864Sbellard } 2537a5ca864Sbellard port = (uint16_t)li; 2547a5ca864Sbellard break; 2557a5ca864Sbellard case 'o': 2567a5ca864Sbellard dev_offset = strtoll (optarg, &end, 0); 2577a5ca864Sbellard if (*end) { 258b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); 2597a5ca864Sbellard } 2607a5ca864Sbellard if (dev_offset < 0) { 261b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); 2627a5ca864Sbellard } 2637a5ca864Sbellard break; 2647a5ca864Sbellard case 'r': 265*b90fb4b8SPaolo Bonzini nbdflags |= NBD_FLAG_READ_ONLY; 26607108b29SNaphtali Sprei flags &= ~BDRV_O_RDWR; 2677a5ca864Sbellard break; 2687a5ca864Sbellard case 'P': 2697a5ca864Sbellard partition = strtol(optarg, &end, 0); 2707a5ca864Sbellard if (*end) 271b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); 2727a5ca864Sbellard if (partition < 1 || partition > 8) 273b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition %d", partition); 2747a5ca864Sbellard break; 275cd831bd7Sths case 'k': 276cd831bd7Sths socket = optarg; 277cd831bd7Sths if (socket[0] != '/') 278b6353beaSRyota Ozaki errx(EXIT_FAILURE, "socket path must be absolute\n"); 279cd831bd7Sths break; 280cd831bd7Sths case 'd': 281cd831bd7Sths disconnect = true; 282cd831bd7Sths break; 283cd831bd7Sths case 'c': 284cd831bd7Sths device = optarg; 285cd831bd7Sths break; 2863b05a8e9Sths case 'e': 2873b05a8e9Sths shared = strtol(optarg, &end, 0); 2883b05a8e9Sths if (*end) { 289b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); 2903b05a8e9Sths } 2913b05a8e9Sths if (shared < 1) { 292b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); 2933b05a8e9Sths } 2943b05a8e9Sths break; 29575818250Sths case 't': 29675818250Sths persistent = 1; 29775818250Sths break; 2987a5ca864Sbellard case 'v': 2997a5ca864Sbellard verbose = 1; 3007a5ca864Sbellard break; 3017a5ca864Sbellard case 'V': 3027a5ca864Sbellard version(argv[0]); 3037a5ca864Sbellard exit(0); 3047a5ca864Sbellard break; 3057a5ca864Sbellard case 'h': 3067a5ca864Sbellard usage(argv[0]); 3077a5ca864Sbellard exit(0); 3087a5ca864Sbellard break; 3097a5ca864Sbellard case '?': 310b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Try `%s --help' for more information.", 3117a5ca864Sbellard argv[0]); 3127a5ca864Sbellard } 3137a5ca864Sbellard } 3147a5ca864Sbellard 3157a5ca864Sbellard if ((argc - optind) != 1) { 316b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid number of argument.\n" 3177a5ca864Sbellard "Try `%s --help' for more information.", 3187a5ca864Sbellard argv[0]); 3197a5ca864Sbellard } 3207a5ca864Sbellard 321cd831bd7Sths if (disconnect) { 322cd831bd7Sths fd = open(argv[optind], O_RDWR); 323cd831bd7Sths if (fd == -1) 324cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Cannot open %s", argv[optind]); 325cd831bd7Sths 326cd831bd7Sths nbd_disconnect(fd); 327cd831bd7Sths 328cd831bd7Sths close(fd); 329cd831bd7Sths 330cd831bd7Sths printf("%s disconnected\n", argv[optind]); 331cd831bd7Sths 332cd831bd7Sths return 0; 333cd831bd7Sths } 334cd831bd7Sths 3357a5ca864Sbellard bdrv_init(); 3367a5ca864Sbellard 3377a5ca864Sbellard bs = bdrv_new("hda"); 3387a5ca864Sbellard 339cb7cf0e3SRyota Ozaki if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) { 340cb7cf0e3SRyota Ozaki errno = -ret; 341cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); 342cb7cf0e3SRyota Ozaki } 3437a5ca864Sbellard 3447a5ca864Sbellard fd_size = bs->total_sectors * 512; 3457a5ca864Sbellard 3467a5ca864Sbellard if (partition != -1 && 3477a5ca864Sbellard find_partition(bs, partition, &dev_offset, &fd_size)) 348cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not find partition %d", partition); 3497a5ca864Sbellard 350cd831bd7Sths if (device) { 351cd831bd7Sths pid_t pid; 3523b05a8e9Sths int sock; 3533b05a8e9Sths 354cb7cf0e3SRyota Ozaki /* want to fail before daemonizing */ 355cb7cf0e3SRyota Ozaki if (access(device, R_OK|W_OK) == -1) { 356cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not access '%s'", device); 357cb7cf0e3SRyota Ozaki } 358cb7cf0e3SRyota Ozaki 359f5de141bSAnthony Liguori if (!verbose) { 360f5de141bSAnthony Liguori /* detach client and server */ 361f97742d0SAlexandre Raymond if (qemu_daemon(0, 0) == -1) { 362cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to daemonize"); 363f5de141bSAnthony Liguori } 364f5de141bSAnthony Liguori } 365cd831bd7Sths 366cd831bd7Sths if (socket == NULL) { 36722ff51eeSBlue Swirl snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, 36822ff51eeSBlue Swirl basename(device)); 369cd831bd7Sths socket = sockpath; 370cd831bd7Sths } 371cd831bd7Sths 372cd831bd7Sths pid = fork(); 373cd831bd7Sths if (pid < 0) 374cd831bd7Sths return 1; 375cd831bd7Sths if (pid != 0) { 376cd831bd7Sths off_t size; 377cd831bd7Sths size_t blocksize; 378cd831bd7Sths 379cd831bd7Sths ret = 0; 380cd831bd7Sths bdrv_close(bs); 381cd831bd7Sths 382cd831bd7Sths do { 383cd831bd7Sths sock = unix_socket_outgoing(socket); 384cd831bd7Sths if (sock == -1) { 385cb7cf0e3SRyota Ozaki if (errno != ENOENT && errno != ECONNREFUSED) { 386cb7cf0e3SRyota Ozaki ret = 1; 387cd831bd7Sths goto out; 388cb7cf0e3SRyota Ozaki } 389cd831bd7Sths sleep(1); /* wait children */ 390cd831bd7Sths } 391cd831bd7Sths } while (sock == -1); 392cd831bd7Sths 393cd831bd7Sths fd = open(device, O_RDWR); 394cd831bd7Sths if (fd == -1) { 395cd831bd7Sths ret = 1; 396cd831bd7Sths goto out; 397cd831bd7Sths } 398cd831bd7Sths 3991d45f8b5SLaurent Vivier ret = nbd_receive_negotiate(sock, NULL, &nbdflags, 4001d45f8b5SLaurent Vivier &size, &blocksize); 401cd831bd7Sths if (ret == -1) { 402cd831bd7Sths ret = 1; 403cd831bd7Sths goto out; 404cd831bd7Sths } 405cd831bd7Sths 406*b90fb4b8SPaolo Bonzini ret = nbd_init(fd, sock, nbdflags, size, blocksize); 407cd831bd7Sths if (ret == -1) { 408cd831bd7Sths ret = 1; 409cd831bd7Sths goto out; 410cd831bd7Sths } 411cd831bd7Sths 412cd831bd7Sths printf("NBD device %s is now connected to file %s\n", 413cd831bd7Sths device, argv[optind]); 414cd831bd7Sths 415cd831bd7Sths /* update partition table */ 416cd831bd7Sths 417cd831bd7Sths show_parts(device); 418cd831bd7Sths 419e301b13dSJes Sorensen ret = nbd_client(fd); 420e301b13dSJes Sorensen if (ret) { 421e301b13dSJes Sorensen ret = 1; 422e301b13dSJes Sorensen } 423cd831bd7Sths close(fd); 424cd831bd7Sths out: 425cd831bd7Sths kill(pid, SIGTERM); 426cd831bd7Sths unlink(socket); 427cd831bd7Sths 428cd831bd7Sths return ret; 429cd831bd7Sths } 430cd831bd7Sths /* children */ 431cd831bd7Sths } 432cd831bd7Sths 4337267c094SAnthony Liguori sharing_fds = g_malloc((shared + 1) * sizeof(int)); 4343b05a8e9Sths 435cd831bd7Sths if (socket) { 4363b05a8e9Sths sharing_fds[0] = unix_socket_incoming(socket); 437cd831bd7Sths } else { 4383b05a8e9Sths sharing_fds[0] = tcp_socket_incoming(bindto, port); 439cd831bd7Sths } 440cd831bd7Sths 4413b05a8e9Sths if (sharing_fds[0] == -1) 4427a5ca864Sbellard return 1; 4433b05a8e9Sths max_fd = sharing_fds[0]; 4443b05a8e9Sths nb_fds++; 4457a5ca864Sbellard 44672aef731SChristoph Hellwig data = qemu_blockalign(bs, NBD_BUFFER_SIZE); 4473b05a8e9Sths if (data == NULL) 448b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Cannot allocate data buffer"); 4493b05a8e9Sths 4503b05a8e9Sths do { 4513b05a8e9Sths 4523b05a8e9Sths FD_ZERO(&fds); 4533b05a8e9Sths for (i = 0; i < nb_fds; i++) 4543b05a8e9Sths FD_SET(sharing_fds[i], &fds); 4553b05a8e9Sths 4563b05a8e9Sths ret = select(max_fd + 1, &fds, NULL, NULL, NULL); 4573b05a8e9Sths if (ret == -1) 4583b05a8e9Sths break; 4593b05a8e9Sths 4603b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) 4613b05a8e9Sths ret--; 4623b05a8e9Sths for (i = 1; i < nb_fds && ret; i++) { 4633b05a8e9Sths if (FD_ISSET(sharing_fds[i], &fds)) { 4643b05a8e9Sths if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, 465*b90fb4b8SPaolo Bonzini &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) { 4663b05a8e9Sths close(sharing_fds[i]); 4673b05a8e9Sths nb_fds--; 4683b05a8e9Sths sharing_fds[i] = sharing_fds[nb_fds]; 4693b05a8e9Sths i--; 4703b05a8e9Sths } 4713b05a8e9Sths ret--; 4723b05a8e9Sths } 4733b05a8e9Sths } 4743b05a8e9Sths /* new connection ? */ 4753b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) { 4763b05a8e9Sths if (nb_fds < shared + 1) { 4773b05a8e9Sths sharing_fds[nb_fds] = accept(sharing_fds[0], 4783b05a8e9Sths (struct sockaddr *)&addr, 4793b05a8e9Sths &addr_len); 4803b05a8e9Sths if (sharing_fds[nb_fds] != -1 && 481*b90fb4b8SPaolo Bonzini nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) { 4823b05a8e9Sths if (sharing_fds[nb_fds] > max_fd) 4833b05a8e9Sths max_fd = sharing_fds[nb_fds]; 4843b05a8e9Sths nb_fds++; 4853b05a8e9Sths } 4863b05a8e9Sths } 4873b05a8e9Sths } 48875818250Sths } while (persistent || nb_fds > 1); 489f8a83245SHerve Poussineau qemu_vfree(data); 4907a5ca864Sbellard 4913b05a8e9Sths close(sharing_fds[0]); 4927a5ca864Sbellard bdrv_close(bs); 4937267c094SAnthony Liguori g_free(sharing_fds); 494cd831bd7Sths if (socket) 495cd831bd7Sths unlink(socket); 4967a5ca864Sbellard 4977a5ca864Sbellard return 0; 4987a5ca864Sbellard } 499