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 <vfs/msdosfs/bootsect.h>
65 #include <vfs/msdosfs/bpb.h>
66 #include "msdos/denode.h"
67 #include <vfs/msdosfs/fat.h>
68 #include <vfs/msdosfs/msdosfsmount.h>
69 
70 #include <mkfs_msdos.h>
71 
72 #include "makefs.h"
73 #include "msdos.h"
74 
75 struct msdosfsmount *
76 m_msdosfs_mount(struct m_vnode *devvp)
77 {
78 	struct msdosfsmount *pmp = NULL;
79 	struct m_buf *bp;
80 	union bootsector *bsp;
81 	struct byte_bpb33 *b33;
82 	struct byte_bpb50 *b50;
83 	struct byte_bpb710 *b710;
84 	uint8_t SecPerClust;
85 	int	ronly = 0, error;
86 	unsigned secsize = 512;
87 
88 	MSDOSFS_DPRINTF(("%s(bread 0)\n", __func__));
89 	if ((error = bread((void *)devvp, 0, secsize, 0, &bp)) != 0)
90 		goto error_exit;
91 
92 	bsp = (union bootsector *)bp->b_data;
93 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
94 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
95 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
96 
97 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
98 	    bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
99 		MSDOSFS_DPRINTF(("bootsig0 %d bootsig1 %d\n",
100 		    bsp->bs50.bsBootSectSig0,
101 		    bsp->bs50.bsBootSectSig1));
102 		error = EINVAL;
103 		goto error_exit;
104 	}
105 
106 	pmp = ecalloc(1, sizeof(*pmp));
107 	/*
108 	 * Compute several useful quantities from the bpb in the
109 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
110 	 * the fields that are different between dos 5 and dos 3.3.
111 	 */
112 	SecPerClust = b50->bpbSecPerClust;
113 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
114 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
115 	pmp->pm_FATs = b50->bpbFATs;
116 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
117 	pmp->pm_Sectors = getushort(b50->bpbSectors);
118 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
119 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
120 	pmp->pm_Heads = getushort(b50->bpbHeads);
121 	pmp->pm_Media = b50->bpbMedia;
122 
123 	MSDOSFS_DPRINTF(("%s(BytesPerSec=%u, ResSectors=%u, FATs=%d, "
124 	    "RootDirEnts=%u, Sectors=%u, FATsecs=%lu, SecPerTrack=%u, "
125 	    "Heads=%u, Media=%u)\n",
126 	    __func__, pmp->pm_BytesPerSec, pmp->pm_ResSectors,
127 	    pmp->pm_FATs, pmp->pm_RootDirEnts, pmp->pm_Sectors,
128 	    pmp->pm_FATsecs, pmp->pm_SecPerTrack, pmp->pm_Heads,
129 	    pmp->pm_Media));
130 
131 	/* XXX - We should probably check more values here */
132 	if (!pmp->pm_BytesPerSec || !SecPerClust
133 		|| pmp->pm_SecPerTrack > 63) {
134 		MSDOSFS_DPRINTF(("bytespersec %d secperclust %d "
135 		    "secpertrack %d\n", pmp->pm_BytesPerSec,
136 		    SecPerClust, pmp->pm_SecPerTrack));
137 		error = EINVAL;
138 		goto error_exit;
139 	}
140 
141 	if (pmp->pm_Sectors == 0) {
142 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
143 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
144 	} else {
145 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
146 		pmp->pm_HugeSectors = pmp->pm_Sectors;
147 	}
148 
149 	pmp->pm_flags = 0;
150 	if (pmp->pm_RootDirEnts == 0) {
151 		unsigned short vers = getushort(b710->bpbFSVers);
152 		/*
153 		 * Some say that bsBootSectSig[23] must be zero, but
154 		 * Windows does not require this and some digital cameras
155 		 * do not set these to zero.  Therefore, do not insist.
156 		 */
157 		if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
158 			MSDOSFS_DPRINTF(("sectors %d fatsecs %lu vers %d\n",
159 			    pmp->pm_Sectors, pmp->pm_FATsecs, vers));
160 			error = EINVAL;
161 			goto error_exit;
162 		}
163 		pmp->pm_fatmask = FAT32_MASK;
164 		pmp->pm_fatmult = 4;
165 		pmp->pm_fatdiv = 1;
166 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
167 
168 		/* mirrorring is enabled if the FATMIRROR bit is not set */
169 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
170 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
171 		else
172 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
173 	} else
174 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
175 
176 	/* Check that fs has nonzero FAT size */
177 	if (pmp->pm_FATsecs == 0) {
178 		MSDOSFS_DPRINTF(("FATsecs is 0\n"));
179 		error = EINVAL;
180 		goto error_exit;
181 	}
182 
183 	pmp->pm_fatblk = pmp->pm_ResSectors;
184 	if (FAT32(pmp)) {
185 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
186 		pmp->pm_firstcluster = pmp->pm_fatblk
187 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
188 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
189 	} else {
190 		pmp->pm_rootdirblk = pmp->pm_fatblk +
191 			(pmp->pm_FATs * pmp->pm_FATsecs);
192 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
193 				       + pmp->pm_BytesPerSec - 1)
194 			/ pmp->pm_BytesPerSec;/* in sectors */
195 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
196 	}
197 
198 	pmp->pm_maxcluster = ((pmp->pm_HugeSectors - pmp->pm_firstcluster) /
199 	    SecPerClust) + 1;
200 	/* XXX unused + non existent in DragonFly */
201 	//pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
202 
203 	if (pmp->pm_fatmask == 0) {
204 		if (pmp->pm_maxcluster
205 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
206 			/*
207 			 * This will usually be a floppy disk. This size makes
208 			 * sure that one FAT entry will not be split across
209 			 * multiple blocks.
210 			 */
211 			pmp->pm_fatmask = FAT12_MASK;
212 			pmp->pm_fatmult = 3;
213 			pmp->pm_fatdiv = 2;
214 		} else {
215 			pmp->pm_fatmask = FAT16_MASK;
216 			pmp->pm_fatmult = 2;
217 			pmp->pm_fatdiv = 1;
218 		}
219 	}
220 	if (FAT12(pmp))
221 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
222 	else
223 		pmp->pm_fatblocksize = MAXBSIZE;
224 
225 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
226 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
227 
228 	/*
229 	 * Compute mask and shift value for isolating cluster relative byte
230 	 * offsets and cluster numbers from a file offset.
231 	 */
232 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
233 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
234 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
235 
236 	MSDOSFS_DPRINTF(("%s(fatmask=%lu, fatmult=%u, fatdiv=%u, "
237 	    "fatblocksize=%lu, fatblocksec=%lu, bnshift=%lu, pbcluster=%lu, "
238 	    "crbomask=%lu, cnshift=%lu)\n",
239 	    __func__, (unsigned long)pmp->pm_fatmask, pmp->pm_fatmult,
240 	    pmp->pm_fatdiv, pmp->pm_fatblocksize, pmp->pm_fatblocksec,
241 	    pmp->pm_bnshift, pmp->pm_bpcluster, pmp->pm_crbomask,
242 	    pmp->pm_cnshift));
243 	/*
244 	 * Check for valid cluster size
245 	 * must be a power of 2
246 	 */
247 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
248 		MSDOSFS_DPRINTF(("bpcluster %lu cnshift %lu\n",
249 		    pmp->pm_bpcluster, pmp->pm_cnshift));
250 		error = EINVAL;
251 		goto error_exit;
252 	}
253 
254 	/*
255 	 * Release the bootsector buffer.
256 	 */
257 	brelse(bp);
258 	bp = NULL;
259 
260 	/*
261 	 * Check FSInfo.
262 	 */
263 	if (pmp->pm_fsinfo) {
264 		struct fsinfo *fp;
265 
266 		/*
267 		 * XXX	If the fsinfo block is stored on media with
268 		 *	2KB or larger sectors, is the fsinfo structure
269 		 *	padded at the end or in the middle?
270 		 */
271 		if ((error = bread((void *)devvp, pmp->pm_fsinfo,
272 		    pmp->pm_BytesPerSec, 0, &bp)) != 0)
273 			goto error_exit;
274 		fp = (struct fsinfo *)bp->b_data;
275 		if (!memcmp(fp->fsisig1, "RRaA", 4)
276 		    && !memcmp(fp->fsisig2, "rrAa", 4)
277 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4))
278 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
279 		else
280 			pmp->pm_fsinfo = 0;
281 		brelse(bp);
282 		bp = NULL;
283 	}
284 
285 	/*
286 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
287 	 * XXX
288 	 */
289 	if (pmp->pm_fsinfo) {
290 		if ((pmp->pm_nxtfree == 0xffffffffUL) ||
291 		    (pmp->pm_nxtfree > pmp->pm_maxcluster))
292 			pmp->pm_fsinfo = 0;
293 	}
294 
295 	/*
296 	 * Allocate memory for the bitmap of allocated clusters, and then
297 	 * fill it in.
298 	 */
299 	pmp->pm_inusemap = ecalloc(sizeof(*pmp->pm_inusemap),
300 	    ((pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS));
301 	/*
302 	 * fillinusemap() needs pm_devvp.
303 	 */
304 	/* XXX unused + not compiled when MAKEFS in DragonFly */
305 	//pmp->pm_dev = 0;
306 	pmp->pm_devvp = (void *)devvp;
307 
308 	/*
309 	 * Have the inuse map filled in.
310 	 */
311 	if ((error = fillinusemap(pmp)) != 0) {
312 		MSDOSFS_DPRINTF(("fillinusemap %d\n", error));
313 		goto error_exit;
314 	}
315 
316 	/*
317 	 * Finish up.
318 	 */
319 	if (ronly)
320 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
321 	else
322 		pmp->pm_fmod = 1;
323 
324 	/*
325 	 * If we ever do quotas for DOS filesystems this would be a place
326 	 * to fill in the info in the msdosfsmount structure. You dolt,
327 	 * quotas on dos filesystems make no sense because files have no
328 	 * owners on dos filesystems. of course there is some empty space
329 	 * in the directory entry where we could put uid's and gid's.
330 	 */
331 
332 	return pmp;
333 
334 error_exit:
335 	if (bp)
336 		brelse(bp);
337 	if (pmp) {
338 		if (pmp->pm_inusemap)
339 			free(pmp->pm_inusemap);
340 		free(pmp);
341 	}
342 	errno = error;
343 	return NULL;
344 }
345 
346 int
347 msdosfs_root(struct msdosfsmount *pmp, struct m_vnode *vp) {
348 	struct denode *ndep;
349 	int error;
350 
351 	*vp = *(struct m_vnode *)pmp->pm_devvp;
352 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0) {
353 		errno = error;
354 		return -1;
355 	}
356 	vp->v_data = ndep;
357 	return 0;
358 }
359 
360 /*
361  * If we have an FSInfo block, update it.
362  */
363 int
364 msdosfs_fsiflush(struct msdosfsmount *pmp)
365 {
366 	struct fsinfo *fp;
367 	struct m_buf *bp;
368 	int error;
369 
370 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
371 		error = 0;
372 		goto out;
373 	}
374 	error = bread((void *)pmp->pm_devvp, pmp->pm_fsinfo,
375 	    pmp->pm_BytesPerSec, NOCRED, &bp);
376 	if (error != 0) {
377 		brelse(bp);
378 		goto out;
379 	}
380 	fp = (struct fsinfo *)bp->b_data;
381 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
382 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
383 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
384 	error = bwrite(bp);
385 
386 out:
387 	return (error);
388 }
389