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