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