diff --git a/sys/compat/linsysfs/linsysfs.c b/sys/compat/linsysfs/linsysfs.c index 7f70221b420d..b70cb43d0f9a 100644 --- a/sys/compat/linsysfs/linsysfs.c +++ b/sys/compat/linsysfs/linsysfs.c @@ -1,565 +1,593 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2006 IronPort Systems * 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, 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MALLOC_DEFINE(M_LINSYSFS, "linsysfs", "Linsysfs structures"); struct scsi_host_queue { TAILQ_ENTRY(scsi_host_queue) scsi_host_next; char *path; char *name; }; TAILQ_HEAD(,scsi_host_queue) scsi_host_q; static int host_number = 0; static int atoi(const char *str) { return (int)strtol(str, (char **)NULL, 10); } /* * Filler function for proc_name */ static int linsysfs_scsiname(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "%s\n", scsi_host->name); return (0); } } sbuf_printf(sb, "unknown\n"); return (0); } /* * Filler function for device sym-link */ static int linsysfs_link_scsi_host(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "../../../devices%s", scsi_host->path); return(0); } } sbuf_printf(sb, "unknown\n"); return (0); } static int linsysfs_fill_data(PFS_FILL_ARGS) { sbuf_printf(sb, "%s", (char *)pn->pn_data); return (0); } static int linsysfs_fill_vendor(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_device(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_subvendor(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_subdevice(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_revid(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_config(PFS_FILL_ARGS) { uint8_t config[48]; device_t dev; uint32_t reg; dev = (device_t)pn->pn_data; bzero(config, sizeof(config)); reg = pci_get_vendor(dev); config[0] = reg; config[1] = reg >> 8; reg = pci_get_device(dev); config[2] = reg; config[3] = reg >> 8; reg = pci_get_revid(dev); config[8] = reg; reg = pci_get_subvendor(dev); config[44] = reg; config[45] = reg >> 8; reg = pci_get_subdevice(dev); config[46] = reg; config[47] = reg >> 8; sbuf_bcat(sb, config, sizeof(config)); return (0); } /* * Filler function for PCI uevent file */ static int linsysfs_fill_uevent_pci(PFS_FILL_ARGS) { device_t dev; dev = (device_t)pn->pn_data; sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n" "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n", linux_driver_get_name_dev(dev), pci_get_class(dev), pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev), pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); return (0); } /* * Filler function for drm uevent file */ static int linsysfs_fill_uevent_drm(PFS_FILL_ARGS) { device_t dev; int unit; dev = (device_t)pn->pn_data; unit = device_get_unit(dev); sbuf_printf(sb, "MAJOR=226\nMINOR=%d\nDEVNAME=dri/card%d\nDEVTYPE=dri_minor\n", unit, unit); return (0); } static char * get_full_pfs_path(struct pfs_node *cur) { char *temp, *path; temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); path[0] = '\0'; do { snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path); strlcpy(path, temp, MAXPATHLEN); cur = cur->pn_parent; } while (cur->pn_parent != NULL); path[strlen(path) - 1] = '\0'; /* remove extra slash */ free(temp, M_TEMP); return (path); } /* * Filler function for symlink from drm char device to PCI device */ static int linsysfs_fill_vgapci(PFS_FILL_ARGS) { char *path; path = get_full_pfs_path((struct pfs_node*)pn->pn_data); sbuf_printf(sb, "../../../%s", path); free(path, M_TEMP); return (0); } #undef PCI_DEV #define PCI_DEV "pci" #define DRMN_DEV "drmn" static int linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, struct pfs_node *chardev, struct pfs_node *drm, char *path, char *prefix) { struct scsi_host_queue *scsi_host; struct pfs_node *sub_dir, *cur_file; int i, nchildren, error; device_t *children, parent; devclass_t devclass; const char *name = NULL; struct pci_devinfo *dinfo; char *device, *host, *new_path, *devname; + children = NULL; + device = host = NULL; new_path = path; devname = malloc(16, M_TEMP, M_WAITOK); parent = device_get_parent(dev); if (parent) { devclass = device_get_devclass(parent); if (devclass != NULL) name = devclass_get_name(devclass); if (name && strcmp(name, PCI_DEV) == 0) { dinfo = device_get_ivars(dev); if (dinfo) { device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path[0] = '\000'; strcpy(new_path, path); host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); device[0] = '\000'; sprintf(device, "%s:%02x:%02x.%x", prefix, dinfo->cfg.bus, dinfo->cfg.slot, dinfo->cfg.func); strcat(new_path, "/"); strcat(new_path, device); dir = pfs_create_dir(dir, device, NULL, NULL, NULL, 0); + if (dir == NULL) { + error = EEXIST; + goto out; + } cur_file = pfs_create_file(dir, "vendor", &linsysfs_fill_vendor, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "device", &linsysfs_fill_device, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "subsystem_vendor", &linsysfs_fill_subvendor, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "subsystem_device", &linsysfs_fill_subdevice, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "revision", &linsysfs_fill_revid, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "config", &linsysfs_fill_config, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "uevent", &linsysfs_fill_uevent_pci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_link(dir, "subsystem", &linsysfs_fill_data, NULL, NULL, NULL, 0); /* libdrm just checks that the link ends in "/pci" */ cur_file->pn_data = "/sys/bus/pci"; if (dinfo->cfg.baseclass == PCIC_STORAGE) { /* DJA only make this if needed */ sprintf(host, "host%d", host_number++); strcat(new_path, "/"); strcat(new_path, host); pfs_create_dir(dir, host, NULL, NULL, NULL, 0); scsi_host = malloc(sizeof( struct scsi_host_queue), - M_DEVBUF, M_NOWAIT); + M_DEVBUF, M_WAITOK); scsi_host->path = malloc( strlen(new_path) + 1, - M_DEVBUF, M_NOWAIT); + M_DEVBUF, M_WAITOK); scsi_host->path[0] = '\000'; bcopy(new_path, scsi_host->path, strlen(new_path) + 1); scsi_host->name = "unknown"; sub_dir = pfs_create_dir(scsi, host, NULL, NULL, NULL, 0); pfs_create_link(sub_dir, "device", &linsysfs_link_scsi_host, NULL, NULL, NULL, 0); pfs_create_file(sub_dir, "proc_name", &linsysfs_scsiname, NULL, NULL, NULL, PFS_RD); scsi_host->name = linux_driver_get_name_dev(dev); TAILQ_INSERT_TAIL(&scsi_host_q, scsi_host, scsi_host_next); } - free(device, M_TEMP); - free(host, M_TEMP); } } devclass = device_get_devclass(dev); if (devclass != NULL) name = devclass_get_name(devclass); else name = NULL; if (name != NULL && strcmp(name, DRMN_DEV) == 0 && device_get_unit(dev) >= 0) { dinfo = device_get_ivars(parent); if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) { pfs_create_dir(dir, "drm", NULL, NULL, NULL, 0); sprintf(devname, "226:%d", device_get_unit(dev)); sub_dir = pfs_create_dir(chardev, devname, NULL, NULL, NULL, 0); cur_file = pfs_create_link(sub_dir, "device", &linsysfs_fill_vgapci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dir; cur_file = pfs_create_file(sub_dir, "uevent", &linsysfs_fill_uevent_drm, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; sprintf(devname, "card%d", device_get_unit(dev)); sub_dir = pfs_create_dir(drm, devname, NULL, NULL, NULL, 0); cur_file = pfs_create_link(sub_dir, "device", &linsysfs_fill_vgapci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dir; } } } error = device_get_children(dev, &children, &nchildren); if (error == 0) { - for (i = 0; i < nchildren; i++) - if (children[i]) - linsysfs_run_bus(children[i], dir, scsi, + for (i = 0; i < nchildren; i++) { + if (children[i]) { + error = linsysfs_run_bus(children[i], dir, scsi, chardev, drm, new_path, prefix); - free(children, M_TEMP); + if (error != 0) { + printf( + "linsysfs_run_bus: %s omitted from sysfs tree, error %d\n", + device_get_nameunit(children[i]), + error); + } + } + } + + /* + * We override the error to avoid cascading failures; the + * innermost device that failed in a tree is probably the most + * significant one for diagnostics, its parents would be noise. + */ + error = 0; } + +out: + free(host, M_TEMP); + free(device, M_TEMP); + if (children != NULL) + free(children, M_TEMP); if (new_path != path) free(new_path, M_TEMP); free(devname, M_TEMP); - return (1); + return (error); } /* * Filler function for sys/devices/system/cpu/{online,possible,present} */ static int linsysfs_cpuonline(PFS_FILL_ARGS) { sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid); return (0); } /* * Filler function for sys/devices/system/cpu/cpuX/online */ static int linsysfs_cpuxonline(PFS_FILL_ARGS) { sbuf_printf(sb, "1\n"); return (0); } static void linsysfs_listcpus(struct pfs_node *dir) { struct pfs_node *cpu; char *name; int i, count, len; len = 1; count = mp_maxcpus; while (count > 10) { count /= 10; len++; } len += sizeof("cpu"); name = malloc(len, M_TEMP, M_WAITOK); for (i = 0; i < mp_ncpus; ++i) { /* /sys/devices/system/cpu/cpuX */ sprintf(name, "cpu%d", i); cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuxonline, NULL, NULL, NULL, PFS_RD); } free(name, M_TEMP); } /* * Constructor */ static int linsysfs_init(PFS_INIT_ARGS) { struct pfs_node *root; struct pfs_node *class; struct pfs_node *dir, *sys, *cpu; struct pfs_node *drm; struct pfs_node *pci; struct pfs_node *scsi; struct pfs_node *devdir, *chardev; struct pfs_node *kernel; devclass_t devclass; device_t dev; TAILQ_INIT(&scsi_host_q); root = pi->pi_root; /* /sys/bus/... */ dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0); /* /sys/class/... */ class = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0); drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0); pfs_create_dir(class, "power_supply", NULL, NULL, NULL, 0); /* /sys/class/net/.. */ net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0); /* /sys/dev/... */ devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0); chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0); /* /sys/devices/... */ dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); devclass = devclass_find("root"); if (devclass == NULL) { return (0); } + /* + * This assumes that the root node is unlikely to error out in + * linsysfs_run_bus, which may or may not be true. + */ dev = devclass_get_device(devclass, 0); linsysfs_run_bus(dev, pci, scsi, chardev, drm, "/pci0000:00", "0000"); /* /sys/devices/system */ sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0); /* /sys/devices/system/cpu */ cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); pfs_create_file(cpu, "possible", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); pfs_create_file(cpu, "present", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); linsysfs_listcpus(cpu); /* /sys/kernel */ kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0); /* /sys/kernel/debug, mountpoint for lindebugfs. */ pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0); linsysfs_net_init(); return (0); } /* * Destructor */ static int linsysfs_uninit(PFS_INIT_ARGS) { struct scsi_host_queue *scsi_host, *scsi_host_tmp; TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, scsi_host_tmp) { TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); free(scsi_host->path, M_TEMP); free(scsi_host, M_TEMP); } linsysfs_net_uninit(); return (0); } PSEUDOFS(linsysfs, 1, VFCF_JAIL); #if defined(__aarch64__) || defined(__amd64__) MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); #else MODULE_DEPEND(linsysfs, linux, 1, 1, 1); #endif diff --git a/sys/fs/pseudofs/pseudofs.c b/sys/fs/pseudofs/pseudofs.c index ef45f96a6192..cc34489b92f2 100644 --- a/sys/fs/pseudofs/pseudofs.c +++ b/sys/fs/pseudofs/pseudofs.c @@ -1,533 +1,533 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2001 Dag-Erling Smørgrav * 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, this list of conditions and the following disclaimer * in this position and unchanged. * 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. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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. */ #include #include "opt_pseudofs.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes"); SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "pseudofs"); #ifdef PSEUDOFS_TRACE int pfs_trace; SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0, "enable tracing of pseudofs vnode operations"); #endif #if PFS_FSNAMELEN != MFSNAMELEN #error "PFS_FSNAMELEN is not equal to MFSNAMELEN" #endif /* * Allocate and initialize a node */ static struct pfs_node * pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int flags) { struct pfs_node *pn; int malloc_flags; size_t len; len = strlen(name); KASSERT(len < PFS_NAMELEN, ("%s(): node name is too long", __func__)); if (flags & PFS_NOWAIT) malloc_flags = M_NOWAIT | M_ZERO; else malloc_flags = M_WAITOK | M_ZERO; pn = malloc(sizeof(*pn) + len + 1, M_PFSNODES, malloc_flags); if (pn == NULL) return (NULL); mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK); memcpy(pn->pn_name, name, len); pn->pn_type = type; pn->pn_info = pi; return (pn); } static struct pfs_node * pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type) { return (pfs_alloc_node_flags(pi, name, type, 0)); } /* * Add a node to a directory */ static int pfs_add_node(struct pfs_node *parent, struct pfs_node *pn) { struct pfs_node *iter; KASSERT(parent != NULL, ("%s(): parent is NULL", __func__)); KASSERT(pn->pn_parent == NULL, ("%s(): node already has a parent", __func__)); KASSERT(parent->pn_info != NULL, ("%s(): parent has no pn_info", __func__)); KASSERT(parent->pn_type == pfstype_dir || parent->pn_type == pfstype_procdir || parent->pn_type == pfstype_root, ("%s(): parent is not a directory", __func__)); #ifdef INVARIANTS /* XXX no locking! */ if (pn->pn_type == pfstype_procdir) for (iter = parent; iter != NULL; iter = iter->pn_parent) KASSERT(iter->pn_type != pfstype_procdir, ("%s(): nested process directories", __func__)); for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) { if (pn->pn_type == pfstype_procdir) KASSERT(iter->pn_type != pfstype_procdir, ("%s(): sibling process directories", __func__)); } #endif pn->pn_parent = parent; pfs_fileno_alloc(pn); pfs_lock(parent); for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) { if (strcmp(pn->pn_name, iter->pn_name) != 0) continue; - printf("pfs_add_node: homonymous siblings: '%s/%s' type %d", + printf("pfs_add_node: homonymous siblings: '%s/%s' type %d\n", parent->pn_name, pn->pn_name, pn->pn_type); /* Do not detach, because we are not yet attached. */ pn->pn_parent = NULL; pfs_unlock(parent); return (EEXIST); } if ((parent->pn_flags & PFS_PROCDEP) != 0) pn->pn_flags |= PFS_PROCDEP; if (parent->pn_nodes == NULL) { KASSERT(parent->pn_last_node == NULL, ("%s(): pn_last_node not NULL", __func__)); parent->pn_nodes = pn; parent->pn_last_node = pn; } else { KASSERT(parent->pn_last_node != NULL, ("%s(): pn_last_node is NULL", __func__)); KASSERT(parent->pn_last_node->pn_next == NULL, ("%s(): pn_last_node->pn_next not NULL", __func__)); parent->pn_last_node->pn_next = pn; parent->pn_last_node = pn; } pfs_unlock(parent); return (0); } /* * Detach a node from its parent */ static void pfs_detach_node(struct pfs_node *pn) { struct pfs_node *node, *parent = pn->pn_parent; struct pfs_node **iter; KASSERT(parent != NULL, ("%s(): node has no parent", __func__)); KASSERT(parent->pn_info == pn->pn_info, ("%s(): parent has different pn_info", __func__)); pfs_lock(parent); if (pn == parent->pn_last_node) { if (pn == pn->pn_nodes) { parent->pn_last_node = NULL; } else { for (node = parent->pn_nodes; node->pn_next != pn; node = node->pn_next) continue; parent->pn_last_node = node; } } iter = &parent->pn_nodes; while (*iter != NULL) { if (*iter == pn) { *iter = pn->pn_next; break; } iter = &(*iter)->pn_next; } pn->pn_parent = NULL; pfs_unlock(parent); } /* * Add . and .. to a directory */ static int pfs_fixup_dir_flags(struct pfs_node *parent, int flags) { struct pfs_node *dot, *dotdot; int rc; dot = pfs_alloc_node_flags(parent->pn_info, ".", pfstype_this, flags); if (dot == NULL) return (ENOMEM); dotdot = pfs_alloc_node_flags(parent->pn_info, "..", pfstype_parent, flags); if (dotdot == NULL) { pfs_destroy(dot); return (ENOMEM); } rc = pfs_add_node(parent, dot); if (rc == 0) rc = pfs_add_node(parent, dotdot); if (rc != 0) { pfs_destroy(dot); pfs_destroy(dotdot); } return (rc); } static void pfs_fixup_dir(struct pfs_node *parent) { pfs_fixup_dir_flags(parent, 0); } /* * Create a directory */ struct pfs_node * pfs_create_dir(struct pfs_node *parent, const char *name, pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, int flags) { struct pfs_node *pn; int rc; pn = pfs_alloc_node_flags(parent->pn_info, name, (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir, flags); if (pn == NULL) return (NULL); pn->pn_attr = attr; pn->pn_vis = vis; pn->pn_destroy = destroy; pn->pn_flags = flags; rc = pfs_add_node(parent, pn); if (rc == 0) rc = pfs_fixup_dir_flags(pn, flags); if (rc != 0) { pfs_destroy(pn); pn = NULL; } return (pn); } /* * Create a file */ struct pfs_node * pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill, pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, int flags) { struct pfs_node *pn; pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_file, flags); if (pn == NULL) return (NULL); pn->pn_fill = fill; pn->pn_attr = attr; pn->pn_vis = vis; pn->pn_destroy = destroy; pn->pn_flags = flags; if (pfs_add_node(parent, pn) != 0) { pfs_destroy(pn); pn = NULL; } return (pn); } /* * Create a symlink */ struct pfs_node * pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill, pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, int flags) { struct pfs_node *pn; pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_symlink, flags); if (pn == NULL) return (NULL); pn->pn_fill = fill; pn->pn_attr = attr; pn->pn_vis = vis; pn->pn_destroy = destroy; pn->pn_flags = flags; if (pfs_add_node(parent, pn) != 0) { pfs_destroy(pn); pn = NULL; } return (pn); } /* * Locate a node by name */ struct pfs_node * pfs_find_node(struct pfs_node *parent, const char *name) { struct pfs_node *pn; pfs_lock(parent); for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next) if (strcmp(pn->pn_name, name) == 0) break; pfs_unlock(parent); return (pn); } /* * Destroy a node and all its descendants. If the node to be destroyed * has a parent, the parent's mutex must be held. */ int pfs_destroy(struct pfs_node *pn) { struct pfs_node *iter; KASSERT(pn != NULL, ("%s(): node is NULL", __func__)); KASSERT(pn->pn_info != NULL, ("%s(): node has no pn_info", __func__)); if (pn->pn_parent) pfs_detach_node(pn); /* destroy children */ if (pn->pn_type == pfstype_dir || pn->pn_type == pfstype_procdir || pn->pn_type == pfstype_root) { pfs_lock(pn); while (pn->pn_nodes != NULL) { iter = pn->pn_nodes; pn->pn_nodes = iter->pn_next; iter->pn_parent = NULL; pfs_unlock(pn); pfs_destroy(iter); pfs_lock(pn); } pfs_unlock(pn); } /* revoke vnodes and fileno */ pfs_purge(pn); /* callback to free any private resources */ if (pn->pn_destroy != NULL) pn_destroy(pn); /* destroy the node */ pfs_fileno_free(pn); mtx_destroy(&pn->pn_mutex); free(pn, M_PFSNODES); return (0); } /* * Mount a pseudofs instance */ int pfs_mount(struct pfs_info *pi, struct mount *mp) { struct statfs *sbp; if (mp->mnt_flag & MNT_UPDATE) return (EOPNOTSUPP); MNT_ILOCK(mp); mp->mnt_flag |= MNT_LOCAL; mp->mnt_kern_flag |= MNTK_NOMSYNC; MNT_IUNLOCK(mp); mp->mnt_data = pi; vfs_getnewfsid(mp); sbp = &mp->mnt_stat; vfs_mountedfrom(mp, pi->pi_name); sbp->f_bsize = PAGE_SIZE; sbp->f_iosize = PAGE_SIZE; sbp->f_blocks = 2; sbp->f_bfree = 2; sbp->f_bavail = 2; sbp->f_files = 0; sbp->f_ffree = 0; return (0); } /* * Compatibility shim for old mount(2) system call */ int pfs_cmount(struct mntarg *ma, void *data, uint64_t flags) { int error; error = kernel_mount(ma, flags); return (error); } /* * Unmount a pseudofs instance */ int pfs_unmount(struct mount *mp, int mntflags) { int error; error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, curthread); return (error); } /* * Return a root vnode */ int pfs_root(struct mount *mp, int flags, struct vnode **vpp) { struct pfs_info *pi; pi = (struct pfs_info *)mp->mnt_data; return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID)); } /* * Return filesystem stats */ int pfs_statfs(struct mount *mp, struct statfs *sbp) { /* no-op: always called with mp->mnt_stat */ return (0); } /* * Initialize a pseudofs instance */ int pfs_init(struct pfs_info *pi, struct vfsconf *vfc) { struct pfs_node *root; int error; pfs_fileno_init(pi); /* set up the root directory */ root = pfs_alloc_node(pi, "/", pfstype_root); pi->pi_root = root; pfs_fileno_alloc(root); pfs_fixup_dir(root); /* construct file hierarchy */ error = (pi->pi_init)(pi, vfc); if (error) { pfs_destroy(root); pi->pi_root = NULL; return (error); } if (bootverbose) printf("%s registered\n", pi->pi_name); return (0); } /* * Destroy a pseudofs instance */ int pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) { int error; pfs_destroy(pi->pi_root); pi->pi_root = NULL; pfs_fileno_uninit(pi); if (bootverbose) printf("%s unregistered\n", pi->pi_name); error = (pi->pi_uninit)(pi, vfc); return (error); } /* * Handle load / unload events */ static int pfs_modevent(module_t mod, int evt, void *arg) { switch (evt) { case MOD_LOAD: pfs_vncache_load(); break; case MOD_UNLOAD: case MOD_SHUTDOWN: pfs_vncache_unload(); break; default: return EOPNOTSUPP; break; } return 0; } /* * Module declaration */ static moduledata_t pseudofs_data = { "pseudofs", pfs_modevent, NULL }; DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); MODULE_VERSION(pseudofs, 1);