xref: /freebsd/sys/fs/devfs/devfs_dir.c (revision 31e8b07376cebdb87648d977a2f1c10e024b67d0)
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 	mtx_lock(&dirlist_mtx);
102 	dle = devfs_dir_findent_locked(dir);
103 	if (dle != NULL) {
104 		dle->refcnt++;
105 		mtx_unlock(&dirlist_mtx);
106 		return;
107 	}
108 
109 	dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK);
110 	dle_new->dir = strdup(dir, M_DEVFS4);
111 	dle_new->refcnt = 1;
112 
113 	LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link);
114 	mtx_unlock(&dirlist_mtx);
115 }
116 
117 void
118 devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de)
119 {
120 	char dirname[SPECNAMELEN + 1], *namep;
121 
122 	namep = devfs_fqpn(dirname, dm, de, NULL);
123 	KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep"));
124 
125 	devfs_dir_ref(namep);
126 }
127 
128 static void
129 devfs_dir_unref(const char *dir)
130 {
131 	struct dirlistent *dle;
132 
133 	if (*dir == '\0')
134 		return;
135 
136 	mtx_lock(&dirlist_mtx);
137 	dle = devfs_dir_findent_locked(dir);
138 	KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir));
139 	dle->refcnt--;
140 	KASSERT(dle->refcnt >= 0, ("devfs_dir_unref: negative refcnt"));
141 	if (dle->refcnt == 0) {
142 		LIST_REMOVE(dle, link);
143 		mtx_unlock(&dirlist_mtx);
144 		free(dle->dir, M_DEVFS4);
145 		free(dle, M_DEVFS4);
146 	} else
147 		mtx_unlock(&dirlist_mtx);
148 }
149 
150 void
151 devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de)
152 {
153 	char dirname[SPECNAMELEN + 1], *namep;
154 
155 	namep = devfs_fqpn(dirname, dm, de, NULL);
156 	KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep"));
157 
158 	devfs_dir_unref(namep);
159 }
160 
161 /* Returns 1 if the path p1 contains the path p2. */
162 int
163 devfs_pathpath(const char *p1, const char *p2)
164 {
165 
166 	for (;;p1++, p2++) {
167 		if (*p1 != *p2) {
168 			if (*p1 == '/' && *p2 == '\0')
169 				return (1);
170 			else
171 				return (0);
172 		} else if (*p1 == '\0')
173 			return (1);
174 	}
175 	/* NOTREACHED */
176 }
177