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