devfs_devs.c revision 273736
187866Ssheldonh/*-
287866Ssheldonh * Copyright (c) 2000,2004
387866Ssheldonh *	Poul-Henning Kamp.  All rights reserved.
487866Ssheldonh *
587866Ssheldonh * Redistribution and use in source and binary forms, with or without
687866Ssheldonh * modification, are permitted provided that the following conditions
787866Ssheldonh * are met:
887866Ssheldonh * 1. Redistributions of source code must retain the above copyright
987866Ssheldonh *    notice, this list of conditions and the following disclaimer.
1087866Ssheldonh * 2. Neither the name of the University nor the names of its contributors
1187866Ssheldonh *    may be used to endorse or promote products derived from this software
1287866Ssheldonh *    without specific prior written permission.
1387866Ssheldonh *
1487866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1587866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1687866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1787866Ssheldonh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1887866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1987866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2087866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2187866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2287866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2387866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2487866Ssheldonh * SUCH DAMAGE.
2587866Ssheldonh *
2687866Ssheldonh * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
2787866Ssheldonh *
2887866Ssheldonh * $FreeBSD: stable/10/sys/fs/devfs/devfs_devs.c 273736 2014-10-27 14:38:00Z hselasky $
2987866Ssheldonh */
3087866Ssheldonh
3187866Ssheldonh#include <sys/param.h>
3287866Ssheldonh#include <sys/systm.h>
33150802Sbp#include <sys/conf.h>
3487866Ssheldonh#include <sys/dirent.h>
3587866Ssheldonh#include <sys/kernel.h>
3687866Ssheldonh#include <sys/limits.h>
3787866Ssheldonh#include <sys/lock.h>
3887866Ssheldonh#include <sys/malloc.h>
3987866Ssheldonh#include <sys/proc.h>
4087866Ssheldonh#include <sys/sx.h>
4187866Ssheldonh#include <sys/sysctl.h>
4287866Ssheldonh#include <sys/vnode.h>
4387866Ssheldonh
4487866Ssheldonh#include <sys/kdb.h>
4587866Ssheldonh
4687866Ssheldonh#include <fs/devfs/devfs.h>
4787866Ssheldonh#include <fs/devfs/devfs_int.h>
4887866Ssheldonh
4987866Ssheldonh#include <security/mac/mac_framework.h>
5087866Ssheldonh
5187866Ssheldonh/*
5287866Ssheldonh * The one true (but secret) list of active devices in the system.
5387866Ssheldonh * Locked by dev_lock()/devmtx
5487866Ssheldonh */
5587866Ssheldonhstruct cdev_priv_list cdevp_list = TAILQ_HEAD_INITIALIZER(cdevp_list);
5687866Ssheldonh
5787866Ssheldonhstruct unrhdr *devfs_inos;
5887866Ssheldonh
5987866Ssheldonh
6087866Ssheldonhstatic MALLOC_DEFINE(M_DEVFS2, "DEVFS2", "DEVFS data 2");
6187866Ssheldonhstatic MALLOC_DEFINE(M_DEVFS3, "DEVFS3", "DEVFS data 3");
6287866Ssheldonhstatic MALLOC_DEFINE(M_CDEVP, "DEVFS1", "DEVFS cdev_priv storage");
6387866Ssheldonh
6487866Ssheldonhstatic SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
6587866Ssheldonh
6687866Ssheldonhstatic unsigned devfs_generation;
67150802SbpSYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
68150802Sbp	&devfs_generation, 0, "DEVFS generation number");
6987866Ssheldonh
7087866Ssheldonhunsigned devfs_rule_depth = 1;
7187866SsheldonhSYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
7287866Ssheldonh	&devfs_rule_depth, 0, "Max depth of ruleset include");
7387866Ssheldonh
7487866Ssheldonh/*
7587866Ssheldonh * Helper sysctl for devname(3).  We're given a dev_t and return the
7687866Ssheldonh * name, if any, registered by the device driver.
7787866Ssheldonh */
7887866Ssheldonhstatic int
7987866Ssheldonhsysctl_devname(SYSCTL_HANDLER_ARGS)
8087866Ssheldonh{
8187866Ssheldonh	int error;
8287866Ssheldonh	dev_t ud;
8387866Ssheldonh	struct cdev_priv *cdp;
8487866Ssheldonh	struct cdev *dev;
8587866Ssheldonh
8687866Ssheldonh	error = SYSCTL_IN(req, &ud, sizeof (ud));
8787866Ssheldonh	if (error)
8887866Ssheldonh		return (error);
8987866Ssheldonh	if (ud == NODEV)
9087866Ssheldonh		return (EINVAL);
9187866Ssheldonh	dev = NULL;
9287866Ssheldonh	dev_lock();
9387866Ssheldonh	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list)
9487866Ssheldonh		if (cdp->cdp_inode == ud) {
9587866Ssheldonh			dev = &cdp->cdp_c;
9687866Ssheldonh			dev_refl(dev);
9787866Ssheldonh			break;
9887866Ssheldonh		}
9987866Ssheldonh	dev_unlock();
10087866Ssheldonh	if (dev == NULL)
10187866Ssheldonh		return (ENOENT);
10287866Ssheldonh	error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
10387866Ssheldonh	dev_rel(dev);
10487866Ssheldonh	return (error);
10587866Ssheldonh}
10687866Ssheldonh
10787866SsheldonhSYSCTL_PROC(_kern, OID_AUTO, devname,
10887866Ssheldonh    CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
10987866Ssheldonh    NULL, 0, sysctl_devname, "", "devname(3) handler");
11087866Ssheldonh
11187866SsheldonhSYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
11287866Ssheldonh    SYSCTL_NULL_INT_PTR, sizeof(struct cdev), "sizeof(struct cdev)");
11387866Ssheldonh
11487866SsheldonhSYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD,
11587866Ssheldonh    SYSCTL_NULL_INT_PTR, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)");
11687866Ssheldonh
11787866Ssheldonhstruct cdev *
11887866Ssheldonhdevfs_alloc(int flags)
11987866Ssheldonh{
12087866Ssheldonh	struct cdev_priv *cdp;
12187866Ssheldonh	struct cdev *cdev;
12287866Ssheldonh	struct timespec ts;
12387866Ssheldonh
12487866Ssheldonh	cdp = malloc(sizeof *cdp, M_CDEVP, M_ZERO |
12587866Ssheldonh	    ((flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK));
12687866Ssheldonh	if (cdp == NULL)
12787866Ssheldonh		return (NULL);
12887866Ssheldonh
12987866Ssheldonh	cdp->cdp_dirents = &cdp->cdp_dirent0;
13087866Ssheldonh	cdp->cdp_dirent0 = NULL;
13187866Ssheldonh	cdp->cdp_maxdirent = 0;
13287866Ssheldonh	cdp->cdp_inode = 0;
13387866Ssheldonh
134150802Sbp	cdev = &cdp->cdp_c;
13587866Ssheldonh
13687866Ssheldonh	LIST_INIT(&cdev->si_children);
13787866Ssheldonh	vfs_timestamp(&ts);
13887866Ssheldonh	cdev->si_atime = cdev->si_mtime = cdev->si_ctime = ts;
13987866Ssheldonh	cdev->si_cred = NULL;
14087866Ssheldonh
14187866Ssheldonh	return (cdev);
14287866Ssheldonh}
14387866Ssheldonh
14487866Ssheldonhint
14587866Ssheldonhdevfs_dev_exists(const char *name)
14687866Ssheldonh{
14787866Ssheldonh	struct cdev_priv *cdp;
14887866Ssheldonh
14987866Ssheldonh	mtx_assert(&devmtx, MA_OWNED);
15087866Ssheldonh
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
165void
166devfs_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
179struct devfs_dirent *
180devfs_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		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
190			continue;
191		break;
192	}
193	KASSERT(de == NULL || (de->de_flags & DE_DOOMED) == 0,
194	    ("devfs_find: returning a doomed entry"));
195	return (de);
196}
197
198struct devfs_dirent *
199devfs_newdirent(char *name, int namelen)
200{
201	int i;
202	struct devfs_dirent *de;
203	struct dirent d;
204
205	d.d_namlen = namelen;
206	i = sizeof (*de) + GENERIC_DIRSIZ(&d);
207	de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO);
208	de->de_dirent = (struct dirent *)(de + 1);
209	de->de_dirent->d_namlen = namelen;
210	de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
211	bcopy(name, de->de_dirent->d_name, namelen);
212	de->de_dirent->d_name[namelen] = '\0';
213	vfs_timestamp(&de->de_ctime);
214	de->de_mtime = de->de_atime = de->de_ctime;
215	de->de_links = 1;
216	de->de_holdcnt = 1;
217#ifdef MAC
218	mac_devfs_init(de);
219#endif
220	return (de);
221}
222
223struct devfs_dirent *
224devfs_parent_dirent(struct devfs_dirent *de)
225{
226
227	if (de->de_dirent->d_type != DT_DIR)
228		return (de->de_dir);
229
230	if (de->de_flags & (DE_DOT | DE_DOTDOT))
231		return (NULL);
232
233	de = TAILQ_FIRST(&de->de_dlist);	/* "." */
234	if (de == NULL)
235		return (NULL);
236	de = TAILQ_NEXT(de, de_list);		/* ".." */
237	if (de == NULL)
238		return (NULL);
239
240	return (de->de_dir);
241}
242
243struct devfs_dirent *
244devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode)
245{
246	struct devfs_dirent *dd;
247	struct devfs_dirent *de;
248
249	/* Create the new directory */
250	dd = devfs_newdirent(name, namelen);
251	TAILQ_INIT(&dd->de_dlist);
252	dd->de_dirent->d_type = DT_DIR;
253	dd->de_mode = 0555;
254	dd->de_links = 2;
255	dd->de_dir = dd;
256	if (inode != 0)
257		dd->de_inode = inode;
258	else
259		dd->de_inode = alloc_unr(devfs_inos);
260
261	/*
262	 * "." and ".." are always the two first entries in the
263	 * de_dlist list.
264	 *
265	 * Create the "." entry in the new directory.
266	 */
267	de = devfs_newdirent(".", 1);
268	de->de_dirent->d_type = DT_DIR;
269	de->de_flags |= DE_DOT;
270	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
271	de->de_dir = dd;
272
273	/* Create the ".." entry in the new directory. */
274	de = devfs_newdirent("..", 2);
275	de->de_dirent->d_type = DT_DIR;
276	de->de_flags |= DE_DOTDOT;
277	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
278	if (dotdot == NULL) {
279		de->de_dir = dd;
280	} else {
281		de->de_dir = dotdot;
282		sx_assert(&dmp->dm_lock, SX_XLOCKED);
283		TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
284		dotdot->de_links++;
285		devfs_rules_apply(dmp, dd);
286	}
287
288#ifdef MAC
289	mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd);
290#endif
291	return (dd);
292}
293
294void
295devfs_dirent_free(struct devfs_dirent *de)
296{
297	free(de, M_DEVFS3);
298}
299
300/*
301 * Removes a directory if it is empty. Also empty parent directories are
302 * removed recursively.
303 */
304static void
305devfs_rmdir_empty(struct devfs_mount *dm, struct devfs_dirent *de)
306{
307	struct devfs_dirent *dd, *de_dot, *de_dotdot;
308
309	sx_assert(&dm->dm_lock, SX_XLOCKED);
310
311	for (;;) {
312		KASSERT(de->de_dirent->d_type == DT_DIR,
313		    ("devfs_rmdir_empty: de is not a directory"));
314
315		if ((de->de_flags & DE_DOOMED) != 0 || de == dm->dm_rootdir)
316			return;
317
318		de_dot = TAILQ_FIRST(&de->de_dlist);
319		KASSERT(de_dot != NULL, ("devfs_rmdir_empty: . missing"));
320		de_dotdot = TAILQ_NEXT(de_dot, de_list);
321		KASSERT(de_dotdot != NULL, ("devfs_rmdir_empty: .. missing"));
322		/* Return if the directory is not empty. */
323		if (TAILQ_NEXT(de_dotdot, de_list) != NULL)
324			return;
325
326		dd = devfs_parent_dirent(de);
327		KASSERT(dd != NULL, ("devfs_rmdir_empty: NULL dd"));
328		TAILQ_REMOVE(&de->de_dlist, de_dot, de_list);
329		TAILQ_REMOVE(&de->de_dlist, de_dotdot, de_list);
330		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
331		DEVFS_DE_HOLD(dd);
332		devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
333		devfs_delete(dm, de_dot, DEVFS_DEL_NORECURSE);
334		devfs_delete(dm, de_dotdot, DEVFS_DEL_NORECURSE);
335		if (DEVFS_DE_DROP(dd)) {
336			devfs_dirent_free(dd);
337			return;
338		}
339
340		de = dd;
341	}
342}
343
344/*
345 * The caller needs to hold the dm for the duration of the call since
346 * dm->dm_lock may be temporary dropped.
347 */
348void
349devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int flags)
350{
351	struct devfs_dirent *dd;
352	struct vnode *vp;
353
354	KASSERT((de->de_flags & DE_DOOMED) == 0,
355		("devfs_delete doomed dirent"));
356	de->de_flags |= DE_DOOMED;
357
358	if ((flags & DEVFS_DEL_NORECURSE) == 0) {
359		dd = devfs_parent_dirent(de);
360		if (dd != NULL)
361			DEVFS_DE_HOLD(dd);
362		if (de->de_flags & DE_USER) {
363			KASSERT(dd != NULL, ("devfs_delete: NULL dd"));
364			devfs_dir_unref_de(dm, dd);
365		}
366	} else
367		dd = NULL;
368
369	mtx_lock(&devfs_de_interlock);
370	vp = de->de_vnode;
371	if (vp != NULL) {
372		VI_LOCK(vp);
373		mtx_unlock(&devfs_de_interlock);
374		vholdl(vp);
375		sx_unlock(&dm->dm_lock);
376		if ((flags & DEVFS_DEL_VNLOCKED) == 0)
377			vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
378		else
379			VI_UNLOCK(vp);
380		vgone(vp);
381		if ((flags & DEVFS_DEL_VNLOCKED) == 0)
382			VOP_UNLOCK(vp, 0);
383		vdrop(vp);
384		sx_xlock(&dm->dm_lock);
385	} else
386		mtx_unlock(&devfs_de_interlock);
387	if (de->de_symlink) {
388		free(de->de_symlink, M_DEVFS);
389		de->de_symlink = NULL;
390	}
391#ifdef MAC
392	mac_devfs_destroy(de);
393#endif
394	if (de->de_inode > DEVFS_ROOTINO) {
395		devfs_free_cdp_inode(de->de_inode);
396		de->de_inode = 0;
397	}
398	if (DEVFS_DE_DROP(de))
399		devfs_dirent_free(de);
400
401	if (dd != NULL) {
402		if (DEVFS_DE_DROP(dd))
403			devfs_dirent_free(dd);
404		else
405			devfs_rmdir_empty(dm, dd);
406	}
407}
408
409/*
410 * Called on unmount.
411 * Recursively removes the entire tree.
412 * The caller needs to hold the dm for the duration of the call.
413 */
414
415static void
416devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
417{
418	struct devfs_dirent *de;
419
420	sx_assert(&dm->dm_lock, SX_XLOCKED);
421
422	DEVFS_DE_HOLD(dd);
423	for (;;) {
424		/*
425		 * Use TAILQ_LAST() to remove "." and ".." last.
426		 * We might need ".." to resolve a path in
427		 * devfs_dir_unref_de().
428		 */
429		de = TAILQ_LAST(&dd->de_dlist, devfs_dlist_head);
430		if (de == NULL)
431			break;
432		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
433		if (de->de_flags & DE_USER)
434			devfs_dir_unref_de(dm, dd);
435		if (de->de_flags & (DE_DOT | DE_DOTDOT))
436			devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
437		else if (de->de_dirent->d_type == DT_DIR)
438			devfs_purge(dm, de);
439		else
440			devfs_delete(dm, de, DEVFS_DEL_NORECURSE);
441	}
442	if (DEVFS_DE_DROP(dd))
443		devfs_dirent_free(dd);
444	else if ((dd->de_flags & DE_DOOMED) == 0)
445		devfs_delete(dm, dd, DEVFS_DEL_NORECURSE);
446}
447
448/*
449 * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
450 * by the mount points dm_idx.
451 * This function extends the array when necessary, taking into account that
452 * the default array is 1 element and not malloc'ed.
453 */
454static void
455devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
456{
457	struct devfs_dirent **dep;
458	int siz;
459
460	siz = (dm->dm_idx + 1) * sizeof *dep;
461	dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
462	dev_lock();
463	if (dm->dm_idx <= cdp->cdp_maxdirent) {
464		/* We got raced */
465		dev_unlock();
466		free(dep, M_DEVFS2);
467		return;
468	}
469	memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
470	if (cdp->cdp_maxdirent > 0)
471		free(cdp->cdp_dirents, M_DEVFS2);
472	cdp->cdp_dirents = dep;
473	/*
474	 * XXX: if malloc told us how much we actually got this could
475	 * XXX: be optimized.
476	 */
477	cdp->cdp_maxdirent = dm->dm_idx;
478	dev_unlock();
479}
480
481/*
482 * The caller needs to hold the dm for the duration of the call.
483 */
484static int
485devfs_populate_loop(struct devfs_mount *dm, int cleanup)
486{
487	struct cdev_priv *cdp;
488	struct devfs_dirent *de;
489	struct devfs_dirent *dd;
490	struct cdev *pdev;
491	int de_flags, j;
492	char *q, *s;
493
494	sx_assert(&dm->dm_lock, SX_XLOCKED);
495	dev_lock();
496	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
497
498		KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
499
500		/*
501		 * If we are unmounting, or the device has been destroyed,
502		 * clean up our dirent.
503		 */
504		if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
505		    dm->dm_idx <= cdp->cdp_maxdirent &&
506		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
507			de = cdp->cdp_dirents[dm->dm_idx];
508			cdp->cdp_dirents[dm->dm_idx] = NULL;
509			KASSERT(cdp == de->de_cdp,
510			    ("%s %d %s %p %p", __func__, __LINE__,
511			    cdp->cdp_c.si_name, cdp, de->de_cdp));
512			KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
513			dev_unlock();
514
515			TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
516			de->de_cdp = NULL;
517			de->de_inode = 0;
518			devfs_delete(dm, de, 0);
519			dev_lock();
520			cdp->cdp_inuse--;
521			dev_unlock();
522			return (1);
523		}
524		/*
525	 	 * GC any lingering devices
526		 */
527		if (!(cdp->cdp_flags & CDP_ACTIVE)) {
528			if (cdp->cdp_inuse > 0)
529				continue;
530			TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
531			dev_unlock();
532			dev_rel(&cdp->cdp_c);
533			return (1);
534		}
535		/*
536		 * Don't create any new dirents if we are unmounting
537		 */
538		if (cleanup)
539			continue;
540		KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
541
542		if (dm->dm_idx <= cdp->cdp_maxdirent &&
543		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
544			de = cdp->cdp_dirents[dm->dm_idx];
545			KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
546			continue;
547		}
548
549
550		cdp->cdp_inuse++;
551		dev_unlock();
552
553		if (dm->dm_idx > cdp->cdp_maxdirent)
554		        devfs_metoo(cdp, dm);
555
556		dd = dm->dm_rootdir;
557		s = cdp->cdp_c.si_name;
558		for (;;) {
559			for (q = s; *q != '/' && *q != '\0'; q++)
560				continue;
561			if (*q != '/')
562				break;
563			de = devfs_find(dd, s, q - s, 0);
564			if (de == NULL)
565				de = devfs_vmkdir(dm, s, q - s, dd, 0);
566			else if (de->de_dirent->d_type == DT_LNK) {
567				de = devfs_find(dd, s, q - s, DT_DIR);
568				if (de == NULL)
569					de = devfs_vmkdir(dm, s, q - s, dd, 0);
570				de->de_flags |= DE_COVERED;
571			}
572			s = q + 1;
573			dd = de;
574			KASSERT(dd->de_dirent->d_type == DT_DIR &&
575			    (dd->de_flags & (DE_DOT | DE_DOTDOT)) == 0,
576			    ("%s: invalid directory (si_name=%s)",
577			    __func__, cdp->cdp_c.si_name));
578
579		}
580		de_flags = 0;
581		de = devfs_find(dd, s, q - s, DT_LNK);
582		if (de != NULL)
583			de_flags |= DE_COVERED;
584
585		de = devfs_newdirent(s, q - s);
586		if (cdp->cdp_c.si_flags & SI_ALIAS) {
587			de->de_uid = 0;
588			de->de_gid = 0;
589			de->de_mode = 0755;
590			de->de_dirent->d_type = DT_LNK;
591			pdev = cdp->cdp_c.si_parent;
592			j = strlen(pdev->si_name) + 1;
593			de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
594			bcopy(pdev->si_name, de->de_symlink, j);
595		} else {
596			de->de_uid = cdp->cdp_c.si_uid;
597			de->de_gid = cdp->cdp_c.si_gid;
598			de->de_mode = cdp->cdp_c.si_mode;
599			de->de_dirent->d_type = DT_CHR;
600		}
601		de->de_flags |= de_flags;
602		de->de_inode = cdp->cdp_inode;
603		de->de_cdp = cdp;
604#ifdef MAC
605		mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount,
606		    &cdp->cdp_c, de);
607#endif
608		de->de_dir = dd;
609		TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
610		devfs_rules_apply(dm, de);
611		dev_lock();
612		/* XXX: could check that cdp is still active here */
613		KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
614		    ("%s %d\n", __func__, __LINE__));
615		cdp->cdp_dirents[dm->dm_idx] = de;
616		KASSERT(de->de_cdp != (void *)0xdeadc0de,
617		    ("%s %d\n", __func__, __LINE__));
618		dev_unlock();
619		return (1);
620	}
621	dev_unlock();
622	return (0);
623}
624
625/*
626 * The caller needs to hold the dm for the duration of the call.
627 */
628void
629devfs_populate(struct devfs_mount *dm)
630{
631	unsigned gen;
632
633	sx_assert(&dm->dm_lock, SX_XLOCKED);
634	gen = devfs_generation;
635	if (dm->dm_generation == gen)
636		return;
637	while (devfs_populate_loop(dm, 0))
638		continue;
639	dm->dm_generation = gen;
640}
641
642/*
643 * The caller needs to hold the dm for the duration of the call.
644 */
645void
646devfs_cleanup(struct devfs_mount *dm)
647{
648
649	sx_assert(&dm->dm_lock, SX_XLOCKED);
650	while (devfs_populate_loop(dm, 1))
651		continue;
652	devfs_purge(dm, dm->dm_rootdir);
653}
654
655/*
656 * devfs_create() and devfs_destroy() are called from kern_conf.c and
657 * in both cases the devlock() mutex is held, so no further locking
658 * is necessary and no sleeping allowed.
659 */
660
661void
662devfs_create(struct cdev *dev)
663{
664	struct cdev_priv *cdp;
665
666	mtx_assert(&devmtx, MA_OWNED);
667	cdp = cdev2priv(dev);
668	cdp->cdp_flags |= CDP_ACTIVE;
669	cdp->cdp_inode = alloc_unrl(devfs_inos);
670	dev_refl(dev);
671	TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
672	devfs_generation++;
673}
674
675void
676devfs_destroy(struct cdev *dev)
677{
678	struct cdev_priv *cdp;
679
680	mtx_assert(&devmtx, MA_OWNED);
681	cdp = cdev2priv(dev);
682	cdp->cdp_flags &= ~CDP_ACTIVE;
683	devfs_generation++;
684}
685
686ino_t
687devfs_alloc_cdp_inode(void)
688{
689
690	return (alloc_unr(devfs_inos));
691}
692
693void
694devfs_free_cdp_inode(ino_t ino)
695{
696
697	if (ino > 0)
698		free_unr(devfs_inos, ino);
699}
700
701static void
702devfs_devs_init(void *junk __unused)
703{
704
705	devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
706}
707
708SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);
709