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