xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
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_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
112 	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
113 		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
114 		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
115 		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
116 	}
117 	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
118 		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
119 		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
120 	}
121 
122 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
123 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
124 	else if (!(pmp->pm_flags &
125 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
126 		struct vnode *rootvp;
127 
128 		/*
129 		 * Try to divine whether to support Win'95 long filenames
130 		 */
131 		if (FAT32(pmp))
132 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
133 		else {
134 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
135 				return error;
136 			pmp->pm_flags |= findwin95(VTODE(rootvp))
137 				? MSDOSFSMNT_LONGNAME
138 					: MSDOSFSMNT_SHORTNAME;
139 			vput(rootvp);
140 		}
141 	}
142 	return 0;
143 }
144 
145 /*
146  * mp - path - addr in user space of mount point (ie /usr or whatever)
147  * data - addr in user space of mount params including the name of the block
148  * special file to treat as a filesystem.
149  */
150 static int
151 msdosfs_mount(mp, path, data, ndp, td)
152 	struct mount *mp;
153 	char *path;
154 	caddr_t data;
155 	struct nameidata *ndp;
156 	struct thread *td;
157 {
158 	struct vnode *devvp;	  /* vnode for blk device to mount */
159 	struct msdosfs_args args; /* will hold data from mount request */
160 	/* msdosfs specific mount control block */
161 	struct msdosfsmount *pmp = NULL;
162 	size_t size;
163 	int error, flags;
164 	mode_t accessmode;
165 
166 	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
167 	if (error)
168 		return (error);
169 	if (args.magic != MSDOSFS_ARGSMAGIC)
170 		args.flags = 0;
171 	/*
172 	 * If updating, check whether changing from read-only to
173 	 * read/write; if there is no device name, that's all we do.
174 	 */
175 	if (mp->mnt_flag & MNT_UPDATE) {
176 		pmp = VFSTOMSDOSFS(mp);
177 		error = 0;
178 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
179 			flags = WRITECLOSE;
180 			if (mp->mnt_flag & MNT_FORCE)
181 				flags |= FORCECLOSE;
182 			error = vflush(mp, 0, flags);
183 		}
184 		if (!error && (mp->mnt_flag & MNT_RELOAD))
185 			/* not yet implemented */
186 			error = EOPNOTSUPP;
187 		if (error)
188 			return (error);
189 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (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 			if (suser(td)) {
195 				devvp = pmp->pm_devvp;
196 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
197 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
198 						   td->td_ucred, td);
199 				if (error) {
200 					VOP_UNLOCK(devvp, 0, td);
201 					return (error);
202 				}
203 				VOP_UNLOCK(devvp, 0, td);
204 			}
205 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
206 		}
207 		if (args.fspec == 0) {
208 #ifdef	__notyet__	/* doesn't work correctly with current mountd	XXX */
209 			if (args.flags & MSDOSFSMNT_MNTOPT) {
210 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
211 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
212 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
213 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
214 			}
215 #endif
216 			/*
217 			 * Process export requests.
218 			 */
219 			return (vfs_export(mp, &args.export));
220 		}
221 	}
222 	/*
223 	 * Not an update, or updating the name: look up the name
224 	 * and verify that it refers to a sensible block device.
225 	 */
226 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
227 	error = namei(ndp);
228 	if (error)
229 		return (error);
230 	devvp = ndp->ni_vp;
231 	NDFREE(ndp, NDF_ONLY_PNBUF);
232 
233 	if (!vn_isdisk(devvp, &error)) {
234 		vrele(devvp);
235 		return (error);
236 	}
237 	/*
238 	 * If mount by non-root, then verify that user has necessary
239 	 * permissions on the device.
240 	 */
241 	if (suser(td)) {
242 		accessmode = VREAD;
243 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
244 			accessmode |= VWRITE;
245 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
246 		error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
247 		if (error) {
248 			vput(devvp);
249 			return (error);
250 		}
251 		VOP_UNLOCK(devvp, 0, td);
252 	}
253 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
254 		error = mountmsdosfs(devvp, mp, td, &args);
255 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
256 		pmp = VFSTOMSDOSFS(mp);
257 #endif
258 	} else {
259 		if (devvp != pmp->pm_devvp)
260 			error = EINVAL;	/* XXX needs translation */
261 		else
262 			vrele(devvp);
263 	}
264 	if (error) {
265 		vrele(devvp);
266 		return (error);
267 	}
268 
269 	error = update_mp(mp, &args);
270 	if (error) {
271 		msdosfs_unmount(mp, MNT_FORCE, td);
272 		return error;
273 	}
274 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
275 	    &size);
276 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
277 	(void) msdosfs_statfs(mp, &mp->mnt_stat, td);
278 #ifdef MSDOSFS_DEBUG
279 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
280 #endif
281 	return (0);
282 }
283 
284 static int
285 mountmsdosfs(devvp, mp, td, argp)
286 	struct vnode *devvp;
287 	struct mount *mp;
288 	struct thread *td;
289 	struct msdosfs_args *argp;
290 {
291 	struct msdosfsmount *pmp;
292 	struct buf *bp;
293 	dev_t dev = devvp->v_rdev;
294 	union bootsector *bsp;
295 	struct byte_bpb33 *b33;
296 	struct byte_bpb50 *b50;
297 	struct byte_bpb710 *b710;
298 	u_int8_t SecPerClust;
299 	u_long clusters;
300 	int	ronly, error;
301 
302 	/*
303 	 * Disallow multiple mounts of the same device.
304 	 * Disallow mounting of a device that is currently in use
305 	 * (except for root, which might share swap device for miniroot).
306 	 * Flush out any old buffers remaining from a previous use.
307 	 */
308 	error = vfs_mountedon(devvp);
309 	if (error)
310 		return (error);
311 	if (vcount(devvp) > 1 && devvp != rootvp)
312 		return (EBUSY);
313 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
314 	error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
315 	VOP_UNLOCK(devvp, 0, td);
316 	if (error)
317 		return (error);
318 
319 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
320 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
321 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
322 	VOP_UNLOCK(devvp, 0, td);
323 	if (error)
324 		return (error);
325 
326 	bp  = NULL; /* both used in error_exit */
327 	pmp = NULL;
328 
329 	/*
330 	 * Read the boot sector of the filesystem, and then check the
331 	 * boot signature.  If not a dos boot sector then error out.
332 	 *
333 	 * NOTE: 2048 is a maximum sector size in current...
334 	 */
335 	error = bread(devvp, 0, 2048, NOCRED, &bp);
336 	if (error)
337 		goto error_exit;
338 	bp->b_flags |= B_AGE;
339 	bsp = (union bootsector *)bp->b_data;
340 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
341 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
342 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
343 
344 #ifndef MSDOSFS_NOCHECKSIG
345 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
346 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
347 		error = EINVAL;
348 		goto error_exit;
349 	}
350 #endif
351 
352 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
353 	pmp->pm_mountp = mp;
354 
355 	/*
356 	 * Compute several useful quantities from the bpb in the
357 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
358 	 * the fields that are different between dos 5 and dos 3.3.
359 	 */
360 	SecPerClust = b50->bpbSecPerClust;
361 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
362 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
363 	pmp->pm_FATs = b50->bpbFATs;
364 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
365 	pmp->pm_Sectors = getushort(b50->bpbSectors);
366 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
367 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
368 	pmp->pm_Heads = getushort(b50->bpbHeads);
369 	pmp->pm_Media = b50->bpbMedia;
370 
371 	/* calculate the ratio of sector size to DEV_BSIZE */
372 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
373 
374 	/* XXX - We should probably check more values here */
375 	if (!pmp->pm_BytesPerSec || !SecPerClust
376 		|| !pmp->pm_Heads || pmp->pm_Heads > 255
377 #ifdef PC98
378     		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
379 #else
380 		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
381 #endif
382 		error = EINVAL;
383 		goto error_exit;
384 	}
385 
386 	if (pmp->pm_Sectors == 0) {
387 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
388 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
389 	} else {
390 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
391 		pmp->pm_HugeSectors = pmp->pm_Sectors;
392 	}
393 	if (pmp->pm_HugeSectors > 0xffffffff /
394 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
395 		/*
396 		 * We cannot deal currently with this size of disk
397 		 * due to fileid limitations (see msdosfs_getattr and
398 		 * msdosfs_readdir)
399 		 */
400 		error = EINVAL;
401 		printf("mountmsdosfs(): disk too big, sorry\n");
402 		goto error_exit;
403 	}
404 
405 	if (pmp->pm_RootDirEnts == 0) {
406 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
407 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
408 		    || pmp->pm_Sectors
409 		    || pmp->pm_FATsecs
410 		    || getushort(b710->bpbFSVers)) {
411 			error = EINVAL;
412 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
413 			goto error_exit;
414 		}
415 		pmp->pm_fatmask = FAT32_MASK;
416 		pmp->pm_fatmult = 4;
417 		pmp->pm_fatdiv = 1;
418 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
419 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
420 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
421 		else
422 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
423 	} else
424 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
425 
426 	/*
427 	 * Check a few values (could do some more):
428 	 * - logical sector size: power of 2, >= block size
429 	 * - sectors per cluster: power of 2, >= 1
430 	 * - number of sectors:   >= 1, <= size of partition
431 	 */
432 	if ( (SecPerClust == 0)
433 	  || (SecPerClust & (SecPerClust - 1))
434 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
435 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
436 	  || (pmp->pm_HugeSectors == 0)
437 	) {
438 		error = EINVAL;
439 		goto error_exit;
440 	}
441 
442 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
443 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
444 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
445 	SecPerClust         *= pmp->pm_BlkPerSec;
446 
447 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
448 
449 	if (FAT32(pmp)) {
450 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
451 		pmp->pm_firstcluster = pmp->pm_fatblk
452 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
453 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
454 	} else {
455 		pmp->pm_rootdirblk = pmp->pm_fatblk +
456 			(pmp->pm_FATs * pmp->pm_FATsecs);
457 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
458 				       + DEV_BSIZE - 1)
459 			/ DEV_BSIZE; /* in blocks */
460 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
461 	}
462 
463 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
464 	    SecPerClust + 1;
465 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
466 
467 	if (pmp->pm_fatmask == 0) {
468 		if (pmp->pm_maxcluster
469 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
470 			/*
471 			 * This will usually be a floppy disk. This size makes
472 			 * sure that one fat entry will not be split across
473 			 * multiple blocks.
474 			 */
475 			pmp->pm_fatmask = FAT12_MASK;
476 			pmp->pm_fatmult = 3;
477 			pmp->pm_fatdiv = 2;
478 		} else {
479 			pmp->pm_fatmask = FAT16_MASK;
480 			pmp->pm_fatmult = 2;
481 			pmp->pm_fatdiv = 1;
482 		}
483 	}
484 
485 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
486 	if (pmp->pm_maxcluster >= clusters) {
487 		printf("Warning: number of clusters (%ld) exceeds FAT "
488 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
489 		pmp->pm_maxcluster = clusters - 1;
490 	}
491 
492 
493 	if (FAT12(pmp))
494 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
495 	else
496 		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
497 
498 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
499 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
500 
501 	/*
502 	 * Compute mask and shift value for isolating cluster relative byte
503 	 * offsets and cluster numbers from a file offset.
504 	 */
505 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
506 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
507 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
508 
509 	/*
510 	 * Check for valid cluster size
511 	 * must be a power of 2
512 	 */
513 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
514 		error = EINVAL;
515 		goto error_exit;
516 	}
517 
518 	/*
519 	 * Release the bootsector buffer.
520 	 */
521 	brelse(bp);
522 	bp = NULL;
523 
524 	/*
525 	 * Check FSInfo.
526 	 */
527 	if (pmp->pm_fsinfo) {
528 		struct fsinfo *fp;
529 
530 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
531 		    NOCRED, &bp)) != 0)
532 			goto error_exit;
533 		fp = (struct fsinfo *)bp->b_data;
534 		if (!bcmp(fp->fsisig1, "RRaA", 4)
535 		    && !bcmp(fp->fsisig2, "rrAa", 4)
536 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
537 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
538 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
539 		else
540 			pmp->pm_fsinfo = 0;
541 		brelse(bp);
542 		bp = NULL;
543 	}
544 
545 	/*
546 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
547 	 */
548 
549 	/*
550 	 * Allocate memory for the bitmap of allocated clusters, and then
551 	 * fill it in.
552 	 */
553 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
554 				   / N_INUSEBITS)
555 				  * sizeof(*pmp->pm_inusemap),
556 				  M_MSDOSFSFAT, M_WAITOK);
557 
558 	/*
559 	 * fillinusemap() needs pm_devvp.
560 	 */
561 	pmp->pm_dev = dev;
562 	pmp->pm_devvp = devvp;
563 
564 	/*
565 	 * Have the inuse map filled in.
566 	 */
567 	if ((error = fillinusemap(pmp)) != 0)
568 		goto error_exit;
569 
570 	/*
571 	 * If they want fat updates to be synchronous then let them suffer
572 	 * the performance degradation in exchange for the on disk copy of
573 	 * the fat being correct just about all the time.  I suppose this
574 	 * would be a good thing to turn on if the kernel is still flakey.
575 	 */
576 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
577 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
578 
579 	/*
580 	 * Finish up.
581 	 */
582 	if (ronly)
583 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
584 	else
585 		pmp->pm_fmod = 1;
586 	mp->mnt_data = (qaddr_t) pmp;
587 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
588 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
589 	mp->mnt_flag |= MNT_LOCAL;
590 	devvp->v_rdev->si_mountpoint = mp;
591 
592 	return 0;
593 
594 error_exit:
595 	if (bp)
596 		brelse(bp);
597 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td);
598 	if (pmp) {
599 		if (pmp->pm_inusemap)
600 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
601 		free(pmp, M_MSDOSFSMNT);
602 		mp->mnt_data = (qaddr_t)0;
603 	}
604 	return (error);
605 }
606 
607 /*
608  * Unmount the filesystem described by mp.
609  */
610 static int
611 msdosfs_unmount(mp, mntflags, td)
612 	struct mount *mp;
613 	int mntflags;
614 	struct thread *td;
615 {
616 	struct msdosfsmount *pmp;
617 	int error, flags;
618 
619 	flags = 0;
620 	if (mntflags & MNT_FORCE)
621 		flags |= FORCECLOSE;
622 	error = vflush(mp, 0, flags);
623 	if (error)
624 		return error;
625 	pmp = VFSTOMSDOSFS(mp);
626 	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
627 #ifdef MSDOSFS_DEBUG
628 	{
629 		struct vnode *vp = pmp->pm_devvp;
630 
631 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
632 		printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
633 		    vp->vi_flag, vp->v_usecount, vp->v_writecount,
634 		    vp->v_holdcnt);
635 		printf("id %lu, mount %p, op %p\n",
636 		    vp->v_id, vp->v_mount, vp->v_op);
637 		printf("freef %p, freeb %p, mount %p\n",
638 		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
639 		    vp->v_mount);
640 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
641 		    TAILQ_FIRST(&vp->v_cleanblkhd),
642 		    TAILQ_FIRST(&vp->v_dirtyblkhd),
643 		    vp->v_numoutput, vp->v_type);
644 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
645 		    vp->v_socket, vp->v_tag,
646 		    ((u_int *)vp->v_data)[0],
647 		    ((u_int *)vp->v_data)[1]);
648 	}
649 #endif
650 	error = VOP_CLOSE(pmp->pm_devvp,
651 		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
652 		    NOCRED, td);
653 	vrele(pmp->pm_devvp);
654 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
655 	free(pmp, M_MSDOSFSMNT);
656 	mp->mnt_data = (qaddr_t)0;
657 	mp->mnt_flag &= ~MNT_LOCAL;
658 	return (error);
659 }
660 
661 static int
662 msdosfs_root(mp, vpp)
663 	struct mount *mp;
664 	struct vnode **vpp;
665 {
666 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
667 	struct denode *ndep;
668 	int error;
669 
670 #ifdef MSDOSFS_DEBUG
671 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
672 #endif
673 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
674 	if (error)
675 		return (error);
676 	*vpp = DETOV(ndep);
677 	return (0);
678 }
679 
680 static int
681 msdosfs_statfs(mp, sbp, td)
682 	struct mount *mp;
683 	struct statfs *sbp;
684 	struct thread *td;
685 {
686 	struct msdosfsmount *pmp;
687 
688 	pmp = VFSTOMSDOSFS(mp);
689 	sbp->f_bsize = pmp->pm_bpcluster;
690 	sbp->f_iosize = pmp->pm_bpcluster;
691 	sbp->f_blocks = pmp->pm_maxcluster + 1;
692 	sbp->f_bfree = pmp->pm_freeclustercount;
693 	sbp->f_bavail = pmp->pm_freeclustercount;
694 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
695 	sbp->f_ffree = 0;	/* what to put in here? */
696 	if (sbp != &mp->mnt_stat) {
697 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
698 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
699 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
700 	}
701 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
702 	return (0);
703 }
704 
705 static int
706 msdosfs_sync(mp, waitfor, cred, td)
707 	struct mount *mp;
708 	int waitfor;
709 	struct ucred *cred;
710 	struct thread *td;
711 {
712 	struct vnode *vp, *nvp;
713 	struct denode *dep;
714 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
715 	int error, allerror = 0;
716 
717 	/*
718 	 * If we ever switch to not updating all of the fats all the time,
719 	 * this would be the place to update them from the first one.
720 	 */
721 	if (pmp->pm_fmod != 0) {
722 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
723 			panic("msdosfs_sync: rofs mod");
724 		else {
725 			/* update fats here */
726 		}
727 	}
728 	/*
729 	 * Write back each (modified) denode.
730 	 */
731 	mtx_lock(&mntvnode_mtx);
732 loop:
733 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
734 		/*
735 		 * If the vnode that we are about to sync is no longer
736 		 * associated with this mount point, start over.
737 		 */
738 		if (vp->v_mount != mp)
739 			goto loop;
740 		nvp = TAILQ_NEXT(vp, v_nmntvnodes);
741 
742 		mtx_unlock(&mntvnode_mtx);
743 		mtx_lock(&vp->v_interlock);
744 		dep = VTODE(vp);
745 		if (vp->v_type == VNON ||
746 		    ((dep->de_flag &
747 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
748 		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
749 			mtx_unlock(&vp->v_interlock);
750 			mtx_lock(&mntvnode_mtx);
751 			continue;
752 		}
753 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
754 		if (error) {
755 			mtx_lock(&mntvnode_mtx);
756 			if (error == ENOENT)
757 				goto loop;
758 			continue;
759 		}
760 		error = VOP_FSYNC(vp, cred, waitfor, td);
761 		if (error)
762 			allerror = error;
763 		VOP_UNLOCK(vp, 0, td);
764 		vrele(vp);
765 		mtx_lock(&mntvnode_mtx);
766 	}
767 	mtx_unlock(&mntvnode_mtx);
768 
769 	/*
770 	 * Flush filesystem control info.
771 	 */
772 	if (waitfor != MNT_LAZY) {
773 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
774 		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
775 		if (error)
776 			allerror = error;
777 		VOP_UNLOCK(pmp->pm_devvp, 0, td);
778 	}
779 	return (allerror);
780 }
781 
782 static int
783 msdosfs_fhtovp(mp, fhp, vpp)
784 	struct mount *mp;
785 	struct fid *fhp;
786 	struct vnode **vpp;
787 {
788 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
789 	struct defid *defhp = (struct defid *) fhp;
790 	struct denode *dep;
791 	int error;
792 
793 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
794 	if (error) {
795 		*vpp = NULLVP;
796 		return (error);
797 	}
798 	*vpp = DETOV(dep);
799 	return (0);
800 }
801 
802 static int
803 msdosfs_vptofh(vp, fhp)
804 	struct vnode *vp;
805 	struct fid *fhp;
806 {
807 	struct denode *dep;
808 	struct defid *defhp;
809 
810 	dep = VTODE(vp);
811 	defhp = (struct defid *)fhp;
812 	defhp->defid_len = sizeof(struct defid);
813 	defhp->defid_dirclust = dep->de_dirclust;
814 	defhp->defid_dirofs = dep->de_diroffset;
815 	/* defhp->defid_gen = dep->de_gen; */
816 	return (0);
817 }
818 
819 static struct vfsops msdosfs_vfsops = {
820 	msdosfs_mount,
821 	vfs_stdstart,
822 	msdosfs_unmount,
823 	msdosfs_root,
824 	vfs_stdquotactl,
825 	msdosfs_statfs,
826 	msdosfs_sync,
827 	vfs_stdvget,
828 	msdosfs_fhtovp,
829 	vfs_stdcheckexp,
830 	msdosfs_vptofh,
831 	msdosfs_init,
832 	msdosfs_uninit,
833 	vfs_stdextattrctl,
834 };
835 
836 VFS_SET(msdosfs_vfsops, msdosfs, 0);
837