Home | History | Annotate | Line # | Download | only in cd9660
cd9660_vfsops.c revision 1.41
      1 /*	$NetBSD: cd9660_vfsops.c,v 1.41 2007/06/30 09:37:55 pooka 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.41 2007/06/30 09:37:55 pooka 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_JUSTDEFINE(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 #ifdef	ISODEVMAP
    549 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
    550 		iso_dunmap(isomp->im_dev);
    551 #endif
    552 
    553 	if (isomp->im_devvp->v_type != VBAD)
    554 		isomp->im_devvp->v_specmountpoint = NULL;
    555 
    556 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
    557 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, l);
    558 	vput(isomp->im_devvp);
    559 	free(isomp, M_ISOFSMNT);
    560 	mp->mnt_data = NULL;
    561 	mp->mnt_flag &= ~MNT_LOCAL;
    562 	return (error);
    563 }
    564 
    565 /*
    566  * Return root of a filesystem
    567  */
    568 int
    569 cd9660_root(mp, vpp)
    570 	struct mount *mp;
    571 	struct vnode **vpp;
    572 {
    573 	struct iso_mnt *imp = VFSTOISOFS(mp);
    574 	struct iso_directory_record *dp =
    575 	    (struct iso_directory_record *)imp->root;
    576 	ino_t ino = isodirino(dp, imp);
    577 
    578 	/*
    579 	 * With RRIP we must use the `.' entry of the root directory.
    580 	 * Simply tell vget, that it's a relocated directory.
    581 	 */
    582 	return (cd9660_vget_internal(mp, ino, vpp,
    583 				     imp->iso_ftype == ISO_FTYPE_RRIP, dp));
    584 }
    585 
    586 /*
    587  * Do operations associated with quotas, not supported
    588  */
    589 /* ARGSUSED */
    590 int
    591 cd9660_quotactl(
    592     struct mount *mp,
    593     int cmd,
    594     uid_t uid,
    595     void *arg,
    596     struct lwp *l)
    597 {
    598 
    599 	return (EOPNOTSUPP);
    600 }
    601 
    602 /*
    603  * Get file system statistics.
    604  */
    605 int
    606 cd9660_statvfs(
    607     struct mount *mp,
    608     struct statvfs *sbp,
    609     struct lwp *l)
    610 {
    611 	struct iso_mnt *isomp;
    612 
    613 	isomp = VFSTOISOFS(mp);
    614 
    615 	sbp->f_bsize = isomp->logical_block_size;
    616 	sbp->f_frsize = sbp->f_bsize;
    617 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
    618 	sbp->f_blocks = isomp->volume_space_size;
    619 	sbp->f_bfree = 0; /* total free blocks */
    620 	sbp->f_bavail = 0; /* blocks free for non superuser */
    621 	sbp->f_bresvd = 0; /* total reserved blocks */
    622 	sbp->f_files =  0; /* total files */
    623 	sbp->f_ffree = 0; /* free file nodes */
    624 	sbp->f_favail = 0; /* free file nodes for non superuser */
    625 	sbp->f_fresvd = 0; /* reserved file nodes */
    626 	copy_statvfs_info(sbp, mp);
    627 	/* Use the first spare for flags: */
    628 	sbp->f_spare[0] = isomp->im_flags;
    629 	return 0;
    630 }
    631 
    632 /* ARGSUSED */
    633 int
    634 cd9660_sync(
    635     struct mount *mp,
    636     int waitfor,
    637     kauth_cred_t cred,
    638     struct lwp *l)
    639 {
    640 	return (0);
    641 }
    642 
    643 /*
    644  * File handle to vnode
    645  *
    646  * Have to be really careful about stale file handles:
    647  * - check that the inode number is in range
    648  * - call iget() to get the locked inode
    649  * - check for an unallocated inode (i_mode == 0)
    650  * - check that the generation number matches
    651  */
    652 
    653 struct ifid {
    654 	ushort	ifid_len;
    655 	ushort	ifid_pad;
    656 	int	ifid_ino;
    657 	long	ifid_start;
    658 };
    659 
    660 /* ARGSUSED */
    661 int
    662 cd9660_fhtovp(mp, fhp, vpp)
    663 	struct mount *mp;
    664 	struct fid *fhp;
    665 	struct vnode **vpp;
    666 {
    667 	struct ifid ifh;
    668 	struct iso_node *ip;
    669 	struct vnode *nvp;
    670 	int error;
    671 
    672 	if (fhp->fid_len != sizeof(ifh))
    673 		return EINVAL;
    674 
    675 	memcpy(&ifh, fhp, sizeof(ifh));
    676 #ifdef	ISOFS_DBG
    677 	printf("fhtovp: ino %d, start %ld\n",
    678 	    ifh.ifid_ino, ifh.ifid_start);
    679 #endif
    680 
    681 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
    682 		*vpp = NULLVP;
    683 		return (error);
    684 	}
    685 	ip = VTOI(nvp);
    686 	if (ip->inode.iso_mode == 0) {
    687 		vput(nvp);
    688 		*vpp = NULLVP;
    689 		return (ESTALE);
    690 	}
    691 	*vpp = nvp;
    692 	return (0);
    693 }
    694 
    695 int
    696 cd9660_vget(mp, ino, vpp)
    697 	struct mount *mp;
    698 	ino_t ino;
    699 	struct vnode **vpp;
    700 {
    701 
    702 	/*
    703 	 * XXXX
    704 	 * It would be nice if we didn't always set the `relocated' flag
    705 	 * and force the extra read, but I don't want to think about fixing
    706 	 * that right now.
    707 	 */
    708 	return (cd9660_vget_internal(mp, ino, vpp,
    709 #if 0
    710 				     VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
    711 #else
    712 				     0,
    713 #endif
    714 				     NULL));
    715 }
    716 
    717 int
    718 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
    719 	struct mount *mp;
    720 	ino_t ino;
    721 	struct vnode **vpp;
    722 	int relocated;
    723 	struct iso_directory_record *isodir;
    724 {
    725 	struct iso_mnt *imp;
    726 	struct iso_node *ip;
    727 #ifdef ISODEVMAP
    728 	struct iso_dnode *dp;
    729 #endif
    730 	struct buf *bp;
    731 	struct vnode *vp, *nvp;
    732 	dev_t dev;
    733 	int error;
    734 
    735 	imp = VFSTOISOFS(mp);
    736 	dev = imp->im_dev;
    737 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
    738 		return (0);
    739 
    740 	/* Allocate a new vnode/iso_node. */
    741 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
    742 		*vpp = NULLVP;
    743 		return (error);
    744 	}
    745 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
    746 	memset(ip, 0, sizeof(struct iso_node));
    747 	vp->v_data = ip;
    748 	ip->i_vnode = vp;
    749 	ip->i_dev = dev;
    750 	ip->i_number = ino;
    751 
    752 	/*
    753 	 * Put it onto its hash chain and lock it so that other requests for
    754 	 * this inode will block if they arrive while we are sleeping waiting
    755 	 * for old data structures to be purged or for the contents of the
    756 	 * disk portion of this inode to be read.
    757 	 */
    758 	cd9660_ihashins(ip);
    759 
    760 	if (isodir == 0) {
    761 		int lbn, off;
    762 
    763 		lbn = lblkno(imp, ino);
    764 		if (lbn >= imp->volume_space_size) {
    765 			vput(vp);
    766 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
    767 			return (ESTALE);
    768 		}
    769 
    770 		off = blkoff(imp, ino);
    771 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
    772 			vput(vp);
    773 			printf("fhtovp: crosses block boundary %d\n",
    774 			    off + ISO_DIRECTORY_RECORD_SIZE);
    775 			return (ESTALE);
    776 		}
    777 
    778 		error = bread(imp->im_devvp,
    779 			      lbn << (imp->im_bshift - DEV_BSHIFT),
    780 			      imp->logical_block_size, NOCRED, &bp);
    781 		if (error) {
    782 			vput(vp);
    783 			brelse(bp);
    784 			printf("fhtovp: bread error %d\n",error);
    785 			return (error);
    786 		}
    787 		isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
    788 
    789 		if (off + isonum_711(isodir->length) >
    790 		    imp->logical_block_size) {
    791 			vput(vp);
    792 			if (bp != 0)
    793 				brelse(bp);
    794 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
    795 			    off +isonum_711(isodir->length), off,
    796 			    isonum_711(isodir->length));
    797 			return (ESTALE);
    798 		}
    799 
    800 #if 0
    801 		if (isonum_733(isodir->extent) +
    802 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
    803 			if (bp != 0)
    804 				brelse(bp);
    805 			printf("fhtovp: file start miss %d vs %d\n",
    806 			    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
    807 			    ifhp->ifid_start);
    808 			return (ESTALE);
    809 		}
    810 #endif
    811 	} else
    812 		bp = 0;
    813 
    814 	ip->i_mnt = imp;
    815 	ip->i_devvp = imp->im_devvp;
    816 	VREF(ip->i_devvp);
    817 
    818 	if (relocated) {
    819 		/*
    820 		 * On relocated directories we must
    821 		 * read the `.' entry out of a dir.
    822 		 */
    823 		ip->iso_start = ino >> imp->im_bshift;
    824 		if (bp != 0)
    825 			brelse(bp);
    826 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
    827 			vput(vp);
    828 			return (error);
    829 		}
    830 		isodir = (struct iso_directory_record *)bp->b_data;
    831 	}
    832 
    833 	ip->iso_extent = isonum_733(isodir->extent);
    834 	ip->i_size = isonum_733(isodir->size);
    835 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
    836 
    837 	/*
    838 	 * Setup time stamp, attribute
    839 	 */
    840 	vp->v_type = VNON;
    841 	switch (imp->iso_ftype) {
    842 	default:	/* ISO_FTYPE_9660 */
    843 	    {
    844 		struct buf *bp2;
    845 		int off;
    846 		if ((imp->im_flags & ISOFSMNT_EXTATT)
    847 		    && (off = isonum_711(isodir->ext_attr_length)))
    848 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
    849 			    NULL, &bp2);
    850 		else
    851 			bp2 = NULL;
    852 		cd9660_defattr(isodir, ip, bp2);
    853 		cd9660_deftstamp(isodir, ip, bp2);
    854 		if (bp2)
    855 			brelse(bp2);
    856 		break;
    857 	    }
    858 	case ISO_FTYPE_RRIP:
    859 		cd9660_rrip_analyze(isodir, ip, imp);
    860 		break;
    861 	}
    862 
    863 	if (bp != 0)
    864 		brelse(bp);
    865 
    866 	/*
    867 	 * Initialize the associated vnode
    868 	 */
    869 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
    870 	case VFIFO:
    871 		vp->v_op = cd9660_fifoop_p;
    872 		break;
    873 	case VCHR:
    874 	case VBLK:
    875 		/*
    876 		 * if device, look at device number table for translation
    877 		 */
    878 #ifdef	ISODEVMAP
    879 		if ((dp = iso_dmap(dev, ino, 0)) != NULL)
    880 			ip->inode.iso_rdev = dp->d_dev;
    881 #endif
    882 		vp->v_op = cd9660_specop_p;
    883 		if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
    884 			/*
    885 			 * Discard unneeded vnode, but save its iso_node.
    886 			 * Note that the lock is carried over in the iso_node
    887 			 * to the replacement vnode.
    888 			 */
    889 			nvp->v_data = vp->v_data;
    890 			vp->v_data = NULL;
    891 			VOP_UNLOCK(vp, 0);
    892 			vp->v_op = spec_vnodeop_p;
    893 			vrele(vp);
    894 			vgone(vp);
    895 			lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
    896 			/*
    897 			 * Reinitialize aliased inode.
    898 			 */
    899 			vp = nvp;
    900 			ip->i_vnode = vp;
    901 		}
    902 		break;
    903 	case VLNK:
    904 	case VNON:
    905 	case VSOCK:
    906 	case VDIR:
    907 	case VBAD:
    908 		break;
    909 	case VREG:
    910 		uvm_vnp_setsize(vp, ip->i_size);
    911 		break;
    912 	}
    913 
    914 	if (ip->iso_extent == imp->root_extent)
    915 		vp->v_flag |= VROOT;
    916 
    917 	/*
    918 	 * XXX need generation number?
    919 	 */
    920 
    921 	genfs_node_init(vp, &cd9660_genfsops);
    922 	*vpp = vp;
    923 	return (0);
    924 }
    925 
    926 /*
    927  * Vnode pointer to File handle
    928  */
    929 /* ARGSUSED */
    930 int
    931 cd9660_vptofh(vp, fhp, fh_size)
    932 	struct vnode *vp;
    933 	struct fid *fhp;
    934 	size_t *fh_size;
    935 {
    936 	struct iso_node *ip = VTOI(vp);
    937 	struct ifid ifh;
    938 
    939 	if (*fh_size < sizeof(struct ifid)) {
    940 		*fh_size = sizeof(struct ifid);
    941 		return E2BIG;
    942 	}
    943 	*fh_size = sizeof(struct ifid);
    944 
    945 	memset(&ifh, 0, sizeof(ifh));
    946 	ifh.ifid_len = sizeof(struct ifid);
    947 	ifh.ifid_ino = ip->i_number;
    948 	ifh.ifid_start = ip->iso_start;
    949 	memcpy(fhp, &ifh, sizeof(ifh));
    950 
    951 #ifdef	ISOFS_DBG
    952 	printf("vptofh: ino %d, start %ld\n",
    953 	    ifh.ifid_ino,ifh.ifid_start);
    954 #endif
    955 	return 0;
    956 }
    957 
    958 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
    959 {
    960 
    961 	sysctl_createv(clog, 0, NULL, NULL,
    962 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
    963 		       NULL, 0, NULL, 0,
    964 		       CTL_VFS, CTL_EOL);
    965 	sysctl_createv(clog, 0, NULL, NULL,
    966 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
    967 		       SYSCTL_DESCR("ISO-9660 file system"),
    968 		       NULL, 0, NULL, 0,
    969 		       CTL_VFS, 14, CTL_EOL);
    970 
    971 	sysctl_createv(clog, 0, NULL, NULL,
    972 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    973 		       CTLTYPE_INT, "utf8_joliet",
    974 		       SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
    975 		       NULL, 0, &cd9660_utf8_joliet, 0,
    976 		       CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
    977 
    978 }
    979