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