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