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