1d318c565SJaakko Heinonen /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d63027b6SPedro 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
29d318c565SJaakko Heinonen #include <sys/param.h>
30d318c565SJaakko Heinonen #include <sys/conf.h>
31d318c565SJaakko Heinonen #include <sys/types.h>
32d318c565SJaakko Heinonen #include <sys/kernel.h>
33d318c565SJaakko Heinonen #include <sys/libkern.h>
34d318c565SJaakko Heinonen #include <sys/lock.h>
35d318c565SJaakko Heinonen #include <sys/malloc.h>
36d318c565SJaakko Heinonen #include <sys/mutex.h>
37d318c565SJaakko Heinonen #include <sys/queue.h>
38d318c565SJaakko Heinonen #include <sys/systm.h>
39d318c565SJaakko Heinonen #include <sys/sx.h>
40d318c565SJaakko Heinonen
41d318c565SJaakko Heinonen #include <fs/devfs/devfs.h>
42d318c565SJaakko Heinonen #include <fs/devfs/devfs_int.h>
43d318c565SJaakko Heinonen
44d318c565SJaakko Heinonen struct dirlistent {
45d318c565SJaakko Heinonen char *dir;
46d318c565SJaakko Heinonen int refcnt;
47d318c565SJaakko Heinonen LIST_ENTRY(dirlistent) link;
48d318c565SJaakko Heinonen };
49d318c565SJaakko Heinonen
50d318c565SJaakko Heinonen static LIST_HEAD(, dirlistent) devfs_dirlist =
51d318c565SJaakko Heinonen LIST_HEAD_INITIALIZER(devfs_dirlist);
52d318c565SJaakko Heinonen
53d318c565SJaakko Heinonen static MALLOC_DEFINE(M_DEVFS4, "DEVFS4", "DEVFS directory list");
54d318c565SJaakko Heinonen
55d318c565SJaakko Heinonen static struct mtx dirlist_mtx;
56d318c565SJaakko Heinonen MTX_SYSINIT(dirlist_mtx, &dirlist_mtx, "devfs dirlist lock", MTX_DEF);
57d318c565SJaakko Heinonen
58d318c565SJaakko Heinonen /* Returns 1 if the path is in the directory list. */
59d318c565SJaakko Heinonen int
devfs_dir_find(const char * path)60d318c565SJaakko Heinonen devfs_dir_find(const char *path)
61d318c565SJaakko Heinonen {
62d318c565SJaakko Heinonen struct dirlistent *dle;
63d318c565SJaakko Heinonen
64d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx);
65d318c565SJaakko Heinonen LIST_FOREACH(dle, &devfs_dirlist, link) {
66d318c565SJaakko Heinonen if (devfs_pathpath(dle->dir, path) != 0) {
67d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
68d318c565SJaakko Heinonen return (1);
69d318c565SJaakko Heinonen }
70d318c565SJaakko Heinonen }
71d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
72d318c565SJaakko Heinonen
73d318c565SJaakko Heinonen return (0);
74d318c565SJaakko Heinonen }
75d318c565SJaakko Heinonen
76d318c565SJaakko Heinonen static struct dirlistent *
devfs_dir_findent_locked(const char * dir)77d318c565SJaakko Heinonen devfs_dir_findent_locked(const char *dir)
78d318c565SJaakko Heinonen {
79d318c565SJaakko Heinonen struct dirlistent *dle;
80d318c565SJaakko Heinonen
81d318c565SJaakko Heinonen mtx_assert(&dirlist_mtx, MA_OWNED);
82d318c565SJaakko Heinonen
83d318c565SJaakko Heinonen LIST_FOREACH(dle, &devfs_dirlist, link) {
84d318c565SJaakko Heinonen if (strcmp(dir, dle->dir) == 0)
85d318c565SJaakko Heinonen return (dle);
86d318c565SJaakko Heinonen }
87d318c565SJaakko Heinonen
88d318c565SJaakko Heinonen return (NULL);
89d318c565SJaakko Heinonen }
90d318c565SJaakko Heinonen
91d318c565SJaakko Heinonen static void
devfs_dir_ref(const char * dir)92d318c565SJaakko Heinonen devfs_dir_ref(const char *dir)
93d318c565SJaakko Heinonen {
94d318c565SJaakko Heinonen struct dirlistent *dle, *dle_new;
95d318c565SJaakko Heinonen
96d318c565SJaakko Heinonen if (*dir == '\0')
97d318c565SJaakko Heinonen return;
98d318c565SJaakko Heinonen
9938c9c65eSEmmanuel Vadot dle_new = malloc(sizeof(*dle), M_DEVFS4, M_WAITOK);
10038c9c65eSEmmanuel Vadot dle_new->dir = strdup(dir, M_DEVFS4);
10138c9c65eSEmmanuel Vadot dle_new->refcnt = 1;
10238c9c65eSEmmanuel Vadot
103d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx);
104d318c565SJaakko Heinonen dle = devfs_dir_findent_locked(dir);
105d318c565SJaakko Heinonen if (dle != NULL) {
106d318c565SJaakko Heinonen dle->refcnt++;
107d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
10838c9c65eSEmmanuel Vadot free(dle_new->dir, M_DEVFS4);
10938c9c65eSEmmanuel Vadot free(dle_new, M_DEVFS4);
110d318c565SJaakko Heinonen return;
111d318c565SJaakko Heinonen }
112d318c565SJaakko Heinonen LIST_INSERT_HEAD(&devfs_dirlist, dle_new, link);
113d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
114d318c565SJaakko Heinonen }
115d318c565SJaakko Heinonen
116d318c565SJaakko Heinonen void
devfs_dir_ref_de(struct devfs_mount * dm,struct devfs_dirent * de)117d318c565SJaakko Heinonen devfs_dir_ref_de(struct devfs_mount *dm, struct devfs_dirent *de)
118d318c565SJaakko Heinonen {
119d318c565SJaakko Heinonen char dirname[SPECNAMELEN + 1], *namep;
120d318c565SJaakko Heinonen
121d318c565SJaakko Heinonen namep = devfs_fqpn(dirname, dm, de, NULL);
122d318c565SJaakko Heinonen KASSERT(namep != NULL, ("devfs_ref_dir_de: NULL namep"));
123d318c565SJaakko Heinonen
124d318c565SJaakko Heinonen devfs_dir_ref(namep);
125d318c565SJaakko Heinonen }
126d318c565SJaakko Heinonen
127d318c565SJaakko Heinonen static void
devfs_dir_unref(const char * dir)128d318c565SJaakko Heinonen devfs_dir_unref(const char *dir)
129d318c565SJaakko Heinonen {
130d318c565SJaakko Heinonen struct dirlistent *dle;
131d318c565SJaakko Heinonen
132d318c565SJaakko Heinonen if (*dir == '\0')
133d318c565SJaakko Heinonen return;
134d318c565SJaakko Heinonen
135d318c565SJaakko Heinonen mtx_lock(&dirlist_mtx);
136d318c565SJaakko Heinonen dle = devfs_dir_findent_locked(dir);
137d318c565SJaakko Heinonen KASSERT(dle != NULL, ("devfs_dir_unref: dir %s not referenced", dir));
138d318c565SJaakko Heinonen dle->refcnt--;
139d318c565SJaakko Heinonen KASSERT(dle->refcnt >= 0, ("devfs_dir_unref: negative refcnt"));
140d318c565SJaakko Heinonen if (dle->refcnt == 0) {
141d318c565SJaakko Heinonen LIST_REMOVE(dle, link);
142d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
143d318c565SJaakko Heinonen free(dle->dir, M_DEVFS4);
144d318c565SJaakko Heinonen free(dle, M_DEVFS4);
145d318c565SJaakko Heinonen } else
146d318c565SJaakko Heinonen mtx_unlock(&dirlist_mtx);
147d318c565SJaakko Heinonen }
148d318c565SJaakko Heinonen
149d318c565SJaakko Heinonen void
devfs_dir_unref_de(struct devfs_mount * dm,struct devfs_dirent * de)150d318c565SJaakko Heinonen devfs_dir_unref_de(struct devfs_mount *dm, struct devfs_dirent *de)
151d318c565SJaakko Heinonen {
152d318c565SJaakko Heinonen char dirname[SPECNAMELEN + 1], *namep;
153d318c565SJaakko Heinonen
154d318c565SJaakko Heinonen namep = devfs_fqpn(dirname, dm, de, NULL);
155d318c565SJaakko Heinonen KASSERT(namep != NULL, ("devfs_unref_dir_de: NULL namep"));
156d318c565SJaakko Heinonen
157d318c565SJaakko Heinonen devfs_dir_unref(namep);
158d318c565SJaakko Heinonen }
159d318c565SJaakko Heinonen
160d318c565SJaakko Heinonen /* Returns 1 if the path p1 contains the path p2. */
161d318c565SJaakko Heinonen int
devfs_pathpath(const char * p1,const char * p2)162d318c565SJaakko Heinonen devfs_pathpath(const char *p1, const char *p2)
163d318c565SJaakko Heinonen {
164d318c565SJaakko Heinonen
165d318c565SJaakko Heinonen for (;;p1++, p2++) {
166d318c565SJaakko Heinonen if (*p1 != *p2) {
167d318c565SJaakko Heinonen if (*p1 == '/' && *p2 == '\0')
168d318c565SJaakko Heinonen return (1);
169d318c565SJaakko Heinonen else
170d318c565SJaakko Heinonen return (0);
171d318c565SJaakko Heinonen } else if (*p1 == '\0')
172d318c565SJaakko Heinonen return (1);
173d318c565SJaakko Heinonen }
174d318c565SJaakko Heinonen /* NOTREACHED */
175d318c565SJaakko Heinonen }
176