8baf67
From: Peter Osterlund <petero2@telia.com>
8baf67
Subject: Re: [RFC][PATCH] Control pktcdvd with an auxiliary character device
8baf67
To: linux-kernel@vger.kernel.org
8baf67
Cc: Christoph Hellwig <hch@infradead.org>, Arnd Bergmann <arnd@arndb.de>,
8baf67
        Jens Axboe <axboe@suse.de>, Andrew Morton <akpm@osdl.org>
8baf67
Date: Wed Jul 14 02:17:27 2004 +0200
8baf67
8baf67
Peter Osterlund <petero2@telia.com> writes:
8baf67
8baf67
> Peter Osterlund <petero2@telia.com> writes:
8baf67
> 
8baf67
> > Christoph Hellwig <hch@infradead.org> writes:
8baf67
> > 
8baf67
> > > On Sun, Jul 11, 2004 at 01:20:45AM +0200, Arnd Bergmann wrote:
8baf67
> > > > These are actually incorrect definitions since the ioctl argument is
8baf67
> > > > not a pointer to unsigned int but instead just an int. However, that's
8baf67
> > > > too late to fix without breaking the existing tools.
8baf67
> > > 
8baf67
> > > The tools need to change anyway to get away from the broken behaviour to
8baf67
> > > issue in ioctl on the actual block device to bind it..
8baf67
> > 
8baf67
> > OK, I'll create a patch that gets rid of the ioctl interface and uses
8baf67
> > an auxiliary character device instead to control device bindings.
8baf67
> 
8baf67
> Here is a patch for 2.6.7-mm7 that does that. The driver creates a
8baf67
> misc character device and bind/unbind of the block devices are
8baf67
> controlled by ioctl commands on the char device.
8baf67
> 
8baf67
> This patch needs corresponding changes in the pktsetup user space
8baf67
> program. I'll post a patch for pktsetup as a separate message.
8baf67
8baf67
And here is a patch for udftools-1.0.0b3 that updates the pktsetup
8baf67
program to make it able to use the character device for block device
8baf67
setup/teardown.
8baf67
8baf67
---
8baf67
8baf67
 udftools-1.0.0b3-petero/pktsetup/pktsetup.c |  245 +++++++++++++++++++++++++++-
8baf67
 1 files changed, 238 insertions(+), 7 deletions(-)
8baf67
8baf67
diff -puN pktsetup/pktsetup.c~pktsetup-char-dev pktsetup/pktsetup.c
8baf67
--- udftools-1.0.0b3/pktsetup/pktsetup.c~pktsetup-char-dev	2004-07-12 19:57:51.000000000 +0200
8baf67
+++ udftools-1.0.0b3-petero/pktsetup/pktsetup.c	2004-07-14 00:34:02.471317888 +0200
8baf67
@@ -1,5 +1,6 @@
8baf67
 /*
8baf67
  * Copyright (c) 1999,2000	Jens Axboe <axboe@suse.de>
8baf67
+ * Copyright (c) 2004		Peter Osterlund <petero2@telia.com>
8baf67
  *
8baf67
  *   This program is free software; you can redistribute it and/or modify
8baf67
  *   it under the terms of the GNU General Public License as published by
8baf67
@@ -19,6 +20,7 @@
8baf67
 #include <stdio.h>
8baf67
 #include <fcntl.h>
8baf67
 #include <sys ioctl.h="">
8baf67
+#include <sys stat.h="">
8baf67
 #include <unistd.h>
8baf67
 #include <getopt.h>
8baf67
 #include <bits types.h="">
8baf67
@@ -33,8 +35,33 @@
8baf67
 #define PACKET_SETUP_DEV	_IOW('X', 1, unsigned int)
8baf67
 #define PACKET_TEARDOWN_DEV	_IOW('X', 2, unsigned int)
8baf67
 #endif
8baf67
+#ifndef PACKET_CTRL_CMD
8baf67
+#define PACKET_CTRL_CMD		_IOWR('X', 1, struct pkt_ctrl_command)
8baf67
+#endif
8baf67
+
8baf67
+#define MAJOR(dev)      ((dev & 0xfff00) >> 8)
8baf67
+#define MINOR(dev)      ((dev & 0xff) | ((dev >> 12) & 0xfff00))
8baf67
+#define MKDEV(ma,mi)    ((mi & 0xff) | (ma << 8) | ((mi & ~0xff) << 12))
8baf67
+
8baf67
+#define MISC_MAJOR		10
8baf67
+#define CTL_DIR "/dev/pktcdvd"
8baf67
+#define CTL_DEV "control"
8baf67
+
8baf67
+#define PKT_CTRL_CMD_SETUP	0
8baf67
+#define PKT_CTRL_CMD_TEARDOWN	1
8baf67
+#define PKT_CTRL_CMD_STATUS	2
8baf67
+
8baf67
+struct pkt_ctrl_command {
8baf67
+	__u32 command;				/* in: Setup, teardown, status */
8baf67
+	__u32 dev_index;			/* in/out: Device index */
8baf67
+	__u32 dev;				/* in/out: Device nr for cdrw device */
8baf67
+	__u32 pkt_dev;				/* out: Device nr for packet device */
8baf67
+	__u32 num_devices;			/* out: Largest device index + 1 */
8baf67
+	__u32 padding;
8baf67
+};
8baf67
+
8baf67
 
