msdosfs_vfsops.c revision 33768
1/*	$Id: msdosfs_vfsops.c,v 1.27 1998/02/23 14:57:50 kato Exp $ */
2/*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/namei.h>
54#include <sys/proc.h>
55#include <sys/kernel.h>
56#include <sys/vnode.h>
57#include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
58#include <sys/mount.h>
59#include <sys/buf.h>
60#include <sys/fcntl.h>
61#include <sys/malloc.h>
62#include <sys/stat.h> 				/* defines ALLPERMS */
63
64#include <msdosfs/bpb.h>
65#include <msdosfs/bootsect.h>
66#include <msdosfs/direntry.h>
67#include <msdosfs/denode.h>
68#include <msdosfs/msdosfsmount.h>
69#include <msdosfs/fat.h>
70
71MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
72static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
73
74static int	update_mp __P((struct mount *mp, struct msdosfs_args *argp));
75static int	mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
76				  struct proc *p, struct msdosfs_args *argp));
77static int	msdosfs_fhtovp __P((struct mount *, struct fid *,
78				    struct sockaddr *, struct vnode **, int *,
79				    struct ucred **));
80static int	msdosfs_mount __P((struct mount *, char *, caddr_t,
81				   struct nameidata *, struct proc *));
82static int	msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
83				      struct proc *));
84static int	msdosfs_root __P((struct mount *, struct vnode **));
85static int	msdosfs_start __P((struct mount *, int, struct proc *));
86static int	msdosfs_statfs __P((struct mount *, struct statfs *,
87				    struct proc *));
88static int	msdosfs_sync __P((struct mount *, int, struct ucred *,
89				  struct proc *));
90static int	msdosfs_unmount __P((struct mount *, int, struct proc *));
91static int	msdosfs_vget __P((struct mount *mp, ino_t ino,
92				  struct vnode **vpp));
93static int	msdosfs_vptofh __P((struct vnode *, struct fid *));
94
95static int
96update_mp(mp, argp)
97	struct mount *mp;
98	struct msdosfs_args *argp;
99{
100	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
101	int error;
102
103	pmp->pm_gid = argp->gid;
104	pmp->pm_uid = argp->uid;
105	pmp->pm_mask = argp->mask & ALLPERMS;
106	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
107	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
108		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
109		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
110		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
111	}
112	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
113		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
114		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
115	}
116
117#ifndef __FreeBSD__
118	/*
119	 * GEMDOS knows nothing (yet) about win95
120	 */
121	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
122		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
123#endif
124
125	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
126		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
127	else if (!(pmp->pm_flags &
128	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
129		struct vnode *rootvp;
130
131		/*
132		 * Try to divine whether to support Win'95 long filenames
133		 */
134		if (FAT32(pmp))
135			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
136		else {
137			if ((error = msdosfs_root(mp, &rootvp)) != 0)
138				return error;
139			pmp->pm_flags |= findwin95(VTODE(rootvp))
140				? MSDOSFSMNT_LONGNAME
141					: MSDOSFSMNT_SHORTNAME;
142			vput(rootvp);
143		}
144	}
145	return 0;
146}
147
148#ifndef __FreeBSD__
149int
150msdosfs_mountroot()
151{
152	register struct mount *mp;
153	struct proc *p = curproc;	/* XXX */
154	size_t size;
155	int error;
156	struct msdosfs_args args;
157
158	if (root_device->dv_class != DV_DISK)
159		return (ENODEV);
160
161	/*
162	 * Get vnodes for swapdev and rootdev.
163	 */
164	if (bdevvp(rootdev, &rootvp))
165		panic("msdosfs_mountroot: can't setup rootvp");
166
167	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
168	bzero((char *)mp, (u_long)sizeof(struct mount));
169	mp->mnt_op = &msdosfs_vfsops;
170	mp->mnt_flag = 0;
171	LIST_INIT(&mp->mnt_vnodelist);
172
173	args.flags = 0;
174	args.uid = 0;
175	args.gid = 0;
176	args.mask = 0777;
177
178	if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
179		free(mp, M_MOUNT);
180		return (error);
181	}
182
183	if ((error = update_mp(mp, &args)) != 0) {
184		(void)msdosfs_unmount(mp, 0, p);
185		free(mp, M_MOUNT);
186		return (error);
187	}
188
189	if ((error = vfs_lock(mp)) != 0) {
190		(void)msdosfs_unmount(mp, 0, p);
191		free(mp, M_MOUNT);
192		return (error);
193	}
194
195	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
196	mp->mnt_vnodecovered = NULLVP;
197	(void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
198	    &size);
199	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
200	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
201	    &size);
202	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
203	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
204	vfs_unlock(mp);
205	return (0);
206}
207#endif
208
209/*
210 * mp - path - addr in user space of mount point (ie /usr or whatever)
211 * data - addr in user space of mount params including the name of the block
212 * special file to treat as a filesystem.
213 */
214static int
215msdosfs_mount(mp, path, data, ndp, p)
216	struct mount *mp;
217	char *path;
218	caddr_t data;
219	struct nameidata *ndp;
220	struct proc *p;
221{
222	struct vnode *devvp;	  /* vnode for blk device to mount */
223	struct msdosfs_args args; /* will hold data from mount request */
224	/* msdosfs specific mount control block */
225	struct msdosfsmount *pmp = NULL;
226	size_t size;
227	int error, flags;
228	mode_t accessmode;
229
230	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
231	if (error)
232		return (error);
233	if (args.magic != MSDOSFS_ARGSMAGIC) {
234		printf("Old mount_msdosfs, flags=%d\n", args.flags);
235		args.flags = 0;
236	}
237	/*
238	 * If updating, check whether changing from read-only to
239	 * read/write; if there is no device name, that's all we do.
240	 */
241	if (mp->mnt_flag & MNT_UPDATE) {
242		pmp = VFSTOMSDOSFS(mp);
243		error = 0;
244		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
245			flags = WRITECLOSE;
246			if (mp->mnt_flag & MNT_FORCE)
247				flags |= FORCECLOSE;
248			error = vflush(mp, NULLVP, flags);
249		}
250		if (!error && (mp->mnt_flag & MNT_RELOAD))
251			/* not yet implemented */
252			error = EOPNOTSUPP;
253		if (error)
254			return (error);
255		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
256			/*
257			 * If upgrade to read-write by non-root, then verify
258			 * that user has necessary permissions on the device.
259			 */
260			if (p->p_ucred->cr_uid != 0) {
261				devvp = pmp->pm_devvp;
262				vn_lock(devvp, LK_EXCLUSIVE, p);
263				error = VOP_ACCESS(devvp, VREAD | VWRITE,
264						   p->p_ucred, p);
265				if (error) {
266					VOP_UNLOCK(devvp, 0, p);
267					return (error);
268				}
269				VOP_UNLOCK(devvp, 0, p);
270			}
271			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
272		}
273		if (args.fspec == 0) {
274#ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
275			if (args.flags & MSDOSFSMNT_MNTOPT) {
276				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
277				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
278				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
279					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
280			}
281#endif
282			/*
283			 * Process export requests.
284			 */
285			return (vfs_export(mp, &pmp->pm_export, &args.export));
286		}
287	}
288	/*
289	 * Not an update, or updating the name: look up the name
290	 * and verify that it refers to a sensible block device.
291	 */
292	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
293	error = namei(ndp);
294	if (error)
295		return (error);
296	devvp = ndp->ni_vp;
297
298	if (devvp->v_type != VBLK) {
299		vrele(devvp);
300		return (ENOTBLK);
301	}
302	if (major(devvp->v_rdev) >= nblkdev) {
303		vrele(devvp);
304		return (ENXIO);
305	}
306	/*
307	 * If mount by non-root, then verify that user has necessary
308	 * permissions on the device.
309	 */
310	if (p->p_ucred->cr_uid != 0) {
311		accessmode = VREAD;
312		if ((mp->mnt_flag & MNT_RDONLY) == 0)
313			accessmode |= VWRITE;
314		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
315		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
316		if (error) {
317			vput(devvp);
318			return (error);
319		}
320		VOP_UNLOCK(devvp, 0, p);
321	}
322	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
323		error = mountmsdosfs(devvp, mp, p, &args);
324#ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
325		pmp = VFSTOMSDOSFS(mp);
326#endif
327	} else {
328		if (devvp != pmp->pm_devvp)
329			error = EINVAL;	/* XXX needs translation */
330		else
331			vrele(devvp);
332	}
333	if (error) {
334		vrele(devvp);
335		return (error);
336	}
337
338	error = update_mp(mp, &args);
339	if (error) {
340		msdosfs_unmount(mp, MNT_FORCE, p);
341		return error;
342	}
343
344	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
345	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
346	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
347	    &size);
348	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
349	(void) msdosfs_statfs(mp, &mp->mnt_stat, p);
350#ifdef MSDOSFS_DEBUG
351	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
352#endif
353	return (0);
354}
355
356static int
357mountmsdosfs(devvp, mp, p, argp)
358	struct vnode *devvp;
359	struct mount *mp;
360	struct proc *p;
361	struct msdosfs_args *argp;
362{
363	struct msdosfsmount *pmp;
364	struct buf *bp;
365	dev_t dev = devvp->v_rdev;
366#ifndef __FreeBSD__
367	struct partinfo dpart;
368#endif
369	union bootsector *bsp;
370	struct byte_bpb33 *b33;
371	struct byte_bpb50 *b50;
372#ifdef	PC98
373	u_int	pc98_wrk;
374	u_int	Phy_Sector_Size;
375#endif
376	struct byte_bpb710 *b710;
377	u_int8_t SecPerClust;
378	int	ronly, error;
379	int	bsize = 0, dtype = 0, tmp;
380
381	/*
382	 * Disallow multiple mounts of the same device.
383	 * Disallow mounting of a device that is currently in use
384	 * (except for root, which might share swap device for miniroot).
385	 * Flush out any old buffers remaining from a previous use.
386	 */
387	error = vfs_mountedon(devvp);
388	if (error)
389		return (error);
390	if (vcount(devvp) > 1 && devvp != rootvp)
391		return (EBUSY);
392	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
393	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
394	VOP_UNLOCK(devvp, 0, p);
395	if (error)
396		return (error);
397
398	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
399	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
400	if (error)
401		return (error);
402
403	bp  = NULL; /* both used in error_exit */
404	pmp = NULL;
405
406#ifndef __FreeBSD__
407	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
408		/*
409	 	 * We need the disklabel to calculate the size of a FAT entry
410		 * later on. Also make sure the partition contains a filesystem
411		 * of type FS_MSDOS. This doesn't work for floppies, so we have
412		 * to check for them too.
413	 	 *
414	 	 * At least some parts of the msdos fs driver seem to assume
415		 * that the size of a disk block will always be 512 bytes.
416		 * Let's check it...
417		 */
418		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
419				  FREAD, NOCRED, p);
420		if (error)
421			goto error_exit;
422		tmp   = dpart.part->p_fstype;
423		dtype = dpart.disklab->d_type;
424		bsize = dpart.disklab->d_secsize;
425		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
426			error = EINVAL;
427			goto error_exit;
428		}
429	}
430#endif
431
432	/*
433	 * Read the boot sector of the filesystem, and then check the
434	 * boot signature.  If not a dos boot sector then error out.
435	 */
436#ifdef	PC98
437	devvp->v_flag &= 0xffff;
438	error = bread(devvp, 0, 1024, NOCRED, &bp);
439#else
440	error = bread(devvp, 0, 512, NOCRED, &bp);
441#endif
442	if (error)
443		goto error_exit;
444	bp->b_flags |= B_AGE;
445	bsp = (union bootsector *)bp->b_data;
446	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
447	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
448	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
449
450#ifndef __FreeBSD__
451	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
452#endif
453#ifdef PC98
454		if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0
455		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1)
456		    && (bsp->bs50.bsBootSectSig0 != 0       /* PC98 DOS 3.3x */
457		    || bsp->bs50.bsBootSectSig1 != 0)
458		    && (bsp->bs50.bsBootSectSig0 != 0x90    /* PC98 DOS 5.0  */
459		    || bsp->bs50.bsBootSectSig1 != 0x3d)
460		    && (bsp->bs50.bsBootSectSig0 != 0x46    /* PC98 DOS 3.3B */
461		    || bsp->bs50.bsBootSectSig1 != 0xfa)) {
462#else
463		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
464		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
465#endif
466			error = EINVAL;
467			goto error_exit;
468		}
469#ifndef __FreeBSD__
470	}
471#endif
472
473	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
474	bzero((caddr_t)pmp, sizeof *pmp);
475	pmp->pm_mountp = mp;
476
477	/*
478	 * Compute several useful quantities from the bpb in the
479	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
480	 * the fields that are different between dos 5 and dos 3.3.
481	 */
482	SecPerClust = b50->bpbSecPerClust;
483	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
484	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
485	pmp->pm_FATs = b50->bpbFATs;
486	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
487	pmp->pm_Sectors = getushort(b50->bpbSectors);
488	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
489	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
490	pmp->pm_Heads = getushort(b50->bpbHeads);
491	pmp->pm_Media = b50->bpbMedia;
492
493#ifndef __FreeBSD__
494	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
495#endif
496		/* XXX - We should probably check more values here */
497		if (!pmp->pm_BytesPerSec || !SecPerClust
498			|| !pmp->pm_Heads || pmp->pm_Heads > 255
499#ifdef PC98
500	    		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
501#else
502			|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
503#endif
504			error = EINVAL;
505			goto error_exit;
506		}
507#ifndef __FreeBSD__
508	}
509#endif
510
511	if (pmp->pm_Sectors == 0) {
512		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
513		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
514	} else {
515		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
516		pmp->pm_HugeSectors = pmp->pm_Sectors;
517	}
518#ifdef	PC98	/* for PC98		added Satoshi Yasuda	*/
519	Phy_Sector_Size = 512;
520	if ((devvp->v_rdev>>8) == 2) {	/* floppy check */
521		if (((devvp->v_rdev&077) == 2) && (pmp->pm_HugeSectors == 1232)) {
522				Phy_Sector_Size = 1024;	/* 2HD */
523				/*
524				 * 1024byte/sector support
525				 */
526				devvp->v_flag |= 0x10000;
527		} else {
528			if ((((devvp->v_rdev&077) == 3)	/* 2DD 8 or 9 sector */
529				&& (pmp->pm_HugeSectors == 1440)) /* 9 sector */
530				|| (((devvp->v_rdev&077) == 4)
531				&& (pmp->pm_HugeSectors == 1280)) /* 8 sector */
532				|| (((devvp->v_rdev&077) == 5)
533				&& (pmp->pm_HugeSectors == 2880))) { /* 1.44M */
534					Phy_Sector_Size = 512;
535			} else {
536				if (((devvp->v_rdev&077) != 1)
537				    && ((devvp->v_rdev&077) != 0)) { /* 2HC */
538					error = EINVAL;
539					goto error_exit;
540				}
541			}
542		}
543	}
544	pc98_wrk = pmp->pm_BytesPerSec / Phy_Sector_Size;
545	pmp->pm_BytesPerSec = Phy_Sector_Size;
546	SecPerClust = SecPerClust * pc98_wrk;
547	pmp->pm_HugeSectors = pmp->pm_HugeSectors * pc98_wrk;
548	pmp->pm_ResSectors = pmp->pm_ResSectors * pc98_wrk;
549	pmp->pm_FATsecs = pmp->pm_FATsecs * pc98_wrk;
550	pmp->pm_SecPerTrack = pmp->pm_SecPerTrack * pc98_wrk;
551	pmp->pm_HiddenSects = pmp->pm_HiddenSects * pc98_wrk;
552#endif			/*						*/
553	if (pmp->pm_HugeSectors > 0xffffffff / pmp->pm_BytesPerSec + 1) {
554		/*
555		 * We cannot deal currently with this size of disk
556		 * due to fileid limitations (see msdosfs_getattr and
557		 * msdosfs_readdir)
558		 */
559		error = EINVAL;
560		goto error_exit;
561	}
562
563	if (pmp->pm_RootDirEnts == 0) {
564		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
565		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
566		    || pmp->pm_Sectors
567		    || pmp->pm_FATsecs
568		    || getushort(b710->bpbFSVers)) {
569			error = EINVAL;
570			goto error_exit;
571		}
572		pmp->pm_fatmask = FAT32_MASK;
573		pmp->pm_fatmult = 4;
574		pmp->pm_fatdiv = 1;
575		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
576		if (getushort(b710->bpbExtFlags) & FATMIRROR)
577			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
578		else
579			pmp->pm_flags |= MSDOSFS_FATMIRROR;
580	} else
581		pmp->pm_flags |= MSDOSFS_FATMIRROR;
582
583#ifndef __FreeBSD__
584	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
585		if (FAT32(pmp)) {
586			/*
587			 * GEMDOS doesn't know fat32.
588			 */
589			error = EINVAL;
590			goto error_exit;
591		}
592
593		/*
594		 * Check a few values (could do some more):
595		 * - logical sector size: power of 2, >= block size
596		 * - sectors per cluster: power of 2, >= 1
597		 * - number of sectors:   >= 1, <= size of partition
598		 */
599		if ( (SecPerClust == 0)
600		  || (SecPerClust & (SecPerClust - 1))
601		  || (pmp->pm_BytesPerSec < bsize)
602		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
603		  || (pmp->pm_HugeSectors == 0)
604		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
605							> dpart.part->p_size)
606		   ) {
607			error = EINVAL;
608			goto error_exit;
609		}
610		/*
611		 * XXX - Many parts of the msdos fs driver seem to assume that
612		 * the number of bytes per logical sector (BytesPerSec) will
613		 * always be the same as the number of bytes per disk block
614		 * Let's pretend it is.
615		 */
616		tmp = pmp->pm_BytesPerSec / bsize;
617		pmp->pm_BytesPerSec  = bsize;
618		pmp->pm_HugeSectors *= tmp;
619		pmp->pm_HiddenSects *= tmp;
620		pmp->pm_ResSectors  *= tmp;
621		pmp->pm_Sectors     *= tmp;
622		pmp->pm_FATsecs     *= tmp;
623		SecPerClust         *= tmp;
624	}
625#endif
626	pmp->pm_fatblk = pmp->pm_ResSectors;
627	if (FAT32(pmp)) {
628		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
629		pmp->pm_firstcluster = pmp->pm_fatblk
630			+ (pmp->pm_FATs * pmp->pm_FATsecs);
631		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
632	} else {
633		pmp->pm_rootdirblk = pmp->pm_fatblk +
634			(pmp->pm_FATs * pmp->pm_FATsecs);
635		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
636				       + pmp->pm_BytesPerSec - 1)
637			/ pmp->pm_BytesPerSec;/* in sectors */
638		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
639	}
640
641	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
642	    SecPerClust;
643	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
644	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
645
646#ifndef __FreeBSD__
647	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
648		if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
649		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
650		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
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	} else
661#endif
662	if (pmp->pm_fatmask == 0) {
663		if (pmp->pm_maxcluster
664		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
665			/*
666			 * This will usually be a floppy disk. This size makes
667			 * sure that one fat entry will not be split across
668			 * multiple blocks.
669			 */
670			pmp->pm_fatmask = FAT12_MASK;
671			pmp->pm_fatmult = 3;
672			pmp->pm_fatdiv = 2;
673		} else {
674			pmp->pm_fatmask = FAT16_MASK;
675			pmp->pm_fatmult = 2;
676			pmp->pm_fatdiv = 1;
677		}
678	}
679	if (FAT12(pmp))
680		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
681	else
682		pmp->pm_fatblocksize = MAXBSIZE;
683
684	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
685	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
686
687	/*
688	 * Compute mask and shift value for isolating cluster relative byte
689	 * offsets and cluster numbers from a file offset.
690	 */
691	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
692	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
693	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
694
695	/*
696	 * Check for valid cluster size
697	 * must be a power of 2
698	 */
699	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
700		error = EINVAL;
701		goto error_exit;
702	}
703
704	/*
705	 * Release the bootsector buffer.
706	 */
707	brelse(bp);
708	bp = NULL;
709
710	/*
711	 * Check FSInfo.
712	 */
713	if (pmp->pm_fsinfo) {
714		struct fsinfo *fp;
715
716		if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
717			goto error_exit;
718		fp = (struct fsinfo *)bp->b_data;
719		if (!bcmp(fp->fsisig1, "RRaA", 4)
720		    && !bcmp(fp->fsisig2, "rrAa", 4)
721		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
722		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
723			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
724		else
725			pmp->pm_fsinfo = 0;
726		brelse(bp);
727		bp = NULL;
728	}
729
730	/*
731	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
732	 */
733
734	/*
735	 * Allocate memory for the bitmap of allocated clusters, and then
736	 * fill it in.
737	 */
738	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
739				   / N_INUSEBITS)
740				  * sizeof(*pmp->pm_inusemap),
741				  M_MSDOSFSFAT, M_WAITOK);
742
743	/*
744	 * fillinusemap() needs pm_devvp.
745	 */
746	pmp->pm_dev = dev;
747	pmp->pm_devvp = devvp;
748
749	/*
750	 * Have the inuse map filled in.
751	 */
752	if ((error = fillinusemap(pmp)) != 0)
753		goto error_exit;
754
755	/*
756	 * If they want fat updates to be synchronous then let them suffer
757	 * the performance degradation in exchange for the on disk copy of
758	 * the fat being correct just about all the time.  I suppose this
759	 * would be a good thing to turn on if the kernel is still flakey.
760	 */
761	if (mp->mnt_flag & MNT_SYNCHRONOUS)
762		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
763
764	/*
765	 * Finish up.
766	 */
767	if (ronly)
768		pmp->pm_flags |= MSDOSFSMNT_RONLY;
769	else
770		pmp->pm_fmod = 1;
771	mp->mnt_data = (qaddr_t) pmp;
772	mp->mnt_stat.f_fsid.val[0] = (long)dev;
773	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
774	mp->mnt_flag |= MNT_LOCAL;
775	devvp->v_specflags |= SI_MOUNTEDON;
776
777	return 0;
778
779error_exit:
780	if (bp)
781		brelse(bp);
782	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
783	if (pmp) {
784		if (pmp->pm_inusemap)
785			free(pmp->pm_inusemap, M_MSDOSFSFAT);
786		free(pmp, M_MSDOSFSMNT);
787		mp->mnt_data = (qaddr_t)0;
788	}
789	return (error);
790}
791
792static int
793msdosfs_start(mp, flags, p)
794	struct mount *mp;
795	int flags;
796	struct proc *p;
797{
798
799	return (0);
800}
801
802/*
803 * Unmount the filesystem described by mp.
804 */
805static int
806msdosfs_unmount(mp, mntflags, p)
807	struct mount *mp;
808	int mntflags;
809	struct proc *p;
810{
811	struct msdosfsmount *pmp;
812	int error, flags;
813
814	flags = 0;
815	if (mntflags & MNT_FORCE)
816		flags |= FORCECLOSE;
817	error = vflush(mp, NULLVP, flags);
818	if (error)
819		return error;
820	pmp = VFSTOMSDOSFS(mp);
821	pmp->pm_devvp->v_specflags &= ~SI_MOUNTEDON;
822#ifdef MSDOSFS_DEBUG
823	{
824		struct vnode *vp = pmp->pm_devvp;
825
826		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
827		printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
828		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
829		printf("lastr %d, id %lu, mount %p, op %p\n",
830		    vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op);
831		printf("freef %p, freeb %p, mount %p\n",
832		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
833		    vp->v_mount);
834		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
835		    vp->v_cleanblkhd.lh_first,
836		    vp->v_dirtyblkhd.lh_first,
837		    vp->v_numoutput, vp->v_type);
838		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
839		    vp->v_socket, vp->v_tag,
840		    ((u_int *)vp->v_data)[0],
841		    ((u_int *)vp->v_data)[1]);
842	}
843#endif
844	error = VOP_CLOSE(pmp->pm_devvp, (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
845	    NOCRED, p);
846	vrele(pmp->pm_devvp);
847	free(pmp->pm_inusemap, M_MSDOSFSFAT);
848	free(pmp, M_MSDOSFSMNT);
849	mp->mnt_data = (qaddr_t)0;
850	mp->mnt_flag &= ~MNT_LOCAL;
851	return (error);
852}
853
854static int
855msdosfs_root(mp, vpp)
856	struct mount *mp;
857	struct vnode **vpp;
858{
859	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
860	struct denode *ndep;
861	int error;
862
863#ifdef MSDOSFS_DEBUG
864	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
865#endif
866	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
867	if (error)
868		return (error);
869	*vpp = DETOV(ndep);
870	return (0);
871}
872
873static int
874msdosfs_quotactl(mp, cmds, uid, arg, p)
875	struct mount *mp;
876	int cmds;
877	uid_t uid;
878	caddr_t arg;
879	struct proc *p;
880{
881	return EOPNOTSUPP;
882}
883
884static int
885msdosfs_statfs(mp, sbp, p)
886	struct mount *mp;
887	struct statfs *sbp;
888	struct proc *p;
889{
890	struct msdosfsmount *pmp;
891
892	pmp = VFSTOMSDOSFS(mp);
893	sbp->f_bsize = pmp->pm_bpcluster;
894	sbp->f_iosize = pmp->pm_bpcluster;
895	sbp->f_blocks = pmp->pm_nmbrofclusters;
896	sbp->f_bfree = pmp->pm_freeclustercount;
897	sbp->f_bavail = pmp->pm_freeclustercount;
898	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
899	sbp->f_ffree = 0;	/* what to put in here? */
900	if (sbp != &mp->mnt_stat) {
901		sbp->f_type = mp->mnt_vfc->vfc_typenum;
902		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
903		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
904	}
905	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
906	return (0);
907}
908
909static int
910msdosfs_sync(mp, waitfor, cred, p)
911	struct mount *mp;
912	int waitfor;
913	struct ucred *cred;
914	struct proc *p;
915{
916	struct vnode *vp, *nvp;
917	struct denode *dep;
918	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
919	int error, allerror = 0;
920
921	/*
922	 * If we ever switch to not updating all of the fats all the time,
923	 * this would be the place to update them from the first one.
924	 */
925	if (pmp->pm_fmod != 0)
926		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
927			panic("msdosfs_sync: rofs mod");
928		else {
929			/* update fats here */
930		}
931	/*
932	 * Write back each (modified) denode.
933	 */
934	simple_lock(&mntvnode_slock);
935loop:
936	for (vp = mp->mnt_vnodelist.lh_first;
937	     vp != NULL;
938	     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
946		simple_lock(&vp->v_interlock);
947		nvp = vp->v_mntvnodes.le_next;
948		dep = VTODE(vp);
949		if (vp->v_type == VNON || ((dep->de_flag &
950		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0)
951		    && vp->v_dirtyblkhd.lh_first == NULL) {
952			simple_unlock(&vp->v_interlock);
953			continue;
954		}
955		simple_unlock(&mntvnode_slock);
956		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
957		if (error) {
958			simple_lock(&mntvnode_slock);
959			if (error == ENOENT)
960				goto loop;
961			continue;
962		}
963		error = VOP_FSYNC(vp, cred, waitfor, p);
964		if (error)
965			allerror = error;
966		VOP_UNLOCK(vp, 0, p);
967		vrele(vp);	/* done with this one	 */
968		simple_lock(&mntvnode_slock);
969	}
970	simple_unlock(&mntvnode_slock);
971
972	/*
973	 * Flush filesystem control info.
974	 */
975	error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
976	if (error)
977		allerror = error;
978	return (allerror);
979}
980
981static int
982msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
983	struct mount *mp;
984	struct fid *fhp;
985	struct sockaddr *nam;
986	struct vnode **vpp;
987	int *exflagsp;
988	struct ucred **credanonp;
989{
990	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
991	struct defid *defhp = (struct defid *) fhp;
992	struct denode *dep;
993	struct netcred *np;
994	int error;
995
996	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
997	if (np == NULL)
998		return (EACCES);
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	*exflagsp = np->netc_exflags;
1006	*credanonp = &np->netc_anon;
1007	return (0);
1008}
1009
1010static int
1011msdosfs_vptofh(vp, fhp)
1012	struct vnode *vp;
1013	struct fid *fhp;
1014{
1015	struct denode *dep;
1016	struct defid *defhp;
1017
1018	dep = VTODE(vp);
1019	defhp = (struct defid *)fhp;
1020	defhp->defid_len = sizeof(struct defid);
1021	defhp->defid_dirclust = dep->de_dirclust;
1022	defhp->defid_dirofs = dep->de_diroffset;
1023	/* defhp->defid_gen = dep->de_gen; */
1024	return (0);
1025}
1026
1027static int
1028msdosfs_vget(mp, ino, vpp)
1029	struct mount *mp;
1030	ino_t ino;
1031	struct vnode **vpp;
1032{
1033	return EOPNOTSUPP;
1034}
1035
1036static struct vfsops msdosfs_vfsops = {
1037	msdosfs_mount,
1038	msdosfs_start,
1039	msdosfs_unmount,
1040	msdosfs_root,
1041	msdosfs_quotactl,
1042	msdosfs_statfs,
1043	msdosfs_sync,
1044	msdosfs_vget,
1045	msdosfs_fhtovp,
1046	msdosfs_vptofh,
1047	msdosfs_init
1048};
1049
1050VFS_SET(msdosfs_vfsops, msdos, MOUNT_MSDOS, 0);
1051