Home | History | Annotate | Line # | Download | only in cd9660
cd9660_vfsops.c revision 1.4
      1 /*	$NetBSD: cd9660_vfsops.c,v 1.4 2003/04/03 15:37:55 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.4 2003/04/03 15:37:55 christos Exp $");
     45 
     46 #if defined(_KERNEL_OPT)
     47 #include "opt_compat_netbsd.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/namei.h>
     53 #include <sys/proc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/vnode.h>
     56 #include <miscfs/specfs/specdev.h>
     57 #include <sys/mount.h>
     58 #include <sys/buf.h>
     59 #include <sys/file.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/device.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/cdio.h>
     64 #include <sys/errno.h>
     65 #include <sys/malloc.h>
     66 #include <sys/pool.h>
     67 #include <sys/stat.h>
     68 #include <sys/conf.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_DEFINE(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 	cd9660_mount,
     92 	cd9660_start,
     93 	cd9660_unmount,
     94 	cd9660_root,
     95 	cd9660_quotactl,
     96 	cd9660_statfs,
     97 	cd9660_sync,
     98 	cd9660_vget,
     99 	cd9660_fhtovp,
    100 	cd9660_vptofh,
    101 	cd9660_init,
    102 	cd9660_reinit,
    103 	cd9660_done,
    104 	cd9660_sysctl,
    105 	cd9660_mountroot,
    106 	cd9660_check_export,
    107 	cd9660_vnodeopv_descs,
    108 };
    109 
    110 struct genfs_ops cd9660_genfsops = {
    111 	genfs_size,
    112 };
    113 
    114 /*
    115  * Called by vfs_mountroot when iso is going to be mounted as root.
    116  *
    117  * Name is updated by mount(8) after booting.
    118  */
    119 #define ROOTNAME	"root_device"
    120 
    121 static int iso_makemp __P((struct iso_mnt *isomp, struct buf *bp, int *ea_len));
    122 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
    123 		struct proc *p, struct iso_args *argp));
    124 
    125 int
    126 cd9660_mountroot()
    127 {
    128 	struct mount *mp;
    129 	struct proc *p = curproc;	/* XXX */
    130 	int error;
    131 	struct iso_args args;
    132 
    133 	if (root_device->dv_class != DV_DISK)
    134 		return (ENODEV);
    135 
    136 	/*
    137 	 * Get vnodes for swapdev and rootdev.
    138 	 */
    139 	if (bdevvp(rootdev, &rootvp))
    140 		panic("cd9660_mountroot: can't setup rootvp");
    141 
    142 	if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
    143 			!= 0) {
    144 		vrele(rootvp);
    145 		return (error);
    146 	}
    147 
    148 	args.flags = ISOFSMNT_ROOT;
    149 	if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0) {
    150 		mp->mnt_op->vfs_refcount--;
    151 		vfs_unbusy(mp);
    152 		free(mp, M_MOUNT);
    153 		vrele(rootvp);
    154 		return (error);
    155 	}
    156 	simple_lock(&mountlist_slock);
    157 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    158 	simple_unlock(&mountlist_slock);
    159 	(void)cd9660_statfs(mp, &mp->mnt_stat, p);
    160 	vfs_unbusy(mp);
    161 	inittodr(0);
    162 	return (0);
    163 }
    164 
    165 /*
    166  * VFS Operations.
    167  *
    168  * mount system call
    169  */
    170 int
    171 cd9660_mount(mp, path, data, ndp, p)
    172 	struct mount *mp;
    173 	const char *path;
    174 	void *data;
    175 	struct nameidata *ndp;
    176 	struct proc *p;
    177 {
    178 	struct vnode *devvp;
    179 	struct iso_args args;
    180 	size_t size;
    181 	int error;
    182 	struct iso_mnt *imp = NULL;
    183 
    184 	if (mp->mnt_flag & MNT_GETARGS) {
    185 		imp = VFSTOISOFS(mp);
    186 		if (imp == NULL)
    187 			return EIO;
    188 		args.fspec = NULL;
    189 		args.flags = imp->im_flags;
    190 		vfs_showexport(mp, &args.export, &imp->im_export);
    191 		return copyout(&args, data, sizeof(args));
    192 	}
    193 	error = copyin(data, &args, sizeof (struct iso_args));
    194 	if (error)
    195 		return (error);
    196 
    197 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    198 		return (EROFS);
    199 
    200 	/*
    201 	 * If updating, check whether changing from read-only to
    202 	 * read/write; if there is no device name, that's all we do.
    203 	 */
    204 	if (mp->mnt_flag & MNT_UPDATE) {
    205 		imp = VFSTOISOFS(mp);
    206 		if (args.fspec == 0)
    207 			return (vfs_export(mp, &imp->im_export, &args.export));
    208 	}
    209 	/*
    210 	 * Not an update, or updating the name: look up the name
    211 	 * and verify that it refers to a sensible block device.
    212 	 */
    213 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    214 	if ((error = namei(ndp)) != 0)
    215 		return (error);
    216 	devvp = ndp->ni_vp;
    217 
    218 	if (devvp->v_type != VBLK) {
    219 		vrele(devvp);
    220 		return ENOTBLK;
    221 	}
    222 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    223 		vrele(devvp);
    224 		return ENXIO;
    225 	}
    226 	/*
    227 	 * If mount by non-root, then verify that user has necessary
    228 	 * permissions on the device.
    229 	 */
    230 	if (p->p_ucred->cr_uid != 0) {
    231 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    232 		error = VOP_ACCESS(devvp, VREAD, p->p_ucred, p);
    233 		VOP_UNLOCK(devvp, 0);
    234 		if (error) {
    235 			vrele(devvp);
    236 			return (error);
    237 		}
    238 	}
    239 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    240 		error = iso_mountfs(devvp, mp, p, &args);
    241 	else {
    242 		if (devvp != imp->im_devvp)
    243 			error = EINVAL;	/* needs translation */
    244 		else
    245 			vrele(devvp);
    246 	}
    247 	if (error) {
    248 		vrele(devvp);
    249 		return error;
    250 	}
    251 	imp = VFSTOISOFS(mp);
    252 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    253 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    254 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    255 	    &size);
    256 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    257 	return 0;
    258 }
    259 
    260 /*
    261  * Make a mount point from a volume descriptor
    262  */
    263 static int
    264 iso_makemp(isomp, bp, ea_len)
    265 	struct iso_mnt *isomp;
    266 	struct buf *bp;
    267 	int *ea_len;
    268 {
    269 	struct iso_primary_descriptor *pri;
    270 	int logical_block_size;
    271 	struct iso_directory_record *rootp;
    272 
    273 	pri = (struct iso_primary_descriptor *)bp->b_data;
    274 
    275 	logical_block_size = isonum_723 (pri->logical_block_size);
    276 
    277 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
    278 	    || (logical_block_size & (logical_block_size - 1)) != 0)
    279 		return -1;
    280 
    281 	rootp = (struct iso_directory_record *)pri->root_directory_record;
    282 
    283 	isomp->logical_block_size = logical_block_size;
    284 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
    285 	memcpy(isomp->root, rootp, sizeof(isomp->root));
    286 	isomp->root_extent = isonum_733 (rootp->extent);
    287 	isomp->root_size = isonum_733 (rootp->size);
    288 	isomp->im_joliet_level = 0;
    289 
    290 	isomp->im_bmask = logical_block_size - 1;
    291 	isomp->im_bshift = 0;
    292 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
    293 		isomp->im_bshift++;
    294 
    295 	if (ea_len != NULL)
    296 		*ea_len = isonum_711(rootp->ext_attr_length);
    297 
    298 	return 0;
    299 }
    300 
    301 /*
    302  * Common code for mount and mountroot
    303  */
    304 static int
    305 iso_mountfs(devvp, mp, p, argp)
    306 	struct vnode *devvp;
    307 	struct mount *mp;
    308 	struct proc *p;
    309 	struct iso_args *argp;
    310 {
    311 	struct iso_mnt *isomp = (struct iso_mnt *)0;
    312 	struct buf *bp = NULL, *pribp = NULL, *supbp = NULL;
    313 	dev_t dev = devvp->v_rdev;
    314 	int error = EINVAL;
    315 	int needclose = 0;
    316 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    317 	int iso_bsize;
    318 	int iso_blknum;
    319 	int joliet_level;
    320 	struct iso_volume_descriptor *vdp;
    321 	struct iso_supplementary_descriptor *sup;
    322 	int sess = 0;
    323 	int ext_attr_length;
    324 	struct disklabel label;
    325 
    326 	if (!ronly)
    327 		return EROFS;
    328 
    329 	/*
    330 	 * Disallow multiple mounts of the same device.
    331 	 * Disallow mounting of a device that is currently in use
    332 	 * (except for root, which might share swap device for miniroot).
    333 	 * Flush out any old buffers remaining from a previous use.
    334 	 */
    335 	if ((error = vfs_mountedon(devvp)) != 0)
    336 		return error;
    337 	if (vcount(devvp) > 1 && devvp != rootvp)
    338 		return EBUSY;
    339 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    340 		return (error);
    341 
    342 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    343 	if (error)
    344 		return error;
    345 	needclose = 1;
    346 
    347 	/* This is the "logical sector size".  The standard says this
    348 	 * should be 2048 or the physical sector size on the device,
    349 	 * whichever is greater.  For now, we'll just use a constant.
    350 	 */
    351 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
    352 
    353 	error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED, p);
    354 	if (!error &&
    355 	    label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) {
    356 		/* XXX more sanity checks? */
    357 		sess = label.d_partitions[DISKPART(dev)].p_cdsession;
    358 	} else {
    359 		/* fallback to old method */
    360 		error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED, p);
    361 		if (error)
    362 			sess = 0;	/* never mind */
    363 	}
    364 #ifdef ISO_DEBUG
    365 	printf("isofs: session offset (part %d) %d\n", DISKPART(dev), sess);
    366 #endif
    367 
    368 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
    369 		if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
    370 				   iso_bsize, NOCRED, &bp)) != 0)
    371 			goto out;
    372 
    373 		vdp = (struct iso_volume_descriptor *)bp->b_data;
    374 		if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
    375 			error = EINVAL;
    376 			goto out;
    377 		}
    378 
    379 		switch (isonum_711(vdp->type)) {
    380 		case ISO_VD_PRIMARY:
    381 			if (pribp == NULL) {
    382 				pribp = bp;
    383 				bp = NULL;
    384 			}
    385 			break;
    386 
    387 		case ISO_VD_SUPPLEMENTARY:
    388 			if (supbp == NULL) {
    389 				supbp = bp;
    390 				bp = NULL;
    391 			}
    392 			break;
    393 
    394 		default:
    395 			break;
    396 		}
    397 
    398 		if (isonum_711 (vdp->type) == ISO_VD_END) {
    399 			brelse(bp);
    400 			bp = NULL;
    401 			break;
    402 		}
    403 
    404 		if (bp != NULL) {
    405 			brelse(bp);
    406 			bp = NULL;
    407 		}
    408 	}
    409 
    410 	if (pribp == NULL) {
    411 		error = EINVAL;
    412 		goto out;
    413 	}
    414 
    415 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
    416 	memset(isomp, 0, sizeof *isomp);
    417 	if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
    418 		error = EINVAL;
    419 		goto out;
    420 	}
    421 
    422 	isomp->volume_space_size += sess;
    423 
    424 	pribp->b_flags |= B_AGE;
    425 	brelse(pribp);
    426 	pribp = NULL;
    427 
    428 	mp->mnt_data = isomp;
    429 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    430 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_CD9660);
    431 	mp->mnt_maxsymlinklen = 0;
    432 	mp->mnt_flag |= MNT_LOCAL;
    433 	mp->mnt_dev_bshift = iso_bsize;
    434 	mp->mnt_fs_bshift = isomp->im_bshift;
    435 	isomp->im_mountp = mp;
    436 	isomp->im_dev = dev;
    437 	isomp->im_devvp = devvp;
    438 
    439 	devvp->v_specmountpoint = mp;
    440 
    441 	/* Check the Rock Ridge Extension support */
    442 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
    443 		struct iso_directory_record *rootp;
    444 
    445 		if ((error = bread(isomp->im_devvp,
    446 				   (isomp->root_extent + ext_attr_length) <<
    447 				   (isomp->im_bshift - DEV_BSHIFT),
    448 				   isomp->logical_block_size, NOCRED,
    449 				   &bp)) != 0)
    450 		    goto out;
    451 
    452 		rootp = (struct iso_directory_record *)bp->b_data;
    453 
    454 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
    455 		    argp->flags  |= ISOFSMNT_NORRIP;
    456 		} else {
    457 		    argp->flags  &= ~ISOFSMNT_GENS;
    458 		}
    459 
    460 		/*
    461 		 * The contents are valid,
    462 		 * but they will get reread as part of another vnode, so...
    463 		 */
    464 		bp->b_flags |= B_AGE;
    465 		brelse(bp);
    466 		bp = NULL;
    467 	}
    468 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
    469 		 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
    470 
    471 	if (isomp->im_flags & ISOFSMNT_GENS)
    472 		isomp->iso_ftype = ISO_FTYPE_9660;
    473 	else if (isomp->im_flags & ISOFSMNT_NORRIP) {
    474 		isomp->iso_ftype = ISO_FTYPE_DEFAULT;
    475 		if (argp->flags & ISOFSMNT_NOCASETRANS)
    476 			isomp->im_flags |= ISOFSMNT_NOCASETRANS;
    477 	} else
    478 		isomp->iso_ftype = ISO_FTYPE_RRIP;
    479 
    480 	/* Check the Joliet Extension support */
    481 	if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
    482 	    (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
    483 	    supbp != NULL) {
    484 		joliet_level = 0;
    485 		sup = (struct iso_supplementary_descriptor *)supbp->b_data;
    486 
    487 		if ((isonum_711(sup->flags) & 1) == 0) {
    488 			if (memcmp(sup->escape, "%/@", 3) == 0)
    489 				joliet_level = 1;
    490 			if (memcmp(sup->escape, "%/C", 3) == 0)
    491 				joliet_level = 2;
    492 			if (memcmp(sup->escape, "%/E", 3) == 0)
    493 				joliet_level = 3;
    494 		}
    495 		if (joliet_level != 0) {
    496 			if (iso_makemp(isomp, supbp, NULL) == -1) {
    497 				error = EINVAL;
    498 				goto out;
    499 			}
    500 			isomp->im_joliet_level = joliet_level;
    501 		}
    502 	}
    503 
    504 	if (supbp != NULL) {
    505 		brelse(supbp);
    506 		supbp = NULL;
    507 	}
    508 
    509 	return 0;
    510 out:
    511 	if (bp)
    512 		brelse(bp);
    513 	if (pribp)
    514 		brelse(pribp);
    515 	if (supbp)
    516 		brelse(supbp);
    517 	if (needclose) {
    518 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    519 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
    520 		VOP_UNLOCK(devvp, 0);
    521 	}
    522 	if (isomp) {
    523 		free(isomp, M_ISOFSMNT);
    524 		mp->mnt_data = NULL;
    525 	}
    526 	return error;
    527 }
    528 
    529 /*
    530  * Make a filesystem operational.
    531  * Nothing to do at the moment.
    532  */
    533 /* ARGSUSED */
    534 int
    535 cd9660_start(mp, flags, p)
    536 	struct mount *mp;
    537 	int flags;
    538 	struct proc *p;
    539 {
    540 	return 0;
    541 }
    542 
    543 /*
    544  * unmount system call
    545  */
    546 int
    547 cd9660_unmount(mp, mntflags, p)
    548 	struct mount *mp;
    549 	int mntflags;
    550 	struct proc *p;
    551 {
    552 	struct iso_mnt *isomp;
    553 	int error, flags = 0;
    554 
    555 	if (mntflags & MNT_FORCE)
    556 		flags |= FORCECLOSE;
    557 #if 0
    558 	mntflushbuf(mp, 0);
    559 	if (mntinvalbuf(mp))
    560 		return EBUSY;
    561 #endif
    562 	if ((error = vflush(mp, NULLVP, flags)) != 0)
    563 		return (error);
    564 
    565 	isomp = VFSTOISOFS(mp);
    566 
    567 #ifdef	ISODEVMAP
    568 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
    569 		iso_dunmap(isomp->im_dev);
    570 #endif
    571 
    572 	if (isomp->im_devvp->v_type != VBAD)
    573 		isomp->im_devvp->v_specmountpoint = NULL;
    574 
    575 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
    576 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
    577 	vput(isomp->im_devvp);
    578 	free(isomp, M_ISOFSMNT);
    579 	mp->mnt_data = NULL;
    580 	mp->mnt_flag &= ~MNT_LOCAL;
    581 	return (error);
    582 }
    583 
    584 /*
    585  * Return root of a filesystem
    586  */
    587 int
    588 cd9660_root(mp, vpp)
    589 	struct mount *mp;
    590 	struct vnode **vpp;
    591 {
    592 	struct iso_mnt *imp = VFSTOISOFS(mp);
    593 	struct iso_directory_record *dp =
    594 	    (struct iso_directory_record *)imp->root;
    595 	ino_t ino = isodirino(dp, imp);
    596 
    597 	/*
    598 	 * With RRIP we must use the `.' entry of the root directory.
    599 	 * Simply tell vget, that it's a relocated directory.
    600 	 */
    601 	return (cd9660_vget_internal(mp, ino, vpp,
    602 				     imp->iso_ftype == ISO_FTYPE_RRIP, dp));
    603 }
    604 
    605 /*
    606  * Do operations associated with quotas, not supported
    607  */
    608 /* ARGSUSED */
    609 int
    610 cd9660_quotactl(mp, cmd, uid, arg, p)
    611 	struct mount *mp;
    612 	int cmd;
    613 	uid_t uid;
    614 	caddr_t arg;
    615 	struct proc *p;
    616 {
    617 
    618 	return (EOPNOTSUPP);
    619 }
    620 
    621 /*
    622  * Get file system statistics.
    623  */
    624 int
    625 cd9660_statfs(mp, sbp, p)
    626 	struct mount *mp;
    627 	struct statfs *sbp;
    628 	struct proc *p;
    629 {
    630 	struct iso_mnt *isomp;
    631 
    632 	isomp = VFSTOISOFS(mp);
    633 
    634 #ifdef COMPAT_09
    635 	sbp->f_type = 5;
    636 #else
    637 	sbp->f_type = 0;
    638 #endif
    639 	sbp->f_bsize = isomp->logical_block_size;
    640 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
    641 	sbp->f_blocks = isomp->volume_space_size;
    642 	sbp->f_bfree = 0; /* total free blocks */
    643 	sbp->f_bavail = 0; /* blocks free for non superuser */
    644 	sbp->f_files =  0; /* total files */
    645 	sbp->f_ffree = 0; /* free file nodes */
    646 	if (sbp != &mp->mnt_stat) {
    647 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    648 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    649 	}
    650 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    651 	/* Use the first spare for flags: */
    652 	sbp->f_spare[0] = isomp->im_flags;
    653 	return 0;
    654 }
    655 
    656 /* ARGSUSED */
    657 int
    658 cd9660_sync(mp, waitfor, cred, p)
    659 	struct mount *mp;
    660 	int waitfor;
    661 	struct ucred *cred;
    662 	struct proc *p;
    663 {
    664 	return (0);
    665 }
    666 
    667 /*
    668  * File handle to vnode
    669  *
    670  * Have to be really careful about stale file handles:
    671  * - check that the inode number is in range
    672  * - call iget() to get the locked inode
    673  * - check for an unallocated inode (i_mode == 0)
    674  * - check that the generation number matches
    675  */
    676 
    677 struct ifid {
    678 	ushort	ifid_len;
    679 	ushort	ifid_pad;
    680 	int	ifid_ino;
    681 	long	ifid_start;
    682 };
    683 
    684 /* ARGSUSED */
    685 int
    686 cd9660_fhtovp(mp, fhp, vpp)
    687 	struct mount *mp;
    688 	struct fid *fhp;
    689 	struct vnode **vpp;
    690 {
    691 	struct ifid *ifhp = (struct ifid *)fhp;
    692 	struct iso_node *ip;
    693 	struct vnode *nvp;
    694 	int error;
    695 
    696 #ifdef	ISOFS_DBG
    697 	printf("fhtovp: ino %d, start %ld\n",
    698 	    ifhp->ifid_ino, ifhp->ifid_start);
    699 #endif
    700 
    701 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
    702 		*vpp = NULLVP;
    703 		return (error);
    704 	}
    705 	ip = VTOI(nvp);
    706 	if (ip->inode.iso_mode == 0) {
    707 		vput(nvp);
    708 		*vpp = NULLVP;
    709 		return (ESTALE);
    710 	}
    711 	*vpp = nvp;
    712 	return (0);
    713 }
    714 
    715 /* ARGSUSED */
    716 int
    717 cd9660_check_export(mp, nam, exflagsp, credanonp)
    718 	struct mount *mp;
    719 	struct mbuf *nam;
    720 	int *exflagsp;
    721 	struct ucred **credanonp;
    722 {
    723 	struct netcred *np;
    724 	struct iso_mnt *imp = VFSTOISOFS(mp);
    725 
    726 #ifdef	ISOFS_DBG
    727 	printf("check_export: ino %d, start %ld\n",
    728 	    ifhp->ifid_ino, ifhp->ifid_start);
    729 #endif
    730 
    731 	/*
    732 	 * Get the export permission structure for this <mp, client> tuple.
    733 	 */
    734 	np = vfs_export_lookup(mp, &imp->im_export, nam);
    735 	if (np == NULL)
    736 		return (EACCES);
    737 
    738 	*exflagsp = np->netc_exflags;
    739 	*credanonp = &np->netc_anon;
    740 	return (0);
    741 }
    742 
    743 int
    744 cd9660_vget(mp, ino, vpp)
    745 	struct mount *mp;
    746 	ino_t ino;
    747 	struct vnode **vpp;
    748 {
    749 
    750 	/*
    751 	 * XXXX
    752 	 * It would be nice if we didn't always set the `relocated' flag
    753 	 * and force the extra read, but I don't want to think about fixing
    754 	 * that right now.
    755 	 */
    756 	return (cd9660_vget_internal(mp, ino, vpp,
    757 #if 0
    758 				     VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
    759 #else
    760 				     0,
    761 #endif
    762 				     NULL));
    763 }
    764 
    765 int
    766 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
    767 	struct mount *mp;
    768 	ino_t ino;
    769 	struct vnode **vpp;
    770 	int relocated;
    771 	struct iso_directory_record *isodir;
    772 {
    773 	struct iso_mnt *imp;
    774 	struct iso_node *ip;
    775 #ifdef ISODEVMAP
    776 	struct iso_dnode *dp;
    777 #endif
    778 	struct buf *bp;
    779 	struct vnode *vp, *nvp;
    780 	dev_t dev;
    781 	int error;
    782 
    783 	imp = VFSTOISOFS(mp);
    784 	dev = imp->im_dev;
    785 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
    786 		return (0);
    787 
    788 	/* Allocate a new vnode/iso_node. */
    789 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
    790 		*vpp = NULLVP;
    791 		return (error);
    792 	}
    793 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
    794 	memset(ip, 0, sizeof(struct iso_node));
    795 	vp->v_data = ip;
    796 	ip->i_vnode = vp;
    797 	ip->i_dev = dev;
    798 	ip->i_number = ino;
    799 
    800 	/*
    801 	 * Put it onto its hash chain and lock it so that other requests for
    802 	 * this inode will block if they arrive while we are sleeping waiting
    803 	 * for old data structures to be purged or for the contents of the
    804 	 * disk portion of this inode to be read.
    805 	 */
    806 	cd9660_ihashins(ip);
    807 
    808 	if (isodir == 0) {
    809 		int lbn, off;
    810 
    811 		lbn = lblkno(imp, ino);
    812 		if (lbn >= imp->volume_space_size) {
    813 			vput(vp);
    814 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
    815 			return (ESTALE);
    816 		}
    817 
    818 		off = blkoff(imp, ino);
    819 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
    820 			vput(vp);
    821 			printf("fhtovp: crosses block boundary %d\n",
    822 			    off + ISO_DIRECTORY_RECORD_SIZE);
    823 			return (ESTALE);
    824 		}
    825 
    826 		error = bread(imp->im_devvp,
    827 			      lbn << (imp->im_bshift - DEV_BSHIFT),
    828 			      imp->logical_block_size, NOCRED, &bp);
    829 		if (error) {
    830 			vput(vp);
    831 			brelse(bp);
    832 			printf("fhtovp: bread error %d\n",error);
    833 			return (error);
    834 		}
    835 		isodir = (struct iso_directory_record *)(bp->b_data + off);
    836 
    837 		if (off + isonum_711(isodir->length) >
    838 		    imp->logical_block_size) {
    839 			vput(vp);
    840 			if (bp != 0)
    841 				brelse(bp);
    842 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
    843 			    off +isonum_711(isodir->length), off,
    844 			    isonum_711(isodir->length));
    845 			return (ESTALE);
    846 		}
    847 
    848 #if 0
    849 		if (isonum_733(isodir->extent) +
    850 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
    851 			if (bp != 0)
    852 				brelse(bp);
    853 			printf("fhtovp: file start miss %d vs %d\n",
    854 			    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
    855 			    ifhp->ifid_start);
    856 			return (ESTALE);
    857 		}
    858 #endif
    859 	} else
    860 		bp = 0;
    861 
    862 	ip->i_mnt = imp;
    863 	ip->i_devvp = imp->im_devvp;
    864 	VREF(ip->i_devvp);
    865 
    866 	if (relocated) {
    867 		/*
    868 		 * On relocated directories we must
    869 		 * read the `.' entry out of a dir.
    870 		 */
    871 		ip->iso_start = ino >> imp->im_bshift;
    872 		if (bp != 0)
    873 			brelse(bp);
    874 		if ((error = VOP_BLKATOFF(vp, (off_t)0, NULL, &bp)) != 0) {
    875 			vput(vp);
    876 			return (error);
    877 		}
    878 		isodir = (struct iso_directory_record *)bp->b_data;
    879 	}
    880 
    881 	ip->iso_extent = isonum_733(isodir->extent);
    882 	ip->i_size = isonum_733(isodir->size);
    883 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
    884 
    885 	/*
    886 	 * Setup time stamp, attribute
    887 	 */
    888 	vp->v_type = VNON;
    889 	switch (imp->iso_ftype) {
    890 	default:	/* ISO_FTYPE_9660 */
    891 	    {
    892 		struct buf *bp2;
    893 		int off;
    894 		if ((imp->im_flags & ISOFSMNT_EXTATT)
    895 		    && (off = isonum_711(isodir->ext_attr_length)))
    896 			VOP_BLKATOFF(vp, (off_t)-(off << imp->im_bshift), NULL,
    897 				     &bp2);
    898 		else
    899 			bp2 = NULL;
    900 		cd9660_defattr(isodir, ip, bp2);
    901 		cd9660_deftstamp(isodir, ip, bp2);
    902 		if (bp2)
    903 			brelse(bp2);
    904 		break;
    905 	    }
    906 	case ISO_FTYPE_RRIP:
    907 		cd9660_rrip_analyze(isodir, ip, imp);
    908 		break;
    909 	}
    910 
    911 	if (bp != 0)
    912 		brelse(bp);
    913 
    914 	/*
    915 	 * Initialize the associated vnode
    916 	 */
    917 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
    918 	case VFIFO:
    919 		vp->v_op = cd9660_fifoop_p;
    920 		break;
    921 	case VCHR:
    922 	case VBLK:
    923 		/*
    924 		 * if device, look at device number table for translation
    925 		 */
    926 #ifdef	ISODEVMAP
    927 		if ((dp = iso_dmap(dev, ino, 0)) != NULL)
    928 			ip->inode.iso_rdev = dp->d_dev;
    929 #endif
    930 		vp->v_op = cd9660_specop_p;
    931 		if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
    932 			/*
    933 			 * Discard unneeded vnode, but save its iso_node.
    934 			 * Note that the lock is carried over in the iso_node
    935 			 * to the replacement vnode.
    936 			 */
    937 			nvp->v_data = vp->v_data;
    938 			vp->v_data = NULL;
    939 			VOP_UNLOCK(vp, 0);
    940 			vp->v_op = spec_vnodeop_p;
    941 			vrele(vp);
    942 			vgone(vp);
    943 			lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
    944 			/*
    945 			 * Reinitialize aliased inode.
    946 			 */
    947 			vp = nvp;
    948 			ip->i_vnode = vp;
    949 		}
    950 		break;
    951 	case VLNK:
    952 	case VNON:
    953 	case VSOCK:
    954 	case VDIR:
    955 	case VBAD:
    956 		break;
    957 	case VREG:
    958 		uvm_vnp_setsize(vp, ip->i_size);
    959 		break;
    960 	}
    961 
    962 	if (ip->iso_extent == imp->root_extent)
    963 		vp->v_flag |= VROOT;
    964 
    965 	/*
    966 	 * XXX need generation number?
    967 	 */
    968 
    969 	genfs_node_init(vp, &cd9660_genfsops);
    970 	*vpp = vp;
    971 	return (0);
    972 }
    973 
    974 /*
    975  * Vnode pointer to File handle
    976  */
    977 /* ARGSUSED */
    978 int
    979 cd9660_vptofh(vp, fhp)
    980 	struct vnode *vp;
    981 	struct fid *fhp;
    982 {
    983 	struct iso_node *ip = VTOI(vp);
    984 	struct ifid *ifhp;
    985 
    986 	ifhp = (struct ifid *)fhp;
    987 	ifhp->ifid_len = sizeof(struct ifid);
    988 
    989 	ifhp->ifid_ino = ip->i_number;
    990 	ifhp->ifid_start = ip->iso_start;
    991 
    992 #ifdef	ISOFS_DBG
    993 	printf("vptofh: ino %d, start %ld\n",
    994 	    ifhp->ifid_ino,ifhp->ifid_start);
    995 #endif
    996 	return 0;
    997 }
    998 
    999 int
   1000 cd9660_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1001 	int *name;
   1002 	u_int namelen;
   1003 	void *oldp;
   1004 	size_t *oldlenp;
   1005 	void *newp;
   1006 	size_t newlen;
   1007 	struct proc *p;
   1008 {
   1009 	return (EOPNOTSUPP);
   1010 }
   1011