ia64/xen-unstable

changeset 6298:e8c2c3123ec6

Improve error paths and cleanup code.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Aug 19 17:38:07 2005 +0000 (2005-08-19)
parents 1a0723cd37f1
children 631cc5dc3e8a
files linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c linux-2.6-xen-sparse/include/asm-xen/xenbus.h
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Fri Aug 19 16:06:43 2005 +0000
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Fri Aug 19 17:38:07 2005 +0000
     1.3 @@ -182,16 +182,20 @@ static void backend_changed(struct xenbu
     1.4  			    "frontend-id", "%li", &be->frontend_id,
     1.5  			    "frontend", NULL, &frontend,
     1.6  			    NULL);
     1.7 -	if (err == -ENOENT || err == -ERANGE ||
     1.8 +	if (XENBUS_EXIST_ERR(err) ||
     1.9  	    strlen(frontend) == 0 || !xenbus_exists(frontend, "")) {
    1.10 -		if (frontend)
    1.11 -			kfree(frontend);
    1.12  		/* If we can't get a frontend path and a frontend-id,
    1.13  		 * then our bus-id is no longer valid and we need to
    1.14  		 * destroy the backend device.
    1.15  		 */
    1.16  		goto device_fail;
    1.17  	}
    1.18 +	if (err < 0) {
    1.19 +		xenbus_dev_error(dev, err,
    1.20 +				 "reading %s/frontend or frontend-id",
    1.21 +				 dev->nodename);
    1.22 +		goto device_fail;
    1.23 +	}
    1.24  
    1.25  	if (!be->frontpath || strcmp(frontend, be->frontpath)) {
    1.26  		if (be->watch.node)
    1.27 @@ -199,6 +203,7 @@ static void backend_changed(struct xenbu
    1.28  		if (be->frontpath)
    1.29  			kfree(be->frontpath);
    1.30  		be->frontpath = frontend;
    1.31 +		frontend = NULL;
    1.32  		be->watch.node = be->frontpath;
    1.33  		be->watch.callback = frontend_changed;
    1.34  		err = register_xenbus_watch(&be->watch);
    1.35 @@ -206,14 +211,13 @@ static void backend_changed(struct xenbu
    1.36  			be->watch.node = NULL;
    1.37  			goto device_fail;
    1.38  		}
    1.39 -	} else
    1.40 -		kfree(frontend);
    1.41 +	}
    1.42  
    1.43  	err = xenbus_scanf(dev->nodename, "physical-device", "%li", &pdev);
    1.44 -	if (err == -ENOENT || err == -ERANGE)
    1.45 +	if (XENBUS_EXIST_ERR(err))
    1.46  		goto out;
    1.47  	if (err < 0) {
    1.48 -		xenbus_dev_error(dev, err, "Reading physical-device");
    1.49 +		xenbus_dev_error(dev, err, "reading physical-device");
    1.50  		goto device_fail;
    1.51  	}
    1.52  	if (be->pdev && be->pdev != pdev) {
    1.53 @@ -253,12 +257,14 @@ static void backend_changed(struct xenbu
    1.54  		frontend_changed(&be->watch, be->frontpath);
    1.55  	}
    1.56  
    1.57 + out:
    1.58 +	if (frontend)
    1.59 +		kfree(frontend);
    1.60  	return;
    1.61  
    1.62   device_fail:
    1.63  	device_unregister(&be->dev->dev);
    1.64 - out:
    1.65 -	return;
    1.66 +	goto out;
    1.67  }
    1.68  
    1.69  static int blkback_probe(struct xenbus_device *dev,
     2.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Fri Aug 19 16:06:43 2005 +0000
     2.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Fri Aug 19 17:38:07 2005 +0000
     2.3 @@ -1134,25 +1134,24 @@ static void watch_for_status(struct xenb
     2.4  			    "info", "%u", &binfo,
     2.5  			    "sector-size", "%lu", &sector_size,
     2.6  			    NULL);
     2.7 -
     2.8 -	if (err)
     2.9 +	if (err) {
    2.10  		xenbus_dev_error(info->dev, err, "reading backend fields");
    2.11 -	else {
    2.12 -		xlvbd_add(sectors, info->vdevice, info->handle, binfo,
    2.13 -			  sector_size);
    2.14 -		info->connected = 1;
    2.15 +		return;
    2.16 +	}
    2.17 +
    2.18 +	xlvbd_add(sectors, info->vdevice, info->handle, binfo, sector_size);
    2.19 +	info->connected = 1;
    2.20  
    2.21 -		/* First to connect?  blkif is now connected. */
    2.22 -		if (blkif_vbds_connected++ == 0)
    2.23 -			blkif_state = BLKIF_STATE_CONNECTED;
    2.24 -
    2.25 -		xenbus_dev_ok(info->dev);
    2.26 +	/* First to connect?  blkif is now connected. */
    2.27 +	if (blkif_vbds_connected++ == 0)
    2.28 +		blkif_state = BLKIF_STATE_CONNECTED;
    2.29  
    2.30 -		/* Kick pending requests. */
    2.31 -		spin_lock_irq(&blkif_io_lock);
    2.32 -		kick_pending_request_queues();
    2.33 -		spin_unlock_irq(&blkif_io_lock);
    2.34 -	}
    2.35 +	xenbus_dev_ok(info->dev);
    2.36 +
    2.37 +	/* Kick pending requests. */
    2.38 +	spin_lock_irq(&blkif_io_lock);
    2.39 +	kick_pending_request_queues();
    2.40 +	spin_unlock_irq(&blkif_io_lock);
    2.41  }
    2.42  
    2.43  static int setup_blkring(struct xenbus_device *dev, unsigned int backend_id)
    2.44 @@ -1199,36 +1198,28 @@ static int talk_to_backend(struct xenbus
    2.45  	const char *message;
    2.46  	int err, backend_id;
    2.47  
    2.48 -	backend = xenbus_read(dev->nodename, "backend", NULL);
    2.49 -	if (IS_ERR(backend)) {
    2.50 -		err = PTR_ERR(backend);
    2.51 -		if (err == -ENOENT)
    2.52 -			goto out;
    2.53 -		xenbus_dev_error(dev, err, "reading %s/backend",
    2.54 +	backend = NULL;
    2.55 +	err = xenbus_gather(dev->nodename,
    2.56 +			    "backend-id", "%i", &backend_id,
    2.57 +			    "backend", NULL, &backend,
    2.58 +			    NULL);
    2.59 +	if (XENBUS_EXIST_ERR(err))
    2.60 +		goto out;
    2.61 +	if (backend && strlen(backend) == 0) {
    2.62 +		err = -ENOENT;
    2.63 +		goto out;
    2.64 +	}
    2.65 +	if (err < 0) {
    2.66 +		xenbus_dev_error(dev, err, "reading %s/backend or backend-id",
    2.67  				 dev->nodename);
    2.68  		goto out;
    2.69  	}
    2.70 -	if (strlen(backend) == 0) {
    2.71 -		err = -ENOENT;
    2.72 -		goto free_backend;
    2.73 -	}
    2.74 -
    2.75 -	/* FIXME: This driver can't handle backends on different
    2.76 -	 * domains.  Check and fail gracefully. */
    2.77 -	err = xenbus_scanf(dev->nodename, "backend-id", "%i", &backend_id);
    2.78 -	if (err == -ENOENT)
    2.79 -		goto free_backend;
    2.80 - 	if (err < 0) {
    2.81 -		xenbus_dev_error(dev, err, "reading %s/backend-id",
    2.82 -				 dev->nodename);
    2.83 - 		goto free_backend;
    2.84 - 	}
    2.85  
    2.86  	/* First device?  We create shared ring, alloc event channel. */
    2.87  	if (blkif_vbds == 0) {
    2.88  		err = setup_blkring(dev, backend_id);
    2.89  		if (err)
    2.90 -			goto free_backend;
    2.91 +			goto out;
    2.92  	}
    2.93  
    2.94  	err = xenbus_transaction_start(dev->nodename);
    2.95 @@ -1258,9 +1249,11 @@ static int talk_to_backend(struct xenbus
    2.96  		goto abort_transaction;
    2.97  	}
    2.98  
    2.99 -	info->watch.node = info->backend = backend;
   2.100 +	info->backend = backend;
   2.101 +	backend = NULL;
   2.102 +
   2.103 +	info->watch.node = info->backend;
   2.104  	info->watch.callback = watch_for_status;
   2.105 -
   2.106  	err = register_xenbus_watch(&info->watch);
   2.107  	if (err) {
   2.108  		message = "registering watch on backend";
   2.109 @@ -1272,20 +1265,20 @@ static int talk_to_backend(struct xenbus
   2.110  		xenbus_dev_error(dev, err, "completing transaction");
   2.111  		goto destroy_blkring;
   2.112  	}
   2.113 -	return 0;
   2.114  
   2.115 -abort_transaction:
   2.116 + out:
   2.117 +	if (backend)
   2.118 +		kfree(backend);
   2.119 +	return err;
   2.120 +
   2.121 + abort_transaction:
   2.122  	xenbus_transaction_end(1);
   2.123  	/* Have to do this *outside* transaction.  */
   2.124  	xenbus_dev_error(dev, err, "%s", message);
   2.125 -destroy_blkring:
   2.126 + destroy_blkring:
   2.127  	if (blkif_vbds == 0)
   2.128  		blkif_free();
   2.129 -free_backend:
   2.130 -	kfree(backend);
   2.131 -out:
   2.132 -	printk("%s:%u = %i\n", __FILE__, __LINE__, err);
   2.133 -	return err;
   2.134 +	goto out;
   2.135  }
   2.136  
   2.137  /* Setup supplies the backend dir, virtual device.
   2.138 @@ -1301,7 +1294,7 @@ static int blkfront_probe(struct xenbus_
   2.139  
   2.140  	/* FIXME: Use dynamic device id if this is not set. */
   2.141  	err = xenbus_scanf(dev->nodename, "virtual-device", "%i", &vdevice);
   2.142 -	if (err == -ENOENT)
   2.143 +	if (XENBUS_EXIST_ERR(err))
   2.144  		return err;
   2.145  	if (err < 0) {
   2.146  		xenbus_dev_error(dev, err, "reading virtual-device");
   2.147 @@ -1316,6 +1309,7 @@ static int blkfront_probe(struct xenbus_
   2.148  	info->dev = dev;
   2.149  	info->vdevice = vdevice;
   2.150  	info->connected = 0;
   2.151 +
   2.152  	/* Front end dir is a number, which is used as the id. */
   2.153  	info->handle = simple_strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
   2.154  	dev->data = info;
   2.155 @@ -1427,8 +1421,8 @@ static int __init xlblk_init(void)
   2.156  
   2.157  #ifdef CONFIG_XEN_BLKDEV_GRANT
   2.158      /* A grant for every ring slot, plus one for the ring itself. */
   2.159 -    if ( 0 > gnttab_alloc_grant_references(MAXIMUM_OUTSTANDING_BLOCK_REQS + 1,
   2.160 -                                           &gref_head, &gref_terminal) )
   2.161 +    if (gnttab_alloc_grant_references(MAXIMUM_OUTSTANDING_BLOCK_REQS + 1,
   2.162 +				      &gref_head, &gref_terminal) < 0)
   2.163          return 1;
   2.164      printk(KERN_ALERT "Blkif frontend is using grant tables.\n");
   2.165  #endif
     3.1 --- a/linux-2.6-xen-sparse/include/asm-xen/xenbus.h	Fri Aug 19 16:06:43 2005 +0000
     3.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/xenbus.h	Fri Aug 19 17:38:07 2005 +0000
     3.3 @@ -128,4 +128,14 @@ void reregister_xenbus_watches(void);
     3.4  void xenbus_suspend(void);
     3.5  void xenbus_resume(void);
     3.6  
     3.7 +#define XENBUS_IS_ERR_READ(str) ({			\
     3.8 +	if (!IS_ERR(str) && strlen(str) == 0) {		\
     3.9 +		kfree(str);				\
    3.10 +		str = ERR_PTR(-ERANGE);			\
    3.11 +	}						\
    3.12 +	IS_ERR(str);					\
    3.13 +})
    3.14 +
    3.15 +#define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
    3.16 +
    3.17  #endif /* _ASM_XEN_XENBUS_H */