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