Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/compat/lindebugfs/lindebugfs.c
106139 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2016-2018, Matthew Macy <[email protected]>
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*
27
*/
28
29
#include <sys/param.h>
30
#include <sys/systm.h>
31
#include <sys/queue.h>
32
#include <sys/blist.h>
33
#include <sys/conf.h>
34
#include <sys/exec.h>
35
#include <sys/filedesc.h>
36
#include <sys/kernel.h>
37
#include <sys/linker.h>
38
#include <sys/malloc.h>
39
#include <sys/mount.h>
40
#include <sys/mutex.h>
41
#include <sys/proc.h>
42
#include <sys/resourcevar.h>
43
#include <sys/sbuf.h>
44
#include <sys/smp.h>
45
#include <sys/socket.h>
46
#include <sys/vnode.h>
47
#include <sys/bus.h>
48
#include <sys/pciio.h>
49
50
#include <dev/pci/pcivar.h>
51
#include <dev/pci/pcireg.h>
52
53
#include <net/if.h>
54
55
#include <vm/vm.h>
56
#include <vm/pmap.h>
57
#include <vm/vm_map.h>
58
#include <vm/vm_param.h>
59
#include <vm/vm_object.h>
60
#include <vm/swap_pager.h>
61
62
#include <machine/bus.h>
63
64
#include <compat/linux/linux_ioctl.h>
65
#include <compat/linux/linux_mib.h>
66
#include <compat/linux/linux_util.h>
67
#include <fs/pseudofs/pseudofs.h>
68
69
#include <asm/atomic.h>
70
#include <linux/compat.h>
71
#include <linux/debugfs.h>
72
#include <linux/fs.h>
73
74
MALLOC_DEFINE(M_DFSINT, "debugfsint", "Linux debugfs internal");
75
76
static struct pfs_node *debugfs_root;
77
78
#define DM_SYMLINK 0x1
79
#define DM_DIR 0x2
80
#define DM_FILE 0x3
81
82
struct dentry_meta {
83
struct dentry dm_dnode;
84
const struct file_operations *dm_fops;
85
void *dm_data;
86
umode_t dm_mode;
87
int dm_type;
88
};
89
90
static int
91
debugfs_attr(PFS_ATTR_ARGS)
92
{
93
struct dentry_meta *dm;
94
95
dm = pn->pn_data;
96
97
vap->va_mode = dm->dm_mode;
98
return (0);
99
}
100
101
static int
102
debugfs_destroy(PFS_DESTROY_ARGS)
103
{
104
struct dentry_meta *dm;
105
106
dm = pn->pn_data;
107
if (dm != NULL && dm->dm_type == DM_SYMLINK)
108
free(dm->dm_data, M_DFSINT);
109
110
free(dm, M_DFSINT);
111
return (0);
112
}
113
114
static int
115
debugfs_fill(PFS_FILL_ARGS)
116
{
117
struct dentry_meta *d;
118
struct linux_file lf = {};
119
struct vnode vn;
120
char *buf;
121
int rc;
122
off_t off = 0;
123
124
if ((rc = linux_set_current_flags(curthread, M_NOWAIT)))
125
return (rc);
126
127
d = pn->pn_data;
128
vn.v_data = d->dm_data;
129
130
rc = d->dm_fops->open(&vn, &lf);
131
if (rc < 0) {
132
#ifdef INVARIANTS
133
printf("%s:%d open failed with %d\n", __func__, __LINE__, rc);
134
#endif
135
return (-rc);
136
}
137
138
rc = -ENODEV;
139
switch (uio->uio_rw) {
140
case UIO_READ:
141
if (d->dm_fops->read != NULL) {
142
rc = -ENOMEM;
143
buf = malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT);
144
if (buf != NULL) {
145
rc = d->dm_fops->read(&lf, buf, sb->s_size,
146
&off);
147
if (rc > 0)
148
sbuf_bcpy(sb, buf, strlen(buf));
149
150
free(buf, M_DFSINT);
151
}
152
}
153
break;
154
case UIO_WRITE:
155
if (d->dm_fops->write != NULL) {
156
sbuf_finish(sb);
157
rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb),
158
&off);
159
}
160
break;
161
}
162
163
if (d->dm_fops->release)
164
d->dm_fops->release(&vn, &lf);
165
166
if (rc < 0) {
167
#ifdef INVARIANTS
168
printf("%s:%d read/write failed with %d\n", __func__, __LINE__, rc);
169
#endif
170
return (-rc);
171
}
172
return (0);
173
}
174
175
static int
176
debugfs_fill_data(PFS_FILL_ARGS)
177
{
178
struct dentry_meta *dm;
179
180
dm = pn->pn_data;
181
sbuf_printf(sb, "%s", (char *)dm->dm_data);
182
return (0);
183
}
184
185
struct dentry *
186
debugfs_create_file(const char *name, umode_t mode,
187
struct dentry *parent, void *data,
188
const struct file_operations *fops)
189
{
190
struct dentry_meta *dm;
191
struct dentry *dnode;
192
struct pfs_node *pnode;
193
int flags;
194
195
dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
196
if (dm == NULL)
197
return (NULL);
198
dnode = &dm->dm_dnode;
199
dm->dm_fops = fops;
200
dm->dm_data = data;
201
dm->dm_mode = mode;
202
dm->dm_type = DM_FILE;
203
if (parent != NULL)
204
pnode = parent->d_pfs_node;
205
else
206
pnode = debugfs_root;
207
208
flags = fops->write ? PFS_RDWR : PFS_RD;
209
pfs_create_file(pnode, &dnode->d_pfs_node, name, debugfs_fill,
210
debugfs_attr, NULL, debugfs_destroy, flags | PFS_NOWAIT);
211
if (dnode->d_pfs_node == NULL) {
212
free(dm, M_DFSINT);
213
return (NULL);
214
}
215
dnode->d_pfs_node->pn_data = dm;
216
217
return (dnode);
218
}
219
220
struct dentry *
221
debugfs_create_file_size(const char *name, umode_t mode,
222
struct dentry *parent, void *data,
223
const struct file_operations *fops,
224
loff_t file_size __unused)
225
{
226
227
return debugfs_create_file(name, mode, parent, data, fops);
228
}
229
230
/*
231
* NOTE: Files created with the _unsafe moniker will not be protected from
232
* debugfs core file removals. It is the responsibility of @fops to protect
233
* its file using debugfs_file_get() and debugfs_file_put().
234
*
235
* FreeBSD's LinuxKPI lindebugfs does not perform file removals at the time
236
* of writing. Therefore there is no difference between functions with _unsafe
237
* and functions without _unsafe when using lindebugfs. Functions with _unsafe
238
* exist only for Linux compatibility.
239
*/
240
struct dentry *
241
debugfs_create_file_unsafe(const char *name, umode_t mode,
242
struct dentry *parent, void *data,
243
const struct file_operations *fops)
244
{
245
246
return (debugfs_create_file(name, mode, parent, data, fops));
247
}
248
249
struct dentry *
250
debugfs_create_mode_unsafe(const char *name, umode_t mode,
251
struct dentry *parent, void *data,
252
const struct file_operations *fops,
253
const struct file_operations *fops_ro,
254
const struct file_operations *fops_wo)
255
{
256
umode_t read = mode & S_IRUGO;
257
umode_t write = mode & S_IWUGO;
258
259
if (read && !write)
260
return (debugfs_create_file_unsafe(name, mode, parent, data, fops_ro));
261
262
if (write && !read)
263
return (debugfs_create_file_unsafe(name, mode, parent, data, fops_wo));
264
265
return (debugfs_create_file_unsafe(name, mode, parent, data, fops));
266
}
267
268
struct dentry *
269
debugfs_create_dir(const char *name, struct dentry *parent)
270
{
271
struct dentry_meta *dm;
272
struct dentry *dnode;
273
struct pfs_node *pnode;
274
275
if (name == NULL)
276
return (NULL);
277
278
dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
279
if (dm == NULL)
280
return (NULL);
281
dnode = &dm->dm_dnode;
282
dm->dm_mode = 0700;
283
dm->dm_type = DM_DIR;
284
if (parent != NULL)
285
pnode = parent->d_pfs_node;
286
else
287
pnode = debugfs_root;
288
289
pfs_create_dir(pnode, &dnode->d_pfs_node, name, debugfs_attr, NULL,
290
debugfs_destroy, PFS_RD | PFS_NOWAIT);
291
if (dnode->d_pfs_node == NULL) {
292
free(dm, M_DFSINT);
293
return (NULL);
294
}
295
dnode->d_pfs_node->pn_data = dm;
296
return (dnode);
297
}
298
299
struct dentry *
300
debugfs_create_symlink(const char *name, struct dentry *parent,
301
const char *dest)
302
{
303
struct dentry_meta *dm;
304
struct dentry *dnode;
305
struct pfs_node *pnode;
306
void *data;
307
308
data = strdup_flags(dest, M_DFSINT, M_NOWAIT);
309
if (data == NULL)
310
return (NULL);
311
dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
312
if (dm == NULL)
313
goto fail1;
314
dnode = &dm->dm_dnode;
315
dm->dm_mode = 0700;
316
dm->dm_type = DM_SYMLINK;
317
dm->dm_data = data;
318
if (parent != NULL)
319
pnode = parent->d_pfs_node;
320
else
321
pnode = debugfs_root;
322
323
pfs_create_link(pnode, &dnode->d_pfs_node, name, &debugfs_fill_data,
324
NULL, NULL, NULL, PFS_NOWAIT);
325
if (dnode->d_pfs_node == NULL)
326
goto fail;
327
dnode->d_pfs_node->pn_data = dm;
328
return (dnode);
329
fail:
330
free(dm, M_DFSINT);
331
fail1:
332
free(data, M_DFSINT);
333
return (NULL);
334
}
335
336
struct dentry *
337
debugfs_lookup(const char *name, struct dentry *parent)
338
{
339
struct dentry_meta *dm;
340
struct dentry *dnode;
341
struct pfs_node *pnode;
342
343
pnode = pfs_find_node(parent->d_pfs_node, name);
344
if (pnode == NULL)
345
return (NULL);
346
347
dm = (struct dentry_meta *)pnode->pn_data;
348
dnode = &dm->dm_dnode;
349
350
return (dnode);
351
}
352
353
void
354
debugfs_remove(struct dentry *dnode)
355
{
356
if (dnode == NULL)
357
return;
358
359
pfs_destroy(dnode->d_pfs_node);
360
}
361
362
void
363
debugfs_remove_recursive(struct dentry *dnode)
364
{
365
if (dnode == NULL)
366
return;
367
368
pfs_destroy(dnode->d_pfs_node);
369
}
370
371
static int
372
debugfs_bool_get(void *data, uint64_t *ullval)
373
{
374
bool *bval = data;
375
376
if (*bval)
377
*ullval = 1;
378
else
379
*ullval = 0;
380
381
return (0);
382
}
383
384
static int
385
debugfs_bool_set(void *data, uint64_t ullval)
386
{
387
bool *bval = data;
388
389
if (ullval)
390
*bval = 1;
391
else
392
*bval = 0;
393
394
return (0);
395
}
396
397
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool, debugfs_bool_get, debugfs_bool_set, "%llu\n");
398
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_ro, debugfs_bool_get, NULL, "%llu\n");
399
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_wo, NULL, debugfs_bool_set, "%llu\n");
400
401
void
402
debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, bool *value)
403
{
404
405
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
406
&fops_bool_ro, &fops_bool_wo);
407
}
408
409
410
static int
411
debugfs_u8_get(void *data, uint64_t *value)
412
{
413
uint8_t *u8data = data;
414
*value = *u8data;
415
return (0);
416
}
417
418
static int
419
debugfs_u8_set(void *data, uint64_t value)
420
{
421
uint8_t *u8data = data;
422
*u8data = (uint8_t)value;
423
return (0);
424
}
425
426
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%u\n");
427
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%u\n");
428
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%u\n");
429
430
void
431
debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value)
432
{
433
434
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
435
&fops_u8_ro, &fops_u8_wo);
436
}
437
438
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%016llx\n");
439
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%016llx\n");
440
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%016llx\n");
441
442
void
443
debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value)
444
{
445
446
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
447
&fops_x8_ro, &fops_x8_wo);
448
}
449
450
451
static int
452
debugfs_u16_get(void *data, uint64_t *value)
453
{
454
uint16_t *u16data = data;
455
*value = *u16data;
456
return (0);
457
}
458
459
static int
460
debugfs_u16_set(void *data, uint64_t value)
461
{
462
uint16_t *u16data = data;
463
*u16data = (uint16_t)value;
464
return (0);
465
}
466
467
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%u\n");
468
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%u\n");
469
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%u\n");
470
471
void
472
debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value)
473
{
474
475
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
476
&fops_u16_ro, &fops_u16_wo);
477
}
478
479
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%016llx\n");
480
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%016llx\n");
481
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%016llx\n");
482
483
void
484
debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value)
485
{
486
487
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
488
&fops_x16_ro, &fops_x16_wo);
489
}
490
491
492
static int
493
debugfs_u32_get(void *data, uint64_t *value)
494
{
495
uint32_t *u32data = data;
496
*value = *u32data;
497
return (0);
498
}
499
500
static int
501
debugfs_u32_set(void *data, uint64_t value)
502
{
503
uint32_t *u32data = data;
504
*u32data = (uint32_t)value;
505
return (0);
506
}
507
508
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%u\n");
509
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%u\n");
510
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%u\n");
511
512
void
513
debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value)
514
{
515
516
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
517
&fops_u32_ro, &fops_u32_wo);
518
}
519
520
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%016llx\n");
521
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%016llx\n");
522
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%016llx\n");
523
524
void
525
debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value)
526
{
527
528
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
529
&fops_x32_ro, &fops_x32_wo);
530
}
531
532
533
static int
534
debugfs_u64_get(void *data, uint64_t *value)
535
{
536
uint64_t *u64data = data;
537
*value = *u64data;
538
return (0);
539
}
540
541
static int
542
debugfs_u64_set(void *data, uint64_t value)
543
{
544
uint64_t *u64data = data;
545
*u64data = (uint64_t)value;
546
return (0);
547
}
548
549
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%u\n");
550
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%u\n");
551
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%u\n");
552
553
void
554
debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value)
555
{
556
557
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
558
&fops_u64_ro, &fops_u64_wo);
559
}
560
561
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
562
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
563
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
564
565
void
566
debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value)
567
{
568
569
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
570
&fops_x64_ro, &fops_x64_wo);
571
}
572
573
574
static int
575
debugfs_ulong_get(void *data, uint64_t *value)
576
{
577
uint64_t *uldata = data;
578
*value = *uldata;
579
return (0);
580
}
581
582
static int
583
debugfs_ulong_set(void *data, uint64_t value)
584
{
585
uint64_t *uldata = data;
586
*uldata = value;
587
return (0);
588
}
589
590
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
591
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
592
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
593
594
void
595
debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, unsigned long *value)
596
{
597
598
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
599
&fops_ulong_ro, &fops_ulong_wo);
600
}
601
602
603
static int
604
debugfs_atomic_t_get(void *data, uint64_t *value)
605
{
606
atomic_t *atomic_data = data;
607
*value = atomic_read(atomic_data);
608
return (0);
609
}
610
611
static int
612
debugfs_atomic_t_set(void *data, uint64_t value)
613
{
614
atomic_t *atomic_data = data;
615
atomic_set(atomic_data, (int)value);
616
return (0);
617
}
618
619
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, debugfs_atomic_t_set, "%d\n");
620
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%d\n");
621
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%d\n");
622
623
void
624
debugfs_create_atomic_t(const char *name, umode_t mode, struct dentry *parent, atomic_t *value)
625
{
626
627
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
628
&fops_atomic_t_ro, &fops_atomic_t_wo);
629
}
630
631
632
static int
633
fops_str_open(struct inode *inode, struct file *filp)
634
{
635
636
return (simple_open(inode, filp));
637
}
638
639
static ssize_t
640
fops_str_read(struct file *filp, char __user *ubuf, size_t read_size,
641
loff_t *ppos)
642
{
643
ssize_t ret;
644
char *str, *str_with_newline;
645
size_t str_len, str_with_newline_len;
646
647
if (filp->private_data == NULL)
648
return (-EINVAL);
649
650
str = *(char **)filp->private_data;
651
str_len = strlen(str);
652
653
/*
654
* `str_with_newline` is terminated with a newline, but is not
655
* NUL-terminated.
656
*/
657
str_with_newline_len = str_len + 1;
658
str_with_newline = kmalloc(str_with_newline_len, GFP_KERNEL);
659
if (str_with_newline == NULL)
660
return (-ENOMEM);
661
662
strncpy(str_with_newline, str, str_len);
663
str_with_newline[str_len] = '\n';
664
665
ret = simple_read_from_buffer(ubuf, read_size, ppos,
666
str_with_newline, str_with_newline_len);
667
668
kfree(str_with_newline);
669
670
return (ret);
671
}
672
673
static ssize_t
674
fops_str_write(struct file *filp, const char *buf, size_t write_size,
675
loff_t *ppos)
676
{
677
char *old, *new;
678
size_t old_len, new_len;
679
680
if (filp->private_data == NULL)
681
return (-EINVAL);
682
683
old = *(char **)filp->private_data;
684
new = NULL;
685
686
/*
687
* We enforce concatenation of the newly written value to the existing
688
* value.
689
*/
690
old_len = strlen(old);
691
if (*ppos && *ppos != old_len)
692
return (-EINVAL);
693
694
new_len = old_len + write_size;
695
if (new_len + 1 > PAGE_SIZE)
696
return (-E2BIG);
697
698
new = kmalloc(new_len + 1, GFP_KERNEL);
699
if (new == NULL)
700
return (-ENOMEM);
701
702
memcpy(new, old, old_len);
703
if (copy_from_user(new + old_len, buf, write_size) != 0) {
704
kfree(new);
705
return (-EFAULT);
706
}
707
708
new[new_len] = '\0';
709
strim(new);
710
711
filp->private_data = &new;
712
713
kfree(old);
714
715
return (write_size);
716
}
717
718
static const struct file_operations fops_str = {
719
.owner = THIS_MODULE,
720
.open = fops_str_open,
721
.read = fops_str_read,
722
.write = fops_str_write,
723
.llseek = no_llseek
724
};
725
static const struct file_operations fops_str_ro = {
726
.owner = THIS_MODULE,
727
.open = fops_str_open,
728
.read = fops_str_read,
729
.llseek = no_llseek
730
};
731
static const struct file_operations fops_str_wo = {
732
.owner = THIS_MODULE,
733
.open = fops_str_open,
734
.write = fops_str_write,
735
.llseek = no_llseek
736
};
737
738
void
739
debugfs_create_str(const char *name, umode_t mode, struct dentry *parent,
740
char **value)
741
{
742
debugfs_create_mode_unsafe(name, mode, parent, value,
743
&fops_str, &fops_str_ro, &fops_str_wo);
744
}
745
746
747
static ssize_t
748
fops_blob_read(struct file *filp, char __user *ubuf, size_t read_size, loff_t *ppos)
749
{
750
struct debugfs_blob_wrapper *blob;
751
752
blob = filp->private_data;
753
if (blob == NULL)
754
return (-EINVAL);
755
if (blob->size == 0 || blob->data == NULL)
756
return (-EINVAL);
757
758
return (simple_read_from_buffer(ubuf, read_size, ppos, blob->data, blob->size));
759
}
760
761
static int
762
fops_blob_open(struct inode *inode, struct file *filp)
763
{
764
765
return (simple_open(inode, filp));
766
}
767
768
static const struct file_operations __fops_blob_ro = {
769
.owner = THIS_MODULE,
770
.open = fops_blob_open,
771
.read = fops_blob_read,
772
.llseek = no_llseek
773
};
774
775
struct dentry *
776
debugfs_create_blob(const char *name, umode_t mode, struct dentry *parent,
777
struct debugfs_blob_wrapper *value)
778
{
779
/* Blobs are read-only. */
780
return (debugfs_create_file(name, mode & 0444, parent, value, &__fops_blob_ro));
781
}
782
783
784
static int
785
lindebugfs_init(PFS_INIT_ARGS)
786
{
787
788
debugfs_root = pi->pi_root;
789
790
(void)debugfs_create_symlink("kcov", NULL, "/dev/kcov");
791
792
return (0);
793
}
794
795
static int
796
lindebugfs_uninit(PFS_INIT_ARGS)
797
{
798
799
return (0);
800
}
801
802
PSEUDOFS(lindebugfs, 1, VFCF_JAIL);
803
MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1);
804
805