msdosfs_vfsops.c revision 71576
14273SN/A/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 71576 2001-01-24 12:35:55Z jasone $ */
217519Sapetcher/*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
34273SN/A
44273SN/A/*-
54273SN/A * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
64273SN/A * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
74273SN/A * All rights reserved.
84273SN/A * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
91674SN/A *
104273SN/A * Redistribution and use in source and binary forms, with or without
114273SN/A * modification, are permitted provided that the following conditions
124273SN/A * are met:
134273SN/A * 1. Redistributions of source code must retain the above copyright
141674SN/A *    notice, this list of conditions and the following disclaimer.
154273SN/A * 2. Redistributions in binary form must reproduce the above copyright
164273SN/A *    notice, this list of conditions and the following disclaimer in the
174273SN/A *    documentation and/or other materials provided with the distribution.
181674SN/A * 3. All advertising materials mentioning features or use of this software
194273SN/A *    must display the following acknowledgement:
204273SN/A *	This product includes software developed by TooLs GmbH.
214273SN/A * 4. The name of TooLs GmbH may not be used to endorse or promote products
224273SN/A *    derived from this software without specific prior written permission.
234273SN/A *
244273SN/A * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
251674SN/A * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
261674SN/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
271674SN/A * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
281674SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
291674SN/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
301674SN/A * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
311674SN/A * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
321674SN/A * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
331674SN/A * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341674SN/A */
351674SN/A/*
3617519Sapetcher * Written by Paul Popelka (paulp@uts.amdahl.com)
371674SN/A *
381674SN/A * You can do anything you want with this software, just don't say you wrote
391674SN/A * it, and don't remove this notice.
401674SN/A *
411674SN/A * This software is provided "as is".
421674SN/A *
431674SN/A * The author supplies this software to be publicly redistributed on the
441674SN/A * understanding that the author is not responsible for the correct
451674SN/A * functioning of this software in any circumstances and is not liable for
461674SN/A * any damages caused by this software.
471674SN/A *
481674SN/A * October 1992
491674SN/A */
501674SN/A
511674SN/A#include <sys/param.h>
521674SN/A#include <sys/systm.h>
5317519Sapetcher#include <sys/conf.h>
5417519Sapetcher#include <sys/namei.h>
551674SN/A#include <sys/proc.h>
561674SN/A#include <sys/kernel.h>
571674SN/A#include <sys/vnode.h>
581674SN/A#include <sys/mount.h>
591674SN/A#include <sys/bio.h>
601674SN/A#include <sys/buf.h>
611674SN/A#include <sys/fcntl.h>
621674SN/A#include <sys/malloc.h>
631674SN/A#include <sys/stat.h> 				/* defines ALLPERMS */
641674SN/A#include <sys/mutex.h>
651674SN/A
661674SN/A#include <msdosfs/bpb.h>
671674SN/A#include <msdosfs/bootsect.h>
681674SN/A#include <msdosfs/direntry.h>
691674SN/A#include <msdosfs/denode.h>
701674SN/A#include <msdosfs/msdosfsmount.h>
711674SN/A#include <msdosfs/fat.h>
721674SN/A
731674SN/A#define MSDOSFS_DFLTBSIZE       4096
741674SN/A
751674SN/A#if 1 /*def PC98*/
761674SN/A/*
7716229Sgoetz * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
781674SN/A *       garbage or a random value :-{
791674SN/A *       If you want to use that broken-signatured media, define the
8017519Sapetcher *       following symbol even though PC/AT.
811674SN/A *       (ex. mount PC-98 DOS formatted FD on PC/AT)
821674SN/A */
831674SN/A#define	MSDOSFS_NOCHECKSIG
841674SN/A#endif
851674SN/A
8617519SapetcherMALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
871674SN/Astatic MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
8816229Sgoetz
8917519Sapetcherstatic int	update_mp __P((struct mount *mp, struct msdosfs_args *argp));
901674SN/Astatic int	mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
911674SN/A				  struct proc *p, struct msdosfs_args *argp));
921674SN/Astatic int	msdosfs_fhtovp __P((struct mount *, struct fid *,
931674SN/A				    struct vnode **));
941674SN/Astatic int	msdosfs_checkexp __P((struct mount *, struct sockaddr *,
951674SN/A				    int *, struct ucred **));
961674SN/Astatic int	msdosfs_mount __P((struct mount *, char *, caddr_t,
971674SN/A				   struct nameidata *, struct proc *));
981674SN/Astatic int	msdosfs_root __P((struct mount *, struct vnode **));
991674SN/Astatic int	msdosfs_statfs __P((struct mount *, struct statfs *,
1001674SN/A				    struct proc *));
1011674SN/Astatic int	msdosfs_sync __P((struct mount *, int, struct ucred *,
1021674SN/A				  struct proc *));
1031674SN/Astatic int	msdosfs_unmount __P((struct mount *, int, struct proc *));
1041674SN/Astatic int	msdosfs_vptofh __P((struct vnode *, struct fid *));
1051674SN/A
1061674SN/Astatic int
1071674SN/Aupdate_mp(mp, argp)
1081674SN/A	struct mount *mp;
1091674SN/A	struct msdosfs_args *argp;
1101674SN/A{
11117519Sapetcher	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1121674SN/A	int error;
1131674SN/A
1141674SN/A	pmp->pm_gid = argp->gid;
1151674SN/A	pmp->pm_uid = argp->uid;
1161674SN/A	pmp->pm_mask = argp->mask & ALLPERMS;
1171674SN/A	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
1181674SN/A	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
1191674SN/A		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
1201674SN/A		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
1211674SN/A		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
1221674SN/A	}
12317519Sapetcher	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
1241674SN/A		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
12517519Sapetcher		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
1261674SN/A	}
1271674SN/A
1281674SN/A#ifndef __FreeBSD__
1291674SN/A	/*
1301674SN/A	 * GEMDOS knows nothing (yet) about win95
1311674SN/A	 */
1321674SN/A	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
13317519Sapetcher		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
13417519Sapetcher#endif
1351674SN/A
1361674SN/A	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1371674SN/A		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
1381674SN/A	else if (!(pmp->pm_flags &
1391674SN/A	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
1401674SN/A		struct vnode *rootvp;
1411674SN/A
1421674SN/A		/*
1431674SN/A		 * Try to divine whether to support Win'95 long filenames
1441674SN/A		 */
1451674SN/A		if (FAT32(pmp))
1461674SN/A			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
1471674SN/A		else {
1481674SN/A			if ((error = msdosfs_root(mp, &rootvp)) != 0)
1491674SN/A				return error;
1501674SN/A			pmp->pm_flags |= findwin95(VTODE(rootvp))
1511674SN/A				? MSDOSFSMNT_LONGNAME
1521674SN/A					: MSDOSFSMNT_SHORTNAME;
1531674SN/A			vput(rootvp);
1541674SN/A		}
1551674SN/A	}
1561674SN/A	return 0;
1571674SN/A}
1581674SN/A
1591674SN/A#ifndef __FreeBSD__
1601674SN/Aint
1611674SN/Amsdosfs_mountroot()
1621674SN/A{
1631674SN/A	register struct mount *mp;
1641674SN/A	struct proc *p = curproc;	/* XXX */
1651674SN/A	size_t size;
16617519Sapetcher	int error;
1671674SN/A	struct msdosfs_args args;
1681674SN/A
1691674SN/A	if (root_device->dv_class != DV_DISK)
1701674SN/A		return (ENODEV);
1711674SN/A
1721674SN/A	/*
1731674SN/A	 * Get vnodes for swapdev and rootdev.
1741674SN/A	 */
1751674SN/A	if (bdevvp(rootdev, &rootvp))
1761674SN/A		panic("msdosfs_mountroot: can't setup rootvp");
1771674SN/A
1781674SN/A	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
1791674SN/A	mp->mnt_op = &msdosfs_vfsops;
1801674SN/A	mp->mnt_flag = 0;
18117519Sapetcher	LIST_INIT(&mp->mnt_vnodelist);
1821674SN/A
18317519Sapetcher	args.flags = 0;
1841674SN/A	args.uid = 0;
1851674SN/A	args.gid = 0;
1861674SN/A	args.mask = 0777;
1871674SN/A
1881674SN/A	if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
1891674SN/A		free(mp, M_MOUNT);
1901674SN/A		return (error);
1911674SN/A	}
1921674SN/A
1931674SN/A	if ((error = update_mp(mp, &args)) != 0) {
1941674SN/A		(void)msdosfs_unmount(mp, 0, p);
1951674SN/A		free(mp, M_MOUNT);
1961674SN/A		return (error);
1971674SN/A	}
1981674SN/A
1991674SN/A	if ((error = vfs_lock(mp)) != 0) {
2001674SN/A		(void)msdosfs_unmount(mp, 0, p);
2011674SN/A		free(mp, M_MOUNT);
2021674SN/A		return (error);
2031674SN/A	}
2041674SN/A
2051674SN/A	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
2061674SN/A	mp->mnt_vnodecovered = NULLVP;
2071674SN/A	(void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
2081674SN/A	    &size);
2091674SN/A	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
2101674SN/A	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
2111674SN/A	    &size);
2121674SN/A	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
2131674SN/A	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
2141674SN/A	vfs_unlock(mp);
2151674SN/A	return (0);
2161674SN/A}
2171674SN/A#endif
2181674SN/A
2191674SN/A/*
2201674SN/A * mp - path - addr in user space of mount point (ie /usr or whatever)
2211674SN/A * data - addr in user space of mount params including the name of the block
2221674SN/A * special file to treat as a filesystem.
2231674SN/A */
2241674SN/Astatic int
2251674SN/Amsdosfs_mount(mp, path, data, ndp, p)
2261674SN/A	struct mount *mp;
2271674SN/A	char *path;
2281674SN/A	caddr_t data;
2291674SN/A	struct nameidata *ndp;
2301674SN/A	struct proc *p;
2311674SN/A{
2321674SN/A	struct vnode *devvp;	  /* vnode for blk device to mount */
2331674SN/A	struct msdosfs_args args; /* will hold data from mount request */
2341674SN/A	/* msdosfs specific mount control block */
2351674SN/A	struct msdosfsmount *pmp = NULL;
2361674SN/A	size_t size;
2371674SN/A	int error, flags;
2381674SN/A	mode_t accessmode;
2391674SN/A
2401674SN/A	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
2411674SN/A	if (error)
2421674SN/A		return (error);
2431674SN/A	if (args.magic != MSDOSFS_ARGSMAGIC)
2441674SN/A		args.flags = 0;
2451674SN/A	/*
2461674SN/A	 * If updating, check whether changing from read-only to
2471674SN/A	 * read/write; if there is no device name, that's all we do.
2481674SN/A	 */
2491674SN/A	if (mp->mnt_flag & MNT_UPDATE) {
2501674SN/A		pmp = VFSTOMSDOSFS(mp);
2511674SN/A		error = 0;
2521674SN/A		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
2531674SN/A			flags = WRITECLOSE;
2541674SN/A			if (mp->mnt_flag & MNT_FORCE)
2551674SN/A				flags |= FORCECLOSE;
2561674SN/A			error = vflush(mp, NULLVP, flags);
2571674SN/A		}
2581674SN/A		if (!error && (mp->mnt_flag & MNT_RELOAD))
2591674SN/A			/* not yet implemented */
2601674SN/A			error = EOPNOTSUPP;
2611674SN/A		if (error)
2621674SN/A			return (error);
2631674SN/A		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
2641674SN/A			/*
2651674SN/A			 * If upgrade to read-write by non-root, then verify
2661674SN/A			 * that user has necessary permissions on the device.
2671674SN/A			 */
2681674SN/A			if (p->p_ucred->cr_uid != 0) {
2691674SN/A				devvp = pmp->pm_devvp;
2701674SN/A				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
2711674SN/A				error = VOP_ACCESS(devvp, VREAD | VWRITE,
2721674SN/A						   p->p_ucred, p);
2731674SN/A				if (error) {
2741674SN/A					VOP_UNLOCK(devvp, 0, p);
2751674SN/A					return (error);
2761674SN/A				}
2771674SN/A				VOP_UNLOCK(devvp, 0, p);
2781674SN/A			}
2791674SN/A			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
2801674SN/A		}
2811674SN/A		if (args.fspec == 0) {
2821674SN/A#ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
2831674SN/A			if (args.flags & MSDOSFSMNT_MNTOPT) {
2841674SN/A				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
2851674SN/A				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
2861674SN/A				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
2871674SN/A					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
2881674SN/A			}
2891674SN/A#endif
2901674SN/A			/*
2911674SN/A			 * Process export requests.
2921674SN/A			 */
2931674SN/A			return (vfs_export(mp, &pmp->pm_export, &args.export));
2941674SN/A		}
2951674SN/A	}
2961674SN/A	/*
2971674SN/A	 * Not an update, or updating the name: look up the name
2981674SN/A	 * and verify that it refers to a sensible block device.
2991674SN/A	 */
3001674SN/A	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
3011674SN/A	error = namei(ndp);
3021674SN/A	if (error)
3031674SN/A		return (error);
3041674SN/A	devvp = ndp->ni_vp;
3051674SN/A	NDFREE(ndp, NDF_ONLY_PNBUF);
3061674SN/A
3071674SN/A	if (!vn_isdisk(devvp, &error)) {
3081674SN/A		vrele(devvp);
3091674SN/A		return (error);
3101674SN/A	}
3111674SN/A	/*
3121674SN/A	 * If mount by non-root, then verify that user has necessary
3131674SN/A	 * permissions on the device.
3141674SN/A	 */
31517519Sapetcher	if (p->p_ucred->cr_uid != 0) {
31617519Sapetcher		accessmode = VREAD;
3171674SN/A		if ((mp->mnt_flag & MNT_RDONLY) == 0)
3181674SN/A			accessmode |= VWRITE;
3191674SN/A		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
3201674SN/A		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
3211674SN/A		if (error) {
3221674SN/A			vput(devvp);
3231674SN/A			return (error);
3241674SN/A		}
3251674SN/A		VOP_UNLOCK(devvp, 0, p);
3261674SN/A	}
3271674SN/A	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
3281674SN/A		error = mountmsdosfs(devvp, mp, p, &args);
3291674SN/A#ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
3301674SN/A		pmp = VFSTOMSDOSFS(mp);
3311674SN/A#endif
3321674SN/A	} else {
3331674SN/A		if (devvp != pmp->pm_devvp)
3341674SN/A			error = EINVAL;	/* XXX needs translation */
3351674SN/A		else
3361674SN/A			vrele(devvp);
3371674SN/A	}
3381674SN/A	if (error) {
3391674SN/A		vrele(devvp);
3401674SN/A		return (error);
3411674SN/A	}
3421674SN/A
3431674SN/A	error = update_mp(mp, &args);
3441674SN/A	if (error) {
3451674SN/A		msdosfs_unmount(mp, MNT_FORCE, p);
3461674SN/A		return error;
3471674SN/A	}
3481674SN/A
3491674SN/A	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
3501674SN/A	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
3511674SN/A	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
3521674SN/A	    &size);
35317519Sapetcher	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
3541674SN/A	(void) msdosfs_statfs(mp, &mp->mnt_stat, p);
35517519Sapetcher#ifdef MSDOSFS_DEBUG
3561674SN/A	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
3571674SN/A#endif
3581674SN/A	return (0);
3591674SN/A}
3601674SN/A
3611674SN/Astatic int
3621674SN/Amountmsdosfs(devvp, mp, p, argp)
363	struct vnode *devvp;
364	struct mount *mp;
365	struct proc *p;
366	struct msdosfs_args *argp;
367{
368	struct msdosfsmount *pmp;
369	struct buf *bp;
370	dev_t dev = devvp->v_rdev;
371#ifndef __FreeBSD__
372	struct partinfo dpart;
373	int bsize = 0, dtype = 0, tmp;
374#endif
375	union bootsector *bsp;
376	struct byte_bpb33 *b33;
377	struct byte_bpb50 *b50;
378	struct byte_bpb710 *b710;
379	u_int8_t SecPerClust;
380	u_long clusters;
381	int	ronly, error;
382
383	/*
384	 * Disallow multiple mounts of the same device.
385	 * Disallow mounting of a device that is currently in use
386	 * (except for root, which might share swap device for miniroot).
387	 * Flush out any old buffers remaining from a previous use.
388	 */
389	error = vfs_mountedon(devvp);
390	if (error)
391		return (error);
392	if (vcount(devvp) > 1 && devvp != rootvp)
393		return (EBUSY);
394	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
395	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
396	VOP_UNLOCK(devvp, 0, p);
397	if (error)
398		return (error);
399
400	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
401	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
402	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
403	VOP_UNLOCK(devvp, 0, p);
404	if (error)
405		return (error);
406
407	bp  = NULL; /* both used in error_exit */
408	pmp = NULL;
409
410#ifndef __FreeBSD__
411	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
412		/*
413	 	 * We need the disklabel to calculate the size of a FAT entry
414		 * later on. Also make sure the partition contains a filesystem
415		 * of type FS_MSDOS. This doesn't work for floppies, so we have
416		 * to check for them too.
417	 	 *
418	 	 * At least some parts of the msdos fs driver seem to assume
419		 * that the size of a disk block will always be 512 bytes.
420		 * Let's check it...
421		 */
422		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
423				  FREAD, NOCRED, p);
424		if (error)
425			goto error_exit;
426		tmp   = dpart.part->p_fstype;
427		dtype = dpart.disklab->d_type;
428		bsize = dpart.disklab->d_secsize;
429		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
430			error = EINVAL;
431			goto error_exit;
432		}
433	}
434#endif
435
436	/*
437	 * Read the boot sector of the filesystem, and then check the
438	 * boot signature.  If not a dos boot sector then error out.
439	 *
440	 * NOTE: 2048 is a maximum sector size in current...
441	 */
442	error = bread(devvp, 0, 2048, NOCRED, &bp);
443	if (error)
444		goto error_exit;
445	bp->b_flags |= B_AGE;
446	bsp = (union bootsector *)bp->b_data;
447	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
448	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
449	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
450
451#ifndef __FreeBSD__
452	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
453#endif
454#ifndef MSDOSFS_NOCHECKSIG
455		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
456		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
457			error = EINVAL;
458			goto error_exit;
459		}
460#endif
461#ifndef __FreeBSD__
462	}
463#endif
464
465	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
466	pmp->pm_mountp = mp;
467
468	/*
469	 * Compute several useful quantities from the bpb in the
470	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
471	 * the fields that are different between dos 5 and dos 3.3.
472	 */
473	SecPerClust = b50->bpbSecPerClust;
474	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
475	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
476	pmp->pm_FATs = b50->bpbFATs;
477	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
478	pmp->pm_Sectors = getushort(b50->bpbSectors);
479	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
480	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
481	pmp->pm_Heads = getushort(b50->bpbHeads);
482	pmp->pm_Media = b50->bpbMedia;
483
484	/* calculate the ratio of sector size to DEV_BSIZE */
485	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
486
487#ifndef __FreeBSD__
488	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
489#endif
490		/* XXX - We should probably check more values here */
491		if (!pmp->pm_BytesPerSec || !SecPerClust
492			|| !pmp->pm_Heads || pmp->pm_Heads > 255
493#ifdef PC98
494	    		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
495#else
496			|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
497#endif
498			error = EINVAL;
499			goto error_exit;
500		}
501#ifndef __FreeBSD__
502	}
503#endif
504
505	if (pmp->pm_Sectors == 0) {
506		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
507		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
508	} else {
509		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
510		pmp->pm_HugeSectors = pmp->pm_Sectors;
511	}
512	if (pmp->pm_HugeSectors > 0xffffffff /
513	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 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		printf("mountmsdosfs(): disk too big, sorry\n");
521		goto error_exit;
522	}
523
524	if (pmp->pm_RootDirEnts == 0) {
525		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
526		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
527		    || pmp->pm_Sectors
528		    || pmp->pm_FATsecs
529		    || getushort(b710->bpbFSVers)) {
530			error = EINVAL;
531			printf("mountmsdosfs(): bad FAT32 filesystem\n");
532			goto error_exit;
533		}
534		pmp->pm_fatmask = FAT32_MASK;
535		pmp->pm_fatmult = 4;
536		pmp->pm_fatdiv = 1;
537		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
538		if (getushort(b710->bpbExtFlags) & FATMIRROR)
539			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
540		else
541			pmp->pm_flags |= MSDOSFS_FATMIRROR;
542	} else
543		pmp->pm_flags |= MSDOSFS_FATMIRROR;
544
545	/*
546	 * Check a few values (could do some more):
547	 * - logical sector size: power of 2, >= block size
548	 * - sectors per cluster: power of 2, >= 1
549	 * - number of sectors:   >= 1, <= size of partition
550	 */
551	if ( (SecPerClust == 0)
552	  || (SecPerClust & (SecPerClust - 1))
553	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
554	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
555	  || (pmp->pm_HugeSectors == 0)
556	) {
557		error = EINVAL;
558		goto error_exit;
559	}
560
561	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
562	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
563	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
564	SecPerClust         *= pmp->pm_BlkPerSec;
565
566	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
567
568	if (FAT32(pmp)) {
569		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
570		pmp->pm_firstcluster = pmp->pm_fatblk
571			+ (pmp->pm_FATs * pmp->pm_FATsecs);
572		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
573	} else {
574		pmp->pm_rootdirblk = pmp->pm_fatblk +
575			(pmp->pm_FATs * pmp->pm_FATsecs);
576		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
577				       + DEV_BSIZE - 1)
578			/ DEV_BSIZE; /* in blocks */
579		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
580	}
581
582	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
583	    SecPerClust + 1;
584	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
585
586#ifndef __FreeBSD__
587	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
588		if ((pmp->pm_maxcluster <= (0xff0 - 2))
589		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
590		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
591		    ) {
592			pmp->pm_fatmask = FAT12_MASK;
593			pmp->pm_fatmult = 3;
594			pmp->pm_fatdiv = 2;
595		} else {
596			pmp->pm_fatmask = FAT16_MASK;
597			pmp->pm_fatmult = 2;
598			pmp->pm_fatdiv = 1;
599		}
600	} else
601#endif
602	if (pmp->pm_fatmask == 0) {
603		if (pmp->pm_maxcluster
604		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
605			/*
606			 * This will usually be a floppy disk. This size makes
607			 * sure that one fat entry will not be split across
608			 * multiple blocks.
609			 */
610			pmp->pm_fatmask = FAT12_MASK;
611			pmp->pm_fatmult = 3;
612			pmp->pm_fatdiv = 2;
613		} else {
614			pmp->pm_fatmask = FAT16_MASK;
615			pmp->pm_fatmult = 2;
616			pmp->pm_fatdiv = 1;
617		}
618	}
619
620	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
621	if (pmp->pm_maxcluster >= clusters) {
622		printf("Warning: number of clusters (%ld) exceeds FAT "
623		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
624		pmp->pm_maxcluster = clusters - 1;
625	}
626
627
628	if (FAT12(pmp))
629		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
630	else
631		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
632
633	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
634	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
635
636	/*
637	 * Compute mask and shift value for isolating cluster relative byte
638	 * offsets and cluster numbers from a file offset.
639	 */
640	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
641	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
642	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
643
644	/*
645	 * Check for valid cluster size
646	 * must be a power of 2
647	 */
648	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
649		error = EINVAL;
650		goto error_exit;
651	}
652
653	/*
654	 * Release the bootsector buffer.
655	 */
656	brelse(bp);
657	bp = NULL;
658
659	/*
660	 * Check FSInfo.
661	 */
662	if (pmp->pm_fsinfo) {
663		struct fsinfo *fp;
664
665		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
666		    NOCRED, &bp)) != 0)
667			goto error_exit;
668		fp = (struct fsinfo *)bp->b_data;
669		if (!bcmp(fp->fsisig1, "RRaA", 4)
670		    && !bcmp(fp->fsisig2, "rrAa", 4)
671		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
672		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
673			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
674		else
675			pmp->pm_fsinfo = 0;
676		brelse(bp);
677		bp = NULL;
678	}
679
680	/*
681	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
682	 */
683
684	/*
685	 * Allocate memory for the bitmap of allocated clusters, and then
686	 * fill it in.
687	 */
688	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
689				   / N_INUSEBITS)
690				  * sizeof(*pmp->pm_inusemap),
691				  M_MSDOSFSFAT, M_WAITOK);
692
693	/*
694	 * fillinusemap() needs pm_devvp.
695	 */
696	pmp->pm_dev = dev;
697	pmp->pm_devvp = devvp;
698
699	/*
700	 * Have the inuse map filled in.
701	 */
702	if ((error = fillinusemap(pmp)) != 0)
703		goto error_exit;
704
705	/*
706	 * If they want fat updates to be synchronous then let them suffer
707	 * the performance degradation in exchange for the on disk copy of
708	 * the fat being correct just about all the time.  I suppose this
709	 * would be a good thing to turn on if the kernel is still flakey.
710	 */
711	if (mp->mnt_flag & MNT_SYNCHRONOUS)
712		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
713
714	/*
715	 * Finish up.
716	 */
717	if (ronly)
718		pmp->pm_flags |= MSDOSFSMNT_RONLY;
719	else
720		pmp->pm_fmod = 1;
721	mp->mnt_data = (qaddr_t) pmp;
722	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
723	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
724	mp->mnt_flag |= MNT_LOCAL;
725	devvp->v_rdev->si_mountpoint = mp;
726
727	return 0;
728
729error_exit:
730	if (bp)
731		brelse(bp);
732	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
733	if (pmp) {
734		if (pmp->pm_inusemap)
735			free(pmp->pm_inusemap, M_MSDOSFSFAT);
736		free(pmp, M_MSDOSFSMNT);
737		mp->mnt_data = (qaddr_t)0;
738	}
739	return (error);
740}
741
742/*
743 * Unmount the filesystem described by mp.
744 */
745static int
746msdosfs_unmount(mp, mntflags, p)
747	struct mount *mp;
748	int mntflags;
749	struct proc *p;
750{
751	struct msdosfsmount *pmp;
752	int error, flags;
753
754	flags = 0;
755	if (mntflags & MNT_FORCE)
756		flags |= FORCECLOSE;
757	error = vflush(mp, NULLVP, flags);
758	if (error)
759		return error;
760	pmp = VFSTOMSDOSFS(mp);
761	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
762#ifdef MSDOSFS_DEBUG
763	{
764		struct vnode *vp = pmp->pm_devvp;
765
766		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
767		printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
768		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
769		printf("id %lu, mount %p, op %p\n",
770		    vp->v_id, vp->v_mount, vp->v_op);
771		printf("freef %p, freeb %p, mount %p\n",
772		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
773		    vp->v_mount);
774		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
775		    TAILQ_FIRST(&vp->v_cleanblkhd),
776		    TAILQ_FIRST(&vp->v_dirtyblkhd),
777		    vp->v_numoutput, vp->v_type);
778		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
779		    vp->v_socket, vp->v_tag,
780		    ((u_int *)vp->v_data)[0],
781		    ((u_int *)vp->v_data)[1]);
782	}
783#endif
784	error = VOP_CLOSE(pmp->pm_devvp,
785		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
786		    NOCRED, p);
787	vrele(pmp->pm_devvp);
788	free(pmp->pm_inusemap, M_MSDOSFSFAT);
789	free(pmp, M_MSDOSFSMNT);
790	mp->mnt_data = (qaddr_t)0;
791	mp->mnt_flag &= ~MNT_LOCAL;
792	return (error);
793}
794
795static int
796msdosfs_root(mp, vpp)
797	struct mount *mp;
798	struct vnode **vpp;
799{
800	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
801	struct denode *ndep;
802	int error;
803
804#ifdef MSDOSFS_DEBUG
805	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
806#endif
807	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
808	if (error)
809		return (error);
810	*vpp = DETOV(ndep);
811	return (0);
812}
813
814static int
815msdosfs_statfs(mp, sbp, p)
816	struct mount *mp;
817	struct statfs *sbp;
818	struct proc *p;
819{
820	struct msdosfsmount *pmp;
821
822	pmp = VFSTOMSDOSFS(mp);
823	sbp->f_bsize = pmp->pm_bpcluster;
824	sbp->f_iosize = pmp->pm_bpcluster;
825	sbp->f_blocks = pmp->pm_maxcluster + 1;
826	sbp->f_bfree = pmp->pm_freeclustercount;
827	sbp->f_bavail = pmp->pm_freeclustercount;
828	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
829	sbp->f_ffree = 0;	/* what to put in here? */
830	if (sbp != &mp->mnt_stat) {
831		sbp->f_type = mp->mnt_vfc->vfc_typenum;
832		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
833		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
834	}
835	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
836	return (0);
837}
838
839static int
840msdosfs_sync(mp, waitfor, cred, p)
841	struct mount *mp;
842	int waitfor;
843	struct ucred *cred;
844	struct proc *p;
845{
846	struct vnode *vp, *nvp;
847	struct denode *dep;
848	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
849	int error, allerror = 0;
850
851	/*
852	 * If we ever switch to not updating all of the fats all the time,
853	 * this would be the place to update them from the first one.
854	 */
855	if (pmp->pm_fmod != 0) {
856		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
857			panic("msdosfs_sync: rofs mod");
858		else {
859			/* update fats here */
860		}
861	}
862	/*
863	 * Write back each (modified) denode.
864	 */
865	mtx_enter(&mntvnode_mtx, MTX_DEF);
866loop:
867	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
868		/*
869		 * If the vnode that we are about to sync is no longer
870		 * associated with this mount point, start over.
871		 */
872		if (vp->v_mount != mp)
873			goto loop;
874
875		mtx_enter(&vp->v_interlock, MTX_DEF);
876		nvp = LIST_NEXT(vp, v_mntvnodes);
877		dep = VTODE(vp);
878		if (vp->v_type == VNON ||
879		    ((dep->de_flag &
880		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
881		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
882			mtx_exit(&vp->v_interlock, MTX_DEF);
883			continue;
884		}
885		mtx_exit(&mntvnode_mtx, MTX_DEF);
886		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
887		if (error) {
888			mtx_enter(&mntvnode_mtx, MTX_DEF);
889			if (error == ENOENT)
890				goto loop;
891			continue;
892		}
893		error = VOP_FSYNC(vp, cred, waitfor, p);
894		if (error)
895			allerror = error;
896		VOP_UNLOCK(vp, 0, p);
897		vrele(vp);
898		mtx_enter(&mntvnode_mtx, MTX_DEF);
899	}
900	mtx_exit(&mntvnode_mtx, MTX_DEF);
901
902	/*
903	 * Flush filesystem control info.
904	 */
905	if (waitfor != MNT_LAZY) {
906		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
907		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
908		if (error)
909			allerror = error;
910		VOP_UNLOCK(pmp->pm_devvp, 0, p);
911	}
912	return (allerror);
913}
914
915static int
916msdosfs_fhtovp(mp, fhp, vpp)
917	struct mount *mp;
918	struct fid *fhp;
919	struct vnode **vpp;
920{
921	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
922	struct defid *defhp = (struct defid *) fhp;
923	struct denode *dep;
924	int error;
925
926	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
927	if (error) {
928		*vpp = NULLVP;
929		return (error);
930	}
931	*vpp = DETOV(dep);
932	return (0);
933}
934
935static int
936msdosfs_checkexp(mp, nam,  exflagsp, credanonp)
937	struct mount *mp;
938	struct sockaddr *nam;
939	int *exflagsp;
940	struct ucred **credanonp;
941{
942	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
943	struct netcred *np;
944
945	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
946	if (np == NULL)
947		return (EACCES);
948	*exflagsp = np->netc_exflags;
949	*credanonp = &np->netc_anon;
950	return (0);
951}
952
953static int
954msdosfs_vptofh(vp, fhp)
955	struct vnode *vp;
956	struct fid *fhp;
957{
958	struct denode *dep;
959	struct defid *defhp;
960
961	dep = VTODE(vp);
962	defhp = (struct defid *)fhp;
963	defhp->defid_len = sizeof(struct defid);
964	defhp->defid_dirclust = dep->de_dirclust;
965	defhp->defid_dirofs = dep->de_diroffset;
966	/* defhp->defid_gen = dep->de_gen; */
967	return (0);
968}
969
970static struct vfsops msdosfs_vfsops = {
971	msdosfs_mount,
972	vfs_stdstart,
973	msdosfs_unmount,
974	msdosfs_root,
975	vfs_stdquotactl,
976	msdosfs_statfs,
977	msdosfs_sync,
978	vfs_stdvget,
979	msdosfs_fhtovp,
980	msdosfs_checkexp,
981	msdosfs_vptofh,
982	msdosfs_init,
983	msdosfs_uninit,
984	vfs_stdextattrctl,
985};
986
987VFS_SET(msdosfs_vfsops, msdos, 0);
988