Add missing FreeBSD #include (Juergen Lock)
[qemu/mini2440.git] / qemu-nbd.c
blob916150e424e67a4290651db99412f4ff78992803
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <qemu-common.h>
21 #include "block_int.h"
22 #include "nbd.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <err.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <arpa/inet.h>
33 #include <signal.h>
35 #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
37 #define NBD_BUFFER_SIZE (1024*1024)
39 int verbose;
41 static void usage(const char *name)
43 printf(
44 "Usage: %s [OPTIONS] FILE\n"
45 "QEMU Disk Network Block Device Server\n"
46 "\n"
47 " -p, --port=PORT port to listen on (default `1024')\n"
48 " -o, --offset=OFFSET offset into the image\n"
49 " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
50 " -k, --socket=PATH path to the unix socket\n"
51 " (default '"SOCKET_PATH"')\n"
52 " -r, --read-only export read-only\n"
53 " -P, --partition=NUM only expose partition NUM\n"
54 " -s, --snapshot use snapshot file\n"
55 " -n, --nocache disable host cache\n"
56 " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
57 " -d, --disconnect disconnect the specified device\n"
58 " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
59 " -t, --persistent don't exit on the last connection\n"
60 " -v, --verbose display extra debugging information\n"
61 " -h, --help display this help and exit\n"
62 " -V, --version output version information and exit\n"
63 "\n"
64 "Report bugs to <anthony@codemonkey.ws>\n"
65 , name, "DEVICE");
68 static void version(const char *name)
70 printf(
71 "%s version 0.0.1\n"
72 "Written by Anthony Liguori.\n"
73 "\n"
74 "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
75 "This is free software; see the source for copying conditions. There is NO\n"
76 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
77 , name);
80 struct partition_record
82 uint8_t bootable;
83 uint8_t start_head;
84 uint32_t start_cylinder;
85 uint8_t start_sector;
86 uint8_t system;
87 uint8_t end_head;
88 uint8_t end_cylinder;
89 uint8_t end_sector;
90 uint32_t start_sector_abs;
91 uint32_t nb_sectors_abs;
94 static void read_partition(uint8_t *p, struct partition_record *r)
96 r->bootable = p[0];
97 r->start_head = p[1];
98 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
99 r->start_sector = p[2] & 0x3f;
100 r->system = p[4];
101 r->end_head = p[5];
102 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
103 r->end_sector = p[6] & 0x3f;
104 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
105 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
108 static int find_partition(BlockDriverState *bs, int partition,
109 off_t *offset, off_t *size)
111 struct partition_record mbr[4];
112 uint8_t data[512];
113 int i;
114 int ext_partnum = 4;
116 if (bdrv_read(bs, 0, data, 1))
117 errx(EINVAL, "error while reading");
119 if (data[510] != 0x55 || data[511] != 0xaa) {
120 errno = -EINVAL;
121 return -1;
124 for (i = 0; i < 4; i++) {
125 read_partition(&data[446 + 16 * i], &mbr[i]);
127 if (!mbr[i].nb_sectors_abs)
128 continue;
130 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
131 struct partition_record ext[4];
132 uint8_t data1[512];
133 int j;
135 if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
136 errx(EINVAL, "error while reading");
138 for (j = 0; j < 4; j++) {
139 read_partition(&data1[446 + 16 * j], &ext[j]);
140 if (!ext[j].nb_sectors_abs)
141 continue;
143 if ((ext_partnum + j + 1) == partition) {
144 *offset = (uint64_t)ext[j].start_sector_abs << 9;
145 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
146 return 0;
149 ext_partnum += 4;
150 } else if ((i + 1) == partition) {
151 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
152 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
153 return 0;
157 errno = -ENOENT;
158 return -1;
161 static void show_parts(const char *device)
163 if (fork() == 0) {
164 int nbd;
166 /* linux just needs an open() to trigger
167 * the partition table update
168 * but remember to load the module with max_part != 0 :
169 * modprobe nbd max_part=63
171 nbd = open(device, O_RDWR);
172 if (nbd != -1)
173 close(nbd);
174 exit(0);
178 int main(int argc, char **argv)
180 BlockDriverState *bs;
181 off_t dev_offset = 0;
182 off_t offset = 0;
183 bool readonly = false;
184 bool disconnect = false;
185 const char *bindto = "0.0.0.0";
186 int port = 1024;
187 struct sockaddr_in addr;
188 socklen_t addr_len = sizeof(addr);
189 off_t fd_size;
190 char *device = NULL;
191 char *socket = NULL;
192 char sockpath[128];
193 const char *sopt = "hVbo:p:rsnP:c:dvk:e:t";
194 struct option lopt[] = {
195 { "help", 0, 0, 'h' },
196 { "version", 0, 0, 'V' },
197 { "bind", 1, 0, 'b' },
198 { "port", 1, 0, 'p' },
199 { "socket", 1, 0, 'k' },
200 { "offset", 1, 0, 'o' },
201 { "read-only", 0, 0, 'r' },
202 { "partition", 1, 0, 'P' },
203 { "connect", 1, 0, 'c' },
204 { "disconnect", 0, 0, 'd' },
205 { "snapshot", 0, 0, 's' },
206 { "nocache", 0, 0, 'n' },
207 { "shared", 1, 0, 'e' },
208 { "persistent", 0, 0, 't' },
209 { "verbose", 0, 0, 'v' },
210 { NULL, 0, 0, 0 }
212 int ch;
213 int opt_ind = 0;
214 int li;
215 char *end;
216 int flags = 0;
217 int partition = -1;
218 int ret;
219 int shared = 1;
220 uint8_t *data;
221 fd_set fds;
222 int *sharing_fds;
223 int fd;
224 int i;
225 int nb_fds = 0;
226 int max_fd;
227 int persistent = 0;
229 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
230 switch (ch) {
231 case 's':
232 flags |= BDRV_O_SNAPSHOT;
233 break;
234 case 'n':
235 flags |= BDRV_O_DIRECT;
236 break;
237 case 'b':
238 bindto = optarg;
239 break;
240 case 'p':
241 li = strtol(optarg, &end, 0);
242 if (*end) {
243 errx(EINVAL, "Invalid port `%s'", optarg);
245 if (li < 1 || li > 65535) {
246 errx(EINVAL, "Port out of range `%s'", optarg);
248 port = (uint16_t)li;
249 break;
250 case 'o':
251 dev_offset = strtoll (optarg, &end, 0);
252 if (*end) {
253 errx(EINVAL, "Invalid offset `%s'", optarg);
255 if (dev_offset < 0) {
256 errx(EINVAL, "Offset must be positive `%s'", optarg);
258 break;
259 case 'r':
260 readonly = true;
261 break;
262 case 'P':
263 partition = strtol(optarg, &end, 0);
264 if (*end)
265 errx(EINVAL, "Invalid partition `%s'", optarg);
266 if (partition < 1 || partition > 8)
267 errx(EINVAL, "Invalid partition %d", partition);
268 break;
269 case 'k':
270 socket = optarg;
271 if (socket[0] != '/')
272 errx(EINVAL, "socket path must be absolute\n");
273 break;
274 case 'd':
275 disconnect = true;
276 break;
277 case 'c':
278 device = optarg;
279 break;
280 case 'e':
281 shared = strtol(optarg, &end, 0);
282 if (*end) {
283 errx(EINVAL, "Invalid shared device number '%s'", optarg);
285 if (shared < 1) {
286 errx(EINVAL, "Shared device number must be greater than 0\n");
288 break;
289 case 't':
290 persistent = 1;
291 break;
292 case 'v':
293 verbose = 1;
294 break;
295 case 'V':
296 version(argv[0]);
297 exit(0);
298 break;
299 case 'h':
300 usage(argv[0]);
301 exit(0);
302 break;
303 case '?':
304 errx(EINVAL, "Try `%s --help' for more information.",
305 argv[0]);
309 if ((argc - optind) != 1) {
310 errx(EINVAL, "Invalid number of argument.\n"
311 "Try `%s --help' for more information.",
312 argv[0]);
315 if (disconnect) {
316 fd = open(argv[optind], O_RDWR);
317 if (fd == -1)
318 errx(errno, "Cannot open %s", argv[optind]);
320 nbd_disconnect(fd);
322 close(fd);
324 printf("%s disconnected\n", argv[optind]);
326 return 0;
329 bdrv_init();
331 bs = bdrv_new("hda");
332 if (bs == NULL)
333 return 1;
335 if (bdrv_open(bs, argv[optind], flags) == -1)
336 return 1;
338 fd_size = bs->total_sectors * 512;
340 if (partition != -1 &&
341 find_partition(bs, partition, &dev_offset, &fd_size))
342 errx(errno, "Could not find partition %d", partition);
344 if (device) {
345 pid_t pid;
346 int sock;
348 if (!verbose)
349 daemon(0, 0); /* detach client and server */
351 if (socket == NULL) {
352 sprintf(sockpath, SOCKET_PATH, basename(device));
353 socket = sockpath;
356 pid = fork();
357 if (pid < 0)
358 return 1;
359 if (pid != 0) {
360 off_t size;
361 size_t blocksize;
363 ret = 0;
364 bdrv_close(bs);
366 do {
367 sock = unix_socket_outgoing(socket);
368 if (sock == -1) {
369 if (errno != ENOENT && errno != ECONNREFUSED)
370 goto out;
371 sleep(1); /* wait children */
373 } while (sock == -1);
375 fd = open(device, O_RDWR);
376 if (fd == -1) {
377 ret = 1;
378 goto out;
381 ret = nbd_receive_negotiate(sock, &size, &blocksize);
382 if (ret == -1) {
383 ret = 1;
384 goto out;
387 ret = nbd_init(fd, sock, size, blocksize);
388 if (ret == -1) {
389 ret = 1;
390 goto out;
393 printf("NBD device %s is now connected to file %s\n",
394 device, argv[optind]);
396 /* update partition table */
398 show_parts(device);
400 nbd_client(fd, sock);
401 close(fd);
402 out:
403 kill(pid, SIGTERM);
404 unlink(socket);
406 return ret;
408 /* children */
411 sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
412 if (sharing_fds == NULL)
413 errx(ENOMEM, "Cannot allocate sharing fds");
415 if (socket) {
416 sharing_fds[0] = unix_socket_incoming(socket);
417 } else {
418 sharing_fds[0] = tcp_socket_incoming(bindto, port);
421 if (sharing_fds[0] == -1)
422 return 1;
423 max_fd = sharing_fds[0];
424 nb_fds++;
426 data = qemu_memalign(512, NBD_BUFFER_SIZE);
427 if (data == NULL)
428 errx(ENOMEM, "Cannot allocate data buffer");
430 do {
432 FD_ZERO(&fds);
433 for (i = 0; i < nb_fds; i++)
434 FD_SET(sharing_fds[i], &fds);
436 ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
437 if (ret == -1)
438 break;
440 if (FD_ISSET(sharing_fds[0], &fds))
441 ret--;
442 for (i = 1; i < nb_fds && ret; i++) {
443 if (FD_ISSET(sharing_fds[i], &fds)) {
444 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
445 &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
446 close(sharing_fds[i]);
447 nb_fds--;
448 sharing_fds[i] = sharing_fds[nb_fds];
449 i--;
451 ret--;
454 /* new connection ? */
455 if (FD_ISSET(sharing_fds[0], &fds)) {
456 if (nb_fds < shared + 1) {
457 sharing_fds[nb_fds] = accept(sharing_fds[0],
458 (struct sockaddr *)&addr,
459 &addr_len);
460 if (sharing_fds[nb_fds] != -1 &&
461 nbd_negotiate(bs, sharing_fds[nb_fds], fd_size) != -1) {
462 if (sharing_fds[nb_fds] > max_fd)
463 max_fd = sharing_fds[nb_fds];
464 nb_fds++;
468 } while (persistent || nb_fds > 1);
469 qemu_free(data);
471 close(sharing_fds[0]);
472 bdrv_close(bs);
473 qemu_free(sharing_fds);
474 if (socket)
475 unlink(socket);
477 return 0;