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