1 /* $NetBSD: cd9660_vfsops.c,v 1.63 2008/06/28 01:34:05 rumble Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 9 * Support code is derived from software contributed to Berkeley 10 * by Atsushi Murai (amurai@spec.co.jp). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.63 2008/06/28 01:34:05 rumble Exp $"); 41 42 #if defined(_KERNEL_OPT) 43 #include "opt_compat_netbsd.h" 44 #endif 45 46 #include <sys/param.h> 47 #include <sys/sysctl.h> 48 #include <sys/systm.h> 49 #include <sys/namei.h> 50 #include <sys/proc.h> 51 #include <sys/kernel.h> 52 #include <sys/vnode.h> 53 #include <miscfs/genfs/genfs.h> 54 #include <miscfs/specfs/specdev.h> 55 #include <sys/mount.h> 56 #include <sys/buf.h> 57 #include <sys/file.h> 58 #include <sys/disklabel.h> 59 #include <sys/device.h> 60 #include <sys/ioctl.h> 61 #include <sys/cdio.h> 62 #include <sys/errno.h> 63 #include <sys/malloc.h> 64 #include <sys/pool.h> 65 #include <sys/stat.h> 66 #include <sys/conf.h> 67 #include <sys/dirent.h> 68 #include <sys/kauth.h> 69 #include <sys/module.h> 70 71 #include <fs/cd9660/iso.h> 72 #include <fs/cd9660/cd9660_extern.h> 73 #include <fs/cd9660/iso_rrip.h> 74 #include <fs/cd9660/cd9660_node.h> 75 #include <fs/cd9660/cd9660_mount.h> 76 77 MODULE(MODULE_CLASS_VFS, cd9660, NULL); 78 79 MALLOC_JUSTDEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure"); 80 81 static struct sysctllog *cd9660_sysctl_log; 82 83 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc; 84 extern const struct vnodeopv_desc cd9660_specop_opv_desc; 85 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc; 86 87 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = { 88 &cd9660_vnodeop_opv_desc, 89 &cd9660_specop_opv_desc, 90 &cd9660_fifoop_opv_desc, 91 NULL, 92 }; 93 94 struct vfsops cd9660_vfsops = { 95 MOUNT_CD9660, 96 sizeof (struct iso_args), 97 cd9660_mount, 98 cd9660_start, 99 cd9660_unmount, 100 cd9660_root, 101 (void *)eopnotsupp, 102 cd9660_statvfs, 103 cd9660_sync, 104 cd9660_vget, 105 cd9660_fhtovp, 106 cd9660_vptofh, 107 cd9660_init, 108 cd9660_reinit, 109 cd9660_done, 110 cd9660_mountroot, 111 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 112 vfs_stdextattrctl, 113 (void *)eopnotsupp, /* vfs_suspendctl */ 114 genfs_renamelock_enter, 115 genfs_renamelock_exit, 116 (void *)eopnotsupp, 117 cd9660_vnodeopv_descs, 118 0, /* refcount */ 119 { NULL, NULL } /* list */ 120 }; 121 122 static const struct genfs_ops cd9660_genfsops = { 123 .gop_size = genfs_size, 124 }; 125 126 /* 127 * Called by vfs_mountroot when iso is going to be mounted as root. 128 * 129 * Name is updated by mount(8) after booting. 130 */ 131 #define ROOTNAME "root_device" 132 133 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len); 134 static int iso_mountfs(struct vnode *devvp, struct mount *mp, 135 struct lwp *l, struct iso_args *argp); 136 137 static int 138 cd9660_modcmd(modcmd_t cmd, void *arg) 139 { 140 int error; 141 142 switch (cmd) { 143 case MODULE_CMD_INIT: 144 error = vfs_attach(&cd9660_vfsops); 145 if (error != 0) 146 break; 147 sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL, 148 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL, 149 NULL, 0, NULL, 0, 150 CTL_VFS, CTL_EOL); 151 sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL, 152 CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660", 153 SYSCTL_DESCR("ISO-9660 file system"), 154 NULL, 0, NULL, 0, 155 CTL_VFS, 14, CTL_EOL); 156 sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL, 157 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 158 CTLTYPE_INT, "utf8_joliet", 159 SYSCTL_DESCR("Encode Joliet filenames to UTF-8"), 160 NULL, 0, &cd9660_utf8_joliet, 0, 161 CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL); 162 /* 163 * XXX the "14" above could be dynamic, thereby eliminating 164 * one more instance of the "number to vfs" mapping problem, 165 * but "14" is the order as taken from sys/mount.h 166 */ 167 break; 168 case MODULE_CMD_FINI: 169 error = vfs_detach(&cd9660_vfsops); 170 if (error != 0) 171 break; 172 sysctl_teardown(&cd9660_sysctl_log); 173 break; 174 default: 175 error = ENOTTY; 176 break; 177 } 178 179 return (error); 180 } 181 182 int 183 cd9660_mountroot(void) 184 { 185 struct mount *mp; 186 struct lwp *l = curlwp; 187 int error; 188 struct iso_args args; 189 190 if (device_class(root_device) != DV_DISK) 191 return (ENODEV); 192 193 if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp)) 194 != 0) { 195 vrele(rootvp); 196 return (error); 197 } 198 199 args.flags = ISOFSMNT_ROOT; 200 if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) { 201 vfs_unbusy(mp, false, NULL); 202 vfs_destroy(mp); 203 return (error); 204 } 205 mutex_enter(&mountlist_lock); 206 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 207 mutex_exit(&mountlist_lock); 208 (void)cd9660_statvfs(mp, &mp->mnt_stat); 209 vfs_unbusy(mp, false, NULL); 210 return (0); 211 } 212 213 /* 214 * VFS Operations. 215 * 216 * mount system call 217 */ 218 int 219 cd9660_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 220 { 221 struct lwp *l = curlwp; 222 struct nameidata nd; 223 struct vnode *devvp; 224 struct iso_args *args = data; 225 int error; 226 struct iso_mnt *imp = VFSTOISOFS(mp); 227 228 if (*data_len < sizeof *args) 229 return EINVAL; 230 231 if (mp->mnt_flag & MNT_GETARGS) { 232 if (imp == NULL) 233 return EIO; 234 args->fspec = NULL; 235 args->flags = imp->im_flags; 236 *data_len = sizeof (*args); 237 return 0; 238 } 239 240 if ((mp->mnt_flag & MNT_RDONLY) == 0) 241 return (EROFS); 242 243 if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL) 244 return EINVAL; 245 246 /* 247 * Not an update, or updating the name: look up the name 248 * and verify that it refers to a sensible block device. 249 */ 250 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec); 251 if ((error = namei(&nd)) != 0) 252 return (error); 253 devvp = nd.ni_vp; 254 255 if (devvp->v_type != VBLK) { 256 vrele(devvp); 257 return ENOTBLK; 258 } 259 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 260 vrele(devvp); 261 return ENXIO; 262 } 263 /* 264 * If mount by non-root, then verify that user has necessary 265 * permissions on the device. 266 */ 267 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) { 268 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 269 error = VOP_ACCESS(devvp, VREAD, l->l_cred); 270 VOP_UNLOCK(devvp, 0); 271 if (error) { 272 vrele(devvp); 273 return (error); 274 } 275 } 276 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 277 error = VOP_OPEN(devvp, FREAD, FSCRED); 278 if (error) 279 goto fail; 280 error = iso_mountfs(devvp, mp, l, args); 281 if (error) { 282 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 283 (void)VOP_CLOSE(devvp, FREAD, NOCRED); 284 VOP_UNLOCK(devvp, 0); 285 goto fail; 286 } 287 } else { 288 vrele(devvp); 289 if (devvp != imp->im_devvp) 290 return (EINVAL); /* needs translation */ 291 } 292 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 293 mp->mnt_op->vfs_name, mp, l); 294 295 fail: 296 vrele(devvp); 297 return (error); 298 } 299 300 /* 301 * Make a mount point from a volume descriptor 302 */ 303 static int 304 iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len) 305 { 306 struct iso_primary_descriptor *pri; 307 int logical_block_size; 308 struct iso_directory_record *rootp; 309 310 pri = (struct iso_primary_descriptor *)bp->b_data; 311 312 logical_block_size = isonum_723 (pri->logical_block_size); 313 314 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE 315 || (logical_block_size & (logical_block_size - 1)) != 0) 316 return -1; 317 318 rootp = (struct iso_directory_record *)pri->root_directory_record; 319 320 isomp->logical_block_size = logical_block_size; 321 isomp->volume_space_size = isonum_733 (pri->volume_space_size); 322 memcpy(isomp->root, rootp, sizeof(isomp->root)); 323 isomp->root_extent = isonum_733 (rootp->extent); 324 isomp->root_size = isonum_733 (rootp->size); 325 isomp->im_joliet_level = 0; 326 327 isomp->im_bmask = logical_block_size - 1; 328 isomp->im_bshift = 0; 329 while ((1 << isomp->im_bshift) < isomp->logical_block_size) 330 isomp->im_bshift++; 331 332 if (ea_len != NULL) 333 *ea_len = isonum_711(rootp->ext_attr_length); 334 335 return 0; 336 } 337 338 /* 339 * Common code for mount and mountroot 340 */ 341 static int 342 iso_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, 343 struct iso_args *argp) 344 { 345 struct iso_mnt *isomp = (struct iso_mnt *)0; 346 struct buf *bp = NULL, *pribp = NULL, *supbp = NULL; 347 dev_t dev = devvp->v_rdev; 348 int error = EINVAL; 349 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 350 int iso_bsize; 351 int iso_blknum; 352 int joliet_level; 353 struct iso_volume_descriptor *vdp; 354 struct iso_supplementary_descriptor *sup; 355 int sess = 0; 356 int ext_attr_length; 357 struct disklabel label; 358 359 if (!ronly) 360 return EROFS; 361 362 /* Flush out any old buffers remaining from a previous use. */ 363 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0) 364 return (error); 365 366 /* This is the "logical sector size". The standard says this 367 * should be 2048 or the physical sector size on the device, 368 * whichever is greater. For now, we'll just use a constant. 369 */ 370 iso_bsize = ISO_DEFAULT_BLOCK_SIZE; 371 372 error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED); 373 if (!error && 374 label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) { 375 /* XXX more sanity checks? */ 376 sess = label.d_partitions[DISKPART(dev)].p_cdsession; 377 } else { 378 /* fallback to old method */ 379 error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED); 380 if (error) 381 sess = 0; /* never mind */ 382 } 383 #ifdef ISO_DEBUG 384 printf("isofs: session offset (part %d) %d\n", DISKPART(dev), sess); 385 #endif 386 387 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) { 388 if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize), 389 iso_bsize, NOCRED, 0, &bp)) != 0) 390 goto out; 391 392 vdp = (struct iso_volume_descriptor *)bp->b_data; 393 if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) { 394 error = EINVAL; 395 goto out; 396 } 397 398 switch (isonum_711(vdp->type)) { 399 case ISO_VD_PRIMARY: 400 if (pribp == NULL) { 401 pribp = bp; 402 bp = NULL; 403 } 404 break; 405 406 case ISO_VD_SUPPLEMENTARY: 407 if (supbp == NULL) { 408 supbp = bp; 409 bp = NULL; 410 } 411 break; 412 413 default: 414 break; 415 } 416 417 if (isonum_711 (vdp->type) == ISO_VD_END) { 418 brelse(bp, 0); 419 bp = NULL; 420 break; 421 } 422 423 if (bp != NULL) { 424 brelse(bp, 0); 425 bp = NULL; 426 } 427 } 428 429 if (pribp == NULL) { 430 error = EINVAL; 431 goto out; 432 } 433 434 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK); 435 memset(isomp, 0, sizeof *isomp); 436 if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) { 437 error = EINVAL; 438 goto out; 439 } 440 441 isomp->volume_space_size += sess; 442 443 brelse(pribp, BC_AGE); 444 pribp = NULL; 445 446 mp->mnt_data = isomp; 447 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev; 448 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660); 449 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 450 mp->mnt_stat.f_namemax = MAXNAMLEN; 451 mp->mnt_flag |= MNT_LOCAL; 452 mp->mnt_iflag |= IMNT_MPSAFE; 453 mp->mnt_dev_bshift = iso_bsize; 454 mp->mnt_fs_bshift = isomp->im_bshift; 455 isomp->im_mountp = mp; 456 isomp->im_dev = dev; 457 isomp->im_devvp = devvp; 458 459 /* Check the Rock Ridge Extension support */ 460 if (!(argp->flags & ISOFSMNT_NORRIP)) { 461 struct iso_directory_record *rootp; 462 463 if ((error = bread(isomp->im_devvp, 464 (isomp->root_extent + ext_attr_length) << 465 (isomp->im_bshift - DEV_BSHIFT), 466 isomp->logical_block_size, NOCRED, 467 0, &bp)) != 0) 468 goto out; 469 470 rootp = (struct iso_directory_record *)bp->b_data; 471 472 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) { 473 argp->flags |= ISOFSMNT_NORRIP; 474 } else { 475 argp->flags &= ~ISOFSMNT_GENS; 476 } 477 478 /* 479 * The contents are valid, 480 * but they will get reread as part of another vnode, so... 481 */ 482 brelse(bp, BC_AGE); 483 bp = NULL; 484 } 485 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS | 486 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS); 487 488 if (isomp->im_flags & ISOFSMNT_GENS) 489 isomp->iso_ftype = ISO_FTYPE_9660; 490 else if (isomp->im_flags & ISOFSMNT_NORRIP) { 491 isomp->iso_ftype = ISO_FTYPE_DEFAULT; 492 if (argp->flags & ISOFSMNT_NOCASETRANS) 493 isomp->im_flags |= ISOFSMNT_NOCASETRANS; 494 } else 495 isomp->iso_ftype = ISO_FTYPE_RRIP; 496 497 /* Check the Joliet Extension support */ 498 if ((argp->flags & ISOFSMNT_NORRIP) != 0 && 499 (argp->flags & ISOFSMNT_NOJOLIET) == 0 && 500 supbp != NULL) { 501 joliet_level = 0; 502 sup = (struct iso_supplementary_descriptor *)supbp->b_data; 503 504 if ((isonum_711(sup->flags) & 1) == 0) { 505 if (memcmp(sup->escape, "%/@", 3) == 0) 506 joliet_level = 1; 507 if (memcmp(sup->escape, "%/C", 3) == 0) 508 joliet_level = 2; 509 if (memcmp(sup->escape, "%/E", 3) == 0) 510 joliet_level = 3; 511 } 512 if (joliet_level != 0) { 513 if (iso_makemp(isomp, supbp, NULL) == -1) { 514 error = EINVAL; 515 goto out; 516 } 517 isomp->im_joliet_level = joliet_level; 518 } 519 } 520 521 if (supbp != NULL) { 522 brelse(supbp, 0); 523 supbp = NULL; 524 } 525 526 devvp->v_specmountpoint = mp; 527 528 return 0; 529 out: 530 if (bp) 531 brelse(bp, 0); 532 if (pribp) 533 brelse(pribp, 0); 534 if (supbp) 535 brelse(supbp, 0); 536 if (isomp) { 537 free(isomp, M_ISOFSMNT); 538 mp->mnt_data = NULL; 539 } 540 return error; 541 } 542 543 /* 544 * Make a filesystem operational. 545 * Nothing to do at the moment. 546 */ 547 /* ARGSUSED */ 548 int 549 cd9660_start(struct mount *mp, int flags) 550 { 551 return 0; 552 } 553 554 /* 555 * unmount system call 556 */ 557 int 558 cd9660_unmount(struct mount *mp, int mntflags) 559 { 560 struct iso_mnt *isomp; 561 int error, flags = 0; 562 563 if (mntflags & MNT_FORCE) 564 flags |= FORCECLOSE; 565 if ((error = vflush(mp, NULLVP, flags)) != 0) 566 return (error); 567 568 isomp = VFSTOISOFS(mp); 569 570 if (isomp->im_devvp->v_type != VBAD) 571 isomp->im_devvp->v_specmountpoint = NULL; 572 573 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY); 574 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED); 575 vput(isomp->im_devvp); 576 free(isomp, M_ISOFSMNT); 577 mp->mnt_data = NULL; 578 mp->mnt_flag &= ~MNT_LOCAL; 579 return (error); 580 } 581 582 /* 583 * Return root of a filesystem 584 */ 585 int 586 cd9660_root(struct mount *mp, struct vnode **vpp) 587 { 588 struct iso_mnt *imp = VFSTOISOFS(mp); 589 struct iso_directory_record *dp = 590 (struct iso_directory_record *)imp->root; 591 ino_t ino = isodirino(dp, imp); 592 593 /* 594 * With RRIP we must use the `.' entry of the root directory. 595 * Simply tell vget, that it's a relocated directory. 596 */ 597 return (cd9660_vget_internal(mp, ino, vpp, 598 imp->iso_ftype == ISO_FTYPE_RRIP, dp)); 599 } 600 601 /* 602 * Get file system statistics. 603 */ 604 int 605 cd9660_statvfs(struct mount *mp, struct statvfs *sbp) 606 { 607 struct iso_mnt *isomp; 608 609 isomp = VFSTOISOFS(mp); 610 611 sbp->f_bsize = isomp->logical_block_size; 612 sbp->f_frsize = sbp->f_bsize; 613 sbp->f_iosize = sbp->f_bsize; /* XXX */ 614 sbp->f_blocks = isomp->volume_space_size; 615 sbp->f_bfree = 0; /* total free blocks */ 616 sbp->f_bavail = 0; /* blocks free for non superuser */ 617 sbp->f_bresvd = 0; /* total reserved blocks */ 618 sbp->f_files = 0; /* total files */ 619 sbp->f_ffree = 0; /* free file nodes */ 620 sbp->f_favail = 0; /* free file nodes for non superuser */ 621 sbp->f_fresvd = 0; /* reserved file nodes */ 622 copy_statvfs_info(sbp, mp); 623 /* Use the first spare for flags: */ 624 sbp->f_spare[0] = isomp->im_flags; 625 return 0; 626 } 627 628 /* ARGSUSED */ 629 int 630 cd9660_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 631 { 632 return 0; 633 } 634 635 /* 636 * File handle to vnode 637 * 638 * Have to be really careful about stale file handles: 639 * - check that the inode number is in range 640 * - call iget() to get the locked inode 641 * - check for an unallocated inode (i_mode == 0) 642 * - check that the generation number matches 643 */ 644 645 struct ifid { 646 ushort ifid_len; 647 ushort ifid_pad; 648 int ifid_ino; 649 long ifid_start; 650 }; 651 652 /* ARGSUSED */ 653 int 654 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 655 { 656 struct ifid ifh; 657 struct iso_node *ip; 658 struct vnode *nvp; 659 int error; 660 661 if (fhp->fid_len != sizeof(ifh)) 662 return EINVAL; 663 664 memcpy(&ifh, fhp, sizeof(ifh)); 665 #ifdef ISOFS_DBG 666 printf("fhtovp: ino %d, start %ld\n", 667 ifh.ifid_ino, ifh.ifid_start); 668 #endif 669 670 if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) { 671 *vpp = NULLVP; 672 return (error); 673 } 674 ip = VTOI(nvp); 675 if (ip->inode.iso_mode == 0) { 676 vput(nvp); 677 *vpp = NULLVP; 678 return (ESTALE); 679 } 680 *vpp = nvp; 681 return (0); 682 } 683 684 int 685 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 686 { 687 688 /* 689 * XXXX 690 * It would be nice if we didn't always set the `relocated' flag 691 * and force the extra read, but I don't want to think about fixing 692 * that right now. 693 */ 694 return (cd9660_vget_internal(mp, ino, vpp, 695 #if 0 696 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP, 697 #else 698 0, 699 #endif 700 NULL)); 701 } 702 703 int 704 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp, 705 int relocated, struct iso_directory_record *isodir) 706 { 707 struct iso_mnt *imp; 708 struct iso_node *ip; 709 struct buf *bp; 710 struct vnode *vp; 711 dev_t dev; 712 int error; 713 714 imp = VFSTOISOFS(mp); 715 dev = imp->im_dev; 716 717 retry: 718 if ((*vpp = cd9660_ihashget(dev, ino, LK_EXCLUSIVE)) != NULLVP) 719 return (0); 720 721 /* Allocate a new vnode/iso_node. */ 722 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) { 723 *vpp = NULLVP; 724 return (error); 725 } 726 ip = pool_get(&cd9660_node_pool, PR_WAITOK); 727 728 /* 729 * If someone beat us to it, put back the freshly allocated 730 * vnode/inode pair and retry. 731 */ 732 mutex_enter(&cd9660_hashlock); 733 if (cd9660_ihashget(dev, ino, 0) != NULL) { 734 mutex_exit(&cd9660_hashlock); 735 ungetnewvnode(vp); 736 pool_put(&cd9660_node_pool, ip); 737 goto retry; 738 } 739 740 memset(ip, 0, sizeof(struct iso_node)); 741 vp->v_data = ip; 742 ip->i_vnode = vp; 743 ip->i_dev = dev; 744 ip->i_number = ino; 745 ip->i_mnt = imp; 746 ip->i_devvp = imp->im_devvp; 747 genfs_node_init(vp, &cd9660_genfsops); 748 749 /* 750 * Put it onto its hash chain and lock it so that other requests for 751 * this inode will block if they arrive while we are sleeping waiting 752 * for old data structures to be purged or for the contents of the 753 * disk portion of this inode to be read. 754 */ 755 cd9660_ihashins(ip); 756 mutex_exit(&cd9660_hashlock); 757 758 if (isodir == 0) { 759 int lbn, off; 760 761 lbn = lblkno(imp, ino); 762 if (lbn >= imp->volume_space_size) { 763 vput(vp); 764 printf("fhtovp: lbn exceed volume space %d\n", lbn); 765 return (ESTALE); 766 } 767 768 off = blkoff(imp, ino); 769 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) { 770 vput(vp); 771 printf("fhtovp: crosses block boundary %d\n", 772 off + ISO_DIRECTORY_RECORD_SIZE); 773 return (ESTALE); 774 } 775 776 error = bread(imp->im_devvp, 777 lbn << (imp->im_bshift - DEV_BSHIFT), 778 imp->logical_block_size, NOCRED, 0, &bp); 779 if (error) { 780 vput(vp); 781 brelse(bp, 0); 782 printf("fhtovp: bread error %d\n",error); 783 return (error); 784 } 785 isodir = (struct iso_directory_record *)((char *)bp->b_data + off); 786 787 if (off + isonum_711(isodir->length) > 788 imp->logical_block_size) { 789 vput(vp); 790 if (bp != 0) 791 brelse(bp, 0); 792 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n", 793 off +isonum_711(isodir->length), off, 794 isonum_711(isodir->length)); 795 return (ESTALE); 796 } 797 798 #if 0 799 if (isonum_733(isodir->extent) + 800 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) { 801 if (bp != 0) 802 brelse(bp, 0); 803 printf("fhtovp: file start miss %d vs %d\n", 804 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length), 805 ifhp->ifid_start); 806 return (ESTALE); 807 } 808 #endif 809 } else 810 bp = 0; 811 812 VREF(ip->i_devvp); 813 814 if (relocated) { 815 /* 816 * On relocated directories we must 817 * read the `.' entry out of a dir. 818 */ 819 ip->iso_start = ino >> imp->im_bshift; 820 if (bp != 0) 821 brelse(bp, 0); 822 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) { 823 vput(vp); 824 return (error); 825 } 826 isodir = (struct iso_directory_record *)bp->b_data; 827 } 828 829 ip->iso_extent = isonum_733(isodir->extent); 830 ip->i_size = isonum_733(isodir->size); 831 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent; 832 833 /* 834 * Setup time stamp, attribute 835 */ 836 vp->v_type = VNON; 837 switch (imp->iso_ftype) { 838 default: /* ISO_FTYPE_9660 */ 839 { 840 struct buf *bp2; 841 int off; 842 if ((imp->im_flags & ISOFSMNT_EXTATT) 843 && (off = isonum_711(isodir->ext_attr_length))) 844 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), 845 NULL, &bp2); 846 else 847 bp2 = NULL; 848 cd9660_defattr(isodir, ip, bp2); 849 cd9660_deftstamp(isodir, ip, bp2); 850 if (bp2) 851 brelse(bp2, 0); 852 break; 853 } 854 case ISO_FTYPE_RRIP: 855 cd9660_rrip_analyze(isodir, ip, imp); 856 break; 857 } 858 859 if (bp != 0) 860 brelse(bp, 0); 861 862 /* 863 * Initialize the associated vnode 864 */ 865 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) { 866 case VFIFO: 867 vp->v_op = cd9660_fifoop_p; 868 break; 869 case VCHR: 870 case VBLK: 871 /* 872 * if device, look at device number table for translation 873 */ 874 vp->v_op = cd9660_specop_p; 875 spec_node_init(vp, ip->inode.iso_rdev); 876 break; 877 case VLNK: 878 case VNON: 879 case VSOCK: 880 case VDIR: 881 case VBAD: 882 break; 883 case VREG: 884 uvm_vnp_setsize(vp, ip->i_size); 885 break; 886 } 887 888 if (vp->v_type != VREG) 889 uvm_vnp_setsize(vp, 0); 890 891 if (ip->iso_extent == imp->root_extent) 892 vp->v_vflag |= VV_ROOT; 893 894 /* 895 * XXX need generation number? 896 */ 897 898 *vpp = vp; 899 return (0); 900 } 901 902 /* 903 * Vnode pointer to File handle 904 */ 905 /* ARGSUSED */ 906 int 907 cd9660_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 908 { 909 struct iso_node *ip = VTOI(vp); 910 struct ifid ifh; 911 912 if (*fh_size < sizeof(struct ifid)) { 913 *fh_size = sizeof(struct ifid); 914 return E2BIG; 915 } 916 *fh_size = sizeof(struct ifid); 917 918 memset(&ifh, 0, sizeof(ifh)); 919 ifh.ifid_len = sizeof(struct ifid); 920 ifh.ifid_ino = ip->i_number; 921 ifh.ifid_start = ip->iso_start; 922 memcpy(fhp, &ifh, sizeof(ifh)); 923 924 #ifdef ISOFS_DBG 925 printf("vptofh: ino %d, start %ld\n", 926 ifh.ifid_ino,ifh.ifid_start); 927 #endif 928 return 0; 929 } 930