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