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