1237d1b14SEd Maste /*-
22037e988SEd Maste  * SPDX-License-Identifier: BSD-4-Clause
32037e988SEd Maste  *
4237d1b14SEd Maste  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5237d1b14SEd Maste  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6237d1b14SEd Maste  * All rights reserved.
7237d1b14SEd Maste  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8237d1b14SEd Maste  *
9237d1b14SEd Maste  * Redistribution and use in source and binary forms, with or without
10237d1b14SEd Maste  * modification, are permitted provided that the following conditions
11237d1b14SEd Maste  * are met:
12237d1b14SEd Maste  * 1. Redistributions of source code must retain the above copyright
13237d1b14SEd Maste  *    notice, this list of conditions and the following disclaimer.
14237d1b14SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
15237d1b14SEd Maste  *    notice, this list of conditions and the following disclaimer in the
16237d1b14SEd Maste  *    documentation and/or other materials provided with the distribution.
17237d1b14SEd Maste  * 3. All advertising materials mentioning features or use of this software
18237d1b14SEd Maste  *    must display the following acknowledgement:
19237d1b14SEd Maste  *	This product includes software developed by TooLs GmbH.
20237d1b14SEd Maste  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21237d1b14SEd Maste  *    derived from this software without specific prior written permission.
22237d1b14SEd Maste  *
23237d1b14SEd Maste  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24237d1b14SEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25237d1b14SEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26237d1b14SEd Maste  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27237d1b14SEd Maste  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28237d1b14SEd Maste  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29237d1b14SEd Maste  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30237d1b14SEd Maste  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31237d1b14SEd Maste  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32237d1b14SEd Maste  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33237d1b14SEd Maste  */
342037e988SEd Maste /*-
35237d1b14SEd Maste  * Written by Paul Popelka (paulp@uts.amdahl.com)
36237d1b14SEd Maste  *
37237d1b14SEd Maste  * You can do anything you want with this software, just don't say you wrote
38237d1b14SEd Maste  * it, and don't remove this notice.
39237d1b14SEd Maste  *
40237d1b14SEd Maste  * This software is provided "as is".
41237d1b14SEd Maste  *
42237d1b14SEd Maste  * The author supplies this software to be publicly redistributed on the
43237d1b14SEd Maste  * understanding that the author is not responsible for the correct
44237d1b14SEd Maste  * functioning of this software in any circumstances and is not liable for
45237d1b14SEd Maste  * any damages caused by this software.
46237d1b14SEd Maste  *
47237d1b14SEd Maste  * October 1992
48237d1b14SEd Maste  */
49237d1b14SEd Maste 
50237d1b14SEd Maste #include <sys/cdefs.h>
51237d1b14SEd Maste /* $NetBSD: msdosfs_vfsops.c,v 1.10 2016/01/30 09:59:27 mlelstv Exp $ */
52237d1b14SEd Maste #include <sys/param.h>
5398dc8da5SEd Maste #include <sys/mount.h>
54237d1b14SEd Maste 
55237d1b14SEd Maste #include <errno.h>
56aaa38524SConrad Meyer #include <stdbool.h>
5798dc8da5SEd Maste #include <stdio.h>
58237d1b14SEd Maste #include <string.h>
5998dc8da5SEd Maste #include <stdlib.h>
60237d1b14SEd Maste #include <util.h>
61237d1b14SEd Maste 
6298dc8da5SEd Maste #include <fs/msdosfs/bootsect.h>
6398dc8da5SEd Maste #include <fs/msdosfs/bpb.h>
64ba2cfa80SAlex Richardson #include "msdos/denode.h"
6551e79affSEd Maste #include <fs/msdosfs/fat.h>
66840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h>
6798dc8da5SEd Maste 
6898dc8da5SEd Maste #include <mkfs_msdos.h>
6998dc8da5SEd Maste 
70237d1b14SEd Maste #include "makefs.h"
71237d1b14SEd Maste #include "msdos.h"
72237d1b14SEd Maste 
73237d1b14SEd Maste struct msdosfsmount *
m_msdosfs_mount(struct m_vnode * devvp)74d485c77fSKonstantin Belousov m_msdosfs_mount(struct m_vnode *devvp)
75237d1b14SEd Maste {
76237d1b14SEd Maste 	struct msdosfsmount *pmp = NULL;
77d485c77fSKonstantin Belousov 	struct m_buf *bp;
78237d1b14SEd Maste 	union bootsector *bsp;
79237d1b14SEd Maste 	struct byte_bpb33 *b33;
80237d1b14SEd Maste 	struct byte_bpb50 *b50;
81237d1b14SEd Maste 	struct byte_bpb710 *b710;
82237d1b14SEd Maste 	uint8_t SecPerClust;
8398dc8da5SEd Maste 	int	ronly = 0, error;
84237d1b14SEd Maste 	unsigned secsize = 512;
85237d1b14SEd Maste 
8698dc8da5SEd Maste 	MSDOSFS_DPRINTF(("%s(bread 0)\n", __func__));
87d485c77fSKonstantin Belousov 	if ((error = bread((void *)devvp, 0, secsize, 0, &bp)) != 0)
88237d1b14SEd Maste 		goto error_exit;
89237d1b14SEd Maste 
90237d1b14SEd Maste 	bsp = (union bootsector *)bp->b_data;
91237d1b14SEd Maste 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
92237d1b14SEd Maste 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
93237d1b14SEd Maste 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
94237d1b14SEd Maste 
9598dc8da5SEd Maste 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
9698dc8da5SEd Maste 	    bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
9798dc8da5SEd Maste 		MSDOSFS_DPRINTF(("bootsig0 %d bootsig1 %d\n",
98237d1b14SEd Maste 		    bsp->bs50.bsBootSectSig0,
99237d1b14SEd Maste 		    bsp->bs50.bsBootSectSig1));
100237d1b14SEd Maste 		error = EINVAL;
101237d1b14SEd Maste 		goto error_exit;
102237d1b14SEd Maste 	}
103237d1b14SEd Maste 
10498dc8da5SEd Maste 	pmp = ecalloc(1, sizeof(*pmp));
105237d1b14SEd Maste 	/*
106237d1b14SEd Maste 	 * Compute several useful quantities from the bpb in the
107237d1b14SEd Maste 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
108237d1b14SEd Maste 	 * the fields that are different between dos 5 and dos 3.3.
109237d1b14SEd Maste 	 */
110237d1b14SEd Maste 	SecPerClust = b50->bpbSecPerClust;
111237d1b14SEd Maste 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
112237d1b14SEd Maste 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
113237d1b14SEd Maste 	pmp->pm_FATs = b50->bpbFATs;
114237d1b14SEd Maste 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
115237d1b14SEd Maste 	pmp->pm_Sectors = getushort(b50->bpbSectors);
116237d1b14SEd Maste 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
117237d1b14SEd Maste 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
118237d1b14SEd Maste 	pmp->pm_Heads = getushort(b50->bpbHeads);
119237d1b14SEd Maste 	pmp->pm_Media = b50->bpbMedia;
120237d1b14SEd Maste 
12198dc8da5SEd Maste 	MSDOSFS_DPRINTF(("%s(BytesPerSec=%u, ResSectors=%u, FATs=%d, "
12298dc8da5SEd Maste 	    "RootDirEnts=%u, Sectors=%u, FATsecs=%lu, SecPerTrack=%u, "
12398dc8da5SEd Maste 	    "Heads=%u, Media=%u)\n",
12498dc8da5SEd Maste 	    __func__, pmp->pm_BytesPerSec, pmp->pm_ResSectors,
12598dc8da5SEd Maste 	    pmp->pm_FATs, pmp->pm_RootDirEnts, pmp->pm_Sectors,
12698dc8da5SEd Maste 	    pmp->pm_FATsecs, pmp->pm_SecPerTrack, pmp->pm_Heads,
12798dc8da5SEd Maste 	    pmp->pm_Media));
12898dc8da5SEd Maste 
129237d1b14SEd Maste 	/* XXX - We should probably check more values here */
130237d1b14SEd Maste 	if (!pmp->pm_BytesPerSec || !SecPerClust
131237d1b14SEd Maste 		|| pmp->pm_SecPerTrack > 63) {
13298dc8da5SEd Maste 		MSDOSFS_DPRINTF(("bytespersec %d secperclust %d "
13398dc8da5SEd Maste 		    "secpertrack %d\n", pmp->pm_BytesPerSec,
13498dc8da5SEd Maste 		    SecPerClust, pmp->pm_SecPerTrack));
135237d1b14SEd Maste 		error = EINVAL;
136237d1b14SEd Maste 		goto error_exit;
137237d1b14SEd Maste 	}
138237d1b14SEd Maste 
139237d1b14SEd Maste 	if (pmp->pm_Sectors == 0) {
140237d1b14SEd Maste 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
141237d1b14SEd Maste 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
142237d1b14SEd Maste 	} else {
143237d1b14SEd Maste 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
144237d1b14SEd Maste 		pmp->pm_HugeSectors = pmp->pm_Sectors;
145237d1b14SEd Maste 	}
146237d1b14SEd Maste 
14798dc8da5SEd Maste 	pmp->pm_flags = 0;
148237d1b14SEd Maste 	if (pmp->pm_RootDirEnts == 0) {
149237d1b14SEd Maste 		unsigned short vers = getushort(b710->bpbFSVers);
150237d1b14SEd Maste 		/*
151237d1b14SEd Maste 		 * Some say that bsBootSectSig[23] must be zero, but
152237d1b14SEd Maste 		 * Windows does not require this and some digital cameras
153237d1b14SEd Maste 		 * do not set these to zero.  Therefore, do not insist.
154237d1b14SEd Maste 		 */
155237d1b14SEd Maste 		if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
15698dc8da5SEd Maste 			MSDOSFS_DPRINTF(("sectors %d fatsecs %lu vers %d\n",
157237d1b14SEd Maste 			    pmp->pm_Sectors, pmp->pm_FATsecs, vers));
158237d1b14SEd Maste 			error = EINVAL;
159237d1b14SEd Maste 			goto error_exit;
160237d1b14SEd Maste 		}
161237d1b14SEd Maste 		pmp->pm_fatmask = FAT32_MASK;
162237d1b14SEd Maste 		pmp->pm_fatmult = 4;
163237d1b14SEd Maste 		pmp->pm_fatdiv = 1;
164237d1b14SEd Maste 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
165237d1b14SEd Maste 
166237d1b14SEd Maste 		/* mirrorring is enabled if the FATMIRROR bit is not set */
167237d1b14SEd Maste 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
168237d1b14SEd Maste 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
169237d1b14SEd Maste 		else
170237d1b14SEd Maste 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
171237d1b14SEd Maste 	} else
172237d1b14SEd Maste 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
173237d1b14SEd Maste 
174237d1b14SEd Maste 	/* Check that fs has nonzero FAT size */
175237d1b14SEd Maste 	if (pmp->pm_FATsecs == 0) {
17698dc8da5SEd Maste 		MSDOSFS_DPRINTF(("FATsecs is 0\n"));
177237d1b14SEd Maste 		error = EINVAL;
178237d1b14SEd Maste 		goto error_exit;
179237d1b14SEd Maste 	}
180237d1b14SEd Maste 
181237d1b14SEd Maste 	pmp->pm_fatblk = pmp->pm_ResSectors;
182237d1b14SEd Maste 	if (FAT32(pmp)) {
183237d1b14SEd Maste 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
184237d1b14SEd Maste 		pmp->pm_firstcluster = pmp->pm_fatblk
185237d1b14SEd Maste 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
186237d1b14SEd Maste 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
187237d1b14SEd Maste 	} else {
188237d1b14SEd Maste 		pmp->pm_rootdirblk = pmp->pm_fatblk +
189237d1b14SEd Maste 			(pmp->pm_FATs * pmp->pm_FATsecs);
190237d1b14SEd Maste 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
191237d1b14SEd Maste 				       + pmp->pm_BytesPerSec - 1)
192237d1b14SEd Maste 			/ pmp->pm_BytesPerSec;/* in sectors */
193237d1b14SEd Maste 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
194237d1b14SEd Maste 	}
195237d1b14SEd Maste 
19698dc8da5SEd Maste 	pmp->pm_maxcluster = ((pmp->pm_HugeSectors - pmp->pm_firstcluster) /
19798dc8da5SEd Maste 	    SecPerClust) + 1;
198237d1b14SEd Maste 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
199237d1b14SEd Maste 
20098dc8da5SEd Maste 	if (pmp->pm_fatmask == 0) {
201237d1b14SEd Maste 		if (pmp->pm_maxcluster
202237d1b14SEd Maste 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
203237d1b14SEd Maste 			/*
204237d1b14SEd Maste 			 * This will usually be a floppy disk. This size makes
205237d1b14SEd Maste 			 * sure that one FAT entry will not be split across
206237d1b14SEd Maste 			 * multiple blocks.
207237d1b14SEd Maste 			 */
208237d1b14SEd Maste 			pmp->pm_fatmask = FAT12_MASK;
209237d1b14SEd Maste 			pmp->pm_fatmult = 3;
210237d1b14SEd Maste 			pmp->pm_fatdiv = 2;
211237d1b14SEd Maste 		} else {
212237d1b14SEd Maste 			pmp->pm_fatmask = FAT16_MASK;
213237d1b14SEd Maste 			pmp->pm_fatmult = 2;
214237d1b14SEd Maste 			pmp->pm_fatdiv = 1;
215237d1b14SEd Maste 		}
216237d1b14SEd Maste 	}
217237d1b14SEd Maste 	if (FAT12(pmp))
218237d1b14SEd Maste 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
219237d1b14SEd Maste 	else
220237d1b14SEd Maste 		pmp->pm_fatblocksize = MAXBSIZE;
221237d1b14SEd Maste 
222237d1b14SEd Maste 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
223237d1b14SEd Maste 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
224237d1b14SEd Maste 
225237d1b14SEd Maste 	/*
226237d1b14SEd Maste 	 * Compute mask and shift value for isolating cluster relative byte
227237d1b14SEd Maste 	 * offsets and cluster numbers from a file offset.
228237d1b14SEd Maste 	 */
229237d1b14SEd Maste 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
230237d1b14SEd Maste 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
231237d1b14SEd Maste 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
232237d1b14SEd Maste 
23398dc8da5SEd Maste 	MSDOSFS_DPRINTF(("%s(fatmask=%lu, fatmult=%u, fatdiv=%u, "
23498dc8da5SEd Maste 	    "fatblocksize=%lu, fatblocksec=%lu, bnshift=%lu, pbcluster=%lu, "
23598dc8da5SEd Maste 	    "crbomask=%lu, cnshift=%lu)\n",
23698dc8da5SEd Maste 	    __func__, (unsigned long)pmp->pm_fatmask, pmp->pm_fatmult,
23798dc8da5SEd Maste 	    pmp->pm_fatdiv, pmp->pm_fatblocksize, pmp->pm_fatblocksec,
23898dc8da5SEd Maste 	    pmp->pm_bnshift, pmp->pm_bpcluster, pmp->pm_crbomask,
23998dc8da5SEd Maste 	    pmp->pm_cnshift));
240237d1b14SEd Maste 	/*
241237d1b14SEd Maste 	 * Check for valid cluster size
242237d1b14SEd Maste 	 * must be a power of 2
243237d1b14SEd Maste 	 */
244237d1b14SEd Maste 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
24598dc8da5SEd Maste 		MSDOSFS_DPRINTF(("bpcluster %lu cnshift %lu\n",
246237d1b14SEd Maste 		    pmp->pm_bpcluster, pmp->pm_cnshift));
247237d1b14SEd Maste 		error = EINVAL;
248237d1b14SEd Maste 		goto error_exit;
249237d1b14SEd Maste 	}
250237d1b14SEd Maste 
251237d1b14SEd Maste 	/*
252237d1b14SEd Maste 	 * Release the bootsector buffer.
253237d1b14SEd Maste 	 */
2545b292f9aSEd Maste 	brelse(bp);
255237d1b14SEd Maste 	bp = NULL;
256237d1b14SEd Maste 
257237d1b14SEd Maste 	/*
258237d1b14SEd Maste 	 * Check FSInfo.
259237d1b14SEd Maste 	 */
260237d1b14SEd Maste 	if (pmp->pm_fsinfo) {
261237d1b14SEd Maste 		struct fsinfo *fp;
262237d1b14SEd Maste 
263237d1b14SEd Maste 		/*
264237d1b14SEd Maste 		 * XXX	If the fsinfo block is stored on media with
265237d1b14SEd Maste 		 *	2KB or larger sectors, is the fsinfo structure
266237d1b14SEd Maste 		 *	padded at the end or in the middle?
267237d1b14SEd Maste 		 */
268d485c77fSKonstantin Belousov 		if ((error = bread((void *)devvp, pmp->pm_fsinfo,
269d485c77fSKonstantin Belousov 		    pmp->pm_BytesPerSec, 0, &bp)) != 0)
270237d1b14SEd Maste 			goto error_exit;
271237d1b14SEd Maste 		fp = (struct fsinfo *)bp->b_data;
272237d1b14SEd Maste 		if (!memcmp(fp->fsisig1, "RRaA", 4)
273237d1b14SEd Maste 		    && !memcmp(fp->fsisig2, "rrAa", 4)
27498dc8da5SEd Maste 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4))
275237d1b14SEd Maste 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
276237d1b14SEd Maste 		else
277237d1b14SEd Maste 			pmp->pm_fsinfo = 0;
2785b292f9aSEd Maste 		brelse(bp);
279237d1b14SEd Maste 		bp = NULL;
280237d1b14SEd Maste 	}
281237d1b14SEd Maste 
282237d1b14SEd Maste 	/*
283237d1b14SEd Maste 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
284237d1b14SEd Maste 	 * XXX
285237d1b14SEd Maste 	 */
286237d1b14SEd Maste 	if (pmp->pm_fsinfo) {
287237d1b14SEd Maste 		if ((pmp->pm_nxtfree == 0xffffffffUL) ||
288237d1b14SEd Maste 		    (pmp->pm_nxtfree > pmp->pm_maxcluster))
289237d1b14SEd Maste 			pmp->pm_fsinfo = 0;
290237d1b14SEd Maste 	}
291237d1b14SEd Maste 
292237d1b14SEd Maste 	/*
293237d1b14SEd Maste 	 * Allocate memory for the bitmap of allocated clusters, and then
294237d1b14SEd Maste 	 * fill it in.
295237d1b14SEd Maste 	 */
296237d1b14SEd Maste 	pmp->pm_inusemap = ecalloc(sizeof(*pmp->pm_inusemap),
297237d1b14SEd Maste 	    ((pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS));
298237d1b14SEd Maste 	/*
299237d1b14SEd Maste 	 * fillinusemap() needs pm_devvp.
300237d1b14SEd Maste 	 */
301237d1b14SEd Maste 	pmp->pm_dev = 0;
302d485c77fSKonstantin Belousov 	pmp->pm_devvp = (void *)devvp;
303237d1b14SEd Maste 
304237d1b14SEd Maste 	/*
305237d1b14SEd Maste 	 * Have the inuse map filled in.
306237d1b14SEd Maste 	 */
307237d1b14SEd Maste 	if ((error = fillinusemap(pmp)) != 0) {
30898dc8da5SEd Maste 		MSDOSFS_DPRINTF(("fillinusemap %d\n", error));
309237d1b14SEd Maste 		goto error_exit;
310237d1b14SEd Maste 	}
311237d1b14SEd Maste 
312237d1b14SEd Maste 	/*
313237d1b14SEd Maste 	 * Finish up.
314237d1b14SEd Maste 	 */
315237d1b14SEd Maste 	if (ronly)
316237d1b14SEd Maste 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
317237d1b14SEd Maste 	else
318237d1b14SEd Maste 		pmp->pm_fmod = 1;
319237d1b14SEd Maste 
320237d1b14SEd Maste 	/*
321237d1b14SEd Maste 	 * If we ever do quotas for DOS filesystems this would be a place
322237d1b14SEd Maste 	 * to fill in the info in the msdosfsmount structure. You dolt,
323237d1b14SEd Maste 	 * quotas on dos filesystems make no sense because files have no
324237d1b14SEd Maste 	 * owners on dos filesystems. of course there is some empty space
325237d1b14SEd Maste 	 * in the directory entry where we could put uid's and gid's.
326237d1b14SEd Maste 	 */
327237d1b14SEd Maste 
328237d1b14SEd Maste 	return pmp;
329237d1b14SEd Maste 
330237d1b14SEd Maste error_exit:
331237d1b14SEd Maste 	if (bp)
33298dc8da5SEd Maste 		brelse(bp);
333237d1b14SEd Maste 	if (pmp) {
334237d1b14SEd Maste 		if (pmp->pm_inusemap)
335237d1b14SEd Maste 			free(pmp->pm_inusemap);
336237d1b14SEd Maste 		free(pmp);
337237d1b14SEd Maste 	}
338237d1b14SEd Maste 	errno = error;
339237d1b14SEd Maste 	return NULL;
340237d1b14SEd Maste }
341237d1b14SEd Maste 
342237d1b14SEd Maste int
msdosfs_root(struct msdosfsmount * pmp,struct m_vnode * vp)343d485c77fSKonstantin Belousov msdosfs_root(struct msdosfsmount *pmp, struct m_vnode *vp) {
344237d1b14SEd Maste 	struct denode *ndep;
345237d1b14SEd Maste 	int error;
346237d1b14SEd Maste 
347d485c77fSKonstantin Belousov 	*vp = *(struct m_vnode *)pmp->pm_devvp;
348ae7e8a02SKonstantin Belousov 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, 0, &ndep)) != 0) {
349237d1b14SEd Maste 		errno = error;
350237d1b14SEd Maste 		return -1;
351237d1b14SEd Maste 	}
352237d1b14SEd Maste 	vp->v_data = ndep;
353237d1b14SEd Maste 	return 0;
354237d1b14SEd Maste }
3558651679aSXin LI 
3568651679aSXin LI /*
3578651679aSXin LI  * If we have an FSInfo block, update it.
3588651679aSXin LI  */
3598651679aSXin LI int
msdosfs_fsiflush(struct msdosfsmount * pmp)3608651679aSXin LI msdosfs_fsiflush(struct msdosfsmount *pmp)
3618651679aSXin LI {
3628651679aSXin LI 	struct fsinfo *fp;
363d485c77fSKonstantin Belousov 	struct m_buf *bp;
3648651679aSXin LI 	int error;
3658651679aSXin LI 
3668651679aSXin LI 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
3678651679aSXin LI 		error = 0;
3688651679aSXin LI 		goto out;
3698651679aSXin LI 	}
370d485c77fSKonstantin Belousov 	error = bread((void *)pmp->pm_devvp, pmp->pm_fsinfo,
371d485c77fSKonstantin Belousov 	    pmp->pm_BytesPerSec, NOCRED, &bp);
3728651679aSXin LI 	if (error != 0) {
3738651679aSXin LI 		brelse(bp);
3748651679aSXin LI 		goto out;
3758651679aSXin LI 	}
3768651679aSXin LI 	fp = (struct fsinfo *)bp->b_data;
3778651679aSXin LI 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
3788651679aSXin LI 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
3798651679aSXin LI 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
3808651679aSXin LI 	error = bwrite(bp);
3818651679aSXin LI 
3828651679aSXin LI out:
3838651679aSXin LI 	return (error);
3848651679aSXin LI }
385