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