Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/compat/lindebugfs/lindebugfs.c
39483 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
dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
276
if (dm == NULL)
277
return (NULL);
278
dnode = &dm->dm_dnode;
279
dm->dm_mode = 0700;
280
dm->dm_type = DM_DIR;
281
if (parent != NULL)
282
pnode = parent->d_pfs_node;
283
else
284
pnode = debugfs_root;
285
286
pfs_create_dir(pnode, &dnode->d_pfs_node, name, debugfs_attr, NULL,
287
debugfs_destroy, PFS_RD | PFS_NOWAIT);
288
if (dnode->d_pfs_node == NULL) {
289
free(dm, M_DFSINT);
290
return (NULL);
291
}
292
dnode->d_pfs_node->pn_data = dm;
293
return (dnode);
294
}
295
296
struct dentry *
297
debugfs_create_symlink(const char *name, struct dentry *parent,
298
const char *dest)
299
{
300
struct dentry_meta *dm;
301
struct dentry *dnode;
302
struct pfs_node *pnode;
303
void *data;
304
305
data = strdup_flags(dest, M_DFSINT, M_NOWAIT);
306
if (data == NULL)
307
return (NULL);
308
dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
309
if (dm == NULL)
310
goto fail1;
311
dnode = &dm->dm_dnode;
312
dm->dm_mode = 0700;
313
dm->dm_type = DM_SYMLINK;
314
dm->dm_data = data;
315
if (parent != NULL)
316
pnode = parent->d_pfs_node;
317
else
318
pnode = debugfs_root;
319
320
pfs_create_link(pnode, &dnode->d_pfs_node, name, &debugfs_fill_data,
321
NULL, NULL, NULL, PFS_NOWAIT);
322
if (dnode->d_pfs_node == NULL)
323
goto fail;
324
dnode->d_pfs_node->pn_data = dm;
325
return (dnode);
326
fail:
327
free(dm, M_DFSINT);
328
fail1:
329
free(data, M_DFSINT);
330
return (NULL);
331
}
332
333
struct dentry *
334
debugfs_lookup(const char *name, struct dentry *parent)
335
{
336
struct dentry_meta *dm;
337
struct dentry *dnode;
338
struct pfs_node *pnode;
339
340
pnode = pfs_find_node(parent->d_pfs_node, name);
341
if (pnode == NULL)
342
return (NULL);
343
344
dm = (struct dentry_meta *)pnode->pn_data;
345
dnode = &dm->dm_dnode;
346
347
return (dnode);
348
}
349
350
void
351
debugfs_remove(struct dentry *dnode)
352
{
353
if (dnode == NULL)
354
return;
355
356
pfs_destroy(dnode->d_pfs_node);
357
}
358
359
void
360
debugfs_remove_recursive(struct dentry *dnode)
361
{
362
if (dnode == NULL)
363
return;
364
365
pfs_destroy(dnode->d_pfs_node);
366
}
367
368
static int
369
debugfs_bool_get(void *data, uint64_t *ullval)
370
{
371
bool *bval = data;
372
373
if (*bval)
374
*ullval = 1;
375
else
376
*ullval = 0;
377
378
return (0);
379
}
380
381
static int
382
debugfs_bool_set(void *data, uint64_t ullval)
383
{
384
bool *bval = data;
385
386
if (ullval)
387
*bval = 1;
388
else
389
*bval = 0;
390
391
return (0);
392
}
393
394
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool, debugfs_bool_get, debugfs_bool_set, "%llu\n");
395
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_ro, debugfs_bool_get, NULL, "%llu\n");
396
DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_wo, NULL, debugfs_bool_set, "%llu\n");
397
398
void
399
debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, bool *value)
400
{
401
402
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
403
&fops_bool_ro, &fops_bool_wo);
404
}
405
406
407
static int
408
debugfs_u8_get(void *data, uint64_t *value)
409
{
410
uint8_t *u8data = data;
411
*value = *u8data;
412
return (0);
413
}
414
415
static int
416
debugfs_u8_set(void *data, uint64_t value)
417
{
418
uint8_t *u8data = data;
419
*u8data = (uint8_t)value;
420
return (0);
421
}
422
423
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%u\n");
424
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%u\n");
425
DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%u\n");
426
427
void
428
debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value)
429
{
430
431
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
432
&fops_u8_ro, &fops_u8_wo);
433
}
434
435
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%016llx\n");
436
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%016llx\n");
437
DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%016llx\n");
438
439
void
440
debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value)
441
{
442
443
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
444
&fops_x8_ro, &fops_x8_wo);
445
}
446
447
448
static int
449
debugfs_u16_get(void *data, uint64_t *value)
450
{
451
uint16_t *u16data = data;
452
*value = *u16data;
453
return (0);
454
}
455
456
static int
457
debugfs_u16_set(void *data, uint64_t value)
458
{
459
uint16_t *u16data = data;
460
*u16data = (uint16_t)value;
461
return (0);
462
}
463
464
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%u\n");
465
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%u\n");
466
DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%u\n");
467
468
void
469
debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value)
470
{
471
472
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
473
&fops_u16_ro, &fops_u16_wo);
474
}
475
476
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%016llx\n");
477
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%016llx\n");
478
DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%016llx\n");
479
480
void
481
debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value)
482
{
483
484
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
485
&fops_x16_ro, &fops_x16_wo);
486
}
487
488
489
static int
490
debugfs_u32_get(void *data, uint64_t *value)
491
{
492
uint32_t *u32data = data;
493
*value = *u32data;
494
return (0);
495
}
496
497
static int
498
debugfs_u32_set(void *data, uint64_t value)
499
{
500
uint32_t *u32data = data;
501
*u32data = (uint32_t)value;
502
return (0);
503
}
504
505
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%u\n");
506
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%u\n");
507
DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%u\n");
508
509
void
510
debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value)
511
{
512
513
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
514
&fops_u32_ro, &fops_u32_wo);
515
}
516
517
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%016llx\n");
518
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%016llx\n");
519
DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%016llx\n");
520
521
void
522
debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value)
523
{
524
525
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
526
&fops_x32_ro, &fops_x32_wo);
527
}
528
529
530
static int
531
debugfs_u64_get(void *data, uint64_t *value)
532
{
533
uint64_t *u64data = data;
534
*value = *u64data;
535
return (0);
536
}
537
538
static int
539
debugfs_u64_set(void *data, uint64_t value)
540
{
541
uint64_t *u64data = data;
542
*u64data = (uint64_t)value;
543
return (0);
544
}
545
546
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%u\n");
547
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%u\n");
548
DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%u\n");
549
550
void
551
debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value)
552
{
553
554
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
555
&fops_u64_ro, &fops_u64_wo);
556
}
557
558
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
559
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
560
DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
561
562
void
563
debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value)
564
{
565
566
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
567
&fops_x64_ro, &fops_x64_wo);
568
}
569
570
571
static int
572
debugfs_ulong_get(void *data, uint64_t *value)
573
{
574
uint64_t *uldata = data;
575
*value = *uldata;
576
return (0);
577
}
578
579
static int
580
debugfs_ulong_set(void *data, uint64_t value)
581
{
582
uint64_t *uldata = data;
583
*uldata = value;
584
return (0);
585
}
586
587
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
588
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
589
DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
590
591
void
592
debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, unsigned long *value)
593
{
594
595
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
596
&fops_ulong_ro, &fops_ulong_wo);
597
}
598
599
600
static int
601
debugfs_atomic_t_get(void *data, uint64_t *value)
602
{
603
atomic_t *atomic_data = data;
604
*value = atomic_read(atomic_data);
605
return (0);
606
}
607
608
static int
609
debugfs_atomic_t_set(void *data, uint64_t value)
610
{
611
atomic_t *atomic_data = data;
612
atomic_set(atomic_data, (int)value);
613
return (0);
614
}
615
616
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, debugfs_atomic_t_set, "%d\n");
617
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%d\n");
618
DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%d\n");
619
620
void
621
debugfs_create_atomic_t(const char *name, umode_t mode, struct dentry *parent, atomic_t *value)
622
{
623
624
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
625
&fops_atomic_t_ro, &fops_atomic_t_wo);
626
}
627
628
629
static int
630
fops_str_open(struct inode *inode, struct file *filp)
631
{
632
633
return (simple_open(inode, filp));
634
}
635
636
static ssize_t
637
fops_str_read(struct file *filp, char __user *ubuf, size_t read_size,
638
loff_t *ppos)
639
{
640
ssize_t ret;
641
char *str, *str_with_newline;
642
size_t str_len, str_with_newline_len;
643
644
if (filp->private_data == NULL)
645
return (-EINVAL);
646
647
str = *(char **)filp->private_data;
648
str_len = strlen(str);
649
650
/*
651
* `str_with_newline` is terminated with a newline, but is not
652
* NUL-terminated.
653
*/
654
str_with_newline_len = str_len + 1;
655
str_with_newline = kmalloc(str_with_newline_len, GFP_KERNEL);
656
if (str_with_newline == NULL)
657
return (-ENOMEM);
658
659
strncpy(str_with_newline, str, str_len);
660
str_with_newline[str_len] = '\n';
661
662
ret = simple_read_from_buffer(ubuf, read_size, ppos,
663
str_with_newline, str_with_newline_len);
664
665
kfree(str_with_newline);
666
667
return (ret);
668
}
669
670
static ssize_t
671
fops_str_write(struct file *filp, const char *buf, size_t write_size,
672
loff_t *ppos)
673
{
674
char *old, *new;
675
size_t old_len, new_len;
676
677
if (filp->private_data == NULL)
678
return (-EINVAL);
679
680
old = *(char **)filp->private_data;
681
new = NULL;
682
683
/*
684
* We enforce concatenation of the newly written value to the existing
685
* value.
686
*/
687
old_len = strlen(old);
688
if (*ppos && *ppos != old_len)
689
return (-EINVAL);
690
691
new_len = old_len + write_size;
692
if (new_len + 1 > PAGE_SIZE)
693
return (-E2BIG);
694
695
new = kmalloc(new_len + 1, GFP_KERNEL);
696
if (new == NULL)
697
return (-ENOMEM);
698
699
memcpy(new, old, old_len);
700
if (copy_from_user(new + old_len, buf, write_size) != 0) {
701
kfree(new);
702
return (-EFAULT);
703
}
704
705
new[new_len] = '\0';
706
strim(new);
707
708
filp->private_data = &new;
709
710
kfree(old);
711
712
return (write_size);
713
}
714
715
static const struct file_operations fops_str = {
716
.owner = THIS_MODULE,
717
.open = fops_str_open,
718
.read = fops_str_read,
719
.write = fops_str_write,
720
.llseek = no_llseek
721
};
722
static const struct file_operations fops_str_ro = {
723
.owner = THIS_MODULE,
724
.open = fops_str_open,
725
.read = fops_str_read,
726
.llseek = no_llseek
727
};
728
static const struct file_operations fops_str_wo = {
729
.owner = THIS_MODULE,
730
.open = fops_str_open,
731
.write = fops_str_write,
732
.llseek = no_llseek
733
};
734
735
void
736
debugfs_create_str(const char *name, umode_t mode, struct dentry *parent,
737
char **value)
738
{
739
debugfs_create_mode_unsafe(name, mode, parent, value,
740
&fops_str, &fops_str_ro, &fops_str_wo);
741
}
742
743
744
static ssize_t
745
fops_blob_read(struct file *filp, char __user *ubuf, size_t read_size, loff_t *ppos)
746
{
747
struct debugfs_blob_wrapper *blob;
748
749
blob = filp->private_data;
750
if (blob == NULL)
751
return (-EINVAL);
752
if (blob->size == 0 || blob->data == NULL)
753
return (-EINVAL);
754
755
return (simple_read_from_buffer(ubuf, read_size, ppos, blob->data, blob->size));
756
}
757
758
static int
759
fops_blob_open(struct inode *inode, struct file *filp)
760
{
761
762
return (simple_open(inode, filp));
763
}
764
765
static const struct file_operations __fops_blob_ro = {
766
.owner = THIS_MODULE,
767
.open = fops_blob_open,
768
.read = fops_blob_read,
769
.llseek = no_llseek
770
};
771
772
struct dentry *
773
debugfs_create_blob(const char *name, umode_t mode, struct dentry *parent,
774
struct debugfs_blob_wrapper *value)
775
{
776
/* Blobs are read-only. */
777
return (debugfs_create_file(name, mode & 0444, parent, value, &__fops_blob_ro));
778
}
779
780
781
static int
782
lindebugfs_init(PFS_INIT_ARGS)
783
{
784
785
debugfs_root = pi->pi_root;
786
787
(void)debugfs_create_symlink("kcov", NULL, "/dev/kcov");
788
789
return (0);
790
}
791
792
static int
793
lindebugfs_uninit(PFS_INIT_ARGS)
794
{
795
796
return (0);
797
}
798
799
PSEUDOFS(lindebugfs, 1, VFCF_JAIL);
800
MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1);
801
802