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
30 #ifndef _LINUX_SYSFS_H_
31 #define _LINUX_SYSFS_H_
32
33 #include <sys/sysctl.h>
34
35 struct attribute {
36 const char *name;
37 struct module *owner;
38 mode_t mode;
39 };
40
41 struct sysfs_ops {
42 ssize_t (*show)(struct kobject *, struct attribute *, char *);
43 ssize_t (*store)(struct kobject *, struct attribute *, const char *,
44 size_t);
45 };
46
47 struct attribute_group {
48 const char *name;
49 mode_t (*is_visible)(struct kobject *,
50 struct attribute *, int);
51 struct attribute **attrs;
52 };
53
54 #define __ATTR(_name, _mode, _show, _store) { \
55 .attr = { .name = __stringify(_name), .mode = _mode }, \
56 .show = _show, .store = _store, \
57 }
58
59 #define __ATTR_RO(_name) { \
60 .attr = { .name = __stringify(_name), .mode = 0444 }, \
61 .show = _name##_show, \
62 }
63
64 #define __ATTR_NULL { .attr = { .name = NULL } }
65
66 /*
67 * Handle our generic '\0' terminated 'C' string.
68 * Two cases:
69 * a variable string: point arg1 at it, arg2 is max length.
70 * a constant string: point arg1 at it, arg2 is zero.
71 */
72
73 static inline int
sysctl_handle_attr(SYSCTL_HANDLER_ARGS)74 sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
75 {
76 struct kobject *kobj;
77 struct attribute *attr;
78 const struct sysfs_ops *ops;
79 char *buf;
80 int error;
81 ssize_t len;
82
83 kobj = arg1;
84 attr = (struct attribute *)arg2;
85 if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
86 return (ENODEV);
87 buf = (char *)get_zeroed_page(GFP_KERNEL);
88 if (buf == NULL)
89 return (ENOMEM);
90 ops = kobj->ktype->sysfs_ops;
91 if (ops->show) {
92 len = ops->show(kobj, attr, buf);
93 /*
94 * It's valid to not have a 'show' so just return an
95 * empty string.
96 */
97 if (len < 0) {
98 error = -len;
99 if (error != EIO)
100 goto out;
101 buf[0] = '\0';
102 } else if (len) {
103 len--;
104 if (len >= PAGE_SIZE)
105 len = PAGE_SIZE - 1;
106 /* Trim trailing newline. */
107 buf[len] = '\0';
108 }
109
110 /* Trim trailing newline. */
111 len--;
112 buf[len] = '\0';
113 }
114
115 /* Leave one trailing byte to append a newline. */
116 error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
117 if (error != 0 || req->newptr == NULL || ops->store == NULL)
118 goto out;
119 len = strlcat(buf, "\n", PAGE_SIZE);
120 KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
121 len = ops->store(kobj, attr, buf, len);
122 if (len < 0)
123 error = -len;
124 out:
125 free_page((unsigned long)buf);
126
127 return (error);
128 }
129
130 static inline int
sysfs_create_file(struct kobject * kobj,const struct attribute * attr)131 sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
132 {
133
134 sysctl_add_oid(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
135 attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
136 (uintptr_t)attr, sysctl_handle_attr, "A", "");
137
138 return (0);
139 }
140
141 static inline void
sysfs_remove_file(struct kobject * kobj,const struct attribute * attr)142 sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
143 {
144
145 if (kobj->oidp)
146 sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
147 }
148
149 static inline void
sysfs_remove_group(struct kobject * kobj,const struct attribute_group * grp)150 sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
151 {
152
153 if (kobj->oidp)
154 sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
155 }
156
157 static inline int
sysfs_create_group(struct kobject * kobj,const struct attribute_group * grp)158 sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
159 {
160 struct attribute **attr;
161 struct sysctl_oid *oidp;
162
163 oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
164 OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
165 for (attr = grp->attrs; *attr != NULL; attr++) {
166 sysctl_add_oid(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
167 (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
168 kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
169 }
170
171 return (0);
172 }
173
174 static inline int
sysfs_create_dir(struct kobject * kobj)175 sysfs_create_dir(struct kobject *kobj)
176 {
177
178 kobj->oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
179 OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
180
181 return (0);
182 }
183
184 static inline void
sysfs_remove_dir(struct kobject * kobj)185 sysfs_remove_dir(struct kobject *kobj)
186 {
187
188 if (kobj->oidp == NULL)
189 return;
190 sysctl_remove_oid(kobj->oidp, 1, 1);
191 }
192
193 #define sysfs_attr_init(attr) do {} while(0)
194
195 #endif /* _LINUX_SYSFS_H_ */
196