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