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