Skip to content

Commit d6f47be

Browse files
Adriano dos Santos Fernandestorvalds
authored andcommitted
drivers/char/mem.c: memory_open() cleanup: lookup minor device number from devlist
memory_open() ignores devlist and does a switch for each item, duplicating code and conditional definitions. Clean it up by adding backing_dev_info to devlist and use it to lookup for the minor device. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Adriano dos Santos Fernandes <adrianosf@uol.com.br> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 31a985f commit d6f47be

1 file changed

Lines changed: 45 additions & 70 deletions

File tree

‎drivers/char/mem.c‎

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -863,59 +863,58 @@ static const struct file_operations kmsg_fops = {
863863
.write = kmsg_write,
864864
};
865865

866-
static int memory_open(struct inode * inode, struct file * filp)
867-
{
868-
int ret = 0;
869-
870-
lock_kernel();
871-
switch (iminor(inode)) {
872-
case 1:
873-
filp->f_op = &mem_fops;
874-
filp->f_mapping->backing_dev_info =
875-
&directly_mappable_cdev_bdi;
876-
break;
866+
static const struct {
867+
unsigned int minor;
868+
char *name;
869+
umode_t mode;
870+
const struct file_operations *fops;
871+
struct backing_dev_info *dev_info;
872+
} devlist[] = { /* list of minor devices */
873+
{1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops,
874+
&directly_mappable_cdev_bdi},
877875
#ifdef CONFIG_DEVKMEM
878-
case 2:
879-
filp->f_op = &kmem_fops;
880-
filp->f_mapping->backing_dev_info =
881-
&directly_mappable_cdev_bdi;
882-
break;
876+
{2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops,
877+
&directly_mappable_cdev_bdi},
883878
#endif
884-
case 3:
885-
filp->f_op = &null_fops;
886-
break;
879+
{3, "null", S_IRUGO | S_IWUGO, &null_fops, NULL},
887880
#ifdef CONFIG_DEVPORT
888-
case 4:
889-
filp->f_op = &port_fops;
890-
break;
881+
{4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops, NULL},
891882
#endif
892-
case 5:
893-
filp->f_mapping->backing_dev_info = &zero_bdi;
894-
filp->f_op = &zero_fops;
895-
break;
896-
case 7:
897-
filp->f_op = &full_fops;
898-
break;
899-
case 8:
900-
filp->f_op = &random_fops;
901-
break;
902-
case 9:
903-
filp->f_op = &urandom_fops;
904-
break;
905-
case 11:
906-
filp->f_op = &kmsg_fops;
907-
break;
883+
{5, "zero", S_IRUGO | S_IWUGO, &zero_fops, &zero_bdi},
884+
{7, "full", S_IRUGO | S_IWUGO, &full_fops, NULL},
885+
{8, "random", S_IRUGO | S_IWUSR, &random_fops, NULL},
886+
{9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops, NULL},
887+
{11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops, NULL},
908888
#ifdef CONFIG_CRASH_DUMP
909-
case 12:
910-
filp->f_op = &oldmem_fops;
911-
break;
889+
{12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops, NULL},
912890
#endif
913-
default:
914-
unlock_kernel();
915-
return -ENXIO;
891+
};
892+
893+
static int memory_open(struct inode *inode, struct file *filp)
894+
{
895+
int ret = 0;
896+
int i;
897+
898+
lock_kernel();
899+
900+
for (i = 0; i < ARRAY_SIZE(devlist); i++) {
901+
if (devlist[i].minor == iminor(inode)) {
902+
filp->f_op = devlist[i].fops;
903+
if (devlist[i].dev_info) {
904+
filp->f_mapping->backing_dev_info =
905+
devlist[i].dev_info;
906+
}
907+
908+
break;
909+
}
916910
}
917-
if (filp->f_op && filp->f_op->open)
918-
ret = filp->f_op->open(inode,filp);
911+
912+
if (i == ARRAY_SIZE(devlist))
913+
ret = -ENXIO;
914+
else
915+
if (filp->f_op && filp->f_op->open)
916+
ret = filp->f_op->open(inode, filp);
917+
919918
unlock_kernel();
920919
return ret;
921920
}
@@ -924,30 +923,6 @@ static const struct file_operations memory_fops = {
924923
.open = memory_open, /* just a selector for the real open */
925924
};
926925

927-
static const struct {
928-
unsigned int minor;
929-
char *name;
930-
umode_t mode;
931-
const struct file_operations *fops;
932-
} devlist[] = { /* list of minor devices */
933-
{1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
934-
#ifdef CONFIG_DEVKMEM
935-
{2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
936-
#endif
937-
{3, "null", S_IRUGO | S_IWUGO, &null_fops},
938-
#ifdef CONFIG_DEVPORT
939-
{4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
940-
#endif
941-
{5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
942-
{7, "full", S_IRUGO | S_IWUGO, &full_fops},
943-
{8, "random", S_IRUGO | S_IWUSR, &random_fops},
944-
{9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
945-
{11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
946-
#ifdef CONFIG_CRASH_DUMP
947-
{12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
948-
#endif
949-
};
950-
951926
static struct class *mem_class;
952927

953928
static int __init chr_dev_init(void)

0 commit comments

Comments
 (0)