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