ia64/xen-unstable

changeset 11671:500043f8ccff

[BLKTAP] have blktap use a dynamic major
blktap currently uses a hardcoded major of 254 for the device. This is
not robust in anyway and needs to be dynamic.

Note: it is better not to have the daemon create the node, and have udev
create it instead. But since the daemon currently creates the node
anyway, it is still the way this is done. That change needs to be made
at another time.

Signed-off-by: Steven Rostedt <srostedt@redhat.com>
author Andrew Warfield <andy@xensource.com>
date Thu Sep 28 12:47:45 2006 -0700 (2006-09-28)
parents 3b7e11cbeb94
children d90be316e5f5
files linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c tools/blktap/drivers/blktapctrl.c tools/blktap/lib/blktaplib.h
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c	Thu Sep 28 12:12:59 2006 -0700
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c	Thu Sep 28 12:47:45 2006 -0700
     1.3 @@ -196,13 +196,11 @@ static struct grant_handle_pair
     1.4  
     1.5  static int blktap_read_ufe_ring(tap_blkif_t *info); /*local prototypes*/
     1.6  
     1.7 -#define BLKTAP_MINOR 0  /*/dev/xen/blktap resides at device number
     1.8 -			  major=254, minor numbers begin at 0            */ 
     1.9 -#define BLKTAP_DEV_MAJOR 254         /* TODO: Make major number dynamic  *
    1.10 -                                      * and create devices in the kernel *
    1.11 -				      */
    1.12 +#define BLKTAP_MINOR 0  /*/dev/xen/blktap has a dynamic major */
    1.13  #define BLKTAP_DEV_DIR  "/dev/xen"
    1.14  
    1.15 +static int blktap_major;
    1.16 +
    1.17  /* blktap IOCTLs: */
    1.18  #define BLKTAP_IOCTL_KICK_FE         1
    1.19  #define BLKTAP_IOCTL_KICK_BE         2 /* currently unused */
    1.20 @@ -631,7 +629,7 @@ static int blktap_ioctl(struct inode *in
    1.21  			return -1;
    1.22  	}
    1.23  	case BLKTAP_IOCTL_MAJOR:
    1.24 -		return BLKTAP_DEV_MAJOR;
    1.25 +		return blktap_major;
    1.26  
    1.27  	case BLKTAP_QUERY_ALLOC_REQS:
    1.28  	{
    1.29 @@ -1360,7 +1358,8 @@ static int __init blkif_init(void)
    1.30  	/*Create the blktap devices, but do not map memory or waitqueue*/
    1.31  	for(i = 0; i < MAX_TAP_DEV; i++) translate_domid[i].domid = 0xFFFF;
    1.32  
    1.33 -	ret = register_chrdev(BLKTAP_DEV_MAJOR,"blktap",&blktap_fops);
    1.34 +	/* Dynamically allocate a major for this device */
    1.35 +	ret = register_chrdev(0, "blktap", &blktap_fops);
    1.36  	blktap_dir = devfs_mk_dir(NULL, "xen", 0, NULL);
    1.37  
    1.38  	if ( (ret < 0)||(blktap_dir < 0) ) {
    1.39 @@ -1368,6 +1367,8 @@ static int __init blkif_init(void)
    1.40  		return -ENOMEM;
    1.41  	}	
    1.42  	
    1.43 +	blktap_major = ret;
    1.44 +
    1.45  	for(i = 0; i < MAX_TAP_DEV; i++ ) {
    1.46  		info = tapfds[i] = kzalloc(sizeof(tap_blkif_t),GFP_KERNEL);
    1.47  		if(tapfds[i] == NULL) return -ENOMEM;
    1.48 @@ -1375,7 +1376,7 @@ static int __init blkif_init(void)
    1.49  		info->pid = 0;
    1.50  		info->blkif = NULL;
    1.51  
    1.52 -		ret = devfs_mk_cdev(MKDEV(BLKTAP_DEV_MAJOR, i),
    1.53 +		ret = devfs_mk_cdev(MKDEV(blktap_major, i),
    1.54  			S_IFCHR|S_IRUGO|S_IWUSR, "xen/blktap%d", i);
    1.55  
    1.56  		if(ret != 0) return -ENOMEM;
     2.1 --- a/tools/blktap/drivers/blktapctrl.c	Thu Sep 28 12:12:59 2006 -0700
     2.2 +++ b/tools/blktap/drivers/blktapctrl.c	Thu Sep 28 12:47:45 2006 -0700
     2.3 @@ -67,6 +67,8 @@ int run = 1;
     2.4  int max_timeout = MAX_TIMEOUT;
     2.5  int ctlfd = 0;
     2.6  
     2.7 +int blktap_major;
     2.8 +
     2.9  static int open_ctrl_socket(char *devname);
    2.10  static int write_msg(int fd, int msgtype, void *ptr, void *ptr2);
    2.11  static int read_msg(int fd, int msgtype, void *ptr);
    2.12 @@ -108,7 +110,18 @@ static void make_blktap_dev(char *devnam
    2.13  		if (mknod(devname, S_IFCHR|0600,
    2.14                  	makedev(major, minor)) == 0)
    2.15  			DPRINTF("Created %s device\n",devname);
    2.16 -	} else DPRINTF("%s device already exists\n",devname);
    2.17 +	} else {
    2.18 +		DPRINTF("%s device already exists\n",devname);
    2.19 +		/* it already exists, but is it the same major number */
    2.20 +		if (((st.st_rdev>>8) & 0xff) != major) {
    2.21 +			DPRINTF("%s has old major %d\n",
    2.22 +				devname,
    2.23 +				(unsigned int)((st.st_rdev >> 8) & 0xff));
    2.24 +			/* only try again if we succed in deleting it */
    2.25 +			if (!unlink(devname))
    2.26 +				make_blktap_dev(devname, major, minor);
    2.27 +		}
    2.28 +	}
    2.29  }
    2.30  
    2.31  static int get_new_dev(int *major, int *minor, blkif_t *blkif)
    2.32 @@ -623,6 +636,30 @@ static void print_drivers(void)
    2.33  		DPRINTF("Found driver: [%s]\n",dtypes[i]->name);
    2.34  } 
    2.35  
    2.36 +static int find_blktap_major(void)
    2.37 +{
    2.38 +	FILE *fp;
    2.39 +	int major;
    2.40 +	char device[256];
    2.41 +
    2.42 +	if ((fp = fopen("/proc/devices", "r")) == NULL)
    2.43 +		return -1;
    2.44 +
    2.45 +	/* Skip title */
    2.46 +	fscanf(fp,"%*s %*s\n");
    2.47 +	while (fscanf(fp, "%d %255s\n", &major, device) == 2) {
    2.48 +		if (strncmp("blktap", device, 6) == 0)
    2.49 +			break;
    2.50 +	}
    2.51 +
    2.52 +	fclose(fp);
    2.53 +
    2.54 +	if (strncmp("blktap", device, 6) == 0)
    2.55 +		return major;
    2.56 +
    2.57 +	return -1;
    2.58 +}
    2.59 +
    2.60  int main(int argc, char *argv[])
    2.61  {
    2.62  	char *devname;
    2.63 @@ -646,7 +683,10 @@ int main(int argc, char *argv[])
    2.64  
    2.65  	/*Attach to blktap0 */	
    2.66  	asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
    2.67 -	make_blktap_dev(devname,254,0);
    2.68 +	blktap_major = find_blktap_major();
    2.69 +	if (blktap_major < 0)
    2.70 +		goto open_failed;
    2.71 +	make_blktap_dev(devname,blktap_major,0);
    2.72  	ctlfd = open(devname, O_RDWR);
    2.73  	if (ctlfd == -1) {
    2.74  		DPRINTF("blktap0 open failed\n");
     3.1 --- a/tools/blktap/lib/blktaplib.h	Thu Sep 28 12:12:59 2006 -0700
     3.2 +++ b/tools/blktap/lib/blktaplib.h	Thu Sep 28 12:47:45 2006 -0700
     3.3 @@ -80,9 +80,10 @@ static inline int BLKTAP_MODE_VALID(unsi
     3.4  #define MAX_PENDING_REQS 64
     3.5  #define BLKTAP_DEV_DIR   "/dev/xen"
     3.6  #define BLKTAP_DEV_NAME  "blktap"
     3.7 -#define BLKTAP_DEV_MAJOR 254
     3.8  #define BLKTAP_DEV_MINOR 0
     3.9  
    3.10 +extern int blktap_major;
    3.11 +
    3.12  #define BLKTAP_RING_PAGES       1 /* Front */
    3.13  #define BLKTAP_MMAP_REGION_SIZE (BLKTAP_RING_PAGES + MMAP_PAGES)
    3.14