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