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