public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Kay Sievers <kay.sievers@vrfy.org>, Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 06/20] mem_class: use minor as index instead of searching the array
Date: Tue, 15 Sep 2009 12:12:42 -0700	[thread overview]
Message-ID: <1253041976-1111-6-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20090915181247.GA32167@kroah.com>

From: Kay Sievers <kay.sievers@vrfy.org>

Declare the device list with the minor numbers as the index, which saves us from
searching for a matching list entry. Remove old devfs permissions declaration.

Signed-off-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
 drivers/char/mem.c |   82 +++++++++++++++++++++++++--------------------------
 1 files changed, 40 insertions(+), 42 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 645237b..bed3503 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -864,71 +864,67 @@ static const struct file_operations kmsg_fops = {
 	.write =	kmsg_write,
 };
 
-static const struct {
-	unsigned int		minor;
-	char			*name;
-	umode_t			mode;
-	const struct file_operations	*fops;
-	struct backing_dev_info	*dev_info;
-} devlist[] = { /* list of minor devices */
-	{1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops,
-		&directly_mappable_cdev_bdi},
+static const struct memdev {
+	const char *name;
+	const struct file_operations *fops;
+	struct backing_dev_info *dev_info;
+} devlist[] = {
+	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
 #ifdef CONFIG_DEVKMEM
-	{2, "kmem",    S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops,
-		&directly_mappable_cdev_bdi},
+	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
 #endif
-	{3, "null",    S_IRUGO | S_IWUGO,           &null_fops, NULL},
+	[ 3] = {"null", &null_fops, NULL },
 #ifdef CONFIG_DEVPORT
-	{4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops, NULL},
+	[ 4] = { "port", &port_fops, NULL },
 #endif
-	{5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops, &zero_bdi},
-	{7, "full",    S_IRUGO | S_IWUGO,           &full_fops, NULL},
-	{8, "random",  S_IRUGO | S_IWUSR,           &random_fops, NULL},
-	{9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops, NULL},
-	{11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops, NULL},
+	[ 5] = { "zero", &zero_fops, &zero_bdi },
+	[ 6] = { "full", &full_fops, NULL },
+	[ 7] = { "random", &random_fops, NULL },
+	[ 9] = { "urandom", &urandom_fops, NULL },
+	[11] = { "kmsg", &kmsg_fops, NULL },
 #ifdef CONFIG_CRASH_DUMP
-	{12,"oldmem",    S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops, NULL},
+	[12] = { "oldmem", &oldmem_fops, NULL },
 #endif
 };
 
 static int memory_open(struct inode *inode, struct file *filp)
 {
-	int ret = 0;
-	int i;
+	int minor;
+	const struct memdev *dev;
+	int ret = -ENXIO;
 
 	lock_kernel();
 
-	for (i = 0; i < ARRAY_SIZE(devlist); i++) {
-		if (devlist[i].minor == iminor(inode)) {
-			filp->f_op = devlist[i].fops;
-			if (devlist[i].dev_info) {
-				filp->f_mapping->backing_dev_info =
-					devlist[i].dev_info;
-			}
+	minor = iminor(inode);
+	if (minor >= ARRAY_SIZE(devlist))
+		goto out;
 
-			break;
-		}
-	}
+	dev = &devlist[minor];
+	if (!dev->fops)
+		goto out;
 
-	if (i == ARRAY_SIZE(devlist))
-		ret = -ENXIO;
-	else
-		if (filp->f_op && filp->f_op->open)
-			ret = filp->f_op->open(inode, filp);
+	filp->f_op = dev->fops;
+	if (dev->dev_info)
+		filp->f_mapping->backing_dev_info = dev->dev_info;
 
+	if (dev->fops->open)
+		ret = dev->fops->open(inode, filp);
+	else
+		ret = 0;
+out:
 	unlock_kernel();
 	return ret;
 }
 
 static const struct file_operations memory_fops = {
-	.open		= memory_open,	/* just a selector for the real open */
+	.open		= memory_open,
 };
 
 static struct class *mem_class;
 
 static int __init chr_dev_init(void)
 {
-	int i;
+	int minor;
 	int err;
 
 	err = bdi_init(&zero_bdi);
@@ -939,10 +935,12 @@ static int __init chr_dev_init(void)
 		printk("unable to get major %d for memory devs\n", MEM_MAJOR);
 
 	mem_class = class_create(THIS_MODULE, "mem");
-	for (i = 0; i < ARRAY_SIZE(devlist); i++)
-		device_create(mem_class, NULL,
-			      MKDEV(MEM_MAJOR, devlist[i].minor), NULL,
-			      devlist[i].name);
+	for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
+		if (!devlist[minor].name)
+			continue;
+		device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
+			      NULL, devlist[minor].name);
+	}
 
 	return 0;
 }
