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