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 77201Sjmcp 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, 151201Sjmcp "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 510201Sjmcp * with pseudo floppies organization 5110Sstevel@tonic-gate */ 512201Sjmcp 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 */ 787*607Swyllys if (!diskchanged && !(fsp->pcfs_flags & PCFS_IRRECOV)) { 788*607Swyllys int err; 789*607Swyllys if ((err = pc_getfat(fsp))) 790*607Swyllys return (err); 791*607Swyllys } 7920Sstevel@tonic-gate fsp->pcfs_flags |= PCFS_LOCKED; 7930Sstevel@tonic-gate fsp->pcfs_owner = curthread; 7940Sstevel@tonic-gate fsp->pcfs_count++; 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate return (0); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate void 8000Sstevel@tonic-gate pc_unlockfs(struct pcfs *fsp) 8010Sstevel@tonic-gate { 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate if ((fsp->pcfs_flags & PCFS_LOCKED) == 0) 8040Sstevel@tonic-gate panic("pc_unlockfs"); 8050Sstevel@tonic-gate if (--fsp->pcfs_count < 0) 8060Sstevel@tonic-gate panic("pc_unlockfs: count"); 8070Sstevel@tonic-gate if (fsp->pcfs_count == 0) { 8080Sstevel@tonic-gate fsp->pcfs_flags &= ~PCFS_LOCKED; 8090Sstevel@tonic-gate fsp->pcfs_owner = 0; 8100Sstevel@tonic-gate mutex_exit(&fsp->pcfs_lock); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate /* 8150Sstevel@tonic-gate * isDosDrive() 8160Sstevel@tonic-gate * Boolean function. Give it the systid field for an fdisk partition 8170Sstevel@tonic-gate * and it decides if that's a systid that describes a DOS drive. We 8180Sstevel@tonic-gate * use systid values defined in sys/dktp/fdisk.h. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate static int 8210Sstevel@tonic-gate isDosDrive(uchar_t checkMe) 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate return ((checkMe == DOSOS12) || (checkMe == DOSOS16) || 8240Sstevel@tonic-gate (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) || 8250Sstevel@tonic-gate (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) || 8260Sstevel@tonic-gate (checkMe == DIAGPART)); 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * isDosExtended() 8310Sstevel@tonic-gate * Boolean function. Give it the systid field for an fdisk partition 8320Sstevel@tonic-gate * and it decides if that's a systid that describes an extended DOS 8330Sstevel@tonic-gate * partition. 8340Sstevel@tonic-gate */ 8350Sstevel@tonic-gate static int 8360Sstevel@tonic-gate isDosExtended(uchar_t checkMe) 8370Sstevel@tonic-gate { 8380Sstevel@tonic-gate return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA)); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate /* 8420Sstevel@tonic-gate * isBootPart() 8430Sstevel@tonic-gate * Boolean function. Give it the systid field for an fdisk partition 8440Sstevel@tonic-gate * and it decides if that's a systid that describes a Solaris boot 8450Sstevel@tonic-gate * partition. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate static int 8480Sstevel@tonic-gate isBootPart(uchar_t checkMe) 8490Sstevel@tonic-gate { 8500Sstevel@tonic-gate return (checkMe == X86BOOT); 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* 8540Sstevel@tonic-gate * noLogicalDrive() 8550Sstevel@tonic-gate * Display error message about not being able to find a logical 8560Sstevel@tonic-gate * drive. 8570Sstevel@tonic-gate */ 8580Sstevel@tonic-gate static void 8590Sstevel@tonic-gate noLogicalDrive(int requested) 8600Sstevel@tonic-gate { 8610Sstevel@tonic-gate if (requested == BOOT_PARTITION_DRIVE) { 8620Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: no boot partition"); 8630Sstevel@tonic-gate } else { 8640Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: no such logical drive"); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* 8690Sstevel@tonic-gate * findTheDrive() 8700Sstevel@tonic-gate * Discover offset of the requested logical drive, and return 8710Sstevel@tonic-gate * that offset (startSector), the systid of that drive (sysid), 8720Sstevel@tonic-gate * and a buffer pointer (bp), with the buffer contents being 8730Sstevel@tonic-gate * the first sector of the logical drive (i.e., the sector that 8740Sstevel@tonic-gate * contains the BPB for that drive). 8750Sstevel@tonic-gate */ 8760Sstevel@tonic-gate static int 8770Sstevel@tonic-gate findTheDrive(dev_t dev, int askedFor, int *error, buf_t **bp, 8780Sstevel@tonic-gate daddr_t *startSector, uchar_t *sysid) 8790Sstevel@tonic-gate { 8800Sstevel@tonic-gate struct ipart dosp[FD_NUMPART]; /* incore fdisk partition structure */ 8810Sstevel@tonic-gate struct mboot *dosp_ptr; /* boot structure pointer */ 8820Sstevel@tonic-gate daddr_t lastseek = 0; /* Disk block we sought previously */ 8830Sstevel@tonic-gate daddr_t diskblk = 0; /* Disk block to get */ 8840Sstevel@tonic-gate daddr_t xstartsect; /* base of Extended DOS partition */ 8850Sstevel@tonic-gate int logicalDriveCount = 0; /* Count of logical drives seen */ 8860Sstevel@tonic-gate int extendedPart = -1; /* index of extended dos partition */ 8870Sstevel@tonic-gate int primaryPart = -1; /* index of primary dos partition */ 8880Sstevel@tonic-gate int bootPart = -1; /* index of a Solaris boot partition */ 8890Sstevel@tonic-gate int xnumsect = -1; /* length of extended DOS partition */ 8900Sstevel@tonic-gate int driveIndex; /* computed FDISK table index */ 8910Sstevel@tonic-gate int i; 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Count of drives in the current extended partition's 8940Sstevel@tonic-gate * FDISK table, and indexes of the drives themselves. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate int extndDrives[FD_NUMPART]; 8970Sstevel@tonic-gate int numDrives = 0; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /* 9000Sstevel@tonic-gate * Count of drives (beyond primary) in master boot record's 9010Sstevel@tonic-gate * FDISK table, and indexes of the drives themselves. 9020Sstevel@tonic-gate */ 9030Sstevel@tonic-gate int extraDrives[FD_NUMPART]; 9040Sstevel@tonic-gate int numExtraDrives = 0; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * Copy from disk block into memory aligned structure for fdisk usage. 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr; 9100Sstevel@tonic-gate bcopy(dosp_ptr->parts, dosp, sizeof (struct ipart) * FD_NUMPART); 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* 9130Sstevel@tonic-gate * Get a summary of what is in the Master FDISK table. 9140Sstevel@tonic-gate * Normally we expect to find one partition marked as a DOS drive. 9150Sstevel@tonic-gate * This partition is the one Windows calls the primary dos partition. 9160Sstevel@tonic-gate * If the machine has any logical drives then we also expect 9170Sstevel@tonic-gate * to find a partition marked as an extended DOS partition. 9180Sstevel@tonic-gate * 9190Sstevel@tonic-gate * Sometimes we'll find multiple partitions marked as DOS drives. 9200Sstevel@tonic-gate * The Solaris fdisk program allows these partitions 9210Sstevel@tonic-gate * to be created, but Windows fdisk no longer does. We still need 9220Sstevel@tonic-gate * to support these, though, since Windows does. We also need to fix 9230Sstevel@tonic-gate * our fdisk to behave like the Windows version. 9240Sstevel@tonic-gate * 9250Sstevel@tonic-gate * It turns out that some off-the-shelf media have *only* an 9260Sstevel@tonic-gate * Extended partition, so we need to deal with that case as well. 9270Sstevel@tonic-gate * 9280Sstevel@tonic-gate * Only a single (the first) Extended or Boot Partition will 9290Sstevel@tonic-gate * be recognized. Any others will be ignored. 9300Sstevel@tonic-gate */ 9310Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 932*607Swyllys PC_DPRINTF1(2, "findTheDrive: found partition type %02x", 933*607Swyllys dosp[i].systid); 934*607Swyllys 9350Sstevel@tonic-gate if (isDosDrive(dosp[i].systid)) { 9360Sstevel@tonic-gate if (primaryPart < 0) { 9370Sstevel@tonic-gate logicalDriveCount++; 9380Sstevel@tonic-gate primaryPart = i; 9390Sstevel@tonic-gate } else { 9400Sstevel@tonic-gate extraDrives[numExtraDrives++] = i; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate continue; 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate if ((extendedPart < 0) && isDosExtended(dosp[i].systid)) { 9450Sstevel@tonic-gate extendedPart = i; 9460Sstevel@tonic-gate continue; 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate if ((bootPart < 0) && isBootPart(dosp[i].systid)) { 9490Sstevel@tonic-gate bootPart = i; 9500Sstevel@tonic-gate continue; 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (askedFor == BOOT_PARTITION_DRIVE) { 9550Sstevel@tonic-gate if (bootPart < 0) { 9560Sstevel@tonic-gate noLogicalDrive(askedFor); 9570Sstevel@tonic-gate *error = EINVAL; 9580Sstevel@tonic-gate return (0); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate *sysid = dosp[bootPart].systid; 9610Sstevel@tonic-gate *startSector = ltohi(dosp[bootPart].relsect); 9620Sstevel@tonic-gate return (1); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate if (askedFor == PRIMARY_DOS_DRIVE && primaryPart >= 0) { 9660Sstevel@tonic-gate *sysid = dosp[primaryPart].systid; 9670Sstevel@tonic-gate *startSector = ltohi(dosp[primaryPart].relsect); 9680Sstevel@tonic-gate return (1); 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* 9720Sstevel@tonic-gate * We are not looking for the C: drive (or the primary drive 9730Sstevel@tonic-gate * was not found), so we had better have an extended partition 9740Sstevel@tonic-gate * or extra drives in the Master FDISK table. 9750Sstevel@tonic-gate */ 9760Sstevel@tonic-gate if ((extendedPart < 0) && (numExtraDrives == 0)) { 9770Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: no extended dos partition"); 9780Sstevel@tonic-gate noLogicalDrive(askedFor); 9790Sstevel@tonic-gate *error = EINVAL; 9800Sstevel@tonic-gate return (0); 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate if (extendedPart >= 0) { 9840Sstevel@tonic-gate diskblk = xstartsect = ltohi(dosp[extendedPart].relsect); 9850Sstevel@tonic-gate xnumsect = ltohi(dosp[extendedPart].numsect); 9860Sstevel@tonic-gate do { 9870Sstevel@tonic-gate /* 9880Sstevel@tonic-gate * If the seek would not cause us to change 9890Sstevel@tonic-gate * position on the drive, then we're out of 9900Sstevel@tonic-gate * extended partitions to examine. 9910Sstevel@tonic-gate */ 9920Sstevel@tonic-gate if (diskblk == lastseek) 9930Sstevel@tonic-gate break; 9940Sstevel@tonic-gate logicalDriveCount += numDrives; 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Seek the next extended partition, and find 9970Sstevel@tonic-gate * logical drives within it. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate brelse(*bp); 10000Sstevel@tonic-gate *bp = bread(dev, diskblk, PC_SAFESECSIZE); 10010Sstevel@tonic-gate if ((*bp)->b_flags & B_ERROR) { 10020Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getfattype: read error\n"); 10030Sstevel@tonic-gate *error = EIO; 10040Sstevel@tonic-gate return (0); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate lastseek = diskblk; 10070Sstevel@tonic-gate dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr; 10080Sstevel@tonic-gate if (ltohs(dosp_ptr->signature) != MBB_MAGIC) { 10090Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: " 10100Sstevel@tonic-gate "extended partition signature err"); 10110Sstevel@tonic-gate *error = EINVAL; 10120Sstevel@tonic-gate return (0); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate bcopy(dosp_ptr->parts, dosp, 10150Sstevel@tonic-gate sizeof (struct ipart) * FD_NUMPART); 10160Sstevel@tonic-gate /* 10170Sstevel@tonic-gate * Count up drives, and track where the next 10180Sstevel@tonic-gate * extended partition is in case we need it. We 10190Sstevel@tonic-gate * are expecting only one extended partition. If 10200Sstevel@tonic-gate * there is more than one we'll only go to the 10210Sstevel@tonic-gate * first one we see, but warn about ignoring. 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate numDrives = 0; 10240Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 10250Sstevel@tonic-gate if (isDosDrive(dosp[i].systid)) { 10260Sstevel@tonic-gate extndDrives[numDrives++] = i; 10270Sstevel@tonic-gate continue; 10280Sstevel@tonic-gate } else if (isDosExtended(dosp[i].systid)) { 10290Sstevel@tonic-gate if (diskblk != lastseek) { 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * Already found an extended 10320Sstevel@tonic-gate * partition in this table. 10330Sstevel@tonic-gate */ 10340Sstevel@tonic-gate cmn_err(CE_NOTE, 10350Sstevel@tonic-gate "!pcfs: ignoring unexpected" 10360Sstevel@tonic-gate " additional extended" 10370Sstevel@tonic-gate " partition"); 10380Sstevel@tonic-gate continue; 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate diskblk = xstartsect + 10410Sstevel@tonic-gate ltohi(dosp[i].relsect); 10420Sstevel@tonic-gate continue; 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate } while (askedFor > logicalDriveCount + numDrives); 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate if (askedFor <= logicalDriveCount + numDrives) { 10480Sstevel@tonic-gate /* 10490Sstevel@tonic-gate * The number of logical drives we've found thus 10500Sstevel@tonic-gate * far is enough to get us to the one we were 10510Sstevel@tonic-gate * searching for. 10520Sstevel@tonic-gate */ 10530Sstevel@tonic-gate driveIndex = logicalDriveCount + numDrives - askedFor; 10540Sstevel@tonic-gate *sysid = dosp[extndDrives[driveIndex]].systid; 10550Sstevel@tonic-gate *startSector = 10560Sstevel@tonic-gate ltohi(dosp[extndDrives[driveIndex]].relsect) + 10570Sstevel@tonic-gate lastseek; 10580Sstevel@tonic-gate if (*startSector > (xstartsect + xnumsect)) { 10590Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: extended partition " 10600Sstevel@tonic-gate "values bad"); 10610Sstevel@tonic-gate *error = EINVAL; 10620Sstevel@tonic-gate return (0); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate return (1); 10650Sstevel@tonic-gate } else { 10660Sstevel@tonic-gate /* 10670Sstevel@tonic-gate * We ran out of extended dos partition 10680Sstevel@tonic-gate * drives. The only hope now is to go 10690Sstevel@tonic-gate * back to extra drives defined in the master 10700Sstevel@tonic-gate * fdisk table. But we overwrote that table 10710Sstevel@tonic-gate * already, so we must load it in again. 10720Sstevel@tonic-gate */ 10730Sstevel@tonic-gate logicalDriveCount += numDrives; 10740Sstevel@tonic-gate brelse(*bp); 10750Sstevel@tonic-gate *bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE); 10760Sstevel@tonic-gate if ((*bp)->b_flags & B_ERROR) { 10770Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getfattype: read error\n"); 10780Sstevel@tonic-gate *error = EIO; 10790Sstevel@tonic-gate return (0); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate dosp_ptr = (struct mboot *)(*bp)->b_un.b_addr; 10820Sstevel@tonic-gate bcopy(dosp_ptr->parts, dosp, 10830Sstevel@tonic-gate sizeof (struct ipart) * FD_NUMPART); 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate /* 10870Sstevel@tonic-gate * Still haven't found the drive, is it an extra 10880Sstevel@tonic-gate * drive defined in the main FDISK table? 10890Sstevel@tonic-gate */ 10900Sstevel@tonic-gate if (askedFor <= logicalDriveCount + numExtraDrives) { 10910Sstevel@tonic-gate driveIndex = logicalDriveCount + numExtraDrives - askedFor; 10920Sstevel@tonic-gate *sysid = dosp[extraDrives[driveIndex]].systid; 10930Sstevel@tonic-gate *startSector = ltohi(dosp[extraDrives[driveIndex]].relsect); 10940Sstevel@tonic-gate return (1); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate /* 10970Sstevel@tonic-gate * Still haven't found the drive, and there is 10980Sstevel@tonic-gate * nowhere else to look. 10990Sstevel@tonic-gate */ 11000Sstevel@tonic-gate noLogicalDrive(askedFor); 11010Sstevel@tonic-gate *error = EINVAL; 11020Sstevel@tonic-gate return (0); 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate /* 1106*607Swyllys * FAT12/FAT16 specific consistency checks. 1107*607Swyllys */ 1108*607Swyllys static int 1109*607Swyllys check_bpb_fat16(struct bootsec *bpb) 1110*607Swyllys { 1111*607Swyllys if (pcfsdebuglevel >= 5) { 1112*607Swyllys PC_DPRINTF1(5, "check_bpb_fat: RootEntCount = %d", 1113*607Swyllys ltohs(bpb->rdirents[0])); 1114*607Swyllys PC_DPRINTF1(5, "check_bpb_fat16: TotSec16 = %d", 1115*607Swyllys ltohs(bpb->numsect[0])); 1116*607Swyllys PC_DPRINTF1(5, "check_bpb_fat16: FATSz16 = %d", 1117*607Swyllys ltohs(bpb->fatsec)); 1118*607Swyllys PC_DPRINTF1(5, "check_bpb_fat16: TotSec32 = %d", 1119*607Swyllys ltohi(bpb->totalsec)); 1120*607Swyllys } 1121*607Swyllys return (ltohs(bpb->rdirents[0]) > 0 && /* RootEntCnt > 0 */ 1122*607Swyllys ((ltohs(bpb->numsect[0]) == 0 && /* TotSec16 == 0 */ 1123*607Swyllys ltohi(bpb->totalsec) > 0) || /* TotSec32 > 0 */ 1124*607Swyllys ltohs(bpb->numsect[0]) > 0) && /* TotSec16 > 0 */ 1125*607Swyllys ltohs(bpb->fatsec) > 0); /* FatSz16 > 0 */ 1126*607Swyllys } 1127*607Swyllys 1128*607Swyllys /* 1129*607Swyllys * FAT32 specific consistency checks. 1130*607Swyllys */ 1131*607Swyllys static int 1132*607Swyllys check_bpb_fat32(struct fat32_bootsec *bpb) 1133*607Swyllys { 1134*607Swyllys if (pcfsdebuglevel >= 5) { 1135*607Swyllys PC_DPRINTF1(5, "check_bpb_fat32: RootEntCount = %d", 1136*607Swyllys ltohs(bpb->f_bs.rdirents[0])); 1137*607Swyllys PC_DPRINTF1(5, "check_bpb_fat32: TotSec16 = %d", 1138*607Swyllys ltohs(bpb->f_bs.numsect[0])); 1139*607Swyllys PC_DPRINTF1(5, "check_bpb_fat32: FATSz16 = %d", 1140*607Swyllys ltohs(bpb->f_bs.fatsec)); 1141*607Swyllys PC_DPRINTF1(5, "check_bpb_fat32: TotSec32 = %d", 1142*607Swyllys ltohi(bpb->f_bs.totalsec)); 1143*607Swyllys PC_DPRINTF1(5, "check_bpb_fat32: FATSz32 = %d", 1144*607Swyllys ltohi(bpb->f_fatlength)); 1145*607Swyllys } 1146*607Swyllys return (ltohs(bpb->f_bs.rdirents[0]) == 0 && 1147*607Swyllys ltohs(bpb->f_bs.numsect[0]) == 0 && 1148*607Swyllys ltohs(bpb->f_bs.fatsec) == 0 && 1149*607Swyllys ltohi(bpb->f_bs.totalsec) > 0 && 1150*607Swyllys ltohi(bpb->f_fatlength) > 0); 1151*607Swyllys } 1152*607Swyllys 1153*607Swyllys /* 1154*607Swyllys * Calculate the number of clusters in order to determine 1155*607Swyllys * the type of FAT we are looking at. This is the only 1156*607Swyllys * recommended way of determining FAT type, though there 1157*607Swyllys * are other hints in the data, this is the best way. 1158*607Swyllys */ 1159*607Swyllys static ulong_t 1160*607Swyllys bpb_to_numclusters(uchar_t *cp) 1161*607Swyllys { 1162*607Swyllys struct fat32_bootsec *bpb; 1163*607Swyllys 1164*607Swyllys ulong_t rootdirsectors; 1165*607Swyllys ulong_t FATsz; 1166*607Swyllys ulong_t TotSec; 1167*607Swyllys ulong_t DataSec; 1168*607Swyllys ulong_t CountOfClusters; 1169*607Swyllys char FileSysType[9]; 1170*607Swyllys 1171*607Swyllys /* 1172*607Swyllys * Cast it to FAT32 bpb. If it turns out to be FAT12/16, its 1173*607Swyllys * OK, we won't try accessing the data beyond the FAT16 header 1174*607Swyllys * boundary. 1175*607Swyllys */ 1176*607Swyllys bpb = (struct fat32_bootsec *)cp; 1177*607Swyllys 1178*607Swyllys if (pcfsdebuglevel >= 5) { 1179*607Swyllys if (ltohs(bpb->f_bs.rdirents[0]) != 0) { 1180*607Swyllys memcpy(FileSysType, &cp[54], 8); 1181*607Swyllys FileSysType[8] = 0; 1182*607Swyllys PC_DPRINTF1(5, "debug_bpb: FAT12/FAT16 FileSysType = " 1183*607Swyllys "%s", FileSysType); 1184*607Swyllys } 1185*607Swyllys } 1186*607Swyllys 1187*607Swyllys rootdirsectors = ((ltohs(bpb->f_bs.rdirents[0]) * 32) + 1188*607Swyllys (ltohs(bpb->f_bs.bps[0]) - 1)) / ltohs(bpb->f_bs.bps[0]); 1189*607Swyllys 1190*607Swyllys if (ltohs(bpb->f_bs.fatsec) != 0) 1191*607Swyllys FATsz = ltohs(bpb->f_bs.fatsec); 1192*607Swyllys else 1193*607Swyllys FATsz = ltohi(bpb->f_fatlength); 1194*607Swyllys 1195*607Swyllys if (ltohs(bpb->f_bs.numsect[0]) != 0) 1196*607Swyllys TotSec = ltohs(bpb->f_bs.numsect[0]); 1197*607Swyllys else 1198*607Swyllys TotSec = ltohi(bpb->f_bs.totalsec); 1199*607Swyllys 1200*607Swyllys DataSec = TotSec - (ltohs(bpb->f_bs.res_sec[0]) + 1201*607Swyllys (bpb->f_bs.nfat * FATsz) + rootdirsectors); 1202*607Swyllys 1203*607Swyllys CountOfClusters = DataSec / bpb->f_bs.spcl; 1204*607Swyllys 1205*607Swyllys PC_DPRINTF1(5, "debug_bpb: CountOfClusters = %ld", CountOfClusters); 1206*607Swyllys 1207*607Swyllys return (CountOfClusters); 1208*607Swyllys 1209*607Swyllys } 1210*607Swyllys 1211*607Swyllys static int 1212*607Swyllys fattype(ulong_t CountOfClusters) 1213*607Swyllys { 1214*607Swyllys /* 1215*607Swyllys * From Microsoft: 1216*607Swyllys * In the following example, when it says <, it does not mean <=. 1217*607Swyllys * Note also that the numbers are correct. The first number for 1218*607Swyllys * FAT12 is 4085; the second number for FAT16 is 65525. These numbers 1219*607Swyllys * and the '<' signs are not wrong. 1220*607Swyllys */ 1221*607Swyllys 1222*607Swyllys /* Watch for edge cases */ 1223*607Swyllys if ((CountOfClusters >= 4085 && CountOfClusters <= 4095) || 1224*607Swyllys (CountOfClusters >= 65525 && CountOfClusters <= 65535)) { 1225*607Swyllys PC_DPRINTF1(5, "debug_bpb: Cannot determine FAT yet - %ld", 1226*607Swyllys CountOfClusters); 1227*607Swyllys return (-1); /* Cannot be determined yet */ 1228*607Swyllys } else if (CountOfClusters < 4085) { 1229*607Swyllys /* Volume is FAT12 */ 1230*607Swyllys PC_DPRINTF0(5, "debug_bpb: This must be FAT12"); 1231*607Swyllys return (0); 1232*607Swyllys } else if (CountOfClusters < 65525) { 1233*607Swyllys /* Volume is FAT16 */ 1234*607Swyllys PC_DPRINTF0(5, "debug_bpb: This must be FAT16"); 1235*607Swyllys return (PCFS_FAT16); 1236*607Swyllys } else { 1237*607Swyllys /* Volume is FAT32 */ 1238*607Swyllys PC_DPRINTF0(5, "debug_bpb: This must be FAT32"); 1239*607Swyllys return (PCFS_FAT32); 1240*607Swyllys } 1241*607Swyllys } 1242*607Swyllys 1243*607Swyllys #define VALID_SECSIZE(s) (s == 512 || s == 1024 || s == 2048 || s == 4096) 1244*607Swyllys 1245*607Swyllys #define VALID_SPCL(s) (s == 1 || s == 2 || s == 4 || s == 8 || s == 16 ||\ 1246*607Swyllys s == 32 || s == 64 || s == 128) 1247*607Swyllys 1248*607Swyllys static int 1249*607Swyllys secondaryBPBChecks(uchar_t *cp) 1250*607Swyllys { 1251*607Swyllys struct bootsec *bpb = (struct bootsec *)cp; 1252*607Swyllys struct fat32_bootsec *f32bpb = (struct fat32_bootsec *)cp; 1253*607Swyllys 1254*607Swyllys /* 1255*607Swyllys * Perform secondary checks to try and determine what sort 1256*607Swyllys * of FAT partition we have based on other, less reliable, 1257*607Swyllys * data in the BPB header. 1258*607Swyllys */ 1259*607Swyllys if (ltohs(bpb->fatsec) != 0) { 1260*607Swyllys /* 1261*607Swyllys * Must be FAT12 or FAT16, check the 1262*607Swyllys * FilSysType string (not 100% reliable). 1263*607Swyllys */ 1264*607Swyllys if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT12", 5)) { 1265*607Swyllys PC_DPRINTF0(5, "secondaryBPBCheck says: FAT12"); 1266*607Swyllys return (0); /* FAT12 */ 1267*607Swyllys } else if (!memcmp((cp + PCFS_TYPESTRING_OFFSET16), "FAT16", 1268*607Swyllys 5)) { 1269*607Swyllys PC_DPRINTF0(5, "secondaryBPBCheck says: FAT16"); 1270*607Swyllys return (PCFS_FAT16); 1271*607Swyllys } else { 1272*607Swyllys /* 1273*607Swyllys * Try to use the BPB_Media byte 1274*607Swyllys * 1275*607Swyllys * If the media byte indicates a floppy we'll 1276*607Swyllys * assume FAT12, otherwise we'll assume FAT16. 1277*607Swyllys */ 1278*607Swyllys switch (bpb->mediadesriptor) { 1279*607Swyllys case SS8SPT: 1280*607Swyllys case DS8SPT: 1281*607Swyllys case SS9SPT: 1282*607Swyllys case DS9SPT: 1283*607Swyllys case DS18SPT: 1284*607Swyllys case DS9_15SPT: 1285*607Swyllys PC_DPRINTF0(5, 1286*607Swyllys "secondaryBPBCheck says: FAT12"); 1287*607Swyllys return (0); /* FAT12 */ 1288*607Swyllys case MD_FIXED: 1289*607Swyllys PC_DPRINTF0(5, 1290*607Swyllys "secondaryBPBCheck says: FAT16"); 1291*607Swyllys return (PCFS_FAT16); 1292*607Swyllys default: 1293*607Swyllys cmn_err(CE_NOTE, 1294*607Swyllys "!pcfs: unknown FAT type"); 1295*607Swyllys return (-1); 1296*607Swyllys } 1297*607Swyllys } 1298*607Swyllys } else if (ltohi(f32bpb->f_fatlength) > 0) { 1299*607Swyllys PC_DPRINTF0(5, "secondaryBPBCheck says: FAT32"); 1300*607Swyllys return (PCFS_FAT32); 1301*607Swyllys } else { 1302*607Swyllys /* We don't know */ 1303*607Swyllys PC_DPRINTF0(5, "secondaryBPBCheck says: unknown!!"); 1304*607Swyllys return (-1); 1305*607Swyllys } 1306*607Swyllys } 1307*607Swyllys 1308*607Swyllys /* 1309*607Swyllys * Check to see if the BPB we found is correct. 1310*607Swyllys * 1311*607Swyllys * First, look for obvious, tell-tale signs of trouble: 1312*607Swyllys * The NumFATs value should always be 2. Sometimes it can be a '1' 1313*607Swyllys * on FLASH memory cards and other non-disk-based media, so we 1314*607Swyllys * will allow that as well. 1315*607Swyllys * 1316*607Swyllys * We also look at the Media byte, the valid range is 0xF0, or 1317*607Swyllys * 0xF8 thru 0xFF, anything else means this is probably not a good 1318*607Swyllys * BPB. 1319*607Swyllys * 1320*607Swyllys * Finally, check the BPB Magic number at the end of the 512 byte 1321*607Swyllys * block, it must be 0xAA55. 1322*607Swyllys * 1323*607Swyllys * If that all is good, calculate the number of clusters and 1324*607Swyllys * do some final verification steps. 1325*607Swyllys * 1326*607Swyllys * If all is well, return success (1) and set the fattypep 1327*607Swyllys * value to the correct FAT value. 1328*607Swyllys */ 1329*607Swyllys static int 1330*607Swyllys isBPB(uchar_t *cp, int *fattypep) 1331*607Swyllys { 1332*607Swyllys int ret = 1; 1333*607Swyllys struct bootsec *bpb = (struct bootsec *)cp; 1334*607Swyllys 1335*607Swyllys uint_t numclusters; /* number of clusters in file area */ 1336*607Swyllys ushort_t secsize = (int)ltohs(bpb->bps[0]); 1337*607Swyllys 1338*607Swyllys if (pcfsdebuglevel >= 3) { 1339*607Swyllys if (!VALID_SECSIZE(secsize)) 1340*607Swyllys PC_DPRINTF1(3, "check_bpb: invalid bps value %d", 1341*607Swyllys secsize); 1342*607Swyllys 1343*607Swyllys if (!VALID_SPCL(bpb->spcl)) 1344*607Swyllys PC_DPRINTF1(3, "check_bpb: invalid spcl value %d", 1345*607Swyllys bpb->spcl); 1346*607Swyllys 1347*607Swyllys if ((secsize * bpb->spcl) >= (32 * 1024)) 1348*607Swyllys PC_DPRINTF3(3, "check_bpb: BPC > 32K %d x %d = %d", 1349*607Swyllys secsize, 1350*607Swyllys bpb->spcl, 1351*607Swyllys secsize * bpb->spcl); 1352*607Swyllys 1353*607Swyllys if (bpb->nfat == 0) 1354*607Swyllys PC_DPRINTF1(3, "check_bpb: bad NumFATs value %d", 1355*607Swyllys bpb->nfat); 1356*607Swyllys 1357*607Swyllys if (ltohs(bpb->res_sec[0]) == 0) 1358*607Swyllys PC_DPRINTF1(3, "check_bpb: bad RsvdSecCnt value %d", 1359*607Swyllys ltohs(bpb->res_sec[0])); 1360*607Swyllys 1361*607Swyllys PC_DPRINTF1(5, "check_bpb: Media byte = %02x", 1362*607Swyllys bpb->mediadesriptor); 1363*607Swyllys 1364*607Swyllys } 1365*607Swyllys if ((bpb->nfat == 0) || 1366*607Swyllys (bpb->mediadesriptor != 0xF0 && bpb->mediadesriptor < 0xF8) || 1367*607Swyllys (ltohs(cp[510]) != MBB_MAGIC) || 1368*607Swyllys !VALID_SECSIZE(secsize) || 1369*607Swyllys !VALID_SPCL(bpb->spcl) || 1370*607Swyllys (secsize * bpb->spcl >= (64 * 1024)) || 1371*607Swyllys !(ltohs(bpb->res_sec[0]))) 1372*607Swyllys return (0); 1373*607Swyllys 1374*607Swyllys /* 1375*607Swyllys * Basic sanity checks passed so far, now try to determine which 1376*607Swyllys * FAT format to use. 1377*607Swyllys */ 1378*607Swyllys numclusters = bpb_to_numclusters(cp); 1379*607Swyllys 1380*607Swyllys *fattypep = fattype(numclusters); 1381*607Swyllys 1382*607Swyllys /* Do some final sanity checks for each specific type of FAT */ 1383*607Swyllys switch (*fattypep) { 1384*607Swyllys case 0: /* FAT12 */ 1385*607Swyllys case PCFS_FAT16: 1386*607Swyllys if (!check_bpb_fat16((struct bootsec *)cp)) 1387*607Swyllys return (0); 1388*607Swyllys break; 1389*607Swyllys case PCFS_FAT32: 1390*607Swyllys if (!check_bpb_fat32((struct fat32_bootsec *)cp)) 1391*607Swyllys return (0); 1392*607Swyllys break; 1393*607Swyllys default: /* not sure yet */ 1394*607Swyllys *fattypep = secondaryBPBChecks(cp); 1395*607Swyllys if (*fattypep == -1) { 1396*607Swyllys /* Still nothing, give it up. */ 1397*607Swyllys return (0); 1398*607Swyllys } 1399*607Swyllys break; 1400*607Swyllys } 1401*607Swyllys PC_DPRINTF0(5, "isBPB: BPB passes verification tests"); 1402*607Swyllys return (1); 1403*607Swyllys } 1404*607Swyllys 1405*607Swyllys 1406*607Swyllys /* 14070Sstevel@tonic-gate * Get the FAT type for the DOS medium. 14080Sstevel@tonic-gate * 1409*607Swyllys * ------------------------- 1410*607Swyllys * According to Microsoft: 1411*607Swyllys * The FAT type one of FAT12, FAT16, or FAT32 is determined by the 1412*607Swyllys * count of clusters on the volume and nothing else. 1413*607Swyllys * ------------------------- 14140Sstevel@tonic-gate * 14150Sstevel@tonic-gate */ 14160Sstevel@tonic-gate static int 14170Sstevel@tonic-gate pc_getfattype( 14180Sstevel@tonic-gate struct vnode *devvp, 14190Sstevel@tonic-gate int ldrive, 14200Sstevel@tonic-gate daddr_t *strtsectp, 14210Sstevel@tonic-gate int *fattypep) 14220Sstevel@tonic-gate { 14230Sstevel@tonic-gate uchar_t *cp; /* for searching out FAT string */ 14240Sstevel@tonic-gate buf_t *bp = NULL; /* Disk buffer pointer */ 14250Sstevel@tonic-gate int rval = 0; 14260Sstevel@tonic-gate uchar_t sysid = 0; /* System ID character */ 14270Sstevel@tonic-gate dev_t dev = devvp->v_rdev; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate *strtsectp = (daddr_t)0; 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate /* 14320Sstevel@tonic-gate * Open the device so we can check out the BPB or FDISK table, 14330Sstevel@tonic-gate * then read in the sector. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate PC_DPRINTF2(5, "pc_getfattype: dev=%x ldrive=%x ", (int)dev, ldrive); 14360Sstevel@tonic-gate if (rval = VOP_OPEN(&devvp, FREAD, CRED())) { 14370Sstevel@tonic-gate PC_DPRINTF1(1, "pc_getfattype: open error=%d\n", rval); 14380Sstevel@tonic-gate return (rval); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * Read block 0 from device 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate bp = bread(dev, (daddr_t)0, PC_SAFESECSIZE); 14450Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 14460Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getfattype: read error\n"); 14470Sstevel@tonic-gate rval = EIO; 14480Sstevel@tonic-gate goto out; 14490Sstevel@tonic-gate } 14500Sstevel@tonic-gate 1451*607Swyllys cp = (uchar_t *)bp->b_un.b_addr; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate /* 1454*607Swyllys * If the first block is not a valid BPB, look for the 1455*607Swyllys * through the FDISK table. 14560Sstevel@tonic-gate */ 1457*607Swyllys if (!isBPB(cp, fattypep)) { 1458*607Swyllys /* find the partition table and get 512 bytes from it. */ 1459*607Swyllys PC_DPRINTF0(5, "pc_getfattype: using FDISK table to find BPB"); 14600Sstevel@tonic-gate 1461*607Swyllys if (findTheDrive(dev, ldrive, &rval, &bp, 1462*607Swyllys strtsectp, &sysid) == 0) 14630Sstevel@tonic-gate goto out; 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate brelse(bp); 14660Sstevel@tonic-gate bp = bread(dev, *strtsectp, PC_SAFESECSIZE); 14670Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 14680Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getfattype: read error\n"); 14690Sstevel@tonic-gate rval = EIO; 14700Sstevel@tonic-gate goto out; 14710Sstevel@tonic-gate } 1472*607Swyllys cp = (uchar_t *)bp->b_un.b_addr; 14730Sstevel@tonic-gate 1474*607Swyllys /* If this one is still no good, give it up. */ 1475*607Swyllys if (!isBPB(cp, fattypep)) { 1476*607Swyllys rval = EIO; 14770Sstevel@tonic-gate goto out; 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate } 14800Sstevel@tonic-gate 14810Sstevel@tonic-gate out: 1482*607Swyllys /* 1483*607Swyllys * Release the buffer used 1484*607Swyllys */ 14850Sstevel@tonic-gate if (bp != NULL) 14860Sstevel@tonic-gate brelse(bp); 14870Sstevel@tonic-gate (void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, CRED()); 14880Sstevel@tonic-gate return (rval); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate /* 14930Sstevel@tonic-gate * Get the boot parameter block and file allocation table. 14940Sstevel@tonic-gate * If there is an old FAT, invalidate it. 14950Sstevel@tonic-gate */ 14960Sstevel@tonic-gate int 14970Sstevel@tonic-gate pc_getfat(struct pcfs *fsp) 14980Sstevel@tonic-gate { 14990Sstevel@tonic-gate struct vfs *vfsp = PCFSTOVFS(fsp); 15000Sstevel@tonic-gate struct buf *tp = 0; 15010Sstevel@tonic-gate struct buf *bp = 0; 15020Sstevel@tonic-gate uchar_t *fatp = NULL; 15030Sstevel@tonic-gate uchar_t *fat_changemap = NULL; 15040Sstevel@tonic-gate struct bootsec *bootp; 15050Sstevel@tonic-gate struct fat32_bootsec *f32b; 15060Sstevel@tonic-gate struct vnode *devvp; 15070Sstevel@tonic-gate int error; 15080Sstevel@tonic-gate int fatsize; 15090Sstevel@tonic-gate int fat_changemapsize; 15100Sstevel@tonic-gate int flags = 0; 15110Sstevel@tonic-gate int nfat; 15120Sstevel@tonic-gate int secsize; 15130Sstevel@tonic-gate int fatsec; 1514*607Swyllys int fattype; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate PC_DPRINTF0(5, "pc_getfat\n"); 15170Sstevel@tonic-gate devvp = fsp->pcfs_devvp; 15180Sstevel@tonic-gate if (fsp->pcfs_fatp) { 15190Sstevel@tonic-gate /* 15200Sstevel@tonic-gate * There is a FAT in core. 15210Sstevel@tonic-gate * If there are open file pcnodes or we have modified it or 15220Sstevel@tonic-gate * it hasn't timed out yet use the in core FAT. 15230Sstevel@tonic-gate * Otherwise invalidate it and get a new one 15240Sstevel@tonic-gate */ 15250Sstevel@tonic-gate #ifdef notdef 15260Sstevel@tonic-gate if (fsp->pcfs_frefs || 15270Sstevel@tonic-gate (fsp->pcfs_flags & PCFS_FATMOD) || 15280Sstevel@tonic-gate (gethrestime_sec() < fsp->pcfs_fattime)) { 15290Sstevel@tonic-gate return (0); 15300Sstevel@tonic-gate } else { 15310Sstevel@tonic-gate mutex_enter(&pcfslock); 15320Sstevel@tonic-gate pc_invalfat(fsp); 15330Sstevel@tonic-gate mutex_exit(&pcfslock); 15340Sstevel@tonic-gate } 15350Sstevel@tonic-gate #endif /* notdef */ 15360Sstevel@tonic-gate return (0); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate /* 15390Sstevel@tonic-gate * Open block device mounted on. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate error = VOP_OPEN(&devvp, 15420Sstevel@tonic-gate (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, 15430Sstevel@tonic-gate CRED()); 15440Sstevel@tonic-gate if (error) { 15450Sstevel@tonic-gate PC_DPRINTF1(1, "pc_getfat: open error=%d\n", error); 15460Sstevel@tonic-gate return (error); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate /* 15490Sstevel@tonic-gate * Get boot parameter block and check it for validity 15500Sstevel@tonic-gate */ 15510Sstevel@tonic-gate tp = bread(fsp->pcfs_xdev, fsp->pcfs_dosstart, PC_SAFESECSIZE); 15520Sstevel@tonic-gate if (tp->b_flags & (B_ERROR | B_STALE)) { 15530Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getfat: boot block error\n"); 15540Sstevel@tonic-gate flags = tp->b_flags & B_ERROR; 15550Sstevel@tonic-gate error = EIO; 15560Sstevel@tonic-gate goto out; 15570Sstevel@tonic-gate } 15580Sstevel@tonic-gate tp->b_flags |= B_STALE | B_AGE; 15590Sstevel@tonic-gate bootp = (struct bootsec *)tp->b_un.b_addr; 15600Sstevel@tonic-gate 1561*607Swyllys 15620Sstevel@tonic-gate /* get the sector size - may be more than 512 bytes */ 15630Sstevel@tonic-gate secsize = (int)ltohs(bootp->bps[0]); 15640Sstevel@tonic-gate /* check for bogus sector size - fat should be at least 1 sector */ 15650Sstevel@tonic-gate if (IS_FAT32(fsp)) { 15660Sstevel@tonic-gate f32b = (struct fat32_bootsec *)bootp; 15670Sstevel@tonic-gate fatsec = ltohi(f32b->f_fatlength); 15680Sstevel@tonic-gate } else { 15690Sstevel@tonic-gate fatsec = ltohs(bootp->fatsec); 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate if (secsize < 512 || fatsec < 1 || bootp->nfat < 1) { 15720Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: FAT size error"); 15730Sstevel@tonic-gate error = EINVAL; 15740Sstevel@tonic-gate goto out; 15750Sstevel@tonic-gate } 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate switch (bootp->mediadesriptor) { 15780Sstevel@tonic-gate default: 15790Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: media-descriptor error, 0x%x", 15800Sstevel@tonic-gate bootp->mediadesriptor); 15810Sstevel@tonic-gate error = EINVAL; 15820Sstevel@tonic-gate goto out; 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate case MD_FIXED: 15850Sstevel@tonic-gate /* 1586201Sjmcp * PCMCIA pseudo floppy is type MD_FIXED, 15870Sstevel@tonic-gate * but is accessed like a floppy 15880Sstevel@tonic-gate */ 15890Sstevel@tonic-gate if (!(fsp->pcfs_flags & PCFS_PCMCIA_NO_CIS)) { 15900Sstevel@tonic-gate fsp->pcfs_flags |= PCFS_NOCHK; 15910Sstevel@tonic-gate } 15920Sstevel@tonic-gate /* FALLTHRU */ 15930Sstevel@tonic-gate case SS8SPT: 15940Sstevel@tonic-gate case DS8SPT: 15950Sstevel@tonic-gate case SS9SPT: 15960Sstevel@tonic-gate case DS9SPT: 15970Sstevel@tonic-gate case DS18SPT: 15980Sstevel@tonic-gate case DS9_15SPT: 15990Sstevel@tonic-gate fsp->pcfs_secsize = secsize; 16000Sstevel@tonic-gate fsp->pcfs_sdshift = secsize / DEV_BSIZE - 1; 16010Sstevel@tonic-gate fsp->pcfs_entps = secsize / sizeof (struct pcdir); 16020Sstevel@tonic-gate fsp->pcfs_spcl = (int)bootp->spcl; 16030Sstevel@tonic-gate fsp->pcfs_fatsec = fatsec; 16040Sstevel@tonic-gate fsp->pcfs_spt = (int)ltohs(bootp->spt); 16050Sstevel@tonic-gate fsp->pcfs_rdirsec = (int)ltohs(bootp->rdirents[0]) 16060Sstevel@tonic-gate * sizeof (struct pcdir) / secsize; 16070Sstevel@tonic-gate fsp->pcfs_clsize = fsp->pcfs_spcl * secsize; 16080Sstevel@tonic-gate fsp->pcfs_fatstart = fsp->pcfs_dosstart + 16090Sstevel@tonic-gate (daddr_t)ltohs(bootp->res_sec[0]); 16100Sstevel@tonic-gate fsp->pcfs_rdirstart = fsp->pcfs_fatstart + 16110Sstevel@tonic-gate (bootp->nfat * fsp->pcfs_fatsec); 16120Sstevel@tonic-gate fsp->pcfs_datastart = fsp->pcfs_rdirstart + fsp->pcfs_rdirsec; 16130Sstevel@tonic-gate if (IS_FAT32(fsp)) 16140Sstevel@tonic-gate fsp->pcfs_rdirstart = ltohi(f32b->f_rootcluster); 16150Sstevel@tonic-gate fsp->pcfs_ncluster = (((int)(ltohs(bootp->numsect[0]) ? 16160Sstevel@tonic-gate ltohs(bootp->numsect[0]) : ltohi(bootp->totalsec))) - 16170Sstevel@tonic-gate fsp->pcfs_datastart + fsp->pcfs_dosstart) / fsp->pcfs_spcl; 16180Sstevel@tonic-gate fsp->pcfs_numfat = (int)bootp->nfat; 16190Sstevel@tonic-gate fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER; 16200Sstevel@tonic-gate break; 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate /* 16240Sstevel@tonic-gate * Get FAT and check it for validity 16250Sstevel@tonic-gate */ 16260Sstevel@tonic-gate fatsize = fsp->pcfs_fatsec * fsp->pcfs_secsize; 16270Sstevel@tonic-gate fatp = kmem_alloc(fatsize, KM_SLEEP); 16280Sstevel@tonic-gate error = pc_readfat(fsp, fatp, fsp->pcfs_fatstart, fatsize); 16290Sstevel@tonic-gate if (error) { 16300Sstevel@tonic-gate flags = B_ERROR; 16310Sstevel@tonic-gate goto out; 16320Sstevel@tonic-gate } 16330Sstevel@tonic-gate fat_changemapsize = (fatsize / fsp->pcfs_clsize) + 1; 16340Sstevel@tonic-gate fat_changemap = kmem_zalloc(fat_changemapsize, KM_SLEEP); 16350Sstevel@tonic-gate 1636*607Swyllys /* 1637*607Swyllys * The only definite signature check is that the 1638*607Swyllys * media descriptor byte should match the first byte 1639*607Swyllys * of the FAT block. 1640*607Swyllys */ 1641*607Swyllys if (fatp[0] != bootp->mediadesriptor) { 16420Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: FAT signature error"); 16430Sstevel@tonic-gate error = EINVAL; 16440Sstevel@tonic-gate goto out; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate /* 16470Sstevel@tonic-gate * Checking for fatsec and number of supported clusters, should 1648*607Swyllys * actually determine a FAT12/FAT media. 16490Sstevel@tonic-gate */ 16500Sstevel@tonic-gate if (fsp->pcfs_flags & PCFS_FAT16) { 16510Sstevel@tonic-gate if ((fsp->pcfs_fatsec <= 12) && 16520Sstevel@tonic-gate ((fatsize * 2 / 3) >= fsp->pcfs_ncluster)) { 16530Sstevel@tonic-gate /* 16540Sstevel@tonic-gate * We have a 12-bit FAT, rather than a 16-bit FAT. 16550Sstevel@tonic-gate * Ignore what the fdisk table says. 16560Sstevel@tonic-gate */ 16570Sstevel@tonic-gate PC_DPRINTF0(2, "pc_getfattype: forcing 12-bit FAT\n"); 16580Sstevel@tonic-gate fsp->pcfs_flags &= ~PCFS_FAT16; 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate /* 16620Sstevel@tonic-gate * Sanity check our FAT is large enough for the 16630Sstevel@tonic-gate * clusters we think we have. 16640Sstevel@tonic-gate */ 16650Sstevel@tonic-gate if ((fsp->pcfs_flags & PCFS_FAT16) && 16660Sstevel@tonic-gate ((fatsize / 2) < fsp->pcfs_ncluster)) { 16670Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: FAT too small for number of clusters"); 16680Sstevel@tonic-gate error = EINVAL; 16690Sstevel@tonic-gate goto out; 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate /* 16730Sstevel@tonic-gate * Get alternate FATs and check for consistency 16740Sstevel@tonic-gate * This is an inlined version of pc_readfat(). 16750Sstevel@tonic-gate * Since we're only comparing FAT and alternate FAT, 16760Sstevel@tonic-gate * there's no reason to let pc_readfat() copy data out 16770Sstevel@tonic-gate * of the buf. Instead, compare in-situ, one cluster 16780Sstevel@tonic-gate * at a time. 16790Sstevel@tonic-gate */ 16800Sstevel@tonic-gate for (nfat = 1; nfat < fsp->pcfs_numfat; nfat++) { 16810Sstevel@tonic-gate size_t startsec; 16820Sstevel@tonic-gate size_t off; 16830Sstevel@tonic-gate 16840Sstevel@tonic-gate startsec = fsp->pcfs_fatstart + nfat * fsp->pcfs_fatsec; 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate for (off = 0; off < fatsize; off += fsp->pcfs_clsize) { 16870Sstevel@tonic-gate bp = bread(fsp->pcfs_xdev, pc_dbdaddr(fsp, 16880Sstevel@tonic-gate startsec + 16890Sstevel@tonic-gate pc_cltodb(fsp, pc_lblkno(fsp, off))), 16900Sstevel@tonic-gate MIN(fsp->pcfs_clsize, fatsize - off)); 16910Sstevel@tonic-gate if (bp->b_flags & (B_ERROR | B_STALE)) { 16920Sstevel@tonic-gate cmn_err(CE_NOTE, 16930Sstevel@tonic-gate "!pcfs: alternate FAT #%d read error" 16940Sstevel@tonic-gate " at byte %ld", nfat, off); 16950Sstevel@tonic-gate flags = B_ERROR; 16960Sstevel@tonic-gate error = EIO; 16970Sstevel@tonic-gate goto out; 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate bp->b_flags |= B_STALE | B_AGE; 17000Sstevel@tonic-gate if (bcmp(bp->b_un.b_addr, 17010Sstevel@tonic-gate fatp + off, 17020Sstevel@tonic-gate MIN(fsp->pcfs_clsize, fatsize - off))) { 17030Sstevel@tonic-gate cmn_err(CE_NOTE, 17040Sstevel@tonic-gate "!pcfs: alternate FAT #%d corrupted" 17050Sstevel@tonic-gate " at byte %ld", nfat, off); 17060Sstevel@tonic-gate flags = B_ERROR; 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate brelse(bp); 17090Sstevel@tonic-gate bp = NULL; /* prevent double release */ 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate 17130Sstevel@tonic-gate fsp->pcfs_fatsize = fatsize; 17140Sstevel@tonic-gate fsp->pcfs_fatp = fatp; 17150Sstevel@tonic-gate fsp->pcfs_fat_changemapsize = fat_changemapsize; 17160Sstevel@tonic-gate fsp->pcfs_fat_changemap = fat_changemap; 17170Sstevel@tonic-gate fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT; 17180Sstevel@tonic-gate fsp->pcfs_fatjustread = 1; 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate brelse(tp); 17210Sstevel@tonic-gate tp = NULL; 17220Sstevel@tonic-gate if (IS_FAT32(fsp)) { 17230Sstevel@tonic-gate /* get fsinfo */ 17240Sstevel@tonic-gate struct fat32_boot_fsinfo fsinfo_disk; 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate fsp->f32fsinfo_sector = ltohs(f32b->f_infosector); 17270Sstevel@tonic-gate tp = bread(fsp->pcfs_xdev, 17280Sstevel@tonic-gate fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector), 17290Sstevel@tonic-gate PC_SAFESECSIZE); 17300Sstevel@tonic-gate if (tp->b_flags & (B_ERROR | B_STALE)) { 17310Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: error reading fat32 fsinfo"); 17320Sstevel@tonic-gate flags = tp->b_flags & B_ERROR; 17330Sstevel@tonic-gate brelse(tp); 17340Sstevel@tonic-gate tp = NULL; 17350Sstevel@tonic-gate error = EIO; 17360Sstevel@tonic-gate goto out; 17370Sstevel@tonic-gate } 17380Sstevel@tonic-gate tp->b_flags |= B_STALE | B_AGE; 17390Sstevel@tonic-gate bcopy((void *)(tp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF), 17400Sstevel@tonic-gate &fsinfo_disk, sizeof (struct fat32_boot_fsinfo)); 17410Sstevel@tonic-gate brelse(tp); 17420Sstevel@tonic-gate tp = NULL; 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate /* translated fields */ 17450Sstevel@tonic-gate fsp->fsinfo_native.fs_signature = 17460Sstevel@tonic-gate ltohi(fsinfo_disk.fs_signature); 17470Sstevel@tonic-gate fsp->fsinfo_native.fs_free_clusters = 17480Sstevel@tonic-gate ltohi(fsinfo_disk.fs_free_clusters); 17490Sstevel@tonic-gate if (fsp->fsinfo_native.fs_signature != FAT32_FS_SIGN) { 17500Sstevel@tonic-gate cmn_err(CE_NOTE, 17510Sstevel@tonic-gate "!pcfs: fat32 fsinfo signature mismatch."); 17520Sstevel@tonic-gate error = EINVAL; 17530Sstevel@tonic-gate goto out; 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate return (0); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate out: 17600Sstevel@tonic-gate cmn_err(CE_NOTE, "!pcfs: illegal disk format"); 17610Sstevel@tonic-gate if (tp) 17620Sstevel@tonic-gate brelse(tp); 17630Sstevel@tonic-gate if (bp) 17640Sstevel@tonic-gate brelse(bp); 17650Sstevel@tonic-gate if (fatp) 17660Sstevel@tonic-gate kmem_free(fatp, fatsize); 17670Sstevel@tonic-gate if (fat_changemap) 17680Sstevel@tonic-gate kmem_free(fat_changemap, fat_changemapsize); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate if (flags) { 17710Sstevel@tonic-gate pc_mark_irrecov(fsp); 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate (void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ? 17740Sstevel@tonic-gate FREAD : FREAD|FWRITE, 1, (offset_t)0, CRED()); 17750Sstevel@tonic-gate return (error); 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate int 17790Sstevel@tonic-gate pc_syncfat(struct pcfs *fsp) 17800Sstevel@tonic-gate { 17810Sstevel@tonic-gate struct buf *bp; 17820Sstevel@tonic-gate int nfat; 17830Sstevel@tonic-gate int error; 17840Sstevel@tonic-gate struct fat32_boot_fsinfo fsinfo_disk; 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate PC_DPRINTF0(7, "pcfs_syncfat\n"); 17870Sstevel@tonic-gate if ((fsp->pcfs_fatp == (uchar_t *)0) || 17880Sstevel@tonic-gate !(fsp->pcfs_flags & PCFS_FATMOD)) 17890Sstevel@tonic-gate return (0); 17900Sstevel@tonic-gate /* 17910Sstevel@tonic-gate * write out all copies of FATs 17920Sstevel@tonic-gate */ 17930Sstevel@tonic-gate fsp->pcfs_flags &= ~PCFS_FATMOD; 17940Sstevel@tonic-gate fsp->pcfs_fattime = gethrestime_sec() + PCFS_DISKTIMEOUT; 17950Sstevel@tonic-gate for (nfat = 0; nfat < fsp->pcfs_numfat; nfat++) { 17960Sstevel@tonic-gate error = pc_writefat(fsp, 17970Sstevel@tonic-gate fsp->pcfs_fatstart + nfat*fsp->pcfs_fatsec); 17980Sstevel@tonic-gate if (error) { 17990Sstevel@tonic-gate pc_mark_irrecov(fsp); 18000Sstevel@tonic-gate return (EIO); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate } 18030Sstevel@tonic-gate pc_clear_fatchanges(fsp); 18040Sstevel@tonic-gate PC_DPRINTF0(6, "pcfs_syncfat: wrote out FAT\n"); 18050Sstevel@tonic-gate /* write out fsinfo */ 18060Sstevel@tonic-gate if (IS_FAT32(fsp)) { 18070Sstevel@tonic-gate bp = bread(fsp->pcfs_xdev, 18080Sstevel@tonic-gate fsp->pcfs_dosstart + pc_dbdaddr(fsp, fsp->f32fsinfo_sector), 18090Sstevel@tonic-gate PC_SAFESECSIZE); 18100Sstevel@tonic-gate if (bp->b_flags & (B_ERROR | B_STALE)) { 18110Sstevel@tonic-gate brelse(bp); 18120Sstevel@tonic-gate return (EIO); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate bcopy((void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF), 18150Sstevel@tonic-gate &fsinfo_disk, sizeof (struct fat32_boot_fsinfo)); 18160Sstevel@tonic-gate /* translate fields */ 18170Sstevel@tonic-gate fsinfo_disk.fs_free_clusters = 18180Sstevel@tonic-gate htoli(fsp->fsinfo_native.fs_free_clusters); 18190Sstevel@tonic-gate fsinfo_disk.fs_next_cluster = (uint32_t)FSINFO_UNKNOWN; 18200Sstevel@tonic-gate bcopy(&fsinfo_disk, 18210Sstevel@tonic-gate (void *)(bp->b_un.b_addr + FAT32_BOOT_FSINFO_OFF), 18220Sstevel@tonic-gate sizeof (struct fat32_boot_fsinfo)); 18230Sstevel@tonic-gate bwrite2(bp); 18240Sstevel@tonic-gate error = geterror(bp); 18250Sstevel@tonic-gate brelse(bp); 18260Sstevel@tonic-gate if (error) { 18270Sstevel@tonic-gate pc_mark_irrecov(fsp); 18280Sstevel@tonic-gate return (EIO); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate return (0); 18320Sstevel@tonic-gate } 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate void 18350Sstevel@tonic-gate pc_invalfat(struct pcfs *fsp) 18360Sstevel@tonic-gate { 18370Sstevel@tonic-gate struct pcfs *xfsp; 18380Sstevel@tonic-gate int mount_cnt = 0; 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate PC_DPRINTF0(7, "pc_invalfat\n"); 18410Sstevel@tonic-gate if (fsp->pcfs_fatp == (uchar_t *)0) 18420Sstevel@tonic-gate panic("pc_invalfat"); 18430Sstevel@tonic-gate /* 18440Sstevel@tonic-gate * Release FAT 18450Sstevel@tonic-gate */ 18460Sstevel@tonic-gate kmem_free(fsp->pcfs_fatp, fsp->pcfs_fatsize); 18470Sstevel@tonic-gate fsp->pcfs_fatp = NULL; 18480Sstevel@tonic-gate kmem_free(fsp->pcfs_fat_changemap, fsp->pcfs_fat_changemapsize); 18490Sstevel@tonic-gate fsp->pcfs_fat_changemap = NULL; 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * Invalidate all the blocks associated with the device. 18520Sstevel@tonic-gate * Not needed if stateless. 18530Sstevel@tonic-gate */ 18540Sstevel@tonic-gate for (xfsp = pc_mounttab; xfsp; xfsp = xfsp->pcfs_nxt) 18550Sstevel@tonic-gate if (xfsp != fsp && xfsp->pcfs_xdev == fsp->pcfs_xdev) 18560Sstevel@tonic-gate mount_cnt++; 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate if (!mount_cnt) 18590Sstevel@tonic-gate binval(fsp->pcfs_xdev); 18600Sstevel@tonic-gate /* 18610Sstevel@tonic-gate * close mounted device 18620Sstevel@tonic-gate */ 18630Sstevel@tonic-gate (void) VOP_CLOSE(fsp->pcfs_devvp, 18640Sstevel@tonic-gate (PCFSTOVFS(fsp)->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, 18650Sstevel@tonic-gate 1, (offset_t)0, CRED()); 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate void 18690Sstevel@tonic-gate pc_badfs(struct pcfs *fsp) 18700Sstevel@tonic-gate { 18710Sstevel@tonic-gate cmn_err(CE_WARN, "corrupted PC file system on dev %x.%x\n", 18720Sstevel@tonic-gate getmajor(fsp->pcfs_devvp->v_rdev), 18730Sstevel@tonic-gate getminor(fsp->pcfs_devvp->v_rdev)); 18740Sstevel@tonic-gate } 18750Sstevel@tonic-gate 18760Sstevel@tonic-gate /* 18770Sstevel@tonic-gate * The problem with supporting NFS on the PCFS filesystem is that there 18780Sstevel@tonic-gate * is no good place to keep the generation number. The only possible 18790Sstevel@tonic-gate * place is inside a directory entry. There are a few words that we 18800Sstevel@tonic-gate * don't use - they store NT & OS/2 attributes, and the creation/last access 18810Sstevel@tonic-gate * time of the file - but it seems wrong to use them. In addition, directory 18820Sstevel@tonic-gate * entries come and go. If a directory is removed completely, its directory 18830Sstevel@tonic-gate * blocks are freed and the generation numbers are lost. Whereas in ufs, 18840Sstevel@tonic-gate * inode blocks are dedicated for inodes, so the generation numbers are 18850Sstevel@tonic-gate * permanently kept on the disk. 18860Sstevel@tonic-gate */ 18870Sstevel@tonic-gate static int 18880Sstevel@tonic-gate pcfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp) 18890Sstevel@tonic-gate { 18900Sstevel@tonic-gate struct pcnode *pcp; 18910Sstevel@tonic-gate struct pc_fid *pcfid; 18920Sstevel@tonic-gate struct pcfs *fsp; 18930Sstevel@tonic-gate struct pcdir *ep; 18940Sstevel@tonic-gate daddr_t eblkno; 18950Sstevel@tonic-gate int eoffset; 18960Sstevel@tonic-gate struct buf *bp; 18970Sstevel@tonic-gate int error; 18980Sstevel@tonic-gate pc_cluster32_t cn; 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate pcfid = (struct pc_fid *)fidp; 19010Sstevel@tonic-gate fsp = VFSTOPCFS(vfsp); 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate error = pc_lockfs(fsp, 0, 0); 19040Sstevel@tonic-gate if (error) { 19050Sstevel@tonic-gate *vpp = NULL; 19060Sstevel@tonic-gate return (error); 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate if (pcfid->pcfid_block == 0) { 19100Sstevel@tonic-gate pcp = pc_getnode(fsp, (daddr_t)0, 0, (struct pcdir *)0); 19110Sstevel@tonic-gate pcp->pc_flags |= PC_EXTERNAL; 19120Sstevel@tonic-gate *vpp = PCTOV(pcp); 19130Sstevel@tonic-gate pc_unlockfs(fsp); 19140Sstevel@tonic-gate return (0); 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate eblkno = pcfid->pcfid_block; 19170Sstevel@tonic-gate eoffset = pcfid->pcfid_offset; 19180Sstevel@tonic-gate if ((pc_dbtocl(fsp, 19190Sstevel@tonic-gate eblkno - fsp->pcfs_dosstart) >= fsp->pcfs_ncluster) || 19200Sstevel@tonic-gate (eoffset > fsp->pcfs_clsize)) { 19210Sstevel@tonic-gate pc_unlockfs(fsp); 19220Sstevel@tonic-gate *vpp = NULL; 19230Sstevel@tonic-gate return (EINVAL); 19240Sstevel@tonic-gate } 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate if (eblkno >= fsp->pcfs_datastart || (eblkno-fsp->pcfs_rdirstart) 19270Sstevel@tonic-gate < (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) { 19280Sstevel@tonic-gate bp = bread(fsp->pcfs_xdev, eblkno, fsp->pcfs_clsize); 19290Sstevel@tonic-gate } else { 19300Sstevel@tonic-gate bp = bread(fsp->pcfs_xdev, eblkno, 19310Sstevel@tonic-gate (int)(fsp->pcfs_datastart - eblkno) * fsp->pcfs_secsize); 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate if (bp->b_flags & (B_ERROR | B_STALE)) { 19340Sstevel@tonic-gate error = geterror(bp); 19350Sstevel@tonic-gate brelse(bp); 19360Sstevel@tonic-gate if (error) 19370Sstevel@tonic-gate pc_mark_irrecov(fsp); 19380Sstevel@tonic-gate *vpp = NULL; 19390Sstevel@tonic-gate pc_unlockfs(fsp); 19400Sstevel@tonic-gate return (error); 19410Sstevel@tonic-gate } 19420Sstevel@tonic-gate ep = (struct pcdir *)(bp->b_un.b_addr + eoffset); 19430Sstevel@tonic-gate /* 19440Sstevel@tonic-gate * Ok, if this is a valid file handle that we gave out, 19450Sstevel@tonic-gate * then simply ensuring that the creation time matches, 19460Sstevel@tonic-gate * the entry has not been deleted, and it has a valid first 19470Sstevel@tonic-gate * character should be enough. 19480Sstevel@tonic-gate * 19490Sstevel@tonic-gate * Unfortunately, verifying that the <blkno, offset> _still_ 19500Sstevel@tonic-gate * refers to a directory entry is not easy, since we'd have 19510Sstevel@tonic-gate * to search _all_ directories starting from root to find it. 19520Sstevel@tonic-gate * That's a high price to pay just in case somebody is forging 19530Sstevel@tonic-gate * file handles. So instead we verify that as much of the 19540Sstevel@tonic-gate * entry is valid as we can: 19550Sstevel@tonic-gate * 19560Sstevel@tonic-gate * 1. The starting cluster is 0 (unallocated) or valid 19570Sstevel@tonic-gate * 2. It is not an LFN entry 19580Sstevel@tonic-gate * 3. It is not hidden (unless mounted as such) 19590Sstevel@tonic-gate * 4. It is not the label 19600Sstevel@tonic-gate */ 19610Sstevel@tonic-gate cn = pc_getstartcluster(fsp, ep); 19620Sstevel@tonic-gate /* 19630Sstevel@tonic-gate * if the starting cluster is valid, but not valid according 19640Sstevel@tonic-gate * to pc_validcl(), force it to be to simplify the following if. 19650Sstevel@tonic-gate */ 19660Sstevel@tonic-gate if (cn == 0) 19670Sstevel@tonic-gate cn = PCF_FIRSTCLUSTER; 19680Sstevel@tonic-gate if (IS_FAT32(fsp)) { 19690Sstevel@tonic-gate if (cn >= PCF_LASTCLUSTER32) 19700Sstevel@tonic-gate cn = PCF_FIRSTCLUSTER; 19710Sstevel@tonic-gate } else { 19720Sstevel@tonic-gate if (cn >= PCF_LASTCLUSTER) 19730Sstevel@tonic-gate cn = PCF_FIRSTCLUSTER; 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate if ((!pc_validcl(fsp, cn)) || 19760Sstevel@tonic-gate (PCDL_IS_LFN(ep)) || 19770Sstevel@tonic-gate (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) || 19780Sstevel@tonic-gate ((ep->pcd_attr & PCA_LABEL) == PCA_LABEL)) { 19790Sstevel@tonic-gate bp->b_flags |= B_STALE | B_AGE; 19800Sstevel@tonic-gate brelse(bp); 19810Sstevel@tonic-gate pc_unlockfs(fsp); 19820Sstevel@tonic-gate return (EINVAL); 19830Sstevel@tonic-gate } 19840Sstevel@tonic-gate if ((ep->pcd_crtime.pct_time == pcfid->pcfid_ctime) && 19850Sstevel@tonic-gate (ep->pcd_filename[0] != PCD_ERASED) && 19860Sstevel@tonic-gate (pc_validchar(ep->pcd_filename[0]) || 19870Sstevel@tonic-gate (ep->pcd_filename[0] == '.' && ep->pcd_filename[1] == '.'))) { 19880Sstevel@tonic-gate pcp = pc_getnode(fsp, eblkno, eoffset, ep); 19890Sstevel@tonic-gate pcp->pc_flags |= PC_EXTERNAL; 19900Sstevel@tonic-gate *vpp = PCTOV(pcp); 19910Sstevel@tonic-gate } else { 19920Sstevel@tonic-gate *vpp = NULL; 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate bp->b_flags |= B_STALE | B_AGE; 19950Sstevel@tonic-gate brelse(bp); 19960Sstevel@tonic-gate pc_unlockfs(fsp); 19970Sstevel@tonic-gate return (0); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate /* 2001201Sjmcp * if device is a PCMCIA pseudo floppy, return 1 20020Sstevel@tonic-gate * otherwise, return 0 20030Sstevel@tonic-gate */ 20040Sstevel@tonic-gate static int 2005201Sjmcp pcfs_pseudo_floppy(dev_t rdev) 20060Sstevel@tonic-gate { 20070Sstevel@tonic-gate int error, err; 20080Sstevel@tonic-gate struct dk_cinfo info; 20090Sstevel@tonic-gate ldi_handle_t lh; 20100Sstevel@tonic-gate ldi_ident_t li; 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate err = ldi_ident_from_mod(&modlinkage, &li); 20130Sstevel@tonic-gate if (err) { 20140Sstevel@tonic-gate PC_DPRINTF1(1, 2015201Sjmcp "pcfs_pseudo_floppy: ldi_ident_from_mod err=%d\n", err); 20160Sstevel@tonic-gate return (0); 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate err = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD, CRED(), &lh, li); 20200Sstevel@tonic-gate ldi_ident_release(li); 20210Sstevel@tonic-gate if (err) { 20220Sstevel@tonic-gate PC_DPRINTF1(1, 2023201Sjmcp "pcfs_pseudo_floppy: ldi_open err=%d\n", err); 20240Sstevel@tonic-gate return (0); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate /* return value stored in err is purposfully ignored */ 20280Sstevel@tonic-gate error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL, 20290Sstevel@tonic-gate CRED(), &err); 20300Sstevel@tonic-gate 20310Sstevel@tonic-gate err = ldi_close(lh, FREAD, CRED()); 20320Sstevel@tonic-gate if (err != 0) { 20330Sstevel@tonic-gate PC_DPRINTF1(1, 2034201Sjmcp "pcfs_pseudo_floppy: ldi_close err=%d\n", err); 20350Sstevel@tonic-gate return (0); 20360Sstevel@tonic-gate } 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate if ((error == 0) && (info.dki_ctype == DKC_PCMCIA_MEM) && 20390Sstevel@tonic-gate (info.dki_flags & DKI_PCMCIA_PFD)) 20400Sstevel@tonic-gate return (1); 20410Sstevel@tonic-gate else 20420Sstevel@tonic-gate return (0); 20430Sstevel@tonic-gate } 20440Sstevel@tonic-gate 20450Sstevel@tonic-gate /* 20460Sstevel@tonic-gate * Unfortunately, FAT32 fat's can be pretty big (On a 1 gig jaz drive, about 20470Sstevel@tonic-gate * a meg), so we can't bread() it all in at once. This routine reads a 20480Sstevel@tonic-gate * fat a chunk at a time. 20490Sstevel@tonic-gate */ 20500Sstevel@tonic-gate static int 20510Sstevel@tonic-gate pc_readfat(struct pcfs *fsp, uchar_t *fatp, daddr_t start, size_t fatsize) 20520Sstevel@tonic-gate { 20530Sstevel@tonic-gate struct buf *bp; 20540Sstevel@tonic-gate size_t off; 20550Sstevel@tonic-gate size_t readsize; 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate readsize = fsp->pcfs_clsize; 20580Sstevel@tonic-gate for (off = 0; off < fatsize; off += readsize, fatp += readsize) { 20590Sstevel@tonic-gate if (readsize > (fatsize - off)) 20600Sstevel@tonic-gate readsize = fatsize - off; 20610Sstevel@tonic-gate bp = bread(fsp->pcfs_xdev, 20620Sstevel@tonic-gate pc_dbdaddr(fsp, start + 20630Sstevel@tonic-gate pc_cltodb(fsp, pc_lblkno(fsp, off))), 20640Sstevel@tonic-gate readsize); 20650Sstevel@tonic-gate if (bp->b_flags & (B_ERROR | B_STALE)) { 20660Sstevel@tonic-gate brelse(bp); 20670Sstevel@tonic-gate return (EIO); 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate bp->b_flags |= B_STALE | B_AGE; 20700Sstevel@tonic-gate bcopy(bp->b_un.b_addr, fatp, readsize); 20710Sstevel@tonic-gate brelse(bp); 20720Sstevel@tonic-gate } 20730Sstevel@tonic-gate return (0); 20740Sstevel@tonic-gate } 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate /* 20770Sstevel@tonic-gate * We write the FAT out a _lot_, in order to make sure that it 20780Sstevel@tonic-gate * is up-to-date. But on a FAT32 system (large drive, small clusters) 20790Sstevel@tonic-gate * the FAT might be a couple of megabytes, and writing it all out just 20800Sstevel@tonic-gate * because we created or deleted a small file is painful (especially 20810Sstevel@tonic-gate * since we do it for each alternate FAT too). So instead, for FAT16 and 20820Sstevel@tonic-gate * FAT32 we only write out the bit that has changed. We don't clear 20830Sstevel@tonic-gate * the 'updated' fields here because the caller might be writing out 20840Sstevel@tonic-gate * several FATs, so the caller must use pc_clear_fatchanges() after 20850Sstevel@tonic-gate * all FATs have been updated. 20860Sstevel@tonic-gate */ 20870Sstevel@tonic-gate static int 20880Sstevel@tonic-gate pc_writefat(struct pcfs *fsp, daddr_t start) 20890Sstevel@tonic-gate { 20900Sstevel@tonic-gate struct buf *bp; 20910Sstevel@tonic-gate size_t off; 20920Sstevel@tonic-gate size_t writesize; 20930Sstevel@tonic-gate int error; 20940Sstevel@tonic-gate uchar_t *fatp = fsp->pcfs_fatp; 20950Sstevel@tonic-gate size_t fatsize = fsp->pcfs_fatsize; 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate writesize = fsp->pcfs_clsize; 20980Sstevel@tonic-gate for (off = 0; off < fatsize; off += writesize, fatp += writesize) { 20990Sstevel@tonic-gate if (writesize > (fatsize - off)) 21000Sstevel@tonic-gate writesize = fatsize - off; 21010Sstevel@tonic-gate if (!pc_fat_is_changed(fsp, pc_lblkno(fsp, off))) { 21020Sstevel@tonic-gate continue; 21030Sstevel@tonic-gate } 21040Sstevel@tonic-gate bp = ngeteblk(writesize); 21050Sstevel@tonic-gate bp->b_edev = fsp->pcfs_xdev; 21060Sstevel@tonic-gate bp->b_dev = cmpdev(bp->b_edev); 21070Sstevel@tonic-gate bp->b_blkno = pc_dbdaddr(fsp, start + 21080Sstevel@tonic-gate pc_cltodb(fsp, pc_lblkno(fsp, off))); 21090Sstevel@tonic-gate bcopy(fatp, bp->b_un.b_addr, writesize); 21100Sstevel@tonic-gate bwrite2(bp); 21110Sstevel@tonic-gate error = geterror(bp); 21120Sstevel@tonic-gate brelse(bp); 21130Sstevel@tonic-gate if (error) { 21140Sstevel@tonic-gate return (error); 21150Sstevel@tonic-gate } 21160Sstevel@tonic-gate } 21170Sstevel@tonic-gate return (0); 21180Sstevel@tonic-gate } 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate /* 21210Sstevel@tonic-gate * Mark the FAT cluster that 'cn' is stored in as modified. 21220Sstevel@tonic-gate */ 21230Sstevel@tonic-gate void 21240Sstevel@tonic-gate pc_mark_fat_updated(struct pcfs *fsp, pc_cluster32_t cn) 21250Sstevel@tonic-gate { 21260Sstevel@tonic-gate pc_cluster32_t bn; 21270Sstevel@tonic-gate size_t size; 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate /* which fat block is the cluster number stored in? */ 21300Sstevel@tonic-gate if (IS_FAT32(fsp)) { 21310Sstevel@tonic-gate size = sizeof (pc_cluster32_t); 21320Sstevel@tonic-gate bn = pc_lblkno(fsp, cn * size); 21330Sstevel@tonic-gate fsp->pcfs_fat_changemap[bn] = 1; 21340Sstevel@tonic-gate } else if (IS_FAT16(fsp)) { 21350Sstevel@tonic-gate size = sizeof (pc_cluster16_t); 21360Sstevel@tonic-gate bn = pc_lblkno(fsp, cn * size); 21370Sstevel@tonic-gate fsp->pcfs_fat_changemap[bn] = 1; 21380Sstevel@tonic-gate } else { 21390Sstevel@tonic-gate offset_t off; 21400Sstevel@tonic-gate pc_cluster32_t nbn; 21410Sstevel@tonic-gate 21420Sstevel@tonic-gate ASSERT(IS_FAT12(fsp)); 21430Sstevel@tonic-gate off = cn + (cn >> 1); 21440Sstevel@tonic-gate bn = pc_lblkno(fsp, off); 21450Sstevel@tonic-gate fsp->pcfs_fat_changemap[bn] = 1; 21460Sstevel@tonic-gate /* does this field wrap into the next fat cluster? */ 21470Sstevel@tonic-gate nbn = pc_lblkno(fsp, off + 1); 21480Sstevel@tonic-gate if (nbn != bn) { 21490Sstevel@tonic-gate fsp->pcfs_fat_changemap[nbn] = 1; 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate } 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate /* 21550Sstevel@tonic-gate * return whether the FAT cluster 'bn' is updated and needs to 21560Sstevel@tonic-gate * be written out. 21570Sstevel@tonic-gate */ 21580Sstevel@tonic-gate int 21590Sstevel@tonic-gate pc_fat_is_changed(struct pcfs *fsp, pc_cluster32_t bn) 21600Sstevel@tonic-gate { 21610Sstevel@tonic-gate return (fsp->pcfs_fat_changemap[bn] == 1); 21620Sstevel@tonic-gate } 2163