Skip to content

Commit 68eef3b

Browse files
Joe KortyLinus Torvalds
authored andcommitted
[PATCH] Simplify proc/devices and fix early termination regression
Make baby-simple the code for /proc/devices. Based on the proven design for /proc/interrupts. This also fixes the early-termination regression 2.6.16 introduced, as demonstrated by: # dd if=/proc/devices bs=1 Character devices: 1 mem 27+0 records in 27+0 records out This should also work (but is untested) when /proc/devices >4096 bytes, which I believe is what the original 2.6.16 rewrite fixed. [akpm@osdl.org: cleanups, simplifications] Signed-off-by: Joe Korty <joe.korty@ccur.com> Cc: Neil Horman <nhorman@tuxdriver.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent a2c348f commit 68eef3b

4 files changed

Lines changed: 62 additions & 306 deletions

File tree

‎block/genhd.c‎

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <linux/buffer_head.h>
1818
#include <linux/mutex.h>
1919

20-
#define MAX_PROBE_HASH 255 /* random */
21-
2220
static struct subsystem block_subsys;
2321

2422
static DEFINE_MUTEX(block_subsys_lock);
@@ -31,108 +29,29 @@ static struct blk_major_name {
3129
struct blk_major_name *next;
3230
int major;
3331
char name[16];
34-
} *major_names[MAX_PROBE_HASH];
32+
} *major_names[BLKDEV_MAJOR_HASH_SIZE];
3533

3634
/* index in the above - for now: assume no multimajor ranges */
3735
static inline int major_to_index(int major)
3836
{
39-
return major % MAX_PROBE_HASH;
40-
}
41-
42-
struct blkdev_info {
43-
int index;
44-
struct blk_major_name *bd;
45-
};
46-
47-
/*
48-
* iterate over a list of blkdev_info structures. allows
49-
* the major_names array to be iterated over from outside this file
50-
* must be called with the block_subsys_lock held
51-
*/
52-
void *get_next_blkdev(void *dev)
53-
{
54-
struct blkdev_info *info;
55-
56-
if (dev == NULL) {
57-
info = kmalloc(sizeof(*info), GFP_KERNEL);
58-
if (!info)
59-
goto out;
60-
info->index=0;
61-
info->bd = major_names[info->index];
62-
if (info->bd)
63-
goto out;
64-
} else {
65-
info = dev;
66-
}
67-
68-
while (info->index < ARRAY_SIZE(major_names)) {
69-
if (info->bd)
70-
info->bd = info->bd->next;
71-
if (info->bd)
72-
goto out;
73-
/*
74-
* No devices on this chain, move to the next
75-
*/
76-
info->index++;
77-
info->bd = (info->index < ARRAY_SIZE(major_names)) ?
78-
major_names[info->index] : NULL;
79-
if (info->bd)
80-
goto out;
81-
}
82-
83-
out:
84-
return info;
85-
}
86-
87-
void *acquire_blkdev_list(void)
88-
{
89-
mutex_lock(&block_subsys_lock);
90-
return get_next_blkdev(NULL);
91-
}
92-
93-
void release_blkdev_list(void *dev)
94-
{
95-
mutex_unlock(&block_subsys_lock);
96-
kfree(dev);
37+
return major % BLKDEV_MAJOR_HASH_SIZE;
9738
}
9839

40+
#ifdef CONFIG_PROC_FS
9941

