xref: /freebsd/sys/fs/devfs/devfs_devs.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000,2004
5  *	Poul-Henning Kamp.  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. Neither the name of the University nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
29  *
30  * $FreeBSD$
31  */
32 
33 #include "opt_compat.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/dirent.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/sx.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 
48 #include <sys/kdb.h>
49 
50 #include <fs/devfs/devfs.h>
51 #include <fs/devfs/devfs_int.h>
52 
53 #include <security/mac/mac_framework.h>
54 
55 /*
56  * The one true (but secret) list of active devices in the system.
57  * Locked by dev_lock()/devmtx
58  */
59 struct cdev_priv_list cdevp_list = TAILQ_HEAD_INITIALIZER(cdevp_list);
60 
61 struct unrhdr *devfs_inos;
62 
63 
64 static MALLOC_DEFINE(M_DEVFS2, "DEVFS2", "DEVFS data 2");
65 static MALLOC_DEFINE(M_DEVFS3, "DEVFS3", "DEVFS data 3");
66 static MALLOC_DEFINE(M_CDEVP, "DEVFS1", "DEVFS cdev_priv storage");
67 
68 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
69 
70 static unsigned devfs_generation;
71 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
72 	&devfs_generation, 0, "DEVFS generation number");
73 
74 unsigned devfs_rule_depth = 1;
75 SYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
76 	&devfs_rule_depth, 0, "Max depth of ruleset include");
77 
78 /*
79  * Helper sysctl for devname(3).  We're given a dev_t and return the
80  * name, if any, registered by the device driver.
81  */
82 static int
83 sysctl_devname(SYSCTL_HANDLER_ARGS)
84 {
85 	int error;
86 	dev_t ud;
87 #ifdef COMPAT_FREEBSD11
88 	uint32_t ud_compat;
89 #endif
90 	struct cdev_priv *cdp;
91 	struct cdev *dev;
92 
93 #ifdef COMPAT_FREEBSD11
94 	if (req->newlen == sizeof(ud_compat)) {
95 		error = SYSCTL_IN(req, &ud_compat, sizeof(ud_compat));
96 		if (error == 0)
97 			ud = ud_compat == (uint32_t)NODEV ? NODEV : ud_compat;
98 	} else
99 #endif
100 		error = SYSCTL_IN(req, &ud, sizeof (ud));
101 	if (error)
102 		return (error);
103 	if (ud == NODEV)
104 		return (EINVAL);
105 	dev = NULL;
106 	dev_lock();
107 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list)
108 		if (cdp->cdp_inode == ud) {
109 			dev = &cdp->cdp_c;
110 			dev_refl(dev);
111 			break;
112 		}
113 	dev_unlock();
114 	if (dev == NULL)
115 		return (ENOENT);
116 	error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
117 	dev_rel(dev);
118 	return (error);
119 }
120 
121 SYSCTL_PROC(_kern, OID_AUTO, devname,
122     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
123     NULL, 0, sysctl_devname, "", "devname(3) handler");
124 
125 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
126     SYSCTL_NULL_INT_PTR, sizeof(struct cdev), "sizeof(struct cdev)");
127 
128 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD,
129     SYSCTL_NULL_INT_PTR, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)");
130 
131 struct cdev *
132 devfs_alloc(int flags)
133 {
134 	struct cdev_priv *cdp;
135 	struct cdev *cdev;
136 	struct timespec ts;
137 
138 	cdp = malloc(sizeof *cdp, M_CDEVP, M_ZERO |
139 	    ((flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK));
140 	if (cdp == NULL)
141 		return (NULL);
142 
143 	cdp->cdp_dirents = &cdp->cdp_dirent0;
144 
145 	cdev = &cdp->cdp_c;
146 	LIST_INIT(&cdev->si_children);
147 	vfs_timestamp(&ts);
148 	cdev->si_atime = cdev->si_mtime = cdev->si_ctime = ts;
149 
150 	return (cdev);
151 }
152 
153 int
154 devfs_dev_exists(const char *name)
155 {
156 	struct cdev_priv *cdp;
157 
158 	mtx_assert(&devmtx, MA_OWNED);
159 
160 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
161 		if ((cdp->cdp_flags & CDP_ACTIVE) == 0)
162 			continue;
163 		if (devfs_pathpath(cdp->cdp_c.si_name, name) != 0)
164 			return (1);
165 		if (devfs_pathpath(name, cdp->cdp_c.si_name) != 0)
166 			return (1);
167 	}
168 	if (devfs_dir_find(name) != 0)
169 		return (1);
170 
171 	return (0);
172 }
173 
174 void
175 devfs_free(struct cdev *cdev)
176 {
177 	struct cdev_priv *cdp;
178 
179 	cdp = cdev2priv(cdev);
180 	if (cdev->si_cred != NULL)
181 		crfree(cdev->si_cred);
182 	devfs_free_cdp_inode(cdp->cdp_inode);
183 	if (cdp->cdp_maxdirent > 0)
184 		free(cdp->cdp_dirents, M_DEVFS2);
185 	free(cdp, M_CDEVP);
186 }
187 
188 struct devfs_dirent *
189 devfs_find(struct devfs_dirent *dd, const char *name, int namelen, int type)
190 {
191 	struct devfs_dirent *de;
192 
193 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
194 		if (namelen != de->de_dirent->d_namlen)
195 			continue;
196 		if (type != 0 && type != de->de_dirent->d_type)
197 			continue;
198 
199 		/*
200 		 * The race with finding non-active name is not
201 		 * completely closed by the check, but it is similar
202 		 * to the devfs_allocv() in making it unlikely enough.
203 		 */
204 		if (de->de_dirent->d_type == DT_CHR &&
205 		    (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
206 			continue;
207 
208 		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
209 			continue;
210 		break;
211 	}
212 	KASSERT(de == NULL || (de->de_flags & DE_DOOMED) == 0,
213 	    ("devfs_find: returning a doomed entry"));
214 	return (de);
215 }
216 
217 struct devfs_dirent *
218 devfs_newdirent(char *name, int namelen)
219 {
220 	int i;
221 	struct devfs_dirent *de;
222 	struct dirent d;
223 
224 	d.d_namlen = namelen;
225 	i = sizeof(*de) + GENERIC_DIRSIZ(&d);
226 	de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO);
227 	de->de_dirent = (struct dirent *)(de + 1);
228 	de->de_dirent->d_namlen = namelen;
229 	de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
230 	bcopy(name, de->de_dirent->d_name, namelen);
231 	de->de_dirent->d_name[namelen] = '\0';
232 	vfs_timestamp(&de->de_ctime);
233 	de->de_mtime = de->de_atime = de->de_ctime;
234 	de->de_links = 1;
235 	de->de_holdcnt = 1;
236 #ifdef MAC
237 	mac_devfs_init(de);
238 #endif
239 	return (de);
240 }
241 
242 struct devfs_dirent *
243 devfs_parent_dirent(struct devfs_dirent *de)
244 {
245 
246 	if (de->de_dirent->d_type != DT_DIR)
247 		return (de->de_dir);
248 
249 	if (de->de_flags & (DE_DOT | DE_DOTDOT))
250 		return (NULL);
251 
252 	de = TAILQ_FIRST(&de->de_dlist);	/* "." */
253 	if (de == NULL)
254 		return (NULL);
255 	de = TAILQ_NEXT(de, de_list);		/* ".." */
256 	if (de == NULL)
257 		return (NULL);
258 
259 	return (de->de_dir);
260 }
261 
262 struct devfs_dirent *
263 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen,
264     struct devfs_dirent *dotdot, u_int inode)
265 {
266 	struct devfs_dirent *dd;
267 	struct devfs_dirent *de;
268 
269 	/* Create the new directory */
270 	dd = devfs_newdirent(name, namelen);
271 	TAILQ_INIT(&dd->de_dlist);
272 	dd->de_dirent->d_type = DT_DIR;
273 	dd->de_mode = 0555;
274 	dd->de_links = 2;
275 	dd->de_dir = dd;
276 	if (inode != 0)
277 		dd->de_inode = inode;
278 	else
279 		dd->de_inode = alloc_unr(devfs_inos);
280 
281 	/*
282 	 * "." and ".." are always the two first entries in the
283 	 * de_dlist list.
284 	 *
285 	 * Create the "." entry in the new directory.
286 	 */
287 	de = devfs_newdirent(".", 1);
288 	de->de_dirent->d_type = DT_DIR;
289 	de->de_flags |= DE_DOT;
290 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
291 	de->de_dir = dd;
292 
293 	/* Create the ".." entry in the new directory. */
294 	de = devfs_newdirent("..", 2);
295 	de->de_dirent->d_type = DT_DIR;
296 	de->de_flags |= DE_DOTDOT;
297 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
298 	if (dotdot == NULL) {
299 		de->de_dir = dd;
300 	} else {
301 		de->de_dir = dotdot;
302 		sx_assert(&dmp->dm_lock, SX_XLOCKED);
303 		TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
304 		dotdot->de_links++;
305 		devfs_rules_apply(dmp, dd);
306 	}
307 
308 #ifdef MAC
309 	mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd);
310 #endif
311 	return (dd);
312 }
313 
314 void
315 devfs_dirent_free(struct devfs_dirent *de)
316 {
317 	struct vnode *vp;
318 
319 	vp = de->de_vnode;
320 	mtx_lock(&devfs_de_interlock);
321 	if (vp != NULL && vp->v_data == de)
322 		vp->v_data = NULL;
323 	mtx_unlock(&devfs_de_interlock);
324 	free(de, M_DEVFS3);
325 }
326 
327 /*
328  * Removes a directory if it is empty. Also empty parent directories are
329  * removed recursively.
330  */
331 static void
332 devfs_rmdir_empty(struct devfs_mount *dm, struct devfs_dirent *de)
333 {
334 	struct devfs_dirent *dd, *de_dot, *de_dotdot;
335 
336 	sx_assert(&dm->dm_lock, SX_XLOCKED);
337 
338 	for (;;) {
339 		KASSERT(de->de_dirent->d_type == DT_DIR,
340 		    ("devfs_rmdir_empty: de is not a directory"));
341 
342 		if ((de->de_flags & DE_DOOMED) != 0 || de == dm->dm_rootdir)
343 			return;
344 
345 		de_dot = TAILQ_FIRST(&de->de_dlist);
346 		KASSERT(de_dot != NULL, ("devfs_rmdir_empty: . missing"));
347 		de_dotdot = TAILQ_NEXT(de_dot, de_list);
348 		KASSERT(de_dotdot != NULL, ("devfs_rmdir_empty: .. missing"));
349 		/* Return if the directory is not empty. */
350 		if (TAILQ_NEXT(de_dotdot, de_list) != NULL)
351 			return;
352 
353 		dd = devfs_parent_dirent(de);
354 		KASSERT(dd != NULL, ("devfs_rmdir_empty: NULL dd"));
355 		TAILQ_REMOVE(&de->de_dlist, de_dot, de_list);
356 		TAILQ_REMOVE(&de->de_dlist, de_dotdot, de_list);
357 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
358 		DEVFS_DE_HOLD(dd);
359 		devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
360 		devfs_delete(dm, de_dot, DEVFS_DEL_NORECURSE);
361 		devfs_delete(dm, de_dotdot, DEVFS_DEL_NORECURSE);
362 		if (DEVFS_DE_DROP(dd)) {
363 			devfs_dirent_free(dd);
364 			return;
365 		}
366 
367 		de = dd;
368 	}
369 }
370 
371 /*
372  * The caller needs to hold the dm for the duration of the call since
373  * dm->dm_lock may be temporary dropped.
374  */
375 void
376 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int flags)
377 {
378 	struct devfs_dirent *dd;
379 	struct vnode *vp;
380 
381 	KASSERT((de->de_flags & DE_DOOMED) == 0,
382 		("devfs_delete doomed dirent"));
383 	de->de_flags |= DE_DOOMED;
384 
385 	if ((flags & DEVFS_DEL_NORECURSE) == 0) {
386 		dd = devfs_parent_dirent(de);
387 		if (dd != NULL)
388 			DEVFS_DE_HOLD(dd);
389 		if (de->de_flags & DE_USER) {
390 			KASSERT(dd != NULL, ("devfs_delete: NULL dd"));
391 			devfs_dir_unref_de(dm, dd);
392 		}
393 	} else
394 		dd = NULL;
395 
396 	mtx_lock(&devfs_de_interlock);
397 	vp = de->de_vnode;
398 	if (vp != NULL) {
399 		VI_LOCK(vp);
400 		mtx_unlock(&devfs_de_interlock);
401 		vholdl(vp);
402 		sx_unlock(&dm->dm_lock);
403 		if ((flags & DEVFS_DEL_VNLOCKED) == 0)
404 			vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
405 		else
406 			VI_UNLOCK(vp);
407 		vgone(vp);
408 		if ((flags & DEVFS_DEL_VNLOCKED) == 0)
409 			VOP_UNLOCK(vp, 0);
410 		vdrop(vp);
411 		sx_xlock(&dm->dm_lock);
412 	} else
413 		mtx_unlock(&devfs_de_interlock);
414 	if (de->de_symlink) {
415 		free(de->de_symlink, M_DEVFS);
416 		de->de_symlink = NULL;
417 	}
418 #ifdef MAC
419 	mac_devfs_destroy(de);
420 #endif
421 	if (de->de_inode > DEVFS_ROOTINO) {
422 		devfs_free_cdp_inode(de->de_inode);
423 		de->de_inode = 0;
424 	}
425 	if (DEVFS_DE_DROP(de))
426 		devfs_dirent_free(de);
427 
428 	if (dd != NULL) {
429 		if (DEVFS_DE_DROP(dd))
430 			devfs_dirent_free(dd);
431 		else
432 			devfs_rmdir_empty(dm, dd);
433 	}
434 }
435 
436 /*
437  * Called on unmount.
438  * Recursively removes the entire tree.
439  * The caller needs to hold the dm for the duration of the call.
440  */
441 
442 static void
443 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
444 {
445 	struct devfs_dirent *de;
446 
447 	sx_assert(&dm->dm_lock, SX_XLOCKED);
448 
449 	DEVFS_DE_HOLD(dd);
450 	for (;;) {
451 		/*
452 		 * Use TAILQ_LAST() to remove "." and ".." last.
453 		 * We might need ".." to resolve a path in
454 		 * devfs_dir_unref_de().
455 		 */
456 		de = TAILQ_LAST(&dd->de_dlist, devfs_dlist_head);
457 		if (de == NULL)
458 			break;
459 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
460 		if (de->de_flags & DE_USER)
461 			devfs_dir_unref_de(dm, dd);
462 		if (de->de_flags & (DE_DOT | DE_DOTDOT))
463 			devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
464 		else if (de->de_dirent->d_type == DT_DIR)
465 			devfs_purge(dm, de);
466 		else
467 			devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
468 	}
469 	if (DEVFS_DE_DROP(dd))
470 		devfs_dirent_free(dd);
471 	else if ((dd->de_flags & DE_DOOMED) == 0)
472 		devfs_delete(dm, dd, DEVFS_DEL_NORECURSE);
473 }
474 
475 /*
476  * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
477  * by the mount points dm_idx.
478  * This function extends the array when necessary, taking into account that
479  * the default array is 1 element and not malloc'ed.
480  */
481 static void
482 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
483 {
484 	struct devfs_dirent **dep;
485 	int siz;
486 
487 	siz = (dm->dm_idx + 1) * sizeof *dep;
488 	dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
489 	dev_lock();
490 	if (dm->dm_idx <= cdp->cdp_maxdirent) {
491 		/* We got raced */
492 		dev_unlock();
493 		free(dep, M_DEVFS2);
494 		return;
495 	}
496 	memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
497 	if (cdp->cdp_maxdirent > 0)
498 		free(cdp->cdp_dirents, M_DEVFS2);
499 	cdp->cdp_dirents = dep;
500 	/*
501 	 * XXX: if malloc told us how much we actually got this could
502 	 * XXX: be optimized.
503 	 */
504 	cdp->cdp_maxdirent = dm->dm_idx;
505 	dev_unlock();
506 }
507 
508 /*
509  * The caller needs to hold the dm for the duration of the call.
510  */
511 static int
512 devfs_populate_loop(struct devfs_mount *dm, int cleanup)
513 {
514 	struct cdev_priv *cdp;
515 	struct devfs_dirent *de;
516 	struct devfs_dirent *dd, *dt;
517 	struct cdev *pdev;
518 	int de_flags, depth, j;
519 	char *q, *s;
520 
521 	sx_assert(&dm->dm_lock, SX_XLOCKED);
522 	dev_lock();
523 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
524 
525 		KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
526 
527 		/*
528 		 * If we are unmounting, or the device has been destroyed,
529 		 * clean up our dirent.
530 		 */
531 		if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
532 		    dm->dm_idx <= cdp->cdp_maxdirent &&
533 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
534 			de = cdp->cdp_dirents[dm->dm_idx];
535 			cdp->cdp_dirents[dm->dm_idx] = NULL;
536 			KASSERT(cdp == de->de_cdp,
537 			    ("%s %d %s %p %p", __func__, __LINE__,
538 			    cdp->cdp_c.si_name, cdp, de->de_cdp));
539 			KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
540 			dev_unlock();
541 
542 			TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
543 			de->de_cdp = NULL;
544 			de->de_inode = 0;
545 			devfs_delete(dm, de, 0);
546 			dev_lock();
547 			cdp->cdp_inuse--;
548 			dev_unlock();
549 			return (1);
550 		}
551 		/*
552 	 	 * GC any lingering devices
553 		 */
554 		if (!(cdp->cdp_flags & CDP_ACTIVE)) {
555 			if (cdp->cdp_inuse > 0)
556 				continue;
557 			TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
558 			dev_unlock();
559 			dev_rel(&cdp->cdp_c);
560 			return (1);
561 		}
562 		/*
563 		 * Don't create any new dirents if we are unmounting
564 		 */
565 		if (cleanup)
566 			continue;
567 		KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
568 
569 		if (dm->dm_idx <= cdp->cdp_maxdirent &&
570 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
571 			de = cdp->cdp_dirents[dm->dm_idx];
572 			KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
573 			continue;
574 		}
575 
576 
577 		cdp->cdp_inuse++;
578 		dev_unlock();
579 
580 		if (dm->dm_idx > cdp->cdp_maxdirent)
581 		        devfs_metoo(cdp, dm);
582 
583 		dd = dm->dm_rootdir;
584 		s = cdp->cdp_c.si_name;
585 		for (;;) {
586 			for (q = s; *q != '/' && *q != '\0'; q++)
587 				continue;
588 			if (*q != '/')
589 				break;
590 			de = devfs_find(dd, s, q - s, 0);
591 			if (de == NULL)
592 				de = devfs_vmkdir(dm, s, q - s, dd, 0);
593 			else if (de->de_dirent->d_type == DT_LNK) {
594 				de = devfs_find(dd, s, q - s, DT_DIR);
595 				if (de == NULL)
596 					de = devfs_vmkdir(dm, s, q - s, dd, 0);
597 				de->de_flags |= DE_COVERED;
598 			}
599 			s = q + 1;
600 			dd = de;
601 			KASSERT(dd->de_dirent->d_type == DT_DIR &&
602 			    (dd->de_flags & (DE_DOT | DE_DOTDOT)) == 0,
603 			    ("%s: invalid directory (si_name=%s)",
604 			    __func__, cdp->cdp_c.si_name));
605 
606 		}
607 		de_flags = 0;
608 		de = devfs_find(dd, s, q - s, DT_LNK);
609 		if (de != NULL)
610 			de_flags |= DE_COVERED;
611 
612 		de = devfs_newdirent(s, q - s);
613 		if (cdp->cdp_c.si_flags & SI_ALIAS) {
614 			de->de_uid = 0;
615 			de->de_gid = 0;
616 			de->de_mode = 0755;
617 			de->de_dirent->d_type = DT_LNK;
618 			pdev = cdp->cdp_c.si_parent;
619 			dt = dd;
620 			depth = 0;
621 			while (dt != dm->dm_rootdir &&
622 			    (dt = devfs_parent_dirent(dt)) != NULL)
623 				depth++;
624 			j = depth * 3 + strlen(pdev->si_name) + 1;
625 			de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
626 			de->de_symlink[0] = 0;
627 			while (depth-- > 0)
628 				strcat(de->de_symlink, "../");
629 			strcat(de->de_symlink, pdev->si_name);
630 		} else {
631 			de->de_uid = cdp->cdp_c.si_uid;
632 			de->de_gid = cdp->cdp_c.si_gid;
633 			de->de_mode = cdp->cdp_c.si_mode;
634 			de->de_dirent->d_type = DT_CHR;
635 		}
636 		de->de_flags |= de_flags;
637 		de->de_inode = cdp->cdp_inode;
638 		de->de_cdp = cdp;
639 #ifdef MAC
640 		mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount,
641 		    &cdp->cdp_c, de);
642 #endif
643 		de->de_dir = dd;
644 		TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
645 		devfs_rules_apply(dm, de);
646 		dev_lock();
647 		/* XXX: could check that cdp is still active here */
648 		KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
649 		    ("%s %d\n", __func__, __LINE__));
650 		cdp->cdp_dirents[dm->dm_idx] = de;
651 		KASSERT(de->de_cdp != (void *)0xdeadc0de,
652 		    ("%s %d\n", __func__, __LINE__));
653 		dev_unlock();
654 		return (1);
655 	}
656 	dev_unlock();
657 	return (0);
658 }
659 
660 /*
661  * The caller needs to hold the dm for the duration of the call.
662  */
663 void
664 devfs_populate(struct devfs_mount *dm)
665 {
666 	unsigned gen;
667 
668 	sx_assert(&dm->dm_lock, SX_XLOCKED);
669 	gen = devfs_generation;
670 	if (dm->dm_generation == gen)
671 		return;
672 	while (devfs_populate_loop(dm, 0))
673 		continue;
674 	dm->dm_generation = gen;
675 }
676 
677 /*
678  * The caller needs to hold the dm for the duration of the call.
679  */
680 void
681 devfs_cleanup(struct devfs_mount *dm)
682 {
683 
684 	sx_assert(&dm->dm_lock, SX_XLOCKED);
685 	while (devfs_populate_loop(dm, 1))
686 		continue;
687 	devfs_purge(dm, dm->dm_rootdir);
688 }
689 
690 /*
691  * devfs_create() and devfs_destroy() are called from kern_conf.c and
692  * in both cases the devlock() mutex is held, so no further locking
693  * is necessary and no sleeping allowed.
694  */
695 
696 void
697 devfs_create(struct cdev *dev)
698 {
699 	struct cdev_priv *cdp;
700 
701 	mtx_assert(&devmtx, MA_OWNED);
702 	cdp = cdev2priv(dev);
703 	cdp->cdp_flags |= CDP_ACTIVE;
704 	cdp->cdp_inode = alloc_unrl(devfs_inos);
705 	dev_refl(dev);
706 	TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
707 	devfs_generation++;
708 }
709 
710 void
711 devfs_destroy(struct cdev *dev)
712 {
713 	struct cdev_priv *cdp;
714 
715 	mtx_assert(&devmtx, MA_OWNED);
716 	cdp = cdev2priv(dev);
717 	cdp->cdp_flags &= ~CDP_ACTIVE;
718 	devfs_generation++;
719 }
720 
721 ino_t
722 devfs_alloc_cdp_inode(void)
723 {
724 
725 	return (alloc_unr(devfs_inos));
726 }
727 
728 void
729 devfs_free_cdp_inode(ino_t ino)
730 {
731 
732 	if (ino > 0)
733 		free_unr(devfs_inos, ino);
734 }
735 
736 static void
737 devfs_devs_init(void *junk __unused)
738 {
739 
740 	devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
741 }
742 
743 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);
744