xref: /illumos-gate/usr/src/uts/common/fs/pcfs/pc_vfsops.c (revision 5a59a8b3)
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
5*5a59a8b3Srsb  * Common Development and Distribution License (the "License").
6*5a59a8b3Srsb  * 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*5a59a8b3Srsb  * Copyright 2006 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>
377c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
387c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/uio.h>
417c478bd9Sstevel@tonic-gate #include <sys/conf.h>
427c478bd9Sstevel@tonic-gate #undef NFSCLIENT
437c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
447c478bd9Sstevel@tonic-gate #include <sys/mount.h>
457c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
497c478bd9Sstevel@tonic-gate #include <sys/conf.h>
507c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
517c478bd9Sstevel@tonic-gate #include <sys/swap.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/sunldi.h>
547c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/pc_label.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
587c478bd9Sstevel@tonic-gate #include <sys/fs/pc_node.h>
597c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
607c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
617c478bd9Sstevel@tonic-gate #include <sys/vol.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>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * The majority of PC media use a 512 sector size, but
697c478bd9Sstevel@tonic-gate  * occasionally you will run across a 1k sector size.
707c478bd9Sstevel@tonic-gate  * For media with a 1k sector size, fd_strategy() requires
717c478bd9Sstevel@tonic-gate  * the I/O size to be a 1k multiple; so when the sector size
727c478bd9Sstevel@tonic-gate  * is not yet known, always read 1k.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #define	PC_SAFESECSIZE	(PC_SECSIZE * 2)
757c478bd9Sstevel@tonic-gate 
7690c30842Sjmcp static int pcfs_pseudo_floppy(dev_t);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int pcfsinit(int, char *);
797c478bd9Sstevel@tonic-gate static int pcfs_mount(struct vfs *, struct vnode *, struct mounta *,
807c478bd9Sstevel@tonic-gate 	struct cred *);
817c478bd9Sstevel@tonic-gate static int pcfs_unmount(struct vfs *, int, struct cred *);
827c478bd9Sstevel@tonic-gate static int pcfs_root(struct vfs *, struct vnode **);
837c478bd9Sstevel@tonic-gate static int pcfs_statvfs(struct vfs *, struct statvfs64 *);
847c478bd9Sstevel@tonic-gate static int pc_syncfsnodes(struct pcfs *);
857c478bd9Sstevel@tonic-gate static int pcfs_sync(struct vfs *, short, struct cred *);
867c478bd9Sstevel@tonic-gate static int pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int pc_getfattype(struct vnode *, int, daddr_t *, int *);
897c478bd9Sstevel@tonic-gate static int pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start,
907c478bd9Sstevel@tonic-gate     size_t fatsize);
917c478bd9Sstevel@tonic-gate static int pc_writefat(struct pcfs *fsp, daddr_t start);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * pcfs mount options table
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static char *nohidden_cancel[] = {MNTOPT_PCFS_HIDDEN, NULL};
987c478bd9Sstevel@tonic-gate static char *hidden_cancel[] = {MNTOPT_PCFS_NOHIDDEN, NULL};
997c478bd9Sstevel@tonic-gate static char *nofoldcase_cancel[] = {MNTOPT_PCFS_FOLDCASE, NULL};
1007c478bd9Sstevel@tonic-gate static char *foldcase_cancel[] = {MNTOPT_PCFS_NOFOLDCASE, NULL};
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static mntopt_t mntopts[] = {
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
1057c478bd9Sstevel@tonic-gate  *		opt data
1067c478bd9Sstevel@tonic-gate  */
10743893bfeSmm 	{ MNTOPT_PCFS_NOHIDDEN,	nohidden_cancel, NULL,		0,
1087c478bd9Sstevel@tonic-gate 		NULL },
10943893bfeSmm 	{ MNTOPT_PCFS_HIDDEN,	hidden_cancel, NULL,		MO_DEFAULT,
1107c478bd9Sstevel@tonic-gate 		NULL },
1117c478bd9Sstevel@tonic-gate 	{ MNTOPT_PCFS_NOFOLDCASE, nofoldcase_cancel, NULL,	MO_DEFAULT,
1127c478bd9Sstevel@tonic-gate 		NULL },
1137c478bd9Sstevel@tonic-gate 	{ MNTOPT_PCFS_FOLDCASE,	foldcase_cancel, NULL,		0,
1147c478bd9Sstevel@tonic-gate 		NULL }
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static mntopts_t pcfs_mntopts = {
1187c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1197c478bd9Sstevel@tonic-gate 	mntopts
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int pcfsdebuglevel = 0;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * pcfslock:	protects the list of mounted pc filesystems "pc_mounttab.
1267c478bd9Sstevel@tonic-gate  * pcfs_lock:	(inside per filesystem structure "pcfs")
1277c478bd9Sstevel@tonic-gate  *		per filesystem lock. Most of the vfsops and vnodeops are
1287c478bd9Sstevel@tonic-gate  *		protected by this lock.
1297c478bd9Sstevel@tonic-gate  * pcnodes_lock: protects the pcnode hash table "pcdhead", "pcfhead".
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * Lock hierarchy: pcfslock > pcfs_lock > pcnodes_lock
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate kmutex_t	pcfslock;
1347c478bd9Sstevel@tonic-gate krwlock_t pcnodes_lock; /* protect the pcnode hash table "pcdhead", "pcfhead" */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int pcfstype;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
1397c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1407c478bd9Sstevel@tonic-gate 	"pcfs",
1417c478bd9Sstevel@tonic-gate 	pcfsinit,
142*5a59a8b3Srsb 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS,
1437c478bd9Sstevel@tonic-gate 	&pcfs_mntopts
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
1497c478bd9Sstevel@tonic-gate 	&mod_fsops,
15090c30842Sjmcp 	"PC filesystem v%I%",
1517c478bd9Sstevel@tonic-gate 	&vfw
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1557c478bd9Sstevel@tonic-gate 	MODREV_1,
1567c478bd9Sstevel@tonic-gate 	&modlfs,
1577c478bd9Sstevel@tonic-gate 	NULL
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate int
1617c478bd9Sstevel@tonic-gate _init(void)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int	error;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #if !defined(lint)
1667c478bd9Sstevel@tonic-gate 	/* make sure the on-disk structures are sane */
1677c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir) == 32);
1687c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir_lfn) == 32);
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate 	mutex_init(&pcfslock, NULL, MUTEX_DEFAULT, NULL);
1717c478bd9Sstevel@tonic-gate 	rw_init(&pcnodes_lock, NULL, RW_DEFAULT, NULL);
1727c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1737c478bd9Sstevel@tonic-gate 	if (error) {
1747c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcfslock);
1757c478bd9Sstevel@tonic-gate 		rw_destroy(&pcnodes_lock);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (error);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate int
1817c478bd9Sstevel@tonic-gate _fini(void)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int	error;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1867c478bd9Sstevel@tonic-gate 	if (error)
1877c478bd9Sstevel@tonic-gate 		return (error);
1887c478bd9Sstevel@tonic-gate 	mutex_destroy(&pcfslock);
1897c478bd9Sstevel@tonic-gate 	rw_destroy(&pcnodes_lock);
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * Tear down the operations vectors
1927c478bd9Sstevel@tonic-gate 	 */
1937c478bd9Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(pcfstype);
1947c478bd9Sstevel@tonic-gate 	vn_freevnodeops(pcfs_fvnodeops);
1957c478bd9Sstevel@tonic-gate 	vn_freevnodeops(pcfs_dvnodeops);
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate int
2007c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
2067c478bd9Sstevel@tonic-gate static int
2077c478bd9Sstevel@tonic-gate pcfsinit(int fstype, char *name)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t pcfs_vfsops_template[] = {
2107c478bd9Sstevel@tonic-gate 		VFSNAME_MOUNT, pcfs_mount,
2117c478bd9Sstevel@tonic-gate 		VFSNAME_UNMOUNT, pcfs_unmount,
2127c478bd9Sstevel@tonic-gate 		VFSNAME_ROOT, pcfs_root,
2137c478bd9Sstevel@tonic-gate 		VFSNAME_STATVFS, pcfs_statvfs,
2147c478bd9Sstevel@tonic-gate 		VFSNAME_SYNC, (fs_generic_func_p) pcfs_sync,
2157c478bd9Sstevel@tonic-gate 		VFSNAME_VGET, pcfs_vget,
2167c478bd9Sstevel@tonic-gate 		NULL, NULL
2177c478bd9Sstevel@tonic-gate 	};
2187c478bd9Sstevel@tonic-gate 	int error;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, pcfs_vfsops_template, NULL);
2217c478bd9Sstevel@tonic-gate 	if (error != 0) {
2227c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad vfs ops template");
2237c478bd9Sstevel@tonic-gate 		return (error);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	error = vn_make_ops("pcfs", pcfs_fvnodeops_template, &pcfs_fvnodeops);
2277c478bd9Sstevel@tonic-gate 	if (error != 0) {
2287c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2297c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad file vnode ops template");
2307c478bd9Sstevel@tonic-gate 		return (error);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	error = vn_make_ops("pcfsd", pcfs_dvnodeops_template, &pcfs_dvnodeops);
2347c478bd9Sstevel@tonic-gate 	if (error != 0) {
2357c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2367c478bd9Sstevel@tonic-gate 		vn_freevnodeops(pcfs_fvnodeops);
2377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad dir vnode ops template");
2387c478bd9Sstevel@tonic-gate 		return (error);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	pcfstype = fstype;
2427c478bd9Sstevel@tonic-gate 	(void) pc_init();
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static struct pcfs *pc_mounttab = NULL;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate extern struct pcfs_args pc_tz;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  *  Define some special logical drives we use internal to this file.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate #define	BOOT_PARTITION_DRIVE	99
2547c478bd9Sstevel@tonic-gate #define	PRIMARY_DOS_DRIVE	1
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * pc_mount system call
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate pcfs_mount(
2617c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
2627c478bd9Sstevel@tonic-gate 	struct vnode *mvp,
2637c478bd9Sstevel@tonic-gate 	struct mounta *uap,
2647c478bd9Sstevel@tonic-gate 	struct cred *cr)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
2677c478bd9Sstevel@tonic-gate 	struct vnode *bvp;
2687c478bd9Sstevel@tonic-gate 	struct vnode *devvp;
2697c478bd9Sstevel@tonic-gate 	struct pathname special;
2707c478bd9Sstevel@tonic-gate 	daddr_t dosstart;
2717c478bd9Sstevel@tonic-gate 	dev_t pseudodev;
2727c478bd9Sstevel@tonic-gate 	dev_t xdev;
2737c478bd9Sstevel@tonic-gate 	char *spnp;
2747c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
2757c478bd9Sstevel@tonic-gate 	int datalen = uap->datalen;
2767c478bd9Sstevel@tonic-gate 	int dos_ldrive = 0;
2777c478bd9Sstevel@tonic-gate 	int error;
2787c478bd9Sstevel@tonic-gate 	int fattype;
2797c478bd9Sstevel@tonic-gate 	int spnlen;
2807c478bd9Sstevel@tonic-gate 	int wantbootpart = 0;
2817c478bd9Sstevel@tonic-gate 	struct vioc_info info;
2827c478bd9Sstevel@tonic-gate 	int rval;		/* set but not used */
2837c478bd9Sstevel@tonic-gate 	minor_t	minor;
2847c478bd9Sstevel@tonic-gate 	int oflag, aflag;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
2877c478bd9Sstevel@tonic-gate 		return (error);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_mount\n");
2907c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
2917c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2947c478bd9Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2957c478bd9Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
2967c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2977c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2987c478bd9Sstevel@tonic-gate 		return (EBUSY);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * The caller is responsible for making sure to always
3047c478bd9Sstevel@tonic-gate 	 * pass in sizeof(struct pcfs_args) (or the old one).
3057c478bd9Sstevel@tonic-gate 	 * Doing this is the only way to know an EINVAL return
3067c478bd9Sstevel@tonic-gate 	 * from mount(2) is due to the "not a DOS filesystem"
3077c478bd9Sstevel@tonic-gate 	 * EINVAL that pc_verify/pc_getfattype could return.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	if ((datalen != sizeof (struct pcfs_args)) &&
3107c478bd9Sstevel@tonic-gate 	    (datalen != sizeof (struct old_pcfs_args))) {
3117c478bd9Sstevel@tonic-gate 		return (EINVAL);
3127c478bd9Sstevel@tonic-gate 	} else {
3137c478bd9Sstevel@tonic-gate 		struct pcfs_args tmp_tz;
3147c478bd9Sstevel@tonic-gate 		int hidden = 0;
3157c478bd9Sstevel@tonic-gate 		int foldcase = 0;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		tmp_tz.flags = 0;
3187c478bd9Sstevel@tonic-gate 		if (copyin(data, &tmp_tz, datalen)) {
3197c478bd9Sstevel@tonic-gate 			return (EFAULT);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		if (datalen == sizeof (struct pcfs_args)) {
3227c478bd9Sstevel@tonic-gate 			hidden = tmp_tz.flags & PCFS_MNT_HIDDEN;
3237c478bd9Sstevel@tonic-gate 			foldcase = tmp_tz.flags & PCFS_MNT_FOLDCASE;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (hidden)
3277c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_HIDDEN,	NULL, 0);
3287c478bd9Sstevel@tonic-gate 		if (foldcase)
3297c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_FOLDCASE, NULL, 0);
3307c478bd9Sstevel@tonic-gate 		/*
3317c478bd9Sstevel@tonic-gate 		 * more than one pc filesystem can be mounted on x86
3327c478bd9Sstevel@tonic-gate 		 * so the pc_tz structure is now a critical region
3337c478bd9Sstevel@tonic-gate 		 */
3347c478bd9Sstevel@tonic-gate 		mutex_enter(&pcfslock);
3357c478bd9Sstevel@tonic-gate 		if (pc_mounttab == NULL)
3367c478bd9Sstevel@tonic-gate 			bcopy(&tmp_tz, &pc_tz, sizeof (struct pcfs_args));
3377c478bd9Sstevel@tonic-gate 		mutex_exit(&pcfslock);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	if (error = pn_get(uap->spec, UIO_USERSPACE, &special)) {
3437c478bd9Sstevel@tonic-gate 		return (error);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	if (error =
3467c478bd9Sstevel@tonic-gate 	    lookupname(special.pn_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp)) {
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * look for suffix to special
3497c478bd9Sstevel@tonic-gate 		 * which indicates a request to mount the solaris boot
3507c478bd9Sstevel@tonic-gate 		 * partition, or a DOS logical drive on the hard disk
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		spnlen = special.pn_pathlen;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		if (spnlen > 5) {
3557c478bd9Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 5;
3567c478bd9Sstevel@tonic-gate 			if (*spnp++ == ':' && *spnp++ == 'b' &&
3577c478bd9Sstevel@tonic-gate 			    *spnp++ == 'o' && *spnp++ == 'o' &&
3587c478bd9Sstevel@tonic-gate 			    *spnp++ == 't') {
3597c478bd9Sstevel@tonic-gate 				/*
3607c478bd9Sstevel@tonic-gate 				 * Looks as if they want to mount
3617c478bd9Sstevel@tonic-gate 				 * the Solaris boot partition
3627c478bd9Sstevel@tonic-gate 				 */
3637c478bd9Sstevel@tonic-gate 				wantbootpart = 1;
3647c478bd9Sstevel@tonic-gate 				dos_ldrive = BOOT_PARTITION_DRIVE;
3657c478bd9Sstevel@tonic-gate 				spnp = special.pn_path + spnlen - 5;
3667c478bd9Sstevel@tonic-gate 				*spnp = '\0';
3677c478bd9Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3687c478bd9Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (!wantbootpart) {
3737c478bd9Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 1;
3747c478bd9Sstevel@tonic-gate 			if (spnlen > 2 && *spnp >= 'c' && *spnp <= 'z') {
3757c478bd9Sstevel@tonic-gate 				spnlen--;
3767c478bd9Sstevel@tonic-gate 				dos_ldrive = *spnp-- - 'c' + 1;
3777c478bd9Sstevel@tonic-gate 			} else if (spnlen > 2 && *spnp >= '0' && *spnp <= '9') {
3787c478bd9Sstevel@tonic-gate 				spnlen--;
3797c478bd9Sstevel@tonic-gate 				dos_ldrive = *spnp-- - '0';
3807c478bd9Sstevel@tonic-gate 				if (spnlen > 2 && *spnp >= '0' &&
3817c478bd9Sstevel@tonic-gate 				    *spnp <= '9') {
3827c478bd9Sstevel@tonic-gate 					spnlen--;
3837c478bd9Sstevel@tonic-gate 					dos_ldrive += 10 * (*spnp-- - '0');
3847c478bd9Sstevel@tonic-gate 				}
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 			if (spnlen > 1 && dos_ldrive && dos_ldrive <= 24 &&
3877c478bd9Sstevel@tonic-gate 			    *spnp == ':') {
3887c478bd9Sstevel@tonic-gate 				/*
3897c478bd9Sstevel@tonic-gate 				 * remove suffix so that we have a real
3907c478bd9Sstevel@tonic-gate 				 * device name
3917c478bd9Sstevel@tonic-gate 				 */
3927c478bd9Sstevel@tonic-gate 				*spnp = '\0';
3937c478bd9Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3947c478bd9Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 		if (error) {
3987c478bd9Sstevel@tonic-gate 			pn_free(&special);
3997c478bd9Sstevel@tonic-gate 			return (error);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	pn_free(&special);
4037c478bd9Sstevel@tonic-gate 	if (bvp->v_type != VBLK) {
4047c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
4057c478bd9Sstevel@tonic-gate 		return (ENOTBLK);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	xdev = bvp->v_rdev;
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Verify caller's permission to open the device special file.
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
4127c478bd9Sstevel@tonic-gate 	    ((uap->flags & MS_RDONLY) != 0)) {
4137c478bd9Sstevel@tonic-gate 		oflag = FREAD;
4147c478bd9Sstevel@tonic-gate 		aflag = VREAD;
4157c478bd9Sstevel@tonic-gate 	} else {
4167c478bd9Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
4177c478bd9Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	if ((error = VOP_ACCESS(bvp, aflag, 0, cr)) != 0 ||
4207c478bd9Sstevel@tonic-gate 	    (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
4217c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
4227c478bd9Sstevel@tonic-gate 		return (error);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	VN_RELE(bvp);
4267c478bd9Sstevel@tonic-gate 	if (getmajor(xdev) >= devcnt) {
4277c478bd9Sstevel@tonic-gate 		return (ENXIO);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Ensure that this device (or logical drive) isn't already mounted,
4317c478bd9Sstevel@tonic-gate 	 * unless this is a REMOUNT request
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	if (dos_ldrive) {
4347c478bd9Sstevel@tonic-gate 		mutex_enter(&pcfslock);
4357c478bd9Sstevel@tonic-gate 		for (fsp = pc_mounttab; fsp; fsp = fsp->pcfs_nxt)
4367c478bd9Sstevel@tonic-gate 			if (fsp->pcfs_xdev == xdev &&
4377c478bd9Sstevel@tonic-gate 			    fsp->pcfs_ldrv == dos_ldrive) {
4387c478bd9Sstevel@tonic-gate 				mutex_exit(&pcfslock);
4397c478bd9Sstevel@tonic-gate 				if (uap->flags & MS_REMOUNT) {
4407c478bd9Sstevel@tonic-gate 					return (0);
4417c478bd9Sstevel@tonic-gate 				} else {
4427c478bd9Sstevel@tonic-gate 					return (EBUSY);
4437c478bd9Sstevel@tonic-gate 				}
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 		/*
4467c478bd9Sstevel@tonic-gate 		 * Assign a unique device number for the vfs
4477c478bd9Sstevel@tonic-gate 		 * The old way (getudev() + a constantly incrementing
4487c478bd9Sstevel@tonic-gate 		 * major number) was wrong because it changes vfs_dev
4497c478bd9Sstevel@tonic-gate 		 * across mounts and reboots, which breaks nfs file handles.
4507c478bd9Sstevel@tonic-gate 		 * UFS just uses the real dev_t. We can't do that because
4517c478bd9Sstevel@tonic-gate 		 * of the way pcfs opens fdisk partitons (the :c and :d
4527c478bd9Sstevel@tonic-gate 		 * partitions are on the same dev_t). Though that _might_
4537c478bd9Sstevel@tonic-gate 		 * actually be ok, since the file handle contains an
4547c478bd9Sstevel@tonic-gate 		 * absolute block number, it's probably better to make them
4557c478bd9Sstevel@tonic-gate 		 * different. So I think we should retain the original
4567c478bd9Sstevel@tonic-gate 		 * dev_t, but come up with a different minor number based
4577c478bd9Sstevel@tonic-gate 		 * on the logical drive that will _always_ come up the same.
4587c478bd9Sstevel@tonic-gate 		 * For now, we steal the upper 6 bits.
4597c478bd9Sstevel@tonic-gate 		 */
4607c478bd9Sstevel@tonic-gate #ifdef notdef
4617c478bd9Sstevel@tonic-gate 		/* what should we do here? */
4627c478bd9Sstevel@tonic-gate 		if (((getminor(xdev) >> 12) & 0x3F) != 0)
4637c478bd9Sstevel@tonic-gate 			printf("whoops - upper bits used!\n");
4647c478bd9Sstevel@tonic-gate #endif
4657c478bd9Sstevel@tonic-gate 		minor = ((dos_ldrive << 12) | getminor(xdev)) & MAXMIN32;
4667c478bd9Sstevel@tonic-gate 		pseudodev = makedevice(getmajor(xdev), minor);
4677c478bd9Sstevel@tonic-gate 		if (vfs_devmounting(pseudodev, vfsp)) {
4687c478bd9Sstevel@tonic-gate 			mutex_exit(&pcfslock);
4697c478bd9Sstevel@tonic-gate 			return (EBUSY);
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 		if (vfs_devismounted(pseudodev)) {
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 		mutex_exit(&pcfslock);
4807c478bd9Sstevel@tonic-gate 	} else {
4817c478bd9Sstevel@tonic-gate 		if (vfs_devmounting(xdev, vfsp)) {
4827c478bd9Sstevel@tonic-gate 			return (EBUSY);
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		if (vfs_devismounted(xdev))
4857c478bd9Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
4867c478bd9Sstevel@tonic-gate 				return (0);
4877c478bd9Sstevel@tonic-gate 			} else {
4887c478bd9Sstevel@tonic-gate 				return (EBUSY);
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		pseudodev = xdev;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
4947c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4957c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * Mount the filesystem
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	devvp = makespecvp(xdev, VBLK);
5027c478bd9Sstevel@tonic-gate 	if (IS_SWAPVP(devvp)) {
5037c478bd9Sstevel@tonic-gate 		VN_RELE(devvp);
5047c478bd9Sstevel@tonic-gate 		return (EBUSY);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * special handling for PCMCIA memory card
50990c30842Sjmcp 	 * with pseudo floppies organization
5107c478bd9Sstevel@tonic-gate 	 */
51190c30842Sjmcp 	if (dos_ldrive == 0 && pcfs_pseudo_floppy(xdev)) {
5127c478bd9Sstevel@tonic-gate 		dosstart = (daddr_t)0;
5137c478bd9Sstevel@tonic-gate 		fattype = PCFS_PCMCIA_NO_CIS;
5147c478bd9Sstevel@tonic-gate 	} else {
5157c478bd9Sstevel@tonic-gate 		if (error = pc_getfattype(devvp, dos_ldrive, &dosstart,
5167c478bd9Sstevel@tonic-gate 		    &fattype)) {
5177c478bd9Sstevel@tonic-gate 			VN_RELE(devvp);
5187c478bd9Sstevel@tonic-gate 			return (error);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0, B_INVAL, cr);
5237c478bd9Sstevel@tonic-gate 	fsp = kmem_zalloc((uint_t)sizeof (struct pcfs), KM_SLEEP);
5247c478bd9Sstevel@tonic-gate 	fsp->pcfs_vfs = vfsp;
5257c478bd9Sstevel@tonic-gate 	fsp->pcfs_flags = fattype;
5267c478bd9Sstevel@tonic-gate 	fsp->pcfs_devvp = devvp;
5277c478bd9Sstevel@tonic-gate 	fsp->pcfs_xdev = xdev;
5287c478bd9Sstevel@tonic-gate 	fsp->pcfs_ldrv = dos_ldrive;
5297c478bd9Sstevel@tonic-gate 	fsp->pcfs_dosstart = dosstart;
5307c478bd9Sstevel@tonic-gate 	mutex_init(&fsp->pcfs_lock, NULL, MUTEX_DEFAULT, NULL);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/* set the "nocheck" flag if volmgt is managing this volume */
5337c478bd9Sstevel@tonic-gate 	info.vii_pathlen = 0;
5347c478bd9Sstevel@tonic-gate 	info.vii_devpath = 0;
5357c478bd9Sstevel@tonic-gate 	error = cdev_ioctl(fsp->pcfs_xdev, VOLIOCINFO, (intptr_t)&info,
5367c478bd9Sstevel@tonic-gate 	    FKIOCTL|FREAD, kcred, &rval);
5377c478bd9Sstevel@tonic-gate 	if (error == 0) {
5387c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_NOCHK;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_HIDDEN, NULL))
5427c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_HIDDEN;
5437c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_FOLDCASE, NULL))
5447c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_FOLDCASE;
5457c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = pseudodev;
5467c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = pcfstype;
5477c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, pseudodev, pcfstype);
5487c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)fsp;
5497c478bd9Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	error = pc_verify(fsp);
5527c478bd9Sstevel@tonic-gate 	if (error) {
5537c478bd9Sstevel@tonic-gate 		VN_RELE(devvp);
5547c478bd9Sstevel@tonic-gate 		mutex_destroy(&fsp->pcfs_lock);
5557c478bd9Sstevel@tonic-gate 		kmem_free(fsp, (uint_t)sizeof (struct pcfs));
5567c478bd9Sstevel@tonic-gate 		return (error);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->pcfs_clsize;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	mutex_enter(&pcfslock);
5617c478bd9Sstevel@tonic-gate 	fsp->pcfs_nxt = pc_mounttab;
5627c478bd9Sstevel@tonic-gate 	pc_mounttab = fsp;
5637c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
5647c478bd9Sstevel@tonic-gate 	return (0);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * vfs operations
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /* ARGSUSED */
5727c478bd9Sstevel@tonic-gate static int
5737c478bd9Sstevel@tonic-gate pcfs_unmount(
5747c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
5757c478bd9Sstevel@tonic-gate 	int flag,
5767c478bd9Sstevel@tonic-gate 	struct cred *cr)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	struct pcfs *fsp, *fsp1;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
5817c478bd9Sstevel@tonic-gate 		return (EPERM);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
5857c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE)
5887c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_unmount\n");
5917c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * We don't have to lock fsp because the VVFSLOCK in vfs layer will
5947c478bd9Sstevel@tonic-gate 	 * prevent lookuppn from crossing the mount point.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_nrefs) {
5977c478bd9Sstevel@tonic-gate 		return (EBUSY);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * Allow an unmount (regardless of state) if the fs instance has
6027c478bd9Sstevel@tonic-gate 	 * been marked as beyond recovery.
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_IRRECOV) {
6057c478bd9Sstevel@tonic-gate 		mutex_enter(&pcfslock);
6067c478bd9Sstevel@tonic-gate 		rw_enter(&pcnodes_lock, RW_WRITER);
6077c478bd9Sstevel@tonic-gate 		pc_diskchanged(fsp);
6087c478bd9Sstevel@tonic-gate 		rw_exit(&pcnodes_lock);
6097c478bd9Sstevel@tonic-gate 		mutex_exit(&pcfslock);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/* now there should be no pcp node on pcfhead or pcdhead. */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	mutex_enter(&pcfslock);
6157c478bd9Sstevel@tonic-gate 	if (fsp == pc_mounttab) {
6167c478bd9Sstevel@tonic-gate 		pc_mounttab = fsp->pcfs_nxt;
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		for (fsp1 = pc_mounttab; fsp1 != NULL; fsp1 = fsp1->pcfs_nxt)
6197c478bd9Sstevel@tonic-gate 			if (fsp1->pcfs_nxt == fsp)
6207c478bd9Sstevel@tonic-gate 				fsp1->pcfs_nxt = fsp->pcfs_nxt;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_fatp != (uchar_t *)0) {
6247c478bd9Sstevel@tonic-gate 		pc_invalfat(fsp);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	VN_RELE(fsp->pcfs_devvp);
6297c478bd9Sstevel@tonic-gate 	mutex_destroy(&fsp->pcfs_lock);
6307c478bd9Sstevel@tonic-gate 	kmem_free(fsp, (uint_t)sizeof (struct pcfs));
6317c478bd9Sstevel@tonic-gate 	return (0);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate  * find root of pcfs
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate static int
6387c478bd9Sstevel@tonic-gate pcfs_root(
6397c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
6407c478bd9Sstevel@tonic-gate 	struct vnode **vpp)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
6437c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
6447c478bd9Sstevel@tonic-gate 	int error;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6477c478bd9Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
6487c478bd9Sstevel@tonic-gate 		return (error);
6497c478bd9Sstevel@tonic-gate 	pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
6507c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(9, "pcfs_root(0x%p) pcp= 0x%p\n",
6517c478bd9Sstevel@tonic-gate 	    (void *)vfsp, (void *)pcp);
6527c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
6537c478bd9Sstevel@tonic-gate 	*vpp = PCTOV(pcp);
6547c478bd9Sstevel@tonic-gate 	pcp->pc_flags |= PC_EXTERNAL;
6557c478bd9Sstevel@tonic-gate 	return (0);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate  * Get file system statistics.
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate static int
6627c478bd9Sstevel@tonic-gate pcfs_statvfs(
6637c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
6647c478bd9Sstevel@tonic-gate 	struct statvfs64 *sp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
6677c478bd9Sstevel@tonic-gate 	int error;
6687c478bd9Sstevel@tonic-gate 	dev32_t d32;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6717c478bd9Sstevel@tonic-gate 	error = pc_getfat(fsp);
6727c478bd9Sstevel@tonic-gate 	if (error)
6737c478bd9Sstevel@tonic-gate 		return (error);
6747c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (*sp));
6757c478bd9Sstevel@tonic-gate 	sp->f_bsize = sp->f_frsize = fsp->pcfs_clsize;
6767c478bd9Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->pcfs_ncluster;
6777c478bd9Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree = (fsblkcnt64_t)pc_freeclusters(fsp);
6787c478bd9Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)-1;
6797c478bd9Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)-1;
6807c478bd9Sstevel@tonic-gate 	sp->f_favail = (fsfilcnt64_t)-1;
6817c478bd9Sstevel@tonic-gate #ifdef notdef
6827c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, fsp->pcfs_devvp->v_rdev);
6837c478bd9Sstevel@tonic-gate #endif /* notdef */
6847c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
6857c478bd9Sstevel@tonic-gate 	sp->f_fsid = d32;
6867c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
6877c478bd9Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
6887c478bd9Sstevel@tonic-gate 	sp->f_namemax = PCFNAMESIZE;
6897c478bd9Sstevel@tonic-gate 	return (0);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate static int
6937c478bd9Sstevel@tonic-gate pc_syncfsnodes(struct pcfs *fsp)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	struct pchead *hp;
6967c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
6977c478bd9Sstevel@tonic-gate 	int error;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfsnodes\n");
7007c478bd9Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
7017c478bd9Sstevel@tonic-gate 		return (error);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (!(error = pc_syncfat(fsp))) {
7047c478bd9Sstevel@tonic-gate 		hp = pcfhead;
7057c478bd9Sstevel@tonic-gate 		while (hp < & pcfhead [ NPCHASH ]) {
7067c478bd9Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_READER);
7077c478bd9Sstevel@tonic-gate 			pcp = hp->pch_forw;
7087c478bd9Sstevel@tonic-gate 			while (pcp != (struct pcnode *)hp) {
7097c478bd9Sstevel@tonic-gate 				if (VFSTOPCFS(PCTOV(pcp) -> v_vfsp) == fsp)
7107c478bd9Sstevel@tonic-gate 					if (error = pc_nodesync(pcp))
7117c478bd9Sstevel@tonic-gate 						break;
7127c478bd9Sstevel@tonic-gate 				pcp = pcp -> pc_forw;
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7157c478bd9Sstevel@tonic-gate 			if (error)
7167c478bd9Sstevel@tonic-gate 				break;
7177c478bd9Sstevel@tonic-gate 			hp++;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
7217c478bd9Sstevel@tonic-gate 	return (error);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate  * Flush any pending I/O.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7287c478bd9Sstevel@tonic-gate static int
7297c478bd9Sstevel@tonic-gate pcfs_sync(
7307c478bd9Sstevel@tonic-gate 	struct vfs *vfsp,
7317c478bd9Sstevel@tonic-gate 	short flag,
7327c478bd9Sstevel@tonic-gate 	struct cred *cr)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
7357c478bd9Sstevel@tonic-gate 	int error = 0;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* this prevents the filesystem from being umounted. */
7387c478bd9Sstevel@tonic-gate 	mutex_enter(&pcfslock);
7397c478bd9Sstevel@tonic-gate 	if (vfsp != NULL) {
7407c478bd9Sstevel@tonic-gate 		fsp = VFSTOPCFS(vfsp);
7417c478bd9Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_IRRECOV)) {
7427c478bd9Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7437c478bd9Sstevel@tonic-gate 		} else {
7447c478bd9Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_WRITER);
7457c478bd9Sstevel@tonic-gate 			pc_diskchanged(fsp);
7467c478bd9Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7477c478bd9Sstevel@tonic-gate 			error = EIO;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 	} else {
7507c478bd9Sstevel@tonic-gate 		fsp = pc_mounttab;
7517c478bd9Sstevel@tonic-gate 		while (fsp != NULL) {
7527c478bd9Sstevel@tonic-gate 			if (fsp->pcfs_flags & PCFS_IRRECOV) {
7537c478bd9Sstevel@tonic-gate 				rw_enter(&pcnodes_lock, RW_WRITER);
7547c478bd9Sstevel@tonic-gate 				pc_diskchanged(fsp);
7557c478bd9Sstevel@tonic-gate 				rw_exit(&pcnodes_lock);
7567c478bd9Sstevel@tonic-gate 				error = EIO;
7577c478bd9Sstevel@tonic-gate 				break;
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7607c478bd9Sstevel@tonic-gate 			if (error) break;
7617c478bd9Sstevel@tonic-gate 			fsp = fsp->pcfs_nxt;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	mutex_exit(&pcfslock);
7657c478bd9Sstevel@tonic-gate 	return (error);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate int
7697c478bd9Sstevel@tonic-gate pc_lockfs(struct pcfs *fsp, int diskchanged, int releasing)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_IRRECOV) && !releasing)
7727c478bd9Sstevel@tonic-gate 		return (EIO);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) && (fsp->pcfs_owner == curthread)) {
7757c478bd9Sstevel@tonic-gate 		fsp->pcfs_count++;
7767c478bd9Sstevel@tonic-gate 	} else {
7777c478bd9Sstevel@tonic-gate 		mutex_enter(&fsp->pcfs_lock);
7787c478bd9Sstevel@tonic-gate 		if (fsp->pcfs_flags & PCFS_LOCKED)
7797c478bd9Sstevel@tonic-gate 			panic("pc_lockfs");
7807c478bd9Sstevel@tonic-gate 		/*
7817c478bd9Sstevel@tonic-gate 		 * We check the IRRECOV bit again just in case somebody
7827c478bd9Sstevel@tonic-gate 		 * snuck past the initial check but then got held up before
7837c478bd9Sstevel@tonic-gate 		 * they could grab the lock.  (And in the meantime someone
7847c478bd9Sstevel@tonic-gate 		 * had grabbed the lock and set the bit)
7857c478bd9Sstevel@tonic-gate 		 */
7860576819eSwyllys 		if (!diskchanged && !(fsp->pcfs_flags & PCFS_IRRECOV)) {
7870576819eSwyllys 			int err;
7880576819eSwyllys 			if ((err = pc_getfat(fsp)))
7890576819eSwyllys 				return (err);
7900576819eSwyllys 		}
7917c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_LOCKED;
7927c478bd9Sstevel@tonic-gate 		fsp->pcfs_owner = curthread;
7937c478bd9Sstevel@tonic-gate 		fsp->pcfs_count++;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	return (0);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate void
7997c478bd9Sstevel@tonic-gate pc_unlockfs(struct pcfs *fsp)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) == 0)
8037c478bd9Sstevel@tonic-gate 		panic("pc_unlockfs");
8047c478bd9Sstevel@tonic-gate 	if (--fsp->pcfs_count < 0)
8057c478bd9Sstevel@tonic-gate 		panic("pc_unlockfs: count");
8067c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_count == 0) {
8077c478bd9Sstevel@tonic-gate 		fsp->pcfs_flags &= ~PCFS_LOCKED;
8087c478bd9Sstevel@tonic-gate 		fsp->pcfs_owner = 0;
8097c478bd9Sstevel@tonic-gate 		mutex_exit(&fsp->pcfs_lock);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * isDosDrive()
8157c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8167c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes a DOS drive.  We
8177c478bd9Sstevel@tonic-gate  *	use systid values defined in sys/dktp/fdisk.h.
8187c478bd9Sstevel@tonic-gate  */
8197c478bd9Sstevel@tonic-gate static int
8207c478bd9Sstevel@tonic-gate isDosDrive(uchar_t checkMe)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
8237c478bd9Sstevel@tonic-gate 	    (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
8247c478bd9Sstevel@tonic-gate 	    (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
8257c478bd9Sstevel@tonic-gate 	    (checkMe == DIAGPART));
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate  * isDosExtended()
8307c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8317c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes an extended DOS
8327c478bd9Sstevel@tonic-gate  *	partition.
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate static int
8357c478bd9Sstevel@tonic-gate isDosExtended(uchar_t checkMe)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate  * isBootPart()
8427c478bd9Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8437c478bd9Sstevel@tonic-gate  *	and it decides if that's a systid that describes a Solaris boot
8447c478bd9Sstevel@tonic-gate  *	partition.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate static int
8477c478bd9Sstevel@tonic-gate isBootPart(uchar_t checkMe)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	return (checkMe == X86BOOT);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * noLogicalDrive()
8547c478bd9Sstevel@tonic-gate  *	Display error message about not being able to find a logical
8557c478bd9Sstevel@tonic-gate  *	drive.
8567c478bd9Sstevel@tonic-gate  */
8577c478bd9Sstevel@tonic-gate static void
8587c478bd9Sstevel@tonic-gate noLogicalDrive(int requested)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	if (requested == BOOT_PARTITION_DRIVE) {
8617c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no boot partition");
8627c478bd9Sstevel@tonic-gate 	} else {
8637c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no such logical drive");
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate  * findTheDrive()
8697c478bd9Sstevel@tonic-gate  *	Discover offset of the requested logical drive, and return
8707c478bd9Sstevel@tonic-gate  *	that offset (startSector), the systid of that drive (sysid),
8717c478bd9Sstevel@tonic-gate  *	and a buffer pointer (bp), with the buffer contents being
8727c478bd9Sstevel@tonic-gate  *	the first sector of the logical drive (i.e., the sector that
8737c478bd9Sstevel@tonic-gate  *	contains the BPB for that drive).
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate static int
8767c478bd9Sstevel@tonic-gate findTheDrive(dev_t dev, int askedFor, int *error, buf_t **bp,
8777c478bd9Sstevel@tonic-gate     daddr_t *startSector, uchar_t *sysid)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	struct ipart dosp[FD_NUMPART];	/* incore fdisk partition structure */
8807c478bd9Sstevel@tonic-gate 	struct mboot *dosp_ptr;		/* boot structure pointer */
8817c478bd9Sstevel@tonic-gate 	daddr_t lastseek = 0;		/* Disk block we sought previously */
8827c478bd9Sstevel@tonic-gate 	daddr_t diskblk = 0;		/* Disk block to get */
8837c478bd9Sstevel@tonic-gate 	daddr_t xstartsect;		/* base of Extended DOS partition */
8847c478bd9Sstevel@tonic-gate 	int logicalDriveCount = 0;	/* Count of logical drives seen */
8857c478bd9Sstevel@tonic-gate 	int extendedPart = -1;		/* index of extended dos partition */
8867c478bd9Sstevel@tonic-gate 	int primaryPart = -1;		/* index of primary dos partition */
8877c478bd9Sstevel@tonic-gate 	int bootPart = -1;		/* index of a Solaris boot partition */
8887c478bd9Sstevel@tonic-gate 	int xnumsect = -1;		/* length of extended DOS partition */
8897c478bd9Sstevel@tonic-gate 	int driveIndex;			/* computed FDISK table index */
8907c478bd9Sstevel@tonic-gate 	int i;
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * Count of drives in the current extended partition's
8937c478bd9Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
8947c478bd9Sstevel@tonic-gate 	 */
8957c478bd9Sstevel@tonic-gate 	int extndDrives[FD_NUMPART];
8967c478bd9Sstevel@tonic-gate 	int numDrives = 0;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * Count of drives (beyond primary) in master boot record's
9007c478bd9Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
9017c478bd9Sstevel@tonic-gate 	 */
9027c478bd9Sstevel@tonic-gate 	int extraDrives[FD_NUMPART];
9037c478bd9Sstevel@tonic-gate 	int numExtraDrives = 0;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/*
9067c478bd9Sstevel@tonic-gate 	 *  Copy from disk block into memory aligned structure for fdisk usage.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
9097c478bd9Sstevel@tonic-gate 	bcopy(dosp_ptr->parts, dosp, sizeof (struct ipart) * FD_NUMPART);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/*
9127c478bd9Sstevel@tonic-gate 	 * Get a summary of what is in the Master FDISK table.
9137c478bd9Sstevel@tonic-gate 	 * Normally we expect to find one partition marked as a DOS drive.
9147c478bd9Sstevel@tonic-gate 	 * This partition is the one Windows calls the primary dos partition.
9157c478bd9Sstevel@tonic-gate 	 * If the machine has any logical drives then we also expect
9167c478bd9Sstevel@tonic-gate 	 * to find a partition marked as an extended DOS partition.
9177c478bd9Sstevel@tonic-gate 	 *
9187c478bd9Sstevel@tonic-gate 	 * Sometimes we'll find multiple partitions marked as DOS drives.
9197c478bd9Sstevel@tonic-gate 	 * The Solaris fdisk program allows these partitions
9207c478bd9Sstevel@tonic-gate 	 * to be created, but Windows fdisk no longer does.  We still need
9217c478bd9Sstevel@tonic-gate 	 * to support these, though, since Windows does.  We also need to fix
9227c478bd9Sstevel@tonic-gate 	 * our fdisk to behave like the Windows version.
9237c478bd9Sstevel@tonic-gate 	 *
9247c478bd9Sstevel@tonic-gate 	 * It turns out that some off-the-shelf media have *only* an
9257c478bd9Sstevel@tonic-gate 	 * Extended partition, so we need to deal with that case as well.
9267c478bd9Sstevel@tonic-gate 	 *
9277c478bd9Sstevel@tonic-gate 	 * Only a single (the first) Extended or Boot Partition will
9287c478bd9Sstevel@tonic-gate 	 * be recognized.  Any others will be ignored.
9297c478bd9Sstevel@tonic-gate 	 */
9307c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
9310576819eSwyllys 		PC_DPRINTF1(2, "findTheDrive: found partition type %02x",
9320576819eSwyllys 			dosp[i].systid);
9330576819eSwyllys 
9347c478bd9Sstevel@tonic-gate 		if (isDosDrive(dosp[i].systid)) {
9357c478bd9Sstevel@tonic-gate 			if (primaryPart < 0) {
9367c478bd9Sstevel@tonic-gate 				logicalDriveCount++;
9377c478bd9Sstevel@tonic-gate 				primaryPart = i;
9387c478bd9Sstevel@tonic-gate 			} else {
9397c478bd9Sstevel@tonic-gate 				extraDrives[numExtraDrives++] = i;
9407c478bd9Sstevel@tonic-gate 			}
9417c478bd9Sstevel@tonic-gate 			continue;
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		if ((extendedPart < 0) && isDosExtended(dosp[i].systid)) {
9447c478bd9Sstevel@tonic-gate 			extendedPart = i;
9457c478bd9Sstevel@tonic-gate 			continue;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 		if ((bootPart < 0) && isBootPart(dosp[i].systid)) {
9487c478bd9Sstevel@tonic-gate 			bootPart = i;
9497c478bd9Sstevel@tonic-gate 			continue;
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (askedFor == BOOT_PARTITION_DRIVE) {
9547c478bd9Sstevel@tonic-gate 		if (bootPart < 0) {
9557c478bd9Sstevel@tonic-gate 			noLogicalDrive(askedFor);
9567c478bd9Sstevel@tonic-gate 			*error = EINVAL;
9577c478bd9Sstevel@tonic-gate 			return (0);
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 		*sysid = dosp[bootPart].systid;
9607c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[bootPart].relsect);
9617c478bd9Sstevel@tonic-gate 		return (1);
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if (askedFor == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
9657c478bd9Sstevel@tonic-gate 		*sysid = dosp[primaryPart].systid;
9667c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[primaryPart].relsect);
9677c478bd9Sstevel@tonic-gate 		return (1);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	/*
9717c478bd9Sstevel@tonic-gate 	 * We are not looking for the C: drive (or the primary drive
9727c478bd9Sstevel@tonic-gate 	 * was not found), so we had better have an extended partition
9737c478bd9Sstevel@tonic-gate 	 * or extra drives in the Master FDISK table.
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate 	if ((extendedPart < 0) && (numExtraDrives == 0)) {
9767c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no extended dos partition");
9777c478bd9Sstevel@tonic-gate 		noLogicalDrive(askedFor);
9787c478bd9Sstevel@tonic-gate 		*error = EINVAL;
9797c478bd9Sstevel@tonic-gate 		return (0);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (extendedPart >= 0) {
9837c478bd9Sstevel@tonic-gate 		diskblk = xstartsect = ltohi(dosp[extendedPart].relsect);
9847c478bd9Sstevel@tonic-gate 		xnumsect = ltohi(dosp[extendedPart].numsect);
9857c478bd9Sstevel@tonic-gate 		do {
9867c478bd9Sstevel@tonic-gate 			/*
9877c478bd9Sstevel@tonic-gate 			 *  If the seek would not cause us to change
9887c478bd9Sstevel@tonic-gate 			 *  position on the drive, then we're out of
9897c478bd9Sstevel@tonic-gate 			 *  extended partitions to examine.
9907c478bd9Sstevel@tonic-gate 			 */
9917c478bd9Sstevel@tonic-gate 			if (diskblk == lastseek)
9927c478bd9Sstevel@tonic-gate 				break;
9937c478bd9Sstevel@tonic-gate 			logicalDriveCount += numDrives;
9947c478bd9Sstevel@tonic-gate 			/*
9957c478bd9Sstevel@tonic-gate 			 *  Seek the next extended partition, and find
9967c478bd9Sstevel@tonic-gate 			 *  logical drives within it.
9977c478bd9Sstevel@tonic-gate 			 */
9987c478bd9Sstevel@tonic-gate 			brelse(*bp);
9997c478bd9Sstevel@tonic-gate 			*bp = bread(dev, diskblk, PC_SAFESECSIZE);
10007c478bd9Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10017c478bd9Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10027c478bd9Sstevel@tonic-gate 				*error = EIO;
10037c478bd9Sstevel@tonic-gate 				return (0);
10047c478bd9Sstevel@tonic-gate 			}
10057c478bd9Sstevel@tonic-gate 			lastseek = diskblk;
10067c478bd9Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10077c478bd9Sstevel@tonic-gate 			if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
10087c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: "
10097c478bd9Sstevel@tonic-gate 				    "extended partition signature err");
10107c478bd9Sstevel@tonic-gate 				*error = EINVAL;
10117c478bd9Sstevel@tonic-gate 				return (0);
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10147c478bd9Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10157c478bd9Sstevel@tonic-gate 			/*
10167c478bd9Sstevel@tonic-gate 			 *  Count up drives, and track where the next
10177c478bd9Sstevel@tonic-gate 			 *  extended partition is in case we need it.  We
10187c478bd9Sstevel@tonic-gate 			 *  are expecting only one extended partition.  If
10197c478bd9Sstevel@tonic-gate 			 *  there is more than one we'll only go to the
10207c478bd9Sstevel@tonic-gate 			 *  first one we see, but warn about ignoring.
10217c478bd9Sstevel@tonic-gate 			 */
10227c478bd9Sstevel@tonic-gate 			numDrives = 0;
10237c478bd9Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
10247c478bd9Sstevel@tonic-gate 				if (isDosDrive(dosp[i].systid)) {
10257c478bd9Sstevel@tonic-gate 					extndDrives[numDrives++] = i;
10267c478bd9Sstevel@tonic-gate 					continue;
10277c478bd9Sstevel@tonic-gate 				} else if (isDosExtended(dosp[i].systid)) {
10287c478bd9Sstevel@tonic-gate 					if (diskblk != lastseek) {
10297c478bd9Sstevel@tonic-gate 						/*
10307c478bd9Sstevel@tonic-gate 						 * Already found an extended
10317c478bd9Sstevel@tonic-gate 						 * partition in this table.
10327c478bd9Sstevel@tonic-gate 						 */
10337c478bd9Sstevel@tonic-gate 						cmn_err(CE_NOTE,
10347c478bd9Sstevel@tonic-gate 						    "!pcfs: ignoring unexpected"
10357c478bd9Sstevel@tonic-gate 						    " additional extended"
10367c478bd9Sstevel@tonic-gate 						    " partition");
10377c478bd9Sstevel@tonic-gate 						continue;
10387c478bd9Sstevel@tonic-gate 					}
10397c478bd9Sstevel@tonic-gate 					diskblk = xstartsect +
10407c478bd9Sstevel@tonic-gate 					    ltohi(dosp[i].relsect);
10417c478bd9Sstevel@tonic-gate 					continue;
10427c478bd9Sstevel@tonic-gate 				}
10437c478bd9Sstevel@tonic-gate 			}
10447c478bd9Sstevel@tonic-gate 		} while (askedFor > logicalDriveCount + numDrives);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 		if (askedFor <= logicalDriveCount + numDrives) {
10477c478bd9Sstevel@tonic-gate 			/*
10487c478bd9Sstevel@tonic-gate 			 * The number of logical drives we've found thus
10497c478bd9Sstevel@tonic-gate 			 * far is enough to get us to the one we were
10507c478bd9Sstevel@tonic-gate 			 * searching for.
10517c478bd9Sstevel@tonic-gate 			 */
10527c478bd9Sstevel@tonic-gate 			driveIndex = logicalDriveCount + numDrives - askedFor;
10537c478bd9Sstevel@tonic-gate 			*sysid = dosp[extndDrives[driveIndex]].systid;
10547c478bd9Sstevel@tonic-gate 			*startSector =
10557c478bd9Sstevel@tonic-gate 			    ltohi(dosp[extndDrives[driveIndex]].relsect) +
10567c478bd9Sstevel@tonic-gate 			    lastseek;
10577c478bd9Sstevel@tonic-gate 			if (*startSector > (xstartsect + xnumsect)) {
10587c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: extended partition "
10597c478bd9Sstevel@tonic-gate 				    "values bad");
10607c478bd9Sstevel@tonic-gate 				*error = EINVAL;
10617c478bd9Sstevel@tonic-gate 				return (0);
10627c478bd9Sstevel@tonic-gate 			}
10637c478bd9Sstevel@tonic-gate 			return (1);
10647c478bd9Sstevel@tonic-gate 		} else {
10657c478bd9Sstevel@tonic-gate 			/*
10667c478bd9Sstevel@tonic-gate 			 * We ran out of extended dos partition
10677c478bd9Sstevel@tonic-gate 			 * drives.  The only hope now is to go
10687c478bd9Sstevel@tonic-gate 			 * back to extra drives defined in the master
10697c478bd9Sstevel@tonic-gate 			 * fdisk table.  But we overwrote that table
10707c478bd9Sstevel@tonic-gate 			 * already, so we must load it in again.
10717c478bd9Sstevel@tonic-gate 			 */
10727c478bd9Sstevel@tonic-gate 			logicalDriveCount += numDrives;
10737c478bd9Sstevel@tonic-gate 			brelse(*bp);
10747c478bd9Sstevel@tonic-gate 			*bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
10757c478bd9Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10767c478bd9Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10777c478bd9Sstevel@tonic-gate 				*error = EIO;
10787c478bd9Sstevel@tonic-gate 				return (0);
10797c478bd9Sstevel@tonic-gate 			}
10807c478bd9Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10817c478bd9Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10827c478bd9Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 *  Still haven't found the drive, is it an extra
10877c478bd9Sstevel@tonic-gate 	 *  drive defined in the main FDISK table?
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	if (askedFor <= logicalDriveCount + numExtraDrives) {
10907c478bd9Sstevel@tonic-gate 		driveIndex = logicalDriveCount + numExtraDrives - askedFor;
10917c478bd9Sstevel@tonic-gate 		*sysid = dosp[extraDrives[driveIndex]].systid;
10927c478bd9Sstevel@tonic-gate 		*startSector = ltohi(dosp[extraDrives[driveIndex]].relsect);
10937c478bd9Sstevel@tonic-gate 		return (1);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 	/*
10967c478bd9Sstevel@tonic-gate 	 *  Still haven't found the drive, and there is
10977c478bd9Sstevel@tonic-gate 	 *  nowhere else to look.
10987c478bd9Sstevel@tonic-gate 	 */
10997c478bd9Sstevel@tonic-gate 	noLogicalDrive(askedFor);
11007c478bd9Sstevel@tonic-gate 	*error = EINVAL;
11017c478bd9Sstevel@tonic-gate 	return (0);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11040576819eSwyllys /*
11050576819eSwyllys  * FAT12/FAT16 specific consistency checks.
11060576819eSwyllys  */
11070576819eSwyllys static int
11080576819eSwyllys check_bpb_fat16(struct bootsec *bpb)
11090576819eSwyllys {
11100576819eSwyllys 	if (pcfsdebuglevel >= 5) {
11110576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat: RootEntCount = %d",
11120576819eSwyllys 			ltohs(bpb->rdirents[0]));
11130576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec16 = %d",
11140576819eSwyllys 			ltohs(bpb->numsect[0]));
11150576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: FATSz16 = %d",
11160576819eSwyllys 			ltohs(bpb->fatsec));
11170576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat16: TotSec32 = %d",
11180576819eSwyllys 			ltohi(bpb->totalsec));
11190576819eSwyllys 	}
11200576819eSwyllys 	return (ltohs(bpb->rdirents[0]) > 0 &&	 /* RootEntCnt > 0 */
11210576819eSwyllys 		((ltohs(bpb->numsect[0]) == 0 && /* TotSec16 == 0 */
11220576819eSwyllys 		ltohi(bpb->totalsec) > 0) ||	 /* TotSec32 > 0 */
11230576819eSwyllys 		ltohs(bpb->numsect[0]) > 0) && /* TotSec16 > 0 */
11240576819eSwyllys 		ltohs(bpb->fatsec) > 0);	 /* FatSz16 > 0 */
11250576819eSwyllys }
11260576819eSwyllys 
11270576819eSwyllys /*
11280576819eSwyllys  * FAT32 specific consistency checks.
11290576819eSwyllys  */
11300576819eSwyllys static int
11310576819eSwyllys check_bpb_fat32(struct fat32_bootsec *bpb)
11320576819eSwyllys {
11330576819eSwyllys 	if (pcfsdebuglevel >= 5) {
11340576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: RootEntCount = %d",
11350576819eSwyllys 			ltohs(bpb->f_bs.rdirents[0]));
11360576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec16 = %d",
11370576819eSwyllys 			ltohs(bpb->f_bs.numsect[0]));
11380576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz16 = %d",
11390576819eSwyllys 			ltohs(bpb->f_bs.fatsec));
11400576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: TotSec32 = %d",
11410576819eSwyllys 			ltohi(bpb->f_bs.totalsec));
11420576819eSwyllys 		PC_DPRINTF1(5, "check_bpb_fat32: FATSz32 = %d",
11430576819eSwyllys 			ltohi(bpb->f_fatlength));
11440576819eSwyllys 	}
11450576819eSwyllys 	return (ltohs(bpb->f_bs.rdirents[0]) == 0 &&
11460576819eSwyllys 		ltohs(bpb->f_bs.numsect[0]) == 0 &&
11470576819eSwyllys 		ltohs(bpb->f_bs.fatsec) == 0 &&
11480576819eSwyllys 		ltohi(bpb->f_bs.totalsec) > 0 &&
11490576819eSwyllys 		ltohi(bpb->f_fatlength) > 0);
11500576819eSwyllys }
11510576819eSwyllys 
11520576819eSwyllys /*
11530576819eSwyllys  * Calculate the number of clusters in order to determine
11540576819eSwyllys  * the type of FAT we are looking at.  This is the only
11550576819eSwyllys  * recommended way of determining FAT type, though there
11560576819eSwyllys  * are other hints in the data, this is the best way.
11570576819eSwyllys  */
11580576819eSwyllys static ulong_t
11590576819eSwyllys bpb_to_numclusters(uchar_t *cp)
11600576819eSwyllys {
11610576819eSwyllys 	struct fat32_bootsec *bpb;
11620576819eSwyllys 
11630576819eSwyllys 	ulong_t rootdirsectors;
11640576819eSwyllys 	ulong_t FATsz;
11650576819eSwyllys 	ulong_t TotSec;
11660576819eSwyllys 	ulong_t DataSec;
11670576819eSwyllys 	ulong_t CountOfClusters;
11680576819eSwyllys 	char FileSysType[9];
11690576819eSwyllys 
11700576819eSwyllys 	/*
11710576819eSwyllys 	 * Cast it to FAT32 bpb. If it turns out to be FAT12/16, its
11720576819eSwyllys 	 * OK, we won't try accessing the data beyond the FAT16 header
11730576819eSwyllys 	 * boundary.
11740576819eSwyllys 	 */
11750576819eSwyllys 	bpb = (struct fat32_bootsec *)cp;
11760576819eSwyllys 
11770576819eSwyllys 	if (pcfsdebuglevel >= 5) {
11780576819eSwyllys 		if (ltohs(bpb->f_bs.rdirents[0]) != 0) {
117957e22c6cSwyllys 			(void) memcpy(FileSysType, &cp[54], 8);
11800576819eSwyllys 			FileSysType[8] = 0;
11810576819eSwyllys 			PC_DPRINTF1(5, "debug_bpb: FAT12/FAT16 FileSysType = "
11820576819eSwyllys 				"%s", FileSysType);
11830576819eSwyllys 		}
11840576819eSwyllys 	}
11850576819eSwyllys 
11860576819eSwyllys 	rootdirsectors = ((ltohs(bpb->f_bs.rdirents[0]) * 32) +
11870576819eSwyllys 		(ltohs(bpb->f_bs.bps[0]) - 1)) / ltohs(bpb->f_bs.bps[0]);
11880576819eSwyllys 
11890576819eSwyllys 	if (ltohs(bpb->f_bs.fatsec) != 0)
11900576819eSwyllys 		FATsz = ltohs(bpb->f_bs.fatsec);
11910576819eSwyllys 	else
11920576819eSwyllys 		FATsz = ltohi(bpb->f_fatlength);
11930576819eSwyllys 
11940576819eSwyllys 	if (ltohs(bpb->f_bs.numsect[0]) != 0)
11950576819eSwyllys 		TotSec = ltohs(bpb->f_bs.numsect[0]);
11960576819eSwyllys 	else
11970576819eSwyllys 		TotSec = ltohi(bpb->f_bs.totalsec);
11980576819eSwyllys 
11990576819eSwyllys 	DataSec = TotSec - (ltohs(bpb->f_bs.res_sec[0]) +
12000576819eSwyllys 			(bpb->f_bs.nfat * FATsz) + rootdirsectors);
12010576819eSwyllys 
12020576819eSwyllys 	CountOfClusters = DataSec / bpb->f_bs.spcl;
12030576819eSwyllys 
12040576819eSwyllys 	PC_DPRINTF1(5, "debug_bpb: CountOfClusters = %ld", CountOfClusters);
12050576819eSwyllys 
12060576819eSwyllys 	return (CountOfClusters);
12070576819eSwyllys 
12080576819eSwyllys }
12090576819eSwyllys 
12100576819eSwyllys static int
12110576819eSwyllys fattype(ulong_t CountOfClusters)
12120576819eSwyllys {
12130576819eSwyllys 	/*
12140576819eSwyllys 	 * From Microsoft:
12150576819eSwyllys 	 * In the following example, when it says <, it does not mean <=.
12160576819eSwyllys 	 * Note also that the numbers are correct.  The first number for
12170576819eSwyllys 	 * FAT12 is 4085; the second number for FAT16 is 65525. These numbers
12180576819eSwyllys 	 * and the '<' signs are not wrong.
12190576819eSwyllys 	 */
12200576819eSwyllys 
12210576819eSwyllys 	/* Watch for edge cases */
12220576819eSwyllys 	if ((CountOfClusters >= 4085 && CountOfClusters <= 4095) ||
12230576819eSwyllys 	    (CountOfClusters >= 65525 && CountOfClusters <= 65535)) {
12240576819eSwyllys 		PC_DPRINTF1(5, "debug_bpb: Cannot determine FAT yet - %ld",
12250576819eSwyllys 			CountOfClusters);
12260576819eSwyllys 		return (-1); /* Cannot be determined yet */
12270576819eSwyllys 	} else if (CountOfClusters < 4085) {
12280576819eSwyllys 		/* Volume is FAT12 */
12290576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT12");
12300576819eSwyllys 		return (0);
12310576819eSwyllys 	} else if (CountOfClusters < 65525) {
12320576819eSwyllys 		/* Volume is FAT16 */
12330576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT16");
12340576819eSwyllys 		return (PCFS_FAT16);
12350576819eSwyllys 	} else {
12360576819eSwyllys 		/* Volume is FAT32 */
12370576819eSwyllys 		PC_DPRINTF0(5, "debug_bpb: This must be FAT32");
12380576819eSwyllys 		return (PCFS_FAT32);
12390576819eSwyllys 	}
12400576819eSwyllys }
12410576819eSwyllys 
12420576819eSwyllys #define	VALID_SECSIZE(s) (s == 512 || s == 1024 || s == 2048 || s == 4096)
12430576819eSwyllys 
12440576819eSwyllys #define	VALID_SPCL(s) (s == 1 || s == 2 || s == 4 || s == 8 || s == 16 ||\
12450576819eSwyllys 	s == 32 || s == 64 || s == 128)
12460576819eSwyllys 
12470576819eSwyllys static int
12480576819eSwyllys secondaryBPBChecks(uchar_t *cp)
12490576819eSwyllys {
12500576819eSwyllys 	struct bootsec *bpb = (struct bootsec *)cp;
12510576819eSwyllys 	struct fat32_bootsec *f32bpb = (struct fat32_bootsec *)cp;
12520576819eSwyllys 
12530576819eSwyllys 	/*
12540576819eSwyllys 	 * Perform secondary checks to try and determine what sort
12550576819eSwyllys 	 * of FAT partition we have based on other, less reliable,
12560576819eSwyllys 	 * data in the BPB header.
12570576819eSwyllys 	 */
12580576819eSwyllys 	if (ltohs(bpb->fatsec) != 0) {
12590576819eSwyllys 		/*
12600576819eSwyllys 		 * Must be FAT12 or FAT16, check the
12610576819eSwyllys 		 * FilSysType string (not 100% reliable).
12620576819eSwyllys 		 */
12630576819eSwyllys 		if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT12", 5)) {
12640576819eSwyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT12");
12650576819eSwyllys 			return (0); /* FAT12 */
12660576819eSwyllys 		} else if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT16",
12670576819eSwyllys 			5)) {
12680576819eSwyllys 			PC_DPRINTF0(5, "secondaryBPBCheck says: FAT16");
12690576819eSwyllys 			return (PCFS_FAT16);
12700576819eSwyllys 		} else {
12710576819eSwyllys 			/*
12720576819eSwyllys 			 * Try to use the BPB_Media byte
12730576819eSwyllys 			 *
12740576819eSwyllys 			 *  If the media byte indicates a floppy we'll
12750576819eSwyllys 			 *  assume FAT12, otherwise we'll assume FAT16.
12760576819eSwyllys 			 */
12770576819eSwyllys 			switch (bpb->mediadesriptor) {
12780576819eSwyllys 				case SS8SPT:
12790576819eSwyllys 				case DS8SPT:
12800576819eSwyllys 				case SS9SPT:
12810576819eSwyllys 				case DS9SPT:
12820576819eSwyllys 				case DS18SPT:
12830576819eSwyllys 				case DS9_15SPT:
12840576819eSwyllys 					PC_DPRINTF0(5,
12850576819eSwyllys 					"secondaryBPBCheck says: FAT12");
12860576819eSwyllys 					return (0); /* FAT12 */
12870576819eSwyllys 				case MD_FIXED:
12880576819eSwyllys 					PC_DPRINTF0(5,
12890576819eSwyllys 					"secondaryBPBCheck says: FAT16");
12900576819eSwyllys 					return (PCFS_FAT16);
12910576819eSwyllys 				default:
12920576819eSwyllys 					cmn_err(CE_NOTE,
12930576819eSwyllys 						"!pcfs: unknown FAT type");
12940576819eSwyllys 					return (-1);
12950576819eSwyllys 			}
12960576819eSwyllys 		}
12970576819eSwyllys 	} else if (ltohi(f32bpb->f_fatlength) > 0) {
12980576819eSwyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: FAT32");
12990576819eSwyllys 		return (PCFS_FAT32);
13000576819eSwyllys 	} else {
13010576819eSwyllys 		/* We don't know */
13020576819eSwyllys 		PC_DPRINTF0(5, "secondaryBPBCheck says: unknown!!");
13030576819eSwyllys 		return (-1);
13040576819eSwyllys 	}
13050576819eSwyllys }
13060576819eSwyllys 
13070576819eSwyllys /*
13080576819eSwyllys  * Check to see if the BPB we found is correct.
13090576819eSwyllys  *
13100576819eSwyllys  * First, look for obvious, tell-tale signs of trouble:
13110576819eSwyllys  * The NumFATs value should always be 2.  Sometimes it can be a '1'
13120576819eSwyllys  * on FLASH memory cards and other non-disk-based media, so we
13130576819eSwyllys  * will allow that as well.
13140576819eSwyllys  *
13150576819eSwyllys  * We also look at the Media byte, the valid range is 0xF0, or
13160576819eSwyllys  * 0xF8 thru 0xFF, anything else means this is probably not a good
13170576819eSwyllys  * BPB.
13180576819eSwyllys  *
13190576819eSwyllys  * Finally, check the BPB Magic number at the end of the 512 byte
13200576819eSwyllys  * block, it must be 0xAA55.
13210576819eSwyllys  *
13220576819eSwyllys  * If that all is good, calculate the number of clusters and
13230576819eSwyllys  * do some final verification steps.
13240576819eSwyllys  *
13250576819eSwyllys  * If all is well, return success (1) and set the fattypep
13260576819eSwyllys  * value to the correct FAT value.
13270576819eSwyllys  */
13280576819eSwyllys static int
13290576819eSwyllys isBPB(uchar_t *cp, int *fattypep)
13300576819eSwyllys {
13310576819eSwyllys 	struct bootsec *bpb = (struct bootsec *)cp;
13320576819eSwyllys 
13330576819eSwyllys 	uint_t numclusters;		/* number of clusters in file area */
13340576819eSwyllys 	ushort_t secsize = (int)ltohs(bpb->bps[0]);
13350576819eSwyllys 
13360576819eSwyllys 	if (pcfsdebuglevel >= 3) {
13370576819eSwyllys 		if (!VALID_SECSIZE(secsize))
13380576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: invalid bps value %d",
13390576819eSwyllys 				secsize);
13400576819eSwyllys 
13410576819eSwyllys 		if (!VALID_SPCL(bpb->spcl))
13420576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: invalid spcl value %d",
13430576819eSwyllys 				bpb->spcl);
13440576819eSwyllys 
13450576819eSwyllys 		if ((secsize * bpb->spcl) >= (32 * 1024))
13460576819eSwyllys 			PC_DPRINTF3(3, "check_bpb: BPC > 32K  %d x %d = %d",
13470576819eSwyllys 				secsize,
13480576819eSwyllys 				bpb->spcl,
13490576819eSwyllys 				secsize * bpb->spcl);
13500576819eSwyllys 
13510576819eSwyllys 		if (bpb->nfat == 0)
13520576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: bad NumFATs value %d",
13530576819eSwyllys 				bpb->nfat);
13540576819eSwyllys 
13550576819eSwyllys 		if (ltohs(bpb->res_sec[0]) == 0)
13560576819eSwyllys 			PC_DPRINTF1(3, "check_bpb: bad RsvdSecCnt value %d",
13570576819eSwyllys 				ltohs(bpb->res_sec[0]));
13580576819eSwyllys 
13590576819eSwyllys 		PC_DPRINTF1(5, "check_bpb: Media byte = %02x",
13600576819eSwyllys 			bpb->mediadesriptor);
13610576819eSwyllys 
13620576819eSwyllys 	}
13630576819eSwyllys 	if ((bpb->nfat == 0) ||
13640576819eSwyllys 		(bpb->mediadesriptor != 0xF0 && bpb->mediadesriptor < 0xF8) ||
13650576819eSwyllys 		(ltohs(cp[510]) != MBB_MAGIC) ||
13660576819eSwyllys 		!VALID_SECSIZE(secsize) ||
13670576819eSwyllys 		!VALID_SPCL(bpb->spcl) ||
13680576819eSwyllys 		(secsize * bpb->spcl >= (64 * 1024)) ||
13690576819eSwyllys 		!(ltohs(bpb->res_sec[0])))
13700576819eSwyllys 		return (0);
13710576819eSwyllys 
13720576819eSwyllys 	/*
13730576819eSwyllys 	 * Basic sanity checks passed so far, now try to determine which
13740576819eSwyllys 	 * FAT format to use.
13750576819eSwyllys 	 */
13760576819eSwyllys 	numclusters = bpb_to_numclusters(cp);
13770576819eSwyllys 
13780576819eSwyllys 	*fattypep = fattype(numclusters);
13790576819eSwyllys 
13800576819eSwyllys 	/* Do some final sanity checks for each specific type of FAT */
13810576819eSwyllys 	switch (*fattypep) {
13820576819eSwyllys 		case 0: /* FAT12 */
13830576819eSwyllys 		case PCFS_FAT16:
13840576819eSwyllys 			if (!check_bpb_fat16((struct bootsec *)cp))
13850576819eSwyllys 				return (0);
13860576819eSwyllys 			break;
13870576819eSwyllys 		case PCFS_FAT32:
13880576819eSwyllys 			if (!check_bpb_fat32((struct fat32_bootsec *)cp))
13890576819eSwyllys 				return (0);
13900576819eSwyllys 			break;
13910576819eSwyllys 		default: /* not sure yet */
13920576819eSwyllys 			*fattypep = secondaryBPBChecks(cp);
13930576819eSwyllys 			if (*fattypep == -1) {
13940576819eSwyllys 				/* Still nothing, give it up. */
13950576819eSwyllys 				return (0);
13960576819eSwyllys 			}
13970576819eSwyllys 			break;
13980576819eSwyllys 	}
13990576819eSwyllys 	PC_DPRINTF0(5, "isBPB: BPB passes verification tests");
14000576819eSwyllys 	return (1);
14010576819eSwyllys }
14020576819eSwyllys 
14030576819eSwyllys 
14047c478bd9Sstevel@tonic-gate /*
14057c478bd9Sstevel@tonic-gate  * Get the FAT type for the DOS medium.
14067c478bd9Sstevel@tonic-gate  *
14070576819eSwyllys  * -------------------------
14080576819eSwyllys  * According to Microsoft:
14090576819eSwyllys  *   The FAT type one of FAT12, FAT16, or FAT32 is determined by the
14100576819eSwyllys  * count of clusters on the volume and nothing else.
14110576819eSwyllys  * -------------------------
14127c478bd9Sstevel@tonic-gate  *
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate static int
14157c478bd9Sstevel@tonic-gate pc_getfattype(
14167c478bd9Sstevel@tonic-gate 	struct vnode *devvp,
14177c478bd9Sstevel@tonic-gate 	int ldrive,
14187c478bd9Sstevel@tonic-gate 	daddr_t *strtsectp,
14197c478bd9Sstevel@tonic-gate 	int *fattypep)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	uchar_t *cp;			/* for searching out FAT string */
14227c478bd9Sstevel@tonic-gate 	buf_t *bp = NULL;		/* Disk buffer pointer */
14237c478bd9Sstevel@tonic-gate 	int rval = 0;
14247c478bd9Sstevel@tonic-gate 	uchar_t sysid = 0;		/* System ID character */
14257c478bd9Sstevel@tonic-gate 	dev_t	dev = devvp->v_rdev;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	*strtsectp = (daddr_t)0;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14307c478bd9Sstevel@tonic-gate 	 * Open the device so we can check out the BPB or FDISK table,
14317c478bd9Sstevel@tonic-gate 	 * then read in the sector.
14327c478bd9Sstevel@tonic-gate 	 */
14337c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(5, "pc_getfattype: dev=%x  ldrive=%x  ", (int)dev, ldrive);
14347c478bd9Sstevel@tonic-gate 	if (rval = VOP_OPEN(&devvp, FREAD, CRED())) {
14357c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfattype: open error=%d\n", rval);
14367c478bd9Sstevel@tonic-gate 		return (rval);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	/*
14407c478bd9Sstevel@tonic-gate 	 *  Read block 0 from device
14417c478bd9Sstevel@tonic-gate 	 */
14427c478bd9Sstevel@tonic-gate 	bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
14437c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
14447c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfattype: read error\n");
14457c478bd9Sstevel@tonic-gate 		rval = EIO;
14467c478bd9Sstevel@tonic-gate 		goto out;
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	cp = (uchar_t *)bp->b_un.b_addr;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14520576819eSwyllys 	 * If the first block is not a valid BPB, look for the
14530576819eSwyllys 	 * through the FDISK table.
14547c478bd9Sstevel@tonic-gate 	 */
14550576819eSwyllys 	if (!isBPB(cp, fattypep)) {
14560576819eSwyllys 		/* find the partition table and get 512 bytes from it. */
14570576819eSwyllys 		PC_DPRINTF0(5, "pc_getfattype: using FDISK table to find BPB");
14587c478bd9Sstevel@tonic-gate 
14590576819eSwyllys 		if (findTheDrive(dev, ldrive, &rval, &bp,
14600576819eSwyllys 			strtsectp, &sysid) == 0)
14610576819eSwyllys 			goto out;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 		brelse(bp);
14647c478bd9Sstevel@tonic-gate 		bp = bread(dev, *strtsectp, PC_SAFESECSIZE);
14657c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
14667c478bd9Sstevel@tonic-gate 			PC_DPRINTF0(1, "pc_getfattype: read error\n");
14677c478bd9Sstevel@tonic-gate 			rval = EIO;
14687c478bd9Sstevel@tonic-gate 			goto out;
14697c478bd9Sstevel@tonic-gate 		}
14700576819eSwyllys 		cp = (uchar_t *)bp->b_un.b_addr;
14717c478bd9Sstevel@tonic-gate 
14720576819eSwyllys 		/* If this one is still no good, give it up. */
14730576819eSwyllys 		if (!isBPB(cp, fattypep)) {
14740576819eSwyllys 			rval = EIO;
14757c478bd9Sstevel@tonic-gate 			goto out;
14767c478bd9Sstevel@tonic-gate 		}
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate out:
14800576819eSwyllys 	/*
14810576819eSwyllys 	 * Release the buffer used
14820576819eSwyllys 	 */
14837c478bd9Sstevel@tonic-gate 	if (bp != NULL)
14847c478bd9Sstevel@tonic-gate 		brelse(bp);
14857c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, CRED());
14867c478bd9Sstevel@tonic-gate 	return (rval);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate /*
14917c478bd9Sstevel@tonic-gate  * Get the boot parameter block and file allocation table.
14927c478bd9Sstevel@tonic-gate  * If there is an old FAT, invalidate it.
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate int
14957c478bd9Sstevel@tonic-gate pc_getfat(struct pcfs *fsp)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	struct vfs *vfsp = PCFSTOVFS(fsp);
14987c478bd9Sstevel@tonic-gate 	struct buf *tp = 0;
14997c478bd9Sstevel@tonic-gate 	struct buf *bp = 0;
15007c478bd9Sstevel@tonic-gate 	uchar_t *fatp = NULL;
15017c478bd9Sstevel@tonic-gate 	uchar_t *fat_changemap = NULL;
15027c478bd9Sstevel@tonic-gate 	struct bootsec *bootp;
15037c478bd9Sstevel@tonic-gate 	struct fat32_bootsec *f32b;
15047c478bd9Sstevel@tonic-gate 	struct vnode *devvp;
15057c478bd9Sstevel@tonic-gate 	int error;
15067c478bd9Sstevel@tonic-gate 	int fatsize;
15077c478bd9Sstevel@tonic-gate 	int fat_changemapsize;
15087c478bd9Sstevel@tonic-gate 	int flags = 0;
15097c478bd9Sstevel@tonic-gate 	int nfat;
15107c478bd9Sstevel@tonic-gate 	int secsize;
15117c478bd9Sstevel@tonic-gate 	int fatsec;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(5, "pc_getfat\n");
15147c478bd9Sstevel@tonic-gate 	devvp = fsp->pcfs_devvp;
15157c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_fatp) {
15167c478bd9Sstevel@tonic-gate 		/*
15177c478bd9Sstevel@tonic-gate 		 * There is a FAT in core.
15187c478bd9Sstevel@tonic-gate 		 * If there are open file pcnodes or we have modified it or
15197c478bd9Sstevel@tonic-gate 		 * it hasn't timed out yet use the in core FAT.
15207c478bd9Sstevel@tonic-gate 		 * Otherwise invalidate it and get a new one
15217c478bd9Sstevel@tonic-gate 		 */
15227c478bd9Sstevel@tonic-gate #ifdef notdef
15237c478bd9Sstevel@tonic-gate 		if (fsp->pcfs_frefs ||
15247c478bd9Sstevel@tonic-gate 		    (fsp->pcfs_flags & PCFS_FATMOD) ||
15257c478bd9Sstevel@tonic-gate 		    (gethrestime_sec() < fsp->pcfs_fattime)) {
15267c478bd9Sstevel@tonic-gate 			return (0);
15277c478bd9Sstevel@tonic-gate 		} else {
15287c478bd9Sstevel@tonic-gate 			mutex_enter(&pcfslock);
15297c478bd9Sstevel@tonic-gate 			pc_invalfat(fsp);
15307c478bd9Sstevel@tonic-gate 			mutex_exit(&pcfslock);
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate #endif /* notdef */
15337c478bd9Sstevel@tonic-gate 		return (0);
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * Open block device mounted on.
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	error = VOP_OPEN(&devvp,
15397c478bd9Sstevel@tonic-gate 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
15407c478bd9Sstevel@tonic-gate 	    CRED());
15417c478bd9Sstevel@tonic-gate 	if (error) {
15427c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfat: open error=%d\n", error);
15437c478bd9Sstevel@tonic-gate 		return (error);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	/*
15467c478bd9Sstevel@tonic-gate 	 * Get boot parameter block and check it for validity
15477c478bd9Sstevel@tonic-gate 	 */
15487c478bd9Sstevel@tonic-gate 	tp = bread(fsp->pcfs_xdev, fsp->pcfs_dosstart, PC_SAFESECSIZE);
15497c478bd9Sstevel@tonic-gate 	if (tp->b_flags & (B_ERROR | B_STALE)) {
15507c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfat: boot block error\n");
15517c478bd9Sstevel@tonic-gate 		flags = tp->b_flags & B_ERROR;
15527c478bd9Sstevel@tonic-gate 		error = EIO;
15537c478bd9Sstevel@tonic-gate 		goto out;
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
15567c478bd9Sstevel@tonic-gate 	bootp = (struct bootsec *)tp->b_un.b_addr;
15577c478bd9Sstevel@tonic-gate 
15580576819eSwyllys 
15597c478bd9Sstevel@tonic-gate 	/* get the sector size - may be more than 512 bytes */
15607c478bd9Sstevel@tonic-gate 	secsize = (int)ltohs(bootp->bps[0]);
15617c478bd9Sstevel@tonic-gate 	/* check for bogus sector size - fat should be at least 1 sector */
15627c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
15637c478bd9Sstevel@tonic-gate 		f32b = (struct fat32_bootsec *)bootp;
15647c478bd9Sstevel@tonic-gate 		fatsec = ltohi(f32b->f_fatlength);
15657c478bd9Sstevel@tonic-gate 	} else {
15667c478bd9Sstevel@tonic-gate 		fatsec = ltohs(bootp->fatsec);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 	if (secsize < 512 || fatsec < 1 || bootp->nfat < 1) {
15697c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT size error");
15707c478bd9Sstevel@tonic-gate 		error = EINVAL;
15717c478bd9Sstevel@tonic-gate 		goto out;
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	switch (bootp->mediadesriptor) {
15757c478bd9Sstevel@tonic-gate 	default:
15767c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: media-descriptor error, 0x%x",
15777c478bd9Sstevel@tonic-gate 		    bootp->mediadesriptor);
15787c478bd9Sstevel@tonic-gate 		error = EINVAL;
15797c478bd9Sstevel@tonic-gate 		goto out;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	case MD_FIXED:
15827c478bd9Sstevel@tonic-gate 		/*
158390c30842Sjmcp 		 * PCMCIA pseudo floppy is type MD_FIXED,
15847c478bd9Sstevel@tonic-gate 		 * but is accessed like a floppy
15857c478bd9Sstevel@tonic-gate 		 */
15867c478bd9Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_PCMCIA_NO_CIS)) {
15877c478bd9Sstevel@tonic-gate 			fsp->pcfs_flags |= PCFS_NOCHK;
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
15907c478bd9Sstevel@tonic-gate 	case SS8SPT:
15917c478bd9Sstevel@tonic-gate 	case DS8SPT:
15927c478bd9Sstevel@tonic-gate 	case SS9SPT:
15937c478bd9Sstevel@tonic-gate 	case DS9SPT:
15947c478bd9Sstevel@tonic-gate 	case DS18SPT:
15957c478bd9Sstevel@tonic-gate 	case DS9_15SPT:
15967c478bd9Sstevel@tonic-gate 		fsp->pcfs_secsize = secsize;
15977c478bd9Sstevel@tonic-gate 		fsp->pcfs_sdshift = secsize / DEV_BSIZE - 1;
15987c478bd9Sstevel@tonic-gate 		fsp->pcfs_entps = secsize / sizeof (struct pcdir);
15997c478bd9Sstevel@tonic-gate 		fsp->pcfs_spcl = (int)bootp->spcl;
16007c478bd9Sstevel@tonic-gate 		fsp->pcfs_fatsec = fatsec;
16017c478bd9Sstevel@tonic-gate 		fsp->pcfs_spt = (int)ltohs(bootp->spt);
16027c478bd9Sstevel@tonic-gate 		fsp->pcfs_rdirsec = (int)ltohs(bootp->rdirents[0])
16037c478bd9Sstevel@tonic-gate 		    * sizeof (struct pcdir) / secsize;
16047c478bd9Sstevel@tonic-gate 		fsp->pcfs_clsize = fsp->pcfs_spcl * secsize;
16057c478bd9Sstevel@tonic-gate 		fsp->pcfs_fatstart = fsp->pcfs_dosstart +
16067c478bd9Sstevel@tonic-gate 		    (daddr_t)ltohs(bootp->res_sec[0]);
16077c478bd9Sstevel@tonic-gate 		fsp->pcfs_rdirstart = fsp->pcfs_fatstart +
16087c478bd9Sstevel@tonic-gate 		    (bootp->nfat * fsp->pcfs_fatsec);
16097c478bd9Sstevel@tonic-gate 		fsp->pcfs_datastart = fsp->pcfs_rdirstart + fsp->pcfs_rdirsec;
16107c478bd9Sstevel@tonic-gate 		if (IS_FAT32(fsp))
16117c478bd9Sstevel@tonic-gate 			fsp->pcfs_rdirstart = ltohi(f32b->f_rootcluster);
16127c478bd9Sstevel@tonic-gate 		fsp->pcfs_ncluster = (((int)(ltohs(bootp->numsect[0]) ?
16137c478bd9Sstevel@tonic-gate 		    ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec))) -
16147c478bd9Sstevel@tonic-gate 		    fsp->pcfs_datastart + fsp->pcfs_dosstart) / fsp->pcfs_spcl;
16157c478bd9Sstevel@tonic-gate 		fsp->pcfs_numfat = (int)bootp->nfat;
16167c478bd9Sstevel@tonic-gate 		fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER;
16177c478bd9Sstevel@tonic-gate 		break;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * Get FAT and check it for validity
16227c478bd9Sstevel@tonic-gate 	 */
16237c478bd9Sstevel@tonic-gate 	fatsize = fsp->pcfs_fatsec * fsp->pcfs_secsize;
16247c478bd9Sstevel@tonic-gate 	fatp = kmem_alloc(fatsize, KM_SLEEP);
16257c478bd9Sstevel@tonic-gate 	error = pc_readfat(fsp, fatp, fsp->pcfs_fatstart, fatsize);
16267c478bd9Sstevel@tonic-gate 	if (error) {
16277c478bd9Sstevel@tonic-gate 		flags = B_ERROR;
16287c478bd9Sstevel@tonic-gate 		goto out;
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 	fat_changemapsize = (fatsize / fsp->pcfs_clsize) + 1;
16317c478bd9Sstevel@tonic-gate 	fat_changemap = kmem_zalloc(fat_changemapsize, KM_SLEEP);
16327c478bd9Sstevel@tonic-gate 
16330576819eSwyllys 	/*
16340576819eSwyllys 	 * The only definite signature check is that the
16350576819eSwyllys 	 * media descriptor byte should match the first byte
16360576819eSwyllys 	 * of the FAT block.
16370576819eSwyllys 	 */
16380576819eSwyllys 	if (fatp[0] != bootp->mediadesriptor) {
16397c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT signature error");
16407c478bd9Sstevel@tonic-gate 		error = EINVAL;
16417c478bd9Sstevel@tonic-gate 		goto out;
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * Checking for fatsec and number of supported clusters, should
16450576819eSwyllys 	 * actually determine a FAT12/FAT media.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_FAT16) {
16487c478bd9Sstevel@tonic-gate 		if ((fsp->pcfs_fatsec <= 12) &&
16497c478bd9Sstevel@tonic-gate 		    ((fatsize * 2 / 3) >= fsp->pcfs_ncluster)) {
16507c478bd9Sstevel@tonic-gate 			/*
16517c478bd9Sstevel@tonic-gate 			 * We have a 12-bit FAT, rather than a 16-bit FAT.
16527c478bd9Sstevel@tonic-gate 			 * Ignore what the fdisk table says.
16537c478bd9Sstevel@tonic-gate 			 */
16547c478bd9Sstevel@tonic-gate 			PC_DPRINTF0(2, "pc_getfattype: forcing 12-bit FAT\n");
16557c478bd9Sstevel@tonic-gate 			fsp->pcfs_flags &= ~PCFS_FAT16;
16567c478bd9Sstevel@tonic-gate 		}
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Sanity check our FAT is large enough for the
16607c478bd9Sstevel@tonic-gate 	 * clusters we think we have.
16617c478bd9Sstevel@tonic-gate 	 */
16627c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_FAT16) &&
16637c478bd9Sstevel@tonic-gate 	    ((fatsize / 2) < fsp->pcfs_ncluster)) {
16647c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT too small for number of clusters");
16657c478bd9Sstevel@tonic-gate 		error = EINVAL;
16667c478bd9Sstevel@tonic-gate 		goto out;
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	/*
16707c478bd9Sstevel@tonic-gate 	 * Get alternate FATs and check for consistency
16717c478bd9Sstevel@tonic-gate 	 * This is an inlined version of pc_readfat().
16727c478bd9Sstevel@tonic-gate 	 * Since we're only comparing FAT and alternate FAT,
16737c478bd9Sstevel@tonic-gate 	 * there's no reason to let pc_readfat() copy data out
16747c478bd9Sstevel@tonic-gate 	 * of the buf. Instead, compare in-situ, one cluster
16757c478bd9Sstevel@tonic-gate 	 * at a time.
16767c478bd9Sstevel@tonic-gate 	 */
16777c478bd9Sstevel@tonic-gate 	for (nfat = 1; nfat < fsp->pcfs_numfat; nfat++) {
16787c478bd9Sstevel@tonic-gate 		size_t startsec;
16797c478bd9Sstevel@tonic-gate 		size_t off;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 		startsec = fsp->pcfs_fatstart + nfat * fsp->pcfs_fatsec;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		for (off = 0; off < fatsize; off += fsp->pcfs_clsize) {
16847c478bd9Sstevel@tonic-gate 			bp = bread(fsp->pcfs_xdev, pc_dbdaddr(fsp,
16857c478bd9Sstevel@tonic-gate 				startsec +
16867c478bd9Sstevel@tonic-gate 				pc_cltodb(fsp, pc_lblkno(fsp, off))),
16877c478bd9Sstevel@tonic-gate 				MIN(fsp->pcfs_clsize, fatsize - off));
16887c478bd9Sstevel@tonic-gate 			if (bp->b_flags & (B_ERROR | B_STALE)) {
16897c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
16907c478bd9Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d read error"
16917c478bd9Sstevel@tonic-gate 					" at byte %ld", nfat, off);
16927c478bd9Sstevel@tonic-gate 				flags = B_ERROR;
16937c478bd9Sstevel@tonic-gate 				error = EIO;
16947c478bd9Sstevel@tonic-gate 				goto out;
16957c478bd9Sstevel@tonic-gate 			}
16967c478bd9Sstevel@tonic-gate 			bp->b_flags |= B_STALE | B_AGE;
16977c478bd9Sstevel@tonic-gate 			if (bcmp(bp->b_un.b_addr,
16987c478bd9Sstevel@tonic-gate 			    fatp + off,
16997c478bd9Sstevel@tonic-gate 			    MIN(fsp->pcfs_clsize, fatsize - off))) {
17007c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17017c478bd9Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d corrupted"
17027c478bd9Sstevel@tonic-gate 					" at byte %ld", nfat, off);
17037c478bd9Sstevel@tonic-gate 				flags = B_ERROR;
17047c478bd9Sstevel@tonic-gate 			}
17057c478bd9Sstevel@tonic-gate 			brelse(bp);
17067c478bd9Sstevel@tonic-gate 			bp = NULL;	/* prevent double release */
17077c478bd9Sstevel@tonic-gate 		}
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatsize = fatsize;
17117c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatp = fatp;
17127c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemapsize = fat_changemapsize;
17137c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = fat_changemap;
17147c478bd9Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
17157c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatjustread = 1;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	brelse(tp);
17187c478bd9Sstevel@tonic-gate 	tp = NULL;
17197c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
17207c478bd9Sstevel@tonic-gate 		/* get fsinfo */
17217c478bd9Sstevel@tonic-gate 		struct fat32_boot_fsinfo fsinfo_disk;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		fsp->f32fsinfo_sector = ltohs(f32b->f_infosector);
17247c478bd9Sstevel@tonic-gate 		tp = bread(fsp->pcfs_xdev,
17257c478bd9Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
17267c478bd9Sstevel@tonic-gate 		    PC_SAFESECSIZE);
17277c478bd9Sstevel@tonic-gate 		if (tp->b_flags & (B_ERROR | B_STALE)) {
17287c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: error reading fat32 fsinfo");
17297c478bd9Sstevel@tonic-gate 			flags = tp->b_flags & B_ERROR;
17307c478bd9Sstevel@tonic-gate 			brelse(tp);
17317c478bd9Sstevel@tonic-gate 			tp = NULL;
17327c478bd9Sstevel@tonic-gate 			error = EIO;
17337c478bd9Sstevel@tonic-gate 			goto out;
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 		tp->b_flags |= B_STALE | B_AGE;
17367c478bd9Sstevel@tonic-gate 		bcopy((void *)(tp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
17377c478bd9Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
17387c478bd9Sstevel@tonic-gate 		brelse(tp);
17397c478bd9Sstevel@tonic-gate 		tp = NULL;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		/* translated fields */
17427c478bd9Sstevel@tonic-gate 		fsp->fsinfo_native.fs_signature =
17437c478bd9Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_signature);
17447c478bd9Sstevel@tonic-gate 		fsp->fsinfo_native.fs_free_clusters =
17457c478bd9Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_free_clusters);
17467c478bd9Sstevel@tonic-gate 		if (fsp->fsinfo_native.fs_signature != FAT32_FS_SIGN) {
17477c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
17487c478bd9Sstevel@tonic-gate 			    "!pcfs: fat32 fsinfo signature mismatch.");
17497c478bd9Sstevel@tonic-gate 			error = EINVAL;
17507c478bd9Sstevel@tonic-gate 			goto out;
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	return (0);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate out:
17577c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!pcfs: illegal disk format");
17587c478bd9Sstevel@tonic-gate 	if (tp)
17597c478bd9Sstevel@tonic-gate 		brelse(tp);
17607c478bd9Sstevel@tonic-gate 	if (bp)
17617c478bd9Sstevel@tonic-gate 		brelse(bp);
17627c478bd9Sstevel@tonic-gate 	if (fatp)
17637c478bd9Sstevel@tonic-gate 		kmem_free(fatp, fatsize);
17647c478bd9Sstevel@tonic-gate 	if (fat_changemap)
17657c478bd9Sstevel@tonic-gate 		kmem_free(fat_changemap, fat_changemapsize);
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (flags) {
17687c478bd9Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
17697c478bd9Sstevel@tonic-gate 	}
17707c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
17717c478bd9Sstevel@tonic-gate 	    FREAD : FREAD|FWRITE, 1, (offset_t)0, CRED());
17727c478bd9Sstevel@tonic-gate 	return (error);
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate int
17767c478bd9Sstevel@tonic-gate pc_syncfat(struct pcfs *fsp)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	struct buf *bp;
17797c478bd9Sstevel@tonic-gate 	int nfat;
17807c478bd9Sstevel@tonic-gate 	int	error;
17817c478bd9Sstevel@tonic-gate 	struct fat32_boot_fsinfo fsinfo_disk;
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfat\n");
17847c478bd9Sstevel@tonic-gate 	if ((fsp->pcfs_fatp == (uchar_t *)0) ||
17857c478bd9Sstevel@tonic-gate 	    !(fsp->pcfs_flags & PCFS_FATMOD))
17867c478bd9Sstevel@tonic-gate 		return (0);
17877c478bd9Sstevel@tonic-gate 	/*
17887c478bd9Sstevel@tonic-gate 	 * write out all copies of FATs
17897c478bd9Sstevel@tonic-gate 	 */
17907c478bd9Sstevel@tonic-gate 	fsp->pcfs_flags &= ~PCFS_FATMOD;
17917c478bd9Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
17927c478bd9Sstevel@tonic-gate 	for (nfat = 0; nfat < fsp->pcfs_numfat; nfat++) {
17937c478bd9Sstevel@tonic-gate 		error = pc_writefat(fsp,
17947c478bd9Sstevel@tonic-gate 		    fsp->pcfs_fatstart + nfat*fsp->pcfs_fatsec);
17957c478bd9Sstevel@tonic-gate 		if (error) {
17967c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
17977c478bd9Sstevel@tonic-gate 			return (EIO);
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	pc_clear_fatchanges(fsp);
18017c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(6, "pcfs_syncfat: wrote out FAT\n");
18027c478bd9Sstevel@tonic-gate 	/* write out fsinfo */
18037c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
18047c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
18057c478bd9Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
18067c478bd9Sstevel@tonic-gate 		    PC_SAFESECSIZE);
18077c478bd9Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
18087c478bd9Sstevel@tonic-gate 			brelse(bp);
18097c478bd9Sstevel@tonic-gate 			return (EIO);
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 		bcopy((void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18127c478bd9Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
18137c478bd9Sstevel@tonic-gate 		/* translate fields */
18147c478bd9Sstevel@tonic-gate 		fsinfo_disk.fs_free_clusters =
18157c478bd9Sstevel@tonic-gate 		    htoli(fsp->fsinfo_native.fs_free_clusters);
18167c478bd9Sstevel@tonic-gate 		fsinfo_disk.fs_next_cluster = (uint32_t)FSINFO_UNKNOWN;
18177c478bd9Sstevel@tonic-gate 		bcopy(&fsinfo_disk,
18187c478bd9Sstevel@tonic-gate 		    (void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
18197c478bd9Sstevel@tonic-gate 		    sizeof (struct fat32_boot_fsinfo));
18207c478bd9Sstevel@tonic-gate 		bwrite2(bp);
18217c478bd9Sstevel@tonic-gate 		error = geterror(bp);
18227c478bd9Sstevel@tonic-gate 		brelse(bp);
18237c478bd9Sstevel@tonic-gate 		if (error) {
18247c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
18257c478bd9Sstevel@tonic-gate 			return (EIO);
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 	return (0);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate void
18327c478bd9Sstevel@tonic-gate pc_invalfat(struct pcfs *fsp)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate 	struct pcfs *xfsp;
18357c478bd9Sstevel@tonic-gate 	int mount_cnt = 0;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	PC_DPRINTF0(7, "pc_invalfat\n");
18387c478bd9Sstevel@tonic-gate 	if (fsp->pcfs_fatp == (uchar_t *)0)
18397c478bd9Sstevel@tonic-gate 		panic("pc_invalfat");
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * Release FAT
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fatp, fsp->pcfs_fatsize);
18447c478bd9Sstevel@tonic-gate 	fsp->pcfs_fatp = NULL;
18457c478bd9Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fat_changemap, fsp->pcfs_fat_changemapsize);
18467c478bd9Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = NULL;
18477c478bd9Sstevel@tonic-gate 	/*
18487c478bd9Sstevel@tonic-gate 	 * Invalidate all the blocks associated with the device.
18497c478bd9Sstevel@tonic-gate 	 * Not needed if stateless.
18507c478bd9Sstevel@tonic-gate 	 */
18517c478bd9Sstevel@tonic-gate 	for (xfsp = pc_mounttab; xfsp; xfsp = xfsp->pcfs_nxt)
18527c478bd9Sstevel@tonic-gate 		if (xfsp != fsp && xfsp->pcfs_xdev == fsp->pcfs_xdev)
18537c478bd9Sstevel@tonic-gate 			mount_cnt++;
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if (!mount_cnt)
18567c478bd9Sstevel@tonic-gate 		binval(fsp->pcfs_xdev);
18577c478bd9Sstevel@tonic-gate 	/*
18587c478bd9Sstevel@tonic-gate 	 * close mounted device
18597c478bd9Sstevel@tonic-gate 	 */
18607c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(fsp->pcfs_devvp,
18617c478bd9Sstevel@tonic-gate 	    (PCFSTOVFS(fsp)->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
18627c478bd9Sstevel@tonic-gate 	    1, (offset_t)0, CRED());
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate void
18667c478bd9Sstevel@tonic-gate pc_badfs(struct pcfs *fsp)
18677c478bd9Sstevel@tonic-gate {
18687c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "corrupted PC file system on dev %x.%x\n",
18697c478bd9Sstevel@tonic-gate 	    getmajor(fsp->pcfs_devvp->v_rdev),
18707c478bd9Sstevel@tonic-gate 	    getminor(fsp->pcfs_devvp->v_rdev));
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate  * The problem with supporting NFS on the PCFS filesystem is that there
18757c478bd9Sstevel@tonic-gate  * is no good place to keep the generation number. The only possible
18767c478bd9Sstevel@tonic-gate  * place is inside a directory entry. There are a few words that we
18777c478bd9Sstevel@tonic-gate  * don't use - they store NT & OS/2 attributes, and the creation/last access
18787c478bd9Sstevel@tonic-gate  * time of the file - but it seems wrong to use them. In addition, directory
18797c478bd9Sstevel@tonic-gate  * entries come and go. If a directory is removed completely, its directory
18807c478bd9Sstevel@tonic-gate  * blocks are freed and the generation numbers are lost. Whereas in ufs,
18817c478bd9Sstevel@tonic-gate  * inode blocks are dedicated for inodes, so the generation numbers are
18827c478bd9Sstevel@tonic-gate  * permanently kept on the disk.
18837c478bd9Sstevel@tonic-gate  */
18847c478bd9Sstevel@tonic-gate static int
18857c478bd9Sstevel@tonic-gate pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
18867c478bd9Sstevel@tonic-gate {
18877c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
18887c478bd9Sstevel@tonic-gate 	struct pc_fid *pcfid;
18897c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
18907c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
18917c478bd9Sstevel@tonic-gate 	daddr_t eblkno;
18927c478bd9Sstevel@tonic-gate 	int eoffset;
18937c478bd9Sstevel@tonic-gate 	struct buf *bp;
18947c478bd9Sstevel@tonic-gate 	int error;
18957c478bd9Sstevel@tonic-gate 	pc_cluster32_t	cn;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	pcfid = (struct pc_fid *)fidp;
18987c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	error = pc_lockfs(fsp, 0, 0);
19017c478bd9Sstevel@tonic-gate 	if (error) {
19027c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19037c478bd9Sstevel@tonic-gate 		return (error);
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (pcfid->pcfid_block == 0) {
19077c478bd9Sstevel@tonic-gate 		pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
19087c478bd9Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
19097c478bd9Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
19107c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19117c478bd9Sstevel@tonic-gate 		return (0);
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 	eblkno = pcfid->pcfid_block;
19147c478bd9Sstevel@tonic-gate 	eoffset = pcfid->pcfid_offset;
19157c478bd9Sstevel@tonic-gate 	if ((pc_dbtocl(fsp,
19167c478bd9Sstevel@tonic-gate 	    eblkno - fsp->pcfs_dosstart) >= fsp->pcfs_ncluster) ||
19177c478bd9Sstevel@tonic-gate 	    (eoffset > fsp->pcfs_clsize)) {
19187c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19197c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19207c478bd9Sstevel@tonic-gate 		return (EINVAL);
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	if (eblkno >= fsp->pcfs_datastart || (eblkno-fsp->pcfs_rdirstart)
19247c478bd9Sstevel@tonic-gate 	    < (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) {
19257c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno, fsp->pcfs_clsize);
19267c478bd9Sstevel@tonic-gate 	} else {
19277c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno,
19287c478bd9Sstevel@tonic-gate 		    (int)(fsp->pcfs_datastart - eblkno) * fsp->pcfs_secsize);
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 	if (bp->b_flags & (B_ERROR | B_STALE)) {
19317c478bd9Sstevel@tonic-gate 		error = geterror(bp);
19327c478bd9Sstevel@tonic-gate 		brelse(bp);
19337c478bd9Sstevel@tonic-gate 		if (error)
19347c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
19357c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19367c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19377c478bd9Sstevel@tonic-gate 		return (error);
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 	ep = (struct pcdir *)(bp->b_un.b_addr + eoffset);
19407c478bd9Sstevel@tonic-gate 	/*
19417c478bd9Sstevel@tonic-gate 	 * Ok, if this is a valid file handle that we gave out,
19427c478bd9Sstevel@tonic-gate 	 * then simply ensuring that the creation time matches,
19437c478bd9Sstevel@tonic-gate 	 * the entry has not been deleted, and it has a valid first
19447c478bd9Sstevel@tonic-gate 	 * character should be enough.
19457c478bd9Sstevel@tonic-gate 	 *
19467c478bd9Sstevel@tonic-gate 	 * Unfortunately, verifying that the <blkno, offset> _still_
19477c478bd9Sstevel@tonic-gate 	 * refers to a directory entry is not easy, since we'd have
19487c478bd9Sstevel@tonic-gate 	 * to search _all_ directories starting from root to find it.
19497c478bd9Sstevel@tonic-gate 	 * That's a high price to pay just in case somebody is forging
19507c478bd9Sstevel@tonic-gate 	 * file handles. So instead we verify that as much of the
19517c478bd9Sstevel@tonic-gate 	 * entry is valid as we can:
19527c478bd9Sstevel@tonic-gate 	 *
19537c478bd9Sstevel@tonic-gate 	 * 1. The starting cluster is 0 (unallocated) or valid
19547c478bd9Sstevel@tonic-gate 	 * 2. It is not an LFN entry
19557c478bd9Sstevel@tonic-gate 	 * 3. It is not hidden (unless mounted as such)
19567c478bd9Sstevel@tonic-gate 	 * 4. It is not the label
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, ep);
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * if the starting cluster is valid, but not valid according
19617c478bd9Sstevel@tonic-gate 	 * to pc_validcl(), force it to be to simplify the following if.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	if (cn == 0)
19647c478bd9Sstevel@tonic-gate 		cn = PCF_FIRSTCLUSTER;
19657c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
19667c478bd9Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER32)
19677c478bd9Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
19687c478bd9Sstevel@tonic-gate 	} else {
19697c478bd9Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER)
19707c478bd9Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 	if ((!pc_validcl(fsp, cn)) ||
19737c478bd9Sstevel@tonic-gate 	    (PCDL_IS_LFN(ep)) ||
19747c478bd9Sstevel@tonic-gate 	    (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) ||
19757c478bd9Sstevel@tonic-gate 	    ((ep->pcd_attr & PCA_LABEL) == PCA_LABEL)) {
19767c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
19777c478bd9Sstevel@tonic-gate 		brelse(bp);
19787c478bd9Sstevel@tonic-gate 		pc_unlockfs(fsp);
19797c478bd9Sstevel@tonic-gate 		return (EINVAL);
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate 	if ((ep->pcd_crtime.pct_time == pcfid->pcfid_ctime) &&
19827c478bd9Sstevel@tonic-gate 	    (ep->pcd_filename[0] != PCD_ERASED) &&
19837c478bd9Sstevel@tonic-gate 	    (pc_validchar(ep->pcd_filename[0]) ||
19847c478bd9Sstevel@tonic-gate 		(ep->pcd_filename[0] == '.' && ep->pcd_filename[1] == '.'))) {
19857c478bd9Sstevel@tonic-gate 		pcp = pc_getnode(fsp, eblkno, eoffset, ep);
19867c478bd9Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
19877c478bd9Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
19887c478bd9Sstevel@tonic-gate 	} else {
19897c478bd9Sstevel@tonic-gate 		*vpp = NULL;
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	bp->b_flags |= B_STALE | B_AGE;
19927c478bd9Sstevel@tonic-gate 	brelse(bp);
19937c478bd9Sstevel@tonic-gate 	pc_unlockfs(fsp);
19947c478bd9Sstevel@tonic-gate 	return (0);
19957c478bd9Sstevel@tonic-gate }
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate /*
199890c30842Sjmcp  * if device is a PCMCIA pseudo floppy, return 1
19997c478bd9Sstevel@tonic-gate  * otherwise, return 0
20007c478bd9Sstevel@tonic-gate  */
20017c478bd9Sstevel@tonic-gate static int
200290c30842Sjmcp pcfs_pseudo_floppy(dev_t rdev)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	int			error, err;
20057c478bd9Sstevel@tonic-gate 	struct dk_cinfo		info;
20067c478bd9Sstevel@tonic-gate 	ldi_handle_t		lh;
20077c478bd9Sstevel@tonic-gate 	ldi_ident_t		li;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	err = ldi_ident_from_mod(&modlinkage, &li);
20107c478bd9Sstevel@tonic-gate 	if (err) {
20117c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
201290c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_ident_from_mod err=%d\n", err);
20137c478bd9Sstevel@tonic-gate 		return (0);
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	err = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD, CRED(), &lh, li);
20177c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
20187c478bd9Sstevel@tonic-gate 	if (err) {
20197c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
202090c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_open err=%d\n", err);
20217c478bd9Sstevel@tonic-gate 		return (0);
20227c478bd9Sstevel@tonic-gate 	}
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	/* return value stored in err is purposfully ignored */
20257c478bd9Sstevel@tonic-gate 	error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
20267c478bd9Sstevel@tonic-gate 	    CRED(), &err);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	err = ldi_close(lh, FREAD, CRED());
20297c478bd9Sstevel@tonic-gate 	if (err != 0) {
20307c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(1,
203190c30842Sjmcp 		    "pcfs_pseudo_floppy: ldi_close err=%d\n", err);
20327c478bd9Sstevel@tonic-gate 		return (0);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if ((error == 0) && (info.dki_ctype == DKC_PCMCIA_MEM) &&
20367c478bd9Sstevel@tonic-gate 		(info.dki_flags & DKI_PCMCIA_PFD))
20377c478bd9Sstevel@tonic-gate 		return (1);
20387c478bd9Sstevel@tonic-gate 	else
20397c478bd9Sstevel@tonic-gate 		return (0);
20407c478bd9Sstevel@tonic-gate }
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate /*
20437c478bd9Sstevel@tonic-gate  * Unfortunately, FAT32 fat's can be pretty big (On a 1 gig jaz drive, about
20447c478bd9Sstevel@tonic-gate  * a meg), so we can't bread() it all in at once. This routine reads a
20457c478bd9Sstevel@tonic-gate  * fat a chunk at a time.
20467c478bd9Sstevel@tonic-gate  */
20477c478bd9Sstevel@tonic-gate static int
20487c478bd9Sstevel@tonic-gate pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start, size_t fatsize)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	struct buf *bp;
20517c478bd9Sstevel@tonic-gate 	size_t off;
20527c478bd9Sstevel@tonic-gate 	size_t readsize;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	readsize = fsp->pcfs_clsize;
20557c478bd9Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += readsize, fatp += readsize) {
20567c478bd9Sstevel@tonic-gate 		if (readsize > (fatsize - off))
20577c478bd9Sstevel@tonic-gate 			readsize = fatsize - off;
20587c478bd9Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
20597c478bd9Sstevel@tonic-gate 		    pc_dbdaddr(fsp, start +
20607c478bd9Sstevel@tonic-gate 			pc_cltodb(fsp, pc_lblkno(fsp, off))),
20617c478bd9Sstevel@tonic-gate 		    readsize);
20627c478bd9Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
20637c478bd9Sstevel@tonic-gate 			brelse(bp);
20647c478bd9Sstevel@tonic-gate 			return (EIO);
20657c478bd9Sstevel@tonic-gate 		}
20667c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
20677c478bd9Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, fatp, readsize);
20687c478bd9Sstevel@tonic-gate 		brelse(bp);
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 	return (0);
20717c478bd9Sstevel@tonic-gate }
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate /*
20747c478bd9Sstevel@tonic-gate  * We write the FAT out a _lot_, in order to make sure that it
20757c478bd9Sstevel@tonic-gate  * is up-to-date. But on a FAT32 system (large drive, small clusters)
20767c478bd9Sstevel@tonic-gate  * the FAT might be a couple of megabytes, and writing it all out just
20777c478bd9Sstevel@tonic-gate  * because we created or deleted a small file is painful (especially
20787c478bd9Sstevel@tonic-gate  * since we do it for each alternate FAT too). So instead, for FAT16 and
20797c478bd9Sstevel@tonic-gate  * FAT32 we only write out the bit that has changed. We don't clear
20807c478bd9Sstevel@tonic-gate  * the 'updated' fields here because the caller might be writing out
20817c478bd9Sstevel@tonic-gate  * several FATs, so the caller must use pc_clear_fatchanges() after
20827c478bd9Sstevel@tonic-gate  * all FATs have been updated.
20837c478bd9Sstevel@tonic-gate  */
20847c478bd9Sstevel@tonic-gate static int
20857c478bd9Sstevel@tonic-gate pc_writefat(struct pcfs *fsp, daddr_t start)
20867c478bd9Sstevel@tonic-gate {
20877c478bd9Sstevel@tonic-gate 	struct buf *bp;
20887c478bd9Sstevel@tonic-gate 	size_t off;
20897c478bd9Sstevel@tonic-gate 	size_t writesize;
20907c478bd9Sstevel@tonic-gate 	int	error;
20917c478bd9Sstevel@tonic-gate 	uchar_t *fatp = fsp->pcfs_fatp;
20927c478bd9Sstevel@tonic-gate 	size_t fatsize = fsp->pcfs_fatsize;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	writesize = fsp->pcfs_clsize;
20957c478bd9Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += writesize, fatp += writesize) {
20967c478bd9Sstevel@tonic-gate 		if (writesize > (fatsize - off))
20977c478bd9Sstevel@tonic-gate 			writesize = fatsize - off;
20987c478bd9Sstevel@tonic-gate 		if (!pc_fat_is_changed(fsp, pc_lblkno(fsp, off))) {
20997c478bd9Sstevel@tonic-gate 			continue;
21007c478bd9Sstevel@tonic-gate 		}
21017c478bd9Sstevel@tonic-gate 		bp = ngeteblk(writesize);
21027c478bd9Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
21037c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
21047c478bd9Sstevel@tonic-gate 		bp->b_blkno = pc_dbdaddr(fsp, start +
21057c478bd9Sstevel@tonic-gate 		    pc_cltodb(fsp, pc_lblkno(fsp, off)));
21067c478bd9Sstevel@tonic-gate 		bcopy(fatp, bp->b_un.b_addr, writesize);
21077c478bd9Sstevel@tonic-gate 		bwrite2(bp);
21087c478bd9Sstevel@tonic-gate 		error = geterror(bp);
21097c478bd9Sstevel@tonic-gate 		brelse(bp);
21107c478bd9Sstevel@tonic-gate 		if (error) {
21117c478bd9Sstevel@tonic-gate 			return (error);
21127c478bd9Sstevel@tonic-gate 		}
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 	return (0);
21157c478bd9Sstevel@tonic-gate }
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate  * Mark the FAT cluster that 'cn' is stored in as modified.
21197c478bd9Sstevel@tonic-gate  */
21207c478bd9Sstevel@tonic-gate void
21217c478bd9Sstevel@tonic-gate pc_mark_fat_updated(struct pcfs *fsp, pc_cluster32_t cn)
21227c478bd9Sstevel@tonic-gate {
21237c478bd9Sstevel@tonic-gate 	pc_cluster32_t	bn;
21247c478bd9Sstevel@tonic-gate 	size_t		size;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	/* which fat block is the cluster number stored in? */
21277c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
21287c478bd9Sstevel@tonic-gate 		size = sizeof (pc_cluster32_t);
21297c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21307c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21317c478bd9Sstevel@tonic-gate 	} else if (IS_FAT16(fsp)) {
21327c478bd9Sstevel@tonic-gate 		size = sizeof (pc_cluster16_t);
21337c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
21347c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21357c478bd9Sstevel@tonic-gate 	} else {
21367c478bd9Sstevel@tonic-gate 		offset_t off;
21377c478bd9Sstevel@tonic-gate 		pc_cluster32_t nbn;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		ASSERT(IS_FAT12(fsp));
21407c478bd9Sstevel@tonic-gate 		off = cn + (cn >> 1);
21417c478bd9Sstevel@tonic-gate 		bn = pc_lblkno(fsp, off);
21427c478bd9Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
21437c478bd9Sstevel@tonic-gate 		/* does this field wrap into the next fat cluster? */
21447c478bd9Sstevel@tonic-gate 		nbn = pc_lblkno(fsp, off + 1);
21457c478bd9Sstevel@tonic-gate 		if (nbn != bn) {
21467c478bd9Sstevel@tonic-gate 			fsp->pcfs_fat_changemap[nbn] = 1;
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate /*
21527c478bd9Sstevel@tonic-gate  * return whether the FAT cluster 'bn' is updated and needs to
21537c478bd9Sstevel@tonic-gate  * be written out.
21547c478bd9Sstevel@tonic-gate  */
21557c478bd9Sstevel@tonic-gate int
21567c478bd9Sstevel@tonic-gate pc_fat_is_changed(struct pcfs *fsp, pc_cluster32_t bn)
21577c478bd9Sstevel@tonic-gate {
21587c478bd9Sstevel@tonic-gate 	return (fsp->pcfs_fat_changemap[bn] == 1);
21597c478bd9Sstevel@tonic-gate }
2160