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