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