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