Whamcloud - gitweb
LU-5396 libcfs: add sparse annotation __user wherever needed
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-proc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-proc.c
37  *
38  * Author: Zach Brown <zab@zabbo.net>
39  * Author: Peter J. Braam <braam@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/unistd.h>
50 #include <net/sock.h>
51 #include <linux/uio.h>
52
53 #include <asm/uaccess.h>
54
55 #include <linux/fs.h>
56 #include <linux/file.h>
57 #include <linux/stat.h>
58 #include <linux/list.h>
59 #include <asm/uaccess.h>
60
61 #include <linux/proc_fs.h>
62 #include <linux/sysctl.h>
63
64 # define DEBUG_SUBSYSTEM S_LNET
65
66 #include <libcfs/libcfs.h>
67 #include <asm/div64.h>
68 #include "tracefile.h"
69
70 #ifdef CONFIG_SYSCTL
71 static struct ctl_table_header *lnet_table_header = NULL;
72 #endif
73 extern char lnet_upcall[1024];
74 /**
75  * The path of debug log dump upcall script.
76  */
77 extern char lnet_debug_log_upcall[1024];
78
79 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
80                          void __user *buffer, size_t *lenp,
81                          int (*handler)(void *data, int write, loff_t pos,
82                                         void __user *buffer, int len))
83 {
84         int rc = handler(data, write, *ppos, buffer, *lenp);
85
86         if (rc < 0)
87                 return rc;
88
89         if (write) {
90                 *ppos += *lenp;
91         } else {
92                 *lenp = rc;
93                 *ppos += rc;
94         }
95         return 0;
96 }
97 EXPORT_SYMBOL(lprocfs_call_handler);
98
99 static int __proc_dobitmasks(void *data, int write,
100                              loff_t pos, void __user *buffer, int nob)
101 {
102         const int     tmpstrlen = 512;
103         char         *tmpstr;
104         int           rc;
105         unsigned int *mask = data;
106         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
107         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
108
109         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
110         if (rc < 0)
111                 return rc;
112
113         if (!write) {
114                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
115                 rc = strlen(tmpstr);
116
117                 if (pos >= rc) {
118                         rc = 0;
119                 } else {
120                         rc = cfs_trace_copyout_string(buffer, nob,
121                                                       tmpstr + pos, "\n");
122                 }
123         } else {
124                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
125                 if (rc < 0) {
126                         cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
127                         return rc;
128                 }
129
130                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
131                 /* Always print LBUG/LASSERT to console, so keep this mask */
132                 if (is_printk)
133                         *mask |= D_EMERG;
134         }
135
136         cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
137         return rc;
138 }
139
140 static int
141 proc_dobitmasks(struct ctl_table *table, int write,
142                 void __user *buffer, size_t *lenp, loff_t *ppos)
143 {
144         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
145                                     __proc_dobitmasks);
146 }
147
148 static int min_watchdog_ratelimit = 0;          /* disable ratelimiting */
149 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
150
151 static int __proc_dump_kernel(void *data, int write,
152                               loff_t pos, void __user *buffer, int nob)
153 {
154         if (!write)
155                 return 0;
156
157         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
158 }
159
160 static int
161 proc_dump_kernel(struct ctl_table *table, int write, void __user *buffer,
162                  size_t *lenp, loff_t *ppos)
163 {
164         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
165                                     __proc_dump_kernel);
166 }
167
168 static int __proc_daemon_file(void *data, int write,
169                               loff_t pos, void __user *buffer, int nob)
170 {
171         if (!write) {
172                 int len = strlen(cfs_tracefile);
173
174                 if (pos >= len)
175                         return 0;
176
177                 return cfs_trace_copyout_string(buffer, nob,
178                                                 cfs_tracefile + pos, "\n");
179         }
180
181         return cfs_trace_daemon_command_usrstr(buffer, nob);
182 }
183
184 static int
185 proc_daemon_file(struct ctl_table *table, int write, void __user *buffer,
186                  size_t *lenp, loff_t *ppos)
187 {
188         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
189                                      __proc_daemon_file);
190 }
191
192 static int __proc_debug_mb(void *data, int write,
193                            loff_t pos, void __user *buffer, int nob)
194 {
195         if (!write) {
196                 char tmpstr[32];
197                 int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
198                                     cfs_trace_get_debug_mb());
199
200                 if (pos >= len)
201                         return 0;
202
203                 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
204                        "\n");
205         }
206
207         return cfs_trace_set_debug_mb_usrstr(buffer, nob);
208 }
209
210 static int
211 proc_debug_mb(struct ctl_table *table, int write, void __user *buffer,
212               size_t *lenp, loff_t *ppos)
213 {
214         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
215                                     __proc_debug_mb);
216 }
217
218 static int
219 proc_console_max_delay_cs(struct ctl_table *table, int write,
220                           void __user *buffer, size_t *lenp, loff_t *ppos)
221 {
222         int rc, max_delay_cs;
223         struct ctl_table dummy = *table;
224         cfs_duration_t d;
225
226         dummy.data = &max_delay_cs;
227         dummy.proc_handler = &proc_dointvec;
228
229         if (!write) { /* read */
230                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
231                 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
232                 return rc;
233         }
234
235         /* write */
236         max_delay_cs = 0;
237         rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
238         if (rc < 0)
239                 return rc;
240         if (max_delay_cs <= 0)
241                 return -EINVAL;
242
243         d = cfs_time_seconds(max_delay_cs) / 100;
244         if (d == 0 || d < libcfs_console_min_delay)
245                 return -EINVAL;
246         libcfs_console_max_delay = d;
247
248         return rc;
249 }
250
251 static int
252 proc_console_min_delay_cs(struct ctl_table *table, int write,
253                           void __user *buffer, size_t *lenp, loff_t *ppos)
254 {
255         int rc, min_delay_cs;
256         struct ctl_table dummy = *table;
257         cfs_duration_t d;
258
259         dummy.data = &min_delay_cs;
260         dummy.proc_handler = &proc_dointvec;
261
262         if (!write) { /* read */
263                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
264                 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
265                 return rc;
266         }
267
268         /* write */
269         min_delay_cs = 0;
270         rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
271         if (rc < 0)
272                 return rc;
273         if (min_delay_cs <= 0)
274                 return -EINVAL;
275
276         d = cfs_time_seconds(min_delay_cs) / 100;
277         if (d == 0 || d > libcfs_console_max_delay)
278                 return -EINVAL;
279         libcfs_console_min_delay = d;
280
281         return rc;
282 }
283
284 static int
285 proc_console_backoff(struct ctl_table *table, int write, void __user *buffer,
286                      size_t *lenp, loff_t *ppos)
287 {
288         int rc, backoff;
289         struct ctl_table dummy = *table;
290
291         dummy.data = &backoff;
292         dummy.proc_handler = &proc_dointvec;
293
294         if (!write) { /* read */
295                 backoff= libcfs_console_backoff;
296                 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
297                 return rc;
298         }
299
300         /* write */
301         backoff = 0;
302         rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
303         if (rc < 0)
304                 return rc;
305         if (backoff <= 0)
306                 return -EINVAL;
307
308         libcfs_console_backoff = backoff;
309
310         return rc;
311 }
312
313 static int
314 libcfs_force_lbug(struct ctl_table *table, int write, void __user *buffer,
315                   size_t *lenp, loff_t *ppos)
316 {
317         if (write)
318                 LBUG();
319         return 0;
320 }
321
322 static int
323 proc_fail_loc(struct ctl_table *table, int write, void __user *buffer,
324               size_t *lenp, loff_t *ppos)
325 {
326         int rc;
327         long old_fail_loc = cfs_fail_loc;
328
329         rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
330         if (old_fail_loc != cfs_fail_loc)
331                 wake_up(&cfs_race_waitq);
332         return rc;
333 }
334
335 static int __proc_cpt_table(void *data, int write,
336                             loff_t pos, void __user *buffer, int nob)
337 {
338         char *buf = NULL;
339         int   len = 4096;
340         int   rc  = 0;
341
342         if (write)
343                 return -EPERM;
344
345         LASSERT(cfs_cpt_table != NULL);
346
347         while (1) {
348                 LIBCFS_ALLOC(buf, len);
349                 if (buf == NULL)
350                         return -ENOMEM;
351
352                 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
353                 if (rc >= 0)
354                         break;
355
356                 LIBCFS_FREE(buf, len);
357                 if (rc == -EFBIG) {
358                         len <<= 1;
359                         continue;
360                 }
361                 goto out;
362         }
363
364         if (pos >= rc) {
365                 rc = 0;
366                 goto out;
367         }
368
369         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
370  out:
371         if (buf != NULL)
372                 LIBCFS_FREE(buf, len);
373         return rc;
374 }
375
376 static int
377 proc_cpt_table(struct ctl_table *table, int write, void __user *buffer,
378                size_t *lenp, loff_t *ppos)
379 {
380         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
381                                      __proc_cpt_table);
382 }
383
384 static struct ctl_table lnet_table[] = {
385         /*
386          * NB No .strategy entries have been provided since sysctl(8) prefers
387          * to go via /proc for portability.
388          */
389         {
390                 INIT_CTL_NAME
391                 .procname       = "debug",
392                 .data           = &libcfs_debug,
393                 .maxlen         = sizeof(int),
394                 .mode           = 0644,
395                 .proc_handler   = &proc_dobitmasks,
396         },
397         {
398                 INIT_CTL_NAME
399                 .procname       = "subsystem_debug",
400                 .data           = &libcfs_subsystem_debug,
401                 .maxlen         = sizeof(int),
402                 .mode           = 0644,
403                 .proc_handler   = &proc_dobitmasks,
404         },
405         {
406                 INIT_CTL_NAME
407                 .procname       = "printk",
408                 .data           = &libcfs_printk,
409                 .maxlen         = sizeof(int),
410                 .mode           = 0644,
411                 .proc_handler   = &proc_dobitmasks,
412         },
413         {
414                 INIT_CTL_NAME
415                 .procname       = "console_ratelimit",
416                 .data           = &libcfs_console_ratelimit,
417                 .maxlen         = sizeof(int),
418                 .mode           = 0644,
419                 .proc_handler   = &proc_dointvec
420         },
421         {
422                 INIT_CTL_NAME
423                 .procname       = "console_max_delay_centisecs",
424                 .maxlen         = sizeof(int),
425                 .mode           = 0644,
426                 .proc_handler   = &proc_console_max_delay_cs
427         },
428         {
429                 INIT_CTL_NAME
430                 .procname       = "console_min_delay_centisecs",
431                 .maxlen         = sizeof(int),
432                 .mode           = 0644,
433                 .proc_handler   = &proc_console_min_delay_cs
434         },
435         {
436                 INIT_CTL_NAME
437                 .procname       = "console_backoff",
438                 .maxlen         = sizeof(int),
439                 .mode           = 0644,
440                 .proc_handler   = &proc_console_backoff
441         },
442         {
443                 INIT_CTL_NAME
444                 .procname       = "debug_path",
445                 .data           = libcfs_debug_file_path_arr,
446                 .maxlen         = sizeof(libcfs_debug_file_path_arr),
447                 .mode           = 0644,
448                 .proc_handler   = &proc_dostring,
449         },
450         {
451                 INIT_CTL_NAME
452                 .procname       = "cpu_partition_table",
453                 .maxlen         = 128,
454                 .mode           = 0444,
455                 .proc_handler   = &proc_cpt_table,
456         },
457         {
458                 INIT_CTL_NAME
459                 .procname       = "upcall",
460                 .data           = lnet_upcall,
461                 .maxlen         = sizeof(lnet_upcall),
462                 .mode           = 0644,
463                 .proc_handler   = &proc_dostring,
464         },
465         {
466                 INIT_CTL_NAME
467                 .procname       = "debug_log_upcall",
468                 .data           = lnet_debug_log_upcall,
469                 .maxlen         = sizeof(lnet_debug_log_upcall),
470                 .mode           = 0644,
471                 .proc_handler   = &proc_dostring,
472         },
473         {
474                 INIT_CTL_NAME
475                 .procname       = "lnet_memused",
476                 .data           = (int *)&libcfs_kmemory.counter,
477                 .maxlen         = sizeof(int),
478                 .mode           = 0444,
479                 .proc_handler   = &proc_dointvec,
480                 INIT_STRATEGY
481         },
482         {
483                 INIT_CTL_NAME
484                 .procname       = "catastrophe",
485                 .data           = &libcfs_catastrophe,
486                 .maxlen         = sizeof(int),
487                 .mode           = 0444,
488                 .proc_handler   = &proc_dointvec,
489                 INIT_STRATEGY
490         },
491         {
492                 INIT_CTL_NAME
493                 .procname       = "panic_on_lbug",
494                 .data           = &libcfs_panic_on_lbug,
495                 .maxlen         = sizeof(int),
496                 .mode           = 0644,
497                 .proc_handler   = &proc_dointvec,
498                 INIT_STRATEGY
499         },
500         {
501                 INIT_CTL_NAME
502                 .procname       = "dump_kernel",
503                 .maxlen         = 256,
504                 .mode           = 0200,
505                 .proc_handler   = &proc_dump_kernel,
506         },
507         {
508                 INIT_CTL_NAME
509                 .procname       = "daemon_file",
510                 .mode           = 0644,
511                 .maxlen         = 256,
512                 .proc_handler   = &proc_daemon_file,
513         },
514         {
515                 INIT_CTL_NAME
516                 .procname       = "debug_mb",
517                 .mode           = 0644,
518                 .proc_handler   = &proc_debug_mb,
519         },
520         {
521                 INIT_CTL_NAME
522                 .procname       = "watchdog_ratelimit",
523                 .data           = &libcfs_watchdog_ratelimit,
524                 .maxlen         = sizeof(int),
525                 .mode           = 0644,
526                 .proc_handler   = &proc_dointvec_minmax,
527                 .extra1         = &min_watchdog_ratelimit,
528                 .extra2         = &max_watchdog_ratelimit,
529         },
530         {
531                 INIT_CTL_NAME
532                 .procname       = "force_lbug",
533                 .data           = NULL,
534                 .maxlen         = 0,
535                 .mode           = 0200,
536                 .proc_handler   = &libcfs_force_lbug
537         },
538         {
539                 INIT_CTL_NAME
540                 .procname       = "fail_loc",
541                 .data           = &cfs_fail_loc,
542                 .maxlen         = sizeof(cfs_fail_loc),
543                 .mode           = 0644,
544                 .proc_handler   = &proc_fail_loc
545         },
546         {
547                 INIT_CTL_NAME
548                 .procname       = "fail_val",
549                 .data           = &cfs_fail_val,
550                 .maxlen         = sizeof(int),
551                 .mode           = 0644,
552                 .proc_handler   = &proc_dointvec
553         },
554         {
555                 INIT_CTL_NAME
556                 .procname       = "fail_err",
557                 .data           = &cfs_fail_err,
558                 .maxlen         = sizeof(cfs_fail_err),
559                 .mode           = 0644,
560                 .proc_handler   = &proc_dointvec,
561         },
562         { 0 }
563 };
564
565 #ifdef CONFIG_SYSCTL
566 static struct ctl_table top_table[] = {
567         {
568                 INIT_CTL_NAME
569                 .procname       = "lnet",
570                 .mode           = 0555,
571                 .data           = NULL,
572                 .maxlen         = 0,
573                 .child          = lnet_table,
574         },
575         { 0 }
576 };
577 #endif
578
579 int insert_proc(void)
580 {
581 #ifdef CONFIG_SYSCTL
582         if (lnet_table_header == NULL)
583                 lnet_table_header = register_sysctl_table(top_table);
584 #endif
585         return 0;
586 }
587
588 void remove_proc(void)
589 {
590 #ifdef CONFIG_SYSCTL
591         if (lnet_table_header != NULL)
592                 unregister_sysctl_table(lnet_table_header);
593
594         lnet_table_header = NULL;
595 #endif
596 }