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