xref: /openbsd-src/sys/msdosfs/msdosfs_vfsops.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: msdosfs_vfsops.c,v 1.31 2003/04/14 17:55:07 tedu Exp $	*/
2 /*	$NetBSD: msdosfs_vfsops.c,v 1.48 1997/10/18 02:54:57 briggs 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/namei.h>
54 #include <sys/proc.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/file.h>
61 #include <sys/disklabel.h>
62 #include <sys/ioctl.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 
66 #include <msdosfs/bpb.h>
67 #include <msdosfs/bootsect.h>
68 #include <msdosfs/direntry.h>
69 #include <msdosfs/denode.h>
70 #include <msdosfs/msdosfsmount.h>
71 #include <msdosfs/fat.h>
72 
73 int msdosfs_mount(struct mount *, const char *, void *, struct nameidata *,
74 		       struct proc *);
75 int msdosfs_start(struct mount *, int, struct proc *);
76 int msdosfs_unmount(struct mount *, int, struct proc *);
77 int msdosfs_root(struct mount *, struct vnode **);
78 int msdosfs_statfs(struct mount *, struct statfs *, struct proc *);
79 int msdosfs_sync(struct mount *, int, struct ucred *, struct proc *);
80 int msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
81 int msdosfs_vptofh(struct vnode *, struct fid *);
82 int msdosfs_check_export(struct mount *mp, struct mbuf *nam,
83 			      int *extflagsp, struct ucred **credanonp);
84 
85 int msdosfs_mountfs(struct vnode *, struct mount *, struct proc *,
86 			 struct msdosfs_args *);
87 
88 int msdosfs_sync_vnode(struct vnode *, void *);
89 
90 /*
91  * mp - path - addr in user space of mount point (ie /usr or whatever)
92  * data - addr in user space of mount params including the name of the block
93  * special file to treat as a filesystem.
94  */
95 int
96 msdosfs_mount(mp, path, data, ndp, p)
97 	struct mount *mp;
98 	const char *path;
99 	void *data;
100 	struct nameidata *ndp;
101 	struct proc *p;
102 {
103 	struct vnode *devvp;	  /* vnode for blk device to mount */
104 	struct msdosfs_args args; /* will hold data from mount request */
105 	/* msdosfs specific mount control block */
106 	struct msdosfsmount *pmp = NULL;
107 	size_t size;
108 	int error, flags;
109 	mode_t accessmode;
110 
111 	error = copyin(data, &args, sizeof(struct msdosfs_args));
112 	if (error)
113 		return (error);
114 	/*
115 	 * If updating, check whether changing from read-only to
116 	 * read/write; if there is no device name, that's all we do.
117 	 */
118 	if (mp->mnt_flag & MNT_UPDATE) {
119 		pmp = VFSTOMSDOSFS(mp);
120 		error = 0;
121 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
122 			flags = WRITECLOSE;
123 			if (mp->mnt_flag & MNT_FORCE)
124 				flags |= FORCECLOSE;
125 			error = vflush(mp, NULLVP, flags);
126 		}
127 		if (!error && (mp->mnt_flag & MNT_RELOAD))
128 			/* not yet implemented */
129 			error = EOPNOTSUPP;
130 		if (error)
131 			return (error);
132 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_WANTRDWR)) {
133 			/*
134 			 * If upgrade to read-write by non-root, then verify
135 			 * that user has necessary permissions on the device.
136 			 */
137 			if (p->p_ucred->cr_uid != 0) {
138 				devvp = pmp->pm_devvp;
139 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
140 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
141 						   p->p_ucred, p);
142 				if (error) {
143 					VOP_UNLOCK(devvp, 0, p);
144 					return (error);
145 				}
146 				VOP_UNLOCK(devvp, 0, p);
147 			}
148 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
149 		}
150 		if (args.fspec == 0) {
151 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
152 			if (args.flags & MSDOSFSMNT_MNTOPT) {
153 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
154 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
155 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
156 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
157 			}
158 #endif
159 			/*
160 			 * Process export requests.
161 			 */
162 			return (vfs_export(mp, &pmp->pm_export,
163 			    &args.export_info));
164 		}
165 	}
166 	/*
167 	 * Not an update, or updating the name: look up the name
168 	 * and verify that it refers to a sensible block device.
169 	 */
170 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
171 	if ((error = namei(ndp)) != 0)
172 		return (error);
173 	devvp = ndp->ni_vp;
174 
175 	if (devvp->v_type != VBLK) {
176 		vrele(devvp);
177 		return (ENOTBLK);
178 	}
179 	if (major(devvp->v_rdev) >= nblkdev) {
180 		vrele(devvp);
181 		return (ENXIO);
182 	}
183 	/*
184 	 * If mount by non-root, then verify that user has necessary
185 	 * permissions on the device.
186 	 */
187 	if (p->p_ucred->cr_uid != 0) {
188 		accessmode = VREAD;
189 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
190 			accessmode |= VWRITE;
191 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
192 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
193 		if (error) {
194 			vput(devvp);
195 			return (error);
196 		}
197 		VOP_UNLOCK(devvp, 0, p);
198 	}
199 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
200 		error = msdosfs_mountfs(devvp, mp, p, &args);
201 	else {
202 		if (devvp != pmp->pm_devvp)
203 			error = EINVAL;	/* XXX needs translation */
204 		else
205 			vrele(devvp);
206 	}
207 	if (error) {
208 		vrele(devvp);
209 		return (error);
210 	}
211 	pmp = VFSTOMSDOSFS(mp);
212 	pmp->pm_gid = args.gid;
213 	pmp->pm_uid = args.uid;
214 	pmp->pm_mask = args.mask;
215 	pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
216 
217 	/*
218 	 * GEMDOS knows nothing (yet) about win95
219 	 */
220 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
221 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
222 
223 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
224 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
225 	else if (!(pmp->pm_flags & (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
226 		struct vnode *rvp;
227 
228 		/*
229 		 * Try to divine whether to support Win'95 long filenames
230 		 */
231 		if (FAT32(pmp))
232 		        pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
233 		else {
234 		        if ((error = msdosfs_root(mp, &rvp)) != 0) {
235 			        msdosfs_unmount(mp, MNT_FORCE, p);
236 			        return (error);
237 			}
238 			pmp->pm_flags |= findwin95(VTODE(rvp))
239 			     ? MSDOSFSMNT_LONGNAME
240 			     : MSDOSFSMNT_SHORTNAME;
241 			vput(rvp);
242 		}
243 	}
244 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
245 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
246 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
247 	    &size);
248 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
249 	bcopy(&args, &mp->mnt_stat.mount_info.msdosfs_args, sizeof(args));
250 #ifdef MSDOSFS_DEBUG
251 	printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp, pmp, pmp->pm_inusemap);
252 #endif
253 	return (0);
254 }
255 
256 int
257 msdosfs_mountfs(devvp, mp, p, argp)
258 	struct vnode *devvp;
259 	struct mount *mp;
260 	struct proc *p;
261 	struct msdosfs_args *argp;
262 {
263 	struct msdosfsmount *pmp;
264 	struct buf *bp;
265 	dev_t dev = devvp->v_rdev;
266 	struct partinfo dpart;
267 	union bootsector *bsp;
268 	struct byte_bpb33 *b33;
269 	struct byte_bpb50 *b50;
270 	struct byte_bpb710 *b710;
271 	extern struct vnode *rootvp;
272 	u_int8_t SecPerClust;
273 	int	ronly, error;
274 	int	bsize = 0, dtype = 0, tmp;
275 	u_long dirsperblk;
276 
277 	/*
278 	 * Disallow multiple mounts of the same device.
279 	 * Disallow mounting of a device that is currently in use
280 	 * (except for root, which might share swap device for miniroot).
281 	 * Flush out any old buffers remaining from a previous use.
282 	 */
283 	if ((error = vfs_mountedon(devvp)) != 0)
284 		return (error);
285 	if (vcount(devvp) > 1 && devvp != rootvp)
286 		return (EBUSY);
287 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
288 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
289 	VOP_UNLOCK(devvp, 0, p);
290 	if (error)
291 		return (error);
292 
293 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
294 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
295 	if (error)
296 		return (error);
297 
298 	bp  = NULL; /* both used in error_exit */
299 	pmp = NULL;
300 
301 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
302 		/*
303 	 	 * We need the disklabel to calculate the size of a FAT entry
304 		 * later on. Also make sure the partition contains a filesystem
305 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
306 		 * to check for them too.
307 	 	 *
308 	 	 * At least some parts of the msdos fs driver seem to assume
309 		 * that the size of a disk block will always be 512 bytes.
310 		 * Let's check it...
311 		 */
312 		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
313 				  FREAD, NOCRED, p);
314 		if (error)
315 			goto error_exit;
316 		tmp   = dpart.part->p_fstype;
317 		dtype = dpart.disklab->d_type;
318 		bsize = dpart.disklab->d_secsize;
319 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
320 			error = EFTYPE;
321 			goto error_exit;
322 		}
323 	}
324 
325 	/*
326 	 * Read the boot sector of the filesystem, and then check the
327 	 * boot signature.  If not a dos boot sector then error out.
328 	 */
329 	if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
330 		goto error_exit;
331 	bp->b_flags |= B_AGE;
332 	bsp = (union bootsector *)bp->b_data;
333 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
334 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
335 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
336 
337 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
338 	bzero((caddr_t)pmp, sizeof *pmp);
339 	pmp->pm_mountp = mp;
340 
341 	/*
342 	 * Compute several useful quantities from the bpb in the
343 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
344 	 * the fields that are different between dos 5 and dos 3.3.
345 	 */
346 	SecPerClust = b50->bpbSecPerClust;
347 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
348 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
349 	pmp->pm_FATs = b50->bpbFATs;
350 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
351 	pmp->pm_Sectors = getushort(b50->bpbSectors);
352 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
353 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
354 	pmp->pm_Heads = getushort(b50->bpbHeads);
355 	pmp->pm_Media = b50->bpbMedia;
356 
357 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
358 		/* XXX - We should probably check more values here */
359     		if (!pmp->pm_BytesPerSec || !SecPerClust
360 	    		|| pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
361 			error = EFTYPE;
362 			goto error_exit;
363 		}
364 	}
365 
366 	if (pmp->pm_Sectors == 0) {
367 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
368 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
369 	} else {
370 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
371 		pmp->pm_HugeSectors = pmp->pm_Sectors;
372 	}
373 
374 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
375 	if (pmp->pm_HugeSectors > 0xffffffff / dirsperblk + 1) {
376 	        /*
377 		 * We cannot deal currently with this size of disk
378 		 * due to fileid limitations (see msdosfs_getattr and
379 		 * msdosfs_readdir)
380 		 */
381 	        error = EINVAL;
382 		goto error_exit;
383 	}
384 
385 	if (pmp->pm_RootDirEnts == 0) {
386 		if (pmp->pm_Sectors || pmp->pm_FATsecs ||
387 		    getushort(b710->bpbFSVers)) {
388 		        error = EINVAL;
389 			goto error_exit;
390 		}
391 		pmp->pm_fatmask = FAT32_MASK;
392 		pmp->pm_fatmult = 4;
393 		pmp->pm_fatdiv = 1;
394 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
395 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
396 		        pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
397 		else
398 		        pmp->pm_flags |= MSDOSFS_FATMIRROR;
399 	} else
400 	        pmp->pm_flags |= MSDOSFS_FATMIRROR;
401 
402 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
403 	        if (FAT32(pmp)) {
404 		        /*
405 			 * GEMDOS doesn't know fat32.
406 			 */
407 		        error = EINVAL;
408 			goto error_exit;
409 		}
410 
411 		/*
412 		 * Check a few values (could do some more):
413 		 * - logical sector size: power of 2, >= block size
414 		 * - sectors per cluster: power of 2, >= 1
415 		 * - number of sectors:   >= 1, <= size of partition
416 		 */
417 		if ( (SecPerClust == 0)
418 		  || (SecPerClust & (SecPerClust - 1))
419 		  || (pmp->pm_BytesPerSec < bsize)
420 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
421 		  || (pmp->pm_HugeSectors == 0)
422 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
423 							> dpart.part->p_size)
424 		   ) {
425 			error = EFTYPE;
426 			goto error_exit;
427 		}
428 		/*
429 		 * XXX - Many parts of the msdos fs driver seem to assume that
430 		 * the number of bytes per logical sector (BytesPerSec) will
431 		 * always be the same as the number of bytes per disk block
432 		 * Let's pretend it is.
433 		 */
434 		tmp = pmp->pm_BytesPerSec / bsize;
435 		pmp->pm_BytesPerSec  = bsize;
436 		pmp->pm_HugeSectors *= tmp;
437 		pmp->pm_HiddenSects *= tmp;
438 		pmp->pm_ResSectors  *= tmp;
439 		pmp->pm_Sectors     *= tmp;
440 		pmp->pm_FATsecs     *= tmp;
441 		SecPerClust         *= tmp;
442 	}
443 	pmp->pm_fatblk = pmp->pm_ResSectors;
444 	if (FAT32(pmp)) {
445 	        pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
446 		pmp->pm_firstcluster = pmp->pm_fatblk
447 		        + (pmp->pm_FATs * pmp->pm_FATsecs);
448 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
449 	} else {
450 	        pmp->pm_rootdirblk = pmp->pm_fatblk +
451 		        (pmp->pm_FATs * pmp->pm_FATsecs);
452 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
453 				       + pmp->pm_BytesPerSec - 1)
454 		        / pmp->pm_BytesPerSec;/* in sectors */
455 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
456 	}
457 
458 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
459 	    SecPerClust;
460 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
461 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
462 
463 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
464 		if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
465 		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
466 		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
467 		     ) {
468 		        pmp->pm_fatmask = FAT12_MASK;
469 			pmp->pm_fatmult = 3;
470 			pmp->pm_fatdiv = 2;
471 		} else {
472 		        pmp->pm_fatmask = FAT16_MASK;
473 			pmp->pm_fatmult = 2;
474 			pmp->pm_fatdiv = 1;
475 		}
476 	} else if (pmp->pm_fatmask == 0) {
477 		if (pmp->pm_maxcluster
478 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
479 			/*
480 			 * This will usually be a floppy disk. This size makes
481 			 * sure that one fat entry will not be split across
482 			 * multiple blocks.
483 			 */
484 			pmp->pm_fatmask = FAT12_MASK;
485 			pmp->pm_fatmult = 3;
486 			pmp->pm_fatdiv = 2;
487 		} else {
488 			pmp->pm_fatmask = FAT16_MASK;
489 			pmp->pm_fatmult = 2;
490 			pmp->pm_fatdiv = 1;
491 		}
492 	}
493 	if (FAT12(pmp))
494 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
495 	else
496 		pmp->pm_fatblocksize = MAXBSIZE;
497 
498 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
499 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 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 * pmp->pm_BytesPerSec;
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 = EFTYPE;
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, 1024, NOCRED, &bp)) != 0)
531 		        goto error_exit;
532 		fp = (struct fsinfo *)bp->b_data;
533 		if (!bcmp(fp->fsisig1, "RRaA", 4)
534 		    && !bcmp(fp->fsisig2, "rrAa", 4)
535 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
536 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
537 		        pmp->pm_nxtfree = getulong(fp->fsinxtfree);
538 		else
539 		        pmp->pm_fsinfo = 0;
540 		brelse(bp);
541 		bp = NULL;
542 	}
543 
544 	/*
545 	 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
546 	 */
547 
548 	/*
549 	 * Allocate memory for the bitmap of allocated clusters, and then
550 	 * fill it in.
551 	 */
552 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
553 				   / N_INUSEBITS)
554 				  * sizeof(*pmp->pm_inusemap),
555 				  M_MSDOSFSFAT, M_WAITOK);
556 
557 	/*
558 	 * fillinusemap() needs pm_devvp.
559 	 */
560 	pmp->pm_dev = dev;
561 	pmp->pm_devvp = devvp;
562 
563 	/*
564 	 * Have the inuse map filled in.
565 	 */
566 	if ((error = fillinusemap(pmp)) != 0)
567 		goto error_exit;
568 
569 	/*
570 	 * If they want fat updates to be synchronous then let them suffer
571 	 * the performance degradation in exchange for the on disk copy of
572 	 * the fat being correct just about all the time.  I suppose this
573 	 * would be a good thing to turn on if the kernel is still flakey.
574 	 */
575 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
576 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
577 
578 	/*
579 	 * Finish up.
580 	 */
581 	if (ronly)
582 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
583 	else
584 		pmp->pm_fmod = 1;
585 	mp->mnt_data = (qaddr_t)pmp;
586         mp->mnt_stat.f_fsid.val[0] = (long)dev;
587         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
588 #ifdef QUOTA
589 	/*
590 	 * If we ever do quotas for DOS filesystems this would be a place
591 	 * to fill in the info in the msdosfsmount structure. You dolt,
592 	 * quotas on dos filesystems make no sense because files have no
593 	 * owners on dos filesystems. of course there is some empty space
594 	 * in the directory entry where we could put uid's and gid's.
595 	 */
596 #endif
597 	devvp->v_specmountpoint = mp;
598 
599 	return (0);
600 
601 error_exit:
602 	devvp->v_specmountpoint = NULL;
603 	if (bp)
604 		brelse(bp);
605 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
606 	if (pmp) {
607 		if (pmp->pm_inusemap)
608 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
609 		free(pmp, M_MSDOSFSMNT);
610 		mp->mnt_data = (qaddr_t)0;
611 	}
612 	return (error);
613 }
614 
615 int
616 msdosfs_start(mp, flags, p)
617 	struct mount *mp;
618 	int flags;
619 	struct proc *p;
620 {
621 
622 	return (0);
623 }
624 
625 /*
626  * Unmount the filesystem described by mp.
627  */
628 int
629 msdosfs_unmount(mp, mntflags, p)
630 	struct mount *mp;
631 	int mntflags;
632 	struct proc *p;
633 {
634 	struct msdosfsmount *pmp;
635 	int error, flags;
636 	struct vnode *vp;
637 
638 	flags = 0;
639 	if (mntflags & MNT_FORCE)
640 		flags |= FORCECLOSE;
641 #ifdef QUOTA
642 #endif
643 	if ((error = vflush(mp, NULLVP, flags)) != 0)
644 		return (error);
645 	pmp = VFSTOMSDOSFS(mp);
646 	pmp->pm_devvp->v_specmountpoint = NULL;
647 	vp = pmp->pm_devvp;
648 #ifdef MSDOSFS_DEBUG
649 	vprint("msdosfs_umount(): just before calling VOP_CLOSE()\n", vp);
650 #endif
651 	error = VOP_CLOSE(vp,
652 	   pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
653 	vrele(vp);
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 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 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
671 		return (error);
672 
673 #ifdef MSDOSFS_DEBUG
674 	printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n",
675 	    mp, pmp, ndep, DETOV(ndep));
676 #endif
677 
678 	*vpp = DETOV(ndep);
679 	return (0);
680 }
681 
682 int
683 msdosfs_statfs(mp, sbp, p)
684 	struct mount *mp;
685 	struct statfs *sbp;
686 	struct proc *p;
687 {
688 	struct msdosfsmount *pmp;
689 
690 	pmp = VFSTOMSDOSFS(mp);
691 	sbp->f_bsize = pmp->pm_bpcluster;
692 	sbp->f_iosize = pmp->pm_bpcluster;
693 	sbp->f_blocks = pmp->pm_nmbrofclusters;
694 	sbp->f_bfree = pmp->pm_freeclustercount;
695 	sbp->f_bavail = pmp->pm_freeclustercount;
696 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
697 	sbp->f_ffree = 0;	/* what to put in here? */
698 	if (sbp != &mp->mnt_stat) {
699 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
700 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
701 		bcopy(&mp->mnt_stat.mount_info.msdosfs_args,
702 		    &sbp->mount_info.msdosfs_args, sizeof(struct msdosfs_args));
703 	}
704 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
705 	return (0);
706 }
707 
708 
709 struct msdosfs_sync_arg {
710 	struct proc *p;
711 	struct ucred *cred;
712 	int allerror;
713 	int waitfor;
714 };
715 
716 int
717 msdosfs_sync_vnode(struct vnode *vp, void *arg)
718 {
719 	struct msdosfs_sync_arg *msa = arg;
720 	int error;
721 	struct denode *dep;
722 
723 	dep = VTODE(vp);
724 	if (vp->v_type == VNON ||
725 	    ((dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0
726 		&& vp->v_dirtyblkhd.lh_first == NULL) ||
727 	    msa->waitfor == MNT_LAZY) {
728 		simple_unlock(&vp->v_interlock);
729 		return (0);
730 	}
731 
732 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, msa->p))
733 		return (0);
734 
735 	if ((error = VOP_FSYNC(vp, msa->cred, msa->waitfor, msa->p)) != 0)
736 		msa->allerror = error;
737 	VOP_UNLOCK(vp, 0, msa->p);
738 	vrele(vp);
739 
740 	return (0);
741 }
742 
743 
744 int
745 msdosfs_sync(mp, waitfor, cred, p)
746 	struct mount *mp;
747 	int waitfor;
748 	struct ucred *cred;
749 	struct proc *p;
750 {
751 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
752 	struct msdosfs_sync_arg msa;
753 	int error;
754 
755 	msa.allerror = 0;
756 	msa.p = p;
757 	msa.cred = cred;
758 	msa.waitfor = waitfor;
759 
760 	/*
761 	 * If we ever switch to not updating all of the fats all the time,
762 	 * this would be the place to update them from the first one.
763 	 */
764 	if (pmp->pm_fmod != 0) {
765 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
766 			panic("msdosfs_sync: rofs mod");
767 		else {
768 			/* update fats here */
769 		}
770 	}
771 	/*
772 	 * Write back each (modified) denode.
773 	 */
774 	vfs_mount_foreach_vnode(mp, msdosfs_sync_vnode, &msa);
775 
776 	/*
777 	 * Force stale file system control information to be flushed.
778 	 */
779 	if (waitfor != MNT_LAZY) {
780 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
781 		if ((error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) != 0)
782 			msa.allerror = error;
783 		VOP_UNLOCK(pmp->pm_devvp, 0, p);
784 	}
785 
786 	return (msa.allerror);
787 }
788 
789 int
790 msdosfs_fhtovp(mp, fhp, vpp)
791 	struct mount *mp;
792 	struct fid *fhp;
793 	struct vnode **vpp;
794 {
795 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
796 	struct defid *defhp = (struct defid *) fhp;
797 	struct denode *dep;
798 	int error;
799 
800 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
801 	if (error) {
802 		*vpp = NULLVP;
803 		return (error);
804 	}
805 	*vpp = DETOV(dep);
806 	return (0);
807 }
808 
809 int
810 msdosfs_vptofh(vp, fhp)
811 	struct vnode *vp;
812 	struct fid *fhp;
813 {
814 	struct denode *dep;
815 	struct defid *defhp;
816 
817 	dep = VTODE(vp);
818 	defhp = (struct defid *)fhp;
819 	defhp->defid_len = sizeof(struct defid);
820 	defhp->defid_dirclust = dep->de_dirclust;
821 	defhp->defid_dirofs = dep->de_diroffset;
822 	/* defhp->defid_gen = dep->de_gen; */
823 	return (0);
824 }
825 
826 int
827 msdosfs_check_export(mp, nam, exflagsp, credanonp)
828 	register struct mount *mp;
829 	struct mbuf *nam;
830 	int *exflagsp;
831 	struct ucred **credanonp;
832 {
833 	register struct netcred *np;
834 	register struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
835 
836 	/*
837 	 * Get the export permission structure for this <mp, client> tuple.
838 	 */
839 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
840 	if (np == NULL)
841 		return (EACCES);
842 
843 	*exflagsp = np->netc_exflags;
844 	*credanonp = &np->netc_anon;
845 	return (0);
846 }
847 
848 #define msdosfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
849 		      eopnotsupp)
850 
851 #define msdosfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
852 					struct proc *))eopnotsupp)
853 
854 #define msdosfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
855                                     size_t, struct proc *))eopnotsupp)
856 
857 struct vfsops msdosfs_vfsops = {
858 	msdosfs_mount,
859 	msdosfs_start,
860 	msdosfs_unmount,
861 	msdosfs_root,
862 	msdosfs_quotactl,
863 	msdosfs_statfs,
864 	msdosfs_sync,
865 	msdosfs_vget,
866 	msdosfs_fhtovp,
867 	msdosfs_vptofh,
868 	msdosfs_init,
869 	msdosfs_sysctl,
870 	msdosfs_check_export
871 };
872