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