make check-block: Use default cache modes
[qemu/ar7.git] / qemu-nbd.c
blobb524b3420f8e20512fb13baea68211aa76d336b7
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu-common.h"
20 #include "block/block.h"
21 #include "block/block_int.h"
22 #include "block/nbd.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/sockets.h"
25 #include "qemu/error-report.h"
26 #include "block/snapshot.h"
27 #include "qapi/util.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <err.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <arpa/inet.h>
38 #include <signal.h>
39 #include <libgen.h>
40 #include <pthread.h>
42 #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
43 #define QEMU_NBD_OPT_CACHE 1
44 #define QEMU_NBD_OPT_AIO 2
45 #define QEMU_NBD_OPT_DISCARD 3
46 #define QEMU_NBD_OPT_DETECT_ZEROES 4
48 static NBDExport *exp;
49 static int verbose;
50 static char *srcpath;
51 static char *sockpath;
52 static int persistent = 0;
53 static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
54 static int shared = 1;
55 static int nb_fds;
57 static void usage(const char *name)
59 (printf) (
60 "Usage: %s [OPTIONS] FILE\n"
61 "QEMU Disk Network Block Device Server\n"
62 "\n"
63 " -h, --help display this help and exit\n"
64 " -V, --version output version information and exit\n"
65 "\n"
66 "Connection properties:\n"
67 " -p, --port=PORT port to listen on (default `%d')\n"
68 " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
69 " -k, --socket=PATH path to the unix socket\n"
70 " (default '"SOCKET_PATH"')\n"
71 " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
72 " -t, --persistent don't exit on the last connection\n"
73 " -v, --verbose display extra debugging information\n"
74 "\n"
75 "Exposing part of the image:\n"
76 " -o, --offset=OFFSET offset into the image\n"
77 " -P, --partition=NUM only expose partition NUM\n"
78 "\n"
79 #ifdef __linux__
80 "Kernel NBD client support:\n"
81 " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
82 " -d, --disconnect disconnect the specified device\n"
83 "\n"
84 #endif
85 "\n"
86 "Block device options:\n"
87 " -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
88 " -r, --read-only export read-only\n"
89 " -s, --snapshot use FILE as an external snapshot, create a temporary\n"
90 " file with backing_file=FILE, redirect the write to\n"
91 " the temporary one\n"
92 " -l, --load-snapshot=SNAPSHOT_PARAM\n"
93 " load an internal snapshot inside FILE and export it\n"
94 " as an read-only device, SNAPSHOT_PARAM format is\n"
95 " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
96 " '[ID_OR_NAME]'\n"
97 " -n, --nocache disable host cache\n"
98 " --cache=MODE set cache mode (none, writeback, ...)\n"
99 #ifdef CONFIG_LINUX_AIO
100 " --aio=MODE set AIO mode (native or threads)\n"
101 #endif
102 " --discard=MODE set discard mode (ignore, unmap)\n"
103 " --detect-zeroes=MODE set detect-zeroes mode (off, on, discard)\n"
104 "\n"
105 "Report bugs to <qemu-devel@nongnu.org>\n"
106 , name, NBD_DEFAULT_PORT, "DEVICE");
109 static void version(const char *name)
111 printf(
112 "%s version 0.0.1\n"
113 "Written by Anthony Liguori.\n"
114 "\n"
115 "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
116 "This is free software; see the source for copying conditions. There is NO\n"
117 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
118 , name);
121 struct partition_record
123 uint8_t bootable;
124 uint8_t start_head;
125 uint32_t start_cylinder;
126 uint8_t start_sector;
127 uint8_t system;
128 uint8_t end_head;
129 uint8_t end_cylinder;
130 uint8_t end_sector;
131 uint32_t start_sector_abs;
132 uint32_t nb_sectors_abs;
135 static void read_partition(uint8_t *p, struct partition_record *r)
137 r->bootable = p[0];
138 r->start_head = p[1];
139 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
140 r->start_sector = p[2] & 0x3f;
141 r->system = p[4];
142 r->end_head = p[5];
143 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
144 r->end_sector = p[6] & 0x3f;
145 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
146 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
149 static int find_partition(BlockDriverState *bs, int partition,
150 off_t *offset, off_t *size)
152 struct partition_record mbr[4];
153 uint8_t data[512];
154 int i;
155 int ext_partnum = 4;
156 int ret;
158 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
159 errno = -ret;
160 err(EXIT_FAILURE, "error while reading");
163 if (data[510] != 0x55 || data[511] != 0xaa) {
164 return -EINVAL;
167 for (i = 0; i < 4; i++) {
168 read_partition(&data[446 + 16 * i], &mbr[i]);
170 if (!mbr[i].nb_sectors_abs)
171 continue;
173 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
174 struct partition_record ext[4];
175 uint8_t data1[512];
176 int j;
178 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
179 errno = -ret;
180 err(EXIT_FAILURE, "error while reading");
183 for (j = 0; j < 4; j++) {
184 read_partition(&data1[446 + 16 * j], &ext[j]);
185 if (!ext[j].nb_sectors_abs)
186 continue;
188 if ((ext_partnum + j + 1) == partition) {
189 *offset = (uint64_t)ext[j].start_sector_abs << 9;
190 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
191 return 0;
194 ext_partnum += 4;
195 } else if ((i + 1) == partition) {
196 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
197 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
198 return 0;
202 return -ENOENT;
205 static void termsig_handler(int signum)
207 state = TERMINATE;
208 qemu_notify_event();
211 static void combine_addr(char *buf, size_t len, const char* address,
212 uint16_t port)
214 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
215 if (strstr(address, ":")) {
216 snprintf(buf, len, "[%s]:%u", address, port);
217 } else {
218 snprintf(buf, len, "%s:%u", address, port);
222 static int tcp_socket_incoming(const char *address, uint16_t port)
224 char address_and_port[128];
225 Error *local_err = NULL;
227 combine_addr(address_and_port, 128, address, port);
228 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
230 if (local_err != NULL) {
231 error_report("%s", error_get_pretty(local_err));
232 error_free(local_err);
234 return fd;
237 static int unix_socket_incoming(const char *path)
239 Error *local_err = NULL;
240 int fd = unix_listen(path, NULL, 0, &local_err);
242 if (local_err != NULL) {
243 error_report("%s", error_get_pretty(local_err));
244 error_free(local_err);
246 return fd;
249 static int unix_socket_outgoing(const char *path)
251 Error *local_err = NULL;
252 int fd = unix_connect(path, &local_err);
254 if (local_err != NULL) {
255 error_report("%s", error_get_pretty(local_err));
256 error_free(local_err);
258 return fd;
261 static void *show_parts(void *arg)
263 char *device = arg;
264 int nbd;
266 /* linux just needs an open() to trigger
267 * the partition table update
268 * but remember to load the module with max_part != 0 :
269 * modprobe nbd max_part=63
271 nbd = open(device, O_RDWR);
272 if (nbd >= 0) {
273 close(nbd);
275 return NULL;
278 static void *nbd_client_thread(void *arg)
280 char *device = arg;
281 off_t size;
282 size_t blocksize;
283 uint32_t nbdflags;
284 int fd, sock;
285 int ret;
286 pthread_t show_parts_thread;
288 sock = unix_socket_outgoing(sockpath);
289 if (sock < 0) {
290 goto out;
293 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
294 &size, &blocksize);
295 if (ret < 0) {
296 goto out_socket;
299 fd = open(device, O_RDWR);
300 if (fd < 0) {
301 /* Linux-only, we can use %m in printf. */
302 fprintf(stderr, "Failed to open %s: %m\n", device);
303 goto out_socket;
306 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
307 if (ret < 0) {
308 goto out_fd;
311 /* update partition table */
312 pthread_create(&show_parts_thread, NULL, show_parts, device);
314 if (verbose) {
315 fprintf(stderr, "NBD device %s is now connected to %s\n",
316 device, srcpath);
317 } else {
318 /* Close stderr so that the qemu-nbd process exits. */
319 dup2(STDOUT_FILENO, STDERR_FILENO);
322 ret = nbd_client(fd);
323 if (ret) {
324 goto out_fd;
326 close(fd);
327 kill(getpid(), SIGTERM);
328 return (void *) EXIT_SUCCESS;
330 out_fd:
331 close(fd);
332 out_socket:
333 closesocket(sock);
334 out:
335 kill(getpid(), SIGTERM);
336 return (void *) EXIT_FAILURE;
339 static int nbd_can_accept(void *opaque)
341 return nb_fds < shared;
344 static void nbd_export_closed(NBDExport *exp)
346 assert(state == TERMINATING);
347 state = TERMINATED;
350 static void nbd_client_closed(NBDClient *client)
352 nb_fds--;
353 if (nb_fds == 0 && !persistent && state == RUNNING) {
354 state = TERMINATE;
356 qemu_notify_event();
357 nbd_client_put(client);
360 static void nbd_accept(void *opaque)
362 int server_fd = (uintptr_t) opaque;
363 struct sockaddr_in addr;
364 socklen_t addr_len = sizeof(addr);
366 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
367 if (fd < 0) {
368 perror("accept");
369 return;
372 if (state >= TERMINATE) {
373 close(fd);
374 return;
377 if (nbd_client_new(exp, fd, nbd_client_closed)) {
378 nb_fds++;
379 } else {
380 shutdown(fd, 2);
381 close(fd);
385 int main(int argc, char **argv)
387 BlockDriverState *bs;
388 BlockDriver *drv;
389 off_t dev_offset = 0;
390 uint32_t nbdflags = 0;
391 bool disconnect = false;
392 const char *bindto = "0.0.0.0";
393 char *device = NULL;
394 int port = NBD_DEFAULT_PORT;
395 off_t fd_size;
396 QemuOpts *sn_opts = NULL;
397 const char *sn_id_or_name = NULL;
398 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
399 struct option lopt[] = {
400 { "help", 0, NULL, 'h' },
401 { "version", 0, NULL, 'V' },
402 { "bind", 1, NULL, 'b' },
403 { "port", 1, NULL, 'p' },
404 { "socket", 1, NULL, 'k' },
405 { "offset", 1, NULL, 'o' },
406 { "read-only", 0, NULL, 'r' },
407 { "partition", 1, NULL, 'P' },
408 { "connect", 1, NULL, 'c' },
409 { "disconnect", 0, NULL, 'd' },
410 { "snapshot", 0, NULL, 's' },
411 { "load-snapshot", 1, NULL, 'l' },
412 { "nocache", 0, NULL, 'n' },
413 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
414 #ifdef CONFIG_LINUX_AIO
415 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
416 #endif
417 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
418 { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES },
419 { "shared", 1, NULL, 'e' },
420 { "format", 1, NULL, 'f' },
421 { "persistent", 0, NULL, 't' },
422 { "verbose", 0, NULL, 'v' },
423 { NULL, 0, NULL, 0 }
425 int ch;
426 int opt_ind = 0;
427 int li;
428 char *end;
429 int flags = BDRV_O_RDWR;
430 int partition = -1;
431 int ret;
432 int fd;
433 bool seen_cache = false;
434 bool seen_discard = false;
435 #ifdef CONFIG_LINUX_AIO
436 bool seen_aio = false;
437 #endif
438 pthread_t client_thread;
439 const char *fmt = NULL;
440 Error *local_err = NULL;
441 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
443 /* The client thread uses SIGTERM to interrupt the server. A signal
444 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
446 struct sigaction sa_sigterm;
447 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
448 sa_sigterm.sa_handler = termsig_handler;
449 sigaction(SIGTERM, &sa_sigterm, NULL);
450 qemu_init_exec_dir(argv[0]);
452 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
453 switch (ch) {
454 case 's':
455 flags |= BDRV_O_SNAPSHOT;
456 break;
457 case 'n':
458 optarg = (char *) "none";
459 /* fallthrough */
460 case QEMU_NBD_OPT_CACHE:
461 if (seen_cache) {
462 errx(EXIT_FAILURE, "-n and --cache can only be specified once");
464 seen_cache = true;
465 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
466 errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
468 break;
469 #ifdef CONFIG_LINUX_AIO
470 case QEMU_NBD_OPT_AIO:
471 if (seen_aio) {
472 errx(EXIT_FAILURE, "--aio can only be specified once");
474 seen_aio = true;
475 if (!strcmp(optarg, "native")) {
476 flags |= BDRV_O_NATIVE_AIO;
477 } else if (!strcmp(optarg, "threads")) {
478 /* this is the default */
479 } else {
480 errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
482 break;
483 #endif
484 case QEMU_NBD_OPT_DISCARD:
485 if (seen_discard) {
486 errx(EXIT_FAILURE, "--discard can only be specified once");
488 seen_discard = true;
489 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
490 errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
492 break;
493 case QEMU_NBD_OPT_DETECT_ZEROES:
494 detect_zeroes =
495 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
496 optarg,
497 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
498 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
499 &local_err);
500 if (local_err) {
501 errx(EXIT_FAILURE, "Failed to parse detect_zeroes mode: %s",
502 error_get_pretty(local_err));
504 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
505 !(flags & BDRV_O_UNMAP)) {
506 errx(EXIT_FAILURE, "setting detect-zeroes to unmap is not allowed "
507 "without setting discard operation to unmap");
509 break;
510 case 'b':
511 bindto = optarg;
512 break;
513 case 'p':
514 li = strtol(optarg, &end, 0);
515 if (*end) {
516 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
518 if (li < 1 || li > 65535) {
519 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
521 port = (uint16_t)li;
522 break;
523 case 'o':
524 dev_offset = strtoll (optarg, &end, 0);
525 if (*end) {
526 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
528 if (dev_offset < 0) {
529 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
531 break;
532 case 'l':
533 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
534 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
535 if (!sn_opts) {
536 errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'",
537 optarg);
539 } else {
540 sn_id_or_name = optarg;
542 /* fall through */
543 case 'r':
544 nbdflags |= NBD_FLAG_READ_ONLY;
545 flags &= ~BDRV_O_RDWR;
546 break;
547 case 'P':
548 partition = strtol(optarg, &end, 0);
549 if (*end) {
550 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
552 if (partition < 1 || partition > 8) {
553 errx(EXIT_FAILURE, "Invalid partition %d", partition);
555 break;
556 case 'k':
557 sockpath = optarg;
558 if (sockpath[0] != '/') {
559 errx(EXIT_FAILURE, "socket path must be absolute\n");
561 break;
562 case 'd':
563 disconnect = true;
564 break;
565 case 'c':
566 device = optarg;
567 break;
568 case 'e':
569 shared = strtol(optarg, &end, 0);
570 if (*end) {
571 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
573 if (shared < 1) {
574 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
576 break;
577 case 'f':
578 fmt = optarg;
579 break;
580 case 't':
581 persistent = 1;
582 break;
583 case 'v':
584 verbose = 1;
585 break;
586 case 'V':
587 version(argv[0]);
588 exit(0);
589 break;
590 case 'h':
591 usage(argv[0]);
592 exit(0);
593 break;
594 case '?':
595 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
596 argv[0]);
600 if ((argc - optind) != 1) {
601 errx(EXIT_FAILURE, "Invalid number of argument.\n"
602 "Try `%s --help' for more information.",
603 argv[0]);
606 if (disconnect) {
607 fd = open(argv[optind], O_RDWR);
608 if (fd < 0) {
609 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
611 nbd_disconnect(fd);
613 close(fd);
615 printf("%s disconnected\n", argv[optind]);
617 return 0;
620 if (device && !verbose) {
621 int stderr_fd[2];
622 pid_t pid;
623 int ret;
625 if (qemu_pipe(stderr_fd) < 0) {
626 err(EXIT_FAILURE, "Error setting up communication pipe");
629 /* Now daemonize, but keep a communication channel open to
630 * print errors and exit with the proper status code.
632 pid = fork();
633 if (pid == 0) {
634 close(stderr_fd[0]);
635 ret = qemu_daemon(1, 0);
637 /* Temporarily redirect stderr to the parent's pipe... */
638 dup2(stderr_fd[1], STDERR_FILENO);
639 if (ret < 0) {
640 err(EXIT_FAILURE, "Failed to daemonize");
643 /* ... close the descriptor we inherited and go on. */
644 close(stderr_fd[1]);
645 } else {
646 bool errors = false;
647 char *buf;
649 /* In the parent. Print error messages from the child until
650 * it closes the pipe.
652 close(stderr_fd[1]);
653 buf = g_malloc(1024);
654 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
655 errors = true;
656 ret = qemu_write_full(STDERR_FILENO, buf, ret);
657 if (ret < 0) {
658 exit(EXIT_FAILURE);
661 if (ret < 0) {
662 err(EXIT_FAILURE, "Cannot read from daemon");
665 /* Usually the daemon should not print any message.
666 * Exit with zero status in that case.
668 exit(errors);
672 if (device != NULL && sockpath == NULL) {
673 sockpath = g_malloc(128);
674 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
677 if (qemu_init_main_loop(&local_err)) {
678 error_report("%s", error_get_pretty(local_err));
679 error_free(local_err);
680 exit(EXIT_FAILURE);
682 bdrv_init();
683 atexit(bdrv_close_all);
685 if (fmt) {
686 drv = bdrv_find_format(fmt);
687 if (!drv) {
688 errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
690 } else {
691 drv = NULL;
694 bs = bdrv_new("hda", &error_abort);
696 srcpath = argv[optind];
697 ret = bdrv_open(&bs, srcpath, NULL, NULL, flags, drv, &local_err);
698 if (ret < 0) {
699 errno = -ret;
700 err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
701 error_get_pretty(local_err));
704 if (sn_opts) {
705 ret = bdrv_snapshot_load_tmp(bs,
706 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
707 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
708 &local_err);
709 } else if (sn_id_or_name) {
710 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
711 &local_err);
713 if (ret < 0) {
714 errno = -ret;
715 err(EXIT_FAILURE,
716 "Failed to load snapshot: %s",
717 error_get_pretty(local_err));
720 bs->detect_zeroes = detect_zeroes;
721 fd_size = bdrv_getlength(bs);
723 if (partition != -1) {
724 ret = find_partition(bs, partition, &dev_offset, &fd_size);
725 if (ret < 0) {
726 errno = -ret;
727 err(EXIT_FAILURE, "Could not find partition %d", partition);
731 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
733 if (sockpath) {
734 fd = unix_socket_incoming(sockpath);
735 } else {
736 fd = tcp_socket_incoming(bindto, port);
739 if (fd < 0) {
740 return 1;
743 if (device) {
744 int ret;
746 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
747 if (ret != 0) {
748 errx(EXIT_FAILURE, "Failed to create client thread: %s",
749 strerror(ret));
751 } else {
752 /* Shut up GCC warnings. */
753 memset(&client_thread, 0, sizeof(client_thread));
756 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
757 (void *)(uintptr_t)fd);
759 /* now when the initialization is (almost) complete, chdir("/")
760 * to free any busy filesystems */
761 if (chdir("/") < 0) {
762 err(EXIT_FAILURE, "Could not chdir to root directory");
765 state = RUNNING;
766 do {
767 main_loop_wait(false);
768 if (state == TERMINATE) {
769 state = TERMINATING;
770 nbd_export_close(exp);
771 nbd_export_put(exp);
772 exp = NULL;
774 } while (state != TERMINATED);
776 bdrv_unref(bs);
777 if (sockpath) {
778 unlink(sockpath);
781 qemu_opts_del(sn_opts);
783 if (device) {
784 void *ret;
785 pthread_join(client_thread, &ret);
786 exit(ret != NULL);
787 } else {
788 exit(EXIT_SUCCESS);