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