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" 477a5ca864Sbellard " -p, --port=PORT port to listen on (default `1024')\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" 65cd831bd7Sths , name, "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; 1157a5ca864Sbellard 1167a5ca864Sbellard if (bdrv_read(bs, 0, data, 1)) 1177a5ca864Sbellard errx(EINVAL, "error while reading"); 1187a5ca864Sbellard 1197a5ca864Sbellard if (data[510] != 0x55 || data[511] != 0xaa) { 1207a5ca864Sbellard errno = -EINVAL; 1217a5ca864Sbellard return -1; 1227a5ca864Sbellard } 1237a5ca864Sbellard 1247a5ca864Sbellard for (i = 0; i < 4; i++) { 1257a5ca864Sbellard read_partition(&data[446 + 16 * i], &mbr[i]); 1267a5ca864Sbellard 1277a5ca864Sbellard if (!mbr[i].nb_sectors_abs) 1287a5ca864Sbellard continue; 1297a5ca864Sbellard 1307a5ca864Sbellard if (mbr[i].system == 0xF || mbr[i].system == 0x5) { 1317a5ca864Sbellard struct partition_record ext[4]; 1327a5ca864Sbellard uint8_t data1[512]; 1337a5ca864Sbellard int j; 1347a5ca864Sbellard 1357a5ca864Sbellard if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) 1367a5ca864Sbellard errx(EINVAL, "error while reading"); 1377a5ca864Sbellard 1387a5ca864Sbellard for (j = 0; j < 4; j++) { 1397a5ca864Sbellard read_partition(&data1[446 + 16 * j], &ext[j]); 1407a5ca864Sbellard if (!ext[j].nb_sectors_abs) 1417a5ca864Sbellard continue; 1427a5ca864Sbellard 1437a5ca864Sbellard if ((ext_partnum + j + 1) == partition) { 1447a5ca864Sbellard *offset = (uint64_t)ext[j].start_sector_abs << 9; 1457a5ca864Sbellard *size = (uint64_t)ext[j].nb_sectors_abs << 9; 1467a5ca864Sbellard return 0; 1477a5ca864Sbellard } 1487a5ca864Sbellard } 1497a5ca864Sbellard ext_partnum += 4; 1507a5ca864Sbellard } else if ((i + 1) == partition) { 1517a5ca864Sbellard *offset = (uint64_t)mbr[i].start_sector_abs << 9; 1527a5ca864Sbellard *size = (uint64_t)mbr[i].nb_sectors_abs << 9; 1537a5ca864Sbellard return 0; 1547a5ca864Sbellard } 1557a5ca864Sbellard } 1567a5ca864Sbellard 1577a5ca864Sbellard errno = -ENOENT; 1587a5ca864Sbellard return -1; 1597a5ca864Sbellard } 1607a5ca864Sbellard 161cd831bd7Sths static void show_parts(const char *device) 162cd831bd7Sths { 163cd831bd7Sths if (fork() == 0) { 164cd831bd7Sths int nbd; 165cd831bd7Sths 166cd831bd7Sths /* linux just needs an open() to trigger 167cd831bd7Sths * the partition table update 168cd831bd7Sths * but remember to load the module with max_part != 0 : 169cd831bd7Sths * modprobe nbd max_part=63 170cd831bd7Sths */ 171cd831bd7Sths nbd = open(device, O_RDWR); 172cd831bd7Sths if (nbd != -1) 173cd831bd7Sths close(nbd); 174cd831bd7Sths exit(0); 175cd831bd7Sths } 176cd831bd7Sths } 177cd831bd7Sths 1787a5ca864Sbellard int main(int argc, char **argv) 1797a5ca864Sbellard { 1807a5ca864Sbellard BlockDriverState *bs; 1817a5ca864Sbellard off_t dev_offset = 0; 1827a5ca864Sbellard off_t offset = 0; 1837a5ca864Sbellard bool readonly = false; 184cd831bd7Sths bool disconnect = false; 1857a5ca864Sbellard const char *bindto = "0.0.0.0"; 1867a5ca864Sbellard int port = 1024; 1877a5ca864Sbellard struct sockaddr_in addr; 1887a5ca864Sbellard socklen_t addr_len = sizeof(addr); 1897a5ca864Sbellard off_t fd_size; 190cd831bd7Sths char *device = NULL; 191cd831bd7Sths char *socket = NULL; 192cd831bd7Sths char sockpath[128]; 193d6aa671fSaliguori const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; 1947a5ca864Sbellard struct option lopt[] = { 195660f11beSBlue Swirl { "help", 0, NULL, 'h' }, 196660f11beSBlue Swirl { "version", 0, NULL, 'V' }, 197660f11beSBlue Swirl { "bind", 1, NULL, 'b' }, 198660f11beSBlue Swirl { "port", 1, NULL, 'p' }, 199660f11beSBlue Swirl { "socket", 1, NULL, 'k' }, 200660f11beSBlue Swirl { "offset", 1, NULL, 'o' }, 201660f11beSBlue Swirl { "read-only", 0, NULL, 'r' }, 202660f11beSBlue Swirl { "partition", 1, NULL, 'P' }, 203660f11beSBlue Swirl { "connect", 1, NULL, 'c' }, 204660f11beSBlue Swirl { "disconnect", 0, NULL, 'd' }, 205660f11beSBlue Swirl { "snapshot", 0, NULL, 's' }, 206660f11beSBlue Swirl { "nocache", 0, NULL, 'n' }, 207660f11beSBlue Swirl { "shared", 1, NULL, 'e' }, 208660f11beSBlue Swirl { "persistent", 0, NULL, 't' }, 209660f11beSBlue Swirl { "verbose", 0, NULL, 'v' }, 210660f11beSBlue Swirl { NULL, 0, NULL, 0 } 2117a5ca864Sbellard }; 2127a5ca864Sbellard int ch; 2137a5ca864Sbellard int opt_ind = 0; 2147a5ca864Sbellard int li; 2157a5ca864Sbellard char *end; 216*f5edb014SNaphtali Sprei int flags = BDRV_O_RDWR; 2177a5ca864Sbellard int partition = -1; 218cd831bd7Sths int ret; 2193b05a8e9Sths int shared = 1; 2202f726488Sths uint8_t *data; 2213b05a8e9Sths fd_set fds; 2223b05a8e9Sths int *sharing_fds; 2233b05a8e9Sths int fd; 2243b05a8e9Sths int i; 2253b05a8e9Sths int nb_fds = 0; 2263b05a8e9Sths int max_fd; 22775818250Sths int persistent = 0; 2287a5ca864Sbellard 2297a5ca864Sbellard while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { 2307a5ca864Sbellard switch (ch) { 2317a5ca864Sbellard case 's': 2322f726488Sths flags |= BDRV_O_SNAPSHOT; 2332f726488Sths break; 2342f726488Sths case 'n': 2359f7965c7Saliguori flags |= BDRV_O_NOCACHE; 2367a5ca864Sbellard break; 2377a5ca864Sbellard case 'b': 2387a5ca864Sbellard bindto = optarg; 2397a5ca864Sbellard break; 2407a5ca864Sbellard case 'p': 2417a5ca864Sbellard li = strtol(optarg, &end, 0); 2427a5ca864Sbellard if (*end) { 2437a5ca864Sbellard errx(EINVAL, "Invalid port `%s'", optarg); 2447a5ca864Sbellard } 2457a5ca864Sbellard if (li < 1 || li > 65535) { 2467a5ca864Sbellard errx(EINVAL, "Port out of range `%s'", optarg); 2477a5ca864Sbellard } 2487a5ca864Sbellard port = (uint16_t)li; 2497a5ca864Sbellard break; 2507a5ca864Sbellard case 'o': 2517a5ca864Sbellard dev_offset = strtoll (optarg, &end, 0); 2527a5ca864Sbellard if (*end) { 2537a5ca864Sbellard errx(EINVAL, "Invalid offset `%s'", optarg); 2547a5ca864Sbellard } 2557a5ca864Sbellard if (dev_offset < 0) { 2567a5ca864Sbellard errx(EINVAL, "Offset must be positive `%s'", optarg); 2577a5ca864Sbellard } 2587a5ca864Sbellard break; 2597a5ca864Sbellard case 'r': 2607a5ca864Sbellard readonly = true; 2617a5ca864Sbellard break; 2627a5ca864Sbellard case 'P': 2637a5ca864Sbellard partition = strtol(optarg, &end, 0); 2647a5ca864Sbellard if (*end) 2657a5ca864Sbellard errx(EINVAL, "Invalid partition `%s'", optarg); 2667a5ca864Sbellard if (partition < 1 || partition > 8) 2677a5ca864Sbellard errx(EINVAL, "Invalid partition %d", partition); 2687a5ca864Sbellard break; 269cd831bd7Sths case 'k': 270cd831bd7Sths socket = optarg; 271cd831bd7Sths if (socket[0] != '/') 272cd831bd7Sths errx(EINVAL, "socket path must be absolute\n"); 273cd831bd7Sths break; 274cd831bd7Sths case 'd': 275cd831bd7Sths disconnect = true; 276cd831bd7Sths break; 277cd831bd7Sths case 'c': 278cd831bd7Sths device = optarg; 279cd831bd7Sths break; 2803b05a8e9Sths case 'e': 2813b05a8e9Sths shared = strtol(optarg, &end, 0); 2823b05a8e9Sths if (*end) { 2833b05a8e9Sths errx(EINVAL, "Invalid shared device number '%s'", optarg); 2843b05a8e9Sths } 2853b05a8e9Sths if (shared < 1) { 2863b05a8e9Sths errx(EINVAL, "Shared device number must be greater than 0\n"); 2873b05a8e9Sths } 2883b05a8e9Sths break; 28975818250Sths case 't': 29075818250Sths persistent = 1; 29175818250Sths break; 2927a5ca864Sbellard case 'v': 2937a5ca864Sbellard verbose = 1; 2947a5ca864Sbellard break; 2957a5ca864Sbellard case 'V': 2967a5ca864Sbellard version(argv[0]); 2977a5ca864Sbellard exit(0); 2987a5ca864Sbellard break; 2997a5ca864Sbellard case 'h': 3007a5ca864Sbellard usage(argv[0]); 3017a5ca864Sbellard exit(0); 3027a5ca864Sbellard break; 3037a5ca864Sbellard case '?': 3047a5ca864Sbellard errx(EINVAL, "Try `%s --help' for more information.", 3057a5ca864Sbellard argv[0]); 3067a5ca864Sbellard } 3077a5ca864Sbellard } 3087a5ca864Sbellard 3097a5ca864Sbellard if ((argc - optind) != 1) { 3107a5ca864Sbellard errx(EINVAL, "Invalid number of argument.\n" 3117a5ca864Sbellard "Try `%s --help' for more information.", 3127a5ca864Sbellard argv[0]); 3137a5ca864Sbellard } 3147a5ca864Sbellard 315cd831bd7Sths if (disconnect) { 316cd831bd7Sths fd = open(argv[optind], O_RDWR); 317cd831bd7Sths if (fd == -1) 318cd831bd7Sths errx(errno, "Cannot open %s", argv[optind]); 319cd831bd7Sths 320cd831bd7Sths nbd_disconnect(fd); 321cd831bd7Sths 322cd831bd7Sths close(fd); 323cd831bd7Sths 324cd831bd7Sths printf("%s disconnected\n", argv[optind]); 325cd831bd7Sths 326cd831bd7Sths return 0; 327cd831bd7Sths } 328cd831bd7Sths 3297a5ca864Sbellard bdrv_init(); 3307a5ca864Sbellard 3317a5ca864Sbellard bs = bdrv_new("hda"); 3327a5ca864Sbellard if (bs == NULL) 3337a5ca864Sbellard return 1; 3347a5ca864Sbellard 3352f726488Sths if (bdrv_open(bs, argv[optind], flags) == -1) 3367a5ca864Sbellard return 1; 3377a5ca864Sbellard 3387a5ca864Sbellard fd_size = bs->total_sectors * 512; 3397a5ca864Sbellard 3407a5ca864Sbellard if (partition != -1 && 3417a5ca864Sbellard find_partition(bs, partition, &dev_offset, &fd_size)) 3427a5ca864Sbellard errx(errno, "Could not find partition %d", partition); 3437a5ca864Sbellard 344cd831bd7Sths if (device) { 345cd831bd7Sths pid_t pid; 3463b05a8e9Sths int sock; 3473b05a8e9Sths 348f5de141bSAnthony Liguori if (!verbose) { 349f5de141bSAnthony Liguori /* detach client and server */ 350f5de141bSAnthony Liguori if (daemon(0, 0) == -1) { 351f5de141bSAnthony Liguori errx(errno, "Failed to daemonize"); 352f5de141bSAnthony Liguori } 353f5de141bSAnthony Liguori } 354cd831bd7Sths 355cd831bd7Sths if (socket == NULL) { 35622ff51eeSBlue Swirl snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, 35722ff51eeSBlue Swirl basename(device)); 358cd831bd7Sths socket = sockpath; 359cd831bd7Sths } 360cd831bd7Sths 361cd831bd7Sths pid = fork(); 362cd831bd7Sths if (pid < 0) 363cd831bd7Sths return 1; 364cd831bd7Sths if (pid != 0) { 365cd831bd7Sths off_t size; 366cd831bd7Sths size_t blocksize; 367cd831bd7Sths 368cd831bd7Sths ret = 0; 369cd831bd7Sths bdrv_close(bs); 370cd831bd7Sths 371cd831bd7Sths do { 372cd831bd7Sths sock = unix_socket_outgoing(socket); 373cd831bd7Sths if (sock == -1) { 374cd831bd7Sths if (errno != ENOENT && errno != ECONNREFUSED) 375cd831bd7Sths goto out; 376cd831bd7Sths sleep(1); /* wait children */ 377cd831bd7Sths } 378cd831bd7Sths } while (sock == -1); 379cd831bd7Sths 380cd831bd7Sths fd = open(device, O_RDWR); 381cd831bd7Sths if (fd == -1) { 382cd831bd7Sths ret = 1; 383cd831bd7Sths goto out; 384cd831bd7Sths } 385cd831bd7Sths 386cd831bd7Sths ret = nbd_receive_negotiate(sock, &size, &blocksize); 387cd831bd7Sths if (ret == -1) { 388cd831bd7Sths ret = 1; 389cd831bd7Sths goto out; 390cd831bd7Sths } 391cd831bd7Sths 392cd831bd7Sths ret = nbd_init(fd, sock, size, blocksize); 393cd831bd7Sths if (ret == -1) { 394cd831bd7Sths ret = 1; 395cd831bd7Sths goto out; 396cd831bd7Sths } 397cd831bd7Sths 398cd831bd7Sths printf("NBD device %s is now connected to file %s\n", 399cd831bd7Sths device, argv[optind]); 400cd831bd7Sths 401cd831bd7Sths /* update partition table */ 402cd831bd7Sths 403cd831bd7Sths show_parts(device); 404cd831bd7Sths 405cd831bd7Sths nbd_client(fd, sock); 406cd831bd7Sths close(fd); 407cd831bd7Sths out: 408cd831bd7Sths kill(pid, SIGTERM); 409cd831bd7Sths unlink(socket); 410cd831bd7Sths 411cd831bd7Sths return ret; 412cd831bd7Sths } 413cd831bd7Sths /* children */ 414cd831bd7Sths } 415cd831bd7Sths 4163b05a8e9Sths sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); 4173b05a8e9Sths 418cd831bd7Sths if (socket) { 4193b05a8e9Sths sharing_fds[0] = unix_socket_incoming(socket); 420cd831bd7Sths } else { 4213b05a8e9Sths sharing_fds[0] = tcp_socket_incoming(bindto, port); 422cd831bd7Sths } 423cd831bd7Sths 4243b05a8e9Sths if (sharing_fds[0] == -1) 4257a5ca864Sbellard return 1; 4263b05a8e9Sths max_fd = sharing_fds[0]; 4273b05a8e9Sths nb_fds++; 4287a5ca864Sbellard 4292f726488Sths data = qemu_memalign(512, NBD_BUFFER_SIZE); 4303b05a8e9Sths if (data == NULL) 4313b05a8e9Sths errx(ENOMEM, "Cannot allocate data buffer"); 4323b05a8e9Sths 4333b05a8e9Sths do { 4343b05a8e9Sths 4353b05a8e9Sths FD_ZERO(&fds); 4363b05a8e9Sths for (i = 0; i < nb_fds; i++) 4373b05a8e9Sths FD_SET(sharing_fds[i], &fds); 4383b05a8e9Sths 4393b05a8e9Sths ret = select(max_fd + 1, &fds, NULL, NULL, NULL); 4403b05a8e9Sths if (ret == -1) 4413b05a8e9Sths break; 4423b05a8e9Sths 4433b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) 4443b05a8e9Sths ret--; 4453b05a8e9Sths for (i = 1; i < nb_fds && ret; i++) { 4463b05a8e9Sths if (FD_ISSET(sharing_fds[i], &fds)) { 4473b05a8e9Sths if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, 4483b05a8e9Sths &offset, readonly, data, NBD_BUFFER_SIZE) != 0) { 4493b05a8e9Sths close(sharing_fds[i]); 4503b05a8e9Sths nb_fds--; 4513b05a8e9Sths sharing_fds[i] = sharing_fds[nb_fds]; 4523b05a8e9Sths i--; 4533b05a8e9Sths } 4543b05a8e9Sths ret--; 4553b05a8e9Sths } 4563b05a8e9Sths } 4573b05a8e9Sths /* new connection ? */ 4583b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) { 4593b05a8e9Sths if (nb_fds < shared + 1) { 4603b05a8e9Sths sharing_fds[nb_fds] = accept(sharing_fds[0], 4613b05a8e9Sths (struct sockaddr *)&addr, 4623b05a8e9Sths &addr_len); 4633b05a8e9Sths if (sharing_fds[nb_fds] != -1 && 46427982661Saliguori nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) { 4653b05a8e9Sths if (sharing_fds[nb_fds] > max_fd) 4663b05a8e9Sths max_fd = sharing_fds[nb_fds]; 4673b05a8e9Sths nb_fds++; 4683b05a8e9Sths } 4693b05a8e9Sths } 4703b05a8e9Sths } 47175818250Sths } while (persistent || nb_fds > 1); 4722f726488Sths qemu_free(data); 4737a5ca864Sbellard 4743b05a8e9Sths close(sharing_fds[0]); 4757a5ca864Sbellard bdrv_close(bs); 4763b05a8e9Sths qemu_free(sharing_fds); 477cd831bd7Sths if (socket) 478cd831bd7Sths unlink(socket); 4797a5ca864Sbellard 4807a5ca864Sbellard return 0; 4817a5ca864Sbellard } 482