xref: /dflybsd-src/sys/vfs/msdosfs/msdosfs_vfsops.c (revision 806a5ed72ca2a93a6cdaf25f6582d9e553e4e1e7)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/conf.h>
56 #include <sys/proc.h>
57 #include <sys/nlookup.h>
58 #include <sys/kernel.h>
59 #include <sys/vnode.h>
60 #include <sys/iconv.h>
61 #include <sys/mount.h>
62 #include <sys/buf.h>
63 #include <sys/fcntl.h>
64 #include <sys/malloc.h>
65 #include <sys/stat.h>
66 #include <vm/vm_zone.h>
67 
68 #include <sys/buf2.h>
69 
70 #include <vfs/msdosfs/bootsect.h>
71 #include <vfs/msdosfs/bpb.h>
72 #include <vfs/msdosfs/direntry.h>
73 #include <vfs/msdosfs/denode.h>
74 #include <vfs/msdosfs/fat.h>
75 #include <vfs/msdosfs/msdosfsmount.h>
76 
77 extern struct vop_ops msdosfs_vnode_vops;
78 struct iconv_functions *msdosfs_iconv;
79 
80 #define ENCODING_UNICODE        "UTF-16BE"
81 #if 1 /*def PC98*/
82 /*
83  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
84  *       garbage or a random value :-{
85  *       If you want to use that broken-signatured media, define the
86  *       following symbol even though PC/AT.
87  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
88  */
89 #define	MSDOSFS_NOCHECKSIG
90 #endif
91 
92 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
93 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
94 
95 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
96     struct msdosfs_args *argp);
97 static int msdosfs_root(struct mount *, struct vnode **);
98 static int msdosfs_statfs(struct mount *, struct statfs *, struct ucred *);
99 static int msdosfs_unmount(struct mount *, int);
100 
101 static int
102 update_mp(struct mount *mp, struct msdosfs_args *argp)
103 {
104 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
105 	int error;
106 	char cs_local[ICONV_CSNMAXLEN];
107 	char cs_dos[ICONV_CSNMAXLEN];
108 
109 	pmp->pm_gid = argp->gid;
110 	pmp->pm_uid = argp->uid;
111 	pmp->pm_mask = argp->mask & ALLPERMS;
112 	pmp->pm_dirmask = argp->dirmask & ALLPERMS;
113 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
114 
115 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
116 		memcpy(cs_local, argp->cs_local, sizeof(cs_local));
117 		memcpy(cs_dos, argp->cs_dos, sizeof(cs_dos));
118 		kprintf("local: %s dos: %s\n",argp->cs_local, argp->cs_dos);
119 		error = msdosfs_iconv->open(cs_local, ENCODING_UNICODE,
120 					    &pmp->pm_w2u);
121 		if(error)
122 			return error;
123 		error = msdosfs_iconv->open(ENCODING_UNICODE, cs_local,
124 					    &pmp->pm_u2w);
125 		if(error)
126 			return error;
127 		error = msdosfs_iconv->open(cs_dos, cs_local, &pmp->pm_u2d);
128 		if(error)
129 			return error;
130 		error = msdosfs_iconv->open(cs_local, cs_dos, &pmp->pm_d2u);
131 		if(error)
132 			return error;
133 	}
134 
135 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
136 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
137 	else
138 		pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
139 	return 0;
140 }
141 
142 /*
143  * mp - path - addr in user space of mount point (ie /usr or whatever)
144  * data - addr in user space of mount params including the name of the block
145  * special file to treat as a filesystem.
146  */
147 static int
148 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
149 {
150 	struct vnode *devvp;	  /* vnode for blk device to mount */
151 	struct msdosfs_args args; /* will hold data from mount request */
152 	/* msdosfs specific mount control block */
153 	struct msdosfsmount *pmp = NULL;
154 	size_t size;
155 	int error, flags;
156 	mode_t accessmode;
157 	struct nlookupdata nd;
158 
159 	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
160 	if (error)
161 		return (error);
162 	/*
163 	 * If updating, check whether changing from read-only to
164 	 * read/write; if there is no device name, that's all we do.
165 	 */
166 	if (mp->mnt_flag & MNT_UPDATE) {
167 		pmp = VFSTOMSDOSFS(mp);
168 		error = 0;
169 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
170 		    (mp->mnt_flag & MNT_RDONLY)) {
171 			flags = WRITECLOSE;
172 			if (mp->mnt_flag & MNT_FORCE)
173 				flags |= FORCECLOSE;
174 			error = vflush(mp, 0, flags);
175 			if (error == 0) {
176 				devvp = pmp->pm_devvp;
177 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
178 				VOP_OPEN(devvp, FREAD, FSCRED, NULL);
179 				VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
180 				vn_unlock(devvp);
181 				pmp->pm_flags |= MSDOSFSMNT_RONLY;
182 			}
183 		}
184 		if (!error && (mp->mnt_flag & MNT_RELOAD))
185 			error = EOPNOTSUPP; /* not yet implemented */
186 		if (error)
187 			return (error);
188 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
189 		    (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
190 			/*
191 			 * If upgrade to read-write by non-root, then verify
192 			 * that user has necessary permissions on the device.
193 			 */
194 			devvp = pmp->pm_devvp;
195 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
196 			if (cred->cr_uid != 0) {
197 				error = VOP_EACCESS(devvp, VREAD | VWRITE,
198 						    cred);
199 				if (error) {
200 					vn_unlock(devvp);
201 					return (error);
202 				}
203 			}
204 			VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
205 			VOP_CLOSE(devvp, FREAD, NULL);
206 			vn_unlock(devvp);
207 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
208 		}
209 		if (args.fspec == NULL) {
210 #ifdef __notyet__	/* doesn't work correctly with current mountd XXX */
211 			if (args.flags & MSDOSFSMNT_MNTOPT) {
212 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
213 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
214 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
215 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
216 			}
217 #endif
218 			/*
219 			 * Process export requests.
220 			 */
221 			return (vfs_export(mp, &pmp->pm_export, &args.export));
222 		}
223 	}
224 	/*
225 	 * Not an update, or updating the name: look up the name
226 	 * and verify that it refers to a sensible disk device.
227 	 */
228 	devvp = NULL;
229 	error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
230 	if (error == 0)
231 		error = nlookup(&nd);
232 	if (error == 0)
233 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
234 	nlookup_done(&nd);
235 	if (error)
236 		return (error);
237 
238 	if (!vn_isdisk(devvp, &error)) {
239 		vrele(devvp);
240 		return (error);
241 	}
242 	/*
243 	 * If mount by non-root, then verify that user has necessary
244 	 * permissions on the device.
245 	 */
246 	if (cred->cr_uid != 0) {
247 		accessmode = VREAD;
248 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 			accessmode |= VWRITE;
250 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
251 		error = VOP_EACCESS(devvp, accessmode, cred);
252 		if (error) {
253 			vput(devvp);
254 			return (error);
255 		}
256 		vn_unlock(devvp);
257 	}
258 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
259 		error = mountmsdosfs(devvp, mp, &args);
260 #ifdef MSDOSFS_DEBUG		/* only needed for the kprintf below */
261 		pmp = VFSTOMSDOSFS(mp);
262 #endif
263 	} else {
264 		if (devvp != pmp->pm_devvp)
265 			error = EINVAL;	/* XXX needs translation */
266 		else
267 			vrele(devvp);
268 	}
269 	if (error) {
270 		vrele(devvp);
271 		return (error);
272 	}
273 
274 	error = update_mp(mp, &args);
275 	if (error) {
276 		msdosfs_unmount(mp, MNT_FORCE);
277 		return error;
278 	}
279 
280 	copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
281 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
282 	msdosfs_statfs(mp, &mp->mnt_stat, cred);
283 #ifdef MSDOSFS_DEBUG
284 	kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n",
285 		mp, pmp, pmp->pm_inusemap);
286 #endif
287 	return (0);
288 }
289 
290 static int
291 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
292 {
293 	struct msdosfsmount *pmp;
294 	struct buf *bp;
295 	cdev_t dev;
296 	union bootsector *bsp;
297 	struct byte_bpb33 *b33;
298 	struct byte_bpb50 *b50;
299 	struct byte_bpb710 *b710;
300 	uint8_t SecPerClust;
301 	u_long clusters;
302 	int ronly, error;
303 
304 	/*
305 	 * Disallow multiple mounts of the same device.
306 	 * Disallow mounting of a device that is currently in use
307 	 * Flush out any old buffers remaining from a previous use.
308 	 */
309 	error = vfs_mountedon(devvp);
310 	if (error)
311 		return (error);
312 	if (vcount(devvp) > 0)
313 		return (EBUSY);
314 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
315 	error = vinvalbuf(devvp, V_SAVE, 0, 0);
316 	vn_unlock(devvp);
317 	if (error)
318 		return (error);
319 
320 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
321 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
322 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
323 	vn_unlock(devvp);
324 	if (error)
325 		return (error);
326 	dev = devvp->v_rdev;
327 	bp  = NULL; /* both used in error_exit */
328 	pmp = NULL;
329 
330 	/*
331 	 * Read the boot sector of the filesystem, and then check the
332 	 * boot signature.  If not a dos boot sector then error out.
333 	 *
334 	 * NOTE: 8192 is a magic size that works for ffs.
335 	 */
336 	error = bread(devvp, 0, 8192, &bp);
337 	if (error)
338 		goto error_exit;
339 	bp->b_flags |= B_AGE;
340 	bsp = (union bootsector *)bp->b_data;
341 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
342 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
343 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
344 
345 #ifndef MSDOSFS_NOCHECKSIG
346 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
347 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
348 		error = EINVAL;
349 		goto error_exit;
350 	}
351 #endif
352 
353 	pmp = kmalloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO);
354 	pmp->pm_mountp = mp;
355 
356 	/*
357 	 * Compute several useful quantities from the bpb in the
358 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
359 	 * the fields that are different between dos 5 and dos 3.3.
360 	 */
361 	SecPerClust = b50->bpbSecPerClust;
362 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
363 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
364 		error = EINVAL;
365 		goto error_exit;
366 	}
367 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
368 	pmp->pm_FATs = b50->bpbFATs;
369 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
370 	pmp->pm_Sectors = getushort(b50->bpbSectors);
371 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
372 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
373 	pmp->pm_Heads = getushort(b50->bpbHeads);
374 	pmp->pm_Media = b50->bpbMedia;
375 
376 	/* calculate the ratio of sector size to DEV_BSIZE */
377 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
378 
379 	/*
380 	 * We don't check pm_Heads nor pm_SecPerTrack, because
381 	 * these may not be set for EFI file systems. We don't
382 	 * use these anyway, so we're unaffected if they are
383 	 * invalid.
384 	 */
385 	if (!pmp->pm_BytesPerSec || !SecPerClust) {
386 		error = EINVAL;
387 		goto error_exit;
388 	}
389 
390 	if (pmp->pm_Sectors == 0) {
391 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
392 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
393 	} else {
394 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
395 		pmp->pm_HugeSectors = pmp->pm_Sectors;
396 	}
397 
398 	if (pmp->pm_RootDirEnts == 0) {
399 		if (pmp->pm_FATsecs
400 		    || getushort(b710->bpbFSVers)) {
401 			error = EINVAL;
402 			kprintf("mountmsdosfs(): bad FAT32 filesystem\n");
403 			goto error_exit;
404 		}
405 		pmp->pm_fatmask = FAT32_MASK;
406 		pmp->pm_fatmult = 4;
407 		pmp->pm_fatdiv = 1;
408 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
409 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
410 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
411 		else
412 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
413 	} else
414 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
415 
416 	/*
417 	 * Check a few values (could do some more):
418 	 * - logical sector size: power of 2, >= block size
419 	 * - sectors per cluster: power of 2, >= 1
420 	 * - number of sectors:   >= 1, <= size of partition
421 	 * - number of FAT sectors: >= 1
422 	 */
423 	if ( (SecPerClust == 0)
424 	  || (SecPerClust & (SecPerClust - 1))
425 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
426 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
427 	  || (pmp->pm_HugeSectors == 0)
428 	  || (pmp->pm_FATsecs == 0)
429 	  || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)) {
430 		error = EINVAL;
431 		goto error_exit;
432 	}
433 
434 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
435 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
436 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
437 	SecPerClust         *= pmp->pm_BlkPerSec;
438 
439 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
440 
441 	if (FAT32(pmp)) {
442 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
443 		pmp->pm_firstcluster = pmp->pm_fatblk
444 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
445 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
446 	} else {
447 		pmp->pm_rootdirblk = pmp->pm_fatblk +
448 			(pmp->pm_FATs * pmp->pm_FATsecs);
449 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
450 			sizeof(struct direntry), DEV_BSIZE); /* in blocks */
451 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
452 	}
453 
454 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
455 	    SecPerClust + 1;
456 
457 	if (pmp->pm_fatmask == 0) {
458 		if (pmp->pm_maxcluster
459 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
460 			/*
461 			 * This will usually be a floppy disk. This size makes
462 			 * sure that one FAT entry will not be split across
463 			 * multiple blocks.
464 			 */
465 			pmp->pm_fatmask = FAT12_MASK;
466 			pmp->pm_fatmult = 3;
467 			pmp->pm_fatdiv = 2;
468 		} else {
469 			pmp->pm_fatmask = FAT16_MASK;
470 			pmp->pm_fatmult = 2;
471 			pmp->pm_fatdiv = 1;
472 		}
473 	}
474 
475 	clusters = ((pmp->pm_FATsecs * DEV_BSIZE) / pmp->pm_fatmult) *
476 		pmp->pm_fatdiv;
477 	if (pmp->pm_maxcluster >= clusters) {
478 		kprintf("Warning: number of clusters (%ld) exceeds FAT "
479 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
480 		pmp->pm_maxcluster = clusters - 1;
481 	}
482 
483 	if (FAT12(pmp))
484 		pmp->pm_fatblocksize = 3 * 512;
485 	else
486 		pmp->pm_fatblocksize = PAGE_SIZE;
487 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
488 	    pmp->pm_BytesPerSec);
489 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
490 	pmp->pm_bnshift = DEV_BSHIFT;
491 
492 	/*
493 	 * Compute mask and shift value for isolating cluster relative byte
494 	 * offsets and cluster numbers from a file offset.
495 	 */
496 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
497 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
498 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
499 
500 	/*
501 	 * Check for valid cluster size
502 	 * must be a power of 2
503 	 */
504 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
505 		error = EINVAL;
506 		goto error_exit;
507 	}
508 
509 	/*
510 	 * Release the bootsector buffer.
511 	 */
512 	bp->b_flags |= B_RELBUF;
513 	brelse(bp);
514 	bp = NULL;
515 
516 	/*
517 	 * Check the fsinfo sector if we have one.  Silently fix up our
518 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
519 	 * or too large.  Ignore fp->fsinfree for now, since we need to
520 	 * read the entire FAT anyway to fill the inuse map.
521 	 */
522 	if (pmp->pm_fsinfo) {
523 		struct fsinfo *fp;
524 
525 		if ((error = bread(devvp, de_bn2doff(pmp, pmp->pm_fsinfo),
526 		    pmp->pm_BytesPerSec, &bp)) != 0)
527 			goto error_exit;
528 		fp = (struct fsinfo *)bp->b_data;
529 		if (!memcmp(fp->fsisig1, "RRaA", 4) &&
530 		    !memcmp(fp->fsisig2, "rrAa", 4) &&
531 		    !memcmp(fp->fsisig3, "\0\0\125\252", 4)) {
532 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
533 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
534 				pmp->pm_nxtfree = CLUST_FIRST;
535 		} else
536 			pmp->pm_fsinfo = 0;
537 		bp->b_flags |= B_RELBUF;
538 		brelse(bp);
539 		bp = NULL;
540 	}
541 
542 	/*
543 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
544 	 * sectors aren't properly reserved in the FAT).  This completes
545 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
546 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
547 	 * internally even if there is no fsinfo.
548 	 */
549 	if (pmp->pm_nxtfree < CLUST_FIRST)
550 		pmp->pm_nxtfree = CLUST_FIRST;
551 
552 	/*
553 	 * Allocate memory for the bitmap of allocated clusters, and then
554 	 * fill it in.
555 	 */
556 	pmp->pm_inusemap = kmalloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
557 				   * sizeof(*pmp->pm_inusemap),
558 				   M_MSDOSFSFAT, M_WAITOK);
559 
560 	/*
561 	 * fillinusemap() needs pm_devvp.
562 	 */
563 	pmp->pm_devvp = devvp;
564 	pmp->pm_dev = dev;
565 
566 	/*
567 	 * Have the inuse map filled in.
568 	 */
569 	MSDOSFS_LOCK_MP(pmp);
570 	error = fillinusemap(pmp);
571 	MSDOSFS_UNLOCK_MP(pmp);
572 	if (error != 0)
573 		goto error_exit;
574 
575 	/*
576 	 * If they want FAT updates to be synchronous then let them suffer
577 	 * the performance degradation in exchange for the on disk copy of
578 	 * the FAT being correct just about all the time.  I suppose this
579 	 * would be a good thing to turn on if the kernel is still flakey.
580 	 */
581 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
582 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
583 
584 	/*
585 	 * Finish up.
586 	 */
587 	if (ronly)
588 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
589 	else
590 		pmp->pm_fmod = 1;
591 	mp->mnt_data = (qaddr_t) pmp;
592 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
593 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
594 	mp->mnt_flag |= MNT_LOCAL;
595 	vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
596 	dev->si_mountpoint = mp;
597 
598 	return (0);
599 
600 error_exit:
601 	if (bp) {
602 		bp->b_flags |= B_RELBUF;
603 		brelse(bp);
604 	}
605 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
606 	VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
607 	vn_unlock(devvp);
608 	if (pmp) {
609 		if (pmp->pm_inusemap)
610 			kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
611 		kfree(pmp, M_MSDOSFSMNT);
612 		mp->mnt_data = (qaddr_t)0;
613 	}
614 	return (error);
615 }
616 
617 /*
618  * Unmount the filesystem described by mp.
619  */
620 static int
621 msdosfs_unmount(struct mount *mp, int mntflags)
622 {
623 	struct msdosfsmount *pmp;
624 	int error, flags;
625 #ifdef MSDOSFS_DEBUG
626 	struct vnode *vp;
627 #endif
628 	flags = 0;
629 	if (mntflags & MNT_FORCE)
630 		flags |= FORCECLOSE;
631 	error = vflush(mp, 0, flags);
632 	if (error)
633 		return error;
634 
635 	pmp = VFSTOMSDOSFS(mp);
636 	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
637 
638 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
639 		if (pmp->pm_w2u)
640 			msdosfs_iconv->close(pmp->pm_w2u);
641 		if (pmp->pm_u2w)
642 			msdosfs_iconv->close(pmp->pm_u2w);
643 		if (pmp->pm_d2u)
644 			msdosfs_iconv->close(pmp->pm_d2u);
645 		if (pmp->pm_u2d)
646 			msdosfs_iconv->close(pmp->pm_u2d);
647 	}
648 
649 #ifdef MSDOSFS_DEBUG
650 	vp = pmp->pm_devvp;
651 	kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
652 	kprintf("flag %08x, refcnt 0x%08x, writecount %d, auxrefs 0x%08x\n",
653 		vp->v_flag, vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
654 	kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops);
655 	kprintf("mount %p\n", vp->v_mount);
656 	kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
657 		RB_ROOT(&vp->v_rbclean_tree), RB_ROOT(&vp->v_rbdirty_tree),
658 		bio_track_active(&vp->v_track_write), vp->v_type);
659 	kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
660 		vp->v_socket, vp->v_tag, ((u_int *)vp->v_data)[0],
661 		((u_int *)vp->v_data)[1]);
662 #endif
663 
664 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
665 	error = VOP_CLOSE(pmp->pm_devvp,
666 		    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD | FWRITE,
667 		    NULL);
668 	vn_unlock(pmp->pm_devvp);
669 	vrele(pmp->pm_devvp);
670 	kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
671 	kfree(pmp, M_MSDOSFSMNT);
672 	mp->mnt_data = (qaddr_t)0;
673 	mp->mnt_flag &= ~MNT_LOCAL;
674 	return (error);
675 }
676 
677 static int
678 msdosfs_root(struct mount *mp, struct vnode **vpp)
679 {
680 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
681 	struct denode *ndep;
682 	int error;
683 
684 	mprintf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
685 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
686 	if (error)
687 		return (error);
688 	*vpp = DETOV(ndep);
689 	return (0);
690 }
691 
692 static int
693 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
694 {
695 	struct msdosfsmount *pmp;
696 
697 	pmp = VFSTOMSDOSFS(mp);
698 	sbp->f_bsize = pmp->pm_bpcluster;
699 	sbp->f_iosize = pmp->pm_bpcluster;
700 	sbp->f_blocks = pmp->pm_maxcluster + 1;
701 	sbp->f_bfree = pmp->pm_freeclustercount;
702 	sbp->f_bavail = pmp->pm_freeclustercount;
703 	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
704 	sbp->f_ffree = 0;	/* what to put in here? */
705 	if (sbp != &mp->mnt_stat) {
706 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
707 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
708 	}
709 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
710 	return (0);
711 }
712 
713 struct scaninfo {
714 	int rescan;
715 	int allerror;
716 	int waitfor;
717 };
718 
719 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
720 
721 /*
722  * If we have an FSInfo block, update it.
723  */
724 static int
725 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
726 {
727 	struct fsinfo *fp;
728 	struct buf *bp;
729 	int error;
730 
731 	MSDOSFS_LOCK_MP(pmp);
732 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
733 		error = 0;
734 		goto unlock;
735 	}
736 	error = bread(pmp->pm_devvp, de_bn2doff(pmp, pmp->pm_fsinfo),
737 	    pmp->pm_BytesPerSec, &bp);
738 	if (error != 0) {
739 		brelse(bp);
740 		goto unlock;
741 	}
742 
743 	fp = (struct fsinfo *)bp->b_data;
744 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
745 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
746 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
747 	if (waitfor == MNT_WAIT)
748 		error = bwrite(bp);
749 	else
750 		bawrite(bp);
751 unlock:
752 	MSDOSFS_UNLOCK_MP(pmp);
753 	return (error);
754 }
755 
756 static int
757 msdosfs_sync(struct mount *mp, int waitfor)
758 {
759 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
760 	struct scaninfo scaninfo;
761 	int error;
762 
763 	/*
764 	 * If we ever switch to not updating all of the FATs all the time,
765 	 * this would be the place to update them from the first one.
766 	 */
767 	if (pmp->pm_fmod != 0) {
768 		if (pmp->pm_flags & MSDOSFSMNT_RONLY) {
769 			panic("msdosfs_sync: rofs mod");
770 		} else {
771 			/* update FATs here */
772 		}
773 	}
774 	/*
775 	 * Write back each (modified) denode.
776 	 */
777 	scaninfo.allerror = 0;
778 	scaninfo.rescan = 1;
779 	while (scaninfo.rescan) {
780 		scaninfo.rescan = 0;
781 		vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL,
782 			      msdosfs_sync_scan, &scaninfo);
783 	}
784 
785 	/*
786 	 * Flush filesystem control info.
787 	 */
788 	if ((waitfor & MNT_LAZY) == 0) {
789 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
790 		if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, 0)) != 0)
791 			scaninfo.allerror = error;
792 		vn_unlock(pmp->pm_devvp);
793 	}
794 
795 	error = msdosfs_fsiflush(pmp, waitfor);
796 	if (error != 0)
797 		scaninfo.allerror = error;
798 	return (scaninfo.allerror);
799 }
800 
801 static int
802 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
803 {
804 	struct scaninfo *info = data;
805 	struct denode *dep;
806 	int error;
807 
808 	dep = VTODE(vp);
809 	if (vp->v_type == VNON || vp->v_type == VBAD ||
810 	    ((dep->de_flag &
811 	    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
812 	    (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
813 		return(0);
814 	}
815 	if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
816 		info->allerror = error;
817 	return(0);
818 }
819 
820 static int
821 msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp,
822 	       struct fid *fhp, struct vnode **vpp)
823 {
824 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
825 	struct defid *defhp = (struct defid *) fhp;
826 	struct denode *dep;
827 	int error;
828 
829 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
830 	if (error) {
831 		*vpp = NULLVP;
832 		return (error);
833 	}
834 	*vpp = DETOV(dep);
835 	return (0);
836 }
837 
838 static int
839 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
840 		 struct ucred **credanonp)
841 {
842 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
843 	struct netcred *np;
844 
845 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
846 	if (np == NULL)
847 		return (EACCES);
848 	*exflagsp = np->netc_exflags;
849 	*credanonp = &np->netc_anon;
850 	return (0);
851 }
852 
853 static int
854 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
855 {
856 	struct denode *dep;
857 	struct defid *defhp;
858 
859 	dep = VTODE(vp);
860 	defhp = (struct defid *)fhp;
861 	defhp->defid_len = sizeof(struct defid);
862 	defhp->defid_dirclust = dep->de_dirclust;
863 	defhp->defid_dirofs = dep->de_diroffset;
864 	/* defhp->defid_gen = dep->de_gen; */
865 	return (0);
866 }
867 
868 static struct vfsops msdosfs_vfsops = {
869 	.vfs_mount =		msdosfs_mount,
870 	.vfs_root =		msdosfs_root,
871 	.vfs_statfs =		msdosfs_statfs,
872 	.vfs_sync =		msdosfs_sync,
873 	.vfs_fhtovp =		msdosfs_fhtovp,
874 	.vfs_checkexp =		msdosfs_checkexp,
875 	.vfs_vptofh =		msdosfs_vptofh,
876 	.vfs_init =		msdosfs_init,
877 	.vfs_uninit =		msdosfs_uninit,
878 	.vfs_unmount =		msdosfs_unmount,
879 };
880 
881 VFS_SET(msdosfs_vfsops, msdos, 0);
882 MODULE_VERSION(msdos, 1);
883