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