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