1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000,2004 5 * Poul-Henning Kamp. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Neither the name of the University nor the names of its contributors 13 * may be used to endorse or promote products derived from this software 14 * without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36 29 * 30 * $FreeBSD$ 31 */ 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 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 dev_t and return the 78 * 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 #ifdef COMPAT_FREEBSD11 86 uint32_t ud_compat; 87 #endif 88 struct cdev_priv *cdp; 89 struct cdev *dev; 90 91 #ifdef COMPAT_FREEBSD11 92 if (req->newlen == sizeof(ud_compat)) { 93 error = SYSCTL_IN(req, &ud_compat, sizeof(ud_compat)); 94 if (error == 0) 95 ud = ud_compat == (uint32_t)NODEV ? NODEV : ud_compat; 96 } else 97 #endif 98 error = SYSCTL_IN(req, &ud, sizeof (ud)); 99 if (error) 100 return (error); 101 if (ud == NODEV) 102 return (EINVAL); 103 dev = NULL; 104 dev_lock(); 105 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) 106 if (cdp->cdp_inode == ud) { 107 dev = &cdp->cdp_c; 108 dev_refl(dev); 109 break; 110 } 111 dev_unlock(); 112 if (dev == NULL) 113 return (ENOENT); 114 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 115 dev_rel(dev); 116 return (error); 117 } 118 119 SYSCTL_PROC(_kern, OID_AUTO, devname, 120 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE, 121 NULL, 0, sysctl_devname, "", "devname(3) handler"); 122 123 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD, 124 SYSCTL_NULL_INT_PTR, sizeof(struct cdev), "sizeof(struct cdev)"); 125 126 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD, 127 SYSCTL_NULL_INT_PTR, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)"); 128 129 struct cdev * 130 devfs_alloc(int flags) 131 { 132 struct cdev_priv *cdp; 133 struct cdev *cdev; 134 struct timespec ts; 135 136 cdp = malloc(sizeof *cdp, M_CDEVP, M_ZERO | 137 ((flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK)); 138 if (cdp == NULL) 139 return (NULL); 140 141 mtx_init(&cdp->cdp_threadlock, "devthrd", NULL, MTX_DEF); 142 143 cdp->cdp_dirents = &cdp->cdp_dirent0; 144 145 cdev = &cdp->cdp_c; 146 LIST_INIT(&cdev->si_children); 147 vfs_timestamp(&ts); 148 cdev->si_atime = cdev->si_mtime = cdev->si_ctime = ts; 149 150 return (cdev); 151 } 152 153 int 154 devfs_dev_exists(const char *name) 155 { 156 struct cdev_priv *cdp; 157 158 mtx_assert(&devmtx, MA_OWNED); 159 160 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 161 if ((cdp->cdp_flags & CDP_ACTIVE) == 0) 162 continue; 163 if (devfs_pathpath(cdp->cdp_c.si_name, name) != 0) 164 return (1); 165 if (devfs_pathpath(name, cdp->cdp_c.si_name) != 0) 166 return (1); 167 } 168 if (devfs_dir_find(name) != 0) 169 return (1); 170 171 return (0); 172 } 173 174 void 175 devfs_free(struct cdev *cdev) 176 { 177 struct cdev_priv *cdp; 178 179 cdp = cdev2priv(cdev); 180 if (cdev->si_cred != NULL) 181 crfree(cdev->si_cred); 182 devfs_free_cdp_inode(cdp->cdp_inode); 183 if (cdp->cdp_maxdirent > 0) 184 free(cdp->cdp_dirents, M_DEVFS2); 185 mtx_destroy(&cdp->cdp_threadlock); 186 free(cdp, M_CDEVP); 187 } 188 189 struct devfs_dirent * 190 devfs_find(struct devfs_dirent *dd, const char *name, int namelen, int type) 191 { 192 struct devfs_dirent *de; 193 194 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 195 if (namelen != de->de_dirent->d_namlen) 196 continue; 197 if (type != 0 && type != de->de_dirent->d_type) 198 continue; 199 200 /* 201 * The race with finding non-active name is not 202 * completely closed by the check, but it is similar 203 * to the devfs_allocv() in making it unlikely enough. 204 */ 205 if (de->de_dirent->d_type == DT_CHR && 206 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0) 207 continue; 208 209 if (bcmp(name, de->de_dirent->d_name, namelen) != 0) 210 continue; 211 break; 212 } 213 KASSERT(de == NULL || (de->de_flags & DE_DOOMED) == 0, 214 ("devfs_find: returning a doomed entry")); 215 return (de); 216 } 217 218 struct devfs_dirent * 219 devfs_newdirent(char *name, int namelen) 220 { 221 int i; 222 struct devfs_dirent *de; 223 struct dirent d; 224 225 d.d_namlen = namelen; 226 i = sizeof(*de) + GENERIC_DIRSIZ(&d); 227 de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO); 228 de->de_dirent = (struct dirent *)(de + 1); 229 de->de_dirent->d_namlen = namelen; 230 de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d); 231 bcopy(name, de->de_dirent->d_name, namelen); 232 dirent_terminate(de->de_dirent); 233 vfs_timestamp(&de->de_ctime); 234 de->de_mtime = de->de_atime = de->de_ctime; 235 de->de_links = 1; 236 de->de_holdcnt = 1; 237 #ifdef MAC 238 mac_devfs_init(de); 239 #endif 240 return (de); 241 } 242 243 struct devfs_dirent * 244 devfs_parent_dirent(struct devfs_dirent *de) 245 { 246 247 if (de->de_dirent->d_type != DT_DIR) 248 return (de->de_dir); 249 250 if (de->de_flags & (DE_DOT | DE_DOTDOT)) 251 return (NULL); 252 253 de = TAILQ_FIRST(&de->de_dlist); /* "." */ 254 if (de == NULL) 255 return (NULL); 256 de = TAILQ_NEXT(de, de_list); /* ".." */ 257 if (de == NULL) 258 return (NULL); 259 260 return (de->de_dir); 261 } 262 263 struct devfs_dirent * 264 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, 265 struct devfs_dirent *dotdot, u_int inode) 266 { 267 struct devfs_dirent *dd; 268 struct devfs_dirent *de; 269 270 /* Create the new directory */ 271 dd = devfs_newdirent(name, namelen); 272 TAILQ_INIT(&dd->de_dlist); 273 dd->de_dirent->d_type = DT_DIR; 274 dd->de_mode = 0555; 275 dd->de_links = 2; 276 dd->de_dir = dd; 277 if (inode != 0) 278 dd->de_inode = inode; 279 else 280 dd->de_inode = alloc_unr(devfs_inos); 281 282 /* 283 * "." and ".." are always the two first entries in the 284 * de_dlist list. 285 * 286 * Create the "." entry in the new directory. 287 */ 288 de = devfs_newdirent(".", 1); 289 de->de_dirent->d_type = DT_DIR; 290 de->de_flags |= DE_DOT; 291 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 292 de->de_dir = dd; 293 294 /* Create the ".." entry in the new directory. */ 295 de = devfs_newdirent("..", 2); 296 de->de_dirent->d_type = DT_DIR; 297 de->de_flags |= DE_DOTDOT; 298 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 299 if (dotdot == NULL) { 300 de->de_dir = dd; 301 } else { 302 de->de_dir = dotdot; 303 sx_assert(&dmp->dm_lock, SX_XLOCKED); 304 TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list); 305 dotdot->de_links++; 306 devfs_rules_apply(dmp, dd); 307 } 308 309 #ifdef MAC 310 mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd); 311 #endif 312 return (dd); 313 } 314 315 void 316 devfs_dirent_free(struct devfs_dirent *de) 317 { 318 struct vnode *vp; 319 320 vp = de->de_vnode; 321 mtx_lock(&devfs_de_interlock); 322 if (vp != NULL && vp->v_data == de) 323 vp->v_data = NULL; 324 mtx_unlock(&devfs_de_interlock); 325 free(de, M_DEVFS3); 326 } 327 328 /* 329 * Removes a directory if it is empty. Also empty parent directories are 330 * removed recursively. 331 */ 332 static void 333 devfs_rmdir_empty(struct devfs_mount *dm, struct devfs_dirent *de) 334 { 335 struct devfs_dirent *dd, *de_dot, *de_dotdot; 336 337 sx_assert(&dm->dm_lock, SX_XLOCKED); 338 339 for (;;) { 340 KASSERT(de->de_dirent->d_type == DT_DIR, 341 ("devfs_rmdir_empty: de is not a directory")); 342 343 if ((de->de_flags & DE_DOOMED) != 0 || de == dm->dm_rootdir) 344 return; 345 346 de_dot = TAILQ_FIRST(&de->de_dlist); 347 KASSERT(de_dot != NULL, ("devfs_rmdir_empty: . missing")); 348 de_dotdot = TAILQ_NEXT(de_dot, de_list); 349 KASSERT(de_dotdot != NULL, ("devfs_rmdir_empty: .. missing")); 350 /* Return if the directory is not empty. */ 351 if (TAILQ_NEXT(de_dotdot, de_list) != NULL) 352 return; 353 354 dd = devfs_parent_dirent(de); 355 KASSERT(dd != NULL, ("devfs_rmdir_empty: NULL dd")); 356 TAILQ_REMOVE(&de->de_dlist, de_dot, de_list); 357 TAILQ_REMOVE(&de->de_dlist, de_dotdot, de_list); 358 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 359 DEVFS_DE_HOLD(dd); 360 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 361 devfs_delete(dm, de_dot, DEVFS_DEL_NORECURSE); 362 devfs_delete(dm, de_dotdot, DEVFS_DEL_NORECURSE); 363 if (DEVFS_DE_DROP(dd)) { 364 devfs_dirent_free(dd); 365 return; 366 } 367 368 de = dd; 369 } 370 } 371 372 /* 373 * The caller needs to hold the dm for the duration of the call since 374 * dm->dm_lock may be temporary dropped. 375 */ 376 void 377 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int flags) 378 { 379 struct devfs_dirent *dd; 380 struct vnode *vp; 381 382 KASSERT((de->de_flags & DE_DOOMED) == 0, 383 ("devfs_delete doomed dirent")); 384 de->de_flags |= DE_DOOMED; 385 386 if ((flags & DEVFS_DEL_NORECURSE) == 0) { 387 dd = devfs_parent_dirent(de); 388 if (dd != NULL) 389 DEVFS_DE_HOLD(dd); 390 if (de->de_flags & DE_USER) { 391 KASSERT(dd != NULL, ("devfs_delete: NULL dd")); 392 devfs_dir_unref_de(dm, dd); 393 } 394 } else 395 dd = NULL; 396 397 mtx_lock(&devfs_de_interlock); 398 vp = de->de_vnode; 399 if (vp != NULL) { 400 VI_LOCK(vp); 401 mtx_unlock(&devfs_de_interlock); 402 vholdl(vp); 403 sx_unlock(&dm->dm_lock); 404 if ((flags & DEVFS_DEL_VNLOCKED) == 0) 405 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 406 else 407 VI_UNLOCK(vp); 408 vgone(vp); 409 if ((flags & DEVFS_DEL_VNLOCKED) == 0) 410 VOP_UNLOCK(vp); 411 vdrop(vp); 412 sx_xlock(&dm->dm_lock); 413 } else 414 mtx_unlock(&devfs_de_interlock); 415 if (de->de_symlink) { 416 free(de->de_symlink, M_DEVFS); 417 de->de_symlink = NULL; 418 } 419 #ifdef MAC 420 mac_devfs_destroy(de); 421 #endif 422 if (de->de_inode > DEVFS_ROOTINO) { 423 devfs_free_cdp_inode(de->de_inode); 424 de->de_inode = 0; 425 } 426 if (DEVFS_DE_DROP(de)) 427 devfs_dirent_free(de); 428 429 if (dd != NULL) { 430 if (DEVFS_DE_DROP(dd)) 431 devfs_dirent_free(dd); 432 else 433 devfs_rmdir_empty(dm, dd); 434 } 435 } 436 437 /* 438 * Called on unmount. 439 * Recursively removes the entire tree. 440 * The caller needs to hold the dm for the duration of the call. 441 */ 442 443 static void 444 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd) 445 { 446 struct devfs_dirent *de; 447 448 sx_assert(&dm->dm_lock, SX_XLOCKED); 449 450 DEVFS_DE_HOLD(dd); 451 for (;;) { 452 /* 453 * Use TAILQ_LAST() to remove "." and ".." last. 454 * We might need ".." to resolve a path in 455 * devfs_dir_unref_de(). 456 */ 457 de = TAILQ_LAST(&dd->de_dlist, devfs_dlist_head); 458 if (de == NULL) 459 break; 460 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 461 if (de->de_flags & DE_USER) 462 devfs_dir_unref_de(dm, dd); 463 if (de->de_flags & (DE_DOT | DE_DOTDOT)) 464 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 465 else if (de->de_dirent->d_type == DT_DIR) 466 devfs_purge(dm, de); 467 else 468 devfs_delete(dm, de, DEVFS_DEL_NORECURSE); 469 } 470 if (DEVFS_DE_DROP(dd)) 471 devfs_dirent_free(dd); 472 else if ((dd->de_flags & DE_DOOMED) == 0) 473 devfs_delete(dm, dd, DEVFS_DEL_NORECURSE); 474 } 475 476 /* 477 * Each cdev_priv has an array of pointers to devfs_dirent which is indexed 478 * by the mount points dm_idx. 479 * This function extends the array when necessary, taking into account that 480 * the default array is 1 element and not malloc'ed. 481 */ 482 static void 483 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm) 484 { 485 struct devfs_dirent **dep; 486 int siz; 487 488 siz = (dm->dm_idx + 1) * sizeof *dep; 489 dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO); 490 dev_lock(); 491 if (dm->dm_idx <= cdp->cdp_maxdirent) { 492 /* We got raced */ 493 dev_unlock(); 494 free(dep, M_DEVFS2); 495 return; 496 } 497 memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep); 498 if (cdp->cdp_maxdirent > 0) 499 free(cdp->cdp_dirents, M_DEVFS2); 500 cdp->cdp_dirents = dep; 501 /* 502 * XXX: if malloc told us how much we actually got this could 503 * XXX: be optimized. 504 */ 505 cdp->cdp_maxdirent = dm->dm_idx; 506 dev_unlock(); 507 } 508 509 /* 510 * The caller needs to hold the dm for the duration of the call. 511 */ 512 static int 513 devfs_populate_loop(struct devfs_mount *dm, int cleanup) 514 { 515 struct cdev_priv *cdp; 516 struct devfs_dirent *de; 517 struct devfs_dirent *dd, *dt; 518 struct cdev *pdev; 519 int de_flags, depth, j; 520 char *q, *s; 521 522 sx_assert(&dm->dm_lock, SX_XLOCKED); 523 dev_lock(); 524 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 525 526 KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents")); 527 528 /* 529 * If we are unmounting, or the device has been destroyed, 530 * clean up our dirent. 531 */ 532 if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) && 533 dm->dm_idx <= cdp->cdp_maxdirent && 534 cdp->cdp_dirents[dm->dm_idx] != NULL) { 535 de = cdp->cdp_dirents[dm->dm_idx]; 536 cdp->cdp_dirents[dm->dm_idx] = NULL; 537 KASSERT(cdp == de->de_cdp, 538 ("%s %d %s %p %p", __func__, __LINE__, 539 cdp->cdp_c.si_name, cdp, de->de_cdp)); 540 KASSERT(de->de_dir != NULL, ("Null de->de_dir")); 541 dev_unlock(); 542 543 TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list); 544 de->de_cdp = NULL; 545 de->de_inode = 0; 546 devfs_delete(dm, de, 0); 547 dev_lock(); 548 cdp->cdp_inuse--; 549 dev_unlock(); 550 return (1); 551 } 552 /* 553 * GC any lingering devices 554 */ 555 if (!(cdp->cdp_flags & CDP_ACTIVE)) { 556 if (cdp->cdp_inuse > 0) 557 continue; 558 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list); 559 dev_unlock(); 560 dev_rel(&cdp->cdp_c); 561 return (1); 562 } 563 /* 564 * Don't create any new dirents if we are unmounting 565 */ 566 if (cleanup) 567 continue; 568 KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!")); 569 570 if (dm->dm_idx <= cdp->cdp_maxdirent && 571 cdp->cdp_dirents[dm->dm_idx] != NULL) { 572 de = cdp->cdp_dirents[dm->dm_idx]; 573 KASSERT(cdp == de->de_cdp, ("inconsistent cdp")); 574 continue; 575 } 576 577 578 cdp->cdp_inuse++; 579 dev_unlock(); 580 581 if (dm->dm_idx > cdp->cdp_maxdirent) 582 devfs_metoo(cdp, dm); 583 584 dd = dm->dm_rootdir; 585 s = cdp->cdp_c.si_name; 586 for (;;) { 587 for (q = s; *q != '/' && *q != '\0'; q++) 588 continue; 589 if (*q != '/') 590 break; 591 de = devfs_find(dd, s, q - s, 0); 592 if (de == NULL) 593 de = devfs_vmkdir(dm, s, q - s, dd, 0); 594 else if (de->de_dirent->d_type == DT_LNK) { 595 de = devfs_find(dd, s, q - s, DT_DIR); 596 if (de == NULL) 597 de = devfs_vmkdir(dm, s, q - s, dd, 0); 598 de->de_flags |= DE_COVERED; 599 } 600 s = q + 1; 601 dd = de; 602 KASSERT(dd->de_dirent->d_type == DT_DIR && 603 (dd->de_flags & (DE_DOT | DE_DOTDOT)) == 0, 604 ("%s: invalid directory (si_name=%s)", 605 __func__, cdp->cdp_c.si_name)); 606 607 } 608 de_flags = 0; 609 de = devfs_find(dd, s, q - s, DT_LNK); 610 if (de != NULL) 611 de_flags |= DE_COVERED; 612 613 de = devfs_newdirent(s, q - s); 614 if (cdp->cdp_c.si_flags & SI_ALIAS) { 615 de->de_uid = 0; 616 de->de_gid = 0; 617 de->de_mode = 0755; 618 de->de_dirent->d_type = DT_LNK; 619 pdev = cdp->cdp_c.si_parent; 620 dt = dd; 621 depth = 0; 622 while (dt != dm->dm_rootdir && 623 (dt = devfs_parent_dirent(dt)) != NULL) 624 depth++; 625 j = depth * 3 + strlen(pdev->si_name) + 1; 626 de->de_symlink = malloc(j, M_DEVFS, M_WAITOK); 627 de->de_symlink[0] = 0; 628 while (depth-- > 0) 629 strcat(de->de_symlink, "../"); 630 strcat(de->de_symlink, pdev->si_name); 631 } else { 632 de->de_uid = cdp->cdp_c.si_uid; 633 de->de_gid = cdp->cdp_c.si_gid; 634 de->de_mode = cdp->cdp_c.si_mode; 635 de->de_dirent->d_type = DT_CHR; 636 } 637 de->de_flags |= de_flags; 638 de->de_inode = cdp->cdp_inode; 639 de->de_cdp = cdp; 640 #ifdef MAC 641 mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount, 642 &cdp->cdp_c, de); 643 #endif 644 de->de_dir = dd; 645 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 646 devfs_rules_apply(dm, de); 647 dev_lock(); 648 /* XXX: could check that cdp is still active here */ 649 KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL, 650 ("%s %d\n", __func__, __LINE__)); 651 cdp->cdp_dirents[dm->dm_idx] = de; 652 KASSERT(de->de_cdp != (void *)0xdeadc0de, 653 ("%s %d\n", __func__, __LINE__)); 654 dev_unlock(); 655 return (1); 656 } 657 dev_unlock(); 658 return (0); 659 } 660 661 /* 662 * The caller needs to hold the dm for the duration of the call. 663 */ 664 void 665 devfs_populate(struct devfs_mount *dm) 666 { 667 unsigned gen; 668 669 sx_assert(&dm->dm_lock, SX_XLOCKED); 670 gen = devfs_generation; 671 if (dm->dm_generation == gen) 672 return; 673 while (devfs_populate_loop(dm, 0)) 674 continue; 675 dm->dm_generation = gen; 676 } 677 678 /* 679 * The caller needs to hold the dm for the duration of the call. 680 */ 681 void 682 devfs_cleanup(struct devfs_mount *dm) 683 { 684 685 sx_assert(&dm->dm_lock, SX_XLOCKED); 686 while (devfs_populate_loop(dm, 1)) 687 continue; 688 devfs_purge(dm, dm->dm_rootdir); 689 } 690 691 /* 692 * devfs_create() and devfs_destroy() are called from kern_conf.c and 693 * in both cases the devlock() mutex is held, so no further locking 694 * is necessary and no sleeping allowed. 695 */ 696 697 void 698 devfs_create(struct cdev *dev) 699 { 700 struct cdev_priv *cdp; 701 702 mtx_assert(&devmtx, MA_OWNED); 703 cdp = cdev2priv(dev); 704 cdp->cdp_flags |= CDP_ACTIVE; 705 cdp->cdp_inode = alloc_unrl(devfs_inos); 706 dev_refl(dev); 707 TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list); 708 devfs_generation++; 709 } 710 711 void 712 devfs_destroy(struct cdev *dev) 713 { 714 struct cdev_priv *cdp; 715 716 mtx_assert(&devmtx, MA_OWNED); 717 cdp = cdev2priv(dev); 718 cdp->cdp_flags &= ~CDP_ACTIVE; 719 devfs_generation++; 720 } 721 722 ino_t 723 devfs_alloc_cdp_inode(void) 724 { 725 726 return (alloc_unr(devfs_inos)); 727 } 728 729 void 730 devfs_free_cdp_inode(ino_t ino) 731 { 732 733 if (ino > 0) 734 free_unr(devfs_inos, ino); 735 } 736 737 static void 738 devfs_devs_init(void *junk __unused) 739 { 740 741 devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx); 742 } 743 744 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL); 745