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