xref: /freebsd/sys/fs/devfs/devfs_devs.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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_devfs.h"
32 #include "opt_mac.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/dirent.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/mac.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/sx.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 
48 #include <sys/kdb.h>
49 
50 #include <fs/devfs/devfs.h>
51 #include <fs/devfs/devfs_int.h>
52 
53 /*
54  * The one true (but secret) list of active devices in the system.
55  * Locked by dev_lock()/devmtx
56  */
57 static TAILQ_HEAD(,cdev_priv) 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 #ifdef MAC
184 	mac_init_devfsdirent(de);
185 #endif
186 	return (de);
187 }
188 
189 struct devfs_dirent *
190 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode)
191 {
192 	struct devfs_dirent *dd;
193 	struct devfs_dirent *de;
194 
195 	/* Create the new directory */
196 	dd = devfs_newdirent(name, namelen);
197 	TAILQ_INIT(&dd->de_dlist);
198 	dd->de_dirent->d_type = DT_DIR;
199 	dd->de_mode = 0555;
200 	dd->de_links = 2;
201 	dd->de_dir = dd;
202 	if (inode != 0)
203 		dd->de_inode = inode;
204 	else
205 		dd->de_inode = alloc_unr(devfs_inos);
206 
207 	/* Create the "." entry in the new directory */
208 	de = devfs_newdirent(".", 1);
209 	de->de_dirent->d_type = DT_DIR;
210 	de->de_flags |= DE_DOT;
211 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
212 	de->de_dir = dd;
213 
214 	/* Create the ".." entry in the new directory */
215 	de = devfs_newdirent("..", 2);
216 	de->de_dirent->d_type = DT_DIR;
217 	de->de_flags |= DE_DOTDOT;
218 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
219 	if (dotdot == NULL) {
220 		de->de_dir = dd;
221 	} else {
222 		de->de_dir = dotdot;
223 		TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
224 		dotdot->de_links++;
225 	}
226 
227 #ifdef MAC
228 	mac_create_devfs_directory(dmp->dm_mount, name, namelen, dd);
229 #endif
230 	return (dd);
231 }
232 
233 void
234 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de)
235 {
236 
237 	if (de->de_symlink) {
238 		free(de->de_symlink, M_DEVFS);
239 		de->de_symlink = NULL;
240 	}
241 	if (de->de_vnode != NULL) {
242 		vhold(de->de_vnode);
243 		de->de_vnode->v_data = NULL;
244 		vgone(de->de_vnode);
245 		vdrop(de->de_vnode);
246 		de->de_vnode = NULL;
247 	}
248 #ifdef MAC
249 	mac_destroy_devfsdirent(de);
250 #endif
251 	if (de->de_inode > DEVFS_ROOTINO) {
252 		free_unr(devfs_inos, de->de_inode);
253 		de->de_inode = 0;
254 	}
255 	free(de, M_DEVFS3);
256 }
257 
258 /*
259  * Called on unmount.
260  * Recursively removes the entire tree
261  */
262 
263 static void
264 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
265 {
266 	struct devfs_dirent *de;
267 
268 	sx_assert(&dm->dm_lock, SX_XLOCKED);
269 	for (;;) {
270 		de = TAILQ_FIRST(&dd->de_dlist);
271 		if (de == NULL)
272 			break;
273 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
274 		if (de->de_flags & (DE_DOT|DE_DOTDOT))
275 			devfs_delete(dm, de);
276 		else if (de->de_dirent->d_type == DT_DIR)
277 			devfs_purge(dm, de);
278 		else
279 			devfs_delete(dm, de);
280 	}
281 	devfs_delete(dm, dd);
282 }
283 
284 /*
285  * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
286  * by the mount points dm_idx.
287  * This function extends the array when necessary, taking into account that
288  * the default array is 1 element and not malloc'ed.
289  */
290 static void
291 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
292 {
293 	struct devfs_dirent **dep;
294 	int siz;
295 
296 	siz = (dm->dm_idx + 1) * sizeof *dep;
297 	dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
298 	dev_lock();
299 	if (dm->dm_idx <= cdp->cdp_maxdirent) {
300 		/* We got raced */
301 		dev_unlock();
302 		free(dep, M_DEVFS2);
303 		return;
304 	}
305 	memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
306 	if (cdp->cdp_maxdirent > 0)
307 		free(cdp->cdp_dirents, M_DEVFS2);
308 	cdp->cdp_dirents = dep;
309 	/*
310 	 * XXX: if malloc told us how much we actually got this could
311 	 * XXX: be optimized.
312 	 */
313 	cdp->cdp_maxdirent = dm->dm_idx;
314 	dev_unlock();
315 }
316 
317 static int
318 devfs_populate_loop(struct devfs_mount *dm, int cleanup)
319 {
320 	struct cdev_priv *cdp;
321 	struct devfs_dirent *de;
322 	struct devfs_dirent *dd;
323 	struct cdev *pdev;
324 	int j;
325 	char *q, *s;
326 
327 	sx_assert(&dm->dm_lock, SX_XLOCKED);
328 	dev_lock();
329 	TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
330 
331 		KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
332 
333 		/*
334 		 * If we are unmounting, or the device has been destroyed,
335 		 * clean up our dirent.
336 		 */
337 		if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
338 		    dm->dm_idx <= cdp->cdp_maxdirent &&
339 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
340 			de = cdp->cdp_dirents[dm->dm_idx];
341 			cdp->cdp_dirents[dm->dm_idx] = NULL;
342 			cdp->cdp_inuse--;
343 			KASSERT(cdp == de->de_cdp,
344 			    ("%s %d %s %p %p", __func__, __LINE__,
345 			    cdp->cdp_c.si_name, cdp, de->de_cdp));
346 			KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
347 			dev_unlock();
348 
349 			TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
350 			de->de_cdp = NULL;
351 			de->de_inode = 0;
352 			devfs_delete(dm, de);
353 			return (1);
354 		}
355 		/*
356 	 	 * GC any lingering devices
357 		 */
358 		if (!(cdp->cdp_flags & CDP_ACTIVE)) {
359 			if (cdp->cdp_inuse > 0)
360 				continue;
361 			TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
362 			dev_unlock();
363 			dev_rel(&cdp->cdp_c);
364 			return (1);
365 		}
366 		/*
367 		 * Don't create any new dirents if we are unmounting
368 		 */
369 		if (cleanup)
370 			continue;
371 		KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
372 
373 		if (dm->dm_idx <= cdp->cdp_maxdirent &&
374 		    cdp->cdp_dirents[dm->dm_idx] != NULL) {
375 			de = cdp->cdp_dirents[dm->dm_idx];
376 			KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
377 			continue;
378 		}
379 
380 
381 		cdp->cdp_inuse++;
382 		dev_unlock();
383 
384 		if (dm->dm_idx > cdp->cdp_maxdirent)
385 		        devfs_metoo(cdp, dm);
386 
387 		dd = dm->dm_rootdir;
388 		s = cdp->cdp_c.si_name;
389 		for (;;) {
390 			for (q = s; *q != '/' && *q != '\0'; q++)
391 				continue;
392 			if (*q != '/')
393 				break;
394 			de = devfs_find(dd, s, q - s);
395 			if (de == NULL)
396 				de = devfs_vmkdir(dm, s, q - s, dd, 0);
397 			s = q + 1;
398 			dd = de;
399 		}
400 
401 		de = devfs_newdirent(s, q - s);
402 		if (cdp->cdp_c.si_flags & SI_ALIAS) {
403 			de->de_uid = 0;
404 			de->de_gid = 0;
405 			de->de_mode = 0755;
406 			de->de_dirent->d_type = DT_LNK;
407 			pdev = cdp->cdp_c.si_parent;
408 			j = strlen(pdev->si_name) + 1;
409 			de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
410 			bcopy(pdev->si_name, de->de_symlink, j);
411 		} else {
412 			de->de_uid = cdp->cdp_c.si_uid;
413 			de->de_gid = cdp->cdp_c.si_gid;
414 			de->de_mode = cdp->cdp_c.si_mode;
415 			de->de_dirent->d_type = DT_CHR;
416 		}
417 		de->de_inode = cdp->cdp_inode;
418 		de->de_cdp = cdp;
419 #ifdef MAC
420 		mac_create_devfs_device(cdp->cdp_c.si_cred, dm->dm_mount,
421 		    &cdp->cdp_c, de);
422 #endif
423 		de->de_dir = dd;
424 		TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
425 		devfs_rules_apply(dm, de);
426 		dev_lock();
427 		/* XXX: could check that cdp is still active here */
428 		KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
429 		    ("%s %d\n", __func__, __LINE__));
430 		cdp->cdp_dirents[dm->dm_idx] = de;
431 		KASSERT(de->de_cdp != (void *)0xdeadc0de,
432 		    ("%s %d\n", __func__, __LINE__));
433 		dev_unlock();
434 		return (1);
435 	}
436 	dev_unlock();
437 	return (0);
438 }
439 
440 void
441 devfs_populate(struct devfs_mount *dm)
442 {
443 
444 	sx_assert(&dm->dm_lock, SX_XLOCKED);
445 	if (dm->dm_generation == devfs_generation)
446 		return;
447 	while (devfs_populate_loop(dm, 0))
448 		continue;
449 	dm->dm_generation = devfs_generation;
450 }
451 
452 void
453 devfs_cleanup(struct devfs_mount *dm)
454 {
455 
456 	sx_assert(&dm->dm_lock, SX_XLOCKED);
457 	while (devfs_populate_loop(dm, 1))
458 		continue;
459 	devfs_purge(dm, dm->dm_rootdir);
460 }
461 
462 /*
463  * devfs_create() and devfs_destroy() are called from kern_conf.c and
464  * in both cases the devlock() mutex is held, so no further locking
465  * is necesary and no sleeping allowed.
466  */
467 
468 void
469 devfs_create(struct cdev *dev)
470 {
471 	struct cdev_priv *cdp;
472 
473 	mtx_assert(&devmtx, MA_OWNED);
474 	cdp = dev->si_priv;
475 	cdp->cdp_flags |= CDP_ACTIVE;
476 	cdp->cdp_inode = alloc_unrl(devfs_inos);
477 	dev_refl(dev);
478 	TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
479 	devfs_generation++;
480 }
481 
482 void
483 devfs_destroy(struct cdev *dev)
484 {
485 	struct cdev_priv *cdp;
486 
487 	mtx_assert(&devmtx, MA_OWNED);
488 	cdp = dev->si_priv;
489 	cdp->cdp_flags &= ~CDP_ACTIVE;
490 	devfs_generation++;
491 }
492 
493 static void
494 devfs_devs_init(void *junk __unused)
495 {
496 
497 	devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
498 }
499 
500 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);
501