1 /* $OpenBSD: cd9660_vfsops.c,v 1.95 2021/03/05 07:10:06 jsg Exp $ */ 2 /* $NetBSD: cd9660_vfsops.c,v 1.26 1997/06/13 15:38:58 pk Exp $ */ 3 4 /*- 5 * Copyright (c) 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley 9 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 10 * Support code is derived from software contributed to Berkeley 11 * by Atsushi Murai (amurai@spec.co.jp). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)cd9660_vfsops.c 8.9 (Berkeley) 12/5/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/namei.h> 43 #include <sys/proc.h> 44 #include <sys/kernel.h> 45 #include <sys/vnode.h> 46 #include <sys/lock.h> 47 #include <sys/specdev.h> 48 #include <sys/mount.h> 49 #include <sys/buf.h> 50 #include <sys/fcntl.h> 51 #include <sys/disklabel.h> 52 #include <sys/ioctl.h> 53 #include <sys/cdio.h> 54 #include <sys/errno.h> 55 #include <sys/malloc.h> 56 #include <sys/stat.h> 57 58 #include <isofs/cd9660/iso.h> 59 #include <isofs/cd9660/cd9660_extern.h> 60 #include <isofs/cd9660/iso_rrip.h> 61 #include <isofs/cd9660/cd9660_node.h> 62 63 const struct vfsops cd9660_vfsops = { 64 .vfs_mount = cd9660_mount, 65 .vfs_start = cd9660_start, 66 .vfs_unmount = cd9660_unmount, 67 .vfs_root = cd9660_root, 68 .vfs_quotactl = cd9660_quotactl, 69 .vfs_statfs = cd9660_statfs, 70 .vfs_sync = cd9660_sync, 71 .vfs_vget = cd9660_vget, 72 .vfs_fhtovp = cd9660_fhtovp, 73 .vfs_vptofh = cd9660_vptofh, 74 .vfs_init = cd9660_init, 75 .vfs_sysctl = cd9660_sysctl, 76 .vfs_checkexp = cd9660_check_export, 77 }; 78 79 /* 80 * Called by vfs_mountroot when iso is going to be mounted as root. 81 */ 82 83 static int iso_mountfs(struct vnode *devvp, struct mount *mp, 84 struct proc *p, struct iso_args *argp); 85 int iso_disklabelspoof(dev_t dev, void (*strat)(struct buf *), 86 struct disklabel *lp); 87 88 int 89 cd9660_mountroot(void) 90 { 91 struct mount *mp; 92 extern struct vnode *rootvp; 93 struct proc *p = curproc; /* XXX */ 94 int error; 95 struct iso_args args; 96 97 /* 98 * Get vnodes for swapdev and rootdev. 99 */ 100 if ((error = bdevvp(swapdev, &swapdev_vp)) || 101 (error = bdevvp(rootdev, &rootvp))) { 102 printf("cd9660_mountroot: can't setup bdevvp's"); 103 return (error); 104 } 105 106 if ((error = vfs_rootmountalloc("cd9660", "root_device", &mp)) != 0) 107 return (error); 108 args.flags = ISOFSMNT_ROOT; 109 if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0) { 110 vfs_unbusy(mp); 111 vfs_mount_free(mp); 112 return (error); 113 } 114 115 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 116 (void)cd9660_statfs(mp, &mp->mnt_stat, p); 117 vfs_unbusy(mp); 118 inittodr(0); 119 120 return (0); 121 } 122 123 /* 124 * VFS Operations. 125 * 126 * mount system call 127 */ 128 int 129 cd9660_mount(struct mount *mp, const char *path, void *data, 130 struct nameidata *ndp, struct proc *p) 131 { 132 struct iso_mnt *imp = NULL; 133 struct iso_args *args = data; 134 struct vnode *devvp; 135 char fspec[MNAMELEN]; 136 int error; 137 138 if ((mp->mnt_flag & MNT_RDONLY) == 0) 139 return (EROFS); 140 141 /* 142 * If updating, check whether changing from read-only to 143 * read/write; if there is no device name, that's all we do. 144 */ 145 if (mp->mnt_flag & MNT_UPDATE) { 146 imp = VFSTOISOFS(mp); 147 if (args && args->fspec == NULL) 148 return (vfs_export(mp, &imp->im_export, 149 &args->export_info)); 150 return (0); 151 } 152 153 /* 154 * Not an update, or updating the name: look up the name 155 * and verify that it refers to a sensible block device. 156 */ 157 error = copyinstr(args->fspec, fspec, sizeof(fspec), NULL); 158 if (error) 159 return (error); 160 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, p); 161 if ((error = namei(ndp)) != 0) 162 return (error); 163 devvp = ndp->ni_vp; 164 165 if (devvp->v_type != VBLK) { 166 vrele(devvp); 167 return (ENOTBLK); 168 } 169 if (major(devvp->v_rdev) >= nblkdev) { 170 vrele(devvp); 171 return (ENXIO); 172 } 173 174 if ((mp->mnt_flag & MNT_UPDATE) == 0) 175 error = iso_mountfs(devvp, mp, p, args); 176 else { 177 if (devvp != imp->im_devvp) 178 error = EINVAL; /* needs translation */ 179 else 180 vrele(devvp); 181 } 182 if (error) { 183 vrele(devvp); 184 return (error); 185 } 186 187 bzero(mp->mnt_stat.f_mntonname, MNAMELEN); 188 strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN); 189 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN); 190 strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN); 191 bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN); 192 strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN); 193 bcopy(args, &mp->mnt_stat.mount_info.iso_args, sizeof(*args)); 194 195 cd9660_statfs(mp, &mp->mnt_stat, p); 196 197 return (0); 198 } 199 200 /* 201 * Common code for mount and mountroot 202 */ 203 static int 204 iso_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p, 205 struct iso_args *argp) 206 { 207 struct iso_mnt *isomp = NULL; 208 struct buf *bp = NULL; 209 struct buf *pribp = NULL, *supbp = NULL; 210 dev_t dev = devvp->v_rdev; 211 int error = EINVAL; 212 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 213 extern struct vnode *rootvp; 214 int iso_bsize; 215 int iso_blknum; 216 int joliet_level; 217 struct iso_volume_descriptor *vdp; 218 struct iso_primary_descriptor *pri = NULL; 219 struct iso_supplementary_descriptor *sup = NULL; 220 struct iso_directory_record *rootp; 221 int logical_block_size; 222 int sess; 223 224 if (!ronly) 225 return (EROFS); 226 227 /* 228 * Disallow multiple mounts of the same device. 229 * Disallow mounting of a device that is currently in use 230 * (except for root, which might share swap device for miniroot). 231 * Flush out any old buffers remaining from a previous use. 232 */ 233 if ((error = vfs_mountedon(devvp)) != 0) 234 return (error); 235 if (vcount(devvp) > 1 && devvp != rootvp) 236 return (EBUSY); 237 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 238 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, INFSLP); 239 VOP_UNLOCK(devvp); 240 if (error) 241 return (error); 242 243 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 244 if (error) 245 return (error); 246 247 /* 248 * This is the "logical sector size". The standard says this 249 * should be 2048 or the physical sector size on the device, 250 * whichever is greater. For now, we'll just use a constant. 251 */ 252 iso_bsize = ISO_DEFAULT_BLOCK_SIZE; 253 254 if (argp->flags & ISOFSMNT_SESS) { 255 sess = argp->sess; 256 if (sess < 0) 257 sess = 0; 258 } else { 259 sess = 0; 260 error = VOP_IOCTL(devvp, CDIOREADMSADDR, (caddr_t)&sess, 0, 261 FSCRED, p); 262 if (error) 263 sess = 0; 264 } 265 266 joliet_level = 0; 267 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) { 268 if ((error = bread(devvp, 269 (iso_blknum + sess) * btodb(iso_bsize), 270 iso_bsize, &bp)) != 0) 271 goto out; 272 273 vdp = (struct iso_volume_descriptor *)bp->b_data; 274 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) { 275 error = EINVAL; 276 goto out; 277 } 278 279 switch (isonum_711 (vdp->type)){ 280 case ISO_VD_PRIMARY: 281 if (pribp == NULL) { 282 pribp = bp; 283 bp = NULL; 284 pri = (struct iso_primary_descriptor *)vdp; 285 } 286 break; 287 case ISO_VD_SUPPLEMENTARY: 288 if (supbp == NULL) { 289 supbp = bp; 290 bp = NULL; 291 sup = (struct iso_supplementary_descriptor *)vdp; 292 293 if (!(argp->flags & ISOFSMNT_NOJOLIET)) { 294 if (bcmp(sup->escape, "%/@", 3) == 0) 295 joliet_level = 1; 296 if (bcmp(sup->escape, "%/C", 3) == 0) 297 joliet_level = 2; 298 if (bcmp(sup->escape, "%/E", 3) == 0) 299 joliet_level = 3; 300 301 if (isonum_711 (sup->flags) & 1) 302 joliet_level = 0; 303 } 304 } 305 break; 306 307 case ISO_VD_END: 308 goto vd_end; 309 310 default: 311 break; 312 } 313 if (bp) { 314 brelse(bp); 315 bp = NULL; 316 } 317 } 318 vd_end: 319 if (bp) { 320 brelse(bp); 321 bp = NULL; 322 } 323 324 if (pri == NULL) { 325 error = EINVAL; 326 goto out; 327 } 328 329 logical_block_size = isonum_723 (pri->logical_block_size); 330 331 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE 332 || (logical_block_size & (logical_block_size - 1)) != 0) { 333 error = EINVAL; 334 goto out; 335 } 336 337 rootp = (struct iso_directory_record *)pri->root_directory_record; 338 339 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK); 340 bzero((caddr_t)isomp, sizeof *isomp); 341 isomp->logical_block_size = logical_block_size; 342 isomp->volume_space_size = isonum_733 (pri->volume_space_size); 343 bcopy (rootp, isomp->root, sizeof isomp->root); 344 isomp->root_extent = isonum_733 (rootp->extent); 345 isomp->root_size = isonum_733 (rootp->size); 346 isomp->joliet_level = 0; 347 /* 348 * Since an ISO9660 multi-session CD can also access previous sessions, 349 * we have to include them into the space considerations. 350 */ 351 isomp->volume_space_size += sess; 352 isomp->im_bmask = logical_block_size - 1; 353 isomp->im_bshift = ffs(logical_block_size) - 1; 354 355 brelse(pribp); 356 pribp = NULL; 357 358 mp->mnt_data = isomp; 359 mp->mnt_stat.f_fsid.val[0] = (long)dev; 360 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 361 mp->mnt_stat.f_namemax = NAME_MAX; 362 mp->mnt_flag |= MNT_LOCAL; 363 isomp->im_mountp = mp; 364 isomp->im_dev = dev; 365 isomp->im_devvp = devvp; 366 367 /* Check the Rock Ridge Extension support */ 368 if (!(argp->flags & ISOFSMNT_NORRIP)) { 369 if ((error = bread(isomp->im_devvp, (isomp->root_extent + 370 isonum_711(rootp->ext_attr_length)) << 371 (isomp->im_bshift - DEV_BSHIFT), 372 isomp->logical_block_size, &bp)) != 0) 373 goto out; 374 375 rootp = (struct iso_directory_record *)bp->b_data; 376 377 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) { 378 argp->flags |= ISOFSMNT_NORRIP; 379 } else { 380 argp->flags &= ~ISOFSMNT_GENS; 381 } 382 383 /* 384 * The contents are valid, 385 * but they will get reread as part of another vnode, so... 386 */ 387 brelse(bp); 388 bp = NULL; 389 } 390 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS | 391 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET); 392 switch (isomp->im_flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS)) { 393 default: 394 isomp->iso_ftype = ISO_FTYPE_DEFAULT; 395 break; 396 case ISOFSMNT_GENS|ISOFSMNT_NORRIP: 397 isomp->iso_ftype = ISO_FTYPE_9660; 398 break; 399 case 0: 400 isomp->iso_ftype = ISO_FTYPE_RRIP; 401 break; 402 } 403 404 /* Decide whether to use the Joliet descriptor */ 405 406 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) { 407 rootp = (struct iso_directory_record *) 408 sup->root_directory_record; 409 bcopy(rootp, isomp->root, sizeof isomp->root); 410 isomp->root_extent = isonum_733(rootp->extent); 411 isomp->root_size = isonum_733(rootp->size); 412 isomp->joliet_level = joliet_level; 413 } 414 415 if (supbp) { 416 brelse(supbp); 417 supbp = NULL; 418 } 419 420 devvp->v_specmountpoint = mp; 421 422 return (0); 423 out: 424 if (devvp->v_specinfo) 425 devvp->v_specmountpoint = NULL; 426 if (bp) 427 brelse(bp); 428 if (supbp) 429 brelse(supbp); 430 431 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 432 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 433 VOP_UNLOCK(devvp); 434 435 if (isomp) { 436 free((caddr_t)isomp, M_ISOFSMNT, 0); 437 mp->mnt_data = NULL; 438 } 439 return (error); 440 } 441 442 /* 443 * Test to see if the device is an ISOFS filesystem. 444 */ 445 int 446 iso_disklabelspoof(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp) 447 { 448 struct buf *bp = NULL; 449 struct iso_volume_descriptor *vdp; 450 struct iso_primary_descriptor *pri; 451 int logical_block_size; 452 int error = EINVAL; 453 int iso_blknum; 454 int i; 455 456 bp = geteblk(ISO_DEFAULT_BLOCK_SIZE); 457 bp->b_dev = dev; 458 459 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) { 460 bp->b_blkno = iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE); 461 bp->b_bcount = ISO_DEFAULT_BLOCK_SIZE; 462 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE); 463 SET(bp->b_flags, B_BUSY | B_READ | B_RAW); 464 465 /*printf("d_secsize %d iso_blknum %d b_blkno %d bcount %d\n", 466 lp->d_secsize, iso_blknum, bp->b_blkno, bp->b_bcount);*/ 467 468 (*strat)(bp); 469 470 if (biowait(bp)) 471 goto out; 472 473 vdp = (struct iso_volume_descriptor *)bp->b_data; 474 /*printf("%2x%2x%2x type %2x\n", vdp->id[0], vdp->id[1], 475 vdp->id[2], isonum_711(vdp->type));*/ 476 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0 || 477 isonum_711 (vdp->type) == ISO_VD_END) 478 goto out; 479 480 if (isonum_711 (vdp->type) == ISO_VD_PRIMARY) 481 break; 482 } 483 484 if (isonum_711 (vdp->type) != ISO_VD_PRIMARY) 485 goto out; 486 487 pri = (struct iso_primary_descriptor *)vdp; 488 logical_block_size = isonum_723 (pri->logical_block_size); 489 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE || 490 (logical_block_size & (logical_block_size - 1)) != 0) 491 goto out; 492 493 /* 494 * build a disklabel for the CD 495 */ 496 strncpy(lp->d_typename, pri->volume_id, sizeof lp->d_typename); 497 strncpy(lp->d_packname, pri->volume_id+16, sizeof lp->d_packname); 498 for (i = 0; i < MAXPARTITIONS; i++) { 499 DL_SETPSIZE(&lp->d_partitions[i], 0); 500 DL_SETPOFFSET(&lp->d_partitions[i], 0); 501 } 502 DL_SETPOFFSET(&lp->d_partitions[0], 0); 503 DL_SETPSIZE(&lp->d_partitions[0], DL_GETDSIZE(lp)); 504 lp->d_partitions[0].p_fstype = FS_ISO9660; 505 DL_SETPOFFSET(&lp->d_partitions[RAW_PART], 0); 506 DL_SETPSIZE(&lp->d_partitions[RAW_PART], DL_GETDSIZE(lp)); 507 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 508 lp->d_npartitions = MAXPARTITIONS; 509 lp->d_bbsize = 8192; /* fake */ 510 lp->d_sbsize = 64*1024; /* fake */ 511 lp->d_version = 1; 512 513 lp->d_magic = DISKMAGIC; 514 lp->d_magic2 = DISKMAGIC; 515 lp->d_checksum = dkcksum(lp); 516 error = 0; 517 out: 518 bp->b_flags |= B_INVAL; 519 brelse(bp); 520 return (error); 521 } 522 523 /* 524 * Make a filesystem operational. 525 * Nothing to do at the moment. 526 */ 527 /* ARGSUSED */ 528 int 529 cd9660_start(struct mount *mp, int flags, struct proc *p) 530 { 531 return (0); 532 } 533 534 /* 535 * unmount system call 536 */ 537 int 538 cd9660_unmount(struct mount *mp, int mntflags, struct proc *p) 539 { 540 struct iso_mnt *isomp; 541 int error, flags = 0; 542 543 if (mntflags & MNT_FORCE) 544 flags |= FORCECLOSE; 545 #if 0 546 mntflushbuf(mp, 0); 547 if (mntinvalbuf(mp)) 548 return (EBUSY); 549 #endif 550 if ((error = vflush(mp, NULLVP, flags)) != 0) 551 return (error); 552 553 isomp = VFSTOISOFS(mp); 554 555 isomp->im_devvp->v_specmountpoint = NULL; 556 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY); 557 (void)VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p); 558 vput(isomp->im_devvp); 559 free((caddr_t)isomp, M_ISOFSMNT, 0); 560 mp->mnt_data = NULL; 561 mp->mnt_flag &= ~MNT_LOCAL; 562 return (0); 563 } 564 565 /* 566 * Return root of a filesystem 567 */ 568 int 569 cd9660_root(struct mount *mp, struct vnode **vpp) 570 { 571 struct iso_mnt *imp = VFSTOISOFS(mp); 572 struct iso_directory_record *dp = 573 (struct iso_directory_record *)imp->root; 574 cdino_t ino = isodirino(dp, imp); 575 576 /* 577 * With RRIP we must use the `.' entry of the root directory. 578 * Simply tell vget, that it's a relocated directory. 579 */ 580 return (cd9660_vget_internal(mp, ino, vpp, 581 imp->iso_ftype == ISO_FTYPE_RRIP, dp)); 582 } 583 584 /* 585 * Do operations associated with quotas, not supported 586 */ 587 /* ARGSUSED */ 588 int 589 cd9660_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, 590 struct proc *p) 591 { 592 593 return (EOPNOTSUPP); 594 } 595 596 /* 597 * Get file system statistics. 598 */ 599 int 600 cd9660_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 601 { 602 struct iso_mnt *isomp; 603 604 isomp = VFSTOISOFS(mp); 605 606 sbp->f_bsize = isomp->logical_block_size; 607 sbp->f_iosize = sbp->f_bsize; /* XXX */ 608 sbp->f_blocks = isomp->volume_space_size; 609 sbp->f_bfree = 0; /* total free blocks */ 610 sbp->f_bavail = 0; /* blocks free for non superuser */ 611 sbp->f_files = 0; /* total files */ 612 sbp->f_ffree = 0; /* free file nodes */ 613 sbp->f_favail = 0; /* file nodes free for non superuser */ 614 copy_statfs_info(sbp, mp); 615 616 return (0); 617 } 618 619 /* ARGSUSED */ 620 int 621 cd9660_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, 622 struct proc *p) 623 { 624 return (0); 625 } 626 627 /* 628 * File handle to vnode 629 * 630 * Have to be really careful about stale file handles: 631 * - check that the inode number is in range 632 * - call iget() to get the locked inode 633 * - check for an unallocated inode (i_mode == 0) 634 * - check that the generation number matches 635 */ 636 637 struct ifid { 638 ushort ifid_len; 639 ushort ifid_pad; 640 int ifid_ino; 641 long ifid_start; 642 }; 643 644 /* ARGSUSED */ 645 int 646 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 647 { 648 struct ifid *ifhp = (struct ifid *)fhp; 649 struct iso_node *ip; 650 struct vnode *nvp; 651 int error; 652 653 #ifdef ISOFS_DBG 654 printf("fhtovp: ino %d, start %ld\n", ifhp->ifid_ino, 655 ifhp->ifid_start); 656 #endif 657 658 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) { 659 *vpp = NULLVP; 660 return (error); 661 } 662 ip = VTOI(nvp); 663 if (ip->inode.iso_mode == 0) { 664 vput(nvp); 665 *vpp = NULLVP; 666 return (ESTALE); 667 } 668 *vpp = nvp; 669 return (0); 670 } 671 672 int 673 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 674 { 675 676 if (ino > (cdino_t)-1) 677 panic("cd9660_vget: alien ino_t %llu", 678 (unsigned long long)ino); 679 680 /* 681 * XXXX 682 * It would be nice if we didn't always set the `relocated' flag 683 * and force the extra read, but I don't want to think about fixing 684 * that right now. 685 */ 686 return (cd9660_vget_internal(mp, ino, vpp, 687 #if 0 688 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP, 689 #else 690 0, 691 #endif 692 NULL)); 693 } 694 695 int 696 cd9660_vget_internal(struct mount *mp, cdino_t ino, struct vnode **vpp, 697 int relocated, struct iso_directory_record *isodir) 698 { 699 struct iso_mnt *imp; 700 struct iso_node *ip; 701 struct buf *bp; 702 struct vnode *vp, *nvp; 703 dev_t dev; 704 int error; 705 706 retry: 707 imp = VFSTOISOFS(mp); 708 dev = imp->im_dev; 709 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP) 710 return (0); 711 712 /* Allocate a new vnode/iso_node. */ 713 if ((error = getnewvnode(VT_ISOFS, mp, &cd9660_vops, &vp)) != 0) { 714 *vpp = NULLVP; 715 return (error); 716 } 717 ip = malloc(sizeof(*ip), M_ISOFSNODE, M_WAITOK | M_ZERO); 718 rrw_init_flags(&ip->i_lock, "isoinode", RWL_DUPOK | RWL_IS_VNODE); 719 vp->v_data = ip; 720 ip->i_vnode = vp; 721 ip->i_dev = dev; 722 ip->i_number = ino; 723 724 /* 725 * Put it onto its hash chain and lock it so that other requests for 726 * this inode will block if they arrive while we are sleeping waiting 727 * for old data structures to be purged or for the contents of the 728 * disk portion of this inode to be read. 729 */ 730 error = cd9660_ihashins(ip); 731 732 if (error) { 733 vrele(vp); 734 735 if (error == EEXIST) 736 goto retry; 737 738 return (error); 739 } 740 741 if (isodir == 0) { 742 int lbn, off; 743 744 lbn = lblkno(imp, ino); 745 if (lbn >= imp->volume_space_size) { 746 vput(vp); 747 printf("fhtovp: lbn exceed volume space %d\n", lbn); 748 return (ESTALE); 749 } 750 751 off = blkoff(imp, ino); 752 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) 753 { 754 vput(vp); 755 printf("fhtovp: crosses block boundary %d\n", 756 off + ISO_DIRECTORY_RECORD_SIZE); 757 return (ESTALE); 758 } 759 760 error = bread(imp->im_devvp, 761 lbn << (imp->im_bshift - DEV_BSHIFT), 762 imp->logical_block_size, &bp); 763 if (error) { 764 vput(vp); 765 brelse(bp); 766 printf("fhtovp: bread error %d\n",error); 767 return (error); 768 } 769 isodir = (struct iso_directory_record *)(bp->b_data + off); 770 771 if (off + isonum_711(isodir->length) > 772 imp->logical_block_size) { 773 vput(vp); 774 if (bp != 0) 775 brelse(bp); 776 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n", 777 off +isonum_711(isodir->length), off, 778 isonum_711(isodir->length)); 779 return (ESTALE); 780 } 781 782 #if 0 783 if (isonum_733(isodir->extent) + 784 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) { 785 if (bp != 0) 786 brelse(bp); 787 printf("fhtovp: file start miss %d vs %d\n", 788 isonum_733(isodir->extent) + 789 isonum_711(isodir->ext_attr_length), 790 ifhp->ifid_start); 791 return (ESTALE); 792 } 793 #endif 794 } else 795 bp = 0; 796 797 ip->i_mnt = imp; 798 ip->i_devvp = imp->im_devvp; 799 vref(ip->i_devvp); 800 801 if (relocated) { 802 /* 803 * On relocated directories we must 804 * read the `.' entry out of a dir. 805 */ 806 ip->iso_start = ino >> imp->im_bshift; 807 if (bp != 0) 808 brelse(bp); 809 if ((error = cd9660_bufatoff(ip, (off_t)0, NULL, &bp)) != 0) { 810 vput(vp); 811 return (error); 812 } 813 isodir = (struct iso_directory_record *)bp->b_data; 814 } 815 816 ip->iso_extent = isonum_733(isodir->extent); 817 ip->i_size = (u_int32_t) isonum_733(isodir->size); 818 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent; 819 820 /* 821 * Setup time stamp, attribute 822 */ 823 vp->v_type = VNON; 824 switch (imp->iso_ftype) { 825 default: /* ISO_FTYPE_9660 */ 826 { 827 struct buf *bp2; 828 int off; 829 if ((imp->im_flags & ISOFSMNT_EXTATT) && 830 (off = isonum_711(isodir->ext_attr_length))) 831 cd9660_bufatoff(ip, (off_t)-(off << imp->im_bshift), 832 NULL, &bp2); 833 else 834 bp2 = NULL; 835 cd9660_defattr(isodir, ip, bp2); 836 cd9660_deftstamp(isodir, ip, bp2); 837 if (bp2) 838 brelse(bp2); 839 break; 840 } 841 case ISO_FTYPE_RRIP: 842 cd9660_rrip_analyze(isodir, ip, imp); 843 break; 844 } 845 846 if (bp != 0) 847 brelse(bp); 848 849 /* 850 * Initialize the associated vnode 851 */ 852 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) { 853 case VFIFO: 854 #ifdef FIFO 855 vp->v_op = &cd9660_fifovops; 856 break; 857 #else 858 vput(vp); 859 return (EOPNOTSUPP); 860 #endif /* FIFO */ 861 case VCHR: 862 case VBLK: 863 /* 864 * if device, look at device number table for translation 865 */ 866 vp->v_op = &cd9660_specvops; 867 if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) { 868 /* 869 * Discard unneeded vnode, but save its iso_node. 870 * Note that the lock is carried over in the iso_node 871 */ 872 nvp->v_data = vp->v_data; 873 vp->v_data = NULL; 874 vp->v_op = &spec_vops; 875 vrele(vp); 876 vgone(vp); 877 /* 878 * Reinitialize aliased inode. 879 */ 880 vp = nvp; 881 ip->i_vnode = vp; 882 } 883 break; 884 case VLNK: 885 case VNON: 886 case VSOCK: 887 case VDIR: 888 case VBAD: 889 break; 890 case VREG: 891 uvm_vnp_setsize(vp, ip->i_size); 892 break; 893 } 894 895 if (ip->iso_extent == imp->root_extent) 896 vp->v_flag |= VROOT; 897 898 /* 899 * XXX need generation number? 900 */ 901 902 *vpp = vp; 903 return (0); 904 } 905 906 /* 907 * Vnode pointer to File handle 908 */ 909 /* ARGSUSED */ 910 int 911 cd9660_vptofh(struct vnode *vp, struct fid *fhp) 912 { 913 struct iso_node *ip = VTOI(vp); 914 struct ifid *ifhp; 915 916 ifhp = (struct ifid *)fhp; 917 ifhp->ifid_len = sizeof(struct ifid); 918 919 ifhp->ifid_ino = ip->i_number; 920 ifhp->ifid_start = ip->iso_start; 921 922 #ifdef ISOFS_DBG 923 printf("vptofh: ino %d, start %ld\n", 924 ifhp->ifid_ino,ifhp->ifid_start); 925 #endif 926 return (0); 927 } 928 929 /* 930 * Verify a remote client has export rights and return these rights via 931 * exflagsp and credanonp. 932 */ 933 int 934 cd9660_check_export(struct mount *mp, struct mbuf *nam, int *exflagsp, 935 struct ucred **credanonp) 936 { 937 struct netcred *np; 938 struct iso_mnt *imp = VFSTOISOFS(mp); 939 940 /* 941 * Get the export permission structure for this <mp, client> tuple. 942 */ 943 np = vfs_export_lookup(mp, &imp->im_export, nam); 944 if (np == NULL) 945 return (EACCES); 946 947 *exflagsp = np->netc_exflags; 948 *credanonp = &np->netc_anon; 949 return (0); 950 } 951