xref: /onnv-gate/usr/src/uts/common/fs/pcfs/pc_vfsops.c (revision 201:c63238553fc3)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/disp.h>
360Sstevel@tonic-gate #include <sys/buf.h>
370Sstevel@tonic-gate #include <sys/vfs.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/fdio.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/uio.h>
420Sstevel@tonic-gate #include <sys/conf.h>
430Sstevel@tonic-gate #undef NFSCLIENT
440Sstevel@tonic-gate #include <sys/statvfs.h>
450Sstevel@tonic-gate #include <sys/mount.h>
460Sstevel@tonic-gate #include <sys/pathname.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate #include <sys/conf.h>
510Sstevel@tonic-gate #include <sys/mkdev.h>
520Sstevel@tonic-gate #include <sys/swap.h>
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/sunldi.h>
550Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
560Sstevel@tonic-gate #include <sys/fs/pc_label.h>
570Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
580Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
590Sstevel@tonic-gate #include <sys/fs/pc_node.h>
600Sstevel@tonic-gate #include <fs/fs_subr.h>
610Sstevel@tonic-gate #include <sys/modctl.h>
620Sstevel@tonic-gate #include <sys/vol.h>
630Sstevel@tonic-gate #include <sys/dkio.h>
640Sstevel@tonic-gate #include <sys/open.h>
650Sstevel@tonic-gate #include <sys/mntent.h>
660Sstevel@tonic-gate #include <sys/policy.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * The majority of PC media use a 512 sector size, but
700Sstevel@tonic-gate  * occasionally you will run across a 1k sector size.
710Sstevel@tonic-gate  * For media with a 1k sector size, fd_strategy() requires
720Sstevel@tonic-gate  * the I/O size to be a 1k multiple; so when the sector size
730Sstevel@tonic-gate  * is not yet known, always read 1k.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate #define	PC_SAFESECSIZE	(PC_SECSIZE * 2)
760Sstevel@tonic-gate 
77*201Sjmcp static int pcfs_pseudo_floppy(dev_t);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static int pcfsinit(int, char *);
800Sstevel@tonic-gate static int pcfs_mount(struct vfs *, struct vnode *, struct mounta *,
810Sstevel@tonic-gate 	struct cred *);
820Sstevel@tonic-gate static int pcfs_unmount(struct vfs *, int, struct cred *);
830Sstevel@tonic-gate static int pcfs_root(struct vfs *, struct vnode **);
840Sstevel@tonic-gate static int pcfs_statvfs(struct vfs *, struct statvfs64 *);
850Sstevel@tonic-gate static int pc_syncfsnodes(struct pcfs *);
860Sstevel@tonic-gate static int pcfs_sync(struct vfs *, short, struct cred *);
870Sstevel@tonic-gate static int pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int pc_getfattype(struct vnode *, int, daddr_t *, int *);
900Sstevel@tonic-gate static int pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start,
910Sstevel@tonic-gate     size_t fatsize);
920Sstevel@tonic-gate static int pc_writefat(struct pcfs *fsp, daddr_t start);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * pcfs mount options table
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static char *nohidden_cancel[] = {MNTOPT_PCFS_HIDDEN, NULL};
990Sstevel@tonic-gate static char *hidden_cancel[] = {MNTOPT_PCFS_NOHIDDEN, NULL};
1000Sstevel@tonic-gate static char *nofoldcase_cancel[] = {MNTOPT_PCFS_FOLDCASE, NULL};
1010Sstevel@tonic-gate static char *foldcase_cancel[] = {MNTOPT_PCFS_NOFOLDCASE, NULL};
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static mntopt_t mntopts[] = {
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
1060Sstevel@tonic-gate  *		opt data
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate 	{ MNTOPT_PCFS_NOHIDDEN,	nohidden_cancel, NULL,		MO_DEFAULT,
1090Sstevel@tonic-gate 		NULL },
1100Sstevel@tonic-gate 	{ MNTOPT_PCFS_HIDDEN,	hidden_cancel, NULL,		0,
1110Sstevel@tonic-gate 		NULL },
1120Sstevel@tonic-gate 	{ MNTOPT_PCFS_NOFOLDCASE, nofoldcase_cancel, NULL,	MO_DEFAULT,
1130Sstevel@tonic-gate 		NULL },
1140Sstevel@tonic-gate 	{ MNTOPT_PCFS_FOLDCASE,	foldcase_cancel, NULL,		0,
1150Sstevel@tonic-gate 		NULL }
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static mntopts_t pcfs_mntopts = {
1190Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1200Sstevel@tonic-gate 	mntopts
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate int pcfsdebuglevel = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * pcfslock:	protects the list of mounted pc filesystems "pc_mounttab.
1270Sstevel@tonic-gate  * pcfs_lock:	(inside per filesystem structure "pcfs")
1280Sstevel@tonic-gate  *		per filesystem lock. Most of the vfsops and vnodeops are
1290Sstevel@tonic-gate  *		protected by this lock.
1300Sstevel@tonic-gate  * pcnodes_lock: protects the pcnode hash table "pcdhead", "pcfhead".
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  * Lock hierarchy: pcfslock > pcfs_lock > pcnodes_lock
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate kmutex_t	pcfslock;
1350Sstevel@tonic-gate krwlock_t pcnodes_lock; /* protect the pcnode hash table "pcdhead", "pcfhead" */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int pcfstype;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static vfsdef_t vfw = {
1400Sstevel@tonic-gate 	VFSDEF_VERSION,
1410Sstevel@tonic-gate 	"pcfs",
1420Sstevel@tonic-gate 	pcfsinit,
1430Sstevel@tonic-gate 	VSW_HASPROTO|VSW_CANREMOUNT,
1440Sstevel@tonic-gate 	&pcfs_mntopts
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static struct modlfs modlfs = {
1500Sstevel@tonic-gate 	&mod_fsops,
151*201Sjmcp 	"PC filesystem v%I%",
1520Sstevel@tonic-gate 	&vfw
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static struct modlinkage modlinkage = {
1560Sstevel@tonic-gate 	MODREV_1,
1570Sstevel@tonic-gate 	&modlfs,
1580Sstevel@tonic-gate 	NULL
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate int
1620Sstevel@tonic-gate _init(void)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	int	error;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #if !defined(lint)
1670Sstevel@tonic-gate 	/* make sure the on-disk structures are sane */
1680Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir) == 32);
1690Sstevel@tonic-gate 	ASSERT(sizeof (struct pcdir_lfn) == 32);
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate 	mutex_init(&pcfslock, NULL, MUTEX_DEFAULT, NULL);
1720Sstevel@tonic-gate 	rw_init(&pcnodes_lock, NULL, RW_DEFAULT, NULL);
1730Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1740Sstevel@tonic-gate 	if (error) {
1750Sstevel@tonic-gate 		mutex_destroy(&pcfslock);
1760Sstevel@tonic-gate 		rw_destroy(&pcnodes_lock);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	return (error);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate int
1820Sstevel@tonic-gate _fini(void)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	int	error;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1870Sstevel@tonic-gate 	if (error)
1880Sstevel@tonic-gate 		return (error);
1890Sstevel@tonic-gate 	mutex_destroy(&pcfslock);
1900Sstevel@tonic-gate 	rw_destroy(&pcnodes_lock);
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Tear down the operations vectors
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(pcfstype);
1950Sstevel@tonic-gate 	vn_freevnodeops(pcfs_fvnodeops);
1960Sstevel@tonic-gate 	vn_freevnodeops(pcfs_dvnodeops);
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
2010Sstevel@tonic-gate _info(struct modinfo *modinfop)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /* ARGSUSED1 */
2070Sstevel@tonic-gate static int
2080Sstevel@tonic-gate pcfsinit(int fstype, char *name)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	static const fs_operation_def_t pcfs_vfsops_template[] = {
2110Sstevel@tonic-gate 		VFSNAME_MOUNT, pcfs_mount,
2120Sstevel@tonic-gate 		VFSNAME_UNMOUNT, pcfs_unmount,
2130Sstevel@tonic-gate 		VFSNAME_ROOT, pcfs_root,
2140Sstevel@tonic-gate 		VFSNAME_STATVFS, pcfs_statvfs,
2150Sstevel@tonic-gate 		VFSNAME_SYNC, (fs_generic_func_p) pcfs_sync,
2160Sstevel@tonic-gate 		VFSNAME_VGET, pcfs_vget,
2170Sstevel@tonic-gate 		NULL, NULL
2180Sstevel@tonic-gate 	};
2190Sstevel@tonic-gate 	int error;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	error = vfs_setfsops(fstype, pcfs_vfsops_template, NULL);
2220Sstevel@tonic-gate 	if (error != 0) {
2230Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad vfs ops template");
2240Sstevel@tonic-gate 		return (error);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	error = vn_make_ops("pcfs", pcfs_fvnodeops_template, &pcfs_fvnodeops);
2280Sstevel@tonic-gate 	if (error != 0) {
2290Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2300Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad file vnode ops template");
2310Sstevel@tonic-gate 		return (error);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	error = vn_make_ops("pcfsd", pcfs_dvnodeops_template, &pcfs_dvnodeops);
2350Sstevel@tonic-gate 	if (error != 0) {
2360Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2370Sstevel@tonic-gate 		vn_freevnodeops(pcfs_fvnodeops);
2380Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcfsinit: bad dir vnode ops template");
2390Sstevel@tonic-gate 		return (error);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	pcfstype = fstype;
2430Sstevel@tonic-gate 	(void) pc_init();
2440Sstevel@tonic-gate 	return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static struct pcfs *pc_mounttab = NULL;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate extern struct pcfs_args pc_tz;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  *  Define some special logical drives we use internal to this file.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate #define	BOOT_PARTITION_DRIVE	99
2550Sstevel@tonic-gate #define	PRIMARY_DOS_DRIVE	1
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * pc_mount system call
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate static int
2610Sstevel@tonic-gate pcfs_mount(
2620Sstevel@tonic-gate 	struct vfs *vfsp,
2630Sstevel@tonic-gate 	struct vnode *mvp,
2640Sstevel@tonic-gate 	struct mounta *uap,
2650Sstevel@tonic-gate 	struct cred *cr)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	struct pcfs *fsp;
2680Sstevel@tonic-gate 	struct vnode *bvp;
2690Sstevel@tonic-gate 	struct vnode *devvp;
2700Sstevel@tonic-gate 	struct pathname special;
2710Sstevel@tonic-gate 	daddr_t dosstart;
2720Sstevel@tonic-gate 	dev_t pseudodev;
2730Sstevel@tonic-gate 	dev_t xdev;
2740Sstevel@tonic-gate 	char *spnp;
2750Sstevel@tonic-gate 	char *data = uap->dataptr;
2760Sstevel@tonic-gate 	int datalen = uap->datalen;
2770Sstevel@tonic-gate 	int dos_ldrive = 0;
2780Sstevel@tonic-gate 	int error;
2790Sstevel@tonic-gate 	int fattype;
2800Sstevel@tonic-gate 	int spnlen;
2810Sstevel@tonic-gate 	int wantbootpart = 0;
2820Sstevel@tonic-gate 	struct vioc_info info;
2830Sstevel@tonic-gate 	int rval;		/* set but not used */
2840Sstevel@tonic-gate 	minor_t	minor;
2850Sstevel@tonic-gate 	int oflag, aflag;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
2880Sstevel@tonic-gate 		return (error);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_mount\n");
2910Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
2920Sstevel@tonic-gate 		return (ENOTDIR);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2950Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2960Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
2970Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2980Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2990Sstevel@tonic-gate 		return (EBUSY);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * The caller is responsible for making sure to always
3050Sstevel@tonic-gate 	 * pass in sizeof(struct pcfs_args) (or the old one).
3060Sstevel@tonic-gate 	 * Doing this is the only way to know an EINVAL return
3070Sstevel@tonic-gate 	 * from mount(2) is due to the "not a DOS filesystem"
3080Sstevel@tonic-gate 	 * EINVAL that pc_verify/pc_getfattype could return.
3090Sstevel@tonic-gate 	 */
3100Sstevel@tonic-gate 	if ((datalen != sizeof (struct pcfs_args)) &&
3110Sstevel@tonic-gate 	    (datalen != sizeof (struct old_pcfs_args))) {
3120Sstevel@tonic-gate 		return (EINVAL);
3130Sstevel@tonic-gate 	} else {
3140Sstevel@tonic-gate 		struct pcfs_args tmp_tz;
3150Sstevel@tonic-gate 		int hidden = 0;
3160Sstevel@tonic-gate 		int foldcase = 0;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		tmp_tz.flags = 0;
3190Sstevel@tonic-gate 		if (copyin(data, &tmp_tz, datalen)) {
3200Sstevel@tonic-gate 			return (EFAULT);
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 		if (datalen == sizeof (struct pcfs_args)) {
3230Sstevel@tonic-gate 			hidden = tmp_tz.flags & PCFS_MNT_HIDDEN;
3240Sstevel@tonic-gate 			foldcase = tmp_tz.flags & PCFS_MNT_FOLDCASE;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		if (hidden)
3280Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_HIDDEN,	NULL, 0);
3290Sstevel@tonic-gate 		if (foldcase)
3300Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_PCFS_FOLDCASE, NULL, 0);
3310Sstevel@tonic-gate 		/*
3320Sstevel@tonic-gate 		 * more than one pc filesystem can be mounted on x86
3330Sstevel@tonic-gate 		 * so the pc_tz structure is now a critical region
3340Sstevel@tonic-gate 		 */
3350Sstevel@tonic-gate 		mutex_enter(&pcfslock);
3360Sstevel@tonic-gate 		if (pc_mounttab == NULL)
3370Sstevel@tonic-gate 			bcopy(&tmp_tz, &pc_tz, sizeof (struct pcfs_args));
3380Sstevel@tonic-gate 		mutex_exit(&pcfslock);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	if (error = pn_get(uap->spec, UIO_USERSPACE, &special)) {
3440Sstevel@tonic-gate 		return (error);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	if (error =
3470Sstevel@tonic-gate 	    lookupname(special.pn_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp)) {
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * look for suffix to special
3500Sstevel@tonic-gate 		 * which indicates a request to mount the solaris boot
3510Sstevel@tonic-gate 		 * partition, or a DOS logical drive on the hard disk
3520Sstevel@tonic-gate 		 */
3530Sstevel@tonic-gate 		spnlen = special.pn_pathlen;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		if (spnlen > 5) {
3560Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 5;
3570Sstevel@tonic-gate 			if (*spnp++ == ':' && *spnp++ == 'b' &&
3580Sstevel@tonic-gate 			    *spnp++ == 'o' && *spnp++ == 'o' &&
3590Sstevel@tonic-gate 			    *spnp++ == 't') {
3600Sstevel@tonic-gate 				/*
3610Sstevel@tonic-gate 				 * Looks as if they want to mount
3620Sstevel@tonic-gate 				 * the Solaris boot partition
3630Sstevel@tonic-gate 				 */
3640Sstevel@tonic-gate 				wantbootpart = 1;
3650Sstevel@tonic-gate 				dos_ldrive = BOOT_PARTITION_DRIVE;
3660Sstevel@tonic-gate 				spnp = special.pn_path + spnlen - 5;
3670Sstevel@tonic-gate 				*spnp = '\0';
3680Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3690Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3700Sstevel@tonic-gate 			}
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		if (!wantbootpart) {
3740Sstevel@tonic-gate 			spnp = special.pn_path + spnlen - 1;
3750Sstevel@tonic-gate 			if (spnlen > 2 && *spnp >= 'c' && *spnp <= 'z') {
3760Sstevel@tonic-gate 				spnlen--;
3770Sstevel@tonic-gate 				dos_ldrive = *spnp-- - 'c' + 1;
3780Sstevel@tonic-gate 			} else if (spnlen > 2 && *spnp >= '0' && *spnp <= '9') {
3790Sstevel@tonic-gate 				spnlen--;
3800Sstevel@tonic-gate 				dos_ldrive = *spnp-- - '0';
3810Sstevel@tonic-gate 				if (spnlen > 2 && *spnp >= '0' &&
3820Sstevel@tonic-gate 				    *spnp <= '9') {
3830Sstevel@tonic-gate 					spnlen--;
3840Sstevel@tonic-gate 					dos_ldrive += 10 * (*spnp-- - '0');
3850Sstevel@tonic-gate 				}
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 			if (spnlen > 1 && dos_ldrive && dos_ldrive <= 24 &&
3880Sstevel@tonic-gate 			    *spnp == ':') {
3890Sstevel@tonic-gate 				/*
3900Sstevel@tonic-gate 				 * remove suffix so that we have a real
3910Sstevel@tonic-gate 				 * device name
3920Sstevel@tonic-gate 				 */
3930Sstevel@tonic-gate 				*spnp = '\0';
3940Sstevel@tonic-gate 				error = lookupname(special.pn_path,
3950Sstevel@tonic-gate 				    UIO_SYSSPACE, FOLLOW, NULLVPP, &bvp);
3960Sstevel@tonic-gate 			}
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		if (error) {
3990Sstevel@tonic-gate 			pn_free(&special);
4000Sstevel@tonic-gate 			return (error);
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 	pn_free(&special);
4040Sstevel@tonic-gate 	if (bvp->v_type != VBLK) {
4050Sstevel@tonic-gate 		VN_RELE(bvp);
4060Sstevel@tonic-gate 		return (ENOTBLK);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 	xdev = bvp->v_rdev;
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Verify caller's permission to open the device special file.
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
4130Sstevel@tonic-gate 	    ((uap->flags & MS_RDONLY) != 0)) {
4140Sstevel@tonic-gate 		oflag = FREAD;
4150Sstevel@tonic-gate 		aflag = VREAD;
4160Sstevel@tonic-gate 	} else {
4170Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
4180Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	if ((error = VOP_ACCESS(bvp, aflag, 0, cr)) != 0 ||
4210Sstevel@tonic-gate 	    (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
4220Sstevel@tonic-gate 		VN_RELE(bvp);
4230Sstevel@tonic-gate 		return (error);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	VN_RELE(bvp);
4270Sstevel@tonic-gate 	if (getmajor(xdev) >= devcnt) {
4280Sstevel@tonic-gate 		return (ENXIO);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * Ensure that this device (or logical drive) isn't already mounted,
4320Sstevel@tonic-gate 	 * unless this is a REMOUNT request
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	if (dos_ldrive) {
4350Sstevel@tonic-gate 		mutex_enter(&pcfslock);
4360Sstevel@tonic-gate 		for (fsp = pc_mounttab; fsp; fsp = fsp->pcfs_nxt)
4370Sstevel@tonic-gate 			if (fsp->pcfs_xdev == xdev &&
4380Sstevel@tonic-gate 			    fsp->pcfs_ldrv == dos_ldrive) {
4390Sstevel@tonic-gate 				mutex_exit(&pcfslock);
4400Sstevel@tonic-gate 				if (uap->flags & MS_REMOUNT) {
4410Sstevel@tonic-gate 					return (0);
4420Sstevel@tonic-gate 				} else {
4430Sstevel@tonic-gate 					return (EBUSY);
4440Sstevel@tonic-gate 				}
4450Sstevel@tonic-gate 			}
4460Sstevel@tonic-gate 		/*
4470Sstevel@tonic-gate 		 * Assign a unique device number for the vfs
4480Sstevel@tonic-gate 		 * The old way (getudev() + a constantly incrementing
4490Sstevel@tonic-gate 		 * major number) was wrong because it changes vfs_dev
4500Sstevel@tonic-gate 		 * across mounts and reboots, which breaks nfs file handles.
4510Sstevel@tonic-gate 		 * UFS just uses the real dev_t. We can't do that because
4520Sstevel@tonic-gate 		 * of the way pcfs opens fdisk partitons (the :c and :d
4530Sstevel@tonic-gate 		 * partitions are on the same dev_t). Though that _might_
4540Sstevel@tonic-gate 		 * actually be ok, since the file handle contains an
4550Sstevel@tonic-gate 		 * absolute block number, it's probably better to make them
4560Sstevel@tonic-gate 		 * different. So I think we should retain the original
4570Sstevel@tonic-gate 		 * dev_t, but come up with a different minor number based
4580Sstevel@tonic-gate 		 * on the logical drive that will _always_ come up the same.
4590Sstevel@tonic-gate 		 * For now, we steal the upper 6 bits.
4600Sstevel@tonic-gate 		 */
4610Sstevel@tonic-gate #ifdef notdef
4620Sstevel@tonic-gate 		/* what should we do here? */
4630Sstevel@tonic-gate 		if (((getminor(xdev) >> 12) & 0x3F) != 0)
4640Sstevel@tonic-gate 			printf("whoops - upper bits used!\n");
4650Sstevel@tonic-gate #endif
4660Sstevel@tonic-gate 		minor = ((dos_ldrive << 12) | getminor(xdev)) & MAXMIN32;
4670Sstevel@tonic-gate 		pseudodev = makedevice(getmajor(xdev), minor);
4680Sstevel@tonic-gate 		if (vfs_devmounting(pseudodev, vfsp)) {
4690Sstevel@tonic-gate 			mutex_exit(&pcfslock);
4700Sstevel@tonic-gate 			return (EBUSY);
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 		if (vfs_devismounted(pseudodev)) {
4730Sstevel@tonic-gate 			mutex_exit(&pcfslock);
4740Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
4750Sstevel@tonic-gate 				return (0);
4760Sstevel@tonic-gate 			} else {
4770Sstevel@tonic-gate 				return (EBUSY);
4780Sstevel@tonic-gate 			}
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		mutex_exit(&pcfslock);
4810Sstevel@tonic-gate 	} else {
4820Sstevel@tonic-gate 		if (vfs_devmounting(xdev, vfsp)) {
4830Sstevel@tonic-gate 			return (EBUSY);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 		if (vfs_devismounted(xdev))
4860Sstevel@tonic-gate 			if (uap->flags & MS_REMOUNT) {
4870Sstevel@tonic-gate 				return (0);
4880Sstevel@tonic-gate 			} else {
4890Sstevel@tonic-gate 				return (EBUSY);
4900Sstevel@tonic-gate 			}
4910Sstevel@tonic-gate 		pseudodev = xdev;
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
4950Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4960Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * Mount the filesystem
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 	devvp = makespecvp(xdev, VBLK);
5030Sstevel@tonic-gate 	if (IS_SWAPVP(devvp)) {
5040Sstevel@tonic-gate 		VN_RELE(devvp);
5050Sstevel@tonic-gate 		return (EBUSY);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * special handling for PCMCIA memory card
510*201Sjmcp 	 * with pseudo floppies organization
5110Sstevel@tonic-gate 	 */
512*201Sjmcp 	if (dos_ldrive == 0 && pcfs_pseudo_floppy(xdev)) {
5130Sstevel@tonic-gate 		dosstart = (daddr_t)0;
5140Sstevel@tonic-gate 		fattype = PCFS_PCMCIA_NO_CIS;
5150Sstevel@tonic-gate 	} else {
5160Sstevel@tonic-gate 		if (error = pc_getfattype(devvp, dos_ldrive, &dosstart,
5170Sstevel@tonic-gate 		    &fattype)) {
5180Sstevel@tonic-gate 			VN_RELE(devvp);
5190Sstevel@tonic-gate 			return (error);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0, B_INVAL, cr);
5240Sstevel@tonic-gate 	fsp = kmem_zalloc((uint_t)sizeof (struct pcfs), KM_SLEEP);
5250Sstevel@tonic-gate 	fsp->pcfs_vfs = vfsp;
5260Sstevel@tonic-gate 	fsp->pcfs_flags = fattype;
5270Sstevel@tonic-gate 	fsp->pcfs_devvp = devvp;
5280Sstevel@tonic-gate 	fsp->pcfs_xdev = xdev;
5290Sstevel@tonic-gate 	fsp->pcfs_ldrv = dos_ldrive;
5300Sstevel@tonic-gate 	fsp->pcfs_dosstart = dosstart;
5310Sstevel@tonic-gate 	mutex_init(&fsp->pcfs_lock, NULL, MUTEX_DEFAULT, NULL);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* set the "nocheck" flag if volmgt is managing this volume */
5340Sstevel@tonic-gate 	info.vii_pathlen = 0;
5350Sstevel@tonic-gate 	info.vii_devpath = 0;
5360Sstevel@tonic-gate 	error = cdev_ioctl(fsp->pcfs_xdev, VOLIOCINFO, (intptr_t)&info,
5370Sstevel@tonic-gate 	    FKIOCTL|FREAD, kcred, &rval);
5380Sstevel@tonic-gate 	if (error == 0) {
5390Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_NOCHK;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_HIDDEN, NULL))
5430Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_HIDDEN;
5440Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_PCFS_FOLDCASE, NULL))
5450Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_FOLDCASE;
5460Sstevel@tonic-gate 	vfsp->vfs_dev = pseudodev;
5470Sstevel@tonic-gate 	vfsp->vfs_fstype = pcfstype;
5480Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, pseudodev, pcfstype);
5490Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)fsp;
5500Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	error = pc_verify(fsp);
5530Sstevel@tonic-gate 	if (error) {
5540Sstevel@tonic-gate 		VN_RELE(devvp);
5550Sstevel@tonic-gate 		mutex_destroy(&fsp->pcfs_lock);
5560Sstevel@tonic-gate 		kmem_free(fsp, (uint_t)sizeof (struct pcfs));
5570Sstevel@tonic-gate 		return (error);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->pcfs_clsize;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	mutex_enter(&pcfslock);
5620Sstevel@tonic-gate 	fsp->pcfs_nxt = pc_mounttab;
5630Sstevel@tonic-gate 	pc_mounttab = fsp;
5640Sstevel@tonic-gate 	mutex_exit(&pcfslock);
5650Sstevel@tonic-gate 	return (0);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate  * vfs operations
5700Sstevel@tonic-gate  */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate /* ARGSUSED */
5730Sstevel@tonic-gate static int
5740Sstevel@tonic-gate pcfs_unmount(
5750Sstevel@tonic-gate 	struct vfs *vfsp,
5760Sstevel@tonic-gate 	int flag,
5770Sstevel@tonic-gate 	struct cred *cr)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	struct pcfs *fsp, *fsp1;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
5820Sstevel@tonic-gate 		return (EPERM);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*
5850Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
5860Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
5870Sstevel@tonic-gate 	 */
5880Sstevel@tonic-gate 	if (flag & MS_FORCE)
5890Sstevel@tonic-gate 		return (ENOTSUP);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	PC_DPRINTF0(4, "pcfs_unmount\n");
5920Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * We don't have to lock fsp because the VVFSLOCK in vfs layer will
5950Sstevel@tonic-gate 	 * prevent lookuppn from crossing the mount point.
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	if (fsp->pcfs_nrefs) {
5980Sstevel@tonic-gate 		return (EBUSY);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * Allow an unmount (regardless of state) if the fs instance has
6030Sstevel@tonic-gate 	 * been marked as beyond recovery.
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_IRRECOV) {
6060Sstevel@tonic-gate 		mutex_enter(&pcfslock);
6070Sstevel@tonic-gate 		rw_enter(&pcnodes_lock, RW_WRITER);
6080Sstevel@tonic-gate 		pc_diskchanged(fsp);
6090Sstevel@tonic-gate 		rw_exit(&pcnodes_lock);
6100Sstevel@tonic-gate 		mutex_exit(&pcfslock);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/* now there should be no pcp node on pcfhead or pcdhead. */
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	mutex_enter(&pcfslock);
6160Sstevel@tonic-gate 	if (fsp == pc_mounttab) {
6170Sstevel@tonic-gate 		pc_mounttab = fsp->pcfs_nxt;
6180Sstevel@tonic-gate 	} else {
6190Sstevel@tonic-gate 		for (fsp1 = pc_mounttab; fsp1 != NULL; fsp1 = fsp1->pcfs_nxt)
6200Sstevel@tonic-gate 			if (fsp1->pcfs_nxt == fsp)
6210Sstevel@tonic-gate 				fsp1->pcfs_nxt = fsp->pcfs_nxt;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (fsp->pcfs_fatp != (uchar_t *)0) {
6250Sstevel@tonic-gate 		pc_invalfat(fsp);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	mutex_exit(&pcfslock);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	VN_RELE(fsp->pcfs_devvp);
6300Sstevel@tonic-gate 	mutex_destroy(&fsp->pcfs_lock);
6310Sstevel@tonic-gate 	kmem_free(fsp, (uint_t)sizeof (struct pcfs));
6320Sstevel@tonic-gate 	return (0);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /*
6360Sstevel@tonic-gate  * find root of pcfs
6370Sstevel@tonic-gate  */
6380Sstevel@tonic-gate static int
6390Sstevel@tonic-gate pcfs_root(
6400Sstevel@tonic-gate 	struct vfs *vfsp,
6410Sstevel@tonic-gate 	struct vnode **vpp)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	struct pcfs *fsp;
6440Sstevel@tonic-gate 	struct pcnode *pcp;
6450Sstevel@tonic-gate 	int error;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6480Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
6490Sstevel@tonic-gate 		return (error);
6500Sstevel@tonic-gate 	pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
6510Sstevel@tonic-gate 	PC_DPRINTF2(9, "pcfs_root(0x%p) pcp= 0x%p\n",
6520Sstevel@tonic-gate 	    (void *)vfsp, (void *)pcp);
6530Sstevel@tonic-gate 	pc_unlockfs(fsp);
6540Sstevel@tonic-gate 	*vpp = PCTOV(pcp);
6550Sstevel@tonic-gate 	pcp->pc_flags |= PC_EXTERNAL;
6560Sstevel@tonic-gate 	return (0);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Get file system statistics.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static int
6630Sstevel@tonic-gate pcfs_statvfs(
6640Sstevel@tonic-gate 	struct vfs *vfsp,
6650Sstevel@tonic-gate 	struct statvfs64 *sp)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	struct pcfs *fsp;
6680Sstevel@tonic-gate 	int error;
6690Sstevel@tonic-gate 	dev32_t d32;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
6720Sstevel@tonic-gate 	error = pc_getfat(fsp);
6730Sstevel@tonic-gate 	if (error)
6740Sstevel@tonic-gate 		return (error);
6750Sstevel@tonic-gate 	bzero(sp, sizeof (*sp));
6760Sstevel@tonic-gate 	sp->f_bsize = sp->f_frsize = fsp->pcfs_clsize;
6770Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->pcfs_ncluster;
6780Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree = (fsblkcnt64_t)pc_freeclusters(fsp);
6790Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)-1;
6800Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)-1;
6810Sstevel@tonic-gate 	sp->f_favail = (fsfilcnt64_t)-1;
6820Sstevel@tonic-gate #ifdef notdef
6830Sstevel@tonic-gate 	(void) cmpldev(&d32, fsp->pcfs_devvp->v_rdev);
6840Sstevel@tonic-gate #endif /* notdef */
6850Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
6860Sstevel@tonic-gate 	sp->f_fsid = d32;
6870Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
6880Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
6890Sstevel@tonic-gate 	sp->f_namemax = PCFNAMESIZE;
6900Sstevel@tonic-gate 	return (0);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate static int
6940Sstevel@tonic-gate pc_syncfsnodes(struct pcfs *fsp)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	struct pchead *hp;
6970Sstevel@tonic-gate 	struct pcnode *pcp;
6980Sstevel@tonic-gate 	int error;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfsnodes\n");
7010Sstevel@tonic-gate 	if (error = pc_lockfs(fsp, 0, 0))
7020Sstevel@tonic-gate 		return (error);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	if (!(error = pc_syncfat(fsp))) {
7050Sstevel@tonic-gate 		hp = pcfhead;
7060Sstevel@tonic-gate 		while (hp < & pcfhead [ NPCHASH ]) {
7070Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_READER);
7080Sstevel@tonic-gate 			pcp = hp->pch_forw;
7090Sstevel@tonic-gate 			while (pcp != (struct pcnode *)hp) {
7100Sstevel@tonic-gate 				if (VFSTOPCFS(PCTOV(pcp) -> v_vfsp) == fsp)
7110Sstevel@tonic-gate 					if (error = pc_nodesync(pcp))
7120Sstevel@tonic-gate 						break;
7130Sstevel@tonic-gate 				pcp = pcp -> pc_forw;
7140Sstevel@tonic-gate 			}
7150Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7160Sstevel@tonic-gate 			if (error)
7170Sstevel@tonic-gate 				break;
7180Sstevel@tonic-gate 			hp++;
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	pc_unlockfs(fsp);
7220Sstevel@tonic-gate 	return (error);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Flush any pending I/O.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate /*ARGSUSED*/
7290Sstevel@tonic-gate static int
7300Sstevel@tonic-gate pcfs_sync(
7310Sstevel@tonic-gate 	struct vfs *vfsp,
7320Sstevel@tonic-gate 	short flag,
7330Sstevel@tonic-gate 	struct cred *cr)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	struct pcfs *fsp;
7360Sstevel@tonic-gate 	int error = 0;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/* this prevents the filesystem from being umounted. */
7390Sstevel@tonic-gate 	mutex_enter(&pcfslock);
7400Sstevel@tonic-gate 	if (vfsp != NULL) {
7410Sstevel@tonic-gate 		fsp = VFSTOPCFS(vfsp);
7420Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_IRRECOV)) {
7430Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7440Sstevel@tonic-gate 		} else {
7450Sstevel@tonic-gate 			rw_enter(&pcnodes_lock, RW_WRITER);
7460Sstevel@tonic-gate 			pc_diskchanged(fsp);
7470Sstevel@tonic-gate 			rw_exit(&pcnodes_lock);
7480Sstevel@tonic-gate 			error = EIO;
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 	} else {
7510Sstevel@tonic-gate 		fsp = pc_mounttab;
7520Sstevel@tonic-gate 		while (fsp != NULL) {
7530Sstevel@tonic-gate 			if (fsp->pcfs_flags & PCFS_IRRECOV) {
7540Sstevel@tonic-gate 				rw_enter(&pcnodes_lock, RW_WRITER);
7550Sstevel@tonic-gate 				pc_diskchanged(fsp);
7560Sstevel@tonic-gate 				rw_exit(&pcnodes_lock);
7570Sstevel@tonic-gate 				error = EIO;
7580Sstevel@tonic-gate 				break;
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 			error = pc_syncfsnodes(fsp);
7610Sstevel@tonic-gate 			if (error) break;
7620Sstevel@tonic-gate 			fsp = fsp->pcfs_nxt;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	mutex_exit(&pcfslock);
7660Sstevel@tonic-gate 	return (error);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate int
7700Sstevel@tonic-gate pc_lockfs(struct pcfs *fsp, int diskchanged, int releasing)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_IRRECOV) && !releasing)
7730Sstevel@tonic-gate 		return (EIO);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) && (fsp->pcfs_owner == curthread)) {
7760Sstevel@tonic-gate 		fsp->pcfs_count++;
7770Sstevel@tonic-gate 	} else {
7780Sstevel@tonic-gate 		mutex_enter(&fsp->pcfs_lock);
7790Sstevel@tonic-gate 		if (fsp->pcfs_flags & PCFS_LOCKED)
7800Sstevel@tonic-gate 			panic("pc_lockfs");
7810Sstevel@tonic-gate 		/*
7820Sstevel@tonic-gate 		 * We check the IRRECOV bit again just in case somebody
7830Sstevel@tonic-gate 		 * snuck past the initial check but then got held up before
7840Sstevel@tonic-gate 		 * they could grab the lock.  (And in the meantime someone
7850Sstevel@tonic-gate 		 * had grabbed the lock and set the bit)
7860Sstevel@tonic-gate 		 */
7870Sstevel@tonic-gate 		if (!diskchanged && !(fsp->pcfs_flags & PCFS_IRRECOV))
7880Sstevel@tonic-gate 			(void) pc_getfat(fsp);
7890Sstevel@tonic-gate 		fsp->pcfs_flags |= PCFS_LOCKED;
7900Sstevel@tonic-gate 		fsp->pcfs_owner = curthread;
7910Sstevel@tonic-gate 		fsp->pcfs_count++;
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	return (0);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate void
7970Sstevel@tonic-gate pc_unlockfs(struct pcfs *fsp)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_LOCKED) == 0)
8010Sstevel@tonic-gate 		panic("pc_unlockfs");
8020Sstevel@tonic-gate 	if (--fsp->pcfs_count < 0)
8030Sstevel@tonic-gate 		panic("pc_unlockfs: count");
8040Sstevel@tonic-gate 	if (fsp->pcfs_count == 0) {
8050Sstevel@tonic-gate 		fsp->pcfs_flags &= ~PCFS_LOCKED;
8060Sstevel@tonic-gate 		fsp->pcfs_owner = 0;
8070Sstevel@tonic-gate 		mutex_exit(&fsp->pcfs_lock);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * isDosDrive()
8130Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8140Sstevel@tonic-gate  *	and it decides if that's a systid that describes a DOS drive.  We
8150Sstevel@tonic-gate  *	use systid values defined in sys/dktp/fdisk.h.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate static int
8180Sstevel@tonic-gate isDosDrive(uchar_t checkMe)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
8210Sstevel@tonic-gate 	    (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
8220Sstevel@tonic-gate 	    (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
8230Sstevel@tonic-gate 	    (checkMe == DIAGPART));
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate  * isDosExtended()
8280Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8290Sstevel@tonic-gate  *	and it decides if that's a systid that describes an extended DOS
8300Sstevel@tonic-gate  *	partition.
8310Sstevel@tonic-gate  */
8320Sstevel@tonic-gate static int
8330Sstevel@tonic-gate isDosExtended(uchar_t checkMe)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate  * isBootPart()
8400Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
8410Sstevel@tonic-gate  *	and it decides if that's a systid that describes a Solaris boot
8420Sstevel@tonic-gate  *	partition.
8430Sstevel@tonic-gate  */
8440Sstevel@tonic-gate static int
8450Sstevel@tonic-gate isBootPart(uchar_t checkMe)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate 	return (checkMe == X86BOOT);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate  * noLogicalDrive()
8520Sstevel@tonic-gate  *	Display error message about not being able to find a logical
8530Sstevel@tonic-gate  *	drive.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate static void
8560Sstevel@tonic-gate noLogicalDrive(int requested)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	if (requested == BOOT_PARTITION_DRIVE) {
8590Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no boot partition");
8600Sstevel@tonic-gate 	} else {
8610Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no such logical drive");
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * findTheDrive()
8670Sstevel@tonic-gate  *	Discover offset of the requested logical drive, and return
8680Sstevel@tonic-gate  *	that offset (startSector), the systid of that drive (sysid),
8690Sstevel@tonic-gate  *	and a buffer pointer (bp), with the buffer contents being
8700Sstevel@tonic-gate  *	the first sector of the logical drive (i.e., the sector that
8710Sstevel@tonic-gate  *	contains the BPB for that drive).
8720Sstevel@tonic-gate  */
8730Sstevel@tonic-gate static int
8740Sstevel@tonic-gate findTheDrive(dev_t dev, int askedFor, int *error, buf_t **bp,
8750Sstevel@tonic-gate     daddr_t *startSector, uchar_t *sysid)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 	struct ipart dosp[FD_NUMPART];	/* incore fdisk partition structure */
8780Sstevel@tonic-gate 	struct mboot *dosp_ptr;		/* boot structure pointer */
8790Sstevel@tonic-gate 	daddr_t lastseek = 0;		/* Disk block we sought previously */
8800Sstevel@tonic-gate 	daddr_t diskblk = 0;		/* Disk block to get */
8810Sstevel@tonic-gate 	daddr_t xstartsect;		/* base of Extended DOS partition */
8820Sstevel@tonic-gate 	int logicalDriveCount = 0;	/* Count of logical drives seen */
8830Sstevel@tonic-gate 	int extendedPart = -1;		/* index of extended dos partition */
8840Sstevel@tonic-gate 	int primaryPart = -1;		/* index of primary dos partition */
8850Sstevel@tonic-gate 	int bootPart = -1;		/* index of a Solaris boot partition */
8860Sstevel@tonic-gate 	int xnumsect = -1;		/* length of extended DOS partition */
8870Sstevel@tonic-gate 	int driveIndex;			/* computed FDISK table index */
8880Sstevel@tonic-gate 	int i;
8890Sstevel@tonic-gate 	/*
8900Sstevel@tonic-gate 	 * Count of drives in the current extended partition's
8910Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
8920Sstevel@tonic-gate 	 */
8930Sstevel@tonic-gate 	int extndDrives[FD_NUMPART];
8940Sstevel@tonic-gate 	int numDrives = 0;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * Count of drives (beyond primary) in master boot record's
8980Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	int extraDrives[FD_NUMPART];
9010Sstevel@tonic-gate 	int numExtraDrives = 0;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 *  Copy from disk block into memory aligned structure for fdisk usage.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
9070Sstevel@tonic-gate 	bcopy(dosp_ptr->parts, dosp, sizeof (struct ipart) * FD_NUMPART);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/*
9100Sstevel@tonic-gate 	 * Get a summary of what is in the Master FDISK table.
9110Sstevel@tonic-gate 	 * Normally we expect to find one partition marked as a DOS drive.
9120Sstevel@tonic-gate 	 * This partition is the one Windows calls the primary dos partition.
9130Sstevel@tonic-gate 	 * If the machine has any logical drives then we also expect
9140Sstevel@tonic-gate 	 * to find a partition marked as an extended DOS partition.
9150Sstevel@tonic-gate 	 *
9160Sstevel@tonic-gate 	 * Sometimes we'll find multiple partitions marked as DOS drives.
9170Sstevel@tonic-gate 	 * The Solaris fdisk program allows these partitions
9180Sstevel@tonic-gate 	 * to be created, but Windows fdisk no longer does.  We still need
9190Sstevel@tonic-gate 	 * to support these, though, since Windows does.  We also need to fix
9200Sstevel@tonic-gate 	 * our fdisk to behave like the Windows version.
9210Sstevel@tonic-gate 	 *
9220Sstevel@tonic-gate 	 * It turns out that some off-the-shelf media have *only* an
9230Sstevel@tonic-gate 	 * Extended partition, so we need to deal with that case as well.
9240Sstevel@tonic-gate 	 *
9250Sstevel@tonic-gate 	 * Only a single (the first) Extended or Boot Partition will
9260Sstevel@tonic-gate 	 * be recognized.  Any others will be ignored.
9270Sstevel@tonic-gate 	 */
9280Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
9290Sstevel@tonic-gate 		if (isDosDrive(dosp[i].systid)) {
9300Sstevel@tonic-gate 			if (primaryPart < 0) {
9310Sstevel@tonic-gate 				logicalDriveCount++;
9320Sstevel@tonic-gate 				primaryPart = i;
9330Sstevel@tonic-gate 			} else {
9340Sstevel@tonic-gate 				extraDrives[numExtraDrives++] = i;
9350Sstevel@tonic-gate 			}
9360Sstevel@tonic-gate 			continue;
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 		if ((extendedPart < 0) && isDosExtended(dosp[i].systid)) {
9390Sstevel@tonic-gate 			extendedPart = i;
9400Sstevel@tonic-gate 			continue;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 		if ((bootPart < 0) && isBootPart(dosp[i].systid)) {
9430Sstevel@tonic-gate 			bootPart = i;
9440Sstevel@tonic-gate 			continue;
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (askedFor == BOOT_PARTITION_DRIVE) {
9490Sstevel@tonic-gate 		if (bootPart < 0) {
9500Sstevel@tonic-gate 			noLogicalDrive(askedFor);
9510Sstevel@tonic-gate 			*error = EINVAL;
9520Sstevel@tonic-gate 			return (0);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		*sysid = dosp[bootPart].systid;
9550Sstevel@tonic-gate 		*startSector = ltohi(dosp[bootPart].relsect);
9560Sstevel@tonic-gate 		return (1);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if (askedFor == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
9600Sstevel@tonic-gate 		*sysid = dosp[primaryPart].systid;
9610Sstevel@tonic-gate 		*startSector = ltohi(dosp[primaryPart].relsect);
9620Sstevel@tonic-gate 		return (1);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * We are not looking for the C: drive (or the primary drive
9670Sstevel@tonic-gate 	 * was not found), so we had better have an extended partition
9680Sstevel@tonic-gate 	 * or extra drives in the Master FDISK table.
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	if ((extendedPart < 0) && (numExtraDrives == 0)) {
9710Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no extended dos partition");
9720Sstevel@tonic-gate 		noLogicalDrive(askedFor);
9730Sstevel@tonic-gate 		*error = EINVAL;
9740Sstevel@tonic-gate 		return (0);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	if (extendedPart >= 0) {
9780Sstevel@tonic-gate 		diskblk = xstartsect = ltohi(dosp[extendedPart].relsect);
9790Sstevel@tonic-gate 		xnumsect = ltohi(dosp[extendedPart].numsect);
9800Sstevel@tonic-gate 		do {
9810Sstevel@tonic-gate 			/*
9820Sstevel@tonic-gate 			 *  If the seek would not cause us to change
9830Sstevel@tonic-gate 			 *  position on the drive, then we're out of
9840Sstevel@tonic-gate 			 *  extended partitions to examine.
9850Sstevel@tonic-gate 			 */
9860Sstevel@tonic-gate 			if (diskblk == lastseek)
9870Sstevel@tonic-gate 				break;
9880Sstevel@tonic-gate 			logicalDriveCount += numDrives;
9890Sstevel@tonic-gate 			/*
9900Sstevel@tonic-gate 			 *  Seek the next extended partition, and find
9910Sstevel@tonic-gate 			 *  logical drives within it.
9920Sstevel@tonic-gate 			 */
9930Sstevel@tonic-gate 			brelse(*bp);
9940Sstevel@tonic-gate 			*bp = bread(dev, diskblk, PC_SAFESECSIZE);
9950Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
9960Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
9970Sstevel@tonic-gate 				*error = EIO;
9980Sstevel@tonic-gate 				return (0);
9990Sstevel@tonic-gate 			}
10000Sstevel@tonic-gate 			lastseek = diskblk;
10010Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10020Sstevel@tonic-gate 			if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
10030Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: "
10040Sstevel@tonic-gate 				    "extended partition signature err");
10050Sstevel@tonic-gate 				*error = EINVAL;
10060Sstevel@tonic-gate 				return (0);
10070Sstevel@tonic-gate 			}
10080Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10090Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10100Sstevel@tonic-gate 			/*
10110Sstevel@tonic-gate 			 *  Count up drives, and track where the next
10120Sstevel@tonic-gate 			 *  extended partition is in case we need it.  We
10130Sstevel@tonic-gate 			 *  are expecting only one extended partition.  If
10140Sstevel@tonic-gate 			 *  there is more than one we'll only go to the
10150Sstevel@tonic-gate 			 *  first one we see, but warn about ignoring.
10160Sstevel@tonic-gate 			 */
10170Sstevel@tonic-gate 			numDrives = 0;
10180Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
10190Sstevel@tonic-gate 				if (isDosDrive(dosp[i].systid)) {
10200Sstevel@tonic-gate 					extndDrives[numDrives++] = i;
10210Sstevel@tonic-gate 					continue;
10220Sstevel@tonic-gate 				} else if (isDosExtended(dosp[i].systid)) {
10230Sstevel@tonic-gate 					if (diskblk != lastseek) {
10240Sstevel@tonic-gate 						/*
10250Sstevel@tonic-gate 						 * Already found an extended
10260Sstevel@tonic-gate 						 * partition in this table.
10270Sstevel@tonic-gate 						 */
10280Sstevel@tonic-gate 						cmn_err(CE_NOTE,
10290Sstevel@tonic-gate 						    "!pcfs: ignoring unexpected"
10300Sstevel@tonic-gate 						    " additional extended"
10310Sstevel@tonic-gate 						    " partition");
10320Sstevel@tonic-gate 						continue;
10330Sstevel@tonic-gate 					}
10340Sstevel@tonic-gate 					diskblk = xstartsect +
10350Sstevel@tonic-gate 					    ltohi(dosp[i].relsect);
10360Sstevel@tonic-gate 					continue;
10370Sstevel@tonic-gate 				}
10380Sstevel@tonic-gate 			}
10390Sstevel@tonic-gate 		} while (askedFor > logicalDriveCount + numDrives);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 		if (askedFor <= logicalDriveCount + numDrives) {
10420Sstevel@tonic-gate 			/*
10430Sstevel@tonic-gate 			 * The number of logical drives we've found thus
10440Sstevel@tonic-gate 			 * far is enough to get us to the one we were
10450Sstevel@tonic-gate 			 * searching for.
10460Sstevel@tonic-gate 			 */
10470Sstevel@tonic-gate 			driveIndex = logicalDriveCount + numDrives - askedFor;
10480Sstevel@tonic-gate 			*sysid = dosp[extndDrives[driveIndex]].systid;
10490Sstevel@tonic-gate 			*startSector =
10500Sstevel@tonic-gate 			    ltohi(dosp[extndDrives[driveIndex]].relsect) +
10510Sstevel@tonic-gate 			    lastseek;
10520Sstevel@tonic-gate 			if (*startSector > (xstartsect + xnumsect)) {
10530Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!pcfs: extended partition "
10540Sstevel@tonic-gate 				    "values bad");
10550Sstevel@tonic-gate 				*error = EINVAL;
10560Sstevel@tonic-gate 				return (0);
10570Sstevel@tonic-gate 			}
10580Sstevel@tonic-gate 			return (1);
10590Sstevel@tonic-gate 		} else {
10600Sstevel@tonic-gate 			/*
10610Sstevel@tonic-gate 			 * We ran out of extended dos partition
10620Sstevel@tonic-gate 			 * drives.  The only hope now is to go
10630Sstevel@tonic-gate 			 * back to extra drives defined in the master
10640Sstevel@tonic-gate 			 * fdisk table.  But we overwrote that table
10650Sstevel@tonic-gate 			 * already, so we must load it in again.
10660Sstevel@tonic-gate 			 */
10670Sstevel@tonic-gate 			logicalDriveCount += numDrives;
10680Sstevel@tonic-gate 			brelse(*bp);
10690Sstevel@tonic-gate 			*bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
10700Sstevel@tonic-gate 			if ((*bp)->b_flags & B_ERROR) {
10710Sstevel@tonic-gate 				PC_DPRINTF0(1, "pc_getfattype: read error\n");
10720Sstevel@tonic-gate 				*error = EIO;
10730Sstevel@tonic-gate 				return (0);
10740Sstevel@tonic-gate 			}
10750Sstevel@tonic-gate 			dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr;
10760Sstevel@tonic-gate 			bcopy(dosp_ptr->parts, dosp,
10770Sstevel@tonic-gate 			    sizeof (struct ipart) * FD_NUMPART);
10780Sstevel@tonic-gate 		}
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 *  Still haven't found the drive, is it an extra
10820Sstevel@tonic-gate 	 *  drive defined in the main FDISK table?
10830Sstevel@tonic-gate 	 */
10840Sstevel@tonic-gate 	if (askedFor <= logicalDriveCount + numExtraDrives) {
10850Sstevel@tonic-gate 		driveIndex = logicalDriveCount + numExtraDrives - askedFor;
10860Sstevel@tonic-gate 		*sysid = dosp[extraDrives[driveIndex]].systid;
10870Sstevel@tonic-gate 		*startSector = ltohi(dosp[extraDrives[driveIndex]].relsect);
10880Sstevel@tonic-gate 		return (1);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	/*
10910Sstevel@tonic-gate 	 *  Still haven't found the drive, and there is
10920Sstevel@tonic-gate 	 *  nowhere else to look.
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 	noLogicalDrive(askedFor);
10950Sstevel@tonic-gate 	*error = EINVAL;
10960Sstevel@tonic-gate 	return (0);
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate  * Get the FAT type for the DOS medium.
11010Sstevel@tonic-gate  *
11020Sstevel@tonic-gate  * We look at sector 0 and determine if we are looking at an FDISK table
11030Sstevel@tonic-gate  * or a BIOS Parameter Block (BPB).  Most of the time, if its a floppy
11040Sstevel@tonic-gate  * we'll be looking at a BPB and if we're looking at a hard drive we'll be
11050Sstevel@tonic-gate  * examining an FDISK table.  Those fun exceptions do happen, though.
11060Sstevel@tonic-gate  *
11070Sstevel@tonic-gate  * If we are looking at a BPB, we can calculate and verify the FAT size.
11080Sstevel@tonic-gate  * If we are looking at an FDISK partition table, we scan the partition
11090Sstevel@tonic-gate  * table for the requested logical volume.
11100Sstevel@tonic-gate  */
11110Sstevel@tonic-gate static int
11120Sstevel@tonic-gate pc_getfattype(
11130Sstevel@tonic-gate 	struct vnode *devvp,
11140Sstevel@tonic-gate 	int ldrive,
11150Sstevel@tonic-gate 	daddr_t *strtsectp,
11160Sstevel@tonic-gate 	int *fattypep)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate 	struct mboot *dosp_ptr;		/* boot structure pointer */
11190Sstevel@tonic-gate 	struct bootsec *bootp, *bpbp;	/* for detailed sector examination */
11200Sstevel@tonic-gate 	uchar_t *cp;			/* for searching out FAT string */
11210Sstevel@tonic-gate 	uint_t overhead;		/* sectors not part of file area */
11220Sstevel@tonic-gate 	uint_t numclusters;		/* number of clusters in file area */
11230Sstevel@tonic-gate 	uint_t bytesoffat;		/* computed number of bytes in a FAT */
11240Sstevel@tonic-gate 	int secsize;			/* Sector size in bytes/sec */
11250Sstevel@tonic-gate 	buf_t *bp = NULL;		/* Disk buffer pointer */
11260Sstevel@tonic-gate 	int rval = 0;
11270Sstevel@tonic-gate 	uchar_t sysid = 0;		/* System ID character */
11280Sstevel@tonic-gate 	dev_t	dev = devvp->v_rdev;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	*strtsectp = (daddr_t)0;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/*
11330Sstevel@tonic-gate 	 * Open the device so we can check out the BPB or FDISK table,
11340Sstevel@tonic-gate 	 * then read in the sector.
11350Sstevel@tonic-gate 	 */
11360Sstevel@tonic-gate 	PC_DPRINTF2(5, "pc_getfattype: dev=%x  ldrive=%x  ", (int)dev, ldrive);
11370Sstevel@tonic-gate 	if (rval = VOP_OPEN(&devvp, FREAD, CRED())) {
11380Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfattype: open error=%d\n", rval);
11390Sstevel@tonic-gate 		return (rval);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/*
11430Sstevel@tonic-gate 	 *  Read block 0 from device
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE);
11460Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
11470Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfattype: read error\n");
11480Sstevel@tonic-gate 		rval = EIO;
11490Sstevel@tonic-gate 		goto out;
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * If this is a logical volume with a FAT file system,
11540Sstevel@tonic-gate 	 * then we expect to find a "file system boot sector"
11550Sstevel@tonic-gate 	 * (also called a BIOS Perameter Block -- or BPB) in
11560Sstevel@tonic-gate 	 * the first physical sector.
11570Sstevel@tonic-gate 	 */
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/*
11600Sstevel@tonic-gate 	 * The word "FAT" is encoded into the BPB beginning at
11610Sstevel@tonic-gate 	 * PCFS_TYPESTRING_OFFSET16 in 12 and 16 bit FATs.  It is
11620Sstevel@tonic-gate 	 * encoded at FS_TYPESTRING_OFFSET32 in 32 bit FATs.
11630Sstevel@tonic-gate 	 */
11640Sstevel@tonic-gate 	cp = (uchar_t *)bp->b_un.b_addr;
11650Sstevel@tonic-gate 	if (*(cp + PCFS_TYPESTRING_OFFSET16) == 'F' &&
11660Sstevel@tonic-gate 	    *(cp + PCFS_TYPESTRING_OFFSET16 + 1) == 'A' &&
11670Sstevel@tonic-gate 	    *(cp + PCFS_TYPESTRING_OFFSET16 + 2) == 'T') {
11680Sstevel@tonic-gate 		PC_DPRINTF0(5, "Found the FAT string at 12/16 location\n");
11690Sstevel@tonic-gate 		/*
11700Sstevel@tonic-gate 		 * Compute a bits/fat value
11710Sstevel@tonic-gate 		 */
11720Sstevel@tonic-gate 		bpbp = (struct bootsec *)bp->b_un.b_addr;
11730Sstevel@tonic-gate 		secsize = (int)ltohs(bpbp->bps[0]);
11740Sstevel@tonic-gate 		/*
11750Sstevel@tonic-gate 		 * Check for bogus sector size -
11760Sstevel@tonic-gate 		 *	fat should be at least 1 sector
11770Sstevel@tonic-gate 		 * If anything looks weird, we have to bail and try looking
11780Sstevel@tonic-gate 		 * for an FDISK table instead.
11790Sstevel@tonic-gate 		 */
11800Sstevel@tonic-gate 		if (secsize < 512 || (int)ltohs(bpbp->fatsec) < 1 ||
11810Sstevel@tonic-gate 		    bpbp->nfat < 1 || bpbp->spcl < 1) {
11820Sstevel@tonic-gate 			PC_DPRINTF4(5, "One or more BPB fields bad\n"
11830Sstevel@tonic-gate 			    "bytes/sec = %d, sec/fat = %d, numfats = %d, "
11840Sstevel@tonic-gate 			    "sec/clust = %d\n", secsize,
11850Sstevel@tonic-gate 			    (int)ltohs(bpbp->fatsec), bpbp->nfat, bpbp->spcl);
11860Sstevel@tonic-gate 			goto lookforfdisk;
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 		overhead = bpbp->nfat * ltohs(bpbp->fatsec);
11900Sstevel@tonic-gate 		overhead += ltohs(bpbp->res_sec[0]);
11910Sstevel@tonic-gate 		overhead += (ltohs(bpbp->rdirents[0]) *
11920Sstevel@tonic-gate 		    sizeof (struct pcdir)) / secsize;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 		numclusters = ((ltohs(bpbp->numsect[0]) ?
11950Sstevel@tonic-gate 		    ltohs(bpbp->numsect[0]) : ltohi(bpbp->totalsec)) -
11960Sstevel@tonic-gate 		    overhead) / bpbp->spcl;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		/*
11990Sstevel@tonic-gate 		 * If the number of clusters looks bad, go look for an
12000Sstevel@tonic-gate 		 * FDISK table.
12010Sstevel@tonic-gate 		 */
12020Sstevel@tonic-gate 		if (numclusters < 1) {
12030Sstevel@tonic-gate 			PC_DPRINTF1(5, "num clusters is bad ( = %d )\n",
12040Sstevel@tonic-gate 			    numclusters);
12050Sstevel@tonic-gate 			goto lookforfdisk;
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		/*
12090Sstevel@tonic-gate 		 * The number of bytes of FAT determines the maximum number
12100Sstevel@tonic-gate 		 * of entries of a given size that FAT can contain.
12110Sstevel@tonic-gate 		 * The FAT can only contain (bytes of FAT)*8/12 12-bit entries
12120Sstevel@tonic-gate 		 * and (bytes of FAT)*8/16 16-bit entries.
12130Sstevel@tonic-gate 		 */
12140Sstevel@tonic-gate 		bytesoffat = ltohs(bpbp->fatsec) * secsize;
12150Sstevel@tonic-gate 		PC_DPRINTF1(5, "Computed bytes of fat = %u\n", bytesoffat);
12160Sstevel@tonic-gate 		if ((bytesoffat * 2 / 3) >= numclusters &&
12170Sstevel@tonic-gate 		    *(cp + PCFS_TYPESTRING_OFFSET16 + 4) == '2') {
12180Sstevel@tonic-gate 			PC_DPRINTF0(4, "pc_getfattype: 12-bit FAT\n");
12190Sstevel@tonic-gate 			*fattypep = 0;
12200Sstevel@tonic-gate 			rval = 0;
12210Sstevel@tonic-gate 			goto out;
12220Sstevel@tonic-gate 		} else if (*(cp + PCFS_TYPESTRING_OFFSET16 + 4) == '6') {
12230Sstevel@tonic-gate 			/*
12240Sstevel@tonic-gate 			 * this check can result in a false positive, where
12250Sstevel@tonic-gate 			 * we believe a FAT12 filesystem to be a FAT16 one
12260Sstevel@tonic-gate 			 * (if the type recorded in the header block lies).
12270Sstevel@tonic-gate 			 * we recover from being lied to, in 'pc_getfat' by
12280Sstevel@tonic-gate 			 * Forcing fat12 over fat16, if
12290Sstevel@tonic-gate 			 *    'pcfs_fatsec <= 12' and
12300Sstevel@tonic-gate 			 *    '(byteoffat * 2 / 3) >= numclusters'
12310Sstevel@tonic-gate 			 * the obvious check would have been:
12320Sstevel@tonic-gate 			 * 	else if ((bytesoffat / 2) >= numclusters &&
12330Sstevel@tonic-gate 			 * 	*(cp + PCFS_TYPESTRING_OFFSET16 + 4) == '6')
12340Sstevel@tonic-gate 			 */
12350Sstevel@tonic-gate 			PC_DPRINTF0(4, "pc_getfattype: 16-bit FAT\n");
12360Sstevel@tonic-gate 			*fattypep = PCFS_FAT16;
12370Sstevel@tonic-gate 			rval = 0;
12380Sstevel@tonic-gate 			goto out;
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 		goto lookforfdisk;
12410Sstevel@tonic-gate 	} else if (*(cp + PCFS_TYPESTRING_OFFSET32) == 'F' &&
12420Sstevel@tonic-gate 	    *(cp + PCFS_TYPESTRING_OFFSET32 + 1) == 'A' &&
12430Sstevel@tonic-gate 	    *(cp + PCFS_TYPESTRING_OFFSET32 + 2) == 'T') {
12440Sstevel@tonic-gate 		PC_DPRINTF0(5, "Found the FAT string at 32 location\n");
12450Sstevel@tonic-gate 		bpbp = (struct bootsec *)bp->b_un.b_addr;
12460Sstevel@tonic-gate 		if ((int)ltohs(bpbp->fatsec) != 0) {
12470Sstevel@tonic-gate 			/*
12480Sstevel@tonic-gate 			 * Not a good sign, we expect it to be zero if
12490Sstevel@tonic-gate 			 * this is really a FAT32 BPB.  All we can do
12500Sstevel@tonic-gate 			 * is consider the string an anomaly, and go look
12510Sstevel@tonic-gate 			 * for an FDISK table.
12520Sstevel@tonic-gate 			 */
12530Sstevel@tonic-gate 			PC_DPRINTF0(5, "But secs/fat non-zero\n");
12540Sstevel@tonic-gate 			goto lookforfdisk;
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 		PC_DPRINTF0(4, "pc_getfattype: 32-bit FAT\n");
12570Sstevel@tonic-gate 		*fattypep = PCFS_FAT32;
12580Sstevel@tonic-gate 		rval = 0;
12590Sstevel@tonic-gate 		goto out;
12600Sstevel@tonic-gate 	} else {
12610Sstevel@tonic-gate 		/*
12620Sstevel@tonic-gate 		 *  We've got some legacy cases (like the stuff fdformat
12630Sstevel@tonic-gate 		 *  produces!).  Basically this is pre-MSDOS4.0 FATs.
12640Sstevel@tonic-gate 		 *
12650Sstevel@tonic-gate 		 *  We'll declare a match if:
12660Sstevel@tonic-gate 		 *	1. Bytes/Sector and other fields seem reasonable
12670Sstevel@tonic-gate 		 *	2. The media byte matches a known one.
12680Sstevel@tonic-gate 		 *
12690Sstevel@tonic-gate 		 *  If the media byte indicates a floppy we'll
12700Sstevel@tonic-gate 		 *  assume FAT12, otherwise we'll assume FAT16.
12710Sstevel@tonic-gate 		 */
12720Sstevel@tonic-gate 		bpbp = (struct bootsec *)bp->b_un.b_addr;
12730Sstevel@tonic-gate 		secsize = (int)ltohs(bpbp->bps[0]);
12740Sstevel@tonic-gate 		if (secsize && secsize % 512 == 0 &&
12750Sstevel@tonic-gate 		    ltohs(bpbp->fatsec) > 0 && bpbp->nfat > 0 &&
12760Sstevel@tonic-gate 		    bpbp->spcl > 0 && ltohs(bpbp->res_sec[0]) >= 1 &&
12770Sstevel@tonic-gate 		    ltohs(bpbp->numsect[0]) > 0) {
12780Sstevel@tonic-gate 			switch (bpbp->mediadesriptor) {
12790Sstevel@tonic-gate 			case SS8SPT:
12800Sstevel@tonic-gate 			case DS8SPT:
12810Sstevel@tonic-gate 			case SS9SPT:
12820Sstevel@tonic-gate 			case DS9SPT:
12830Sstevel@tonic-gate 			case DS18SPT:
12840Sstevel@tonic-gate 			case DS9_15SPT:
12850Sstevel@tonic-gate 				*fattypep = 0;
12860Sstevel@tonic-gate 				rval = 0;
12870Sstevel@tonic-gate 				goto out;
12880Sstevel@tonic-gate 			case MD_FIXED:
12890Sstevel@tonic-gate 				*fattypep = PCFS_FAT16;
12900Sstevel@tonic-gate 				rval = 0;
12910Sstevel@tonic-gate 				goto out;
12920Sstevel@tonic-gate 			default:
12930Sstevel@tonic-gate 				goto lookforfdisk;
12940Sstevel@tonic-gate 			}
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate lookforfdisk:
12990Sstevel@tonic-gate 	/*
13000Sstevel@tonic-gate 	 *  If we got here then we didn't find a BPB.
13010Sstevel@tonic-gate 	 *  We now assume we are looking at the start of a hard drive,
13020Sstevel@tonic-gate 	 *  where the first sector will be a Master Boot Record (MBR).
13030Sstevel@tonic-gate 	 *  The MBR contains a partition table (also called an FDISK
13040Sstevel@tonic-gate 	 *  table) and should end with a signature word (MBB_MAGIC).
13050Sstevel@tonic-gate 	 *
13060Sstevel@tonic-gate 	 *  Check signature at end of boot block for good value.
13070Sstevel@tonic-gate 	 *  If not then error with invalid request.
13080Sstevel@tonic-gate 	 */
13090Sstevel@tonic-gate 	dosp_ptr = (struct mboot *)bp->b_un.b_addr;
13100Sstevel@tonic-gate 	if (ltohs(dosp_ptr->signature) != MBB_MAGIC) {
13110Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: MBR signature error");
13120Sstevel@tonic-gate 		rval = EINVAL;
13130Sstevel@tonic-gate 		goto out;
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 	if (ldrive <= 0) {
13160Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: no logical drive specified");
13170Sstevel@tonic-gate 		rval = EINVAL;
13180Sstevel@tonic-gate 		goto out;
13190Sstevel@tonic-gate 	}
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	if (findTheDrive(dev, ldrive, &rval, &bp, strtsectp, &sysid) == 0)
13220Sstevel@tonic-gate 		goto out;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * Check the sysid value of the logical drive.
13260Sstevel@tonic-gate 	 * Return the correct value for the type of FAT found.
13270Sstevel@tonic-gate 	 * Else return a value of -1 for unknown FAT type.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	if ((sysid == DOS_FAT32) || (sysid == DOS_FAT32_LBA)) {
13300Sstevel@tonic-gate 		*fattypep = PCFS_FAT32 | PCFS_NOCHK;
13310Sstevel@tonic-gate 		PC_DPRINTF0(4, "pc_getfattype: 32-bit FAT\n");
13320Sstevel@tonic-gate 	} else if ((sysid == DOS_SYSFAT16) || (sysid == DOS_SYSHUGE) ||
13330Sstevel@tonic-gate 	    (sysid == DIAGPART) ||
13340Sstevel@tonic-gate 	    (sysid == DOS_FAT16P_LBA) || (sysid == DOS_FAT16_LBA)) {
13350Sstevel@tonic-gate 		*fattypep = PCFS_FAT16 | PCFS_NOCHK;
13360Sstevel@tonic-gate 		PC_DPRINTF0(4, "pc_getfattype: 16-bit FAT\n");
13370Sstevel@tonic-gate 	} else if (sysid == DOS_SYSFAT12) {
13380Sstevel@tonic-gate 		*fattypep = PCFS_NOCHK;
13390Sstevel@tonic-gate 		PC_DPRINTF0(4, "pc_getfattype: 12-bit FAT\n");
13400Sstevel@tonic-gate 	} else if (sysid == X86BOOT) {
13410Sstevel@tonic-gate 		brelse(bp);
13420Sstevel@tonic-gate 		bp = bread(dev, *strtsectp, PC_SAFESECSIZE);
13430Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
13440Sstevel@tonic-gate 			PC_DPRINTF0(1, "pc_getfattype: read error\n");
13450Sstevel@tonic-gate 			rval = EIO;
13460Sstevel@tonic-gate 			goto out;
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 		bootp = (struct bootsec *)bp->b_un.b_addr;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 		/* get the sector size - may be more than 512 bytes */
13510Sstevel@tonic-gate 		secsize = (int)ltohs(bootp->bps[0]);
13520Sstevel@tonic-gate 		/*
13530Sstevel@tonic-gate 		 * Check for bogus sector size -
13540Sstevel@tonic-gate 		 *	fat should be at least 1 sector
13550Sstevel@tonic-gate 		 */
13560Sstevel@tonic-gate 		if (secsize < 512 || (int)ltohs(bootp->fatsec) < 1 ||
13570Sstevel@tonic-gate 		    bootp->nfat < 1 || bootp->spcl < 1) {
13580Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: FAT size error");
13590Sstevel@tonic-gate 			rval = EINVAL;
13600Sstevel@tonic-gate 			goto out;
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 		overhead = bootp->nfat * ltohs(bootp->fatsec);
13640Sstevel@tonic-gate 		overhead += ltohs(bootp->res_sec[0]);
13650Sstevel@tonic-gate 		overhead += (ltohs(bootp->rdirents[0]) *
13660Sstevel@tonic-gate 		    sizeof (struct pcdir)) / secsize;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 		numclusters = ((ltohs(bootp->numsect[0]) ?
13690Sstevel@tonic-gate 		    ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec)) -
13700Sstevel@tonic-gate 		    overhead) / bootp->spcl;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 		if (numclusters > DOS_F12MAXC) {
13730Sstevel@tonic-gate 			PC_DPRINTF0(4, "pc_getfattype: 16-bit FAT BOOTPART\n");
13740Sstevel@tonic-gate 			*fattypep = PCFS_FAT16 | PCFS_NOCHK | PCFS_BOOTPART;
13750Sstevel@tonic-gate 		} else {
13760Sstevel@tonic-gate 			PC_DPRINTF0(4, "pc_getfattype: 12-bit FAT BOOTPART\n");
13770Sstevel@tonic-gate 			*fattypep = PCFS_NOCHK | PCFS_BOOTPART;
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 	} else {
13800Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: unknown FAT type");
13810Sstevel@tonic-gate 		rval = EINVAL;
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate /*
13850Sstevel@tonic-gate  *   Release the buffer used
13860Sstevel@tonic-gate  */
13870Sstevel@tonic-gate out:
13880Sstevel@tonic-gate 	if (bp != NULL)
13890Sstevel@tonic-gate 		brelse(bp);
13900Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, CRED());
13910Sstevel@tonic-gate 	return (rval);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate /*
13960Sstevel@tonic-gate  * Get the boot parameter block and file allocation table.
13970Sstevel@tonic-gate  * If there is an old FAT, invalidate it.
13980Sstevel@tonic-gate  */
13990Sstevel@tonic-gate int
14000Sstevel@tonic-gate pc_getfat(struct pcfs *fsp)
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate 	struct vfs *vfsp = PCFSTOVFS(fsp);
14030Sstevel@tonic-gate 	struct buf *tp = 0;
14040Sstevel@tonic-gate 	struct buf *bp = 0;
14050Sstevel@tonic-gate 	uchar_t *fatp = NULL;
14060Sstevel@tonic-gate 	uchar_t *fat_changemap = NULL;
14070Sstevel@tonic-gate 	struct bootsec *bootp;
14080Sstevel@tonic-gate 	struct fat32_bootsec *f32b;
14090Sstevel@tonic-gate 	struct vnode *devvp;
14100Sstevel@tonic-gate 	int error;
14110Sstevel@tonic-gate 	int fatsize;
14120Sstevel@tonic-gate 	int fat_changemapsize;
14130Sstevel@tonic-gate 	int flags = 0;
14140Sstevel@tonic-gate 	int nfat;
14150Sstevel@tonic-gate 	int secsize;
14160Sstevel@tonic-gate 	int fatsec;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	PC_DPRINTF0(5, "pc_getfat\n");
14190Sstevel@tonic-gate 	devvp = fsp->pcfs_devvp;
14200Sstevel@tonic-gate 	if (fsp->pcfs_fatp) {
14210Sstevel@tonic-gate 		/*
14220Sstevel@tonic-gate 		 * There is a FAT in core.
14230Sstevel@tonic-gate 		 * If there are open file pcnodes or we have modified it or
14240Sstevel@tonic-gate 		 * it hasn't timed out yet use the in core FAT.
14250Sstevel@tonic-gate 		 * Otherwise invalidate it and get a new one
14260Sstevel@tonic-gate 		 */
14270Sstevel@tonic-gate #ifdef notdef
14280Sstevel@tonic-gate 		if (fsp->pcfs_frefs ||
14290Sstevel@tonic-gate 		    (fsp->pcfs_flags & PCFS_FATMOD) ||
14300Sstevel@tonic-gate 		    (gethrestime_sec() < fsp->pcfs_fattime)) {
14310Sstevel@tonic-gate 			return (0);
14320Sstevel@tonic-gate 		} else {
14330Sstevel@tonic-gate 			mutex_enter(&pcfslock);
14340Sstevel@tonic-gate 			pc_invalfat(fsp);
14350Sstevel@tonic-gate 			mutex_exit(&pcfslock);
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate #endif /* notdef */
14380Sstevel@tonic-gate 		return (0);
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate 	/*
14410Sstevel@tonic-gate 	 * Open block device mounted on.
14420Sstevel@tonic-gate 	 */
14430Sstevel@tonic-gate 	error = VOP_OPEN(&devvp,
14440Sstevel@tonic-gate 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
14450Sstevel@tonic-gate 	    CRED());
14460Sstevel@tonic-gate 	if (error) {
14470Sstevel@tonic-gate 		PC_DPRINTF1(1, "pc_getfat: open error=%d\n", error);
14480Sstevel@tonic-gate 		return (error);
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * Get boot parameter block and check it for validity
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	tp = bread(fsp->pcfs_xdev, fsp->pcfs_dosstart, PC_SAFESECSIZE);
14540Sstevel@tonic-gate 	if (tp->b_flags & (B_ERROR | B_STALE)) {
14550Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_getfat: boot block error\n");
14560Sstevel@tonic-gate 		flags = tp->b_flags & B_ERROR;
14570Sstevel@tonic-gate 		error = EIO;
14580Sstevel@tonic-gate 		goto out;
14590Sstevel@tonic-gate 	}
14600Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
14610Sstevel@tonic-gate 	bootp = (struct bootsec *)tp->b_un.b_addr;
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	/* get the sector size - may be more than 512 bytes */
14640Sstevel@tonic-gate 	secsize = (int)ltohs(bootp->bps[0]);
14650Sstevel@tonic-gate 	/* check for bogus sector size - fat should be at least 1 sector */
14660Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
14670Sstevel@tonic-gate 		f32b = (struct fat32_bootsec *)bootp;
14680Sstevel@tonic-gate 		fatsec = ltohi(f32b->f_fatlength);
14690Sstevel@tonic-gate 	} else {
14700Sstevel@tonic-gate 		fatsec = ltohs(bootp->fatsec);
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 	if (secsize < 512 || fatsec < 1 || bootp->nfat < 1) {
14730Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT size error");
14740Sstevel@tonic-gate 		error = EINVAL;
14750Sstevel@tonic-gate 		goto out;
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	switch (bootp->mediadesriptor) {
14790Sstevel@tonic-gate 	default:
14800Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: media-descriptor error, 0x%x",
14810Sstevel@tonic-gate 		    bootp->mediadesriptor);
14820Sstevel@tonic-gate 		error = EINVAL;
14830Sstevel@tonic-gate 		goto out;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	case MD_FIXED:
14860Sstevel@tonic-gate 		/*
1487*201Sjmcp 		 * PCMCIA pseudo floppy is type MD_FIXED,
14880Sstevel@tonic-gate 		 * but is accessed like a floppy
14890Sstevel@tonic-gate 		 */
14900Sstevel@tonic-gate 		if (!(fsp->pcfs_flags & PCFS_PCMCIA_NO_CIS)) {
14910Sstevel@tonic-gate 			fsp->pcfs_flags |= PCFS_NOCHK;
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 		/* FALLTHRU */
14940Sstevel@tonic-gate 	case SS8SPT:
14950Sstevel@tonic-gate 	case DS8SPT:
14960Sstevel@tonic-gate 	case SS9SPT:
14970Sstevel@tonic-gate 	case DS9SPT:
14980Sstevel@tonic-gate 	case DS18SPT:
14990Sstevel@tonic-gate 	case DS9_15SPT:
15000Sstevel@tonic-gate 		fsp->pcfs_secsize = secsize;
15010Sstevel@tonic-gate 		fsp->pcfs_sdshift = secsize / DEV_BSIZE - 1;
15020Sstevel@tonic-gate 		fsp->pcfs_entps = secsize / sizeof (struct pcdir);
15030Sstevel@tonic-gate 		fsp->pcfs_spcl = (int)bootp->spcl;
15040Sstevel@tonic-gate 		fsp->pcfs_fatsec = fatsec;
15050Sstevel@tonic-gate 		fsp->pcfs_spt = (int)ltohs(bootp->spt);
15060Sstevel@tonic-gate 		fsp->pcfs_rdirsec = (int)ltohs(bootp->rdirents[0])
15070Sstevel@tonic-gate 		    * sizeof (struct pcdir) / secsize;
15080Sstevel@tonic-gate 		fsp->pcfs_clsize = fsp->pcfs_spcl * secsize;
15090Sstevel@tonic-gate 		fsp->pcfs_fatstart = fsp->pcfs_dosstart +
15100Sstevel@tonic-gate 		    (daddr_t)ltohs(bootp->res_sec[0]);
15110Sstevel@tonic-gate 		fsp->pcfs_rdirstart = fsp->pcfs_fatstart +
15120Sstevel@tonic-gate 		    (bootp->nfat * fsp->pcfs_fatsec);
15130Sstevel@tonic-gate 		fsp->pcfs_datastart = fsp->pcfs_rdirstart + fsp->pcfs_rdirsec;
15140Sstevel@tonic-gate 		if (IS_FAT32(fsp))
15150Sstevel@tonic-gate 			fsp->pcfs_rdirstart = ltohi(f32b->f_rootcluster);
15160Sstevel@tonic-gate 		fsp->pcfs_ncluster = (((int)(ltohs(bootp->numsect[0]) ?
15170Sstevel@tonic-gate 		    ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec))) -
15180Sstevel@tonic-gate 		    fsp->pcfs_datastart + fsp->pcfs_dosstart) / fsp->pcfs_spcl;
15190Sstevel@tonic-gate 		fsp->pcfs_numfat = (int)bootp->nfat;
15200Sstevel@tonic-gate 		fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER;
15210Sstevel@tonic-gate 		break;
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	/*
15250Sstevel@tonic-gate 	 * Get FAT and check it for validity
15260Sstevel@tonic-gate 	 */
15270Sstevel@tonic-gate 	fatsize = fsp->pcfs_fatsec * fsp->pcfs_secsize;
15280Sstevel@tonic-gate 	fatp = kmem_alloc(fatsize, KM_SLEEP);
15290Sstevel@tonic-gate 	error = pc_readfat(fsp, fatp, fsp->pcfs_fatstart, fatsize);
15300Sstevel@tonic-gate 	if (error) {
15310Sstevel@tonic-gate 		flags = B_ERROR;
15320Sstevel@tonic-gate 		goto out;
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 	fat_changemapsize = (fatsize / fsp->pcfs_clsize) + 1;
15350Sstevel@tonic-gate 	fat_changemap = kmem_zalloc(fat_changemapsize, KM_SLEEP);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	if (fatp[0] != bootp->mediadesriptor ||
15380Sstevel@tonic-gate 	    fatp[1] != 0xFF || fatp[2] != 0xFF) {
15390Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT signature error");
15400Sstevel@tonic-gate 		error = EINVAL;
15410Sstevel@tonic-gate 		goto out;
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 	/*
15440Sstevel@tonic-gate 	 * Checking for fatsec and number of supported clusters, should
15450Sstevel@tonic-gate 	 * actually determine a FAT12/FAT media. See pc_getfattype().
15460Sstevel@tonic-gate 	 * fatp[3] != 0xFF is necessary for FAT validity.
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	if (fsp->pcfs_flags & PCFS_FAT16) {
15490Sstevel@tonic-gate 		if ((fsp->pcfs_fatsec <= 12) &&
15500Sstevel@tonic-gate 		    ((fatsize * 2 / 3) >= fsp->pcfs_ncluster)) {
15510Sstevel@tonic-gate 			/*
15520Sstevel@tonic-gate 			 * We have a 12-bit FAT, rather than a 16-bit FAT.
15530Sstevel@tonic-gate 			 * Ignore what the fdisk table says.
15540Sstevel@tonic-gate 			 */
15550Sstevel@tonic-gate 			PC_DPRINTF0(2, "pc_getfattype: forcing 12-bit FAT\n");
15560Sstevel@tonic-gate 			fsp->pcfs_flags &= ~PCFS_FAT16;
15570Sstevel@tonic-gate 		} else if (fatp[3] != 0xFF) {
15580Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: FAT signature error");
15590Sstevel@tonic-gate 			error = EINVAL;
15600Sstevel@tonic-gate 			goto out;
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 	/*
15640Sstevel@tonic-gate 	 * Sanity check our FAT is large enough for the
15650Sstevel@tonic-gate 	 * clusters we think we have.
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	if ((fsp->pcfs_flags & PCFS_FAT16) &&
15680Sstevel@tonic-gate 	    ((fatsize / 2) < fsp->pcfs_ncluster)) {
15690Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!pcfs: FAT too small for number of clusters");
15700Sstevel@tonic-gate 		error = EINVAL;
15710Sstevel@tonic-gate 		goto out;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/*
15750Sstevel@tonic-gate 	 * Get alternate FATs and check for consistency
15760Sstevel@tonic-gate 	 * This is an inlined version of pc_readfat().
15770Sstevel@tonic-gate 	 * Since we're only comparing FAT and alternate FAT,
15780Sstevel@tonic-gate 	 * there's no reason to let pc_readfat() copy data out
15790Sstevel@tonic-gate 	 * of the buf. Instead, compare in-situ, one cluster
15800Sstevel@tonic-gate 	 * at a time.
15810Sstevel@tonic-gate 	 */
15820Sstevel@tonic-gate 	for (nfat = 1; nfat < fsp->pcfs_numfat; nfat++) {
15830Sstevel@tonic-gate 		size_t startsec;
15840Sstevel@tonic-gate 		size_t off;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 		startsec = fsp->pcfs_fatstart + nfat * fsp->pcfs_fatsec;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 		for (off = 0; off < fatsize; off += fsp->pcfs_clsize) {
15890Sstevel@tonic-gate 			bp = bread(fsp->pcfs_xdev, pc_dbdaddr(fsp,
15900Sstevel@tonic-gate 				startsec +
15910Sstevel@tonic-gate 				pc_cltodb(fsp, pc_lblkno(fsp, off))),
15920Sstevel@tonic-gate 				MIN(fsp->pcfs_clsize, fatsize - off));
15930Sstevel@tonic-gate 			if (bp->b_flags & (B_ERROR | B_STALE)) {
15940Sstevel@tonic-gate 				cmn_err(CE_NOTE,
15950Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d read error"
15960Sstevel@tonic-gate 					" at byte %ld", nfat, off);
15970Sstevel@tonic-gate 				flags = B_ERROR;
15980Sstevel@tonic-gate 				error = EIO;
15990Sstevel@tonic-gate 				goto out;
16000Sstevel@tonic-gate 			}
16010Sstevel@tonic-gate 			bp->b_flags |= B_STALE | B_AGE;
16020Sstevel@tonic-gate 			if (bcmp(bp->b_un.b_addr,
16030Sstevel@tonic-gate 			    fatp + off,
16040Sstevel@tonic-gate 			    MIN(fsp->pcfs_clsize, fatsize - off))) {
16050Sstevel@tonic-gate 				cmn_err(CE_NOTE,
16060Sstevel@tonic-gate 					"!pcfs: alternate FAT #%d corrupted"
16070Sstevel@tonic-gate 					" at byte %ld", nfat, off);
16080Sstevel@tonic-gate 				flags = B_ERROR;
16090Sstevel@tonic-gate 			}
16100Sstevel@tonic-gate 			brelse(bp);
16110Sstevel@tonic-gate 			bp = NULL;	/* prevent double release */
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	fsp->pcfs_fatsize = fatsize;
16160Sstevel@tonic-gate 	fsp->pcfs_fatp = fatp;
16170Sstevel@tonic-gate 	fsp->pcfs_fat_changemapsize = fat_changemapsize;
16180Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = fat_changemap;
16190Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
16200Sstevel@tonic-gate 	fsp->pcfs_fatjustread = 1;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	brelse(tp);
16230Sstevel@tonic-gate 	tp = NULL;
16240Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16250Sstevel@tonic-gate 		/* get fsinfo */
16260Sstevel@tonic-gate 		struct fat32_boot_fsinfo fsinfo_disk;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 		fsp->f32fsinfo_sector = ltohs(f32b->f_infosector);
16290Sstevel@tonic-gate 		tp = bread(fsp->pcfs_xdev,
16300Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
16310Sstevel@tonic-gate 		    PC_SAFESECSIZE);
16320Sstevel@tonic-gate 		if (tp->b_flags & (B_ERROR | B_STALE)) {
16330Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!pcfs: error reading fat32 fsinfo");
16340Sstevel@tonic-gate 			flags = tp->b_flags & B_ERROR;
16350Sstevel@tonic-gate 			brelse(tp);
16360Sstevel@tonic-gate 			tp = NULL;
16370Sstevel@tonic-gate 			error = EIO;
16380Sstevel@tonic-gate 			goto out;
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		tp->b_flags |= B_STALE | B_AGE;
16410Sstevel@tonic-gate 		bcopy((void *)(tp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
16420Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
16430Sstevel@tonic-gate 		brelse(tp);
16440Sstevel@tonic-gate 		tp = NULL;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 		/* translated fields */
16470Sstevel@tonic-gate 		fsp->fsinfo_native.fs_signature =
16480Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_signature);
16490Sstevel@tonic-gate 		fsp->fsinfo_native.fs_free_clusters =
16500Sstevel@tonic-gate 		    ltohi(fsinfo_disk.fs_free_clusters);
16510Sstevel@tonic-gate 		if (fsp->fsinfo_native.fs_signature != FAT32_FS_SIGN) {
16520Sstevel@tonic-gate 			cmn_err(CE_NOTE,
16530Sstevel@tonic-gate 			    "!pcfs: fat32 fsinfo signature mismatch.");
16540Sstevel@tonic-gate 			error = EINVAL;
16550Sstevel@tonic-gate 			goto out;
16560Sstevel@tonic-gate 		}
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	return (0);
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate out:
16620Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!pcfs: illegal disk format");
16630Sstevel@tonic-gate 	if (tp)
16640Sstevel@tonic-gate 		brelse(tp);
16650Sstevel@tonic-gate 	if (bp)
16660Sstevel@tonic-gate 		brelse(bp);
16670Sstevel@tonic-gate 	if (fatp)
16680Sstevel@tonic-gate 		kmem_free(fatp, fatsize);
16690Sstevel@tonic-gate 	if (fat_changemap)
16700Sstevel@tonic-gate 		kmem_free(fat_changemap, fat_changemapsize);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	if (flags) {
16730Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 	(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
16760Sstevel@tonic-gate 	    FREAD : FREAD|FWRITE, 1, (offset_t)0, CRED());
16770Sstevel@tonic-gate 	return (error);
16780Sstevel@tonic-gate }
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate int
16810Sstevel@tonic-gate pc_syncfat(struct pcfs *fsp)
16820Sstevel@tonic-gate {
16830Sstevel@tonic-gate 	struct buf *bp;
16840Sstevel@tonic-gate 	int nfat;
16850Sstevel@tonic-gate 	int	error;
16860Sstevel@tonic-gate 	struct fat32_boot_fsinfo fsinfo_disk;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	PC_DPRINTF0(7, "pcfs_syncfat\n");
16890Sstevel@tonic-gate 	if ((fsp->pcfs_fatp == (uchar_t *)0) ||
16900Sstevel@tonic-gate 	    !(fsp->pcfs_flags & PCFS_FATMOD))
16910Sstevel@tonic-gate 		return (0);
16920Sstevel@tonic-gate 	/*
16930Sstevel@tonic-gate 	 * write out all copies of FATs
16940Sstevel@tonic-gate 	 */
16950Sstevel@tonic-gate 	fsp->pcfs_flags &= ~PCFS_FATMOD;
16960Sstevel@tonic-gate 	fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT;
16970Sstevel@tonic-gate 	for (nfat = 0; nfat < fsp->pcfs_numfat; nfat++) {
16980Sstevel@tonic-gate 		error = pc_writefat(fsp,
16990Sstevel@tonic-gate 		    fsp->pcfs_fatstart + nfat*fsp->pcfs_fatsec);
17000Sstevel@tonic-gate 		if (error) {
17010Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
17020Sstevel@tonic-gate 			return (EIO);
17030Sstevel@tonic-gate 		}
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 	pc_clear_fatchanges(fsp);
17060Sstevel@tonic-gate 	PC_DPRINTF0(6, "pcfs_syncfat: wrote out FAT\n");
17070Sstevel@tonic-gate 	/* write out fsinfo */
17080Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
17090Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
17100Sstevel@tonic-gate 		    fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector),
17110Sstevel@tonic-gate 		    PC_SAFESECSIZE);
17120Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
17130Sstevel@tonic-gate 			brelse(bp);
17140Sstevel@tonic-gate 			return (EIO);
17150Sstevel@tonic-gate 		}
17160Sstevel@tonic-gate 		bcopy((void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
17170Sstevel@tonic-gate 		    &fsinfo_disk, sizeof (struct fat32_boot_fsinfo));
17180Sstevel@tonic-gate 		/* translate fields */
17190Sstevel@tonic-gate 		fsinfo_disk.fs_free_clusters =
17200Sstevel@tonic-gate 		    htoli(fsp->fsinfo_native.fs_free_clusters);
17210Sstevel@tonic-gate 		fsinfo_disk.fs_next_cluster = (uint32_t)FSINFO_UNKNOWN;
17220Sstevel@tonic-gate 		bcopy(&fsinfo_disk,
17230Sstevel@tonic-gate 		    (void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF),
17240Sstevel@tonic-gate 		    sizeof (struct fat32_boot_fsinfo));
17250Sstevel@tonic-gate 		bwrite2(bp);
17260Sstevel@tonic-gate 		error = geterror(bp);
17270Sstevel@tonic-gate 		brelse(bp);
17280Sstevel@tonic-gate 		if (error) {
17290Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
17300Sstevel@tonic-gate 			return (EIO);
17310Sstevel@tonic-gate 		}
17320Sstevel@tonic-gate 	}
17330Sstevel@tonic-gate 	return (0);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate void
17370Sstevel@tonic-gate pc_invalfat(struct pcfs *fsp)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 	struct pcfs *xfsp;
17400Sstevel@tonic-gate 	int mount_cnt = 0;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	PC_DPRINTF0(7, "pc_invalfat\n");
17430Sstevel@tonic-gate 	if (fsp->pcfs_fatp == (uchar_t *)0)
17440Sstevel@tonic-gate 		panic("pc_invalfat");
17450Sstevel@tonic-gate 	/*
17460Sstevel@tonic-gate 	 * Release FAT
17470Sstevel@tonic-gate 	 */
17480Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fatp, fsp->pcfs_fatsize);
17490Sstevel@tonic-gate 	fsp->pcfs_fatp = NULL;
17500Sstevel@tonic-gate 	kmem_free(fsp->pcfs_fat_changemap, fsp->pcfs_fat_changemapsize);
17510Sstevel@tonic-gate 	fsp->pcfs_fat_changemap = NULL;
17520Sstevel@tonic-gate 	/*
17530Sstevel@tonic-gate 	 * Invalidate all the blocks associated with the device.
17540Sstevel@tonic-gate 	 * Not needed if stateless.
17550Sstevel@tonic-gate 	 */
17560Sstevel@tonic-gate 	for (xfsp = pc_mounttab; xfsp; xfsp = xfsp->pcfs_nxt)
17570Sstevel@tonic-gate 		if (xfsp != fsp && xfsp->pcfs_xdev == fsp->pcfs_xdev)
17580Sstevel@tonic-gate 			mount_cnt++;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	if (!mount_cnt)
17610Sstevel@tonic-gate 		binval(fsp->pcfs_xdev);
17620Sstevel@tonic-gate 	/*
17630Sstevel@tonic-gate 	 * close mounted device
17640Sstevel@tonic-gate 	 */
17650Sstevel@tonic-gate 	(void) VOP_CLOSE(fsp->pcfs_devvp,
17660Sstevel@tonic-gate 	    (PCFSTOVFS(fsp)->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
17670Sstevel@tonic-gate 	    1, (offset_t)0, CRED());
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate void
17710Sstevel@tonic-gate pc_badfs(struct pcfs *fsp)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	cmn_err(CE_WARN, "corrupted PC file system on dev %x.%x\n",
17740Sstevel@tonic-gate 	    getmajor(fsp->pcfs_devvp->v_rdev),
17750Sstevel@tonic-gate 	    getminor(fsp->pcfs_devvp->v_rdev));
17760Sstevel@tonic-gate }
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate /*
17790Sstevel@tonic-gate  * The problem with supporting NFS on the PCFS filesystem is that there
17800Sstevel@tonic-gate  * is no good place to keep the generation number. The only possible
17810Sstevel@tonic-gate  * place is inside a directory entry. There are a few words that we
17820Sstevel@tonic-gate  * don't use - they store NT & OS/2 attributes, and the creation/last access
17830Sstevel@tonic-gate  * time of the file - but it seems wrong to use them. In addition, directory
17840Sstevel@tonic-gate  * entries come and go. If a directory is removed completely, its directory
17850Sstevel@tonic-gate  * blocks are freed and the generation numbers are lost. Whereas in ufs,
17860Sstevel@tonic-gate  * inode blocks are dedicated for inodes, so the generation numbers are
17870Sstevel@tonic-gate  * permanently kept on the disk.
17880Sstevel@tonic-gate  */
17890Sstevel@tonic-gate static int
17900Sstevel@tonic-gate pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	struct pcnode *pcp;
17930Sstevel@tonic-gate 	struct pc_fid *pcfid;
17940Sstevel@tonic-gate 	struct pcfs *fsp;
17950Sstevel@tonic-gate 	struct pcdir *ep;
17960Sstevel@tonic-gate 	daddr_t eblkno;
17970Sstevel@tonic-gate 	int eoffset;
17980Sstevel@tonic-gate 	struct buf *bp;
17990Sstevel@tonic-gate 	int error;
18000Sstevel@tonic-gate 	pc_cluster32_t	cn;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	pcfid = (struct pc_fid *)fidp;
18030Sstevel@tonic-gate 	fsp = VFSTOPCFS(vfsp);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	error = pc_lockfs(fsp, 0, 0);
18060Sstevel@tonic-gate 	if (error) {
18070Sstevel@tonic-gate 		*vpp = NULL;
18080Sstevel@tonic-gate 		return (error);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	if (pcfid->pcfid_block == 0) {
18120Sstevel@tonic-gate 		pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0);
18130Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
18140Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
18150Sstevel@tonic-gate 		pc_unlockfs(fsp);
18160Sstevel@tonic-gate 		return (0);
18170Sstevel@tonic-gate 	}
18180Sstevel@tonic-gate 	eblkno = pcfid->pcfid_block;
18190Sstevel@tonic-gate 	eoffset = pcfid->pcfid_offset;
18200Sstevel@tonic-gate 	if ((pc_dbtocl(fsp,
18210Sstevel@tonic-gate 	    eblkno - fsp->pcfs_dosstart) >= fsp->pcfs_ncluster) ||
18220Sstevel@tonic-gate 	    (eoffset > fsp->pcfs_clsize)) {
18230Sstevel@tonic-gate 		pc_unlockfs(fsp);
18240Sstevel@tonic-gate 		*vpp = NULL;
18250Sstevel@tonic-gate 		return (EINVAL);
18260Sstevel@tonic-gate 	}
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	if (eblkno >= fsp->pcfs_datastart || (eblkno-fsp->pcfs_rdirstart)
18290Sstevel@tonic-gate 	    < (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) {
18300Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno, fsp->pcfs_clsize);
18310Sstevel@tonic-gate 	} else {
18320Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev, eblkno,
18330Sstevel@tonic-gate 		    (int)(fsp->pcfs_datastart - eblkno) * fsp->pcfs_secsize);
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate 	if (bp->b_flags & (B_ERROR | B_STALE)) {
18360Sstevel@tonic-gate 		error = geterror(bp);
18370Sstevel@tonic-gate 		brelse(bp);
18380Sstevel@tonic-gate 		if (error)
18390Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
18400Sstevel@tonic-gate 		*vpp = NULL;
18410Sstevel@tonic-gate 		pc_unlockfs(fsp);
18420Sstevel@tonic-gate 		return (error);
18430Sstevel@tonic-gate 	}
18440Sstevel@tonic-gate 	ep = (struct pcdir *)(bp->b_un.b_addr + eoffset);
18450Sstevel@tonic-gate 	/*
18460Sstevel@tonic-gate 	 * Ok, if this is a valid file handle that we gave out,
18470Sstevel@tonic-gate 	 * then simply ensuring that the creation time matches,
18480Sstevel@tonic-gate 	 * the entry has not been deleted, and it has a valid first
18490Sstevel@tonic-gate 	 * character should be enough.
18500Sstevel@tonic-gate 	 *
18510Sstevel@tonic-gate 	 * Unfortunately, verifying that the <blkno, offset> _still_
18520Sstevel@tonic-gate 	 * refers to a directory entry is not easy, since we'd have
18530Sstevel@tonic-gate 	 * to search _all_ directories starting from root to find it.
18540Sstevel@tonic-gate 	 * That's a high price to pay just in case somebody is forging
18550Sstevel@tonic-gate 	 * file handles. So instead we verify that as much of the
18560Sstevel@tonic-gate 	 * entry is valid as we can:
18570Sstevel@tonic-gate 	 *
18580Sstevel@tonic-gate 	 * 1. The starting cluster is 0 (unallocated) or valid
18590Sstevel@tonic-gate 	 * 2. It is not an LFN entry
18600Sstevel@tonic-gate 	 * 3. It is not hidden (unless mounted as such)
18610Sstevel@tonic-gate 	 * 4. It is not the label
18620Sstevel@tonic-gate 	 */
18630Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, ep);
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * if the starting cluster is valid, but not valid according
18660Sstevel@tonic-gate 	 * to pc_validcl(), force it to be to simplify the following if.
18670Sstevel@tonic-gate 	 */
18680Sstevel@tonic-gate 	if (cn == 0)
18690Sstevel@tonic-gate 		cn = PCF_FIRSTCLUSTER;
18700Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
18710Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER32)
18720Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
18730Sstevel@tonic-gate 	} else {
18740Sstevel@tonic-gate 		if (cn >= PCF_LASTCLUSTER)
18750Sstevel@tonic-gate 			cn = PCF_FIRSTCLUSTER;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 	if ((!pc_validcl(fsp, cn)) ||
18780Sstevel@tonic-gate 	    (PCDL_IS_LFN(ep)) ||
18790Sstevel@tonic-gate 	    (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) ||
18800Sstevel@tonic-gate 	    ((ep->pcd_attr & PCA_LABEL) == PCA_LABEL)) {
18810Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
18820Sstevel@tonic-gate 		brelse(bp);
18830Sstevel@tonic-gate 		pc_unlockfs(fsp);
18840Sstevel@tonic-gate 		return (EINVAL);
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 	if ((ep->pcd_crtime.pct_time == pcfid->pcfid_ctime) &&
18870Sstevel@tonic-gate 	    (ep->pcd_filename[0] != PCD_ERASED) &&
18880Sstevel@tonic-gate 	    (pc_validchar(ep->pcd_filename[0]) ||
18890Sstevel@tonic-gate 		(ep->pcd_filename[0] == '.' && ep->pcd_filename[1] == '.'))) {
18900Sstevel@tonic-gate 		pcp = pc_getnode(fsp, eblkno, eoffset, ep);
18910Sstevel@tonic-gate 		pcp->pc_flags |= PC_EXTERNAL;
18920Sstevel@tonic-gate 		*vpp = PCTOV(pcp);
18930Sstevel@tonic-gate 	} else {
18940Sstevel@tonic-gate 		*vpp = NULL;
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate 	bp->b_flags |= B_STALE | B_AGE;
18970Sstevel@tonic-gate 	brelse(bp);
18980Sstevel@tonic-gate 	pc_unlockfs(fsp);
18990Sstevel@tonic-gate 	return (0);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate /*
1903*201Sjmcp  * if device is a PCMCIA pseudo floppy, return 1
19040Sstevel@tonic-gate  * otherwise, return 0
19050Sstevel@tonic-gate  */
19060Sstevel@tonic-gate static int
1907*201Sjmcp pcfs_pseudo_floppy(dev_t rdev)
19080Sstevel@tonic-gate {
19090Sstevel@tonic-gate 	int			error, err;
19100Sstevel@tonic-gate 	struct dk_cinfo		info;
19110Sstevel@tonic-gate 	ldi_handle_t		lh;
19120Sstevel@tonic-gate 	ldi_ident_t		li;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	err = ldi_ident_from_mod(&modlinkage, &li);
19150Sstevel@tonic-gate 	if (err) {
19160Sstevel@tonic-gate 		PC_DPRINTF1(1,
1917*201Sjmcp 		    "pcfs_pseudo_floppy: ldi_ident_from_mod err=%d\n", err);
19180Sstevel@tonic-gate 		return (0);
19190Sstevel@tonic-gate 	}
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	err = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD, CRED(), &lh, li);
19220Sstevel@tonic-gate 	ldi_ident_release(li);
19230Sstevel@tonic-gate 	if (err) {
19240Sstevel@tonic-gate 		PC_DPRINTF1(1,
1925*201Sjmcp 		    "pcfs_pseudo_floppy: ldi_open err=%d\n", err);
19260Sstevel@tonic-gate 		return (0);
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/* return value stored in err is purposfully ignored */
19300Sstevel@tonic-gate 	error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
19310Sstevel@tonic-gate 	    CRED(), &err);
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	err = ldi_close(lh, FREAD, CRED());
19340Sstevel@tonic-gate 	if (err != 0) {
19350Sstevel@tonic-gate 		PC_DPRINTF1(1,
1936*201Sjmcp 		    "pcfs_pseudo_floppy: ldi_close err=%d\n", err);
19370Sstevel@tonic-gate 		return (0);
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	if ((error == 0) && (info.dki_ctype == DKC_PCMCIA_MEM) &&
19410Sstevel@tonic-gate 		(info.dki_flags & DKI_PCMCIA_PFD))
19420Sstevel@tonic-gate 		return (1);
19430Sstevel@tonic-gate 	else
19440Sstevel@tonic-gate 		return (0);
19450Sstevel@tonic-gate }
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate /*
19480Sstevel@tonic-gate  * Unfortunately, FAT32 fat's can be pretty big (On a 1 gig jaz drive, about
19490Sstevel@tonic-gate  * a meg), so we can't bread() it all in at once. This routine reads a
19500Sstevel@tonic-gate  * fat a chunk at a time.
19510Sstevel@tonic-gate  */
19520Sstevel@tonic-gate static int
19530Sstevel@tonic-gate pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start, size_t fatsize)
19540Sstevel@tonic-gate {
19550Sstevel@tonic-gate 	struct buf *bp;
19560Sstevel@tonic-gate 	size_t off;
19570Sstevel@tonic-gate 	size_t readsize;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 	readsize = fsp->pcfs_clsize;
19600Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += readsize, fatp += readsize) {
19610Sstevel@tonic-gate 		if (readsize > (fatsize - off))
19620Sstevel@tonic-gate 			readsize = fatsize - off;
19630Sstevel@tonic-gate 		bp = bread(fsp->pcfs_xdev,
19640Sstevel@tonic-gate 		    pc_dbdaddr(fsp, start +
19650Sstevel@tonic-gate 			pc_cltodb(fsp, pc_lblkno(fsp, off))),
19660Sstevel@tonic-gate 		    readsize);
19670Sstevel@tonic-gate 		if (bp->b_flags & (B_ERROR | B_STALE)) {
19680Sstevel@tonic-gate 			brelse(bp);
19690Sstevel@tonic-gate 			return (EIO);
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 		bp->b_flags |= B_STALE | B_AGE;
19720Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, fatp, readsize);
19730Sstevel@tonic-gate 		brelse(bp);
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 	return (0);
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate  * We write the FAT out a _lot_, in order to make sure that it
19800Sstevel@tonic-gate  * is up-to-date. But on a FAT32 system (large drive, small clusters)
19810Sstevel@tonic-gate  * the FAT might be a couple of megabytes, and writing it all out just
19820Sstevel@tonic-gate  * because we created or deleted a small file is painful (especially
19830Sstevel@tonic-gate  * since we do it for each alternate FAT too). So instead, for FAT16 and
19840Sstevel@tonic-gate  * FAT32 we only write out the bit that has changed. We don't clear
19850Sstevel@tonic-gate  * the 'updated' fields here because the caller might be writing out
19860Sstevel@tonic-gate  * several FATs, so the caller must use pc_clear_fatchanges() after
19870Sstevel@tonic-gate  * all FATs have been updated.
19880Sstevel@tonic-gate  */
19890Sstevel@tonic-gate static int
19900Sstevel@tonic-gate pc_writefat(struct pcfs *fsp, daddr_t start)
19910Sstevel@tonic-gate {
19920Sstevel@tonic-gate 	struct buf *bp;
19930Sstevel@tonic-gate 	size_t off;
19940Sstevel@tonic-gate 	size_t writesize;
19950Sstevel@tonic-gate 	int	error;
19960Sstevel@tonic-gate 	uchar_t *fatp = fsp->pcfs_fatp;
19970Sstevel@tonic-gate 	size_t fatsize = fsp->pcfs_fatsize;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	writesize = fsp->pcfs_clsize;
20000Sstevel@tonic-gate 	for (off = 0; off < fatsize; off += writesize, fatp += writesize) {
20010Sstevel@tonic-gate 		if (writesize > (fatsize - off))
20020Sstevel@tonic-gate 			writesize = fatsize - off;
20030Sstevel@tonic-gate 		if (!pc_fat_is_changed(fsp, pc_lblkno(fsp, off))) {
20040Sstevel@tonic-gate 			continue;
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 		bp = ngeteblk(writesize);
20070Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
20080Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
20090Sstevel@tonic-gate 		bp->b_blkno = pc_dbdaddr(fsp, start +
20100Sstevel@tonic-gate 		    pc_cltodb(fsp, pc_lblkno(fsp, off)));
20110Sstevel@tonic-gate 		bcopy(fatp, bp->b_un.b_addr, writesize);
20120Sstevel@tonic-gate 		bwrite2(bp);
20130Sstevel@tonic-gate 		error = geterror(bp);
20140Sstevel@tonic-gate 		brelse(bp);
20150Sstevel@tonic-gate 		if (error) {
20160Sstevel@tonic-gate 			return (error);
20170Sstevel@tonic-gate 		}
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate 	return (0);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * Mark the FAT cluster that 'cn' is stored in as modified.
20240Sstevel@tonic-gate  */
20250Sstevel@tonic-gate void
20260Sstevel@tonic-gate pc_mark_fat_updated(struct pcfs *fsp, pc_cluster32_t cn)
20270Sstevel@tonic-gate {
20280Sstevel@tonic-gate 	pc_cluster32_t	bn;
20290Sstevel@tonic-gate 	size_t		size;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	/* which fat block is the cluster number stored in? */
20320Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
20330Sstevel@tonic-gate 		size = sizeof (pc_cluster32_t);
20340Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
20350Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
20360Sstevel@tonic-gate 	} else if (IS_FAT16(fsp)) {
20370Sstevel@tonic-gate 		size = sizeof (pc_cluster16_t);
20380Sstevel@tonic-gate 		bn = pc_lblkno(fsp, cn * size);
20390Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
20400Sstevel@tonic-gate 	} else {
20410Sstevel@tonic-gate 		offset_t off;
20420Sstevel@tonic-gate 		pc_cluster32_t nbn;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		ASSERT(IS_FAT12(fsp));
20450Sstevel@tonic-gate 		off = cn + (cn >> 1);
20460Sstevel@tonic-gate 		bn = pc_lblkno(fsp, off);
20470Sstevel@tonic-gate 		fsp->pcfs_fat_changemap[bn] = 1;
20480Sstevel@tonic-gate 		/* does this field wrap into the next fat cluster? */
20490Sstevel@tonic-gate 		nbn = pc_lblkno(fsp, off + 1);
20500Sstevel@tonic-gate 		if (nbn != bn) {
20510Sstevel@tonic-gate 			fsp->pcfs_fat_changemap[nbn] = 1;
20520Sstevel@tonic-gate 		}
20530Sstevel@tonic-gate 	}
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20570Sstevel@tonic-gate  * return whether the FAT cluster 'bn' is updated and needs to
20580Sstevel@tonic-gate  * be written out.
20590Sstevel@tonic-gate  */
20600Sstevel@tonic-gate int
20610Sstevel@tonic-gate pc_fat_is_changed(struct pcfs *fsp, pc_cluster32_t bn)
20620Sstevel@tonic-gate {
20630Sstevel@tonic-gate 	return (fsp->pcfs_fat_changemap[bn] == 1);
20640Sstevel@tonic-gate }
2065