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