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