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