2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/queue.h>
35 #include <sys/blist.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/linker.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
45 #include <sys/resourcevar.h>
48 #include <sys/socket.h>
49 #include <sys/vnode.h>
51 #include <sys/pciio.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_object.h>
63 #include <vm/swap_pager.h>
65 #include <machine/bus.h>
67 #include <compat/linux/linux_ioctl.h>
68 #include <compat/linux/linux_mib.h>
69 #include <compat/linux/linux_util.h>
70 #include <fs/pseudofs/pseudofs.h>
72 #include <linux/debugfs.h>
73 #include <linux/seq_file.h>
74 #include <linux/compat.h>
76 MALLOC_DEFINE(M_DFSINT, "debugfsint", "Linux debugfs internal");
78 static struct pfs_node *debugfs_root;
80 #define DM_SYMLINK 0x1
85 struct dentry dm_dnode;
86 const struct file_operations *dm_fops;
93 debugfs_attr(PFS_ATTR_ARGS)
95 struct dentry_meta *dm;
99 vap->va_mode = dm->dm_mode;
104 debugfs_destroy(PFS_DESTROY_ARGS)
106 struct dentry_meta *dm;
109 if (dm->dm_type == DM_SYMLINK)
110 free(dm->dm_data, M_DFSINT);
117 debugfs_fill(PFS_FILL_ARGS)
119 struct dentry_meta *d;
120 struct linux_file lf;
130 if ((rc = linux_set_current_flags(curthread, M_NOWAIT)))
132 vn.v_data = d->dm_data;
133 buf = uio->uio_iov[0].iov_base;
134 len = min(uio->uio_iov[0].iov_len, uio->uio_resid);
136 lf.private_data = NULL;
137 rc = d->dm_fops->open(&vn, &lf);
140 printf("%s:%d open failed with %d\n", __FUNCTION__, __LINE__, rc);
144 sf = lf.private_data;
146 if (uio->uio_rw == UIO_READ) {
147 if (d->dm_fops->read)
148 rc = d->dm_fops->read(&lf, NULL, len, &off);
152 if (d->dm_fops->write)
153 rc = d->dm_fops->write(&lf, buf, len, &off);
157 if (d->dm_fops->release)
158 d->dm_fops->release(&vn, &lf);
160 single_release(&vn, &lf);
164 printf("%s:%d read/write failed with %d\n", __FUNCTION__, __LINE__, rc);
172 debugfs_fill_data(PFS_FILL_ARGS)
174 struct dentry_meta *dm;
177 sbuf_printf(sb, "%s", (char *)dm->dm_data);
182 debugfs_create_file(const char *name, umode_t mode,
183 struct dentry *parent, void *data,
184 const struct file_operations *fops)
186 struct dentry_meta *dm;
187 struct dentry *dnode;
188 struct pfs_node *pnode;
191 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
194 dnode = &dm->dm_dnode;
198 dm->dm_type = DM_FILE;
200 pnode = parent->d_pfs_node;
202 pnode = debugfs_root;
204 flags = fops->write ? PFS_RDWR : PFS_RD;
205 dnode->d_pfs_node = pfs_create_file(pnode, name, debugfs_fill,
206 debugfs_attr, NULL, debugfs_destroy, flags | PFS_NOWAIT);
207 if (dnode->d_pfs_node == NULL) {
211 dnode->d_pfs_node->pn_data = dm;
217 debugfs_create_dir(const char *name, struct dentry *parent)
219 struct dentry_meta *dm;
220 struct dentry *dnode;
221 struct pfs_node *pnode;
223 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
226 dnode = &dm->dm_dnode;
228 dm->dm_type = DM_DIR;
230 pnode = parent->d_pfs_node;
232 pnode = debugfs_root;
234 dnode->d_pfs_node = pfs_create_dir(pnode, name, debugfs_attr, NULL, debugfs_destroy, PFS_RD | PFS_NOWAIT);
235 if (dnode->d_pfs_node == NULL) {
239 dnode->d_pfs_node->pn_data = dm;
244 debugfs_create_symlink(const char *name, struct dentry *parent,
247 struct dentry_meta *dm;
248 struct dentry *dnode;
249 struct pfs_node *pnode;
252 data = strdup_flags(dest, M_DFSINT, M_NOWAIT);
255 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
258 dnode = &dm->dm_dnode;
260 dm->dm_type = DM_SYMLINK;
263 pnode = parent->d_pfs_node;
265 pnode = debugfs_root;
267 dnode->d_pfs_node = pfs_create_link(pnode, name, &debugfs_fill_data, NULL, NULL, NULL, PFS_NOWAIT);
268 if (dnode->d_pfs_node == NULL)
270 dnode->d_pfs_node->pn_data = dm;
275 free(data, M_DFSINT);
280 debugfs_remove(struct dentry *dnode)
285 pfs_destroy(dnode->d_pfs_node);
289 debugfs_remove_recursive(struct dentry *dnode)
294 pfs_destroy(dnode->d_pfs_node);
299 debugfs_init(PFS_INIT_ARGS)
302 debugfs_root = pi->pi_root;
307 debugfs_uninit(PFS_INIT_ARGS)
312 #ifdef PR_ALLOW_MOUNT_LINSYSFS
313 PSEUDOFS(debugfs, 1, PR_ALLOW_MOUNT_LINSYSFS);
315 PSEUDOFS(debugfs, 1, VFCF_JAIL);
317 MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1);