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