[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-3.0.3-testing] [BLKTAP] have blktap use a dynamic major



# HG changeset patch
# User Andrew Warfield <andy@xxxxxxxxxxxxx>
# Date 1159472865 25200
# Node ID 500043f8ccff0338363da8b08e39827b522f7b6a
# Parent  3b7e11cbeb94066bd6c6c445ce019b8bee875adc
[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@xxxxxxxxxx>
---
 linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c |   17 ++++----
 tools/blktap/drivers/blktapctrl.c                |   44 +++++++++++++++++++++--
 tools/blktap/lib/blktaplib.h                     |    3 +
 3 files changed, 53 insertions(+), 11 deletions(-)

diff -r 3b7e11cbeb94 -r 500043f8ccff 
linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c
--- a/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c  Thu Sep 28 12:12:59 
2006 -0700
+++ b/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c  Thu Sep 28 12:47:45 
2006 -0700
@@ -196,12 +196,10 @@ static struct grant_handle_pair
 
 static int blktap_read_ufe_ring(tap_blkif_t *info); /*local prototypes*/
 
-#define BLKTAP_MINOR 0  /*/dev/xen/blktap resides at device number
-                         major=254, minor numbers begin at 0            */ 
-#define BLKTAP_DEV_MAJOR 254         /* TODO: Make major number dynamic  *
-                                      * and create devices in the kernel *
-                                     */
+#define BLKTAP_MINOR 0  /*/dev/xen/blktap has a dynamic major */
 #define BLKTAP_DEV_DIR  "/dev/xen"
+
+static int blktap_major;
 
 /* blktap IOCTLs: */
 #define BLKTAP_IOCTL_KICK_FE         1
@@ -631,7 +629,7 @@ static int blktap_ioctl(struct inode *in
                        return -1;
        }
        case BLKTAP_IOCTL_MAJOR:
-               return BLKTAP_DEV_MAJOR;
+               return blktap_major;
 
        case BLKTAP_QUERY_ALLOC_REQS:
        {
@@ -1360,7 +1358,8 @@ static int __init blkif_init(void)
        /*Create the blktap devices, but do not map memory or waitqueue*/
        for(i = 0; i < MAX_TAP_DEV; i++) translate_domid[i].domid = 0xFFFF;
 
-       ret = register_chrdev(BLKTAP_DEV_MAJOR,"blktap",&blktap_fops);
+       /* Dynamically allocate a major for this device */
+       ret = register_chrdev(0, "blktap", &blktap_fops);
        blktap_dir = devfs_mk_dir(NULL, "xen", 0, NULL);
 
        if ( (ret < 0)||(blktap_dir < 0) ) {
@@ -1368,6 +1367,8 @@ static int __init blkif_init(void)
                return -ENOMEM;
        }       
        
+       blktap_major = ret;
+
        for(i = 0; i < MAX_TAP_DEV; i++ ) {
                info = tapfds[i] = kzalloc(sizeof(tap_blkif_t),GFP_KERNEL);
                if(tapfds[i] == NULL) return -ENOMEM;
@@ -1375,7 +1376,7 @@ static int __init blkif_init(void)
                info->pid = 0;
                info->blkif = NULL;
 
-               ret = devfs_mk_cdev(MKDEV(BLKTAP_DEV_MAJOR, i),
+               ret = devfs_mk_cdev(MKDEV(blktap_major, i),
                        S_IFCHR|S_IRUGO|S_IWUSR, "xen/blktap%d", i);
 
                if(ret != 0) return -ENOMEM;
diff -r 3b7e11cbeb94 -r 500043f8ccff tools/blktap/drivers/blktapctrl.c
--- a/tools/blktap/drivers/blktapctrl.c Thu Sep 28 12:12:59 2006 -0700
+++ b/tools/blktap/drivers/blktapctrl.c Thu Sep 28 12:47:45 2006 -0700
@@ -67,6 +67,8 @@ int max_timeout = MAX_TIMEOUT;
 int max_timeout = MAX_TIMEOUT;
 int ctlfd = 0;
 
+int blktap_major;
+
 static int open_ctrl_socket(char *devname);
 static int write_msg(int fd, int msgtype, void *ptr, void *ptr2);
 static int read_msg(int fd, int msgtype, void *ptr);
@@ -108,7 +110,18 @@ static void make_blktap_dev(char *devnam
                if (mknod(devname, S_IFCHR|0600,
                        makedev(major, minor)) == 0)
                        DPRINTF("Created %s device\n",devname);
-       } else DPRINTF("%s device already exists\n",devname);
+       } else {
+               DPRINTF("%s device already exists\n",devname);
+               /* it already exists, but is it the same major number */
+               if (((st.st_rdev>>8) & 0xff) != major) {
+                       DPRINTF("%s has old major %d\n",
+                               devname,
+                               (unsigned int)((st.st_rdev >> 8) & 0xff));
+                       /* only try again if we succed in deleting it */
+                       if (!unlink(devname))
+                               make_blktap_dev(devname, major, minor);
+               }
+       }
 }
 
 static int get_new_dev(int *major, int *minor, blkif_t *blkif)
@@ -623,6 +636,30 @@ static void print_drivers(void)
                DPRINTF("Found driver: [%s]\n",dtypes[i]->name);
 } 
 
+static int find_blktap_major(void)
+{
+       FILE *fp;
+       int major;
+       char device[256];
+
+       if ((fp = fopen("/proc/devices", "r")) == NULL)
+               return -1;
+
+       /* Skip title */
+       fscanf(fp,"%*s %*s\n");
+       while (fscanf(fp, "%d %255s\n", &major, device) == 2) {
+               if (strncmp("blktap", device, 6) == 0)
+                       break;
+       }
+
+       fclose(fp);
+
+       if (strncmp("blktap", device, 6) == 0)
+               return major;
+
+       return -1;
+}
+
 int main(int argc, char *argv[])
 {
        char *devname;
@@ -646,7 +683,10 @@ int main(int argc, char *argv[])
 
        /*Attach to blktap0 */  
        asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
-       make_blktap_dev(devname,254,0);
+       blktap_major = find_blktap_major();
+       if (blktap_major < 0)
+               goto open_failed;
+       make_blktap_dev(devname,blktap_major,0);
        ctlfd = open(devname, O_RDWR);
        if (ctlfd == -1) {
                DPRINTF("blktap0 open failed\n");
diff -r 3b7e11cbeb94 -r 500043f8ccff tools/blktap/lib/blktaplib.h
--- a/tools/blktap/lib/blktaplib.h      Thu Sep 28 12:12:59 2006 -0700
+++ b/tools/blktap/lib/blktaplib.h      Thu Sep 28 12:47:45 2006 -0700
@@ -80,8 +80,9 @@ static inline int BLKTAP_MODE_VALID(unsi
 #define MAX_PENDING_REQS 64
 #define BLKTAP_DEV_DIR   "/dev/xen"
 #define BLKTAP_DEV_NAME  "blktap"
-#define BLKTAP_DEV_MAJOR 254
 #define BLKTAP_DEV_MINOR 0
+
+extern int blktap_major;
 
 #define BLKTAP_RING_PAGES       1 /* Front */
 #define BLKTAP_MMAP_REGION_SIZE (BLKTAP_RING_PAGES + MMAP_PAGES)

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.