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