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