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