1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef _LINUXKPI_LINUX_SYSFS_H_
30 #define _LINUXKPI_LINUX_SYSFS_H_
31
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
35
36 #include <linux/kobject.h>
37 #include <linux/stringify.h>
38 #include <linux/mm.h>
39
40 struct sysfs_ops {
41 ssize_t (*show)(struct kobject *, struct attribute *, char *);
42 ssize_t (*store)(struct kobject *, struct attribute *, const char *,
43 size_t);
44 };
45
46 struct attribute_group {
47 const char *name;
48 mode_t (*is_visible)(struct kobject *,
49 struct attribute *, int);
50 struct attribute **attrs;
51 };
52
53 #define __ATTR(_name, _mode, _show, _store) { \
54 .attr = { .name = __stringify(_name), .mode = _mode }, \
55 .show = _show, .store = _store, \
56 }
57 #define __ATTR_RO(_name) __ATTR(_name, 0444, _name##_show, NULL)
58 #define __ATTR_WO(_name) __ATTR(_name, 0200, NULL, _name##_store)
59 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
60 #define __ATTR_NULL { .attr = { .name = NULL } }
61
62 #define ATTRIBUTE_GROUPS(_name) \
63 static struct attribute_group _name##_group = { \
64 .name = __stringify(_name), \
65 .attrs = _name##_attrs, \
66 }; \
67 static const struct attribute_group *_name##_groups[] = { \
68 &_name##_group, \
69 NULL, \
70 }
71
72 /*
73 * Handle our generic '\0' terminated 'C' string.
74 * Two cases:
75 * a variable string: point arg1 at it, arg2 is max length.
76 * a constant string: point arg1 at it, arg2 is zero.
77 */
78
79 static inline int
sysctl_handle_attr(SYSCTL_HANDLER_ARGS)80 sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
81 {
82 struct kobject *kobj;
83 struct attribute *attr;
84 const struct sysfs_ops *ops;
85 char *buf;
86 int error;
87 ssize_t len;
88
89 kobj = arg1;
90 attr = (struct attribute *)(intptr_t)arg2;
91 if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
92 return (ENODEV);
93 buf = (char *)get_zeroed_page(GFP_KERNEL);
94 if (buf == NULL)
95 return (ENOMEM);
96 ops = kobj->ktype->sysfs_ops;
97 if (ops->show) {
98 len = ops->show(kobj, attr, buf);
99 /*
100 * It's valid to not have a 'show' so just return an
101 * empty string.
102 */
103 if (len < 0) {
104 error = -len;
105 if (error != EIO)
106 goto out;
107 buf[0] = '\0';
108 } else if (len) {
109 len--;
110 if (len >= PAGE_SIZE)
111 len = PAGE_SIZE - 1;
112 /* Trim trailing newline. */
113 buf[len] = '\0';
114 }
115 }
116
117 /* Leave one trailing byte to append a newline. */
118 error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
119 if (error != 0 || req->newptr == NULL || ops->store == NULL)
120 goto out;
121 len = strlcat(buf, "\n", PAGE_SIZE);
122 KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
123 len = ops->store(kobj, attr, buf, len);
124 if (len < 0)
125 error = -len;
126 out:
127 free_page((unsigned long)buf);
128
129 return (error);
130 }
131
132 static inline int
sysfs_create_file(struct kobject * kobj,const struct attribute * attr)133 sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
134 {
135 struct sysctl_oid *oid;
136
137 oid = SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
138 attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
139 (uintptr_t)attr, sysctl_handle_attr, "A", "");
140 if (!oid) {
141 return (-ENOMEM);
142 }
143
144 return (0);
145 }
146
147 static inline void
sysfs_remove_file(struct kobject * kobj,const struct attribute * attr)148 sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
149 {
150
151 if (kobj->oidp)
152 sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
153 }
154
155 static inline int
sysfs_create_files(struct kobject * kobj,const struct attribute * const * attrs)156 sysfs_create_files(struct kobject *kobj, const struct attribute * const *attrs)
157 {
158 int error = 0;
159 int i;
160
161 for (i = 0; attrs[i] && !error; i++)
162 error = sysfs_create_file(kobj, attrs[i]);
163 while (error && --i >= 0)
164 sysfs_remove_file(kobj, attrs[i]);
165
166 return (error);
167 }
168
169 static inline void
sysfs_remove_files(struct kobject * kobj,const struct attribute * const * attrs)170 sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attrs)
171 {
172 int i;
173
174 for (i = 0; attrs[i]; i++)
175 sysfs_remove_file(kobj, attrs[i]);
176 }
177
178 static inline int
sysfs_create_group(struct kobject * kobj,const struct attribute_group * grp)179 sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
180 {
181 struct attribute **attr;
182 struct sysctl_oid *oidp;
183
184 /* Don't create the group node if grp->name is undefined. */
185 if (grp->name)
186 oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
187 OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
188 else
189 oidp = kobj->oidp;
190 for (attr = grp->attrs; *attr != NULL; attr++) {
191 SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
192 (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
193 kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
194 }
195
196 return (0);
197 }
198
199 static inline void
sysfs_remove_group(struct kobject * kobj,const struct attribute_group * grp)200 sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
201 {
202
203 if (kobj->oidp)
204 sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
205 }
206
207 static inline int
sysfs_create_groups(struct kobject * kobj,const struct attribute_group ** grps)208 sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps)
209 {
210 int error = 0;
211 int i;
212
213 if (grps == NULL)
214 goto done;
215 for (i = 0; grps[i] && !error; i++)
216 error = sysfs_create_group(kobj, grps[i]);
217 while (error && --i >= 0)
218 sysfs_remove_group(kobj, grps[i]);
219 done:
220 return (error);
221 }
222
223 static inline void
sysfs_remove_groups(struct kobject * kobj,const struct attribute_group ** grps)224 sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **grps)
225 {
226 int i;
227
228 if (grps == NULL)
229 return;
230 for (i = 0; grps[i]; i++)
231 sysfs_remove_group(kobj, grps[i]);
232 }
233
234 static inline int
sysfs_merge_group(struct kobject * kobj,const struct attribute_group * grp)235 sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp)
236 {
237
238 /* Really expected behavior is to return failure if group exists. */
239 return (sysfs_create_group(kobj, grp));
240 }
241
242 static inline void
sysfs_unmerge_group(struct kobject * kobj,const struct attribute_group * grp)243 sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp)
244 {
245 struct attribute **attr;
246 struct sysctl_oid *oidp;
247
248 SYSCTL_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp)) {
249 if (strcmp(oidp->oid_name, grp->name) != 0)
250 continue;
251 for (attr = grp->attrs; *attr != NULL; attr++) {
252 sysctl_remove_name(oidp, (*attr)->name, 1, 1);
253 }
254 }
255 }
256
257 static inline int
sysfs_create_dir(struct kobject * kobj)258 sysfs_create_dir(struct kobject *kobj)
259 {
260 struct sysctl_oid *oid;
261
262 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
263 OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
264 if (!oid) {
265 return (-ENOMEM);
266 }
267 kobj->oidp = oid;
268
269 return (0);
270 }
271
272 static inline void
sysfs_remove_dir(struct kobject * kobj)273 sysfs_remove_dir(struct kobject *kobj)
274 {
275
276 if (kobj->oidp == NULL)
277 return;
278 sysctl_remove_oid(kobj->oidp, 1, 1);
279 }
280
281 static inline bool
sysfs_streq(const char * s1,const char * s2)282 sysfs_streq(const char *s1, const char *s2)
283 {
284 int l1, l2;
285
286 l1 = strlen(s1);
287 l2 = strlen(s2);
288
289 if (l1 != 0 && s1[l1-1] == '\n')
290 l1--;
291 if (l2 != 0 && s2[l2-1] == '\n')
292 l2--;
293
294 return (l1 == l2 && strncmp(s1, s2, l1) == 0);
295 }
296
297 static inline int
sysfs_emit(char * buf,const char * fmt,...)298 sysfs_emit(char *buf, const char *fmt, ...)
299 {
300 va_list args;
301 int i;
302
303 if (!buf || offset_in_page(buf)) {
304 pr_warn("invalid sysfs_emit: buf:%p\n", buf);
305 return (0);
306 }
307
308 va_start(args, fmt);
309 i = vscnprintf(buf, PAGE_SIZE, fmt, args);
310 va_end(args);
311
312 return (i);
313 }
314
315 static inline int
sysfs_emit_at(char * buf,int at,const char * fmt,...)316 sysfs_emit_at(char *buf, int at, const char *fmt, ...)
317 {
318 va_list args;
319 int i;
320
321 if (!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE) {
322 pr_warn("invalid sysfs_emit: buf:%p at:%d\n", buf, at);
323 return (0);
324 }
325
326 va_start(args, fmt);
327 i = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
328 va_end(args);
329
330 return (i);
331 }
332
333 #define sysfs_attr_init(attr) do {} while(0)
334
335 #endif /* _LINUXKPI_LINUX_SYSFS_H_ */
336