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