2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Jaakko Heinonen <jh@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/kernel.h>
35 #include <sys/libkern.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/systm.h>
43 #include <fs/devfs/devfs.h>
44 #include <fs/devfs/devfs_int.h>
49 LIST_ENTRY(dirlistent) link;
52 static LIST_HEAD(, dirlistent) devfs_dirlist =
53 LIST_HEAD_INITIALIZER(devfs_dirlist);
55 static MALLOC_DEFINE(M_DEVFS4, "DEVFS4", "DEVFS directory list");
57 static struct mtx dirlist_mtx;
58 MTX_SYSINIT(dirlist_mtx, &dirlist_mtx, "devfs dirlist lock", MTX_DEF);
60 /* Returns 1 if the path is in the directory list. */
62 devfs_dir_find(const char *path)
64 struct dirlistent *dle;
66 mtx_lock(&dirlist_mtx);
67 LIST_FOREACH(dle, &devfs_dirlist, link) {
68 if (devfs_pathpath(dle->dir, path) != 0) {
69 mtx_unlock(&dirlist_mtx);
73 mtx_unlock(&dirlist_mtx);
78 static struct dirlistent *
79 devfs_dir_findent_locked(const char *dir)
81 struct dirlistent *dle;
83 mtx_assert(&dirlist_mtx, MA_OWNED);
85 LIST_FOREACH(dle, &devfs_dirlist, link) {
86 if (strcmp(dir, dle->dir) == 0)
94 devfs_dir_ref(const char *dir)
96 struct dirlistent *dle, *dle_new;
101 dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK);
102 dle_new->dir = strdup(dir, M_DEVFS4);
105 mtx_lock(&dirlist_mtx);
106 dle = devfs_dir_findent_locked(dir);
109 mtx_unlock(&dirlist_mtx);
110 free(dle_new->dir, M_DEVFS4);
111 free(dle_new, M_DEVFS4);
114 LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link);
115 mtx_unlock(&dirlist_mtx);
119 devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de)
121 char dirname[SPECNAMELEN + 1], *namep;
123 namep = devfs_fqpn(dirname, dm, de, NULL);
124 KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep"));
126 devfs_dir_ref(namep);
130 devfs_dir_unref(const char *dir)
132 struct dirlistent *dle;
137 mtx_lock(&dirlist_mtx);
138 dle = devfs_dir_findent_locked(dir);
139 KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir));
141 KASSERT(dle->refcnt >= 0, ("devfs_dir_unref: negative refcnt"));
142 if (dle->refcnt == 0) {
143 LIST_REMOVE(dle, link);
144 mtx_unlock(&dirlist_mtx);
145 free(dle->dir, M_DEVFS4);
148 mtx_unlock(&dirlist_mtx);
152 devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de)
154 char dirname[SPECNAMELEN + 1], *namep;
156 namep = devfs_fqpn(dirname, dm, de, NULL);
157 KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep"));
159 devfs_dir_unref(namep);
162 /* Returns 1 if the path p1 contains the path p2. */
164 devfs_pathpath(const char *p1, const char *p2)
169 if (*p1 == '/' && *p2 == '\0')
173 } else if (*p1 == '\0')