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