xref: /minix3/usr.sbin/makefs/msdos/msdosfs_vfsops.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
19f988b79SJean-Baptiste Boric /*-
29f988b79SJean-Baptiste Boric  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
39f988b79SJean-Baptiste Boric  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
49f988b79SJean-Baptiste Boric  * All rights reserved.
59f988b79SJean-Baptiste Boric  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
89f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
99f988b79SJean-Baptiste Boric  * are met:
109f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
119f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
129f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
139f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
149f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
159f988b79SJean-Baptiste Boric  * 3. All advertising materials mentioning features or use of this software
169f988b79SJean-Baptiste Boric  *    must display the following acknowledgement:
179f988b79SJean-Baptiste Boric  *	This product includes software developed by TooLs GmbH.
189f988b79SJean-Baptiste Boric  * 4. The name of TooLs GmbH may not be used to endorse or promote products
199f988b79SJean-Baptiste Boric  *    derived from this software without specific prior written permission.
209f988b79SJean-Baptiste Boric  *
219f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
229f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239f988b79SJean-Baptiste Boric  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249f988b79SJean-Baptiste Boric  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
259f988b79SJean-Baptiste Boric  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
269f988b79SJean-Baptiste Boric  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
279f988b79SJean-Baptiste Boric  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
289f988b79SJean-Baptiste Boric  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
299f988b79SJean-Baptiste Boric  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
309f988b79SJean-Baptiste Boric  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
319f988b79SJean-Baptiste Boric  */
329f988b79SJean-Baptiste Boric /*
339f988b79SJean-Baptiste Boric  * Written by Paul Popelka (paulp@uts.amdahl.com)
349f988b79SJean-Baptiste Boric  *
359f988b79SJean-Baptiste Boric  * You can do anything you want with this software, just don't say you wrote
369f988b79SJean-Baptiste Boric  * it, and don't remove this notice.
379f988b79SJean-Baptiste Boric  *
389f988b79SJean-Baptiste Boric  * This software is provided "as is".
399f988b79SJean-Baptiste Boric  *
409f988b79SJean-Baptiste Boric  * The author supplies this software to be publicly redistributed on the
419f988b79SJean-Baptiste Boric  * understanding that the author is not responsible for the correct
429f988b79SJean-Baptiste Boric  * functioning of this software in any circumstances and is not liable for
439f988b79SJean-Baptiste Boric  * any damages caused by this software.
449f988b79SJean-Baptiste Boric  *
459f988b79SJean-Baptiste Boric  * October 1992
469f988b79SJean-Baptiste Boric  */
479f988b79SJean-Baptiste Boric 
489f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
499f988b79SJean-Baptiste Boric #include "nbtool_config.h"
509f988b79SJean-Baptiste Boric #endif
519f988b79SJean-Baptiste Boric 
529f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
53*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.9 2015/03/29 05:52:59 agc Exp $");
549f988b79SJean-Baptiste Boric 
559f988b79SJean-Baptiste Boric #include <sys/param.h>
569f988b79SJean-Baptiste Boric 
579f988b79SJean-Baptiste Boric #include <ffs/buf.h>
589f988b79SJean-Baptiste Boric 
599f988b79SJean-Baptiste Boric #include <fs/msdosfs/bpb.h>
609f988b79SJean-Baptiste Boric #include <fs/msdosfs/bootsect.h>
619f988b79SJean-Baptiste Boric #include <fs/msdosfs/direntry.h>
629f988b79SJean-Baptiste Boric #include <fs/msdosfs/denode.h>
639f988b79SJean-Baptiste Boric #include <fs/msdosfs/msdosfsmount.h>
649f988b79SJean-Baptiste Boric #include <fs/msdosfs/fat.h>
659f988b79SJean-Baptiste Boric 
669f988b79SJean-Baptiste Boric #include <stdio.h>
679f988b79SJean-Baptiste Boric #include <errno.h>
689f988b79SJean-Baptiste Boric #include <stdlib.h>
699f988b79SJean-Baptiste Boric #include <string.h>
709f988b79SJean-Baptiste Boric #include <util.h>
719f988b79SJean-Baptiste Boric 
729f988b79SJean-Baptiste Boric #include "makefs.h"
739f988b79SJean-Baptiste Boric #include "msdos.h"
749f988b79SJean-Baptiste Boric #include "mkfs_msdos.h"
759f988b79SJean-Baptiste Boric 
769f988b79SJean-Baptiste Boric #ifdef MSDOSFS_DEBUG
779f988b79SJean-Baptiste Boric #define DPRINTF(a) printf a
789f988b79SJean-Baptiste Boric #else
799f988b79SJean-Baptiste Boric #define DPRINTF(a)
809f988b79SJean-Baptiste Boric #endif
819f988b79SJean-Baptiste Boric 
829f988b79SJean-Baptiste Boric struct msdosfsmount *
msdosfs_mount(struct vnode * devvp,int flags)839f988b79SJean-Baptiste Boric msdosfs_mount(struct vnode *devvp, int flags)
849f988b79SJean-Baptiste Boric {
859f988b79SJean-Baptiste Boric 	struct msdosfsmount *pmp = NULL;
869f988b79SJean-Baptiste Boric 	struct buf *bp;
879f988b79SJean-Baptiste Boric 	union bootsector *bsp;
889f988b79SJean-Baptiste Boric 	struct byte_bpb33 *b33;
899f988b79SJean-Baptiste Boric 	struct byte_bpb50 *b50;
909f988b79SJean-Baptiste Boric 	struct byte_bpb710 *b710;
919f988b79SJean-Baptiste Boric 	uint8_t SecPerClust;
929f988b79SJean-Baptiste Boric 	int	ronly = 0, error, tmp;
939f988b79SJean-Baptiste Boric 	int	bsize;
949f988b79SJean-Baptiste Boric 	struct msdos_options *m = devvp->fs->fs_specific;
959f988b79SJean-Baptiste Boric 	uint64_t psize = m->create_size;
969f988b79SJean-Baptiste Boric 	unsigned secsize = 512;
979f988b79SJean-Baptiste Boric 
989f988b79SJean-Baptiste Boric 	DPRINTF(("%s(bread 0)\n", __func__));
99*0a6a1f1dSLionel Sambuc 	if ((error = bread(devvp, 0, secsize, 0, &bp)) != 0)
1009f988b79SJean-Baptiste Boric 		goto error_exit;
1019f988b79SJean-Baptiste Boric 
1029f988b79SJean-Baptiste Boric 	bsp = (union bootsector *)bp->b_data;
1039f988b79SJean-Baptiste Boric 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
1049f988b79SJean-Baptiste Boric 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
1059f988b79SJean-Baptiste Boric 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
1069f988b79SJean-Baptiste Boric 
1079f988b79SJean-Baptiste Boric 	if (!(flags & MSDOSFSMNT_GEMDOSFS)) {
1089f988b79SJean-Baptiste Boric 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
1099f988b79SJean-Baptiste Boric 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
1109f988b79SJean-Baptiste Boric 			DPRINTF(("bootsig0 %d bootsig1 %d\n",
1119f988b79SJean-Baptiste Boric 			    bsp->bs50.bsBootSectSig0,
1129f988b79SJean-Baptiste Boric 			    bsp->bs50.bsBootSectSig1));
1139f988b79SJean-Baptiste Boric 			error = EINVAL;
1149f988b79SJean-Baptiste Boric 			goto error_exit;
1159f988b79SJean-Baptiste Boric 		}
1169f988b79SJean-Baptiste Boric 		bsize = 0;
1179f988b79SJean-Baptiste Boric 	} else
1189f988b79SJean-Baptiste Boric 		bsize = 512;
1199f988b79SJean-Baptiste Boric 
1209f988b79SJean-Baptiste Boric 	pmp = ecalloc(1, sizeof *pmp);
1219f988b79SJean-Baptiste Boric 	/*
1229f988b79SJean-Baptiste Boric 	 * Compute several useful quantities from the bpb in the
1239f988b79SJean-Baptiste Boric 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
1249f988b79SJean-Baptiste Boric 	 * the fields that are different between dos 5 and dos 3.3.
1259f988b79SJean-Baptiste Boric 	 */
1269f988b79SJean-Baptiste Boric 	SecPerClust = b50->bpbSecPerClust;
1279f988b79SJean-Baptiste Boric 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
1289f988b79SJean-Baptiste Boric 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
1299f988b79SJean-Baptiste Boric 	pmp->pm_FATs = b50->bpbFATs;
1309f988b79SJean-Baptiste Boric 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
1319f988b79SJean-Baptiste Boric 	pmp->pm_Sectors = getushort(b50->bpbSectors);
1329f988b79SJean-Baptiste Boric 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
1339f988b79SJean-Baptiste Boric 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
1349f988b79SJean-Baptiste Boric 	pmp->pm_Heads = getushort(b50->bpbHeads);
1359f988b79SJean-Baptiste Boric 	pmp->pm_Media = b50->bpbMedia;
1369f988b79SJean-Baptiste Boric 
1379f988b79SJean-Baptiste Boric 	DPRINTF(("%s(BytesPerSec=%u, ResSectors=%u, FATs=%d, RootDirEnts=%u, "
1389f988b79SJean-Baptiste Boric 	    "Sectors=%u, FATsecs=%lu, SecPerTrack=%u, Heads=%u, Media=%u)\n",
1399f988b79SJean-Baptiste Boric 	    __func__, pmp->pm_BytesPerSec, pmp->pm_ResSectors, pmp->pm_FATs,
1409f988b79SJean-Baptiste Boric 	    pmp->pm_RootDirEnts, pmp->pm_Sectors, pmp->pm_FATsecs,
1419f988b79SJean-Baptiste Boric 	    pmp->pm_SecPerTrack, pmp->pm_Heads, pmp->pm_Media));
1429f988b79SJean-Baptiste Boric 	if (!(flags & MSDOSFSMNT_GEMDOSFS)) {
1439f988b79SJean-Baptiste Boric 		/* XXX - We should probably check more values here */
1449f988b79SJean-Baptiste Boric     		if (!pmp->pm_BytesPerSec || !SecPerClust
1459f988b79SJean-Baptiste Boric 	    		|| pmp->pm_SecPerTrack > 63) {
1469f988b79SJean-Baptiste Boric 			DPRINTF(("bytespersec %d secperclust %d "
1479f988b79SJean-Baptiste Boric 			    "secpertrack %d\n",
1489f988b79SJean-Baptiste Boric 			    pmp->pm_BytesPerSec, SecPerClust,
1499f988b79SJean-Baptiste Boric 			    pmp->pm_SecPerTrack));
1509f988b79SJean-Baptiste Boric 			error = EINVAL;
1519f988b79SJean-Baptiste Boric 			goto error_exit;
1529f988b79SJean-Baptiste Boric 		}
1539f988b79SJean-Baptiste Boric 	}
1549f988b79SJean-Baptiste Boric 
1559f988b79SJean-Baptiste Boric 	if (pmp->pm_Sectors == 0) {
1569f988b79SJean-Baptiste Boric 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
1579f988b79SJean-Baptiste Boric 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
1589f988b79SJean-Baptiste Boric 	} else {
1599f988b79SJean-Baptiste Boric 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
1609f988b79SJean-Baptiste Boric 		pmp->pm_HugeSectors = pmp->pm_Sectors;
1619f988b79SJean-Baptiste Boric 	}
1629f988b79SJean-Baptiste Boric 
1639f988b79SJean-Baptiste Boric 	if (pmp->pm_RootDirEnts == 0) {
1649f988b79SJean-Baptiste Boric 		unsigned short vers = getushort(b710->bpbFSVers);
1659f988b79SJean-Baptiste Boric 		/*
1669f988b79SJean-Baptiste Boric 		 * Some say that bsBootSectSig[23] must be zero, but
1679f988b79SJean-Baptiste Boric 		 * Windows does not require this and some digital cameras
1689f988b79SJean-Baptiste Boric 		 * do not set these to zero.  Therefore, do not insist.
1699f988b79SJean-Baptiste Boric 		 */
1709f988b79SJean-Baptiste Boric 		if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
1719f988b79SJean-Baptiste Boric 			DPRINTF(("sectors %d fatsecs %lu vers %d\n",
1729f988b79SJean-Baptiste Boric 			    pmp->pm_Sectors, pmp->pm_FATsecs, vers));
1739f988b79SJean-Baptiste Boric 			error = EINVAL;
1749f988b79SJean-Baptiste Boric 			goto error_exit;
1759f988b79SJean-Baptiste Boric 		}
1769f988b79SJean-Baptiste Boric 		pmp->pm_fatmask = FAT32_MASK;
1779f988b79SJean-Baptiste Boric 		pmp->pm_fatmult = 4;
1789f988b79SJean-Baptiste Boric 		pmp->pm_fatdiv = 1;
1799f988b79SJean-Baptiste Boric 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
1809f988b79SJean-Baptiste Boric 
1819f988b79SJean-Baptiste Boric 		/* mirrorring is enabled if the FATMIRROR bit is not set */
1829f988b79SJean-Baptiste Boric 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
1839f988b79SJean-Baptiste Boric 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
1849f988b79SJean-Baptiste Boric 		else
1859f988b79SJean-Baptiste Boric 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
1869f988b79SJean-Baptiste Boric 	} else
1879f988b79SJean-Baptiste Boric 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
1889f988b79SJean-Baptiste Boric 
1899f988b79SJean-Baptiste Boric 	if (flags & MSDOSFSMNT_GEMDOSFS) {
1909f988b79SJean-Baptiste Boric 		if (FAT32(pmp)) {
1919f988b79SJean-Baptiste Boric 			DPRINTF(("FAT32 for GEMDOS\n"));
1929f988b79SJean-Baptiste Boric 			/*
1939f988b79SJean-Baptiste Boric 			 * GEMDOS doesn't know FAT32.
1949f988b79SJean-Baptiste Boric 			 */
1959f988b79SJean-Baptiste Boric 			error = EINVAL;
1969f988b79SJean-Baptiste Boric 			goto error_exit;
1979f988b79SJean-Baptiste Boric 		}
1989f988b79SJean-Baptiste Boric 
1999f988b79SJean-Baptiste Boric 		/*
2009f988b79SJean-Baptiste Boric 		 * Check a few values (could do some more):
2019f988b79SJean-Baptiste Boric 		 * - logical sector size: power of 2, >= block size
2029f988b79SJean-Baptiste Boric 		 * - sectors per cluster: power of 2, >= 1
2039f988b79SJean-Baptiste Boric 		 * - number of sectors:   >= 1, <= size of partition
2049f988b79SJean-Baptiste Boric 		 */
2059f988b79SJean-Baptiste Boric 		if ( (SecPerClust == 0)
2069f988b79SJean-Baptiste Boric 		  || (SecPerClust & (SecPerClust - 1))
2079f988b79SJean-Baptiste Boric 		  || (pmp->pm_BytesPerSec < bsize)
2089f988b79SJean-Baptiste Boric 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
2099f988b79SJean-Baptiste Boric 		  || (pmp->pm_HugeSectors == 0)
2109f988b79SJean-Baptiste Boric 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
2119f988b79SJean-Baptiste Boric 		      > psize)) {
2129f988b79SJean-Baptiste Boric 			DPRINTF(("consistency checks for GEMDOS\n"));
2139f988b79SJean-Baptiste Boric 			error = EINVAL;
2149f988b79SJean-Baptiste Boric 			goto error_exit;
2159f988b79SJean-Baptiste Boric 		}
2169f988b79SJean-Baptiste Boric 		/*
2179f988b79SJean-Baptiste Boric 		 * XXX - Many parts of the msdosfs driver seem to assume that
2189f988b79SJean-Baptiste Boric 		 * the number of bytes per logical sector (BytesPerSec) will
2199f988b79SJean-Baptiste Boric 		 * always be the same as the number of bytes per disk block
2209f988b79SJean-Baptiste Boric 		 * Let's pretend it is.
2219f988b79SJean-Baptiste Boric 		 */
2229f988b79SJean-Baptiste Boric 		tmp = pmp->pm_BytesPerSec / bsize;
2239f988b79SJean-Baptiste Boric 		pmp->pm_BytesPerSec  = bsize;
2249f988b79SJean-Baptiste Boric 		pmp->pm_HugeSectors *= tmp;
2259f988b79SJean-Baptiste Boric 		pmp->pm_HiddenSects *= tmp;
2269f988b79SJean-Baptiste Boric 		pmp->pm_ResSectors  *= tmp;
2279f988b79SJean-Baptiste Boric 		pmp->pm_Sectors     *= tmp;
2289f988b79SJean-Baptiste Boric 		pmp->pm_FATsecs     *= tmp;
2299f988b79SJean-Baptiste Boric 		SecPerClust         *= tmp;
2309f988b79SJean-Baptiste Boric 	}
2319f988b79SJean-Baptiste Boric 
2329f988b79SJean-Baptiste Boric 	/* Check that fs has nonzero FAT size */
2339f988b79SJean-Baptiste Boric 	if (pmp->pm_FATsecs == 0) {
2349f988b79SJean-Baptiste Boric 		DPRINTF(("FATsecs is 0\n"));
2359f988b79SJean-Baptiste Boric 		error = EINVAL;
2369f988b79SJean-Baptiste Boric 		goto error_exit;
2379f988b79SJean-Baptiste Boric 	}
2389f988b79SJean-Baptiste Boric 
2399f988b79SJean-Baptiste Boric 	pmp->pm_fatblk = pmp->pm_ResSectors;
2409f988b79SJean-Baptiste Boric 	if (FAT32(pmp)) {
2419f988b79SJean-Baptiste Boric 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
2429f988b79SJean-Baptiste Boric 		pmp->pm_firstcluster = pmp->pm_fatblk
2439f988b79SJean-Baptiste Boric 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
2449f988b79SJean-Baptiste Boric 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
2459f988b79SJean-Baptiste Boric 	} else {
2469f988b79SJean-Baptiste Boric 		pmp->pm_rootdirblk = pmp->pm_fatblk +
2479f988b79SJean-Baptiste Boric 			(pmp->pm_FATs * pmp->pm_FATsecs);
2489f988b79SJean-Baptiste Boric 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
2499f988b79SJean-Baptiste Boric 				       + pmp->pm_BytesPerSec - 1)
2509f988b79SJean-Baptiste Boric 			/ pmp->pm_BytesPerSec;/* in sectors */
2519f988b79SJean-Baptiste Boric 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
2529f988b79SJean-Baptiste Boric 	}
2539f988b79SJean-Baptiste Boric 
2549f988b79SJean-Baptiste Boric 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
2559f988b79SJean-Baptiste Boric 	    SecPerClust;
2569f988b79SJean-Baptiste Boric 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
2579f988b79SJean-Baptiste Boric 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
2589f988b79SJean-Baptiste Boric 
2599f988b79SJean-Baptiste Boric 	if (flags & MSDOSFSMNT_GEMDOSFS) {
2609f988b79SJean-Baptiste Boric 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) {
2619f988b79SJean-Baptiste Boric 			pmp->pm_fatmask = FAT12_MASK;
2629f988b79SJean-Baptiste Boric 			pmp->pm_fatmult = 3;
2639f988b79SJean-Baptiste Boric 			pmp->pm_fatdiv = 2;
2649f988b79SJean-Baptiste Boric 		} else {
2659f988b79SJean-Baptiste Boric 			pmp->pm_fatmask = FAT16_MASK;
2669f988b79SJean-Baptiste Boric 			pmp->pm_fatmult = 2;
2679f988b79SJean-Baptiste Boric 			pmp->pm_fatdiv = 1;
2689f988b79SJean-Baptiste Boric 		}
2699f988b79SJean-Baptiste Boric 	} else if (pmp->pm_fatmask == 0) {
2709f988b79SJean-Baptiste Boric 		if (pmp->pm_maxcluster
2719f988b79SJean-Baptiste Boric 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
2729f988b79SJean-Baptiste Boric 			/*
2739f988b79SJean-Baptiste Boric 			 * This will usually be a floppy disk. This size makes
2749f988b79SJean-Baptiste Boric 			 * sure that one FAT entry will not be split across
2759f988b79SJean-Baptiste Boric 			 * multiple blocks.
2769f988b79SJean-Baptiste Boric 			 */
2779f988b79SJean-Baptiste Boric 			pmp->pm_fatmask = FAT12_MASK;
2789f988b79SJean-Baptiste Boric 			pmp->pm_fatmult = 3;
2799f988b79SJean-Baptiste Boric 			pmp->pm_fatdiv = 2;
2809f988b79SJean-Baptiste Boric 		} else {
2819f988b79SJean-Baptiste Boric 			pmp->pm_fatmask = FAT16_MASK;
2829f988b79SJean-Baptiste Boric 			pmp->pm_fatmult = 2;
2839f988b79SJean-Baptiste Boric 			pmp->pm_fatdiv = 1;
2849f988b79SJean-Baptiste Boric 		}
2859f988b79SJean-Baptiste Boric 	}
2869f988b79SJean-Baptiste Boric 	if (FAT12(pmp))
2879f988b79SJean-Baptiste Boric 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
2889f988b79SJean-Baptiste Boric 	else
2899f988b79SJean-Baptiste Boric 		pmp->pm_fatblocksize = MAXBSIZE;
2909f988b79SJean-Baptiste Boric 
2919f988b79SJean-Baptiste Boric 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
2929f988b79SJean-Baptiste Boric 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
2939f988b79SJean-Baptiste Boric 
2949f988b79SJean-Baptiste Boric 	/*
2959f988b79SJean-Baptiste Boric 	 * Compute mask and shift value for isolating cluster relative byte
2969f988b79SJean-Baptiste Boric 	 * offsets and cluster numbers from a file offset.
2979f988b79SJean-Baptiste Boric 	 */
2989f988b79SJean-Baptiste Boric 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
2999f988b79SJean-Baptiste Boric 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
3009f988b79SJean-Baptiste Boric 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
3019f988b79SJean-Baptiste Boric 
3029f988b79SJean-Baptiste Boric 	DPRINTF(("%s(fatmask=%lu, fatmult=%u, fatdiv=%u, fatblocksize=%lu, "
3039f988b79SJean-Baptiste Boric 	    "fatblocksec=%lu, bnshift=%lu, pbcluster=%lu, crbomask=%lu, "
3049f988b79SJean-Baptiste Boric 	    "cnshift=%lu)\n",
3059f988b79SJean-Baptiste Boric 	    __func__, pmp->pm_fatmask, pmp->pm_fatmult, pmp->pm_fatdiv,
3069f988b79SJean-Baptiste Boric 	    pmp->pm_fatblocksize, pmp->pm_fatblocksec, pmp->pm_bnshift,
3079f988b79SJean-Baptiste Boric 	    pmp->pm_bpcluster, pmp->pm_crbomask, pmp->pm_cnshift));
3089f988b79SJean-Baptiste Boric 	/*
3099f988b79SJean-Baptiste Boric 	 * Check for valid cluster size
3109f988b79SJean-Baptiste Boric 	 * must be a power of 2
3119f988b79SJean-Baptiste Boric 	 */
3129f988b79SJean-Baptiste Boric 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
3139f988b79SJean-Baptiste Boric 		DPRINTF(("bpcluster %lu cnshift %lu\n",
3149f988b79SJean-Baptiste Boric 		    pmp->pm_bpcluster, pmp->pm_cnshift));
3159f988b79SJean-Baptiste Boric 		error = EINVAL;
3169f988b79SJean-Baptiste Boric 		goto error_exit;
3179f988b79SJean-Baptiste Boric 	}
3189f988b79SJean-Baptiste Boric 
3199f988b79SJean-Baptiste Boric 	/*
3209f988b79SJean-Baptiste Boric 	 * Release the bootsector buffer.
3219f988b79SJean-Baptiste Boric 	 */
3229f988b79SJean-Baptiste Boric 	brelse(bp, BC_AGE);
3239f988b79SJean-Baptiste Boric 	bp = NULL;
3249f988b79SJean-Baptiste Boric 
3259f988b79SJean-Baptiste Boric 	/*
3269f988b79SJean-Baptiste Boric 	 * Check FSInfo.
3279f988b79SJean-Baptiste Boric 	 */
3289f988b79SJean-Baptiste Boric 	if (pmp->pm_fsinfo) {
3299f988b79SJean-Baptiste Boric 		struct fsinfo *fp;
3309f988b79SJean-Baptiste Boric 
3319f988b79SJean-Baptiste Boric 		/*
3329f988b79SJean-Baptiste Boric 		 * XXX	If the fsinfo block is stored on media with
3339f988b79SJean-Baptiste Boric 		 *	2KB or larger sectors, is the fsinfo structure
3349f988b79SJean-Baptiste Boric 		 *	padded at the end or in the middle?
3359f988b79SJean-Baptiste Boric 		 */
3369f988b79SJean-Baptiste Boric 		DPRINTF(("%s(bread %lu)\n", __func__,
3379f988b79SJean-Baptiste Boric 		    (unsigned long)de_bn2kb(pmp, pmp->pm_fsinfo)));
3389f988b79SJean-Baptiste Boric 		if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
339*0a6a1f1dSLionel Sambuc 		    pmp->pm_BytesPerSec, 0, &bp)) != 0)
3409f988b79SJean-Baptiste Boric 			goto error_exit;
3419f988b79SJean-Baptiste Boric 		fp = (struct fsinfo *)bp->b_data;
3429f988b79SJean-Baptiste Boric 		if (!memcmp(fp->fsisig1, "RRaA", 4)
3439f988b79SJean-Baptiste Boric 		    && !memcmp(fp->fsisig2, "rrAa", 4)
3449f988b79SJean-Baptiste Boric 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
3459f988b79SJean-Baptiste Boric 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
3469f988b79SJean-Baptiste Boric 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
3479f988b79SJean-Baptiste Boric 		else
3489f988b79SJean-Baptiste Boric 			pmp->pm_fsinfo = 0;
3499f988b79SJean-Baptiste Boric 		brelse(bp, 0);
3509f988b79SJean-Baptiste Boric 		bp = NULL;
3519f988b79SJean-Baptiste Boric 	}
3529f988b79SJean-Baptiste Boric 
3539f988b79SJean-Baptiste Boric 	/*
3549f988b79SJean-Baptiste Boric 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
3559f988b79SJean-Baptiste Boric 	 * XXX
3569f988b79SJean-Baptiste Boric 	 */
3579f988b79SJean-Baptiste Boric 	if (pmp->pm_fsinfo) {
3589f988b79SJean-Baptiste Boric 		if ((pmp->pm_nxtfree == 0xffffffffUL) ||
3599f988b79SJean-Baptiste Boric 		    (pmp->pm_nxtfree > pmp->pm_maxcluster))
3609f988b79SJean-Baptiste Boric 			pmp->pm_fsinfo = 0;
3619f988b79SJean-Baptiste Boric 	}
3629f988b79SJean-Baptiste Boric 
3639f988b79SJean-Baptiste Boric 	/*
3649f988b79SJean-Baptiste Boric 	 * Allocate memory for the bitmap of allocated clusters, and then
3659f988b79SJean-Baptiste Boric 	 * fill it in.
3669f988b79SJean-Baptiste Boric 	 */
3679f988b79SJean-Baptiste Boric 	pmp->pm_inusemap = ecalloc(sizeof(*pmp->pm_inusemap),
3689f988b79SJean-Baptiste Boric 	    ((pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS));
3699f988b79SJean-Baptiste Boric 	/*
3709f988b79SJean-Baptiste Boric 	 * fillinusemap() needs pm_devvp.
3719f988b79SJean-Baptiste Boric 	 */
3729f988b79SJean-Baptiste Boric 	pmp->pm_dev = 0;
3739f988b79SJean-Baptiste Boric 	pmp->pm_devvp = devvp;
3749f988b79SJean-Baptiste Boric 
3759f988b79SJean-Baptiste Boric 	/*
3769f988b79SJean-Baptiste Boric 	 * Have the inuse map filled in.
3779f988b79SJean-Baptiste Boric 	 */
3789f988b79SJean-Baptiste Boric 	if ((error = fillinusemap(pmp)) != 0) {
3799f988b79SJean-Baptiste Boric 		DPRINTF(("fillinusemap %d\n", error));
3809f988b79SJean-Baptiste Boric 		goto error_exit;
3819f988b79SJean-Baptiste Boric 	}
3829f988b79SJean-Baptiste Boric 
3839f988b79SJean-Baptiste Boric 	/*
3849f988b79SJean-Baptiste Boric 	 * Finish up.
3859f988b79SJean-Baptiste Boric 	 */
3869f988b79SJean-Baptiste Boric 	if (ronly)
3879f988b79SJean-Baptiste Boric 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
3889f988b79SJean-Baptiste Boric 	else
3899f988b79SJean-Baptiste Boric 		pmp->pm_fmod = 1;
3909f988b79SJean-Baptiste Boric 
3919f988b79SJean-Baptiste Boric 	/*
3929f988b79SJean-Baptiste Boric 	 * If we ever do quotas for DOS filesystems this would be a place
3939f988b79SJean-Baptiste Boric 	 * to fill in the info in the msdosfsmount structure. You dolt,
3949f988b79SJean-Baptiste Boric 	 * quotas on dos filesystems make no sense because files have no
3959f988b79SJean-Baptiste Boric 	 * owners on dos filesystems. of course there is some empty space
3969f988b79SJean-Baptiste Boric 	 * in the directory entry where we could put uid's and gid's.
3979f988b79SJean-Baptiste Boric 	 */
3989f988b79SJean-Baptiste Boric 
3999f988b79SJean-Baptiste Boric 	return pmp;
4009f988b79SJean-Baptiste Boric 
4019f988b79SJean-Baptiste Boric error_exit:
4029f988b79SJean-Baptiste Boric 	if (bp)
4039f988b79SJean-Baptiste Boric 		brelse(bp, BC_AGE);
4049f988b79SJean-Baptiste Boric 	if (pmp) {
4059f988b79SJean-Baptiste Boric 		if (pmp->pm_inusemap)
4069f988b79SJean-Baptiste Boric 			free(pmp->pm_inusemap);
4079f988b79SJean-Baptiste Boric 		free(pmp);
4089f988b79SJean-Baptiste Boric 	}
4099f988b79SJean-Baptiste Boric 	errno = error;
410*0a6a1f1dSLionel Sambuc 	return NULL;
4119f988b79SJean-Baptiste Boric }
4129f988b79SJean-Baptiste Boric 
4139f988b79SJean-Baptiste Boric int
msdosfs_root(struct msdosfsmount * pmp,struct vnode * vp)4149f988b79SJean-Baptiste Boric msdosfs_root(struct msdosfsmount *pmp, struct vnode *vp) {
4159f988b79SJean-Baptiste Boric 	struct denode *ndep;
4169f988b79SJean-Baptiste Boric 	int error;
4179f988b79SJean-Baptiste Boric 
4189f988b79SJean-Baptiste Boric 	*vp = *pmp->pm_devvp;
4199f988b79SJean-Baptiste Boric 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0) {
4209f988b79SJean-Baptiste Boric 		errno = error;
4219f988b79SJean-Baptiste Boric 		return -1;
4229f988b79SJean-Baptiste Boric 	}
4239f988b79SJean-Baptiste Boric 	vp->v_data = ndep;
4249f988b79SJean-Baptiste Boric 	return 0;
4259f988b79SJean-Baptiste Boric }
426