1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010 Jaakko Heinonen <jh@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/types.h>
34 #include <sys/kernel.h>
35 #include <sys/libkern.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/systm.h>
41 #include <sys/sx.h>
42
43 #include <fs/devfs/devfs.h>
44 #include <fs/devfs/devfs_int.h>
45
46 struct dirlistent {
47 char *dir;
48 int refcnt;
49 LIST_ENTRY(dirlistent) link;
50 };
51
52 static LIST_HEAD(, dirlistent) devfs_dirlist =
53 LIST_HEAD_INITIALIZER(devfs_dirlist);
54
55 static MALLOC_DEFINE(M_DEVFS4, "DEVFS4", "DEVFS directory list");
56
57 static struct mtx dirlist_mtx;
58 MTX_SYSINIT(dirlist_mtx, &dirlist_mtx, "devfs dirlist lock", MTX_DEF);
59
60 /* Returns 1 if the path is in the directory list. */
61 int
62 devfs_dir_find(const char *path)
63 {
64 struct dirlistent *dle;
65
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);
70 return (1);
71 }
72 }
73 mtx_unlock(&dirlist_mtx);
74
75 return (0);
76 }
77
78 static struct dirlistent *
79 devfs_dir_findent_locked(const char *dir)
80 {
81 struct dirlistent *dle;
82
83 mtx_assert(&dirlist_mtx, MA_OWNED);
84
85 LIST_FOREACH(dle, &devfs_dirlist, link) {
86 if (strcmp(dir, dle->dir) == 0)
87 return (dle);
88 }
89
90 return (NULL);
91 }
92
93 static void
94 devfs_dir_ref(const char *dir)
95 {
96 struct dirlistent *dle, *dle_new;
97
98 if (*dir == '\0')
99 return;
100
101 dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK);
102 dle_new->dir = strdup(dir, M_DEVFS4);
103 dle_new->refcnt = 1;
104
105 mtx_lock(&dirlist_mtx);
106 dle = devfs_dir_findent_locked(dir);
107 if (dle != NULL) {
108 dle->refcnt++;
109 mtx_unlock(&dirlist_mtx);
110 free(dle_new->dir, M_DEVFS4);
111 free(dle_new, M_DEVFS4);
112 return;
113 }
114 LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link);
115 mtx_unlock(&dirlist_mtx);
116 }
117
118 void
119 devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de)
120 {
121 char dirname[SPECNAMELEN + 1], *namep;
122
123 namep = devfs_fqpn(dirname, dm, de, NULL);
124 KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep"));
125
126 devfs_dir_ref(namep);
127 }
128
129 static void
130 devfs_dir_unref(const char *dir)
131 {
132 struct dirlistent *dle;
133
134 if (*dir == '\0')
135 return;
136
137 mtx_lock(&dirlist_mtx);
138 dle = devfs_dir_findent_locked(dir);
139 KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir));
140 dle->refcnt--;
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);
146 free(dle, M_DEVFS4);
147 } else
148 mtx_unlock(&dirlist_mtx);
149 }
150
151 void
152 devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de)
153 {
154 char dirname[SPECNAMELEN + 1], *namep;
155
156 namep = devfs_fqpn(dirname, dm, de, NULL);
157 KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep"));
158
159 devfs_dir_unref(namep);
160 }
161
162 /* Returns 1 if the path p1 contains the path p2. */
163 int
164 devfs_pathpath(const char *p1, const char *p2)
165 {
166
167 for (;;p1++, p2++) {
168 if (*p1 != *p2) {
169 if (*p1 == '/' && *p2 == '\0')
170 return (1);
171 else
172 return (0);
173 } else if (*p1 == '\0')
174 return (1);
175 }
176 /* NOTREACHED */
177 }
Cache object: 1b0e02c25fb1f29fb4fd8e2e53c02c1b
|