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