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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
31 #ifndef _LINUX_SYSFS_H_
32 #define _LINUX_SYSFS_H_
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
36 #include <sys/errno.h>
38 #include <linux/kobject.h>
41 ssize_t (*show)(struct kobject *, struct attribute *, char *);
42 ssize_t (*store)(struct kobject *, struct attribute *, const char *,
46 struct attribute_group {
48 mode_t (*is_visible)(struct kobject *,
49 struct attribute *, int);
50 struct attribute **attrs;
53 #define __ATTR(_name, _mode, _show, _store) { \
54 .attr = { .name = __stringify(_name), .mode = _mode }, \
55 .show = _show, .store = _store, \
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)
61 #define __ATTR_NULL { .attr = { .name = NULL } }
63 #define ATTRIBUTE_GROUPS(_name) \
64 static struct attribute_group _name##_group = { \
65 .name = __stringify(_name), \
66 .attrs = _name##_attrs, \
68 static const struct attribute_group *_name##_groups[] = { \
74 * Handle our generic '\0' terminated 'C' string.
76 * a variable string: point arg1 at it, arg2 is max length.
77 * a constant string: point arg1 at it, arg2 is zero.
81 sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
84 struct attribute *attr;
85 const struct sysfs_ops *ops;
91 attr = (struct attribute *)(intptr_t)arg2;
92 if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
94 buf = (char *)get_zeroed_page(GFP_KERNEL);
97 ops = kobj->ktype->sysfs_ops;
99 len = ops->show(kobj, attr, buf);
101 * It's valid to not have a 'show' so just return an
111 if (len >= PAGE_SIZE)
113 /* Trim trailing newline. */
118 /* Leave one trailing byte to append a newline. */
119 error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
120 if (error != 0 || req->newptr == NULL || ops->store == NULL)
122 len = strlcat(buf, "\n", PAGE_SIZE);
123 KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
124 len = ops->store(kobj, attr, buf, len);
128 free_page((unsigned long)buf);
134 sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
136 struct sysctl_oid *oid;
138 oid = SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
139 attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
140 (uintptr_t)attr, sysctl_handle_attr, "A", "");
149 sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
153 sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
157 sysfs_create_files(struct kobject *kobj, const struct attribute * const *attrs)
162 for (i = 0; attrs[i] && !error; i++)
163 error = sysfs_create_file(kobj, attrs[i]);
164 while (error && --i >= 0)
165 sysfs_remove_file(kobj, attrs[i]);
171 sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attrs)
175 for (i = 0; attrs[i]; i++)
176 sysfs_remove_file(kobj, attrs[i]);
180 sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
182 struct attribute **attr;
183 struct sysctl_oid *oidp;
185 /* Don't create the group node if grp->name is undefined. */
187 oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
188 OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
191 for (attr = grp->attrs; *attr != NULL; attr++) {
192 SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
193 (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
194 kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
201 sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
205 sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
209 sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps)
216 for (i = 0; grps[i] && !error; i++)
217 error = sysfs_create_group(kobj, grps[i]);
218 while (error && --i >= 0)
219 sysfs_remove_group(kobj, grps[i]);
225 sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **grps)
231 for (i = 0; grps[i]; i++)
232 sysfs_remove_group(kobj, grps[i]);
236 sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp)
239 /* Really expected behavior is to return failure if group exists. */
240 return (sysfs_create_group(kobj, grp));
244 sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp)
246 struct attribute **attr;
247 struct sysctl_oid *oidp;
249 SLIST_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp), oid_link) {
250 if (strcmp(oidp->oid_name, grp->name) != 0)
252 for (attr = grp->attrs; *attr != NULL; attr++) {
253 sysctl_remove_name(oidp, (*attr)->name, 1, 1);
259 sysfs_create_dir(struct kobject *kobj)
261 struct sysctl_oid *oid;
263 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
264 OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
274 sysfs_remove_dir(struct kobject *kobj)
277 if (kobj->oidp == NULL)
279 sysctl_remove_oid(kobj->oidp, 1, 1);
283 sysfs_streq(const char *s1, const char *s2)
290 if (l1 != 0 && s1[l1-1] == '\n')
292 if (l2 != 0 && s2[l2-1] == '\n')
295 return (l1 == l2 && strncmp(s1, s2, l1) == 0);
298 #define sysfs_attr_init(attr) do {} while(0)
300 #endif /* _LINUX_SYSFS_H_ */