2 * Copyright (c) 2010 Jaakko Heinonen <jh@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/kernel.h>
33 #include <sys/libkern.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
41 #include <fs/devfs/devfs.h>
42 #include <fs/devfs/devfs_int.h>
47 LIST_ENTRY(dirlistent) link;
50 static LIST_HEAD(, dirlistent) devfs_dirlist =
51 LIST_HEAD_INITIALIZER(devfs_dirlist);
53 static MALLOC_DEFINE(M_DEVFS4, "DEVFS4", "DEVFS directory list");
55 static struct mtx dirlist_mtx;
56 MTX_SYSINIT(dirlist_mtx, &dirlist_mtx, "devfs dirlist lock", MTX_DEF);
58 /* Returns 1 if the path is in the directory list. */
60 devfs_dir_find(const char *path)
62 struct dirlistent *dle;
64 mtx_lock(&dirlist_mtx);
65 LIST_FOREACH(dle, &devfs_dirlist, link) {
66 if (devfs_pathpath(dle->dir, path) != 0) {
67 mtx_unlock(&dirlist_mtx);
71 mtx_unlock(&dirlist_mtx);
76 static struct dirlistent *
77 devfs_dir_findent_locked(const char *dir)
79 struct dirlistent *dle;
81 mtx_assert(&dirlist_mtx, MA_OWNED);
83 LIST_FOREACH(dle, &devfs_dirlist, link) {
84 if (strcmp(dir, dle->dir) == 0)
92 devfs_dir_ref(const char *dir)
94 struct dirlistent *dle, *dle_new;
99 dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK);
100 dle_new->dir = strdup(dir, M_DEVFS4);
103 mtx_lock(&dirlist_mtx);
104 dle = devfs_dir_findent_locked(dir);
107 mtx_unlock(&dirlist_mtx);
108 free(dle_new->dir, M_DEVFS4);
109 free(dle_new, M_DEVFS4);
112 LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link);
113 mtx_unlock(&dirlist_mtx);
117 devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de)
119 char dirname[SPECNAMELEN + 1], *namep;
121 namep = devfs_fqpn(dirname, dm, de, NULL);
122 KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep"));
124 devfs_dir_ref(namep);
128 devfs_dir_unref(const char *dir)
130 struct dirlistent *dle;
135 mtx_lock(&dirlist_mtx);
136 dle = devfs_dir_findent_locked(dir);
137 KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir));
139 KASSERT(dle->refcnt >= 0, ("devfs_dir_unref: negative refcnt"));
140 if (dle->refcnt == 0) {
141 LIST_REMOVE(dle, link);
142 mtx_unlock(&dirlist_mtx);
143 free(dle->dir, M_DEVFS4);
146 mtx_unlock(&dirlist_mtx);
150 devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de)
152 char dirname[SPECNAMELEN + 1], *namep;
154 namep = devfs_fqpn(dirname, dm, de, NULL);
155 KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep"));
157 devfs_dir_unref(namep);
160 /* Returns 1 if the path p1 contains the path p2. */
162 devfs_pathpath(const char *p1, const char *p2)
167 if (*p1 == '/' && *p2 == '\0')
171 } else if (*p1 == '\0')