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