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