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