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