xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision c697fb7f)
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 	struct export_args exp;
194 	int error;
195 
196 	if (data == NULL)
197 		return (EINVAL);
198 	error = copyin(data, &args, sizeof args);
199 	if (error)
200 		return (error);
201 	vfs_oexport_conv(&args.export, &exp);
202 
203 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
204 	ma = mount_arg(ma, "export", &exp, sizeof(exp));
205 	ma = mount_argf(ma, "uid", "%d", args.uid);
206 	ma = mount_argf(ma, "gid", "%d", args.gid);
207 	ma = mount_argf(ma, "mask", "%d", args.mask);
208 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
209 
210 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
211 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
212 	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
213 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
214 
215 	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
216 	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
217 	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
218 
219 	error = kernel_mount(ma, flags);
220 
221 	return (error);
222 }
223 
224 /*
225  * mp - path - addr in user space of mount point (ie /usr or whatever)
226  * data - addr in user space of mount params including the name of the block
227  * special file to treat as a filesystem.
228  */
229 static int
230 msdosfs_mount(struct mount *mp)
231 {
232 	struct vnode *devvp;	  /* vnode for blk device to mount */
233 	struct thread *td;
234 	/* msdosfs specific mount control block */
235 	struct msdosfsmount *pmp = NULL;
236 	struct nameidata ndp;
237 	int error, flags;
238 	accmode_t accmode;
239 	char *from;
240 
241 	td = curthread;
242 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
243 		return (EINVAL);
244 
245 	/*
246 	 * If updating, check whether changing from read-only to
247 	 * read/write; if there is no device name, that's all we do.
248 	 */
249 	if (mp->mnt_flag & MNT_UPDATE) {
250 		pmp = VFSTOMSDOSFS(mp);
251 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
252 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
253 			error = VFS_SYNC(mp, MNT_WAIT);
254 			if (error)
255 				return (error);
256 			flags = WRITECLOSE;
257 			if (mp->mnt_flag & MNT_FORCE)
258 				flags |= FORCECLOSE;
259 			error = vflush(mp, 0, flags, td);
260 			if (error)
261 				return (error);
262 
263 			/*
264 			 * Now the volume is clean.  Mark it so while the
265 			 * device is still rw.
266 			 */
267 			error = markvoldirty(pmp, 0);
268 			if (error) {
269 				(void)markvoldirty(pmp, 1);
270 				return (error);
271 			}
272 
273 			/* Downgrade the device from rw to ro. */
274 			g_topology_lock();
275 			error = g_access(pmp->pm_cp, 0, -1, 0);
276 			g_topology_unlock();
277 			if (error) {
278 				(void)markvoldirty(pmp, 1);
279 				return (error);
280 			}
281 
282 			/*
283 			 * Backing out after an error was painful in the
284 			 * above.  Now we are committed to succeeding.
285 			 */
286 			pmp->pm_fmod = 0;
287 			pmp->pm_flags |= MSDOSFSMNT_RONLY;
288 			MNT_ILOCK(mp);
289 			mp->mnt_flag |= MNT_RDONLY;
290 			MNT_IUNLOCK(mp);
291 		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
292 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
293 			/*
294 			 * If upgrade to read-write by non-root, then verify
295 			 * that user has necessary permissions on the device.
296 			 */
297 			devvp = pmp->pm_devvp;
298 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
299 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
300 			    td->td_ucred, td);
301 			if (error)
302 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
303 			if (error) {
304 				VOP_UNLOCK(devvp);
305 				return (error);
306 			}
307 			VOP_UNLOCK(devvp);
308 			g_topology_lock();
309 			error = g_access(pmp->pm_cp, 0, 1, 0);
310 			g_topology_unlock();
311 			if (error)
312 				return (error);
313 
314 			/* Now that the volume is modifiable, mark it dirty. */
315 			error = markvoldirty_upgrade(pmp, true, true);
316 			if (error) {
317 				/*
318 				 * If dirtying the superblock failed, drop GEOM
319 				 * 'w' refs (we're still RO).
320 				 */
321 				g_topology_lock();
322 				(void)g_access(pmp->pm_cp, 0, -1, 0);
323 				g_topology_unlock();
324 
325 				return (error);
326 			}
327 
328 			pmp->pm_fmod = 1;
329 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
330 			MNT_ILOCK(mp);
331 			mp->mnt_flag &= ~MNT_RDONLY;
332 			MNT_IUNLOCK(mp);
333 		}
334 	}
335 	/*
336 	 * Not an update, or updating the name: look up the name
337 	 * and verify that it refers to a sensible disk device.
338 	 */
339 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
340 		return (EINVAL);
341 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
342 	error = namei(&ndp);
343 	if (error)
344 		return (error);
345 	devvp = ndp.ni_vp;
346 	NDFREE(&ndp, NDF_ONLY_PNBUF);
347 
348 	if (!vn_isdisk(devvp, &error)) {
349 		vput(devvp);
350 		return (error);
351 	}
352 	/*
353 	 * If mount by non-root, then verify that user has necessary
354 	 * permissions on the device.
355 	 */
356 	accmode = VREAD;
357 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
358 		accmode |= VWRITE;
359 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
360 	if (error)
361 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
362 	if (error) {
363 		vput(devvp);
364 		return (error);
365 	}
366 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
367 		error = mountmsdosfs(devvp, mp);
368 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
369 		pmp = VFSTOMSDOSFS(mp);
370 #endif
371 	} else {
372 		vput(devvp);
373 		if (devvp != pmp->pm_devvp)
374 			return (EINVAL);	/* XXX needs translation */
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);
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 
395 static int
396 mountmsdosfs(struct vnode *devvp, struct mount *mp)
397 {
398 	struct msdosfsmount *pmp;
399 	struct buf *bp;
400 	struct cdev *dev;
401 	union bootsector *bsp;
402 	struct byte_bpb33 *b33;
403 	struct byte_bpb50 *b50;
404 	struct byte_bpb710 *b710;
405 	uint8_t SecPerClust;
406 	u_long clusters;
407 	int ronly, error;
408 	struct g_consumer *cp;
409 	struct bufobj *bo;
410 
411 	bp = NULL;		/* This and pmp both used in error_exit. */
412 	pmp = NULL;
413 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
414 
415 	dev = devvp->v_rdev;
416 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
417 	    (uintptr_t)mp) == 0) {
418 		VOP_UNLOCK(devvp);
419 		return (EBUSY);
420 	}
421 	g_topology_lock();
422 	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
423 	g_topology_unlock();
424 	if (error != 0) {
425 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
426 		VOP_UNLOCK(devvp);
427 		return (error);
428 	}
429 	dev_ref(dev);
430 	bo = &devvp->v_bufobj;
431 	VOP_UNLOCK(devvp);
432 	if (dev->si_iosize_max != 0)
433 		mp->mnt_iosize_max = dev->si_iosize_max;
434 	if (mp->mnt_iosize_max > MAXPHYS)
435 		mp->mnt_iosize_max = MAXPHYS;
436 
437 	/*
438 	 * Read the boot sector of the filesystem, and then check the
439 	 * boot signature.  If not a dos boot sector then error out.
440 	 *
441 	 * NOTE: 8192 is a magic size that works for ffs.
442 	 */
443 	error = bread(devvp, 0, 8192, NOCRED, &bp);
444 	if (error)
445 		goto error_exit;
446 	bp->b_flags |= B_AGE;
447 	bsp = (union bootsector *)bp->b_data;
448 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
449 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
450 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
451 
452 #ifndef MSDOSFS_NOCHECKSIG
453 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
454 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
455 		error = EINVAL;
456 		goto error_exit;
457 	}
458 #endif
459 
460 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
461 	pmp->pm_mountp = mp;
462 	pmp->pm_cp = cp;
463 	pmp->pm_bo = bo;
464 
465 	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
466 
467 	/*
468 	 * Initialize ownerships and permissions, since nothing else will
469 	 * initialize them iff we are mounting root.
470 	 */
471 	pmp->pm_uid = UID_ROOT;
472 	pmp->pm_gid = GID_WHEEL;
473 	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
474 	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
475 
476 	/*
477 	 * Compute several useful quantities from the bpb in the
478 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
479 	 * the fields that are different between dos 5 and dos 3.3.
480 	 */
481 	SecPerClust = b50->bpbSecPerClust;
482 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
483 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
484 		error = EINVAL;
485 		goto error_exit;
486 	}
487 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
488 	pmp->pm_FATs = b50->bpbFATs;
489 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
490 	pmp->pm_Sectors = getushort(b50->bpbSectors);
491 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
492 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
493 	pmp->pm_Heads = getushort(b50->bpbHeads);
494 	pmp->pm_Media = b50->bpbMedia;
495 
496 	/* calculate the ratio of sector size to DEV_BSIZE */
497 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
498 
499 	/*
500 	 * We don't check pm_Heads nor pm_SecPerTrack, because
501 	 * these may not be set for EFI file systems. We don't
502 	 * use these anyway, so we're unaffected if they are
503 	 * invalid.
504 	 */
505 	if (!pmp->pm_BytesPerSec || !SecPerClust) {
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 
518 	if (pmp->pm_RootDirEnts == 0) {
519 		if (pmp->pm_FATsecs
520 		    || getushort(b710->bpbFSVers)) {
521 			error = EINVAL;
522 #ifdef MSDOSFS_DEBUG
523 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
524 #endif
525 			goto error_exit;
526 		}
527 		pmp->pm_fatmask = FAT32_MASK;
528 		pmp->pm_fatmult = 4;
529 		pmp->pm_fatdiv = 1;
530 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
531 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
532 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
533 		else
534 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
535 	} else
536 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
537 
538 	/*
539 	 * Check a few values (could do some more):
540 	 * - logical sector size: power of 2, >= block size
541 	 * - sectors per cluster: power of 2, >= 1
542 	 * - number of sectors:   >= 1, <= size of partition
543 	 * - number of FAT sectors: >= 1
544 	 */
545 	if ( (SecPerClust == 0)
546 	  || (SecPerClust & (SecPerClust - 1))
547 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
548 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
549 	  || (pmp->pm_HugeSectors == 0)
550 	  || (pmp->pm_FATsecs == 0)
551 	  || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
552 	) {
553 		error = EINVAL;
554 		goto error_exit;
555 	}
556 
557 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
558 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
559 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
560 	SecPerClust         *= pmp->pm_BlkPerSec;
561 
562 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
563 
564 	if (FAT32(pmp)) {
565 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
566 		pmp->pm_firstcluster = pmp->pm_fatblk
567 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
568 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
569 	} else {
570 		pmp->pm_rootdirblk = pmp->pm_fatblk +
571 			(pmp->pm_FATs * pmp->pm_FATsecs);
572 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
573 			sizeof(struct direntry), DEV_BSIZE); /* in blocks */
574 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
575 	}
576 
577 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
578 	    SecPerClust + 1;
579 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;	/* XXX not used? */
580 
581 	if (pmp->pm_fatmask == 0) {
582 		if (pmp->pm_maxcluster
583 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
584 			/*
585 			 * This will usually be a floppy disk. This size makes
586 			 * sure that one FAT entry will not be split across
587 			 * multiple blocks.
588 			 */
589 			pmp->pm_fatmask = FAT12_MASK;
590 			pmp->pm_fatmult = 3;
591 			pmp->pm_fatdiv = 2;
592 		} else {
593 			pmp->pm_fatmask = FAT16_MASK;
594 			pmp->pm_fatmult = 2;
595 			pmp->pm_fatdiv = 1;
596 		}
597 	}
598 
599 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
600 	if (pmp->pm_maxcluster >= clusters) {
601 #ifdef MSDOSFS_DEBUG
602 		printf("Warning: number of clusters (%ld) exceeds FAT "
603 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
604 #endif
605 		pmp->pm_maxcluster = clusters - 1;
606 	}
607 
608 	if (FAT12(pmp))
609 		pmp->pm_fatblocksize = 3 * 512;
610 	else
611 		pmp->pm_fatblocksize = PAGE_SIZE;
612 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
613 	    pmp->pm_BytesPerSec);
614 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
615 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
616 
617 	/*
618 	 * Compute mask and shift value for isolating cluster relative byte
619 	 * offsets and cluster numbers from a file offset.
620 	 */
621 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
622 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
623 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
624 
625 	/*
626 	 * Check for valid cluster size
627 	 * must be a power of 2
628 	 */
629 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
630 		error = EINVAL;
631 		goto error_exit;
632 	}
633 
634 	/*
635 	 * Release the bootsector buffer.
636 	 */
637 	brelse(bp);
638 	bp = NULL;
639 
640 	/*
641 	 * Check the fsinfo sector if we have one.  Silently fix up our
642 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
643 	 * or too large.  Ignore fp->fsinfree for now, since we need to
644 	 * read the entire FAT anyway to fill the inuse map.
645 	 */
646 	if (pmp->pm_fsinfo) {
647 		struct fsinfo *fp;
648 
649 		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
650 		    NOCRED, &bp)) != 0)
651 			goto error_exit;
652 		fp = (struct fsinfo *)bp->b_data;
653 		if (!bcmp(fp->fsisig1, "RRaA", 4)
654 		    && !bcmp(fp->fsisig2, "rrAa", 4)
655 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
656 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
657 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
658 				pmp->pm_nxtfree = CLUST_FIRST;
659 		} else
660 			pmp->pm_fsinfo = 0;
661 		brelse(bp);
662 		bp = NULL;
663 	}
664 
665 	/*
666 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
667 	 * sectors aren't properly reserved in the FAT).  This completes
668 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
669 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
670 	 * internally even if there is no fsinfo.
671 	 */
672 	if (pmp->pm_nxtfree < CLUST_FIRST)
673 		pmp->pm_nxtfree = CLUST_FIRST;
674 
675 	/*
676 	 * Allocate memory for the bitmap of allocated clusters, and then
677 	 * fill it in.
678 	 */
679 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
680 				  * sizeof(*pmp->pm_inusemap),
681 				  M_MSDOSFSFAT, M_WAITOK);
682 
683 	/*
684 	 * fillinusemap() needs pm_devvp.
685 	 */
686 	pmp->pm_devvp = devvp;
687 	pmp->pm_dev = dev;
688 
689 	/*
690 	 * Have the inuse map filled in.
691 	 */
692 	MSDOSFS_LOCK_MP(pmp);
693 	error = fillinusemap(pmp);
694 	MSDOSFS_UNLOCK_MP(pmp);
695 	if (error != 0)
696 		goto error_exit;
697 
698 	/*
699 	 * If they want FAT updates to be synchronous then let them suffer
700 	 * the performance degradation in exchange for the on disk copy of
701 	 * the FAT being correct just about all the time.  I suppose this
702 	 * would be a good thing to turn on if the kernel is still flakey.
703 	 */
704 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
705 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
706 
707 	/*
708 	 * Finish up.
709 	 */
710 	if (ronly)
711 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
712 	else {
713 		if ((error = markvoldirty(pmp, 1)) != 0)
714 			goto error_exit;
715 		pmp->pm_fmod = 1;
716 	}
717 	mp->mnt_data =  pmp;
718 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
719 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
720 	MNT_ILOCK(mp);
721 	mp->mnt_flag |= MNT_LOCAL;
722 	mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
723 	MNT_IUNLOCK(mp);
724 
725 	return (0);
726 
727 error_exit:
728 	if (bp)
729 		brelse(bp);
730 	if (cp != NULL) {
731 		g_topology_lock();
732 		g_vfs_close(cp);
733 		g_topology_unlock();
734 	}
735 	if (pmp) {
736 		lockdestroy(&pmp->pm_fatlock);
737 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
738 		free(pmp, M_MSDOSFSMNT);
739 		mp->mnt_data = NULL;
740 	}
741 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
742 	dev_rel(dev);
743 	return (error);
744 }
745 
746 /*
747  * Unmount the filesystem described by mp.
748  */
749 static int
750 msdosfs_unmount(struct mount *mp, int mntflags)
751 {
752 	struct msdosfsmount *pmp;
753 	int error, flags;
754 
755 	error = flags = 0;
756 	pmp = VFSTOMSDOSFS(mp);
757 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
758 		error = msdosfs_sync(mp, MNT_WAIT);
759 	if ((mntflags & MNT_FORCE) != 0)
760 		flags |= FORCECLOSE;
761 	else if (error != 0)
762 		return (error);
763 	error = vflush(mp, 0, flags, curthread);
764 	if (error != 0 && error != ENXIO)
765 		return (error);
766 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
767 		error = markvoldirty(pmp, 0);
768 		if (error && error != ENXIO) {
769 			(void)markvoldirty(pmp, 1);
770 			return (error);
771 		}
772 	}
773 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
774 		if (pmp->pm_w2u)
775 			msdosfs_iconv->close(pmp->pm_w2u);
776 		if (pmp->pm_u2w)
777 			msdosfs_iconv->close(pmp->pm_u2w);
778 		if (pmp->pm_d2u)
779 			msdosfs_iconv->close(pmp->pm_d2u);
780 		if (pmp->pm_u2d)
781 			msdosfs_iconv->close(pmp->pm_u2d);
782 	}
783 
784 #ifdef MSDOSFS_DEBUG
785 	{
786 		struct vnode *vp = pmp->pm_devvp;
787 		struct bufobj *bo;
788 
789 		bo = &vp->v_bufobj;
790 		BO_LOCK(bo);
791 		VI_LOCK(vp);
792 		vn_printf(vp,
793 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
794 		printf("freef %p, freeb %p, mount %p\n",
795 		    TAILQ_NEXT(vp, v_vnodelist), vp->v_vnodelist.tqe_prev,
796 		    vp->v_mount);
797 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
798 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
799 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
800 		    vp->v_bufobj.bo_numoutput, vp->v_type);
801 		VI_UNLOCK(vp);
802 		BO_UNLOCK(bo);
803 	}
804 #endif
805 	g_topology_lock();
806 	g_vfs_close(pmp->pm_cp);
807 	g_topology_unlock();
808 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
809 	vrele(pmp->pm_devvp);
810 	dev_rel(pmp->pm_dev);
811 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
812 	lockdestroy(&pmp->pm_fatlock);
813 	free(pmp, M_MSDOSFSMNT);
814 	mp->mnt_data = NULL;
815 	MNT_ILOCK(mp);
816 	mp->mnt_flag &= ~MNT_LOCAL;
817 	MNT_IUNLOCK(mp);
818 	return (error);
819 }
820 
821 static int
822 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
823 {
824 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
825 	struct denode *ndep;
826 	int error;
827 
828 #ifdef MSDOSFS_DEBUG
829 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
830 #endif
831 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
832 	if (error)
833 		return (error);
834 	*vpp = DETOV(ndep);
835 	return (0);
836 }
837 
838 static int
839 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
840 {
841 	struct msdosfsmount *pmp;
842 
843 	pmp = VFSTOMSDOSFS(mp);
844 	sbp->f_bsize = pmp->pm_bpcluster;
845 	sbp->f_iosize = pmp->pm_bpcluster;
846 	sbp->f_blocks = pmp->pm_maxcluster + 1;
847 	sbp->f_bfree = pmp->pm_freeclustercount;
848 	sbp->f_bavail = pmp->pm_freeclustercount;
849 	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
850 	sbp->f_ffree = 0;	/* what to put in here? */
851 	return (0);
852 }
853 
854 /*
855  * If we have an FSInfo block, update it.
856  */
857 static int
858 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
859 {
860 	struct fsinfo *fp;
861 	struct buf *bp;
862 	int error;
863 
864 	MSDOSFS_LOCK_MP(pmp);
865 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
866 		error = 0;
867 		goto unlock;
868 	}
869 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
870 	    NOCRED, &bp);
871 	if (error != 0) {
872 		goto unlock;
873 	}
874 	fp = (struct fsinfo *)bp->b_data;
875 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
876 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
877 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
878 	if (waitfor == MNT_WAIT)
879 		error = bwrite(bp);
880 	else
881 		bawrite(bp);
882 unlock:
883 	MSDOSFS_UNLOCK_MP(pmp);
884 	return (error);
885 }
886 
887 static int
888 msdosfs_sync(struct mount *mp, int waitfor)
889 {
890 	struct vnode *vp, *nvp;
891 	struct thread *td;
892 	struct denode *dep;
893 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
894 	int error, allerror = 0;
895 
896 	td = curthread;
897 
898 	/*
899 	 * If we ever switch to not updating all of the FATs all the time,
900 	 * this would be the place to update them from the first one.
901 	 */
902 	if (pmp->pm_fmod != 0) {
903 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
904 			panic("msdosfs_sync: rofs mod");
905 		else {
906 			/* update FATs here */
907 		}
908 	}
909 	/*
910 	 * Write back each (modified) denode.
911 	 */
912 loop:
913 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
914 		if (vp->v_type == VNON) {
915 			VI_UNLOCK(vp);
916 			continue;
917 		}
918 		dep = VTODE(vp);
919 		if ((dep->de_flag &
920 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
921 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
922 		    waitfor == MNT_LAZY)) {
923 			VI_UNLOCK(vp);
924 			continue;
925 		}
926 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
927 		if (error) {
928 			if (error == ENOENT) {
929 				MNT_VNODE_FOREACH_ALL_ABORT(mp, nvp);
930 				goto loop;
931 			}
932 			continue;
933 		}
934 		error = VOP_FSYNC(vp, waitfor, td);
935 		if (error)
936 			allerror = error;
937 		VOP_UNLOCK(vp);
938 		vrele(vp);
939 	}
940 
941 	/*
942 	 * Flush filesystem control info.
943 	 */
944 	if (waitfor != MNT_LAZY) {
945 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
946 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
947 		if (error)
948 			allerror = error;
949 		VOP_UNLOCK(pmp->pm_devvp);
950 	}
951 
952 	error = msdosfs_fsiflush(pmp, waitfor);
953 	if (error != 0)
954 		allerror = error;
955 	return (allerror);
956 }
957 
958 static int
959 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
960 {
961 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
962 	struct defid *defhp = (struct defid *) fhp;
963 	struct denode *dep;
964 	int error;
965 
966 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
967 	if (error) {
968 		*vpp = NULLVP;
969 		return (error);
970 	}
971 	*vpp = DETOV(dep);
972 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
973 	return (0);
974 }
975 
976 static struct vfsops msdosfs_vfsops = {
977 	.vfs_fhtovp =		msdosfs_fhtovp,
978 	.vfs_mount =		msdosfs_mount,
979 	.vfs_cmount =		msdosfs_cmount,
980 	.vfs_root =		msdosfs_root,
981 	.vfs_statfs =		msdosfs_statfs,
982 	.vfs_sync =		msdosfs_sync,
983 	.vfs_unmount =		msdosfs_unmount,
984 };
985 
986 VFS_SET(msdosfs_vfsops, msdosfs, 0);
987 MODULE_VERSION(msdosfs, 1);
988