Whamcloud - gitweb
LU-6068 misc: update Intel copyright messages 2014
[fs/lustre-release.git] / lustre / obdclass / linux / linux-sysctl.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) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, 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
37 #include <linux/module.h>
38 #include <linux/sysctl.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/sysctl.h>
42 #include <linux/version.h>
43 #include <linux/proc_fs.h>
44 #include <linux/slab.h>
45 #include <linux/stat.h>
46 #include <linux/ctype.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
49 #include <linux/utsname.h>
50
51 #define DEBUG_SUBSYSTEM S_CLASS
52
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55 #include <obd_class.h>
56
57 #ifdef CONFIG_SYSCTL
58 static struct ctl_table_header *obd_table_header;
59 #endif
60
61 static int
62 proc_set_timeout(struct ctl_table *table, int write, void __user *buffer,
63                  size_t *lenp, loff_t *ppos)
64 {
65         int rc;
66
67         rc = proc_dointvec(table, write, buffer, lenp, ppos);
68         if (ldlm_timeout >= obd_timeout)
69                 ldlm_timeout = max(obd_timeout / 3, 1U);
70         return rc;
71 }
72
73 static int
74 proc_memory_alloc(struct ctl_table *table, int write, void __user *buffer,
75                   size_t *lenp, loff_t *ppos)
76 {
77         char buf[22];
78         int len;
79
80         if (!*lenp || (*ppos && !write)) {
81                 *lenp = 0;
82                 return 0;
83         }
84         if (write)
85                 return -EINVAL;
86
87         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
88         if (len > *lenp)
89                 len = *lenp;
90         buf[len] = '\0';
91         if (copy_to_user(buffer, buf, len))
92                 return -EFAULT;
93         *lenp = len;
94         *ppos += *lenp;
95         return 0;
96 }
97
98 static int
99 proc_pages_alloc(struct ctl_table *table, int write, void __user *buffer,
100                  size_t *lenp, loff_t *ppos)
101 {
102         char buf[22];
103         int len;
104
105         if (!*lenp || (*ppos && !write)) {
106                 *lenp = 0;
107                 return 0;
108         }
109         if (write)
110                 return -EINVAL;
111
112         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
113         if (len > *lenp)
114                 len = *lenp;
115         buf[len] = '\0';
116         if (copy_to_user(buffer, buf, len))
117                 return -EFAULT;
118         *lenp = len;
119         *ppos += *lenp;
120         return 0;
121 }
122
123 static int
124 proc_mem_max(struct ctl_table *table, int write, void __user *buffer,
125              size_t *lenp, loff_t *ppos)
126 {
127         char buf[22];
128         int len;
129
130         if (!*lenp || (*ppos && !write)) {
131                 *lenp = 0;
132                 return 0;
133         }
134         if (write)
135                 return -EINVAL;
136
137         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
138         if (len > *lenp)
139                 len = *lenp;
140         buf[len] = '\0';
141         if (copy_to_user(buffer, buf, len))
142                 return -EFAULT;
143         *lenp = len;
144         *ppos += *lenp;
145         return 0;
146 }
147
148 static int
149 proc_pages_max(struct ctl_table *table, int write, void __user *buffer,
150                size_t *lenp, loff_t *ppos)
151 {
152         char buf[22];
153         int len;
154
155         if (!*lenp || (*ppos && !write)) {
156                 *lenp = 0;
157                 return 0;
158         }
159         if (write)
160                 return -EINVAL;
161
162         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
163         if (len > *lenp)
164                 len = *lenp;
165         buf[len] = '\0';
166         if (copy_to_user(buffer, buf, len))
167                 return -EFAULT;
168         *lenp = len;
169         *ppos += *lenp;
170         return 0;
171 }
172
173 static int
174 proc_max_dirty_pages_in_mb(struct ctl_table *table, int write,
175                            void __user *buffer, size_t *lenp, loff_t *ppos)
176 {
177         __u64 val;
178         int rc = 0;
179
180         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
181                 *lenp = 0;
182                 return 0;
183         }
184         if (write) {
185                 rc = lprocfs_write_frac_u64_helper(buffer, *lenp, &val,
186                                                1 << (20 - PAGE_CACHE_SHIFT));
187
188                 /* Don't allow them to let dirty pages exceed 90% of system
189                  * memory and set a hard minimum of 4MB. */
190                 if (val > ((totalram_pages / 10) * 9)) {
191                         CERROR("Refusing to set max dirty pages to "LPU64", "
192                                "which is more than 90%% of available RAM; "
193                                "setting to %lu\n", val,
194                                ((totalram_pages / 10) * 9));
195                         obd_max_dirty_pages = ((totalram_pages / 10) * 9);
196                 } else if (val < 4 << (20 - PAGE_CACHE_SHIFT)) {
197                         obd_max_dirty_pages = 4 << (20 - PAGE_CACHE_SHIFT);
198                 } else {
199                         obd_max_dirty_pages = val;
200                 }
201         } else {
202                 char buf[21];
203                 int len;
204
205                 len = lprocfs_read_frac_helper(buf, sizeof(buf),
206                                                *(unsigned long *)table->data,
207                                                1 << (20 - PAGE_CACHE_SHIFT));
208                 if (len > *lenp)
209                         len = *lenp;
210                 buf[len] = '\0';
211                 if (copy_to_user(buffer, buf, len))
212                         return -EFAULT;
213                 *lenp = len;
214         }
215         *ppos += *lenp;
216         return rc;
217 }
218
219 #ifdef RANDOM_FAIL_ALLOC
220 static int proc_alloc_fail_rate(struct ctl_table *table, int write,
221                                 void __user *buffer, size_t *lenp, loff_t *ppos)
222 {
223         int rc          = 0;
224
225         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
226                 *lenp = 0;
227                 return 0;
228         }
229         if (write) {
230                 rc = lprocfs_write_frac_helper(buffer, *lenp,
231                                                (unsigned int*)table->data,
232                                                OBD_ALLOC_FAIL_MULT);
233         } else {
234                 char buf[21];
235                 int  len;
236
237                 len = lprocfs_read_frac_helper(buf, 21,
238                                                *(unsigned int*)table->data,
239                                                OBD_ALLOC_FAIL_MULT);
240                 if (len > *lenp)
241                         len = *lenp;
242                 buf[len] = '\0';
243                 if (copy_to_user(buffer, buf, len))
244                         return -EFAULT;
245                 *lenp = len;
246         }
247         *ppos += *lenp;
248         return rc;
249 }
250 #endif
251
252 #ifdef CONFIG_SYSCTL
253 static struct ctl_table obd_table[] = {
254         {
255                 INIT_CTL_NAME
256                 .procname       = "timeout",
257                 .data           = &obd_timeout,
258                 .maxlen         = sizeof(int),
259                 .mode           = 0644,
260                 .proc_handler   = &proc_set_timeout
261         },
262         {
263                 INIT_CTL_NAME
264                 .procname       = "debug_peer_on_timeout",
265                 .data           = &obd_debug_peer_on_timeout,
266                 .maxlen         = sizeof(int),
267                 .mode           = 0644,
268                 .proc_handler   = &proc_dointvec
269         },
270         {
271                 INIT_CTL_NAME
272                 .procname       = "dump_on_timeout",
273                 .data           = &obd_dump_on_timeout,
274                 .maxlen         = sizeof(int),
275                 .mode           = 0644,
276                 .proc_handler   = &proc_dointvec
277         },
278         {
279                 INIT_CTL_NAME
280                 .procname       = "dump_on_eviction",
281                 .data           = &obd_dump_on_eviction,
282                 .maxlen         = sizeof(int),
283                 .mode           = 0644,
284                 .proc_handler   = &proc_dointvec
285         },
286         {
287                 INIT_CTL_NAME
288                 .procname       = "memused",
289                 .data           = NULL,
290                 .maxlen         = 0,
291                 .mode           = 0444,
292                 .proc_handler   = &proc_memory_alloc
293         },
294         {
295                 INIT_CTL_NAME
296                 .procname       = "pagesused",
297                 .data           = NULL,
298                 .maxlen         = 0,
299                 .mode           = 0444,
300                 .proc_handler   = &proc_pages_alloc
301         },
302         {
303                 INIT_CTL_NAME
304                 .procname       = "memused_max",
305                 .data           = NULL,
306                 .maxlen         = 0,
307                 .mode           = 0444,
308                 .proc_handler   = &proc_mem_max
309         },
310         {
311                 INIT_CTL_NAME
312                 .procname       = "pagesused_max",
313                 .data           = NULL,
314                 .maxlen         = 0,
315                 .mode           = 0444,
316                 .proc_handler   = &proc_pages_max
317         },
318         {
319                 INIT_CTL_NAME
320                 .procname       = "ldlm_timeout",
321                 .data           = &ldlm_timeout,
322                 .maxlen         = sizeof(int),
323                 .mode           = 0644,
324                 .proc_handler   = &proc_set_timeout
325         },
326 #ifdef RANDOM_FAIL_ALLOC
327         {
328                 INIT_CTL_NAME
329                 .procname       = "alloc_fail_rate",
330                 .data           = &obd_alloc_fail_rate,
331                 .maxlen         = sizeof(int),
332                 .mode           = 0644,
333                 .proc_handler   = &proc_alloc_fail_rate
334         },
335 #endif
336         {
337                 INIT_CTL_NAME
338                 .procname       = "max_dirty_mb",
339                 .data           = &obd_max_dirty_pages,
340                 .maxlen         = sizeof(unsigned long),
341                 .mode           = 0644,
342                 .proc_handler   = &proc_max_dirty_pages_in_mb
343         },
344         {
345                 INIT_CTL_NAME
346                 .procname       = "bulk_timeout",
347                 .data           = &bulk_timeout,
348                 .maxlen         = sizeof(int),
349                 .mode           = 0644,
350                 .proc_handler   = &proc_dointvec
351         },
352         {
353                 INIT_CTL_NAME
354                 .procname       = "at_min",
355                 .data           = &at_min,
356                 .maxlen         = sizeof(int),
357                 .mode           = 0644,
358                 .proc_handler   = &proc_dointvec
359         },
360         {
361                 INIT_CTL_NAME
362                 .procname       = "at_max",
363                 .data           = &at_max,
364                 .maxlen         = sizeof(int),
365                 .mode           = 0644,
366                 .proc_handler   = &proc_dointvec
367         },
368         {
369                 INIT_CTL_NAME
370                 .procname       = "at_extra",
371                 .data           = &at_extra,
372                 .maxlen         = sizeof(int),
373                 .mode           = 0644,
374                 .proc_handler   = &proc_dointvec
375         },
376         {
377                 INIT_CTL_NAME
378                 .procname       = "at_early_margin",
379                 .data           = &at_early_margin,
380                 .maxlen         = sizeof(int),
381                 .mode           = 0644,
382                 .proc_handler   = &proc_dointvec
383         },
384         {
385                 INIT_CTL_NAME
386                 .procname       = "at_history",
387                 .data           = &at_history,
388                 .maxlen         = sizeof(int),
389                 .mode           = 0644,
390                 .proc_handler   = &proc_dointvec
391         },
392         { 0 }
393 };
394
395 static struct ctl_table parent_table[] = {
396         {
397                 INIT_CTL_NAME
398                 .procname       = "lustre",
399                 .data           = NULL,
400                 .maxlen         = 0,
401                 .mode           = 0555,
402                 .child          = obd_table
403         },
404         { 0 }
405 };
406 #endif
407
408 void obd_sysctl_init (void)
409 {
410 #ifdef CONFIG_SYSCTL
411         if ( !obd_table_header )
412                 obd_table_header = register_sysctl_table(parent_table);
413 #endif
414 }
415
416 void obd_sysctl_clean (void)
417 {
418 #ifdef CONFIG_SYSCTL
419         if ( obd_table_header )
420                 unregister_sysctl_table(obd_table_header);
421         obd_table_header = NULL;
422 #endif
423 }