xref: /freebsd/sys/fs/devfs/devfs_devs.c (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
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 "opt_mac.h"
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 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
67 
68 static unsigned devfs_generation;
69 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
70 	&devfs_generation, 0, "DEVFS generation number");
71 
72 unsigned devfs_rule_depth = 1;
73 SYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
74 	&devfs_rule_depth, 0, "Max depth of ruleset include");
75 
76 /*
77  * Helper sysctl for devname(3).  We're given a struct cdev * and return
78  * the name, if any, registered by the device driver.
79  */
80 static int
81 sysctl_devname(SYSCTL_HANDLER_ARGS)
82 {
83 	int error;
84 	dev_t ud;
85 	struct cdev_priv *cdp;
86 
87 	error = SYSCTL_IN(req, &ud, sizeof (ud));
88 	if (error)
89 		return (error);
90 	if (ud == NODEV)
91 		return(EINVAL);
92 /*
93 	ud ^ devfs_random();
94 */
95 	dev_lock();
96 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list)
97 		if (cdp->cdp_inode == ud)
98 			break;
99 	dev_unlock();
100 	if (cdp == NULL)
101 		return(ENOENT);
102 	return(SYSCTL_OUT(req, cdp->cdp_c.si_name, strlen(cdp->cdp_c.si_name) + 1));
103 	return (error);
104 }
105 
106 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
107 	NULL, 0, sysctl_devname, "", "devname(3) handler");
108 
109 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
110     0, sizeof(struct cdev), "sizeof(struct cdev)");
111 
112 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD,
113     0, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)");
114 
115 struct cdev *
116 devfs_alloc(void)
117 {
118 	struct cdev_priv *cdp;
119 	struct cdev *cdev;
120 
121 	cdp = malloc(sizeof *cdp, M_CDEVP, M_USE_RESERVE | M_ZERO | M_WAITOK);
122 
123 	cdp->cdp_dirents = &cdp->cdp_dirent0;
124 	cdp->cdp_dirent0 = NULL;
125 	cdp->cdp_maxdirent = 0;
126 
127 	cdev = &cdp->cdp_c;
128 	cdev->si_priv = cdp;
129 
130 	cdev->si_name = cdev->__si_namebuf;
131 	LIST_INIT(&cdev->si_children);
132 	return (cdev);
133 }
134 
135 void
136 devfs_free(struct cdev *cdev)
137 {
138 	struct cdev_priv *cdp;
139 
140 	cdp = cdev->si_priv;
141 	if (cdev->si_cred != NULL)
142 		crfree(cdev->si_cred);
143 	if (cdp->cdp_inode > 0)
144 		free_unr(devfs_inos, cdp->cdp_inode);
145 	if (cdp->cdp_maxdirent > 0)
146 		free(cdp->cdp_dirents, M_DEVFS2);
147 	free(cdp, M_CDEVP);
148 }
149 
150 struct devfs_dirent *
151 devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
152 {
153 	struct devfs_dirent *de;
154 
155 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
156 		if (namelen != de->de_dirent->d_namlen)
157 			continue;
158 		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
159 			continue;
160 		break;
161 	}
162 	return (de);
163 }
164 
165 struct devfs_dirent *
166 devfs_newdirent(char *name, int namelen)
167 {
168 	int i;
169 	struct devfs_dirent *de;
170 	struct dirent d;
171 
172 	d.d_namlen = namelen;
173 	i = sizeof (*de) + GENERIC_DIRSIZ(&d);
174 	de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO);
175 	de->de_dirent = (struct dirent *)(de + 1);
176 	de->de_dirent->d_namlen = namelen;
177 	de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
178 	bcopy(name, de->de_dirent->d_name, namelen);
179 	de->de_dirent->d_name[namelen] = '\0';
180 	vfs_timestamp(&de->de_ctime);
181 	de->de_mtime = de->de_atime = de->de_ctime;
182 	de->de_links = 1;
183 	de->de_holdcnt = 1;
184 #ifdef MAC
185 	mac_devfs_init(de);
186 #endif
187 	return (de);
188 }
189 
190 struct devfs_dirent *
191 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode)
192 {
193 	struct devfs_dirent *dd;
194 	struct devfs_dirent *de;
195 
196 	/* Create the new directory */
197 	dd = devfs_newdirent(name, namelen);
198 	TAILQ_INIT(&dd->de_dlist);
199 	dd->de_dirent->d_type = DT_DIR;
200 	dd->de_mode = 0555;
201 	dd->de_links = 2;
202 	dd->de_dir = dd;
203 	if (inode != 0)
204 		dd->de_inode = inode;
205 	else
206 		dd->de_inode = alloc_unr(devfs_inos);
207 
208 	/* Create the "." entry in the new directory */
209 	de = devfs_newdirent(".", 1);
210 	de->de_dirent->d_type = DT_DIR;
211 	de->de_flags |= DE_DOT;
212 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
213 	de->de_dir = dd;
214 
215 	/* Create the ".." entry in the new directory */
216 	de = devfs_newdirent("..", 2);
217 	de->de_dirent->d_type = DT_DIR;
218 	de->de_flags |= DE_DOTDOT;
219 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
220 	if (dotdot == NULL) {
221 		de->de_dir = dd;
222 	} else {
223 		de->de_dir = dotdot;
224 		TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
225 		dotdot->de_links++;
226 	}
227 
228 #ifdef MAC
229 	mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd);
230 #endif
231 	return (dd);
232 }
233 
234 void
235 devfs_dirent_free(struct devfs_dirent *de)
236 {
237 	free(de, M_DEVFS3);
238 }
239 
240 /*
241  * The caller needs to hold the dm for the duration of the call since
242  * dm->dm_lock may be temporary dropped.
243  */
244 void
245 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int vp_locked)
246 {
247 	struct vnode *vp;
248 
249 	KASSERT((de->de_flags & DE_DOOMED) == 0,
250 		("devfs_delete doomed dirent"));
251 	de->de_flags |= DE_DOOMED;
252 	mtx_lock(&devfs_de_interlock);
253 	vp = de->de_vnode;
254 	if (vp != NULL) {
255 		VI_LOCK(vp);
256 		mtx_unlock(&devfs_de_interlock);
257 		vholdl(vp);
258 		sx_unlock(&dm->dm_lock);
259 		if (!vp_locked)
260 			vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
261 		else
262 			VI_UNLOCK(vp);
263 		vgone(vp);
264 		if (!vp_locked)
265 			VOP_UNLOCK(vp, 0);
266 		vdrop(vp);
267 		sx_xlock(&dm->dm_lock);
268 	} else
269 		mtx_unlock(&devfs_de_interlock);
270 	if (de->de_symlink) {
271 		free(de->de_symlink, M_DEVFS);
272 		de->de_symlink = NULL;
273 	}
274 #ifdef MAC
275 	mac_devfs_destroy(de);
276 #endif
277 	if (de->de_inode > DEVFS_ROOTINO) {
278 		free_unr(devfs_inos, de->de_inode);
279 		de->de_inode = 0;
280 	}
281 	if (DEVFS_DE_DROP(de))
282 		devfs_dirent_free(de);
283 }
284 
285 /*
286  * Called on unmount.
287  * Recursively removes the entire tree.
288  * The caller needs to hold the dm for the duration of the call.
289  */
290 
291 static void
292 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
293 {
294 	struct devfs_dirent *de;
295 
296 	sx_assert(&dm->dm_lock, SX_XLOCKED);
297 	for (;;) {
298 		de = TAILQ_FIRST(&dd->de_dlist);
299 		if (de == NULL)
300 			break;
301 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
302 		if (de->de_flags & (DE_DOT|DE_DOTDOT))
303 			devfs_delete(dm, de, 0);
304 		else if (de->de_dirent->d_type == DT_DIR)
305 			devfs_purge(dm, de);
306 		else
307 			devfs_delete(dm, de, 0);
308 	}
309 	devfs_delete(dm, dd, 0);
310 }
311 
312 /*
313  * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
314  * by the mount points dm_idx.
315  * This function extends the array when necessary, taking into account that
316  * the default array is 1 element and not malloc'ed.
317  */
318 static void
319 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
320 {
321 	struct devfs_dirent **dep;
322 	int siz;
323 
324 	siz = (dm->dm_idx + 1) * sizeof *dep;
325 	dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
326 	dev_lock();
327 	if (dm->dm_idx <= cdp->cdp_maxdirent) {
328 		/* We got raced */
329 		dev_unlock();
330 		free(dep, M_DEVFS2);
331 		return;
332 	}
333 	memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
334 	if (cdp->cdp_maxdirent > 0)
335 		free(cdp->cdp_dirents, M_DEVFS2);
336 	cdp->cdp_dirents = dep;
337 	/*
338 	 * XXX: if malloc told us how much we actually got this could
339 	 * XXX: be optimized.
340 	 */
341 	cdp->cdp_maxdirent = dm->dm_idx;
342 	dev_unlock();
343 }
344 
345 /*
346  * The caller needs to hold the dm for the duration of the call.
347  */
348 static int
349 devfs_populate_loop(struct devfs_mount *dm, int cleanup)
350 {
351 	struct cdev_priv *cdp;
352 	struct devfs_dirent *de;
353 	struct devfs_dirent *dd;
354 	struct cdev *pdev;
355 	int j;
356 	char *q, *s;
357 
358 	sx_assert(&dm->dm_lock, SX_XLOCKED);
359 	dev_lock();
360 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
361 
362 		KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
363 
364 		/*
365 		 * If we are unmounting, or the device has been destroyed,
366 		 * clean up our dirent.
367 		 */
368 		if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
369 		    dm->dm_idx <= cdp->cdp_maxdirent &&
370 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
371 			de = cdp->cdp_dirents[dm->dm_idx];
372 			cdp->cdp_dirents[dm->dm_idx] = NULL;
373 			KASSERT(cdp == de->de_cdp,
374 			    ("%s %d %s %p %p", __func__, __LINE__,
375 			    cdp->cdp_c.si_name, cdp, de->de_cdp));
376 			KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
377 			dev_unlock();
378 
379 			TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
380 			de->de_cdp = NULL;
381 			de->de_inode = 0;
382 			devfs_delete(dm, de, 0);
383 			dev_lock();
384 			cdp->cdp_inuse--;
385 			dev_unlock();
386 			return (1);
387 		}
388 		/*
389 	 	 * GC any lingering devices
390 		 */
391 		if (!(cdp->cdp_flags & CDP_ACTIVE)) {
392 			if (cdp->cdp_inuse > 0)
393 				continue;
394 			TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
395 			dev_unlock();
396 			dev_rel(&cdp->cdp_c);
397 			return (1);
398 		}
399 		/*
400 		 * Don't create any new dirents if we are unmounting
401 		 */
402 		if (cleanup)
403 			continue;
404 		KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
405 
406 		if (dm->dm_idx <= cdp->cdp_maxdirent &&
407 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
408 			de = cdp->cdp_dirents[dm->dm_idx];
409 			KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
410 			continue;
411 		}
412 
413 
414 		cdp->cdp_inuse++;
415 		dev_unlock();
416 
417 		if (dm->dm_idx > cdp->cdp_maxdirent)
418 		        devfs_metoo(cdp, dm);
419 
420 		dd = dm->dm_rootdir;
421 		s = cdp->cdp_c.si_name;
422 		for (;;) {
423 			for (q = s; *q != '/' && *q != '\0'; q++)
424 				continue;
425 			if (*q != '/')
426 				break;
427 			de = devfs_find(dd, s, q - s);
428 			if (de == NULL)
429 				de = devfs_vmkdir(dm, s, q - s, dd, 0);
430 			s = q + 1;
431 			dd = de;
432 		}
433 
434 		de = devfs_newdirent(s, q - s);
435 		if (cdp->cdp_c.si_flags & SI_ALIAS) {
436 			de->de_uid = 0;
437 			de->de_gid = 0;
438 			de->de_mode = 0755;
439 			de->de_dirent->d_type = DT_LNK;
440 			pdev = cdp->cdp_c.si_parent;
441 			j = strlen(pdev->si_name) + 1;
442 			de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
443 			bcopy(pdev->si_name, de->de_symlink, j);
444 		} else {
445 			de->de_uid = cdp->cdp_c.si_uid;
446 			de->de_gid = cdp->cdp_c.si_gid;
447 			de->de_mode = cdp->cdp_c.si_mode;
448 			de->de_dirent->d_type = DT_CHR;
449 		}
450 		de->de_inode = cdp->cdp_inode;
451 		de->de_cdp = cdp;
452 #ifdef MAC
453 		mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount,
454 		    &cdp->cdp_c, de);
455 #endif
456 		de->de_dir = dd;
457 		TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
458 		devfs_rules_apply(dm, de);
459 		dev_lock();
460 		/* XXX: could check that cdp is still active here */
461 		KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
462 		    ("%s %d\n", __func__, __LINE__));
463 		cdp->cdp_dirents[dm->dm_idx] = de;
464 		KASSERT(de->de_cdp != (void *)0xdeadc0de,
465 		    ("%s %d\n", __func__, __LINE__));
466 		dev_unlock();
467 		return (1);
468 	}
469 	dev_unlock();
470 	return (0);
471 }
472 
473 /*
474  * The caller needs to hold the dm for the duration of the call.
475  */
476 void
477 devfs_populate(struct devfs_mount *dm)
478 {
479 
480 	sx_assert(&dm->dm_lock, SX_XLOCKED);
481 	if (dm->dm_generation == devfs_generation)
482 		return;
483 	while (devfs_populate_loop(dm, 0))
484 		continue;
485 	dm->dm_generation = devfs_generation;
486 }
487 
488 /*
489  * The caller needs to hold the dm for the duration of the call.
490  */
491 void
492 devfs_cleanup(struct devfs_mount *dm)
493 {
494 
495 	sx_assert(&dm->dm_lock, SX_XLOCKED);
496 	while (devfs_populate_loop(dm, 1))
497 		continue;
498 	devfs_purge(dm, dm->dm_rootdir);
499 }
500 
501 /*
502  * devfs_create() and devfs_destroy() are called from kern_conf.c and
503  * in both cases the devlock() mutex is held, so no further locking
504  * is necesary and no sleeping allowed.
505  */
506 
507 void
508 devfs_create(struct cdev *dev)
509 {
510 	struct cdev_priv *cdp;
511 
512 	mtx_assert(&devmtx, MA_OWNED);
513 	cdp = dev->si_priv;
514 	cdp->cdp_flags |= CDP_ACTIVE;
515 	cdp->cdp_inode = alloc_unrl(devfs_inos);
516 	dev_refl(dev);
517 	TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
518 	devfs_generation++;
519 }
520 
521 void
522 devfs_destroy(struct cdev *dev)
523 {
524 	struct cdev_priv *cdp;
525 
526 	mtx_assert(&devmtx, MA_OWNED);
527 	cdp = dev->si_priv;
528 	cdp->cdp_flags &= ~CDP_ACTIVE;
529 	devfs_generation++;
530 }
531 
532 static void
533 devfs_devs_init(void *junk __unused)
534 {
535 
536 	devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
537 }
538 
539 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);
540