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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95 39 * $FreeBSD$ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/namei.h> 45 #include <sys/proc.h> 46 #include <sys/kernel.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/cdio.h> 52 #include <sys/conf.h> 53 #include <sys/fcntl.h> 54 #include <sys/malloc.h> 55 #include <sys/stat.h> 56 #include <sys/syslog.h> 57 58 59 #include <isofs/cd9660/iso.h> 60 #include <isofs/cd9660/iso_rrip.h> 61 #include <isofs/cd9660/cd9660_node.h> 62 #include <isofs/cd9660/cd9660_mount.h> 63 64 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure"); 65 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part"); 66 67 static int cd9660_mount __P((struct mount *, 68 char *, caddr_t, struct nameidata *, struct proc *)); 69 static int cd9660_unmount __P((struct mount *, int, struct proc *)); 70 static int cd9660_root __P((struct mount *, struct vnode **)); 71 static int cd9660_statfs __P((struct mount *, struct statfs *, struct proc *)); 72 static int cd9660_vget __P((struct mount *, ino_t, struct vnode **)); 73 static int cd9660_fhtovp __P((struct mount *, struct fid *, struct vnode **)); 74 static int cd9660_vptofh __P((struct vnode *, struct fid *)); 75 76 static struct vfsops cd9660_vfsops = { 77 cd9660_mount, 78 vfs_stdstart, 79 cd9660_unmount, 80 cd9660_root, 81 vfs_stdquotactl, 82 cd9660_statfs, 83 vfs_stdsync, 84 cd9660_vget, 85 cd9660_fhtovp, 86 vfs_stdcheckexp, 87 cd9660_vptofh, 88 cd9660_init, 89 cd9660_uninit, 90 vfs_stdextattrctl, 91 }; 92 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY); 93 MODULE_VERSION(cd9660, 1); 94 95 96 /* 97 * Called by vfs_mountroot when iso is going to be mounted as root. 98 */ 99 100 static int iso_get_ssector __P((dev_t dev, struct proc *p)); 101 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp, 102 struct proc *p, struct iso_args *argp)); 103 104 /* 105 * Try to find the start of the last data track on this CD-ROM. This 106 * is used to mount the last session of a multi-session CD. Bail out 107 * and return 0 if we fail, this is always a safe bet. 108 */ 109 static int 110 iso_get_ssector(dev, p) 111 dev_t dev; 112 struct proc *p; 113 { 114 struct ioc_toc_header h; 115 struct ioc_read_toc_single_entry t; 116 int i; 117 struct cdevsw *bd; 118 d_ioctl_t *ioctlp; 119 120 bd = devsw(dev); 121 ioctlp = bd->d_ioctl; 122 if (ioctlp == NULL) 123 return 0; 124 125 if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, p) != 0) 126 return 0; 127 128 for (i = h.ending_track; i >= 0; i--) { 129 t.address_format = CD_LBA_FORMAT; 130 t.track = i; 131 if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, p) != 0) 132 return 0; 133 if ((t.entry.control & 4) != 0) 134 /* found a data track */ 135 break; 136 } 137 138 if (i < 0) 139 return 0; 140 141 return ntohl(t.entry.addr.lba); 142 } 143 144 static int iso_mountroot __P((struct mount *mp, struct proc *p)); 145 146 static int 147 iso_mountroot(mp, p) 148 struct mount *mp; 149 struct proc *p; 150 { 151 struct iso_args args; 152 int error; 153 154 if ((error = bdevvp(rootdev, &rootvp))) { 155 printf("iso_mountroot: can't find rootvp\n"); 156 return (error); 157 } 158 args.flags = ISOFSMNT_ROOT; 159 args.ssector = iso_get_ssector(rootdev, p); 160 if (bootverbose) 161 printf("iso_mountroot(): using session at block %d\n", 162 args.ssector); 163 if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0) 164 return (error); 165 166 (void)cd9660_statfs(mp, &mp->mnt_stat, p); 167 return (0); 168 } 169 170 /* 171 * VFS Operations. 172 * 173 * mount system call 174 */ 175 static int 176 cd9660_mount(mp, path, data, ndp, p) 177 register struct mount *mp; 178 char *path; 179 caddr_t data; 180 struct nameidata *ndp; 181 struct proc *p; 182 { 183 struct vnode *devvp; 184 struct iso_args args; 185 size_t size; 186 int error; 187 mode_t accessmode; 188 struct iso_mnt *imp = 0; 189 190 if ((mp->mnt_flag & MNT_ROOTFS) != 0) { 191 return (iso_mountroot(mp, p)); 192 } 193 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args)))) 194 return (error); 195 196 if ((mp->mnt_flag & MNT_RDONLY) == 0) 197 return (EROFS); 198 199 /* 200 * If updating, check whether changing from read-only to 201 * read/write; if there is no device name, that's all we do. 202 */ 203 if (mp->mnt_flag & MNT_UPDATE) { 204 imp = VFSTOISOFS(mp); 205 if (args.fspec == 0) 206 return (vfs_export(mp, &args.export)); 207 } 208 /* 209 * Not an update, or updating the name: look up the name 210 * and verify that it refers to a sensible block device. 211 */ 212 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 213 if ((error = namei(ndp))) 214 return (error); 215 NDFREE(ndp, NDF_ONLY_PNBUF); 216 devvp = ndp->ni_vp; 217 218 if (!vn_isdisk(devvp, &error)) { 219 vrele(devvp); 220 return (error); 221 } 222 223 /* 224 * Verify that user has necessary permissions on the device, 225 * or has superuser abilities 226 */ 227 accessmode = VREAD; 228 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 229 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p); 230 if (error) 231 error = suser(p); 232 if (error) { 233 vput(devvp); 234 return (error); 235 } 236 VOP_UNLOCK(devvp, 0, p); 237 238 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 239 error = iso_mountfs(devvp, mp, p, &args); 240 } else { 241 if (devvp != imp->im_devvp) 242 error = EINVAL; /* needs translation */ 243 else 244 vrele(devvp); 245 } 246 if (error) { 247 vrele(devvp); 248 return error; 249 } 250 imp = VFSTOISOFS(mp); 251 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 252 &size); 253 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 254 (void) cd9660_statfs(mp, &mp->mnt_stat, p); 255 return 0; 256 } 257 258 /* 259 * Common code for mount and mountroot 260 */ 261 static int 262 iso_mountfs(devvp, mp, p, argp) 263 register struct vnode *devvp; 264 struct mount *mp; 265 struct proc *p; 266 struct iso_args *argp; 267 { 268 register struct iso_mnt *isomp = (struct iso_mnt *)0; 269 struct buf *bp = NULL; 270 struct buf *pribp = NULL, *supbp = NULL; 271 dev_t dev = devvp->v_rdev; 272 int error = EINVAL; 273 int needclose = 0; 274 int high_sierra = 0; 275 int iso_bsize; 276 int iso_blknum; 277 int joliet_level; 278 struct iso_volume_descriptor *vdp = 0; 279 struct iso_primary_descriptor *pri = NULL; 280 struct iso_sierra_primary_descriptor *pri_sierra = NULL; 281 struct iso_supplementary_descriptor *sup = NULL; 282 struct iso_directory_record *rootp; 283 int logical_block_size; 284 285 if (!(mp->mnt_flag & MNT_RDONLY)) 286 return EROFS; 287 288 /* 289 * Disallow multiple mounts of the same device. 290 * Disallow mounting of a device that is currently in use 291 * (except for root, which might share swap device for miniroot). 292 * Flush out any old buffers remaining from a previous use. 293 */ 294 if ((error = vfs_mountedon(devvp))) 295 return error; 296 if (vcount(devvp) > 1 && devvp != rootvp) 297 return EBUSY; 298 if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))) 299 return (error); 300 301 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 302 error = VOP_OPEN(devvp, FREAD, FSCRED, p); 303 VOP_UNLOCK(devvp, 0, p); 304 if (error) 305 return error; 306 307 needclose = 1; 308 309 /* This is the "logical sector size". The standard says this 310 * should be 2048 or the physical sector size on the device, 311 * whichever is greater. For now, we'll just use a constant. 312 */ 313 iso_bsize = ISO_DEFAULT_BLOCK_SIZE; 314 315 joliet_level = 0; 316 for (iso_blknum = 16 + argp->ssector; 317 iso_blknum < 100 + argp->ssector; 318 iso_blknum++) { 319 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize), 320 iso_bsize, NOCRED, &bp)) != 0) 321 goto out; 322 323 vdp = (struct iso_volume_descriptor *)bp->b_data; 324 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) { 325 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID, 326 sizeof vdp->id) != 0) { 327 error = EINVAL; 328 goto out; 329 } else 330 high_sierra = 1; 331 } 332 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){ 333 case ISO_VD_PRIMARY: 334 if (pribp == NULL) { 335 pribp = bp; 336 bp = NULL; 337 pri = (struct iso_primary_descriptor *)vdp; 338 pri_sierra = 339 (struct iso_sierra_primary_descriptor *)vdp; 340 } 341 break; 342 343 case ISO_VD_SUPPLEMENTARY: 344 if (supbp == NULL) { 345 supbp = bp; 346 bp = NULL; 347 sup = (struct iso_supplementary_descriptor *)vdp; 348 349 if (!(argp->flags & ISOFSMNT_NOJOLIET)) { 350 if (bcmp(sup->escape, "%/@", 3) == 0) 351 joliet_level = 1; 352 if (bcmp(sup->escape, "%/C", 3) == 0) 353 joliet_level = 2; 354 if (bcmp(sup->escape, "%/E", 3) == 0) 355 joliet_level = 3; 356 357 if ((isonum_711 (sup->flags) & 1) && 358 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0) 359 joliet_level = 0; 360 } 361 } 362 break; 363 364 case ISO_VD_END: 365 goto vd_end; 366 367 default: 368 break; 369 } 370 if (bp) { 371 brelse(bp); 372 bp = NULL; 373 } 374 } 375 vd_end: 376 if (bp) { 377 brelse(bp); 378 bp = NULL; 379 } 380 381 if (pri == NULL) { 382 error = EINVAL; 383 goto out; 384 } 385 386 logical_block_size = 387 isonum_723 (high_sierra? 388 pri_sierra->logical_block_size: 389 pri->logical_block_size); 390 391 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE 392 || (logical_block_size & (logical_block_size - 1)) != 0) { 393 error = EINVAL; 394 goto out; 395 } 396 397 rootp = (struct iso_directory_record *) 398 (high_sierra? 399 pri_sierra->root_directory_record: 400 pri->root_directory_record); 401 402 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO); 403 isomp->logical_block_size = logical_block_size; 404 isomp->volume_space_size = 405 isonum_733 (high_sierra? 406 pri_sierra->volume_space_size: 407 pri->volume_space_size); 408 isomp->joliet_level = 0; 409 /* 410 * Since an ISO9660 multi-session CD can also access previous 411 * sessions, we have to include them into the space consider- 412 * ations. This doesn't yield a very accurate number since 413 * parts of the old sessions might be inaccessible now, but we 414 * can't do much better. This is also important for the NFS 415 * filehandle validation. 416 */ 417 isomp->volume_space_size += argp->ssector; 418 bcopy (rootp, isomp->root, sizeof isomp->root); 419 isomp->root_extent = isonum_733 (rootp->extent); 420 isomp->root_size = isonum_733 (rootp->size); 421 422 isomp->im_bmask = logical_block_size - 1; 423 isomp->im_bshift = ffs(logical_block_size) - 1; 424 425 pribp->b_flags |= B_AGE; 426 brelse(pribp); 427 pribp = NULL; 428 429 mp->mnt_data = (qaddr_t)isomp; 430 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 431 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 432 mp->mnt_maxsymlinklen = 0; 433 mp->mnt_flag |= MNT_LOCAL; 434 isomp->im_mountp = mp; 435 isomp->im_dev = dev; 436 isomp->im_devvp = devvp; 437 438 devvp->v_rdev->si_mountpoint = mp; 439 440 /* Check the Rock Ridge Extention support */ 441 if (!(argp->flags & ISOFSMNT_NORRIP)) { 442 if ((error = bread(isomp->im_devvp, 443 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) << 444 (isomp->im_bshift - DEV_BSHIFT), 445 isomp->logical_block_size, NOCRED, &bp)) != 0) 446 goto out; 447 448 rootp = (struct iso_directory_record *)bp->b_data; 449 450 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) { 451 argp->flags |= ISOFSMNT_NORRIP; 452 } else { 453 argp->flags &= ~ISOFSMNT_GENS; 454 } 455 456 /* 457 * The contents are valid, 458 * but they will get reread as part of another vnode, so... 459 */ 460 bp->b_flags |= B_AGE; 461 brelse(bp); 462 bp = NULL; 463 } 464 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS | 465 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET); 466 467 if (high_sierra) { 468 /* this effectively ignores all the mount flags */ 469 log(LOG_INFO, "cd9660: High Sierra Format\n"); 470 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA; 471 } else 472 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) { 473 default: 474 isomp->iso_ftype = ISO_FTYPE_DEFAULT; 475 break; 476 case ISOFSMNT_GENS|ISOFSMNT_NORRIP: 477 isomp->iso_ftype = ISO_FTYPE_9660; 478 break; 479 case 0: 480 log(LOG_INFO, "cd9660: RockRidge Extension\n"); 481 isomp->iso_ftype = ISO_FTYPE_RRIP; 482 break; 483 } 484 485 /* Decide whether to use the Joliet descriptor */ 486 487 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) { 488 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level); 489 rootp = (struct iso_directory_record *) 490 sup->root_directory_record; 491 bcopy (rootp, isomp->root, sizeof isomp->root); 492 isomp->root_extent = isonum_733 (rootp->extent); 493 isomp->root_size = isonum_733 (rootp->size); 494 isomp->joliet_level = joliet_level; 495 supbp->b_flags |= B_AGE; 496 } 497 498 if (supbp) { 499 brelse(supbp); 500 supbp = NULL; 501 } 502 503 return 0; 504 out: 505 devvp->v_rdev->si_mountpoint = NULL; 506 if (bp) 507 brelse(bp); 508 if (pribp) 509 brelse(pribp); 510 if (supbp) 511 brelse(supbp); 512 if (needclose) 513 (void)VOP_CLOSE(devvp, FREAD, NOCRED, p); 514 if (isomp) { 515 free((caddr_t)isomp, M_ISOFSMNT); 516 mp->mnt_data = (qaddr_t)0; 517 } 518 return error; 519 } 520 521 /* 522 * unmount system call 523 */ 524 static int 525 cd9660_unmount(mp, mntflags, p) 526 struct mount *mp; 527 int mntflags; 528 struct proc *p; 529 { 530 register struct iso_mnt *isomp; 531 int error, flags = 0; 532 533 if (mntflags & MNT_FORCE) 534 flags |= FORCECLOSE; 535 #if 0 536 mntflushbuf(mp, 0); 537 if (mntinvalbuf(mp)) 538 return EBUSY; 539 #endif 540 if ((error = vflush(mp, 0, flags))) 541 return (error); 542 543 isomp = VFSTOISOFS(mp); 544 545 isomp->im_devvp->v_rdev->si_mountpoint = NULL; 546 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p); 547 vrele(isomp->im_devvp); 548 free((caddr_t)isomp, M_ISOFSMNT); 549 mp->mnt_data = (qaddr_t)0; 550 mp->mnt_flag &= ~MNT_LOCAL; 551 return (error); 552 } 553 554 /* 555 * Return root of a filesystem 556 */ 557 static int 558 cd9660_root(mp, vpp) 559 struct mount *mp; 560 struct vnode **vpp; 561 { 562 struct iso_mnt *imp = VFSTOISOFS(mp); 563 struct iso_directory_record *dp = 564 (struct iso_directory_record *)imp->root; 565 ino_t ino = isodirino(dp, imp); 566 567 /* 568 * With RRIP we must use the `.' entry of the root directory. 569 * Simply tell vget, that it's a relocated directory. 570 */ 571 return (cd9660_vget_internal(mp, ino, vpp, 572 imp->iso_ftype == ISO_FTYPE_RRIP, dp)); 573 } 574 575 /* 576 * Get file system statistics. 577 */ 578 int 579 cd9660_statfs(mp, sbp, p) 580 struct mount *mp; 581 register struct statfs *sbp; 582 struct proc *p; 583 { 584 register struct iso_mnt *isomp; 585 586 isomp = VFSTOISOFS(mp); 587 588 sbp->f_bsize = isomp->logical_block_size; 589 sbp->f_iosize = sbp->f_bsize; /* XXX */ 590 sbp->f_blocks = isomp->volume_space_size; 591 sbp->f_bfree = 0; /* total free blocks */ 592 sbp->f_bavail = 0; /* blocks free for non superuser */ 593 sbp->f_files = 0; /* total files */ 594 sbp->f_ffree = 0; /* free file nodes */ 595 if (sbp != &mp->mnt_stat) { 596 sbp->f_type = mp->mnt_vfc->vfc_typenum; 597 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 598 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 599 } 600 return 0; 601 } 602 603 /* 604 * File handle to vnode 605 * 606 * Have to be really careful about stale file handles: 607 * - check that the inode number is in range 608 * - call iget() to get the locked inode 609 * - check for an unallocated inode (i_mode == 0) 610 * - check that the generation number matches 611 */ 612 613 struct ifid { 614 ushort ifid_len; 615 ushort ifid_pad; 616 int ifid_ino; 617 long ifid_start; 618 }; 619 620 /* ARGSUSED */ 621 int 622 cd9660_fhtovp(mp, fhp, vpp) 623 register struct mount *mp; 624 struct fid *fhp; 625 struct vnode **vpp; 626 { 627 struct ifid *ifhp = (struct ifid *)fhp; 628 register struct iso_node *ip; 629 struct vnode *nvp; 630 int error; 631 632 #ifdef ISOFS_DBG 633 printf("fhtovp: ino %d, start %ld\n", 634 ifhp->ifid_ino, ifhp->ifid_start); 635 #endif 636 637 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) { 638 *vpp = NULLVP; 639 return (error); 640 } 641 ip = VTOI(nvp); 642 if (ip->inode.iso_mode == 0) { 643 vput(nvp); 644 *vpp = NULLVP; 645 return (ESTALE); 646 } 647 *vpp = nvp; 648 return (0); 649 } 650 651 int 652 cd9660_vget(mp, ino, vpp) 653 struct mount *mp; 654 ino_t ino; 655 struct vnode **vpp; 656 { 657 658 /* 659 * XXXX 660 * It would be nice if we didn't always set the `relocated' flag 661 * and force the extra read, but I don't want to think about fixing 662 * that right now. 663 */ 664 return (cd9660_vget_internal(mp, ino, vpp, 665 #if 0 666 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP, 667 #else 668 0, 669 #endif 670 (struct iso_directory_record *)0)); 671 } 672 673 int 674 cd9660_vget_internal(mp, ino, vpp, relocated, isodir) 675 struct mount *mp; 676 ino_t ino; 677 struct vnode **vpp; 678 int relocated; 679 struct iso_directory_record *isodir; 680 { 681 struct iso_mnt *imp; 682 struct iso_node *ip; 683 struct buf *bp; 684 struct vnode *vp; 685 dev_t dev; 686 int error; 687 688 imp = VFSTOISOFS(mp); 689 dev = imp->im_dev; 690 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP) 691 return (0); 692 693 /* Allocate a new vnode/iso_node. */ 694 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) { 695 *vpp = NULLVP; 696 return (error); 697 } 698 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE, 699 M_WAITOK | M_ZERO); 700 lockinit(&vp->v_lock, PINOD, "isonode", 0, 0); 701 /* 702 * ISOFS uses stdlock and can share lock structure 703 */ 704 vp->v_vnlock = &vp->v_lock; 705 vp->v_data = ip; 706 ip->i_vnode = vp; 707 ip->i_dev = dev; 708 ip->i_number = ino; 709 710 /* 711 * Put it onto its hash chain and lock it so that other requests for 712 * this inode will block if they arrive while we are sleeping waiting 713 * for old data structures to be purged or for the contents of the 714 * disk portion of this inode to be read. 715 */ 716 cd9660_ihashins(ip); 717 718 if (isodir == 0) { 719 int lbn, off; 720 721 lbn = lblkno(imp, ino); 722 if (lbn >= imp->volume_space_size) { 723 vput(vp); 724 printf("fhtovp: lbn exceed volume space %d\n", lbn); 725 return (ESTALE); 726 } 727 728 off = blkoff(imp, ino); 729 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) { 730 vput(vp); 731 printf("fhtovp: crosses block boundary %d\n", 732 off + ISO_DIRECTORY_RECORD_SIZE); 733 return (ESTALE); 734 } 735 736 error = bread(imp->im_devvp, 737 lbn << (imp->im_bshift - DEV_BSHIFT), 738 imp->logical_block_size, NOCRED, &bp); 739 if (error) { 740 vput(vp); 741 brelse(bp); 742 printf("fhtovp: bread error %d\n",error); 743 return (error); 744 } 745 isodir = (struct iso_directory_record *)(bp->b_data + off); 746 747 if (off + isonum_711(isodir->length) > 748 imp->logical_block_size) { 749 vput(vp); 750 if (bp != 0) 751 brelse(bp); 752 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n", 753 off +isonum_711(isodir->length), off, 754 isonum_711(isodir->length)); 755 return (ESTALE); 756 } 757 758 #if 0 759 if (isonum_733(isodir->extent) + 760 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) { 761 if (bp != 0) 762 brelse(bp); 763 printf("fhtovp: file start miss %d vs %d\n", 764 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length), 765 ifhp->ifid_start); 766 return (ESTALE); 767 } 768 #endif 769 } else 770 bp = 0; 771 772 ip->i_mnt = imp; 773 ip->i_devvp = imp->im_devvp; 774 VREF(ip->i_devvp); 775 776 if (relocated) { 777 /* 778 * On relocated directories we must 779 * read the `.' entry out of a dir. 780 */ 781 ip->iso_start = ino >> imp->im_bshift; 782 if (bp != 0) 783 brelse(bp); 784 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) { 785 vput(vp); 786 return (error); 787 } 788 isodir = (struct iso_directory_record *)bp->b_data; 789 } 790 791 ip->iso_extent = isonum_733(isodir->extent); 792 ip->i_size = isonum_733(isodir->size); 793 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent; 794 795 /* 796 * Setup time stamp, attribute 797 */ 798 vp->v_type = VNON; 799 switch (imp->iso_ftype) { 800 default: /* ISO_FTYPE_9660 */ 801 { 802 struct buf *bp2; 803 int off; 804 if ((imp->im_flags & ISOFSMNT_EXTATT) 805 && (off = isonum_711(isodir->ext_attr_length))) 806 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL, 807 &bp2); 808 else 809 bp2 = NULL; 810 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660); 811 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660); 812 if (bp2) 813 brelse(bp2); 814 break; 815 } 816 case ISO_FTYPE_RRIP: 817 cd9660_rrip_analyze(isodir, ip, imp); 818 break; 819 } 820 821 if (bp != 0) 822 brelse(bp); 823 824 /* 825 * Initialize the associated vnode 826 */ 827 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) { 828 case VFIFO: 829 vp->v_op = cd9660_fifoop_p; 830 break; 831 case VCHR: 832 case VBLK: 833 vp->v_op = cd9660_specop_p; 834 vp = addaliasu(vp, ip->inode.iso_rdev); 835 ip->i_vnode = vp; 836 break; 837 default: 838 break; 839 } 840 841 if (ip->iso_extent == imp->root_extent) 842 vp->v_flag |= VROOT; 843 844 /* 845 * XXX need generation number? 846 */ 847 848 *vpp = vp; 849 return (0); 850 } 851 852 /* 853 * Vnode pointer to File handle 854 */ 855 /* ARGSUSED */ 856 int 857 cd9660_vptofh(vp, fhp) 858 struct vnode *vp; 859 struct fid *fhp; 860 { 861 register struct iso_node *ip = VTOI(vp); 862 register struct ifid *ifhp; 863 864 ifhp = (struct ifid *)fhp; 865 ifhp->ifid_len = sizeof(struct ifid); 866 867 ifhp->ifid_ino = ip->i_number; 868 ifhp->ifid_start = ip->iso_start; 869 870 #ifdef ISOFS_DBG 871 printf("vptofh: ino %d, start %ld\n", 872 ifhp->ifid_ino,ifhp->ifid_start); 873 #endif 874 return 0; 875 } 876