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