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