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