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