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