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