sysfs.h revision 219820
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef	_LINUX_SYSFS_H_
30#define	_LINUX_SYSFS_H_
31
32#include <sys/sysctl.h>
33
34struct attribute {
35	const char 	*name;
36	struct module	*owner;
37	mode_t		mode;
38};
39
40struct 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
46struct 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
58#define	__ATTR_RO(_name) {						\
59	.attr = { .name = __stringify(_name), .mode = 0444 },		\
60	.show   = _name##_show,						\
61}
62
63#define	__ATTR_NULL	{ .attr = { .name = NULL } }
64
65/*
66 * Handle our generic '\0' terminated 'C' string.
67 * Two cases:
68 *      a variable string:  point arg1 at it, arg2 is max length.
69 *      a constant string:  point arg1 at it, arg2 is zero.
70 */
71
72static inline int
73sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
74{
75	struct kobject *kobj;
76	struct attribute *attr;
77	const struct sysfs_ops *ops;
78	void *buf;
79	int error;
80	ssize_t len;
81
82	kobj = arg1;
83	attr = (struct attribute *)arg2;
84	buf = (void *)get_zeroed_page(GFP_KERNEL);
85	len = 1;	/* Copy out a NULL byte at least. */
86	if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
87		return (ENODEV);
88	ops = kobj->ktype->sysfs_ops;
89	if (buf == NULL)
90		return (ENOMEM);
91	if (ops->show) {
92		len = ops->show(kobj, attr, buf);
93		/*
94		 * It's valid not to have a 'show' so we just return 1 byte
95		 * of NULL.
96	 	 */
97		if (len < 0) {
98			error = -len;
99			len = 1;
100			if (error != EIO)
101				goto out;
102		}
103	}
104	error = SYSCTL_OUT(req, buf, len);
105	if (error || !req->newptr || ops->store == NULL)
106		goto out;
107	error = SYSCTL_IN(req, buf, PAGE_SIZE);
108	if (error)
109		goto out;
110	len = ops->store(kobj, attr, buf, req->newlen);
111	if (len < 0)
112		error = -len;
113out:
114	free_page((unsigned long)buf);
115
116	return (error);
117}
118
119static inline int
120sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
121{
122
123	sysctl_add_oid(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
124	    attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
125	    (uintptr_t)attr, sysctl_handle_attr, "A", "");
126
127	return (0);
128}
129
130static inline void
131sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
132{
133
134	if (kobj->oidp)
135		sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
136}
137
138static inline void
139sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
140{
141
142	if (kobj->oidp)
143		sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
144}
145
146static inline int
147sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
148{
149	struct attribute **attr;
150	struct sysctl_oid *oidp;
151
152	oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
153	    OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
154	for (attr = grp->attrs; *attr != NULL; attr++) {
155		sysctl_add_oid(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
156		    (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
157		    kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
158	}
159
160	return (0);
161}
162
163static inline int
164sysfs_create_dir(struct kobject *kobj)
165{
166
167	kobj->oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
168	    OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
169
170        return (0);
171}
172
173static inline void
174sysfs_remove_dir(struct kobject *kobj)
175{
176
177	if (kobj->oidp == NULL)
178		return;
179	sysctl_remove_oid(kobj->oidp, 1, 1);
180}
181
182#endif	/* _LINUX_SYSFS_H_ */
183