xref: /freebsd/sys/fs/devfs/devfs_devs.c (revision 9886bcdf9326929b650dc843802a25400f597365)
1 #define DEBUG 1
2 /*
3  * Copyright (c) 2000
4  *	Poul-Henning Kamp.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Neither the name of the University nor the names of its contributors
12  *    may be used to endorse or promote products derived from this software
13  *    without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
28  *
29  * $FreeBSD$
30  */
31 
32 #include "opt_devfs.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/dirent.h>
37 #include <sys/conf.h>
38 #include <sys/vnode.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 
44 #include <machine/atomic.h>
45 
46 #include <fs/devfs/devfs.h>
47 
48 static dev_t devfs_inot[NDEVFSINO];
49 static dev_t *devfs_overflow;
50 static int devfs_ref[NDEVFSINO];
51 static int *devfs_refoverflow;
52 static int devfs_nextino = 3;
53 static int devfs_numino;
54 static int devfs_topino;
55 static int devfs_noverflowwant = NDEVFSOVERFLOW;
56 static int devfs_noverflow;
57 static unsigned devfs_generation;
58 
59 static void devfs_attemptoverflow(int insist);
60 static struct devfs_dirent *devfs_find (struct devfs_dirent *dd, const char *name, int namelen);
61 
62 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
63 SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW,
64 	&devfs_noverflowwant, 0, "Size of DEVFS overflow table");
65 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
66 	&devfs_generation, 0, "DEVFS generation number");
67 SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD,
68 	&devfs_numino, 0, "DEVFS inodes");
69 SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD,
70 	&devfs_topino, 0, "DEVFS highest inode#");
71 
72 static int *
73 devfs_itor(int inode)
74 {
75 	if (inode < NDEVFSINO)
76 		return (&devfs_ref[inode]);
77 	else if (inode < NDEVFSINO + devfs_noverflow)
78 		return (&devfs_refoverflow[inode - NDEVFSINO]);
79 	else
80 		panic ("YRK!");
81 }
82 
83 static void
84 devfs_dropref(int inode)
85 {
86 	int *ip;
87 
88 	ip = devfs_itor(inode);
89 	atomic_add_int(ip, -1);
90 }
91 
92 static int
93 devfs_getref(int inode)
94 {
95 	int *ip, i, j;
96 	dev_t *dp;
97 
98 	ip = devfs_itor(inode);
99 	dp = devfs_itod(inode);
100 	for (;;) {
101 		i = *ip;
102 		j = i + 1;
103 		if (!atomic_cmpset_int(ip, i, j))
104 			continue;
105 		if (*dp != NULL)
106 			return (1);
107 		atomic_add_int(ip, -1);
108 		return(0);
109 	}
110 }
111 
112 struct devfs_dirent **
113 devfs_itode (struct devfs_mount *dm, int inode)
114 {
115 
116 	if (inode < NDEVFSINO)
117 		return (&dm->dm_dirent[inode]);
118 	if (devfs_overflow == NULL)
119 		return (NULL);
120 	if (inode < NDEVFSINO + devfs_noverflow)
121 		return (&dm->dm_overflow[inode - NDEVFSINO]);
122 	return (NULL);
123 }
124 
125 dev_t *
126 devfs_itod (int inode)
127 {
128 
129 	if (inode < NDEVFSINO)
130 		return (&devfs_inot[inode]);
131 	if (devfs_overflow == NULL)
132 		return (NULL);
133 	if (inode < NDEVFSINO + devfs_noverflow)
134 		return (&devfs_overflow[inode - NDEVFSINO]);
135 	return (NULL);
136 }
137 
138 static void
139 devfs_attemptoverflow(int insist)
140 {
141 	dev_t **ot;
142 	int *or;
143 	int n, nb;
144 
145 	/* Check if somebody beat us to it */
146 	if (devfs_overflow != NULL)
147 		return;
148 	ot = NULL;
149 	or = NULL;
150 	n = devfs_noverflowwant;
151 	nb = sizeof (struct dev_t *) * n;
152 	MALLOC(ot, dev_t **, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
153 	if (ot == NULL)
154 		goto bail;
155 	nb = sizeof (int) * n;
156 	MALLOC(or, int *, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
157 	if (or == NULL)
158 		goto bail;
159 	if (!atomic_cmpset_ptr(&devfs_overflow, NULL, ot))
160 		goto bail;
161 	devfs_refoverflow = or;
162 	devfs_noverflow = n;
163 	printf("DEVFS Overflow table with %d entries allocated when %d in use\n", n, devfs_numino);
164 	return;
165 
166 bail:
167 	/* Somebody beat us to it, or something went wrong. */
168 	if (ot != NULL)
169 		FREE(ot, M_DEVFS);
170 	if (or != NULL)
171 		FREE(or, M_DEVFS);
172 	return;
173 }
174 
175 static struct devfs_dirent *
176 devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
177 {
178 	struct devfs_dirent *de;
179 
180 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
181 		if (namelen != de->de_dirent->d_namlen)
182 			continue;
183 		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
184 			continue;
185 		break;
186 	}
187 	return (de);
188 }
189 
190 struct devfs_dirent *
191 devfs_newdirent(char *name, int namelen)
192 {
193 	int i;
194 	struct devfs_dirent *de;
195 	struct dirent d;
196 
197 	d.d_namlen = namelen;
198 	i = sizeof (*de) + GENERIC_DIRSIZ(&d);
199 	MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK | M_ZERO);
200 	de->de_dirent = (struct dirent *)(de + 1);
201 	de->de_dirent->d_namlen = namelen;
202 	de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
203 	bcopy(name, de->de_dirent->d_name, namelen + 1);
204 	nanotime(&de->de_ctime);
205 	de->de_mtime = de->de_atime = de->de_ctime;
206 	de->de_links = 1;
207 	return (de);
208 }
209 
210 struct devfs_dirent *
211 devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot)
212 {
213 	struct devfs_dirent *dd;
214 	struct devfs_dirent *de;
215 
216 	dd = devfs_newdirent(name, namelen);
217 
218 	TAILQ_INIT(&dd->de_dlist);
219 
220 	dd->de_dirent->d_type = DT_DIR;
221 	dd->de_mode = 0755;
222 	dd->de_links = 2;
223 	dd->de_dir = dd;
224 
225 	de = devfs_newdirent(".", 1);
226 	de->de_dirent->d_type = DT_DIR;
227 	de->de_dir = dd;
228 	de->de_flags |= DE_DOT;
229 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
230 
231 	de = devfs_newdirent("..", 2);
232 	de->de_dirent->d_type = DT_DIR;
233 	if (dotdot == NULL)
234 		de->de_dir = dd;
235 	else
236 		de->de_dir = dotdot;
237 	de->de_flags |= DE_DOTDOT;
238 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
239 
240 	return (dd);
241 }
242 
243 static void
244 devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de)
245 {
246 
247 	if (de->de_symlink) {
248 		FREE(de->de_symlink, M_DEVFS);
249 		de->de_symlink = NULL;
250 	}
251 	if (de->de_vnode)
252 		de->de_vnode->v_data = NULL;
253 	TAILQ_REMOVE(&dd->de_dlist, de, de_list);
254 	FREE(de, M_DEVFS);
255 }
256 
257 void
258 devfs_purge(struct devfs_dirent *dd)
259 {
260 	struct devfs_dirent *de;
261 
262 	for (;;) {
263 		de = TAILQ_FIRST(&dd->de_dlist);
264 		if (de == NULL)
265 			break;
266 		devfs_delete(dd, de);
267 	}
268 	FREE(dd, M_DEVFS);
269 }
270 
271 
272 int
273 devfs_populate(struct devfs_mount *dm)
274 {
275 	int i, j;
276 	dev_t dev, pdev;
277 	struct devfs_dirent *dd;
278 	struct devfs_dirent *de, **dep;
279 	char *q, *s;
280 
281 	if (dm->dm_generation == devfs_generation)
282 		return (0);
283 	lockmgr(&dm->dm_lock, LK_UPGRADE, 0, curproc);
284 	if (devfs_noverflow && dm->dm_overflow == NULL) {
285 		i = devfs_noverflow * sizeof (struct devfs_dirent *);
286 		MALLOC(dm->dm_overflow, struct devfs_dirent **, i,
287 			M_DEVFS, M_WAITOK | M_ZERO);
288 	}
289 	while (dm->dm_generation != devfs_generation) {
290 		dm->dm_generation = devfs_generation;
291 		for (i = 0; i <= devfs_topino; i++) {
292 			dev = *devfs_itod(i);
293 			dep = devfs_itode(dm, i);
294 			de = *dep;
295 			if (dev == NULL && de == DE_DELETED) {
296 				*dep = NULL;
297 				continue;
298 			}
299 			if (dev == NULL && de != NULL) {
300 				dd = de->de_dir;
301 				*dep = NULL;
302 				TAILQ_REMOVE(&dd->de_dlist, de, de_list);
303 				if (de->de_vnode)
304 					de->de_vnode->v_data = NULL;
305 				FREE(de, M_DEVFS);
306 				devfs_dropref(i);
307 				continue;
308 			}
309 			if (dev == NULL)
310 				continue;
311 			if (de != NULL)
312 				continue;
313 			if (!devfs_getref(i))
314 				continue;
315 			dd = dm->dm_basedir;
316 			s = dev->si_name;
317 			for (;;) {
318 				for (q = s; *q != '/' && *q != '\0'; q++)
319 					continue;
320 				if (*q != '/')
321 					break;
322 				de = devfs_find(dd, s, q - s);
323 				if (de == NULL) {
324 					de = devfs_vmkdir(s, q - s, dd);
325 					de->de_inode = dm->dm_inode++;
326 					TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
327 					dd->de_links++;
328 				}
329 				s = q + 1;
330 				dd = de;
331 			}
332 			de = devfs_newdirent(s, q - s);
333 			if (dev->si_flags & SI_ALIAS) {
334 				de->de_inode = dm->dm_inode++;
335 				de->de_uid = 0;
336 				de->de_gid = 0;
337 				de->de_mode = 0666;
338 				de->de_dirent->d_type = DT_LNK;
339 				pdev = dev->si_drv1;
340 				j = strlen(pdev->si_name) + 1;
341 				MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK);
342 				bcopy(pdev->si_name, de->de_symlink, j);
343 			} else {
344 				de->de_inode = i;
345 				de->de_uid = dev->si_uid;
346 				de->de_gid = dev->si_gid;
347 				de->de_mode = dev->si_mode;
348 				de->de_dirent->d_type = DT_CHR;
349 			}
350 			*dep = de;
351 			de->de_dir = dd;
352 			TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
353 #if 0
354 			printf("Add ino%d %s\n", i, dev->si_name);
355 #endif
356 		}
357 	}
358 	lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, curproc);
359 	return (0);
360 }
361 
362 static void
363 devfs_create(dev_t dev)
364 {
365 	int ino, i, *ip;
366 	dev_t *dp;
367 
368 	for (;;) {
369 		/* Grab the next inode number */
370 		ino = devfs_nextino;
371 		i = ino + 1;
372 		/* wrap around when we reach the end */
373 		if (i >= NDEVFSINO + devfs_noverflow)
374 			i = 3;
375 		if (!atomic_cmpset_int(&devfs_nextino, ino, i))
376 			continue;
377 
378 		/* see if it was occupied */
379 		dp = devfs_itod(ino);
380 		if (dp == NULL)
381 			Debugger("dp == NULL\n");
382 		if (*dp != NULL)
383 			continue;
384 		ip = devfs_itor(ino);
385 		if (ip == NULL)
386 			Debugger("ip == NULL\n");
387 		if (*ip != 0)
388 			continue;
389 
390 		if (!atomic_cmpset_ptr(dp, NULL, dev))
391 			continue;
392 
393 		dev->si_inode = ino;
394 		for (;;) {
395 			i = devfs_topino;
396 			if (i >= ino)
397 				break;
398 			if (atomic_cmpset_int(&devfs_topino, i, ino))
399 				break;
400 			printf("failed topino %d %d\n", i, ino);
401 		}
402 		break;
403 	}
404 
405 	atomic_add_int(&devfs_numino, 1);
406 	atomic_add_int(&devfs_generation, 1);
407 	if (devfs_overflow == NULL && devfs_numino + 100 > NDEVFSINO)
408 		devfs_attemptoverflow(0);
409 }
410 
411 static void
412 devfs_destroy(dev_t dev)
413 {
414 	int ino, i;
415 
416 	ino = dev->si_inode;
417 	dev->si_inode = 0;
418 	if (ino == 0)
419 		return;
420 	if (atomic_cmpset_ptr(devfs_itod(ino), dev, NULL)) {
421 		atomic_add_int(&devfs_generation, 1);
422 		atomic_add_int(&devfs_numino, -1);
423 		i = devfs_nextino;
424 		if (ino < i)
425 			atomic_cmpset_int(&devfs_nextino, i, ino);
426 	}
427 }
428 
429 devfs_create_t *devfs_create_hook = devfs_create;
430 devfs_destroy_t *devfs_destroy_hook = devfs_destroy;
431 int devfs_present = 1;
432