1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*-
* Copyright (c) 2010 Isilon Systems, Inc.
* Copyright (c) 2010 iX Systems, Inc.
* Copyright (c) 2010 Panasas, Inc.
* Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LINUXKPI_LINUX_SYSFS_H_
#define _LINUXKPI_LINUX_SYSFS_H_
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/errno.h>
#include <linux/kobject.h>
#include <linux/stringify.h>
#include <linux/mm.h>
struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *, char *);
ssize_t (*store)(struct kobject *, struct attribute *, const char *,
size_t);
};
struct attribute_group {
const char *name;
mode_t (*is_visible)(struct kobject *,
struct attribute *, int);
struct attribute **attrs;
};
struct bin_attribute {
struct attribute attr;
size_t size;
ssize_t (*read)(struct linux_file *, struct kobject *,
struct bin_attribute *, char *, loff_t, size_t);
ssize_t (*write)(struct linux_file *, struct kobject *,
struct bin_attribute *, char *, loff_t, size_t);
};
#define __ATTR(_name, _mode, _show, _store) { \
.attr = { .name = __stringify(_name), .mode = _mode }, \
.show = _show, .store = _store, \
}
#define __ATTR_RO(_name) { \
.attr = { .name = __stringify(_name), .mode = 0444 }, \
.show = _name##_show, \
}
#define __ATTR_WO(_name) __ATTR(_name, 0200, NULL, _name##_store)
#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
#define __ATTR_NULL { .attr = { .name = NULL } }
#define ATTRIBUTE_GROUPS(_name) \
static struct attribute_group _name##_group = { \
.name = __stringify(_name), \
.attrs = _name##_attrs, \
}; \
static const struct attribute_group *_name##_groups[] = { \
&_name##_group, \
NULL, \
}
#define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
.attr = { .name = __stringify(_name), .mode = _mode }, \
.read = _read, .write = _write, .size = _size, \
}
#define __BIN_ATTR_RO(_name, _size) { \
.attr = { .name = __stringify(_name), .mode = 0444 }, \
.read = _name##_read, .size = _size, \
}
#define __BIN_ATTR_WO(_name, _size) { \
.attr = { .name = __stringify(_name), .mode = 0200 }, \
.write = _name##_write, .size = _size, \
}
#define __BIN_ATTR_WR(_name, _size) { \
.attr = { .name = __stringify(_name), .mode = 0644 }, \
.read = _name##_read, .write = _name##_write, .size = _size, \
}
#define BIN_ATTR(_name, _mode, _read, _write, _size) \
struct bin_attribute bin_attr_##_name = \
__BIN_ATTR(_name, _mode, _read, _write, _size);
#define BIN_ATTR_RO(_name, _size) \
struct bin_attribute bin_attr_##_name = \
__BIN_ATTR_RO(_name, _size);
#define BIN_ATTR_WO(_name, _size) \
struct bin_attribute bin_attr_##_name = \
__BIN_ATTR_WO(_name, _size);
#define BIN_ATTR_WR(_name, _size) \
struct bin_attribute bin_attr_##_name = \
__BIN_ATTR_WR(_name, _size);
/*
* Handle our generic '\0' terminated 'C' string.
* Two cases:
* a variable string: point arg1 at it, arg2 is max length.
* a constant string: point arg1 at it, arg2 is zero.
*/
static inline int
sysctl_handle_attr(SYSCTL_HANDLER_ARGS)
{
struct kobject *kobj;
struct attribute *attr;
const struct sysfs_ops *ops;
char *buf;
int error;
ssize_t len;
kobj = arg1;
attr = (struct attribute *)(intptr_t)arg2;
if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
return (ENODEV);
buf = (char *)get_zeroed_page(GFP_KERNEL);
if (buf == NULL)
return (ENOMEM);
ops = kobj->ktype->sysfs_ops;
if (ops->show) {
len = ops->show(kobj, attr, buf);
/*
* It's valid to not have a 'show' so just return an
* empty string.
*/
if (len < 0) {
error = -len;
if (error != EIO)
goto out;
buf[0] = '\0';
} else if (len) {
len--;
if (len >= PAGE_SIZE)
len = PAGE_SIZE - 1;
/* Trim trailing newline. */
buf[len] = '\0';
}
}
/* Leave one trailing byte to append a newline. */
error = sysctl_handle_string(oidp, buf, PAGE_SIZE - 1, req);
if (error != 0 || req->newptr == NULL || ops->store == NULL)
goto out;
len = strlcat(buf, "\n", PAGE_SIZE);
KASSERT(len < PAGE_SIZE, ("new attribute truncated"));
len = ops->store(kobj, attr, buf, len);
if (len < 0)
error = -len;
out:
free_page((unsigned long)buf);
return (error);
}
static inline int
sysfs_create_file(struct kobject *kobj, const struct attribute *attr)
{
struct sysctl_oid *oid;
oid = SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
attr->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, kobj,
(uintptr_t)attr, sysctl_handle_attr, "A", "");
if (!oid) {
return (-ENOMEM);
}
return (0);
}
static inline struct kobject *
__sysfs_lookup_group(struct kobject *kobj, const char *group)
{
int found;
struct sysctl_oid *group_oidp;
struct kobject *group_kobj;
found = 0;
if (group != NULL) {
SYSCTL_FOREACH(group_oidp, SYSCTL_CHILDREN(kobj->oidp)) {
if (strcmp(group_oidp->oid_name, group) != 0)
continue;
found = 1;
break;
}
} else {
found = 1;
group_oidp = kobj->oidp;
}
if (!found)
return (NULL);
group_kobj = group_oidp->oid_arg1;
return (group_kobj);
}
static inline int
sysfs_add_file_to_group(struct kobject *kobj,
const struct attribute *attr, const char *group)
{
int ret;
struct kobject *group_kobj;
group_kobj = __sysfs_lookup_group(kobj, group);
if (group_kobj == NULL)
return (-ENOENT);
ret = sysfs_create_file(group_kobj, attr);
return (ret);
}
static inline void
sysfs_remove_file(struct kobject *kobj, const struct attribute *attr)
{
if (kobj->oidp)
sysctl_remove_name(kobj->oidp, attr->name, 1, 1);
}
static inline void
sysfs_remove_file_from_group(struct kobject *kobj,
const struct attribute *attr, const char *group)
{
struct kobject *group_kobj;
group_kobj = __sysfs_lookup_group(kobj, group);
if (group_kobj == NULL)
return;
sysfs_remove_file(group_kobj, attr);
}
static inline int
sysctl_handle_bin_attr(SYSCTL_HANDLER_ARGS)
{
struct kobject *kobj;
struct bin_attribute *attr;
char *buf;
int error;
ssize_t len;
kobj = arg1;
attr = (struct bin_attribute *)(intptr_t)arg2;
if (kobj->ktype == NULL || kobj->ktype->sysfs_ops == NULL)
return (ENODEV);
buf = (char *)get_zeroed_page(GFP_KERNEL);
if (buf == NULL)
return (ENOMEM);
if (attr->read) {
len = attr->read(
NULL, /* <-- struct file, unimplemented */
kobj, attr, buf, req->oldidx, PAGE_SIZE);
if (len < 0) {
error = -len;
if (error != EIO)
goto out;
}
}
error = sysctl_handle_opaque(oidp, buf, PAGE_SIZE, req);
if (error != 0 || req->newptr == NULL || attr->write == NULL)
goto out;
len = attr->write(
NULL, /* <-- struct file, unimplemented */
kobj, attr, buf, req->newidx, req->newlen);
if (len < 0)
error = -len;
out:
free_page((unsigned long)buf);
return (error);
}
static inline int
sysfs_create_bin_file(struct kobject *kobj, const struct bin_attribute *attr)
{
struct sysctl_oid *oid;
int ctlflags;
ctlflags = CTLTYPE_OPAQUE | CTLFLAG_MPSAFE;
if (attr->attr.mode & (S_IRUSR | S_IWUSR))
ctlflags |= CTLFLAG_RW;
else if (attr->attr.mode & S_IRUSR)
ctlflags |= CTLFLAG_RD;
else if (attr->attr.mode & S_IWUSR)
ctlflags |= CTLFLAG_WR;
oid = SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(kobj->oidp), OID_AUTO,
attr->attr.name, ctlflags, kobj,
(uintptr_t)attr, sysctl_handle_bin_attr, "", "");
if (oid == NULL)
return (-ENOMEM);
return (0);
}
static inline void
sysfs_remove_bin_file(struct kobject *kobj, const struct bin_attribute *attr)
{
if (kobj->oidp)
sysctl_remove_name(kobj->oidp, attr->attr.name, 1, 1);
}
static inline int
sysfs_create_link(struct kobject *kobj __unused,
struct kobject *target __unused, const char *name __unused)
{
/* TODO */
return (0);
}
static inline void
sysfs_remove_link(struct kobject *kobj, const char *name)
{
/* TODO (along with sysfs_create_link) */
}
static inline int
sysfs_create_files(struct kobject *kobj, const struct attribute * const *attrs)
{
int error = 0;
int i;
for (i = 0; attrs[i] && !error; i++)
error = sysfs_create_file(kobj, attrs[i]);
while (error && --i >= 0)
sysfs_remove_file(kobj, attrs[i]);
return (error);
}
static inline void
sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attrs)
{
int i;
for (i = 0; attrs[i]; i++)
sysfs_remove_file(kobj, attrs[i]);
}
static inline int
sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp)
{
struct attribute **attr;
struct sysctl_oid *oidp;
/* Don't create the group node if grp->name is undefined. */
if (grp->name)
oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp),
OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name);
else
oidp = kobj->oidp;
for (attr = grp->attrs; *attr != NULL; attr++) {
SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO,
(*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE,
kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", "");
}
return (0);
}
static inline void
sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp)
{
if (kobj->oidp)
sysctl_remove_name(kobj->oidp, grp->name, 1, 1);
}
static inline int
sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps)
{
int error = 0;
int i;
if (grps == NULL)
goto done;
for (i = 0; grps[i] && !error; i++)
error = sysfs_create_group(kobj, grps[i]);
while (error && --i >= 0)
sysfs_remove_group(kobj, grps[i]);
done:
return (error);
}
static inline void
sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **grps)
{
int i;
if (grps == NULL)
return;
for (i = 0; grps[i]; i++)
sysfs_remove_group(kobj, grps[i]);
}
static inline int
sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp)
{
/* Really expected behavior is to return failure if group exists. */
return (sysfs_create_group(kobj, grp));
}
static inline void
sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp)
{
struct attribute **attr;
struct sysctl_oid *oidp;
SYSCTL_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp)) {
if (strcmp(oidp->oid_name, grp->name) != 0)
continue;
for (attr = grp->attrs; *attr != NULL; attr++) {
sysctl_remove_name(oidp, (*attr)->name, 1, 1);
}
}
}
static inline int
sysfs_create_dir(struct kobject *kobj)
{
struct sysctl_oid *oid;
oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->parent->oidp),
OID_AUTO, kobj->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, kobj->name);
if (!oid) {
return (-ENOMEM);
}
kobj->oidp = oid;
return (0);
}
static inline void
sysfs_remove_dir(struct kobject *kobj)
{
if (kobj->oidp == NULL)
return;
sysctl_remove_oid(kobj->oidp, 1, 1);
}
static inline bool
sysfs_streq(const char *s1, const char *s2)
{
int l1, l2;
l1 = strlen(s1);
l2 = strlen(s2);
if (l1 != 0 && s1[l1-1] == '\n')
l1--;
if (l2 != 0 && s2[l2-1] == '\n')
l2--;
return (l1 == l2 && strncmp(s1, s2, l1) == 0);
}
static inline int
sysfs_emit(char *buf, const char *fmt, ...)
{
va_list args;
int i;
if (!buf || offset_in_page(buf)) {
pr_warn("invalid sysfs_emit: buf:%p\n", buf);
return (0);
}
va_start(args, fmt);
i = vscnprintf(buf, PAGE_SIZE, fmt, args);
va_end(args);
return (i);
}
static inline int
sysfs_emit_at(char *buf, int at, const char *fmt, ...)
{
va_list args;
int i;
if (!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE) {
pr_warn("invalid sysfs_emit: buf:%p at:%d\n", buf, at);
return (0);
}
va_start(args, fmt);
i = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
va_end(args);
return (i);
}
static inline int
_sysfs_match_string(const char * const *a, size_t l, const char *s)
{
const char *p;
int i;
for (i = 0; i < l; i++) {
p = a[i];
if (p == NULL)
break;
if (sysfs_streq(p, s))
return (i);
}
return (-ENOENT);
}
#define sysfs_match_string(a, s) _sysfs_match_string(a, ARRAY_SIZE(a), s)
#define sysfs_attr_init(attr) do {} while(0)
#endif /* _LINUXKPI_LINUX_SYSFS_H_ */
|