msdosfs_vfsops.c revision 102391
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 102391 2002-08-25 02:34:18Z bde $ */
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.bsPBP;
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("Next free cluster in FSInfo (%u) exceeds maxcluster (%u)\n",
550				pmp->pm_nxtfree, pmp->pm_maxcluster);
551		error = EINVAL;
552		goto error_exit;
553	}
554
555	/*
556	 * Allocate memory for the bitmap of allocated clusters, and then
557	 * fill it in.
558	 */
559	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
560				   / N_INUSEBITS)
561				  * sizeof(*pmp->pm_inusemap),
562				  M_MSDOSFSFAT, M_WAITOK);
563
564	/*
565	 * fillinusemap() needs pm_devvp.
566	 */
567	pmp->pm_dev = dev;
568	pmp->pm_devvp = devvp;
569
570	/*
571	 * Have the inuse map filled in.
572	 */
573	if ((error = fillinusemap(pmp)) != 0)
574		goto error_exit;
575
576	/*
577	 * If they want fat updates to be synchronous then let them suffer
578	 * the performance degradation in exchange for the on disk copy of
579	 * the fat being correct just about all the time.  I suppose this
580	 * would be a good thing to turn on if the kernel is still flakey.
581	 */
582	if (mp->mnt_flag & MNT_SYNCHRONOUS)
583		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
584
585	/*
586	 * Finish up.
587	 */
588	if (ronly)
589		pmp->pm_flags |= MSDOSFSMNT_RONLY;
590	else
591		pmp->pm_fmod = 1;
592	mp->mnt_data = (qaddr_t) pmp;
593	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
594	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
595	mp->mnt_flag |= MNT_LOCAL;
596	devvp->v_rdev->si_mountpoint = mp;
597
598	return 0;
599
600error_exit:
601	if (bp)
602		brelse(bp);
603	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td);
604	if (pmp) {
605		if (pmp->pm_inusemap)
606			free(pmp->pm_inusemap, M_MSDOSFSFAT);
607		free(pmp, M_MSDOSFSMNT);
608		mp->mnt_data = (qaddr_t)0;
609	}
610	return (error);
611}
612
613/*
614 * Unmount the filesystem described by mp.
615 */
616static int
617msdosfs_unmount(mp, mntflags, td)
618	struct mount *mp;
619	int mntflags;
620	struct thread *td;
621{
622	struct msdosfsmount *pmp;
623	int error, flags;
624
625	flags = 0;
626	if (mntflags & MNT_FORCE)
627		flags |= FORCECLOSE;
628	error = vflush(mp, 0, flags);
629	if (error)
630		return error;
631	pmp = VFSTOMSDOSFS(mp);
632	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
633#ifdef MSDOSFS_DEBUG
634	{
635		struct vnode *vp = pmp->pm_devvp;
636
637		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
638		printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
639		    vp->vi_flag, vp->v_usecount, vp->v_writecount,
640		    vp->v_holdcnt);
641		printf("id %lu, mount %p, op %p\n",
642		    vp->v_id, vp->v_mount, vp->v_op);
643		printf("freef %p, freeb %p, mount %p\n",
644		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
645		    vp->v_mount);
646		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
647		    TAILQ_FIRST(&vp->v_cleanblkhd),
648		    TAILQ_FIRST(&vp->v_dirtyblkhd),
649		    vp->v_numoutput, vp->v_type);
650		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
651		    vp->v_socket, vp->v_tag,
652		    ((u_int *)vp->v_data)[0],
653		    ((u_int *)vp->v_data)[1]);
654	}
655#endif
656	error = VOP_CLOSE(pmp->pm_devvp,
657		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
658		    NOCRED, td);
659	vrele(pmp->pm_devvp);
660	free(pmp->pm_inusemap, M_MSDOSFSFAT);
661	free(pmp, M_MSDOSFSMNT);
662	mp->mnt_data = (qaddr_t)0;
663	mp->mnt_flag &= ~MNT_LOCAL;
664	return (error);
665}
666
667static int
668msdosfs_root(mp, vpp)
669	struct mount *mp;
670	struct vnode **vpp;
671{
672	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
673	struct denode *ndep;
674	int error;
675
676#ifdef MSDOSFS_DEBUG
677	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
678#endif
679	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
680	if (error)
681		return (error);
682	*vpp = DETOV(ndep);
683	return (0);
684}
685
686static int
687msdosfs_statfs(mp, sbp, td)
688	struct mount *mp;
689	struct statfs *sbp;
690	struct thread *td;
691{
692	struct msdosfsmount *pmp;
693
694	pmp = VFSTOMSDOSFS(mp);
695	sbp->f_bsize = pmp->pm_bpcluster;
696	sbp->f_iosize = pmp->pm_bpcluster;
697	sbp->f_blocks = pmp->pm_maxcluster + 1;
698	sbp->f_bfree = pmp->pm_freeclustercount;
699	sbp->f_bavail = pmp->pm_freeclustercount;
700	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
701	sbp->f_ffree = 0;	/* what to put in here? */
702	if (sbp != &mp->mnt_stat) {
703		sbp->f_type = mp->mnt_vfc->vfc_typenum;
704		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
705		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
706	}
707	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
708	return (0);
709}
710
711static int
712msdosfs_sync(mp, waitfor, cred, td)
713	struct mount *mp;
714	int waitfor;
715	struct ucred *cred;
716	struct thread *td;
717{
718	struct vnode *vp, *nvp;
719	struct denode *dep;
720	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
721	int error, allerror = 0;
722
723	/*
724	 * If we ever switch to not updating all of the fats all the time,
725	 * this would be the place to update them from the first one.
726	 */
727	if (pmp->pm_fmod != 0) {
728		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
729			panic("msdosfs_sync: rofs mod");
730		else {
731			/* update fats here */
732		}
733	}
734	/*
735	 * Write back each (modified) denode.
736	 */
737	mtx_lock(&mntvnode_mtx);
738loop:
739	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
740		/*
741		 * If the vnode that we are about to sync is no longer
742		 * associated with this mount point, start over.
743		 */
744		if (vp->v_mount != mp)
745			goto loop;
746		nvp = TAILQ_NEXT(vp, v_nmntvnodes);
747
748		mtx_unlock(&mntvnode_mtx);
749		mtx_lock(&vp->v_interlock);
750		dep = VTODE(vp);
751		if (vp->v_type == VNON ||
752		    ((dep->de_flag &
753		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
754		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
755			mtx_unlock(&vp->v_interlock);
756			mtx_lock(&mntvnode_mtx);
757			continue;
758		}
759		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
760		if (error) {
761			mtx_lock(&mntvnode_mtx);
762			if (error == ENOENT)
763				goto loop;
764			continue;
765		}
766		error = VOP_FSYNC(vp, cred, waitfor, td);
767		if (error)
768			allerror = error;
769		VOP_UNLOCK(vp, 0, td);
770		vrele(vp);
771		mtx_lock(&mntvnode_mtx);
772	}
773	mtx_unlock(&mntvnode_mtx);
774
775	/*
776	 * Flush filesystem control info.
777	 */
778	if (waitfor != MNT_LAZY) {
779		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
780		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
781		if (error)
782			allerror = error;
783		VOP_UNLOCK(pmp->pm_devvp, 0, td);
784	}
785	return (allerror);
786}
787
788static int
789msdosfs_fhtovp(mp, fhp, vpp)
790	struct mount *mp;
791	struct fid *fhp;
792	struct vnode **vpp;
793{
794	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
795	struct defid *defhp = (struct defid *) fhp;
796	struct denode *dep;
797	int error;
798
799	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
800	if (error) {
801		*vpp = NULLVP;
802		return (error);
803	}
804	*vpp = DETOV(dep);
805	return (0);
806}
807
808static int
809msdosfs_vptofh(vp, fhp)
810	struct vnode *vp;
811	struct fid *fhp;
812{
813	struct denode *dep;
814	struct defid *defhp;
815
816	dep = VTODE(vp);
817	defhp = (struct defid *)fhp;
818	defhp->defid_len = sizeof(struct defid);
819	defhp->defid_dirclust = dep->de_dirclust;
820	defhp->defid_dirofs = dep->de_diroffset;
821	/* defhp->defid_gen = dep->de_gen; */
822	return (0);
823}
824
825static struct vfsops msdosfs_vfsops = {
826	msdosfs_mount,
827	vfs_stdstart,
828	msdosfs_unmount,
829	msdosfs_root,
830	vfs_stdquotactl,
831	msdosfs_statfs,
832	msdosfs_sync,
833	vfs_stdvget,
834	msdosfs_fhtovp,
835	vfs_stdcheckexp,
836	msdosfs_vptofh,
837	msdosfs_init,
838	msdosfs_uninit,
839	vfs_stdextattrctl,
840};
841
842VFS_SET(msdosfs_vfsops, msdosfs, 0);
843