Whamcloud - gitweb
LU-8066 obdclass: move lustre sysctl to sysfs
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/module.h>
34 #include <linux/sysctl.h>
35 #include <linux/sched.h>
36 #include <linux/mm.h>
37 #include <linux/sysctl.h>
38 #include <linux/version.h>
39 #include <linux/proc_fs.h>
40 #include <linux/slab.h>
41 #include <linux/stat.h>
42 #include <linux/ctype.h>
43 #include <asm/bitops.h>
44 #include <asm/uaccess.h>
45 #include <linux/utsname.h>
46
47 #define DEBUG_SUBSYSTEM S_CLASS
48
49 #include <obd_support.h>
50 #include <lprocfs_status.h>
51 #include <obd_class.h>
52
53 #ifdef CONFIG_SYSCTL
54 static struct ctl_table_header *obd_table_header;
55 #endif
56
57 struct static_lustre_uintvalue_attr {
58         struct {
59                 struct attribute attr;
60                 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
61                                 char *buf);
62                 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
63                                  const char *buf, size_t len);
64         } u;
65         int *value;
66 };
67
68 static ssize_t static_uintvalue_show(struct kobject *kobj,
69                                      struct attribute *attr,
70                                      char *buf)
71 {
72         struct static_lustre_uintvalue_attr *lattr = (void *)attr;
73
74         return sprintf(buf, "%d\n", *lattr->value);
75 }
76
77 static ssize_t static_uintvalue_store(struct kobject *kobj,
78                                       struct attribute *attr,
79                                       const char *buffer, size_t count)
80 {
81         struct static_lustre_uintvalue_attr *lattr  = (void *)attr;
82         unsigned int val;
83         int rc;
84
85         rc = kstrtouint(buffer, 10, &val);
86         if (rc)
87                 return rc;
88
89         *lattr->value = val;
90
91         return count;
92 }
93
94 #define LUSTRE_STATIC_UINT_ATTR(name, value) \
95 static struct static_lustre_uintvalue_attr lustre_sattr_##name =        \
96                                         {__ATTR(name, 0644,             \
97                                                 static_uintvalue_show,  \
98                                                 static_uintvalue_store),\
99                                                 value }
100
101 LUSTRE_STATIC_UINT_ATTR(timeout, &obd_timeout);
102
103 #ifdef CONFIG_SYSCTL
104 static int
105 proc_memory_alloc(struct ctl_table *table, int write, void __user *buffer,
106                   size_t *lenp, loff_t *ppos)
107 {
108         char buf[22];
109         int len;
110
111         if (!*lenp || (*ppos && !write)) {
112                 *lenp = 0;
113                 return 0;
114         }
115         if (write)
116                 return -EINVAL;
117
118         len = snprintf(buf, sizeof(buf), "%llu\n", obd_memory_sum());
119         if (len > *lenp)
120                 len = *lenp;
121         buf[len] = '\0';
122         if (copy_to_user(buffer, buf, len))
123                 return -EFAULT;
124         *lenp = len;
125         *ppos += *lenp;
126         return 0;
127 }
128
129 static int
130 proc_mem_max(struct ctl_table *table, int write, void __user *buffer,
131              size_t *lenp, loff_t *ppos)
132 {
133         char buf[22];
134         int len;
135
136         if (!*lenp || (*ppos && !write)) {
137                 *lenp = 0;
138                 return 0;
139         }
140         if (write)
141                 return -EINVAL;
142
143         len = snprintf(buf, sizeof(buf), "%llu\n", obd_memory_max());
144         if (len > *lenp)
145                 len = *lenp;
146         buf[len] = '\0';
147         if (copy_to_user(buffer, buf, len))
148                 return -EFAULT;
149         *lenp = len;
150         *ppos += *lenp;
151         return 0;
152 }
153 #endif /* CONFIG_SYSCTL */
154
155 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
156                                  char *buf)
157 {
158         return sprintf(buf, "%lu\n",
159                        obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
160 }
161
162 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
163                                   const char *buffer, size_t count)
164 {
165         unsigned long val;
166         int rc;
167
168         rc = kstrtoul(buffer, 10, &val);
169         if (rc)
170                 return rc;
171
172         val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
173
174         if (val > ((totalram_pages / 10) * 9)) {
175                 /* Somebody wants to assign too much memory to dirty pages */
176                 return -EINVAL;
177         }
178
179         if (val < 4 << (20 - PAGE_SHIFT)) {
180                 /* Less than 4 Mb for dirty cache is also bad */
181                 return -EINVAL;
182         }
183
184         obd_max_dirty_pages = val;
185
186         return count;
187 }
188 LUSTRE_RW_ATTR(max_dirty_mb);
189
190 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
191 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
192 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
193 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
194 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
195 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
196 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
197 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
198
199 #ifdef HAVE_SERVER_SUPPORT
200 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
201 #endif
202
203 #ifdef CONFIG_SYSCTL
204 static struct ctl_table obd_table[] = {
205         {
206                 INIT_CTL_NAME
207                 .procname       = "memused",
208                 .data           = NULL,
209                 .maxlen         = 0,
210                 .mode           = 0444,
211                 .proc_handler   = &proc_memory_alloc
212         },
213         {
214                 INIT_CTL_NAME
215                 .procname       = "memused_max",
216                 .data           = NULL,
217                 .maxlen         = 0,
218                 .mode           = 0444,
219                 .proc_handler   = &proc_mem_max
220         },
221         {
222                 INIT_CTL_NAME
223                 .procname       = "bulk_timeout",
224                 .data           = &bulk_timeout,
225                 .maxlen         = sizeof(int),
226                 .mode           = 0644,
227                 .proc_handler   = &proc_dointvec
228         },
229         { 0 }
230 };
231
232 static struct ctl_table parent_table[] = {
233         {
234                 INIT_CTL_NAME
235                 .procname       = "lustre",
236                 .data           = NULL,
237                 .maxlen         = 0,
238                 .mode           = 0555,
239                 .child          = obd_table
240         },
241         { 0 }
242 };
243 #endif
244
245 static struct attribute *lustre_attrs[] = {
246         &lustre_sattr_timeout.u.attr,
247         &lustre_attr_max_dirty_mb.attr,
248         &lustre_sattr_debug_peer_on_timeout.u.attr,
249         &lustre_sattr_dump_on_timeout.u.attr,
250         &lustre_sattr_dump_on_eviction.u.attr,
251         &lustre_sattr_at_min.u.attr,
252         &lustre_sattr_at_max.u.attr,
253         &lustre_sattr_at_extra.u.attr,
254         &lustre_sattr_at_early_margin.u.attr,
255         &lustre_sattr_at_history.u.attr,
256 #ifdef HAVE_SERVER_SUPPORT
257         &lustre_sattr_ldlm_timeout.u.attr,
258 #endif
259         NULL,
260 };
261
262 static struct attribute_group lustre_attr_group = {
263         .attrs = lustre_attrs,
264 };
265
266 int obd_sysctl_init(void)
267 {
268 #ifdef CONFIG_SYSCTL
269         if ( !obd_table_header )
270                 obd_table_header = register_sysctl_table(parent_table);
271 #endif
272         return sysfs_create_group(lustre_kobj, &lustre_attr_group);
273 }
274
275 void obd_sysctl_clean (void)
276 {
277 #ifdef CONFIG_SYSCTL
278         if ( obd_table_header )
279                 unregister_sysctl_table(obd_table_header);
280         obd_table_header = NULL;
281 #endif
282 }