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