xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision dd4f32ae62426a10a84b4322756d82c06c202c4e)
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/bufobj.h>
57 #include <sys/conf.h>
58 #include <sys/fcntl.h>
59 #include <sys/iconv.h>
60 #include <sys/kernel.h>
61 #include <sys/lock.h>
62 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/mutex.h>
65 #include <sys/namei.h>
66 #include <sys/priv.h>
67 #include <sys/proc.h>
68 #include <sys/rwlock.h>
69 #include <sys/stat.h>
70 #include <sys/taskqueue.h>
71 #include <sys/vnode.h>
72 
73 #include <geom/geom.h>
74 #include <geom/geom_vfs.h>
75 
76 #include <fs/msdosfs/bootsect.h>
77 #include <fs/msdosfs/bpb.h>
78 #include <fs/msdosfs/direntry.h>
79 #include <fs/msdosfs/denode.h>
80 #include <fs/msdosfs/fat.h>
81 #include <fs/msdosfs/msdosfsmount.h>
82 
83 #ifdef MSDOSFS_DEBUG
84 #include <sys/rwlock.h>
85 #endif
86 
87 static const char msdosfs_lock_msg[] = "fatlk";
88 
89 /* Mount options that we support. */
90 static const char *msdosfs_opts[] = {
91 	"async", "noatime", "noclusterr", "noclusterw",
92 	"export", "force", "from", "sync",
93 	"cs_dos", "cs_local", "cs_win", "dirmask",
94 	"gid", "kiconv", "longname",
95 	"longnames", "mask", "shortname", "shortnames",
96 	"uid", "win95", "nowin95",
97 	NULL
98 };
99 
100 #if 1 /*def PC98*/
101 /*
102  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
103  *       garbage or a random value :-{
104  *       If you want to use that broken-signatured media, define the
105  *       following symbol even though PC/AT.
106  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
107  */
108 #define	MSDOSFS_NOCHECKSIG
109 #endif
110 
111 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
112 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
113 
114 struct iconv_functions *msdosfs_iconv;
115 
116 static int	update_mp(struct mount *mp, struct thread *td);
117 static int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
118 static void	msdosfs_remount_ro(void *arg, int pending);
119 static vfs_fhtovp_t	msdosfs_fhtovp;
120 static vfs_mount_t	msdosfs_mount;
121 static vfs_root_t	msdosfs_root;
122 static vfs_statfs_t	msdosfs_statfs;
123 static vfs_sync_t	msdosfs_sync;
124 static vfs_unmount_t	msdosfs_unmount;
125 
126 /* Maximum length of a character set name (arbitrary). */
127 #define	MAXCSLEN	64
128 
129 static int
130 update_mp(struct mount *mp, struct thread *td)
131 {
132 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
133 	void *dos, *win, *local;
134 	int error, v;
135 
136 	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
137 		if (msdosfs_iconv != NULL) {
138 			error = vfs_getopt(mp->mnt_optnew,
139 			    "cs_win", &win, NULL);
140 			if (!error)
141 				error = vfs_getopt(mp->mnt_optnew,
142 				    "cs_local", &local, NULL);
143 			if (!error)
144 				error = vfs_getopt(mp->mnt_optnew,
145 				    "cs_dos", &dos, NULL);
146 			if (!error) {
147 				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
148 				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
149 				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
150 				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
151 			}
152 			if (error != 0)
153 				return (error);
154 		} else {
155 			pmp->pm_w2u = NULL;
156 			pmp->pm_u2w = NULL;
157 			pmp->pm_d2u = NULL;
158 			pmp->pm_u2d = NULL;
159 		}
160 	}
161 
162 	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
163 		pmp->pm_gid = v;
164 	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
165 		pmp->pm_uid = v;
166 	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
167 		pmp->pm_mask = v & ALLPERMS;
168 	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
169 		pmp->pm_dirmask = v & ALLPERMS;
170 	vfs_flagopt(mp->mnt_optnew, "shortname",
171 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
172 	vfs_flagopt(mp->mnt_optnew, "shortnames",
173 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
174 	vfs_flagopt(mp->mnt_optnew, "longname",
175 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
176 	vfs_flagopt(mp->mnt_optnew, "longnames",
177 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
178 	vfs_flagopt(mp->mnt_optnew, "kiconv",
179 	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
180 
181 	if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
182 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
183 	else
184 		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
185 
186 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
187 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
188 	else
189 		pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
190 	return 0;
191 }
192 
193 static int
194 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
195 {
196 	struct msdosfs_args args;
197 	int error;
198 
199 	if (data == NULL)
200 		return (EINVAL);
201 	error = copyin(data, &args, sizeof args);
202 	if (error)
203 		return (error);
204 
205 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
206 	ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
207 	ma = mount_argf(ma, "uid", "%d", args.uid);
208 	ma = mount_argf(ma, "gid", "%d", args.gid);
209 	ma = mount_argf(ma, "mask", "%d", args.mask);
210 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
211 
212 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
213 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
214 	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
215 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
216 
217 	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
218 	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
219 	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
220 
221 	error = kernel_mount(ma, flags);
222 
223 	return (error);
224 }
225 
226 /*
227  * mp - path - addr in user space of mount point (ie /usr or whatever)
228  * data - addr in user space of mount params including the name of the block
229  * special file to treat as a filesystem.
230  */
231 static int
232 msdosfs_mount(struct mount *mp)
233 {
234 	struct vnode *devvp, *odevvp;	  /* vnode for blk device to mount */
235 	struct thread *td;
236 	/* msdosfs specific mount control block */
237 	struct msdosfsmount *pmp = NULL;
238 	struct nameidata ndp;
239 	int error, flags;
240 	accmode_t accmode;
241 	char *from;
242 
243 	td = curthread;
244 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
245 		return (EINVAL);
246 
247 	/*
248 	 * If updating, check whether changing from read-only to
249 	 * read/write; if there is no device name, that's all we do.
250 	 */
251 	if (mp->mnt_flag & MNT_UPDATE) {
252 		pmp = VFSTOMSDOSFS(mp);
253 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
254 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
255 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
256 				return (error);
257 			error = vfs_write_suspend_umnt(mp);
258 			if (error != 0)
259 				return (error);
260 
261 			flags = WRITECLOSE;
262 			if (mp->mnt_flag & MNT_FORCE)
263 				flags |= FORCECLOSE;
264 			error = vflush(mp, 0, flags, td);
265 			if (error != 0) {
266 				vfs_write_resume(mp, 0);
267 				return (error);
268 			}
269 
270 			/*
271 			 * Now the volume is clean.  Mark it so while the
272 			 * device is still rw.
273 			 */
274 			error = markvoldirty(pmp, 0);
275 			if (error != 0) {
276 				vfs_write_resume(mp, 0);
277 				(void)markvoldirty(pmp, 1);
278 				return (error);
279 			}
280 
281 			/* Downgrade the device from rw to ro. */
282 			g_topology_lock();
283 			error = g_access(pmp->pm_cp, 0, -1, 0);
284 			g_topology_unlock();
285 			if (error) {
286 				vfs_write_resume(mp, 0);
287 				(void)markvoldirty(pmp, 1);
288 				return (error);
289 			}
290 
291 			/*
292 			 * Backing out after an error was painful in the
293 			 * above.  Now we are committed to succeeding.
294 			 */
295 			pmp->pm_fmod = 0;
296 			pmp->pm_flags |= MSDOSFSMNT_RONLY;
297 			MNT_ILOCK(mp);
298 			mp->mnt_flag |= MNT_RDONLY;
299 			MNT_IUNLOCK(mp);
300 			vfs_write_resume(mp, 0);
301 		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
302 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
303 			/*
304 			 * If upgrade to read-write by non-root, then verify
305 			 * that user has necessary permissions on the device.
306 			 */
307 			odevvp = pmp->pm_odevvp;
308 			vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
309 			error = VOP_ACCESS(odevvp, VREAD | VWRITE,
310 			    td->td_ucred, td);
311 			if (error)
312 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
313 			if (error) {
314 				VOP_UNLOCK(odevvp);
315 				return (error);
316 			}
317 			VOP_UNLOCK(odevvp);
318 			g_topology_lock();
319 			error = g_access(pmp->pm_cp, 0, 1, 0);
320 			g_topology_unlock();
321 			if (error)
322 				return (error);
323 
324 			/* Now that the volume is modifiable, mark it dirty. */
325 			error = markvoldirty_upgrade(pmp, true, true);
326 			if (error) {
327 				/*
328 				 * If dirtying the superblock failed, drop GEOM
329 				 * 'w' refs (we're still RO).
330 				 */
331 				g_topology_lock();
332 				(void)g_access(pmp->pm_cp, 0, -1, 0);
333 				g_topology_unlock();
334 
335 				return (error);
336 			}
337 
338 			pmp->pm_fmod = 1;
339 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
340 			MNT_ILOCK(mp);
341 			mp->mnt_flag &= ~MNT_RDONLY;
342 			MNT_IUNLOCK(mp);
343 		}
344 
345 		/*
346 		 * Avoid namei() below.  The "from" option is not set.
347 		 * Update of the devvp is pointless for this case.
348 		 */
349 		if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
350 			return (0);
351 	}
352 	/*
353 	 * Not an update, or updating the name: look up the name
354 	 * and verify that it refers to a sensible disk device.
355 	 */
356 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
357 		return (EINVAL);
358 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from);
359 	error = namei(&ndp);
360 	if (error)
361 		return (error);
362 	devvp = ndp.ni_vp;
363 	NDFREE_PNBUF(&ndp);
364 
365 	if (!vn_isdisk_error(devvp, &error)) {
366 		vput(devvp);
367 		return (error);
368 	}
369 	/*
370 	 * If mount by non-root, then verify that user has necessary
371 	 * permissions on the device.
372 	 */
373 	accmode = VREAD;
374 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
375 		accmode |= VWRITE;
376 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
377 	if (error)
378 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
379 	if (error) {
380 		vput(devvp);
381 		return (error);
382 	}
383 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
384 		error = mountmsdosfs(devvp, mp);
385 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
386 		pmp = VFSTOMSDOSFS(mp);
387 #endif
388 	} else {
389 		vput(devvp);
390 		if (devvp != pmp->pm_odevvp)
391 			return (EINVAL);	/* XXX needs translation */
392 	}
393 	if (error) {
394 		vrele(devvp);
395 		return (error);
396 	}
397 
398 	error = update_mp(mp, td);
399 	if (error) {
400 		if ((mp->mnt_flag & MNT_UPDATE) == 0)
401 			msdosfs_unmount(mp, MNT_FORCE);
402 		return error;
403 	}
404 
405 	vfs_mountedfrom(mp, from);
406 #ifdef MSDOSFS_DEBUG
407 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
408 #endif
409 	return (0);
410 }
411 
412 /*
413  * The FAT12 and FAT16 file systems use a limited size root directory that
414  * can be created with 1 to 65535 entries for files, directories, or a disk
415  * label (but DOS or Windows creates at most 512 root directory entries).
416  * This function calculates the number of free root directory entries by
417  * counting the non-deleted entries (not starting with 0xE5) and by adding
418  * the amount of never used entries (with the position indicated by an
419  * entry that starts with 0x00).
420  */
421 static int
422 rootdir_free(struct msdosfsmount* pmp)
423 {
424 	struct buf *bp;
425 	struct direntry *dep;
426 	u_long readsize;
427 	int dirclu;
428 	int diridx;
429 	int dirmax;
430 	int dirleft;
431 	int ffree;
432 
433 	dirclu = pmp->pm_rootdirblk;
434 
435 	/*
436 	 * The msdosfs code ignores pm_RootDirEnts and uses pm_rootdirsize
437 	 * (measured in DEV_BSIZE) to prevent excess root dir allocations.
438 	 */
439 	dirleft = howmany(pmp->pm_rootdirsize * DEV_BSIZE,
440 			  sizeof(struct direntry));
441 
442 	/* Read in chunks of default maximum root directory size */
443 	readsize = 512 * sizeof(struct direntry);
444 
445 #ifdef MSDOSFS_DEBUG
446 	printf("rootdir_free: blkpersec=%lu fatblksize=%lu dirsize=%lu "
447 	    "firstclu=%lu dirclu=%d entries=%d rootdirsize=%lu "
448 	    "bytespersector=%hu bytepercluster=%lu\n",
449 	    pmp->pm_BlkPerSec, pmp->pm_fatblocksize, readsize,
450 	    pmp->pm_firstcluster, dirclu, dirleft, pmp->pm_rootdirsize,
451 	    pmp->pm_BytesPerSec, pmp->pm_bpcluster);
452 #endif
453 	ffree = dirleft;
454 	while (dirleft > 0 && ffree > 0) {
455 		if (readsize > dirleft * sizeof(struct direntry))
456 			readsize = dirleft * sizeof(struct direntry);
457 #ifdef MSDOSFS_DEBUG
458 		printf("rootdir_free: dirclu=%d dirleft=%d readsize=%lu\n",
459 		       dirclu, dirleft, readsize);
460 #endif
461 		if (bread(pmp->pm_devvp, dirclu, readsize, NOCRED, &bp) != 0) {
462 			printf("rootdir_free: read error\n");
463 			if (bp != NULL)
464 				brelse(bp);
465 			return (-1);
466 		}
467 		dirmax = readsize / sizeof(struct direntry);
468 		for (diridx = 0; diridx < dirmax && dirleft > 0;
469 		     diridx++, dirleft--) {
470 			dep = (struct direntry*)bp->b_data + diridx;
471 #ifdef MSDOSFS_DEBUG
472 			if (dep->deName[0] == SLOT_DELETED)
473 				printf("rootdir_free: idx=%d <deleted>\n",
474 				    diridx);
475 			else if (dep->deName[0] == SLOT_EMPTY)
476 				printf("rootdir_free: idx=%d <end marker>\n",
477 				    diridx);
478 			else if (dep->deAttributes == ATTR_WIN95)
479 				printf("rootdir_free: idx=%d <LFN part %d>\n",
480 				    diridx, (dep->deName[0] & 0x1f) + 1);
481 			else if (dep->deAttributes & ATTR_VOLUME)
482 				printf("rootdir_free: idx=%d label='%11.11s'\n",
483 				    diridx, dep->deName);
484 			else if (dep->deAttributes & ATTR_DIRECTORY)
485 				printf("rootdir_free: idx=%d dir='%11.11s'\n",
486 				    diridx, dep->deName);
487 			else
488 				printf("rootdir_free: idx=%d file='%11.11s'\n",
489 				    diridx, dep->deName);
490 #endif
491 			if (dep->deName[0] == SLOT_EMPTY)
492 				dirleft = 0;
493 			else if (dep->deName[0] != SLOT_DELETED)
494 				ffree--;
495 		}
496 		brelse(bp);
497 		bp = NULL;
498 		dirclu += readsize / DEV_BSIZE;
499 	}
500 	return (ffree);
501 }
502 
503 static int
504 mountmsdosfs(struct vnode *odevvp, struct mount *mp)
505 {
506 	struct msdosfsmount *pmp;
507 	struct buf *bp;
508 	struct cdev *dev;
509 	struct vnode *devvp;
510 	union bootsector *bsp;
511 	struct byte_bpb33 *b33;
512 	struct byte_bpb50 *b50;
513 	struct byte_bpb710 *b710;
514 	uint8_t SecPerClust;
515 	u_long clusters;
516 	int ronly, error;
517 	struct g_consumer *cp;
518 	struct bufobj *bo;
519 
520 	bp = NULL;		/* This and pmp both used in error_exit. */
521 	pmp = NULL;
522 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
523 
524 	devvp = mntfs_allocvp(mp, odevvp);
525 	dev = devvp->v_rdev;
526 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
527 	    (uintptr_t)mp) == 0) {
528 		mntfs_freevp(devvp);
529 		return (EBUSY);
530 	}
531 	g_topology_lock();
532 	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
533 	g_topology_unlock();
534 	if (error != 0) {
535 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
536 		mntfs_freevp(devvp);
537 		return (error);
538 	}
539 	dev_ref(dev);
540 	bo = &devvp->v_bufobj;
541 	BO_LOCK(&odevvp->v_bufobj);
542 	odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
543 	BO_UNLOCK(&odevvp->v_bufobj);
544 	VOP_UNLOCK(devvp);
545 	if (dev->si_iosize_max != 0)
546 		mp->mnt_iosize_max = dev->si_iosize_max;
547 	if (mp->mnt_iosize_max > maxphys)
548 		mp->mnt_iosize_max = maxphys;
549 
550 	/*
551 	 * Read the boot sector of the filesystem, and then check the
552 	 * boot signature.  If not a dos boot sector then error out.
553 	 *
554 	 * NOTE: 8192 is a magic size that works for ffs.
555 	 */
556 	error = bread(devvp, 0, 8192, NOCRED, &bp);
557 	if (error)
558 		goto error_exit;
559 	bp->b_flags |= B_AGE;
560 	bsp = (union bootsector *)bp->b_data;
561 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
562 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
563 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
564 
565 #ifndef MSDOSFS_NOCHECKSIG
566 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
567 	    bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
568 		error = EINVAL;
569 		goto error_exit;
570 	}
571 #endif
572 
573 	pmp = malloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO);
574 	pmp->pm_mountp = mp;
575 	pmp->pm_cp = cp;
576 	pmp->pm_bo = bo;
577 
578 	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
579 	lockinit(&pmp->pm_checkpath_lock, 0, "msdoscp", 0, 0);
580 
581 	TASK_INIT(&pmp->pm_rw2ro_task, 0, msdosfs_remount_ro, pmp);
582 
583 	/*
584 	 * Initialize ownerships and permissions, since nothing else will
585 	 * initialize them iff we are mounting root.
586 	 */
587 	pmp->pm_uid = UID_ROOT;
588 	pmp->pm_gid = GID_WHEEL;
589 	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
590 	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
591 
592 	/*
593 	 * Compute several useful quantities from the bpb in the
594 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
595 	 * the fields that are different between dos 5 and dos 3.3.
596 	 */
597 	SecPerClust = b50->bpbSecPerClust;
598 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
599 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
600 		error = EINVAL;
601 		goto error_exit;
602 	}
603 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
604 	pmp->pm_FATs = b50->bpbFATs;
605 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
606 	pmp->pm_Sectors = getushort(b50->bpbSectors);
607 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
608 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
609 	pmp->pm_Heads = getushort(b50->bpbHeads);
610 	pmp->pm_Media = b50->bpbMedia;
611 
612 	/* calculate the ratio of sector size to DEV_BSIZE */
613 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
614 
615 	/*
616 	 * We don't check pm_Heads nor pm_SecPerTrack, because
617 	 * these may not be set for EFI file systems. We don't
618 	 * use these anyway, so we're unaffected if they are
619 	 * invalid.
620 	 */
621 	if (pmp->pm_BytesPerSec == 0 || SecPerClust == 0) {
622 		error = EINVAL;
623 		goto error_exit;
624 	}
625 
626 	if (pmp->pm_Sectors == 0) {
627 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
628 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
629 	} else {
630 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
631 		pmp->pm_HugeSectors = pmp->pm_Sectors;
632 	}
633 
634 	if (pmp->pm_RootDirEnts == 0) {
635 		if (pmp->pm_FATsecs != 0 || getushort(b710->bpbFSVers) != 0) {
636 			error = EINVAL;
637 #ifdef MSDOSFS_DEBUG
638 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
639 #endif
640 			goto error_exit;
641 		}
642 		pmp->pm_fatmask = FAT32_MASK;
643 		pmp->pm_fatmult = 4;
644 		pmp->pm_fatdiv = 1;
645 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
646 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) != 0)
647 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
648 		else
649 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
650 	} else
651 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
652 
653 	/*
654 	 * Check a few values (could do some more):
655 	 * - logical sector size: power of 2, >= block size
656 	 * - sectors per cluster: power of 2, >= 1
657 	 * - number of sectors:   >= 1, <= size of partition
658 	 * - number of FAT sectors: >= 1
659 	 */
660 	if (SecPerClust == 0 || (SecPerClust & (SecPerClust - 1)) != 0 ||
661 	    pmp->pm_BytesPerSec < DEV_BSIZE ||
662 	    (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) != 0 ||
663 	    pmp->pm_HugeSectors == 0 || pmp->pm_FATsecs == 0 ||
664 	    SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE) {
665 		error = EINVAL;
666 		goto error_exit;
667 	}
668 
669 	if ((off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec <
670 	    pmp->pm_HugeSectors /* overflow */ ||
671 	    (off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec >
672 	    cp->provider->mediasize /* past end of vol */) {
673 		error = EINVAL;
674 		goto error_exit;
675 	}
676 
677 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
678 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
679 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
680 	SecPerClust         *= pmp->pm_BlkPerSec;
681 
682 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
683 
684 	if (FAT32(pmp)) {
685 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
686 		pmp->pm_firstcluster = pmp->pm_fatblk +
687 		    pmp->pm_FATs * pmp->pm_FATsecs;
688 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
689 	} else {
690 		pmp->pm_rootdirblk = pmp->pm_fatblk +
691 		    pmp->pm_FATs * pmp->pm_FATsecs;
692 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
693 		    sizeof(struct direntry), DEV_BSIZE); /* in blocks */
694 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
695 	}
696 
697 	if (pmp->pm_HugeSectors <= pmp->pm_firstcluster) {
698 		error = EINVAL;
699 		goto error_exit;
700 	}
701 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
702 	    SecPerClust + 1;
703 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;
704 
705 	if (pmp->pm_fatmask == 0) {
706 		/*
707 		 * The last 10 (or 16?) clusters are reserved and must not
708 		 * be allocated for data.
709 		 */
710 		if (pmp->pm_maxcluster < (CLUST_RSRVD & FAT12_MASK)) {
711 			/*
712 			 * This will usually be a floppy disk. This size makes
713 			 * sure that one FAT entry will not be split across
714 			 * multiple blocks.
715 			 */
716 			pmp->pm_fatmask = FAT12_MASK;
717 			pmp->pm_fatmult = 3;
718 			pmp->pm_fatdiv = 2;
719 		} else {
720 			pmp->pm_fatmask = FAT16_MASK;
721 			pmp->pm_fatmult = 2;
722 			pmp->pm_fatdiv = 1;
723 		}
724 	}
725 
726 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv ;
727 	if (pmp->pm_maxcluster >= clusters) {
728 #ifdef MSDOSFS_DEBUG
729 		printf("Warning: number of clusters (%ld) exceeds FAT "
730 		    "capacity (%ld)\n", pmp->pm_maxcluster - 1, clusters);
731 #endif
732 		pmp->pm_maxcluster = clusters - 1;
733 	}
734 
735 	if (FAT12(pmp))
736 		pmp->pm_fatblocksize = 3 * 512;
737 	else
738 		pmp->pm_fatblocksize = PAGE_SIZE;
739 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
740 	    pmp->pm_BytesPerSec);
741 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
742 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
743 
744 	/*
745 	 * Compute mask and shift value for isolating cluster relative byte
746 	 * offsets and cluster numbers from a file offset.
747 	 */
748 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
749 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
750 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
751 
752 	/*
753 	 * Check for valid cluster size
754 	 * must be a power of 2
755 	 */
756 	if ((pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) != 0) {
757 		error = EINVAL;
758 		goto error_exit;
759 	}
760 
761 	/*
762 	 * Release the bootsector buffer.
763 	 */
764 	brelse(bp);
765 	bp = NULL;
766 
767 	/*
768 	 * Check the fsinfo sector if we have one.  Silently fix up our
769 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
770 	 * or too large.  Ignore fp->fsinfree for now, since we need to
771 	 * read the entire FAT anyway to fill the inuse map.
772 	 */
773 	if (pmp->pm_fsinfo) {
774 		struct fsinfo *fp;
775 
776 		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
777 		    NOCRED, &bp)) != 0)
778 			goto error_exit;
779 		fp = (struct fsinfo *)bp->b_data;
780 		if (!bcmp(fp->fsisig1, "RRaA", 4) &&
781 		    !bcmp(fp->fsisig2, "rrAa", 4) &&
782 		    !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
783 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
784 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
785 				pmp->pm_nxtfree = CLUST_FIRST;
786 		} else
787 			pmp->pm_fsinfo = 0;
788 		brelse(bp);
789 		bp = NULL;
790 	}
791 
792 	/*
793 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
794 	 * sectors aren't properly reserved in the FAT).  This completes
795 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
796 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
797 	 * internally even if there is no fsinfo.
798 	 */
799 	if (pmp->pm_nxtfree < CLUST_FIRST)
800 		pmp->pm_nxtfree = CLUST_FIRST;
801 
802 	/*
803 	 * Allocate memory for the bitmap of allocated clusters, and then
804 	 * fill it in.
805 	 */
806 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1,
807 	    N_INUSEBITS) * sizeof(*pmp->pm_inusemap), M_MSDOSFSFAT, M_WAITOK);
808 
809 	/*
810 	 * fillinusemap() needs pm_devvp.
811 	 */
812 	pmp->pm_devvp = devvp;
813 	pmp->pm_odevvp = odevvp;
814 	pmp->pm_dev = dev;
815 
816 	/*
817 	 * Have the inuse map filled in.
818 	 */
819 	MSDOSFS_LOCK_MP(pmp);
820 	error = fillinusemap(pmp);
821 	MSDOSFS_UNLOCK_MP(pmp);
822 	if (error != 0)
823 		goto error_exit;
824 
825 	/*
826 	 * If they want FAT updates to be synchronous then let them suffer
827 	 * the performance degradation in exchange for the on disk copy of
828 	 * the FAT being correct just about all the time.  I suppose this
829 	 * would be a good thing to turn on if the kernel is still flakey.
830 	 */
831 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
832 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
833 
834 	/*
835 	 * Finish up.
836 	 */
837 	if (ronly)
838 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
839 	else {
840 		if ((error = markvoldirty(pmp, 1)) != 0)
841 			goto error_exit;
842 		pmp->pm_fmod = 1;
843 	}
844 
845 	if (FAT32(pmp)) {
846 		pmp->pm_rootdirfree = 0;
847 	} else {
848 		pmp->pm_rootdirfree = rootdir_free(pmp);
849 		if (pmp->pm_rootdirfree < 0)
850 			goto error_exit;
851 	}
852 
853 	mp->mnt_data =  pmp;
854 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
855 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
856 	MNT_ILOCK(mp);
857 	mp->mnt_flag |= MNT_LOCAL;
858 	mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
859 	MNT_IUNLOCK(mp);
860 
861 	return (0);
862 
863 error_exit:
864 	if (bp != NULL)
865 		brelse(bp);
866 	if (cp != NULL) {
867 		g_topology_lock();
868 		g_vfs_close(cp);
869 		g_topology_unlock();
870 	}
871 	if (pmp != NULL) {
872 		lockdestroy(&pmp->pm_fatlock);
873 		lockdestroy(&pmp->pm_checkpath_lock);
874 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
875 		free(pmp, M_MSDOSFSMNT);
876 		mp->mnt_data = NULL;
877 	}
878 	BO_LOCK(&odevvp->v_bufobj);
879 	odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
880 	BO_UNLOCK(&odevvp->v_bufobj);
881 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
882 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
883 	mntfs_freevp(devvp);
884 	dev_rel(dev);
885 	return (error);
886 }
887 
888 /*
889  * Unmount the filesystem described by mp.
890  */
891 static int
892 msdosfs_unmount(struct mount *mp, int mntflags)
893 {
894 	struct msdosfsmount *pmp;
895 	int error, flags;
896 	bool susp;
897 
898 	error = flags = 0;
899 	pmp = VFSTOMSDOSFS(mp);
900 	susp = (pmp->pm_flags & MSDOSFSMNT_RONLY) == 0;
901 
902 	if (susp) {
903 		error = vfs_write_suspend_umnt(mp);
904 		if (error != 0)
905 			return (error);
906 	}
907 
908 	if ((mntflags & MNT_FORCE) != 0)
909 		flags |= FORCECLOSE;
910 	error = vflush(mp, 0, flags, curthread);
911 	if (error != 0 && error != ENXIO) {
912 		if (susp)
913 			vfs_write_resume(mp, VR_START_WRITE);
914 		return (error);
915 	}
916 	if (susp) {
917 		error = markvoldirty(pmp, 0);
918 		if (error != 0 && error != ENXIO) {
919 			if (susp)
920 				vfs_write_resume(mp, VR_START_WRITE);
921 			(void)markvoldirty(pmp, 1);
922 			return (error);
923 		}
924 	}
925 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
926 		if (pmp->pm_w2u)
927 			msdosfs_iconv->close(pmp->pm_w2u);
928 		if (pmp->pm_u2w)
929 			msdosfs_iconv->close(pmp->pm_u2w);
930 		if (pmp->pm_d2u)
931 			msdosfs_iconv->close(pmp->pm_d2u);
932 		if (pmp->pm_u2d)
933 			msdosfs_iconv->close(pmp->pm_u2d);
934 	}
935 
936 #ifdef MSDOSFS_DEBUG
937 	{
938 		struct vnode *vp = pmp->pm_devvp;
939 		struct bufobj *bo;
940 
941 		bo = &vp->v_bufobj;
942 		BO_LOCK(bo);
943 		VI_LOCK(vp);
944 		vn_printf(vp,
945 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
946 		printf("freef %p, freeb %p, mount %p\n",
947 		    TAILQ_NEXT(vp, v_vnodelist), vp->v_vnodelist.tqe_prev,
948 		    vp->v_mount);
949 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
950 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
951 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
952 		    vp->v_bufobj.bo_numoutput, vp->v_type);
953 		VI_UNLOCK(vp);
954 		BO_UNLOCK(bo);
955 	}
956 #endif
957 	if (susp)
958 		vfs_write_resume(mp, VR_START_WRITE);
959 
960 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
961 	g_topology_lock();
962 	g_vfs_close(pmp->pm_cp);
963 	g_topology_unlock();
964 	BO_LOCK(&pmp->pm_odevvp->v_bufobj);
965 	pmp->pm_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
966 	BO_UNLOCK(&pmp->pm_odevvp->v_bufobj);
967 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
968 	mntfs_freevp(pmp->pm_devvp);
969 	vrele(pmp->pm_odevvp);
970 	dev_rel(pmp->pm_dev);
971 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
972 	lockdestroy(&pmp->pm_fatlock);
973 	lockdestroy(&pmp->pm_checkpath_lock);
974 	free(pmp, M_MSDOSFSMNT);
975 	mp->mnt_data = NULL;
976 	return (error);
977 }
978 
979 static void
980 msdosfs_remount_ro(void *arg, int pending)
981 {
982 	struct msdosfsmount *pmp;
983 	int error;
984 
985 	pmp = arg;
986 
987 	MSDOSFS_LOCK_MP(pmp);
988 	if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0) {
989 		while ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
990 			msleep(&pmp->pm_flags, &pmp->pm_fatlock, PVFS,
991 			    "msdoserrro", hz);
992 	} else if ((pmp->pm_mountp->mnt_flag & MNT_RDONLY) == 0) {
993 		pmp->pm_flags |= MSDOSFS_ERR_RO;
994 		MSDOSFS_UNLOCK_MP(pmp);
995 		printf("%s: remounting read-only due to corruption\n",
996 		    pmp->pm_mountp->mnt_stat.f_mntfromname);
997 		error = vfs_remount_ro(pmp->pm_mountp);
998 		if (error != 0)
999 			printf("%s: remounting read-only failed: error %d\n",
1000 			    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
1001 		else
1002 			printf("remounted %s read-only\n",
1003 			    pmp->pm_mountp->mnt_stat.f_mntfromname);
1004 		MSDOSFS_LOCK_MP(pmp);
1005 		pmp->pm_flags &= ~MSDOSFS_ERR_RO;
1006 		wakeup(&pmp->pm_flags);
1007 	}
1008 	MSDOSFS_UNLOCK_MP(pmp);
1009 
1010 	vfs_unbusy(pmp->pm_mountp);
1011 }
1012 
1013 void
1014 msdosfs_integrity_error(struct msdosfsmount *pmp)
1015 {
1016 	int error;
1017 
1018 	error = vfs_busy(pmp->pm_mountp, MBF_NOWAIT);
1019 	if (error == 0)
1020 		taskqueue_enqueue(taskqueue_thread, &pmp->pm_rw2ro_task);
1021 	else
1022 		printf("%s: integrity error busying failed, error %d\n",
1023 		    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
1024 }
1025 
1026 static int
1027 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
1028 {
1029 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1030 	struct denode *ndep;
1031 	int error;
1032 
1033 #ifdef MSDOSFS_DEBUG
1034 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
1035 #endif
1036 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, LK_EXCLUSIVE, &ndep);
1037 	if (error)
1038 		return (error);
1039 	*vpp = DETOV(ndep);
1040 	return (0);
1041 }
1042 
1043 static int
1044 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
1045 {
1046 	struct msdosfsmount *pmp;
1047 
1048 	pmp = VFSTOMSDOSFS(mp);
1049 	sbp->f_bsize = pmp->pm_bpcluster;
1050 	sbp->f_iosize = pmp->pm_bpcluster;
1051 	sbp->f_blocks = pmp->pm_maxcluster - CLUST_FIRST + 1;
1052 	sbp->f_bfree = pmp->pm_freeclustercount;
1053 	sbp->f_bavail = pmp->pm_freeclustercount;
1054 	sbp->f_files =	howmany(pmp->pm_rootdirsize * DEV_BSIZE,
1055 				sizeof(struct direntry));
1056 	sbp->f_ffree = pmp->pm_rootdirfree;
1057 	return (0);
1058 }
1059 
1060 /*
1061  * If we have an FSInfo block, update it.
1062  */
1063 static int
1064 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
1065 {
1066 	struct fsinfo *fp;
1067 	struct buf *bp;
1068 	int error;
1069 
1070 	MSDOSFS_LOCK_MP(pmp);
1071 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
1072 		error = 0;
1073 		goto unlock;
1074 	}
1075 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
1076 	    NOCRED, &bp);
1077 	if (error != 0) {
1078 		goto unlock;
1079 	}
1080 	fp = (struct fsinfo *)bp->b_data;
1081 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
1082 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
1083 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
1084 	if (waitfor == MNT_WAIT)
1085 		error = bwrite(bp);
1086 	else
1087 		bawrite(bp);
1088 unlock:
1089 	MSDOSFS_UNLOCK_MP(pmp);
1090 	return (error);
1091 }
1092 
1093 static int
1094 msdosfs_sync(struct mount *mp, int waitfor)
1095 {
1096 	struct vnode *vp, *nvp;
1097 	struct thread *td;
1098 	struct denode *dep;
1099 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1100 	int error, allerror = 0;
1101 
1102 	td = curthread;
1103 
1104 	/*
1105 	 * If we ever switch to not updating all of the FATs all the time,
1106 	 * this would be the place to update them from the first one.
1107 	 */
1108 	if (pmp->pm_fmod != 0) {
1109 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1110 			panic("msdosfs_sync: rofs mod");
1111 		else {
1112 			/* update FATs here */
1113 		}
1114 	}
1115 	/*
1116 	 * Write back each (modified) denode.
1117 	 */
1118 loop:
1119 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
1120 		if (vp->v_type == VNON) {
1121 			VI_UNLOCK(vp);
1122 			continue;
1123 		}
1124 		dep = VTODE(vp);
1125 		if ((dep->de_flag &
1126 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
1127 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1128 		    waitfor == MNT_LAZY)) {
1129 			VI_UNLOCK(vp);
1130 			continue;
1131 		}
1132 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1133 		if (error) {
1134 			if (error == ENOENT) {
1135 				MNT_VNODE_FOREACH_ALL_ABORT(mp, nvp);
1136 				goto loop;
1137 			}
1138 			continue;
1139 		}
1140 		error = VOP_FSYNC(vp, waitfor, td);
1141 		if (error)
1142 			allerror = error;
1143 		vput(vp);
1144 	}
1145 
1146 	/*
1147 	 * Flush filesystem control info.
1148 	 */
1149 	if (waitfor != MNT_LAZY) {
1150 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
1151 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
1152 		if (error)
1153 			allerror = error;
1154 		VOP_UNLOCK(pmp->pm_devvp);
1155 	}
1156 
1157 	error = msdosfs_fsiflush(pmp, waitfor);
1158 	if (error != 0)
1159 		allerror = error;
1160 
1161 	if (allerror == 0 && waitfor == MNT_SUSPEND) {
1162 		MNT_ILOCK(mp);
1163 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1164 		MNT_IUNLOCK(mp);
1165 	}
1166 	return (allerror);
1167 }
1168 
1169 static int
1170 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1171 {
1172 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1173 	struct defid *defhp = (struct defid *) fhp;
1174 	struct denode *dep;
1175 	int error;
1176 
1177 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs,
1178 	    LK_EXCLUSIVE, &dep);
1179 	if (error) {
1180 		*vpp = NULLVP;
1181 		return (error);
1182 	}
1183 	*vpp = DETOV(dep);
1184 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1185 	return (0);
1186 }
1187 
1188 static struct vfsops msdosfs_vfsops = {
1189 	.vfs_fhtovp =		msdosfs_fhtovp,
1190 	.vfs_mount =		msdosfs_mount,
1191 	.vfs_cmount =		msdosfs_cmount,
1192 	.vfs_root =		msdosfs_root,
1193 	.vfs_statfs =		msdosfs_statfs,
1194 	.vfs_sync =		msdosfs_sync,
1195 	.vfs_unmount =		msdosfs_unmount,
1196 };
1197 
1198 VFS_SET(msdosfs_vfsops, msdosfs, 0);
1199 MODULE_VERSION(msdosfs, 1);
1200