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