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