xref: /illumos-gate/usr/src/uts/common/fs/pcfs/pc_vfsops.c (revision aa59c4cb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*aa59c4cbSrsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/user.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/disp.h>
357c478bd9Sstevel@tonic-gate #include <sys/buf.h>
367c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
37*aa59c4cbSrsb #include <sys/vfs_opreg.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/conf.h>
437c478bd9Sstevel@tonic-gate #undef NFSCLIENT
447c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/mount.h>
467c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <sys/conf.h>
517c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
527c478bd9Sstevel@tonic-gate #include <sys/swap.h>
537c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunldi.h>
557c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/pc_label.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
587c478bd9Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
597c478bd9Sstevel@tonic-gate #include <sys/fs/pc_node.h>
607c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
617c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
637c478bd9Sstevel@tonic-gate #include <sys/open.h>
647c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
657c478bd9Sstevel@tonic-gate #include <sys/policy.h>
66264a6e74Sfrankho #include <sys/atomic.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * The majority of PC media use a 512 sector size, but
707c478bd9Sstevel@tonic-gate  * occasionally you will run across a 1k sector size.
717c478bd9Sstevel@tonic-gate  * For media with a 1k sector size, fd_strategy() requires
727c478bd9Sstevel@tonic-gate  * the I/O size to be a 1k multiple; so when the sector size
737c478bd9Sstevel@tonic-gate  * is not yet known, always read 1k.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate #define	PC_SAFESECSIZE	(PC_SECSIZE * 2)
767c478bd9Sstevel@tonic-gate 
7790c30842Sjmcp static int pcfs_pseudo_floppy(dev_t);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int pcfsinit(int, char *);
807c478bd9Sstevel@tonic-gate static int pcfs_mount(struct vfs *, struct vnode *, struct mounta *,
817c478bd9Sstevel@tonic-gate 	struct cred *);
827c478bd9Sstevel@tonic-gate static int pcfs_unmount(struct vfs *, int, struct cred *);
837c478bd9Sstevel@tonic-gate static int pcfs_root(struct vfs *, struct vnode **);
847c478bd9Sstevel@tonic-gate static int pcfs_statvfs(struct vfs *, struct statvfs64 *);
857c478bd9Sstevel@tonic-gate static int pc_syncfsnodes(struct pcfs *);
867c478bd9Sstevel@tonic-gate static int pcfs_sync(struct vfs *, short, struct cred *);
877c478bd9Sstevel@tonic-gate static int pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
88264a6e74Sfrankho static void pcfs_freevfs(vfs_t *vfsp);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int pc_getfattype(struct vnode *, int, daddr_t *, int *);
917c478bd9Sstevel@tonic-gate static int pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start,
927c478bd9Sstevel@tonic-gate     size_t fatsize);
937c478bd9Sstevel@tonic-gate static int pc_writefat(struct pcfs *fsp, daddr_t start);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * pcfs mount options table
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 
99264a6e74Sfrankho static char *nohidden_cancel[] = { MNTOPT_PCFS_HIDDEN, NULL };
100264a6e74Sfrankho static char *hidden_cancel[] = { MNTOPT_PCFS_NOHIDDEN, NULL };
101264a6e74Sfrankho static char *nofoldcase_cancel[] = { MNTOPT_PCFS_FOLDCASE, NULL };
102264a6e74Sfrankho static char *foldcase_cancel[] = { MNTOPT_PCFS_NOFOLDCASE, NULL };
103264a6e74Sfrankho static char *clamptime_cancel[] = { MNTOPT_PCFS_NOCLAMPTIME, NULL };
104264a6e74Sfrankho static char *noclamptime_cancel[] = { MNTOPT_PCFS_CLAMPTIME, NULL };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static mntopt_t mntopts[] = {
1077c478bd9Sstevel@tonic-gate /*
108264a6e74Sfrankho  *	option name	cancel option	default arg	flags	opt data
1097c478bd9Sstevel@tonic-gate  */
110264a6e74Sfrankho 	{ MNTOPT_PCFS_NOHIDDEN, nohidden_cancel, NULL, 0, NULL },
111264a6e74Sfrankho 	{ MNTOPT_PCFS_HIDDEN, hidden_cancel, NULL, MO_DEFAULT, NULL },
112264a6e74Sfrankho 	{ MNTOPT_PCFS_NOFOLDCASE, nofoldcase_cancel, NULL, MO_DEFAULT, NULL },
113264a6e74Sfrankho 	{ MNTOPT_PCFS_FOLDCASE, foldcase_cancel, NULL, 0, NULL },
114264a6e74Sfrankho 	{ MNTOPT_PCFS_CLAMPTIME, clamptime_cancel, NULL, MO_DEFAULT, NULL },
115264a6e74Sfrankho 	{ MNTOPT_PCFS_NOCLAMPTIME, noclamptime_cancel, NULL, NULL, NULL }
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static mntopts_t pcfs_mntopts = {
1197c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1207c478bd9Sstevel@tonic-gate 	mntopts
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int pcfsdebuglevel = 0;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * pcfslock:	protects the list of mounted pc filesystems "pc_mounttab.
1277c478bd9Sstevel@tonic-gate  * pcfs_lock:	(inside per filesystem structure "pcfs")
1287c478bd9Sstevel@tonic-gate  *		per filesystem lock. Most of the vfsops and vnodeops are
1297c478bd9Sstevel@tonic-gate  *		protected by this lock.
1307c478bd9Sstevel@tonic-gate  * pcnodes_lock: protects the pcnode hash table "pcdhead", "pcfhead".
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Lock hierarchy: pcfslock > pcfs_lock > pcnodes_lock
133264a6e74Sfrankho  *
134264a6e74Sfrankho  * pcfs_mountcount:	used to prevent module unloads while there is still
135264a6e74Sfrankho  *			pcfs state from a former mount hanging around. With
136264a6e74Sfrankho  *			forced umount support, the filesystem module must not
137264a6e74Sfrankho  *			be allowed to go away before the last VFS_FREEVFS()
138264a6e74Sfrankho  *			call has been made.
139264a6e74Sfrankho  *			Since this is just an atomic counter, there's no need
140264a6e74Sfrankho  *			for locking.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate kmutex_t	pcfslock;
143264a6e74Sfrankho krwlock_t	pcnodes_lock;
144264a6e74Sfrankho uint32_t	pcfs_mountcount;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static int pcfstype;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
1497c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1507c478bd9Sstevel@tonic-gate 	"pcfs",
1517c478bd9Sstevel@tonic-gate 	pcfsinit,
1525a59a8b3Srsb 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS,
1537c478bd9Sstevel@tonic-gate 	&pcfs_mntopts
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
1597c478bd9Sstevel@tonic-gate 	&mod_fsops,
1609bd42341Sfrankho 	"PC filesystem v1.100",
1617c478bd9Sstevel@tonic-gate 	&vfw
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1657c478bd9Sstevel@tonic-gate 	MODREV_1,
1667c478bd9Sstevel@tonic-gate 	&modlfs,
1677c478bd9Sstevel@tonic-gate 	NULL
1687c478bd9Sstevel@tonic-gate };
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate int
1717c478bd9Sstevel@tonic-gate _init(void)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	int	error;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #if !defined(lint)
1767c478bd9Sstevel@tonic-gate 	/* make sure the on-disk structures are sane */
1777c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir) == 32);
1787c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir_lfn) == 32);
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 	mutex_init(&pcfslock, NULL, MUTEX_DEFAULT, NULL);
1817c478bd9Sstevel@tonic-gate 	rw_init(&pcnodes_lock, NULL, RW_DEFAULT, NULL);
1827c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1837c478bd9Sstevel@tonic-gate 	if (error) {
1847c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcfslock);
1857c478bd9Sstevel@tonic-gate 		rw_destroy(&pcnodes_lock);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	return (error);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate int
1917c478bd9Sstevel@tonic-gate _fini(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int	error;
1947c478bd9Sstevel@tonic-gate 
195264a6e74Sfrankho 	/*
196264a6e74Sfrankho 	 * If a forcedly unmounted instance is still hanging around,
197264a6e74Sfrankho 	 * we cannot allow the module to be unloaded because that would
198264a6e74Sfrankho 	 * cause panics once the VFS framework decides it's time to call
199264a6e74Sfrankho 	 * into VFS_FREEVFS().
200264a6e74Sfrankho 	 */
201264a6e74Sfrankho 	if (pcfs_mountcount)
202264a6e74Sfrankho 		return (EBUSY);
203264a6e74Sfrankho 
2047c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
2057c478bd9Sstevel@tonic-gate 	if (error)
2067c478bd9Sstevel@tonic-gate 		return (error);
2077c478bd9Sstevel@tonic-gate 	mutex_destroy(&pcfslock);
2087c478bd9Sstevel@tonic-gate 	rw_destroy(&pcnodes_lock);
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Tear down the operations vectors
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(pcfstype);
2137c478bd9Sstevel@tonic-gate 	vn_freevnodeops(pcfs_fvnodeops);
2147c478bd9Sstevel@tonic-gate 	vn_freevnodeops(pcfs_dvnodeops);
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate int
2197c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
2257c478bd9Sstevel@tonic-gate static int
2267c478bd9Sstevel@tonic-gate pcfsinit(int fstype, char *name)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t pcfs_vfsops_template[] = {
229*aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = pcfs_mount },
230*aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = pcfs_unmount },
231*aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = pcfs_root },
232*aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = pcfs_statvfs },
233*aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = pcfs_sync },
234*aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = pcfs_vget },
235*aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = pcfs_freevfs },
236*aa59c4cbSrsb 		NULL,			NULL
2377c478bd9Sstevel@tonic-gate 	};
2387c478bd9Sstevel@tonic-gate 	int error;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, pcfs_vfsops_template, NULL);
2417c478bd9Sstevel@tonic-gate 	if (error != 0) {
2427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad vfs ops template");
2437c478bd9Sstevel@tonic-gate 		return (error);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	error = vn_make_ops("pcfs", pcfs_fvnodeops_template, &pcfs_fvnodeops);
2477c478bd9Sstevel@tonic-gate 	if (error != 0) {
2487c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2497c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad file vnode ops template");
2507c478bd9Sstevel@tonic-gate 		return (error);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	error = vn_make_ops("pcfsd", pcfs_dvnodeops_template, &pcfs_dvnodeops);
2547c478bd9Sstevel@tonic-gate 	if (error != 0) {
2557c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2567c478bd9Sstevel@tonic-gate 		vn_freevnodeops(pcfs_fvnodeops);
2577c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad dir vnode ops template");
2587c478bd9Sstevel@tonic-gate 		return (error);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	pcfstype = fstype;
2627c478bd9Sstevel@tonic-gate 	(void) pc_init();
263264a6e74Sfrankho 	pcfs_mountcount = 0;
2647c478bd9Sstevel@tonic-gate 	return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static struct pcfs *pc_mounttab = NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate extern struct pcfs_args pc_tz;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  *  Define some special logical drives we use internal to this file.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate #define	BOOT_PARTITION_DRIVE	99
2757c478bd9Sstevel@tonic-gate #define	PRIMARY_DOS_DRIVE	1
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * pc_mount system call
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate static int
2817c478bd9Sstevel@tonic-gate pcfs_mount(
2827c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
2837c478bd9Sstevel@tonic-gate 	struct vnode *mvp,
2847c478bd9Sstevel@tonic-gate 	struct mounta *uap,
2857c478bd9Sstevel@tonic-gate 	struct cred *cr)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
2887c478bd9Sstevel@tonic-gate 	struct vnode *bvp;
2897c478bd9Sstevel@tonic-gate 	struct vnode *devvp;
2907c478bd9Sstevel@tonic-gate 	struct pathname special;
2917c478bd9Sstevel@tonic-gate 	daddr_t dosstart;
2927c478bd9Sstevel@tonic-gate 	dev_t pseudodev;
2937c478bd9Sstevel@tonic-gate 	dev_t xdev;
2949bd42341Sfrankho 	char *c;
2957c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
2967c478bd9Sstevel@tonic-gate 	int datalen = uap->datalen;
2977c478bd9Sstevel@tonic-gate 	int dos_ldrive = 0;
2987c478bd9Sstevel@tonic-gate 	int error;
2997c478bd9Sstevel@tonic-gate 	int fattype;
3007c478bd9Sstevel@tonic-gate 	minor_t	minor;
3017c478bd9Sstevel@tonic-gate 	int oflag, aflag;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
3047c478bd9Sstevel@tonic-gate 		return (error);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_mount\n");
3077c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
3087c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
3117c478bd9Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
3127c478bd9Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
3137c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
3147c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
3157c478bd9Sstevel@tonic-gate 		return (EBUSY);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * The caller is responsible for making sure to always
3217c478bd9Sstevel@tonic-gate 	 * pass in sizeof(struct pcfs_args) (or the old one).
3227c478bd9Sstevel@tonic-gate 	 * Doing this is the only way to know an EINVAL return
3237c478bd9Sstevel@tonic-gate 	 * from mount(2) is due to the "not a DOS filesystem"
3247c478bd9Sstevel@tonic-gate 	 * EINVAL that pc_verify/pc_getfattype could return.
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if ((datalen != sizeof (struct pcfs_args)) &&
3277c478bd9Sstevel@tonic-gate 	    (datalen != sizeof (struct old_pcfs_args))) {
3287c478bd9Sstevel@tonic-gate 		return (EINVAL);
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		struct pcfs_args tmp_tz;
3317c478bd9Sstevel@tonic-gate 		int hidden = 0;
3327c478bd9Sstevel@tonic-gate 		int foldcase = 0;
333264a6e74Sfrankho 		int noclamptime = 0;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		tmp_tz.flags = 0;
3367c478bd9Sstevel@tonic-gate 		if (copyin(data, &tmp_tz, datalen)) {
3377c478bd9Sstevel@tonic-gate 			return (EFAULT);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		if (datalen == sizeof (struct pcfs_args)) {
3407c478bd9Sstevel@tonic-gate 			hidden = tmp_tz.flags & PCFS_MNT_HIDDEN;
3417c478bd9Sstevel@tonic-gate 			foldcase = tmp_tz.flags & PCFS_MNT_FOLDCASE;
342264a6e74Sfrankho 			noclamptime = tmp_tz.flags & PCFS_MNT_NOCLAMPTIME;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		if (hidden)
3467c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_HIDDEN,	NULL, 0);
3477c478bd9Sstevel@tonic-gate 		if (foldcase)
3487c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_FOLDCASE, NULL, 0);
349264a6e74Sfrankho 		if (noclamptime)
350264a6e74Sfrankho 			vfs_setmntopt(vfsp, MNTOPT_PCFS_NOCLAMPTIME, NULL, 0);
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * more than one pc filesystem can be mounted on x86
3537c478bd9Sstevel@tonic-gate 		 * so the pc_tz structure is now a critical region
3547c478bd9Sstevel@tonic-gate 		 */
3557c478bd9Sstevel@tonic-gate 		mutex_enter(&pcfslock);
3567c478bd9Sstevel@tonic-gate 		if (pc_mounttab == NULL)
3577c478bd9Sstevel@tonic-gate 			bcopy(&tmp_tz, &pc_tz, sizeof (struct pcfs_args));
3587c478bd9Sstevel@tonic-gate 		mutex_exit(&pcfslock);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if (error = pn_get(uap->spec, UIO_USERSPACE, &special)) {
3647c478bd9Sstevel@tonic-gate 		return (error);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	if (error =
3677c478bd9Sstevel@tonic-gate 	    lookupname(special.pn_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp)) {
3687c478bd9Sstevel@tonic-gate 		/*
3699bd42341Sfrankho 		 * Split the pathname string at the last ':' separator.
3709bd42341Sfrankho 		 * If there's no ':' in the device name, or the ':' is the
3719bd42341Sfrankho 		 * last character in the string, the name is invalid and
3729bd42341Sfrankho 		 * the error from the previous lookup will be returned.
3737c478bd9Sstevel@tonic-gate 		 */
3749bd42341Sfrankho 		c = strrchr(special.pn_path, ':');
3759bd42341Sfrankho 		if (c == NULL || strlen(c) == 0)
3769bd42341Sfrankho 			goto devlookup_done;
3777c478bd9Sstevel@tonic-gate 
3789bd42341Sfrankho 		*c++ = '\0';
3797c478bd9Sstevel@tonic-gate 
3809bd42341Sfrankho 		/*
3819bd42341Sfrankho 		 * PCFS partition name suffixes can be:
3829bd42341Sfrankho 		 *	- "boot" to indicate the X86BOOT partition
3839bd42341Sfrankho 		 *	- a drive letter [c-z] for the "DOS logical drive"
3849bd42341Sfrankho 		 *	- a drive number 1..24 for the "DOS logical drive"
3859bd42341Sfrankho 		 */
3869bd42341Sfrankho 		if (strcasecmp(c, "boot") == 0) {
3879bd42341Sfrankho 			/*
3889bd42341Sfrankho 			 * The Solaris boot partition is requested.
3899bd42341Sfrankho 			 */
3909bd42341Sfrankho 			dos_ldrive = BOOT_PARTITION_DRIVE;
3919bd42341Sfrankho 		} else if (strspn(c, "0123456789") == strlen(c)) {
3929bd42341Sfrankho 			/*
3939bd42341Sfrankho 			 * All digits - parse the partition number.
3949bd42341Sfrankho 			 */
3959bd42341Sfrankho 			long drvnum = 0;
3969bd42341Sfrankho 
3979bd42341Sfrankho 			if ((error = ddi_strtol(c, NULL, 10, &drvnum)) == 0) {
3987c478bd9Sstevel@tonic-gate 				/*
3999bd42341Sfrankho 				 * A number alright - in the allowed range ?
4007c478bd9Sstevel@tonic-gate 				 */
4019bd42341Sfrankho 				if (drvnum > 24 || drvnum == 0)
4029bd42341Sfrankho 					error = EINVAL;
4037c478bd9Sstevel@tonic-gate 			}
4049bd42341Sfrankho 			if (error)
4059bd42341Sfrankho 				goto devlookup_done;
4069bd42341Sfrankho 			dos_ldrive = (int)drvnum;
4079bd42341Sfrankho 		} else if (strlen(c) == 1) {
4089bd42341Sfrankho 			/*
4099bd42341Sfrankho 			 * A single trailing character - is it [c-zC-Z] ?
4109bd42341Sfrankho 			 */
4119bd42341Sfrankho 			*c = tolower(*c);
4129bd42341Sfrankho 			if (*c < 'c' || *c > 'z') {
4139bd42341Sfrankho 				error = EINVAL;
4149bd42341Sfrankho 				goto devlookup_done;
4159bd42341Sfrankho 			}
4169bd42341Sfrankho 			dos_ldrive = 1 + *c - 'c';
4179bd42341Sfrankho 		} else {
4189bd42341Sfrankho 			/*
4199bd42341Sfrankho 			 * Can't parse this - pass through previous error.
4209bd42341Sfrankho 			 */
4219bd42341Sfrankho 			goto devlookup_done;
4227c478bd9Sstevel@tonic-gate 		}
4239bd42341Sfrankho 
4249bd42341Sfrankho 		ASSERT(dos_ldrive > 0);
4259bd42341Sfrankho 
4269bd42341Sfrankho 		error = lookupname(special.pn_path, UIO_SYSSPACE, FOLLOW,
4279bd42341Sfrankho 		    NULLVPP, &bvp);
4287c478bd9Sstevel@tonic-gate 	}
4299bd42341Sfrankho devlookup_done:
4307c478bd9Sstevel@tonic-gate 	pn_free(&special);
4319bd42341Sfrankho 	if (error)
4329bd42341Sfrankho 		return (error);
4339bd42341Sfrankho 
4347c478bd9Sstevel@tonic-gate 	if (bvp->v_type != VBLK) {
4357c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
4367c478bd9Sstevel@tonic-gate 		return (ENOTBLK);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	xdev = bvp->v_rdev;
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * Verify caller's permission to open the device special file.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
4437c478bd9Sstevel@tonic-gate 	    ((uap->flags & MS_RDONLY) != 0)) {
4447c478bd9Sstevel@tonic-gate 		oflag = FREAD;
4457c478bd9Sstevel@tonic-gate 		aflag = VREAD;
4467c478bd9Sstevel@tonic-gate 	} else {
4477c478bd9Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
4487c478bd9Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	if ((error = VOP_ACCESS(bvp, aflag, 0, cr)) != 0 ||
4517c478bd9Sstevel@tonic-gate 	    (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
4527c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
4537c478bd9Sstevel@tonic-gate 		return (error);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	VN_RELE(bvp);
4577c478bd9Sstevel@tonic-gate 	if (getmajor(xdev) >= devcnt) {
4587c478bd9Sstevel@tonic-gate 		return (ENXIO);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	/*
4619bd42341Sfrankho 	 * Ensure that this logical drive isn't already mounted, unless
4629bd42341Sfrankho 	 * this is a REMOUNT request.
4639bd42341Sfrankho 	 * Note: The framework will perform this check if the "...:c"
4649bd42341Sfrankho 	 * PCFS-style "logical drive" syntax has not been used and an
4659bd42341Sfrankho 	 * actually existing physical device is backing this filesystem.
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	if (dos_ldrive) {
4687c478bd9Sstevel@tonic-gate 		mutex_enter(&pcfslock);
4697c478bd9Sstevel@tonic-gate 		for (fsp = pc_mounttab; fsp; fsp = fsp->pcfs_nxt)
4707c478bd9Sstevel@tonic-gate 			if (fsp->pcfs_xdev == xdev &&
4717c478bd9Sstevel@tonic-gate 			    fsp->pcfs_ldrv == dos_ldrive) {
4727c478bd9Sstevel@tonic-gate 				mutex_exit(&pcfslock);
4737c478bd9Sstevel@tonic-gate 				if (uap->flags & MS_REMOUNT) {
4747c478bd9Sstevel@tonic-gate 					return (0);
4757c478bd9Sstevel@tonic-gate 				} else {
4767c478bd9Sstevel@tonic-gate 					return (EBUSY);
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 		/*
4807c478bd9Sstevel@tonic-gate 		 * Assign a unique device number for the vfs
4817c478bd9Sstevel@tonic-gate 		 * The old way (getudev() + a constantly incrementing
4827c478bd9Sstevel@tonic-gate 		 * major number) was wrong because it changes vfs_dev
4837c478bd9Sstevel@tonic-gate 		 * across mounts and reboots, which breaks nfs file handles.
4847c478bd9Sstevel@tonic-gate 		 * UFS just uses the real dev_t. We can't do that because
4857c478bd9Sstevel@tonic-gate 		 * of the way pcfs opens fdisk partitons (the :c and :d
4867c478bd9Sstevel@tonic-gate 		 * partitions are on the same dev_t). Though that _might_
4877c478bd9Sstevel@tonic-gate 		 * actually be ok, since the file handle contains an
4887c478bd9Sstevel@tonic-gate 		 * absolute block number, it's probably better to make them
4897c478bd9Sstevel@tonic-gate 		 * different. So I think we should retain the original
4907c478bd9Sstevel@tonic-gate 		 * dev_t, but come up with a different minor number based
4917c478bd9Sstevel@tonic-gate 		 * on the logical drive that will _always_ come up the same.
4927c478bd9Sstevel@tonic-gate 		 * For now, we steal the upper 6 bits.
4937c478bd9Sstevel@tonic-gate 		 */
4947c478bd9Sstevel@tonic-gate #ifdef notdef
4957c478bd9Sstevel@tonic-gate 		/* what should we do here? */
4967c478bd9Sstevel@tonic-gate 		if (((getminor(xdev) >> 12) & 0x3F) != 0)
4977c478bd9Sstevel@tonic-gate 			printf("whoops - upper bits used!\n");
4987c478bd9Sstevel@tonic-gate #endif
4997c478bd9Sstevel@tonic-gate 		minor = ((dos_ldrive << 12) | getminor(xdev)) & MAXMIN32;
5007c478bd9Sstevel@tonic-gate 		pseudodev = makedevice(getmajor(xdev), minor);
5017c478bd9Sstevel@tonic-gate 		if (vfs_devmounting(pseudodev, vfsp)) {
5027c478bd9Sstevel@tonic-gate 			mutex_exit(&pcfslock);
5037c478bd9Sstevel@tonic-gate 			return (EBUSY);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 		if (vfs_devismounted(pseudodev)) {
5067c478bd9Sstevel@tonic-gate 			mutex_exit(&pcfslock);
5077c478bd9Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
5087c478bd9Sstevel@tonic-gate 				return (0);
5097c478bd9Sstevel@tonic-gate 			} else {
5107c478bd9Sstevel@tonic-gate 				return (EBUSY);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		mutex_exit(&pcfslock);
5147c478bd9Sstevel@tonic-gate 	} else {
5157c478bd9Sstevel@tonic-gate 		if (vfs_devmounting(xdev, vfsp)) {
5167c478bd9Sstevel@tonic-gate 			return (EBUSY);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		if (vfs_devismounted(xdev))
5197c478bd9Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
5207c478bd9Sstevel@tonic-gate 				return (0);
5217c478bd9Sstevel@tonic-gate 			} else {
5227c478bd9Sstevel@tonic-gate 				return (EBUSY);
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 		pseudodev = xdev;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
5287c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
5297c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Mount the filesystem
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	devvp = makespecvp(xdev, VBLK);
5367c478bd9Sstevel@tonic-gate 	if (IS_SWAPVP(devvp)) {
5377c478bd9Sstevel@tonic-gate 		VN_RELE(devvp);
5387c478bd9Sstevel@tonic-gate 		return (EBUSY);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/*
5427c478bd9Sstevel@tonic-gate 	 * special handling for PCMCIA memory card
54390c30842Sjmcp 	 * with pseudo floppies organization
5447c478bd9Sstevel@tonic-gate 	 */
54590c30842Sjmcp 	if (dos_ldrive == 0 && pcfs_pseudo_floppy(xdev)) {
5467c478bd9Sstevel@tonic-gate 		dosstart = (daddr_t)0;
5477c478bd9Sstevel@tonic-gate 		fattype = PCFS_PCMCIA_NO_CIS;
5487c478bd9Sstevel@tonic-gate 	} else {
5497c478bd9Sstevel@tonic-gate 		if (error = pc_getfattype(devvp, dos_ldrive, &dosstart,
5507c478bd9Sstevel@tonic-gate 		    &fattype)) {
5517c478bd9Sstevel@tonic-gate 			VN_RELE(devvp);
5527c478bd9Sstevel@tonic-gate 			return (error);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0, B_INVAL, cr);
5577c478bd9Sstevel@tonic-gate 	fsp = kmem_zalloc((uint_t)sizeof (struct pcfs), KM_SLEEP);
5587c478bd9Sstevel@tonic-gate 	fsp->pcfs_vfs = vfsp;
5597c478bd9Sstevel@tonic-gate 	fsp->pcfs_flags = fattype;
5607c478bd9Sstevel@tonic-gate 	fsp->pcfs_devvp = devvp;
5617c478bd9Sstevel@tonic-gate 	fsp->pcfs_xdev = xdev;
5627c478bd9Sstevel@tonic-gate 	fsp->pcfs_ldrv = dos_ldrive;
5637c478bd9Sstevel@tonic-gate 	fsp->pcfs_dosstart = dosstart;
5647c478bd9Sstevel@tonic-gate 	mutex_init(&fsp->pcfs_lock, NULL, MUTEX_DEFAULT, NULL);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_HIDDEN, NULL))
5677c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_HIDDEN;
5687c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_FOLDCASE, NULL))
5697c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_FOLDCASE;
570264a6e74Sfrankho 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_NOCLAMPTIME, NULL))
571264a6e74Sfrankho 		fsp->pcfs_flags |= PCFS_NOCLAMPTIME;
5727c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = pseudodev;
5737c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = pcfstype;
5747c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, pseudodev, pcfstype);
5757c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)fsp;
5767c478bd9Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	error = pc_verify(fsp);
5797c478bd9Sstevel@tonic-gate 	if (error) {
5807c478bd9Sstevel@tonic-gate 		VN_RELE(devvp);
5817c478bd9Sstevel@tonic-gate 		mutex_destroy(&fsp->pcfs_lock);
5827c478bd9Sstevel@tonic-gate 		kmem_free(fsp, (uint_t)sizeof (struct pcfs));
5837c478bd9Sstevel@tonic-gate 		return (error);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->pcfs_clsize;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	mutex_enter(&pcfslock);
5887c478bd9Sstevel@tonic-gate 	fsp->pcfs_nxt = pc_mounttab;
5897c478bd9Sstevel@tonic-gate 	pc_mounttab = fsp;
5907c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
591264a6e74Sfrankho 	atomic_inc_32(&pcfs_mountcount);
5927c478bd9Sstevel@tonic-gate 	return (0);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * vfs operations
5977c478bd9Sstevel@tonic-gate  */
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /* ARGSUSED */
6007c478bd9Sstevel@tonic-gate static int
6017c478bd9Sstevel@tonic-gate pcfs_unmount(
6027c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
6037c478bd9Sstevel@tonic-gate 	int flag,
6047c478bd9Sstevel@tonic-gate 	struct cred *cr)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	struct pcfs *fsp, *fsp1;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
6097c478bd9Sstevel@tonic-gate 		return (EPERM);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_unmount\n");
6127c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
613264a6e74Sfrankho 
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * We don't have to lock fsp because the VVFSLOCK in vfs layer will
6167c478bd9Sstevel@tonic-gate 	 * prevent lookuppn from crossing the mount point.
617264a6e74Sfrankho 	 * If this is not a forced umount request and there's ongoing I/O,
618264a6e74Sfrankho 	 * don't allow the mount to proceed.
6197c478bd9Sstevel@tonic-gate 	 */
620264a6e74Sfrankho 	if (flag & MS_FORCE)
621264a6e74Sfrankho 		vfsp->vfs_flag |= VFS_UNMOUNTED;
622264a6e74Sfrankho 	else if (fsp->pcfs_nrefs)
6237c478bd9Sstevel@tonic-gate 		return (EBUSY);
624264a6e74Sfrankho 
625264a6e74Sfrankho 	mutex_enter(&pcfslock);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
628264a6e74Sfrankho 	 * If this is a forced umount request or if the fs instance has
629264a6e74Sfrankho 	 * been marked as beyond recovery, allow the umount to proceed
630264a6e74Sfrankho 	 * regardless of state. pc_diskchanged() forcibly releases all
631264a6e74Sfrankho 	 * inactive vnodes/pcnodes.
6327c478bd9Sstevel@tonic-gate 	 */
633264a6e74Sfrankho 	if (flag & MS_FORCE || fsp->pcfs_flags & PCFS_IRRECOV) {
6347c478bd9Sstevel@tonic-gate 		rw_enter(&pcnodes_lock, RW_WRITER);
6357c478bd9Sstevel@tonic-gate 		pc_diskchanged(fsp);
6367c478bd9Sstevel@tonic-gate 		rw_exit(&pcnodes_lock);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	/* now there should be no pcp node on pcfhead or pcdhead. */
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (fsp == pc_mounttab) {
6427c478bd9Sstevel@tonic-gate 		pc_mounttab = fsp->pcfs_nxt;
6437c478bd9Sstevel@tonic-gate 	} else {
6447c478bd9Sstevel@tonic-gate 		for (fsp1 = pc_mounttab; fsp1 != NULL; fsp1 = fsp1->pcfs_nxt)
6457c478bd9Sstevel@tonic-gate 			if (fsp1->pcfs_nxt == fsp)
6467c478bd9Sstevel@tonic-gate 				fsp1->pcfs_nxt = fsp->pcfs_nxt;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
6507c478bd9Sstevel@tonic-gate 
651264a6e74Sfrankho 	/*
652264a6e74Sfrankho 	 * Since we support VFS_FREEVFS(), there's no need to
653264a6e74Sfrankho 	 * free the fsp right now. The framework will tell us
654264a6e74Sfrankho 	 * when the right time to do so has arrived by calling
655264a6e74Sfrankho 	 * into pcfs_freevfs.
656264a6e74Sfrankho 	 */
6577c478bd9Sstevel@tonic-gate 	return (0);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * find root of pcfs
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate static int
6647c478bd9Sstevel@tonic-gate pcfs_root(
6657c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
6667c478bd9Sstevel@tonic-gate 	struct vnode **vpp)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
6697c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
6707c478bd9Sstevel@tonic-gate 	int error;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6737c478bd9Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
6747c478bd9Sstevel@tonic-gate 		return (error);
675264a6e74Sfrankho 
6767c478bd9Sstevel@tonic-gate 	pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
6777c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(9, "pcfs_root(0x%p) pcp= 0x%p\n",
6787c478bd9Sstevel@tonic-gate 	    (void *)vfsp, (void *)pcp);
6797c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
6807c478bd9Sstevel@tonic-gate 	*vpp = PCTOV(pcp);
6817c478bd9Sstevel@tonic-gate 	pcp->pc_flags |= PC_EXTERNAL;
6827c478bd9Sstevel@tonic-gate 	return (0);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate  * Get file system statistics.
6877c478bd9Sstevel@tonic-gate  */
6887c478bd9Sstevel@tonic-gate static int
6897c478bd9Sstevel@tonic-gate pcfs_statvfs(
6907c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
6917c478bd9Sstevel@tonic-gate 	struct statvfs64 *sp)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
6947c478bd9Sstevel@tonic-gate 	int error;
6957c478bd9Sstevel@tonic-gate 	dev32_t d32;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6987c478bd9Sstevel@tonic-gate 	error = pc_getfat(fsp);
6997c478bd9Sstevel@tonic-gate 	if (error)
7007c478bd9Sstevel@tonic-gate 		return (error);
7017c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (*sp));
7027c478bd9Sstevel@tonic-gate 	sp->f_bsize = sp->f_frsize = fsp->pcfs_clsize;
7037c478bd9Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->pcfs_ncluster;
7047c478bd9Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree = (fsblkcnt64_t)pc_freeclusters(fsp);
7057c478bd9Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)-1;
7067c478bd9Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)-1;
7077c478bd9Sstevel@tonic-gate 	sp->f_favail = (fsfilcnt64_t)-1;
7087c478bd9Sstevel@tonic-gate #ifdef notdef
7097c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, fsp->pcfs_devvp->v_rdev);
7107c478bd9Sstevel@tonic-gate #endif /* notdef */
7117c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
7127c478bd9Sstevel@tonic-gate 	sp->f_fsid = d32;
7137c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
7147c478bd9Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
7157c478bd9Sstevel@tonic-gate 	sp->f_namemax = PCFNAMESIZE;
7167c478bd9Sstevel@tonic-gate 	return (0);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static int
7207c478bd9Sstevel@tonic-gate pc_syncfsnodes(struct pcfs *fsp)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	struct pchead *hp;
7237c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
7247c478bd9Sstevel@tonic-gate 	int error;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfsnodes\n");
7277c478bd9Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
7287c478bd9Sstevel@tonic-gate 		return (error);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	if (!(error = pc_syncfat(fsp))) {
7317c478bd9Sstevel@tonic-gate 		hp = pcfhead;
7327c478bd9Sstevel@tonic-gate 		while (hp < & pcfhead [ NPCHASH ]) {
7337c478bd9Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_READER);
7347c478bd9Sstevel@tonic-gate 			pcp = hp->pch_forw;
7357c478bd9Sstevel@tonic-gate 			while (pcp != (struct pcnode *)hp) {
7367c478bd9Sstevel@tonic-gate 				if (VFSTOPCFS(PCTOV(pcp) -> v_vfsp) == fsp)
7377c478bd9Sstevel@tonic-gate 					if (error = pc_nodesync(pcp))
7387c478bd9Sstevel@tonic-gate 						break;
7397c478bd9Sstevel@tonic-gate 				pcp = pcp -> pc_forw;
7407c478bd9Sstevel@tonic-gate 			}
7417c478bd9Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7427c478bd9Sstevel@tonic-gate 			if (error)
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 			hp++;
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
7487c478bd9Sstevel@tonic-gate 	return (error);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Flush any pending I/O.
7537c478bd9Sstevel@tonic-gate  */
7547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7557c478bd9Sstevel@tonic-gate static int
7567c478bd9Sstevel@tonic-gate pcfs_sync(
7577c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
7587c478bd9Sstevel@tonic-gate 	short flag,
7597c478bd9Sstevel@tonic-gate 	struct cred *cr)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
7627c478bd9Sstevel@tonic-gate 	int error = 0;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/* this prevents the filesystem from being umounted. */
7657c478bd9Sstevel@tonic-gate 	mutex_enter(&pcfslock);
7667c478bd9Sstevel@tonic-gate 	if (vfsp != NULL) {
7677c478bd9Sstevel@tonic-gate 		fsp = VFSTOPCFS(vfsp);
7687c478bd9Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_IRRECOV)) {
7697c478bd9Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7707c478bd9Sstevel@tonic-gate 		} else {
7717c478bd9Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_WRITER);
7727c478bd9Sstevel@tonic-gate 			pc_diskchanged(fsp);
7737c478bd9Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7747c478bd9Sstevel@tonic-gate 			error = EIO;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 	} else {
7777c478bd9Sstevel@tonic-gate 		fsp = pc_mounttab;
7787c478bd9Sstevel@tonic-gate 		while (fsp != NULL) {
7797c478bd9Sstevel@tonic-gate 			if (fsp->pcfs_flags & PCFS_IRRECOV) {
7807c478bd9Sstevel@tonic-gate 				rw_enter(&pcnodes_lock, RW_WRITER);
7817c478bd9Sstevel@tonic-gate 				pc_diskchanged(fsp);
7827c478bd9Sstevel@tonic-gate 				rw_exit(&pcnodes_lock);
7837c478bd9Sstevel@tonic-gate 				error = EIO;
7847c478bd9Sstevel@tonic-gate 				break;
7857c478bd9Sstevel@tonic-gate 			}
7867c478bd9Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7877c478bd9Sstevel@tonic-gate 			if (error) break;
7887c478bd9Sstevel@tonic-gate 			fsp = fsp->pcfs_nxt;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
7927c478bd9Sstevel@tonic-gate 	return (error);
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate int
7967c478bd9Sstevel@tonic-gate pc_lockfs(struct pcfs *fsp, int diskchanged, int releasing)
7977c478bd9Sstevel@tonic-gate {
798264a6e74Sfrankho 	int err;
799264a6e74Sfrankho 
8007c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_IRRECOV) && !releasing)
8017c478bd9Sstevel@tonic-gate 		return (EIO);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) && (fsp->pcfs_owner == curthread)) {
8047c478bd9Sstevel@tonic-gate 		fsp->pcfs_count++;
8057c478bd9Sstevel@tonic-gate 	} else {
8067c478bd9Sstevel@tonic-gate 		mutex_enter(&fsp->pcfs_lock);
8077c478bd9Sstevel@tonic-gate 		if (fsp->pcfs_flags & PCFS_LOCKED)
8087c478bd9Sstevel@tonic-gate 			panic("pc_lockfs");
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * We check the IRRECOV bit again just in case somebody
8117c478bd9Sstevel@tonic-gate 		 * snuck past the initial check but then got held up before
8127c478bd9Sstevel@tonic-gate 		 * they could grab the lock.  (And in the meantime someone
8137c478bd9Sstevel@tonic-gate 		 * had grabbed the lock and set the bit)
8147c478bd9Sstevel@tonic-gate 		 */
8150576819eSwyllys 		if (!diskchanged && !(fsp->pcfs_flags & PCFS_IRRECOV)) {
816264a6e74Sfrankho 			if ((err = pc_getfat(fsp))) {
817264a6e74Sfrankho 				mutex_exit(&fsp->pcfs_lock);
8180576819eSwyllys 				return (err);
819264a6e74Sfrankho 			}
8200576819eSwyllys 		}
8217c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_LOCKED;
8227c478bd9Sstevel@tonic-gate 		fsp->pcfs_owner = curthread;
8237c478bd9Sstevel@tonic-gate 		fsp->pcfs_count++;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	return (0);
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate void
8297c478bd9Sstevel@tonic-gate pc_unlockfs(struct pcfs *fsp)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) == 0)
8337c478bd9Sstevel@tonic-gate 		panic("pc_unlockfs");
8347c478bd9Sstevel@tonic-gate 	if (--fsp->pcfs_count < 0)
8357c478bd9Sstevel@tonic-gate 		panic("pc_unlockfs: count");
8367c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_count == 0) {
8377c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags &= ~PCFS_LOCKED;
8387c478bd9Sstevel@tonic-gate 		fsp->pcfs_owner = 0;
8397c478bd9Sstevel@tonic-gate 		mutex_exit(&fsp->pcfs_lock);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * isDosDrive()
8457c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8467c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes a DOS drive.  We
8477c478bd9Sstevel@tonic-gate  *	use systid values defined in sys/dktp/fdisk.h.
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate static int
8507c478bd9Sstevel@tonic-gate isDosDrive(uchar_t checkMe)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
8537c478bd9Sstevel@tonic-gate 	    (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
8547c478bd9Sstevel@tonic-gate 	    (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
8557c478bd9Sstevel@tonic-gate 	    (checkMe == DIAGPART));
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * isDosExtended()
8607c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8617c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes an extended DOS
8627c478bd9Sstevel@tonic-gate  *	partition.
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate static int
8657c478bd9Sstevel@tonic-gate isDosExtended(uchar_t checkMe)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate /*
8717c478bd9Sstevel@tonic-gate  * isBootPart()
8727c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8737c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes a Solaris boot
8747c478bd9Sstevel@tonic-gate  *	partition.
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate static int
8777c478bd9Sstevel@tonic-gate isBootPart(uchar_t checkMe)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	return (checkMe == X86BOOT);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate  * noLogicalDrive()
8847c478bd9Sstevel@tonic-gate  *	Display error message about not being able to find a logical
8857c478bd9Sstevel@tonic-gate  *	drive.
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate static void
8887c478bd9Sstevel@tonic-gate noLogicalDrive(int requested)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	if (requested == BOOT_PARTITION_DRIVE) {
8917c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no boot partition");
8927c478bd9Sstevel@tonic-gate 	} else {
8937c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no such logical drive");
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * findTheDrive()
8997c478bd9Sstevel@tonic-gate  *	Discover offset of the requested logical drive, and return
9007c478bd9Sstevel@tonic-gate  *	that offset (startSector), the systid of that drive (sysid),
9017c478bd9Sstevel@tonic-gate  *	and a buffer pointer (bp), with the buffer contents being
9027c478bd9Sstevel@tonic-gate  *	the first sector of the logical drive (i.e., the sector that
9037c478bd9Sstevel@tonic-gate  *	contains the BPB for that drive).
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate static int
9069bd42341Sfrankho findTheDrive(dev_t dev, int ldrive, buf_t **bp,
9077c478bd9Sstevel@tonic-gate     daddr_t *startSector, uchar_t *sysid)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	struct ipart dosp[FD_NUMPART];	/* incore fdisk partition structure */
9107c478bd9Sstevel@tonic-gate 	struct mboot *dosp_ptr;		/* boot structure pointer */
9117c478bd9Sstevel@tonic-gate 	daddr_t lastseek = 0;		/* Disk block we sought previously */
9127c478bd9Sstevel@tonic-gate 	daddr_t diskblk = 0;		/* Disk block to get */
9137c478bd9Sstevel@tonic-gate 	daddr_t xstartsect;		/* base of Extended DOS partition */
9147c478bd9Sstevel@tonic-gate 	int logicalDriveCount = 0;	/* Count of logical drives seen */
9157c478bd9Sstevel@tonic-gate 	int extendedPart = -1;		/* index of extended dos partition */
9167c478bd9Sstevel@tonic-gate 	int primaryPart = -1;		/* index of primary dos partition */
9177c478bd9Sstevel@tonic-gate 	int bootPart = -1;		/* index of a Solaris boot partition */
9187c478bd9Sstevel@tonic-gate 	int xnumsect = -1;		/* length of extended DOS partition */
9197c478bd9Sstevel@tonic-gate 	int driveIndex;			/* computed FDISK table index */
9207c478bd9Sstevel@tonic-gate 	int i;
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Count of drives in the current extended partition's
9237c478bd9Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 	int extndDrives[FD_NUMPART];
9267c478bd9Sstevel@tonic-gate 	int numDrives = 0;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Count of drives (beyond primary) in master boot record's
9307c478bd9Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	int extraDrives[FD_NUMPART];
9337c478bd9Sstevel@tonic-gate 	int numExtraDrives = 0;
9347c478bd9Sstevel@tonic-gate 
9359bd42341Sfrankho 	/*
9369bd42341Sfrankho 	 * "ldrive == 0" should never happen, as this is a request to
9379bd42341Sfrankho 	 * mount the physical device (and ignore partitioning). The code
9389bd42341Sfrankho 	 * in pcfs_mount() should have made sure that a logical drive number
9399bd42341Sfrankho 	 * is at least 1, meaning we're looking for drive "C:". It is not
9409bd42341Sfrankho 	 * safe (and a bug in the callers of this function) to request logical
9419bd42341Sfrankho 	 * drive number 0; we could ASSERT() but a graceful EIO is a more
9429bd42341Sfrankho 	 * polite way.
9439bd42341Sfrankho 	 */
9449bd42341Sfrankho 	if (ldrive == 0) {
9459bd42341Sfrankho 		cmn_err(CE_NOTE, "!pcfs: request for logical partition zero");
9469bd42341Sfrankho 		noLogicalDrive(ldrive);
9479bd42341Sfrankho 		return (EIO);
9489bd42341Sfrankho 	}
9499bd42341Sfrankho 
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 *  Copy from disk block into memory aligned structure for fdisk usage.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
9547c478bd9Sstevel@tonic-gate 	bcopy(dosp_ptr->parts, dosp, sizeof (struct ipart) * FD_NUMPART);
9557c478bd9Sstevel@tonic-gate 
9569bd42341Sfrankho 	if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
9579bd42341Sfrankho 		cmn_err(CE_NOTE, "!pcfs: MBR partition table signature err");
9589bd42341Sfrankho 		return (EINVAL);
9599bd42341Sfrankho 	}
9609bd42341Sfrankho 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * Get a summary of what is in the Master FDISK table.
9637c478bd9Sstevel@tonic-gate 	 * Normally we expect to find one partition marked as a DOS drive.
9647c478bd9Sstevel@tonic-gate 	 * This partition is the one Windows calls the primary dos partition.
9657c478bd9Sstevel@tonic-gate 	 * If the machine has any logical drives then we also expect
9667c478bd9Sstevel@tonic-gate 	 * to find a partition marked as an extended DOS partition.
9677c478bd9Sstevel@tonic-gate 	 *
9687c478bd9Sstevel@tonic-gate 	 * Sometimes we'll find multiple partitions marked as DOS drives.
9697c478bd9Sstevel@tonic-gate 	 * The Solaris fdisk program allows these partitions
9707c478bd9Sstevel@tonic-gate 	 * to be created, but Windows fdisk no longer does.  We still need
9717c478bd9Sstevel@tonic-gate 	 * to support these, though, since Windows does.  We also need to fix
9727c478bd9Sstevel@tonic-gate 	 * our fdisk to behave like the Windows version.
9737c478bd9Sstevel@tonic-gate 	 *
9747c478bd9Sstevel@tonic-gate 	 * It turns out that some off-the-shelf media have *only* an
9757c478bd9Sstevel@tonic-gate 	 * Extended partition, so we need to deal with that case as well.
9767c478bd9Sstevel@tonic-gate 	 *
9777c478bd9Sstevel@tonic-gate 	 * Only a single (the first) Extended or Boot Partition will
9787c478bd9Sstevel@tonic-gate 	 * be recognized.  Any others will be ignored.
9797c478bd9Sstevel@tonic-gate 	 */
9807c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
9810576819eSwyllys 		PC_DPRINTF1(2, "findTheDrive: found partition type %02x",
9820576819eSwyllys 			dosp[i].systid);
9830576819eSwyllys 
9847c478bd9Sstevel@tonic-gate 		if (isDosDrive(dosp[i].systid)) {
9857c478bd9Sstevel@tonic-gate 			if (primaryPart < 0) {
9867c478bd9Sstevel@tonic-gate 				logicalDriveCount++;
9877c478bd9Sstevel@tonic-gate 				primaryPart = i;
9887c478bd9Sstevel@tonic-gate 			} else {
9897c478bd9Sstevel@tonic-gate 				extraDrives[numExtraDrives++] = i;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 			continue;
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		if ((extendedPart < 0) && isDosExtended(dosp[i].systid)) {
9947c478bd9Sstevel@tonic-gate 			extendedPart = i;
9957c478bd9Sstevel@tonic-gate 			continue;
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 		if ((bootPart < 0) && isBootPart(dosp[i].systid)) {
9987c478bd9Sstevel@tonic-gate 			bootPart = i;
9997c478bd9Sstevel@tonic-gate 			continue;
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
10039bd42341Sfrankho 	if (ldrive == BOOT_PARTITION_DRIVE) {
10047c478bd9Sstevel@tonic-gate 		if (bootPart < 0) {
10059bd42341Sfrankho 			noLogicalDrive(ldrive);
10069bd42341Sfrankho 			return (EINVAL);
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 		*sysid = dosp[bootPart].systid;
10097c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[bootPart].relsect);
10109bd42341Sfrankho 		return (0);
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10139bd42341Sfrankho 	if (ldrive == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
10147c478bd9Sstevel@tonic-gate 		*sysid = dosp[primaryPart].systid;
10157c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[primaryPart].relsect);
10169bd42341Sfrankho 		return (0);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * We are not looking for the C: drive (or the primary drive
10217c478bd9Sstevel@tonic-gate 	 * was not found), so we had better have an extended partition
10227c478bd9Sstevel@tonic-gate 	 * or extra drives in the Master FDISK table.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	if ((extendedPart < 0) && (numExtraDrives == 0)) {
10257c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no extended dos partition");
10269bd42341Sfrankho 		noLogicalDrive(ldrive);
10279bd42341Sfrankho 		return (EINVAL);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (extendedPart >= 0) {
10317c478bd9Sstevel@tonic-gate 		diskblk = xstartsect = ltohi(dosp[extendedPart].relsect);
10327c478bd9Sstevel@tonic-gate 		xnumsect = ltohi(dosp[extendedPart].numsect);
10337c478bd9Sstevel@tonic-gate 		do {
10347c478bd9Sstevel@tonic-gate 			/*
10357c478bd9Sstevel@tonic-gate 			 *  If the seek would not cause us to change
10367c478bd9Sstevel@tonic-gate 			 *  position on the drive, then we're out of
10377c478bd9Sstevel@tonic-gate 			 *  extended partitions to examine.
10387c478bd9Sstevel@tonic-gate 			 */
10397c478bd9Sstevel@tonic-gate 			if (diskblk == lastseek)
10407c478bd9Sstevel@tonic-gate 				break;
10417c478bd9Sstevel@tonic-gate 			logicalDriveCount += numDrives;
10427c478bd9Sstevel@tonic-gate 			/*
10437c478bd9Sstevel@tonic-gate 			 *  Seek the next extended partition, and find
10447c478bd9Sstevel@tonic-gate 			 *  logical drives within it.
10457c478bd9Sstevel@tonic-gate 			 */
10467c478bd9Sstevel@tonic-gate 			brelse(*bp);
10477c478bd9Sstevel@tonic-gate 			*bp = bread(dev, diskblk, PC_SAFESECSIZE);
10487c478bd9Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10497c478bd9Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10509bd42341Sfrankho 				return (EIO);
10517c478bd9Sstevel@tonic-gate 			}
10527c478bd9Sstevel@tonic-gate 			lastseek = diskblk;
10537c478bd9Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10547c478bd9Sstevel@tonic-gate 			if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
10557c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: "
10567c478bd9Sstevel@tonic-gate 				    "extended partition signature err");
10579bd42341Sfrankho 				return (EINVAL);
10587c478bd9Sstevel@tonic-gate 			}
10597c478bd9Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10607c478bd9Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10617c478bd9Sstevel@tonic-gate 			/*
10627c478bd9Sstevel@tonic-gate 			 *  Count up drives, and track where the next
10637c478bd9Sstevel@tonic-gate 			 *  extended partition is in case we need it.  We
10647c478bd9Sstevel@tonic-gate 			 *  are expecting only one extended partition.  If
10657c478bd9Sstevel@tonic-gate 			 *  there is more than one we'll only go to the
10667c478bd9Sstevel@tonic-gate 			 *  first one we see, but warn about ignoring.
10677c478bd9Sstevel@tonic-gate 			 */
10687c478bd9Sstevel@tonic-gate 			numDrives = 0;
10697c478bd9Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
10707c478bd9Sstevel@tonic-gate 				if (isDosDrive(dosp[i].systid)) {
10717c478bd9Sstevel@tonic-gate 					extndDrives[numDrives++] = i;
10727c478bd9Sstevel@tonic-gate 				} else if (isDosExtended(dosp[i].systid)) {
10737c478bd9Sstevel@tonic-gate 					if (diskblk != lastseek) {
10747c478bd9Sstevel@tonic-gate 						/*
10757c478bd9Sstevel@tonic-gate 						 * Already found an extended
10767c478bd9Sstevel@tonic-gate 						 * partition in this table.
10777c478bd9Sstevel@tonic-gate 						 */
10787c478bd9Sstevel@tonic-gate 						cmn_err(CE_NOTE,
10797c478bd9Sstevel@tonic-gate 						    "!pcfs: ignoring unexpected"
10807c478bd9Sstevel@tonic-gate 						    " additional extended"
10817c478bd9Sstevel@tonic-gate 						    " partition");
10829bd42341Sfrankho 					} else {
10839bd42341Sfrankho 						diskblk = xstartsect +
10849bd42341Sfrankho 						    ltohi(dosp[i].relsect);
10857c478bd9Sstevel@tonic-gate 					}
10867c478bd9Sstevel@tonic-gate 				}
10877c478bd9Sstevel@tonic-gate 			}
10889bd42341Sfrankho 		} while (ldrive > logicalDriveCount + numDrives);
10897c478bd9Sstevel@tonic-gate 
10909bd42341Sfrankho 		if (ldrive <= logicalDriveCount + numDrives) {
10917c478bd9Sstevel@tonic-gate 			/*
10927c478bd9Sstevel@tonic-gate 			 * The number of logical drives we've found thus
10937c478bd9Sstevel@tonic-gate 			 * far is enough to get us to the one we were
10947c478bd9Sstevel@tonic-gate 			 * searching for.
10957c478bd9Sstevel@tonic-gate 			 */
10969bd42341Sfrankho 			driveIndex = logicalDriveCount + numDrives - ldrive;
10977c478bd9Sstevel@tonic-gate 			*sysid = dosp[extndDrives[driveIndex]].systid;
10987c478bd9Sstevel@tonic-gate 			*startSector =
10997c478bd9Sstevel@tonic-gate 			    ltohi(dosp[extndDrives[driveIndex]].relsect) +
11007c478bd9Sstevel@tonic-gate 			    lastseek;
11017c478bd9Sstevel@tonic-gate 			if (*startSector > (xstartsect + xnumsect)) {
11027c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: extended partition "
11037c478bd9Sstevel@tonic-gate 				    "values bad");
11049bd42341Sfrankho 				return (EINVAL);
11057c478bd9Sstevel@tonic-gate 			}
11069bd42341Sfrankho 			return (0);
11077c478bd9Sstevel@tonic-gate 		} else {
11087c478bd9Sstevel@tonic-gate 			/*
11097c478bd9Sstevel@tonic-gate 			 * We ran out of extended dos partition
11107c478bd9Sstevel@tonic-gate 			 * drives.  The only hope now is to go
11117c478bd9Sstevel@tonic-gate 			 * back to extra drives defined in the master
11127c478bd9Sstevel@tonic-gate 			 * fdisk table.  But we overwrote that table
11137c478bd9Sstevel@tonic-gate 			 * already, so we must load it in again.
11147c478bd9Sstevel@tonic-gate 			 */
11157c478bd9Sstevel@tonic-gate 			logicalDriveCount += numDrives;
11167c478bd9Sstevel@tonic-gate 			brelse(*bp);
11177c478bd9Sstevel@tonic-gate 			*bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
11187c478bd9Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
11197c478bd9Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
11209bd42341Sfrankho 				return (EIO);
11217c478bd9Sstevel@tonic-gate 			}
11227c478bd9Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
11237c478bd9Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
11247c478bd9Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
11257c478bd9Sstevel@tonic-gate 		}
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	/*
11287c478bd9Sstevel@tonic-gate 	 *  Still haven't found the drive, is it an extra
11297c478bd9Sstevel@tonic-gate 	 *  drive defined in the main FDISK table?
11307c478bd9Sstevel@tonic-gate 	 */
11319bd42341Sfrankho 	if (ldrive <= logicalDriveCount + numExtraDrives) {
11329bd42341Sfrankho 		driveIndex = logicalDriveCount + numExtraDrives - ldrive;
11339bd42341Sfrankho 		ASSERT(driveIndex < MIN(numExtraDrives, FD_NUMPART));
11347c478bd9Sstevel@tonic-gate 		*sysid = dosp[extraDrives[driveIndex]].systid;
11357c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[extraDrives[driveIndex]].relsect);
11369bd42341Sfrankho 		return (0);
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	/*
11397c478bd9Sstevel@tonic-gate 	 *  Still haven't found the drive, and there is
11407c478bd9Sstevel@tonic-gate 	 *  nowhere else to look.
11417c478bd9Sstevel@tonic-gate 	 */
11429bd42341Sfrankho 	noLogicalDrive(ldrive);
11439bd42341Sfrankho 	return (EINVAL);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11460576819eSwyllys /*
11470576819eSwyllys  * FAT12/FAT16 specific consistency checks.
11480576819eSwyllys  */
11490576819eSwyllys static int
11500576819eSwyllys check_bpb_fat16(struct bootsec *bpb)
11510576819eSwyllys {
11520576819eSwyllys 	if (pcfsdebuglevel >= 5) {
11530576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat: RootEntCount = %d",
11540576819eSwyllys 			ltohs(bpb->rdirents[0]));
11550576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec16 = %d",
11560576819eSwyllys 			ltohs(bpb->numsect[0]));
11570576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: FATSz16 = %d",
11580576819eSwyllys 			ltohs(bpb->fatsec));
11590576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec32 = %d",
11600576819eSwyllys 			ltohi(bpb->totalsec));
11610576819eSwyllys 	}
11620576819eSwyllys 	return (ltohs(bpb->rdirents[0]) > 0 &&	 /* RootEntCnt > 0 */
11630576819eSwyllys 		((ltohs(bpb->numsect[0]) == 0 && /* TotSec16 == 0 */
11640576819eSwyllys 		ltohi(bpb->totalsec) > 0) ||	 /* TotSec32 > 0 */
11650576819eSwyllys 		ltohs(bpb->numsect[0]) > 0) && /* TotSec16 > 0 */
11660576819eSwyllys 		ltohs(bpb->fatsec) > 0);	 /* FatSz16 > 0 */
11670576819eSwyllys }
11680576819eSwyllys 
11690576819eSwyllys /*
11700576819eSwyllys  * FAT32 specific consistency checks.
11710576819eSwyllys  */
11720576819eSwyllys static int
11730576819eSwyllys check_bpb_fat32(struct fat32_bootsec *bpb)
11740576819eSwyllys {
11750576819eSwyllys 	if (pcfsdebuglevel >= 5) {
11760576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: RootEntCount = %d",
11770576819eSwyllys 			ltohs(bpb->f_bs.rdirents[0]));
11780576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec16 = %d",
11790576819eSwyllys 			ltohs(bpb->f_bs.numsect[0]));
11800576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz16 = %d",
11810576819eSwyllys 			ltohs(bpb->f_bs.fatsec));
11820576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec32 = %d",
11830576819eSwyllys 			ltohi(bpb->f_bs.totalsec));
11840576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz32 = %d",
11850576819eSwyllys 			ltohi(bpb->f_fatlength));
11860576819eSwyllys 	}
11870576819eSwyllys 	return (ltohs(bpb->f_bs.rdirents[0]) == 0 &&
11880576819eSwyllys 		ltohs(bpb->f_bs.numsect[0]) == 0 &&
11890576819eSwyllys 		ltohs(bpb->f_bs.fatsec) == 0 &&
11900576819eSwyllys 		ltohi(bpb->f_bs.totalsec) > 0 &&
11910576819eSwyllys 		ltohi(bpb->f_fatlength) > 0);
11920576819eSwyllys }
11930576819eSwyllys 
11940576819eSwyllys /*
11950576819eSwyllys  * Calculate the number of clusters in order to determine
11960576819eSwyllys  * the type of FAT we are looking at.  This is the only
11970576819eSwyllys  * recommended way of determining FAT type, though there
11980576819eSwyllys  * are other hints in the data, this is the best way.
11990576819eSwyllys  */
12000576819eSwyllys static ulong_t
12010576819eSwyllys bpb_to_numclusters(uchar_t *cp)
12020576819eSwyllys {
12030576819eSwyllys 	struct fat32_bootsec *bpb;
12040576819eSwyllys 
12050576819eSwyllys 	ulong_t rootdirsectors;
12060576819eSwyllys 	ulong_t FATsz;
12070576819eSwyllys 	ulong_t TotSec;
12080576819eSwyllys 	ulong_t DataSec;
12090576819eSwyllys 	ulong_t CountOfClusters;
12100576819eSwyllys 	char FileSysType[9];
12110576819eSwyllys 
12120576819eSwyllys 	/*
12130576819eSwyllys 	 * Cast it to FAT32 bpb. If it turns out to be FAT12/16, its
12140576819eSwyllys 	 * OK, we won't try accessing the data beyond the FAT16 header
12150576819eSwyllys 	 * boundary.
12160576819eSwyllys 	 */
12170576819eSwyllys 	bpb = (struct fat32_bootsec *)cp;
12180576819eSwyllys 
12190576819eSwyllys 	if (pcfsdebuglevel >= 5) {
12200576819eSwyllys 		if (ltohs(bpb->f_bs.rdirents[0]) != 0) {
122157e22c6cSwyllys 			(void) memcpy(FileSysType, &cp[54], 8);
12220576819eSwyllys 			FileSysType[8] = 0;
12230576819eSwyllys 			PC_DPRINTF1(5, "debug_bpb: FAT12/FAT16 FileSysType = "
12240576819eSwyllys 				"%s", FileSysType);
12250576819eSwyllys 		}
12260576819eSwyllys 	}
12270576819eSwyllys 
12280576819eSwyllys 	rootdirsectors = ((ltohs(bpb->f_bs.rdirents[0]) * 32) +
12290576819eSwyllys 		(ltohs(bpb->f_bs.bps[0]) - 1)) / ltohs(bpb->f_bs.bps[0]);
12300576819eSwyllys 
12310576819eSwyllys 	if (ltohs(bpb->f_bs.fatsec) != 0)
12320576819eSwyllys 		FATsz = ltohs(bpb->f_bs.fatsec);
12330576819eSwyllys 	else
12340576819eSwyllys 		FATsz = ltohi(bpb->f_fatlength);
12350576819eSwyllys 
12360576819eSwyllys 	if (ltohs(bpb->f_bs.numsect[0]) != 0)
12370576819eSwyllys 		TotSec = ltohs(bpb->f_bs.numsect[0]);
12380576819eSwyllys 	else
12390576819eSwyllys 		TotSec = ltohi(bpb->f_bs.totalsec);
12400576819eSwyllys 
12410576819eSwyllys 	DataSec = TotSec - (ltohs(bpb->f_bs.res_sec[0]) +
12420576819eSwyllys 			(bpb->f_bs.nfat * FATsz) + rootdirsectors);
12430576819eSwyllys 
12440576819eSwyllys 	CountOfClusters = DataSec / bpb->f_bs.spcl;
12450576819eSwyllys 
12460576819eSwyllys 	PC_DPRINTF1(5, "debug_bpb: CountOfClusters = %ld", CountOfClusters);
12470576819eSwyllys 
12480576819eSwyllys 	return (CountOfClusters);
12490576819eSwyllys 
12500576819eSwyllys }
12510576819eSwyllys 
12520576819eSwyllys static int
12530576819eSwyllys fattype(ulong_t CountOfClusters)
12540576819eSwyllys {
12550576819eSwyllys 	/*
12560576819eSwyllys 	 * From Microsoft:
12570576819eSwyllys 	 * In the following example, when it says <, it does not mean <=.
12580576819eSwyllys 	 * Note also that the numbers are correct.  The first number for
12590576819eSwyllys 	 * FAT12 is 4085; the second number for FAT16 is 65525. These numbers
12600576819eSwyllys 	 * and the '<' signs are not wrong.
12610576819eSwyllys 	 */
12620576819eSwyllys 
12630576819eSwyllys 	/* Watch for edge cases */
12640576819eSwyllys 	if ((CountOfClusters >= 4085 && CountOfClusters <= 4095) ||
12650576819eSwyllys 	    (CountOfClusters >= 65525 && CountOfClusters <= 65535)) {
12660576819eSwyllys 		PC_DPRINTF1(5, "debug_bpb: Cannot determine FAT yet - %ld",
12670576819eSwyllys 			CountOfClusters);
12680576819eSwyllys 		return (-1); /* Cannot be determined yet */
12690576819eSwyllys 	} else if (CountOfClusters < 4085) {
12700576819eSwyllys 		/* Volume is FAT12 */
12710576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT12");
12720576819eSwyllys 		return (0);
12730576819eSwyllys 	} else if (CountOfClusters < 65525) {
12740576819eSwyllys 		/* Volume is FAT16 */
12750576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT16");
12760576819eSwyllys 		return (PCFS_FAT16);
12770576819eSwyllys 	} else {
12780576819eSwyllys 		/* Volume is FAT32 */
12790576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT32");
12800576819eSwyllys 		return (PCFS_FAT32);
12810576819eSwyllys 	}
12820576819eSwyllys }
12830576819eSwyllys 
12840576819eSwyllys #define	VALID_SECSIZE(s) (s == 512 || s == 1024 || s == 2048 || s == 4096)
12850576819eSwyllys 
12860576819eSwyllys #define	VALID_SPCL(s) (s == 1 || s == 2 || s == 4 || s == 8 || s == 16 ||\
12870576819eSwyllys 	s == 32 || s == 64 || s == 128)
12880576819eSwyllys 
12890576819eSwyllys static int
12900576819eSwyllys secondaryBPBChecks(uchar_t *cp)
12910576819eSwyllys {
12920576819eSwyllys 	struct bootsec *bpb = (struct bootsec *)cp;
12930576819eSwyllys 	struct fat32_bootsec *f32bpb = (struct fat32_bootsec *)cp;
12940576819eSwyllys 
12950576819eSwyllys 	/*
12960576819eSwyllys 	 * Perform secondary checks to try and determine what sort
12970576819eSwyllys 	 * of FAT partition we have based on other, less reliable,
12980576819eSwyllys 	 * data in the BPB header.
12990576819eSwyllys 	 */
13000576819eSwyllys 	if (ltohs(bpb->fatsec) != 0) {
13010576819eSwyllys 		/*
13020576819eSwyllys 		 * Must be FAT12 or FAT16, check the
13030576819eSwyllys 		 * FilSysType string (not 100% reliable).
13040576819eSwyllys 		 */
13050576819eSwyllys 		if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT12", 5)) {
13060576819eSwyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT12");
13070576819eSwyllys 			return (0); /* FAT12 */
13080576819eSwyllys 		} else if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT16",
13090576819eSwyllys 			5)) {
13100576819eSwyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT16");
13110576819eSwyllys 			return (PCFS_FAT16);
13120576819eSwyllys 		} else {
13130576819eSwyllys 			/*
13140576819eSwyllys 			 * Try to use the BPB_Media byte
13150576819eSwyllys 			 *
13160576819eSwyllys 			 *  If the media byte indicates a floppy we'll
13170576819eSwyllys 			 *  assume FAT12, otherwise we'll assume FAT16.
13180576819eSwyllys 			 */
13190576819eSwyllys 			switch (bpb->mediadesriptor) {
13200576819eSwyllys 				case SS8SPT:
13210576819eSwyllys 				case DS8SPT:
13220576819eSwyllys 				case SS9SPT:
13230576819eSwyllys 				case DS9SPT:
13240576819eSwyllys 				case DS18SPT:
13250576819eSwyllys 				case DS9_15SPT:
13260576819eSwyllys 					PC_DPRINTF0(5,
13270576819eSwyllys 					"secondaryBPBCheck says: FAT12");
13280576819eSwyllys 					return (0); /* FAT12 */
13290576819eSwyllys 				case MD_FIXED:
13300576819eSwyllys 					PC_DPRINTF0(5,
13310576819eSwyllys 					"secondaryBPBCheck says: FAT16");
13320576819eSwyllys 					return (PCFS_FAT16);
13330576819eSwyllys 				default:
13340576819eSwyllys 					cmn_err(CE_NOTE,
13350576819eSwyllys 						"!pcfs: unknown FAT type");
13360576819eSwyllys 					return (-1);
13370576819eSwyllys 			}
13380576819eSwyllys 		}
13390576819eSwyllys 	} else if (ltohi(f32bpb->f_fatlength) > 0) {
13400576819eSwyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: FAT32");
13410576819eSwyllys 		return (PCFS_FAT32);
13420576819eSwyllys 	} else {
13430576819eSwyllys 		/* We don't know */
13440576819eSwyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: unknown!!");
13450576819eSwyllys 		return (-1);
13460576819eSwyllys 	}
13470576819eSwyllys }
13480576819eSwyllys 
13490576819eSwyllys /*
13500576819eSwyllys  * Check to see if the BPB we found is correct.
13510576819eSwyllys  *
13520576819eSwyllys  * First, look for obvious, tell-tale signs of trouble:
13530576819eSwyllys  * The NumFATs value should always be 2.  Sometimes it can be a '1'
13540576819eSwyllys  * on FLASH memory cards and other non-disk-based media, so we
13550576819eSwyllys  * will allow that as well.
13560576819eSwyllys  *
13570576819eSwyllys  * We also look at the Media byte, the valid range is 0xF0, or
13580576819eSwyllys  * 0xF8 thru 0xFF, anything else means this is probably not a good
13590576819eSwyllys  * BPB.
13600576819eSwyllys  *
13610576819eSwyllys  * Finally, check the BPB Magic number at the end of the 512 byte
13620576819eSwyllys  * block, it must be 0xAA55.
13630576819eSwyllys  *
13640576819eSwyllys  * If that all is good, calculate the number of clusters and
13650576819eSwyllys  * do some final verification steps.
13660576819eSwyllys  *
13679bd42341Sfrankho  * If all is well, return success (1) and set the fattypep value to the
13689bd42341Sfrankho  * correct FAT value if the caller provided a pointer to store it in.
13690576819eSwyllys  */
13700576819eSwyllys static int
13710576819eSwyllys isBPB(uchar_t *cp, int *fattypep)
13720576819eSwyllys {
13730576819eSwyllys 	struct bootsec *bpb = (struct bootsec *)cp;
13749bd42341Sfrankho 	int type;
13750576819eSwyllys 
13760576819eSwyllys 	uint_t numclusters;		/* number of clusters in file area */
13770576819eSwyllys 	ushort_t secsize = (int)ltohs(bpb->bps[0]);
13780576819eSwyllys 
13790576819eSwyllys 	if (pcfsdebuglevel >= 3) {
13800576819eSwyllys 		if (!VALID_SECSIZE(secsize))
13810576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: invalid bps value %d",
13820576819eSwyllys 				secsize);
13830576819eSwyllys 
13840576819eSwyllys 		if (!VALID_SPCL(bpb->spcl))
13850576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: invalid spcl value %d",
13860576819eSwyllys 				bpb->spcl);
13870576819eSwyllys 
13880576819eSwyllys 		if ((secsize * bpb->spcl) >= (32 * 1024))
13890576819eSwyllys 			PC_DPRINTF3(3, "check_bpb: BPC > 32K  %d x %d = %d",
13900576819eSwyllys 				secsize,
13910576819eSwyllys 				bpb->spcl,
13920576819eSwyllys 				secsize * bpb->spcl);
13930576819eSwyllys 
13940576819eSwyllys 		if (bpb->nfat == 0)
13950576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: bad NumFATs value %d",
13960576819eSwyllys 				bpb->nfat);
13970576819eSwyllys 
13980576819eSwyllys 		if (ltohs(bpb->res_sec[0]) == 0)
13990576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: bad RsvdSecCnt value %d",
14000576819eSwyllys 				ltohs(bpb->res_sec[0]));
14010576819eSwyllys 
14020576819eSwyllys 		PC_DPRINTF1(5, "check_bpb: Media byte = %02x",
14030576819eSwyllys 			bpb->mediadesriptor);
14040576819eSwyllys 
14050576819eSwyllys 	}
14060576819eSwyllys 	if ((bpb->nfat == 0) ||
14070576819eSwyllys 		(bpb->mediadesriptor != 0xF0 && bpb->mediadesriptor < 0xF8) ||
14080576819eSwyllys 		(ltohs(cp[510]) != MBB_MAGIC) ||
14090576819eSwyllys 		!VALID_SECSIZE(secsize) ||
14100576819eSwyllys 		!VALID_SPCL(bpb->spcl) ||
1411264a6e74Sfrankho 		(secsize * bpb->spcl > (64 * 1024)) ||
14120576819eSwyllys 		!(ltohs(bpb->res_sec[0])))
14130576819eSwyllys 		return (0);
14140576819eSwyllys 
14150576819eSwyllys 	/*
14160576819eSwyllys 	 * Basic sanity checks passed so far, now try to determine which
14170576819eSwyllys 	 * FAT format to use.
14180576819eSwyllys 	 */
14190576819eSwyllys 	numclusters = bpb_to_numclusters(cp);
14200576819eSwyllys 
14219bd42341Sfrankho 	type = fattype(numclusters);
14220576819eSwyllys 
14230576819eSwyllys 	/* Do some final sanity checks for each specific type of FAT */
14249bd42341Sfrankho 	switch (type) {
14250576819eSwyllys 		case 0: /* FAT12 */
14260576819eSwyllys 		case PCFS_FAT16:
14270576819eSwyllys 			if (!check_bpb_fat16((struct bootsec *)cp))
14280576819eSwyllys 				return (0);
14290576819eSwyllys 			break;
14300576819eSwyllys 		case PCFS_FAT32:
14310576819eSwyllys 			if (!check_bpb_fat32((struct fat32_bootsec *)cp))
14320576819eSwyllys 				return (0);
14330576819eSwyllys 			break;
14340576819eSwyllys 		default: /* not sure yet */
14359bd42341Sfrankho 			type = secondaryBPBChecks(cp);
14369bd42341Sfrankho 			if (type == -1) {
14370576819eSwyllys 				/* Still nothing, give it up. */
14380576819eSwyllys 				return (0);
14390576819eSwyllys 			}
14400576819eSwyllys 			break;
14410576819eSwyllys 	}
14429bd42341Sfrankho 
14439bd42341Sfrankho 	if (fattypep)
14449bd42341Sfrankho 		*fattypep = type;
14459bd42341Sfrankho 
14460576819eSwyllys 	PC_DPRINTF0(5, "isBPB: BPB passes verification tests");
14470576819eSwyllys 	return (1);
14480576819eSwyllys }
14490576819eSwyllys 
14500576819eSwyllys 
14517c478bd9Sstevel@tonic-gate /*
14527c478bd9Sstevel@tonic-gate  * Get the FAT type for the DOS medium.
14537c478bd9Sstevel@tonic-gate  *
14540576819eSwyllys  * -------------------------
14550576819eSwyllys  * According to Microsoft:
14560576819eSwyllys  *   The FAT type one of FAT12, FAT16, or FAT32 is determined by the
14570576819eSwyllys  * count of clusters on the volume and nothing else.
14580576819eSwyllys  * -------------------------
14597c478bd9Sstevel@tonic-gate  *
14607c478bd9Sstevel@tonic-gate  */
14617c478bd9Sstevel@tonic-gate static int
14627c478bd9Sstevel@tonic-gate pc_getfattype(
14637c478bd9Sstevel@tonic-gate 	struct vnode *devvp,
14647c478bd9Sstevel@tonic-gate 	int ldrive,
14657c478bd9Sstevel@tonic-gate 	daddr_t *strtsectp,
14667c478bd9Sstevel@tonic-gate 	int *fattypep)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	buf_t *bp = NULL;		/* Disk buffer pointer */
14699bd42341Sfrankho 	int err = 0;
14707c478bd9Sstevel@tonic-gate 	uchar_t sysid = 0;		/* System ID character */
14717c478bd9Sstevel@tonic-gate 	dev_t	dev = devvp->v_rdev;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	/*
14757c478bd9Sstevel@tonic-gate 	 * Open the device so we can check out the BPB or FDISK table,
14767c478bd9Sstevel@tonic-gate 	 * then read in the sector.
14777c478bd9Sstevel@tonic-gate 	 */
14787c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(5, "pc_getfattype: dev=%x  ldrive=%x  ", (int)dev, ldrive);
14799bd42341Sfrankho 	if (err = VOP_OPEN(&devvp, FREAD, CRED())) {
14809bd42341Sfrankho 		PC_DPRINTF1(1, "pc_getfattype: open error=%d\n", err);
14819bd42341Sfrankho 		return (err);
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/*
14859bd42341Sfrankho 	 * Unpartitioned media (floppies and some removeable devices)
14869bd42341Sfrankho 	 * don't have a partition table, the FAT BPB is at disk block 0.
14879bd42341Sfrankho 	 * Start out by reading block 0.
14887c478bd9Sstevel@tonic-gate 	 */
14899bd42341Sfrankho 	*strtsectp = (daddr_t)0;
14909bd42341Sfrankho 	bp = bread(dev, *strtsectp, PC_SAFESECSIZE);
14919bd42341Sfrankho 
14927c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
14939bd42341Sfrankho 		PC_DPRINTF2(1, "pc_getfattype: read error on "
14949bd42341Sfrankho 		    "device %d, disk LBA %d\n", (int)dev, (int)*strtsectp);
14959bd42341Sfrankho 		err = EIO;
14967c478bd9Sstevel@tonic-gate 		goto out;
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	/*
15009bd42341Sfrankho 	 * If a logical drive number is requested, parse the partition table
15019bd42341Sfrankho 	 * and attempt to locate it. Otherwise, proceed immediately to the
15029bd42341Sfrankho 	 * BPB check. findTheDrive(), if successful, returns the disk block
15039bd42341Sfrankho 	 * number where the requested partition starts in "strtsecp".
15047c478bd9Sstevel@tonic-gate 	 */
15059bd42341Sfrankho 	if (ldrive != 0) {
15060576819eSwyllys 		PC_DPRINTF0(5, "pc_getfattype: using FDISK table to find BPB");
15077c478bd9Sstevel@tonic-gate 
15089bd42341Sfrankho 		if (err = findTheDrive(dev, ldrive, &bp, strtsectp, &sysid))
15090576819eSwyllys 			goto out;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		brelse(bp);
15127c478bd9Sstevel@tonic-gate 		bp = bread(dev, *strtsectp, PC_SAFESECSIZE);
15137c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
15149bd42341Sfrankho 			PC_DPRINTF2(1, "pc_getfattype: read error on "
15159bd42341Sfrankho 			    "device %d, disk LBA %d\n",
15169bd42341Sfrankho 			    (int)dev, (int)*strtsectp);
15179bd42341Sfrankho 			err = EIO;
15187c478bd9Sstevel@tonic-gate 			goto out;
15197c478bd9Sstevel@tonic-gate 		}
15209bd42341Sfrankho 	}
15217c478bd9Sstevel@tonic-gate 
15229bd42341Sfrankho 	if (!isBPB((uchar_t *)bp->b_un.b_addr, fattypep)) {
15239bd42341Sfrankho 		PC_DPRINTF2(1, "pc_getfattype: No FAT BPB on device %d, "
15249bd42341Sfrankho 		    "disk LBA %d\n", (int)dev, (int)*strtsectp);
15259bd42341Sfrankho 		err = EIO;
15269bd42341Sfrankho 		goto out;
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate out:
15300576819eSwyllys 	/*
15310576819eSwyllys 	 * Release the buffer used
15320576819eSwyllys 	 */
15337c478bd9Sstevel@tonic-gate 	if (bp != NULL)
15347c478bd9Sstevel@tonic-gate 		brelse(bp);
15357c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, CRED());
15369bd42341Sfrankho 	return (err);
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate /*
15417c478bd9Sstevel@tonic-gate  * Get the boot parameter block and file allocation table.
15427c478bd9Sstevel@tonic-gate  * If there is an old FAT, invalidate it.
15437c478bd9Sstevel@tonic-gate  */
15447c478bd9Sstevel@tonic-gate int
15457c478bd9Sstevel@tonic-gate pc_getfat(struct pcfs *fsp)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate 	struct vfs *vfsp = PCFSTOVFS(fsp);
15487c478bd9Sstevel@tonic-gate 	struct buf *tp = 0;
15497c478bd9Sstevel@tonic-gate 	struct buf *bp = 0;
15507c478bd9Sstevel@tonic-gate 	uchar_t *fatp = NULL;
15517c478bd9Sstevel@tonic-gate 	uchar_t *fat_changemap = NULL;
15527c478bd9Sstevel@tonic-gate 	struct bootsec *bootp;
15537c478bd9Sstevel@tonic-gate 	struct fat32_bootsec *f32b;
15547c478bd9Sstevel@tonic-gate 	struct vnode *devvp;
15557c478bd9Sstevel@tonic-gate 	int error;
15567c478bd9Sstevel@tonic-gate 	int fatsize;
15577c478bd9Sstevel@tonic-gate 	int fat_changemapsize;
15587c478bd9Sstevel@tonic-gate 	int flags = 0;
15597c478bd9Sstevel@tonic-gate 	int nfat;
15607c478bd9Sstevel@tonic-gate 	int secsize;
15617c478bd9Sstevel@tonic-gate 	int fatsec;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(5, "pc_getfat\n");
15647c478bd9Sstevel@tonic-gate 	devvp = fsp->pcfs_devvp;
15657c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_fatp) {
15667c478bd9Sstevel@tonic-gate 		/*
15677c478bd9Sstevel@tonic-gate 		 * There is a FAT in core.
15687c478bd9Sstevel@tonic-gate 		 * If there are open file pcnodes or we have modified it or
15697c478bd9Sstevel@tonic-gate 		 * it hasn't timed out yet use the in core FAT.
15707c478bd9Sstevel@tonic-gate 		 * Otherwise invalidate it and get a new one
15717c478bd9Sstevel@tonic-gate 		 */
15727c478bd9Sstevel@tonic-gate #ifdef notdef
15737c478bd9Sstevel@tonic-gate 		if (fsp->pcfs_frefs ||
15747c478bd9Sstevel@tonic-gate 		    (fsp->pcfs_flags & PCFS_FATMOD) ||
15757c478bd9Sstevel@tonic-gate 		    (gethrestime_sec() < fsp->pcfs_fattime)) {
15767c478bd9Sstevel@tonic-gate 			return (0);
15777c478bd9Sstevel@tonic-gate 		} else {
15787c478bd9Sstevel@tonic-gate 			mutex_enter(&pcfslock);
15797c478bd9Sstevel@tonic-gate 			pc_invalfat(fsp);
15807c478bd9Sstevel@tonic-gate 			mutex_exit(&pcfslock);
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate #endif /* notdef */
15837c478bd9Sstevel@tonic-gate 		return (0);
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 	/*
15867c478bd9Sstevel@tonic-gate 	 * Open block device mounted on.
15877c478bd9Sstevel@tonic-gate 	 */
15887c478bd9Sstevel@tonic-gate 	error = VOP_OPEN(&devvp,
15897c478bd9Sstevel@tonic-gate 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
15907c478bd9Sstevel@tonic-gate 	    CRED());
15917c478bd9Sstevel@tonic-gate 	if (error) {
15927c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfat: open error=%d\n", error);
15937c478bd9Sstevel@tonic-gate 		return (error);
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 	/*
15967c478bd9Sstevel@tonic-gate 	 * Get boot parameter block and check it for validity
15977c478bd9Sstevel@tonic-gate 	 */
15987c478bd9Sstevel@tonic-gate 	tp = bread(fsp->pcfs_xdev, fsp->pcfs_dosstart, PC_SAFESECSIZE);
15999bd42341Sfrankho 	if ((tp->b_flags & (B_ERROR | B_STALE)) ||
16009bd42341Sfrankho 	    !isBPB((uchar_t *)tp->b_un.b_addr, NULL)) {
16019bd42341Sfrankho 		PC_DPRINTF2(1, "pc_getfat: boot block error on device %d, "
16029bd42341Sfrankho 		    "disk LBA %d\n",
16039bd42341Sfrankho 		    (int)fsp->pcfs_xdev, (int)fsp->pcfs_dosstart);
16047c478bd9Sstevel@tonic-gate 		flags = tp->b_flags & B_ERROR;
16057c478bd9Sstevel@tonic-gate 		error = EIO;
16067c478bd9Sstevel@tonic-gate 		goto out;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
16097c478bd9Sstevel@tonic-gate 	bootp = (struct bootsec *)tp->b_un.b_addr;
16107c478bd9Sstevel@tonic-gate 
16110576819eSwyllys 
16127c478bd9Sstevel@tonic-gate 	/* get the sector size - may be more than 512 bytes */
16137c478bd9Sstevel@tonic-gate 	secsize = (int)ltohs(bootp->bps[0]);
16147c478bd9Sstevel@tonic-gate 	/* check for bogus sector size - fat should be at least 1 sector */
16157c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16167c478bd9Sstevel@tonic-gate 		f32b = (struct fat32_bootsec *)bootp;
16177c478bd9Sstevel@tonic-gate 		fatsec = ltohi(f32b->f_fatlength);
16187c478bd9Sstevel@tonic-gate 	} else {
16197c478bd9Sstevel@tonic-gate 		fatsec = ltohs(bootp->fatsec);
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 	if (secsize < 512 || fatsec < 1 || bootp->nfat < 1) {
16227c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT size error");
16237c478bd9Sstevel@tonic-gate 		error = EINVAL;
16247c478bd9Sstevel@tonic-gate 		goto out;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	switch (bootp->mediadesriptor) {
16287c478bd9Sstevel@tonic-gate 	default:
16297c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: media-descriptor error, 0x%x",
16307c478bd9Sstevel@tonic-gate 		    bootp->mediadesriptor);
16317c478bd9Sstevel@tonic-gate 		error = EINVAL;
16327c478bd9Sstevel@tonic-gate 		goto out;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	case MD_FIXED:
16357c478bd9Sstevel@tonic-gate 		/*
163690c30842Sjmcp 		 * PCMCIA pseudo floppy is type MD_FIXED,
16377c478bd9Sstevel@tonic-gate 		 * but is accessed like a floppy
16387c478bd9Sstevel@tonic-gate 		 */
16397c478bd9Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_PCMCIA_NO_CIS)) {
16407c478bd9Sstevel@tonic-gate 			fsp->pcfs_flags |= PCFS_NOCHK;
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
16437c478bd9Sstevel@tonic-gate 	case SS8SPT:
16447c478bd9Sstevel@tonic-gate 	case DS8SPT:
16457c478bd9Sstevel@tonic-gate 	case SS9SPT:
16467c478bd9Sstevel@tonic-gate 	case DS9SPT:
16477c478bd9Sstevel@tonic-gate 	case DS18SPT:
16487c478bd9Sstevel@tonic-gate 	case DS9_15SPT:
16497c478bd9Sstevel@tonic-gate 		fsp->pcfs_secsize = secsize;
16507c478bd9Sstevel@tonic-gate 		fsp->pcfs_sdshift = secsize / DEV_BSIZE - 1;
16517c478bd9Sstevel@tonic-gate 		fsp->pcfs_entps = secsize / sizeof (struct pcdir);
16527c478bd9Sstevel@tonic-gate 		fsp->pcfs_spcl = (int)bootp->spcl;
16537c478bd9Sstevel@tonic-gate 		fsp->pcfs_fatsec = fatsec;
16547c478bd9Sstevel@tonic-gate 		fsp->pcfs_spt = (int)ltohs(bootp->spt);
1655264a6e74Sfrankho 		fsp->pcfs_rdirsec = ((int)ltohs(bootp->rdirents[0])
1656264a6e74Sfrankho 		    * sizeof (struct pcdir) + (secsize - 1)) / secsize;
16577c478bd9Sstevel@tonic-gate 		fsp->pcfs_clsize = fsp->pcfs_spcl * secsize;
16587c478bd9Sstevel@tonic-gate 		fsp->pcfs_fatstart = fsp->pcfs_dosstart +
16597c478bd9Sstevel@tonic-gate 		    (daddr_t)ltohs(bootp->res_sec[0]);
16607c478bd9Sstevel@tonic-gate 		fsp->pcfs_rdirstart = fsp->pcfs_fatstart +
16617c478bd9Sstevel@tonic-gate 		    (bootp->nfat * fsp->pcfs_fatsec);
16627c478bd9Sstevel@tonic-gate 		fsp->pcfs_datastart = fsp->pcfs_rdirstart + fsp->pcfs_rdirsec;
16637c478bd9Sstevel@tonic-gate 		if (IS_FAT32(fsp))
16647c478bd9Sstevel@tonic-gate 			fsp->pcfs_rdirstart = ltohi(f32b->f_rootcluster);
16657c478bd9Sstevel@tonic-gate 		fsp->pcfs_ncluster = (((int)(ltohs(bootp->numsect[0]) ?
16667c478bd9Sstevel@tonic-gate 		    ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec))) -
16677c478bd9Sstevel@tonic-gate 		    fsp->pcfs_datastart + fsp->pcfs_dosstart) / fsp->pcfs_spcl;
16687c478bd9Sstevel@tonic-gate 		fsp->pcfs_numfat = (int)bootp->nfat;
16697c478bd9Sstevel@tonic-gate 		fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER;
16707c478bd9Sstevel@tonic-gate 		break;
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/*
16747c478bd9Sstevel@tonic-gate 	 * Get FAT and check it for validity
16757c478bd9Sstevel@tonic-gate 	 */
16767c478bd9Sstevel@tonic-gate 	fatsize = fsp->pcfs_fatsec * fsp->pcfs_secsize;
16777c478bd9Sstevel@tonic-gate 	fatp = kmem_alloc(fatsize, KM_SLEEP);
16787c478bd9Sstevel@tonic-gate 	error = pc_readfat(fsp, fatp, fsp->pcfs_fatstart, fatsize);
16797c478bd9Sstevel@tonic-gate 	if (error) {
16807c478bd9Sstevel@tonic-gate 		flags = B_ERROR;
16817c478bd9Sstevel@tonic-gate 		goto out;
16827c478bd9Sstevel@tonic-gate 	}
16837c478bd9Sstevel@tonic-gate 	fat_changemapsize = (fatsize / fsp->pcfs_clsize) + 1;
16847c478bd9Sstevel@tonic-gate 	fat_changemap = kmem_zalloc(fat_changemapsize, KM_SLEEP);
16857c478bd9Sstevel@tonic-gate 
16860576819eSwyllys 	/*
16870576819eSwyllys 	 * The only definite signature check is that the
16880576819eSwyllys 	 * media descriptor byte should match the first byte
16890576819eSwyllys 	 * of the FAT block.
16900576819eSwyllys 	 */
16910576819eSwyllys 	if (fatp[0] != bootp->mediadesriptor) {
16927c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT signature error");
16937c478bd9Sstevel@tonic-gate 		error = EINVAL;
16947c478bd9Sstevel@tonic-gate 		goto out;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 	/*
16977c478bd9Sstevel@tonic-gate 	 * Checking for fatsec and number of supported clusters, should
16980576819eSwyllys 	 * actually determine a FAT12/FAT media.
16997c478bd9Sstevel@tonic-gate 	 */
17007c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_FAT16) {
17017c478bd9Sstevel@tonic-gate 		if ((fsp->pcfs_fatsec <= 12) &&
17027c478bd9Sstevel@tonic-gate 		    ((fatsize * 2 / 3) >= fsp->pcfs_ncluster)) {
17037c478bd9Sstevel@tonic-gate 			/*
17047c478bd9Sstevel@tonic-gate 			 * We have a 12-bit FAT, rather than a 16-bit FAT.
17057c478bd9Sstevel@tonic-gate 			 * Ignore what the fdisk table says.
17067c478bd9Sstevel@tonic-gate 			 */
17077c478bd9Sstevel@tonic-gate 			PC_DPRINTF0(2, "pc_getfattype: forcing 12-bit FAT\n");
17087c478bd9Sstevel@tonic-gate 			fsp->pcfs_flags &= ~PCFS_FAT16;
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 	/*
17127c478bd9Sstevel@tonic-gate 	 * Sanity check our FAT is large enough for the
17137c478bd9Sstevel@tonic-gate 	 * clusters we think we have.
17147c478bd9Sstevel@tonic-gate 	 */
17157c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_FAT16) &&
17167c478bd9Sstevel@tonic-gate 	    ((fatsize / 2) < fsp->pcfs_ncluster)) {
17177c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT too small for number of clusters");
17187c478bd9Sstevel@tonic-gate 		error = EINVAL;
17197c478bd9Sstevel@tonic-gate 		goto out;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	/*
17237c478bd9Sstevel@tonic-gate 	 * Get alternate FATs and check for consistency
17247c478bd9Sstevel@tonic-gate 	 * This is an inlined version of pc_readfat().
17257c478bd9Sstevel@tonic-gate 	 * Since we're only comparing FAT and alternate FAT,
17267c478bd9Sstevel@tonic-gate 	 * there's no reason to let pc_readfat() copy data out
17277c478bd9Sstevel@tonic-gate 	 * of the buf. Instead, compare in-situ, one cluster
17287c478bd9Sstevel@tonic-gate 	 * at a time.
17297c478bd9Sstevel@tonic-gate 	 */
17307c478bd9Sstevel@tonic-gate 	for (nfat = 1; nfat < fsp->pcfs_numfat; nfat++) {
17317c478bd9Sstevel@tonic-gate 		size_t startsec;
17327c478bd9Sstevel@tonic-gate 		size_t off;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 		startsec = fsp->pcfs_fatstart + nfat * fsp->pcfs_fatsec;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 		for (off = 0; off < fatsize; off += fsp->pcfs_clsize) {
17377c478bd9Sstevel@tonic-gate 			bp = bread(fsp->pcfs_xdev, pc_dbdaddr(fsp,
17387c478bd9Sstevel@tonic-gate 				startsec +
17397c478bd9Sstevel@tonic-gate 				pc_cltodb(fsp, pc_lblkno(fsp, off))),
17407c478bd9Sstevel@tonic-gate 				MIN(fsp->pcfs_clsize, fatsize - off));
17417c478bd9Sstevel@tonic-gate 			if (bp->b_flags & (B_ERROR | B_STALE)) {
17427c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17437c478bd9Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d read error"
17447c478bd9Sstevel@tonic-gate 					" at byte %ld", nfat, off);
17457c478bd9Sstevel@tonic-gate 				flags = B_ERROR;
17467c478bd9Sstevel@tonic-gate 				error = EIO;
17477c478bd9Sstevel@tonic-gate 				goto out;
17487c478bd9Sstevel@tonic-gate 			}
17497c478bd9Sstevel@tonic-gate 			bp->b_flags |= B_STALE | B_AGE;
17507c478bd9Sstevel@tonic-gate 			if (bcmp(bp->b_un.b_addr,
17517c478bd9Sstevel@tonic-gate 			    fatp + off,
17527c478bd9Sstevel@tonic-gate 			    MIN(fsp->pcfs_clsize, fatsize - off))) {
17537c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17547c478bd9Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d corrupted"
17557c478bd9Sstevel@tonic-gate 					" at byte %ld", nfat, off);
17567c478bd9Sstevel@tonic-gate 				flags = B_ERROR;
17577c478bd9Sstevel@tonic-gate 			}
17587c478bd9Sstevel@tonic-gate 			brelse(bp);
17597c478bd9Sstevel@tonic-gate 			bp = NULL;	/* prevent double release */
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatsize = fatsize;
17647c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatp = fatp;
17657c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemapsize = fat_changemapsize;
17667c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = fat_changemap;
17677c478bd9Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
17687c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatjustread = 1;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	brelse(tp);
17717c478bd9Sstevel@tonic-gate 	tp = NULL;
17727c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
17737c478bd9Sstevel@tonic-gate 		/* get fsinfo */
17747c478bd9Sstevel@tonic-gate 		struct fat32_boot_fsinfo fsinfo_disk;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 		fsp->f32fsinfo_sector = ltohs(f32b->f_infosector);
17777c478bd9Sstevel@tonic-gate 		tp = bread(fsp->pcfs_xdev,
17787c478bd9Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
17797c478bd9Sstevel@tonic-gate 		    PC_SAFESECSIZE);
17807c478bd9Sstevel@tonic-gate 		if (tp->b_flags & (B_ERROR | B_STALE)) {
17817c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: error reading fat32 fsinfo");
17827c478bd9Sstevel@tonic-gate 			flags = tp->b_flags & B_ERROR;
17837c478bd9Sstevel@tonic-gate 			brelse(tp);
17847c478bd9Sstevel@tonic-gate 			tp = NULL;
17857c478bd9Sstevel@tonic-gate 			error = EIO;
17867c478bd9Sstevel@tonic-gate 			goto out;
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 		tp->b_flags |= B_STALE | B_AGE;
17897c478bd9Sstevel@tonic-gate 		bcopy((void *)(tp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
17907c478bd9Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
17917c478bd9Sstevel@tonic-gate 		brelse(tp);
17927c478bd9Sstevel@tonic-gate 		tp = NULL;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 		/* translated fields */
17957c478bd9Sstevel@tonic-gate 		fsp->fsinfo_native.fs_signature =
17967c478bd9Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_signature);
17977c478bd9Sstevel@tonic-gate 		fsp->fsinfo_native.fs_free_clusters =
17987c478bd9Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_free_clusters);
17997c478bd9Sstevel@tonic-gate 		if (fsp->fsinfo_native.fs_signature != FAT32_FS_SIGN) {
18007c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
18017c478bd9Sstevel@tonic-gate 			    "!pcfs: fat32 fsinfo signature mismatch.");
18027c478bd9Sstevel@tonic-gate 			error = EINVAL;
18037c478bd9Sstevel@tonic-gate 			goto out;
18047c478bd9Sstevel@tonic-gate 		}
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	return (0);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate out:
18107c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!pcfs: illegal disk format");
18117c478bd9Sstevel@tonic-gate 	if (tp)
18127c478bd9Sstevel@tonic-gate 		brelse(tp);
18137c478bd9Sstevel@tonic-gate 	if (bp)
18147c478bd9Sstevel@tonic-gate 		brelse(bp);
18157c478bd9Sstevel@tonic-gate 	if (fatp)
18167c478bd9Sstevel@tonic-gate 		kmem_free(fatp, fatsize);
18177c478bd9Sstevel@tonic-gate 	if (fat_changemap)
18187c478bd9Sstevel@tonic-gate 		kmem_free(fat_changemap, fat_changemapsize);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if (flags) {
18217c478bd9Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
18247c478bd9Sstevel@tonic-gate 	    FREAD : FREAD|FWRITE, 1, (offset_t)0, CRED());
18257c478bd9Sstevel@tonic-gate 	return (error);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate int
18297c478bd9Sstevel@tonic-gate pc_syncfat(struct pcfs *fsp)
18307c478bd9Sstevel@tonic-gate {
18317c478bd9Sstevel@tonic-gate 	struct buf *bp;
18327c478bd9Sstevel@tonic-gate 	int nfat;
18337c478bd9Sstevel@tonic-gate 	int	error;
18347c478bd9Sstevel@tonic-gate 	struct fat32_boot_fsinfo fsinfo_disk;
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfat\n");
18377c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_fatp == (uchar_t *)0) ||
18387c478bd9Sstevel@tonic-gate 	    !(fsp->pcfs_flags & PCFS_FATMOD))
18397c478bd9Sstevel@tonic-gate 		return (0);
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * write out all copies of FATs
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	fsp->pcfs_flags &= ~PCFS_FATMOD;
18447c478bd9Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
18457c478bd9Sstevel@tonic-gate 	for (nfat = 0; nfat < fsp->pcfs_numfat; nfat++) {
18467c478bd9Sstevel@tonic-gate 		error = pc_writefat(fsp,
18477c478bd9Sstevel@tonic-gate 		    fsp->pcfs_fatstart + nfat*fsp->pcfs_fatsec);
18487c478bd9Sstevel@tonic-gate 		if (error) {
18497c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
18507c478bd9Sstevel@tonic-gate 			return (EIO);
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 	pc_clear_fatchanges(fsp);
18547c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(6, "pcfs_syncfat: wrote out FAT\n");
18557c478bd9Sstevel@tonic-gate 	/* write out fsinfo */
18567c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
18577c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
18587c478bd9Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
18597c478bd9Sstevel@tonic-gate 		    PC_SAFESECSIZE);
18607c478bd9Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
18617c478bd9Sstevel@tonic-gate 			brelse(bp);
18627c478bd9Sstevel@tonic-gate 			return (EIO);
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 		bcopy((void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18657c478bd9Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
18667c478bd9Sstevel@tonic-gate 		/* translate fields */
18677c478bd9Sstevel@tonic-gate 		fsinfo_disk.fs_free_clusters =
18687c478bd9Sstevel@tonic-gate 		    htoli(fsp->fsinfo_native.fs_free_clusters);
18697c478bd9Sstevel@tonic-gate 		fsinfo_disk.fs_next_cluster = (uint32_t)FSINFO_UNKNOWN;
18707c478bd9Sstevel@tonic-gate 		bcopy(&fsinfo_disk,
18717c478bd9Sstevel@tonic-gate 		    (void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18727c478bd9Sstevel@tonic-gate 		    sizeof (struct fat32_boot_fsinfo));
18737c478bd9Sstevel@tonic-gate 		bwrite2(bp);
18747c478bd9Sstevel@tonic-gate 		error = geterror(bp);
18757c478bd9Sstevel@tonic-gate 		brelse(bp);
18767c478bd9Sstevel@tonic-gate 		if (error) {
18777c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
18787c478bd9Sstevel@tonic-gate 			return (EIO);
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	return (0);
18827c478bd9Sstevel@tonic-gate }
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate void
18857c478bd9Sstevel@tonic-gate pc_invalfat(struct pcfs *fsp)
18867c478bd9Sstevel@tonic-gate {
18877c478bd9Sstevel@tonic-gate 	struct pcfs *xfsp;
18887c478bd9Sstevel@tonic-gate 	int mount_cnt = 0;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pc_invalfat\n");
18917c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_fatp == (uchar_t *)0)
18927c478bd9Sstevel@tonic-gate 		panic("pc_invalfat");
18937c478bd9Sstevel@tonic-gate 	/*
18947c478bd9Sstevel@tonic-gate 	 * Release FAT
18957c478bd9Sstevel@tonic-gate 	 */
18967c478bd9Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fatp, fsp->pcfs_fatsize);
18977c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatp = NULL;
18987c478bd9Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fat_changemap, fsp->pcfs_fat_changemapsize);
18997c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = NULL;
19007c478bd9Sstevel@tonic-gate 	/*
19017c478bd9Sstevel@tonic-gate 	 * Invalidate all the blocks associated with the device.
19027c478bd9Sstevel@tonic-gate 	 * Not needed if stateless.
19037c478bd9Sstevel@tonic-gate 	 */
19047c478bd9Sstevel@tonic-gate 	for (xfsp = pc_mounttab; xfsp; xfsp = xfsp->pcfs_nxt)
19057c478bd9Sstevel@tonic-gate 		if (xfsp != fsp && xfsp->pcfs_xdev == fsp->pcfs_xdev)
19067c478bd9Sstevel@tonic-gate 			mount_cnt++;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	if (!mount_cnt)
19097c478bd9Sstevel@tonic-gate 		binval(fsp->pcfs_xdev);
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * close mounted device
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(fsp->pcfs_devvp,
19147c478bd9Sstevel@tonic-gate 	    (PCFSTOVFS(fsp)->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
19157c478bd9Sstevel@tonic-gate 	    1, (offset_t)0, CRED());
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate void
19197c478bd9Sstevel@tonic-gate pc_badfs(struct pcfs *fsp)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "corrupted PC file system on dev %x.%x\n",
19227c478bd9Sstevel@tonic-gate 	    getmajor(fsp->pcfs_devvp->v_rdev),
19237c478bd9Sstevel@tonic-gate 	    getminor(fsp->pcfs_devvp->v_rdev));
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate /*
19277c478bd9Sstevel@tonic-gate  * The problem with supporting NFS on the PCFS filesystem is that there
19287c478bd9Sstevel@tonic-gate  * is no good place to keep the generation number. The only possible
19297c478bd9Sstevel@tonic-gate  * place is inside a directory entry. There are a few words that we
19307c478bd9Sstevel@tonic-gate  * don't use - they store NT & OS/2 attributes, and the creation/last access
19317c478bd9Sstevel@tonic-gate  * time of the file - but it seems wrong to use them. In addition, directory
19327c478bd9Sstevel@tonic-gate  * entries come and go. If a directory is removed completely, its directory
19337c478bd9Sstevel@tonic-gate  * blocks are freed and the generation numbers are lost. Whereas in ufs,
19347c478bd9Sstevel@tonic-gate  * inode blocks are dedicated for inodes, so the generation numbers are
19357c478bd9Sstevel@tonic-gate  * permanently kept on the disk.
19367c478bd9Sstevel@tonic-gate  */
19377c478bd9Sstevel@tonic-gate static int
19387c478bd9Sstevel@tonic-gate pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
19417c478bd9Sstevel@tonic-gate 	struct pc_fid *pcfid;
19427c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
19437c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
19447c478bd9Sstevel@tonic-gate 	daddr_t eblkno;
19457c478bd9Sstevel@tonic-gate 	int eoffset;
19467c478bd9Sstevel@tonic-gate 	struct buf *bp;
19477c478bd9Sstevel@tonic-gate 	int error;
19487c478bd9Sstevel@tonic-gate 	pc_cluster32_t	cn;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	pcfid = (struct pc_fid *)fidp;
19517c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	error = pc_lockfs(fsp, 0, 0);
19547c478bd9Sstevel@tonic-gate 	if (error) {
19557c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19567c478bd9Sstevel@tonic-gate 		return (error);
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	if (pcfid->pcfid_block == 0) {
19607c478bd9Sstevel@tonic-gate 		pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
19617c478bd9Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
19627c478bd9Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
19637c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19647c478bd9Sstevel@tonic-gate 		return (0);
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 	eblkno = pcfid->pcfid_block;
19677c478bd9Sstevel@tonic-gate 	eoffset = pcfid->pcfid_offset;
19687c478bd9Sstevel@tonic-gate 	if ((pc_dbtocl(fsp,
19697c478bd9Sstevel@tonic-gate 	    eblkno - fsp->pcfs_dosstart) >= fsp->pcfs_ncluster) ||
19707c478bd9Sstevel@tonic-gate 	    (eoffset > fsp->pcfs_clsize)) {
19717c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19727c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19737c478bd9Sstevel@tonic-gate 		return (EINVAL);
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	if (eblkno >= fsp->pcfs_datastart || (eblkno-fsp->pcfs_rdirstart)
19777c478bd9Sstevel@tonic-gate 	    < (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) {
19787c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno, fsp->pcfs_clsize);
19797c478bd9Sstevel@tonic-gate 	} else {
19807c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno,
19817c478bd9Sstevel@tonic-gate 		    (int)(fsp->pcfs_datastart - eblkno) * fsp->pcfs_secsize);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 	if (bp->b_flags & (B_ERROR | B_STALE)) {
19847c478bd9Sstevel@tonic-gate 		error = geterror(bp);
19857c478bd9Sstevel@tonic-gate 		brelse(bp);
19867c478bd9Sstevel@tonic-gate 		if (error)
19877c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
19887c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19897c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19907c478bd9Sstevel@tonic-gate 		return (error);
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 	ep = (struct pcdir *)(bp->b_un.b_addr + eoffset);
19937c478bd9Sstevel@tonic-gate 	/*
19947c478bd9Sstevel@tonic-gate 	 * Ok, if this is a valid file handle that we gave out,
19957c478bd9Sstevel@tonic-gate 	 * then simply ensuring that the creation time matches,
19967c478bd9Sstevel@tonic-gate 	 * the entry has not been deleted, and it has a valid first
19977c478bd9Sstevel@tonic-gate 	 * character should be enough.
19987c478bd9Sstevel@tonic-gate 	 *
19997c478bd9Sstevel@tonic-gate 	 * Unfortunately, verifying that the <blkno, offset> _still_
20007c478bd9Sstevel@tonic-gate 	 * refers to a directory entry is not easy, since we'd have
20017c478bd9Sstevel@tonic-gate 	 * to search _all_ directories starting from root to find it.
20027c478bd9Sstevel@tonic-gate 	 * That's a high price to pay just in case somebody is forging
20037c478bd9Sstevel@tonic-gate 	 * file handles. So instead we verify that as much of the
20047c478bd9Sstevel@tonic-gate 	 * entry is valid as we can:
20057c478bd9Sstevel@tonic-gate 	 *
20067c478bd9Sstevel@tonic-gate 	 * 1. The starting cluster is 0 (unallocated) or valid
20077c478bd9Sstevel@tonic-gate 	 * 2. It is not an LFN entry
20087c478bd9Sstevel@tonic-gate 	 * 3. It is not hidden (unless mounted as such)
20097c478bd9Sstevel@tonic-gate 	 * 4. It is not the label
20107c478bd9Sstevel@tonic-gate 	 */
20117c478bd9Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, ep);
20127c478bd9Sstevel@tonic-gate 	/*
20137c478bd9Sstevel@tonic-gate 	 * if the starting cluster is valid, but not valid according
20147c478bd9Sstevel@tonic-gate 	 * to pc_validcl(), force it to be to simplify the following if.
20157c478bd9Sstevel@tonic-gate 	 */
20167c478bd9Sstevel@tonic-gate 	if (cn == 0)
20177c478bd9Sstevel@tonic-gate 		cn = PCF_FIRSTCLUSTER;
20187c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
20197c478bd9Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER32)
20207c478bd9Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
20217c478bd9Sstevel@tonic-gate 	} else {
20227c478bd9Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER)
20237c478bd9Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 	if ((!pc_validcl(fsp, cn)) ||
20267c478bd9Sstevel@tonic-gate 	    (PCDL_IS_LFN(ep)) ||
20277c478bd9Sstevel@tonic-gate 	    (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) ||
20287c478bd9Sstevel@tonic-gate 	    ((ep->pcd_attr & PCA_LABEL) == PCA_LABEL)) {
20297c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
20307c478bd9Sstevel@tonic-gate 		brelse(bp);
20317c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
20327c478bd9Sstevel@tonic-gate 		return (EINVAL);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 	if ((ep->pcd_crtime.pct_time == pcfid->pcfid_ctime) &&
20357c478bd9Sstevel@tonic-gate 	    (ep->pcd_filename[0] != PCD_ERASED) &&
20367c478bd9Sstevel@tonic-gate 	    (pc_validchar(ep->pcd_filename[0]) ||
20377c478bd9Sstevel@tonic-gate 		(ep->pcd_filename[0] == '.' && ep->pcd_filename[1] == '.'))) {
20387c478bd9Sstevel@tonic-gate 		pcp = pc_getnode(fsp, eblkno, eoffset, ep);
20397c478bd9Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
20407c478bd9Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
20417c478bd9Sstevel@tonic-gate 	} else {
20427c478bd9Sstevel@tonic-gate 		*vpp = NULL;
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	bp->b_flags |= B_STALE | B_AGE;
20457c478bd9Sstevel@tonic-gate 	brelse(bp);
20467c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
20477c478bd9Sstevel@tonic-gate 	return (0);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate /*
205190c30842Sjmcp  * if device is a PCMCIA pseudo floppy, return 1
20527c478bd9Sstevel@tonic-gate  * otherwise, return 0
20537c478bd9Sstevel@tonic-gate  */
20547c478bd9Sstevel@tonic-gate static int
205590c30842Sjmcp pcfs_pseudo_floppy(dev_t rdev)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	int			error, err;
20587c478bd9Sstevel@tonic-gate 	struct dk_cinfo		info;
20597c478bd9Sstevel@tonic-gate 	ldi_handle_t		lh;
20607c478bd9Sstevel@tonic-gate 	ldi_ident_t		li;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	err = ldi_ident_from_mod(&modlinkage, &li);
20637c478bd9Sstevel@tonic-gate 	if (err) {
20647c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
206590c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_ident_from_mod err=%d\n", err);
20667c478bd9Sstevel@tonic-gate 		return (0);
20677c478bd9Sstevel@tonic-gate 	}
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	err = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD, CRED(), &lh, li);
20707c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
20717c478bd9Sstevel@tonic-gate 	if (err) {
20727c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
207390c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_open err=%d\n", err);
20747c478bd9Sstevel@tonic-gate 		return (0);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/* return value stored in err is purposfully ignored */
20787c478bd9Sstevel@tonic-gate 	error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
20797c478bd9Sstevel@tonic-gate 	    CRED(), &err);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	err = ldi_close(lh, FREAD, CRED());
20827c478bd9Sstevel@tonic-gate 	if (err != 0) {
20837c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
208490c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_close err=%d\n", err);
20857c478bd9Sstevel@tonic-gate 		return (0);
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	if ((error == 0) && (info.dki_ctype == DKC_PCMCIA_MEM) &&
20897c478bd9Sstevel@tonic-gate 		(info.dki_flags & DKI_PCMCIA_PFD))
20907c478bd9Sstevel@tonic-gate 		return (1);
20917c478bd9Sstevel@tonic-gate 	else
20927c478bd9Sstevel@tonic-gate 		return (0);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate /*
20967c478bd9Sstevel@tonic-gate  * Unfortunately, FAT32 fat's can be pretty big (On a 1 gig jaz drive, about
20977c478bd9Sstevel@tonic-gate  * a meg), so we can't bread() it all in at once. This routine reads a
20987c478bd9Sstevel@tonic-gate  * fat a chunk at a time.
20997c478bd9Sstevel@tonic-gate  */
21007c478bd9Sstevel@tonic-gate static int
21017c478bd9Sstevel@tonic-gate pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start, size_t fatsize)
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate 	struct buf *bp;
21047c478bd9Sstevel@tonic-gate 	size_t off;
21057c478bd9Sstevel@tonic-gate 	size_t readsize;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	readsize = fsp->pcfs_clsize;
21087c478bd9Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += readsize, fatp += readsize) {
21097c478bd9Sstevel@tonic-gate 		if (readsize > (fatsize - off))
21107c478bd9Sstevel@tonic-gate 			readsize = fatsize - off;
21117c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
21127c478bd9Sstevel@tonic-gate 		    pc_dbdaddr(fsp, start +
21137c478bd9Sstevel@tonic-gate 			pc_cltodb(fsp, pc_lblkno(fsp, off))),
21147c478bd9Sstevel@tonic-gate 		    readsize);
21157c478bd9Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
21167c478bd9Sstevel@tonic-gate 			brelse(bp);
21177c478bd9Sstevel@tonic-gate 			return (EIO);
21187c478bd9Sstevel@tonic-gate 		}
21197c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
21207c478bd9Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, fatp, readsize);
21217c478bd9Sstevel@tonic-gate 		brelse(bp);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 	return (0);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate /*
21277c478bd9Sstevel@tonic-gate  * We write the FAT out a _lot_, in order to make sure that it
21287c478bd9Sstevel@tonic-gate  * is up-to-date. But on a FAT32 system (large drive, small clusters)
21297c478bd9Sstevel@tonic-gate  * the FAT might be a couple of megabytes, and writing it all out just
21307c478bd9Sstevel@tonic-gate  * because we created or deleted a small file is painful (especially
21317c478bd9Sstevel@tonic-gate  * since we do it for each alternate FAT too). So instead, for FAT16 and
21327c478bd9Sstevel@tonic-gate  * FAT32 we only write out the bit that has changed. We don't clear
21337c478bd9Sstevel@tonic-gate  * the 'updated' fields here because the caller might be writing out
21347c478bd9Sstevel@tonic-gate  * several FATs, so the caller must use pc_clear_fatchanges() after
21357c478bd9Sstevel@tonic-gate  * all FATs have been updated.
21367c478bd9Sstevel@tonic-gate  */
21377c478bd9Sstevel@tonic-gate static int
21387c478bd9Sstevel@tonic-gate pc_writefat(struct pcfs *fsp, daddr_t start)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	struct buf *bp;
21417c478bd9Sstevel@tonic-gate 	size_t off;
21427c478bd9Sstevel@tonic-gate 	size_t writesize;
21437c478bd9Sstevel@tonic-gate 	int	error;
21447c478bd9Sstevel@tonic-gate 	uchar_t *fatp = fsp->pcfs_fatp;
21457c478bd9Sstevel@tonic-gate 	size_t fatsize = fsp->pcfs_fatsize;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	writesize = fsp->pcfs_clsize;
21487c478bd9Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += writesize, fatp += writesize) {
21497c478bd9Sstevel@tonic-gate 		if (writesize > (fatsize - off))
21507c478bd9Sstevel@tonic-gate 			writesize = fatsize - off;
21517c478bd9Sstevel@tonic-gate 		if (!pc_fat_is_changed(fsp, pc_lblkno(fsp, off))) {
21527c478bd9Sstevel@tonic-gate 			continue;
21537c478bd9Sstevel@tonic-gate 		}
21547c478bd9Sstevel@tonic-gate 		bp = ngeteblk(writesize);
21557c478bd9Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
21567c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
21577c478bd9Sstevel@tonic-gate 		bp->b_blkno = pc_dbdaddr(fsp, start +
21587c478bd9Sstevel@tonic-gate 		    pc_cltodb(fsp, pc_lblkno(fsp, off)));
21597c478bd9Sstevel@tonic-gate 		bcopy(fatp, bp->b_un.b_addr, writesize);
21607c478bd9Sstevel@tonic-gate 		bwrite2(bp);
21617c478bd9Sstevel@tonic-gate 		error = geterror(bp);
21627c478bd9Sstevel@tonic-gate 		brelse(bp);
21637c478bd9Sstevel@tonic-gate 		if (error) {
21647c478bd9Sstevel@tonic-gate 			return (error);
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 	return (0);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate /*
21717c478bd9Sstevel@tonic-gate  * Mark the FAT cluster that 'cn' is stored in as modified.
21727c478bd9Sstevel@tonic-gate  */
21737c478bd9Sstevel@tonic-gate void
21747c478bd9Sstevel@tonic-gate pc_mark_fat_updated(struct pcfs *fsp, pc_cluster32_t cn)
21757c478bd9Sstevel@tonic-gate {
21767c478bd9Sstevel@tonic-gate 	pc_cluster32_t	bn;
21777c478bd9Sstevel@tonic-gate 	size_t		size;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	/* which fat block is the cluster number stored in? */
21807c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
21817c478bd9Sstevel@tonic-gate 		size = sizeof (pc_cluster32_t);
21827c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21837c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21847c478bd9Sstevel@tonic-gate 	} else if (IS_FAT16(fsp)) {
21857c478bd9Sstevel@tonic-gate 		size = sizeof (pc_cluster16_t);
21867c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21877c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21887c478bd9Sstevel@tonic-gate 	} else {
21897c478bd9Sstevel@tonic-gate 		offset_t off;
21907c478bd9Sstevel@tonic-gate 		pc_cluster32_t nbn;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 		ASSERT(IS_FAT12(fsp));
21937c478bd9Sstevel@tonic-gate 		off = cn + (cn >> 1);
21947c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, off);
21957c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21967c478bd9Sstevel@tonic-gate 		/* does this field wrap into the next fat cluster? */
21977c478bd9Sstevel@tonic-gate 		nbn = pc_lblkno(fsp, off + 1);
21987c478bd9Sstevel@tonic-gate 		if (nbn != bn) {
21997c478bd9Sstevel@tonic-gate 			fsp->pcfs_fat_changemap[nbn] = 1;
22007c478bd9Sstevel@tonic-gate 		}
22017c478bd9Sstevel@tonic-gate 	}
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate /*
22057c478bd9Sstevel@tonic-gate  * return whether the FAT cluster 'bn' is updated and needs to
22067c478bd9Sstevel@tonic-gate  * be written out.
22077c478bd9Sstevel@tonic-gate  */
22087c478bd9Sstevel@tonic-gate int
22097c478bd9Sstevel@tonic-gate pc_fat_is_changed(struct pcfs *fsp, pc_cluster32_t bn)
22107c478bd9Sstevel@tonic-gate {
22117c478bd9Sstevel@tonic-gate 	return (fsp->pcfs_fat_changemap[bn] == 1);
22127c478bd9Sstevel@tonic-gate }
2213264a6e74Sfrankho 
2214264a6e74Sfrankho /*
2215264a6e74Sfrankho  * Implementation of VFS_FREEVFS() to support forced umounts.
2216264a6e74Sfrankho  * This is called by the vfs framework after umount, to trigger
2217264a6e74Sfrankho  * the release of any resources still associated with the given
2218264a6e74Sfrankho  * vfs_t once the need to keep them has gone away.
2219264a6e74Sfrankho  */
2220264a6e74Sfrankho void
2221264a6e74Sfrankho pcfs_freevfs(vfs_t *vfsp)
2222264a6e74Sfrankho {
2223264a6e74Sfrankho 	struct pcfs *fsp = VFSTOPCFS(vfsp);
2224264a6e74Sfrankho 
2225264a6e74Sfrankho 	mutex_enter(&pcfslock);
2226264a6e74Sfrankho 	if (fsp->pcfs_fatp != (uchar_t *)0)
2227264a6e74Sfrankho 		pc_invalfat(fsp);
2228264a6e74Sfrankho 	mutex_exit(&pcfslock);
2229264a6e74Sfrankho 
2230264a6e74Sfrankho 	VN_RELE(fsp->pcfs_devvp);
2231264a6e74Sfrankho 	mutex_destroy(&fsp->pcfs_lock);
2232264a6e74Sfrankho 	kmem_free(fsp, (uint_t)sizeof (struct pcfs));
2233264a6e74Sfrankho 
2234264a6e74Sfrankho 	/*
2235264a6e74Sfrankho 	 * Allow _fini() to succeed now, if so desired.
2236264a6e74Sfrankho 	 */
2237264a6e74Sfrankho 	atomic_dec_32(&pcfs_mountcount);
2238264a6e74Sfrankho }
2239