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