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