1d318c565SJaakko Heinonen /*- 2*d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d63027b6SPedro F. Giffuni * 4d318c565SJaakko Heinonen * Copyright (c) 2010 Jaakko Heinonen <jh@FreeBSD.org> 5d318c565SJaakko Heinonen * All rights reserved. 6d318c565SJaakko Heinonen * 7d318c565SJaakko Heinonen * Redistribution and use in source and binary forms, with or without 8d318c565SJaakko Heinonen * modification, are permitted provided that the following conditions 9d318c565SJaakko Heinonen * are met: 10d318c565SJaakko Heinonen * 1. Redistributions of source code must retain the above copyright 11d318c565SJaakko Heinonen * notice, this list of conditions and the following disclaimer. 12d318c565SJaakko Heinonen * 2. Redistributions in binary form must reproduce the above copyright 13d318c565SJaakko Heinonen * notice, this list of conditions and the following disclaimer in the 14d318c565SJaakko Heinonen * documentation and/or other materials provided with the distribution. 15d318c565SJaakko Heinonen * 16d318c565SJaakko Heinonen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17d318c565SJaakko Heinonen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18d318c565SJaakko Heinonen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19d318c565SJaakko Heinonen * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20d318c565SJaakko Heinonen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21d318c565SJaakko Heinonen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22d318c565SJaakko Heinonen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23d318c565SJaakko Heinonen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24d318c565SJaakko Heinonen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25d318c565SJaakko Heinonen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26d318c565SJaakko Heinonen * SUCH DAMAGE. 27d318c565SJaakko Heinonen * 28d318c565SJaakko Heinonen * $FreeBSD$ 29d318c565SJaakko Heinonen */ 30d318c565SJaakko Heinonen 31d318c565SJaakko Heinonen #include <sys/param.h> 32d318c565SJaakko Heinonen #include <sys/conf.h> 33d318c565SJaakko Heinonen #include <sys/types.h> 34d318c565SJaakko Heinonen #include <sys/kernel.h> 35d318c565SJaakko Heinonen #include <sys/libkern.h> 36d318c565SJaakko Heinonen #include <sys/lock.h> 37d318c565SJaakko Heinonen #include <sys/malloc.h> 38d318c565SJaakko Heinonen #include <sys/mutex.h> 39d318c565SJaakko Heinonen #include <sys/queue.h> 40d318c565SJaakko Heinonen #include <sys/systm.h> 41d318c565SJaakko Heinonen #include <sys/sx.h> 42d318c565SJaakko Heinonen 43d318c565SJaakko Heinonen #include <fs/devfs/devfs.h> 44d318c565SJaakko Heinonen #include <fs/devfs/devfs_int.h> 45d318c565SJaakko Heinonen 46d318c565SJaakko Heinonen struct dirlistent { 47d318c565SJaakko Heinonen char *dir; 48d318c565SJaakko Heinonen int refcnt; 49d318c565SJaakko Heinonen LIST_ENTRY(dirlistent) link; 50d318c565SJaakko Heinonen }; 51d318c565SJaakko Heinonen 52d318c565SJaakko Heinonen static LIST_HEAD(, dirlistent) devfs_dirlist = 53d318c565SJaakko Heinonen LIST_HEAD_INITIALIZER(devfs_dirlist); 54d318c565SJaakko Heinonen 55d318c565SJaakko Heinonen static MALLOC_DEFINE(M_DEVFS4, "DEVFS4", "DEVFS directory list"); 56d318c565SJaakko Heinonen 57d318c565SJaakko Heinonen static struct mtx dirlist_mtx; 58d318c565SJaakko Heinonen MTX_SYSINIT(dirlist_mtx, &dirlist_mtx, "devfs dirlist lock", MTX_DEF); 59d318c565SJaakko Heinonen 60d318c565SJaakko Heinonen /* Returns 1 if the path is in the directory list. */ 61d318c565SJaakko Heinonen int 62d318c565SJaakko Heinonen devfs_dir_find(const char *path) 63d318c565SJaakko Heinonen { 64d318c565SJaakko Heinonen struct dirlistent *dle; 65d318c565SJaakko Heinonen 66d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx); 67d318c565SJaakko Heinonen LIST_FOREACH(dle, &devfs_dirlist, link) { 68d318c565SJaakko Heinonen if (devfs_pathpath(dle->dir, path) != 0) { 69d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 70d318c565SJaakko Heinonen return (1); 71d318c565SJaakko Heinonen } 72d318c565SJaakko Heinonen } 73d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 74d318c565SJaakko Heinonen 75d318c565SJaakko Heinonen return (0); 76d318c565SJaakko Heinonen } 77d318c565SJaakko Heinonen 78d318c565SJaakko Heinonen static struct dirlistent * 79d318c565SJaakko Heinonen devfs_dir_findent_locked(const char *dir) 80d318c565SJaakko Heinonen { 81d318c565SJaakko Heinonen struct dirlistent *dle; 82d318c565SJaakko Heinonen 83d318c565SJaakko Heinonen mtx_assert(&dirlist_mtx, MA_OWNED); 84d318c565SJaakko Heinonen 85d318c565SJaakko Heinonen LIST_FOREACH(dle, &devfs_dirlist, link) { 86d318c565SJaakko Heinonen if (strcmp(dir, dle->dir) == 0) 87d318c565SJaakko Heinonen return (dle); 88d318c565SJaakko Heinonen } 89d318c565SJaakko Heinonen 90d318c565SJaakko Heinonen return (NULL); 91d318c565SJaakko Heinonen } 92d318c565SJaakko Heinonen 93d318c565SJaakko Heinonen static void 94d318c565SJaakko Heinonen devfs_dir_ref(const char *dir) 95d318c565SJaakko Heinonen { 96d318c565SJaakko Heinonen struct dirlistent *dle, *dle_new; 97d318c565SJaakko Heinonen 98d318c565SJaakko Heinonen if (*dir == '\0') 99d318c565SJaakko Heinonen return; 100d318c565SJaakko Heinonen 101d318c565SJaakko Heinonen dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK); 102d318c565SJaakko Heinonen dle_new->dir = strdup(dir, M_DEVFS4); 103d318c565SJaakko Heinonen dle_new->refcnt = 1; 104d318c565SJaakko Heinonen 105d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx); 106d318c565SJaakko Heinonen dle = devfs_dir_findent_locked(dir); 107d318c565SJaakko Heinonen if (dle != NULL) { 108d318c565SJaakko Heinonen dle->refcnt++; 109d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 110d318c565SJaakko Heinonen free(dle_new->dir, M_DEVFS4); 111d318c565SJaakko Heinonen free(dle_new, M_DEVFS4); 112d318c565SJaakko Heinonen return; 113d318c565SJaakko Heinonen } 114d318c565SJaakko Heinonen LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link); 115d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 116d318c565SJaakko Heinonen } 117d318c565SJaakko Heinonen 118d318c565SJaakko Heinonen void 119d318c565SJaakko Heinonen devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de) 120d318c565SJaakko Heinonen { 121d318c565SJaakko Heinonen char dirname[SPECNAMELEN + 1], *namep; 122d318c565SJaakko Heinonen 123d318c565SJaakko Heinonen namep = devfs_fqpn(dirname, dm, de, NULL); 124d318c565SJaakko Heinonen KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep")); 125d318c565SJaakko Heinonen 126d318c565SJaakko Heinonen devfs_dir_ref(namep); 127d318c565SJaakko Heinonen } 128d318c565SJaakko Heinonen 129d318c565SJaakko Heinonen static void 130d318c565SJaakko Heinonen devfs_dir_unref(const char *dir) 131d318c565SJaakko Heinonen { 132d318c565SJaakko Heinonen struct dirlistent *dle; 133d318c565SJaakko Heinonen 134d318c565SJaakko Heinonen if (*dir == '\0') 135d318c565SJaakko Heinonen return; 136d318c565SJaakko Heinonen 137d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx); 138d318c565SJaakko Heinonen dle = devfs_dir_findent_locked(dir); 139d318c565SJaakko Heinonen KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir)); 140d318c565SJaakko Heinonen dle->refcnt--; 141d318c565SJaakko Heinonen KASSERT(dle->refcnt >= 0, ("devfs_dir_unref: negative refcnt")); 142d318c565SJaakko Heinonen if (dle->refcnt == 0) { 143d318c565SJaakko Heinonen LIST_REMOVE(dle, link); 144d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 145d318c565SJaakko Heinonen free(dle->dir, M_DEVFS4); 146d318c565SJaakko Heinonen free(dle, M_DEVFS4); 147d318c565SJaakko Heinonen } else 148d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx); 149d318c565SJaakko Heinonen } 150d318c565SJaakko Heinonen 151d318c565SJaakko Heinonen void 152d318c565SJaakko Heinonen devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de) 153d318c565SJaakko Heinonen { 154d318c565SJaakko Heinonen char dirname[SPECNAMELEN + 1], *namep; 155d318c565SJaakko Heinonen 156d318c565SJaakko Heinonen namep = devfs_fqpn(dirname, dm, de, NULL); 157d318c565SJaakko Heinonen KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep")); 158d318c565SJaakko Heinonen 159d318c565SJaakko Heinonen devfs_dir_unref(namep); 160d318c565SJaakko Heinonen } 161d318c565SJaakko Heinonen 162d318c565SJaakko Heinonen /* Returns 1 if the path p1 contains the path p2. */ 163d318c565SJaakko Heinonen int 164d318c565SJaakko Heinonen devfs_pathpath(const char *p1, const char *p2) 165d318c565SJaakko Heinonen { 166d318c565SJaakko Heinonen 167d318c565SJaakko Heinonen for (;;p1++, p2++) { 168d318c565SJaakko Heinonen if (*p1 != *p2) { 169d318c565SJaakko Heinonen if (*p1 == '/' && *p2 == '\0') 170d318c565SJaakko Heinonen return (1); 171d318c565SJaakko Heinonen else 172d318c565SJaakko Heinonen return (0); 173d318c565SJaakko Heinonen } else if (*p1 == '\0') 174d318c565SJaakko Heinonen return (1); 175d318c565SJaakko Heinonen } 176d318c565SJaakko Heinonen /* NOTREACHED */ 177d318c565SJaakko Heinonen } 178