xref: /qemu/qemu-nbd.c (revision 8167ee88)
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
16*8167ee88SBlue 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>
33cd831bd7Sths 
34cd831bd7Sths #define SOCKET_PATH    "/var/lock/qemu-nbd-%s"
357a5ca864Sbellard 
362f726488Sths #define NBD_BUFFER_SIZE (1024*1024)
372f726488Sths 
38b1d8e52eSblueswir1 static int verbose;
397a5ca864Sbellard 
407a5ca864Sbellard static void usage(const char *name)
417a5ca864Sbellard {
427a5ca864Sbellard     printf(
437a5ca864Sbellard "Usage: %s [OPTIONS] FILE\n"
447a5ca864Sbellard "QEMU Disk Network Block Device Server\n"
457a5ca864Sbellard "\n"
467a5ca864Sbellard "  -p, --port=PORT      port to listen on (default `1024')\n"
477a5ca864Sbellard "  -o, --offset=OFFSET  offset into the image\n"
487a5ca864Sbellard "  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
49cd831bd7Sths "  -k, --socket=PATH    path to the unix socket\n"
50cd831bd7Sths "                       (default '"SOCKET_PATH"')\n"
517a5ca864Sbellard "  -r, --read-only      export read-only\n"
527a5ca864Sbellard "  -P, --partition=NUM  only expose partition NUM\n"
532f726488Sths "  -s, --snapshot       use snapshot file\n"
542f726488Sths "  -n, --nocache        disable host cache\n"
55cd831bd7Sths "  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
56cd831bd7Sths "  -d, --disconnect     disconnect the specified device\n"
573b05a8e9Sths "  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
5875818250Sths "  -t, --persistent     don't exit on the last connection\n"
597a5ca864Sbellard "  -v, --verbose        display extra debugging information\n"
607a5ca864Sbellard "  -h, --help           display this help and exit\n"
617a5ca864Sbellard "  -V, --version        output version information and exit\n"
627a5ca864Sbellard "\n"
637a5ca864Sbellard "Report bugs to <anthony@codemonkey.ws>\n"
64cd831bd7Sths     , name, "DEVICE");
657a5ca864Sbellard }
667a5ca864Sbellard 
677a5ca864Sbellard static void version(const char *name)
687a5ca864Sbellard {
697a5ca864Sbellard     printf(
70315bc7aaSths "%s version 0.0.1\n"
717a5ca864Sbellard "Written by Anthony Liguori.\n"
727a5ca864Sbellard "\n"
737a5ca864Sbellard "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
747a5ca864Sbellard "This is free software; see the source for copying conditions.  There is NO\n"
757a5ca864Sbellard "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
76315bc7aaSths     , name);
777a5ca864Sbellard }
787a5ca864Sbellard 
797a5ca864Sbellard struct partition_record
807a5ca864Sbellard {
817a5ca864Sbellard     uint8_t bootable;
827a5ca864Sbellard     uint8_t start_head;
837a5ca864Sbellard     uint32_t start_cylinder;
847a5ca864Sbellard     uint8_t start_sector;
857a5ca864Sbellard     uint8_t system;
867a5ca864Sbellard     uint8_t end_head;
877a5ca864Sbellard     uint8_t end_cylinder;
887a5ca864Sbellard     uint8_t end_sector;
897a5ca864Sbellard     uint32_t start_sector_abs;
907a5ca864Sbellard     uint32_t nb_sectors_abs;
917a5ca864Sbellard };
927a5ca864Sbellard 
937a5ca864Sbellard static void read_partition(uint8_t *p, struct partition_record *r)
947a5ca864Sbellard {
957a5ca864Sbellard     r->bootable = p[0];
967a5ca864Sbellard     r->start_head = p[1];
977a5ca864Sbellard     r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
987a5ca864Sbellard     r->start_sector = p[2] & 0x3f;
997a5ca864Sbellard     r->system = p[4];
1007a5ca864Sbellard     r->end_head = p[5];
1017a5ca864Sbellard     r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
1027a5ca864Sbellard     r->end_sector = p[6] & 0x3f;
1037a5ca864Sbellard     r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
1047a5ca864Sbellard     r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
1057a5ca864Sbellard }
1067a5ca864Sbellard 
1077a5ca864Sbellard static int find_partition(BlockDriverState *bs, int partition,
1087a5ca864Sbellard                           off_t *offset, off_t *size)
1097a5ca864Sbellard {
1107a5ca864Sbellard     struct partition_record mbr[4];
1117a5ca864Sbellard     uint8_t data[512];
1127a5ca864Sbellard     int i;
1137a5ca864Sbellard     int ext_partnum = 4;
1147a5ca864Sbellard 
1157a5ca864Sbellard     if (bdrv_read(bs, 0, data, 1))
1167a5ca864Sbellard         errx(EINVAL, "error while reading");
1177a5ca864Sbellard 
1187a5ca864Sbellard     if (data[510] != 0x55 || data[511] != 0xaa) {
1197a5ca864Sbellard         errno = -EINVAL;
1207a5ca864Sbellard         return -1;
1217a5ca864Sbellard     }
1227a5ca864Sbellard 
1237a5ca864Sbellard     for (i = 0; i < 4; i++) {
1247a5ca864Sbellard         read_partition(&data[446 + 16 * i], &mbr[i]);
1257a5ca864Sbellard 
1267a5ca864Sbellard         if (!mbr[i].nb_sectors_abs)
1277a5ca864Sbellard             continue;
1287a5ca864Sbellard 
1297a5ca864Sbellard         if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
1307a5ca864Sbellard             struct partition_record ext[4];
1317a5ca864Sbellard             uint8_t data1[512];
1327a5ca864Sbellard             int j;
1337a5ca864Sbellard 
1347a5ca864Sbellard             if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
1357a5ca864Sbellard                 errx(EINVAL, "error while reading");
1367a5ca864Sbellard 
1377a5ca864Sbellard             for (j = 0; j < 4; j++) {
1387a5ca864Sbellard                 read_partition(&data1[446 + 16 * j], &ext[j]);
1397a5ca864Sbellard                 if (!ext[j].nb_sectors_abs)
1407a5ca864Sbellard                     continue;
1417a5ca864Sbellard 
1427a5ca864Sbellard                 if ((ext_partnum + j + 1) == partition) {
1437a5ca864Sbellard                     *offset = (uint64_t)ext[j].start_sector_abs << 9;
1447a5ca864Sbellard                     *size = (uint64_t)ext[j].nb_sectors_abs << 9;
1457a5ca864Sbellard                     return 0;
1467a5ca864Sbellard                 }
1477a5ca864Sbellard             }
1487a5ca864Sbellard             ext_partnum += 4;
1497a5ca864Sbellard         } else if ((i + 1) == partition) {
1507a5ca864Sbellard             *offset = (uint64_t)mbr[i].start_sector_abs << 9;
1517a5ca864Sbellard             *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
1527a5ca864Sbellard             return 0;
1537a5ca864Sbellard         }
1547a5ca864Sbellard     }
1557a5ca864Sbellard 
1567a5ca864Sbellard     errno = -ENOENT;
1577a5ca864Sbellard     return -1;
1587a5ca864Sbellard }
1597a5ca864Sbellard 
160cd831bd7Sths static void show_parts(const char *device)
161cd831bd7Sths {
162cd831bd7Sths     if (fork() == 0) {
163cd831bd7Sths         int nbd;
164cd831bd7Sths 
165cd831bd7Sths         /* linux just needs an open() to trigger
166cd831bd7Sths          * the partition table update
167cd831bd7Sths          * but remember to load the module with max_part != 0 :
168cd831bd7Sths          *     modprobe nbd max_part=63
169cd831bd7Sths          */
170cd831bd7Sths         nbd = open(device, O_RDWR);
171cd831bd7Sths         if (nbd != -1)
172cd831bd7Sths               close(nbd);
173cd831bd7Sths         exit(0);
174cd831bd7Sths     }
175cd831bd7Sths }
176cd831bd7Sths 
1777a5ca864Sbellard int main(int argc, char **argv)
1787a5ca864Sbellard {
1797a5ca864Sbellard     BlockDriverState *bs;
1807a5ca864Sbellard     off_t dev_offset = 0;
1817a5ca864Sbellard     off_t offset = 0;
1827a5ca864Sbellard     bool readonly = false;
183cd831bd7Sths     bool disconnect = false;
1847a5ca864Sbellard     const char *bindto = "0.0.0.0";
1857a5ca864Sbellard     int port = 1024;
1867a5ca864Sbellard     struct sockaddr_in addr;
1877a5ca864Sbellard     socklen_t addr_len = sizeof(addr);
1887a5ca864Sbellard     off_t fd_size;
189cd831bd7Sths     char *device = NULL;
190cd831bd7Sths     char *socket = NULL;
191cd831bd7Sths     char sockpath[128];
192d6aa671fSaliguori     const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
1937a5ca864Sbellard     struct option lopt[] = {
1947a5ca864Sbellard         { "help", 0, 0, 'h' },
1957a5ca864Sbellard         { "version", 0, 0, 'V' },
1967a5ca864Sbellard         { "bind", 1, 0, 'b' },
1977a5ca864Sbellard         { "port", 1, 0, 'p' },
198cd831bd7Sths         { "socket", 1, 0, 'k' },
1997a5ca864Sbellard         { "offset", 1, 0, 'o' },
2007a5ca864Sbellard         { "read-only", 0, 0, 'r' },
2017a5ca864Sbellard         { "partition", 1, 0, 'P' },
202cd831bd7Sths         { "connect", 1, 0, 'c' },
203cd831bd7Sths         { "disconnect", 0, 0, 'd' },
2047a5ca864Sbellard         { "snapshot", 0, 0, 's' },
2052f726488Sths         { "nocache", 0, 0, 'n' },
2063b05a8e9Sths         { "shared", 1, 0, 'e' },
20775818250Sths         { "persistent", 0, 0, 't' },
2087a5ca864Sbellard         { "verbose", 0, 0, 'v' },
209975b092bSths         { NULL, 0, 0, 0 }
2107a5ca864Sbellard     };
2117a5ca864Sbellard     int ch;
2127a5ca864Sbellard     int opt_ind = 0;
2137a5ca864Sbellard     int li;
2147a5ca864Sbellard     char *end;
2152f726488Sths     int flags = 0;
2167a5ca864Sbellard     int partition = -1;
217cd831bd7Sths     int ret;
2183b05a8e9Sths     int shared = 1;
2192f726488Sths     uint8_t *data;
2203b05a8e9Sths     fd_set fds;
2213b05a8e9Sths     int *sharing_fds;
2223b05a8e9Sths     int fd;
2233b05a8e9Sths     int i;
2243b05a8e9Sths     int nb_fds = 0;
2253b05a8e9Sths     int max_fd;
22675818250Sths     int persistent = 0;
2277a5ca864Sbellard 
2287a5ca864Sbellard     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
2297a5ca864Sbellard         switch (ch) {
2307a5ca864Sbellard         case 's':
2312f726488Sths             flags |= BDRV_O_SNAPSHOT;
2322f726488Sths             break;
2332f726488Sths         case 'n':
2349f7965c7Saliguori             flags |= BDRV_O_NOCACHE;
2357a5ca864Sbellard             break;
2367a5ca864Sbellard         case 'b':
2377a5ca864Sbellard             bindto = optarg;
2387a5ca864Sbellard             break;
2397a5ca864Sbellard         case 'p':
2407a5ca864Sbellard             li = strtol(optarg, &end, 0);
2417a5ca864Sbellard             if (*end) {
2427a5ca864Sbellard                 errx(EINVAL, "Invalid port `%s'", optarg);
2437a5ca864Sbellard             }
2447a5ca864Sbellard             if (li < 1 || li > 65535) {
2457a5ca864Sbellard                 errx(EINVAL, "Port out of range `%s'", optarg);
2467a5ca864Sbellard             }
2477a5ca864Sbellard             port = (uint16_t)li;
2487a5ca864Sbellard             break;
2497a5ca864Sbellard         case 'o':
2507a5ca864Sbellard                 dev_offset = strtoll (optarg, &end, 0);
2517a5ca864Sbellard             if (*end) {
2527a5ca864Sbellard                 errx(EINVAL, "Invalid offset `%s'", optarg);
2537a5ca864Sbellard             }
2547a5ca864Sbellard             if (dev_offset < 0) {
2557a5ca864Sbellard                 errx(EINVAL, "Offset must be positive `%s'", optarg);
2567a5ca864Sbellard             }
2577a5ca864Sbellard             break;
2587a5ca864Sbellard         case 'r':
2597a5ca864Sbellard             readonly = true;
2607a5ca864Sbellard             break;
2617a5ca864Sbellard         case 'P':
2627a5ca864Sbellard             partition = strtol(optarg, &end, 0);
2637a5ca864Sbellard             if (*end)
2647a5ca864Sbellard                 errx(EINVAL, "Invalid partition `%s'", optarg);
2657a5ca864Sbellard             if (partition < 1 || partition > 8)
2667a5ca864Sbellard                 errx(EINVAL, "Invalid partition %d", partition);
2677a5ca864Sbellard             break;
268cd831bd7Sths         case 'k':
269cd831bd7Sths             socket = optarg;
270cd831bd7Sths             if (socket[0] != '/')
271cd831bd7Sths                 errx(EINVAL, "socket path must be absolute\n");
272cd831bd7Sths             break;
273cd831bd7Sths         case 'd':
274cd831bd7Sths             disconnect = true;
275cd831bd7Sths             break;
276cd831bd7Sths         case 'c':
277cd831bd7Sths             device = optarg;
278cd831bd7Sths             break;
2793b05a8e9Sths         case 'e':
2803b05a8e9Sths             shared = strtol(optarg, &end, 0);
2813b05a8e9Sths             if (*end) {
2823b05a8e9Sths                 errx(EINVAL, "Invalid shared device number '%s'", optarg);
2833b05a8e9Sths             }
2843b05a8e9Sths             if (shared < 1) {
2853b05a8e9Sths                 errx(EINVAL, "Shared device number must be greater than 0\n");
2863b05a8e9Sths             }
2873b05a8e9Sths             break;
28875818250Sths 	case 't':
28975818250Sths 	    persistent = 1;
29075818250Sths 	    break;
2917a5ca864Sbellard         case 'v':
2927a5ca864Sbellard             verbose = 1;
2937a5ca864Sbellard             break;
2947a5ca864Sbellard         case 'V':
2957a5ca864Sbellard             version(argv[0]);
2967a5ca864Sbellard             exit(0);
2977a5ca864Sbellard             break;
2987a5ca864Sbellard         case 'h':
2997a5ca864Sbellard             usage(argv[0]);
3007a5ca864Sbellard             exit(0);
3017a5ca864Sbellard             break;
3027a5ca864Sbellard         case '?':
3037a5ca864Sbellard             errx(EINVAL, "Try `%s --help' for more information.",
3047a5ca864Sbellard                  argv[0]);
3057a5ca864Sbellard         }
3067a5ca864Sbellard     }
3077a5ca864Sbellard 
3087a5ca864Sbellard     if ((argc - optind) != 1) {
3097a5ca864Sbellard         errx(EINVAL, "Invalid number of argument.\n"
3107a5ca864Sbellard              "Try `%s --help' for more information.",
3117a5ca864Sbellard              argv[0]);
3127a5ca864Sbellard     }
3137a5ca864Sbellard 
314cd831bd7Sths     if (disconnect) {
315cd831bd7Sths         fd = open(argv[optind], O_RDWR);
316cd831bd7Sths         if (fd == -1)
317cd831bd7Sths             errx(errno, "Cannot open %s", argv[optind]);
318cd831bd7Sths 
319cd831bd7Sths         nbd_disconnect(fd);
320cd831bd7Sths 
321cd831bd7Sths         close(fd);
322cd831bd7Sths 
323cd831bd7Sths         printf("%s disconnected\n", argv[optind]);
324cd831bd7Sths 
325cd831bd7Sths 	return 0;
326cd831bd7Sths     }
327cd831bd7Sths 
3287a5ca864Sbellard     bdrv_init();
3297a5ca864Sbellard 
3307a5ca864Sbellard     bs = bdrv_new("hda");
3317a5ca864Sbellard     if (bs == NULL)
3327a5ca864Sbellard         return 1;
3337a5ca864Sbellard 
3342f726488Sths     if (bdrv_open(bs, argv[optind], flags) == -1)
3357a5ca864Sbellard         return 1;
3367a5ca864Sbellard 
3377a5ca864Sbellard     fd_size = bs->total_sectors * 512;
3387a5ca864Sbellard 
3397a5ca864Sbellard     if (partition != -1 &&
3407a5ca864Sbellard         find_partition(bs, partition, &dev_offset, &fd_size))
3417a5ca864Sbellard         errx(errno, "Could not find partition %d", partition);
3427a5ca864Sbellard 
343cd831bd7Sths     if (device) {
344cd831bd7Sths         pid_t pid;
3453b05a8e9Sths         int sock;
3463b05a8e9Sths 
347f5de141bSAnthony Liguori         if (!verbose) {
348f5de141bSAnthony Liguori             /* detach client and server */
349f5de141bSAnthony Liguori             if (daemon(0, 0) == -1) {
350f5de141bSAnthony Liguori                 errx(errno, "Failed to daemonize");
351f5de141bSAnthony Liguori             }
352f5de141bSAnthony Liguori         }
353cd831bd7Sths 
354cd831bd7Sths         if (socket == NULL) {
355cd831bd7Sths             sprintf(sockpath, SOCKET_PATH, basename(device));
356cd831bd7Sths             socket = sockpath;
357cd831bd7Sths         }
358cd831bd7Sths 
359cd831bd7Sths         pid = fork();
360cd831bd7Sths         if (pid < 0)
361cd831bd7Sths             return 1;
362cd831bd7Sths         if (pid != 0) {
363cd831bd7Sths             off_t size;
364cd831bd7Sths             size_t blocksize;
365cd831bd7Sths 
366cd831bd7Sths             ret = 0;
367cd831bd7Sths             bdrv_close(bs);
368cd831bd7Sths 
369cd831bd7Sths             do {
370cd831bd7Sths                 sock = unix_socket_outgoing(socket);
371cd831bd7Sths                 if (sock == -1) {
372cd831bd7Sths                     if (errno != ENOENT && errno != ECONNREFUSED)
373cd831bd7Sths                         goto out;
374cd831bd7Sths                     sleep(1);	/* wait children */
375cd831bd7Sths                 }
376cd831bd7Sths             } while (sock == -1);
377cd831bd7Sths 
378cd831bd7Sths             fd = open(device, O_RDWR);
379cd831bd7Sths             if (fd == -1) {
380cd831bd7Sths                 ret = 1;
381cd831bd7Sths                 goto out;
382cd831bd7Sths             }
383cd831bd7Sths 
384cd831bd7Sths             ret = nbd_receive_negotiate(sock, &size, &blocksize);
385cd831bd7Sths             if (ret == -1) {
386cd831bd7Sths                 ret = 1;
387cd831bd7Sths                 goto out;
388cd831bd7Sths             }
389cd831bd7Sths 
390cd831bd7Sths             ret = nbd_init(fd, sock, size, blocksize);
391cd831bd7Sths             if (ret == -1) {
392cd831bd7Sths                 ret = 1;
393cd831bd7Sths                 goto out;
394cd831bd7Sths             }
395cd831bd7Sths 
396cd831bd7Sths             printf("NBD device %s is now connected to file %s\n",
397cd831bd7Sths                     device, argv[optind]);
398cd831bd7Sths 
399cd831bd7Sths 	    /* update partition table */
400cd831bd7Sths 
401cd831bd7Sths             show_parts(device);
402cd831bd7Sths 
403cd831bd7Sths             nbd_client(fd, sock);
404cd831bd7Sths             close(fd);
405cd831bd7Sths  out:
406cd831bd7Sths             kill(pid, SIGTERM);
407cd831bd7Sths             unlink(socket);
408cd831bd7Sths 
409cd831bd7Sths             return ret;
410cd831bd7Sths         }
411cd831bd7Sths         /* children */
412cd831bd7Sths     }
413cd831bd7Sths 
4143b05a8e9Sths     sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
4153b05a8e9Sths 
416cd831bd7Sths     if (socket) {
4173b05a8e9Sths         sharing_fds[0] = unix_socket_incoming(socket);
418cd831bd7Sths     } else {
4193b05a8e9Sths         sharing_fds[0] = tcp_socket_incoming(bindto, port);
420cd831bd7Sths     }
421cd831bd7Sths 
4223b05a8e9Sths     if (sharing_fds[0] == -1)
4237a5ca864Sbellard         return 1;
4243b05a8e9Sths     max_fd = sharing_fds[0];
4253b05a8e9Sths     nb_fds++;
4267a5ca864Sbellard 
4272f726488Sths     data = qemu_memalign(512, NBD_BUFFER_SIZE);
4283b05a8e9Sths     if (data == NULL)
4293b05a8e9Sths         errx(ENOMEM, "Cannot allocate data buffer");
4303b05a8e9Sths 
4313b05a8e9Sths     do {
4323b05a8e9Sths 
4333b05a8e9Sths         FD_ZERO(&fds);
4343b05a8e9Sths         for (i = 0; i < nb_fds; i++)
4353b05a8e9Sths             FD_SET(sharing_fds[i], &fds);
4363b05a8e9Sths 
4373b05a8e9Sths         ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
4383b05a8e9Sths         if (ret == -1)
4393b05a8e9Sths             break;
4403b05a8e9Sths 
4413b05a8e9Sths         if (FD_ISSET(sharing_fds[0], &fds))
4423b05a8e9Sths             ret--;
4433b05a8e9Sths         for (i = 1; i < nb_fds && ret; i++) {
4443b05a8e9Sths             if (FD_ISSET(sharing_fds[i], &fds)) {
4453b05a8e9Sths                 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
4463b05a8e9Sths                     &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
4473b05a8e9Sths                     close(sharing_fds[i]);
4483b05a8e9Sths                     nb_fds--;
4493b05a8e9Sths                     sharing_fds[i] = sharing_fds[nb_fds];
4503b05a8e9Sths                     i--;
4513b05a8e9Sths                 }
4523b05a8e9Sths                 ret--;
4533b05a8e9Sths             }
4543b05a8e9Sths         }
4553b05a8e9Sths         /* new connection ? */
4563b05a8e9Sths         if (FD_ISSET(sharing_fds[0], &fds)) {
4573b05a8e9Sths             if (nb_fds < shared + 1) {
4583b05a8e9Sths                 sharing_fds[nb_fds] = accept(sharing_fds[0],
4593b05a8e9Sths                                              (struct sockaddr *)&addr,
4603b05a8e9Sths                                              &addr_len);
4613b05a8e9Sths                 if (sharing_fds[nb_fds] != -1 &&
46227982661Saliguori                     nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
4633b05a8e9Sths                         if (sharing_fds[nb_fds] > max_fd)
4643b05a8e9Sths                             max_fd = sharing_fds[nb_fds];
4653b05a8e9Sths                         nb_fds++;
4663b05a8e9Sths                 }
4673b05a8e9Sths             }
4683b05a8e9Sths         }
46975818250Sths     } while (persistent || nb_fds > 1);
4702f726488Sths     qemu_free(data);
4717a5ca864Sbellard 
4723b05a8e9Sths     close(sharing_fds[0]);
4737a5ca864Sbellard     bdrv_close(bs);
4743b05a8e9Sths     qemu_free(sharing_fds);
475cd831bd7Sths     if (socket)
476cd831bd7Sths         unlink(socket);
4777a5ca864Sbellard 
4787a5ca864Sbellard     return 0;
4797a5ca864Sbellard }
480