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