8baf67
-int init_cdrom(int fd)
8baf67
+static int init_cdrom(int fd)
8baf67
 {
8baf67
 	if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) < 0) {
8baf67
 		perror("drive not ready\n");
8baf67
@@ -54,7 +81,7 @@ int init_cdrom(int fd)
8baf67
 	return 0;
8baf67
 }
8baf67
 
8baf67
-void setup_dev(char *pkt_device, char *device, int rem)
8baf67
+static void setup_dev(char *pkt_device, char *device, int rem)
8baf67
 {
8baf67
 	int pkt_fd, dev_fd, cmd;
8baf67
 
8baf67
@@ -88,29 +115,233 @@ void setup_dev(char *pkt_device, char *d
8baf67
 	close(pkt_fd);
8baf67
 }
8baf67
 
8baf67
-int usage(void)
8baf67
+static int usage(void)
8baf67
 {
8baf67
-	printf("pktsetup /dev/pktcdvd0 /dev/cdrom\tsetup device\n");
8baf67
-	printf("pktsetup -d /dev/pktcdvd0\t\ttear down device\n");
8baf67
+	printf("For pktcdvd < 0.2.0:\n");
8baf67
+	printf("  pktsetup /dev/pktcdvd0 /dev/cdrom  setup device\n");
8baf67
+	printf("  pktsetup -d /dev/pktcdvd0          tear down device\n");
8baf67
+	printf("For pktcdvd >= 0.2.0:\n");
8baf67
+	printf("  pktsetup dev_name /dev/cdrom       setup device\n");
8baf67
+	printf("  pktsetup -d dev_name               tear down device\n");
8baf67
+	printf("  pktsetup -d major:minor            tear down device\n");
8baf67
+	printf("  pktsetup -s                        show device mappings\n");
8baf67
 	return 1;
8baf67
 }
8baf67
 
8baf67
+/*
8baf67
+ * Find the minor device number for the pktcdvd control device.
8baf67
+ */
8baf67
+static int get_misc_minor(void)
8baf67
+{
8baf67
+	int minor;
8baf67
+	char name[64];
8baf67
+	FILE *f;
8baf67
+
8baf67
+	if ((f = fopen("/proc/misc", "r")) == NULL)
8baf67
+		return -1;
8baf67
+	while (fscanf(f, " %d %64s", &minor, name) == 2) {
8baf67
+		if (strcmp(name, "pktcdvd") == 0) {
8baf67
+			fclose(f);
8baf67
+			return minor;
8baf67
+		}
8baf67
+	}
8baf67
+	fclose(f);
8baf67
+	return -1;
8baf67
+}
8baf67
+
8baf67
+static const char *pkt_dev_name(const char *dev)
8baf67
+{
8baf67
+	static char buf[128];
8baf67
+	snprintf(buf, sizeof(buf), "%s/%s", CTL_DIR, dev);
8baf67
+	return buf;
8baf67
+}
8baf67
+
8baf67
+static void create_ctl_dev(void)
8baf67
+{
8baf67
+	int misc_minor;
8baf67
+	struct stat stat_buf;
8baf67
+	int dev;
8baf67
+
8baf67
+	if ((misc_minor = get_misc_minor()) < 0) {
8baf67
+		system("/sbin/modprobe pktcdvd");
8baf67
+		misc_minor = get_misc_minor();
8baf67
+	}
8baf67
+	if (misc_minor < 0) {
8baf67
+		fprintf(stderr, "Can't find pktcdvd character device\n");
8baf67
+		return;
8baf67
+	}
8baf67
+	dev = MKDEV(MISC_MAJOR, misc_minor);
8baf67
+
8baf67
+	if ((stat(pkt_dev_name(CTL_DEV), &stat_buf) >= 0) &&
8baf67
+	    S_ISCHR(stat_buf.st_mode) && (stat_buf.st_rdev == dev))
8baf67
+		return;			    /* Already set up */
8baf67
+
8baf67
+	mkdir(CTL_DIR, 0755);
8baf67
+	unlink(pkt_dev_name(CTL_DEV));
8baf67
+	mknod(pkt_dev_name(CTL_DEV), S_IFCHR | 0644, dev);
8baf67
+}
8baf67
+
8baf67
+static int remove_stale_dev_node(int ctl_fd, char *devname)
8baf67
+{
8baf67
+	struct stat stat_buf;
8baf67
+	int i, dev;
8baf67
+	struct pkt_ctrl_command c;
8baf67
+
8baf67
+	if (stat(pkt_dev_name(devname), &stat_buf) < 0)
8baf67
+		return 0;
8baf67
+	if (!S_ISBLK(stat_buf.st_mode))
8baf67
+		return 1;
8baf67
+	dev = stat_buf.st_rdev;
8baf67
+	memset(&c, 0, sizeof(struct pkt_ctrl_command));
8baf67
+	for (i = 0; ; i++) {
8baf67
+		c.command = PKT_CTRL_CMD_STATUS;
8baf67
+		c.dev_index = i;
8baf67
+		if (ioctl(ctl_fd, PACKET_CTRL_CMD, &c) < 0) {
8baf67
+			perror("ioctl");
8baf67
+			return 1;
8baf67
+		}
8baf67
+		if (i >= c.num_devices)
8baf67
+			break;
8baf67
+		if (c.pkt_dev == dev)
8baf67
+			return 1;	    /* busy */
8baf67
+	}
8baf67
+	unlink(pkt_dev_name(devname));
8baf67
+	return 0;
8baf67
+}
8baf67
+
8baf67
+static void setup_dev_chardev(char *pkt_device, char *device, int rem)
8baf67
+{
8baf67
+	struct pkt_ctrl_command c;
8baf67
+	struct stat stat_buf;
8baf67
+	int ctl_fd, dev_fd;
8baf67
+
8baf67
+	memset(&c, 0, sizeof(struct pkt_ctrl_command));
8baf67
+
8baf67
+	create_ctl_dev();
8baf67
+	if ((ctl_fd = open(pkt_dev_name(CTL_DEV), O_RDONLY)) < 0) {
8baf67
+		perror("ctl open");
8baf67
+		return;
8baf67
+	}
8baf67
+
8baf67
+	if (!rem) {
8baf67
+		if ((dev_fd = open(device, O_RDONLY | O_NONBLOCK)) == -1) {
8baf67
+			perror("open cd-rom");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		if (init_cdrom(dev_fd)) {
8baf67
+			close(dev_fd);
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		close(dev_fd);
8baf67
+
8baf67
+		if (stat(device, &stat_buf) < 0) {
8baf67
+			perror("stat cd-rom");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		if (!S_ISBLK(stat_buf.st_mode)) {
8baf67
+			fprintf(stderr, "Not a block device\n");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		c.command = PKT_CTRL_CMD_SETUP;
8baf67
+		c.dev = stat_buf.st_rdev;
8baf67
+
8baf67
+		if (remove_stale_dev_node(ctl_fd, pkt_device) != 0) {
8baf67
+			fprintf(stderr, "Device node '%s' already in use\n", pkt_device);
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		if (ioctl(ctl_fd, PACKET_CTRL_CMD, &c) < 0) {
8baf67
+			perror("ioctl");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		mknod(pkt_dev_name(pkt_device), S_IFBLK | 0640, c.pkt_dev);
8baf67
+	} else {
8baf67
+		int major, minor, remove_node;
8baf67
+
8baf67
+		if ((stat(pkt_dev_name(pkt_device), &stat_buf) >= 0) &&
8baf67
+		    S_ISBLK(stat_buf.st_mode)) {
8baf67
+			major = MAJOR(stat_buf.st_rdev);
8baf67
+			minor = MINOR(stat_buf.st_rdev);
8baf67
+			remove_node = 1;
8baf67
+		} else if (sscanf(pkt_device, "%d:%d", &major, &minor) == 2) {
8baf67
+			remove_node = 0;
8baf67
+		} else {
8baf67
+			fprintf(stderr, "Can't find major/minor numbers\n");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+
8baf67
+		c.command = PKT_CTRL_CMD_TEARDOWN;
8baf67
+		c.pkt_dev = MKDEV(major, minor);
8baf67
+		if (ioctl(ctl_fd, PACKET_CTRL_CMD, &c) < 0) {
8baf67
+			perror("ioctl");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		if (remove_node)
8baf67
+			unlink(pkt_dev_name(pkt_device));
8baf67
+	}
8baf67
+
8baf67
+out_close:
8baf67
+	close(ctl_fd);
8baf67
+}
8baf67
+
8baf67
+static void show_mappings(void)
8baf67
+{
8baf67
+	struct pkt_ctrl_command c;
8baf67
+	int ctl_fd, i;
8baf67
+
8baf67
+	memset(&c, 0, sizeof(struct pkt_ctrl_command));
8baf67
+
8baf67
+	create_ctl_dev();
8baf67
+	if ((ctl_fd = open(pkt_dev_name(CTL_DEV), O_RDONLY)) < 0) {
8baf67
+		perror("ctl open");
8baf67
+		return;
8baf67
+	}
8baf67
+
8baf67
+	for (i = 0; ; i++) {
8baf67
+		c.command = PKT_CTRL_CMD_STATUS;
8baf67
+		c.dev_index = i;
8baf67
+		if (ioctl(ctl_fd, PACKET_CTRL_CMD, &c) < 0) {
8baf67
+			perror("ioctl");
8baf67
+			goto out_close;
8baf67
+		}
8baf67
+		if (i >= c.num_devices)
8baf67
+			break;
8baf67
+		if (c.dev) {
8baf67
+			printf("%2d : %d:%d -> %d:%d\n",
8baf67
+			       i, MAJOR(c.pkt_dev), MINOR(c.pkt_dev),
8baf67
+			       MAJOR(c.dev), MINOR(c.dev));
8baf67
+		}
8baf67
+	}
8baf67
+
8baf67
+out_close:
8baf67
+	close(ctl_fd);
8baf67
+}
8baf67
+
8baf67
 int main(int argc, char **argv)
8baf67
 {
8baf67
 	int rem = 0, c;
8baf67
+	char *pkt_device;
8baf67
+	char *device;
8baf67
 
8baf67
 	if (argc == 1)
8baf67
 		return usage();
8baf67
 
8baf67
-	while ((c = getopt(argc, argv, "d")) != EOF) {
8baf67
+	while ((c = getopt(argc, argv, "ds?")) != EOF) {
8baf67
 		switch (c) {
8baf67
 			case 'd':
8baf67
 				rem = 1;
8baf67
 				break;
8baf67
+			case 's':
8baf67
+				show_mappings();
8baf67
+				return 0;
8baf67
 			default:
8baf67
 				return usage();
8baf67
 		}
8baf67
 	}
8baf67
-	setup_dev(argv[optind], argv[optind + 1], rem);
8baf67
+	pkt_device = argv[optind];
8baf67
+	device = argv[optind + 1];
8baf67
+	if (strchr(pkt_device, '/'))
8baf67
+		setup_dev(pkt_device, device, rem);
8baf67
+	else
8baf67
+		setup_dev_chardev(pkt_device, device, rem);
8baf67
 	return 0;
8baf67
 }
8baf67
_
8baf67
8baf67
-- 
8baf67
Peter Osterlund - petero2@telia.com
8baf67
http://w1.894.telia.com/~u89404340