1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 9 * Support code is derived from software contributed to Berkeley 10 * by Atsushi Murai (amurai@spec.co.jp). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/namei.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/kernel.h> 48 #include <sys/vnode.h> 49 #include <sys/mount.h> 50 #include <sys/bio.h> 51 #include <sys/buf.h> 52 #include <sys/cdio.h> 53 #include <sys/conf.h> 54 #include <sys/fcntl.h> 55 #include <sys/malloc.h> 56 #include <sys/stat.h> 57 #include <sys/syslog.h> 58 #include <sys/iconv.h> 59 60 #include <fs/cd9660/iso.h> 61 #include <fs/cd9660/iso_rrip.h> 62 #include <fs/cd9660/cd9660_node.h> 63 #include <fs/cd9660/cd9660_mount.h> 64 65 #include <geom/geom.h> 66 #include <geom/geom_vfs.h> 67 68 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure"); 69 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part"); 70 71 struct iconv_functions *cd9660_iconv = NULL; 72 73 static vfs_mount_t cd9660_mount; 74 static vfs_cmount_t cd9660_cmount; 75 static vfs_unmount_t cd9660_unmount; 76 static vfs_root_t cd9660_root; 77 static vfs_statfs_t cd9660_statfs; 78 static vfs_vget_t cd9660_vget; 79 static vfs_fhtovp_t cd9660_fhtovp; 80 81 static struct vfsops cd9660_vfsops = { 82 .vfs_fhtovp = cd9660_fhtovp, 83 .vfs_mount = cd9660_mount, 84 .vfs_cmount = cd9660_cmount, 85 .vfs_root = cd9660_root, 86 .vfs_statfs = cd9660_statfs, 87 .vfs_unmount = cd9660_unmount, 88 .vfs_vget = cd9660_vget, 89 }; 90 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY); 91 MODULE_VERSION(cd9660, 1); 92 93 static int cd9660_vfs_hash_cmp(struct vnode *vp, void *pino); 94 static int iso_mountfs(struct vnode *devvp, struct mount *mp); 95 96 /* 97 * VFS Operations. 98 */ 99 100 static int 101 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags) 102 { 103 struct iso_args args; 104 int error; 105 106 error = copyin(data, &args, sizeof args); 107 if (error) 108 return (error); 109 110 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 111 ma = mount_arg(ma, "export", &args.export, sizeof(args.export)); 112 ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64); 113 ma = mount_argsu(ma, "cs_local", args.cs_local, 64); 114 ma = mount_argf(ma, "ssector", "%u", args.ssector); 115 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip"); 116 ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens"); 117 ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt"); 118 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet"); 119 ma = mount_argb(ma, 120 args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet"); 121 ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv"); 122 123 error = kernel_mount(ma, flags); 124 125 return (error); 126 } 127 128 static int 129 cd9660_mount(struct mount *mp) 130 { 131 struct vnode *devvp; 132 struct thread *td; 133 char *fspec; 134 int error; 135 accmode_t accmode; 136 struct nameidata ndp; 137 struct iso_mnt *imp = NULL; 138 139 td = curthread; 140 141 /* 142 * Unconditionally mount as read-only. 143 */ 144 MNT_ILOCK(mp); 145 mp->mnt_flag |= MNT_RDONLY; 146 MNT_IUNLOCK(mp); 147 148 fspec = vfs_getopts(mp->mnt_optnew, "from", &error); 149 if (error) 150 return (error); 151 152 imp = VFSTOISOFS(mp); 153 154 if (mp->mnt_flag & MNT_UPDATE) { 155 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) 156 return (0); 157 } 158 /* 159 * Not an update, or updating the name: look up the name 160 * and verify that it refers to a sensible block device. 161 */ 162 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec); 163 if ((error = namei(&ndp))) 164 return (error); 165 NDFREE_PNBUF(&ndp); 166 devvp = ndp.ni_vp; 167 168 if (!vn_isdisk_error(devvp, &error)) { 169 vput(devvp); 170 return (error); 171 } 172 173 /* 174 * Verify that user has necessary permissions on the device, 175 * or has superuser abilities 176 */ 177 accmode = VREAD; 178 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td); 179 if (error) 180 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 181 if (error) { 182 vput(devvp); 183 return (error); 184 } 185 186 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 187 error = iso_mountfs(devvp, mp); 188 if (error) 189 vrele(devvp); 190 } else { 191 if (devvp != imp->im_devvp) 192 error = EINVAL; /* needs translation */ 193 vput(devvp); 194 } 195 if (error) 196 return (error); 197 vfs_mountedfrom(mp, fspec); 198 return (0); 199 } 200 201 /* 202 * Common code for mount and mountroot 203 */ 204 static int 205 iso_mountfs(struct vnode *devvp, struct mount *mp) 206 { 207 struct iso_mnt *isomp = NULL; 208 struct buf *bp = NULL; 209 struct buf *pribp = NULL, *supbp = NULL; 210 struct cdev *dev; 211 int error = EINVAL; 212 int high_sierra = 0; 213 int iso_bsize; 214 int iso_blknum; 215 int joliet_level; 216 int isverified = 0; 217 struct iso_volume_descriptor *vdp = NULL; 218 struct iso_primary_descriptor *pri = NULL; 219 struct iso_sierra_primary_descriptor *pri_sierra = NULL; 220 struct iso_supplementary_descriptor *sup = NULL; 221 struct iso_directory_record *rootp; 222 int logical_block_size, ssector; 223 struct g_consumer *cp; 224 struct bufobj *bo; 225 char *cs_local, *cs_disk; 226 227 dev = devvp->v_rdev; 228 dev_ref(dev); 229 g_topology_lock(); 230 error = g_vfs_open(devvp, &cp, "cd9660", 0); 231 if (error == 0) 232 g_getattr("MNT::verified", cp, &isverified); 233 g_topology_unlock(); 234 VOP_UNLOCK(devvp); 235 if (error) 236 goto out; 237 if (devvp->v_rdev->si_iosize_max != 0) 238 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 239 if (mp->mnt_iosize_max > maxphys) 240 mp->mnt_iosize_max = maxphys; 241 242 bo = &devvp->v_bufobj; 243 244 /* This is the "logical sector size". The standard says this 245 * should be 2048 or the physical sector size on the device, 246 * whichever is greater. 247 */ 248 if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) { 249 error = EINVAL; 250 goto out; 251 } 252 253 iso_bsize = cp->provider->sectorsize; 254 255 joliet_level = 0; 256 if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector)) 257 ssector = 0; 258 for (iso_blknum = 16 + ssector; 259 iso_blknum < 100 + ssector; 260 iso_blknum++) { 261 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE), 262 iso_bsize, NOCRED, &bp)) != 0) 263 goto out; 264 265 vdp = (struct iso_volume_descriptor *)bp->b_data; 266 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) { 267 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID, 268 sizeof vdp->id_sierra) != 0) { 269 error = EINVAL; 270 goto out; 271 } else 272 high_sierra = 1; 273 } 274 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){ 275 case ISO_VD_PRIMARY: 276 if (pribp == NULL) { 277 pribp = bp; 278 bp = NULL; 279 pri = (struct iso_primary_descriptor *)vdp; 280 pri_sierra = 281 (struct iso_sierra_primary_descriptor *)vdp; 282 } 283 break; 284 285 case ISO_VD_SUPPLEMENTARY: 286 if (supbp == NULL) { 287 supbp = bp; 288 bp = NULL; 289 sup = (struct iso_supplementary_descriptor *)vdp; 290 291 if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) { 292 if (bcmp(sup->escape, "%/@", 3) == 0) 293 joliet_level = 1; 294 if (bcmp(sup->escape, "%/C", 3) == 0) 295 joliet_level = 2; 296 if (bcmp(sup->escape, "%/E", 3) == 0) 297 joliet_level = 3; 298 299 if ((isonum_711 (sup->flags) & 1) && 300 !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0)) 301 joliet_level = 0; 302 } 303 } 304 break; 305 306 case ISO_VD_END: 307 goto vd_end; 308 309 default: 310 break; 311 } 312 if (bp != NULL) { 313 brelse(bp); 314 bp = NULL; 315 } 316 } 317 vd_end: 318 if (bp != NULL) { 319 brelse(bp); 320 bp = NULL; 321 } 322 323 if (pri == NULL) { 324 error = EINVAL; 325 goto out; 326 } 327 328 logical_block_size = 329 isonum_723 (high_sierra? 330 pri_sierra->logical_block_size: 331 pri->logical_block_size); 332 333 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE 334 || (logical_block_size & (logical_block_size - 1)) != 0) { 335 error = EINVAL; 336 goto out; 337 } 338 339 if (logical_block_size < cp->provider->sectorsize) { 340 printf("cd9660: Unsupported logical block size %u\n", 341 logical_block_size); 342 error = EINVAL; 343 goto out; 344 } 345 346 rootp = (struct iso_directory_record *) 347 (high_sierra? 348 pri_sierra->root_directory_record: 349 pri->root_directory_record); 350 351 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO); 352 isomp->im_cp = cp; 353 isomp->im_bo = bo; 354 isomp->logical_block_size = logical_block_size; 355 isomp->volume_space_size = 356 isonum_733 (high_sierra? 357 pri_sierra->volume_space_size: 358 pri->volume_space_size); 359 isomp->joliet_level = 0; 360 /* 361 * Since an ISO9660 multi-session CD can also access previous 362 * sessions, we have to include them into the space consider- 363 * ations. This doesn't yield a very accurate number since 364 * parts of the old sessions might be inaccessible now, but we 365 * can't do much better. This is also important for the NFS 366 * filehandle validation. 367 */ 368 isomp->volume_space_size += ssector; 369 memcpy(isomp->root, rootp, sizeof isomp->root); 370 isomp->root_extent = isonum_733 (rootp->extent); 371 isomp->root_size = isonum_733 (rootp->size); 372 373 isomp->im_bmask = logical_block_size - 1; 374 isomp->im_bshift = ffs(logical_block_size) - 1; 375 376 pribp->b_flags |= B_AGE; 377 brelse(pribp); 378 pribp = NULL; 379 rootp = NULL; 380 pri = NULL; 381 pri_sierra = NULL; 382 383 mp->mnt_data = isomp; 384 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 385 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 386 MNT_ILOCK(mp); 387 if (isverified) 388 mp->mnt_flag |= MNT_VERIFIED; 389 mp->mnt_flag |= MNT_LOCAL; 390 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED; 391 MNT_IUNLOCK(mp); 392 isomp->im_mountp = mp; 393 isomp->im_dev = dev; 394 isomp->im_devvp = devvp; 395 396 vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP); 397 vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS); 398 vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT); 399 vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET); 400 vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV); 401 402 /* Check the Rock Ridge Extension support */ 403 if (!(isomp->im_flags & ISOFSMNT_NORRIP)) { 404 if ((error = bread(isomp->im_devvp, (isomp->root_extent + 405 isonum_711(((struct iso_directory_record *)isomp->root)-> 406 ext_attr_length)) << (isomp->im_bshift - DEV_BSHIFT), 407 isomp->logical_block_size, NOCRED, &bp)) != 0) 408 goto out; 409 410 rootp = (struct iso_directory_record *)bp->b_data; 411 412 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) { 413 isomp->im_flags |= ISOFSMNT_NORRIP; 414 } else { 415 isomp->im_flags &= ~ISOFSMNT_GENS; 416 } 417 418 /* 419 * The contents are valid, 420 * but they will get reread as part of another vnode, so... 421 */ 422 bp->b_flags |= B_AGE; 423 brelse(bp); 424 bp = NULL; 425 rootp = NULL; 426 } 427 428 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) { 429 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error); 430 if (error) 431 goto out; 432 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error); 433 if (error) 434 goto out; 435 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l); 436 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d); 437 } else { 438 isomp->im_d2l = NULL; 439 isomp->im_l2d = NULL; 440 } 441 442 if (high_sierra) { 443 /* this effectively ignores all the mount flags */ 444 if (bootverbose) 445 log(LOG_INFO, "cd9660: High Sierra Format\n"); 446 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA; 447 } else 448 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) { 449 default: 450 isomp->iso_ftype = ISO_FTYPE_DEFAULT; 451 break; 452 case ISOFSMNT_GENS|ISOFSMNT_NORRIP: 453 isomp->iso_ftype = ISO_FTYPE_9660; 454 break; 455 case 0: 456 if (bootverbose) 457 log(LOG_INFO, "cd9660: RockRidge Extension\n"); 458 isomp->iso_ftype = ISO_FTYPE_RRIP; 459 break; 460 } 461 462 /* Decide whether to use the Joliet descriptor */ 463 464 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) { 465 if (bootverbose) 466 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", 467 joliet_level); 468 rootp = (struct iso_directory_record *) 469 sup->root_directory_record; 470 memcpy(isomp->root, rootp, sizeof isomp->root); 471 isomp->root_extent = isonum_733 (rootp->extent); 472 isomp->root_size = isonum_733 (rootp->size); 473 isomp->joliet_level = joliet_level; 474 supbp->b_flags |= B_AGE; 475 } 476 477 if (supbp) { 478 brelse(supbp); 479 supbp = NULL; 480 sup = NULL; 481 } 482 483 return 0; 484 out: 485 if (bp != NULL) 486 brelse(bp); 487 if (pribp != NULL) 488 brelse(pribp); 489 if (supbp != NULL) 490 brelse(supbp); 491 if (cp != NULL) { 492 g_topology_lock(); 493 g_vfs_close(cp); 494 g_topology_unlock(); 495 } 496 if (isomp) { 497 free(isomp, M_ISOFSMNT); 498 mp->mnt_data = NULL; 499 } 500 dev_rel(dev); 501 return error; 502 } 503 504 /* 505 * unmount system call 506 */ 507 static int 508 cd9660_unmount(struct mount *mp, int mntflags) 509 { 510 struct iso_mnt *isomp; 511 int error, flags = 0; 512 513 if (mntflags & MNT_FORCE) 514 flags |= FORCECLOSE; 515 if ((error = vflush(mp, 0, flags, curthread))) 516 return (error); 517 518 isomp = VFSTOISOFS(mp); 519 520 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) { 521 if (isomp->im_d2l) 522 cd9660_iconv->close(isomp->im_d2l); 523 if (isomp->im_l2d) 524 cd9660_iconv->close(isomp->im_l2d); 525 } 526 g_topology_lock(); 527 g_vfs_close(isomp->im_cp); 528 g_topology_unlock(); 529 vrele(isomp->im_devvp); 530 dev_rel(isomp->im_dev); 531 free(isomp, M_ISOFSMNT); 532 mp->mnt_data = NULL; 533 return (error); 534 } 535 536 /* 537 * Return root of a filesystem 538 */ 539 static int 540 cd9660_root(struct mount *mp, int flags, struct vnode **vpp) 541 { 542 struct iso_mnt *imp = VFSTOISOFS(mp); 543 struct iso_directory_record *dp = 544 (struct iso_directory_record *)imp->root; 545 cd_ino_t ino = isodirino(dp, imp); 546 547 /* 548 * With RRIP we must use the `.' entry of the root directory. 549 * Simply tell vget, that it's a relocated directory. 550 */ 551 return (cd9660_vget_internal(mp, ino, flags, vpp, 552 imp->iso_ftype == ISO_FTYPE_RRIP, dp)); 553 } 554 555 /* 556 * Get filesystem statistics. 557 */ 558 static int 559 cd9660_statfs(struct mount *mp, struct statfs *sbp) 560 { 561 struct iso_mnt *isomp; 562 563 isomp = VFSTOISOFS(mp); 564 565 sbp->f_bsize = isomp->logical_block_size; 566 sbp->f_iosize = sbp->f_bsize; /* XXX */ 567 sbp->f_blocks = isomp->volume_space_size; 568 sbp->f_bfree = 0; /* total free blocks */ 569 sbp->f_bavail = 0; /* blocks free for non superuser */ 570 sbp->f_files = 0; /* total files */ 571 sbp->f_ffree = 0; /* free file nodes */ 572 return 0; 573 } 574 575 /* 576 * File handle to vnode 577 * 578 * Have to be really careful about stale file handles: 579 * - check that the inode number is in range 580 * - call iget() to get the locked inode 581 * - check for an unallocated inode (i_mode == 0) 582 * - check that the generation number matches 583 */ 584 585 /* ARGSUSED */ 586 static int 587 cd9660_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp) 588 { 589 struct ifid ifh; 590 struct iso_node *ip; 591 struct vnode *nvp; 592 int error; 593 594 memcpy(&ifh, fhp, sizeof(ifh)); 595 596 #ifdef ISOFS_DBG 597 printf("fhtovp: ino %d, start %ld\n", 598 ifh.ifid_ino, ifh.ifid_start); 599 #endif 600 601 if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) { 602 *vpp = NULLVP; 603 return (error); 604 } 605 ip = VTOI(nvp); 606 if (ip->inode.iso_mode == 0) { 607 vput(nvp); 608 *vpp = NULLVP; 609 return (ESTALE); 610 } 611 *vpp = nvp; 612 vnode_create_vobject(*vpp, ip->i_size, curthread); 613 return (0); 614 } 615 616 /* 617 * Conform to standard VFS interface; can't vget arbitrary inodes beyond 4GB 618 * into media with current inode scheme and 32-bit ino_t. This shouldn't be 619 * needed for anything other than nfsd, and who exports a mounted DVD over NFS? 620 */ 621 static int 622 cd9660_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 623 { 624 625 /* 626 * XXXX 627 * It would be nice if we didn't always set the `relocated' flag 628 * and force the extra read, but I don't want to think about fixing 629 * that right now. 630 */ 631 return (cd9660_vget_internal(mp, ino, flags, vpp, 632 #if 0 633 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP, 634 #else 635 0, 636 #endif 637 (struct iso_directory_record *)0)); 638 } 639 640 /* Use special comparator for full 64-bit ino comparison. */ 641 static int 642 cd9660_vfs_hash_cmp(struct vnode *vp, void *pino) 643 { 644 struct iso_node *ip; 645 cd_ino_t ino; 646 647 ip = VTOI(vp); 648 ino = *(cd_ino_t *)pino; 649 return (ip->i_number != ino); 650 } 651 652 int 653 cd9660_vget_internal(struct mount *mp, cd_ino_t ino, int flags, 654 struct vnode **vpp, int relocated, struct iso_directory_record *isodir) 655 { 656 struct iso_mnt *imp; 657 struct iso_node *ip; 658 struct buf *bp; 659 struct vnode *vp; 660 int error; 661 struct thread *td; 662 663 td = curthread; 664 error = vfs_hash_get(mp, ino, flags, td, vpp, cd9660_vfs_hash_cmp, 665 &ino); 666 if (error || *vpp != NULL) 667 return (error); 668 669 /* 670 * We must promote to an exclusive lock for vnode creation. This 671 * can happen if lookup is passed LOCKSHARED. 672 */ 673 if ((flags & LK_TYPE_MASK) == LK_SHARED) { 674 flags &= ~LK_TYPE_MASK; 675 flags |= LK_EXCLUSIVE; 676 } 677 678 /* 679 * We do not lock vnode creation as it is believed to be too 680 * expensive for such rare case as simultaneous creation of vnode 681 * for same ino by different processes. We just allow them to race 682 * and check later to decide who wins. Let the race begin! 683 */ 684 685 imp = VFSTOISOFS(mp); 686 687 /* Allocate a new vnode/iso_node. */ 688 if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) { 689 *vpp = NULLVP; 690 return (error); 691 } 692 ip = malloc(sizeof(struct iso_node), M_ISOFSNODE, 693 M_WAITOK | M_ZERO); 694 vp->v_data = ip; 695 ip->i_vnode = vp; 696 ip->i_number = ino; 697 698 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL); 699 error = insmntque(vp, mp); 700 if (error != 0) { 701 free(ip, M_ISOFSNODE); 702 *vpp = NULLVP; 703 return (error); 704 } 705 error = vfs_hash_insert(vp, ino, flags, td, vpp, cd9660_vfs_hash_cmp, 706 &ino); 707 if (error || *vpp != NULL) 708 return (error); 709 710 if (isodir == NULL) { 711 int lbn, off; 712 713 lbn = lblkno(imp, ino); 714 if (lbn >= imp->volume_space_size) { 715 vput(vp); 716 printf("fhtovp: lbn exceed volume space %d\n", lbn); 717 return (ESTALE); 718 } 719 720 off = blkoff(imp, ino); 721 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) { 722 vput(vp); 723 printf("fhtovp: crosses block boundary %d\n", 724 off + ISO_DIRECTORY_RECORD_SIZE); 725 return (ESTALE); 726 } 727 728 error = bread(imp->im_devvp, 729 lbn << (imp->im_bshift - DEV_BSHIFT), 730 imp->logical_block_size, NOCRED, &bp); 731 if (error) { 732 vput(vp); 733 printf("fhtovp: bread error %d\n",error); 734 return (error); 735 } 736 isodir = (struct iso_directory_record *)(bp->b_data + off); 737 738 if (off + isonum_711(isodir->length) > 739 imp->logical_block_size) { 740 vput(vp); 741 brelse(bp); 742 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n", 743 off +isonum_711(isodir->length), off, 744 isonum_711(isodir->length)); 745 return (ESTALE); 746 } 747 748 #if 0 749 if (isonum_733(isodir->extent) + 750 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) { 751 brelse(bp); 752 printf("fhtovp: file start miss %d vs %d\n", 753 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length), 754 ifhp->ifid_start); 755 return (ESTALE); 756 } 757 #endif 758 } else 759 bp = NULL; 760 761 ip->i_mnt = imp; 762 763 if (relocated) { 764 /* 765 * On relocated directories we must 766 * read the `.' entry out of a dir. 767 */ 768 ip->iso_start = ino >> imp->im_bshift; 769 if (bp != NULL) 770 brelse(bp); 771 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) { 772 vput(vp); 773 return (error); 774 } 775 isodir = (struct iso_directory_record *)bp->b_data; 776 } 777 778 ip->iso_extent = isonum_733(isodir->extent); 779 ip->i_size = isonum_733(isodir->size); 780 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent; 781 782 /* 783 * Setup time stamp, attribute 784 */ 785 vp->v_type = VNON; 786 switch (imp->iso_ftype) { 787 default: /* ISO_FTYPE_9660 */ 788 { 789 struct buf *bp2; 790 int off; 791 if ((imp->im_flags & ISOFSMNT_EXTATT) 792 && (off = isonum_711(isodir->ext_attr_length))) 793 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL, 794 &bp2); 795 else 796 bp2 = NULL; 797 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660); 798 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660); 799 if (bp2) 800 brelse(bp2); 801 break; 802 } 803 case ISO_FTYPE_RRIP: 804 cd9660_rrip_analyze(isodir, ip, imp); 805 break; 806 } 807 808 brelse(bp); 809 810 /* 811 * Initialize the associated vnode 812 */ 813 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) { 814 case VFIFO: 815 vp->v_op = &cd9660_fifoops; 816 break; 817 default: 818 VN_LOCK_ASHARE(vp); 819 break; 820 } 821 822 if (ip->iso_extent == imp->root_extent) 823 vp->v_vflag |= VV_ROOT; 824 825 /* 826 * XXX need generation number? 827 */ 828 829 vn_set_state(vp, VSTATE_CONSTRUCTED); 830 *vpp = vp; 831 return (0); 832 } 833