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