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