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