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