File:  [DragonFly] / src / sys / vfs / isofs / cd9660 / cd9660_vfsops.c
Revision 1.15: download - view: text, annotated - select for diffs
Wed May 19 22:53:04 2004 UTC (10 years, 3 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Device layer rollup commit.

* cdevsw_add() is now required.  cdevsw_add() and cdevsw_remove() may specify
  a mask/match indicating the range of supported minor numbers.  Multiple
  cdevsw_add()'s using the same major number, but distinctly different
  ranges, may be issued.  All devices that failed to call cdevsw_add() before
  now do.

* cdevsw_remove() now automatically marks all devices within its supported
  range as being destroyed.

* vnode->v_rdev is no longer resolved when the vnode is created.  Instead,
  only v_udev (a newly added field) is resolved.  v_rdev is resolved when
  the vnode is opened and cleared on the last close.

* A great deal of code was making rather dubious assumptions with regards
  to the validity of devices associated with vnodes, primarily due to
  the persistence of a device structure due to being indexed by (major, minor)
  instead of by (cdevsw, major, minor).  In particular, if you run a program
  which connects to a USB device and then you pull the USB device and plug
  it back in, the vnode subsystem will continue to believe that the device
  is open when, in fact, it isn't (because it was destroyed and recreated).

  In particular, note that all the VFS mount procedures now check devices
  via v_udev instead of v_rdev prior to calling VOP_OPEN(), since v_rdev
  is NULL prior to the first open.

* The disk layer's device interaction has been rewritten.  The disk layer
  (i.e. the slice and disklabel management layer) no longer overloads
  its data onto the device structure representing the underlying physical
  disk.  Instead, the disk layer uses the new cdevsw_add() functionality
  to register its own cdevsw using the underlying device's major number,
  and simply does NOT register the underlying device's cdevsw.  No
  confusion is created because the device hash is now based on
  (cdevsw,major,minor) rather then (major,minor).

  NOTE: This also means that underlying raw disk devices may use the entire
  device minor number instead of having to reserve the bits used by the disk
  layer, and also means that can we (theoretically) stack a fully
  disklabel-supported 'disk' on top of any block device.

* The new reference counting scheme prevents this by associating a device
  with a cdevsw and disconnecting the device from its cdevsw when the cdevsw
  is removed.  Additionally, all udev2dev() lookups run through the cdevsw
  mask/match and only successfully find devices still associated with an
  active cdevsw.

* Major work on MFS:  MFS no longer shortcuts vnode and device creation.  It
  now creates a real vnode and a real device and implements real open and
  close VOPs.  Additionally, due to the disk layer changes, MFS is no longer
  limited to 255 mounts.  The new limit is 16 million.  Since MFS creates a
  real device node, mount_mfs will now create a real /dev/mfs<PID> device
  that can be read from userland (e.g. so you can dump an MFS filesystem).

* BUF AND DEVICE STRATEGY changes.  The struct buf contains a b_dev field.
  In order to properly handle stacked devices we now require that the b_dev
  field be initialized before the device strategy routine is called.  This
  required some additional work in various VFS implementations.  To enforce
  this requirement, biodone() now sets b_dev to NODEV.  The new disk layer
  will adjust b_dev before forwarding a request to the actual physical
  device.

* A bug in the ISO CD boot sequence which resulted in a panic has been fixed.

Testing by: lots of people, but David Rhodus found the most aggregious bugs.

    1: /*-
    2:  * Copyright (c) 1994
    3:  *	The Regents of the University of California.  All rights reserved.
    4:  *
    5:  * This code is derived from software contributed to Berkeley
    6:  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
    7:  * Support code is derived from software contributed to Berkeley
    8:  * by Atsushi Murai (amurai@spec.co.jp).
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  * 3. All advertising materials mentioning features or use of this software
   19:  *    must display the following acknowledgement:
   20:  *	This product includes software developed by the University of
   21:  *	California, Berkeley and its contributors.
   22:  * 4. Neither the name of the University nor the names of its contributors
   23:  *    may be used to endorse or promote products derived from this software
   24:  *    without specific prior written permission.
   25:  *
   26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36:  * SUCH DAMAGE.
   37:  *
   38:  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
   39:  * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
   40:  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.15 2004/05/19 22:53:04 dillon Exp $
   41:  */
   42: 
   43: #include <sys/param.h>
   44: #include <sys/systm.h>
   45: #include <sys/proc.h>
   46: #include <sys/namei.h>
   47: #include <sys/kernel.h>
   48: #include <sys/vnode.h>
   49: #include <sys/mount.h>
   50: #include <sys/buf.h>
   51: #include <sys/cdio.h>
   52: #include <sys/conf.h>
   53: #include <sys/fcntl.h>
   54: #include <sys/malloc.h>
   55: #include <sys/stat.h>
   56: #include <sys/syslog.h>
   57: 
   58: #include <vm/vm_zone.h>
   59: 
   60: #include "iso.h"
   61: #include "iso_rrip.h"
   62: #include "cd9660_node.h"
   63: #include "cd9660_mount.h"
   64: 
   65: MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
   66: MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
   67: 
   68: static int cd9660_mount (struct mount *,
   69: 	    char *, caddr_t, struct nameidata *, struct thread *);
   70: static int cd9660_unmount (struct mount *, int, struct thread *);
   71: static int cd9660_root (struct mount *, struct vnode **);
   72: static int cd9660_statfs (struct mount *, struct statfs *, struct thread *);
   73: static int cd9660_vget (struct mount *, ino_t, struct vnode **);
   74: static int cd9660_fhtovp (struct mount *, struct fid *, struct vnode **);
   75: static int cd9660_checkexp (struct mount *, struct sockaddr *,
   76: 	    int *, struct ucred **);
   77: static int cd9660_vptofh (struct vnode *, struct fid *);
   78: 
   79: static struct vfsops cd9660_vfsops = {
   80: 	cd9660_mount,
   81: 	vfs_stdstart,
   82: 	cd9660_unmount,
   83: 	cd9660_root,
   84: 	vfs_stdquotactl,
   85: 	cd9660_statfs,
   86: 	vfs_stdsync,
   87: 	cd9660_vget,
   88: 	cd9660_fhtovp,
   89: 	cd9660_checkexp,
   90: 	cd9660_vptofh,
   91: 	cd9660_init,
   92: 	cd9660_uninit,
   93: 	vfs_stdextattrctl,
   94: };
   95: VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
   96: MODULE_VERSION(cd9660, 1);
   97: 
   98: 
   99: /*
  100:  * Called by vfs_mountroot when iso is going to be mounted as root.
  101:  */
  102: 
  103: static int iso_get_ssector (dev_t dev, struct thread *td);
  104: static int iso_mountfs (struct vnode *devvp, struct mount *mp,
  105: 			    struct thread *td, struct iso_args *argp);
  106: 
  107: /*
  108:  * Try to find the start of the last data track on this CD-ROM.  This
  109:  * is used to mount the last session of a multi-session CD.  Bail out
  110:  * and return 0 if we fail, this is always a safe bet.
  111:  */
  112: static int
  113: iso_get_ssector(dev_t dev, struct thread *td)
  114: {
  115: 	struct ioc_toc_header h;
  116: 	struct ioc_read_toc_single_entry t;
  117: 	int i;
  118: 
  119: 	if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
  120: 		return 0;
  121: 
  122: 	for (i = h.ending_track; i >= 0; i--) {
  123: 		t.address_format = CD_LBA_FORMAT;
  124: 		t.track = i;
  125: 		if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0) {
  126: 			return 0;
  127: 		}
  128: 		if ((t.entry.control & 4) != 0)
  129: 			/* found a data track */
  130: 			break;
  131: 	}
  132: 
  133: 	if (i < 0)
  134: 		return 0;
  135: 
  136: 	return ntohl(t.entry.addr.lba);
  137: }
  138: 
  139: static int iso_mountroot (struct mount *mp, struct thread *td);
  140: 
  141: static int
  142: iso_mountroot(struct mount *mp, struct thread *td)
  143: {
  144: 	struct iso_args args;
  145: 	int error;
  146: 
  147: 	if ((error = bdevvp(rootdev, &rootvp))) {
  148: 		printf("iso_mountroot: can't find rootvp\n");
  149: 		return (error);
  150: 	}
  151: 	args.flags = ISOFSMNT_ROOT;
  152: 
  153: 	vn_lock(rootvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
  154: 	error = VOP_OPEN(rootvp, FREAD, FSCRED, td);
  155: 	VOP_UNLOCK(rootvp, NULL, 0, td);
  156: 	if (error)
  157: 		return (error);
  158: 
  159: 	args.ssector = iso_get_ssector(rootdev, td);
  160: 
  161: 	(void)VOP_CLOSE(rootvp, FREAD, td);
  162: 
  163: 	if (bootverbose)
  164: 		printf("iso_mountroot(): using session at block %d\n",
  165: 		       args.ssector);
  166: 	if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
  167: 		return (error);
  168: 
  169: 	(void)cd9660_statfs(mp, &mp->mnt_stat, td);
  170: 	return (0);
  171: }
  172: 
  173: /*
  174:  * VFS Operations.
  175:  *
  176:  * mount system call
  177:  */
  178: static int
  179: cd9660_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
  180: 	     struct thread *td)
  181: {
  182: 	struct vnode *devvp;
  183: 	struct iso_args args;
  184: 	size_t size;
  185: 	int error;
  186: 	mode_t accessmode;
  187: 	struct iso_mnt *imp = 0;
  188: 
  189: 	if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
  190: 		return (iso_mountroot(mp, td));
  191: 	}
  192: 	if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
  193: 		return (error);
  194: 
  195: 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
  196: 		return (EROFS);
  197: 
  198: 	KKASSERT(td->td_proc);
  199: 
  200: 	/*
  201: 	 * If updating, check whether changing from read-only to
  202: 	 * read/write; if there is no device name, that's all we do.
  203: 	 */
  204: 	if (mp->mnt_flag & MNT_UPDATE) {
  205: 		imp = VFSTOISOFS(mp);
  206: 		if (args.fspec == 0)
  207: 			return (vfs_export(mp, &imp->im_export, &args.export));
  208: 	}
  209: 	/*
  210: 	 * Not an update, or updating the name: look up the name
  211: 	 * and verify that it refers to a sensible block device.
  212: 	 */
  213: 	NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
  214: 	if ((error = namei(ndp)))
  215: 		return (error);
  216: 	NDFREE(ndp, NDF_ONLY_PNBUF);
  217: 	devvp = ndp->ni_vp;
  218: 
  219: 	if (!vn_isdisk(devvp, &error)) {
  220: 		vrele(devvp);
  221: 		return (error);
  222: 	}
  223: 
  224: 	/*       
  225: 	 * Verify that user has necessary permissions on the device,
  226: 	 * or has superuser abilities
  227: 	 */
  228: 	accessmode = VREAD;
  229: 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
  230: 	error = VOP_ACCESS(devvp, accessmode, td->td_proc->p_ucred, td);
  231: 	if (error) 
  232: 		error = suser(td);
  233: 	if (error) {
  234: 		vput(devvp);
  235: 		return (error);
  236: 	}
  237: 	VOP_UNLOCK(devvp, NULL, 0, td);
  238: 
  239: 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
  240: 		error = iso_mountfs(devvp, mp, td, &args);
  241: 	} else {
  242: 		if (devvp != imp->im_devvp)
  243: 			error = EINVAL;	/* needs translation */
  244: 		else
  245: 			vrele(devvp);
  246: 	}
  247: 	if (error) {
  248: 		vrele(devvp);
  249: 		return error;
  250: 	}
  251: 	imp = VFSTOISOFS(mp);
  252: 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
  253: 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
  254: 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
  255: 	    &size);
  256: 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
  257: 	(void) cd9660_statfs(mp, &mp->mnt_stat, td);
  258: 	return 0;
  259: }
  260: 
  261: /*
  262:  * Common code for mount and mountroot
  263:  */
  264: static int
  265: iso_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
  266: 	    struct iso_args *argp)
  267: {
  268: 	struct iso_mnt *isomp = (struct iso_mnt *)0;
  269: 	struct buf *bp = NULL;
  270: 	struct buf *pribp = NULL, *supbp = NULL;
  271: 	dev_t dev;
  272: 	int error = EINVAL;
  273: 	int needclose = 0;
  274: 	int high_sierra = 0;
  275: 	int iso_bsize;
  276: 	int iso_blknum;
  277: 	int joliet_level;
  278: 	struct iso_volume_descriptor *vdp = 0;
  279: 	struct iso_primary_descriptor *pri = NULL;
  280: 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
  281: 	struct iso_supplementary_descriptor *sup = NULL;
  282: 	struct iso_directory_record *rootp;
  283: 	int logical_block_size;
  284: 
  285: 	if (!(mp->mnt_flag & MNT_RDONLY))
  286: 		return EROFS;
  287: 
  288: 	/*
  289: 	 * Disallow multiple mounts of the same device.
  290: 	 * Disallow mounting of a device that is currently in use
  291: 	 * (except for root, which might share swap device for miniroot).
  292: 	 * Flush out any old buffers remaining from a previous use.
  293: 	 */
  294: 	if ((error = vfs_mountedon(devvp)))
  295: 		return error;
  296: 	if (count_udev(devvp) > 0 && devvp != rootvp)
  297: 		return EBUSY;
  298: 	if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
  299: 		return (error);
  300: 
  301: 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
  302: 	error = VOP_OPEN(devvp, FREAD, FSCRED, td);
  303: 	VOP_UNLOCK(devvp, NULL, 0, td);
  304: 	if (error)
  305: 		return error;
  306: 	dev = devvp->v_rdev;
  307: 	if (dev->si_iosize_max != 0)
  308: 		mp->mnt_iosize_max = dev->si_iosize_max;
  309: 	if (mp->mnt_iosize_max > MAXPHYS)
  310: 		mp->mnt_iosize_max = MAXPHYS;
  311: 
  312: 	needclose = 1;
  313: 
  314: 	/* This is the "logical sector size".  The standard says this
  315: 	 * should be 2048 or the physical sector size on the device,
  316: 	 * whichever is greater.  For now, we'll just use a constant.
  317: 	 */
  318: 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
  319: 
  320: 	joliet_level = 0;
  321: 	for (iso_blknum = 16 + argp->ssector;
  322: 	     iso_blknum < 100 + argp->ssector;
  323: 	     iso_blknum++) {
  324: 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
  325: 				  iso_bsize, &bp)) != 0)
  326: 			goto out;
  327: 		
  328: 		vdp = (struct iso_volume_descriptor *)bp->b_data;
  329: 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
  330: 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
  331: 				  sizeof vdp->id) != 0) {
  332: 				error = EINVAL;
  333: 				goto out;
  334: 			} else
  335: 				high_sierra = 1;
  336: 		}
  337: 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
  338: 		case ISO_VD_PRIMARY:
  339: 			if (pribp == NULL) {
  340: 				pribp = bp;
  341: 				bp = NULL;
  342: 				pri = (struct iso_primary_descriptor *)vdp;
  343: 				pri_sierra =
  344: 				  (struct iso_sierra_primary_descriptor *)vdp;
  345: 			}
  346: 			break;
  347: 
  348: 		case ISO_VD_SUPPLEMENTARY:
  349: 			if (supbp == NULL) {
  350: 				supbp = bp;
  351: 				bp = NULL;
  352: 				sup = (struct iso_supplementary_descriptor *)vdp;
  353: 
  354: 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
  355: 					if (bcmp(sup->escape, "%/@", 3) == 0)
  356: 						joliet_level = 1;
  357: 					if (bcmp(sup->escape, "%/C", 3) == 0)
  358: 						joliet_level = 2;
  359: 					if (bcmp(sup->escape, "%/E", 3) == 0)
  360: 						joliet_level = 3;
  361: 
  362: 					if ((isonum_711 (sup->flags) & 1) &&
  363: 					    (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
  364: 						joliet_level = 0;
  365: 				}
  366: 			}
  367: 			break;
  368: 
  369: 		case ISO_VD_END:
  370: 			goto vd_end;
  371: 
  372: 		default:
  373: 			break;
  374: 		}
  375: 		if (bp) {
  376: 			brelse(bp);
  377: 			bp = NULL;
  378: 		}
  379: 	}
  380:  vd_end:
  381: 	if (bp) {
  382: 		brelse(bp);
  383: 		bp = NULL;
  384: 	}
  385: 
  386: 	if (pri == NULL) {
  387: 		error = EINVAL;
  388: 		goto out;
  389: 	}
  390: 
  391: 	logical_block_size =
  392: 		isonum_723 (high_sierra?
  393: 			    pri_sierra->logical_block_size:
  394: 			    pri->logical_block_size);
  395: 
  396: 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
  397: 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
  398: 		error = EINVAL;
  399: 		goto out;
  400: 	}
  401: 
  402: 	rootp = (struct iso_directory_record *)
  403: 		(high_sierra?
  404: 		 pri_sierra->root_directory_record:
  405: 		 pri->root_directory_record);
  406: 
  407: 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
  408: 	bzero((caddr_t)isomp, sizeof *isomp);
  409: 	isomp->logical_block_size = logical_block_size;
  410: 	isomp->volume_space_size =
  411: 		isonum_733 (high_sierra?
  412: 			    pri_sierra->volume_space_size:
  413: 			    pri->volume_space_size);
  414: 	isomp->joliet_level = 0;
  415: 	/*
  416: 	 * Since an ISO9660 multi-session CD can also access previous
  417: 	 * sessions, we have to include them into the space consider-
  418: 	 * ations.  This doesn't yield a very accurate number since
  419: 	 * parts of the old sessions might be inaccessible now, but we
  420: 	 * can't do much better.  This is also important for the NFS
  421: 	 * filehandle validation.
  422: 	 */
  423: 	isomp->volume_space_size += argp->ssector;
  424: 	bcopy (rootp, isomp->root, sizeof isomp->root);
  425: 	isomp->root_extent = isonum_733 (rootp->extent);
  426: 	isomp->root_size = isonum_733 (rootp->size);
  427: 
  428: 	isomp->im_bmask = logical_block_size - 1;
  429: 	isomp->im_bshift = ffs(logical_block_size) - 1;
  430: 
  431: 	pribp->b_flags |= B_AGE;
  432: 	brelse(pribp);
  433: 	pribp = NULL;
  434: 
  435: 	mp->mnt_data = (qaddr_t)isomp;
  436: 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
  437: 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  438: 	mp->mnt_maxsymlinklen = 0;
  439: 	mp->mnt_flag |= MNT_LOCAL;
  440: 	isomp->im_mountp = mp;
  441: 	isomp->im_dev = dev;
  442: 	isomp->im_devvp = devvp;
  443: 
  444: 	dev->si_mountpoint = mp;
  445: 
  446: 	/* Check the Rock Ridge Extention support */
  447: 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
  448: 		if ((error = bread(isomp->im_devvp,
  449: 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
  450: 				  (isomp->im_bshift - DEV_BSHIFT),
  451: 				  isomp->logical_block_size, &bp)) != 0)
  452: 		    goto out;
  453: 		
  454: 		rootp = (struct iso_directory_record *)bp->b_data;
  455: 		
  456: 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
  457: 		    argp->flags	 |= ISOFSMNT_NORRIP;
  458: 		} else {
  459: 		    argp->flags	 &= ~ISOFSMNT_GENS;
  460: 		}
  461: 
  462: 		/*
  463: 		 * The contents are valid,
  464: 		 * but they will get reread as part of another vnode, so...
  465: 		 */
  466: 		bp->b_flags |= B_AGE;
  467: 		brelse(bp);
  468: 		bp = NULL;
  469: 	}
  470: 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
  471: 					 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
  472: 
  473: 	if (high_sierra) {
  474: 		/* this effectively ignores all the mount flags */
  475: 		log(LOG_INFO, "cd9660: High Sierra Format\n");
  476: 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
  477: 	} else
  478: 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
  479: 		  default:
  480: 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
  481: 			  break;
  482: 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
  483: 			  isomp->iso_ftype = ISO_FTYPE_9660;
  484: 			  break;
  485: 		  case 0:
  486: 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
  487: 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
  488: 			  break;
  489: 		}
  490: 
  491: 	/* Decide whether to use the Joliet descriptor */
  492: 
  493: 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
  494: 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
  495: 		rootp = (struct iso_directory_record *)
  496: 			sup->root_directory_record;
  497: 		bcopy (rootp, isomp->root, sizeof isomp->root);
  498: 		isomp->root_extent = isonum_733 (rootp->extent);
  499: 		isomp->root_size = isonum_733 (rootp->size);
  500: 		isomp->joliet_level = joliet_level;
  501: 		supbp->b_flags |= B_AGE;
  502: 	}
  503: 
  504: 	if (supbp) {
  505: 		brelse(supbp);
  506: 		supbp = NULL;
  507: 	}
  508: 
  509: 	return 0;
  510: out:
  511: 	dev->si_mountpoint = NULL;
  512: 	if (bp)
  513: 		brelse(bp);
  514: 	if (pribp)
  515: 		brelse(pribp);
  516: 	if (supbp)
  517: 		brelse(supbp);
  518: 	if (needclose)
  519: 		(void)VOP_CLOSE(devvp, FREAD, td);
  520: 	if (isomp) {
  521: 		free((caddr_t)isomp, M_ISOFSMNT);
  522: 		mp->mnt_data = (qaddr_t)0;
  523: 	}
  524: 	return error;
  525: }
  526: 
  527: /*
  528:  * unmount system call
  529:  */
  530: static int
  531: cd9660_unmount(struct mount *mp, int mntflags, struct thread *td)
  532: {
  533: 	struct iso_mnt *isomp;
  534: 	int error, flags = 0;
  535: 
  536: 	if (mntflags & MNT_FORCE)
  537: 		flags |= FORCECLOSE;
  538: #if 0
  539: 	mntflushbuf(mp, 0);
  540: 	if (mntinvalbuf(mp))
  541: 		return EBUSY;
  542: #endif
  543: 	if ((error = vflush(mp, 0, flags)))
  544: 		return (error);
  545: 
  546: 	isomp = VFSTOISOFS(mp);
  547: 
  548: 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
  549: 	error = VOP_CLOSE(isomp->im_devvp, FREAD, td);
  550: 	vrele(isomp->im_devvp);
  551: 	free((caddr_t)isomp, M_ISOFSMNT);
  552: 	mp->mnt_data = (qaddr_t)0;
  553: 	mp->mnt_flag &= ~MNT_LOCAL;
  554: 	return (error);
  555: }
  556: 
  557: /*
  558:  * Return root of a filesystem
  559:  */
  560: static int
  561: cd9660_root(struct mount *mp, struct vnode **vpp)
  562: {
  563: 	struct iso_mnt *imp = VFSTOISOFS(mp);
  564: 	struct iso_directory_record *dp =
  565: 	    (struct iso_directory_record *)imp->root;
  566: 	ino_t ino = isodirino(dp, imp);
  567: 	
  568: 	/*
  569: 	 * With RRIP we must use the `.' entry of the root directory.
  570: 	 * Simply tell vget, that it's a relocated directory.
  571: 	 */
  572: 	return (cd9660_vget_internal(mp, ino, vpp,
  573: 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
  574: }
  575: 
  576: /*
  577:  * Get file system statistics.
  578:  */
  579: int
  580: cd9660_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
  581: {
  582: 	struct iso_mnt *isomp;
  583: 
  584: 	isomp = VFSTOISOFS(mp);
  585: 
  586: 	sbp->f_bsize = isomp->logical_block_size;
  587: 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
  588: 	sbp->f_blocks = isomp->volume_space_size;
  589: 	sbp->f_bfree = 0; /* total free blocks */
  590: 	sbp->f_bavail = 0; /* blocks free for non superuser */
  591: 	sbp->f_files =	0; /* total files */
  592: 	sbp->f_ffree = 0; /* free file nodes */
  593: 	if (sbp != &mp->mnt_stat) {
  594: 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
  595: 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
  596: 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
  597: 	}
  598: 	return 0;
  599: }
  600: 
  601: /*
  602:  * File handle to vnode
  603:  *
  604:  * Have to be really careful about stale file handles:
  605:  * - check that the inode number is in range
  606:  * - call iget() to get the locked inode
  607:  * - check for an unallocated inode (i_mode == 0)
  608:  * - check that the generation number matches
  609:  */
  610: 
  611: struct ifid {
  612: 	ushort	ifid_len;
  613: 	ushort	ifid_pad;
  614: 	int	ifid_ino;
  615: 	long	ifid_start;
  616: };
  617: 
  618: /* ARGSUSED */
  619: int
  620: cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  621: {
  622: 	struct ifid *ifhp = (struct ifid *)fhp;
  623: 	struct iso_node *ip;
  624: 	struct vnode *nvp;
  625: 	int error;
  626: 	
  627: #ifdef	ISOFS_DBG
  628: 	printf("fhtovp: ino %d, start %ld\n",
  629: 	       ifhp->ifid_ino, ifhp->ifid_start);
  630: #endif
  631: 	
  632: 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
  633: 		*vpp = NULLVP;
  634: 		return (error);
  635: 	}
  636: 	ip = VTOI(nvp);
  637: 	if (ip->inode.iso_mode == 0) {
  638: 		vput(nvp);
  639: 		*vpp = NULLVP;
  640: 		return (ESTALE);
  641: 	}
  642: 	*vpp = nvp;
  643: 	return (0);
  644: }
  645: 
  646: int
  647: cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
  648: 		struct ucred **credanonp)
  649: {
  650: 	struct netcred *np;
  651: 	struct iso_mnt *imp;
  652: 
  653: 	imp = VFSTOISOFS(mp);	
  654: 
  655: 	/*
  656: 	 * Get the export permission structure for this <mp, client> tuple.
  657: 	 */
  658: 	np = vfs_export_lookup(mp, &imp->im_export, nam);
  659: 	if (np == NULL)
  660: 		return (EACCES);
  661: 
  662: 	*exflagsp = np->netc_exflags;
  663: 	*credanonp = &np->netc_anon;
  664: 	return (0);
  665: }
  666: 
  667: int
  668: cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
  669: {
  670: 
  671: 	/*
  672: 	 * XXXX
  673: 	 * It would be nice if we didn't always set the `relocated' flag
  674: 	 * and force the extra read, but I don't want to think about fixing
  675: 	 * that right now.
  676: 	 */
  677: 	return (cd9660_vget_internal(mp, ino, vpp,
  678: #if 0
  679: 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
  680: #else
  681: 	    0,
  682: #endif
  683: 	    (struct iso_directory_record *)0));
  684: }
  685: 
  686: int
  687: cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
  688: 		     int relocated, struct iso_directory_record *isodir)
  689: {
  690: 	struct iso_mnt *imp;
  691: 	struct iso_node *ip;
  692: 	struct buf *bp;
  693: 	struct vnode *vp;
  694: 	dev_t dev;
  695: 	int error;
  696: 
  697: 	imp = VFSTOISOFS(mp);
  698: 	dev = imp->im_dev;
  699: 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
  700: 		return (0);
  701: 
  702: 	/* Allocate a new vnode/iso_node. */
  703: 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
  704: 		*vpp = NULLVP;
  705: 		return (error);
  706: 	}
  707: 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
  708: 	    M_WAITOK);
  709: 	bzero((caddr_t)ip, sizeof(struct iso_node));
  710: 	lockinit(&ip->i_lock, 0, "isonode", 0, 0);
  711: 	vp->v_data = ip;
  712: 	ip->i_vnode = vp;
  713: 	ip->i_dev = dev;
  714: 	ip->i_number = ino;
  715: 
  716: 	/*
  717: 	 * Put it onto its hash chain and lock it so that other requests for
  718: 	 * this inode will block if they arrive while we are sleeping waiting
  719: 	 * for old data structures to be purged or for the contents of the
  720: 	 * disk portion of this inode to be read.
  721: 	 */
  722: 	cd9660_ihashins(ip);
  723: 
  724: 	if (isodir == 0) {
  725: 		int lbn, off;
  726: 
  727: 		lbn = lblkno(imp, ino);
  728: 		if (lbn >= imp->volume_space_size) {
  729: 			vput(vp);
  730: 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
  731: 			return (ESTALE);
  732: 		}
  733: 	
  734: 		off = blkoff(imp, ino);
  735: 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
  736: 			vput(vp);
  737: 			printf("fhtovp: crosses block boundary %d\n",
  738: 			       off + ISO_DIRECTORY_RECORD_SIZE);
  739: 			return (ESTALE);
  740: 		}
  741: 	
  742: 		error = bread(imp->im_devvp,
  743: 			      lbn << (imp->im_bshift - DEV_BSHIFT),
  744: 			      imp->logical_block_size, &bp);
  745: 		if (error) {
  746: 			vput(vp);
  747: 			brelse(bp);
  748: 			printf("fhtovp: bread error %d\n",error);
  749: 			return (error);
  750: 		}
  751: 		isodir = (struct iso_directory_record *)(bp->b_data + off);
  752: 
  753: 		if (off + isonum_711(isodir->length) >
  754: 		    imp->logical_block_size) {
  755: 			vput(vp);
  756: 			if (bp != 0)
  757: 				brelse(bp);
  758: 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
  759: 			       off +isonum_711(isodir->length), off,
  760: 			       isonum_711(isodir->length));
  761: 			return (ESTALE);
  762: 		}
  763: 	
  764: #if 0
  765: 		if (isonum_733(isodir->extent) +
  766: 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
  767: 			if (bp != 0)
  768: 				brelse(bp);
  769: 			printf("fhtovp: file start miss %d vs %d\n",
  770: 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
  771: 			       ifhp->ifid_start);
  772: 			return (ESTALE);
  773: 		}
  774: #endif
  775: 	} else
  776: 		bp = 0;
  777: 
  778: 	ip->i_mnt = imp;
  779: 	ip->i_devvp = imp->im_devvp;
  780: 	vref(ip->i_devvp);
  781: 
  782: 	if (relocated) {
  783: 		/*
  784: 		 * On relocated directories we must
  785: 		 * read the `.' entry out of a dir.
  786: 		 */
  787: 		ip->iso_start = ino >> imp->im_bshift;
  788: 		if (bp != 0)
  789: 			brelse(bp);
  790: 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
  791: 			vput(vp);
  792: 			return (error);
  793: 		}
  794: 		isodir = (struct iso_directory_record *)bp->b_data;
  795: 	}
  796: 
  797: 	ip->iso_extent = isonum_733(isodir->extent);
  798: 	ip->i_size = isonum_733(isodir->size);
  799: 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
  800: 	
  801: 	/*
  802: 	 * Setup time stamp, attribute
  803: 	 */
  804: 	vp->v_type = VNON;
  805: 	switch (imp->iso_ftype) {
  806: 	default:	/* ISO_FTYPE_9660 */
  807: 	    {
  808: 		struct buf *bp2;
  809: 		int off;
  810: 		if ((imp->im_flags & ISOFSMNT_EXTATT)
  811: 		    && (off = isonum_711(isodir->ext_attr_length)))
  812: 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
  813: 				     &bp2);
  814: 		else
  815: 			bp2 = NULL;
  816: 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
  817: 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
  818: 		if (bp2)
  819: 			brelse(bp2);
  820: 		break;
  821: 	    }
  822: 	case ISO_FTYPE_RRIP:
  823: 		cd9660_rrip_analyze(isodir, ip, imp);
  824: 		break;
  825: 	}
  826: 
  827: 	if (bp != 0)
  828: 		brelse(bp);
  829: 
  830: 	/*
  831: 	 * Initialize the associated vnode
  832: 	 */
  833: 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
  834: 	case VFIFO:
  835: 		vp->v_op = cd9660_fifoop_p;
  836: 		break;
  837: 	case VCHR:
  838: 	case VBLK:
  839: 		vp->v_op = cd9660_specop_p;
  840: 		addaliasu(vp, ip->inode.iso_rdev);
  841: 		break;
  842: 	default:
  843: 		break;
  844: 	}
  845: 	
  846: 	if (ip->iso_extent == imp->root_extent)
  847: 		vp->v_flag |= VROOT;
  848: 
  849: 	/*
  850: 	 * XXX need generation number?
  851: 	 */
  852: 	
  853: 	*vpp = vp;
  854: 	return (0);
  855: }
  856: 
  857: /*
  858:  * Vnode pointer to File handle
  859:  */
  860: /* ARGSUSED */
  861: int
  862: cd9660_vptofh(struct vnode *vp, struct fid *fhp)
  863: {
  864: 	struct iso_node *ip = VTOI(vp);
  865: 	struct ifid *ifhp;
  866: 
  867: 	ifhp = (struct ifid *)fhp;
  868: 	ifhp->ifid_len = sizeof(struct ifid);
  869: 
  870: 	ifhp->ifid_ino = ip->i_number;
  871: 	ifhp->ifid_start = ip->iso_start;
  872: 
  873: #ifdef	ISOFS_DBG
  874: 	printf("vptofh: ino %d, start %ld\n",
  875: 	       ifhp->ifid_ino,ifhp->ifid_start);
  876: #endif
  877: 	return 0;
  878: }