All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <srostedt@redhat.com>
To: xen-devel@lists.xensource.com
Cc: andrew.warfield@cl.cam.ac.uk
Subject: [PATCH 2/2] have blktap use a dynamic major
Date: Wed, 27 Sep 2006 12:21:31 -0400	[thread overview]
Message-ID: <451AA50B.5020507@redhat.com> (raw)
In-Reply-To: <451AA3D8.4030909@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 416 bytes --]

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.

-- Steve

Signed-off-by: Steven Rostedt <srostedt@redhat.com>


[-- Attachment #2: xen-linux-blktap-dynamic-major.patch --]
[-- Type: text/x-patch, Size: 4753 bytes --]

diff -r 0b662eb8ad52 linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c
--- a/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c	Wed Sep 27 10:40:59 2006 -0400
+++ b/linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c	Wed Sep 27 10:43:58 2006 -0400
@@ -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:
 	{
@@ -1395,7 +1393,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) ) {
@@ -1403,6 +1402,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;
@@ -1410,7 +1411,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 0b662eb8ad52 tools/blktap/drivers/blktapctrl.c
--- a/tools/blktap/drivers/blktapctrl.c	Wed Sep 27 10:40:59 2006 -0400
+++ b/tools/blktap/drivers/blktapctrl.c	Wed Sep 27 10:43:58 2006 -0400
@@ -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 0b662eb8ad52 tools/blktap/lib/blktaplib.h
--- a/tools/blktap/lib/blktaplib.h	Wed Sep 27 10:40:59 2006 -0400
+++ b/tools/blktap/lib/blktaplib.h	Wed Sep 27 10:43:58 2006 -0400
@@ -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)

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

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

  parent reply	other threads:[~2006-09-27 16:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-27 16:16 [PATCH 0/2] clean up blktap and change to use a dynamic major Steven Rostedt
2006-09-27 16:19 ` [PATCH 1/2] clean up blktap and remove private structure Steven Rostedt
2006-09-28 19:05   ` Andrew Warfield
2006-09-27 16:21 ` Steven Rostedt [this message]
2006-09-28 19:48   ` [PATCH 2/2] have blktap use a dynamic major Andrew Warfield

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=451AA50B.5020507@redhat.com \
    --to=srostedt@redhat.com \
    --cc=andrew.warfield@cl.cam.ac.uk \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.