ia64/xen-unstable

view tools/blktap/drivers/blktapctrl.c @ 19848:5839491bbf20

[IA64] replace MAX_VCPUS with d->max_vcpus where necessary.

don't use MAX_VCPUS, and use vcpu::max_vcpus.
The changeset of 2f9e1348aa98 introduced max_vcpus to allow more vcpus
per guest. This patch is ia64 counter part.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 11:26:05 2009 +0900 (2009-06-29)
parents 45447c0f7c6e
children
line source
1 /*
2 * blktapctrl.c
3 *
4 * userspace controller for the blktap disks.
5 * As requests for new block devices arrive,
6 * the controller spawns off a separate process
7 * per-disk.
8 *
9 *
10 * Copyright (c) 2005 Julian Chesterfield and Andrew Warfield.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 * IN THE SOFTWARE.
35 */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/mman.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <signal.h>
45 #include <fcntl.h>
46 #include <sys/poll.h>
47 #include <sys/ioctl.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <xs.h>
51 #include <sys/time.h>
52 #include <syslog.h>
54 #include "blktaplib.h"
55 #include "blktapctrl.h"
56 #include "tapdisk.h"
57 #include "list.h"
58 #include "xs_api.h" /* for xs_fire_next_watch() */
60 #define PIDFILE "/var/run/blktapctrl.pid"
62 #define NUM_POLL_FDS 2
63 #define MSG_SIZE 4096
64 #define MAX_TIMEOUT 10
65 #define MAX_RAND_VAL 0xFFFF
66 #define MAX_ATTEMPTS 10
68 int run = 1;
69 int max_timeout = MAX_TIMEOUT;
70 int ctlfd = 0;
72 int blktap_major;
74 static int open_ctrl_socket(char *devname);
75 static int write_msg(int fd, int msgtype, void *ptr, void *ptr2);
76 static int read_msg(int fd, int msgtype, void *ptr);
77 static driver_list_entry_t *active_disks[MAX_DISK_TYPES];
80 static unsigned long long tapdisk_get_size(blkif_t *blkif)
81 {
82 image_t *img = (image_t *)blkif->prv;
83 return img->size;
84 }
86 static unsigned long tapdisk_get_secsize(blkif_t *blkif)
87 {
88 image_t *img = (image_t *)blkif->prv;
89 return img->secsize;
90 }
92 static unsigned int tapdisk_get_info(blkif_t *blkif)
93 {
94 image_t *img = (image_t *)blkif->prv;
95 return img->info;
96 }
98 struct blkif_ops tapdisk_ops = {
99 .get_size = tapdisk_get_size,
100 .get_secsize = tapdisk_get_secsize,
101 .get_info = tapdisk_get_info,
102 };
105 static void init_driver_list(void)
106 {
107 int i;
109 for (i = 0; i < MAX_DISK_TYPES; i++)
110 active_disks[i] = NULL;
111 return;
112 }
114 static void init_rng(void)
115 {
116 static uint32_t seed;
117 struct timeval tv;
119 gettimeofday(&tv, NULL);
120 seed = tv.tv_usec;
121 srand48(seed);
122 return;
123 }
125 static int get_tapdisk_pid(blkif_t *blkif)
126 {
127 int ret;
129 if ((ret = write_msg(blkif->fds[WRITE], CTLMSG_PID, blkif, NULL))
130 <= 0) {
131 DPRINTF("Write_msg failed - CTLMSG_PID(%d)\n", ret);
132 return -EINVAL;
133 }
135 if ((ret = read_msg(blkif->fds[READ], CTLMSG_PID_RSP, blkif))
136 <= 0) {
137 DPRINTF("Read_msg failure - CTLMSG_PID(%d)\n", ret);
138 return -EINVAL;
139 }
140 return 1;
141 }
143 /* Look up the disk specified by path:
144 * if found, dev points to the device string in the path
145 * type is the tapdisk driver type id
146 * blkif is the existing interface if this is a shared driver
147 * and NULL otherwise.
148 * return 0 on success, -1 on error.
149 */
151 static int test_path(char *path, char **dev, int *type, blkif_t **blkif,
152 int* use_ioemu)
153 {
154 char *ptr, handle[10];
155 int i, size, found = 0;
156 size_t handle_len;
158 size = sizeof(dtypes)/sizeof(disk_info_t *);
159 *type = MAX_DISK_TYPES + 1;
160 *blkif = NULL;
162 if (!strncmp(path, "tapdisk:", strlen("tapdisk:"))) {
163 *use_ioemu = 0;
164 path += strlen("tapdisk:");
165 } else if (!strncmp(path, "ioemu:", strlen("ioemu:"))) {
166 *use_ioemu = 1;
167 path += strlen("ioemu:");
168 } else {
169 // Use the default for the image type
170 *use_ioemu = -1;
171 }
173 if ( (ptr = strstr(path, ":"))!=NULL) {
174 handle_len = (ptr - path);
175 memcpy(handle, path, handle_len);
176 *dev = ptr + 1;
177 ptr = handle + handle_len;
178 *ptr = '\0';
179 DPRINTF("Detected handle: [%s]\n",handle);
181 for (i = 0; i < size; i++) {
182 if ((strlen(dtypes[i]->handle) == handle_len) &&
183 strncmp(handle, dtypes[i]->handle,
184 handle_len) == 0) {
185 found = 1;
186 }
188 if (found) {
189 if (*use_ioemu == -1)
190 *use_ioemu = dtypes[i]->use_ioemu;
191 *type = dtypes[i]->idnum;
193 if (dtypes[i]->single_handler == 1) {
194 /* Check whether tapdisk process
195 already exists */
196 if (active_disks[dtypes[i]->idnum] == NULL)
197 *blkif = NULL;
198 else
199 *blkif = active_disks[dtypes[i]
200 ->idnum]->blkif;
201 }
203 return 0;
204 }
205 }
206 }
208 /* Fall-through case, we didn't find a disk driver. */
209 DPRINTF("Unknown blktap disk type [%s]!\n",handle);
210 *dev = NULL;
211 return -1;
212 }
215 static void add_disktype(blkif_t *blkif, int type)
216 {
217 driver_list_entry_t *entry, **pprev;
219 if (type > MAX_DISK_TYPES)
220 return;
222 entry = malloc(sizeof(driver_list_entry_t));
223 entry->blkif = blkif;
224 entry->next = NULL;
226 pprev = &active_disks[type];
227 while (*pprev != NULL)
228 pprev = &(*pprev)->next;
230 *pprev = entry;
231 entry->pprev = pprev;
232 }
234 static int qemu_instance_has_disks(pid_t pid)
235 {
236 int i;
237 int count = 0;
238 driver_list_entry_t *entry;
240 for (i = 0; i < MAX_DISK_TYPES; i++) {
241 entry = active_disks[i];
242 while (entry) {
243 if ((entry->blkif->tappid == pid) && dtypes[i]->use_ioemu)
244 count++;
245 entry = entry->next;
246 }
247 }
249 return (count != 0);
250 }
252 static int del_disktype(blkif_t *blkif)
253 {
254 driver_list_entry_t *entry, **pprev;
255 int type = blkif->drivertype, count = 0, close = 0;
257 if (type > MAX_DISK_TYPES)
258 return 1;
260 pprev = &active_disks[type];
261 while ((*pprev != NULL) && ((*pprev)->blkif != blkif))
262 pprev = &(*pprev)->next;
264 if ((entry = *pprev) == NULL) {
265 DPRINTF("DEL_DISKTYPE: No match\n");
266 return 1;
267 }
269 *pprev = entry->next;
270 if (entry->next)
271 entry->next->pprev = pprev;
273 DPRINTF("DEL_DISKTYPE: Freeing entry\n");
274 free(entry);
276 /*
277 * When using ioemu, all disks of one VM are connected to the same
278 * qemu-dm instance. We may close the file handle only if there is
279 * no other disk left for this domain.
280 */
281 if (dtypes[type]->use_ioemu)
282 return !qemu_instance_has_disks(blkif->tappid);
284 /* Caller should close() if no single controller, or list is empty. */
285 return (!dtypes[type]->single_handler || (active_disks[type] == NULL));
286 }
288 static int write_msg(int fd, int msgtype, void *ptr, void *ptr2)
289 {
290 blkif_t *blkif;
291 blkif_info_t *blk;
292 msg_hdr_t *msg;
293 msg_newdev_t *msg_dev;
294 char *p, *buf, *path;
295 int msglen, len, ret;
296 fd_set writefds;
297 struct timeval timeout;
298 image_t *image, *img;
299 uint32_t seed;
301 blkif = (blkif_t *)ptr;
302 blk = blkif->info;
303 image = blkif->prv;
304 len = 0;
306 switch (msgtype)
307 {
308 case CTLMSG_PARAMS:
309 path = (char *)ptr2;
310 DPRINTF("Write_msg called: CTLMSG_PARAMS, sending [%s, %s]\n",
311 blk->params, path);
313 msglen = sizeof(msg_hdr_t) + strlen(path) + 1;
314 buf = malloc(msglen);
316 /*Assign header fields*/
317 msg = (msg_hdr_t *)buf;
318 msg->type = CTLMSG_PARAMS;
319 msg->len = msglen;
320 msg->drivertype = blkif->drivertype;
321 msg->readonly = blkif->readonly;
323 gettimeofday(&timeout, NULL);
324 msg->cookie = blkif->cookie;
325 DPRINTF("Generated cookie, %d\n",blkif->cookie);
327 /*Copy blk->params to msg*/
328 p = buf + sizeof(msg_hdr_t);
329 memcpy(p, path, strlen(path) + 1);
331 break;
333 case CTLMSG_NEWDEV:
334 DPRINTF("Write_msg called: CTLMSG_NEWDEV\n");
336 msglen = sizeof(msg_hdr_t) + sizeof(msg_newdev_t);
337 buf = malloc(msglen);
339 /*Assign header fields*/
340 msg = (msg_hdr_t *)buf;
341 msg->type = CTLMSG_NEWDEV;
342 msg->len = msglen;
343 msg->drivertype = blkif->drivertype;
344 msg->cookie = blkif->cookie;
346 msg_dev = (msg_newdev_t *)(buf + sizeof(msg_hdr_t));
347 msg_dev->devnum = blkif->minor;
348 msg_dev->domid = blkif->domid;
350 break;
352 case CTLMSG_CLOSE:
353 DPRINTF("Write_msg called: CTLMSG_CLOSE\n");
355 msglen = sizeof(msg_hdr_t);
356 buf = malloc(msglen);
358 /*Assign header fields*/
359 msg = (msg_hdr_t *)buf;
360 msg->type = CTLMSG_CLOSE;
361 msg->len = msglen;
362 msg->drivertype = blkif->drivertype;
363 msg->cookie = blkif->cookie;
365 break;
367 case CTLMSG_PID:
368 DPRINTF("Write_msg called: CTLMSG_PID\n");
370 msglen = sizeof(msg_hdr_t);
371 buf = malloc(msglen);
373 /*Assign header fields*/
374 msg = (msg_hdr_t *)buf;
375 msg->type = CTLMSG_PID;
376 msg->len = msglen;
377 msg->drivertype = blkif->drivertype;
378 msg->cookie = blkif->cookie;
380 break;
382 default:
383 return -1;
384 }
386 /*Now send the message*/
387 ret = 0;
388 FD_ZERO(&writefds);
389 FD_SET(fd,&writefds);
390 timeout.tv_sec = max_timeout; /*Wait for up to max_timeout seconds*/
391 timeout.tv_usec = 0;
392 if (select(fd+1, (fd_set *) 0, &writefds,
393 (fd_set *) 0, &timeout) > 0) {
394 len = write(fd, buf, msglen);
395 if (len == -1) DPRINTF("Write failed: (%d)\n",errno);
396 }
397 free(buf);
399 return len;
400 }
402 static int read_msg(int fd, int msgtype, void *ptr)
403 {
404 blkif_t *blkif;
405 blkif_info_t *blk;
406 msg_hdr_t *msg;
407 msg_pid_t *msg_pid;
408 char *p, *buf;
409 int msglen = MSG_SIZE, len, ret;
410 fd_set readfds;
411 struct timeval timeout;
412 image_t *image, *img;
415 blkif = (blkif_t *)ptr;
416 blk = blkif->info;
417 image = blkif->prv;
419 buf = malloc(MSG_SIZE);
421 ret = 0;
422 FD_ZERO(&readfds);
423 FD_SET(fd,&readfds);
424 timeout.tv_sec = max_timeout; /*Wait for up to max_timeout seconds*/
425 timeout.tv_usec = 0;
426 if (select(fd+1, &readfds, (fd_set *) 0,
427 (fd_set *) 0, &timeout) > 0) {
428 ret = read(fd, buf, msglen);
429 }
430 if (ret > 0) {
431 msg = (msg_hdr_t *)buf;
432 switch (msg->type)
433 {
434 case CTLMSG_IMG:
435 img = (image_t *)(buf + sizeof(msg_hdr_t));
436 image->size = img->size;
437 image->secsize = img->secsize;
438 image->info = img->info;
440 DPRINTF("Received CTLMSG_IMG: %llu, %lu, %u\n",
441 image->size, image->secsize, image->info);
442 if(msgtype != CTLMSG_IMG) ret = 0;
443 break;
445 case CTLMSG_IMG_FAIL:
446 DPRINTF("Received CTLMSG_IMG_FAIL, "
447 "unable to open image\n");
448 ret = 0;
449 break;
451 case CTLMSG_NEWDEV_RSP:
452 DPRINTF("Received CTLMSG_NEWDEV_RSP\n");
453 if(msgtype != CTLMSG_NEWDEV_RSP) ret = 0;
454 break;
456 case CTLMSG_NEWDEV_FAIL:
457 DPRINTF("Received CTLMSG_NEWDEV_FAIL\n");
458 ret = 0;
459 break;
461 case CTLMSG_CLOSE_RSP:
462 DPRINTF("Received CTLMSG_CLOSE_RSP\n");
463 if (msgtype != CTLMSG_CLOSE_RSP) ret = 0;
464 break;
466 case CTLMSG_PID_RSP:
467 DPRINTF("Received CTLMSG_PID_RSP\n");
468 if (msgtype != CTLMSG_PID_RSP) ret = 0;
469 else {
470 msg_pid = (msg_pid_t *)
471 (buf + sizeof(msg_hdr_t));
472 blkif->tappid = msg_pid->pid;
473 DPRINTF("\tPID: [%d]\n",blkif->tappid);
474 }
475 break;
476 default:
477 DPRINTF("UNKNOWN MESSAGE TYPE RECEIVED\n");
478 ret = 0;
479 break;
480 }
481 }
483 free(buf);
485 return ret;
487 }
489 static int launch_tapdisk_provider(char **argv)
490 {
491 pid_t child;
493 if ((child = fork()) < 0)
494 return -1;
496 if (!child) {
497 int i;
498 for (i = 0 ; i < sysconf(_SC_OPEN_MAX) ; i++)
499 if (i != STDIN_FILENO &&
500 i != STDOUT_FILENO &&
501 i != STDERR_FILENO)
502 close(i);
504 execvp(argv[0], argv);
505 DPRINTF("execvp failed: %d (%s)\n", errno, strerror(errno));
506 DPRINTF("PATH = %s\n", getenv("PATH"));
507 _exit(1);
508 } else {
509 pid_t got;
510 do {
511 got = waitpid(child, NULL, 0);
512 } while (got != child);
513 }
514 return child;
515 }
517 static int launch_tapdisk(char *wrctldev, char *rdctldev)
518 {
519 char *argv[] = { "tapdisk", wrctldev, rdctldev, NULL };
521 if (launch_tapdisk_provider(argv) < 0)
522 return -1;
524 return 0;
525 }
527 static int launch_tapdisk_ioemu(void)
528 {
529 char *argv[] = { "tapdisk-ioemu", NULL };
530 return launch_tapdisk_provider(argv);
531 }
533 /*
534 * Connect to an ioemu based disk provider (qemu-dm or tapdisk-ioemu)
535 *
536 * If the domain has a device model, connect to qemu-dm through the
537 * domain specific pipe. Otherwise use a single tapdisk-ioemu instance
538 * which is represented by domid 0 and provides access for Dom0 and
539 * all DomUs without device model.
540 */
541 static int connect_qemu(blkif_t *blkif, int domid)
542 {
543 char *rdctldev, *wrctldev;
545 static int tapdisk_ioemu_pid = 0;
546 static int dom0_readfd = 0;
547 static int dom0_writefd = 0;
548 int refresh_pid = 0;
550 if (asprintf(&rdctldev, BLKTAP_CTRL_DIR "/qemu-read-%d", domid) < 0)
551 return -1;
553 if (asprintf(&wrctldev, BLKTAP_CTRL_DIR "/qemu-write-%d", domid) < 0) {
554 free(rdctldev);
555 return -1;
556 }
558 DPRINTF("Using qemu blktap pipe: %s\n", rdctldev);
560 if (domid == 0) {
561 /*
562 * tapdisk-ioemu exits as soon as the last image is
563 * disconnected. Check if it is still running.
564 */
565 if (tapdisk_ioemu_pid == 0 || kill(tapdisk_ioemu_pid, 0)) {
566 /* No device model and tapdisk-ioemu doesn't run yet */
567 DPRINTF("Launching tapdisk-ioemu\n");
568 launch_tapdisk_ioemu();
570 dom0_readfd = open_ctrl_socket(wrctldev);
571 dom0_writefd = open_ctrl_socket(rdctldev);
573 refresh_pid = 1;
574 }
576 DPRINTF("Using tapdisk-ioemu connection\n");
577 blkif->fds[READ] = dom0_readfd;
578 blkif->fds[WRITE] = dom0_writefd;
580 if (refresh_pid) {
581 get_tapdisk_pid(blkif);
582 tapdisk_ioemu_pid = blkif->tappid;
583 }
585 } else if (access(rdctldev, R_OK | W_OK) == 0) {
586 /* Use existing pipe to the device model */
587 DPRINTF("Using qemu-dm connection\n");
588 blkif->fds[READ] = open_ctrl_socket(wrctldev);
589 blkif->fds[WRITE] = open_ctrl_socket(rdctldev);
590 } else {
591 /* No device model => try with tapdisk-ioemu */
592 DPRINTF("No device model\n");
593 connect_qemu(blkif, 0);
594 }
596 free(rdctldev);
597 free(wrctldev);
599 if (blkif->fds[READ] == -1 || blkif->fds[WRITE] == -1)
600 return -1;
602 DPRINTF("Attached to qemu blktap pipes\n");
603 return 0;
604 }
606 /* Launch tapdisk instance */
607 static int connect_tapdisk(blkif_t *blkif, int minor)
608 {
609 char *rdctldev = NULL, *wrctldev = NULL;
610 int ret = -1;
612 DPRINTF("tapdisk process does not exist:\n");
614 if (asprintf(&rdctldev,
615 "%s/tapctrlread%d", BLKTAP_CTRL_DIR, minor) == -1)
616 goto fail;
618 if (asprintf(&wrctldev,
619 "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, minor) == -1)
620 goto fail;
622 blkif->fds[READ] = open_ctrl_socket(rdctldev);
623 blkif->fds[WRITE] = open_ctrl_socket(wrctldev);
625 if (blkif->fds[READ] == -1 || blkif->fds[WRITE] == -1)
626 goto fail;
628 /*launch the new process*/
629 DPRINTF("Launching process, CMDLINE [tapdisk %s %s]\n",
630 wrctldev, rdctldev);
632 if (launch_tapdisk(wrctldev, rdctldev) == -1) {
633 DPRINTF("Unable to fork, cmdline: [tapdisk %s %s]\n",
634 wrctldev, rdctldev);
635 goto fail;
636 }
638 ret = 0;
640 fail:
641 if (rdctldev)
642 free(rdctldev);
644 if (wrctldev)
645 free(wrctldev);
647 return ret;
648 }
650 static int blktapctrl_new_blkif(blkif_t *blkif)
651 {
652 blkif_info_t *blk;
653 int major, minor, fd_read, fd_write, type, new;
654 char *rdctldev, *wrctldev, *ptr;
655 image_t *image;
656 blkif_t *exist = NULL;
657 static uint16_t next_cookie = 0;
658 int use_ioemu;
660 DPRINTF("Received a poll for a new vbd\n");
661 if ( ((blk=blkif->info) != NULL) && (blk->params != NULL) ) {
662 if (blktap_interface_create(ctlfd, &major, &minor, blkif) < 0)
663 return -1;
665 if (test_path(blk->params, &ptr, &type, &exist, &use_ioemu) != 0) {
666 DPRINTF("Error in blktap device string(%s).\n",
667 blk->params);
668 goto fail;
669 }
670 blkif->drivertype = type;
671 blkif->cookie = next_cookie++;
673 if (!exist) {
674 if (use_ioemu) {
675 if (connect_qemu(blkif, blkif->domid))
676 goto fail;
677 } else {
678 if (connect_tapdisk(blkif, minor))
679 goto fail;
680 }
682 } else {
683 DPRINTF("Process exists!\n");
684 blkif->fds[READ] = exist->fds[READ];
685 blkif->fds[WRITE] = exist->fds[WRITE];
686 }
688 add_disktype(blkif, type);
689 blkif->major = major;
690 blkif->minor = minor;
692 image = (image_t *)malloc(sizeof(image_t));
693 blkif->prv = (void *)image;
694 blkif->ops = &tapdisk_ops;
696 /*Retrieve the PID of the new process*/
697 if (get_tapdisk_pid(blkif) <= 0) {
698 DPRINTF("Unable to contact disk process\n");
699 goto fail;
700 }
702 /* Both of the following read and write calls will block up to
703 * max_timeout val*/
704 if (write_msg(blkif->fds[WRITE], CTLMSG_PARAMS, blkif, ptr)
705 <= 0) {
706 DPRINTF("Write_msg failed - CTLMSG_PARAMS\n");
707 goto fail;
708 }
710 if (read_msg(blkif->fds[READ], CTLMSG_IMG, blkif) <= 0) {
711 DPRINTF("Read_msg failure - CTLMSG_IMG\n");
712 goto fail;
713 }
715 } else return -1;
717 return 0;
718 fail:
719 ioctl(ctlfd, BLKTAP_IOCTL_FREEINTF, minor);
720 return -EINVAL;
721 }
723 static int map_new_blktapctrl(blkif_t *blkif)
724 {
725 DPRINTF("Received a poll for a new devmap\n");
726 if (write_msg(blkif->fds[WRITE], CTLMSG_NEWDEV, blkif, NULL) <= 0) {
727 DPRINTF("Write_msg failed - CTLMSG_NEWDEV\n");
728 return -EINVAL;
729 }
731 if (read_msg(blkif->fds[READ], CTLMSG_NEWDEV_RSP, blkif) <= 0) {
732 DPRINTF("Read_msg failed - CTLMSG_NEWDEV_RSP\n");
733 return -EINVAL;
734 }
735 DPRINTF("Exiting map_new_blktapctrl\n");
737 return blkif->minor - 1;
738 }
740 static int unmap_blktapctrl(blkif_t *blkif)
741 {
742 DPRINTF("Unmapping vbd\n");
744 if (write_msg(blkif->fds[WRITE], CTLMSG_CLOSE, blkif, NULL) <= 0) {
745 DPRINTF("Write_msg failed - CTLMSG_CLOSE\n");
746 return -EINVAL;
747 }
749 if (del_disktype(blkif)) {
750 DPRINTF("Closing communication pipe to pid %d\n", blkif->tappid);
751 close(blkif->fds[WRITE]);
752 close(blkif->fds[READ]);
753 }
755 return 0;
756 }
758 int open_ctrl_socket(char *devname)
759 {
760 int ret;
761 int ipc_fd;
762 fd_set socks;
763 struct timeval timeout;
765 if (mkdir(BLKTAP_CTRL_DIR, 0755) == 0)
766 DPRINTF("Created %s directory\n", BLKTAP_CTRL_DIR);
767 ret = mkfifo(devname,S_IRWXU|S_IRWXG|S_IRWXO);
768 if ( (ret != 0) && (errno != EEXIST) ) {
769 DPRINTF("ERROR: pipe failed (%d)\n", errno);
770 exit(0);
771 }
773 ipc_fd = open(devname,O_RDWR|O_NONBLOCK);
775 if (ipc_fd < 0) {
776 DPRINTF("FD open failed\n");
777 return -1;
778 }
780 return ipc_fd;
781 }
783 static void print_drivers(void)
784 {
785 int i, size;
787 size = sizeof(dtypes)/sizeof(disk_info_t *);
788 DPRINTF("blktapctrl: v1.0.0\n");
789 for (i = 0; i < size; i++)
790 DPRINTF("Found driver: [%s]\n",dtypes[i]->name);
791 }
793 static void write_pidfile(long pid)
794 {
795 char buf[100];
796 int len;
797 int fd;
798 int flags;
800 fd = open(PIDFILE, O_RDWR | O_CREAT, 0600);
801 if (fd == -1) {
802 DPRINTF("Opening pid file failed (%d)\n", errno);
803 exit(1);
804 }
806 /* We exit silently if daemon already running. */
807 if (lockf(fd, F_TLOCK, 0) == -1)
808 exit(0);
810 /* Set FD_CLOEXEC, so that tapdisk doesn't get this file
811 descriptor. */
812 if ((flags = fcntl(fd, F_GETFD)) == -1) {
813 DPRINTF("F_GETFD failed (%d)\n", errno);
814 exit(1);
815 }
816 flags |= FD_CLOEXEC;
817 if (fcntl(fd, F_SETFD, flags) == -1) {
818 DPRINTF("F_SETFD failed (%d)\n", errno);
819 exit(1);
820 }
822 len = snprintf(buf, sizeof(buf), "%ld\n", pid);
823 if (write(fd, buf, len) != len) {
824 DPRINTF("Writing pid file failed (%d)\n", errno);
825 exit(1);
826 }
827 }
829 int main(int argc, char *argv[])
830 {
831 char *devname;
832 tapdev_info_t *ctlinfo;
833 int tap_pfd, store_pfd, xs_fd, ret, timeout, pfd_count, count=0;
834 struct xs_handle *h;
835 struct pollfd pfd[NUM_POLL_FDS];
836 pid_t process;
837 char buf[128];
839 __init_blkif();
840 snprintf(buf, sizeof(buf), "BLKTAPCTRL[%d]", getpid());
841 openlog(buf, LOG_CONS|LOG_ODELAY, LOG_DAEMON);
842 if (daemon(0,0)) {
843 DPRINTF("daemon failed (%d)\n", errno);
844 goto open_failed;
845 }
847 print_drivers();
848 init_driver_list();
849 init_rng();
851 register_new_blkif_hook(blktapctrl_new_blkif);
852 register_new_devmap_hook(map_new_blktapctrl);
853 register_new_unmap_hook(unmap_blktapctrl);
855 ctlfd = blktap_interface_open();
856 if (ctlfd < 0) {
857 DPRINTF("couldn't open blktap interface\n");
858 goto open_failed;
859 }
861 retry:
862 /* Set up store connection and watch. */
863 h = xs_daemon_open();
864 if (h == NULL) {
865 DPRINTF("xs_daemon_open failed -- "
866 "is xenstore running?\n");
867 if (count < MAX_ATTEMPTS) {
868 count++;
869 sleep(2);
870 goto retry;
871 } else goto open_failed;
872 }
874 ret = setup_probe_watch(h);
875 if (ret != 0) {
876 DPRINTF("Failed adding device probewatch\n");
877 xs_daemon_close(h);
878 goto open_failed;
879 }
881 ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE );
883 process = getpid();
884 write_pidfile(process);
885 ret = ioctl(ctlfd, BLKTAP_IOCTL_SENDPID, process );
887 /*Static pollhooks*/
888 pfd_count = 0;
889 tap_pfd = pfd_count++;
890 pfd[tap_pfd].fd = ctlfd;
891 pfd[tap_pfd].events = POLLIN;
893 store_pfd = pfd_count++;
894 pfd[store_pfd].fd = xs_fileno(h);
895 pfd[store_pfd].events = POLLIN;
897 while (run) {
898 timeout = 1000; /*Milliseconds*/
899 ret = poll(pfd, pfd_count, timeout);
901 if (ret > 0) {
902 if (pfd[store_pfd].revents) {
903 ret = xs_fire_next_watch(h);
904 }
905 }
906 }
908 xs_daemon_close(h);
909 ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_PASSTHROUGH );
910 close(ctlfd);
911 closelog();
913 return 0;
915 open_failed:
916 DPRINTF("Unable to start blktapctrl\n");
917 closelog();
918 return -1;
919 }
921 /*
922 * Local variables:
923 * c-file-style: "linux"
924 * indent-tabs-mode: t
925 * c-indent-level: 8
926 * c-basic-offset: 8
927 * tab-width: 8
928 * End:
929 */