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