[PATCH] s390: deadlock in dasd_devmap
[safe/jmp/linux-2.6] / drivers / s390 / block / dasd_proc.c
1 /*
2  * File...........: linux/drivers/s390/block/dasd_proc.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2002
9  *
10  * /proc interface for the dasd driver.
11  *
12  * $Revision: 1.33 $
13  */
14
15 #include <linux/config.h>
16 #include <linux/ctype.h>
17 #include <linux/seq_file.h>
18 #include <linux/vmalloc.h>
19 #include <linux/proc_fs.h>
20
21 #include <asm/debug.h>
22 #include <asm/uaccess.h>
23
24 /* This is ugly... */
25 #define PRINTK_HEADER "dasd_proc:"
26
27 #include "dasd_int.h"
28
29 static struct proc_dir_entry *dasd_proc_root_entry = NULL;
30 static struct proc_dir_entry *dasd_devices_entry = NULL;
31 static struct proc_dir_entry *dasd_statistics_entry = NULL;
32
33 static inline char *
34 dasd_get_user_string(const char __user *user_buf, size_t user_len)
35 {
36         char *buffer;
37
38         buffer = kmalloc(user_len + 1, GFP_KERNEL);
39         if (buffer == NULL)
40                 return ERR_PTR(-ENOMEM);
41         if (copy_from_user(buffer, user_buf, user_len) != 0) {
42                 kfree(buffer);
43                 return ERR_PTR(-EFAULT);
44         }
45         /* got the string, now strip linefeed. */
46         if (buffer[user_len - 1] == '\n')
47                 buffer[user_len - 1] = 0;
48         else
49                 buffer[user_len] = 0;
50         return buffer;
51 }
52
53 static int
54 dasd_devices_show(struct seq_file *m, void *v)
55 {
56         struct dasd_device *device;
57         char *substr;
58
59         device = dasd_device_from_devindex((unsigned long) v - 1);
60         if (IS_ERR(device))
61                 return 0;
62         /* Print device number. */
63         seq_printf(m, "%s", device->cdev->dev.bus_id);
64         /* Print discipline string. */
65         if (device != NULL && device->discipline != NULL)
66                 seq_printf(m, "(%s)", device->discipline->name);
67         else
68                 seq_printf(m, "(none)");
69         /* Print kdev. */
70         if (device->gdp)
71                 seq_printf(m, " at (%3d:%6d)",
72                            device->gdp->major, device->gdp->first_minor);
73         else
74                 seq_printf(m, "  at (???:??????)");
75         /* Print device name. */
76         if (device->gdp)
77                 seq_printf(m, " is %-8s", device->gdp->disk_name);
78         else
79                 seq_printf(m, " is ????????");
80         /* Print devices features. */
81         substr = (device->features & DASD_FEATURE_READONLY) ? "(ro)" : " ";
82         seq_printf(m, "%4s: ", substr);
83         /* Print device status information. */
84         switch ((device != NULL) ? device->state : -1) {
85         case -1:
86                 seq_printf(m, "unknown");
87                 break;
88         case DASD_STATE_NEW:
89                 seq_printf(m, "new");
90                 break;
91         case DASD_STATE_KNOWN:
92                 seq_printf(m, "detected");
93                 break;
94         case DASD_STATE_BASIC:
95                 seq_printf(m, "basic");
96                 break;
97         case DASD_STATE_READY:
98         case DASD_STATE_ONLINE:
99                 seq_printf(m, "active ");
100                 if (dasd_check_blocksize(device->bp_block))
101                         seq_printf(m, "n/f       ");
102                 else
103                         seq_printf(m,
104                                    "at blocksize: %d, %ld blocks, %ld MB",
105                                    device->bp_block, device->blocks,
106                                    ((device->bp_block >> 9) *
107                                     device->blocks) >> 11);
108                 break;
109         default:
110                 seq_printf(m, "no stat");
111                 break;
112         }
113         dasd_put_device(device);
114         if (dasd_probeonly)
115                 seq_printf(m, "(probeonly)");
116         seq_printf(m, "\n");
117         return 0;
118 }
119
120 static void *dasd_devices_start(struct seq_file *m, loff_t *pos)
121 {
122         if (*pos >= dasd_max_devindex)
123                 return NULL;
124         return (void *)((unsigned long) *pos + 1);
125 }
126
127 static void *dasd_devices_next(struct seq_file *m, void *v, loff_t *pos)
128 {
129         ++*pos;
130         return dasd_devices_start(m, pos);
131 }
132
133 static void dasd_devices_stop(struct seq_file *m, void *v)
134 {
135 }
136
137 static struct seq_operations dasd_devices_seq_ops = {
138         .start          = dasd_devices_start,
139         .next           = dasd_devices_next,
140         .stop           = dasd_devices_stop,
141         .show           = dasd_devices_show,
142 };
143
144 static int dasd_devices_open(struct inode *inode, struct file *file)
145 {
146         return seq_open(file, &dasd_devices_seq_ops);
147 }
148
149 static struct file_operations dasd_devices_file_ops = {
150         .open           = dasd_devices_open,
151         .read           = seq_read,
152         .llseek         = seq_lseek,
153         .release        = seq_release,
154 };
155
156 static inline int
157 dasd_calc_metrics(char *page, char **start, off_t off,
158                   int count, int *eof, int len)
159 {
160         len = (len > off) ? len - off : 0;
161         if (len > count)
162                 len = count;
163         if (len < count)
164                 *eof = 1;
165         *start = page + off;
166         return len;
167 }
168
169 static inline char *
170 dasd_statistics_array(char *str, int *array, int shift)
171 {
172         int i;
173
174         for (i = 0; i < 32; i++) {
175                 str += sprintf(str, "%7d ", array[i] >> shift);
176                 if (i == 15)
177                         str += sprintf(str, "\n");
178         }
179         str += sprintf(str,"\n");
180         return str;
181 }
182
183 static int
184 dasd_statistics_read(char *page, char **start, off_t off,
185                      int count, int *eof, void *data)
186 {
187         unsigned long len;
188 #ifdef CONFIG_DASD_PROFILE
189         struct dasd_profile_info_t *prof;
190         char *str;
191         int shift;
192
193         /* check for active profiling */
194         if (dasd_profile_level == DASD_PROFILE_OFF) {
195                 len = sprintf(page, "Statistics are off - they might be "
196                                     "switched on using 'echo set on > "
197                                     "/proc/dasd/statistics'\n");
198                 return dasd_calc_metrics(page, start, off, count, eof, len);
199         }
200
201         prof = &dasd_global_profile;
202         /* prevent couter 'overflow' on output */
203         for (shift = 0; (prof->dasd_io_reqs >> shift) > 9999999; shift++);
204
205         str = page;
206         str += sprintf(str, "%d dasd I/O requests\n", prof->dasd_io_reqs);
207         str += sprintf(str, "with %d sectors(512B each)\n",
208                        prof->dasd_io_sects);
209         str += sprintf(str,
210                        "   __<4    ___8    __16    __32    __64    _128 "
211                        "   _256    _512    __1k    __2k    __4k    __8k "
212                        "   _16k    _32k    _64k    128k\n");
213         str += sprintf(str,
214                        "   _256    _512    __1M    __2M    __4M    __8M "
215                        "   _16M    _32M    _64M    128M    256M    512M "
216                        "   __1G    __2G    __4G " "   _>4G\n");
217
218         str += sprintf(str, "Histogram of sizes (512B secs)\n");
219         str = dasd_statistics_array(str, prof->dasd_io_secs, shift);
220         str += sprintf(str, "Histogram of I/O times (microseconds)\n");
221         str = dasd_statistics_array(str, prof->dasd_io_times, shift);
222         str += sprintf(str, "Histogram of I/O times per sector\n");
223         str = dasd_statistics_array(str, prof->dasd_io_timps, shift);
224         str += sprintf(str, "Histogram of I/O time till ssch\n");
225         str = dasd_statistics_array(str, prof->dasd_io_time1, shift);
226         str += sprintf(str, "Histogram of I/O time between ssch and irq\n");
227         str = dasd_statistics_array(str, prof->dasd_io_time2, shift);
228         str += sprintf(str, "Histogram of I/O time between ssch "
229                             "and irq per sector\n");
230         str = dasd_statistics_array(str, prof->dasd_io_time2ps, shift);
231         str += sprintf(str, "Histogram of I/O time between irq and end\n");
232         str = dasd_statistics_array(str, prof->dasd_io_time3, shift);
233         str += sprintf(str, "# of req in chanq at enqueuing (1..32) \n");
234         str = dasd_statistics_array(str, prof->dasd_io_nr_req, shift);
235         len = str - page;
236 #else
237         len = sprintf(page, "Statistics are not activated in this kernel\n");
238 #endif
239         return dasd_calc_metrics(page, start, off, count, eof, len);
240 }
241
242 static int
243 dasd_statistics_write(struct file *file, const char __user *user_buf,
244                       unsigned long user_len, void *data)
245 {
246 #ifdef CONFIG_DASD_PROFILE
247         char *buffer, *str;
248
249         if (user_len > 65536)
250                 user_len = 65536;
251         buffer = dasd_get_user_string(user_buf, user_len);
252         if (IS_ERR(buffer))
253                 return PTR_ERR(buffer);
254         MESSAGE_LOG(KERN_INFO, "/proc/dasd/statictics: '%s'", buffer);
255
256         /* check for valid verbs */
257         for (str = buffer; isspace(*str); str++);
258         if (strncmp(str, "set", 3) == 0 && isspace(str[3])) {
259                 /* 'set xxx' was given */
260                 for (str = str + 4; isspace(*str); str++);
261                 if (strcmp(str, "on") == 0) {
262                         /* switch on statistics profiling */
263                         dasd_profile_level = DASD_PROFILE_ON;
264                         MESSAGE(KERN_INFO, "%s", "Statistics switched on");
265                 } else if (strcmp(str, "off") == 0) {
266                         /* switch off and reset statistics profiling */
267                         memset(&dasd_global_profile,
268                                0, sizeof (struct dasd_profile_info_t));
269                         dasd_profile_level = DASD_PROFILE_OFF;
270                         MESSAGE(KERN_INFO, "%s", "Statistics switched off");
271                 } else
272                         goto out_error;
273         } else if (strncmp(str, "reset", 5) == 0) {
274                 /* reset the statistics */
275                 memset(&dasd_global_profile, 0,
276                        sizeof (struct dasd_profile_info_t));
277                 MESSAGE(KERN_INFO, "%s", "Statistics reset");
278         } else
279                 goto out_error;
280         kfree(buffer);
281         return user_len;
282 out_error:
283         MESSAGE(KERN_WARNING, "%s",
284                 "/proc/dasd/statistics: only 'set on', 'set off' "
285                 "and 'reset' are supported verbs");
286         kfree(buffer);
287         return -EINVAL;
288 #else
289         MESSAGE(KERN_WARNING, "%s",
290                 "/proc/dasd/statistics: is not activated in this kernel");
291         return user_len;
292 #endif                          /* CONFIG_DASD_PROFILE */
293 }
294
295 int
296 dasd_proc_init(void)
297 {
298         dasd_proc_root_entry = proc_mkdir("dasd", &proc_root);
299         dasd_proc_root_entry->owner = THIS_MODULE;
300         dasd_devices_entry = create_proc_entry("devices",
301                                                S_IFREG | S_IRUGO | S_IWUSR,
302                                                dasd_proc_root_entry);
303         dasd_devices_entry->proc_fops = &dasd_devices_file_ops;
304         dasd_devices_entry->owner = THIS_MODULE;
305         dasd_statistics_entry = create_proc_entry("statistics",
306                                                   S_IFREG | S_IRUGO | S_IWUSR,
307                                                   dasd_proc_root_entry);
308         dasd_statistics_entry->read_proc = dasd_statistics_read;
309         dasd_statistics_entry->write_proc = dasd_statistics_write;
310         dasd_statistics_entry->owner = THIS_MODULE;
311         return 0;
312 }
313
314 void
315 dasd_proc_exit(void)
316 {
317         remove_proc_entry("devices", dasd_proc_root_entry);
318         remove_proc_entry("statistics", dasd_proc_root_entry);
319         remove_proc_entry("dasd", &proc_root);
320 }