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 int 146 devfs_dev_exists(const char *name) 147 { 148 struct cdev_priv *cdp; 149 150 mtx_assert(&devmtx, MA_OWNED); 151 152 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 153 if ((cdp->cdp_flags & CDP_ACTIVE) == 0) 154 continue; 155 if (devfs_pathpath(cdp->cdp_c.si_name, name) != 0) 156 return (1); 157 if (devfs_pathpath(name, cdp->cdp_c.si_name) != 0) 158 return (1); 159 } 160 if (devfs_dir_find(name) != 0) 161 return (1); 162 163 return (0); 164 } 165 166 void 167 devfs_free(struct cdev *cdev) 168 { 169 struct cdev_priv *cdp; 170 171 cdp = cdev2priv(cdev); 172 if (cdev->si_cred != NULL) 173 crfree(cdev->si_cred); 174 if (cdp->cdp_inode > 0) 175 free_unr(devfs_inos, cdp->cdp_inode); 176 if (cdp->cdp_maxdirent > 0) 177 free(cdp->cdp_dirents, M_DEVFS2); 178 free(cdp, M_CDEVP); 179 } 180 181 struct devfs_dirent * 182 devfs_find(struct devfs_dirent *dd, const char *name, int namelen, int type) 183 { 184 struct devfs_dirent *de; 185 186 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 187 if (namelen != de->de_dirent->d_namlen) 188 continue; 189 if (type != 0 && type != de->de_dirent->d_type) 190 continue; 191 if (bcmp(name, de->de_dirent->d_name, namelen) != 0) 192 continue; 193 break; 194 } 195 KASSERT(de == NULL || (de->de_flags & DE_DOOMED) == 0, 196 ("devfs_find: returning a doomed entry")); 197 return (de); 198 } 199 200 struct devfs_dirent * 201 devfs_newdirent(char *name, int namelen) 202 { 203 int i; 204 struct devfs_dirent *de; 205 struct dirent d; 206 207 d.d_namlen = namelen; 208 i = sizeof (*de) + GENERIC_DIRSIZ(&d); 209 de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO); 210 de->de_dirent = (struct dirent *)(de + 1); 211 de->de_dirent->d_namlen = namelen; 212 de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d); 213 bcopy(name, de->de_dirent->d_name, namelen); 214 de->de_dirent->d_name[namelen] = '\0'; 215 vfs_timestamp(&de->de_ctime); 216 de->de_mtime = de->de_atime = de->de_ctime; 217 de->de_links = 1; 218 de->de_holdcnt = 1; 219 #ifdef MAC 220 mac_devfs_init(de); 221 #endif 222 return (de); 223 } 224 225 struct devfs_dirent * 226 devfs_parent_dirent(struct devfs_dirent *de) 227 { 228 229 if (de->de_dirent->d_type != DT_DIR) 230 return (de->de_dir); 231 232 if (de->de_flags & (DE_DOT | DE_DOTDOT)) 233 return (NULL); 234 235 de = TAILQ_FIRST(&de->de_dlist); /* "." */ 236 if (de == NULL) 237 return (NULL); 238 de = TAILQ_NEXT(de, de_list); /* ".." */ 239 if (de == NULL) 240 return (NULL); 241 242 return (de->de_dir); 243 } 244 245 struct devfs_dirent * 246 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode) 247 { 248 struct devfs_dirent *dd; 249 struct devfs_dirent *de; 250 251 /* Create the new directory */ 252 dd = devfs_newdirent(name, namelen); 253 TAILQ_INIT(&dd->de_dlist); 254 dd->de_dirent->d_type = DT_DIR; 255 dd->de_mode = 0555; 256 dd->de_links = 2; 257 dd->de_dir = dd; 258 if (inode != 0) 259 dd->de_inode = inode; 260 else 261 dd->de_inode = alloc_unr(devfs_inos); 262 263 /* 264 * "." and ".." are always the two first entries in the 265 * de_dlist list. 266 * 267 * Create the "." entry in the new directory. 268 */ 269 de = devfs_newdirent(".", 1); 270 de->de_dirent->d_type = DT_DIR; 271 de->de_flags |= DE_DOT; 272 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 273 de->de_dir = dd; 274 275 /* Create the ".." entry in the new directory. */ 276 de = devfs_newdirent("..", 2); 277 de->de_dirent->d_type = DT_DIR; 278 de->de_flags |= DE_DOTDOT; 279 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 280 if (dotdot == NULL) { 281 de->de_dir = dd; 282 } else { 283 de->de_dir = dotdot; 284 sx_assert(&dmp->dm_lock, SX_XLOCKED); 285 TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list); 286 dotdot->de_links++; 287 devfs_rules_apply(dmp, dd); 288 } 289 290 #ifdef MAC 291 mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd); 292 #endif 293 return (dd); 294 } 295 296 void 297 devfs_dirent_free(struct devfs_dirent *de) 298 { 299 free(de, M_DEVFS3); 300 } 301 302 /* 303 * Removes a directory if it is empty. Also empty parent directories are 304 * removed recursively. 305 */ 306 static void 307 devfs_rmdir_empty(struct devfs_mount *dm, struct devfs_dirent *de) 308 { 309 struct devfs_dirent *dd, *de_dot, *de_dotdot; 310 311 sx_assert(&dm->dm_lock, SX_XLOCKED); 312 313 for (;;) { 314 KASSERT(de->de_dirent->d_type == DT_DIR, 315 ("devfs_rmdir_empty: de is not a directory")); 316 317 if ((de->de_flags & DE_DOOMED) != 0 || de == dm->dm_rootdir) 318 return; 319 320 de_dot = TAILQ_FIRST(&de->de_dlist); 321 KASSERT(de_dot != NULL, ("devfs_rmdir_empty: . missing")); 322 de_dotdot = TAILQ_NEXT(de_dot, de_list); 323 KASSERT(de_dotdot != NULL, ("devfs_rmdir_empty: .. missing")); 324 /* Return if the directory is not empty. */ 325 if (TAILQ_NEXT(de_dotdot, de_list) != NULL) 326 return; 327 328 dd = devfs_parent_dirent(de); 329 KASSERT(dd != NULL, ("devfs_rmdir_empty: NULL dd")); 330 TAILQ_REMOVE(&de->de_dlist, de_dot, de_list); 331 TAILQ_REMOVE(&de->de_dlist, de_dotdot, de_list); 332 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 333 DEVFS_DE_HOLD(dd); 334 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 335 devfs_delete(dm, de_dot, DEVFS_DEL_NORECURSE); 336 devfs_delete(dm, de_dotdot, DEVFS_DEL_NORECURSE); 337 if (DEVFS_DE_DROP(dd)) { 338 devfs_dirent_free(dd); 339 return; 340 } 341 342 de = dd; 343 } 344 } 345 346 /* 347 * The caller needs to hold the dm for the duration of the call since 348 * dm->dm_lock may be temporary dropped. 349 */ 350 void 351 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int flags) 352 { 353 struct devfs_dirent *dd; 354 struct vnode *vp; 355 356 KASSERT((de->de_flags & DE_DOOMED) == 0, 357 ("devfs_delete doomed dirent")); 358 de->de_flags |= DE_DOOMED; 359 360 if ((flags & DEVFS_DEL_NORECURSE) == 0) { 361 dd = devfs_parent_dirent(de); 362 if (dd != NULL) 363 DEVFS_DE_HOLD(dd); 364 if (de->de_flags & DE_USER) { 365 KASSERT(dd != NULL, ("devfs_delete: NULL dd")); 366 devfs_dir_unref_de(dm, dd); 367 } 368 } else 369 dd = NULL; 370 371 mtx_lock(&devfs_de_interlock); 372 vp = de->de_vnode; 373 if (vp != NULL) { 374 VI_LOCK(vp); 375 mtx_unlock(&devfs_de_interlock); 376 vholdl(vp); 377 sx_unlock(&dm->dm_lock); 378 if ((flags & DEVFS_DEL_VNLOCKED) == 0) 379 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 380 else 381 VI_UNLOCK(vp); 382 vgone(vp); 383 if ((flags & DEVFS_DEL_VNLOCKED) == 0) 384 VOP_UNLOCK(vp, 0); 385 vdrop(vp); 386 sx_xlock(&dm->dm_lock); 387 } else 388 mtx_unlock(&devfs_de_interlock); 389 if (de->de_symlink) { 390 free(de->de_symlink, M_DEVFS); 391 de->de_symlink = NULL; 392 } 393 #ifdef MAC 394 mac_devfs_destroy(de); 395 #endif 396 if (de->de_inode > DEVFS_ROOTINO) { 397 free_unr(devfs_inos, de->de_inode); 398 de->de_inode = 0; 399 } 400 if (DEVFS_DE_DROP(de)) 401 devfs_dirent_free(de); 402 403 if (dd != NULL) { 404 if (DEVFS_DE_DROP(dd)) 405 devfs_dirent_free(dd); 406 else 407 devfs_rmdir_empty(dm, dd); 408 } 409 } 410 411 /* 412 * Called on unmount. 413 * Recursively removes the entire tree. 414 * The caller needs to hold the dm for the duration of the call. 415 */ 416 417 static void 418 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd) 419 { 420 struct devfs_dirent *de; 421 422 sx_assert(&dm->dm_lock, SX_XLOCKED); 423 424 DEVFS_DE_HOLD(dd); 425 for (;;) { 426 /* 427 * Use TAILQ_LAST() to remove "." and ".." last. 428 * We might need ".." to resolve a path in 429 * devfs_dir_unref_de(). 430 */ 431 de = TAILQ_LAST(&dd->de_dlist, devfs_dlist_head); 432 if (de == NULL) 433 break; 434 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 435 if (de->de_flags & DE_USER) 436 devfs_dir_unref_de(dm, dd); 437 if (de->de_flags & (DE_DOT | DE_DOTDOT)) 438 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 439 else if (de->de_dirent->d_type == DT_DIR) 440 devfs_purge(dm, de); 441 else 442 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 443 } 444 if (DEVFS_DE_DROP(dd)) 445 devfs_dirent_free(dd); 446 else if ((dd->de_flags & DE_DOOMED) == 0) 447 devfs_delete(dm, dd, DEVFS_DEL_NORECURSE); 448 } 449 450 /* 451 * Each cdev_priv has an array of pointers to devfs_dirent which is indexed 452 * by the mount points dm_idx. 453 * This function extends the array when necessary, taking into account that 454 * the default array is 1 element and not malloc'ed. 455 */ 456 static void 457 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm) 458 { 459 struct devfs_dirent **dep; 460 int siz; 461 462 siz = (dm->dm_idx + 1) * sizeof *dep; 463 dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO); 464 dev_lock(); 465 if (dm->dm_idx <= cdp->cdp_maxdirent) { 466 /* We got raced */ 467 dev_unlock(); 468 free(dep, M_DEVFS2); 469 return; 470 } 471 memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep); 472 if (cdp->cdp_maxdirent > 0) 473 free(cdp->cdp_dirents, M_DEVFS2); 474 cdp->cdp_dirents = dep; 475 /* 476 * XXX: if malloc told us how much we actually got this could 477 * XXX: be optimized. 478 */ 479 cdp->cdp_maxdirent = dm->dm_idx; 480 dev_unlock(); 481 } 482 483 /* 484 * The caller needs to hold the dm for the duration of the call. 485 */ 486 static int 487 devfs_populate_loop(struct devfs_mount *dm, int cleanup) 488 { 489 struct cdev_priv *cdp; 490 struct devfs_dirent *de; 491 struct devfs_dirent *dd; 492 struct cdev *pdev; 493 int de_flags, j; 494 char *q, *s; 495 496 sx_assert(&dm->dm_lock, SX_XLOCKED); 497 dev_lock(); 498 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 499 500 KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents")); 501 502 /* 503 * If we are unmounting, or the device has been destroyed, 504 * clean up our dirent. 505 */ 506 if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) && 507 dm->dm_idx <= cdp->cdp_maxdirent && 508 cdp->cdp_dirents[dm->dm_idx] != NULL) { 509 de = cdp->cdp_dirents[dm->dm_idx]; 510 cdp->cdp_dirents[dm->dm_idx] = NULL; 511 KASSERT(cdp == de->de_cdp, 512 ("%s %d %s %p %p", __func__, __LINE__, 513 cdp->cdp_c.si_name, cdp, de->de_cdp)); 514 KASSERT(de->de_dir != NULL, ("Null de->de_dir")); 515 dev_unlock(); 516 517 TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list); 518 de->de_cdp = NULL; 519 de->de_inode = 0; 520 devfs_delete(dm, de, 0); 521 dev_lock(); 522 cdp->cdp_inuse--; 523 dev_unlock(); 524 return (1); 525 } 526 /* 527 * GC any lingering devices 528 */ 529 if (!(cdp->cdp_flags & CDP_ACTIVE)) { 530 if (cdp->cdp_inuse > 0) 531 continue; 532 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list); 533 dev_unlock(); 534 dev_rel(&cdp->cdp_c); 535 return (1); 536 } 537 /* 538 * Don't create any new dirents if we are unmounting 539 */ 540 if (cleanup) 541 continue; 542 KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!")); 543 544 if (dm->dm_idx <= cdp->cdp_maxdirent && 545 cdp->cdp_dirents[dm->dm_idx] != NULL) { 546 de = cdp->cdp_dirents[dm->dm_idx]; 547 KASSERT(cdp == de->de_cdp, ("inconsistent cdp")); 548 continue; 549 } 550 551 552 cdp->cdp_inuse++; 553 dev_unlock(); 554 555 if (dm->dm_idx > cdp->cdp_maxdirent) 556 devfs_metoo(cdp, dm); 557 558 dd = dm->dm_rootdir; 559 s = cdp->cdp_c.si_name; 560 for (;;) { 561 for (q = s; *q != '/' && *q != '\0'; q++) 562 continue; 563 if (*q != '/') 564 break; 565 de = devfs_find(dd, s, q - s, 0); 566 if (de == NULL) 567 de = devfs_vmkdir(dm, s, q - s, dd, 0); 568 else if (de->de_dirent->d_type == DT_LNK) { 569 de = devfs_find(dd, s, q - s, DT_DIR); 570 if (de == NULL) 571 de = devfs_vmkdir(dm, s, q - s, dd, 0); 572 de->de_flags |= DE_COVERED; 573 } 574 s = q + 1; 575 dd = de; 576 KASSERT(dd->de_dirent->d_type == DT_DIR && 577 (dd->de_flags & (DE_DOT | DE_DOTDOT)) == 0, 578 ("%s: invalid directory (si_name=%s)", 579 __func__, cdp->cdp_c.si_name)); 580 581 } 582 de_flags = 0; 583 de = devfs_find(dd, s, q - s, DT_LNK); 584 if (de != NULL) 585 de_flags |= DE_COVERED; 586 587 de = devfs_newdirent(s, q - s); 588 if (cdp->cdp_c.si_flags & SI_ALIAS) { 589 de->de_uid = 0; 590 de->de_gid = 0; 591 de->de_mode = 0755; 592 de->de_dirent->d_type = DT_LNK; 593 pdev = cdp->cdp_c.si_parent; 594 j = strlen(pdev->si_name) + 1; 595 de->de_symlink = malloc(j, M_DEVFS, M_WAITOK); 596 bcopy(pdev->si_name, de->de_symlink, j); 597 } else { 598 de->de_uid = cdp->cdp_c.si_uid; 599 de->de_gid = cdp->cdp_c.si_gid; 600 de->de_mode = cdp->cdp_c.si_mode; 601 de->de_dirent->d_type = DT_CHR; 602 } 603 de->de_flags |= de_flags; 604 de->de_inode = cdp->cdp_inode; 605 de->de_cdp = cdp; 606 #ifdef MAC 607 mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount, 608 &cdp->cdp_c, de); 609 #endif 610 de->de_dir = dd; 611 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 612 devfs_rules_apply(dm, de); 613 dev_lock(); 614 /* XXX: could check that cdp is still active here */ 615 KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL, 616 ("%s %d\n", __func__, __LINE__)); 617 cdp->cdp_dirents[dm->dm_idx] = de; 618 KASSERT(de->de_cdp != (void *)0xdeadc0de, 619 ("%s %d\n", __func__, __LINE__)); 620 dev_unlock(); 621 return (1); 622 } 623 dev_unlock(); 624 return (0); 625 } 626 627 /* 628 * The caller needs to hold the dm for the duration of the call. 629 */ 630 void 631 devfs_populate(struct devfs_mount *dm) 632 { 633 634 sx_assert(&dm->dm_lock, SX_XLOCKED); 635 if (dm->dm_generation == devfs_generation) 636 return; 637 while (devfs_populate_loop(dm, 0)) 638 continue; 639 dm->dm_generation = devfs_generation; 640 } 641 642 /* 643 * The caller needs to hold the dm for the duration of the call. 644 */ 645 void 646 devfs_cleanup(struct devfs_mount *dm) 647 { 648 649 sx_assert(&dm->dm_lock, SX_XLOCKED); 650 while (devfs_populate_loop(dm, 1)) 651 continue; 652 devfs_purge(dm, dm->dm_rootdir); 653 } 654 655 /* 656 * devfs_create() and devfs_destroy() are called from kern_conf.c and 657 * in both cases the devlock() mutex is held, so no further locking 658 * is necesary and no sleeping allowed. 659 */ 660 661 void 662 devfs_create(struct cdev *dev) 663 { 664 struct cdev_priv *cdp; 665 666 mtx_assert(&devmtx, MA_OWNED); 667 cdp = cdev2priv(dev); 668 cdp->cdp_flags |= CDP_ACTIVE; 669 cdp->cdp_inode = alloc_unrl(devfs_inos); 670 dev_refl(dev); 671 TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list); 672 devfs_generation++; 673 } 674 675 void 676 devfs_destroy(struct cdev *dev) 677 { 678 struct cdev_priv *cdp; 679 680 mtx_assert(&devmtx, MA_OWNED); 681 cdp = cdev2priv(dev); 682 cdp->cdp_flags &= ~CDP_ACTIVE; 683 devfs_generation++; 684 } 685 686 static void 687 devfs_devs_init(void *junk __unused) 688 { 689 690 devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx); 691 } 692 693 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL); 694