xref: /onnv-gate/usr/src/uts/common/fs/pcfs/pc_vfsops.c (revision 1488:196daa2cf3db)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1488Srsb  * Common Development and Distribution License (the "License").
6*1488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1488Srsb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/user.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/disp.h>
350Sstevel@tonic-gate #include <sys/buf.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/fdio.h>
390Sstevel@tonic-gate #include <sys/file.h>
400Sstevel@tonic-gate #include <sys/uio.h>
410Sstevel@tonic-gate #include <sys/conf.h>
420Sstevel@tonic-gate #undef NFSCLIENT
430Sstevel@tonic-gate #include <sys/statvfs.h>
440Sstevel@tonic-gate #include <sys/mount.h>
450Sstevel@tonic-gate #include <sys/pathname.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/sysmacros.h>
490Sstevel@tonic-gate #include <sys/conf.h>
500Sstevel@tonic-gate #include <sys/mkdev.h>
510Sstevel@tonic-gate #include <sys/swap.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/sunldi.h>
540Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
550Sstevel@tonic-gate #include <sys/fs/pc_label.h>
560Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
570Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
580Sstevel@tonic-gate #include <sys/fs/pc_node.h>
590Sstevel@tonic-gate #include <fs/fs_subr.h>
600Sstevel@tonic-gate #include <sys/modctl.h>
610Sstevel@tonic-gate #include <sys/vol.h>
620Sstevel@tonic-gate #include <sys/dkio.h>
630Sstevel@tonic-gate #include <sys/open.h>
640Sstevel@tonic-gate #include <sys/mntent.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * The majority of PC media use a 512 sector size, but
690Sstevel@tonic-gate  * occasionally you will run across a 1k sector size.
700Sstevel@tonic-gate  * For media with a 1k sector size, fd_strategy() requires
710Sstevel@tonic-gate  * the I/O size to be a 1k multiple; so when the sector size
720Sstevel@tonic-gate  * is not yet known, always read 1k.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate #define	PC_SAFESECSIZE	(PC_SECSIZE * 2)
750Sstevel@tonic-gate 
76201Sjmcp static int pcfs_pseudo_floppy(dev_t);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static int pcfsinit(int, char *);
790Sstevel@tonic-gate static int pcfs_mount(struct vfs *, struct vnode *, struct mounta *,
800Sstevel@tonic-gate 	struct cred *);
810Sstevel@tonic-gate static int pcfs_unmount(struct vfs *, int, struct cred *);
820Sstevel@tonic-gate static int pcfs_root(struct vfs *, struct vnode **);
830Sstevel@tonic-gate static int pcfs_statvfs(struct vfs *, struct statvfs64 *);
840Sstevel@tonic-gate static int pc_syncfsnodes(struct pcfs *);
850Sstevel@tonic-gate static int pcfs_sync(struct vfs *, short, struct cred *);
860Sstevel@tonic-gate static int pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static int pc_getfattype(struct vnode *, int, daddr_t *, int *);
890Sstevel@tonic-gate static int pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start,
900Sstevel@tonic-gate     size_t fatsize);
910Sstevel@tonic-gate static int pc_writefat(struct pcfs *fsp, daddr_t start);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * pcfs mount options table
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char *nohidden_cancel[] = {MNTOPT_PCFS_HIDDEN, NULL};
980Sstevel@tonic-gate static char *hidden_cancel[] = {MNTOPT_PCFS_NOHIDDEN, NULL};
990Sstevel@tonic-gate static char *nofoldcase_cancel[] = {MNTOPT_PCFS_FOLDCASE, NULL};
1000Sstevel@tonic-gate static char *foldcase_cancel[] = {MNTOPT_PCFS_NOFOLDCASE, NULL};
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static mntopt_t mntopts[] = {
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
1050Sstevel@tonic-gate  *		opt data
1060Sstevel@tonic-gate  */
107628Smm156350 	{ MNTOPT_PCFS_NOHIDDEN,	nohidden_cancel, NULL,		0,
1080Sstevel@tonic-gate 		NULL },
109628Smm156350 	{ MNTOPT_PCFS_HIDDEN,	hidden_cancel, NULL,		MO_DEFAULT,
1100Sstevel@tonic-gate 		NULL },
1110Sstevel@tonic-gate 	{ MNTOPT_PCFS_NOFOLDCASE, nofoldcase_cancel, NULL,	MO_DEFAULT,
1120Sstevel@tonic-gate 		NULL },
1130Sstevel@tonic-gate 	{ MNTOPT_PCFS_FOLDCASE,	foldcase_cancel, NULL,		0,
1140Sstevel@tonic-gate 		NULL }
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static mntopts_t pcfs_mntopts = {
1180Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1190Sstevel@tonic-gate 	mntopts
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate int pcfsdebuglevel = 0;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * pcfslock:	protects the list of mounted pc filesystems "pc_mounttab.
1260Sstevel@tonic-gate  * pcfs_lock:	(inside per filesystem structure "pcfs")
1270Sstevel@tonic-gate  *		per filesystem lock. Most of the vfsops and vnodeops are
1280Sstevel@tonic-gate  *		protected by this lock.
1290Sstevel@tonic-gate  * pcnodes_lock: protects the pcnode hash table "pcdhead", "pcfhead".
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  * Lock hierarchy: pcfslock > pcfs_lock > pcnodes_lock
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate kmutex_t	pcfslock;
1340Sstevel@tonic-gate krwlock_t pcnodes_lock; /* protect the pcnode hash table "pcdhead", "pcfhead" */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int pcfstype;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static vfsdef_t vfw = {
1390Sstevel@tonic-gate 	VFSDEF_VERSION,
1400Sstevel@tonic-gate 	"pcfs",
1410Sstevel@tonic-gate 	pcfsinit,
142*1488Srsb 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS,
1430Sstevel@tonic-gate 	&pcfs_mntopts
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static struct modlfs modlfs = {
1490Sstevel@tonic-gate 	&mod_fsops,
150201Sjmcp 	"PC filesystem v%I%",
1510Sstevel@tonic-gate 	&vfw
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static struct modlinkage modlinkage = {
1550Sstevel@tonic-gate 	MODREV_1,
1560Sstevel@tonic-gate 	&modlfs,
1570Sstevel@tonic-gate 	NULL
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate int
1610Sstevel@tonic-gate _init(void)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	int	error;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #if !defined(lint)
1660Sstevel@tonic-gate 	/* make sure the on-disk structures are sane */
1670Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir) == 32);
1680Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir_lfn) == 32);
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 	mutex_init(&pcfslock, NULL, MUTEX_DEFAULT, NULL);
1710Sstevel@tonic-gate 	rw_init(&pcnodes_lock, NULL, RW_DEFAULT, NULL);
1720Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1730Sstevel@tonic-gate 	if (error) {
1740Sstevel@tonic-gate 		mutex_destroy(&pcfslock);
1750Sstevel@tonic-gate 		rw_destroy(&pcnodes_lock);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	return (error);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate int
1810Sstevel@tonic-gate _fini(void)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	int	error;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1860Sstevel@tonic-gate 	if (error)
1870Sstevel@tonic-gate 		return (error);
1880Sstevel@tonic-gate 	mutex_destroy(&pcfslock);
1890Sstevel@tonic-gate 	rw_destroy(&pcnodes_lock);
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Tear down the operations vectors
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(pcfstype);
1940Sstevel@tonic-gate 	vn_freevnodeops(pcfs_fvnodeops);
1950Sstevel@tonic-gate 	vn_freevnodeops(pcfs_dvnodeops);
1960Sstevel@tonic-gate 	return (0);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate int
2000Sstevel@tonic-gate _info(struct modinfo *modinfop)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /* ARGSUSED1 */
2060Sstevel@tonic-gate static int
2070Sstevel@tonic-gate pcfsinit(int fstype, char *name)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	static const fs_operation_def_t pcfs_vfsops_template[] = {
2100Sstevel@tonic-gate 		VFSNAME_MOUNT, pcfs_mount,
2110Sstevel@tonic-gate 		VFSNAME_UNMOUNT, pcfs_unmount,
2120Sstevel@tonic-gate 		VFSNAME_ROOT, pcfs_root,
2130Sstevel@tonic-gate 		VFSNAME_STATVFS, pcfs_statvfs,
2140Sstevel@tonic-gate 		VFSNAME_SYNC, (fs_generic_func_p) pcfs_sync,
2150Sstevel@tonic-gate 		VFSNAME_VGET, pcfs_vget,
2160Sstevel@tonic-gate 		NULL, NULL
2170Sstevel@tonic-gate 	};
2180Sstevel@tonic-gate 	int error;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	error = vfs_setfsops(fstype, pcfs_vfsops_template, NULL);
2210Sstevel@tonic-gate 	if (error != 0) {
2220Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad vfs ops template");
2230Sstevel@tonic-gate 		return (error);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	error = vn_make_ops("pcfs", pcfs_fvnodeops_template, &pcfs_fvnodeops);
2270Sstevel@tonic-gate 	if (error != 0) {
2280Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2290Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad file vnode ops template");
2300Sstevel@tonic-gate 		return (error);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	error = vn_make_ops("pcfsd", pcfs_dvnodeops_template, &pcfs_dvnodeops);
2340Sstevel@tonic-gate 	if (error != 0) {
2350Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2360Sstevel@tonic-gate 		vn_freevnodeops(pcfs_fvnodeops);
2370Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad dir vnode ops template");
2380Sstevel@tonic-gate 		return (error);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	pcfstype = fstype;
2420Sstevel@tonic-gate 	(void) pc_init();
2430Sstevel@tonic-gate 	return (0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static struct pcfs *pc_mounttab = NULL;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate extern struct pcfs_args pc_tz;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  *  Define some special logical drives we use internal to this file.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate #define	BOOT_PARTITION_DRIVE	99
2540Sstevel@tonic-gate #define	PRIMARY_DOS_DRIVE	1
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * pc_mount system call
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate static int
2600Sstevel@tonic-gate pcfs_mount(
2610Sstevel@tonic-gate 	struct vfs *vfsp,
2620Sstevel@tonic-gate 	struct vnode *mvp,
2630Sstevel@tonic-gate 	struct mounta *uap,
2640Sstevel@tonic-gate 	struct cred *cr)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	struct pcfs *fsp;
2670Sstevel@tonic-gate 	struct vnode *bvp;
2680Sstevel@tonic-gate 	struct vnode *devvp;
2690Sstevel@tonic-gate 	struct pathname special;
2700Sstevel@tonic-gate 	daddr_t dosstart;
2710Sstevel@tonic-gate 	dev_t pseudodev;
2720Sstevel@tonic-gate 	dev_t xdev;
2730Sstevel@tonic-gate 	char *spnp;
2740Sstevel@tonic-gate 	char *data = uap->dataptr;
2750Sstevel@tonic-gate 	int datalen = uap->datalen;
2760Sstevel@tonic-gate 	int dos_ldrive = 0;
2770Sstevel@tonic-gate 	int error;
2780Sstevel@tonic-gate 	int fattype;
2790Sstevel@tonic-gate 	int spnlen;
2800Sstevel@tonic-gate 	int wantbootpart = 0;
2810Sstevel@tonic-gate 	struct vioc_info info;
2820Sstevel@tonic-gate 	int rval;		/* set but not used */
2830Sstevel@tonic-gate 	minor_t	minor;
2840Sstevel@tonic-gate 	int oflag, aflag;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
2870Sstevel@tonic-gate 		return (error);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_mount\n");
2900Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
2910Sstevel@tonic-gate 		return (ENOTDIR);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2940Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2950Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
2960Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2970Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2980Sstevel@tonic-gate 		return (EBUSY);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * The caller is responsible for making sure to always
3040Sstevel@tonic-gate 	 * pass in sizeof(struct pcfs_args) (or the old one).
3050Sstevel@tonic-gate 	 * Doing this is the only way to know an EINVAL return
3060Sstevel@tonic-gate 	 * from mount(2) is due to the "not a DOS filesystem"
3070Sstevel@tonic-gate 	 * EINVAL that pc_verify/pc_getfattype could return.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	if ((datalen != sizeof (struct pcfs_args)) &&
3100Sstevel@tonic-gate 	    (datalen != sizeof (struct old_pcfs_args))) {
3110Sstevel@tonic-gate 		return (EINVAL);
3120Sstevel@tonic-gate 	} else {
3130Sstevel@tonic-gate 		struct pcfs_args tmp_tz;
3140Sstevel@tonic-gate 		int hidden = 0;
3150Sstevel@tonic-gate 		int foldcase = 0;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		tmp_tz.flags = 0;
3180Sstevel@tonic-gate 		if (copyin(data, &tmp_tz, datalen)) {
3190Sstevel@tonic-gate 			return (EFAULT);
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 		if (datalen == sizeof (struct pcfs_args)) {
3220Sstevel@tonic-gate 			hidden = tmp_tz.flags & PCFS_MNT_HIDDEN;
3230Sstevel@tonic-gate 			foldcase = tmp_tz.flags & PCFS_MNT_FOLDCASE;
3240Sstevel@tonic-gate 		}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		if (hidden)
3270Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_HIDDEN,	NULL, 0);
3280Sstevel@tonic-gate 		if (foldcase)
3290Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_FOLDCASE, NULL, 0);
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * more than one pc filesystem can be mounted on x86
3320Sstevel@tonic-gate 		 * so the pc_tz structure is now a critical region
3330Sstevel@tonic-gate 		 */
3340Sstevel@tonic-gate 		mutex_enter(&pcfslock);
3350Sstevel@tonic-gate 		if (pc_mounttab == NULL)
3360Sstevel@tonic-gate 			bcopy(&tmp_tz, &pc_tz, sizeof (struct pcfs_args));
3370Sstevel@tonic-gate 		mutex_exit(&pcfslock);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	if (error = pn_get(uap->spec, UIO_USERSPACE, &special)) {
3430Sstevel@tonic-gate 		return (error);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	if (error =
3460Sstevel@tonic-gate 	    lookupname(special.pn_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp)) {
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * look for suffix to special
3490Sstevel@tonic-gate 		 * which indicates a request to mount the solaris boot
3500Sstevel@tonic-gate 		 * partition, or a DOS logical drive on the hard disk
3510Sstevel@tonic-gate 		 */
3520Sstevel@tonic-gate 		spnlen = special.pn_pathlen;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		if (spnlen > 5) {
3550Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 5;
3560Sstevel@tonic-gate 			if (*spnp++ == ':' && *spnp++ == 'b' &&
3570Sstevel@tonic-gate 			    *spnp++ == 'o' && *spnp++ == 'o' &&
3580Sstevel@tonic-gate 			    *spnp++ == 't') {
3590Sstevel@tonic-gate 				/*
3600Sstevel@tonic-gate 				 * Looks as if they want to mount
3610Sstevel@tonic-gate 				 * the Solaris boot partition
3620Sstevel@tonic-gate 				 */
3630Sstevel@tonic-gate 				wantbootpart = 1;
3640Sstevel@tonic-gate 				dos_ldrive = BOOT_PARTITION_DRIVE;
3650Sstevel@tonic-gate 				spnp = special.pn_path + spnlen - 5;
3660Sstevel@tonic-gate 				*spnp = '\0';
3670Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3680Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		if (!wantbootpart) {
3730Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 1;
3740Sstevel@tonic-gate 			if (spnlen > 2 && *spnp >= 'c' && *spnp <= 'z') {
3750Sstevel@tonic-gate 				spnlen--;
3760Sstevel@tonic-gate 				dos_ldrive = *spnp-- - 'c' + 1;
3770Sstevel@tonic-gate 			} else if (spnlen > 2 && *spnp >= '0' && *spnp <= '9') {
3780Sstevel@tonic-gate 				spnlen--;
3790Sstevel@tonic-gate 				dos_ldrive = *spnp-- - '0';
3800Sstevel@tonic-gate 				if (spnlen > 2 && *spnp >= '0' &&
3810Sstevel@tonic-gate 				    *spnp <= '9') {
3820Sstevel@tonic-gate 					spnlen--;
3830Sstevel@tonic-gate 					dos_ldrive += 10 * (*spnp-- - '0');
3840Sstevel@tonic-gate 				}
3850Sstevel@tonic-gate 			}
3860Sstevel@tonic-gate 			if (spnlen > 1 && dos_ldrive && dos_ldrive <= 24 &&
3870Sstevel@tonic-gate 			    *spnp == ':') {
3880Sstevel@tonic-gate 				/*
3890Sstevel@tonic-gate 				 * remove suffix so that we have a real
3900Sstevel@tonic-gate 				 * device name
3910Sstevel@tonic-gate 				 */
3920Sstevel@tonic-gate 				*spnp = '\0';
3930Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3940Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3950Sstevel@tonic-gate 			}
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		if (error) {
3980Sstevel@tonic-gate 			pn_free(&special);
3990Sstevel@tonic-gate 			return (error);
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	pn_free(&special);
4030Sstevel@tonic-gate 	if (bvp->v_type != VBLK) {
4040Sstevel@tonic-gate 		VN_RELE(bvp);
4050Sstevel@tonic-gate 		return (ENOTBLK);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	xdev = bvp->v_rdev;
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * Verify caller's permission to open the device special file.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
4120Sstevel@tonic-gate 	    ((uap->flags & MS_RDONLY) != 0)) {
4130Sstevel@tonic-gate 		oflag = FREAD;
4140Sstevel@tonic-gate 		aflag = VREAD;
4150Sstevel@tonic-gate 	} else {
4160Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
4170Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	if ((error = VOP_ACCESS(bvp, aflag, 0, cr)) != 0 ||
4200Sstevel@tonic-gate 	    (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
4210Sstevel@tonic-gate 		VN_RELE(bvp);
4220Sstevel@tonic-gate 		return (error);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	VN_RELE(bvp);
4260Sstevel@tonic-gate 	if (getmajor(xdev) >= devcnt) {
4270Sstevel@tonic-gate 		return (ENXIO);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Ensure that this device (or logical drive) isn't already mounted,
4310Sstevel@tonic-gate 	 * unless this is a REMOUNT request
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	if (dos_ldrive) {
4340Sstevel@tonic-gate 		mutex_enter(&pcfslock);
4350Sstevel@tonic-gate 		for (fsp = pc_mounttab; fsp; fsp = fsp->pcfs_nxt)
4360Sstevel@tonic-gate 			if (fsp->pcfs_xdev == xdev &&
4370Sstevel@tonic-gate 			    fsp->pcfs_ldrv == dos_ldrive) {
4380Sstevel@tonic-gate 				mutex_exit(&pcfslock);
4390Sstevel@tonic-gate 				if (uap->flags & MS_REMOUNT) {
4400Sstevel@tonic-gate 					return (0);
4410Sstevel@tonic-gate 				} else {
4420Sstevel@tonic-gate 					return (EBUSY);
4430Sstevel@tonic-gate 				}
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 		/*
4460Sstevel@tonic-gate 		 * Assign a unique device number for the vfs
4470Sstevel@tonic-gate 		 * The old way (getudev() + a constantly incrementing
4480Sstevel@tonic-gate 		 * major number) was wrong because it changes vfs_dev
4490Sstevel@tonic-gate 		 * across mounts and reboots, which breaks nfs file handles.
4500Sstevel@tonic-gate 		 * UFS just uses the real dev_t. We can't do that because
4510Sstevel@tonic-gate 		 * of the way pcfs opens fdisk partitons (the :c and :d
4520Sstevel@tonic-gate 		 * partitions are on the same dev_t). Though that _might_
4530Sstevel@tonic-gate 		 * actually be ok, since the file handle contains an
4540Sstevel@tonic-gate 		 * absolute block number, it's probably better to make them
4550Sstevel@tonic-gate 		 * different. So I think we should retain the original
4560Sstevel@tonic-gate 		 * dev_t, but come up with a different minor number based
4570Sstevel@tonic-gate 		 * on the logical drive that will _always_ come up the same.
4580Sstevel@tonic-gate 		 * For now, we steal the upper 6 bits.
4590Sstevel@tonic-gate 		 */
4600Sstevel@tonic-gate #ifdef notdef
4610Sstevel@tonic-gate 		/* what should we do here? */
4620Sstevel@tonic-gate 		if (((getminor(xdev) >> 12) & 0x3F) != 0)
4630Sstevel@tonic-gate 			printf("whoops - upper bits used!\n");
4640Sstevel@tonic-gate #endif
4650Sstevel@tonic-gate 		minor = ((dos_ldrive << 12) | getminor(xdev)) & MAXMIN32;
4660Sstevel@tonic-gate 		pseudodev = makedevice(getmajor(xdev), minor);
4670Sstevel@tonic-gate 		if (vfs_devmounting(pseudodev, vfsp)) {
4680Sstevel@tonic-gate 			mutex_exit(&pcfslock);
4690Sstevel@tonic-gate 			return (EBUSY);
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 		if (vfs_devismounted(pseudodev)) {
4720Sstevel@tonic-gate 			mutex_exit(&pcfslock);
4730Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
4740Sstevel@tonic-gate 				return (0);
4750Sstevel@tonic-gate 			} else {
4760Sstevel@tonic-gate 				return (EBUSY);
4770Sstevel@tonic-gate 			}
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		mutex_exit(&pcfslock);
4800Sstevel@tonic-gate 	} else {
4810Sstevel@tonic-gate 		if (vfs_devmounting(xdev, vfsp)) {
4820Sstevel@tonic-gate 			return (EBUSY);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 		if (vfs_devismounted(xdev))
4850Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
4860Sstevel@tonic-gate 				return (0);
4870Sstevel@tonic-gate 			} else {
4880Sstevel@tonic-gate 				return (EBUSY);
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 		pseudodev = xdev;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
4940Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4950Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * Mount the filesystem
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	devvp = makespecvp(xdev, VBLK);
5020Sstevel@tonic-gate 	if (IS_SWAPVP(devvp)) {
5030Sstevel@tonic-gate 		VN_RELE(devvp);
5040Sstevel@tonic-gate 		return (EBUSY);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * special handling for PCMCIA memory card
509201Sjmcp 	 * with pseudo floppies organization
5100Sstevel@tonic-gate 	 */
511201Sjmcp 	if (dos_ldrive == 0 && pcfs_pseudo_floppy(xdev)) {
5120Sstevel@tonic-gate 		dosstart = (daddr_t)0;
5130Sstevel@tonic-gate 		fattype = PCFS_PCMCIA_NO_CIS;
5140Sstevel@tonic-gate 	} else {
5150Sstevel@tonic-gate 		if (error = pc_getfattype(devvp, dos_ldrive, &dosstart,
5160Sstevel@tonic-gate 		    &fattype)) {
5170Sstevel@tonic-gate 			VN_RELE(devvp);
5180Sstevel@tonic-gate 			return (error);
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0, B_INVAL, cr);
5230Sstevel@tonic-gate 	fsp = kmem_zalloc((uint_t)sizeof (struct pcfs), KM_SLEEP);
5240Sstevel@tonic-gate 	fsp->pcfs_vfs = vfsp;
5250Sstevel@tonic-gate 	fsp->pcfs_flags = fattype;
5260Sstevel@tonic-gate 	fsp->pcfs_devvp = devvp;
5270Sstevel@tonic-gate 	fsp->pcfs_xdev = xdev;
5280Sstevel@tonic-gate 	fsp->pcfs_ldrv = dos_ldrive;
5290Sstevel@tonic-gate 	fsp->pcfs_dosstart = dosstart;
5300Sstevel@tonic-gate 	mutex_init(&fsp->pcfs_lock, NULL, MUTEX_DEFAULT, NULL);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/* set the "nocheck" flag if volmgt is managing this volume */
5330Sstevel@tonic-gate 	info.vii_pathlen = 0;
5340Sstevel@tonic-gate 	info.vii_devpath = 0;
5350Sstevel@tonic-gate 	error = cdev_ioctl(fsp->pcfs_xdev, VOLIOCINFO, (intptr_t)&info,
5360Sstevel@tonic-gate 	    FKIOCTL|FREAD, kcred, &rval);
5370Sstevel@tonic-gate 	if (error == 0) {
5380Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_NOCHK;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_HIDDEN, NULL))
5420Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_HIDDEN;
5430Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_FOLDCASE, NULL))
5440Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_FOLDCASE;
5450Sstevel@tonic-gate 	vfsp->vfs_dev = pseudodev;
5460Sstevel@tonic-gate 	vfsp->vfs_fstype = pcfstype;
5470Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, pseudodev, pcfstype);
5480Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)fsp;
5490Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	error = pc_verify(fsp);
5520Sstevel@tonic-gate 	if (error) {
5530Sstevel@tonic-gate 		VN_RELE(devvp);
5540Sstevel@tonic-gate 		mutex_destroy(&fsp->pcfs_lock);
5550Sstevel@tonic-gate 		kmem_free(fsp, (uint_t)sizeof (struct pcfs));
5560Sstevel@tonic-gate 		return (error);
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->pcfs_clsize;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	mutex_enter(&pcfslock);
5610Sstevel@tonic-gate 	fsp->pcfs_nxt = pc_mounttab;
5620Sstevel@tonic-gate 	pc_mounttab = fsp;
5630Sstevel@tonic-gate 	mutex_exit(&pcfslock);
5640Sstevel@tonic-gate 	return (0);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * vfs operations
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /* ARGSUSED */
5720Sstevel@tonic-gate static int
5730Sstevel@tonic-gate pcfs_unmount(
5740Sstevel@tonic-gate 	struct vfs *vfsp,
5750Sstevel@tonic-gate 	int flag,
5760Sstevel@tonic-gate 	struct cred *cr)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	struct pcfs *fsp, *fsp1;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
5810Sstevel@tonic-gate 		return (EPERM);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
5850Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	if (flag & MS_FORCE)
5880Sstevel@tonic-gate 		return (ENOTSUP);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_unmount\n");
5910Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * We don't have to lock fsp because the VVFSLOCK in vfs layer will
5940Sstevel@tonic-gate 	 * prevent lookuppn from crossing the mount point.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	if (fsp->pcfs_nrefs) {
5970Sstevel@tonic-gate 		return (EBUSY);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Allow an unmount (regardless of state) if the fs instance has
6020Sstevel@tonic-gate 	 * been marked as beyond recovery.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_IRRECOV) {
6050Sstevel@tonic-gate 		mutex_enter(&pcfslock);
6060Sstevel@tonic-gate 		rw_enter(&pcnodes_lock, RW_WRITER);
6070Sstevel@tonic-gate 		pc_diskchanged(fsp);
6080Sstevel@tonic-gate 		rw_exit(&pcnodes_lock);
6090Sstevel@tonic-gate 		mutex_exit(&pcfslock);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/* now there should be no pcp node on pcfhead or pcdhead. */
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	mutex_enter(&pcfslock);
6150Sstevel@tonic-gate 	if (fsp == pc_mounttab) {
6160Sstevel@tonic-gate 		pc_mounttab = fsp->pcfs_nxt;
6170Sstevel@tonic-gate 	} else {
6180Sstevel@tonic-gate 		for (fsp1 = pc_mounttab; fsp1 != NULL; fsp1 = fsp1->pcfs_nxt)
6190Sstevel@tonic-gate 			if (fsp1->pcfs_nxt == fsp)
6200Sstevel@tonic-gate 				fsp1->pcfs_nxt = fsp->pcfs_nxt;
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (fsp->pcfs_fatp != (uchar_t *)0) {
6240Sstevel@tonic-gate 		pc_invalfat(fsp);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	mutex_exit(&pcfslock);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	VN_RELE(fsp->pcfs_devvp);
6290Sstevel@tonic-gate 	mutex_destroy(&fsp->pcfs_lock);
6300Sstevel@tonic-gate 	kmem_free(fsp, (uint_t)sizeof (struct pcfs));
6310Sstevel@tonic-gate 	return (0);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * find root of pcfs
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate static int
6380Sstevel@tonic-gate pcfs_root(
6390Sstevel@tonic-gate 	struct vfs *vfsp,
6400Sstevel@tonic-gate 	struct vnode **vpp)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	struct pcfs *fsp;
6430Sstevel@tonic-gate 	struct pcnode *pcp;
6440Sstevel@tonic-gate 	int error;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6470Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
6480Sstevel@tonic-gate 		return (error);
6490Sstevel@tonic-gate 	pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
6500Sstevel@tonic-gate 	PC_DPRINTF2(9, "pcfs_root(0x%p) pcp= 0x%p\n",
6510Sstevel@tonic-gate 	    (void *)vfsp, (void *)pcp);
6520Sstevel@tonic-gate 	pc_unlockfs(fsp);
6530Sstevel@tonic-gate 	*vpp = PCTOV(pcp);
6540Sstevel@tonic-gate 	pcp->pc_flags |= PC_EXTERNAL;
6550Sstevel@tonic-gate 	return (0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * Get file system statistics.
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate static int
6620Sstevel@tonic-gate pcfs_statvfs(
6630Sstevel@tonic-gate 	struct vfs *vfsp,
6640Sstevel@tonic-gate 	struct statvfs64 *sp)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	struct pcfs *fsp;
6670Sstevel@tonic-gate 	int error;
6680Sstevel@tonic-gate 	dev32_t d32;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6710Sstevel@tonic-gate 	error = pc_getfat(fsp);
6720Sstevel@tonic-gate 	if (error)
6730Sstevel@tonic-gate 		return (error);
6740Sstevel@tonic-gate 	bzero(sp, sizeof (*sp));
6750Sstevel@tonic-gate 	sp->f_bsize = sp->f_frsize = fsp->pcfs_clsize;
6760Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->pcfs_ncluster;
6770Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree = (fsblkcnt64_t)pc_freeclusters(fsp);
6780Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)-1;
6790Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)-1;
6800Sstevel@tonic-gate 	sp->f_favail = (fsfilcnt64_t)-1;
6810Sstevel@tonic-gate #ifdef notdef
6820Sstevel@tonic-gate 	(void) cmpldev(&d32, fsp->pcfs_devvp->v_rdev);
6830Sstevel@tonic-gate #endif /* notdef */
6840Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
6850Sstevel@tonic-gate 	sp->f_fsid = d32;
6860Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
6870Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
6880Sstevel@tonic-gate 	sp->f_namemax = PCFNAMESIZE;
6890Sstevel@tonic-gate 	return (0);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate static int
6930Sstevel@tonic-gate pc_syncfsnodes(struct pcfs *fsp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	struct pchead *hp;
6960Sstevel@tonic-gate 	struct pcnode *pcp;
6970Sstevel@tonic-gate 	int error;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfsnodes\n");
7000Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
7010Sstevel@tonic-gate 		return (error);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (!(error = pc_syncfat(fsp))) {
7040Sstevel@tonic-gate 		hp = pcfhead;
7050Sstevel@tonic-gate 		while (hp < & pcfhead [ NPCHASH ]) {
7060Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_READER);
7070Sstevel@tonic-gate 			pcp = hp->pch_forw;
7080Sstevel@tonic-gate 			while (pcp != (struct pcnode *)hp) {
7090Sstevel@tonic-gate 				if (VFSTOPCFS(PCTOV(pcp) -> v_vfsp) == fsp)
7100Sstevel@tonic-gate 					if (error = pc_nodesync(pcp))
7110Sstevel@tonic-gate 						break;
7120Sstevel@tonic-gate 				pcp = pcp -> pc_forw;
7130Sstevel@tonic-gate 			}
7140Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7150Sstevel@tonic-gate 			if (error)
7160Sstevel@tonic-gate 				break;
7170Sstevel@tonic-gate 			hp++;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 	pc_unlockfs(fsp);
7210Sstevel@tonic-gate 	return (error);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * Flush any pending I/O.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate /*ARGSUSED*/
7280Sstevel@tonic-gate static int
7290Sstevel@tonic-gate pcfs_sync(
7300Sstevel@tonic-gate 	struct vfs *vfsp,
7310Sstevel@tonic-gate 	short flag,
7320Sstevel@tonic-gate 	struct cred *cr)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	struct pcfs *fsp;
7350Sstevel@tonic-gate 	int error = 0;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/* this prevents the filesystem from being umounted. */
7380Sstevel@tonic-gate 	mutex_enter(&pcfslock);
7390Sstevel@tonic-gate 	if (vfsp != NULL) {
7400Sstevel@tonic-gate 		fsp = VFSTOPCFS(vfsp);
7410Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_IRRECOV)) {
7420Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7430Sstevel@tonic-gate 		} else {
7440Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_WRITER);
7450Sstevel@tonic-gate 			pc_diskchanged(fsp);
7460Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7470Sstevel@tonic-gate 			error = EIO;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 	} else {
7500Sstevel@tonic-gate 		fsp = pc_mounttab;
7510Sstevel@tonic-gate 		while (fsp != NULL) {
7520Sstevel@tonic-gate 			if (fsp->pcfs_flags & PCFS_IRRECOV) {
7530Sstevel@tonic-gate 				rw_enter(&pcnodes_lock, RW_WRITER);
7540Sstevel@tonic-gate 				pc_diskchanged(fsp);
7550Sstevel@tonic-gate 				rw_exit(&pcnodes_lock);
7560Sstevel@tonic-gate 				error = EIO;
7570Sstevel@tonic-gate 				break;
7580Sstevel@tonic-gate 			}
7590Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7600Sstevel@tonic-gate 			if (error) break;
7610Sstevel@tonic-gate 			fsp = fsp->pcfs_nxt;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 	mutex_exit(&pcfslock);
7650Sstevel@tonic-gate 	return (error);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate int
7690Sstevel@tonic-gate pc_lockfs(struct pcfs *fsp, int diskchanged, int releasing)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_IRRECOV) && !releasing)
7720Sstevel@tonic-gate 		return (EIO);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) && (fsp->pcfs_owner == curthread)) {
7750Sstevel@tonic-gate 		fsp->pcfs_count++;
7760Sstevel@tonic-gate 	} else {
7770Sstevel@tonic-gate 		mutex_enter(&fsp->pcfs_lock);
7780Sstevel@tonic-gate 		if (fsp->pcfs_flags & PCFS_LOCKED)
7790Sstevel@tonic-gate 			panic("pc_lockfs");
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * We check the IRRECOV bit again just in case somebody
7820Sstevel@tonic-gate 		 * snuck past the initial check but then got held up before
7830Sstevel@tonic-gate 		 * they could grab the lock.  (And in the meantime someone
7840Sstevel@tonic-gate 		 * had grabbed the lock and set the bit)
7850Sstevel@tonic-gate 		 */
786607Swyllys 		if (!diskchanged && !(fsp->pcfs_flags & PCFS_IRRECOV)) {
787607Swyllys 			int err;
788607Swyllys 			if ((err = pc_getfat(fsp)))
789607Swyllys 				return (err);
790607Swyllys 		}
7910Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_LOCKED;
7920Sstevel@tonic-gate 		fsp->pcfs_owner = curthread;
7930Sstevel@tonic-gate 		fsp->pcfs_count++;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 	return (0);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate void
7990Sstevel@tonic-gate pc_unlockfs(struct pcfs *fsp)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) == 0)
8030Sstevel@tonic-gate 		panic("pc_unlockfs");
8040Sstevel@tonic-gate 	if (--fsp->pcfs_count < 0)
8050Sstevel@tonic-gate 		panic("pc_unlockfs: count");
8060Sstevel@tonic-gate 	if (fsp->pcfs_count == 0) {
8070Sstevel@tonic-gate 		fsp->pcfs_flags &= ~PCFS_LOCKED;
8080Sstevel@tonic-gate 		fsp->pcfs_owner = 0;
8090Sstevel@tonic-gate 		mutex_exit(&fsp->pcfs_lock);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate /*
8140Sstevel@tonic-gate  * isDosDrive()
8150Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8160Sstevel@tonic-gate  *	and it decides if that's a systid that describes a DOS drive.  We
8170Sstevel@tonic-gate  *	use systid values defined in sys/dktp/fdisk.h.
8180Sstevel@tonic-gate  */
8190Sstevel@tonic-gate static int
8200Sstevel@tonic-gate isDosDrive(uchar_t checkMe)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
8230Sstevel@tonic-gate 	    (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
8240Sstevel@tonic-gate 	    (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
8250Sstevel@tonic-gate 	    (checkMe == DIAGPART));
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate  * isDosExtended()
8300Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8310Sstevel@tonic-gate  *	and it decides if that's a systid that describes an extended DOS
8320Sstevel@tonic-gate  *	partition.
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate static int
8350Sstevel@tonic-gate isDosExtended(uchar_t checkMe)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate  * isBootPart()
8420Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8430Sstevel@tonic-gate  *	and it decides if that's a systid that describes a Solaris boot
8440Sstevel@tonic-gate  *	partition.
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate static int
8470Sstevel@tonic-gate isBootPart(uchar_t checkMe)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	return (checkMe == X86BOOT);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate  * noLogicalDrive()
8540Sstevel@tonic-gate  *	Display error message about not being able to find a logical
8550Sstevel@tonic-gate  *	drive.
8560Sstevel@tonic-gate  */
8570Sstevel@tonic-gate static void
8580Sstevel@tonic-gate noLogicalDrive(int requested)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	if (requested == BOOT_PARTITION_DRIVE) {
8610Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no boot partition");
8620Sstevel@tonic-gate 	} else {
8630Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no such logical drive");
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate  * findTheDrive()
8690Sstevel@tonic-gate  *	Discover offset of the requested logical drive, and return
8700Sstevel@tonic-gate  *	that offset (startSector), the systid of that drive (sysid),
8710Sstevel@tonic-gate  *	and a buffer pointer (bp), with the buffer contents being
8720Sstevel@tonic-gate  *	the first sector of the logical drive (i.e., the sector that
8730Sstevel@tonic-gate  *	contains the BPB for that drive).
8740Sstevel@tonic-gate  */
8750Sstevel@tonic-gate static int
8760Sstevel@tonic-gate findTheDrive(dev_t dev, int askedFor, int *error, buf_t **bp,
8770Sstevel@tonic-gate     daddr_t *startSector, uchar_t *sysid)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	struct ipart dosp[FD_NUMPART];	/* incore fdisk partition structure */
8800Sstevel@tonic-gate 	struct mboot *dosp_ptr;		/* boot structure pointer */
8810Sstevel@tonic-gate 	daddr_t lastseek = 0;		/* Disk block we sought previously */
8820Sstevel@tonic-gate 	daddr_t diskblk = 0;		/* Disk block to get */
8830Sstevel@tonic-gate 	daddr_t xstartsect;		/* base of Extended DOS partition */
8840Sstevel@tonic-gate 	int logicalDriveCount = 0;	/* Count of logical drives seen */
8850Sstevel@tonic-gate 	int extendedPart = -1;		/* index of extended dos partition */
8860Sstevel@tonic-gate 	int primaryPart = -1;		/* index of primary dos partition */
8870Sstevel@tonic-gate 	int bootPart = -1;		/* index of a Solaris boot partition */
8880Sstevel@tonic-gate 	int xnumsect = -1;		/* length of extended DOS partition */
8890Sstevel@tonic-gate 	int driveIndex;			/* computed FDISK table index */
8900Sstevel@tonic-gate 	int i;
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * Count of drives in the current extended partition's
8930Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
8940Sstevel@tonic-gate 	 */
8950Sstevel@tonic-gate 	int extndDrives[FD_NUMPART];
8960Sstevel@tonic-gate 	int numDrives = 0;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8990Sstevel@tonic-gate 	 * Count of drives (beyond primary) in master boot record's
9000Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	int extraDrives[FD_NUMPART];
9030Sstevel@tonic-gate 	int numExtraDrives = 0;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/*
9060Sstevel@tonic-gate 	 *  Copy from disk block into memory aligned structure for fdisk usage.
9070Sstevel@tonic-gate 	 */
9080Sstevel@tonic-gate 	dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
9090Sstevel@tonic-gate 	bcopy(dosp_ptr->parts, dosp, sizeof (struct ipart) * FD_NUMPART);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/*
9120Sstevel@tonic-gate 	 * Get a summary of what is in the Master FDISK table.
9130Sstevel@tonic-gate 	 * Normally we expect to find one partition marked as a DOS drive.
9140Sstevel@tonic-gate 	 * This partition is the one Windows calls the primary dos partition.
9150Sstevel@tonic-gate 	 * If the machine has any logical drives then we also expect
9160Sstevel@tonic-gate 	 * to find a partition marked as an extended DOS partition.
9170Sstevel@tonic-gate 	 *
9180Sstevel@tonic-gate 	 * Sometimes we'll find multiple partitions marked as DOS drives.
9190Sstevel@tonic-gate 	 * The Solaris fdisk program allows these partitions
9200Sstevel@tonic-gate 	 * to be created, but Windows fdisk no longer does.  We still need
9210Sstevel@tonic-gate 	 * to support these, though, since Windows does.  We also need to fix
9220Sstevel@tonic-gate 	 * our fdisk to behave like the Windows version.
9230Sstevel@tonic-gate 	 *
9240Sstevel@tonic-gate 	 * It turns out that some off-the-shelf media have *only* an
9250Sstevel@tonic-gate 	 * Extended partition, so we need to deal with that case as well.
9260Sstevel@tonic-gate 	 *
9270Sstevel@tonic-gate 	 * Only a single (the first) Extended or Boot Partition will
9280Sstevel@tonic-gate 	 * be recognized.  Any others will be ignored.
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
931607Swyllys 		PC_DPRINTF1(2, "findTheDrive: found partition type %02x",
932607Swyllys 			dosp[i].systid);
933607Swyllys 
9340Sstevel@tonic-gate 		if (isDosDrive(dosp[i].systid)) {
9350Sstevel@tonic-gate 			if (primaryPart < 0) {
9360Sstevel@tonic-gate 				logicalDriveCount++;
9370Sstevel@tonic-gate 				primaryPart = i;
9380Sstevel@tonic-gate 			} else {
9390Sstevel@tonic-gate 				extraDrives[numExtraDrives++] = i;
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate 			continue;
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		if ((extendedPart < 0) && isDosExtended(dosp[i].systid)) {
9440Sstevel@tonic-gate 			extendedPart = i;
9450Sstevel@tonic-gate 			continue;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		if ((bootPart < 0) && isBootPart(dosp[i].systid)) {
9480Sstevel@tonic-gate 			bootPart = i;
9490Sstevel@tonic-gate 			continue;
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	if (askedFor == BOOT_PARTITION_DRIVE) {
9540Sstevel@tonic-gate 		if (bootPart < 0) {
9550Sstevel@tonic-gate 			noLogicalDrive(askedFor);
9560Sstevel@tonic-gate 			*error = EINVAL;
9570Sstevel@tonic-gate 			return (0);
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 		*sysid = dosp[bootPart].systid;
9600Sstevel@tonic-gate 		*startSector = ltohi(dosp[bootPart].relsect);
9610Sstevel@tonic-gate 		return (1);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	if (askedFor == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
9650Sstevel@tonic-gate 		*sysid = dosp[primaryPart].systid;
9660Sstevel@tonic-gate 		*startSector = ltohi(dosp[primaryPart].relsect);
9670Sstevel@tonic-gate 		return (1);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * We are not looking for the C: drive (or the primary drive
9720Sstevel@tonic-gate 	 * was not found), so we had better have an extended partition
9730Sstevel@tonic-gate 	 * or extra drives in the Master FDISK table.
9740Sstevel@tonic-gate 	 */
9750Sstevel@tonic-gate 	if ((extendedPart < 0) && (numExtraDrives == 0)) {
9760Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no extended dos partition");
9770Sstevel@tonic-gate 		noLogicalDrive(askedFor);
9780Sstevel@tonic-gate 		*error = EINVAL;
9790Sstevel@tonic-gate 		return (0);
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	if (extendedPart >= 0) {
9830Sstevel@tonic-gate 		diskblk = xstartsect = ltohi(dosp[extendedPart].relsect);
9840Sstevel@tonic-gate 		xnumsect = ltohi(dosp[extendedPart].numsect);
9850Sstevel@tonic-gate 		do {
9860Sstevel@tonic-gate 			/*
9870Sstevel@tonic-gate 			 *  If the seek would not cause us to change
9880Sstevel@tonic-gate 			 *  position on the drive, then we're out of
9890Sstevel@tonic-gate 			 *  extended partitions to examine.
9900Sstevel@tonic-gate 			 */
9910Sstevel@tonic-gate 			if (diskblk == lastseek)
9920Sstevel@tonic-gate 				break;
9930Sstevel@tonic-gate 			logicalDriveCount += numDrives;
9940Sstevel@tonic-gate 			/*
9950Sstevel@tonic-gate 			 *  Seek the next extended partition, and find
9960Sstevel@tonic-gate 			 *  logical drives within it.
9970Sstevel@tonic-gate 			 */
9980Sstevel@tonic-gate 			brelse(*bp);
9990Sstevel@tonic-gate 			*bp = bread(dev, diskblk, PC_SAFESECSIZE);
10000Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10010Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10020Sstevel@tonic-gate 				*error = EIO;
10030Sstevel@tonic-gate 				return (0);
10040Sstevel@tonic-gate 			}
10050Sstevel@tonic-gate 			lastseek = diskblk;
10060Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10070Sstevel@tonic-gate 			if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
10080Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: "
10090Sstevel@tonic-gate 				    "extended partition signature err");
10100Sstevel@tonic-gate 				*error = EINVAL;
10110Sstevel@tonic-gate 				return (0);
10120Sstevel@tonic-gate 			}
10130Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10140Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10150Sstevel@tonic-gate 			/*
10160Sstevel@tonic-gate 			 *  Count up drives, and track where the next
10170Sstevel@tonic-gate 			 *  extended partition is in case we need it.  We
10180Sstevel@tonic-gate 			 *  are expecting only one extended partition.  If
10190Sstevel@tonic-gate 			 *  there is more than one we'll only go to the
10200Sstevel@tonic-gate 			 *  first one we see, but warn about ignoring.
10210Sstevel@tonic-gate 			 */
10220Sstevel@tonic-gate 			numDrives = 0;
10230Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
10240Sstevel@tonic-gate 				if (isDosDrive(dosp[i].systid)) {
10250Sstevel@tonic-gate 					extndDrives[numDrives++] = i;
10260Sstevel@tonic-gate 					continue;
10270Sstevel@tonic-gate 				} else if (isDosExtended(dosp[i].systid)) {
10280Sstevel@tonic-gate 					if (diskblk != lastseek) {
10290Sstevel@tonic-gate 						/*
10300Sstevel@tonic-gate 						 * Already found an extended
10310Sstevel@tonic-gate 						 * partition in this table.
10320Sstevel@tonic-gate 						 */
10330Sstevel@tonic-gate 						cmn_err(CE_NOTE,
10340Sstevel@tonic-gate 						    "!pcfs: ignoring unexpected"
10350Sstevel@tonic-gate 						    " additional extended"
10360Sstevel@tonic-gate 						    " partition");
10370Sstevel@tonic-gate 						continue;
10380Sstevel@tonic-gate 					}
10390Sstevel@tonic-gate 					diskblk = xstartsect +
10400Sstevel@tonic-gate 					    ltohi(dosp[i].relsect);
10410Sstevel@tonic-gate 					continue;
10420Sstevel@tonic-gate 				}
10430Sstevel@tonic-gate 			}
10440Sstevel@tonic-gate 		} while (askedFor > logicalDriveCount + numDrives);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		if (askedFor <= logicalDriveCount + numDrives) {
10470Sstevel@tonic-gate 			/*
10480Sstevel@tonic-gate 			 * The number of logical drives we've found thus
10490Sstevel@tonic-gate 			 * far is enough to get us to the one we were
10500Sstevel@tonic-gate 			 * searching for.
10510Sstevel@tonic-gate 			 */
10520Sstevel@tonic-gate 			driveIndex = logicalDriveCount + numDrives - askedFor;
10530Sstevel@tonic-gate 			*sysid = dosp[extndDrives[driveIndex]].systid;
10540Sstevel@tonic-gate 			*startSector =
10550Sstevel@tonic-gate 			    ltohi(dosp[extndDrives[driveIndex]].relsect) +
10560Sstevel@tonic-gate 			    lastseek;
10570Sstevel@tonic-gate 			if (*startSector > (xstartsect + xnumsect)) {
10580Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: extended partition "
10590Sstevel@tonic-gate 				    "values bad");
10600Sstevel@tonic-gate 				*error = EINVAL;
10610Sstevel@tonic-gate 				return (0);
10620Sstevel@tonic-gate 			}
10630Sstevel@tonic-gate 			return (1);
10640Sstevel@tonic-gate 		} else {
10650Sstevel@tonic-gate 			/*
10660Sstevel@tonic-gate 			 * We ran out of extended dos partition
10670Sstevel@tonic-gate 			 * drives.  The only hope now is to go
10680Sstevel@tonic-gate 			 * back to extra drives defined in the master
10690Sstevel@tonic-gate 			 * fdisk table.  But we overwrote that table
10700Sstevel@tonic-gate 			 * already, so we must load it in again.
10710Sstevel@tonic-gate 			 */
10720Sstevel@tonic-gate 			logicalDriveCount += numDrives;
10730Sstevel@tonic-gate 			brelse(*bp);
10740Sstevel@tonic-gate 			*bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
10750Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10760Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10770Sstevel@tonic-gate 				*error = EIO;
10780Sstevel@tonic-gate 				return (0);
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10810Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10820Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 *  Still haven't found the drive, is it an extra
10870Sstevel@tonic-gate 	 *  drive defined in the main FDISK table?
10880Sstevel@tonic-gate 	 */
10890Sstevel@tonic-gate 	if (askedFor <= logicalDriveCount + numExtraDrives) {
10900Sstevel@tonic-gate 		driveIndex = logicalDriveCount + numExtraDrives - askedFor;
10910Sstevel@tonic-gate 		*sysid = dosp[extraDrives[driveIndex]].systid;
10920Sstevel@tonic-gate 		*startSector = ltohi(dosp[extraDrives[driveIndex]].relsect);
10930Sstevel@tonic-gate 		return (1);
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 *  Still haven't found the drive, and there is
10970Sstevel@tonic-gate 	 *  nowhere else to look.
10980Sstevel@tonic-gate 	 */
10990Sstevel@tonic-gate 	noLogicalDrive(askedFor);
11000Sstevel@tonic-gate 	*error = EINVAL;
11010Sstevel@tonic-gate 	return (0);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
1105607Swyllys  * FAT12/FAT16 specific consistency checks.
1106607Swyllys  */
1107607Swyllys static int
1108607Swyllys check_bpb_fat16(struct bootsec *bpb)
1109607Swyllys {
1110607Swyllys 	if (pcfsdebuglevel >= 5) {
1111607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat: RootEntCount = %d",
1112607Swyllys 			ltohs(bpb->rdirents[0]));
1113607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec16 = %d",
1114607Swyllys 			ltohs(bpb->numsect[0]));
1115607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat16: FATSz16 = %d",
1116607Swyllys 			ltohs(bpb->fatsec));
1117607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec32 = %d",
1118607Swyllys 			ltohi(bpb->totalsec));
1119607Swyllys 	}
1120607Swyllys 	return (ltohs(bpb->rdirents[0]) > 0 &&	 /* RootEntCnt > 0 */
1121607Swyllys 		((ltohs(bpb->numsect[0]) == 0 && /* TotSec16 == 0 */
1122607Swyllys 		ltohi(bpb->totalsec) > 0) ||	 /* TotSec32 > 0 */
1123607Swyllys 		ltohs(bpb->numsect[0]) > 0) && /* TotSec16 > 0 */
1124607Swyllys 		ltohs(bpb->fatsec) > 0);	 /* FatSz16 > 0 */
1125607Swyllys }
1126607Swyllys 
1127607Swyllys /*
1128607Swyllys  * FAT32 specific consistency checks.
1129607Swyllys  */
1130607Swyllys static int
1131607Swyllys check_bpb_fat32(struct fat32_bootsec *bpb)
1132607Swyllys {
1133607Swyllys 	if (pcfsdebuglevel >= 5) {
1134607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat32: RootEntCount = %d",
1135607Swyllys 			ltohs(bpb->f_bs.rdirents[0]));
1136607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec16 = %d",
1137607Swyllys 			ltohs(bpb->f_bs.numsect[0]));
1138607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz16 = %d",
1139607Swyllys 			ltohs(bpb->f_bs.fatsec));
1140607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec32 = %d",
1141607Swyllys 			ltohi(bpb->f_bs.totalsec));
1142607Swyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz32 = %d",
1143607Swyllys 			ltohi(bpb->f_fatlength));
1144607Swyllys 	}
1145607Swyllys 	return (ltohs(bpb->f_bs.rdirents[0]) == 0 &&
1146607Swyllys 		ltohs(bpb->f_bs.numsect[0]) == 0 &&
1147607Swyllys 		ltohs(bpb->f_bs.fatsec) == 0 &&
1148607Swyllys 		ltohi(bpb->f_bs.totalsec) > 0 &&
1149607Swyllys 		ltohi(bpb->f_fatlength) > 0);
1150607Swyllys }
1151607Swyllys 
1152607Swyllys /*
1153607Swyllys  * Calculate the number of clusters in order to determine
1154607Swyllys  * the type of FAT we are looking at.  This is the only
1155607Swyllys  * recommended way of determining FAT type, though there
1156607Swyllys  * are other hints in the data, this is the best way.
1157607Swyllys  */
1158607Swyllys static ulong_t
1159607Swyllys bpb_to_numclusters(uchar_t *cp)
1160607Swyllys {
1161607Swyllys 	struct fat32_bootsec *bpb;
1162607Swyllys 
1163607Swyllys 	ulong_t rootdirsectors;
1164607Swyllys 	ulong_t FATsz;
1165607Swyllys 	ulong_t TotSec;
1166607Swyllys 	ulong_t DataSec;
1167607Swyllys 	ulong_t CountOfClusters;
1168607Swyllys 	char FileSysType[9];
1169607Swyllys 
1170607Swyllys 	/*
1171607Swyllys 	 * Cast it to FAT32 bpb. If it turns out to be FAT12/16, its
1172607Swyllys 	 * OK, we won't try accessing the data beyond the FAT16 header
1173607Swyllys 	 * boundary.
1174607Swyllys 	 */
1175607Swyllys 	bpb = (struct fat32_bootsec *)cp;
1176607Swyllys 
1177607Swyllys 	if (pcfsdebuglevel >= 5) {
1178607Swyllys 		if (ltohs(bpb->f_bs.rdirents[0]) != 0) {
1179608Swyllys 			(void) memcpy(FileSysType, &cp[54], 8);
1180607Swyllys 			FileSysType[8] = 0;
1181607Swyllys 			PC_DPRINTF1(5, "debug_bpb: FAT12/FAT16 FileSysType = "
1182607Swyllys 				"%s", FileSysType);
1183607Swyllys 		}
1184607Swyllys 	}
1185607Swyllys 
1186607Swyllys 	rootdirsectors = ((ltohs(bpb->f_bs.rdirents[0]) * 32) +
1187607Swyllys 		(ltohs(bpb->f_bs.bps[0]) - 1)) / ltohs(bpb->f_bs.bps[0]);
1188607Swyllys 
1189607Swyllys 	if (ltohs(bpb->f_bs.fatsec) != 0)
1190607Swyllys 		FATsz = ltohs(bpb->f_bs.fatsec);
1191607Swyllys 	else
1192607Swyllys 		FATsz = ltohi(bpb->f_fatlength);
1193607Swyllys 
1194607Swyllys 	if (ltohs(bpb->f_bs.numsect[0]) != 0)
1195607Swyllys 		TotSec = ltohs(bpb->f_bs.numsect[0]);
1196607Swyllys 	else
1197607Swyllys 		TotSec = ltohi(bpb->f_bs.totalsec);
1198607Swyllys 
1199607Swyllys 	DataSec = TotSec - (ltohs(bpb->f_bs.res_sec[0]) +
1200607Swyllys 			(bpb->f_bs.nfat * FATsz) + rootdirsectors);
1201607Swyllys 
1202607Swyllys 	CountOfClusters = DataSec / bpb->f_bs.spcl;
1203607Swyllys 
1204607Swyllys 	PC_DPRINTF1(5, "debug_bpb: CountOfClusters = %ld", CountOfClusters);
1205607Swyllys 
1206607Swyllys 	return (CountOfClusters);
1207607Swyllys 
1208607Swyllys }
1209607Swyllys 
1210607Swyllys static int
1211607Swyllys fattype(ulong_t CountOfClusters)
1212607Swyllys {
1213607Swyllys 	/*
1214607Swyllys 	 * From Microsoft:
1215607Swyllys 	 * In the following example, when it says <, it does not mean <=.
1216607Swyllys 	 * Note also that the numbers are correct.  The first number for
1217607Swyllys 	 * FAT12 is 4085; the second number for FAT16 is 65525. These numbers
1218607Swyllys 	 * and the '<' signs are not wrong.
1219607Swyllys 	 */
1220607Swyllys 
1221607Swyllys 	/* Watch for edge cases */
1222607Swyllys 	if ((CountOfClusters >= 4085 && CountOfClusters <= 4095) ||
1223607Swyllys 	    (CountOfClusters >= 65525 && CountOfClusters <= 65535)) {
1224607Swyllys 		PC_DPRINTF1(5, "debug_bpb: Cannot determine FAT yet - %ld",
1225607Swyllys 			CountOfClusters);
1226607Swyllys 		return (-1); /* Cannot be determined yet */
1227607Swyllys 	} else if (CountOfClusters < 4085) {
1228607Swyllys 		/* Volume is FAT12 */
1229607Swyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT12");
1230607Swyllys 		return (0);
1231607Swyllys 	} else if (CountOfClusters < 65525) {
1232607Swyllys 		/* Volume is FAT16 */
1233607Swyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT16");
1234607Swyllys 		return (PCFS_FAT16);
1235607Swyllys 	} else {
1236607Swyllys 		/* Volume is FAT32 */
1237607Swyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT32");
1238607Swyllys 		return (PCFS_FAT32);
1239607Swyllys 	}
1240607Swyllys }
1241607Swyllys 
1242607Swyllys #define	VALID_SECSIZE(s) (s == 512 || s == 1024 || s == 2048 || s == 4096)
1243607Swyllys 
1244607Swyllys #define	VALID_SPCL(s) (s == 1 || s == 2 || s == 4 || s == 8 || s == 16 ||\
1245607Swyllys 	s == 32 || s == 64 || s == 128)
1246607Swyllys 
1247607Swyllys static int
1248607Swyllys secondaryBPBChecks(uchar_t *cp)
1249607Swyllys {
1250607Swyllys 	struct bootsec *bpb = (struct bootsec *)cp;
1251607Swyllys 	struct fat32_bootsec *f32bpb = (struct fat32_bootsec *)cp;
1252607Swyllys 
1253607Swyllys 	/*
1254607Swyllys 	 * Perform secondary checks to try and determine what sort
1255607Swyllys 	 * of FAT partition we have based on other, less reliable,
1256607Swyllys 	 * data in the BPB header.
1257607Swyllys 	 */
1258607Swyllys 	if (ltohs(bpb->fatsec) != 0) {
1259607Swyllys 		/*
1260607Swyllys 		 * Must be FAT12 or FAT16, check the
1261607Swyllys 		 * FilSysType string (not 100% reliable).
1262607Swyllys 		 */
1263607Swyllys 		if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT12", 5)) {
1264607Swyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT12");
1265607Swyllys 			return (0); /* FAT12 */
1266607Swyllys 		} else if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT16",
1267607Swyllys 			5)) {
1268607Swyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT16");
1269607Swyllys 			return (PCFS_FAT16);
1270607Swyllys 		} else {
1271607Swyllys 			/*
1272607Swyllys 			 * Try to use the BPB_Media byte
1273607Swyllys 			 *
1274607Swyllys 			 *  If the media byte indicates a floppy we'll
1275607Swyllys 			 *  assume FAT12, otherwise we'll assume FAT16.
1276607Swyllys 			 */
1277607Swyllys 			switch (bpb->mediadesriptor) {
1278607Swyllys 				case SS8SPT:
1279607Swyllys 				case DS8SPT:
1280607Swyllys 				case SS9SPT:
1281607Swyllys 				case DS9SPT:
1282607Swyllys 				case DS18SPT:
1283607Swyllys 				case DS9_15SPT:
1284607Swyllys 					PC_DPRINTF0(5,
1285607Swyllys 					"secondaryBPBCheck says: FAT12");
1286607Swyllys 					return (0); /* FAT12 */
1287607Swyllys 				case MD_FIXED:
1288607Swyllys 					PC_DPRINTF0(5,
1289607Swyllys 					"secondaryBPBCheck says: FAT16");
1290607Swyllys 					return (PCFS_FAT16);
1291607Swyllys 				default:
1292607Swyllys 					cmn_err(CE_NOTE,
1293607Swyllys 						"!pcfs: unknown FAT type");
1294607Swyllys 					return (-1);
1295607Swyllys 			}
1296607Swyllys 		}
1297607Swyllys 	} else if (ltohi(f32bpb->f_fatlength) > 0) {
1298607Swyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: FAT32");
1299607Swyllys 		return (PCFS_FAT32);
1300607Swyllys 	} else {
1301607Swyllys 		/* We don't know */
1302607Swyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: unknown!!");
1303607Swyllys 		return (-1);
1304607Swyllys 	}
1305607Swyllys }
1306607Swyllys 
1307607Swyllys /*
1308607Swyllys  * Check to see if the BPB we found is correct.
1309607Swyllys  *
1310607Swyllys  * First, look for obvious, tell-tale signs of trouble:
1311607Swyllys  * The NumFATs value should always be 2.  Sometimes it can be a '1'
1312607Swyllys  * on FLASH memory cards and other non-disk-based media, so we
1313607Swyllys  * will allow that as well.
1314607Swyllys  *
1315607Swyllys  * We also look at the Media byte, the valid range is 0xF0, or
1316607Swyllys  * 0xF8 thru 0xFF, anything else means this is probably not a good
1317607Swyllys  * BPB.
1318607Swyllys  *
1319607Swyllys  * Finally, check the BPB Magic number at the end of the 512 byte
1320607Swyllys  * block, it must be 0xAA55.
1321607Swyllys  *
1322607Swyllys  * If that all is good, calculate the number of clusters and
1323607Swyllys  * do some final verification steps.
1324607Swyllys  *
1325607Swyllys  * If all is well, return success (1) and set the fattypep
1326607Swyllys  * value to the correct FAT value.
1327607Swyllys  */
1328607Swyllys static int
1329607Swyllys isBPB(uchar_t *cp, int *fattypep)
1330607Swyllys {
1331607Swyllys 	struct bootsec *bpb = (struct bootsec *)cp;
1332607Swyllys 
1333607Swyllys 	uint_t numclusters;		/* number of clusters in file area */
1334607Swyllys 	ushort_t secsize = (int)ltohs(bpb->bps[0]);
1335607Swyllys 
1336607Swyllys 	if (pcfsdebuglevel >= 3) {
1337607Swyllys 		if (!VALID_SECSIZE(secsize))
1338607Swyllys 			PC_DPRINTF1(3, "check_bpb: invalid bps value %d",
1339607Swyllys 				secsize);
1340607Swyllys 
1341607Swyllys 		if (!VALID_SPCL(bpb->spcl))
1342607Swyllys 			PC_DPRINTF1(3, "check_bpb: invalid spcl value %d",
1343607Swyllys 				bpb->spcl);
1344607Swyllys 
1345607Swyllys 		if ((secsize * bpb->spcl) >= (32 * 1024))
1346607Swyllys 			PC_DPRINTF3(3, "check_bpb: BPC > 32K  %d x %d = %d",
1347607Swyllys 				secsize,
1348607Swyllys 				bpb->spcl,
1349607Swyllys 				secsize * bpb->spcl);
1350607Swyllys 
1351607Swyllys 		if (bpb->nfat == 0)
1352607Swyllys 			PC_DPRINTF1(3, "check_bpb: bad NumFATs value %d",
1353607Swyllys 				bpb->nfat);
1354607Swyllys 
1355607Swyllys 		if (ltohs(bpb->res_sec[0]) == 0)
1356607Swyllys 			PC_DPRINTF1(3, "check_bpb: bad RsvdSecCnt value %d",
1357607Swyllys 				ltohs(bpb->res_sec[0]));
1358607Swyllys 
1359607Swyllys 		PC_DPRINTF1(5, "check_bpb: Media byte = %02x",
1360607Swyllys 			bpb->mediadesriptor);
1361607Swyllys 
1362607Swyllys 	}
1363607Swyllys 	if ((bpb->nfat == 0) ||
1364607Swyllys 		(bpb->mediadesriptor != 0xF0 && bpb->mediadesriptor < 0xF8) ||
1365607Swyllys 		(ltohs(cp[510]) != MBB_MAGIC) ||
1366607Swyllys 		!VALID_SECSIZE(secsize) ||
1367607Swyllys 		!VALID_SPCL(bpb->spcl) ||
1368607Swyllys 		(secsize * bpb->spcl >= (64 * 1024)) ||
1369607Swyllys 		!(ltohs(bpb->res_sec[0])))
1370607Swyllys 		return (0);
1371607Swyllys 
1372607Swyllys 	/*
1373607Swyllys 	 * Basic sanity checks passed so far, now try to determine which
1374607Swyllys 	 * FAT format to use.
1375607Swyllys 	 */
1376607Swyllys 	numclusters = bpb_to_numclusters(cp);
1377607Swyllys 
1378607Swyllys 	*fattypep = fattype(numclusters);
1379607Swyllys 
1380607Swyllys 	/* Do some final sanity checks for each specific type of FAT */
1381607Swyllys 	switch (*fattypep) {
1382607Swyllys 		case 0: /* FAT12 */
1383607Swyllys 		case PCFS_FAT16:
1384607Swyllys 			if (!check_bpb_fat16((struct bootsec *)cp))
1385607Swyllys 				return (0);
1386607Swyllys 			break;
1387607Swyllys 		case PCFS_FAT32:
1388607Swyllys 			if (!check_bpb_fat32((struct fat32_bootsec *)cp))
1389607Swyllys 				return (0);
1390607Swyllys 			break;
1391607Swyllys 		default: /* not sure yet */
1392607Swyllys 			*fattypep = secondaryBPBChecks(cp);
1393607Swyllys 			if (*fattypep == -1) {
1394607Swyllys 				/* Still nothing, give it up. */
1395607Swyllys 				return (0);
1396607Swyllys 			}
1397607Swyllys 			break;
1398607Swyllys 	}
1399607Swyllys 	PC_DPRINTF0(5, "isBPB: BPB passes verification tests");
1400607Swyllys 	return (1);
1401607Swyllys }
1402607Swyllys 
1403607Swyllys 
1404607Swyllys /*
14050Sstevel@tonic-gate  * Get the FAT type for the DOS medium.
14060Sstevel@tonic-gate  *
1407607Swyllys  * -------------------------
1408607Swyllys  * According to Microsoft:
1409607Swyllys  *   The FAT type one of FAT12, FAT16, or FAT32 is determined by the
1410607Swyllys  * count of clusters on the volume and nothing else.
1411607Swyllys  * -------------------------
14120Sstevel@tonic-gate  *
14130Sstevel@tonic-gate  */
14140Sstevel@tonic-gate static int
14150Sstevel@tonic-gate pc_getfattype(
14160Sstevel@tonic-gate 	struct vnode *devvp,
14170Sstevel@tonic-gate 	int ldrive,
14180Sstevel@tonic-gate 	daddr_t *strtsectp,
14190Sstevel@tonic-gate 	int *fattypep)
14200Sstevel@tonic-gate {
14210Sstevel@tonic-gate 	uchar_t *cp;			/* for searching out FAT string */
14220Sstevel@tonic-gate 	buf_t *bp = NULL;		/* Disk buffer pointer */
14230Sstevel@tonic-gate 	int rval = 0;
14240Sstevel@tonic-gate 	uchar_t sysid = 0;		/* System ID character */
14250Sstevel@tonic-gate 	dev_t	dev = devvp->v_rdev;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	*strtsectp = (daddr_t)0;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	/*
14300Sstevel@tonic-gate 	 * Open the device so we can check out the BPB or FDISK table,
14310Sstevel@tonic-gate 	 * then read in the sector.
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	PC_DPRINTF2(5, "pc_getfattype: dev=%x  ldrive=%x  ", (int)dev, ldrive);
14340Sstevel@tonic-gate 	if (rval = VOP_OPEN(&devvp, FREAD, CRED())) {
14350Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfattype: open error=%d\n", rval);
14360Sstevel@tonic-gate 		return (rval);
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 *  Read block 0 from device
14410Sstevel@tonic-gate 	 */
14420Sstevel@tonic-gate 	bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
14430Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
14440Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfattype: read error\n");
14450Sstevel@tonic-gate 		rval = EIO;
14460Sstevel@tonic-gate 		goto out;
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate 
1449607Swyllys 	cp = (uchar_t *)bp->b_un.b_addr;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/*
1452607Swyllys 	 * If the first block is not a valid BPB, look for the
1453607Swyllys 	 * through the FDISK table.
14540Sstevel@tonic-gate 	 */
1455607Swyllys 	if (!isBPB(cp, fattypep)) {
1456607Swyllys 		/* find the partition table and get 512 bytes from it. */
1457607Swyllys 		PC_DPRINTF0(5, "pc_getfattype: using FDISK table to find BPB");
14580Sstevel@tonic-gate 
1459607Swyllys 		if (findTheDrive(dev, ldrive, &rval, &bp,
1460607Swyllys 			strtsectp, &sysid) == 0)
14610Sstevel@tonic-gate 			goto out;
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 		brelse(bp);
14640Sstevel@tonic-gate 		bp = bread(dev, *strtsectp, PC_SAFESECSIZE);
14650Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
14660Sstevel@tonic-gate 			PC_DPRINTF0(1, "pc_getfattype: read error\n");
14670Sstevel@tonic-gate 			rval = EIO;
14680Sstevel@tonic-gate 			goto out;
14690Sstevel@tonic-gate 		}
1470607Swyllys 		cp = (uchar_t *)bp->b_un.b_addr;
14710Sstevel@tonic-gate 
1472607Swyllys 		/* If this one is still no good, give it up. */
1473607Swyllys 		if (!isBPB(cp, fattypep)) {
1474607Swyllys 			rval = EIO;
14750Sstevel@tonic-gate 			goto out;
14760Sstevel@tonic-gate 		}
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate out:
1480607Swyllys 	/*
1481607Swyllys 	 * Release the buffer used
1482607Swyllys 	 */
14830Sstevel@tonic-gate 	if (bp != NULL)
14840Sstevel@tonic-gate 		brelse(bp);
14850Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, CRED());
14860Sstevel@tonic-gate 	return (rval);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Get the boot parameter block and file allocation table.
14920Sstevel@tonic-gate  * If there is an old FAT, invalidate it.
14930Sstevel@tonic-gate  */
14940Sstevel@tonic-gate int
14950Sstevel@tonic-gate pc_getfat(struct pcfs *fsp)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	struct vfs *vfsp = PCFSTOVFS(fsp);
14980Sstevel@tonic-gate 	struct buf *tp = 0;
14990Sstevel@tonic-gate 	struct buf *bp = 0;
15000Sstevel@tonic-gate 	uchar_t *fatp = NULL;
15010Sstevel@tonic-gate 	uchar_t *fat_changemap = NULL;
15020Sstevel@tonic-gate 	struct bootsec *bootp;
15030Sstevel@tonic-gate 	struct fat32_bootsec *f32b;
15040Sstevel@tonic-gate 	struct vnode *devvp;
15050Sstevel@tonic-gate 	int error;
15060Sstevel@tonic-gate 	int fatsize;
15070Sstevel@tonic-gate 	int fat_changemapsize;
15080Sstevel@tonic-gate 	int flags = 0;
15090Sstevel@tonic-gate 	int nfat;
15100Sstevel@tonic-gate 	int secsize;
15110Sstevel@tonic-gate 	int fatsec;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	PC_DPRINTF0(5, "pc_getfat\n");
15140Sstevel@tonic-gate 	devvp = fsp->pcfs_devvp;
15150Sstevel@tonic-gate 	if (fsp->pcfs_fatp) {
15160Sstevel@tonic-gate 		/*
15170Sstevel@tonic-gate 		 * There is a FAT in core.
15180Sstevel@tonic-gate 		 * If there are open file pcnodes or we have modified it or
15190Sstevel@tonic-gate 		 * it hasn't timed out yet use the in core FAT.
15200Sstevel@tonic-gate 		 * Otherwise invalidate it and get a new one
15210Sstevel@tonic-gate 		 */
15220Sstevel@tonic-gate #ifdef notdef
15230Sstevel@tonic-gate 		if (fsp->pcfs_frefs ||
15240Sstevel@tonic-gate 		    (fsp->pcfs_flags & PCFS_FATMOD) ||
15250Sstevel@tonic-gate 		    (gethrestime_sec() < fsp->pcfs_fattime)) {
15260Sstevel@tonic-gate 			return (0);
15270Sstevel@tonic-gate 		} else {
15280Sstevel@tonic-gate 			mutex_enter(&pcfslock);
15290Sstevel@tonic-gate 			pc_invalfat(fsp);
15300Sstevel@tonic-gate 			mutex_exit(&pcfslock);
15310Sstevel@tonic-gate 		}
15320Sstevel@tonic-gate #endif /* notdef */
15330Sstevel@tonic-gate 		return (0);
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 	/*
15360Sstevel@tonic-gate 	 * Open block device mounted on.
15370Sstevel@tonic-gate 	 */
15380Sstevel@tonic-gate 	error = VOP_OPEN(&devvp,
15390Sstevel@tonic-gate 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
15400Sstevel@tonic-gate 	    CRED());
15410Sstevel@tonic-gate 	if (error) {
15420Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfat: open error=%d\n", error);
15430Sstevel@tonic-gate 		return (error);
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 	/*
15460Sstevel@tonic-gate 	 * Get boot parameter block and check it for validity
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	tp = bread(fsp->pcfs_xdev, fsp->pcfs_dosstart, PC_SAFESECSIZE);
15490Sstevel@tonic-gate 	if (tp->b_flags & (B_ERROR | B_STALE)) {
15500Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfat: boot block error\n");
15510Sstevel@tonic-gate 		flags = tp->b_flags & B_ERROR;
15520Sstevel@tonic-gate 		error = EIO;
15530Sstevel@tonic-gate 		goto out;
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
15560Sstevel@tonic-gate 	bootp = (struct bootsec *)tp->b_un.b_addr;
15570Sstevel@tonic-gate 
1558607Swyllys 
15590Sstevel@tonic-gate 	/* get the sector size - may be more than 512 bytes */
15600Sstevel@tonic-gate 	secsize = (int)ltohs(bootp->bps[0]);
15610Sstevel@tonic-gate 	/* check for bogus sector size - fat should be at least 1 sector */
15620Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
15630Sstevel@tonic-gate 		f32b = (struct fat32_bootsec *)bootp;
15640Sstevel@tonic-gate 		fatsec = ltohi(f32b->f_fatlength);
15650Sstevel@tonic-gate 	} else {
15660Sstevel@tonic-gate 		fatsec = ltohs(bootp->fatsec);
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 	if (secsize < 512 || fatsec < 1 || bootp->nfat < 1) {
15690Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT size error");
15700Sstevel@tonic-gate 		error = EINVAL;
15710Sstevel@tonic-gate 		goto out;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	switch (bootp->mediadesriptor) {
15750Sstevel@tonic-gate 	default:
15760Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: media-descriptor error, 0x%x",
15770Sstevel@tonic-gate 		    bootp->mediadesriptor);
15780Sstevel@tonic-gate 		error = EINVAL;
15790Sstevel@tonic-gate 		goto out;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	case MD_FIXED:
15820Sstevel@tonic-gate 		/*
1583201Sjmcp 		 * PCMCIA pseudo floppy is type MD_FIXED,
15840Sstevel@tonic-gate 		 * but is accessed like a floppy
15850Sstevel@tonic-gate 		 */
15860Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_PCMCIA_NO_CIS)) {
15870Sstevel@tonic-gate 			fsp->pcfs_flags |= PCFS_NOCHK;
15880Sstevel@tonic-gate 		}
15890Sstevel@tonic-gate 		/* FALLTHRU */
15900Sstevel@tonic-gate 	case SS8SPT:
15910Sstevel@tonic-gate 	case DS8SPT:
15920Sstevel@tonic-gate 	case SS9SPT:
15930Sstevel@tonic-gate 	case DS9SPT:
15940Sstevel@tonic-gate 	case DS18SPT:
15950Sstevel@tonic-gate 	case DS9_15SPT:
15960Sstevel@tonic-gate 		fsp->pcfs_secsize = secsize;
15970Sstevel@tonic-gate 		fsp->pcfs_sdshift = secsize / DEV_BSIZE - 1;
15980Sstevel@tonic-gate 		fsp->pcfs_entps = secsize / sizeof (struct pcdir);
15990Sstevel@tonic-gate 		fsp->pcfs_spcl = (int)bootp->spcl;
16000Sstevel@tonic-gate 		fsp->pcfs_fatsec = fatsec;
16010Sstevel@tonic-gate 		fsp->pcfs_spt = (int)ltohs(bootp->spt);
16020Sstevel@tonic-gate 		fsp->pcfs_rdirsec = (int)ltohs(bootp->rdirents[0])
16030Sstevel@tonic-gate 		    * sizeof (struct pcdir) / secsize;
16040Sstevel@tonic-gate 		fsp->pcfs_clsize = fsp->pcfs_spcl * secsize;
16050Sstevel@tonic-gate 		fsp->pcfs_fatstart = fsp->pcfs_dosstart +
16060Sstevel@tonic-gate 		    (daddr_t)ltohs(bootp->res_sec[0]);
16070Sstevel@tonic-gate 		fsp->pcfs_rdirstart = fsp->pcfs_fatstart +
16080Sstevel@tonic-gate 		    (bootp->nfat * fsp->pcfs_fatsec);
16090Sstevel@tonic-gate 		fsp->pcfs_datastart = fsp->pcfs_rdirstart + fsp->pcfs_rdirsec;
16100Sstevel@tonic-gate 		if (IS_FAT32(fsp))
16110Sstevel@tonic-gate 			fsp->pcfs_rdirstart = ltohi(f32b->f_rootcluster);
16120Sstevel@tonic-gate 		fsp->pcfs_ncluster = (((int)(ltohs(bootp->numsect[0]) ?
16130Sstevel@tonic-gate 		    ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec))) -
16140Sstevel@tonic-gate 		    fsp->pcfs_datastart + fsp->pcfs_dosstart) / fsp->pcfs_spcl;
16150Sstevel@tonic-gate 		fsp->pcfs_numfat = (int)bootp->nfat;
16160Sstevel@tonic-gate 		fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER;
16170Sstevel@tonic-gate 		break;
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * Get FAT and check it for validity
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	fatsize = fsp->pcfs_fatsec * fsp->pcfs_secsize;
16240Sstevel@tonic-gate 	fatp = kmem_alloc(fatsize, KM_SLEEP);
16250Sstevel@tonic-gate 	error = pc_readfat(fsp, fatp, fsp->pcfs_fatstart, fatsize);
16260Sstevel@tonic-gate 	if (error) {
16270Sstevel@tonic-gate 		flags = B_ERROR;
16280Sstevel@tonic-gate 		goto out;
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 	fat_changemapsize = (fatsize / fsp->pcfs_clsize) + 1;
16310Sstevel@tonic-gate 	fat_changemap = kmem_zalloc(fat_changemapsize, KM_SLEEP);
16320Sstevel@tonic-gate 
1633607Swyllys 	/*
1634607Swyllys 	 * The only definite signature check is that the
1635607Swyllys 	 * media descriptor byte should match the first byte
1636607Swyllys 	 * of the FAT block.
1637607Swyllys 	 */
1638607Swyllys 	if (fatp[0] != bootp->mediadesriptor) {
16390Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT signature error");
16400Sstevel@tonic-gate 		error = EINVAL;
16410Sstevel@tonic-gate 		goto out;
16420Sstevel@tonic-gate 	}
16430Sstevel@tonic-gate 	/*
16440Sstevel@tonic-gate 	 * Checking for fatsec and number of supported clusters, should
1645607Swyllys 	 * actually determine a FAT12/FAT media.
16460Sstevel@tonic-gate 	 */
16470Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_FAT16) {
16480Sstevel@tonic-gate 		if ((fsp->pcfs_fatsec <= 12) &&
16490Sstevel@tonic-gate 		    ((fatsize * 2 / 3) >= fsp->pcfs_ncluster)) {
16500Sstevel@tonic-gate 			/*
16510Sstevel@tonic-gate 			 * We have a 12-bit FAT, rather than a 16-bit FAT.
16520Sstevel@tonic-gate 			 * Ignore what the fdisk table says.
16530Sstevel@tonic-gate 			 */
16540Sstevel@tonic-gate 			PC_DPRINTF0(2, "pc_getfattype: forcing 12-bit FAT\n");
16550Sstevel@tonic-gate 			fsp->pcfs_flags &= ~PCFS_FAT16;
16560Sstevel@tonic-gate 		}
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 	/*
16590Sstevel@tonic-gate 	 * Sanity check our FAT is large enough for the
16600Sstevel@tonic-gate 	 * clusters we think we have.
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_FAT16) &&
16630Sstevel@tonic-gate 	    ((fatsize / 2) < fsp->pcfs_ncluster)) {
16640Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT too small for number of clusters");
16650Sstevel@tonic-gate 		error = EINVAL;
16660Sstevel@tonic-gate 		goto out;
16670Sstevel@tonic-gate 	}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	/*
16700Sstevel@tonic-gate 	 * Get alternate FATs and check for consistency
16710Sstevel@tonic-gate 	 * This is an inlined version of pc_readfat().
16720Sstevel@tonic-gate 	 * Since we're only comparing FAT and alternate FAT,
16730Sstevel@tonic-gate 	 * there's no reason to let pc_readfat() copy data out
16740Sstevel@tonic-gate 	 * of the buf. Instead, compare in-situ, one cluster
16750Sstevel@tonic-gate 	 * at a time.
16760Sstevel@tonic-gate 	 */
16770Sstevel@tonic-gate 	for (nfat = 1; nfat < fsp->pcfs_numfat; nfat++) {
16780Sstevel@tonic-gate 		size_t startsec;
16790Sstevel@tonic-gate 		size_t off;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 		startsec = fsp->pcfs_fatstart + nfat * fsp->pcfs_fatsec;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 		for (off = 0; off < fatsize; off += fsp->pcfs_clsize) {
16840Sstevel@tonic-gate 			bp = bread(fsp->pcfs_xdev, pc_dbdaddr(fsp,
16850Sstevel@tonic-gate 				startsec +
16860Sstevel@tonic-gate 				pc_cltodb(fsp, pc_lblkno(fsp, off))),
16870Sstevel@tonic-gate 				MIN(fsp->pcfs_clsize, fatsize - off));
16880Sstevel@tonic-gate 			if (bp->b_flags & (B_ERROR | B_STALE)) {
16890Sstevel@tonic-gate 				cmn_err(CE_NOTE,
16900Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d read error"
16910Sstevel@tonic-gate 					" at byte %ld", nfat, off);
16920Sstevel@tonic-gate 				flags = B_ERROR;
16930Sstevel@tonic-gate 				error = EIO;
16940Sstevel@tonic-gate 				goto out;
16950Sstevel@tonic-gate 			}
16960Sstevel@tonic-gate 			bp->b_flags |= B_STALE | B_AGE;
16970Sstevel@tonic-gate 			if (bcmp(bp->b_un.b_addr,
16980Sstevel@tonic-gate 			    fatp + off,
16990Sstevel@tonic-gate 			    MIN(fsp->pcfs_clsize, fatsize - off))) {
17000Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17010Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d corrupted"
17020Sstevel@tonic-gate 					" at byte %ld", nfat, off);
17030Sstevel@tonic-gate 				flags = B_ERROR;
17040Sstevel@tonic-gate 			}
17050Sstevel@tonic-gate 			brelse(bp);
17060Sstevel@tonic-gate 			bp = NULL;	/* prevent double release */
17070Sstevel@tonic-gate 		}
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	fsp->pcfs_fatsize = fatsize;
17110Sstevel@tonic-gate 	fsp->pcfs_fatp = fatp;
17120Sstevel@tonic-gate 	fsp->pcfs_fat_changemapsize = fat_changemapsize;
17130Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = fat_changemap;
17140Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
17150Sstevel@tonic-gate 	fsp->pcfs_fatjustread = 1;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	brelse(tp);
17180Sstevel@tonic-gate 	tp = NULL;
17190Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
17200Sstevel@tonic-gate 		/* get fsinfo */
17210Sstevel@tonic-gate 		struct fat32_boot_fsinfo fsinfo_disk;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 		fsp->f32fsinfo_sector = ltohs(f32b->f_infosector);
17240Sstevel@tonic-gate 		tp = bread(fsp->pcfs_xdev,
17250Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
17260Sstevel@tonic-gate 		    PC_SAFESECSIZE);
17270Sstevel@tonic-gate 		if (tp->b_flags & (B_ERROR | B_STALE)) {
17280Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: error reading fat32 fsinfo");
17290Sstevel@tonic-gate 			flags = tp->b_flags & B_ERROR;
17300Sstevel@tonic-gate 			brelse(tp);
17310Sstevel@tonic-gate 			tp = NULL;
17320Sstevel@tonic-gate 			error = EIO;
17330Sstevel@tonic-gate 			goto out;
17340Sstevel@tonic-gate 		}
17350Sstevel@tonic-gate 		tp->b_flags |= B_STALE | B_AGE;
17360Sstevel@tonic-gate 		bcopy((void *)(tp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
17370Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
17380Sstevel@tonic-gate 		brelse(tp);
17390Sstevel@tonic-gate 		tp = NULL;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 		/* translated fields */
17420Sstevel@tonic-gate 		fsp->fsinfo_native.fs_signature =
17430Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_signature);
17440Sstevel@tonic-gate 		fsp->fsinfo_native.fs_free_clusters =
17450Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_free_clusters);
17460Sstevel@tonic-gate 		if (fsp->fsinfo_native.fs_signature != FAT32_FS_SIGN) {
17470Sstevel@tonic-gate 			cmn_err(CE_NOTE,
17480Sstevel@tonic-gate 			    "!pcfs: fat32 fsinfo signature mismatch.");
17490Sstevel@tonic-gate 			error = EINVAL;
17500Sstevel@tonic-gate 			goto out;
17510Sstevel@tonic-gate 		}
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	return (0);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate out:
17570Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!pcfs: illegal disk format");
17580Sstevel@tonic-gate 	if (tp)
17590Sstevel@tonic-gate 		brelse(tp);
17600Sstevel@tonic-gate 	if (bp)
17610Sstevel@tonic-gate 		brelse(bp);
17620Sstevel@tonic-gate 	if (fatp)
17630Sstevel@tonic-gate 		kmem_free(fatp, fatsize);
17640Sstevel@tonic-gate 	if (fat_changemap)
17650Sstevel@tonic-gate 		kmem_free(fat_changemap, fat_changemapsize);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	if (flags) {
17680Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
17710Sstevel@tonic-gate 	    FREAD : FREAD|FWRITE, 1, (offset_t)0, CRED());
17720Sstevel@tonic-gate 	return (error);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate int
17760Sstevel@tonic-gate pc_syncfat(struct pcfs *fsp)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	struct buf *bp;
17790Sstevel@tonic-gate 	int nfat;
17800Sstevel@tonic-gate 	int	error;
17810Sstevel@tonic-gate 	struct fat32_boot_fsinfo fsinfo_disk;
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfat\n");
17840Sstevel@tonic-gate 	if ((fsp->pcfs_fatp == (uchar_t *)0) ||
17850Sstevel@tonic-gate 	    !(fsp->pcfs_flags & PCFS_FATMOD))
17860Sstevel@tonic-gate 		return (0);
17870Sstevel@tonic-gate 	/*
17880Sstevel@tonic-gate 	 * write out all copies of FATs
17890Sstevel@tonic-gate 	 */
17900Sstevel@tonic-gate 	fsp->pcfs_flags &= ~PCFS_FATMOD;
17910Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
17920Sstevel@tonic-gate 	for (nfat = 0; nfat < fsp->pcfs_numfat; nfat++) {
17930Sstevel@tonic-gate 		error = pc_writefat(fsp,
17940Sstevel@tonic-gate 		    fsp->pcfs_fatstart + nfat*fsp->pcfs_fatsec);
17950Sstevel@tonic-gate 		if (error) {
17960Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
17970Sstevel@tonic-gate 			return (EIO);
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 	pc_clear_fatchanges(fsp);
18010Sstevel@tonic-gate 	PC_DPRINTF0(6, "pcfs_syncfat: wrote out FAT\n");
18020Sstevel@tonic-gate 	/* write out fsinfo */
18030Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
18040Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
18050Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
18060Sstevel@tonic-gate 		    PC_SAFESECSIZE);
18070Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
18080Sstevel@tonic-gate 			brelse(bp);
18090Sstevel@tonic-gate 			return (EIO);
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 		bcopy((void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18120Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
18130Sstevel@tonic-gate 		/* translate fields */
18140Sstevel@tonic-gate 		fsinfo_disk.fs_free_clusters =
18150Sstevel@tonic-gate 		    htoli(fsp->fsinfo_native.fs_free_clusters);
18160Sstevel@tonic-gate 		fsinfo_disk.fs_next_cluster = (uint32_t)FSINFO_UNKNOWN;
18170Sstevel@tonic-gate 		bcopy(&fsinfo_disk,
18180Sstevel@tonic-gate 		    (void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18190Sstevel@tonic-gate 		    sizeof (struct fat32_boot_fsinfo));
18200Sstevel@tonic-gate 		bwrite2(bp);
18210Sstevel@tonic-gate 		error = geterror(bp);
18220Sstevel@tonic-gate 		brelse(bp);
18230Sstevel@tonic-gate 		if (error) {
18240Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
18250Sstevel@tonic-gate 			return (EIO);
18260Sstevel@tonic-gate 		}
18270Sstevel@tonic-gate 	}
18280Sstevel@tonic-gate 	return (0);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate void
18320Sstevel@tonic-gate pc_invalfat(struct pcfs *fsp)
18330Sstevel@tonic-gate {
18340Sstevel@tonic-gate 	struct pcfs *xfsp;
18350Sstevel@tonic-gate 	int mount_cnt = 0;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	PC_DPRINTF0(7, "pc_invalfat\n");
18380Sstevel@tonic-gate 	if (fsp->pcfs_fatp == (uchar_t *)0)
18390Sstevel@tonic-gate 		panic("pc_invalfat");
18400Sstevel@tonic-gate 	/*
18410Sstevel@tonic-gate 	 * Release FAT
18420Sstevel@tonic-gate 	 */
18430Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fatp, fsp->pcfs_fatsize);
18440Sstevel@tonic-gate 	fsp->pcfs_fatp = NULL;
18450Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fat_changemap, fsp->pcfs_fat_changemapsize);
18460Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = NULL;
18470Sstevel@tonic-gate 	/*
18480Sstevel@tonic-gate 	 * Invalidate all the blocks associated with the device.
18490Sstevel@tonic-gate 	 * Not needed if stateless.
18500Sstevel@tonic-gate 	 */
18510Sstevel@tonic-gate 	for (xfsp = pc_mounttab; xfsp; xfsp = xfsp->pcfs_nxt)
18520Sstevel@tonic-gate 		if (xfsp != fsp && xfsp->pcfs_xdev == fsp->pcfs_xdev)
18530Sstevel@tonic-gate 			mount_cnt++;
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	if (!mount_cnt)
18560Sstevel@tonic-gate 		binval(fsp->pcfs_xdev);
18570Sstevel@tonic-gate 	/*
18580Sstevel@tonic-gate 	 * close mounted device
18590Sstevel@tonic-gate 	 */
18600Sstevel@tonic-gate 	(void) VOP_CLOSE(fsp->pcfs_devvp,
18610Sstevel@tonic-gate 	    (PCFSTOVFS(fsp)->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
18620Sstevel@tonic-gate 	    1, (offset_t)0, CRED());
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate void
18660Sstevel@tonic-gate pc_badfs(struct pcfs *fsp)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	cmn_err(CE_WARN, "corrupted PC file system on dev %x.%x\n",
18690Sstevel@tonic-gate 	    getmajor(fsp->pcfs_devvp->v_rdev),
18700Sstevel@tonic-gate 	    getminor(fsp->pcfs_devvp->v_rdev));
18710Sstevel@tonic-gate }
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate /*
18740Sstevel@tonic-gate  * The problem with supporting NFS on the PCFS filesystem is that there
18750Sstevel@tonic-gate  * is no good place to keep the generation number. The only possible
18760Sstevel@tonic-gate  * place is inside a directory entry. There are a few words that we
18770Sstevel@tonic-gate  * don't use - they store NT & OS/2 attributes, and the creation/last access
18780Sstevel@tonic-gate  * time of the file - but it seems wrong to use them. In addition, directory
18790Sstevel@tonic-gate  * entries come and go. If a directory is removed completely, its directory
18800Sstevel@tonic-gate  * blocks are freed and the generation numbers are lost. Whereas in ufs,
18810Sstevel@tonic-gate  * inode blocks are dedicated for inodes, so the generation numbers are
18820Sstevel@tonic-gate  * permanently kept on the disk.
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate static int
18850Sstevel@tonic-gate pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
18860Sstevel@tonic-gate {
18870Sstevel@tonic-gate 	struct pcnode *pcp;
18880Sstevel@tonic-gate 	struct pc_fid *pcfid;
18890Sstevel@tonic-gate 	struct pcfs *fsp;
18900Sstevel@tonic-gate 	struct pcdir *ep;
18910Sstevel@tonic-gate 	daddr_t eblkno;
18920Sstevel@tonic-gate 	int eoffset;
18930Sstevel@tonic-gate 	struct buf *bp;
18940Sstevel@tonic-gate 	int error;
18950Sstevel@tonic-gate 	pc_cluster32_t	cn;
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	pcfid = (struct pc_fid *)fidp;
18980Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	error = pc_lockfs(fsp, 0, 0);
19010Sstevel@tonic-gate 	if (error) {
19020Sstevel@tonic-gate 		*vpp = NULL;
19030Sstevel@tonic-gate 		return (error);
19040Sstevel@tonic-gate 	}
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	if (pcfid->pcfid_block == 0) {
19070Sstevel@tonic-gate 		pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
19080Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
19090Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
19100Sstevel@tonic-gate 		pc_unlockfs(fsp);
19110Sstevel@tonic-gate 		return (0);
19120Sstevel@tonic-gate 	}
19130Sstevel@tonic-gate 	eblkno = pcfid->pcfid_block;
19140Sstevel@tonic-gate 	eoffset = pcfid->pcfid_offset;
19150Sstevel@tonic-gate 	if ((pc_dbtocl(fsp,
19160Sstevel@tonic-gate 	    eblkno - fsp->pcfs_dosstart) >= fsp->pcfs_ncluster) ||
19170Sstevel@tonic-gate 	    (eoffset > fsp->pcfs_clsize)) {
19180Sstevel@tonic-gate 		pc_unlockfs(fsp);
19190Sstevel@tonic-gate 		*vpp = NULL;
19200Sstevel@tonic-gate 		return (EINVAL);
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	if (eblkno >= fsp->pcfs_datastart || (eblkno-fsp->pcfs_rdirstart)
19240Sstevel@tonic-gate 	    < (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) {
19250Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno, fsp->pcfs_clsize);
19260Sstevel@tonic-gate 	} else {
19270Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno,
19280Sstevel@tonic-gate 		    (int)(fsp->pcfs_datastart - eblkno) * fsp->pcfs_secsize);
19290Sstevel@tonic-gate 	}
19300Sstevel@tonic-gate 	if (bp->b_flags & (B_ERROR | B_STALE)) {
19310Sstevel@tonic-gate 		error = geterror(bp);
19320Sstevel@tonic-gate 		brelse(bp);
19330Sstevel@tonic-gate 		if (error)
19340Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
19350Sstevel@tonic-gate 		*vpp = NULL;
19360Sstevel@tonic-gate 		pc_unlockfs(fsp);
19370Sstevel@tonic-gate 		return (error);
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 	ep = (struct pcdir *)(bp->b_un.b_addr + eoffset);
19400Sstevel@tonic-gate 	/*
19410Sstevel@tonic-gate 	 * Ok, if this is a valid file handle that we gave out,
19420Sstevel@tonic-gate 	 * then simply ensuring that the creation time matches,
19430Sstevel@tonic-gate 	 * the entry has not been deleted, and it has a valid first
19440Sstevel@tonic-gate 	 * character should be enough.
19450Sstevel@tonic-gate 	 *
19460Sstevel@tonic-gate 	 * Unfortunately, verifying that the <blkno, offset> _still_
19470Sstevel@tonic-gate 	 * refers to a directory entry is not easy, since we'd have
19480Sstevel@tonic-gate 	 * to search _all_ directories starting from root to find it.
19490Sstevel@tonic-gate 	 * That's a high price to pay just in case somebody is forging
19500Sstevel@tonic-gate 	 * file handles. So instead we verify that as much of the
19510Sstevel@tonic-gate 	 * entry is valid as we can:
19520Sstevel@tonic-gate 	 *
19530Sstevel@tonic-gate 	 * 1. The starting cluster is 0 (unallocated) or valid
19540Sstevel@tonic-gate 	 * 2. It is not an LFN entry
19550Sstevel@tonic-gate 	 * 3. It is not hidden (unless mounted as such)
19560Sstevel@tonic-gate 	 * 4. It is not the label
19570Sstevel@tonic-gate 	 */
19580Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, ep);
19590Sstevel@tonic-gate 	/*
19600Sstevel@tonic-gate 	 * if the starting cluster is valid, but not valid according
19610Sstevel@tonic-gate 	 * to pc_validcl(), force it to be to simplify the following if.
19620Sstevel@tonic-gate 	 */
19630Sstevel@tonic-gate 	if (cn == 0)
19640Sstevel@tonic-gate 		cn = PCF_FIRSTCLUSTER;
19650Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
19660Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER32)
19670Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
19680Sstevel@tonic-gate 	} else {
19690Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER)
19700Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
19710Sstevel@tonic-gate 	}
19720Sstevel@tonic-gate 	if ((!pc_validcl(fsp, cn)) ||
19730Sstevel@tonic-gate 	    (PCDL_IS_LFN(ep)) ||
19740Sstevel@tonic-gate 	    (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) ||
19750Sstevel@tonic-gate 	    ((ep->pcd_attr & PCA_LABEL) == PCA_LABEL)) {
19760Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
19770Sstevel@tonic-gate 		brelse(bp);
19780Sstevel@tonic-gate 		pc_unlockfs(fsp);
19790Sstevel@tonic-gate 		return (EINVAL);
19800Sstevel@tonic-gate 	}
19810Sstevel@tonic-gate 	if ((ep->pcd_crtime.pct_time == pcfid->pcfid_ctime) &&
19820Sstevel@tonic-gate 	    (ep->pcd_filename[0] != PCD_ERASED) &&
19830Sstevel@tonic-gate 	    (pc_validchar(ep->pcd_filename[0]) ||
19840Sstevel@tonic-gate 		(ep->pcd_filename[0] == '.' && ep->pcd_filename[1] == '.'))) {
19850Sstevel@tonic-gate 		pcp = pc_getnode(fsp, eblkno, eoffset, ep);
19860Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
19870Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
19880Sstevel@tonic-gate 	} else {
19890Sstevel@tonic-gate 		*vpp = NULL;
19900Sstevel@tonic-gate 	}
19910Sstevel@tonic-gate 	bp->b_flags |= B_STALE | B_AGE;
19920Sstevel@tonic-gate 	brelse(bp);
19930Sstevel@tonic-gate 	pc_unlockfs(fsp);
19940Sstevel@tonic-gate 	return (0);
19950Sstevel@tonic-gate }
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate /*
1998201Sjmcp  * if device is a PCMCIA pseudo floppy, return 1
19990Sstevel@tonic-gate  * otherwise, return 0
20000Sstevel@tonic-gate  */
20010Sstevel@tonic-gate static int
2002201Sjmcp pcfs_pseudo_floppy(dev_t rdev)
20030Sstevel@tonic-gate {
20040Sstevel@tonic-gate 	int			error, err;
20050Sstevel@tonic-gate 	struct dk_cinfo		info;
20060Sstevel@tonic-gate 	ldi_handle_t		lh;
20070Sstevel@tonic-gate 	ldi_ident_t		li;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	err = ldi_ident_from_mod(&modlinkage, &li);
20100Sstevel@tonic-gate 	if (err) {
20110Sstevel@tonic-gate 		PC_DPRINTF1(1,
2012201Sjmcp 		    "pcfs_pseudo_floppy: ldi_ident_from_mod err=%d\n", err);
20130Sstevel@tonic-gate 		return (0);
20140Sstevel@tonic-gate 	}
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	err = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD, CRED(), &lh, li);
20170Sstevel@tonic-gate 	ldi_ident_release(li);
20180Sstevel@tonic-gate 	if (err) {
20190Sstevel@tonic-gate 		PC_DPRINTF1(1,
2020201Sjmcp 		    "pcfs_pseudo_floppy: ldi_open err=%d\n", err);
20210Sstevel@tonic-gate 		return (0);
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	/* return value stored in err is purposfully ignored */
20250Sstevel@tonic-gate 	error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
20260Sstevel@tonic-gate 	    CRED(), &err);
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	err = ldi_close(lh, FREAD, CRED());
20290Sstevel@tonic-gate 	if (err != 0) {
20300Sstevel@tonic-gate 		PC_DPRINTF1(1,
2031201Sjmcp 		    "pcfs_pseudo_floppy: ldi_close err=%d\n", err);
20320Sstevel@tonic-gate 		return (0);
20330Sstevel@tonic-gate 	}
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	if ((error == 0) && (info.dki_ctype == DKC_PCMCIA_MEM) &&
20360Sstevel@tonic-gate 		(info.dki_flags & DKI_PCMCIA_PFD))
20370Sstevel@tonic-gate 		return (1);
20380Sstevel@tonic-gate 	else
20390Sstevel@tonic-gate 		return (0);
20400Sstevel@tonic-gate }
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate /*
20430Sstevel@tonic-gate  * Unfortunately, FAT32 fat's can be pretty big (On a 1 gig jaz drive, about
20440Sstevel@tonic-gate  * a meg), so we can't bread() it all in at once. This routine reads a
20450Sstevel@tonic-gate  * fat a chunk at a time.
20460Sstevel@tonic-gate  */
20470Sstevel@tonic-gate static int
20480Sstevel@tonic-gate pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start, size_t fatsize)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	struct buf *bp;
20510Sstevel@tonic-gate 	size_t off;
20520Sstevel@tonic-gate 	size_t readsize;
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	readsize = fsp->pcfs_clsize;
20550Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += readsize, fatp += readsize) {
20560Sstevel@tonic-gate 		if (readsize > (fatsize - off))
20570Sstevel@tonic-gate 			readsize = fatsize - off;
20580Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
20590Sstevel@tonic-gate 		    pc_dbdaddr(fsp, start +
20600Sstevel@tonic-gate 			pc_cltodb(fsp, pc_lblkno(fsp, off))),
20610Sstevel@tonic-gate 		    readsize);
20620Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
20630Sstevel@tonic-gate 			brelse(bp);
20640Sstevel@tonic-gate 			return (EIO);
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
20670Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, fatp, readsize);
20680Sstevel@tonic-gate 		brelse(bp);
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 	return (0);
20710Sstevel@tonic-gate }
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate /*
20740Sstevel@tonic-gate  * We write the FAT out a _lot_, in order to make sure that it
20750Sstevel@tonic-gate  * is up-to-date. But on a FAT32 system (large drive, small clusters)
20760Sstevel@tonic-gate  * the FAT might be a couple of megabytes, and writing it all out just
20770Sstevel@tonic-gate  * because we created or deleted a small file is painful (especially
20780Sstevel@tonic-gate  * since we do it for each alternate FAT too). So instead, for FAT16 and
20790Sstevel@tonic-gate  * FAT32 we only write out the bit that has changed. We don't clear
20800Sstevel@tonic-gate  * the 'updated' fields here because the caller might be writing out
20810Sstevel@tonic-gate  * several FATs, so the caller must use pc_clear_fatchanges() after
20820Sstevel@tonic-gate  * all FATs have been updated.
20830Sstevel@tonic-gate  */
20840Sstevel@tonic-gate static int
20850Sstevel@tonic-gate pc_writefat(struct pcfs *fsp, daddr_t start)
20860Sstevel@tonic-gate {
20870Sstevel@tonic-gate 	struct buf *bp;
20880Sstevel@tonic-gate 	size_t off;
20890Sstevel@tonic-gate 	size_t writesize;
20900Sstevel@tonic-gate 	int	error;
20910Sstevel@tonic-gate 	uchar_t *fatp = fsp->pcfs_fatp;
20920Sstevel@tonic-gate 	size_t fatsize = fsp->pcfs_fatsize;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	writesize = fsp->pcfs_clsize;
20950Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += writesize, fatp += writesize) {
20960Sstevel@tonic-gate 		if (writesize > (fatsize - off))
20970Sstevel@tonic-gate 			writesize = fatsize - off;
20980Sstevel@tonic-gate 		if (!pc_fat_is_changed(fsp, pc_lblkno(fsp, off))) {
20990Sstevel@tonic-gate 			continue;
21000Sstevel@tonic-gate 		}
21010Sstevel@tonic-gate 		bp = ngeteblk(writesize);
21020Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
21030Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
21040Sstevel@tonic-gate 		bp->b_blkno = pc_dbdaddr(fsp, start +
21050Sstevel@tonic-gate 		    pc_cltodb(fsp, pc_lblkno(fsp, off)));
21060Sstevel@tonic-gate 		bcopy(fatp, bp->b_un.b_addr, writesize);
21070Sstevel@tonic-gate 		bwrite2(bp);
21080Sstevel@tonic-gate 		error = geterror(bp);
21090Sstevel@tonic-gate 		brelse(bp);
21100Sstevel@tonic-gate 		if (error) {
21110Sstevel@tonic-gate 			return (error);
21120Sstevel@tonic-gate 		}
21130Sstevel@tonic-gate 	}
21140Sstevel@tonic-gate 	return (0);
21150Sstevel@tonic-gate }
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate /*
21180Sstevel@tonic-gate  * Mark the FAT cluster that 'cn' is stored in as modified.
21190Sstevel@tonic-gate  */
21200Sstevel@tonic-gate void
21210Sstevel@tonic-gate pc_mark_fat_updated(struct pcfs *fsp, pc_cluster32_t cn)
21220Sstevel@tonic-gate {
21230Sstevel@tonic-gate 	pc_cluster32_t	bn;
21240Sstevel@tonic-gate 	size_t		size;
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	/* which fat block is the cluster number stored in? */
21270Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
21280Sstevel@tonic-gate 		size = sizeof (pc_cluster32_t);
21290Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21300Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21310Sstevel@tonic-gate 	} else if (IS_FAT16(fsp)) {
21320Sstevel@tonic-gate 		size = sizeof (pc_cluster16_t);
21330Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21340Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21350Sstevel@tonic-gate 	} else {
21360Sstevel@tonic-gate 		offset_t off;
21370Sstevel@tonic-gate 		pc_cluster32_t nbn;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 		ASSERT(IS_FAT12(fsp));
21400Sstevel@tonic-gate 		off = cn + (cn >> 1);
21410Sstevel@tonic-gate 		bn = pc_lblkno(fsp, off);
21420Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21430Sstevel@tonic-gate 		/* does this field wrap into the next fat cluster? */
21440Sstevel@tonic-gate 		nbn = pc_lblkno(fsp, off + 1);
21450Sstevel@tonic-gate 		if (nbn != bn) {
21460Sstevel@tonic-gate 			fsp->pcfs_fat_changemap[nbn] = 1;
21470Sstevel@tonic-gate 		}
21480Sstevel@tonic-gate 	}
21490Sstevel@tonic-gate }
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate /*
21520Sstevel@tonic-gate  * return whether the FAT cluster 'bn' is updated and needs to
21530Sstevel@tonic-gate  * be written out.
21540Sstevel@tonic-gate  */
21550Sstevel@tonic-gate int
21560Sstevel@tonic-gate pc_fat_is_changed(struct pcfs *fsp, pc_cluster32_t bn)
21570Sstevel@tonic-gate {
21580Sstevel@tonic-gate 	return (fsp->pcfs_fat_changemap[bn] == 1);
21590Sstevel@tonic-gate }
2160