xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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 
65 #include <msdosfs/bpb.h>
66 #include <msdosfs/bootsect.h>
67 #include <msdosfs/direntry.h>
68 #include <msdosfs/denode.h>
69 #include <msdosfs/msdosfsmount.h>
70 #include <msdosfs/fat.h>
71 
72 #define MSDOSFS_DFLTBSIZE       4096
73 
74 #if 1 /*def PC98*/
75 /*
76  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
77  *       garbage or a random value :-{
78  *       If you want to use that broken-signatured media, define the
79  *       following symbol even though PC/AT.
80  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
81  */
82 #define	MSDOSFS_NOCHECKSIG
83 #endif
84 
85 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
86 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
87 
88 static int	update_mp __P((struct mount *mp, struct msdosfs_args *argp));
89 static int	mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
90 				  struct proc *p, struct msdosfs_args *argp));
91 static int	msdosfs_fhtovp __P((struct mount *, struct fid *,
92 				    struct vnode **));
93 static int	msdosfs_checkexp __P((struct mount *, struct sockaddr *,
94 				    int *, struct ucred **));
95 static int	msdosfs_mount __P((struct mount *, char *, caddr_t,
96 				   struct nameidata *, struct proc *));
97 static int	msdosfs_root __P((struct mount *, struct vnode **));
98 static int	msdosfs_statfs __P((struct mount *, struct statfs *,
99 				    struct proc *));
100 static int	msdosfs_sync __P((struct mount *, int, struct ucred *,
101 				  struct proc *));
102 static int	msdosfs_unmount __P((struct mount *, int, struct proc *));
103 static int	msdosfs_vptofh __P((struct vnode *, struct fid *));
104 
105 static int
106 update_mp(mp, argp)
107 	struct mount *mp;
108 	struct msdosfs_args *argp;
109 {
110 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
111 	int error;
112 
113 	pmp->pm_gid = argp->gid;
114 	pmp->pm_uid = argp->uid;
115 	pmp->pm_mask = argp->mask & ALLPERMS;
116 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
117 	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
118 		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
119 		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
120 		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
121 	}
122 	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
123 		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
124 		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
125 	}
126 
127 #ifndef __FreeBSD__
128 	/*
129 	 * GEMDOS knows nothing (yet) about win95
130 	 */
131 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
132 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
133 #endif
134 
135 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
136 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
137 	else if (!(pmp->pm_flags &
138 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
139 		struct vnode *rootvp;
140 
141 		/*
142 		 * Try to divine whether to support Win'95 long filenames
143 		 */
144 		if (FAT32(pmp))
145 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
146 		else {
147 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
148 				return error;
149 			pmp->pm_flags |= findwin95(VTODE(rootvp))
150 				? MSDOSFSMNT_LONGNAME
151 					: MSDOSFSMNT_SHORTNAME;
152 			vput(rootvp);
153 		}
154 	}
155 	return 0;
156 }
157 
158 #ifndef __FreeBSD__
159 int
160 msdosfs_mountroot()
161 {
162 	register struct mount *mp;
163 	struct proc *p = curproc;	/* XXX */
164 	size_t size;
165 	int error;
166 	struct msdosfs_args args;
167 
168 	if (root_device->dv_class != DV_DISK)
169 		return (ENODEV);
170 
171 	/*
172 	 * Get vnodes for swapdev and rootdev.
173 	 */
174 	if (bdevvp(rootdev, &rootvp))
175 		panic("msdosfs_mountroot: can't setup rootvp");
176 
177 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
178 	bzero((char *)mp, (u_long)sizeof(struct mount));
179 	mp->mnt_op = &msdosfs_vfsops;
180 	mp->mnt_flag = 0;
181 	LIST_INIT(&mp->mnt_vnodelist);
182 
183 	args.flags = 0;
184 	args.uid = 0;
185 	args.gid = 0;
186 	args.mask = 0777;
187 
188 	if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
189 		free(mp, M_MOUNT);
190 		return (error);
191 	}
192 
193 	if ((error = update_mp(mp, &args)) != 0) {
194 		(void)msdosfs_unmount(mp, 0, p);
195 		free(mp, M_MOUNT);
196 		return (error);
197 	}
198 
199 	if ((error = vfs_lock(mp)) != 0) {
200 		(void)msdosfs_unmount(mp, 0, p);
201 		free(mp, M_MOUNT);
202 		return (error);
203 	}
204 
205 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
206 	mp->mnt_vnodecovered = NULLVP;
207 	(void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
208 	    &size);
209 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
210 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
211 	    &size);
212 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
213 	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
214 	vfs_unlock(mp);
215 	return (0);
216 }
217 #endif
218 
219 /*
220  * mp - path - addr in user space of mount point (ie /usr or whatever)
221  * data - addr in user space of mount params including the name of the block
222  * special file to treat as a filesystem.
223  */
224 static int
225 msdosfs_mount(mp, path, data, ndp, p)
226 	struct mount *mp;
227 	char *path;
228 	caddr_t data;
229 	struct nameidata *ndp;
230 	struct proc *p;
231 {
232 	struct vnode *devvp;	  /* vnode for blk device to mount */
233 	struct msdosfs_args args; /* will hold data from mount request */
234 	/* msdosfs specific mount control block */
235 	struct msdosfsmount *pmp = NULL;
236 	size_t size;
237 	int error, flags;
238 	mode_t accessmode;
239 
240 	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
241 	if (error)
242 		return (error);
243 	if (args.magic != MSDOSFS_ARGSMAGIC)
244 		args.flags = 0;
245 	/*
246 	 * If updating, check whether changing from read-only to
247 	 * read/write; if there is no device name, that's all we do.
248 	 */
249 	if (mp->mnt_flag & MNT_UPDATE) {
250 		pmp = VFSTOMSDOSFS(mp);
251 		error = 0;
252 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
253 			flags = WRITECLOSE;
254 			if (mp->mnt_flag & MNT_FORCE)
255 				flags |= FORCECLOSE;
256 			error = vflush(mp, NULLVP, flags);
257 		}
258 		if (!error && (mp->mnt_flag & MNT_RELOAD))
259 			/* not yet implemented */
260 			error = EOPNOTSUPP;
261 		if (error)
262 			return (error);
263 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
264 			/*
265 			 * If upgrade to read-write by non-root, then verify
266 			 * that user has necessary permissions on the device.
267 			 */
268 			if (p->p_ucred->cr_uid != 0) {
269 				devvp = pmp->pm_devvp;
270 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
271 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
272 						   p->p_ucred, p);
273 				if (error) {
274 					VOP_UNLOCK(devvp, 0, p);
275 					return (error);
276 				}
277 				VOP_UNLOCK(devvp, 0, p);
278 			}
279 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
280 		}
281 		if (args.fspec == 0) {
282 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
283 			if (args.flags & MSDOSFSMNT_MNTOPT) {
284 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
285 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
286 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
287 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
288 			}
289 #endif
290 			/*
291 			 * Process export requests.
292 			 */
293 			return (vfs_export(mp, &pmp->pm_export, &args.export));
294 		}
295 	}
296 	/*
297 	 * Not an update, or updating the name: look up the name
298 	 * and verify that it refers to a sensible block device.
299 	 */
300 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
301 	error = namei(ndp);
302 	if (error)
303 		return (error);
304 	devvp = ndp->ni_vp;
305 	NDFREE(ndp, NDF_ONLY_PNBUF);
306 
307 	if (!vn_isdisk(devvp, &error)) {
308 		vrele(devvp);
309 		return (error);
310 	}
311 	/*
312 	 * If mount by non-root, then verify that user has necessary
313 	 * permissions on the device.
314 	 */
315 	if (p->p_ucred->cr_uid != 0) {
316 		accessmode = VREAD;
317 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
318 			accessmode |= VWRITE;
319 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
320 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
321 		if (error) {
322 			vput(devvp);
323 			return (error);
324 		}
325 		VOP_UNLOCK(devvp, 0, p);
326 	}
327 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
328 		error = mountmsdosfs(devvp, mp, p, &args);
329 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
330 		pmp = VFSTOMSDOSFS(mp);
331 #endif
332 	} else {
333 		if (devvp != pmp->pm_devvp)
334 			error = EINVAL;	/* XXX needs translation */
335 		else
336 			vrele(devvp);
337 	}
338 	if (error) {
339 		vrele(devvp);
340 		return (error);
341 	}
342 
343 	error = update_mp(mp, &args);
344 	if (error) {
345 		msdosfs_unmount(mp, MNT_FORCE, p);
346 		return error;
347 	}
348 
349 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
350 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
351 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
352 	    &size);
353 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
354 	(void) msdosfs_statfs(mp, &mp->mnt_stat, p);
355 #ifdef MSDOSFS_DEBUG
356 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
357 #endif
358 	return (0);
359 }
360 
361 static int
362 mountmsdosfs(devvp, mp, p, argp)
363 	struct vnode *devvp;
364 	struct mount *mp;
365 	struct proc *p;
366 	struct msdosfs_args *argp;
367 {
368 	struct msdosfsmount *pmp;
369 	struct buf *bp;
370 	dev_t dev = devvp->v_rdev;
371 #ifndef __FreeBSD__
372 	struct partinfo dpart;
373 	int bsize = 0, dtype = 0, tmp;
374 #endif
375 	union bootsector *bsp;
376 	struct byte_bpb33 *b33;
377 	struct byte_bpb50 *b50;
378 	struct byte_bpb710 *b710;
379 	u_int8_t SecPerClust;
380 	u_long clusters;
381 	int	ronly, error;
382 
383 	/*
384 	 * Disallow multiple mounts of the same device.
385 	 * Disallow mounting of a device that is currently in use
386 	 * (except for root, which might share swap device for miniroot).
387 	 * Flush out any old buffers remaining from a previous use.
388 	 */
389 	error = vfs_mountedon(devvp);
390 	if (error)
391 		return (error);
392 	if (vcount(devvp) > 1 && devvp != rootvp)
393 		return (EBUSY);
394 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
395 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
396 	VOP_UNLOCK(devvp, 0, p);
397 	if (error)
398 		return (error);
399 
400 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
401 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
402 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
403 	VOP_UNLOCK(devvp, 0, p);
404 	if (error)
405 		return (error);
406 
407 	bp  = NULL; /* both used in error_exit */
408 	pmp = NULL;
409 
410 #ifndef __FreeBSD__
411 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
412 		/*
413 	 	 * We need the disklabel to calculate the size of a FAT entry
414 		 * later on. Also make sure the partition contains a filesystem
415 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
416 		 * to check for them too.
417 	 	 *
418 	 	 * At least some parts of the msdos fs driver seem to assume
419 		 * that the size of a disk block will always be 512 bytes.
420 		 * Let's check it...
421 		 */
422 		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
423 				  FREAD, NOCRED, p);
424 		if (error)
425 			goto error_exit;
426 		tmp   = dpart.part->p_fstype;
427 		dtype = dpart.disklab->d_type;
428 		bsize = dpart.disklab->d_secsize;
429 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
430 			error = EINVAL;
431 			goto error_exit;
432 		}
433 	}
434 #endif
435 
436 	/*
437 	 * Read the boot sector of the filesystem, and then check the
438 	 * boot signature.  If not a dos boot sector then error out.
439 	 *
440 	 * NOTE: 2048 is a maximum sector size in current...
441 	 */
442 	error = bread(devvp, 0, 2048, NOCRED, &bp);
443 	if (error)
444 		goto error_exit;
445 	bp->b_flags |= B_AGE;
446 	bsp = (union bootsector *)bp->b_data;
447 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
448 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
449 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
450 
451 #ifndef __FreeBSD__
452 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
453 #endif
454 #ifndef MSDOSFS_NOCHECKSIG
455 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
456 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
457 			error = EINVAL;
458 			goto error_exit;
459 		}
460 #endif
461 #ifndef __FreeBSD__
462 	}
463 #endif
464 
465 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
466 	bzero((caddr_t)pmp, sizeof *pmp);
467 	pmp->pm_mountp = mp;
468 
469 	/*
470 	 * Compute several useful quantities from the bpb in the
471 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
472 	 * the fields that are different between dos 5 and dos 3.3.
473 	 */
474 	SecPerClust = b50->bpbSecPerClust;
475 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
476 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
477 	pmp->pm_FATs = b50->bpbFATs;
478 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
479 	pmp->pm_Sectors = getushort(b50->bpbSectors);
480 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
481 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
482 	pmp->pm_Heads = getushort(b50->bpbHeads);
483 	pmp->pm_Media = b50->bpbMedia;
484 
485 	/* calculate the ratio of sector size to DEV_BSIZE */
486 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
487 
488 #ifndef __FreeBSD__
489 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
490 #endif
491 		/* XXX - We should probably check more values here */
492 		if (!pmp->pm_BytesPerSec || !SecPerClust
493 			|| !pmp->pm_Heads || pmp->pm_Heads > 255
494 #ifdef PC98
495 	    		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
496 #else
497 			|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
498 #endif
499 			error = EINVAL;
500 			goto error_exit;
501 		}
502 #ifndef __FreeBSD__
503 	}
504 #endif
505 
506 	if (pmp->pm_Sectors == 0) {
507 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
508 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
509 	} else {
510 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
511 		pmp->pm_HugeSectors = pmp->pm_Sectors;
512 	}
513 	if (pmp->pm_HugeSectors > 0xffffffff /
514 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
515 		/*
516 		 * We cannot deal currently with this size of disk
517 		 * due to fileid limitations (see msdosfs_getattr and
518 		 * msdosfs_readdir)
519 		 */
520 		error = EINVAL;
521 		printf("mountmsdosfs(): disk too big, sorry\n");
522 		goto error_exit;
523 	}
524 
525 	if (pmp->pm_RootDirEnts == 0) {
526 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
527 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
528 		    || pmp->pm_Sectors
529 		    || pmp->pm_FATsecs
530 		    || getushort(b710->bpbFSVers)) {
531 			error = EINVAL;
532 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
533 			goto error_exit;
534 		}
535 		pmp->pm_fatmask = FAT32_MASK;
536 		pmp->pm_fatmult = 4;
537 		pmp->pm_fatdiv = 1;
538 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
539 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
540 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
541 		else
542 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
543 	} else
544 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
545 
546 	/*
547 	 * Check a few values (could do some more):
548 	 * - logical sector size: power of 2, >= block size
549 	 * - sectors per cluster: power of 2, >= 1
550 	 * - number of sectors:   >= 1, <= size of partition
551 	 */
552 	if ( (SecPerClust == 0)
553 	  || (SecPerClust & (SecPerClust - 1))
554 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
555 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
556 	  || (pmp->pm_HugeSectors == 0)
557 	) {
558 		error = EINVAL;
559 		goto error_exit;
560 	}
561 
562 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
563 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
564 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
565 	SecPerClust         *= pmp->pm_BlkPerSec;
566 
567 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
568 
569 	if (FAT32(pmp)) {
570 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
571 		pmp->pm_firstcluster = pmp->pm_fatblk
572 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
573 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
574 	} else {
575 		pmp->pm_rootdirblk = pmp->pm_fatblk +
576 			(pmp->pm_FATs * pmp->pm_FATsecs);
577 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
578 				       + DEV_BSIZE - 1)
579 			/ DEV_BSIZE; /* in blocks */
580 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
581 	}
582 
583 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
584 	    SecPerClust + 1;
585 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
586 
587 #ifndef __FreeBSD__
588 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
589 		if ((pmp->pm_maxcluster <= (0xff0 - 2))
590 		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
591 		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
592 		    ) {
593 			pmp->pm_fatmask = FAT12_MASK;
594 			pmp->pm_fatmult = 3;
595 			pmp->pm_fatdiv = 2;
596 		} else {
597 			pmp->pm_fatmask = FAT16_MASK;
598 			pmp->pm_fatmult = 2;
599 			pmp->pm_fatdiv = 1;
600 		}
601 	} else
602 #endif
603 	if (pmp->pm_fatmask == 0) {
604 		if (pmp->pm_maxcluster
605 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
606 			/*
607 			 * This will usually be a floppy disk. This size makes
608 			 * sure that one fat entry will not be split across
609 			 * multiple blocks.
610 			 */
611 			pmp->pm_fatmask = FAT12_MASK;
612 			pmp->pm_fatmult = 3;
613 			pmp->pm_fatdiv = 2;
614 		} else {
615 			pmp->pm_fatmask = FAT16_MASK;
616 			pmp->pm_fatmult = 2;
617 			pmp->pm_fatdiv = 1;
618 		}
619 	}
620 
621 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
622 	if (pmp->pm_maxcluster >= clusters) {
623 		printf("Warning: number of clusters (%ld) exceeds FAT "
624 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
625 		pmp->pm_maxcluster = clusters - 1;
626 	}
627 
628 
629 	if (FAT12(pmp))
630 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
631 	else
632 		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
633 
634 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
635 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
636 
637 	/*
638 	 * Compute mask and shift value for isolating cluster relative byte
639 	 * offsets and cluster numbers from a file offset.
640 	 */
641 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
642 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
643 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
644 
645 	/*
646 	 * Check for valid cluster size
647 	 * must be a power of 2
648 	 */
649 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
650 		error = EINVAL;
651 		goto error_exit;
652 	}
653 
654 	/*
655 	 * Release the bootsector buffer.
656 	 */
657 	brelse(bp);
658 	bp = NULL;
659 
660 	/*
661 	 * Check FSInfo.
662 	 */
663 	if (pmp->pm_fsinfo) {
664 		struct fsinfo *fp;
665 
666 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
667 		    NOCRED, &bp)) != 0)
668 			goto error_exit;
669 		fp = (struct fsinfo *)bp->b_data;
670 		if (!bcmp(fp->fsisig1, "RRaA", 4)
671 		    && !bcmp(fp->fsisig2, "rrAa", 4)
672 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
673 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
674 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
675 		else
676 			pmp->pm_fsinfo = 0;
677 		brelse(bp);
678 		bp = NULL;
679 	}
680 
681 	/*
682 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
683 	 */
684 
685 	/*
686 	 * Allocate memory for the bitmap of allocated clusters, and then
687 	 * fill it in.
688 	 */
689 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
690 				   / N_INUSEBITS)
691 				  * sizeof(*pmp->pm_inusemap),
692 				  M_MSDOSFSFAT, M_WAITOK);
693 
694 	/*
695 	 * fillinusemap() needs pm_devvp.
696 	 */
697 	pmp->pm_dev = dev;
698 	pmp->pm_devvp = devvp;
699 
700 	/*
701 	 * Have the inuse map filled in.
702 	 */
703 	if ((error = fillinusemap(pmp)) != 0)
704 		goto error_exit;
705 
706 	/*
707 	 * If they want fat updates to be synchronous then let them suffer
708 	 * the performance degradation in exchange for the on disk copy of
709 	 * the fat being correct just about all the time.  I suppose this
710 	 * would be a good thing to turn on if the kernel is still flakey.
711 	 */
712 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
713 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
714 
715 	/*
716 	 * Finish up.
717 	 */
718 	if (ronly)
719 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
720 	else
721 		pmp->pm_fmod = 1;
722 	mp->mnt_data = (qaddr_t) pmp;
723 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
724 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
725 	mp->mnt_flag |= MNT_LOCAL;
726 	devvp->v_specmountpoint = mp;
727 
728 	return 0;
729 
730 error_exit:
731 	if (bp)
732 		brelse(bp);
733 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
734 	if (pmp) {
735 		if (pmp->pm_inusemap)
736 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
737 		free(pmp, M_MSDOSFSMNT);
738 		mp->mnt_data = (qaddr_t)0;
739 	}
740 	return (error);
741 }
742 
743 /*
744  * Unmount the filesystem described by mp.
745  */
746 static int
747 msdosfs_unmount(mp, mntflags, p)
748 	struct mount *mp;
749 	int mntflags;
750 	struct proc *p;
751 {
752 	struct msdosfsmount *pmp;
753 	int error, flags;
754 
755 	flags = 0;
756 	if (mntflags & MNT_FORCE)
757 		flags |= FORCECLOSE;
758 	error = vflush(mp, NULLVP, flags);
759 	if (error)
760 		return error;
761 	pmp = VFSTOMSDOSFS(mp);
762 	pmp->pm_devvp->v_specmountpoint = NULL;
763 #ifdef MSDOSFS_DEBUG
764 	{
765 		struct vnode *vp = pmp->pm_devvp;
766 
767 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
768 		printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
769 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
770 		printf("id %lu, mount %p, op %p\n",
771 		    vp->v_id, vp->v_mount, vp->v_op);
772 		printf("freef %p, freeb %p, mount %p\n",
773 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
774 		    vp->v_mount);
775 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
776 		    TAILQ_FIRST(&vp->v_cleanblkhd),
777 		    TAILQ_FIRST(&vp->v_dirtyblkhd),
778 		    vp->v_numoutput, vp->v_type);
779 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
780 		    vp->v_socket, vp->v_tag,
781 		    ((u_int *)vp->v_data)[0],
782 		    ((u_int *)vp->v_data)[1]);
783 	}
784 #endif
785 	error = VOP_CLOSE(pmp->pm_devvp,
786 		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
787 		    NOCRED, p);
788 	vrele(pmp->pm_devvp);
789 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
790 	free(pmp, M_MSDOSFSMNT);
791 	mp->mnt_data = (qaddr_t)0;
792 	mp->mnt_flag &= ~MNT_LOCAL;
793 	return (error);
794 }
795 
796 static int
797 msdosfs_root(mp, vpp)
798 	struct mount *mp;
799 	struct vnode **vpp;
800 {
801 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
802 	struct denode *ndep;
803 	int error;
804 
805 #ifdef MSDOSFS_DEBUG
806 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
807 #endif
808 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
809 	if (error)
810 		return (error);
811 	*vpp = DETOV(ndep);
812 	return (0);
813 }
814 
815 static int
816 msdosfs_statfs(mp, sbp, p)
817 	struct mount *mp;
818 	struct statfs *sbp;
819 	struct proc *p;
820 {
821 	struct msdosfsmount *pmp;
822 
823 	pmp = VFSTOMSDOSFS(mp);
824 	sbp->f_bsize = pmp->pm_bpcluster;
825 	sbp->f_iosize = pmp->pm_bpcluster;
826 	sbp->f_blocks = pmp->pm_maxcluster + 1;
827 	sbp->f_bfree = pmp->pm_freeclustercount;
828 	sbp->f_bavail = pmp->pm_freeclustercount;
829 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
830 	sbp->f_ffree = 0;	/* what to put in here? */
831 	if (sbp != &mp->mnt_stat) {
832 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
833 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
834 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
835 	}
836 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
837 	return (0);
838 }
839 
840 static int
841 msdosfs_sync(mp, waitfor, cred, p)
842 	struct mount *mp;
843 	int waitfor;
844 	struct ucred *cred;
845 	struct proc *p;
846 {
847 	struct vnode *vp, *nvp;
848 	struct denode *dep;
849 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
850 	int error, allerror = 0;
851 
852 	/*
853 	 * If we ever switch to not updating all of the fats all the time,
854 	 * this would be the place to update them from the first one.
855 	 */
856 	if (pmp->pm_fmod != 0) {
857 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
858 			panic("msdosfs_sync: rofs mod");
859 		else {
860 			/* update fats here */
861 		}
862 	}
863 	/*
864 	 * Write back each (modified) denode.
865 	 */
866 	simple_lock(&mntvnode_slock);
867 loop:
868 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
869 		/*
870 		 * If the vnode that we are about to sync is no longer
871 		 * associated with this mount point, start over.
872 		 */
873 		if (vp->v_mount != mp)
874 			goto loop;
875 
876 		simple_lock(&vp->v_interlock);
877 		nvp = vp->v_mntvnodes.le_next;
878 		dep = VTODE(vp);
879 		if (vp->v_type == VNON ||
880 		    ((dep->de_flag &
881 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
882 		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
883 			simple_unlock(&vp->v_interlock);
884 			continue;
885 		}
886 		simple_unlock(&mntvnode_slock);
887 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
888 		if (error) {
889 			simple_lock(&mntvnode_slock);
890 			if (error == ENOENT)
891 				goto loop;
892 			continue;
893 		}
894 		error = VOP_FSYNC(vp, cred, waitfor, p);
895 		if (error)
896 			allerror = error;
897 		VOP_UNLOCK(vp, 0, p);
898 		vrele(vp);
899 		simple_lock(&mntvnode_slock);
900 	}
901 	simple_unlock(&mntvnode_slock);
902 
903 	/*
904 	 * Flush filesystem control info.
905 	 */
906 	if (waitfor != MNT_LAZY) {
907 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
908 		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
909 		if (error)
910 			allerror = error;
911 		VOP_UNLOCK(pmp->pm_devvp, 0, p);
912 	}
913 	return (allerror);
914 }
915 
916 static int
917 msdosfs_fhtovp(mp, fhp, vpp)
918 	struct mount *mp;
919 	struct fid *fhp;
920 	struct vnode **vpp;
921 {
922 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
923 	struct defid *defhp = (struct defid *) fhp;
924 	struct denode *dep;
925 	int error;
926 
927 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
928 	if (error) {
929 		*vpp = NULLVP;
930 		return (error);
931 	}
932 	*vpp = DETOV(dep);
933 	return (0);
934 }
935 
936 static int
937 msdosfs_checkexp(mp, nam,  exflagsp, credanonp)
938 	struct mount *mp;
939 	struct sockaddr *nam;
940 	int *exflagsp;
941 	struct ucred **credanonp;
942 {
943 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
944 	struct netcred *np;
945 
946 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
947 	if (np == NULL)
948 		return (EACCES);
949 	*exflagsp = np->netc_exflags;
950 	*credanonp = &np->netc_anon;
951 	return (0);
952 }
953 
954 static int
955 msdosfs_vptofh(vp, fhp)
956 	struct vnode *vp;
957 	struct fid *fhp;
958 {
959 	struct denode *dep;
960 	struct defid *defhp;
961 
962 	dep = VTODE(vp);
963 	defhp = (struct defid *)fhp;
964 	defhp->defid_len = sizeof(struct defid);
965 	defhp->defid_dirclust = dep->de_dirclust;
966 	defhp->defid_dirofs = dep->de_diroffset;
967 	/* defhp->defid_gen = dep->de_gen; */
968 	return (0);
969 }
970 
971 static struct vfsops msdosfs_vfsops = {
972 	msdosfs_mount,
973 	vfs_stdstart,
974 	msdosfs_unmount,
975 	msdosfs_root,
976 	vfs_stdquotactl,
977 	msdosfs_statfs,
978 	msdosfs_sync,
979 	vfs_stdvget,
980 	msdosfs_fhtovp,
981 	msdosfs_checkexp,
982 	msdosfs_vptofh,
983 	msdosfs_init,
984 	msdosfs_uninit,
985 	vfs_stdextattrctl,
986 };
987 
988 VFS_SET(msdosfs_vfsops, msdos, 0);
989