sysfs.h revision 270710
1169691Skan/*-
2169691Skan * Copyright (c) 2010 Isilon Systems, Inc.
3169691Skan * Copyright (c) 2010 iX Systems, Inc.
4169691Skan * Copyright (c) 2010 Panasas, Inc.
5169691Skan * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6169691Skan * All rights reserved.
7169691Skan *
8169691Skan * Redistribution and use in source and binary forms, with or without
9169691Skan * modification, are permitted provided that the following conditions
10169691Skan * are met:
11169691Skan * 1. Redistributions of source code must retain the above copyright
12169691Skan *    notice unmodified, this list of conditions, and the following
13169691Skan *    disclaimer.
14169691Skan * 2. Redistributions in binary form must reproduce the above copyright
15169691Skan *    notice, this list of conditions and the following disclaimer in the
16169691Skan *    documentation and/or other materials provided with the distribution.
17169691Skan *
18169691Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19169691Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20169691Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21169691Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22169691Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23169691Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24169691Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25169691Skan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26169691Skan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27169691Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28169691Skan */
29169691Skan
30169691Skan#ifndef	_LINUX_SYSFS_H_
31169691Skan#define	_LINUX_SYSFS_H_
32169691Skan
33169691Skan#include <sys/sysctl.h>
34169691Skan
35169691Skanstruct attribute {
36169691Skan	const char 	*name;
37169691Skan	struct module	*owner;
38169691Skan	mode_t		mode;
39169691Skan};
40169691Skan
41169691Skanstruct sysfs_ops {
42169691Skan	ssize_t (*show)(struct kobject *, struct attribute *, char *);
43169691Skan	ssize_t (*store)(struct kobject *, struct attribute *, const char *,
44169691Skan	    size_t);
45169691Skan};
46169691Skan
47169691Skanstruct attribute_group {
48169691Skan	const char		*name;
49169691Skan	mode_t                  (*is_visible)(struct kobject *,
50169691Skan				    struct attribute *, int);
51169691Skan	struct attribute	**attrs;
52169691Skan};
53169691Skan
54169691Skan#define	__ATTR(_name, _mode, _show, _store) {				\
55169691Skan	.attr = { .name = __stringify(_name), .mode = _mode },		\
56169691Skan        .show = _show, .store  = _store,				\
57169691Skan}
58169691Skan
59169691Skan#define	__ATTR_RO(_name) {						\
60169691Skan	.attr = { .name = __stringify(_name), .mode = 0444 },		\
61169691Skan	.show   = _name##_show,						\
62169691Skan}
63169691Skan
64169691Skan#define	__ATTR_NULL	{ .attr = { .name = NULL } }
65169691Skan
66169691Skan/*
67169691Skan * Handle our generic '\0' terminated 'C' string.
68169691Skan * Two cases:
69169691Skan *      a variable string:  point arg1 at it, arg2 is max length.
70169691Skan *      a constant string:  point arg1 at it, arg2 is zero.
71169691Skan */
72169691Skan
73169691Skanstatic inline int
74169691Skansysctl_handle_attr(SYSCTL_HANDLER_ARGS)
75169691Skan{
76169691Skan	struct kobject *kobj;
77169691Skan	struct attribute *attr;
78169691Skan	const struct sysfs_ops *ops;
79169691Skan	char *buf;
80169691Skan	int error;
81169691Skan	ssize_t len;
82169691Skan
83169691Skan	kobj = arg1;
84169691Skan	attr = (struct attribute *)arg2;
85169691Skan	if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
86169691Skan		return (ENODEV);
87169691Skan	buf = (char *)get_zeroed_page(GFP_KERNEL);
88169691Skan	if (buf == NULL)
89169691Skan		return (ENOMEM);
90169691Skan	ops = kobj->ktype->sysfs_ops;
91169691Skan	if (ops->show) {
92169691Skan		len = ops->show(kobj, attr, buf);
93169691Skan		/*
94169691Skan		 * It's valid to not have a 'show' so just return an
95169691Skan		 * empty string.
96169691Skan	 	 */
97169691Skan		if (len < 0) {
98169691Skan			error = -len;
99169691Skan			if (error != EIO)
100169691Skan				goto out;
101169691Skan			buf[0] = '\0';
102169691Skan		} else if (len) {
103169691Skan			len--;
104169691Skan			if (len >= PAGE_SIZE)
105169691Skan				len = PAGE_SIZE - 1;
106169691Skan			/* Trim trailing newline. */
107169691Skan			buf[len] = '\0';
108169691Skan		}
109169691Skan	}
110169691Skan
111169691Skan	/* Leave one trailing byte to append a newline. */
112169691Skan	error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
113169691Skan	if (error != 0 || req->newptr == NULL || ops->store == NULL)
114169691Skan		goto out;
115169691Skan	len = strlcat(buf, "\n", PAGE_SIZE);
116169691Skan	KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
117169691Skan	len = ops->store(kobj, attr, buf, len);
118169691Skan	if (len < 0)
119169691Skan		error = -len;
120169691Skanout:
121169691Skan	free_page((unsigned long)buf);
122169691Skan
123169691Skan	return (error);
124169691Skan}
125169691Skan
126169691Skanstatic inline int
127169691Skansysfs_create_file(struct kobject *kobj, const struct attribute *attr)
128169691Skan{
129169691Skan
130169691Skan	sysctl_add_oid(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
131169691Skan	    attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
132169691Skan	    (uintptr_t)attr, sysctl_handle_attr, "A", "");
133169691Skan
134169691Skan	return (0);
135169691Skan}
136169691Skan
137169691Skanstatic inline void
138169691Skansysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
139169691Skan{
140169691Skan
141169691Skan	if (kobj->oidp)
142169691Skan		sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
143169691Skan}
144169691Skan
145169691Skanstatic inline void
146169691Skansysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
147169691Skan{
148169691Skan
149169691Skan	if (kobj->oidp)
150169691Skan		sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
151169691Skan}
152169691Skan
153169691Skanstatic inline int
154169691Skansysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
155169691Skan{
156169691Skan	struct attribute **attr;
157169691Skan	struct sysctl_oid *oidp;
158169691Skan
159169691Skan	oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
160169691Skan	    OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
161169691Skan	for (attr = grp->attrs; *attr != NULL; attr++) {
162169691Skan		sysctl_add_oid(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
163169691Skan		    (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
164169691Skan		    kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
165169691Skan	}
166169691Skan
167169691Skan	return (0);
168169691Skan}
169169691Skan
170169691Skanstatic inline int
171169691Skansysfs_create_dir(struct kobject *kobj)
172169691Skan{
173169691Skan
174169691Skan	kobj->oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
175169691Skan	    OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
176169691Skan
177169691Skan        return (0);
178169691Skan}
179169691Skan
180169691Skanstatic inline void
181169691Skansysfs_remove_dir(struct kobject *kobj)
182{
183
184	if (kobj->oidp == NULL)
185		return;
186	sysctl_remove_oid(kobj->oidp, 1, 1);
187}
188
189#define sysfs_attr_init(attr) do {} while(0)
190
191#endif	/* _LINUX_SYSFS_H_ */
192