Whamcloud - gitweb
LU-8560 libcfs: handle PAGE_CACHE_* removal in newer kernels
[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, 2015, 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), "%llu\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_mem_max(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), "%llu\n", obd_memory_max());
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_max_dirty_pages_in_mb(struct ctl_table *table, int write,
125                            void __user *buffer, size_t *lenp, loff_t *ppos)
126 {
127         __s64 val;
128         int rc = 0;
129
130         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
131                 *lenp = 0;
132                 return 0;
133         }
134         if (write) {
135                 rc = lprocfs_str_with_units_to_s64(buffer, *lenp, &val, 'M');
136                 if (rc)
137                         return rc;
138
139                 if (val < 0)
140                         return -ERANGE;
141
142                 val >>= PAGE_SHIFT;
143
144                 /* Don't allow them to let dirty pages exceed 90% of system
145                  * memory and set a hard minimum of 4MB. */
146                 if (val > ((totalram_pages / 10) * 9)) {
147                         CERROR("Refusing to set max dirty pages to %lld, "
148                                "which is more than 90%% of available RAM; "
149                                "setting to %lu\n", val,
150                                ((totalram_pages / 10) * 9));
151                         obd_max_dirty_pages = ((totalram_pages / 10) * 9);
152                 } else if (val < 4 << (20 - PAGE_SHIFT)) {
153                         obd_max_dirty_pages = 4 << (20 - PAGE_SHIFT);
154                 } else {
155                         obd_max_dirty_pages = val;
156                 }
157         } else {
158                 char buf[21];
159                 int len;
160
161                 len = lprocfs_read_frac_helper(buf, sizeof(buf),
162                                                *(unsigned long *)table->data,
163                                                1 << (20 - PAGE_SHIFT));
164                 if (len > *lenp)
165                         len = *lenp;
166                 buf[len] = '\0';
167                 if (copy_to_user(buffer, buf, len))
168                         return -EFAULT;
169                 *lenp = len;
170         }
171         *ppos += *lenp;
172         return rc;
173 }
174
175 #ifdef CONFIG_SYSCTL
176 static struct ctl_table obd_table[] = {
177         {
178                 INIT_CTL_NAME
179                 .procname       = "timeout",
180                 .data           = &obd_timeout,
181                 .maxlen         = sizeof(int),
182                 .mode           = 0644,
183                 .proc_handler   = &proc_set_timeout
184         },
185         {
186                 INIT_CTL_NAME
187                 .procname       = "debug_peer_on_timeout",
188                 .data           = &obd_debug_peer_on_timeout,
189                 .maxlen         = sizeof(int),
190                 .mode           = 0644,
191                 .proc_handler   = &proc_dointvec
192         },
193         {
194                 INIT_CTL_NAME
195                 .procname       = "dump_on_timeout",
196                 .data           = &obd_dump_on_timeout,
197                 .maxlen         = sizeof(int),
198                 .mode           = 0644,
199                 .proc_handler   = &proc_dointvec
200         },
201         {
202                 INIT_CTL_NAME
203                 .procname       = "dump_on_eviction",
204                 .data           = &obd_dump_on_eviction,
205                 .maxlen         = sizeof(int),
206                 .mode           = 0644,
207                 .proc_handler   = &proc_dointvec
208         },
209         {
210                 INIT_CTL_NAME
211                 .procname       = "memused",
212                 .data           = NULL,
213                 .maxlen         = 0,
214                 .mode           = 0444,
215                 .proc_handler   = &proc_memory_alloc
216         },
217         {
218                 INIT_CTL_NAME
219                 .procname       = "memused_max",
220                 .data           = NULL,
221                 .maxlen         = 0,
222                 .mode           = 0444,
223                 .proc_handler   = &proc_mem_max
224         },
225         {
226                 INIT_CTL_NAME
227                 .procname       = "ldlm_timeout",
228                 .data           = &ldlm_timeout,
229                 .maxlen         = sizeof(int),
230                 .mode           = 0644,
231                 .proc_handler   = &proc_set_timeout
232         },
233         {
234                 INIT_CTL_NAME
235                 .procname       = "max_dirty_mb",
236                 .data           = &obd_max_dirty_pages,
237                 .maxlen         = sizeof(unsigned long),
238                 .mode           = 0644,
239                 .proc_handler   = &proc_max_dirty_pages_in_mb
240         },
241         {
242                 INIT_CTL_NAME
243                 .procname       = "bulk_timeout",
244                 .data           = &bulk_timeout,
245                 .maxlen         = sizeof(int),
246                 .mode           = 0644,
247                 .proc_handler   = &proc_dointvec
248         },
249         {
250                 INIT_CTL_NAME
251                 .procname       = "at_min",
252                 .data           = &at_min,
253                 .maxlen         = sizeof(int),
254                 .mode           = 0644,
255                 .proc_handler   = &proc_dointvec
256         },
257         {
258                 INIT_CTL_NAME
259                 .procname       = "at_max",
260                 .data           = &at_max,
261                 .maxlen         = sizeof(int),
262                 .mode           = 0644,
263                 .proc_handler   = &proc_dointvec
264         },
265         {
266                 INIT_CTL_NAME
267                 .procname       = "at_extra",
268                 .data           = &at_extra,
269                 .maxlen         = sizeof(int),
270                 .mode           = 0644,
271                 .proc_handler   = &proc_dointvec
272         },
273         {
274                 INIT_CTL_NAME
275                 .procname       = "at_early_margin",
276                 .data           = &at_early_margin,
277                 .maxlen         = sizeof(int),
278                 .mode           = 0644,
279                 .proc_handler   = &proc_dointvec
280         },
281         {
282                 INIT_CTL_NAME
283                 .procname       = "at_history",
284                 .data           = &at_history,
285                 .maxlen         = sizeof(int),
286                 .mode           = 0644,
287                 .proc_handler   = &proc_dointvec
288         },
289         { 0 }
290 };
291
292 static struct ctl_table parent_table[] = {
293         {
294                 INIT_CTL_NAME
295                 .procname       = "lustre",
296                 .data           = NULL,
297                 .maxlen         = 0,
298                 .mode           = 0555,
299                 .child          = obd_table
300         },
301         { 0 }
302 };
303 #endif
304
305 void obd_sysctl_init (void)
306 {
307 #ifdef CONFIG_SYSCTL
308         if ( !obd_table_header )
309                 obd_table_header = register_sysctl_table(parent_table);
310 #endif
311 }
312
313 void obd_sysctl_clean (void)
314 {
315 #ifdef CONFIG_SYSCTL
316         if ( obd_table_header )
317                 unregister_sysctl_table(obd_table_header);
318         obd_table_header = NULL;
319 #endif
320 }