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