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 * $FreeBSD$
30 */
31 #ifndef _LINUXKPI_LINUX_SYSFS_H_
32 #define _LINUXKPI_LINUX_SYSFS_H_
33
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
36 #include <sys/errno.h>
37
38 #include <linux/kobject.h>
39 #include <linux/stringify.h>
40 #include <linux/mm.h>
41
42 struct sysfs_ops {
43 ssize_t (*show)(struct kobject *, struct attribute *, char *);
44 ssize_t (*store)(struct kobject *, struct attribute *, const char *,
45 size_t);
46 };
47
48 struct attribute_group {
49 const char *name;
50 mode_t (*is_visible)(struct kobject *,
51 struct attribute *, int);
52 struct attribute **attrs;
53 };
54
55 #define __ATTR(_name, _mode, _show, _store) { \
56 .attr = { .name = __stringify(_name), .mode = _mode }, \
57 .show = _show, .store = _store, \
58 }
59 #define __ATTR_RO(_name) __ATTR(_name, 0444, _name##_show, NULL)
60 #define __ATTR_WO(_name) __ATTR(_name, 0200, NULL, _name##_store)
61 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
62 #define __ATTR_NULL { .attr = { .name = NULL } }
63
64 #define ATTRIBUTE_GROUPS(_name) \
65 static struct attribute_group _name##_group = { \
66 .name = __stringify(_name), \
67 .attrs = _name##_attrs, \
68 }; \
69 static const struct attribute_group *_name##_groups[] = { \
70 &_name##_group, \
71 NULL, \
72 }
73
74 /*
75 * Handle our generic '\0' terminated 'C' string.
76 * Two cases:
77 * a variable string: point arg1 at it, arg2 is max length.
78 * a constant string: point arg1 at it, arg2 is zero.
79 */
80
81 static inline int
82 sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
83 {
84 struct kobject *kobj;
85 struct attribute *attr;
86 const struct sysfs_ops *ops;
87 char *buf;
88 int error;
89 ssize_t len;
90
91 kobj = arg1;
92 attr = (struct attribute *)(intptr_t)arg2;
93 if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
94 return (ENODEV);
95 buf = (char *)get_zeroed_page(GFP_KERNEL);
96 if (buf == NULL)
97 return (ENOMEM);
98 ops = kobj->ktype->sysfs_ops;
99 if (ops->show) {
100 len = ops->show(kobj, attr, buf);
101 /*
102 * It's valid to not have a 'show' so just return an
103 * empty string.
104 */
105 if (len < 0) {
106 error = -len;
107 if (error != EIO)
108 goto out;
109 buf[0] = '\0';
110 } else if (len) {
111 len--;
112 if (len >= PAGE_SIZE)
113 len = PAGE_SIZE - 1;
114 /* Trim trailing newline. */
115 buf[len] = '\0';
116 }
117 }
118
119 /* Leave one trailing byte to append a newline. */
120 error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
121 if (error != 0 || req->newptr == NULL || ops->store == NULL)
122 goto out;
123 len = strlcat(buf, "\n", PAGE_SIZE);
124 KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
125 len = ops->store(kobj, attr, buf, len);
126 if (len < 0)
127 error = -len;
128 out:
129 free_page((unsigned long)buf);
130
131 return (error);
132 }
133
134 static inline int
135 sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
136 {
137 struct sysctl_oid *oid;
138
139 oid = SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
140 attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
141 (uintptr_t)attr, sysctl_handle_attr, "A", "");
142 if (!oid) {
143 return (-ENOMEM);
144 }
145
146 return (0);
147 }
148
149 static inline void
150 sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
151 {
152
153 if (kobj->oidp)
154 sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
155 }
156
157 static inline int
158 sysfs_create_files(struct kobject *kobj, const struct attribute * const *attrs)
159 {
160 int error = 0;
161 int i;
162
163 for (i = 0; attrs[i] && !error; i++)
164 error = sysfs_create_file(kobj, attrs[i]);
165 while (error && --i >= 0)
166 sysfs_remove_file(kobj, attrs[i]);
167
168 return (error);
169 }
170
171 static inline void
172 sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attrs)
173 {
174 int i;
175
176 for (i = 0; attrs[i]; i++)
177 sysfs_remove_file(kobj, attrs[i]);
178 }
179
180 static inline int
181 sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
182 {
183 struct attribute **attr;
184 struct sysctl_oid *oidp;
185
186 /* Don't create the group node if grp->name is undefined. */
187 if (grp->name)
188 oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
189 OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
190 else
191 oidp = kobj->oidp;
192 for (attr = grp->attrs; *attr != NULL; attr++) {
193 SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
194 (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
195 kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
196 }
197
198 return (0);
199 }
200
201 static inline void
202 sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
203 {
204
205 if (kobj->oidp)
206 sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
207 }
208
209 static inline int
210 sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps)
211 {
212 int error = 0;
213 int i;
214
215 if (grps == NULL)
216 goto done;
217 for (i = 0; grps[i] && !error; i++)
218 error = sysfs_create_group(kobj, grps[i]);
219 while (error && --i >= 0)
220 sysfs_remove_group(kobj, grps[i]);
221 done:
222 return (error);
223 }
224
225 static inline void
226 sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **grps)
227 {
228 int i;
229
230 if (grps == NULL)
231 return;
232 for (i = 0; grps[i]; i++)
233 sysfs_remove_group(kobj, grps[i]);
234 }
235
236 static inline int
237 sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp)
238 {
239
240 /* Really expected behavior is to return failure if group exists. */
241 return (sysfs_create_group(kobj, grp));
242 }
243
244 static inline void
245 sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp)
246 {
247 struct attribute **attr;
248 struct sysctl_oid *oidp;
249
250 SYSCTL_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp)) {
251 if (strcmp(oidp->oid_name, grp->name) != 0)
252 continue;
253 for (attr = grp->attrs; *attr != NULL; attr++) {
254 sysctl_remove_name(oidp, (*attr)->name, 1, 1);
255 }
256 }
257 }
258
259 static inline int
260 sysfs_create_dir(struct kobject *kobj)
261 {
262 struct sysctl_oid *oid;
263
264 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
265 OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
266 if (!oid) {
267 return (-ENOMEM);
268 }
269 kobj->oidp = oid;
270
271 return (0);
272 }
273
274 static inline void
275 sysfs_remove_dir(struct kobject *kobj)
276 {
277
278 if (kobj->oidp == NULL)
279 return;
280 sysctl_remove_oid(kobj->oidp, 1, 1);
281 }
282
283 static inline bool
284 sysfs_streq(const char *s1, const char *s2)
285 {
286 int l1, l2;
287
288 l1 = strlen(s1);
289 l2 = strlen(s2);
290
291 if (l1 != 0 && s1[l1-1] == '\n')
292 l1--;
293 if (l2 != 0 && s2[l2-1] == '\n')
294 l2--;
295
296 return (l1 == l2 && strncmp(s1, s2, l1) == 0);
297 }
298
299 static inline int
300 sysfs_emit(char *buf, const char *fmt, ...)
301 {
302 va_list args;
303 int i;
304
305 if (!buf || offset_in_page(buf)) {
306 pr_warn("invalid sysfs_emit: buf:%p\n", buf);
307 return (0);
308 }
309
310 va_start(args, fmt);
311 i = vscnprintf(buf, PAGE_SIZE, fmt, args);
312 va_end(args);
313
314 return (i);
315 }
316
317 static inline int
318 sysfs_emit_at(char *buf, int at, const char *fmt, ...)
319 {
320 va_list args;
321 int i;
322
323 if (!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE) {
324 pr_warn("invalid sysfs_emit: buf:%p at:%d\n", buf, at);
325 return (0);
326 }
327
328 va_start(args, fmt);
329 i = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
330 va_end(args);
331
332 return (i);
333 }
334
335 #define sysfs_attr_init(attr) do {} while(0)
336
337 #endif /* _LINUXKPI_LINUX_SYSFS_H_ */
Cache object: a6351335a4c99f2ac8ee8c4e11e4b28c
|