xref: /netbsd-src/sys/fs/msdosfs/msdosfs_vfsops.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: msdosfs_vfsops.c,v 1.27 2005/09/10 17:33:45 christos Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.27 2005/09/10 17:33:45 christos Exp $");
52 
53 #if defined(_KERNEL_OPT)
54 #include "opt_quota.h"
55 #include "opt_compat_netbsd.h"
56 #endif
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/namei.h>
62 #include <sys/proc.h>
63 #include <sys/kernel.h>
64 #include <sys/vnode.h>
65 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
66 #include <sys/mount.h>
67 #include <sys/buf.h>
68 #include <sys/file.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/ioctl.h>
72 #include <sys/malloc.h>
73 #include <sys/dirent.h>
74 #include <sys/stat.h>
75 #include <sys/conf.h>
76 
77 #include <fs/msdosfs/bpb.h>
78 #include <fs/msdosfs/bootsect.h>
79 #include <fs/msdosfs/direntry.h>
80 #include <fs/msdosfs/denode.h>
81 #include <fs/msdosfs/msdosfsmount.h>
82 #include <fs/msdosfs/fat.h>
83 
84 #define MSDOSFS_NAMEMAX(pmp) \
85 	(pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
86 
87 int msdosfs_mountroot(void);
88 int msdosfs_mount(struct mount *, const char *, void *,
89     struct nameidata *, struct proc *);
90 int msdosfs_start(struct mount *, int, struct proc *);
91 int msdosfs_unmount(struct mount *, int, struct proc *);
92 int msdosfs_root(struct mount *, struct vnode **);
93 int msdosfs_quotactl(struct mount *, int, uid_t, void *, struct proc *);
94 int msdosfs_statvfs(struct mount *, struct statvfs *, struct proc *);
95 int msdosfs_sync(struct mount *, int, struct ucred *, struct proc *);
96 int msdosfs_vget(struct mount *, ino_t, struct vnode **);
97 int msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
98 int msdosfs_checkexp(struct mount *, struct mbuf *, int *,
99     struct ucred **);
100 int msdosfs_vptofh(struct vnode *, struct fid *);
101 
102 int msdosfs_mountfs(struct vnode *, struct mount *, struct proc *,
103     struct msdosfs_args *);
104 
105 static int update_mp(struct mount *, struct msdosfs_args *);
106 
107 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
108 MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
109 
110 #define ROOTNAME "root_device"
111 
112 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
113 
114 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
115 	&msdosfs_vnodeop_opv_desc,
116 	NULL,
117 };
118 
119 struct vfsops msdosfs_vfsops = {
120 	MOUNT_MSDOS,
121 	msdosfs_mount,
122 	msdosfs_start,
123 	msdosfs_unmount,
124 	msdosfs_root,
125 	msdosfs_quotactl,
126 	msdosfs_statvfs,
127 	msdosfs_sync,
128 	msdosfs_vget,
129 	msdosfs_fhtovp,
130 	msdosfs_vptofh,
131 	msdosfs_init,
132 	msdosfs_reinit,
133 	msdosfs_done,
134 	NULL,
135 	msdosfs_mountroot,
136 	msdosfs_checkexp,
137 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
138 	vfs_stdextattrctl,
139 	msdosfs_vnodeopv_descs,
140 };
141 VFS_ATTACH(msdosfs_vfsops);
142 
143 static int
144 update_mp(mp, argp)
145 	struct mount *mp;
146 	struct msdosfs_args *argp;
147 {
148 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
149 	int error;
150 
151 	pmp->pm_gid = argp->gid;
152 	pmp->pm_uid = argp->uid;
153 	pmp->pm_mask = argp->mask & ALLPERMS;
154 	pmp->pm_dirmask = argp->dirmask & ALLPERMS;
155 	pmp->pm_gmtoff = argp->gmtoff;
156 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
157 
158 	/*
159 	 * GEMDOS knows nothing (yet) about win95
160 	 */
161 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
162 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
163 
164 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
165 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
166 	else if (!(pmp->pm_flags &
167 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
168 		struct vnode *rtvp;
169 
170 		/*
171 		 * Try to divine whether to support Win'95 long filenames
172 		 */
173 		if (FAT32(pmp))
174 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
175 		else {
176 			if ((error = msdosfs_root(mp, &rtvp)) != 0)
177 				return error;
178 			pmp->pm_flags |= findwin95(VTODE(rtvp))
179 				? MSDOSFSMNT_LONGNAME
180 					: MSDOSFSMNT_SHORTNAME;
181 			vput(rtvp);
182 		}
183 	}
184 
185 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
186 
187 	return 0;
188 }
189 
190 int
191 msdosfs_mountroot()
192 {
193 	struct mount *mp;
194 	struct proc *p = curproc;	/* XXX */
195 	int error;
196 	struct msdosfs_args args;
197 
198 	if (root_device->dv_class != DV_DISK)
199 		return (ENODEV);
200 
201 	if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
202 		vrele(rootvp);
203 		return (error);
204 	}
205 
206 	args.flags = MSDOSFSMNT_VERSIONED;
207 	args.uid = 0;
208 	args.gid = 0;
209 	args.mask = 0777;
210 	args.version = MSDOSFSMNT_VERSION;
211 	args.dirmask = 0777;
212 
213 	if ((error = msdosfs_mountfs(rootvp, mp, p, &args)) != 0) {
214 		mp->mnt_op->vfs_refcount--;
215 		vfs_unbusy(mp);
216 		free(mp, M_MOUNT);
217 		return (error);
218 	}
219 
220 	if ((error = update_mp(mp, &args)) != 0) {
221 		(void)msdosfs_unmount(mp, 0, p);
222 		vfs_unbusy(mp);
223 		free(mp, M_MOUNT);
224 		vrele(rootvp);
225 		return (error);
226 	}
227 
228 	simple_lock(&mountlist_slock);
229 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
230 	simple_unlock(&mountlist_slock);
231 	(void)msdosfs_statvfs(mp, &mp->mnt_stat, p);
232 	vfs_unbusy(mp);
233 	return (0);
234 }
235 
236 /*
237  * mp - path - addr in user space of mount point (ie /usr or whatever)
238  * data - addr in user space of mount params including the name of the block
239  * special file to treat as a filesystem.
240  */
241 int
242 msdosfs_mount(mp, path, data, ndp, p)
243 	struct mount *mp;
244 	const char *path;
245 	void *data;
246 	struct nameidata *ndp;
247 	struct proc *p;
248 {
249 	struct vnode *devvp;	  /* vnode for blk device to mount */
250 	struct msdosfs_args args; /* will hold data from mount request */
251 	/* msdosfs specific mount control block */
252 	struct msdosfsmount *pmp = NULL;
253 	int error, flags;
254 	mode_t accessmode;
255 
256 	if (mp->mnt_flag & MNT_GETARGS) {
257 		pmp = VFSTOMSDOSFS(mp);
258 		if (pmp == NULL)
259 			return EIO;
260 		args.fspec = NULL;
261 		args.uid = pmp->pm_uid;
262 		args.gid = pmp->pm_gid;
263 		args.mask = pmp->pm_mask;
264 		args.flags = pmp->pm_flags;
265 		args.version = MSDOSFSMNT_VERSION;
266 		args.dirmask = pmp->pm_dirmask;
267 		args.gmtoff = pmp->pm_gmtoff;
268 		vfs_showexport(mp, &args.export, &pmp->pm_export);
269 		return copyout(&args, data, sizeof(args));
270 	}
271 	error = copyin(data, &args, sizeof(struct msdosfs_args));
272 	if (error)
273 		return (error);
274 
275 	/*
276 	 * If not versioned (i.e. using old mount_msdos(8)), fill in
277 	 * the additional structure items with suitable defaults.
278 	 */
279 	if ((args.flags & MSDOSFSMNT_VERSIONED) == 0) {
280 		args.version = 1;
281 		args.dirmask = args.mask;
282 	}
283 
284 	/*
285 	 * Reset GMT offset for pre-v3 mount structure args.
286 	 */
287 	if (args.version < 3)
288 		args.gmtoff = 0;
289 
290 	/*
291 	 * If updating, check whether changing from read-only to
292 	 * read/write; if there is no device name, that's all we do.
293 	 */
294 	if (mp->mnt_flag & MNT_UPDATE) {
295 		pmp = VFSTOMSDOSFS(mp);
296 		error = 0;
297 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
298 			flags = WRITECLOSE;
299 			if (mp->mnt_flag & MNT_FORCE)
300 				flags |= FORCECLOSE;
301 			error = vflush(mp, NULLVP, flags);
302 		}
303 		if (!error && (mp->mnt_flag & MNT_RELOAD))
304 			/* not yet implemented */
305 			error = EOPNOTSUPP;
306 		if (error)
307 			return (error);
308 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_iflag & IMNT_WANTRDWR)) {
309 			/*
310 			 * If upgrade to read-write by non-root, then verify
311 			 * that user has necessary permissions on the device.
312 			 */
313 			if (p->p_ucred->cr_uid != 0) {
314 				devvp = pmp->pm_devvp;
315 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
316 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
317 						   p->p_ucred, p);
318 				VOP_UNLOCK(devvp, 0);
319 				if (error)
320 					return (error);
321 			}
322 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
323 		}
324 		if (args.fspec == 0) {
325 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
326 			if (args.flags & MSDOSFSMNT_MNTOPT) {
327 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
328 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
329 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
330 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
331 			}
332 #endif
333 			/*
334 			 * Process export requests.
335 			 */
336 			return (vfs_export(mp, &pmp->pm_export, &args.export));
337 		}
338 	}
339 	/*
340 	 * Not an update, or updating the name: look up the name
341 	 * and verify that it refers to a sensible block device.
342 	 */
343 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
344 	if ((error = namei(ndp)) != 0)
345 		return (error);
346 	devvp = ndp->ni_vp;
347 
348 	if (devvp->v_type != VBLK) {
349 		vrele(devvp);
350 		return (ENOTBLK);
351 	}
352 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
353 		vrele(devvp);
354 		return (ENXIO);
355 	}
356 	/*
357 	 * If mount by non-root, then verify that user has necessary
358 	 * permissions on the device.
359 	 */
360 	if (p->p_ucred->cr_uid != 0) {
361 		accessmode = VREAD;
362 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
363 			accessmode |= VWRITE;
364 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
365 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
366 		VOP_UNLOCK(devvp, 0);
367 		if (error) {
368 			vrele(devvp);
369 			return (error);
370 		}
371 	}
372 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
373 		int xflags;
374 
375 		/*
376 		 * Disallow multiple mounts of the same device.
377 		 * Disallow mounting of a device that is currently in use
378 		 * (except for root, which might share swap device for
379 		 * miniroot).
380 		 */
381 		error = vfs_mountedon(devvp);
382 		if (error)
383 			goto fail;
384 		if (vcount(devvp) > 1 && devvp != rootvp) {
385 			error = EBUSY;
386 			goto fail;
387 		}
388 		if (mp->mnt_flag & MNT_RDONLY)
389 			xflags = FREAD;
390 		else
391 			xflags = FREAD|FWRITE;
392 		error = VOP_OPEN(devvp, xflags, FSCRED, p);
393 		if (error)
394 			goto fail;
395 		error = msdosfs_mountfs(devvp, mp, p, &args);
396 		if (error) {
397 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
398 			(void) VOP_CLOSE(devvp, xflags, NOCRED, p);
399 			VOP_UNLOCK(devvp, 0);
400 			goto fail;
401 		}
402 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
403 		pmp = VFSTOMSDOSFS(mp);
404 #endif
405 	} else {
406 		vrele(devvp);
407 		if (devvp != pmp->pm_devvp)
408 			return (EINVAL);	/* needs translation */
409 	}
410 	if ((error = update_mp(mp, &args)) != 0) {
411 		msdosfs_unmount(mp, MNT_FORCE, p);
412 		return error;
413 	}
414 
415 #ifdef MSDOSFS_DEBUG
416 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
417 #endif
418 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
419 	    mp, p);
420 
421 fail:
422 	vrele(devvp);
423 	return (error);
424 }
425 
426 int
427 msdosfs_mountfs(devvp, mp, p, argp)
428 	struct vnode *devvp;
429 	struct mount *mp;
430 	struct proc *p;
431 	struct msdosfs_args *argp;
432 {
433 	struct msdosfsmount *pmp;
434 	struct buf *bp;
435 	dev_t dev = devvp->v_rdev;
436 	struct partinfo dpart;
437 	union bootsector *bsp;
438 	struct byte_bpb33 *b33;
439 	struct byte_bpb50 *b50;
440 	struct byte_bpb710 *b710;
441 	u_int8_t SecPerClust;
442 	int	ronly, error;
443 	int	bsize = 0, dtype = 0, tmp;
444 
445 	/* Flush out any old buffers remaining from a previous use. */
446 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
447 		return (error);
448 
449 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
450 
451 	bp  = NULL; /* both used in error_exit */
452 	pmp = NULL;
453 
454 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
455 		/*
456 	 	 * We need the disklabel to calculate the size of a FAT entry
457 		 * later on. Also make sure the partition contains a filesystem
458 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
459 		 * to check for them too.
460 	 	 *
461 	 	 * At least some parts of the msdos fs driver seem to assume
462 		 * that the size of a disk block will always be 512 bytes.
463 		 * Let's check it...
464 		 */
465 		error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p);
466 		if (error)
467 			goto error_exit;
468 		tmp   = dpart.part->p_fstype;
469 		dtype = dpart.disklab->d_type;
470 		bsize = dpart.disklab->d_secsize;
471 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
472 			error = EINVAL;
473 			goto error_exit;
474 		}
475 	}
476 
477 	/*
478 	 * Read the boot sector of the filesystem, and then check the
479 	 * boot signature.  If not a dos boot sector then error out.
480 	 */
481 	if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
482 		goto error_exit;
483 	bp->b_flags |= B_AGE;
484 	bsp = (union bootsector *)bp->b_data;
485 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
486 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
487 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
488 
489 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
490 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
491 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
492 			error = EINVAL;
493 			goto error_exit;
494 		}
495 	}
496 
497 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
498 	memset(pmp, 0, sizeof *pmp);
499 	pmp->pm_mountp = mp;
500 
501 	/*
502 	 * Compute several useful quantities from the bpb in the
503 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
504 	 * the fields that are different between dos 5 and dos 3.3.
505 	 */
506 	SecPerClust = b50->bpbSecPerClust;
507 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
508 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
509 	pmp->pm_FATs = b50->bpbFATs;
510 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
511 	pmp->pm_Sectors = getushort(b50->bpbSectors);
512 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
513 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
514 	pmp->pm_Heads = getushort(b50->bpbHeads);
515 	pmp->pm_Media = b50->bpbMedia;
516 
517 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
518 		/* XXX - We should probably check more values here */
519     		if (!pmp->pm_BytesPerSec || !SecPerClust
520 	    		|| pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
521 			error = EINVAL;
522 			goto error_exit;
523 		}
524 	}
525 
526 	if (pmp->pm_Sectors == 0) {
527 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
528 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
529 	} else {
530 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
531 		pmp->pm_HugeSectors = pmp->pm_Sectors;
532 	}
533 
534 	if (pmp->pm_RootDirEnts == 0) {
535 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
536 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
537 		    || pmp->pm_Sectors
538 		    || pmp->pm_FATsecs
539 		    || getushort(b710->bpbFSVers)) {
540 			error = EINVAL;
541 			goto error_exit;
542 		}
543 		pmp->pm_fatmask = FAT32_MASK;
544 		pmp->pm_fatmult = 4;
545 		pmp->pm_fatdiv = 1;
546 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
547 
548 		/* mirrorring is enabled if the FATMIRROR bit is not set */
549 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
550 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
551 		else
552 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
553 	} else
554 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
555 
556 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
557 		if (FAT32(pmp)) {
558 			/*
559 			 * GEMDOS doesn't know fat32.
560 			 */
561 			error = EINVAL;
562 			goto error_exit;
563 		}
564 
565 		/*
566 		 * Check a few values (could do some more):
567 		 * - logical sector size: power of 2, >= block size
568 		 * - sectors per cluster: power of 2, >= 1
569 		 * - number of sectors:   >= 1, <= size of partition
570 		 */
571 		if ( (SecPerClust == 0)
572 		  || (SecPerClust & (SecPerClust - 1))
573 		  || (pmp->pm_BytesPerSec < bsize)
574 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
575 		  || (pmp->pm_HugeSectors == 0)
576 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
577 							> dpart.part->p_size)
578 		   ) {
579 			error = EINVAL;
580 			goto error_exit;
581 		}
582 		/*
583 		 * XXX - Many parts of the msdos fs driver seem to assume that
584 		 * the number of bytes per logical sector (BytesPerSec) will
585 		 * always be the same as the number of bytes per disk block
586 		 * Let's pretend it is.
587 		 */
588 		tmp = pmp->pm_BytesPerSec / bsize;
589 		pmp->pm_BytesPerSec  = bsize;
590 		pmp->pm_HugeSectors *= tmp;
591 		pmp->pm_HiddenSects *= tmp;
592 		pmp->pm_ResSectors  *= tmp;
593 		pmp->pm_Sectors     *= tmp;
594 		pmp->pm_FATsecs     *= tmp;
595 		SecPerClust         *= tmp;
596 	}
597 	pmp->pm_fatblk = pmp->pm_ResSectors;
598 	if (FAT32(pmp)) {
599 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
600 		pmp->pm_firstcluster = pmp->pm_fatblk
601 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
602 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
603 	} else {
604 		pmp->pm_rootdirblk = pmp->pm_fatblk +
605 			(pmp->pm_FATs * pmp->pm_FATsecs);
606 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
607 				       + pmp->pm_BytesPerSec - 1)
608 			/ pmp->pm_BytesPerSec;/* in sectors */
609 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
610 	}
611 
612 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
613 	    SecPerClust;
614 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
615 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
616 
617 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
618 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
619 		      && (dtype == DTYPE_FLOPPY
620 			  || (dtype == DTYPE_VND
621 				&& (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
622 		    ) {
623 			pmp->pm_fatmask = FAT12_MASK;
624 			pmp->pm_fatmult = 3;
625 			pmp->pm_fatdiv = 2;
626 		} else {
627 			pmp->pm_fatmask = FAT16_MASK;
628 			pmp->pm_fatmult = 2;
629 			pmp->pm_fatdiv = 1;
630 		}
631 	} else if (pmp->pm_fatmask == 0) {
632 		if (pmp->pm_maxcluster
633 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
634 			/*
635 			 * This will usually be a floppy disk. This size makes
636 			 * sure that one fat entry will not be split across
637 			 * multiple blocks.
638 			 */
639 			pmp->pm_fatmask = FAT12_MASK;
640 			pmp->pm_fatmult = 3;
641 			pmp->pm_fatdiv = 2;
642 		} else {
643 			pmp->pm_fatmask = FAT16_MASK;
644 			pmp->pm_fatmult = 2;
645 			pmp->pm_fatdiv = 1;
646 		}
647 	}
648 	if (FAT12(pmp))
649 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
650 	else
651 		pmp->pm_fatblocksize = MAXBSIZE;
652 
653 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
654 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
655 
656 	/*
657 	 * Compute mask and shift value for isolating cluster relative byte
658 	 * offsets and cluster numbers from a file offset.
659 	 */
660 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
661 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
662 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
663 
664 	/*
665 	 * Check for valid cluster size
666 	 * must be a power of 2
667 	 */
668 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
669 		error = EINVAL;
670 		goto error_exit;
671 	}
672 
673 	/*
674 	 * Release the bootsector buffer.
675 	 */
676 	brelse(bp);
677 	bp = NULL;
678 
679 	/*
680 	 * Check FSInfo.
681 	 */
682 	if (pmp->pm_fsinfo) {
683 		struct fsinfo *fp;
684 
685 		if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
686 			goto error_exit;
687 		fp = (struct fsinfo *)bp->b_data;
688 		if (!memcmp(fp->fsisig1, "RRaA", 4)
689 		    && !memcmp(fp->fsisig2, "rrAa", 4)
690 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
691 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
692 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
693 		else
694 			pmp->pm_fsinfo = 0;
695 		brelse(bp);
696 		bp = NULL;
697 	}
698 
699 	/*
700 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
701 	 * XXX
702 	 */
703 	if (pmp->pm_fsinfo) {
704 		if (pmp->pm_nxtfree == (u_long)-1)
705 			pmp->pm_fsinfo = 0;
706 	}
707 
708 	/*
709 	 * Allocate memory for the bitmap of allocated clusters, and then
710 	 * fill it in.
711 	 */
712 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
713 				   / N_INUSEBITS)
714 				  * sizeof(*pmp->pm_inusemap),
715 				  M_MSDOSFSFAT, M_WAITOK);
716 
717 	/*
718 	 * fillinusemap() needs pm_devvp.
719 	 */
720 	pmp->pm_dev = dev;
721 	pmp->pm_devvp = devvp;
722 
723 	/*
724 	 * Have the inuse map filled in.
725 	 */
726 	if ((error = fillinusemap(pmp)) != 0)
727 		goto error_exit;
728 
729 	/*
730 	 * If they want fat updates to be synchronous then let them suffer
731 	 * the performance degradation in exchange for the on disk copy of
732 	 * the fat being correct just about all the time.  I suppose this
733 	 * would be a good thing to turn on if the kernel is still flakey.
734 	 */
735 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
736 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
737 
738 	/*
739 	 * Finish up.
740 	 */
741 	if (ronly)
742 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
743 	else
744 		pmp->pm_fmod = 1;
745 	mp->mnt_data = pmp;
746 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
747 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
748 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
749 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
750 	mp->mnt_flag |= MNT_LOCAL;
751 	mp->mnt_dev_bshift = pmp->pm_bnshift;
752 	mp->mnt_fs_bshift = pmp->pm_cnshift;
753 
754 #ifdef QUOTA
755 	/*
756 	 * If we ever do quotas for DOS filesystems this would be a place
757 	 * to fill in the info in the msdosfsmount structure. You dolt,
758 	 * quotas on dos filesystems make no sense because files have no
759 	 * owners on dos filesystems. of course there is some empty space
760 	 * in the directory entry where we could put uid's and gid's.
761 	 */
762 #endif
763 	devvp->v_specmountpoint = mp;
764 
765 	return (0);
766 
767 error_exit:;
768 	if (bp)
769 		brelse(bp);
770 	if (pmp) {
771 		if (pmp->pm_inusemap)
772 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
773 		free(pmp, M_MSDOSFSMNT);
774 		mp->mnt_data = NULL;
775 	}
776 	return (error);
777 }
778 
779 int
780 msdosfs_start(mp, flags, p)
781 	struct mount *mp;
782 	int flags;
783 	struct proc *p;
784 {
785 
786 	return (0);
787 }
788 
789 /*
790  * Unmount the filesystem described by mp.
791  */
792 int
793 msdosfs_unmount(mp, mntflags, p)
794 	struct mount *mp;
795 	int mntflags;
796 	struct proc *p;
797 {
798 	struct msdosfsmount *pmp;
799 	int error, flags;
800 
801 	flags = 0;
802 	if (mntflags & MNT_FORCE)
803 		flags |= FORCECLOSE;
804 #ifdef QUOTA
805 #endif
806 	if ((error = vflush(mp, NULLVP, flags)) != 0)
807 		return (error);
808 	pmp = VFSTOMSDOSFS(mp);
809 	if (pmp->pm_devvp->v_type != VBAD)
810 		pmp->pm_devvp->v_specmountpoint = NULL;
811 #ifdef MSDOSFS_DEBUG
812 	{
813 		struct vnode *vp = pmp->pm_devvp;
814 
815 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
816 		printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
817 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
818 		printf("mount %p, op %p\n",
819 		    vp->v_mount, vp->v_op);
820 		printf("freef %p, freeb %p, mount %p\n",
821 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
822 		    vp->v_mount);
823 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
824 		    vp->v_cleanblkhd.lh_first,
825 		    vp->v_dirtyblkhd.lh_first,
826 		    vp->v_numoutput, vp->v_type);
827 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
828 		    vp->v_socket, vp->v_tag,
829 		    ((u_int *)vp->v_data)[0],
830 		    ((u_int *)vp->v_data)[1]);
831 	}
832 #endif
833 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
834 	error = VOP_CLOSE(pmp->pm_devvp,
835 	    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
836 	vput(pmp->pm_devvp);
837 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
838 	free(pmp, M_MSDOSFSMNT);
839 	mp->mnt_data = NULL;
840 	mp->mnt_flag &= ~MNT_LOCAL;
841 	return (error);
842 }
843 
844 int
845 msdosfs_root(mp, vpp)
846 	struct mount *mp;
847 	struct vnode **vpp;
848 {
849 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
850 	struct denode *ndep;
851 	int error;
852 
853 #ifdef MSDOSFS_DEBUG
854 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
855 #endif
856 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
857 		return (error);
858 	*vpp = DETOV(ndep);
859 	return (0);
860 }
861 
862 int
863 msdosfs_quotactl(mp, cmds, uid, arg, p)
864 	struct mount *mp;
865 	int cmds;
866 	uid_t uid;
867 	void *arg;
868 	struct proc *p;
869 {
870 
871 #ifdef QUOTA
872 	return (EOPNOTSUPP);
873 #else
874 	return (EOPNOTSUPP);
875 #endif
876 }
877 
878 int
879 msdosfs_statvfs(mp, sbp, p)
880 	struct mount *mp;
881 	struct statvfs *sbp;
882 	struct proc *p;
883 {
884 	struct msdosfsmount *pmp;
885 
886 	pmp = VFSTOMSDOSFS(mp);
887 	sbp->f_bsize = pmp->pm_bpcluster;
888 	sbp->f_frsize = sbp->f_bsize;
889 	sbp->f_iosize = pmp->pm_bpcluster;
890 	sbp->f_blocks = pmp->pm_nmbrofclusters;
891 	sbp->f_bfree = pmp->pm_freeclustercount;
892 	sbp->f_bavail = pmp->pm_freeclustercount;
893 	sbp->f_bresvd = 0;
894 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
895 	sbp->f_ffree = 0;	/* what to put in here? */
896 	sbp->f_favail = 0;	/* what to put in here? */
897 	sbp->f_fresvd = 0;
898 	copy_statvfs_info(sbp, mp);
899 	return (0);
900 }
901 
902 int
903 msdosfs_sync(mp, waitfor, cred, p)
904 	struct mount *mp;
905 	int waitfor;
906 	struct ucred *cred;
907 	struct proc *p;
908 {
909 	struct vnode *vp, *nvp;
910 	struct denode *dep;
911 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
912 	int error, allerror = 0;
913 
914 	/*
915 	 * If we ever switch to not updating all of the fats all the time,
916 	 * this would be the place to update them from the first one.
917 	 */
918 	if (pmp->pm_fmod != 0) {
919 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
920 			panic("msdosfs_sync: rofs mod");
921 		else {
922 			/* update fats here */
923 		}
924 	}
925 	/*
926 	 * Write back each (modified) denode.
927 	 */
928 	simple_lock(&mntvnode_slock);
929 loop:
930 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
931 		/*
932 		 * If the vnode that we are about to sync is no longer
933 		 * assoicated with this mount point, start over.
934 		 */
935 		if (vp->v_mount != mp)
936 			goto loop;
937 		simple_lock(&vp->v_interlock);
938 		nvp = vp->v_mntvnodes.le_next;
939 		dep = VTODE(vp);
940 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
941 		    (((dep->de_flag &
942 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
943 		     (LIST_EMPTY(&vp->v_dirtyblkhd) &&
944 		      vp->v_uobj.uo_npages == 0))) {
945 			simple_unlock(&vp->v_interlock);
946 			continue;
947 		}
948 		simple_unlock(&mntvnode_slock);
949 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
950 		if (error) {
951 			simple_lock(&mntvnode_slock);
952 			if (error == ENOENT)
953 				goto loop;
954 			continue;
955 		}
956 		if ((error = VOP_FSYNC(vp, cred,
957 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
958 			allerror = error;
959 		vput(vp);
960 		simple_lock(&mntvnode_slock);
961 	}
962 	simple_unlock(&mntvnode_slock);
963 	/*
964 	 * Force stale file system control information to be flushed.
965 	 */
966 	if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
967 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
968 		allerror = error;
969 #ifdef QUOTA
970 	/* qsync(mp); */
971 #endif
972 	return (allerror);
973 }
974 
975 int
976 msdosfs_fhtovp(mp, fhp, vpp)
977 	struct mount *mp;
978 	struct fid *fhp;
979 	struct vnode **vpp;
980 {
981 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
982 	struct defid *defhp = (struct defid *) fhp;
983 	struct denode *dep;
984 	int error;
985 
986 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
987 	if (error) {
988 		*vpp = NULLVP;
989 		return (error);
990 	}
991 	*vpp = DETOV(dep);
992 	return (0);
993 }
994 
995 int
996 msdosfs_checkexp(mp, nam, exflagsp, credanonp)
997 	struct mount *mp;
998 	struct mbuf *nam;
999 	int *exflagsp;
1000 	struct ucred **credanonp;
1001 {
1002 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1003 	struct netcred *np;
1004 
1005 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
1006 	if (np == NULL)
1007 		return (EACCES);
1008 	*exflagsp = np->netc_exflags;
1009 	*credanonp = &np->netc_anon;
1010 	return (0);
1011 }
1012 
1013 int
1014 msdosfs_vptofh(vp, fhp)
1015 	struct vnode *vp;
1016 	struct fid *fhp;
1017 {
1018 	struct denode *dep;
1019 	struct defid *defhp;
1020 
1021 	dep = VTODE(vp);
1022 	defhp = (struct defid *)fhp;
1023 	defhp->defid_len = sizeof(struct defid);
1024 	defhp->defid_dirclust = dep->de_dirclust;
1025 	defhp->defid_dirofs = dep->de_diroffset;
1026 	/* defhp->defid_gen = dep->de_gen; */
1027 	return (0);
1028 }
1029 
1030 int
1031 msdosfs_vget(mp, ino, vpp)
1032 	struct mount *mp;
1033 	ino_t ino;
1034 	struct vnode **vpp;
1035 {
1036 
1037 	return (EOPNOTSUPP);
1038 }
1039 
1040 SYSCTL_SETUP(sysctl_vfs_msdosfs_setup, "sysctl vfs.msdosfs subtree setup")
1041 {
1042 
1043 	sysctl_createv(clog, 0, NULL, NULL,
1044 		       CTLFLAG_PERMANENT,
1045 		       CTLTYPE_NODE, "vfs", NULL,
1046 		       NULL, 0, NULL, 0,
1047 		       CTL_VFS, CTL_EOL);
1048 	sysctl_createv(clog, 0, NULL, NULL,
1049 		       CTLFLAG_PERMANENT,
1050 		       CTLTYPE_NODE, "msdosfs",
1051 		       SYSCTL_DESCR("MS-DOS file system"),
1052 		       NULL, 0, NULL, 0,
1053 		       CTL_VFS, 4, CTL_EOL);
1054 	/*
1055 	 * XXX the "4" above could be dynamic, thereby eliminating one
1056 	 * more instance of the "number to vfs" mapping problem, but
1057 	 * "4" is the order as taken from sys/mount.h
1058 	 */
1059 }
1060