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