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: stable/11/sys/compat/linuxkpi/common/include/linux/sysfs.h 354616 2019-11-11 15:28:27Z hselasky $ 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 40struct 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 46struct 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#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) 60 61#define __ATTR_NULL { .attr = { .name = NULL } } 62 63#define ATTRIBUTE_GROUPS(_name) \ 64 static struct attribute_group _name##_group = { \ 65 .name = __stringify(_name), \ 66 .attrs = _name##_attrs, \ 67 }; \ 68 static const struct attribute_group *_name##_groups[] = { \ 69 &_name##_group, \ 70 NULL, \ 71 } 72 73/* 74 * Handle our generic '\0' terminated 'C' string. 75 * Two cases: 76 * a variable string: point arg1 at it, arg2 is max length. 77 * a constant string: point arg1 at it, arg2 is zero. 78 */ 79 80static inline int 81sysctl_handle_attr(SYSCTL_HANDLER_ARGS) 82{ 83 struct kobject *kobj; 84 struct attribute *attr; 85 const struct sysfs_ops *ops; 86 char *buf; 87 int error; 88 ssize_t len; 89 90 kobj = arg1; 91 attr = (struct attribute *)(intptr_t)arg2; 92 if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL) 93 return (ENODEV); 94 buf = (char *)get_zeroed_page(GFP_KERNEL); 95 if (buf == NULL) 96 return (ENOMEM); 97 ops = kobj->ktype->sysfs_ops; 98 if (ops->show) { 99 len = ops->show(kobj, attr, buf); 100 /* 101 * It's valid to not have a 'show' so just return an 102 * empty string. 103 */ 104 if (len < 0) { 105 error = -len; 106 if (error != EIO) 107 goto out; 108 buf[0] = '\0'; 109 } else if (len) { 110 len--; 111 if (len >= PAGE_SIZE) 112 len = PAGE_SIZE - 1; 113 /* Trim trailing newline. */ 114 buf[len] = '\0'; 115 } 116 } 117 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) 121 goto out; 122 len = strlcat(buf, "\n", PAGE_SIZE); 123 KASSERT(len < PAGE_SIZE, ("new attribute truncated")); 124 len = ops->store(kobj, attr, buf, len); 125 if (len < 0) 126 error = -len; 127out: 128 free_page((unsigned long)buf); 129 130 return (error); 131} 132 133static inline int 134sysfs_create_file(struct kobject *kobj, const struct attribute *attr) 135{ 136 struct sysctl_oid *oid; 137 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", ""); 141 if (!oid) { 142 return (-ENOMEM); 143 } 144 145 return (0); 146} 147 148static inline void 149sysfs_remove_file(struct kobject *kobj, const struct attribute *attr) 150{ 151 152 if (kobj->oidp) 153 sysctl_remove_name(kobj->oidp, attr->name, 1, 1); 154} 155 156static inline int 157sysfs_create_files(struct kobject *kobj, const struct attribute * const *attrs) 158{ 159 int error = 0; 160 int i; 161 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]); 166 167 return (error); 168} 169 170static inline void 171sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attrs) 172{ 173 int i; 174 175 for (i = 0; attrs[i]; i++) 176 sysfs_remove_file(kobj, attrs[i]); 177} 178 179static inline int 180sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp) 181{ 182 struct attribute **attr; 183 struct sysctl_oid *oidp; 184 185 /* Don't create the group node if grp->name is undefined. */ 186 if (grp->name) 187 oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp), 188 OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name); 189 else 190 oidp = kobj->oidp; 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", ""); 195 } 196 197 return (0); 198} 199 200static inline void 201sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp) 202{ 203 204 if (kobj->oidp) 205 sysctl_remove_name(kobj->oidp, grp->name, 1, 1); 206} 207 208static inline int 209sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps) 210{ 211 int error = 0; 212 int i; 213 214 if (grps == NULL) 215 goto done; 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]); 220done: 221 return (error); 222} 223 224static inline void 225sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **grps) 226{ 227 int i; 228 229 if (grps == NULL) 230 return; 231 for (i = 0; grps[i]; i++) 232 sysfs_remove_group(kobj, grps[i]); 233} 234 235static inline int 236sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp) 237{ 238 239 /* Really expected behavior is to return failure if group exists. */ 240 return (sysfs_create_group(kobj, grp)); 241} 242 243static inline void 244sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp) 245{ 246 struct attribute **attr; 247 struct sysctl_oid *oidp; 248 249 SLIST_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp), oid_link) { 250 if (strcmp(oidp->oid_name, grp->name) != 0) 251 continue; 252 for (attr = grp->attrs; *attr != NULL; attr++) { 253 sysctl_remove_name(oidp, (*attr)->name, 1, 1); 254 } 255 } 256} 257 258static inline int 259sysfs_create_dir(struct kobject *kobj) 260{ 261 struct sysctl_oid *oid; 262 263 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp), 264 OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name); 265 if (!oid) { 266 return (-ENOMEM); 267 } 268 kobj->oidp = oid; 269 270 return (0); 271} 272 273static inline void 274sysfs_remove_dir(struct kobject *kobj) 275{ 276 277 if (kobj->oidp == NULL) 278 return; 279 sysctl_remove_oid(kobj->oidp, 1, 1); 280} 281 282static inline bool 283sysfs_streq(const char *s1, const char *s2) 284{ 285 int l1, l2; 286 287 l1 = strlen(s1); 288 l2 = strlen(s2); 289 290 if (l1 != 0 && s1[l1-1] == '\n') 291 l1--; 292 if (l2 != 0 && s2[l2-1] == '\n') 293 l2--; 294 295 return (l1 == l2 && strncmp(s1, s2, l1) == 0); 296} 297 298#define sysfs_attr_init(attr) do {} while(0) 299 300#endif /* _LINUX_SYSFS_H_ */ 301