100-
/*
101-
* Count the number of records in the blkdev_list.
102-
* must be called with the block_subsys_lock held
103-
*/
104-
int count_blkdev_list(void)
42+
void blkdev_show(struct seq_file *f, off_t offset)
10543
{
106-
struct blk_major_name *n;
107-
int i, count;
44+
struct blk_major_name *dp;
10845

109-
count = 0;
110-
111-
for (i = 0; i < ARRAY_SIZE(major_names); i++) {
112-
for (n = major_names[i]; n; n = n->next)
113-
count++;
46+
if (offset < BLKDEV_MAJOR_HASH_SIZE) {
47+
mutex_lock(&block_subsys_lock);
48+
for (dp = major_names[offset]; dp; dp = dp->next)
49+
seq_printf(f, "%3d %s\n", dp->major, dp->name);
50+
mutex_unlock(&block_subsys_lock);
11451
}
115-
116-
return count;
117-
}
118-
119-
/*
120-
* extract the major and name values from a blkdev_info struct
121-
* passed in as a void to *dev. Must be called with
122-
* block_subsys_lock held
123-
*/
124-
int get_blkdev_info(void *dev, int *major, char **name)
125-
{
126-
struct blkdev_info *info = dev;
127-
128-
if (info->bd == NULL)
129-
return 1;
130-
131-
*major = info->bd->major;
132-
*name = info->bd->name;
133-
return 0;
13452
}
13553

54+
#endif /* CONFIG_PROC_FS */
13655

13756
int register_blkdev(unsigned int major, const char *name)
13857
{

‎fs/char_dev.c‎

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/module.h>
1616
#include <linux/smp_lock.h>
1717
#include <linux/devfs_fs_kernel.h>
18+
#include <linux/seq_file.h>
1819

1920
#include <linux/kobject.h>
2021
#include <linux/kobj_map.h>
@@ -27,8 +28,6 @@
2728

2829
static struct kobj_map *cdev_map;
2930

30-
#define MAX_PROBE_HASH 255 /* random */
31-
3231
static DEFINE_MUTEX(chrdevs_lock);
3332

3433
static struct char_device_struct {
@@ -39,93 +38,29 @@ static struct char_device_struct {
3938
char name[64];
4039
struct file_operations *fops;
4140
struct cdev *cdev; /* will die */
42-
} *chrdevs[MAX_PROBE_HASH];
41+
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
4342

4443
/* index in the above */
4544
static inline int major_to_index(int major)
4645
{
47-
return major % MAX_PROBE_HASH;
48-
}
49-
50-
struct chrdev_info {
51-
int index;
52-
struct char_device_struct *cd;
53-
};
54-
55-
void *get_next_chrdev(void *dev)
56-
{
57-
struct chrdev_info *info;
58-
59-
if (dev == NULL) {
60-
info = kmalloc(sizeof(*info), GFP_KERNEL);
61-
if (!info)
62-
goto out;
63-
info->index=0;
64-
info->cd = chrdevs[info->index];
65-
if (info->cd)
66-
goto out;
67-
} else {
68-
info = dev;
69-
}
70-
71-
while (info->index < ARRAY_SIZE(chrdevs)) {
72-
if (info->cd)
73-
info->cd = info->cd->next;
74-
if (info->cd)
75-
goto out;
76-
/*
77-
* No devices on this chain, move to the next
78-
*/
79-
info->index++;
80-
info->cd = (info->index < ARRAY_SIZE(chrdevs)) ?
81-
chrdevs[info->index] : NULL;
82-
if (info->cd)
83-
goto out;
84-
}
85-
86-
out:
87-
return info;
88-
}
89-
90-
void *acquire_chrdev_list(void)
91-
{
92-
mutex_lock(&chrdevs_lock);
93-
return get_next_chrdev(NULL);
94-
}
95-
96-
void release_chrdev_list(void *dev)
97-
{
98-
mutex_unlock(&chrdevs_lock);
99-
kfree(dev);
46+
return major % CHRDEV_MAJOR_HASH_SIZE;
10047
}
10148

49+
#ifdef CONFIG_PROC_FS
10250

103-
int count_chrdev_list(void)
51+
void chrdev_show(struct seq_file *f, off_t offset)
10452
{
10553
struct char_device_struct *cd;
106-
int i, count;
107-
108-
count = 0;
10954

110-
for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
111-
for (cd = chrdevs[i]; cd; cd = cd->next)
112-
count++;
55+
if (offset < CHRDEV_MAJOR_HASH_SIZE) {
56+
mutex_lock(&chrdevs_lock);
57+
for (cd = chrdevs[offset]; cd; cd = cd->next)
58+
seq_printf(f, "%3d %s\n", cd->major, cd->name);
59+
mutex_unlock(&chrdevs_lock);
11360
}
114-
115-
return count;
11661
}
11762

118-
int get_chrdev_info(void *dev, int *major, char **name)
119-
{
120-
struct chrdev_info *info = dev;
121-
122-
if (info->cd == NULL)
123-
return 1;
124-
125-
*major = info->cd->major;
126-
*name = info->cd->name;
127-
return 0;
128-
}
63+
#endif /* CONFIG_PROC_FS */
12964

13065
/*
13166
* Register a single major with a specified minor range.

0 commit comments

Comments
 (0)