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