blob: 0d513cb38c88b022691a3893cf48650ea4a84298 [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Markus Armbrustera8d25322019-05-23 16:35:08 +020024#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +010028#include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
Peter Lievenb3838a42014-08-13 19:20:18 +020029#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010031#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010034#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000035#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010036#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030037#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010038#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080039#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010040#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000042#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000043#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000044#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030046#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050047#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000048
Eric Blake3c1fa352018-12-15 07:53:08 -060049#ifdef __linux__
50#define HAVE_NBD_DEVICE 1
51#else
52#define HAVE_NBD_DEVICE 0
53#endif
54
Peter Lieven713cc672014-08-13 19:20:19 +020055#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000056#define QEMU_NBD_OPT_CACHE 256
57#define QEMU_NBD_OPT_AIO 257
58#define QEMU_NBD_OPT_DISCARD 258
59#define QEMU_NBD_OPT_DETECT_ZEROES 259
60#define QEMU_NBD_OPT_OBJECT 260
61#define QEMU_NBD_OPT_TLSCREDS 261
62#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020063#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000064#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020065#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000066
Eric Blakebd31c212016-05-06 10:26:42 -060067#define MBR_SIZE 512
68
blueswir1b1d8e522008-10-26 13:43:07 +000069static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010070static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020071static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020072static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020073static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020074static int shared = 1;
75static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000076static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000077static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000078static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000079
80static void usage(const char *name)
81{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000083"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060084" or: %s -L [OPTIONS]\n"
85"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000086"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020087" -h, --help display this help and exit\n"
88" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000089"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020090"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -p, --port=PORT port to listen on (default `%d')\n"
92" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
93" -k, --socket=PATH path to the unix socket\n"
94" (default '"SOCKET_PATH"')\n"
95" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
96" -t, --persistent don't exit on the last connection\n"
97" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030098" -x, --export-name=NAME expose export by name (default is empty string)\n"
99" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500103" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600104" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200105"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000106"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600107" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000108" --object type,id=ID,... define an object such as 'secret' for providing\n"
109" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500110" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000111" --tls-authz=ID use id of an earlier --object to provide\n"
112" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300113" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200115" --fork fork off the server process and exit the parent\n"
116" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200117" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600118#if HAVE_NBD_DEVICE
119"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200120"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200121" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
122" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200123#endif
124"\n"
125"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200126" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
127" -r, --read-only export read-only\n"
128" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
129" file with backing_file=FILE, redirect the write to\n"
130" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800131" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200132" load an internal snapshot inside FILE and export it\n"
133" as an read-only device, SNAPSHOT_PARAM format is\n"
134" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
135" '[ID_OR_NAME]'\n"
136" -n, --nocache disable host cache\n"
137" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000138" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200139" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300140" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000141" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200142"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500143QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600144 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000145}
146
147static void version(const char *name)
148{
149 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100150"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000151"Written by Anthony Liguori.\n"
152"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500153QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000154"This is free software; see the source for copying conditions. There is NO\n"
155"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000156 , name);
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
Eric Blake029a88c2020-09-30 07:11:01 -0500159#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100160/*
161 * The client thread uses SIGTERM to interrupt the server. A signal
162 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
163 */
164void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100165{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100166 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200167 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100168}
Eric Blake029a88c2020-09-30 07:11:01 -0500169#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100170
Eric Blake68b96f12019-01-17 13:36:56 -0600171static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
172 const char *hostname)
173{
174 int ret = EXIT_FAILURE;
175 int rc;
176 Error *err = NULL;
177 QIOChannelSocket *sioc;
178 NBDExportInfo *list;
179 int i, j;
180
181 sioc = qio_channel_socket_new();
182 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
183 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000184 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600185 }
186 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
187 &err);
188 if (rc < 0) {
189 if (err) {
190 error_report_err(err);
191 }
192 goto out;
193 }
194 printf("exports available: %d\n", rc);
195 for (i = 0; i < rc; i++) {
196 printf(" export: '%s'\n", list[i].name);
197 if (list[i].description && *list[i].description) {
198 printf(" description: %s\n", list[i].description);
199 }
200 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200201 static const char *const flag_names[] = {
202 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
203 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
204 [NBD_FLAG_SEND_FUA_BIT] = "fua",
205 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
206 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
207 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
208 [NBD_FLAG_SEND_DF_BIT] = "df",
209 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
210 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
211 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500212 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 };
214
Eric Blake68b96f12019-01-17 13:36:56 -0600215 printf(" size: %" PRIu64 "\n", list[i].size);
216 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200217 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
218 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
219 printf(" %s", flag_names[bit]);
220 }
Eric Blake68b96f12019-01-17 13:36:56 -0600221 }
222 printf(" )\n");
223 }
224 if (list[i].min_block) {
225 printf(" min block: %u\n", list[i].min_block);
226 printf(" opt block: %u\n", list[i].opt_block);
227 printf(" max block: %u\n", list[i].max_block);
228 }
229 if (list[i].n_contexts) {
230 printf(" available meta contexts: %d\n", list[i].n_contexts);
231 for (j = 0; j < list[i].n_contexts; j++) {
232 printf(" %s\n", list[i].contexts[j]);
233 }
234 }
235 }
236 nbd_free_export_list(list, rc);
237
238 ret = EXIT_SUCCESS;
239 out:
240 object_unref(OBJECT(sioc));
241 return ret;
242}
243
244
Eric Blake3c1fa352018-12-15 07:53:08 -0600245#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100246static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000247{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100248 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100249 int nbd;
thscd831bd2008-07-03 10:23:51 +0000250
Paolo Bonzinia517e882011-11-04 15:51:21 +0100251 /* linux just needs an open() to trigger
252 * the partition table update
253 * but remember to load the module with max_part != 0 :
254 * modprobe nbd max_part=63
255 */
256 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100257 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000259 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 return NULL;
261}
262
263static void *nbd_client_thread(void *arg)
264{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100265 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600266 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000267 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000268 int fd = -1;
269 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500271 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000273 sioc = qio_channel_socket_new();
274 if (qio_channel_socket_connect_sync(sioc,
275 saddr,
276 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100277 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000278 goto out;
279 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280
Alex Chenaf74b552020-12-08 13:49:44 +0000281 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500283 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100284 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500285 }
Alex Chenaf74b552020-12-08 13:49:44 +0000286 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287 }
288
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100289 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100290 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100291 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100292 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000293 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100294 }
295
Alex Chenaf74b552020-12-08 13:49:44 +0000296 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300297 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000298 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299 }
300
301 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100302 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100303
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100304 if (verbose) {
305 fprintf(stderr, "NBD device %s is now connected to %s\n",
306 device, srcpath);
307 } else {
308 /* Close stderr so that the qemu-nbd process exits. */
309 dup2(STDOUT_FILENO, STDERR_FILENO);
310 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311
Alex Chenaf74b552020-12-08 13:49:44 +0000312 if (nbd_client(fd) < 0) {
313 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315
Alex Chenaf74b552020-12-08 13:49:44 +0000316 ret = EXIT_SUCCESS;
317
318 out:
319 if (fd >= 0) {
320 close(fd);
321 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000322 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600323 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100324 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000325 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000326}
Eric Blake3c1fa352018-12-15 07:53:08 -0600327#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000328
Fam Zhenge4afbf42015-05-19 10:50:59 +0000329static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200330{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500331 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200332}
333
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000334static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000335
Eric Blake0c9390d2017-06-08 17:26:17 -0500336static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200338 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500339 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200340 state = TERMINATE;
341 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000342 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200343 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344}
345
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000346static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
347 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200348{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200349 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000350 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200351 }
352
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800353 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000354 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000355 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200356}
357
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000359{
360 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000361 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000362 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000363 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000364 }
365}
366
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100367
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200368static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100369 const char *bindto,
370 const char *port)
371{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200372 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100373
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200374 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100375 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200376 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
377 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100378 } else {
Eric Blake03992932016-03-03 09:16:48 -0700379 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380 saddr->type = SOCKET_ADDRESS_TYPE_INET;
381 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700382 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700384 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 } else {
Eric Blake03992932016-03-03 09:16:48 -0700386 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 }
388 }
389
390 return saddr;
391}
392
393
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000394static QemuOptsList file_opts = {
395 .name = "file",
396 .implied_opt_name = "file",
397 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
398 .desc = {
399 /* no elements => accept any params */
400 { /* end of list */ }
401 },
402};
403
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000404static QemuOptsList qemu_object_opts = {
405 .name = "object",
406 .implied_opt_name = "qom-type",
407 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
408 .desc = {
409 { }
410 },
411};
412
Kevin Wolf495bf892019-10-11 21:49:17 +0200413static bool qemu_nbd_object_print_help(const char *type, QemuOpts *opts)
414{
415 if (user_creatable_print_help(type, opts)) {
416 exit(0);
417 }
418 return true;
419}
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000420
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000421
Eric Blake68b96f12019-01-17 13:36:56 -0600422static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
423 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000424{
425 Object *obj;
426 QCryptoTLSCreds *creds;
427
428 obj = object_resolve_path_component(
429 object_get_objects_root(), id);
430 if (!obj) {
431 error_setg(errp, "No TLS credentials with id '%s'",
432 id);
433 return NULL;
434 }
435 creds = (QCryptoTLSCreds *)
436 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
437 if (!creds) {
438 error_setg(errp, "Object with id '%s' is not TLS credentials",
439 id);
440 return NULL;
441 }
442
Eric Blake68b96f12019-01-17 13:36:56 -0600443 if (list) {
444 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
445 error_setg(errp,
446 "Expecting TLS credentials with a client endpoint");
447 return NULL;
448 }
449 } else {
450 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
451 error_setg(errp,
452 "Expecting TLS credentials with a server endpoint");
453 return NULL;
454 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000455 }
456 object_ref(obj);
457 return creds;
458}
459
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000460static void setup_address_and_port(const char **address, const char **port)
461{
462 if (*address == NULL) {
463 *address = "0.0.0.0";
464 }
465
466 if (*port == NULL) {
467 *port = stringify(NBD_DEFAULT_PORT);
468 }
469}
470
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000471/*
472 * Check socket parameters compatibility when socket activation is used.
473 */
474static const char *socket_activation_validate_opts(const char *device,
475 const char *sockpath,
476 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600477 const char *port,
478 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000479{
480 if (device != NULL) {
481 return "NBD device can't be set when using socket activation";
482 }
483
484 if (sockpath != NULL) {
485 return "Unix socket can't be set when using socket activation";
486 }
487
488 if (address != NULL) {
489 return "The interface can't be set when using socket activation";
490 }
491
492 if (port != NULL) {
493 return "TCP port number can't be set when using socket activation";
494 }
495
Eric Blake68b96f12019-01-17 13:36:56 -0600496 if (list) {
497 return "List mode is incompatible with socket activation";
498 }
499
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000500 return NULL;
501}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000502
Kevin Wolfb3b52992018-05-16 13:46:37 +0200503static void qemu_nbd_shutdown(void)
504{
505 job_cancel_sync_all();
506 bdrv_close_all();
507}
508
bellard7a5ca862008-05-27 21:13:40 +0000509int main(int argc, char **argv)
510{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200511 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000512 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600513 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500514 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000515 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000516 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100517 const char *port = NULL;
518 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100519 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800520 QemuOpts *sn_opts = NULL;
521 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500522 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000523 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000524 { "help", no_argument, NULL, 'h' },
525 { "version", no_argument, NULL, 'V' },
526 { "bind", required_argument, NULL, 'b' },
527 { "port", required_argument, NULL, 'p' },
528 { "socket", required_argument, NULL, 'k' },
529 { "offset", required_argument, NULL, 'o' },
530 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500531 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600532 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000533 { "connect", required_argument, NULL, 'c' },
534 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600535 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000536 { "snapshot", no_argument, NULL, 's' },
537 { "load-snapshot", required_argument, NULL, 'l' },
538 { "nocache", no_argument, NULL, 'n' },
539 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
540 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
541 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
542 { "detect-zeroes", required_argument, NULL,
543 QEMU_NBD_OPT_DETECT_ZEROES },
544 { "shared", required_argument, NULL, 'e' },
545 { "format", required_argument, NULL, 'f' },
546 { "persistent", no_argument, NULL, 't' },
547 { "verbose", no_argument, NULL, 'v' },
548 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
549 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500550 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000551 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000552 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000553 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300554 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200555 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200556 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000557 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000558 };
559 int ch;
560 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200561 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500562 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200563 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100564 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200565 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100566 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000567 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200568 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200569 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500570 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600571 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500572 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500573 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500574 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000575 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000576 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100577 bool writethrough = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200578 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600579 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200580 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000581 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200582 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200583 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000584
Eric Blake029a88c2020-09-30 07:11:01 -0500585#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100586 os_setup_early_signal_handling();
587 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200588#endif
589
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100590 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100591 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100592 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300593 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100594
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000595 module_call_init(MODULE_INIT_QOM);
596 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300597 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800598 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100599
bellard7a5ca862008-05-27 21:13:40 +0000600 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
601 switch (ch) {
602 case 's':
ths2f726482008-07-03 11:47:46 +0000603 flags |= BDRV_O_SNAPSHOT;
604 break;
605 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200606 optarg = (char *) "none";
607 /* fallthrough */
608 case QEMU_NBD_OPT_CACHE:
609 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100610 error_report("-n and --cache can only be specified once");
611 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200612 }
613 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100614 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100615 error_report("Invalid cache mode `%s'", optarg);
616 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200617 }
bellard7a5ca862008-05-27 21:13:40 +0000618 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200619 case QEMU_NBD_OPT_AIO:
620 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100621 error_report("--aio can only be specified once");
622 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200623 }
624 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000625 if (bdrv_parse_aio(optarg, &flags) < 0) {
626 error_report("Invalid aio mode '%s'", optarg);
627 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200628 }
629 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100630 case QEMU_NBD_OPT_DISCARD:
631 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100632 error_report("--discard can only be specified once");
633 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100634 }
635 seen_discard = true;
636 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100637 error_report("Invalid discard mode `%s'", optarg);
638 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100639 }
640 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200641 case QEMU_NBD_OPT_DETECT_ZEROES:
642 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200643 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200644 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200645 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
646 &local_err);
647 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100648 error_reportf_err(local_err,
649 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100650 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200651 }
652 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
653 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100654 error_report("setting detect-zeroes to unmap is not allowed "
655 "without setting discard operation to unmap");
656 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200657 }
658 break;
bellard7a5ca862008-05-27 21:13:40 +0000659 case 'b':
660 bindto = optarg;
661 break;
662 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100663 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000664 break;
665 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600666 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
667 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100668 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000669 }
bellard7a5ca862008-05-27 21:13:40 +0000670 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800671 case 'l':
672 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100673 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
674 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800675 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100676 error_report("Failed in parsing snapshot param `%s'",
677 optarg);
678 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800679 }
680 } else {
681 sn_id_or_name = optarg;
682 }
683 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000684 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500685 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200686 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000687 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500688 case 'A':
689 alloc_depth = true;
690 break;
Eric Blake636192c2019-01-11 13:47:20 -0600691 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500692 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600693 break;
thscd831bd2008-07-03 10:23:51 +0000694 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100695 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200696 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100697 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100698 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200699 }
thscd831bd2008-07-03 10:23:51 +0000700 break;
701 case 'd':
702 disconnect = true;
703 break;
704 case 'c':
705 device = optarg;
706 break;
ths3b05a8e2008-07-03 12:45:02 +0000707 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600708 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
709 shared < 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100710 error_report("Invalid shared device number '%s'", optarg);
711 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000712 }
ths3b05a8e2008-07-03 12:45:02 +0000713 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000714 case 'f':
715 fmt = optarg;
716 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200717 case 't':
718 persistent = 1;
719 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000720 case 'x':
721 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600722 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
723 error_report("export name '%s' too long", export_name);
724 exit(EXIT_FAILURE);
725 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000726 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500727 case 'D':
728 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600729 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
730 error_report("export description '%s' too long",
731 export_description);
732 exit(EXIT_FAILURE);
733 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500734 break;
bellard7a5ca862008-05-27 21:13:40 +0000735 case 'v':
736 verbose = 1;
737 break;
738 case 'V':
739 version(argv[0]);
740 exit(0);
741 break;
742 case 'h':
743 usage(argv[0]);
744 exit(0);
745 break;
746 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100747 error_report("Try `%s --help' for more information.", argv[0]);
748 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000749 case QEMU_NBD_OPT_OBJECT: {
750 QemuOpts *opts;
751 opts = qemu_opts_parse_noisily(&qemu_object_opts,
752 optarg, true);
753 if (!opts) {
754 exit(EXIT_FAILURE);
755 }
756 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000757 case QEMU_NBD_OPT_TLSCREDS:
758 tlscredsid = optarg;
759 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000760 case QEMU_NBD_OPT_IMAGE_OPTS:
761 imageOpts = true;
762 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300763 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500764 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300765 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000766 case QEMU_NBD_OPT_TLSAUTHZ:
767 tlsauthz = optarg;
768 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200769 case QEMU_NBD_OPT_FORK:
770 fork_process = true;
771 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600772 case 'L':
773 list = true;
774 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200775 case QEMU_NBD_OPT_PID_FILE:
776 pid_file_name = optarg;
777 break;
bellard7a5ca862008-05-27 21:13:40 +0000778 }
779 }
780
Eric Blake68b96f12019-01-17 13:36:56 -0600781 if (list) {
782 if (argc != optind) {
783 error_report("List mode is incompatible with a file name");
784 exit(EXIT_FAILURE);
785 }
Eric Blake0bc16992020-01-23 10:46:50 -0600786 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500787 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500788 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600789 error_report("List mode is incompatible with per-device settings");
790 exit(EXIT_FAILURE);
791 }
792 if (fork_process) {
793 error_report("List mode is incompatible with forking");
794 exit(EXIT_FAILURE);
795 }
796 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100797 error_report("Invalid number of arguments");
798 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100799 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600800 } else if (!export_name) {
801 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000802 }
803
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200804 qemu_opts_foreach(&qemu_object_opts,
805 user_creatable_add_opts_foreach,
Kevin Wolf495bf892019-10-11 21:49:17 +0200806 qemu_nbd_object_print_help, &error_fatal);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000807
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300808 if (!trace_init_backends()) {
809 exit(1);
810 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500811 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300812 qemu_set_log(LOG_TRACE);
813
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000814 socket_activation = check_socket_activation();
815 if (socket_activation == 0) {
816 setup_address_and_port(&bindto, &port);
817 } else {
818 /* Using socket activation - check user didn't use -p etc. */
819 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600820 bindto, port,
821 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000822 if (err_msg != NULL) {
823 error_report("%s", err_msg);
824 exit(EXIT_FAILURE);
825 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100826
827 /* qemu-nbd can only listen on a single socket. */
828 if (socket_activation > 1) {
829 error_report("qemu-nbd does not support socket activation with %s > 1",
830 "LISTEN_FDS");
831 exit(EXIT_FAILURE);
832 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000833 }
834
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000835 if (tlscredsid) {
836 if (sockpath) {
837 error_report("TLS is only supported with IPv4/IPv6");
838 exit(EXIT_FAILURE);
839 }
840 if (device) {
841 error_report("TLS is not supported with a host device");
842 exit(EXIT_FAILURE);
843 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000844 if (tlsauthz && list) {
845 error_report("TLS authorization is incompatible with export list");
846 exit(EXIT_FAILURE);
847 }
Eric Blake68b96f12019-01-17 13:36:56 -0600848 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000849 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200850 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000851 exit(EXIT_FAILURE);
852 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000853 } else {
854 if (tlsauthz) {
855 error_report("--tls-authz is not permitted without --tls-creds");
856 exit(EXIT_FAILURE);
857 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000858 }
859
Eric Blake68b96f12019-01-17 13:36:56 -0600860 if (list) {
861 saddr = nbd_build_socket_address(sockpath, bindto, port);
862 return qemu_nbd_client_list(saddr, tlscreds, bindto);
863 }
864
Eric Blake3c1fa352018-12-15 07:53:08 -0600865#if !HAVE_NBD_DEVICE
866 if (disconnect || device) {
867 error_report("Kernel /dev/nbdN support not available");
868 exit(EXIT_FAILURE);
869 }
870#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000871 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000872 int nbdfd = open(argv[optind], O_RDWR);
873 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100874 error_report("Cannot open %s: %s", argv[optind],
875 strerror(errno));
876 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100877 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000878 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000879
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000880 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000881
882 printf("%s disconnected\n", argv[optind]);
883
Peter Lieven713cc672014-08-13 19:20:19 +0200884 return 0;
thscd831bd2008-07-03 10:23:51 +0000885 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600886#endif
thscd831bd2008-07-03 10:23:51 +0000887
Max Reitzffb31e12016-09-28 22:46:42 +0200888 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100889#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100890 int stderr_fd[2];
891 pid_t pid;
892 int ret;
893
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100894 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100895 error_report("Error setting up communication pipe: %s",
896 strerror(errno));
897 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100898 }
899
900 /* Now daemonize, but keep a communication channel open to
901 * print errors and exit with the proper status code.
902 */
903 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500904 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100905 error_report("Failed to fork: %s", strerror(errno));
906 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500907 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100908 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200909
Raphael Pour0eaf4532020-05-15 08:36:07 +0200910 /* Remember parent's stderr if we will be restoring it. */
911 if (fork_process) {
912 old_stderr = dup(STDERR_FILENO);
913 }
914
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400915 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100916
917 /* Temporarily redirect stderr to the parent's pipe... */
918 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100919 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100920 error_report("Failed to daemonize: %s", strerror(errno));
921 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100922 }
923
924 /* ... close the descriptor we inherited and go on. */
925 close(stderr_fd[1]);
926 } else {
927 bool errors = false;
928 char *buf;
929
930 /* In the parent. Print error messages from the child until
931 * it closes the pipe.
932 */
933 close(stderr_fd[1]);
934 buf = g_malloc(1024);
935 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
936 errors = true;
937 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100938 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100939 exit(EXIT_FAILURE);
940 }
941 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100942 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100943 error_report("Cannot read from daemon: %s",
944 strerror(errno));
945 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100946 }
947
948 /* Usually the daemon should not print any message.
949 * Exit with zero status in that case.
950 */
951 exit(errors);
952 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100953#else /* WIN32 */
954 error_report("Unable to fork into background on Windows hosts");
955 exit(EXIT_FAILURE);
956#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100957 }
958
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100959 if (device != NULL && sockpath == NULL) {
960 sockpath = g_malloc(128);
961 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000962 }
963
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000964 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000965 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000966 saddr = nbd_build_socket_address(sockpath, bindto, port);
Juan Quintelafc8135c2019-08-19 18:08:21 +0200967 if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000968 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000969 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000970 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000971 }
972 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000973 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000974 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000975 for (i = 0; i < socket_activation; i++) {
976 QIOChannelSocket *sioc;
977 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
978 &local_err);
979 if (sioc == NULL) {
980 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200981 error_reportf_err(local_err,
982 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000983 exit(EXIT_FAILURE);
984 }
985 qio_net_listener_add(server, sioc);
986 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000987 }
988 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100989
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300990 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100991 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300992 exit(EXIT_FAILURE);
993 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100994 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200995 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100996
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000997 srcpath = argv[optind];
998 if (imageOpts) {
999 QemuOpts *opts;
1000 if (fmt) {
1001 error_report("--image-opts and -f are mutually exclusive");
1002 exit(EXIT_FAILURE);
1003 }
1004 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1005 if (!opts) {
1006 qemu_opts_reset(&file_opts);
1007 exit(EXIT_FAILURE);
1008 }
1009 options = qemu_opts_to_qdict(opts, NULL);
1010 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001011 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001012 } else {
1013 if (fmt) {
1014 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001015 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001016 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001017 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001018 }
1019
Max Reitz4fbec262015-02-05 13:58:19 -05001020 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001021 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1022 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001023 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001024 }
Max Reitz4fbec262015-02-05 13:58:19 -05001025 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001026
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001027 if (dev_offset) {
1028 QDict *raw_opts = qdict_new();
1029 qdict_put_str(raw_opts, "driver", "raw");
1030 qdict_put_str(raw_opts, "file", bs->node_name);
1031 qdict_put_int(raw_opts, "offset", dev_offset);
1032 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1033 blk_remove_bs(blk);
1034 blk_insert_bs(blk, bs, &error_fatal);
1035 bdrv_unref(bs);
1036 }
1037
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001038 blk_set_enable_write_cache(blk, !writethrough);
1039
Wenchao Xia8c116b02013-12-04 17:10:55 +08001040 if (sn_opts) {
1041 ret = bdrv_snapshot_load_tmp(bs,
1042 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1043 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1044 &local_err);
1045 } else if (sn_id_or_name) {
1046 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1047 &local_err);
1048 }
1049 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001050 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001051 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001052 }
1053
Peter Lievenb3838a42014-08-13 19:20:18 +02001054 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001055
Kevin Wolf00917172020-09-24 17:26:57 +02001056 nbd_server_is_qemu_nbd(true);
1057
1058 export_opts = g_new(BlockExportOptions, 1);
1059 *export_opts = (BlockExportOptions) {
1060 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001061 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001062 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001063 .has_writethrough = true,
1064 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001065 .has_writable = true,
1066 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001067 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001068 .has_name = true,
1069 .name = g_strdup(export_name),
1070 .has_description = !!export_description,
1071 .description = g_strdup(export_description),
1072 .has_bitmaps = !!bitmaps,
1073 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001074 .has_allocation_depth = alloc_depth,
1075 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001076 },
1077 };
1078 blk_exp_add(export_opts, &error_fatal);
1079 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001080
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001081 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001082#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001083 int ret;
1084
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001085 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001086 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001087 error_report("Failed to create client thread: %s", strerror(ret));
1088 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001089 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001090#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001091 } else {
1092 /* Shut up GCC warnings. */
1093 memset(&client_thread, 0, sizeof(client_thread));
1094 }
1095
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001096 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001097
Max Reitz637bc5a2019-05-08 23:18:16 +02001098 if (pid_file_name) {
1099 qemu_write_pidfile(pid_file_name, &error_fatal);
1100 }
1101
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001102 /* now when the initialization is (almost) complete, chdir("/")
1103 * to free any busy filesystems */
1104 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001105 error_report("Could not chdir to root directory: %s",
1106 strerror(errno));
1107 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001108 }
1109
Max Reitzffb31e12016-09-28 22:46:42 +02001110 if (fork_process) {
1111 dup2(old_stderr, STDERR_FILENO);
1112 close(old_stderr);
1113 }
1114
Paolo Bonzini7860a382012-09-18 13:31:56 +02001115 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001116 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001117 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001118 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001119 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001120 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001121 }
1122 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001123
Markus Armbruster26f54e92014-10-07 13:59:04 +02001124 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001125 if (sockpath) {
1126 unlink(sockpath);
1127 }
bellard7a5ca862008-05-27 21:13:40 +00001128
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001129 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001130
Paolo Bonzinia517e882011-11-04 15:51:21 +01001131 if (device) {
1132 void *ret;
1133 pthread_join(client_thread, &ret);
1134 exit(ret != NULL);
1135 } else {
1136 exit(EXIT_SUCCESS);
1137 }
bellard7a5ca862008-05-27 21:13:40 +00001138}