-- 
1.6.4.2


  parent reply	other threads:[~2009-09-15 19:15 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-15 18:12 [GIT PATCH] driver core patches for 2.6.31-git Greg KH
2009-09-15 19:12 ` [PATCH 01/20] Driver core: add new device to bus's list before probing Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 02/20] Driver core: move dev_get/set_drvdata to drivers/base/dd.c Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 03/20] Driver core: Add accessor for device platform data Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 04/20] UIO: remove 'default n' from Kconfig Greg Kroah-Hartman
2009-09-15 19:35   ` Hans J. Koch
2009-09-15 19:12 ` [PATCH 05/20] driver model: constify attribute groups Greg Kroah-Hartman
2009-09-15 19:12 ` Greg Kroah-Hartman [this message]
2009-09-15 19:42   ` [PATCH 06/20] mem_class: use minor as index instead of searching the array Daniel Walker
2009-09-15 19:46     ` Kay Sievers
2009-09-15 20:07       ` Daniel Walker
2009-09-15 20:24         ` Greg KH
2009-09-15 21:18           ` Daniel Walker
2009-09-15 21:20             ` Greg KH
2009-09-15 21:26               ` Daniel Walker
2009-09-15 21:32                 ` Greg KH
2009-09-15 22:06                   ` Daniel Walker
2009-09-19 15:17                 ` Tilman Schmidt
2009-09-19 16:01                   ` Daniel Walker
2009-09-15 19:12 ` [PATCH 07/20] mem_class: fix bug Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 08/20] driver-core: move dma-coherent.c from kernel to driver/base Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 09/20] uio: add generic driver for PCI 2.3 devices Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 10/20] Driver core: Add support for compatibility classes Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 11/20] driver core: platform_device_add_data(): use kmemdup() Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 12/20] hpilo: staging for interrupt handling Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 13/20] hpilo: add interrupt handler Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 14/20] hpilo: add poll f_op Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 15/20] debugfs: Fix mount directory of debugfs by default in events.txt Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 16/20] debugfs: Change debuhgfs directory of trace-events-sample.h Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 17/20] debugfs: Change debugfs directory of IWMC3200 Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 18/20] debugfs: Modified default dir of debugfs for debugging UHCI Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 19/20] debugfs: Modify default debugfs directory for debugging pktcdvd Greg Kroah-Hartman
2009-09-15 19:12 ` [PATCH 20/20] Driver Core: devtmpfs - kernel-maintained tmpfs-based /dev Greg Kroah-Hartman
2009-09-15 23:21   ` Christoph Hellwig
2009-09-15 23:43     ` Greg KH
2009-09-16 12:45 ` [GIT PATCH] driver core patches for 2.6.31-git Christoph Hellwig
2009-09-16 12:59   ` Greg KH
2009-09-16 13:15     ` Christoph Hellwig
2009-09-16 13:59       ` Kay Sievers

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=1253041976-1111-6-git-send-email-gregkh@suse.de \
    --to=gregkh@suse.de \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-kernel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox