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