Home | History | Annotate | Line # | Download | only in msdos
msdosfs_vfsops.c revision 1.2
      1 /*-
      2  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
      3  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
      4  * All rights reserved.
      5  * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by TooLs GmbH.
     18  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 /*
     33  * Written by Paul Popelka (paulp (at) uts.amdahl.com)
     34  *
     35  * You can do anything you want with this software, just don't say you wrote
     36  * it, and don't remove this notice.
     37  *
     38  * This software is provided "as is".
     39  *
     40  * The author supplies this software to be publicly redistributed on the
     41  * understanding that the author is not responsible for the correct
     42  * functioning of this software in any circumstances and is not liable for
     43  * any damages caused by this software.
     44  *
     45  * October 1992
     46  */
     47 
     48 #if HAVE_NBTOOL_CONFIG_H
     49 #include "nbtool_config.h"
     50 #endif
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.2 2013/01/26 00:31:50 christos Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/dirent.h>
     58 #include <sys/mount.h>
     59 #include <sys/file.h>
     60 
     61 #include <ffs/buf.h>
     62 
     63 #include <fs/msdosfs/bpb.h>
     64 #include <fs/msdosfs/bootsect.h>
     65 #include <fs/msdosfs/direntry.h>
     66 #include <fs/msdosfs/denode.h>
     67 #include <fs/msdosfs/msdosfsmount.h>
     68 #include <fs/msdosfs/fat.h>
     69 
     70 #include <stdio.h>
     71 #include <errno.h>
     72 #include <stdlib.h>
     73 #include <string.h>
     74 
     75 #include "makefs.h"
     76 #include "msdos.h"
     77 #include "mkfs_msdos.h"
     78 
     79 #ifdef MSDOSFS_DEBUG
     80 #define DPRINTF(a) printf a
     81 #else
     82 #define DPRINTF(a)
     83 #endif
     84 
     85 struct msdosfsmount *
     86 msdosfs_mount(struct vnode *devvp, int flags)
     87 {
     88 	struct msdosfsmount *pmp = NULL;
     89 	struct buf *bp;
     90 	union bootsector *bsp;
     91 	struct byte_bpb33 *b33;
     92 	struct byte_bpb50 *b50;
     93 	struct byte_bpb710 *b710;
     94 	uint8_t SecPerClust;
     95 	int	ronly = 0, error, tmp;
     96 	int	bsize;
     97 	struct msdos_options *m = devvp->fs;
     98 	uint64_t psize = m->create_size;
     99 	unsigned secsize = 512;
    100 
    101 	if ((error = bread(devvp, 1, secsize, NULL, 0, &bp)) != 0)
    102 		goto error_exit;
    103 
    104 	bsp = (union bootsector *)bp->b_data;
    105 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
    106 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
    107 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
    108 
    109 	if (!(flags & MSDOSFSMNT_GEMDOSFS)) {
    110 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
    111 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
    112 			DPRINTF(("bootsig0 %d bootsig1 %d\n",
    113 			    bsp->bs50.bsBootSectSig0,
    114 			    bsp->bs50.bsBootSectSig1));
    115 			error = EINVAL;
    116 			goto error_exit;
    117 		}
    118 		bsize = 0;
    119 	} else
    120 		bsize = 512;
    121 
    122 	pmp = calloc(1, sizeof *pmp);
    123 	if (pmp == NULL)
    124 		goto error_exit;
    125 
    126 	/*
    127 	 * Compute several useful quantities from the bpb in the
    128 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
    129 	 * the fields that are different between dos 5 and dos 3.3.
    130 	 */
    131 	SecPerClust = b50->bpbSecPerClust;
    132 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
    133 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
    134 	pmp->pm_FATs = b50->bpbFATs;
    135 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
    136 	pmp->pm_Sectors = getushort(b50->bpbSectors);
    137 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
    138 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
    139 	pmp->pm_Heads = getushort(b50->bpbHeads);
    140 	pmp->pm_Media = b50->bpbMedia;
    141 
    142 	if (!(flags & MSDOSFSMNT_GEMDOSFS)) {
    143 		/* XXX - We should probably check more values here */
    144     		if (!pmp->pm_BytesPerSec || !SecPerClust
    145 	    		|| pmp->pm_SecPerTrack > 63) {
    146 			DPRINTF(("bytespersec %d secperclust %d "
    147 			    "secpertrack %d\n",
    148 			    pmp->pm_BytesPerSec, SecPerClust,
    149 			    pmp->pm_SecPerTrack));
    150 			error = EINVAL;
    151 			goto error_exit;
    152 		}
    153 	}
    154 
    155 	if (pmp->pm_Sectors == 0) {
    156 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
    157 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
    158 	} else {
    159 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
    160 		pmp->pm_HugeSectors = pmp->pm_Sectors;
    161 	}
    162 
    163 	if (pmp->pm_RootDirEnts == 0) {
    164 		unsigned short vers = getushort(b710->bpbFSVers);
    165 		/*
    166 		 * Some say that bsBootSectSig[23] must be zero, but
    167 		 * Windows does not require this and some digital cameras
    168 		 * do not set these to zero.  Therefore, do not insist.
    169 		 */
    170 		if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
    171 			DPRINTF(("sectors %d fatsecs %lu vers %d\n",
    172 			    pmp->pm_Sectors, pmp->pm_FATsecs, vers));
    173 			error = EINVAL;
    174 			goto error_exit;
    175 		}
    176 		pmp->pm_fatmask = FAT32_MASK;
    177 		pmp->pm_fatmult = 4;
    178 		pmp->pm_fatdiv = 1;
    179 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
    180 
    181 		/* mirrorring is enabled if the FATMIRROR bit is not set */
    182 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
    183 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
    184 		else
    185 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
    186 	} else
    187 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
    188 
    189 	if (flags & MSDOSFSMNT_GEMDOSFS) {
    190 		if (FAT32(pmp)) {
    191 			DPRINTF(("FAT32 for GEMDOS\n"));
    192 			/*
    193 			 * GEMDOS doesn't know FAT32.
    194 			 */
    195 			error = EINVAL;
    196 			goto error_exit;
    197 		}
    198 
    199 		/*
    200 		 * Check a few values (could do some more):
    201 		 * - logical sector size: power of 2, >= block size
    202 		 * - sectors per cluster: power of 2, >= 1
    203 		 * - number of sectors:   >= 1, <= size of partition
    204 		 */
    205 		if ( (SecPerClust == 0)
    206 		  || (SecPerClust & (SecPerClust - 1))
    207 		  || (pmp->pm_BytesPerSec < bsize)
    208 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
    209 		  || (pmp->pm_HugeSectors == 0)
    210 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
    211 		      > psize)) {
    212 			DPRINTF(("consistency checks for GEMDOS\n"));
    213 			error = EINVAL;
    214 			goto error_exit;
    215 		}
    216 		/*
    217 		 * XXX - Many parts of the msdosfs driver seem to assume that
    218 		 * the number of bytes per logical sector (BytesPerSec) will
    219 		 * always be the same as the number of bytes per disk block
    220 		 * Let's pretend it is.
    221 		 */
    222 		tmp = pmp->pm_BytesPerSec / bsize;
    223 		pmp->pm_BytesPerSec  = bsize;
    224 		pmp->pm_HugeSectors *= tmp;
    225 		pmp->pm_HiddenSects *= tmp;
    226 		pmp->pm_ResSectors  *= tmp;
    227 		pmp->pm_Sectors     *= tmp;
    228 		pmp->pm_FATsecs     *= tmp;
    229 		SecPerClust         *= tmp;
    230 	}
    231 
    232 	/* Check that fs has nonzero FAT size */
    233 	if (pmp->pm_FATsecs == 0) {
    234 		DPRINTF(("FATsecs is 0\n"));
    235 		error = EINVAL;
    236 		goto error_exit;
    237 	}
    238 
    239 	pmp->pm_fatblk = pmp->pm_ResSectors;
    240 	if (FAT32(pmp)) {
    241 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
    242 		pmp->pm_firstcluster = pmp->pm_fatblk
    243 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
    244 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
    245 	} else {
    246 		pmp->pm_rootdirblk = pmp->pm_fatblk +
    247 			(pmp->pm_FATs * pmp->pm_FATsecs);
    248 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
    249 				       + pmp->pm_BytesPerSec - 1)
    250 			/ pmp->pm_BytesPerSec;/* in sectors */
    251 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
    252 	}
    253 
    254 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
    255 	    SecPerClust;
    256 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
    257 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
    258 
    259 	if (flags & MSDOSFSMNT_GEMDOSFS) {
    260 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) {
    261 			pmp->pm_fatmask = FAT12_MASK;
    262 			pmp->pm_fatmult = 3;
    263 			pmp->pm_fatdiv = 2;
    264 		} else {
    265 			pmp->pm_fatmask = FAT16_MASK;
    266 			pmp->pm_fatmult = 2;
    267 			pmp->pm_fatdiv = 1;
    268 		}
    269 	} else if (pmp->pm_fatmask == 0) {
    270 		if (pmp->pm_maxcluster
    271 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
    272 			/*
    273 			 * This will usually be a floppy disk. This size makes
    274 			 * sure that one FAT entry will not be split across
    275 			 * multiple blocks.
    276 			 */
    277 			pmp->pm_fatmask = FAT12_MASK;
    278 			pmp->pm_fatmult = 3;
    279 			pmp->pm_fatdiv = 2;
    280 		} else {
    281 			pmp->pm_fatmask = FAT16_MASK;
    282 			pmp->pm_fatmult = 2;
    283 			pmp->pm_fatdiv = 1;
    284 		}
    285 	}
    286 	if (FAT12(pmp))
    287 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
    288 	else
    289 		pmp->pm_fatblocksize = MAXBSIZE;
    290 
    291 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
    292 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
    293 
    294 	/*
    295 	 * Compute mask and shift value for isolating cluster relative byte
    296 	 * offsets and cluster numbers from a file offset.
    297 	 */
    298 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
    299 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
    300 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
    301 
    302 	/*
    303 	 * Check for valid cluster size
    304 	 * must be a power of 2
    305 	 */
    306 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
    307 		DPRINTF(("bpcluster %lu cnshift %lu\n",
    308 		    pmp->pm_bpcluster, pmp->pm_cnshift));
    309 		error = EINVAL;
    310 		goto error_exit;
    311 	}
    312 
    313 	/*
    314 	 * Release the bootsector buffer.
    315 	 */
    316 	brelse(bp, BC_AGE);
    317 	bp = NULL;
    318 
    319 	/*
    320 	 * Check FSInfo.
    321 	 */
    322 	if (pmp->pm_fsinfo) {
    323 		struct fsinfo *fp;
    324 
    325 		/*
    326 		 * XXX	If the fsinfo block is stored on media with
    327 		 *	2KB or larger sectors, is the fsinfo structure
    328 		 *	padded at the end or in the middle?
    329 		 */
    330 		if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
    331 		    pmp->pm_BytesPerSec, NULL, 0, &bp)) != 0)
    332 			goto error_exit;
    333 		fp = (struct fsinfo *)bp->b_data;
    334 		if (!memcmp(fp->fsisig1, "RRaA", 4)
    335 		    && !memcmp(fp->fsisig2, "rrAa", 4)
    336 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
    337 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
    338 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
    339 		else
    340 			pmp->pm_fsinfo = 0;
    341 		brelse(bp, 0);
    342 		bp = NULL;
    343 	}
    344 
    345 	/*
    346 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
    347 	 * XXX
    348 	 */
    349 	if (pmp->pm_fsinfo) {
    350 		if ((pmp->pm_nxtfree == 0xffffffffUL) ||
    351 		    (pmp->pm_nxtfree > pmp->pm_maxcluster))
    352 			pmp->pm_fsinfo = 0;
    353 	}
    354 
    355 	/*
    356 	 * Allocate memory for the bitmap of allocated clusters, and then
    357 	 * fill it in.
    358 	 */
    359 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS)
    360 				   / N_INUSEBITS)
    361 				  * sizeof(*pmp->pm_inusemap));
    362 	if (pmp->pm_inusemap == NULL)
    363 		goto error_exit;
    364 
    365 	/*
    366 	 * fillinusemap() needs pm_devvp.
    367 	 */
    368 	pmp->pm_dev = 0;
    369 	pmp->pm_devvp = devvp;
    370 
    371 	/*
    372 	 * Have the inuse map filled in.
    373 	 */
    374 	if ((error = fillinusemap(pmp)) != 0) {
    375 		DPRINTF(("fillinusemap %d\n", error));
    376 		goto error_exit;
    377 	}
    378 
    379 	/*
    380 	 * If they want FAT updates to be synchronous then let them suffer
    381 	 * the performance degradation in exchange for the on disk copy of
    382 	 * the FAT being correct just about all the time.  I suppose this
    383 	 * would be a good thing to turn on if the kernel is still flakey.
    384 	 */
    385 	if (flags & MNT_SYNCHRONOUS)
    386 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
    387 
    388 	/*
    389 	 * Finish up.
    390 	 */
    391 	if (ronly)
    392 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
    393 	else
    394 		pmp->pm_fmod = 1;
    395 
    396 	/*
    397 	 * If we ever do quotas for DOS filesystems this would be a place
    398 	 * to fill in the info in the msdosfsmount structure. You dolt,
    399 	 * quotas on dos filesystems make no sense because files have no
    400 	 * owners on dos filesystems. of course there is some empty space
    401 	 * in the directory entry where we could put uid's and gid's.
    402 	 */
    403 
    404 	return pmp;
    405 
    406 error_exit:
    407 	if (bp)
    408 		brelse(bp, BC_AGE);
    409 	if (pmp) {
    410 		if (pmp->pm_inusemap)
    411 			free(pmp->pm_inusemap);
    412 		free(pmp);
    413 	}
    414 	errno = error;
    415 	return pmp;
    416 }
    417 
    418 int
    419 msdosfs_root(struct msdosfsmount *pmp, struct vnode *vp) {
    420 	struct denode *ndep;
    421 	int error;
    422 
    423 	*vp = *pmp->pm_devvp;
    424 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0) {
    425 		errno = error;
    426 		return -1;
    427 	}
    428 	vp->v_data = ndep;
    429 	return 0;
    430 }
    431