msdosfs_vfsops.c revision 75934
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 75934 2001-04-25 07:07:52Z phk $ */
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/conf.h>
54#include <sys/namei.h>
55#include <sys/proc.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <net/radix.h>
59#include <sys/socket.h>
60#include <sys/mount.h>
61#include <sys/bio.h>
62#include <sys/buf.h>
63#include <sys/fcntl.h>
64#include <sys/malloc.h>
65#include <sys/stat.h> 				/* defines ALLPERMS */
66#include <sys/mutex.h>
67
68#include <msdosfs/bpb.h>
69#include <msdosfs/bootsect.h>
70#include <msdosfs/direntry.h>
71#include <msdosfs/denode.h>
72#include <msdosfs/msdosfsmount.h>
73#include <msdosfs/fat.h>
74
75#define MSDOSFS_DFLTBSIZE       4096
76
77#if 1 /*def PC98*/
78/*
79 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
80 *       garbage or a random value :-{
81 *       If you want to use that broken-signatured media, define the
82 *       following symbol even though PC/AT.
83 *       (ex. mount PC-98 DOS formatted FD on PC/AT)
84 */
85#define	MSDOSFS_NOCHECKSIG
86#endif
87
88MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
89static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
90
91static int	update_mp __P((struct mount *mp, struct msdosfs_args *argp));
92static int	mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
93				  struct proc *p, struct msdosfs_args *argp));
94static int	msdosfs_fhtovp __P((struct mount *, struct fid *,
95				    struct vnode **));
96static int	msdosfs_mount __P((struct mount *, char *, caddr_t,
97				   struct nameidata *, struct proc *));
98static int	msdosfs_root __P((struct mount *, struct vnode **));
99static int	msdosfs_statfs __P((struct mount *, struct statfs *,
100				    struct proc *));
101static int	msdosfs_sync __P((struct mount *, int, struct ucred *,
102				  struct proc *));
103static int	msdosfs_unmount __P((struct mount *, int, struct proc *));
104static int	msdosfs_vptofh __P((struct vnode *, struct fid *));
105
106static int
107update_mp(mp, argp)
108	struct mount *mp;
109	struct msdosfs_args *argp;
110{
111	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
112	int error;
113
114	pmp->pm_gid = argp->gid;
115	pmp->pm_uid = argp->uid;
116	pmp->pm_mask = argp->mask & ALLPERMS;
117	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
118	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
119		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
120		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
121		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
122	}
123	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
124		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
125		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
126	}
127
128#ifndef __FreeBSD__
129	/*
130	 * GEMDOS knows nothing (yet) about win95
131	 */
132	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
133		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
134#endif
135
136	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
137		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
138	else if (!(pmp->pm_flags &
139	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
140		struct vnode *rootvp;
141
142		/*
143		 * Try to divine whether to support Win'95 long filenames
144		 */
145		if (FAT32(pmp))
146			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
147		else {
148			if ((error = msdosfs_root(mp, &rootvp)) != 0)
149				return error;
150			pmp->pm_flags |= findwin95(VTODE(rootvp))
151				? MSDOSFSMNT_LONGNAME
152					: MSDOSFSMNT_SHORTNAME;
153			vput(rootvp);
154		}
155	}
156	return 0;
157}
158
159#ifndef __FreeBSD__
160int
161msdosfs_mountroot()
162{
163	register struct mount *mp;
164	struct proc *p = curproc;	/* XXX */
165	size_t size;
166	int error;
167	struct msdosfs_args args;
168
169	if (root_device->dv_class != DV_DISK)
170		return (ENODEV);
171
172	/*
173	 * Get vnodes for swapdev and rootdev.
174	 */
175	if (bdevvp(rootdev, &rootvp))
176		panic("msdosfs_mountroot: can't setup rootvp");
177
178	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
179	mp->mnt_op = &msdosfs_vfsops;
180	mp->mnt_flag = 0;
181	LIST_INIT(&mp->mnt_vnodelist);
182
183	args.flags = 0;
184	args.uid = 0;
185	args.gid = 0;
186	args.mask = 0777;
187
188	if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
189		free(mp, M_MOUNT);
190		return (error);
191	}
192
193	if ((error = update_mp(mp, &args)) != 0) {
194		(void)msdosfs_unmount(mp, 0, p);
195		free(mp, M_MOUNT);
196		return (error);
197	}
198
199	if ((error = vfs_lock(mp)) != 0) {
200		(void)msdosfs_unmount(mp, 0, p);
201		free(mp, M_MOUNT);
202		return (error);
203	}
204
205	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
206	mp->mnt_vnodecovered = NULLVP;
207	(void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
208	    &size);
209	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
210	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
211	    &size);
212	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
213	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
214	vfs_unlock(mp);
215	return (0);
216}
217#endif
218
219/*
220 * mp - path - addr in user space of mount point (ie /usr or whatever)
221 * data - addr in user space of mount params including the name of the block
222 * special file to treat as a filesystem.
223 */
224static int
225msdosfs_mount(mp, path, data, ndp, p)
226	struct mount *mp;
227	char *path;
228	caddr_t data;
229	struct nameidata *ndp;
230	struct proc *p;
231{
232	struct vnode *devvp;	  /* vnode for blk device to mount */
233	struct msdosfs_args args; /* will hold data from mount request */
234	/* msdosfs specific mount control block */
235	struct msdosfsmount *pmp = NULL;
236	size_t size;
237	int error, flags;
238	mode_t accessmode;
239
240	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
241	if (error)
242		return (error);
243	if (args.magic != MSDOSFS_ARGSMAGIC)
244		args.flags = 0;
245	/*
246	 * If updating, check whether changing from read-only to
247	 * read/write; if there is no device name, that's all we do.
248	 */
249	if (mp->mnt_flag & MNT_UPDATE) {
250		pmp = VFSTOMSDOSFS(mp);
251		error = 0;
252		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
253			flags = WRITECLOSE;
254			if (mp->mnt_flag & MNT_FORCE)
255				flags |= FORCECLOSE;
256			error = vflush(mp, NULLVP, flags);
257		}
258		if (!error && (mp->mnt_flag & MNT_RELOAD))
259			/* not yet implemented */
260			error = EOPNOTSUPP;
261		if (error)
262			return (error);
263		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
264			/*
265			 * If upgrade to read-write by non-root, then verify
266			 * that user has necessary permissions on the device.
267			 */
268			if (p->p_ucred->cr_uid != 0) {
269				devvp = pmp->pm_devvp;
270				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
271				error = VOP_ACCESS(devvp, VREAD | VWRITE,
272						   p->p_ucred, p);
273				if (error) {
274					VOP_UNLOCK(devvp, 0, p);
275					return (error);
276				}
277				VOP_UNLOCK(devvp, 0, p);
278			}
279			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
280		}
281		if (args.fspec == 0) {
282#ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
283			if (args.flags & MSDOSFSMNT_MNTOPT) {
284				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
285				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
286				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
287					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
288			}
289#endif
290			/*
291			 * Process export requests.
292			 */
293			return (vfs_export(mp, &args.export));
294		}
295	}
296	/*
297	 * Not an update, or updating the name: look up the name
298	 * and verify that it refers to a sensible block device.
299	 */
300	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
301	error = namei(ndp);
302	if (error)
303		return (error);
304	devvp = ndp->ni_vp;
305	NDFREE(ndp, NDF_ONLY_PNBUF);
306
307	if (!vn_isdisk(devvp, &error)) {
308		vrele(devvp);
309		return (error);
310	}
311	/*
312	 * If mount by non-root, then verify that user has necessary
313	 * permissions on the device.
314	 */
315	if (p->p_ucred->cr_uid != 0) {
316		accessmode = VREAD;
317		if ((mp->mnt_flag & MNT_RDONLY) == 0)
318			accessmode |= VWRITE;
319		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
320		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
321		if (error) {
322			vput(devvp);
323			return (error);
324		}
325		VOP_UNLOCK(devvp, 0, p);
326	}
327	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
328		error = mountmsdosfs(devvp, mp, p, &args);
329#ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
330		pmp = VFSTOMSDOSFS(mp);
331#endif
332	} else {
333		if (devvp != pmp->pm_devvp)
334			error = EINVAL;	/* XXX needs translation */
335		else
336			vrele(devvp);
337	}
338	if (error) {
339		vrele(devvp);
340		return (error);
341	}
342
343	error = update_mp(mp, &args);
344	if (error) {
345		msdosfs_unmount(mp, MNT_FORCE, p);
346		return error;
347	}
348	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
349	    &size);
350	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
351	(void) msdosfs_statfs(mp, &mp->mnt_stat, p);
352#ifdef MSDOSFS_DEBUG
353	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
354#endif
355	return (0);
356}
357
358static int
359mountmsdosfs(devvp, mp, p, argp)
360	struct vnode *devvp;
361	struct mount *mp;
362	struct proc *p;
363	struct msdosfs_args *argp;
364{
365	struct msdosfsmount *pmp;
366	struct buf *bp;
367	dev_t dev = devvp->v_rdev;
368#ifndef __FreeBSD__
369	struct partinfo dpart;
370	int bsize = 0, dtype = 0, tmp;
371#endif
372	union bootsector *bsp;
373	struct byte_bpb33 *b33;
374	struct byte_bpb50 *b50;
375	struct byte_bpb710 *b710;
376	u_int8_t SecPerClust;
377	u_long clusters;
378	int	ronly, error;
379
380	/*
381	 * Disallow multiple mounts of the same device.
382	 * Disallow mounting of a device that is currently in use
383	 * (except for root, which might share swap device for miniroot).
384	 * Flush out any old buffers remaining from a previous use.
385	 */
386	error = vfs_mountedon(devvp);
387	if (error)
388		return (error);
389	if (vcount(devvp) > 1 && devvp != rootvp)
390		return (EBUSY);
391	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
392	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
393	VOP_UNLOCK(devvp, 0, p);
394	if (error)
395		return (error);
396
397	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
398	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
399	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
400	VOP_UNLOCK(devvp, 0, p);
401	if (error)
402		return (error);
403
404	bp  = NULL; /* both used in error_exit */
405	pmp = NULL;
406
407#ifndef __FreeBSD__
408	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
409		/*
410	 	 * We need the disklabel to calculate the size of a FAT entry
411		 * later on. Also make sure the partition contains a filesystem
412		 * of type FS_MSDOS. This doesn't work for floppies, so we have
413		 * to check for them too.
414	 	 *
415	 	 * At least some parts of the msdos fs driver seem to assume
416		 * that the size of a disk block will always be 512 bytes.
417		 * Let's check it...
418		 */
419		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
420				  FREAD, NOCRED, p);
421		if (error)
422			goto error_exit;
423		tmp   = dpart.part->p_fstype;
424		dtype = dpart.disklab->d_type;
425		bsize = dpart.disklab->d_secsize;
426		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
427			error = EINVAL;
428			goto error_exit;
429		}
430	}
431#endif
432
433	/*
434	 * Read the boot sector of the filesystem, and then check the
435	 * boot signature.  If not a dos boot sector then error out.
436	 *
437	 * NOTE: 2048 is a maximum sector size in current...
438	 */
439	error = bread(devvp, 0, 2048, NOCRED, &bp);
440	if (error)
441		goto error_exit;
442	bp->b_flags |= B_AGE;
443	bsp = (union bootsector *)bp->b_data;
444	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
445	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
446	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
447
448#ifndef __FreeBSD__
449	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
450#endif
451#ifndef MSDOSFS_NOCHECKSIG
452		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
453		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
454			error = EINVAL;
455			goto error_exit;
456		}
457#endif
458#ifndef __FreeBSD__
459	}
460#endif
461
462	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
463	pmp->pm_mountp = mp;
464
465	/*
466	 * Compute several useful quantities from the bpb in the
467	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
468	 * the fields that are different between dos 5 and dos 3.3.
469	 */
470	SecPerClust = b50->bpbSecPerClust;
471	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
472	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
473	pmp->pm_FATs = b50->bpbFATs;
474	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
475	pmp->pm_Sectors = getushort(b50->bpbSectors);
476	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
477	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
478	pmp->pm_Heads = getushort(b50->bpbHeads);
479	pmp->pm_Media = b50->bpbMedia;
480
481	/* calculate the ratio of sector size to DEV_BSIZE */
482	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
483
484#ifndef __FreeBSD__
485	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
486#endif
487		/* XXX - We should probably check more values here */
488		if (!pmp->pm_BytesPerSec || !SecPerClust
489			|| !pmp->pm_Heads || pmp->pm_Heads > 255
490#ifdef PC98
491	    		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
492#else
493			|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
494#endif
495			error = EINVAL;
496			goto error_exit;
497		}
498#ifndef __FreeBSD__
499	}
500#endif
501
502	if (pmp->pm_Sectors == 0) {
503		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
504		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
505	} else {
506		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
507		pmp->pm_HugeSectors = pmp->pm_Sectors;
508	}
509	if (pmp->pm_HugeSectors > 0xffffffff /
510	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
511		/*
512		 * We cannot deal currently with this size of disk
513		 * due to fileid limitations (see msdosfs_getattr and
514		 * msdosfs_readdir)
515		 */
516		error = EINVAL;
517		printf("mountmsdosfs(): disk too big, sorry\n");
518		goto error_exit;
519	}
520
521	if (pmp->pm_RootDirEnts == 0) {
522		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
523		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
524		    || pmp->pm_Sectors
525		    || pmp->pm_FATsecs
526		    || getushort(b710->bpbFSVers)) {
527			error = EINVAL;
528			printf("mountmsdosfs(): bad FAT32 filesystem\n");
529			goto error_exit;
530		}
531		pmp->pm_fatmask = FAT32_MASK;
532		pmp->pm_fatmult = 4;
533		pmp->pm_fatdiv = 1;
534		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
535		if (getushort(b710->bpbExtFlags) & FATMIRROR)
536			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
537		else
538			pmp->pm_flags |= MSDOSFS_FATMIRROR;
539	} else
540		pmp->pm_flags |= MSDOSFS_FATMIRROR;
541
542	/*
543	 * Check a few values (could do some more):
544	 * - logical sector size: power of 2, >= block size
545	 * - sectors per cluster: power of 2, >= 1
546	 * - number of sectors:   >= 1, <= size of partition
547	 */
548	if ( (SecPerClust == 0)
549	  || (SecPerClust & (SecPerClust - 1))
550	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
551	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
552	  || (pmp->pm_HugeSectors == 0)
553	) {
554		error = EINVAL;
555		goto error_exit;
556	}
557
558	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
559	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
560	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
561	SecPerClust         *= pmp->pm_BlkPerSec;
562
563	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
564
565	if (FAT32(pmp)) {
566		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
567		pmp->pm_firstcluster = pmp->pm_fatblk
568			+ (pmp->pm_FATs * pmp->pm_FATsecs);
569		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
570	} else {
571		pmp->pm_rootdirblk = pmp->pm_fatblk +
572			(pmp->pm_FATs * pmp->pm_FATsecs);
573		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
574				       + DEV_BSIZE - 1)
575			/ DEV_BSIZE; /* in blocks */
576		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
577	}
578
579	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
580	    SecPerClust + 1;
581	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
582
583#ifndef __FreeBSD__
584	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
585		if ((pmp->pm_maxcluster <= (0xff0 - 2))
586		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
587		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
588		    ) {
589			pmp->pm_fatmask = FAT12_MASK;
590			pmp->pm_fatmult = 3;
591			pmp->pm_fatdiv = 2;
592		} else {
593			pmp->pm_fatmask = FAT16_MASK;
594			pmp->pm_fatmult = 2;
595			pmp->pm_fatdiv = 1;
596		}
597	} else
598#endif
599	if (pmp->pm_fatmask == 0) {
600		if (pmp->pm_maxcluster
601		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
602			/*
603			 * This will usually be a floppy disk. This size makes
604			 * sure that one fat entry will not be split across
605			 * multiple blocks.
606			 */
607			pmp->pm_fatmask = FAT12_MASK;
608			pmp->pm_fatmult = 3;
609			pmp->pm_fatdiv = 2;
610		} else {
611			pmp->pm_fatmask = FAT16_MASK;
612			pmp->pm_fatmult = 2;
613			pmp->pm_fatdiv = 1;
614		}
615	}
616
617	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
618	if (pmp->pm_maxcluster >= clusters) {
619		printf("Warning: number of clusters (%ld) exceeds FAT "
620		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
621		pmp->pm_maxcluster = clusters - 1;
622	}
623
624
625	if (FAT12(pmp))
626		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
627	else
628		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
629
630	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
631	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
632
633	/*
634	 * Compute mask and shift value for isolating cluster relative byte
635	 * offsets and cluster numbers from a file offset.
636	 */
637	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
638	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
639	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
640
641	/*
642	 * Check for valid cluster size
643	 * must be a power of 2
644	 */
645	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
646		error = EINVAL;
647		goto error_exit;
648	}
649
650	/*
651	 * Release the bootsector buffer.
652	 */
653	brelse(bp);
654	bp = NULL;
655
656	/*
657	 * Check FSInfo.
658	 */
659	if (pmp->pm_fsinfo) {
660		struct fsinfo *fp;
661
662		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
663		    NOCRED, &bp)) != 0)
664			goto error_exit;
665		fp = (struct fsinfo *)bp->b_data;
666		if (!bcmp(fp->fsisig1, "RRaA", 4)
667		    && !bcmp(fp->fsisig2, "rrAa", 4)
668		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
669		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
670			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
671		else
672			pmp->pm_fsinfo = 0;
673		brelse(bp);
674		bp = NULL;
675	}
676
677	/*
678	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
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 = (qaddr_t) pmp;
719	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
720	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
721	mp->mnt_flag |= MNT_LOCAL;
722	devvp->v_rdev->si_mountpoint = mp;
723
724	return 0;
725
726error_exit:
727	if (bp)
728		brelse(bp);
729	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
730	if (pmp) {
731		if (pmp->pm_inusemap)
732			free(pmp->pm_inusemap, M_MSDOSFSFAT);
733		free(pmp, M_MSDOSFSMNT);
734		mp->mnt_data = (qaddr_t)0;
735	}
736	return (error);
737}
738
739/*
740 * Unmount the filesystem described by mp.
741 */
742static int
743msdosfs_unmount(mp, mntflags, p)
744	struct mount *mp;
745	int mntflags;
746	struct proc *p;
747{
748	struct msdosfsmount *pmp;
749	int error, flags;
750
751	flags = 0;
752	if (mntflags & MNT_FORCE)
753		flags |= FORCECLOSE;
754	error = vflush(mp, NULLVP, flags);
755	if (error)
756		return error;
757	pmp = VFSTOMSDOSFS(mp);
758	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
759#ifdef MSDOSFS_DEBUG
760	{
761		struct vnode *vp = pmp->pm_devvp;
762
763		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
764		printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
765		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
766		printf("id %lu, mount %p, op %p\n",
767		    vp->v_id, vp->v_mount, vp->v_op);
768		printf("freef %p, freeb %p, mount %p\n",
769		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
770		    vp->v_mount);
771		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
772		    TAILQ_FIRST(&vp->v_cleanblkhd),
773		    TAILQ_FIRST(&vp->v_dirtyblkhd),
774		    vp->v_numoutput, vp->v_type);
775		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
776		    vp->v_socket, vp->v_tag,
777		    ((u_int *)vp->v_data)[0],
778		    ((u_int *)vp->v_data)[1]);
779	}
780#endif
781	error = VOP_CLOSE(pmp->pm_devvp,
782		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
783		    NOCRED, p);
784	vrele(pmp->pm_devvp);
785	free(pmp->pm_inusemap, M_MSDOSFSFAT);
786	free(pmp, M_MSDOSFSMNT);
787	mp->mnt_data = (qaddr_t)0;
788	mp->mnt_flag &= ~MNT_LOCAL;
789	return (error);
790}
791
792static int
793msdosfs_root(mp, vpp)
794	struct mount *mp;
795	struct vnode **vpp;
796{
797	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
798	struct denode *ndep;
799	int error;
800
801#ifdef MSDOSFS_DEBUG
802	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
803#endif
804	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
805	if (error)
806		return (error);
807	*vpp = DETOV(ndep);
808	return (0);
809}
810
811static int
812msdosfs_statfs(mp, sbp, p)
813	struct mount *mp;
814	struct statfs *sbp;
815	struct proc *p;
816{
817	struct msdosfsmount *pmp;
818
819	pmp = VFSTOMSDOSFS(mp);
820	sbp->f_bsize = pmp->pm_bpcluster;
821	sbp->f_iosize = pmp->pm_bpcluster;
822	sbp->f_blocks = pmp->pm_maxcluster + 1;
823	sbp->f_bfree = pmp->pm_freeclustercount;
824	sbp->f_bavail = pmp->pm_freeclustercount;
825	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
826	sbp->f_ffree = 0;	/* what to put in here? */
827	if (sbp != &mp->mnt_stat) {
828		sbp->f_type = mp->mnt_vfc->vfc_typenum;
829		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
830		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
831	}
832	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
833	return (0);
834}
835
836static int
837msdosfs_sync(mp, waitfor, cred, p)
838	struct mount *mp;
839	int waitfor;
840	struct ucred *cred;
841	struct proc *p;
842{
843	struct vnode *vp, *nvp;
844	struct denode *dep;
845	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
846	int error, allerror = 0;
847
848	/*
849	 * If we ever switch to not updating all of the fats all the time,
850	 * this would be the place to update them from the first one.
851	 */
852	if (pmp->pm_fmod != 0) {
853		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
854			panic("msdosfs_sync: rofs mod");
855		else {
856			/* update fats here */
857		}
858	}
859	/*
860	 * Write back each (modified) denode.
861	 */
862	mtx_lock(&mntvnode_mtx);
863loop:
864	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
865		/*
866		 * If the vnode that we are about to sync is no longer
867		 * associated with this mount point, start over.
868		 */
869		if (vp->v_mount != mp)
870			goto loop;
871
872		mtx_lock(&vp->v_interlock);
873		nvp = LIST_NEXT(vp, v_mntvnodes);
874		dep = VTODE(vp);
875		if (vp->v_type == VNON ||
876		    ((dep->de_flag &
877		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
878		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
879			mtx_unlock(&vp->v_interlock);
880			continue;
881		}
882		mtx_unlock(&mntvnode_mtx);
883		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
884		if (error) {
885			mtx_lock(&mntvnode_mtx);
886			if (error == ENOENT)
887				goto loop;
888			continue;
889		}
890		error = VOP_FSYNC(vp, cred, waitfor, p);
891		if (error)
892			allerror = error;
893		VOP_UNLOCK(vp, 0, p);
894		vrele(vp);
895		mtx_lock(&mntvnode_mtx);
896	}
897	mtx_unlock(&mntvnode_mtx);
898
899	/*
900	 * Flush filesystem control info.
901	 */
902	if (waitfor != MNT_LAZY) {
903		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
904		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
905		if (error)
906			allerror = error;
907		VOP_UNLOCK(pmp->pm_devvp, 0, p);
908	}
909	return (allerror);
910}
911
912static int
913msdosfs_fhtovp(mp, fhp, vpp)
914	struct mount *mp;
915	struct fid *fhp;
916	struct vnode **vpp;
917{
918	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
919	struct defid *defhp = (struct defid *) fhp;
920	struct denode *dep;
921	int error;
922
923	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
924	if (error) {
925		*vpp = NULLVP;
926		return (error);
927	}
928	*vpp = DETOV(dep);
929	return (0);
930}
931
932static int
933msdosfs_vptofh(vp, fhp)
934	struct vnode *vp;
935	struct fid *fhp;
936{
937	struct denode *dep;
938	struct defid *defhp;
939
940	dep = VTODE(vp);
941	defhp = (struct defid *)fhp;
942	defhp->defid_len = sizeof(struct defid);
943	defhp->defid_dirclust = dep->de_dirclust;
944	defhp->defid_dirofs = dep->de_diroffset;
945	/* defhp->defid_gen = dep->de_gen; */
946	return (0);
947}
948
949static struct vfsops msdosfs_vfsops = {
950	msdosfs_mount,
951	vfs_stdstart,
952	msdosfs_unmount,
953	msdosfs_root,
954	vfs_stdquotactl,
955	msdosfs_statfs,
956	msdosfs_sync,
957	vfs_stdvget,
958	msdosfs_fhtovp,
959	vfs_stdcheckexp,
960	msdosfs_vptofh,
961	msdosfs_init,
962	msdosfs_uninit,
963	vfs_stdextattrctl,
964};
965
966VFS_SET(msdosfs_vfsops, msdos, 0);
967