ia64/xen-unstable

changeset 18044:b01303f59872

blktap: portability cleanup
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jul 14 10:03:09 2008 +0100 (2008-07-14)
parents f5d23ee2134e
children e2cec6725280
files tools/blktap/drivers/Makefile tools/blktap/drivers/blktapctrl.c tools/blktap/drivers/blktapctrl.h tools/blktap/drivers/blktapctrl_linux.c
line diff
     1.1 --- a/tools/blktap/drivers/Makefile	Mon Jul 14 10:00:47 2008 +0100
     1.2 +++ b/tools/blktap/drivers/Makefile	Mon Jul 14 10:03:09 2008 +0100
     1.3 @@ -36,11 +36,14 @@ BLK-OBJS-y  += block-qcow.o
     1.4  BLK-OBJS-y  += block-qcow2.o
     1.5  BLK-OBJS-y  += aes.o
     1.6  BLK-OBJS-y  += tapaio.o
     1.7 -BLK-OBJS-$(CONFIG_Linux) += blk_linux.c
     1.8 +BLK-OBJS-$(CONFIG_Linux) += blk_linux.o
     1.9 +
    1.10 +BLKTAB-OBJS-y := blktapctrl.o
    1.11 +BLKTAB-OBJS-$(CONFIG_Linux) += blktapctrl_linux.o
    1.12  
    1.13  all: $(IBIN) qcow-util
    1.14  
    1.15 -blktapctrl: blktapctrl.o
    1.16 +blktapctrl: $(BLKTAB-OBJS-y)
    1.17  	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDFLAGS_blktapctrl)
    1.18  
    1.19  tapdisk: tapdisk.o $(BLK-OBJS-y)
     2.1 --- a/tools/blktap/drivers/blktapctrl.c	Mon Jul 14 10:00:47 2008 +0100
     2.2 +++ b/tools/blktap/drivers/blktapctrl.c	Mon Jul 14 10:03:09 2008 +0100
     2.3 @@ -37,7 +37,6 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6  #include <sys/mman.h>
     2.7 -#include <sys/stat.h>
     2.8  #include <err.h>
     2.9  #include <errno.h>
    2.10  #include <sys/types.h>
    2.11 @@ -77,6 +76,32 @@ static int write_msg(int fd, int msgtype
    2.12  static int read_msg(int fd, int msgtype, void *ptr);
    2.13  static driver_list_entry_t *active_disks[MAX_DISK_TYPES];
    2.14  
    2.15 +
    2.16 +static unsigned long long tapdisk_get_size(blkif_t *blkif)
    2.17 +{
    2.18 +	image_t *img = (image_t *)blkif->prv;
    2.19 +	return img->size;
    2.20 +}
    2.21 +
    2.22 +static unsigned long tapdisk_get_secsize(blkif_t *blkif)
    2.23 +{
    2.24 +	image_t *img = (image_t *)blkif->prv;
    2.25 +	return img->secsize;
    2.26 +}
    2.27 +
    2.28 +static unsigned int tapdisk_get_info(blkif_t *blkif)
    2.29 +{
    2.30 +	image_t *img = (image_t *)blkif->prv;
    2.31 +	return img->info;
    2.32 +}
    2.33 +
    2.34 +struct blkif_ops tapdisk_ops = {
    2.35 +	.get_size = tapdisk_get_size,
    2.36 +	.get_secsize = tapdisk_get_secsize,
    2.37 +	.get_info = tapdisk_get_info,
    2.38 +};
    2.39 +
    2.40 +
    2.41  static void init_driver_list(void)
    2.42  {
    2.43  	int i;
    2.44 @@ -97,74 +122,6 @@ static void init_rng(void)
    2.45  	return;
    2.46  }
    2.47  
    2.48 -static void make_blktap_dev(char *devname, int major, int minor)
    2.49 -{
    2.50 -	struct stat st;
    2.51 -	
    2.52 -	if (lstat(devname, &st) != 0) {
    2.53 -		/*Need to create device*/
    2.54 -		if (mkdir(BLKTAP_DEV_DIR, 0755) == 0)
    2.55 -			DPRINTF("Created %s directory\n",BLKTAP_DEV_DIR);
    2.56 -		if (mknod(devname, S_IFCHR|0600,
    2.57 -                	makedev(major, minor)) == 0)
    2.58 -			DPRINTF("Created %s device\n",devname);
    2.59 -	} else {
    2.60 -		DPRINTF("%s device already exists\n",devname);
    2.61 -		/* it already exists, but is it the same major number */
    2.62 -		if (((st.st_rdev>>8) & 0xff) != major) {
    2.63 -			DPRINTF("%s has old major %d\n",
    2.64 -				devname,
    2.65 -				(unsigned int)((st.st_rdev >> 8) & 0xff));
    2.66 -			/* only try again if we succed in deleting it */
    2.67 -			if (!unlink(devname))
    2.68 -				make_blktap_dev(devname, major, minor);
    2.69 -		}
    2.70 -	}
    2.71 -}
    2.72 -
    2.73 -static int get_new_dev(int *major, int *minor, blkif_t *blkif)
    2.74 -{
    2.75 -	domid_translate_t tr;
    2.76 -	domid_translate_ext_t tr_ext;
    2.77 -	int ret;
    2.78 -	char *devname;
    2.79 -	
    2.80 -	if (blkif->be_id >= (1<<28)) {
    2.81 -		/* new-style backend-id, so use the extended structure */
    2.82 -		tr_ext.domid = blkif->domid;
    2.83 -		tr_ext.busid = blkif->be_id;
    2.84 -		ret = ioctl(ctlfd, BLKTAP_IOCTL_NEWINTF_EXT, &tr_ext);
    2.85 -		DPRINTF("Sent domid %d and be_id %d\n", tr_ext.domid,
    2.86 -			tr_ext.busid);
    2.87 -	}
    2.88 -	else {
    2.89 -		/* old-style backend-id; use the old structure */
    2.90 -		tr.domid = blkif->domid;
    2.91 -		tr.busid = (unsigned short)blkif->be_id;
    2.92 -		ret = ioctl(ctlfd, BLKTAP_IOCTL_NEWINTF, tr);
    2.93 -		DPRINTF("Sent domid %d and be_id %d\n", tr.domid, tr.busid);
    2.94 -	}
    2.95 -	
    2.96 -	if ( (ret <= 0)||(ret > MAX_TAP_DEV) ) {
    2.97 -		DPRINTF("Incorrect Dev ID [%d]\n",ret);
    2.98 -		return -1;
    2.99 -	}
   2.100 -	
   2.101 -	*minor = ret;
   2.102 -	*major = ioctl(ctlfd, BLKTAP_IOCTL_MAJOR, ret );
   2.103 -	if (*major < 0) {
   2.104 -		DPRINTF("Incorrect Major ID [%d]\n",*major);
   2.105 -		return -1;
   2.106 -	}
   2.107 -
   2.108 -	if (asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor) == -1)
   2.109 -		return -1;
   2.110 -	make_blktap_dev(devname,*major,*minor);	
   2.111 -	DPRINTF("Received device id %d and major %d\n",
   2.112 -		*minor, *major);
   2.113 -	return 0;
   2.114 -}
   2.115 -
   2.116  static int get_tapdisk_pid(blkif_t *blkif)
   2.117  {
   2.118  	int ret;
   2.119 @@ -651,7 +608,7 @@ static int blktapctrl_new_blkif(blkif_t 
   2.120  
   2.121  	DPRINTF("Received a poll for a new vbd\n");
   2.122  	if ( ((blk=blkif->info) != NULL) && (blk->params != NULL) ) {
   2.123 -		if (get_new_dev(&major, &minor, blkif)<0)
   2.124 +		if (blktap_interface_create(ctlfd, &major, &minor, blkif) < 0)
   2.125  			return -1;
   2.126  
   2.127  		if (test_path(blk->params, &ptr, &type, &exist) != 0) {
   2.128 @@ -843,21 +800,11 @@ int main(int argc, char *argv[])
   2.129  	register_new_devmap_hook(map_new_blktapctrl);
   2.130  	register_new_unmap_hook(unmap_blktapctrl);
   2.131  
   2.132 -	/* Attach to blktap0 */
   2.133 -	if (asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME) == -1)
   2.134 -                goto open_failed;
   2.135 -	if ((ret = xc_find_device_number("blktap0")) < 0) {
   2.136 -		DPRINTF("couldn't find device number for 'blktap0'\n");
   2.137 +	ctlfd = blktap_interface_open();
   2.138 +	if (ctlfd < 0) {
   2.139 +		DPRINTF("couldn't open blktap interface\n");
   2.140  		goto open_failed;
   2.141  	}
   2.142 -	blktap_major = major(ret);
   2.143 -	make_blktap_dev(devname,blktap_major,0);
   2.144 -	ctlfd = open(devname, O_RDWR);
   2.145 -	if (ctlfd == -1) {
   2.146 -		DPRINTF("blktap0 open failed\n");
   2.147 -		goto open_failed;
   2.148 -	}
   2.149 -
   2.150  
   2.151   retry:
   2.152  	/* Set up store connection and watch. */
     3.1 --- a/tools/blktap/drivers/blktapctrl.h	Mon Jul 14 10:00:47 2008 +0100
     3.2 +++ b/tools/blktap/drivers/blktapctrl.h	Mon Jul 14 10:03:09 2008 +0100
     3.3 @@ -30,26 +30,7 @@
     3.4   */
     3.5  
     3.6  
     3.7 -static inline unsigned long long tapdisk_get_size(blkif_t *blkif)
     3.8 -{
     3.9 -	image_t *img = (image_t *)blkif->prv;
    3.10 -	return img->size;
    3.11 -}
    3.12 -
    3.13 -static inline unsigned long tapdisk_get_secsize(blkif_t *blkif)
    3.14 -{
    3.15 -	image_t *img = (image_t *)blkif->prv;
    3.16 -	return img->secsize;
    3.17 -}
    3.18 +int blktap_interface_open(void);
    3.19  
    3.20 -static inline unsigned int tapdisk_get_info(blkif_t *blkif)
    3.21 -{
    3.22 -	image_t *img = (image_t *)blkif->prv;
    3.23 -	return img->info;
    3.24 -}
    3.25 +int blktap_interface_create(int ctlfd, int *major, int *minor, blkif_t *blkif);
    3.26  
    3.27 -struct blkif_ops tapdisk_ops = {
    3.28 -	.get_size = tapdisk_get_size,
    3.29 -	.get_secsize = tapdisk_get_secsize,
    3.30 -	.get_info = tapdisk_get_info,
    3.31 -};
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/blktap/drivers/blktapctrl_linux.c	Mon Jul 14 10:03:09 2008 +0100
     4.3 @@ -0,0 +1,109 @@
     4.4 +
     4.5 +#include <stdio.h>
     4.6 +#include <fcntl.h>
     4.7 +#include <sys/stat.h>
     4.8 +#include <sys/ioctl.h>
     4.9 +
    4.10 +#include "tapdisk.h"
    4.11 +#include "blktaplib.h"
    4.12 +#include "blktapctrl.h"
    4.13 +
    4.14 +static void make_blktap_dev(char *devname, int major, int minor)
    4.15 +{
    4.16 +	struct stat st;
    4.17 + 
    4.18 +	if (lstat(devname, &st) != 0) {
    4.19 +		/*Need to create device*/
    4.20 +		if (mkdir(BLKTAP_DEV_DIR, 0755) == 0)
    4.21 +			DPRINTF("Created %s directory\n",BLKTAP_DEV_DIR);
    4.22 +		if (mknod(devname, S_IFCHR|0600,
    4.23 +			makedev(major, minor)) == 0)
    4.24 +			DPRINTF("Created %s device\n",devname);
    4.25 +	} else {
    4.26 +		DPRINTF("%s device already exists\n",devname); 
    4.27 +		/* it already exists, but is it the same major number */
    4.28 +		if (((st.st_rdev>>8) & 0xff) != major) {
    4.29 +			DPRINTF("%s has old major %d\n",
    4.30 +				devname,
    4.31 +				(unsigned int)((st.st_rdev >> 8) & 0xff));
    4.32 +			/* only try again if we succed in deleting it */
    4.33 +			if (!unlink(devname))
    4.34 +				make_blktap_dev(devname, major, minor);
    4.35 +		}
    4.36 +	}
    4.37 +}
    4.38 +
    4.39 +int blktap_interface_create(int ctlfd, int *major, int *minor, blkif_t *blkif)
    4.40 +{       
    4.41 +        domid_translate_t tr;
    4.42 +        domid_translate_ext_t tr_ext;
    4.43 +        int ret; 
    4.44 +        char *devname;
    4.45 +
    4.46 +        if (blkif->be_id >= (1<<28)) {
    4.47 +                /* new-style backend-id, so use the extended structure */
    4.48 +                tr_ext.domid = blkif->domid;
    4.49 +                tr_ext.busid = blkif->be_id;
    4.50 +                ret = ioctl(ctlfd, BLKTAP_IOCTL_NEWINTF_EXT, &tr_ext);
    4.51 +                DPRINTF("Sent domid %d and be_id %d\n", tr_ext.domid,
    4.52 +                        tr_ext.busid);
    4.53 +        }
    4.54 +        else {
    4.55 +                /* old-style backend-id; use the old structure */
    4.56 +                tr.domid = blkif->domid;
    4.57 +                tr.busid = (unsigned short)blkif->be_id;
    4.58 +                ret = ioctl(ctlfd, BLKTAP_IOCTL_NEWINTF, tr);
    4.59 +                DPRINTF("Sent domid %d and be_id %d\n", tr.domid, tr.busid);
    4.60 +        }
    4.61 +
    4.62 +        if ( (ret <= 0)||(ret > MAX_TAP_DEV) ) {
    4.63 +                DPRINTF("Incorrect Dev ID [%d]\n",ret);
    4.64 +                return -1;
    4.65 +        }
    4.66 +
    4.67 +        *minor = ret;
    4.68 +        *major = ioctl(ctlfd, BLKTAP_IOCTL_MAJOR, ret );
    4.69 +        if (*major < 0) {
    4.70 +                DPRINTF("Incorrect Major ID [%d]\n",*major);
    4.71 +                return -1;
    4.72 +        }
    4.73 +
    4.74 +        if (asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor) == -1)
    4.75 +                return -1;
    4.76 +        make_blktap_dev(devname,*major,*minor);
    4.77 +        DPRINTF("Received device id %d and major %d\n",
    4.78 +                *minor, *major);
    4.79 +        return 0;
    4.80 +}
    4.81 +
    4.82 +
    4.83 +int blktap_interface_open(void)
    4.84 +{
    4.85 +	char *devname;
    4.86 +	int ret;
    4.87 +	int ctlfd;
    4.88 +
    4.89 +	/* Attach to blktap0 */
    4.90 +	if (asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME) == -1)
    4.91 +		goto open_failed;
    4.92 +
    4.93 +	ret = xc_find_device_number("blktap0");
    4.94 +	if (ret < 0) {
    4.95 +		DPRINTF("couldn't find device number for 'blktap0'\n");
    4.96 +		goto open_failed;
    4.97 +	}
    4.98 +
    4.99 +	blktap_major = major(ret);
   4.100 +	make_blktap_dev(devname,blktap_major, 0);
   4.101 +
   4.102 +	ctlfd = open(devname, O_RDWR);
   4.103 +	if (ctlfd == -1) {
   4.104 +		DPRINTF("blktap0 open failed\n");
   4.105 +		goto open_failed;
   4.106 +	}
   4.107 +
   4.108 +	return ctlfd;
   4.109 +
   4.110 +open_failed:
   4.111 +	return -1;
   4.112 +}