ia64/xen-unstable

changeset 8267:5f574f9cb4bd

Tidy up use of environment variables in xenbus-hotplug interaction.

I've changed this to only write "backend" (xenbus_backend.root) into
XENBUS_BASE_PATH. After appending XENBUS_TYPE (also exported) you can
construct the "base path" easily in the scripts. I've completely
removed writing XENBUS_FRONTEND_ID in favour of reading it from the
store at "$XENBUS_PATH/frontend-id".

Avoid re-reading the store over and over for the frontend domain's vm-path.

The patch also fixes a memory leak in backend_bus_id where we
leak the memory referenced by frontend if the function succeeds.

This work is by Christian Limpach.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Wed Dec 07 11:57:26 2005 +0000 (2005-12-07)
parents 91046e4a49dd
children ab300e9fe02b
files linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c tools/examples/block
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c	Wed Dec 07 11:51:31 2005 +0000
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c	Wed Dec 07 11:57:26 2005 +0000
     1.3 @@ -59,8 +59,6 @@ extern struct semaphore xenwatch_mutex;
     1.4  
     1.5  #define streq(a, b) (strcmp((a), (b)) == 0)
     1.6  
     1.7 -static char *kasprintf(const char *fmt, ...);
     1.8 -
     1.9  static struct notifier_block *xenstore_chain;
    1.10  
    1.11  /* If something in array of ids matches this device, return it. */
    1.12 @@ -209,14 +207,13 @@ static int backend_bus_id(char bus_id[BU
    1.13  		return err;
    1.14  	if (strlen(frontend) == 0)
    1.15  		err = -ERANGE;
    1.16 -
    1.17  	if (!err && !xenbus_exists(NULL, frontend, ""))
    1.18  		err = -ENOENT;
    1.19  
    1.20 -	if (err) {
    1.21 -		kfree(frontend);
    1.22 +	kfree(frontend);
    1.23 +
    1.24 +	if (err)
    1.25  		return err;
    1.26 -	}
    1.27  
    1.28  	if (snprintf(bus_id, BUS_ID_SIZE,
    1.29  		     "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
    1.30 @@ -225,69 +222,7 @@ static int backend_bus_id(char bus_id[BU
    1.31  }
    1.32  
    1.33  static int xenbus_hotplug_backend(struct device *dev, char **envp,
    1.34 -				  int num_envp, char *buffer, int buffer_size)
    1.35 -{
    1.36 -	struct xenbus_device *xdev;
    1.37 -	struct xenbus_driver *drv = NULL;
    1.38 -	int i = 0;
    1.39 -	int length = 0;
    1.40 -	char *basepath_end;
    1.41 -	char *frontend_id;
    1.42 -
    1.43 -	DPRINTK("");
    1.44 -
    1.45 -	if (dev == NULL)
    1.46 -		return -ENODEV;
    1.47 -
    1.48 -	xdev = to_xenbus_device(dev);
    1.49 -	if (xdev == NULL)
    1.50 -		return -ENODEV;
    1.51 -
    1.52 -	if (dev->driver)
    1.53 -		drv = to_xenbus_driver(dev->driver);
    1.54 -
    1.55 -	/* stuff we want to pass to /sbin/hotplug */
    1.56 -	add_hotplug_env_var(envp, num_envp, &i,
    1.57 -			    buffer, buffer_size, &length,
    1.58 -			    "XENBUS_TYPE=%s", xdev->devicetype);
    1.59 -
    1.60 -	add_hotplug_env_var(envp, num_envp, &i,
    1.61 -			    buffer, buffer_size, &length,
    1.62 -			    "XENBUS_PATH=%s", xdev->nodename);
    1.63 -
    1.64 -	add_hotplug_env_var(envp, num_envp, &i,
    1.65 -			    buffer, buffer_size, &length,
    1.66 -			    "XENBUS_BASE_PATH=%s", xdev->nodename);
    1.67 -
    1.68 -	basepath_end = strrchr(envp[i - 1], '/');
    1.69 -	length -= strlen(basepath_end);
    1.70 -	*basepath_end = '\0';
    1.71 -	basepath_end = strrchr(envp[i - 1], '/');
    1.72 -	length -= strlen(basepath_end);
    1.73 -	*basepath_end = '\0';
    1.74 -
    1.75 -	basepath_end++;
    1.76 -	frontend_id = kmalloc(strlen(basepath_end) + 1, GFP_KERNEL);
    1.77 -	strcpy(frontend_id, basepath_end);
    1.78 -	add_hotplug_env_var(envp, num_envp, &i,
    1.79 -			    buffer, buffer_size, &length,
    1.80 -			    "XENBUS_FRONTEND_ID=%s", frontend_id);
    1.81 -	kfree(frontend_id);
    1.82 -
    1.83 -	/* terminate, set to next free slot, shrink available space */
    1.84 -	envp[i] = NULL;
    1.85 -	envp = &envp[i];
    1.86 -	num_envp -= i;
    1.87 -	buffer = &buffer[length];
    1.88 -	buffer_size -= length;
    1.89 -
    1.90 -	if (drv && drv->hotplug)
    1.91 -		return drv->hotplug(xdev, envp, num_envp, buffer,
    1.92 -				    buffer_size);
    1.93 -
    1.94 -	return 0;
    1.95 -}
    1.96 -
    1.97 +				  int num_envp, char *buffer, int buffer_size);
    1.98  static int xenbus_probe_backend(const char *type, const char *domid);
    1.99  static struct xen_bus_type xenbus_backend = {
   1.100  	.root = "backend",
   1.101 @@ -304,6 +239,52 @@ static struct xen_bus_type xenbus_backen
   1.102  	},
   1.103  };
   1.104  
   1.105 +static int xenbus_hotplug_backend(struct device *dev, char **envp,
   1.106 +				  int num_envp, char *buffer, int buffer_size)
   1.107 +{
   1.108 +	struct xenbus_device *xdev;
   1.109 +	struct xenbus_driver *drv;
   1.110 +	int i = 0;
   1.111 +	int length = 0;
   1.112 +
   1.113 +	DPRINTK("");
   1.114 +
   1.115 +	if (dev == NULL)
   1.116 +		return -ENODEV;
   1.117 +
   1.118 +	xdev = to_xenbus_device(dev);
   1.119 +	if (xdev == NULL)
   1.120 +		return -ENODEV;
   1.121 +
   1.122 +	/* stuff we want to pass to /sbin/hotplug */
   1.123 +	add_hotplug_env_var(envp, num_envp, &i,
   1.124 +			    buffer, buffer_size, &length,
   1.125 +			    "XENBUS_TYPE=%s", xdev->devicetype);
   1.126 +
   1.127 +	add_hotplug_env_var(envp, num_envp, &i,
   1.128 +			    buffer, buffer_size, &length,
   1.129 +			    "XENBUS_PATH=%s", xdev->nodename);
   1.130 +
   1.131 +	add_hotplug_env_var(envp, num_envp, &i,
   1.132 +			    buffer, buffer_size, &length,
   1.133 +			    "XENBUS_BASE_PATH=%s", xenbus_backend.root);
   1.134 +
   1.135 +	/* terminate, set to next free slot, shrink available space */
   1.136 +	envp[i] = NULL;
   1.137 +	envp = &envp[i];
   1.138 +	num_envp -= i;
   1.139 +	buffer = &buffer[length];
   1.140 +	buffer_size -= length;
   1.141 +
   1.142 +	if (dev->driver) {
   1.143 +		drv = to_xenbus_driver(dev->driver);
   1.144 +		if (drv && drv->hotplug)
   1.145 +			return drv->hotplug(xdev, envp, num_envp, buffer,
   1.146 +					    buffer_size);
   1.147 +	}
   1.148 +
   1.149 +	return 0;
   1.150 +}
   1.151  
   1.152  static void otherend_changed(struct xenbus_watch *watch,
   1.153  			     const char **vec, unsigned int len)
     2.1 --- a/tools/examples/block	Wed Dec 07 11:51:31 2005 +0000
     2.2 +++ b/tools/examples/block	Wed Dec 07 11:57:26 2005 +0000
     2.3 @@ -89,12 +89,12 @@ check_sharing()
     2.4      fi
     2.5    done
     2.6  
     2.7 -  for dom in $(xenstore-list "$XENBUS_BASE_PATH")
     2.8 +  local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
     2.9 +  for dom in $(xenstore-list "$base_path")
    2.10    do
    2.11 -    for dev in $(xenstore-list "$XENBUS_BASE_PATH/$dom")
    2.12 +    for dev in $(xenstore-list "$base_path/$dom")
    2.13      do
    2.14 -      d=$(xenstore_read_default \
    2.15 -            "$XENBUS_BASE_PATH/$dom/$dev/physical-device" "")
    2.16 +      d=$(xenstore_read_default "$base_path/$dom/$dev/physical-device" "")
    2.17  
    2.18        if [ "$d" == "$devmm" ]
    2.19        then
    2.20 @@ -106,7 +106,7 @@ check_sharing()
    2.21              return
    2.22            fi
    2.23          else
    2.24 -          local m=$(xenstore_read "$XENBUS_BASE_PATH/$dom/$dev/mode")
    2.25 +          local m=$(xenstore_read "$base_path/$dom/$dev/mode")
    2.26            m=$(canonicalise_mode "$m")
    2.27  
    2.28            if [ "$m" == 'w' ]
    2.29 @@ -128,12 +128,10 @@ check_sharing()
    2.30  
    2.31  same_vm()
    2.32  {
    2.33 -  local thisdom="$XENBUS_FRONTEND_ID"
    2.34    local otherdom="$1"
    2.35 -  local thisvm=$(xenstore-read "/local/domain/$thisdom/vm")
    2.36    local othervm=$(xenstore-read "/local/domain/$otherdom/vm")
    2.37  
    2.38 -  [ "$thisvm" == "$othervm" ]
    2.39 +  [ "$FRONTEND_UUID" == "$othervm" ]
    2.40  }
    2.41  
    2.42  
    2.43 @@ -240,6 +238,9 @@ case "$command" in
    2.44      case $t in 
    2.45        phy)
    2.46          dev=$(expand_dev $p)
    2.47 +        FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
    2.48 +        FRONTEND_UUID=$(xenstore_read_default \
    2.49 +            "/local/domain/$FRONTEND_ID/vm" 'unknown')
    2.50          claim_lock "block"
    2.51          check_device_sharing "$dev" "$mode"
    2.52  	write_dev "$dev"