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