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