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