提交 389e0cb9 编写于 作者: K Kay Sievers 提交者: Greg Kroah-Hartman

mem_class: use minor as index instead of searching the array

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: NKay Sievers <kay.sievers@vrfy.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@suse.de>
上级 a4dbd674
...@@ -864,71 +864,67 @@ static const struct file_operations kmsg_fops = { ...@@ -864,71 +864,67 @@ static const struct file_operations kmsg_fops = {
.write = kmsg_write, .write = kmsg_write,
}; };
static const struct { static const struct memdev {
unsigned int minor; const char *name;
char *name; const struct file_operations *fops;
umode_t mode; struct backing_dev_info *dev_info;
const struct file_operations *fops; } devlist[] = {
struct backing_dev_info *dev_info; [ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
} devlist[] = { /* list of minor devices */
{1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops,
&directly_mappable_cdev_bdi},
#ifdef CONFIG_DEVKMEM #ifdef CONFIG_DEVKMEM
{2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops, [ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
&directly_mappable_cdev_bdi},
#endif #endif
{3, "null", S_IRUGO | S_IWUGO, &null_fops, NULL}, [ 3] = {"null", &null_fops, NULL },
#ifdef CONFIG_DEVPORT #ifdef CONFIG_DEVPORT
{4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops, NULL}, [ 4] = { "port", &port_fops, NULL },
#endif #endif
{5, "zero", S_IRUGO | S_IWUGO, &zero_fops, &zero_bdi}, [ 5] = { "zero", &zero_fops, &zero_bdi },
{7, "full", S_IRUGO | S_IWUGO, &full_fops, NULL}, [ 6] = { "full", &full_fops, NULL },
{8, "random", S_IRUGO | S_IWUSR, &random_fops, NULL}, [ 7] = { "random", &random_fops, NULL },
{9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops, NULL}, [ 9] = { "urandom", &urandom_fops, NULL },
{11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops, NULL}, [11] = { "kmsg", &kmsg_fops, NULL },
#ifdef CONFIG_CRASH_DUMP #ifdef CONFIG_CRASH_DUMP
{12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops, NULL}, [12] = { "oldmem", &oldmem_fops, NULL },
#endif #endif
}; };
static int memory_open(struct inode *inode, struct file *filp) static int memory_open(struct inode *inode, struct file *filp)
{ {
int ret = 0; int minor;
int i; const struct memdev *dev;
int ret = -ENXIO;
lock_kernel(); lock_kernel();
for (i = 0; i < ARRAY_SIZE(devlist); i++) { minor = iminor(inode);
if (devlist[i].minor == iminor(inode)) { if (minor >= ARRAY_SIZE(devlist))
filp->f_op = devlist[i].fops; goto out;
if (devlist[i].dev_info) {
filp->f_mapping->backing_dev_info =
devlist[i].dev_info;
}
break; dev = &devlist[minor];
} if (!dev->fops)
} goto out;
if (i == ARRAY_SIZE(devlist)) filp->f_op = dev->fops;
ret = -ENXIO; if (dev->dev_info)
else filp->f_mapping->backing_dev_info = dev->dev_info;
if (filp->f_op && filp->f_op->open)
ret = filp->f_op->open(inode, filp);
if (dev->fops->open)
ret = dev->fops->open(inode, filp);
else
ret = 0;
out:
unlock_kernel(); unlock_kernel();
return ret; return ret;
} }
static const struct file_operations memory_fops = { 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 struct class *mem_class;
static int __init chr_dev_init(void) static int __init chr_dev_init(void)
{ {
int i; int minor;
int err; int err;
err = bdi_init(&zero_bdi); err = bdi_init(&zero_bdi);
...@@ -939,10 +935,12 @@ static int __init chr_dev_init(void) ...@@ -939,10 +935,12 @@ static int __init chr_dev_init(void)
printk("unable to get major %d for memory devs\n", MEM_MAJOR); printk("unable to get major %d for memory devs\n", MEM_MAJOR);
mem_class = class_create(THIS_MODULE, "mem"); mem_class = class_create(THIS_MODULE, "mem");
for (i = 0; i < ARRAY_SIZE(devlist); i++) for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
device_create(mem_class, NULL, if (!devlist[minor].name)
MKDEV(MEM_MAJOR, devlist[i].minor), NULL, continue;
devlist[i].name); device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
NULL, devlist[minor].name);
}
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册