ia64/xen-unstable

changeset 19354:c30742011bb8

blktap: Move error signaling to blktapctrl

Until now the udev script for blktap devices needs to decide if to
signal success or failure to xend. As this script runs completely
independent of blktapctrl and tapdisk/ioemu which do the real work,
the udev script can't even theoretically know if tapdisk is happy.

This patch removes the udev script and replaces its checks by new
ones in libblktap.

Signed-off-by: Kevin Wolf <kwolf@suse.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Mar 12 18:48:09 2009 +0000 (2009-03-12)
parents d8741d4dd05a
children 2377bb2d0316
files tools/blktap/drivers/blktapctrl.c tools/blktap/lib/xenbus.c tools/hotplug/Linux/Makefile tools/hotplug/Linux/blktap tools/hotplug/Linux/xen-backend.rules
line diff
     1.1 --- a/tools/blktap/drivers/blktapctrl.c	Thu Mar 12 18:46:26 2009 +0000
     1.2 +++ b/tools/blktap/drivers/blktapctrl.c	Thu Mar 12 18:48:09 2009 +0000
     1.3 @@ -659,9 +659,6 @@ static int blktapctrl_new_blkif(blkif_t 
     1.4  
     1.5  	DPRINTF("Received a poll for a new vbd\n");
     1.6  	if ( ((blk=blkif->info) != NULL) && (blk->params != NULL) ) {
     1.7 -		if (blktap_interface_create(ctlfd, &major, &minor, blkif) < 0)
     1.8 -			return -1;
     1.9 -
    1.10  		if (test_path(blk->params, &ptr, &type, &exist, &use_ioemu) != 0) {
    1.11                          DPRINTF("Error in blktap device string(%s).\n",
    1.12                                  blk->params);
    1.13 @@ -685,10 +682,6 @@ static int blktapctrl_new_blkif(blkif_t 
    1.14  			blkif->fds[WRITE] = exist->fds[WRITE];
    1.15  		}
    1.16  
    1.17 -		add_disktype(blkif, type);
    1.18 -		blkif->major = major;
    1.19 -		blkif->minor = minor;
    1.20 -
    1.21  		image = (image_t *)malloc(sizeof(image_t));
    1.22  		blkif->prv = (void *)image;
    1.23  		blkif->ops = &tapdisk_ops;
    1.24 @@ -712,11 +705,18 @@ static int blktapctrl_new_blkif(blkif_t 
    1.25  			goto fail;
    1.26  		}
    1.27  
    1.28 +		if (blktap_interface_create(ctlfd, &major, &minor, blkif) < 0)
    1.29 +			return -1;
    1.30 +
    1.31 +		blkif->major = major;
    1.32 +		blkif->minor = minor;
    1.33 +
    1.34 +		add_disktype(blkif, type);
    1.35 +
    1.36  	} else return -1;
    1.37  
    1.38  	return 0;
    1.39  fail:
    1.40 -	ioctl(ctlfd, BLKTAP_IOCTL_FREEINTF, minor);
    1.41  	return -EINVAL;
    1.42  }
    1.43  
     2.1 --- a/tools/blktap/lib/xenbus.c	Thu Mar 12 18:46:26 2009 +0000
     2.2 +++ b/tools/blktap/lib/xenbus.c	Thu Mar 12 18:48:09 2009 +0000
     2.3 @@ -48,6 +48,7 @@
     2.4  #include <poll.h>
     2.5  #include <time.h>
     2.6  #include <sys/time.h>
     2.7 +#include <unistd.h>
     2.8  #include "blktaplib.h"
     2.9  #include "list.h"
    2.10  #include "xs_api.h"
    2.11 @@ -149,6 +150,137 @@ static int backend_remove(struct xs_hand
    2.12  	return 0;
    2.13  }
    2.14  
    2.15 +static int check_sharing(struct xs_handle *h, struct backend_info *be)
    2.16 +{
    2.17 +	char *dom_uuid;
    2.18 +	char *cur_dom_uuid;
    2.19 +	char *path;
    2.20 +	char *mode;
    2.21 +	char *params;
    2.22 +	char **domains;
    2.23 +	char **devices;
    2.24 +	int i, j;
    2.25 +	unsigned int num_dom, num_dev;
    2.26 +	blkif_info_t *info;
    2.27 +	int ret = 0;
    2.28 +
    2.29 +	/* If the mode contains '!' or doesn't contain 'w' don't check anything */
    2.30 +	xs_gather(h, be->backpath, "mode", NULL, &mode, NULL);
    2.31 +	if (strchr(mode, '!'))
    2.32 +		goto out;
    2.33 +	if (strchr(mode, 'w') == NULL)
    2.34 +		goto out;
    2.35 +
    2.36 +	/* Get the UUID of the domain we want to attach to */
    2.37 +	if (asprintf(&path, "/local/domain/%ld", be->frontend_id) == -1)
    2.38 +		goto fail;
    2.39 +	xs_gather(h, path, "vm", NULL, &dom_uuid, NULL);
    2.40 +	free(path);
    2.41 +
    2.42 +	/* Iterate through the devices of all VMs */
    2.43 +	domains = xs_directory(h, XBT_NULL, "backend/tap", &num_dom);
    2.44 +	if (domains == NULL)
    2.45 +		num_dom = 0;
    2.46 +
    2.47 +	for (i = 0; !ret && (i < num_dom); i++) {
    2.48 +
    2.49 +		/* If it's the same VM, no action needed */
    2.50 +		if (asprintf(&path, "/local/domain/%s", domains[i]) == -1) {
    2.51 +			ret = -1;
    2.52 +			break;
    2.53 +		}
    2.54 +		xs_gather(h, path, "vm", NULL, &cur_dom_uuid, NULL);
    2.55 +		free(path);
    2.56 +
    2.57 +		if (!strcmp(cur_dom_uuid, dom_uuid)) {
    2.58 +			free(cur_dom_uuid);
    2.59 +			continue;
    2.60 +		}
    2.61 +
    2.62 +		/* Check the devices */
    2.63 +		if (asprintf(&path, "backend/tap/%s", domains[i]) == -1) {
    2.64 +			ret = -1;
    2.65 +			free(cur_dom_uuid);
    2.66 +			break;
    2.67 +		}
    2.68 +		devices = xs_directory(h, XBT_NULL, path, &num_dev);
    2.69 +		if (devices == NULL)
    2.70 +			num_dev = 0;
    2.71 +		free(path);
    2.72 +
    2.73 +		for (j = 0; !ret && (j < num_dev); j++) {
    2.74 +			if (asprintf(&path, "backend/tap/%s/%s", domains[i], devices[j]) == -1) {
    2.75 +				ret = -1;
    2.76 +				break;
    2.77 +			}
    2.78 +			xs_gather(h, path, "params", NULL, &params, NULL);
    2.79 +			free(path);
    2.80 +
    2.81 +			info =  be->blkif->info;
    2.82 +			if (strcmp(params, info->params)) {
    2.83 +				ret = -1;
    2.84 +			}
    2.85 +
    2.86 +			free(params);
    2.87 +		}
    2.88 +
    2.89 +		free(cur_dom_uuid);
    2.90 +		free(devices);
    2.91 +	}
    2.92 +	free(domains);
    2.93 +	free(dom_uuid);
    2.94 +	goto out;
    2.95 +
    2.96 +fail:
    2.97 +	ret = -1;
    2.98 +out:
    2.99 +	free(mode);
   2.100 +	return ret;
   2.101 +}
   2.102 +
   2.103 +static int check_image(struct xs_handle *h, struct backend_info *be,
   2.104 +	const char** errmsg)
   2.105 +{
   2.106 +	const char *tmp;
   2.107 +	const char *path;
   2.108 +	int mode;
   2.109 +	blkif_t *blkif = be->blkif;
   2.110 +	blkif_info_t *info = blkif->info;
   2.111 +
   2.112 +	/* Strip off the image type */
   2.113 +	path = info->params;
   2.114 +
   2.115 +	if (!strncmp(path, "tapdisk:", strlen("tapdisk:"))) {
   2.116 +		path += strlen("tapdisk:");
   2.117 +	} else if (!strncmp(path, "ioemu:", strlen("ioemu:"))) {
   2.118 +		path += strlen("ioemu:");
   2.119 +	}
   2.120 +
   2.121 +	tmp = strchr(path, ':');
   2.122 +	if (tmp != NULL)
   2.123 +		path = tmp + 1;
   2.124 +
   2.125 +	/* Check if the image exists and access is permitted */
   2.126 +	mode = R_OK;
   2.127 +	if (!be->readonly)
   2.128 +		mode |= W_OK;
   2.129 +	if (access(path, mode)) {
   2.130 +		if (errno == ENOENT)
   2.131 +			*errmsg = "File not found.";
   2.132 +		else
   2.133 +			*errmsg = "Insufficient file permissions.";
   2.134 +		return -1;
   2.135 +	}
   2.136 +
   2.137 +	/* Check that the image is not attached to a different VM */
   2.138 +	if (check_sharing(h, be)) {
   2.139 +		*errmsg = "File already in use by other domain";
   2.140 +		return -1;
   2.141 +	}
   2.142 +
   2.143 +	return 0;
   2.144 +}
   2.145 +
   2.146  static void ueblktap_setup(struct xs_handle *h, char *bepath)
   2.147  {
   2.148  	struct backend_info *be;
   2.149 @@ -156,6 +288,7 @@ static void ueblktap_setup(struct xs_han
   2.150  	int len, er, deverr;
   2.151  	long int pdev = 0, handle;
   2.152  	blkif_info_t *blk;
   2.153 +	const char* errmsg = NULL;
   2.154  	
   2.155  	be = be_lookup_be(bepath);
   2.156  	if (be == NULL)
   2.157 @@ -211,6 +344,9 @@ static void ueblktap_setup(struct xs_han
   2.158  			be->pdev = pdev;
   2.159  		}
   2.160  
   2.161 +		if (check_image(h, be, &errmsg))
   2.162 +			goto fail;
   2.163 +
   2.164  		er = blkif_init(be->blkif, handle, be->pdev, be->readonly);
   2.165  		if (er != 0) {
   2.166  			DPRINTF("Unable to open device %s\n",blk->params);
   2.167 @@ -246,12 +382,21 @@ static void ueblktap_setup(struct xs_han
   2.168  	}
   2.169  
   2.170  	be->blkif->state = CONNECTED;
   2.171 +	xs_printf(h, be->backpath, "hotplug-status", "connected");
   2.172 +
   2.173  	DPRINTF("[SETUP] Complete\n\n");
   2.174  	goto close;
   2.175  	
   2.176  fail:
   2.177 -	if ( (be != NULL) && (be->blkif != NULL) ) 
   2.178 +	if (be) {
   2.179 +		if (errmsg == NULL)
   2.180 +			errmsg = "Setting up the backend failed. See the log "
   2.181 +				"files in /var/log/xen/ for details.";
   2.182 +		xs_printf(h, be->backpath, "hotplug-error", errmsg);
   2.183 +		xs_printf(h, be->backpath, "hotplug-status", "error");
   2.184 +
   2.185  		backend_remove(h, be);
   2.186 +	}
   2.187  close:
   2.188  	if (path)
   2.189  		free(path);
   2.190 @@ -286,7 +431,8 @@ static void ueblktap_probe(struct xs_han
   2.191  	len = strsep_len(bepath, '/', 7);
   2.192  	if (len < 0) 
   2.193  		goto free_be;
   2.194 -	bepath[len] = '\0';
   2.195 +	if (bepath[len] != '\0')
   2.196 +		goto free_be;
   2.197  	
   2.198  	be = malloc(sizeof(*be));
   2.199  	if (!be) {
     3.1 --- a/tools/hotplug/Linux/Makefile	Thu Mar 12 18:46:26 2009 +0000
     3.2 +++ b/tools/hotplug/Linux/Makefile	Thu Mar 12 18:48:09 2009 +0000
     3.3 @@ -16,7 +16,6 @@ XEN_SCRIPTS += network-route vif-route
     3.4  XEN_SCRIPTS += network-nat vif-nat
     3.5  XEN_SCRIPTS += block
     3.6  XEN_SCRIPTS += block-enbd block-nbd
     3.7 -XEN_SCRIPTS += blktap
     3.8  XEN_SCRIPTS += vtpm vtpm-delete
     3.9  XEN_SCRIPTS += xen-hotplug-cleanup
    3.10  XEN_SCRIPTS += external-device-migrate
     4.1 --- a/tools/hotplug/Linux/blktap	Thu Mar 12 18:46:26 2009 +0000
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,93 +0,0 @@
     4.4 -#!/bin/bash
     4.5 -
     4.6 -# Copyright (c) 2005, XenSource Ltd.
     4.7 -
     4.8 -dir=$(dirname "$0")
     4.9 -. "$dir/xen-hotplug-common.sh"
    4.10 -. "$dir/block-common.sh"
    4.11 -
    4.12 -findCommand "$@"
    4.13 -
    4.14 -##
    4.15 -# check_blktap_sharing file mode
    4.16 -#
    4.17 -# Perform the sharing check for the given blktap and mode.
    4.18 -#
    4.19 -check_blktap_sharing()
    4.20 -{
    4.21 -    local file="$1"
    4.22 -    local mode="$2"
    4.23 -
    4.24 -    local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
    4.25 -    for dom in $(xenstore-list "$base_path")
    4.26 -    do
    4.27 -        for dev in $(xenstore-list "$base_path/$dom")
    4.28 -        do
    4.29 -            params=$(xenstore_read "$base_path/$dom/$dev/params" | cut -d: -f2)
    4.30 -            if [ "$file" = "$params" ]
    4.31 -            then
    4.32 -
    4.33 -                if [ "$mode" = 'w' ]
    4.34 -                then
    4.35 -                    if ! same_vm "$dom" 
    4.36 -                    then
    4.37 -                        echo 'guest'
    4.38 -                        return
    4.39 -                    fi
    4.40 -                else 
    4.41 -                    local m=$(xenstore_read "$base_path/$dom/$dev/mode")
    4.42 -                    m=$(canonicalise_mode "$m")
    4.43 -
    4.44 -                    if [ "$m" = 'w' ] 
    4.45 -                    then
    4.46 -                        if ! same_vm "$dom"
    4.47 -                        then
    4.48 -                            echo 'guest'
    4.49 -                            return
    4.50 -                        fi
    4.51 -                    fi
    4.52 -                fi
    4.53 -            fi
    4.54 -        done
    4.55 -    done
    4.56 -
    4.57 -    echo 'ok'
    4.58 -}
    4.59 -
    4.60 -
    4.61 -t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
    4.62 -if [ -n "$t" ]
    4.63 -then
    4.64 -    p=$(xenstore_read "$XENBUS_PATH/params")
    4.65 -    # if we have a ':', chew from head including :
    4.66 -    if echo $p | grep -q \:
    4.67 -    then
    4.68 -        p=${p#*:}
    4.69 -    fi
    4.70 -fi
    4.71 -# some versions of readlink cannot be passed a regular file
    4.72 -if [ -L "$p" ]; then
    4.73 -    file=$(readlink -f "$p") || fatal "$p link does not exist."
    4.74 -else
    4.75 -    file="$p"
    4.76 -fi
    4.77 -
    4.78 -if [ "$command" = 'add' ]
    4.79 -then
    4.80 -    [ -e "$file" ] || { fatal $file does not exist; }
    4.81 -
    4.82 -    FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
    4.83 -    FRONTEND_UUID=$(xenstore_read "/local/domain/$FRONTEND_ID/vm")
    4.84 -    mode=$(xenstore_read "$XENBUS_PATH/mode")
    4.85 -    mode=$(canonicalise_mode "$mode")
    4.86 -
    4.87 -    if [ "$mode" != '!' ] 
    4.88 -    then
    4.89 -        result=$(check_blktap_sharing "$file" "$mode")
    4.90 -        [ "$result" = 'ok' ] || ebusy "$file already in use by other domain"
    4.91 -    fi
    4.92 -
    4.93 -    success
    4.94 -fi
    4.95 -
    4.96 -exit 0
     5.1 --- a/tools/hotplug/Linux/xen-backend.rules	Thu Mar 12 18:46:26 2009 +0000
     5.2 +++ b/tools/hotplug/Linux/xen-backend.rules	Thu Mar 12 18:48:09 2009 +0000
     5.3 @@ -1,4 +1,3 @@
     5.4 -SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
     5.5  SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}"
     5.6  SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
     5.7  SUBSYSTEM=="xen-backend", KERNEL=="vif*", ACTION=="online", RUN+="$env{script} online"